Skip to content

Commit e7d6774

Browse files
garrytansyong
andcommitted
fix(sync-gbrain): don't offer a call-graph build for a source with no code pages (#3029)
Step 3.5 offered --dream for any source with pages, including one that holds only markdown (synced with gbrain's default strategy), where a build cannot help, and the injected CLAUDE.md guidance told future sessions to run --dream for any count: 0. On gbrain >= 0.60, code-def for an impossible symbol answers .status (probed live against gbrain 0.60.96: ready for a code source, out_of_scope for a markdown-only one). Step 3.5 now probes the pinned source first, reports the cause instead of asking on out_of_scope, and keeps today's offer when the probe is unknown (older gbrain, an error). gstack's own sync uses --strategy code, so the PR's soft-delete claim does not apply to gstack's path. Co-authored-by: syong <[email protected]>
1 parent 7777c26 commit e7d6774

5 files changed

Lines changed: 133 additions & 22 deletions

File tree

‎sync-gbrain/SKILL.md‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,15 @@ that doesn't declare it (e.g. `gbrain-base` / `gbrain-base-v2`), a `dream` cycle
651651
completes but `resolve_symbol_edges` matches nothing — the graph stays empty no
652652
matter how many times you run it. So "build the call graph" is only meaningful on
653653
a code-aware pack. The `--dream` stage detects this and reports it honestly
654-
(a WARN row) rather than claiming a build that didn't happen. gbrain exposes pack
655-
capability only at cycle runtime (no pre-flight query as of 0.41.x), so we can't
656-
detect it before running. `code-def` / `code-refs` need the same symbol
657-
extraction; they are NOT free "direct lookups" on a non-code-aware pack.
654+
(a WARN row) rather than claiming a build that didn't happen. `code-def` /
655+
`code-refs` need the same symbol extraction; they are NOT free "direct lookups"
656+
on a non-code-aware pack.
657+
658+
A source can also hold no code pages at all while reporting a healthy page count
659+
(a source added outside gstack syncs with gbrain's default markdown strategy).
660+
On gbrain 0.60 or later, `code-def` for a symbol that cannot exist answers
661+
`.status`: `ready` (the source holds code), `out_of_scope` (it holds none, so
662+
`--dream` cannot help), or nothing usable on older gbrain or an error.
658663

659664
Detect whether this source's call graph is built via doctor's `cycle_freshness`
660665
check, matching the cwd `SOURCE_ID` literally. `doctor --fast` skips the DB
@@ -683,10 +688,23 @@ fi
683688
# readCycleStatus(). A fail/warn that doesn't name this source → "unknown"
684689
# (don't mask other-source failures).
685690
echo "call graph for $SOURCE_ID: $CYCLE${CYCLE_WHY:+: $CYCLE_WHY}"
691+
CODE_SCOPE=unknown
692+
if [ -n "$SOURCE_ID" ]; then
693+
CODE_SCOPE=$(gbrain code-def ZzzGstackProbeSymbolThatCannotExist --source "$SOURCE_ID" --limit 1 2>/dev/null \
694+
| sed -n '/^[[:space:]]*{/,$p' | jq -r '.status // "unknown"' 2>/dev/null || echo unknown)
695+
case "$CODE_SCOPE" in ready|out_of_scope) ;; *) CODE_SCOPE=unknown ;; esac
696+
fi
697+
echo "code scope for $SOURCE_ID: $CODE_SCOPE"
686698
```
687699

688-
If `CYCLE == never` AND the user did NOT pass `--dream`/`--full` AND Step 3
689-
`PAGES > 0`, AskUserQuestion via the format in the preamble:
700+
If `CODE_SCOPE == out_of_scope`, do not offer a build: report "this source holds
701+
no code pages (it syncs with gbrain's markdown strategy), so a call-graph build
702+
cannot help; re-sync it with `gbrain sync --strategy code --source <id>` to index
703+
its code" and continue to Step 4.
704+
705+
If `CYCLE == never` AND `CODE_SCOPE` is `ready` or `unknown` AND the user did NOT
706+
pass `--dream`/`--full` AND Step 3 `PAGES > 0`, AskUserQuestion via the format in
707+
the preamble:
690708

691709
> D2 — This repo's call graph isn't built. Build it now?
692710
>
@@ -773,10 +791,14 @@ of the same repo each have their own pin and their own indexed pages, so
773791
semantic results match the code on disk here.
774792

775793
Call-graph queries (`code-callers`/`code-callees`) also need the graph to be
776-
built first — run `/sync-gbrain --dream` (or `--full`) if they return
777-
`count: 0`. This only works if this source's gbrain schema pack extracts code
778-
symbols; on a non-code-aware pack `--dream` completes but the graph stays empty
779-
and reports a WARN. `code-def`/`code-refs` need the same extraction.
794+
built first. A `count: 0` has several causes, and only one is fixed by
795+
`/sync-gbrain --dream` (or `--full`): the graph was never built. Check the others
796+
first: the source may hold no code at all (`gbrain code-def <any-symbol>
797+
--source <id>` answers `status: out_of_scope`), the symbol may be dotted (these
798+
verbs take a bare name), or `--all-sources` was used. `--dream` also needs a
799+
schema pack that extracts code symbols; on another pack it completes, the graph
800+
stays empty and it reports a WARN. `code-def`/`code-refs` need the same
801+
extraction.
780802

781803
Two indexed corpora available via the `gbrain` CLI:
782804
- This worktree's code (auto-pinned via `.gbrain-source`).

‎sync-gbrain/SKILL.md.tmpl‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,15 @@ that doesn't declare it (e.g. `gbrain-base` / `gbrain-base-v2`), a `dream` cycle
327327
completes but `resolve_symbol_edges` matches nothing — the graph stays empty no
328328
matter how many times you run it. So "build the call graph" is only meaningful on
329329
a code-aware pack. The `--dream` stage detects this and reports it honestly
330-
(a WARN row) rather than claiming a build that didn't happen. gbrain exposes pack
331-
capability only at cycle runtime (no pre-flight query as of 0.41.x), so we can't
332-
detect it before running. `code-def` / `code-refs` need the same symbol
333-
extraction; they are NOT free "direct lookups" on a non-code-aware pack.
330+
(a WARN row) rather than claiming a build that didn't happen. `code-def` /
331+
`code-refs` need the same symbol extraction; they are NOT free "direct lookups"
332+
on a non-code-aware pack.
333+
334+
A source can also hold no code pages at all while reporting a healthy page count
335+
(a source added outside gstack syncs with gbrain's default markdown strategy).
336+
On gbrain 0.60 or later, `code-def` for a symbol that cannot exist answers
337+
`.status`: `ready` (the source holds code), `out_of_scope` (it holds none, so
338+
`--dream` cannot help), or nothing usable on older gbrain or an error.
334339

335340
Detect whether this source's call graph is built via doctor's `cycle_freshness`
336341
check, matching the cwd `SOURCE_ID` literally. `doctor --fast` skips the DB
@@ -359,10 +364,23 @@ fi
359364
# readCycleStatus(). A fail/warn that doesn't name this source → "unknown"
360365
# (don't mask other-source failures).
361366
echo "call graph for $SOURCE_ID: $CYCLE${CYCLE_WHY:+: $CYCLE_WHY}"
367+
CODE_SCOPE=unknown
368+
if [ -n "$SOURCE_ID" ]; then
369+
CODE_SCOPE=$(gbrain code-def ZzzGstackProbeSymbolThatCannotExist --source "$SOURCE_ID" --limit 1 2>/dev/null \
370+
| sed -n '/^[[:space:]]*{/,$p' | jq -r '.status // "unknown"' 2>/dev/null || echo unknown)
371+
case "$CODE_SCOPE" in ready|out_of_scope) ;; *) CODE_SCOPE=unknown ;; esac
372+
fi
373+
echo "code scope for $SOURCE_ID: $CODE_SCOPE"
362374
```
363375

364-
If `CYCLE == never` AND the user did NOT pass `--dream`/`--full` AND Step 3
365-
`PAGES > 0`, AskUserQuestion via the format in the preamble:
376+
If `CODE_SCOPE == out_of_scope`, do not offer a build: report "this source holds
377+
no code pages (it syncs with gbrain's markdown strategy), so a call-graph build
378+
cannot help; re-sync it with `gbrain sync --strategy code --source <id>` to index
379+
its code" and continue to Step 4.
380+
381+
If `CYCLE == never` AND `CODE_SCOPE` is `ready` or `unknown` AND the user did NOT
382+
pass `--dream`/`--full` AND Step 3 `PAGES > 0`, AskUserQuestion via the format in
383+
the preamble:
366384

367385
> D2 — This repo's call graph isn't built. Build it now?
368386
>
@@ -449,10 +467,14 @@ of the same repo each have their own pin and their own indexed pages, so
449467
semantic results match the code on disk here.
450468
451469
Call-graph queries (`code-callers`/`code-callees`) also need the graph to be
452-
built first — run `/sync-gbrain --dream` (or `--full`) if they return
453-
`count: 0`. This only works if this source's gbrain schema pack extracts code
454-
symbols; on a non-code-aware pack `--dream` completes but the graph stays empty
455-
and reports a WARN. `code-def`/`code-refs` need the same extraction.
470+
built first. A `count: 0` has several causes, and only one is fixed by
471+
`/sync-gbrain --dream` (or `--full`): the graph was never built. Check the others
472+
first: the source may hold no code at all (`gbrain code-def <any-symbol>
473+
--source <id>` answers `status: out_of_scope`), the symbol may be dotted (these
474+
verbs take a bare name), or `--all-sources` was used. `--dream` also needs a
475+
schema pack that extracts code symbols; on another pack it completes, the graph
476+
stays empty and it reports a WARN. `code-def`/`code-refs` need the same
477+
extraction.
456478
457479
Two indexed corpora available via the `gbrain` CLI:
458480
- This worktree's code (auto-pinned via `.gbrain-source`).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* /sync-gbrain Step 3.5 offered a `--dream` call-graph build for any source
3+
* with pages, including one that holds no code pages (synced with gbrain's
4+
* default markdown strategy), where a build cannot help. On gbrain >= 0.60,
5+
* `code-def` for an impossible symbol answers `.status` (`ready` /
6+
* `out_of_scope`); probed live against gbrain 0.60.96. Runs the template's own
7+
* block against a stub gbrain; older gbrain or a probe error keeps the old
8+
* behavior (`unknown`).
9+
*/
10+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
11+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
12+
import { tmpdir } from "os";
13+
import { join } from "path";
14+
import { spawnSync } from "child_process";
15+
16+
const TEMPLATE = join(import.meta.dir, "..", "sync-gbrain", "SKILL.md.tmpl");
17+
const SRC = "gstack-code-app-1a2b3c4d";
18+
const block = () => {
19+
const blocks = [...readFileSync(TEMPLATE, "utf-8").matchAll(/```bash\n([\s\S]*?)```/g)].map((m) => m[1]!);
20+
return blocks.find((b) => b.includes('echo "code scope for $SOURCE_ID'))!;
21+
};
22+
23+
describe("sync-gbrain Step 3.5 code-scope probe", () => {
24+
let home = "";
25+
beforeEach(() => {
26+
home = mkdtempSync(join(tmpdir(), "gstack-code-scope-"));
27+
const capDir = join(home, ".claude", "skills", "gstack", "bin");
28+
mkdirSync(capDir, { recursive: true });
29+
writeFileSync(join(capDir, "gstack-gbrain-read-capability.ts"), `console.log(JSON.stringify({ status: "source", source_id: ${JSON.stringify(SRC)} }));\n`);
30+
mkdirSync(join(home, "bin"), { recursive: true });
31+
});
32+
afterEach(() => rmSync(home, { recursive: true, force: true }));
33+
34+
function scope(codeDef: string, exit = 0): string {
35+
writeFileSync(join(home, "code-def.out"), codeDef);
36+
writeFileSync(join(home, "bin", "gbrain"), `#!/bin/sh
37+
case "$1" in
38+
doctor) echo '{"checks":[]}' ;;
39+
code-def) [ "$4" = "${SRC}" ] || exit 9; cat "${join(home, "code-def.out")}"; exit ${exit} ;;
40+
*) exit 1 ;;
41+
esac
42+
`, { mode: 0o755 });
43+
const bunDir = process.execPath.replace(/\/[^/]+$/, "");
44+
const r = spawnSync("bash", ["-c", block()], { env: { HOME: home, PATH: `${join(home, "bin")}:${bunDir}:/usr/bin:/bin` }, encoding: "utf-8", timeout: 30_000 });
45+
return (r.stdout || "").trim().split("\n").find((l) => l.startsWith("code scope for"))!;
46+
}
47+
48+
it("a source holding code reports ready (log lines before the JSON are skipped)", () => {
49+
expect(scope(`[code-def] resolving\n{\n "status": "ready",\n "count": 0\n}\n`)).toBe(`code scope for ${SRC}: ready`);
50+
});
51+
it("a markdown-only source reports out_of_scope", () => {
52+
expect(scope(`{"status":"out_of_scope","count":0}\n`)).toBe(`code scope for ${SRC}: out_of_scope`);
53+
});
54+
it("gbrain below 0.60 (no status field) stays unknown, so the offer is unchanged", () => {
55+
expect(scope(`{"count":0,"results":[]}\n`)).toBe(`code scope for ${SRC}: unknown`);
56+
});
57+
it("a probe error stays unknown", () => {
58+
expect(scope("error: no such source\n", 1)).toBe(`code scope for ${SRC}: unknown`);
59+
});
60+
it("the offer is gated on the probe: out_of_scope reports the cause instead of asking", () => {
61+
const tmpl = readFileSync(TEMPLATE, "utf-8");
62+
expect(tmpl).toContain("If `CODE_SCOPE == out_of_scope`, do not offer a build");
63+
expect(tmpl).toContain("If `CYCLE == never` AND `CODE_SCOPE` is `ready` or `unknown`");
64+
});
65+
});

‎test/sync-gbrain-cycle-freshness.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ esac
4848
encoding: "utf-8",
4949
timeout: 30_000,
5050
});
51-
return (r.stdout || "").trim();
51+
// The block also prints the code-scope probe line (B11); these cases pin the call-graph line.
52+
return (r.stdout || "").trim().split("\n")[0]!;
5253
}
5354

5455
it("reports never when this source never completed a cycle (doctor exits 1 on the failing check)", () => {

‎test/sync-gbrain-source-probe.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ test('actual generated Step 3/3.5 shell reads a pretty state and probes the pinn
6060
expect(cycle.status, cycle.stderr).toBe(0);
6161
expect(pages.stdout).toContain('cwd source: client-fixture, page_count: 7');
6262
expect(cycle.stdout).toContain('call graph for client-fixture: never');
63-
expect(f.calls()).toEqual(['sources list --json', 'sources list --json', 'doctor --json --scope=brain']);
63+
expect(f.calls()).toEqual(['sources list --json', 'sources list --json', 'doctor --json --scope=brain',
64+
'code-def ZzzGstackProbeSymbolThatCannotExist --source client-fixture --limit 1']);
6465
} finally { f.cleanup(); }
6566
});
6667

0 commit comments

Comments
 (0)