Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

doctor schema_pack_consistency: a retyped code page is not exempt

garrytan94216c9cf703
diff --git a/docs/architecture/key-files/schema-mutation.md b/docs/architecture/key-files/schema-mutation.md
index c11a8b8ca5..ca34e29c98 100644
--- a/docs/architecture/key-files/schema-mutation.md
+++ b/docs/architecture/key-files/schema-mutation.md
@@ -22,7 +22,7 @@ Key files:
- `src/core/schema-pack/entity-types.ts` — #4772: the one answer to "is this stored type an entity" for the counters. `entityTypesFromPack(manifest)` returns the pack's `primitive: entity` type names (plus undeclared aliases of them) and the legacy names (`entity`, `person`, `company`, `organization`) the pack leaves undeclared; a pack that declares a legacy name under another primitive reclassified it and wins. `resolveEntityTypes(engine, scope)` resolves per source (every requested source id, or brain-wide: the brain's pack plus every registered source with a `schema_pack.source.<id>` override) into an `EntityTypeFilter` (default list + per-source lists that differ), and returns `pack_unavailable` with an empty filter when any pack in scope fails to load (the `best-effort.ts` null contract: empty filter, never hardcoded defaults). `entityTypePredicateSql(alias, filter)` renders it as a parameter-bound `sqlFragment`. #6953: `personTypesFromPack` / `resolvePersonTypes` answer "is this type person-like" the same way (per source, same `pack_unavailable` contract): `person` plus its alias closure (`closure.ts`), never primitive siblings; consumer `cycle/edge-contradictions.ts`: its `works_at` members are admitted before grouping and LIMIT only when the subject is person-like by its source's pack (a pack that does not load admits none) and the target is neither person-like nor a `NON_EMPLOYER_TYPES` type (untyped: `NON_EMPLOYER_PREFIXES`; both lists shared with `link-target-roles.ts`'s `targetTakesVerb`); an open proposal made before that rule is not re-checked when applied (`test/edge-employment-roles.test.ts` + Postgres twin; `personTypesFromPack` in `test/schema-pack-person-types-6953.test.ts`). Consumers: `engine-sql/health.ts` through `HealthDeps.entityTypes` (both engines delegate), `commands/doctor/checks/graph-health.ts` (`graph_coverage`, `orphan_ratio`), `onboard/checks.ts` (`entity_link_coverage`, `timeline_coverage`, and `visibleEntityCoverageSql` for `onboard/impact-capture.ts`; a pack that does not load warns `entity_types_unavailable`), `onboard/mcp-onboarding.ts` (the three entity counts are `null` with `entityTypesStatus: pack_unavailable`; the timeline result predicate) and `extract-ner.ts` (the target-type map; `pack_unavailable` when a source's pack does not load). `mentions/policy.ts` keeps its own linkable-type union. Pinned by `test/onboard-entity-types-pack-4772.test.ts`. Lazy-imports the pack loader so the engines can import it statically.
- `src/core/schema-pack/best-effort.ts` — `loadActivePackBestEffort(ctx)` returns `ResolvedPack | null`. Single source of truth for the pack-aware wiring sites. `null` means EMPTY FILTER (NOT hardcoded defaults, which would silently violate the pack). Threads the tier-4 DB-plane `schema_pack` from `ctx.engine`, so every caller resolves the SAME pack `schema active` / `get_active_schema_pack` report. `readDbSchemaPack(engine?)` is the ONE spelling of that tier-4 read (null-safe, propagates DB read failures, trims missing/empty values to undefined) — shared by both loaders here, the CLI inspection verbs (`schema show/graph/lint/explain`, via `readDbSchemaPackConfig` in `src/commands/schema.ts`, which keeps the cfg-null gate so an unconfigured home never cold-creates a PGLite dir) and the MCP `schema_lint`/`schema_graph`/`schema_explain_type` ops. `loadActivePackForLocalEngine(engine)` is the `remote: false` + FILE-ONLY-config variant for local engine-backed surfaces. Pinned by `test/schema-pack-best-effort.test.ts`, `test/operations-schema-pack.test.ts`, `test/schema-cli-db-config.test.ts`.
-- `src/core/schema-pack/type-usage.ts` — stored-type classifier behind the alias-footgun visibility surfaces: `classifyStoredType(type, pack)` → canonical | alias_of (with the canonical type + `path_prefixes[0]` filing directory) | undeclared, over a STRUCTURAL pack shape so import-file's thin `activePack` and the full manifest both satisfy it. `sanitizeTypeForDisplay` strips control chars + caps length (type strings come from frontmatter and get echoed into terminals); `renderTypeWarningSummary` renders the once-per-type-per-run lines. Consumers: `importFromContent` (advisory `ImportResult.type_warning` at the typeExplicit site — the type is still stored literally, zero filing change), sync/import summary aggregation (+ `SyncResult.type_warnings` so worker-driven syncs surface counts in job results), the `stored_type_is_alias`/`stored_type_undeclared` data-plane lint rules, all gated by config `schema.type_warnings` (default on; lint rules always active). `storedTypeMissesPack(type, pack)` (empty type, or undeclared) is the one predicate every pack-conformance surface uses: `findTypeOrphans` (`schema-pack/review.ts`), `schema_stats` coverage, doctor `schema_pack_consistency`, `gbrain lint`'s `type-undeclared` rule. `WRITER_OWNED_PAGE_SQL` exempts pages gbrain's own writers wrote, judged by provenance (a code importer page, `page_kind = 'code'`; a `dream_generated` `extract_receipt` under `extracts/`), from doctor `schema_pack_consistency`'s undeclared count, so a custom pack copied before gbrain-base-v2 declared those types (#6818) is not reported as drift, while a user's Markdown page typed `code` still is (#8815, `test/doctor-schema-pack-own-types-8815.test.ts`). Pinned by `test/schema-type-usage.test.ts` and `test/schema-pack-type-conformance.test.ts`.
+- `src/core/schema-pack/type-usage.ts` — stored-type classifier behind the alias-footgun visibility surfaces: `classifyStoredType(type, pack)` → canonical | alias_of (with the canonical type + `path_prefixes[0]` filing directory) | undeclared, over a STRUCTURAL pack shape so import-file's thin `activePack` and the full manifest both satisfy it. `sanitizeTypeForDisplay` strips control chars + caps length (type strings come from frontmatter and get echoed into terminals); `renderTypeWarningSummary` renders the once-per-type-per-run lines. Consumers: `importFromContent` (advisory `ImportResult.type_warning` at the typeExplicit site — the type is still stored literally, zero filing change), sync/import summary aggregation (+ `SyncResult.type_warnings` so worker-driven syncs surface counts in job results), the `stored_type_is_alias`/`stored_type_undeclared` data-plane lint rules, all gated by config `schema.type_warnings` (default on; lint rules always active). `storedTypeMissesPack(type, pack)` (empty type, or undeclared) is the one predicate every pack-conformance surface uses: `findTypeOrphans` (`schema-pack/review.ts`), `schema_stats` coverage, doctor `schema_pack_consistency`, `gbrain lint`'s `type-undeclared` rule. `WRITER_OWNED_PAGE_SQL` exempts pages gbrain's own writers wrote, judged by provenance (a code importer page, `type` and `page_kind` both `code`; a `dream_generated` `extract_receipt` under `extracts/`), from doctor `schema_pack_consistency`'s undeclared count, so a custom pack copied before gbrain-base-v2 declared those types (#6818) is not reported as drift, while a user's Markdown page typed `code` still is (#8815, `test/doctor-schema-pack-own-types-8815.test.ts`). Pinned by `test/schema-type-usage.test.ts` and `test/schema-pack-type-conformance.test.ts`.
- `src/core/schema-pack/lint-rules.ts` — 14 pure rule functions. `withMutation`'s pre-write validation gate composes the 10 file-plane rules; the 4 DB-aware rules (`extractable_empty_corpus`, `mutation_count_anomaly`, `stored_type_is_alias`, `stored_type_undeclared`) need an engine (CLI `--with-db`; the stored-type pair accepts `LintOpts.sourceId` scoping, not yet threaded from the CLI). Single source of truth consumed by CLI lint + MCP `schema_lint` + the pre-write validation gate. File-plane rule `link_regex_catastrophic_backtrack` — advisory ReDoS pre-screen flagging the classic nested-quantifier shapes (`(a+)+`, `(a*)*`, `(a+)*`, `(\w+)+`) in a link_type's `inference.regex` via `NESTED_QUANTIFIER_RE`. WARNING not error: a hard reject would disable the whole pack on upgrade (pages fall back to legacy typing). The runtime input-length cap in `redos-guard.ts` is the actual safety net; this rule tells the pack author to fix the pattern.
- `src/core/schema-pack/redos-guard.ts` + `src/core/schema-pack/link-inference.ts` — ReDoS hardening for pack inference regexes. `redos-guard.ts` provides `MAX_REGEX_INPUT_CHARS` (default 64_000, env `GBRAIN_MAX_REGEX_INPUT_CHARS`) — a hard input-length cap, the real runtime safety net (catastrophic backtracking needs a long input; a link-extraction `context` is normally a sentence or short paragraph). Over the cap, `runRegexBounded` throws the tagged `RegexInputTooLargeError` and the regex is skipped (degrade-to-mentions) without entering the `node:vm`. `link-inference.ts:inferLinkTypeFromPack` no-budget branch (test contexts) routes through `runRegexBounded` so the input-length cap + per-regex vm timeout (`PER_REGEX_TIMEOUT_MS = 50`) apply on every path. Defensive hardening + diagnostics; the deterministic ~3100-file sync-wedge root cause remains open. Pinned by `test/redos-hardening.test.ts` + `test/schema-pack-lint-rules.test.ts`.
- `src/core/schema-pack/query-cache-invalidator.ts` — `invalidateQueryCache(engine, sourceId?)` DELETEs query_cache rows so cached search results bound to old page types don't survive a schema mutation.
diff --git a/src/core/schema-pack/type-usage.ts b/src/core/schema-pack/type-usage.ts
index 3e45a46300..bae78b032d 100644
--- a/src/core/schema-pack/type-usage.ts
+++ b/src/core/schema-pack/type-usage.ts
@@ -64,13 +64,14 @@ export function storedTypeMissesPack(type: string | null | undefined, pack: Type
/**
* SQL predicate over `pages p`: the page was written by one of gbrain's own
* writers outside the content taxonomy, judged by how it was written rather
- * than by its type name. A code importer page carries page_kind 'code'; an
+ * than by its type name alone. A code importer page is a `code` page with
+ * page_kind 'code' (retyped to anything else, it is the owner's type); an
* extract receipt is an `extract_receipt` under extracts/ stamped
* dream_generated. gbrain-base-v2 declares both types since #6818; a custom
* pack copied before then lacks them, which is not the owner's schema drift
* (#8815). A user's Markdown page typed `code` matches neither.
*/
-export const WRITER_OWNED_PAGE_SQL = `(p.page_kind = 'code' OR (p.type = 'extract_receipt' AND p.slug LIKE 'extracts/%' AND p.frontmatter->>'dream_generated' = 'true'))`;
+export const WRITER_OWNED_PAGE_SQL = `((p.type = 'code' AND p.page_kind = 'code') OR (p.type = 'extract_receipt' AND p.slug LIKE 'extracts/%' AND p.frontmatter->>'dream_generated' = 'true'))`;
/**
* Type strings come from user/agent frontmatter and get echoed into terminal
diff --git a/test/doctor-schema-pack-own-types-8815.test.ts b/test/doctor-schema-pack-own-types-8815.test.ts
index 0bd4de3d30..5ee759133c 100644
--- a/test/doctor-schema-pack-own-types-8815.test.ts
+++ b/test/doctor-schema-pack-own-types-8815.test.ts
@@ -14,6 +14,8 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { checkSchemaPackConsistency } from '../src/commands/doctor/schema-pack-checks.ts';
import { writeReceipt } from '../src/core/extract/receipt-writer.ts';
import { importCodeFile, importFromContent } from '../src/core/import-file.ts';
+import { runRetypeCore } from '../src/core/schema-pack/retype.ts';
+import type { OperationContext } from '../src/core/operations.ts';
import { emptyHome, withEnv } from './helpers/with-env.ts';
const PACK = `api_version: gbrain-schema-pack-v1
@@ -82,3 +84,18 @@ test('a user Markdown page typed like a gbrain page is still undeclared drift',
const receipt = await consistency();
expect(receipt.message).toContain("'extract_receipt' (1)");
});
+
+test('a code importer page retyped to an undeclared type is drift', async () => {
+ await engine.executeRaw('DELETE FROM pages');
+ await importCodeFile(engine, 'src/example.ts', 'export const x = 1;\n', { noEmbed: true });
+ expect((await consistency()).status).toBe('ok');
+ const ctx = { engine, config: {}, logger: { info() {}, warn() {}, error() {} }, dryRun: false, remote: false } as unknown as OperationContext;
+ const retyped = await withEnv({ GBRAIN_HOME: home, GBRAIN_SCHEMA_PACK: undefined }, () =>
+ runRetypeCore(ctx, { rules: [{ from_type: 'code', to_type: 'custom_fixture' }], apply: true, sourceId: 'default' }));
+ expect(retyped.total_applied).toBe(1);
+ const [page] = await engine.executeRaw<{ type: string; page_kind: string }>("SELECT type, page_kind FROM pages WHERE deleted_at IS NULL");
+ expect(page).toEqual({ type: 'custom_fixture', page_kind: 'code' });
+ const drift = await consistency();
+ expect(drift.status).toBe('warn');
+ expect(drift.message).toContain("'custom_fixture' (1)");
+});