From 6bdfe43dbaeca4cf57bde96f5a01ffe9ee4a18fe Mon Sep 17 00:00:00 2001 From: ming1523 Date: Mon, 2 Nov 2026 17:07:00 +0800 Subject: [PATCH] fix(cli): preserve keep-marker stats prefixes (#3800) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(cli): preserve keep-marker stats prefix * fix: preserve literal dollar signs in stats names * chore: refresh bot re-verification on current head azizur100389's Aug 20 review could not verify the current head ('current visible state is incomplete'). Pushing an empty commit so the GitNexus PR Risk Taxonomy / Reference Set Readiness / Hosted Promotion Readiness audits re-run against the latest tree. No source change intended. * fix: address reviewer feedback for #2950 --------- Co-authored-by: luyua9 Co-authored-by: zuyua9 <948423378@qq.com> Co-authored-by: Gergő Magyar Co-authored-by: haimingZZ --- gitnexus/src/cli/ai-context.ts | 14 +++++++------ gitnexus/test/unit/ai-context.test.ts | 29 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/gitnexus/src/cli/ai-context.ts b/gitnexus/src/cli/ai-context.ts index 7440829b5..a30ea3eff 100644 --- a/gitnexus/src/cli/ai-context.ts +++ b/gitnexus/src/cli/ai-context.ts @@ -407,9 +407,6 @@ async function upsertGitNexusSection( // drop the parenthetical but still refresh the project name so renames // propagate. const newStatsInner = `${stats.nodes || 0} symbols, ${stats.edges || 0} relationships, ${stats.processes || 0} execution flows`; - const statsLine = noStats - ? `Indexed as **${projectName}**` - : `Indexed as **${projectName}** (${newStatsInner})`; // Match either canonical phrasing at line start (`^` with `m` flag) so we // cannot replace prose embedded mid-paragraph. Deliberately no `$`: text @@ -417,10 +414,15 @@ async function upsertGitNexusSection( // The parenthetical is optional so a count-free line left by a prior // --no-stats run still matches — letting the name refresh, and letting // counts return if --no-stats is later dropped. - const statsPattern = /^(?:Indexed as|indexed by GitNexus as) \*\*[^*]+\*\*(?: \([^)]+\))?/m; + const statsPattern = /^(Indexed as|indexed by GitNexus as) \*\*[^*]+\*\*(?: \([^)]+\))?/m; + const statsMatch = existingSection.match(statsPattern); - if (statsPattern.test(existingSection)) { - const updatedSection = existingSection.replace(statsPattern, statsLine); + if (statsMatch) { + const statsPrefix = statsMatch[1]; + const statsLine = noStats + ? `${statsPrefix} **${projectName}**` + : `${statsPrefix} **${projectName}** (${newStatsInner})`; + const updatedSection = existingSection.replace(statsPattern, () => statsLine); // Count-only delta — leave the committed lean block alone (#2907). A // project rename, or --no-stats dropping the parenthetical, still writes. if (stripVolatileCounts(updatedSection) === stripVolatileCounts(existingSection)) { diff --git a/gitnexus/test/unit/ai-context.test.ts b/gitnexus/test/unit/ai-context.test.ts index 6030e9eca..dffe76a2a 100644 --- a/gitnexus/test/unit/ai-context.test.ts +++ b/gitnexus/test/unit/ai-context.test.ts @@ -2117,4 +2117,33 @@ Indexed as **${name}**${counts}. Lean block. await fs.rm(dir, { recursive: true, force: true }); } }); + + it('preserves the existing stats-line prefix when updating a keep marker', async () => { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'gn-keep-prefix-')); + const storage = path.join(dir, '.gitnexus'); + try { + await fs.mkdir(storage, { recursive: true }); + const original = `# Guide + + + +indexed by GitNexus as **OldName** (10 symbols, 20 relationships, 3 execution flows). Custom. + +`; + await fs.writeFile(path.join(dir, 'CLAUDE.md'), original, 'utf-8'); + + const result = await generateAIContextFiles(dir, storage, 'Repo$1Name', { + nodes: 55, + edges: 66, + processes: 7, + }); + + expect(result.files).toContain('CLAUDE.md (updated)'); + expect(await fs.readFile(path.join(dir, 'CLAUDE.md'), 'utf-8')).toContain( + 'indexed by GitNexus as **Repo$1Name** (55 symbols, 66 relationships, 7 execution flows). Custom.', + ); + } finally { + await fs.rm(dir, { recursive: true, force: true }); + } + }); });