Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Outside small-fix wave 3: five contributor fixes (#9597)
Garry Tan0d78dcfa2eae
diff --git a/behavior-changes.d/captured_facts_codex_sessions_9554.md b/behavior-changes.d/captured_facts_codex_sessions_9554.mdnew file mode 100644index 0000000000..d7d685ce1d--- /dev/null+++ b/behavior-changes.d/captured_facts_codex_sessions_9554.md@@ -0,0 +1,4 @@+---+kind: fix+---+gbrain repair captured-facts now recognises facts captured from Codex sessions: it reads the session ids from the rollout file names in the live and archived Codex session folders, so those facts count as not suspect instead of unclassifiable. The preview's counts change on brains that capture from Codex; nothing is deleted or changed by the classification itself.diff --git a/behavior-changes.d/connectors_sync_managed_brain_9124.md b/behavior-changes.d/connectors_sync_managed_brain_9124.mdnew file mode 100644index 0000000000..764db88399--- /dev/null+++ b/behavior-changes.d/connectors_sync_managed_brain_9124.md@@ -0,0 +1,4 @@+---+kind: fix+---+gbrain connectors sync <provider> run in the foreground now imports chat history on a brain whose writes go through the writer: it submits each imported page through the CLI's own write context, the way gbrain transcripts ingest already does. Before, the import was refused on such a brain and no conversations landed. If you saw that refusal, run gbrain connectors sync again in the foreground. A --background or scheduled sync and the connector_sync operation are unchanged and still refused there.diff --git a/behavior-changes.d/doctor_only_engine_free_9527.md b/behavior-changes.d/doctor_only_engine_free_9527.mdnew file mode 100644index 0000000000..790b501b12--- /dev/null+++ b/behavior-changes.d/doctor_only_engine_free_9527.md@@ -0,0 +1,4 @@+---+kind: fix+---+gbrain doctor --only <check> on a check that needs no database (for example skill_resolver) now reports "No finding" at info level when the check has nothing to say, with no connection row. Before, it warned that the database checks stopped early and pointed at a connection that was never tried. A connection that really failed still reports the stop and keeps its connection row.diff --git a/behavior-changes.d/gmail_html_plain_body_8937.md b/behavior-changes.d/gmail_html_plain_body_8937.mdnew file mode 100644index 0000000000..e570033a45--- /dev/null+++ b/behavior-changes.d/gmail_html_plain_body_8937.md@@ -0,0 +1,4 @@+---+kind: fix+---+A Gmail message part labelled text/plain whose content is really an HTML document (it starts with a doctype, html or head tag) is now stripped to readable text, the way an HTML part is. Before, the page stored the raw tags and CSS as the message body. Pages already imported are not rewritten.diff --git a/changelog.d/capy-outside-fix-wave-3.md b/changelog.d/capy-outside-fix-wave-3.mdnew file mode 100644index 0000000000..4bb5cfdd55--- /dev/null+++ b/changelog.d/capy-outside-fix-wave-3.md@@ -0,0 +1,13 @@+**Five small fixes from outside reports: connector sync on a brain with an active writer, Gmail bodies that are HTML labelled as plain text, `repair captured-facts` for Codex sessions, `doctor --only` on checks that need no database, and `regen:all --check` for the CLI flag registry.**++<!-- behavior-change: connectors_sync_managed_brain_9124 -->+<!-- behavior-change: gmail_html_plain_body_8937 -->+<!-- behavior-change: captured_facts_codex_sessions_9554 -->+<!-- behavior-change: doctor_only_engine_free_9527 -->++### Fixed+- `gbrain connectors sync <provider>` run in the foreground now imports chat history on a brain whose writes go through the writer, the way `gbrain transcripts ingest` already does, instead of being refused. A `--background` or scheduled sync, and the `connector_sync` operation, still pass no write context. (#9124, reported and first fixed by @Masashi-Ono0611)+- A Gmail message part labelled `text/plain` that is really an HTML document is now stripped to readable text instead of being stored with its raw tags and CSS. (#8937, reported and first fixed by @Masashi-Ono0611)+- `gbrain repair captured-facts` now recognises facts captured from Codex sessions, in both the live and the archived Codex session folders, instead of counting them as unclassifiable. (#9554, reported and first fixed by @Masashi-Ono0611)+- `gbrain doctor --only <check>` on a check that needs no database now reports "No finding" when the check has nothing to say, instead of a warning that the database checks stopped early. A connection that really failed still reports the stop. (#9527, reported and first fixed by @Masashi-Ono0611)+- `bun run regen:all --check` now fails when the committed CLI flag registry is out of date, by comparing it with a fresh render. (#9019, reported and first fixed by @sx4im)diff --git a/docs/architecture/key-files/core-utilities-2.md b/docs/architecture/key-files/core-utilities-2.mdindex 948670312f..46109a0b9b 100644--- a/docs/architecture/key-files/core-utilities-2.md+++ b/docs/architecture/key-files/core-utilities-2.md@@ -10,7 +10,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL- `src/core/rerank-audit.ts` — failure-only JSONL audit at `~/.gbrain/audit/rerank-failures-YYYY-Www.jsonl` (ISO-week rotation, mirrors `src/core/audit-slug-fallback.ts`). Exports `logRerankFailure({reason, model, query_hash, doc_count, error_summary})` + `readRecentRerankFailures(days)`. The `no_key` reason is written ONCE per process per model by the gateway itself (not per query by `applyReranker`): the provider key is absent, so calls are skipped without HTTP and results pass through unreranked; `auth` means key present but rejected; `unreachable` means no HTTP response at all (connection refused, DNS, reset; a stale `provider_base_urls` entry fails every call this way, Cat 40 R0), while `network` stays a provider 5xx. Deliberately no `logRerankSuccess`: writing once per tokenmax search is hot-path I/O churn AND success events leak query volume + timing into a local audit file. `gbrain doctor`'s `reranker_health` check (`doctor/checks/calibration.ts`) resolves enablement + model through `resolveSearchMode(loadSearchModeConfig)` — the plane search reranks with — and `rerankerReadiness` before reading the audit: readiness is evaluated against the DB-merged config plane (`loadConfigWithEngine`, so DB-plane provider keys and `provider_base_urls` self-host overrides count, exactly like the gateway the CLI configures): a brain with NO embedding provider (`isAvailable('embedding')` false on a configured gateway) is `ok` even when not ready — search runs keyword-only there and never reaches `applyReranker`, so there is nothing to fix; otherwise enabled-but-not-ready (key absent / unknown model) warns with the paste-ready fix; only audit rows for the RESOLVED model count (rows for any other model never make the live default warn or send the operator to verify the wrong key); `no_key` skip rows are informational once ready (those processes ran unreranked; a long-lived worker started before the key was added keeps skipping until restarted) and never outlive the fix as a warn; then the auth / payload / budget / unreachable (3+ rows: names the `provider_base_urls.<provider>` key) / transient / unknown ladders on the non-skip rows; disabled → ok (with an enable hint when the default is ready). Query text SHA-256-prefix-hashed (8 hex chars) for privacy. `GBRAIN_AUDIT_DIR` env override honored via the shared `resolveAuditDir()`.- `src/core/repair/extractor-facts.ts` — `gbrain repair extractor-facts` (#5731), explicit-only and preview-bound. `classifyExtractorFacts` (read-only; also the doctor check's count) takes conversation-extractor facts with `expired_at` set and `row_num` NULL and classes each: `excluded` (page missing or deleted, `superseded_by` set, withdrawn, an active same-text fact on the page, or an earlier same-text candidate), `ambiguous` (unmanaged brain; no committed request of the page with `completed_at = expired_at`, compared in SQL as microsecond UTC text, which only a same-transaction completion satisfies on Postgres; a consumer at or above v0.60.11.0; or the page has ever held fence-projected facts) or `evidenced`. The preview lists every candidate, hashes them with the selection (`--include-ambiguous`) and saves one approved item per page (with its request id) through `preview-approval.ts`; the apply loads only that set, replays a still-pending request with the ctx's CLI write wait, skips pages whose request already committed or whose facts are active again, and restores each still-identical fact with a fresh `row_num` above the page's facts and fence rows. Managed brains publish one database-only `managed_maintenance_restore_extractor_facts` request per page (`submitDatabaseMaintenanceIntent`, dispatched by `prepareMaintenanceMutation` to `prepareExtractorFactsRestore`, which reclassifies under the page lock); unmanaged brains run the same restore in one locked transaction. Previews warn, naming the host, when a consumer older than this release published after `writer_version_cutoff`. On PGLite `now()` has millisecond resolution, so the same-transaction rule is millisecond-exact there.- `src/core/repair/conversation-labels.ts` — `gbrain repair conversation-labels`, explicit-only, preview-bound and destructive (consent payload from the registry's `consentText`). `classifyLabelFacts` (read-only) takes active per-segment extractor facts (`source` exactly `cli:extract-conversation-facts`) on live pages with a bold `METADATA_LABELS` line and classes each `excluded` (page outcome stamped with `extractor_version`, withdrawn, superseded, named by another fact's `superseded_by`, referenced by `open_loops.fact_id`), `evidenced` (context carries `segment 1970-01-01`) or `ambiguous`. The preview decides each page's outcome with the current parser at no cost (`non_extractable`, `awaiting_reextraction`, `cleaned`), hashes it all, saves one approved item per page and prints the capped hand-off (`extract-conversation-facts --slugs … --dry-run`, then `--max-cost-usd`). The apply expires the approved ids still as previewed (appending `retired: conversation-labels <hash>` to the context), expires the completion marker and writes the stamped not-extractable outcome for a page still at the previewed parser input; approved pages apply in batches of `LABEL_BATCH_PAGES` (25): one database-only `managed_maintenance_conversation_label_retire` request per batch on `LABELS_ANCHOR_SLUG` on a managed brain (`prepareConversationLabelRetirement`, every page key locked, each page rechecked), one transaction per batch otherwise. No model calls. Segmented pages are not classified (the label filter reads the body columns): their unstamped per-segment facts are counted as `segmented_pages` / `segmented_facts` and named in a warning. `countLabelFacts` is doctor `conversation_label_facts`' bounded count. Pinned by `test/repair-conversation-labels.test.ts` (+ Postgres arm) and `test/conversation-facts-retained-history.test.ts`.-- `src/core/repair/captured-facts.ts` — `gbrain repair captured-facts`, explicit-only and preview-bound. `classifyCapturedFacts` (read-only; also the `captured_facts_active` count) takes active capture-lane facts and classes each by its session (`captureSessionId`: `sweep:corpus:<file>` resolves through `corpusFileSessionId`): `evidenced` when a gbrain claude-cli scratch project holds the session's harness transcript or its corpus file sits in `<corpus>.quarantine/`; `ambiguous` (paste candidate) when a retained corpus file of the session holds a paste block and at least 60% of the claim's content words occur there only inside paste blocks; `excluded:legitimate_duplicate` when the claim and entity also have an active row that is not proven suspect. Sessions with no transcript and no corpus file are counted `unclassifiable`. The preview hashes every candidate with the selection (`--include-ambiguous`) and saves one approved item per page through `preview-approval.ts`; the apply reclassifies each fact, expires database-only rows through one database-only `managed_maintenance_expire_captured_facts` request per page on a managed brain (owner-checked by `maintenancePreflight`; `prepareCapturedFactsExpiry`, dispatched from `prepareMaintenanceMutation`) or one transaction on an unmanaged brain, and strikes fenced rows in the page's `## Facts` fence through one revision-bound `put_page`, so the canonical projection expires them and a later page write cannot reactivate them. No withdrawal is recorded, so `remember` can save the same claim again. Pinned by `test/repair-captured-facts.test.ts` (+ Postgres arm).+- `src/core/repair/captured-facts.ts` — `gbrain repair captured-facts`, explicit-only and preview-bound. `classifyCapturedFacts` (read-only; also the `captured_facts_active` count) takes active capture-lane facts and classes each by its session (`captureSessionId`: `sweep:corpus:<file>` resolves through `corpusFileSessionId`): `evidenced` when a gbrain claude-cli scratch project holds the session's harness transcript or its corpus file sits in `<corpus>.quarantine/`; `ambiguous` (paste candidate) when a retained corpus file of the session holds a paste block and at least 60% of the claim's content words occur there only inside paste blocks; `excluded:legitimate_duplicate` when the claim and entity also have an active row that is not proven suspect. A session id matching a Codex rollout file name (live or archived) is not suspect; sessions with no transcript and no corpus file are counted `unclassifiable`. The preview hashes every candidate with the selection (`--include-ambiguous`) and saves one approved item per page through `preview-approval.ts`; the apply reclassifies each fact, expires database-only rows through one database-only `managed_maintenance_expire_captured_facts` request per page on a managed brain (owner-checked by `maintenancePreflight`; `prepareCapturedFactsExpiry`, dispatched from `prepareMaintenanceMutation`) or one transaction on an unmanaged brain, and strikes fenced rows in the page's `## Facts` fence through one revision-bound `put_page`, so the canonical projection expires them and a later page write cannot reactivate them. No withdrawal is recorded, so `remember` can save the same claim again. Pinned by `test/repair-captured-facts.test.ts` (+ Postgres arm).- `src/core/repair/redacted-facts.ts` — `gbrain repair redacted-facts` (#7125), explicit-only, preview-bound. `classifyRedactedFacts` (read-only) takes active facts holding a redaction marker and runs `restoreNeutralized` (with the pre-fix `LEGACY_EVAL_SHELL` shape) against the page its context names and the page holding it (`pageTexts`: body + timeline, a segmented body via `readBodyRange` up to 16 MiB, then up to 50 `page_versions` bodies): `evidenced` when every proving text agrees, `ambiguous` (`conflicting_sources`) when a text holds two spans or texts disagree, `unproven` (`no_source_page` / `not_in_source`) otherwise. A numbered row is fenced only while its page's fence holds an active row with its text at that number; else an extractor row takes the database route (#8820) and any other is `unproven` (`fence_row_missing`). A proved fenced row on a segmented page is `segmented` (`segmented_page`): listed with its restoration, never rewritten. The preview hashes every candidate and saves one approved item per page; the apply reclassifies each fact, rewrites database-route rows through one database-only `managed_maintenance_restore_redacted_facts` request per page on a managed brain (`prepareRedactedFactsRestore`, dispatched from `prepareMaintenanceMutation`) or one transaction on an unmanaged brain (exact compare-and-set; context gets `restored: redacted-facts repair`), and rewrites fenced rows' claims in the page's `## Facts` fence through one revision-bound `put_page`, so the projection replaces the row. Pinned by `test/repair-redacted-facts.test.ts` (+ Postgres arm).- `src/core/repair/loop-facts.ts` — `gbrain repair loop-facts` (#5869), explicit-only and preview-bound (`--apply --expect <hash>`): candidates are `loopFactDrift` rows (closed `done`/`dropped` loops whose commitment fact is active, in the loop's source and shared with no open loop); each item is rechecked (`changed_since_preview`) and retired through `retireLoopFact`, the same coordinated strike-plus-expire publication `loops_close` uses.- `src/core/repair/failed-writes.ts` — `gbrain repair failed-writes` (#5983), explicit-only and preview-bound: candidates are failed, uncompacted `persistence_requests` refused by the managed writer guard (`writer_coordinator_required`, or `storage_error` "Publication failed (P0001)"). Caller writes (`put_page` without an internal `kind`, `add_timeline_entry`, `remember`) are classified `replay` / `already_written` / `duplicate` / `superseded` / `unpinned_target` by later requests on the same page and by a committed replay request id, and `file_database_drift` (W4.11) when its last replay attempt was refused `source_changed` and the page has not been written since (`replayWalks`, set reads; the detail and an apply's `file_database_drift` outcome name `gbrain sources reconcile <source> <slug> --preview`; `test/repair-failed-writes-drift-w411.test.ts`); owner-produced writes are `producer_owned` residuals with the producer command (a failed `managed_file_repair` names both `gbrain repair frontmatter` and `gbrain repair fences`). #7602 fact rows: `factsRecovery` ([Facts](core-persistence-facts.md)). The approved set records each target page's revision. Apply resumes an admitted replay attempt, otherwise reclassifies and rechecks the revision, re-runs `authorizeStoredRequest` on the failed row, and resubmits the retained intent on the original trust lane (`laneContext`: remote, take holders, delegated prefixes) through `submitPageMutation` / `submitRememberMutation`, with `put_page` bound to the previewed revision and `remember` pinned to its resolved subject, under a request id derived from the failed request id and attempt (`repair-failed-writes-5983.test.ts`). #5994: `laneContext` sets `ctx.replayAuthority` to the stored authority, so admission (`submissionAuthority` → `replayedAuthority` in `persistence/authority.ts`, and `requestPrincipalForContext`) reuses its principal, delegation, scopes and autoLinkTrusted for the same source incarnation and re-authorizes it live; a restricted write gets back its recorded job (`delegatedJobId`, or the id in a single legacy `wiki/agents/<id>/*` prefix, replayed on the legacy sandboxed path when its original was), and one with neither is accepted by `enforceSubagentSlugFence` (`ops/context.ts`) only through that marker with the exact non-empty stored allow-list. The fence still fails closed for every live dispatch. A put_page replay passes the stored `timeline_section` (#5969). The superseded / already-written / duplicate rule lives in `persistence/terminal-classify.ts`, shared with the #6650 terminal-failure readers.diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex bd9443273e..f4343ffbcc 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -2851,7 +2851,8 @@ sweep) skips gbrain's own claude-cli model sessions and text you paste into aconversation. Facts a gbrain older than v0.60.30.0 captured from them stayactive, so recall and hot memory keep returning them. `gbrain repair captured-facts` expires them. It isexplicit-only and preview-bound, and it runs on the brain host (it reads the-Claude Code session directory and the session corpus there).+Claude Code session directory, the live and archived Codex session folders+and the session corpus there).**Say to your agent:** *"Doctor says some facts were captured from gbrain's ownsessions or from pasted text. Show me which ones before removing anything."*diff --git a/scripts/generate-flag-registry.ts b/scripts/generate-flag-registry.tsindex edca55f93e..5960973478 100644--- a/scripts/generate-flag-registry.ts+++ b/scripts/generate-flag-registry.ts@@ -27,6 +27,8 @@* Output: src/core/cli-flag-registry.generated.ts (committed; freshness is* pinned by test/cli-flag-validation.test.ts the same way build:llms pins the* llms bundles). Regenerate: bun run build:flag-registry+ * Check: bun scripts/generate-flag-registry.ts --check exits 1 when the+ * committed file differs from a fresh render (what regen:all --check runs).** Hand-tuning lane: EXTRA_FLAGS below, for flags that live deeper than the* one-level scan (add with a comment naming the deep module).@@ -553,7 +555,15 @@ ${render(routing)}if (import.meta.main) {const { registry, routing } = buildFlagArtifacts(ROOT);const outPath = join(ROOT, 'src/core/cli-flag-registry.generated.ts');- writeFileSync(outPath, renderRegistryModule(registry, routing));+ const next = renderRegistryModule(registry, routing);+ if (process.argv.includes('--check')) {+ if ((existsSync(outPath) ? readFileSync(outPath, 'utf8') : '') !== next) {+ console.error('src/core/cli-flag-registry.generated.ts is stale: run bun run regen:all');+ process.exit(1);+ }+ process.exit(0);+ }+ writeFileSync(outPath, next);const n = Object.keys(registry).length;const total = Object.values(registry).reduce((a, v) => a + v.length, 0);console.log(`wrote ${outPath} (${n} commands, ${total} flag entries)`);diff --git a/scripts/regen-all.ts b/scripts/regen-all.tsindex a252132e9c..56b03e9a75 100644--- a/scripts/regen-all.ts+++ b/scripts/regen-all.ts@@ -50,7 +50,7 @@ export const ARTIFACTS: Artifact[] = [{ name: 'skills manifest', regen: ['bun', 'run', 'scripts/generate-skills-manifest.ts'], check: ['bash', 'scripts/check-skills-manifest-fresh.sh'] },{ name: 'bundled skills module', regen: ['bun', 'run', 'scripts/generate-bundled-skills.ts'], check: ['bun', 'run', 'scripts/generate-bundled-skills.ts', '--check'] },{ name: 'eval metric glossary', regen: ['bun', 'run', 'scripts/generate-metric-glossary.ts'], check: ['bash', 'scripts/check-eval-glossary-fresh.sh'] },- { name: 'CLI flag registry', regen: ['bun', 'run', 'scripts/generate-flag-registry.ts'], check: [...BUN_TEST, 'test/generate-flag-registry.test.ts'] },+ { name: 'CLI flag registry', regen: ['bun', 'run', 'scripts/generate-flag-registry.ts'], check: ['bun', 'run', 'scripts/generate-flag-registry.ts', '--check'] },{ name: 'plugin tree + persona variants', regen: ['bun', 'run', 'scripts/generate-plugin-tree.ts', '--out', 'plugin', '--variants-out', 'plugin-variants'], check: ['bash', 'scripts/check-plugin-tree.sh'] },{ name: 'structural suites manifest', regen: ['bun', 'scripts/classify-tests.ts'], check: ['bun', 'scripts/classify-tests.ts', '--check'] },{ name: 'QM memory-provider contract', regen: ['bun', 'scripts/build-qm-memory-contract.ts'], check: ['bun', 'scripts/build-qm-memory-contract.ts', '--check'] },diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsvindex bf4589f08e..280bc868bc 100644--- a/scripts/structural-suites.tsv+++ b/scripts/structural-suites.tsv@@ -366,7 +366,7 @@ test/scripts/pregate-remote-gate.test.ts pregate-remote-gate.sh --list 4 readFiltest/scripts/pregate-remote-gate.test.ts reuse of GitHub full runs on the identical tree 9 readFileSynctest/scripts/pregate-remote-gate.test.ts ship_lanes_from (/ship lanes carried from CI, docs/ci-event-parity.md#ship-lanes) 3 readFileSynctest/scripts/promote-stable.test.ts promote-stable.yml 2 readFileSync-test/scripts/regen-all.test.ts regen:all 6 readFileSync+test/scripts/regen-all.test.ts regen:all 7 readFileSynctest/scripts/release-restamp.test.ts release-restamp --stamp-only 2 readFileSynctest/scripts/release-restamp.test.ts release:restamp end to end 16 readFileSynctest/scripts/release-restamp.test.ts release:unstamp and merge: direct 7 readFileSyncdiff --git a/src/cli/commands/connectors.ts b/src/cli/commands/connectors.tsindex e91595a78c..983216ba42 100644--- a/src/cli/commands/connectors.ts+++ b/src/cli/commands/connectors.ts@@ -5,8 +5,9 @@* src/cli/command-table.ts.*/import type { BrainEngine } from '../../core/engine.ts';+import type { CliDispatchContext } from '../command-table.ts';-export async function run(engine: BrainEngine, args: string[]): Promise<void> {+export async function run(engine: BrainEngine, args: string[], ctx: CliDispatchContext): Promise<void> {const { runConnectors } = await import('../../commands/connectors/index.ts');- await runConnectors(engine, args);+ await runConnectors(engine, args, { makeContext: ctx.makeContext });}diff --git a/src/commands/connectors/index.ts b/src/commands/connectors/index.tsindex 48d94cceae..16b039c49e 100644--- a/src/commands/connectors/index.ts+++ b/src/commands/connectors/index.ts@@ -12,6 +12,7 @@*/import type { BrainEngine } from '../../core/engine.ts';+import type { CliDispatchContext } from '../../cli/command-table.ts';import { setCliExitVerdict } from '../../core/cli-force-exit.ts';import { connectorProviders } from '../../core/connectors/registry.ts';@@ -46,7 +47,7 @@ function printHelp(): void {);}-export async function runConnectors(engine: BrainEngine, args: string[]): Promise<void> {+export async function runConnectors(engine: BrainEngine, args: string[], dispatch: Pick<CliDispatchContext, 'makeContext'> = {}): Promise<void> {const sub = args[0];if (!sub || sub === '--help' || sub === '-h' || sub === 'help') {printHelp();@@ -72,7 +73,7 @@ export async function runConnectors(engine: BrainEngine, args: string[]): Promis}case 'sync': {const { runConnectorSyncCmd } = await import('./sync.ts');- await runConnectorSyncCmd(engine, rest);+ await runConnectorSyncCmd(engine, rest, dispatch);return;}case 'logout': {diff --git a/src/commands/connectors/sync.ts b/src/commands/connectors/sync.tsindex 25465249ac..a1fb097de0 100644--- a/src/commands/connectors/sync.ts+++ b/src/commands/connectors/sync.ts@@ -12,6 +12,7 @@*/import type { BrainEngine } from '../../core/engine.ts';+import type { CliDispatchContext } from '../../cli/command-table.ts';import { setCliExitVerdict } from '../../core/cli-force-exit.ts';import { createProgress } from '../../core/progress.ts';import { getCliOptions, cliOptsToProgressOptions } from '../../core/cli-options.ts';@@ -84,7 +85,7 @@ function syncUsageError(message: string, fix: { argv: string[]; inputs: Array<{});}-export async function runConnectorSyncCmd(engine: BrainEngine, args: string[]): Promise<void> {+export async function runConnectorSyncCmd(engine: BrainEngine, args: string[], dispatch: Pick<CliDispatchContext, 'makeContext'> = {}): Promise<void> {if (args.includes('--help') || args.includes('-h')) {console.log(`Usage: ${SYNC_USAGE}`);return;@@ -138,6 +139,7 @@ export async function runConnectorSyncCmd(engine: BrainEngine, args: string[]):reporter.start(`connector.sync.${p}`);try {const r = await runConnectorSync(engine, {+ context: flags.dryRun ? undefined : await dispatch.makeContext?.(engine, { source: sourceId }),provider: p,sourceId,full: flags.full,diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.tsindex e69fbc9857..dd53e70aa3 100644--- a/src/commands/doctor/registry.ts+++ b/src/commands/doctor/registry.ts@@ -541,7 +541,8 @@ export function runDoctorRegistry(ctx: DoctorContext, entries: readonly DoctorEnchecks.push(...later.emits.filter((n) => !ctx.only || ctx.only.has(n)).map((n) => budgetUnknownCheck(n, ctx.connectError instanceof DoctorBudgetError ? ctx.connectError.elapsedMs : 0, ctx.deadline?.budgetMs ?? 0, 'overall')));}}- return ctx.only ? onlyResult(checks, ctx.only, true) : checks;+ // An engine-free --only run stops at the database gate by design: only a failed or skipped connect stopped the database checks early.+ return ctx.only ? onlyResult(checks, ctx.only, ctx.connectError !== undefined || ctx.connectionFailed === true || ctx.fastMode) : checks;}const pastDeadline = ctx.deadline?.deadlineAt != null && Date.now() >= ctx.deadline.deadlineAt;const produced = result.map((c) => {diff --git a/src/core/connectors/sync.ts b/src/core/connectors/sync.tsindex 234711dcb3..8e7b7b342f 100644--- a/src/core/connectors/sync.ts+++ b/src/core/connectors/sync.ts@@ -32,6 +32,7 @@*/import type { BrainEngine } from '../engine.ts';+import type { OperationContext } from '../ops/contract.ts';import { runTranscriptsIngest } from '../transcripts/ingest.ts';import type { TranscriptsIngestResult } from '../transcripts/ingest.ts';import { submitEmbedBackfill } from '../embed-backfill-submit.ts';@@ -194,6 +195,8 @@ export interface ConnectorSyncOpts {/** Epoch ms after which the run starts no request (#7515). */deadlineAt?: number;signal?: AbortSignal;+ /** The CLI's trusted-local operation context, handed to the transcripts ingest: a writer-claimed brain refuses the direct import without it. */+ context?: OperationContext;onProgress?: (p: { phase: 'list' | 'fetch' | 'ingest'; listed: number; fetched: number; imported: number }) => void;deps?: ConnectorSyncDeps;}@@ -424,6 +427,7 @@ export async function runConnectorSync(sourceId,format: spoolFormat,embed: opts.embed ?? false,+ context: opts.context,});importedTotal += r.pages.imported;skippedTotal += r.pages.skipped;diff --git a/src/core/google/google-clients.ts b/src/core/google/google-clients.tsindex a77af045ca..c3cef8c484 100644--- a/src/core/google/google-clients.ts+++ b/src/core/google/google-clients.ts@@ -358,7 +358,10 @@ export function extractBody(part: RawGmailPart | undefined): { text: string; isHcontinue;}if (p.mimeType === 'text/plain' && p.body?.data) {- return { text: decodeB64Url(p.body.data), isHtml: false };+ const plain = decodeB64Url(p.body.data);+ // A text/plain part that is itself an HTML document is markup: flag it so+ // the caller strips it instead of storing raw tags and CSS as the body.+ return { text: plain, isHtml: /^\s*(<!doctype\s|<html[\s>]|<head[\s>])/i.test(plain) };}if (p.mimeType === 'text/html' && p.body?.data && html === null) {html = decodeB64Url(p.body.data);diff --git a/src/core/repair/captured-facts.ts b/src/core/repair/captured-facts.tsindex b5bc10eb7d..24d9641fa6 100644--- a/src/core/repair/captured-facts.ts+++ b/src/core/repair/captured-facts.ts@@ -50,7 +50,7 @@ import type { WriteRequest } from '../persistence/model.ts';import { submitPageMutation } from '../persistence/page-mutations.ts';import { clearApprovedSet, loadApprovedSet, previewChangedError, previewHash, saveApprovedSet } from '../persistence/preview-approval.ts';import { claudeCliSelfSessions } from '../ai/providers/claude-cli-scratch.ts';-import { claudeProjectsDir } from '../bootstrap/host-specs.ts';+import { claudeProjectsDir, codexArchivedSessionsDir, codexSessionsDir } from '../bootstrap/host-specs.ts';import { corpusFileSessionId, corpusTextForExtraction } from '../context/corpus-segments.ts';import { pruneDir } from '../sync.ts';import { afterCursor, repairRequestId, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairListing, type RepairPlan, type RepairScope } from './core.ts';@@ -142,6 +142,22 @@ function harnessSessionIds(projectsRoot: string): Set<string> {return ids;}+/** Session ids of Codex rollouts (`rollout-<timestamp>-<uuid>.jsonl`) in the live (nested by date) and archived (flat) stores. */+function codexSessionIds(roots: string[]): Set<string> {+ const ids = new Set<string>();+ const walk = (dir: string, depth: number) => {+ let entries: import('node:fs').Dirent[];+ try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }+ for (const entry of entries) {+ if (entry.isDirectory()) { if (depth < 4) walk(join(dir, entry.name), depth + 1); continue; }+ const m = /^rollout-.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i.exec(entry.name);+ if (m) ids.add(m[1]!);+ }+ };+ for (const root of roots) walk(root, 0);+ return ids;+}+/** The capture session a fact row came from (`sweep:corpus:<file>` names the corpus file). */export function captureSessionId(sourceSession: string | null): string | null {if (!sourceSession) return null;@@ -168,7 +184,7 @@ export async function classifyCapturedFacts(db: BrainEngine, sourceIds: string[]if (!rows.length) return { candidates: [], unclassifiable: 0, not_suspect: 0, unreadable_corpus_files: [] };const projectsRoot = opts.projectsRoot ?? claudeProjectsDir();const selfSessions = claudeCliSelfSessions(projectsRoot);- const known = harnessSessionIds(projectsRoot);+ const known = new Set([...harnessSessionIds(projectsRoot), ...codexSessionIds([codexSessionsDir(), codexArchivedSessionsDir()])]);const corpusDir = opts.corpusDir === undefined ? await db.getConfig(CORPUS_KEY) : opts.corpusDir;const unreadable: string[] = [];const corpus = sessionFiles(corpusDir, unreadable);diff --git a/test/connectors-sync-context-9124.test.ts b/test/connectors-sync-context-9124.test.tsnew file mode 100644index 0000000000..7c779b9298--- /dev/null+++ b/test/connectors-sync-context-9124.test.ts@@ -0,0 +1,57 @@+/**+ * #9124: `connectors sync` hands the CLI's write context to the transcripts ingest.+ * Without it a writer-claimed brain refuses the direct import, so chat history never lands there.+ * Seams: the ChatGPT fixture handler in process (no network), PGLite, the real ingest behind a recording wrapper.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { runConnectorSync } from '../src/core/connectors/sync.ts';+import { saveCredential } from '../src/core/connectors/credentials.ts';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { runTranscriptsIngest } from '../src/core/transcripts/ingest.ts';+import { chatgptHandler, newFixtureState } from './fixtures/connectors/fixture-server.ts';+import { withEnv } from './helpers/with-env.ts';++let engine: PGLiteEngine;+beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+}, 120_000);+afterAll(async () => { await engine.disconnect(); });++const T0 = 1_786_000_000;++test('runConnectorSync passes its context to every ingest batch, and the import still lands', async () => {+ const handler = chatgptHandler(newFixtureState([{+ id: 'c-9124', title: 'Synthetic topic', createTime: T0 - 100, updateTime: T0,+ turns: [{ role: 'user', text: 'q' }, { role: 'assistant', text: 'a' }],+ }]));+ const home = mkdtempSync(join(tmpdir(), 'gbrain-connectors-9124-'));+ const context = { remote: false, sourceId: 'default' } as unknown as OperationContext;+ const seen: Array<OperationContext | undefined> = [];+ try {+ const result = await withEnv({ GBRAIN_HOME: home }, async () => {+ saveCredential({ provider: 'chatgpt', strategy: 'browser-session', cookie: 'sessionKey=fixture', savedAt: new Date(0).toISOString() });+ return runConnectorSync(engine, {+ provider: 'chatgpt',+ sourceId: 'default',+ context,+ deps: {+ fetchImpl: async (url, init) => handler(new Request(url, init as RequestInit)),+ now: () => (T0 + 100_000) * 1000,+ sleep: () => Promise.resolve(),+ runIngest: (e, o) => { seen.push(o.context); return runTranscriptsIngest(e, o); },+ },+ });+ });+ expect(seen.length).toBeGreaterThan(0);+ for (const c of seen) expect(c).toBe(context);+ expect(result.ingest?.imported).toBe(1);+ } finally {+ rmSync(home, { recursive: true, force: true });+ }+}, 60_000);diff --git a/test/doctor-only-engine-free-9527.test.ts b/test/doctor-only-engine-free-9527.test.tsnew file mode 100644index 0000000000..c63901ed46--- /dev/null+++ b/test/doctor-only-engine-free-9527.test.ts@@ -0,0 +1,48 @@+/**+ * #9527: an engine-free `doctor --only` run stops at the database gate by design, so a requested check that emits+ * nothing reads "No finding", and no connection row claims a connect that was never tried. A connect that really+ * failed still marks the requested checks as stopped early and keeps its connection row.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { buildChecks } from '../src/commands/doctor.ts';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { withEnv } from './helpers/with-env.ts';++let engine: PGLiteEngine;+let home: string;+beforeAll(async () => {+ home = mkdtempSync(join(tmpdir(), 'gbrain-doctor-9527-'));+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+}, 120_000);+afterAll(async () => {+ await engine.disconnect();+ rmSync(home, { recursive: true, force: true });+});++const rows = (engineArg: BrainEngine | null) => withEnv({ GBRAIN_HOME: home, GBRAIN_SKILLS_DIR: undefined, OPENCLAW_WORKSPACE: undefined },+ async () => (await buildChecks(engineArg, ['--only', 'skill_resolver'], 'config-file')).map(c => ({ name: c.name, message: c.message })));++test('engine-free --only: no connection row, and the empty check reads No finding', async () => {+ const checks = await rows(null);+ expect(checks.map(c => c.name)).toEqual(['skill_resolver']);+ expect(checks[0]!.message).toContain('No finding');+});++test('a connect that failed still reports the connection and the checks it stopped', async () => {+ const failing = new Proxy(engine, {+ get(target, key) {+ if (key === 'getStats') return async () => { throw new Error('connection terminated unexpectedly'); };+ const value = Reflect.get(target, key);+ return typeof value === 'function' ? value.bind(target) : value;+ },+ }) as unknown as BrainEngine;+ const checks = await rows(failing);+ expect(checks.map(c => c.name)).toEqual(['connection', 'skill_resolver']);+ expect(checks[1]!.message).toContain('stopped early');+});diff --git a/test/google-extract-body-html-plain.test.ts b/test/google-extract-body-html-plain.test.tsnew file mode 100644index 0000000000..dffb8e9d52--- /dev/null+++ b/test/google-extract-body-html-plain.test.ts@@ -0,0 +1,25 @@+import { expect, test } from 'bun:test';++import { extractBody } from '../src/core/google/google-clients.ts';+import { htmlToText } from '../src/core/google/google-render.ts';++const b64url = (s: string): string => Buffer.from(s, 'utf-8').toString('base64url');++test('an HTML document labelled text/plain is flagged as markup', () => {+ const doc = '<!DOCTYPE html><html><head><style>p{color:red}</style></head><body><p>Hello team</p></body></html>';+ const out = extractBody({ mimeType: 'text/plain', body: { data: b64url(doc) } });+ const body = out.isHtml ? htmlToText(out.text) : out.text;+ expect(out.isHtml).toBe(true);+ expect(body).toBe('Hello team');+});++test('a genuine plain part is kept as plain text', () => {+ const out = extractBody({+ mimeType: 'multipart/alternative',+ parts: [+ { mimeType: 'text/plain', body: { data: b64url('plain body wins') } },+ { mimeType: 'text/html', body: { data: b64url('<p>html body loses</p>') } },+ ],+ });+ expect(out).toEqual({ text: 'plain body wins', isHtml: false });+});diff --git a/test/repair-captured-facts-codex-transcripts.test.ts b/test/repair-captured-facts-codex-transcripts.test.tsnew file mode 100644index 0000000000..2253ef90c2--- /dev/null+++ b/test/repair-captured-facts-codex-transcripts.test.ts@@ -0,0 +1,35 @@+/** A capture-lane fact whose session id names a Codex rollout is classified, not left unclassifiable. */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { classifyCapturedFacts } from '../src/core/repair/captured-facts.ts';+import { withEnv } from './helpers/with-env.ts';++const CODEX_ID = '0199aaaa-bbbb-7ccc-8ddd-eeeeeeeeeeee';++describe('captured-facts classifier reads Codex rollouts', () => {+ let engine: PGLiteEngine;+ const base = mkdtempSync(join(tmpdir(), 'gbrain-captured-codex-'));+ beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ }, 120_000);+ afterAll(async () => { await engine.disconnect(); rmSync(base, { recursive: true, force: true }); });++ test('session id matching a Codex rollout is not_suspect; an unknown id stays unclassifiable', async () => {+ const day = join(base, 'codex', 'sessions', '2026', '01', '02');+ mkdirSync(day, { recursive: true });+ writeFileSync(join(day, `rollout-2026-01-02T03-04-05-${CODEX_ID}.jsonl`), '{}\n');+ mkdirSync(join(base, 'claude', 'projects'), { recursive: true });+ for (const [fact, session] of [['Alice likes tea', CODEX_ID], ['Bob likes jam', 'no-such-session']]) {+ await engine.executeRaw(`INSERT INTO facts (source_id, fact, kind, visibility, source, source_session)+ VALUES ('default', $1, 'fact', 'private', 'hook:writeback', $2)`, [fact, session]);+ }+ const result = await withEnv({ CODEX_HOME: join(base, 'codex'), CLAUDE_CONFIG_DIR: join(base, 'claude') },+ () => classifyCapturedFacts(engine, ['default'], { corpusDir: null }));+ expect({ not_suspect: result.not_suspect, unclassifiable: result.unclassifiable }).toEqual({ not_suspect: 1, unclassifiable: 1 });+ }, 120_000);+});diff --git a/test/scripts/regen-all.test.ts b/test/scripts/regen-all.test.tsindex 96314fa5dc..2e8bdd18ed 100644--- a/test/scripts/regen-all.test.ts+++ b/test/scripts/regen-all.test.ts@@ -27,6 +27,12 @@ describe('regen:all', () => {expect(readFileSync(join(ROOT, 'docs/RELEASING.md'), 'utf8')).toContain('## Generated artifacts');});+ test('the CLI flag registry check compares the committed file with a fresh render (#9019)', async () => {+ const registry = ARTIFACTS.find(a => a.name === 'CLI flag registry')!;+ expect(registry.check).toEqual(['bun', 'run', 'scripts/generate-flag-registry.ts', '--check']);+ expect(await checkArtifacts([registry])).toEqual([]);+ }, 60_000);+test('the public surface is built after the operation manifest it reads', () => {const names = ARTIFACTS.map(a => a.name);expect(names.indexOf('public surface (docs/contracts/public-surface.json)')).toBeGreaterThan(names.indexOf('operation manifest'));