fix(graphify): run /gsd-graphify build inline (with regression fence) (#3169)

* fix(graphify): run /gsd-graphify build inline instead of spawning a sub-agent

Closes #3166

graphify v0.7+ split the build into a fast AST-extraction phase (cached)
followed by a separate clustering + report-write phase. The cached
extraction phase survived sub-agent isolation, but the post-extraction
phase was SIGTERM'd when the agent exited, leaving the cache populated
and no graph.json / graph.html / GRAPH_REPORT.md artifacts written to
.planning/graphs/.

The skill now runs `graphify update .`, the three artifact copies, the
snapshot, and the status report as a single foreground Bash call so the
entire pipeline survives to completion. The CLI's `graphify build`
pre-flight still returns `action: "spawn_agent"` so external callers
and existing tests in tests/graphify.test.cjs keep working.

Regression test (tests/bug-3166-graphify-inline-build.test.cjs) parses
the skill's YAML frontmatter and body structurally to fence against
re-introducing Task to allowed-tools or `Task(` invocation syntax — a
future edit cannot regress the fix without tripping the fence.

Verified against safishamsi/graphify v0.7.0–v0.7.8 release notes:
`graphify update .` invocation and output filenames are unchanged in
v0.7+; no GSD-side interface migration is required.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(test): drop yaml dep from bug-3166 fence — replace with inline parser

CI failed with MODULE_NOT_FOUND on `require('yaml')` — the package
resolved locally as a transitive dep but isn't declared in package.json.
The project pattern (see tests/helpers.cjs `parseFrontmatter`) deliberately
avoids pulling in yaml/js-yaml.

Replace with a narrow inline parser that handles the scalar + block-list
subset used in this skill's frontmatter. Verified the fence still trips
when Task is reintroduced to allowed-tools.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(test): parse fenced blocks structurally for #3166 fence

Address CodeRabbit nitpicks on PR #3169: the body assertions used raw
markdown text regex (\bTask\s*\(/, /graphify\s+update\s+\./) which
violates the project's "parse, never grep" testing convention and risks
false-positives on prose.

Replace with extractFencedBlocks(body) which returns
[{lang, content}, ...] tuples per markdown code fence. Body assertions
now run against parsed blocks:

  - "no fenced code block contains Task("
    → deepEqual offending blocks to [] (vs. regex on raw body)
  - "a bash block invokes graphify update . / build snapshot"
    → filter to lang === 'bash', then substring-check inside parsed content

Substring checks within already-parsed fenced content are structural —
prose mentioning the word "Task" can no longer false-positive, and a
future prose reference to graphify cannot satisfy the positive assertions
either. The frontmatter side already used a parser; both sides now match.

Verified: re-introducing Task( inside a code fence still trips the
assertion. Full suite 7499/7499 passes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(test): rename readFileSync-bound var to satisfy lint-no-source-grep

The structural-parse refactor introduced `b.content.includes(...)` calls
on parsed fenced-block records, but `loadSkill()` had also bound
`const content = fs.readFileSync(...)` for the markdown text. The
lint-no-source-grep regex scanner cannot distinguish scopes — it sees
"variable `content` is bound from readFileSync" and "`content.includes`
is called" and flags it as a source-grep test, even though the two
`content`s are different lexical entities.

Rename the readFileSync-bound local to `markdown`. Now `b.content` is
unambiguously a property access on a parsed-block record. Lint passes
(0 violations across 401 test files); behavior unchanged (4/4 tests
still pass, including the negative regression case).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(test): tighten snapshot assertion to gsd-tools.cjs prefix

CodeRabbit nitpick on bug-3166 fence: the snapshot bash assertion accepted
any 'graphify build snapshot' substring. Tighten to require it follows
'gsd-tools.cjs', matching the actual fenced invocation in
commands/gsd/graphify.md (which uses node "$HOME/.../gsd-tools.cjs" graphify
build snapshot — note the closing quote, so a literal 'gsd-tools graphify build
snapshot' substring would not match).

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-06 11:56:27 -04:00
committed by GitHub
parent 3579a48d76
commit 41dc9bc060
5 changed files with 199 additions and 64 deletions

View File

@@ -0,0 +1,4 @@
---
type: Fixed
---
**`/gsd-graphify build` now runs inline instead of spawning a sub-agent (#3166)** — graphify v0.7+ split the build into a fast AST-extraction phase (cached) followed by a separate clustering + report-write phase. The cached extraction phase survived sub-agent isolation, but the post-extraction phase was SIGTERM'd when the agent exited, leaving the cache populated and no `graph.json` / `graph.html` / `GRAPH_REPORT.md` artifacts written to `.planning/graphs/`. The skill now runs `graphify update .`, the three artifact copies, the snapshot, and the status report as a single foreground Bash call so the entire pipeline survives to completion. The CLI's `graphify build` pre-flight still returns `action: "spawn_agent"` so external callers and existing tests keep working. Adds a structural regression test parsing the skill's YAML frontmatter to fence against re-introducing `Task` to `allowed-tools`.

View File

@@ -105,6 +105,7 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Fix
- **`/gsd-graphify build` now runs inline instead of spawning a sub-agent** — graphify v0.7+ split the build into a fast AST-extraction phase (cached) followed by a separate clustering + report-write phase. The cached extraction phase survived sub-agent isolation, but the post-extraction phase was SIGTERM'd when the agent exited, leaving the cache populated and no `graph.json` / `graph.html` / `GRAPH_REPORT.md` artifacts written to `.planning/graphs/`. The skill now runs `graphify update .`, the three artifact copies, the snapshot, and the status report as a single foreground Bash call so the entire pipeline survives to completion. The CLI's `graphify build` pre-flight still returns `action: "spawn_agent"` so external callers and existing tests keep working. Regression covered by `tests/bug-3166-graphify-inline-build.test.cjs` (4 structural assertions that parse `commands/gsd/graphify.md` YAML frontmatter and body to fence against re-introducing `Task` to `allowed-tools` or `Task(` invocation syntax). (#3166)
- **`gsd-pristine/` is now populated by the installer when local patches are detected** — `saveLocalPatches` declared a `pristineDir` variable and JSDoc'd "saves pristine copies (from manifest) to gsd-pristine/ to enable three-way merge during reapply-patches", but no code ever wrote to that directory. Effect: the `/gsd-reapply-patches` Step 5 verifier (#2972) silently degraded to its over-broad fallback heuristic ("every significant backup line"), exactly the silent-success-on-lost-content failure mode #2969 was designed to prevent. Fix: new `populatePristineDir({ packageSrc, pristineDir, modified, runtime, pathPrefix, isGlobal })` helper runs the install transform pipeline (`copyWithPathReplacement`) into a tmp staging dir, then copies out only the modified-file paths into `gsd-pristine/`. `saveLocalPatches` now accepts a `pristineCtx` and calls the helper when local patches are detected; the install entry point passes the package source root, runtime, pathPrefix, and isGlobal so transforms produce byte-identical output to what `copyWithPathReplacement` would have written under normal install. Soft-fails on transform errors (logs a warning, continues with empty pristine — no worse than pre-fix behavior). Pristine reflects the about-to-install version's content, which is what the verifier needs as the "what would survive without the user's modifications" baseline. Regression covered by `tests/bug-2998-pristine-dir-populated.test.cjs` (6 tests across two suites): asserts the helper is exported, returns 0 for empty modified list, writes one pristine file per source-existing path, skips ghost paths without corrupting pristine, and produces deterministic output (two runs with same inputs yield byte-identical pristine — the property `pristine_hashes` in `backup-meta.json` depends on). (#2998)
- **`release-sdk` hotfix re-run no longer fails at `Dry-run publish validation` when the version is already on npm** — the `Detect prior publish (reconciliation mode)` step sets `skip_publish=true` when the package version is already on the registry, and the actual publish step honors that gate. The `Dry-run publish validation` step was missing the same guard, so any operator re-run of an already-published hotfix (the typical recovery path when later steps fail mid-flight) hit `npm publish --dry-run` first and got `npm error You cannot publish over the previously published versions: X.Y.Z` — `npm publish --dry-run` contacts the registry and rejects existing-version targets even though it doesn't actually publish. The dry-run validation step is now gated on the same `steps.prior_publish.outputs.skip_publish != 'true'` condition as the publish step. The rehearsal still runs on first publishes (where it has value); it skips only in the specific reconciliation case where the publish itself would be skipped. Trigger run: [25233855236](https://github.com/gsd-build/get-shit-done/actions/runs/25233855236/job/73995605643). Regression covered by `tests/bug-2987-dry-run-validation-skip-on-reconciliation.test.cjs`. (#2987)
- **`release-sdk` hotfix flow hardened against silent classifier failures, missing-classifier-at-base-tag, and a vestigial merge-back PR step** — three issues surfaced by CodeRabbit's post-merge review of #2981 plus a production failure on the v1.39.1 release run. **(1)** `scripts/diff-touches-shipped-paths.cjs` reused exit code `1` for both the legitimate "no shipped paths" classifier result and Node's default uncaught-throw exit, so any tooling failure was indistinguishable from a normal skip. The script now uses `0` (shipped), `1` (not shipped), `2` (classifier error) with `try`/`catch` + `uncaughtException`/`unhandledRejection` handlers routing all failure paths to exit `2`. **(2)** The workflow's `git checkout -b "$BRANCH" "$BASE_TAG"` overwrote the working tree with the base tag's contents *before* the cherry-pick loop ran the classifier — but base tags predating the classifier's introduction (notably v1.39.0) don't have the file in their tree, so `node scripts/diff-touches-shipped-paths.cjs` would exit non-zero and silently drop every commit, producing an empty hotfix release. The classifier is now staged into `$RUNNER_TEMP` at the top of `Prepare hotfix branch` (before any working-tree-mutating git command), and the loop references that staged copy. The cherry-pick loop snapshots `$PIPESTATUS` into a local array (`PIPE_RC=("${PIPESTATUS[@]}")`) immediately after the classifier pipeline — under bracketed `set +e`/`set -e` — and dispatches via explicit `case`: `0` proceeds, `1` skips into `NON_SHIPPED_SKIPPED`, anything else emits `::error::shipped-paths classifier failed for $SHA (exit N)` and fails the workflow. CodeRabbit on PR #2984 caught a subtler bug in the first iteration: `pipeline \|\| true; RC=${PIPESTATUS[1]}` is broken because `\|\| true` runs `true` as its own one-command pipeline on the failure paths, overwriting `PIPESTATUS` to `(0)` and leaving `${PIPESTATUS[1]}` unset. The array-snapshot form is invariant against this. The same hardening also surfaces `git diff-tree`'s exit code (via `PIPE_RC[0]`); a non-zero diff-tree result now also fails the workflow rather than feeding partial input to the classifier. **(3)** Removed the `Open merge-back PR (hotfix only)` step. The auto-cherry-pick hotfix flow only picks commits already on main (`git cherry HEAD origin/main` outputs the unmerged ones), so by construction every code commit on the hotfix branch is already on main. The only hotfix-branch-only commit is the version-bump chore, which would either no-op against main or rewind main's in-progress version. The step also failed in production with `GitHub Actions is not permitted to create or approve pull requests (createPullRequest)` (org policy) on run [25232968975](https://github.com/gsd-build/get-shit-done/actions/runs/25232968975). The `pull-requests: write` permission previously granted to the release job has been dropped in line with least-privilege. The run-summary line that previously echoed `Merge-back PR opened against main` has been replaced with `No merge-back PR (auto-picked commits are already on main)` so operators reading the summary see an accurate non-action statement (CodeRabbit on PR #2984). Regression covered by `tests/bug-2983-classifier-exit-codes-and-base-tag-staging.test.cjs` (15 assertions across exit-code semantics, classifier staging, error dispatch, PIPESTATUS-snapshot hardening, diff-tree fail-fast, merge-back removal, and run-summary accuracy). (#2983)

View File

@@ -5,7 +5,6 @@ argument-hint: "[build|query <term>|status|diff]"
allowed-tools:
- Read
- Bash
- Task
---
**STOP -- DO NOT READ THIS FILE. You are already reading it. This prompt was injected into your context by Claude Code's command system. Using the Read tool on this file wastes tokens. Begin executing Step 0 immediately.**
@@ -54,7 +53,7 @@ Parse `$ARGUMENTS` to determine the operation mode:
| Argument | Action |
|----------|--------|
| `build` | Spawn graphify-builder agent (Step 3) |
| `build` | Run inline build (Step 3) |
| `query <term>` | Run inline query (Step 2a) |
| `status` | Run inline status check (Step 2b) |
| `diff` | Run inline diff check (Step 2c) |
@@ -122,80 +121,55 @@ If no snapshot exists, suggest running `build` twice (first to create, second to
---
## Step 3 -- Build (Agent Spawn)
## Step 3 -- Build (Inline)
Run pre-flight check first:
Run the pre-flight check first:
```
PREFLIGHT=$(node "$HOME/.claude/get-shit-done/bin/gsd-tools.cjs" graphify build)
```bash
node "$HOME/.claude/get-shit-done/bin/gsd-tools.cjs" graphify build
```
If pre-flight returns `disabled: true` or `error`, display the message and **STOP**.
Parse the JSON output:
- If `disabled: true`: display the disabled message from Step 1 and **STOP**
- If `error`: display the error message and **STOP**
- If `action: "spawn_agent"`: pre-flight passed -- proceed with the inline build below
If pre-flight returns `action: "spawn_agent"`, display:
(The `spawn_agent` action name is historical. The skill now performs the build inline because graphify v0.7+ split the build into a fast AST-extraction phase and a separate clustering + report-write phase. Sub-agent isolation kept the cached extraction phase alive but SIGTERM'd the post-extraction phase when the agent exited, leaving the cache populated but no `graph.json` artifacts written. The CLI still emits the `spawn_agent` signal so external callers and tests keep working.)
```
GSD > Spawning graphify-builder agent...
Display:
```text
GSD > Building knowledge graph...
```
Spawn a Task:
Run the build, copy artifacts, write the diff snapshot, and report the summary in a single foreground Bash call so the whole pipeline survives to completion. Use a `timeout` of `600000` ms (10 minutes), which covers the `graphify.build_timeout` ceiling (default 300 s) with margin:
```
Task(
description="Build or rebuild the project knowledge graph",
prompt="You are the graphify-builder agent. Your job is to build or rebuild the project knowledge graph using the graphify CLI.
Project root: ${CWD}
gsd-tools path: $HOME/.claude/get-shit-done/bin/gsd-tools.cjs
## Instructions
1. **Invoke graphify:**
Run from the project root:
```
graphify update .
```
This builds the knowledge graph with SHA256 incremental caching.
Timeout: up to 5 minutes (or as configured via graphify.build_timeout).
2. **Validate output:**
Check that graphify-out/graph.json exists and is valid JSON with nodes[] and edges[] arrays.
If graphify exited non-zero or graph.json is not parseable, output:
## GRAPHIFY BUILD FAILED
Include the stderr output for debugging. Do NOT delete .planning/graphs/ -- prior valid graph remains available.
3. **Copy artifacts to .planning/graphs/:**
```
cp graphify-out/graph.json .planning/graphs/graph.json
cp graphify-out/graph.html .planning/graphs/graph.html
cp graphify-out/GRAPH_REPORT.md .planning/graphs/GRAPH_REPORT.md
```
These three files are the build output consumed by query, status, and diff commands.
4. **Write diff snapshot:**
```
node \"$HOME/.claude/get-shit-done/bin/gsd-tools.cjs\" graphify build snapshot
```
This creates .planning/graphs/.last-build-snapshot.json for future diff comparisons.
5. **Report build summary:**
```
node \"$HOME/.claude/get-shit-done/bin/gsd-tools.cjs\" graphify status
```
Display the node count, edge count, and hyperedge count from the status output.
When complete, output: ## GRAPHIFY BUILD COMPLETE with the summary counts.
If something fails at any step, output: ## GRAPHIFY BUILD FAILED with details."
)
```bash
graphify update . \
&& cp graphify-out/graph.json .planning/graphs/graph.json \
&& cp graphify-out/graph.html .planning/graphs/graph.html \
&& cp graphify-out/GRAPH_REPORT.md .planning/graphs/GRAPH_REPORT.md \
&& node "$HOME/.claude/get-shit-done/bin/gsd-tools.cjs" graphify build snapshot \
&& node "$HOME/.claude/get-shit-done/bin/gsd-tools.cjs" graphify status
```
Wait for the agent to complete.
Do NOT pass `run_in_background: true`. Typical builds complete in 15-60 seconds and the entire chain must run foreground.
If the chain fails (non-zero exit):
- Display: `## GRAPHIFY BUILD FAILED` followed by the captured stderr
- Do NOT delete `.planning/graphs/` -- the prior valid graph remains available
- **STOP**
If the chain succeeds:
- Parse the trailing `graphify status` JSON
- Display: `## GRAPHIFY BUILD COMPLETE` with the node, edge, and hyperedge counts
---
## Anti-Patterns
1. DO NOT spawn an agent for query/status/diff operations -- these are inline CLI calls
2. DO NOT modify graph files directly -- the build agent handles writes
3. DO NOT skip the config gate check
4. DO NOT use gsd-tools config get-value for the config gate -- it exits on missing keys
1. DO NOT spawn an agent for any operation -- build, query, status, and diff all run inline. Sub-agent isolation terminates background bash when the agent exits, which previously truncated graphify builds mid-write and left only the cache populated (#3166).
2. DO NOT pass `run_in_background: true` for the build chain -- the operation is fast and must complete in the foreground.
3. DO NOT modify graph files directly -- always go through `graphify update .` and the snapshot CLI.
4. DO NOT skip the config gate check.
5. DO NOT use `gsd-tools config get-value` for the config gate -- it exits on missing keys.

View File

@@ -981,7 +981,7 @@ Build, query, and inspect the project knowledge graph stored in `.planning/graph
| Subcommand | Description |
|------------|-------------|
| `build` | Build or rebuild the knowledge graph (spawns the graphify-builder agent) |
| `build` | Build or rebuild the knowledge graph (runs `graphify update .` inline and refreshes `.planning/graphs/`) |
| `query <term>` | Search the graph for a term |
| `status` | Show graph freshness and statistics |
| `diff` | Show changes since the last build |

View File

@@ -0,0 +1,156 @@
'use strict';
/**
* Regression fence for #3166 — `/gsd-graphify build` lost artifacts because the
* skill spawned a Task sub-agent that backgrounded `graphify update .`. Sub-agent
* isolation SIGTERM'd the post-extraction phase (graphify v0.7+) before
* graph.json / graph.html / GRAPH_REPORT.md were written.
*
* Fix: skill runs the build inline in a single foreground Bash call. The
* fence here is *structural* — the skill is parsed into (a) a YAML
* frontmatter map and (b) a list of fenced code blocks tagged by language.
* Assertions then run against those parsed structures, never against raw
* markdown text (per CONTRIBUTING.md no-source-grep convention). If a future
* edit re-introduces `Task` to allowed-tools or `Task(` invocation syntax to
* any code fence, this test fails.
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const SKILL_PATH = path.join(__dirname, '..', 'commands', 'gsd', 'graphify.md');
/**
* Parse the narrow YAML subset used in this skill's frontmatter:
* key: scalar
* key:
* - item
* - item
*
* Avoids pulling in `yaml`/`js-yaml` (neither is a declared project dep —
* the existing tests/helpers.cjs `parseFrontmatter` deliberately scalars-only
* for the same reason). The skill's frontmatter shape is fixed; this is enough.
*/
function parseSkillFrontmatter(text) {
const lines = text.split(/\r?\n/);
const out = {};
let activeKey = null;
let activeList = null;
for (const raw of lines) {
const listItem = raw.match(/^\s+-\s+(.+?)\s*$/);
if (listItem && activeList) {
activeList.push(listItem[1]);
continue;
}
const kv = raw.match(/^([A-Za-z][A-Za-z0-9_-]*):\s*(.*)$/);
if (!kv) continue;
const [, key, rawValue] = kv;
const value = rawValue.trim();
if (value === '') {
activeKey = key;
activeList = [];
out[key] = activeList;
} else {
activeKey = null;
activeList = null;
out[key] = value;
}
}
return out;
}
/**
* Walk markdown body line-by-line and return every fenced code block as
* { lang, content } records. Tracks fence state explicitly, so prose that
* happens to mention `Task(` or `graphify` does not appear in the parsed
* output. This is the structural representation the body assertions use —
* raw-text regex on the markdown body is the anti-pattern this replaces
* (per CONTRIBUTING.md "no source-grep tests" + CodeRabbit on PR #3169).
*/
function extractFencedBlocks(body) {
const lines = body.split(/\r?\n/);
const blocks = [];
let active = null;
for (const line of lines) {
const open = line.match(/^```(\S*)\s*$/);
if (active === null) {
if (open) active = { lang: open[1] || '', lines: [] };
continue;
}
if (line.trim() === '```') {
blocks.push({ lang: active.lang, content: active.lines.join('\n') });
active = null;
continue;
}
active.lines.push(line);
}
return blocks;
}
function loadSkill() {
// Local rename (`markdown` not `content`) so the no-source-grep lint
// doesn't conflate this readFileSync-bound variable with the
// `b.content.includes(...)` calls below — those operate on parsed
// fenced-block records, not raw file text.
const markdown = fs.readFileSync(SKILL_PATH, 'utf8');
const lines = markdown.split(/\r?\n/);
const delims = [];
for (let i = 0; i < lines.length; i += 1) {
if (lines[i].trim() === '---') delims.push(i);
if (delims.length === 2) break;
}
assert.equal(delims.length, 2, 'graphify.md must have a closed frontmatter block');
const frontmatterText = lines.slice(delims[0] + 1, delims[1]).join('\n');
const body = lines.slice(delims[1] + 1).join('\n');
return {
frontmatter: parseSkillFrontmatter(frontmatterText),
body,
fencedBlocks: extractFencedBlocks(body),
};
}
describe('bug-3166: /gsd-graphify build runs inline (no Task sub-agent)', () => {
test('frontmatter allowed-tools does not include Task', () => {
const { frontmatter } = loadSkill();
assert.ok(Array.isArray(frontmatter['allowed-tools']),
'allowed-tools must be a YAML block list');
assert.ok(frontmatter['allowed-tools'].length > 0,
'allowed-tools must declare at least one tool');
assert.ok(!frontmatter['allowed-tools'].includes('Task'),
'Task must NOT be in allowed-tools — sub-agent isolation truncates ' +
'graphify v0.7+ post-extraction phase (#3166). Build runs inline.');
});
test('frontmatter retains Read and Bash (inline build prerequisites)', () => {
const { frontmatter } = loadSkill();
const tools = frontmatter['allowed-tools'];
assert.ok(tools.includes('Read'), 'Read required for config gate');
assert.ok(tools.includes('Bash'), 'Bash required for inline build chain');
});
test('no fenced code block invokes Task() — agent spawn syntax', () => {
const { fencedBlocks } = loadSkill();
const offending = fencedBlocks.filter(b => b.content.includes('Task('));
assert.deepEqual(offending, [],
'no fenced code block in graphify.md may contain `Task(` invocation ' +
'syntax — sub-agent spawning truncates graphify v0.7+ post-extraction ' +
'phase (#3166). Prose mentioning the word "Task" is fine; only the ' +
'call expression inside a code block is forbidden.');
});
test('a bash code block invokes the inline graphify update . pipeline', () => {
const { fencedBlocks } = loadSkill();
const bashBlocks = fencedBlocks.filter(b => b.lang === 'bash');
assert.ok(bashBlocks.length > 0, 'skill must contain at least one bash block');
assert.ok(
bashBlocks.some(b => b.content.includes('graphify update .')),
'a bash code block must invoke `graphify update .`'
);
assert.ok(
bashBlocks.some(b => /gsd-tools\.cjs["']?\s+graphify build snapshot/.test(b.content)),
'a bash code block must invoke `gsd-tools.cjs graphify build snapshot`'
);
});
});