Skip to content

Commit 9daab63

Browse files
garrytansmithjoshuaradubachAZ-1224
authored
v1.26.5.0 fix wave: gbrain ingest writer (hybrid frontmatter) + gbrain-valid source ids (garrytan#1344)
* fix: use correct `gbrain put <slug>` CLI verb in memory ingest `put_page` is the MCP tool name, not a CLI subcommand. The actual gbrain verb is `put <slug>` with content via stdin and tags in YAML frontmatter. Every transcript / memory ingest fails today on clean installs. Switch to the right verb and inject title/type/tags into the frontmatter that buildTranscriptPage / buildArtifactPage already produce. Bundled in the same function: - timeout: 30s → 60s. Auto-link reconciliation hits 30s once the brain has a few hundred pages. - maxBuffer: 1MB → 16MB. Without it Node truncates gbrain's stderr and callers see only `Command failed:` with no detail. - Surface stderr/stdout in the returned error instead of the bare exception. Verified: bun test test/gstack-memory-ingest.test.ts -> 15/15 pass. bun test on the three test files touching this path -> 362/362. * fix(sync-gbrain): generate gbrain-valid source ids for repos with dots or long names `deriveCodeSourceId` previously concatenated the canonicalized remote with only `/` and whitespace stripped, leaving dots from hostnames (`github.com`) and no length cap. gbrain rejects any source id containing characters outside [a-z0-9-] or longer than 32 chars, so `github.com/<org>/<repo>` produced `gstack-code-github.com-<org>-<repo>` (40 chars, plus dots) and registration failed: code source registration failed: Invalid source id "gstack-code-github.com-radubach-platform". Must be 1-32 lowercase alnum chars with optional interior hyphens. Fix: - Drop the host segment (`github.com` is the same for nearly every user and just consumes the 32-char budget). Use only the last two path segments (org-repo). - Sanitize any remaining non-alnum to hyphens, then collapse and trim. - For genuinely long org/repo names that still exceed the budget, keep the tail (most distinctive end of the slug) and append a 6-char sha1 hash for collision resistance. Adds a regression test that spawns the CLI in temp git repos with controlled remotes (dot in hostname, SCP-style, multi-dot host, long names forcing hash-truncation) and asserts every derived id is ≤32 chars and matches the gbrain validator regex. * fix(memory-ingest): hybrid frontmatter writer + tightened gbrain availability probe PR garrytan#1328 (merged in the prior commit) correctly injects title/type/tags into the YAML frontmatter that buildTranscriptPage already prepends. But buildArtifactPage emits raw markdown without frontmatter, so design-docs, learnings, and builder-profile-entries were landing in gbrain with empty title/type/tags. Add the no-frontmatter wrap branch so artifact pages get the same metadata the inject branch provides for transcripts. Also bring in gbrainAvailable()'s --help probe (originally proposed in PR garrytan#1341 by Alex Medina), with the regex tightened from /(^|\s)put(\s|$)/m to /^\s+put\s/m. Anchoring on the indented subcommand format gbrain's help actually uses keeps the probe from matching "put" appearing as prose in help text, while still failing fast with one clean error if a future gbrain renames or removes the put subcommand. Updates the V1.5 NOTE doc block at the top of the file to describe the current put-via-stdin shape rather than the legacy put_page flag form. Co-Authored-By: Alex Medina <oficina@puntoverdemc.com> * test+fix(memory-ingest): strengthen regression tests, fix inject for malformed-close frontmatter Imports the shim-based regression tests from PR garrytan#1341 (Alex Medina) and strengthens them to assert title, type, and tags actually arrive in put stdin — not just `agent: claude-code`. Asserting the metadata fields matches the regression class that's caused this fix wave: writers can "succeed" while metadata is silently lost. The original PR garrytan#1341 tests would have passed even with title/type/tags missing. Strengthening the test surfaced a deeper issue. buildTranscriptPage joins frontmatter array elements with "\n" and does not append a trailing newline, so the close fence is "\n---<content>" directly, not "\n---\n". PR garrytan#1328's inject branch searched for "\n---\n" and never matched — which means even with PR garrytan#1328 alone, transcript pages were landing in gbrain with no title/type/tags. Two-line fix: search for "\n---" only, since the inject lands before the close fence regardless of what follows it. Also imports PR garrytan#1341's V1.5 NOTE doc-block update and the section comment refresh so the prose stays accurate against the new writer shape. Co-Authored-By: Alex Medina <oficina@puntoverdemc.com> * fix+test(gbrain-sync): handle empty-slug edge in constrainSourceId, add no-origin and basename-empty regression tests PR garrytan#1330 (merged in the prior commit) addressed the dot-in-host and length-overflow cases for source-id derivation, but constrainSourceId silently returned "${prefix}-" when the input sanitized to an empty slug — invalid per gbrain's `^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$` validator on the trailing hyphen. Adds an explicit empty-slug branch that falls back to a sha1-prefixed id ("gstack-code-<6hex>") so the output stays gbrain-valid for every input shape. Two new regression tests cover the corners PR garrytan#1330's coverage left exposed: - no-origin fallback: a cwd repo with no `origin` remote configured must still derive a valid id from the basename. - basename-sanitizes-to-empty: a repo whose path basename is all non-alnum (e.g. "___") must produce the hash-only fallback, not an invalid trailing-hyphen id. Both run the CLI inside temp git repos for genuine end-to-end coverage (matches the pattern PR garrytan#1330 established for its own four remote-shape cases). Co-Authored-By: Richard Dubach <radubach@gmail.com> * chore: bump VERSION to 1.26.5.0 + CHANGELOG entry for fix wave PATCH bump. Three bug fixes (memory-ingest put_page CLI verb mismatch, hybrid frontmatter writer for transcripts AND artifacts, gbrain-valid source-id derivation for github-hosted repos), no new user capability. CHANGELOG release-summary leads with what users can now do (clean- install transcripts populate the brain, github-hosted repos register code sources) and tabulates before/after numbers from real gbrain v0.25.1 smoke output. Itemized changes credit @smithjoshua, @AZ-1224, and @radubach for the originating PRs plus the additional hybrid branch + strengthened tests added on top per Codex plan-review. * docs(todos): file P2 (gbrain install-pin staleness) + P3 (source-id host-collision) follow-ups Two follow-ups surfaced during the v1.26.5.0 fix-wave plan review. P2 — Issue garrytan#1305 part 2: bin/gstack-gbrain-install pins gbrain to v0.18.2 (commit 08b3698) but doesn't move when gstack ships features that depend on newer gbrain ops or schema. Fresh /setup-gbrain on v1.26.x lands users on schema 24 with v1.26 features expecting 32+. Captured for a future fix-wave. P3 — Codex P1.3 from the v1.26.5.0 plan review: deriveCodeSourceId drops the host segment to fit gbrain's 32-char source-id budget, which means github.com/acme/foo and gitlab.com/acme/foo collapse to the same source id. Real but rare; PR garrytan#1330 author explicitly considered this and chose budget over cross-host uniqueness. Captured as a long-tail concern. --------- Co-authored-by: Joshua Smith <joshualowellsmith@gmail.com> Co-authored-by: Richard Dubach <radubach@gmail.com> Co-authored-by: Alex Medina <oficina@puntoverdemc.com>
1 parent 3905bc1 commit 9daab63

7 files changed

Lines changed: 428 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
# Changelog
22

3+
## [1.26.5.0] - 2026-05-06
4+
5+
## **The v1.26 memory feature now actually works on a fresh `/setup-gbrain` install, and `/sync-gbrain --full` actually registers github-hosted code sources.**
6+
7+
Two fix-wave bugs closed in one ship. Until this version, the headline v1.26 features ended setup green but did nothing: every transcript page failed `Unknown command: put_page`, and every `github.com/<org>/<repo>` repo got rejected for an invalid source id. After upgrade, clean-install transcripts land in gbrain with title/type/tags intact, and any github-hosted repo registers a code source on the first try.
8+
9+
### The numbers that matter
10+
11+
Both numbers come from running the binaries against the real gbrain v0.25.1 install on this machine, against `origin/main` first (buggy) and the merged branch second.
12+
13+
| Surface | Before (v1.26.4.0) | After (v1.26.5.0) | Δ |
14+
|---|---|---|---|
15+
| Memory-ingest writer verb | `gbrain put_page --slug ... --title ...` (CLI rejects: `Unknown command`) | `gbrain put <slug>` with frontmatter (CLI accepts) | from 100% fail to 0% fail |
16+
| Transcript pages with title/type/tags | none — fields rode CLI flags that no gbrain version accepts | injected into existing frontmatter on every page | search/filter by `--type transcript` actually returns results now |
17+
| Source id derived for `github.com/garrytan/gstack` | `gstack-code-github.com-garrytan-gstack` (38 chars, contains `.`, fails gbrain `[a-z0-9-]{1,32}` validator) | `gstack-code-garrytan-gstack` (27 chars, valid) | 100% of github-hosted repos go from rejected to accepted |
18+
| Availability probe failure mode | every page errors with `Unknown command: put_page` | one clean error: `gbrain CLI not in PATH or missing put subcommand` | log spam goes from N copies to 1 |
19+
| Available `gbrainPutPage()` timeout | 30 s (auto-link reconciliation hits 30 s on dense brains) | 60 s | brains with hundreds of existing pages stop hitting the ceiling on every put |
20+
| `gbrainPutPage()` error surface | `Command failed:` (Node truncates 1 MB stderr) | first 300 chars of `err.stderr` | debugging stops requiring strace; the failure is visible |
21+
22+
The `gbrain put` verb has existed since v0.18.2 and was always the right CLI surface. The `put_page` shape was the MCP tool name leaking into the CLI path. The hybrid writer now handles both transcript pages (existing frontmatter from `buildTranscriptPage`, inject title/type/tags into it) and raw artifact pages (no frontmatter, wrap with new frontmatter).
23+
24+
### What this means for new users
25+
26+
Run `/setup-gbrain` on a clean install, choose any path, and Step 7.5 actually populates the brain with your transcripts plus their metadata. Run `/sync-gbrain --full` on any github-hosted repo and the code stage registers the source instead of failing the `sources add` validator. The headline v1.26 features finally do the thing they shipped to do.
27+
28+
### Itemized changes
29+
30+
#### Fixed
31+
- `bin/gstack-memory-ingest.ts:gbrainPutPage` — switched the writer from the legacy flag-based `gbrain put_page --slug X --title Y --type Z --tags T` form to the CLI surface `gbrain put <slug>` (positional slug, content via stdin, metadata in YAML frontmatter). Two-branch hybrid: when the page body already starts with frontmatter (transcript pages from `buildTranscriptPage`, which prepends agent/session_id/cwd/git_remote/etc. but no title/type/tags), inject title/type/tags into the existing block before the closing `---`. When the body has no frontmatter (raw artifact pages: design-docs, learnings, builder-profile-entries), wrap with a fresh frontmatter carrying the same fields. Either branch produces a page that gbrain's pages list, search, and tag filters actually surface. Contributed by @smithjoshua (PR #1328: base writer + 60 s timeout + 16 MB maxBuffer + stderr first-line surface) and the artifact-wrap branch added on top here.
32+
- `bin/gstack-memory-ingest.ts:gbrainAvailable` — adds a `gbrain --help` probe with a regex anchored on the indented subcommand format (`/^\s+put\s/m`). Replaces the previous `command -v` only check. If a future gbrain renames or removes `put`, the writer fails fast with one clean error per ingest pass instead of N copies of `Unknown command: put_page`. Contributed by @AZ-1224 (PR #1341: probe origin); regex tightening added on top here per Codex P2 plan-review feedback.
33+
- `bin/gstack-gbrain-sync.ts:deriveCodeSourceId` — drops the host segment from canonical remote URLs (the same `github.com-` prefix on every user's id was eating 12 chars of the 32-char gbrain budget for nothing) and falls back to a 6-char sha1 hash on the slug tail when org/repo names still exceed the limit. Every `github.com/<org>/<repo>` derives a gbrain-valid id on the first try. Contributed by @radubach (PR #1330).
34+
- `bin/gstack-gbrain-sync.ts:constrainSourceId` — handles the empty-slug edge case (input sanitizes to all non-alnum chars). Pre-fix the function returned `${prefix}-` which fails gbrain's validator on the trailing hyphen; now falls back to a deterministic sha1-prefixed id. Surfaced via the new `basename-sanitizes-to-empty` regression test added in this version per Codex plan-review.
35+
36+
#### Added
37+
- `test/gstack-memory-ingest.test.ts` — two regression tests stand up a fake `gbrain` shim on PATH and run the real `--bulk` ingest pipeline against a planted Claude Code session. The first asserts the writer hits `gbrain put <slug>` (not `put_page`) and that title, type, AND tags arrive in the put stdin. The second points the writer at a legacy-only shim and asserts the availability probe surfaces a single missing-subcommand error instead of N per-page failures. Contributed by @AZ-1224 (PR #1341); the assertions for title/type/tags arriving in stdin are added on top here. The strengthened test surfaced a deeper issue in PR #1328's inject branch: it searched for `\n---\n` (with trailing newline) but `buildTranscriptPage` joins frontmatter without a trailing newline, so the search never matched. Two-line fix on top: search for `\n---` only.
38+
- `test/gstack-gbrain-sync.test.ts` — four cases from PR #1330 (dot-host, SCP-style remote, multi-dot host, long org/repo forcing hash-truncate) plus two new edge cases this version (no-origin fallback path; basename-sanitizes-to-empty). Each test spawns the CLI inside a temp git repo and asserts the derived id passes gbrain's validator regex. Contributed by @radubach for the four core cases.
39+
40+
#### For contributors
41+
- Codex outside-voice plan review caught three P1 ship-blockers in the originally proposed merge (the no-frontmatter-wrap branch from PR #1341 alone would have silently dropped title/type/tags from every transcript page — its own tests passed because they only asserted `agent: claude-code`). The plan pivoted from `merge #1341 + cherry-pick from #1328` to `merge #1328 + hybrid writer + cherry-pick #1341's tests, strengthened`. Two-pass live smoke against real gbrain (where the database connects) confirmed source-id length goes 38 → 27 chars; memory-ingest writer correctness was verified by the strengthened shim tests against a real `gbrain` CLI process.
42+
- Two follow-up TODOs filed: P2 to bump the `bin/gstack-gbrain-install` pin in lockstep with gstack memory-feature releases (issue #1305 part 2), P3 to handle source-id cross-host collisions (`github.com/acme/foo` and `gitlab.com/acme/foo` currently collapse to the same id; rare but silent).
43+
344
## [1.26.4.0] - 2026-05-05
445

546
## **`/autoplan` review reports now reliably land at the bottom of the plan, even when an older copy lives mid-file.**

TODOS.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,42 @@
8888

8989
---
9090

91+
### P2: Bump gbrain install-pin in lockstep with gstack memory-feature releases (#1305 part 2)
92+
93+
**What:** `bin/gstack-gbrain-install` pins gbrain to commit `08b3698` (v0.18.2). When gstack ships features that depend on newer gbrain ops or schema (e.g. v1.26.0 manifests + `code-def`/`code-refs`/`reindex-code`), the pin doesn't move with it. Fresh `/setup-gbrain` installs an old gbrain that fails `gbrain doctor` schema_version checks (24 vs latest 32+) until the user manually upgrades.
94+
95+
**Why:** Filed in #1305 alongside the `put_page` CLI bug. Out of scope for the v1.26.5.0 fix wave (separate release-coordination concern: which gbrain version we install vs. how we call it). The install-pin should either (a) auto-bump whenever gstack releases features that need newer gbrain, or (b) detect a stale pin during preamble and either auto-upgrade gbrain or print a one-line FIX hint.
96+
97+
**Pros:** Closes the "fresh-install paper-cut" path. New users land on a healthy schema. Reduces support noise on `/setup-gbrain` flows. Makes the gstack/gbrain release contract visible.
98+
99+
**Cons:** Adds release-cadence coupling between gstack and gbrain. Needs a policy: pin = "minimum version that still works" vs "latest known good." If gbrain ships a breaking change to `put` shape and gstack doesn't update the pin, fresh installs break in a new way.
100+
101+
**Context:** Issue #1305 part 1 (the `put_page` CLI verb bug) was handled in v1.26.5.0. Part 2 (this TODO) is the install-pin staleness. Pin lives in `bin/gstack-gbrain-install` near the top as a constant. Easiest minimal fix: ship the pin as a tracked release artifact (e.g. write it from `package.json` at build time) and add a doctor-style preamble check.
102+
103+
**Effort:** S (human: ~2 days / CC: ~3 hours)
104+
**Priority:** P2
105+
**Depends on:** Nothing.
106+
107+
---
108+
109+
### P3: Source-id host-collision risk in `deriveCodeSourceId` (cross-host duplicate org/repo)
110+
111+
**What:** v1.26.5.0's `deriveCodeSourceId` drops the host segment to fit gbrain's 32-char source-id budget. This means `github.com/acme/foo` and `gitlab.com/acme/foo` collapse to the same `gstack-code-acme-foo`. `ensureSourceRegisteredSync()` in `bin/gstack-gbrain-sync.ts:323` will silently re-register the source when `local_path` differs, evicting one side.
112+
113+
**Why:** Vanishingly rare in practice — same `<org>/<repo>` shape across both github.com and gitlab.com on the same machine almost never happens. But the failure mode is silent (one repo evicts the other in the brain), and the user has no signal anything is wrong.
114+
115+
**Pros:** Closes the silent-eviction edge. Two viable approaches: short host marker (`gh-` / `gl-` / `bb-`) eats 3 chars but keeps cross-host uniqueness; OR include a 3-char hash of the host alongside the org-repo.
116+
117+
**Cons:** Source IDs change shape again — anyone with existing registrations on v1.26.5.0 gets a one-time re-register. Net break-even because the current scheme also changed from v1.26.4.0.
118+
119+
**Context:** Filed in #1320 / #1322 / #1323 / #1331 (the underlying source-id validation bugs), addressed in v1.26.5.0 by dropping host segment + hash-truncating. Cross-host collision was a known accepted tradeoff in PR #1330's design ("vanishingly rare in practice"). Codex outside-voice plan review surfaced it as a long-tail concern; this TODO captures it for a future bump.
120+
121+
**Effort:** XS (human: ~4 hours / CC: ~30 min)
122+
**Priority:** P3
123+
**Depends on:** Nothing.
124+
125+
---
126+
91127
### P3: GBrain skillpack publishing for domain skills
92128

93129
**What:** Domain skills are agent-authored notes per hostname. Right now they're per-machine or per-agent-repo. The natural compounding extension: publish curated skill packs to GBrain (`gstack-brain-sync`) so others can subscribe. "Louise's LinkedIn skills" or "Garry's GitHub skills" become packs anyone can pull.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.26.4.0
1+
1.26.5.0

bin/gstack-gbrain-sync.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { existsSync, statSync, mkdirSync, writeFileSync, readFileSync, unlinkSyn
3333
import { join, dirname } from "path";
3434
import { execSync, execFileSync, spawnSync } from "child_process";
3535
import { homedir } from "os";
36+
import { createHash } from "crypto";
3637

3738
import { detectEngineTier, withErrorContext, canonicalizeRemote } from "../lib/gstack-memory-helpers";
3839
import { sourcePageCount } from "../lib/gbrain-sources";
@@ -158,20 +159,51 @@ function originUrl(): string | null {
158159
}
159160

160161
/**
161-
* Derive a stable source id for the cwd code corpus. Pattern: `gstack-code-<slug>`,
162-
* where <slug> comes from canonicalizeRemote() then `/` → `-` (e.g.,
163-
* `github.com/garrytan/gstack` → `gstack-code-github-com-garrytan-gstack`).
162+
* Derive a stable source id for the cwd code corpus. Pattern: `gstack-code-<slug>`.
164163
*
165-
* Falls back to `gstack-code-<basename(repo)>` when there is no origin (local repo).
164+
* gbrain enforces source ids to be 1-32 lowercase alnum chars with optional interior
165+
* hyphens. We use the last two segments of the canonical remote (org/repo) and skip
166+
* the host — `github.com` etc. is the same for nearly every user and just eats budget.
167+
* If the resulting id still exceeds 32 chars, we keep the tail (most distinctive end)
168+
* and append a 6-char hash of the full slug for collision resistance.
169+
*
170+
* Falls back to the repo basename when there is no origin (local repo).
166171
*/
167172
function deriveCodeSourceId(repoPath: string): string {
168173
const remote = canonicalizeRemote(originUrl());
169174
if (remote) {
170-
return `gstack-code-${remote.replace(/[\/\s]+/g, "-").replace(/-+/g, "-")}`;
175+
const segs = remote.split("/").filter(Boolean);
176+
const slugSource = segs.slice(-2).join("-");
177+
return constrainSourceId("gstack-code", slugSource);
171178
}
172-
// Fallback for repos without a remote.
173179
const base = repoPath.split("/").pop() || "repo";
174-
return `gstack-code-${base.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/-+/g, "-")}`;
180+
return constrainSourceId("gstack-code", base);
181+
}
182+
183+
/**
184+
* Build a gbrain-valid source id (1-32 lowercase alnum + interior hyphens). Sanitizes
185+
* `raw`, prefixes with `prefix`, and falls back to a hashed-tail form when total length
186+
* would exceed 32 chars.
187+
*/
188+
function constrainSourceId(prefix: string, raw: string): string {
189+
const MAX = 32;
190+
const slug = raw.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
191+
// Empty slug after sanitize (e.g. raw was all non-alnum like "___") would
192+
// produce "${prefix}-" which fails gbrain's validator on the trailing
193+
// hyphen. Fall back to a deterministic hash of the original input so the
194+
// result is stable across runs of the same repo.
195+
if (!slug) {
196+
const hash = createHash("sha1").update(raw || "_empty").digest("hex").slice(0, 6);
197+
return `${prefix}-${hash}`;
198+
}
199+
const full = `${prefix}-${slug}`;
200+
if (full.length <= MAX) return full;
201+
const hash = createHash("sha1").update(slug).digest("hex").slice(0, 6);
202+
// Total budget: prefix + "-" + tail + "-" + hash
203+
const tailBudget = MAX - prefix.length - 2 - hash.length;
204+
if (tailBudget < 1) return `${prefix}-${hash}`;
205+
const tail = slug.slice(-tailBudget).replace(/^-+|-+$/g, "");
206+
return tail ? `${prefix}-${tail}-${hash}` : `${prefix}-${hash}`;
175207
}
176208

177209
function gbrainAvailable(): boolean {

bin/gstack-memory-ingest.ts

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
* keep V1 ship-tight. See TODOS.md.
3535
*
3636
* V1.5 NOTE: When `gbrain put_file` ships in the gbrain CLI (cross-repo P0 TODO),
37-
* transcripts will route to Supabase Storage instead of put_page. Until then, all
38-
* content rides put_page; gbrain's native dedup keys on session_id.
37+
* transcripts will route to Supabase Storage instead of the page-write path.
38+
* Until then, all content rides `gbrain put <slug>` (stdin, YAML frontmatter for
39+
* title/type/tags); gbrain's native dedup keys on session_id.
3940
*/
4041

4142
import {
@@ -745,14 +746,25 @@ function buildArtifactPage(path: string, type: MemoryType): PageRecord {
745746
};
746747
}
747748

748-
// ── Writer (calls gbrain put_page) ─────────────────────────────────────────
749+
// ── Writer (calls `gbrain put`) ────────────────────────────────────────────
749750

750751
let _gbrainAvailability: boolean | null = null;
751752
function gbrainAvailable(): boolean {
752753
if (_gbrainAvailability !== null) return _gbrainAvailability;
753754
try {
754755
execSync("command -v gbrain", { stdio: "ignore" });
755-
_gbrainAvailability = true;
756+
// gbrain v0.27 retired the legacy `put_page` flag-form for `put <slug>`
757+
// (content via stdin, metadata as YAML frontmatter). Probe `--help` for
758+
// the `put` subcommand so we surface a single clean error here rather
759+
// than failing every page with "Unknown command: put_page". The regex
760+
// anchors on the indented subcommand format gbrain's help actually uses
761+
// (" put ..."), not any whitespace-bordered "put" word in prose.
762+
const help = execFileSync("gbrain", ["--help"], {
763+
encoding: "utf-8",
764+
timeout: 5000,
765+
stdio: ["ignore", "pipe", "pipe"],
766+
});
767+
_gbrainAvailability = /^\s+put\s/m.test(help);
756768
} catch {
757769
_gbrainAvailability = false;
758770
}
@@ -761,25 +773,63 @@ function gbrainAvailable(): boolean {
761773

762774
function gbrainPutPage(page: PageRecord): { ok: boolean; error?: string } {
763775
if (!gbrainAvailable()) {
764-
return { ok: false, error: "gbrain CLI not in PATH" };
776+
return { ok: false, error: "gbrain CLI not in PATH or missing `put` subcommand" };
777+
}
778+
// gbrain v0.27+ uses `put <slug>` (positional, content via stdin) instead
779+
// of the legacy `put_page` flag form. Metadata rides as YAML frontmatter:
780+
// - When the page body already starts with frontmatter (transcripts), inject
781+
// title/type/tags into the existing block so gbrain's frontmatter parser
782+
// picks them up.
783+
// - When the page body has no frontmatter (raw artifacts: design-docs,
784+
// learnings, builder-profile-entries), wrap with a fresh frontmatter
785+
// carrying the same fields. Without this branch, artifact pages would
786+
// land in gbrain with empty title/type/tags.
787+
let body = page.body;
788+
if (body.startsWith("---\n")) {
789+
// Locate the closing --- delimiter. buildTranscriptPage joins with "\n"
790+
// and does not append a trailing newline, so the close fence looks like
791+
// "...\n---" followed directly by body content (no "\n---\n" pattern).
792+
// Match the close on "\n---" only — the inject lands BEFORE the close
793+
// fence, inside the frontmatter block, regardless of what follows it.
794+
const end = body.indexOf("\n---", 4);
795+
if (end > 0) {
796+
const inject = [
797+
`title: ${JSON.stringify(page.title)}`,
798+
`type: ${page.type}`,
799+
`tags:`,
800+
...page.tags.map((t) => ` - ${t}`),
801+
].join("\n");
802+
body = body.slice(0, end) + "\n" + inject + body.slice(end);
803+
}
804+
} else {
805+
body = [
806+
"---",
807+
`title: ${JSON.stringify(page.title)}`,
808+
`type: ${page.type}`,
809+
`tags: [${page.tags.map((t) => JSON.stringify(t)).join(", ")}]`,
810+
"---",
811+
"",
812+
body,
813+
].join("\n");
765814
}
766815
try {
767-
const args = [
768-
"put_page",
769-
"--slug", page.slug,
770-
"--title", page.title,
771-
"--type", page.type,
772-
"--tags", page.tags.join(","),
773-
];
774-
execFileSync("gbrain", args, {
775-
input: page.body,
816+
execFileSync("gbrain", ["put", page.slug], {
817+
input: body,
776818
encoding: "utf-8",
777-
timeout: 30000,
819+
// Bumped from 30s: auto-link reconciliation on dense transcripts hits
820+
// 30s once the brain has a few hundred existing pages.
821+
timeout: 60000,
822+
// Bumped from default 1MB: without this, gbrain's actual stderr gets
823+
// truncated and callers see only "Command failed:" with no detail.
824+
maxBuffer: 16 * 1024 * 1024,
778825
stdio: ["pipe", "pipe", "pipe"],
779826
});
780827
return { ok: true };
781-
} catch (err) {
782-
return { ok: false, error: err instanceof Error ? err.message : String(err) };
828+
} catch (err: any) {
829+
const stderr = err?.stderr?.toString?.() ?? "";
830+
const stdout = err?.stdout?.toString?.() ?? "";
831+
const detail = stderr || stdout || (err instanceof Error ? err.message : String(err));
832+
return { ok: false, error: detail.split("\n")[0].slice(0, 300) };
783833
}
784834
}
785835

0 commit comments

Comments
 (0)