Files
gbrain/test/infer-type-pack.test.ts
T
646179047a v0.42.56.0 feat(chronicle): Life Chronicle — temporal timeline + thought diary + bi-temporal per-entity ontology (#2390) (#2533)
* feat(chronicle): register event + diary page types (#2390)

Life Chronicle Phase A.1. Adds `event` (timeline atom) and `diary`
(first-person interiority) as temporal-primitive page types under
life/events/ and life/diary/, extractable:false — registered in
ALL_PAGE_TYPES, both base schema packs, and the inferType prefix table,
with parity fixtures. Also lands the chronicle read result types
(ChronicleTimelineRow/ChronicleTimelineOpts/LastSeenResult) consumed by
Phase A.2.

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

* feat(chronicle): event_page_id timeline projection + day/since/last-seen reads (#2390)

Life Chronicle Phase A.2. Adds a nullable event_page_id FK to
timeline_entries (migration v120; mirrored in schema.sql, pglite-schema,
schema-embedded) so a type:event page projects ONE date-index row keyed
to its depth page; a partial UNIQUE(event_page_id, date) makes
re-extraction with a changed summary an update, not a duplicate.

Dual-engine getTimelineForDate / getSince / getLastSeen filter the depth
page on deleted_at, hide soft-deleted event projections at READ time
(not just doctor), order by event effective_date for intra-day sequence,
and honor source scope (sourceIds[] > sourceId). Ops surface as
`gbrain day <date> [--week]`, `gbrain since <date> [--kind]`,
`gbrain last-seen <entity>`.

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

* feat(chronicle): auto-emit extractor — backstop + chronicle_extract job (#2390)

Life Chronicle Phase A.3. A put_page backstop (gated on status==='imported',
the auto-link/timeline trust gate, and the default-OFF auto_chronicle flag;
diary + event pages never eligible) enqueues a chronicle_extract minion job.
The job runs the extractor off the write path: deterministic when/who, an
injectable LLM judge (default = chat gateway; no-op when no gateway), an
all-or-nothing parse barrier (a malformed proposal writes NOTHING), then
content-addressed event pages + a timeline projection via the new dual-engine
upsertEventProjection (idempotent — re-run yields one event + one projection).

New: src/core/chronicle/{eligibility,config,extract-events,backstop}.ts,
engine.upsertEventProjection (both engines), the jobs.ts handler + a 10-min
timeout. 14 unit tests (eligibility, idempotency, parse barrier, backstop
gating + enqueue) green.

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

* feat(chronicle): quick-capture for diary + manual events (#2390)

Life Chronicle Phase A.5. `gbrain capture` now routes the default slug by
type (diary → life/diary/, event → life/events/, else inbox/) and accepts
--who/--what/--where/--kind/--depth to assemble the `event:` frontmatter
block for `--type event`. User-declared event keys win per-key over the
flags. Goes through the existing put_page → write-through → embed path. 6
new unit tests; existing capture tests stay green.

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

* feat(chronicle): bi-temporal per-entity ontology on the facts table (#2390)

Life Chronicle Phase B.10 — the feature's differentiator. Rather than a
parallel store, the open-world per-entity ontology EXTENDS the existing
`facts` table (eng-review G1): migration v121 adds dimension/value/value_hash
/dim_status columns + a deterministic partial-UNIQUE dedup key + an asof read
index. facts already supplies bi-temporal validity, supersession
(superseded_by), visibility (remote redaction), confidence, provenance, and
embedding — all inherited.

Dual-engine methods: mergeOntologyFact (deterministic value_hash dedup →
idempotent retry; same value corroborates; a different value forward-closes
the prior row's valid_until + superseded_by; a BACKDATED conflicting value is
recorded WITHOUT rewriting, surfaced by findOntologyConflicts), getOntology
with `--asof` valid-time travel (expired_at + status + validity-window in the
predicate so quarantined/superseded never leak), discoverOntologyDimensions,
findOntologyConflicts. Novel/LLM-proposed dimensions quarantine; a seed lexicon
canonicalizes name drift (job_role → role). 9 unit tests cover the full
lifecycle; typecheck pins both engines to the interface.

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

* feat(chronicle): ontology ops — get/add/dimensions/contradictions (#2390)

Life Chronicle Phase B.11. Exposes the bi-temporal ontology over CLI + MCP
(contract-first, auto-generated): `gbrain ontology <entity> [--asof]`,
`gbrain ontology-add <entity> <dim> <value>`, `gbrain ontology-dimensions`
(meta-ontology rollup), `gbrain ontology-contradictions`. All reads route
through sourceScopeOpts. Privacy: ontology_get redacts diary-sourced
observations (source under life/diary/) for untrusted (remote) callers. 3 op
tests (incl. the remote-redaction path); 47 op-registry/tool-def/description
tests stay green.

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

* feat(chronicle): agent-context loader — volunteer_chronicle (#2390)

Life Chronicle Phase B.12. `loadChronicleContext` hands an agent the recent
timeline (last N days) + the validity-resolved current ontology for the
entities in play, in one zero-LLM payload, so it orients before acting — the
exact gap behind fumbled chronology. Pure composition over getSince +
getOntology (no new SQL). Exposed as the `volunteer_chronicle` read op
(`gbrain orient [--days] [--entities a,b]`); diary-sourced ontology is redacted
for remote callers. 2 loader tests + op-registry green.

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

* feat(chronicle): backfill op — sweep existing meetings into events (#2390)

Life Chronicle Phase A.8. `chronicle_backfill` (local-only admin op;
`gbrain chronicle-backfill [--since] [--limit] [--dry-run]`) lists existing
meeting/conversation/calendar pages (source-scoped via listPages), filters
through the chronicle eligibility predicate, and enqueues one chronicle_extract
job per eligible page so existing brains populate the timeline. --dry-run
counts only; per-page enqueue failures are surfaced in `errors`, never
swallowed. 2 op tests (dry-run count + enqueue).

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

* feat(chronicle): delight — on-this-day + narrative rendering (#2390)

Life Chronicle Phase A.6 (delight). Dual-engine getOnThisDay (events from the
same month-day in prior years; `gbrain on-this-day [--date]`) reusing the
chronicle JOIN shape (deleted-event hiding + source scope). A pure
renderTimelineNarrative turns timeline rows into prose; `gbrain day --narrative`
returns it alongside the events. 5 tests. (Coverage gap-detection ships with
the advisor collectors next.)

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

* feat(chronicle): proactive advisor collector (#2390)

Life Chronicle Phase A.7. A brain-state advisor collector surfaces two
proactive signals in `gbrain advisor`: unresolved ontology conflicts (warn,
→ `gbrain ontology-contradictions`) and recent meetings with no timeline
coverage (info, → `gbrain chronicle-backfill`). Advisory-only (no dispatch_id);
runs over MCP too (not workspace-dependent); tolerant of pre-migration brains.
3 tests + advisor-op-gate green.

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

* feat(chronicle): doctor chronicle_projection_health probe (#2390)

Life Chronicle Phase B.13. An always-run doctor check (keyed off the
event_page_id schema, NOT a migration verify-hook) counts timeline
projections whose event page is soft-deleted — hidden at read time, surfaced
here as a cleanup backlog (`gbrain integrity auto`). Tolerant of pre-migration
brains. 1 detection test. (auto_chronicle / chronicle.tz flags already work
via getConfig defaults; their docs land with document-release at ship.)

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

* feat(chronicle): E1 temporal recall — chronicle-type boost on temporal queries (#2390)

Life Chronicle Phase A.4 (E1, ambient temporality). Rather than a separate RRF
arm (which needs chunk hydration + risks the fusion path), E1 is a bounded
post-fusion boost: applyChronicleTypeBoost lifts `event`/`diary` results on
temporal queries, wired INSIDE runPostFusionStages' `recency !== 'off'` branch
so it fires ONLY on temporal intent — non-temporal search is bit-for-bit
unchanged (proven by 110 passing search-path tests). Bounded ([1.0,1.25]) +
floor-gated like the other metadata stages; attribution via `chronicle_boost`.
3 unit tests. (Empirical precision/negative measurement lands with the eval.)

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

* feat(chronicle): feature eval — gbrain eval chronicle (PRIMARY proof) (#2390)

Life Chronicle Phase A.9, the North-Star proof. A deterministic, CI-safe eval
(brings its own in-memory PGLite; no LLM, no gateway) builds a synthetic month
corpus with a known gold chronology + a planted ontology supersession + a
planted conflict, then scores the chronicle layer on six gold tasks: day
reconstruction (intra-day order), last-seen exact date, ontology supersession,
--asof valid-time travel, contradiction surfacing, and source isolation.
`gbrain eval chronicle [--json]` exits 0 iff all pass — currently 6/6. The OFF
baseline (raw meeting pages) structurally can't order intra-day events or
time-travel ontology; the ON path does. (The live-LLM OFF-vs-ON agent arm +
LongMemEval temporal slice are a follow-up; this deterministic bar gates CI.)

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

* fix(chronicle): pre-landing review — conflict validity, parse-barrier date, remote conflict redaction (#2390)

Three bugs caught by the codex pre-landing review on the diff:
1. findOntologyConflicts ignored valid_until, so a normal forward supersession
   (founder→advisor from two sources) falsely reported as a live conflict. Now
   restricted to currently-open rows (valid_until IS NULL) in both engines.
2. The extractor parse barrier accepted any when-string >= 4 chars; a non-date
   value slipped past, wrote the event page, then threw on the projection's
   ::date cast (partial write). isValidProposal now requires a real parseable date.
3. The ontology_conflicts op had no remote diary redaction (ontology_get did);
   remote callers now get diary-sourced values filtered, and conflicts that lose
   their disagreement after redaction are dropped.

Three regression tests added; 29 chronicle tests + eval 6/6 green; typecheck clean.

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

* v0.42.56.0 feat(chronicle): Life Chronicle — temporal timeline + diary + bi-temporal ontology (#2390)

Bump VERSION + package.json to 0.42.56.0 and add the CHANGELOG entry for the
Life Chronicle feature (#2390, closes duplicate #2388).

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

* fix(chronicle): register #2390 surfaces with the five CI guard suites (#2390)

CI caught five guard tests that pin registration invariants my diff tripped:
- schema-bootstrap-coverage: the four v122 facts ontology columns join
  COLUMN_EXEMPTIONS (facts is migration-created; the partial indexes live
  inside v122; every reader filters dimension IS NOT NULL — same precedent
  as facts.claim_metric et al).
- no-valid-until-write (R8): both engines' mergeOntologyFact forward-
  supersession is a deliberate, documented valid_until write authority
  (engine-layer, dimension IS NOT NULL only; the contradiction probe still
  never mutates).
- doctor-categories: chronicle_projection_health registered under
  BRAIN_CHECK_NAMES (same class as child_table_orphans).
- checkTypeProliferation: the test is now threshold-relative (computes
  declared from the active pack, seeds declared+6) so base-pack growth
  can't silently move the fixed threshold again.
- schema-cli: gbrain-base page-type count 25 → 27 (event + diary), with
  assertions on both new types.

All 41 guard tests green; typecheck clean.

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

* docs: file Life Chronicle follow-up TODOs (v0.42.56.0, #2390)

Eight deferred items from the CEO/eng review decisions (auto-emit default-flip
fast-follow, live eval arm + LongMemEval slice, passive diary consent,
interval-splitting, federation, place-as-entity, meta-ontology dashboard,
materialized daily pages), each with decision provenance.

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

* docs(architecture): KEY_FILES entry for the Life Chronicle module (#2390)

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

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 10:48:54 -07:00

114 lines
6.0 KiB
TypeScript

// v0.38 T7a: pack-aware inferType parity + extension tests.
//
// Parity gate: `inferTypeFromPack(path, gbrain-base)` must produce
// IDENTICAL output to the legacy `inferType(path)` for every known
// path prefix. If this drifts, gbrain-base.yaml is out of sync with
// the GBRAIN_BASE_PATH_PREFIXES table in markdown.ts.
//
// Extension test: a user pack adding `paper: { path_prefixes:
// ['papers/'] }` must route `papers/foo.md` to 'paper' (the pack
// declaration), bypassing the gbrain-base fallback.
import { describe, expect, test } from 'bun:test';
import { parseMarkdown } from '../src/core/markdown.ts';
import { inferTypeFromPack } from '../src/core/markdown.ts';
import { loadPackFromFile } from '../src/core/schema-pack/loader.ts';
import { parseSchemaPackManifest } from '../src/core/schema-pack/manifest-v1.ts';
import { join } from 'node:path';
const GBRAIN_BASE_PATH = join(import.meta.dir, '../src/core/schema-pack/base/gbrain-base.yaml');
// Representative paths covering every gbrain-base path-prefix entry.
const PARITY_FIXTURES: ReadonlyArray<{ path: string; expected: string; reason: string }> = [
{ path: 'people/alice.md', expected: 'person', reason: 'people/ prefix' },
{ path: 'companies/acme.md', expected: 'company', reason: 'companies/ prefix' },
{ path: 'deals/acme-seed.md', expected: 'deal', reason: 'deals/ prefix' },
{ path: 'yc/w24.md', expected: 'yc', reason: 'yc/ prefix' },
{ path: 'civic/policy/sf.md', expected: 'civic', reason: 'civic/ prefix' },
{ path: 'projects/blog/index.md', expected: 'project', reason: 'projects/ prefix' },
{ path: 'wiki/concepts/inversion.md', expected: 'concept', reason: 'wiki/concepts/ prefix' },
{ path: 'sources/article.md', expected: 'source', reason: 'sources/ prefix' },
{ path: 'media/books/x.md', expected: 'media', reason: 'media/ prefix' },
{ path: 'writing/essay.md', expected: 'writing', reason: 'writing/ prefix' },
{ path: 'wiki/analysis/foo.md', expected: 'analysis', reason: 'wiki/analysis/ wins over wiki/' },
{ path: 'wiki/guides/setup.md', expected: 'guide', reason: 'wiki/guides/ prefix' },
{ path: 'wiki/hardware/x.md', expected: 'hardware', reason: 'wiki/hardware/ prefix' },
{ path: 'wiki/architecture/x.md', expected: 'architecture', reason: 'wiki/architecture/ prefix' },
{ path: 'meetings/2026-04-03.md', expected: 'meeting', reason: 'meetings/ prefix' },
{ path: 'notes/random.md', expected: 'note', reason: 'notes/ prefix' },
{ path: 'emails/em-0001.md', expected: 'email', reason: 'emails/ prefix' },
{ path: 'slack/sl-0037.md', expected: 'slack', reason: 'slack/ prefix' },
{ path: 'cal/2026-05-20.md', expected: 'calendar-event', reason: 'cal/ prefix' },
// v0.42.x — Life Chronicle (#2390): timeline events + thought diary.
{ path: 'life/events/2026-06-18-002-sync.md', expected: 'event', reason: 'life/events/ prefix' },
{ path: 'life/diary/2026-06-18-am.md', expected: 'diary', reason: 'life/diary/ prefix' },
// Stronger-signal wins: writing/ inside projects/
{ path: 'projects/blog/writing/essay.md', expected: 'writing', reason: 'writing/ wins over projects/' },
// Fallback: paths not matching any prefix
{ path: 'random/path.md', expected: 'concept', reason: 'no prefix match → concept default' },
];
describe('inferTypeFromPack (T7a) — gbrain-base parity', () => {
test('parity: every known path maps to the same type via pack as via legacy', () => {
const pack = loadPackFromFile(GBRAIN_BASE_PATH);
for (const { path, expected, reason } of PARITY_FIXTURES) {
const actual = inferTypeFromPack(path, pack);
// For parity, the pack result MUST match the legacy hardcoded result.
// Verify by parsing markdown — parseMarkdown calls the legacy inferType
// when frontmatter doesn't override.
const md = `# ${path}\nbody`;
const parsed = parseMarkdown(md, path);
expect(parsed.type).toBe(expected);
expect(actual).toBe(expected);
// Sanity: the reason annotation isn't a test assertion but documents
// why each fixture exists. Surface unused-variable lint via toBeTruthy.
expect(reason.length).toBeGreaterThan(0);
}
});
test('user pack extends gbrain-base with researcher type', () => {
// Synthetic pack declaring a new type with its own prefix.
const pack = parseSchemaPackManifest({
api_version: 'gbrain-schema-pack-v1',
name: 'research-test',
version: '0.1.0',
extends: null,
page_types: [
{ name: 'researcher', primitive: 'entity', path_prefixes: ['researchers/'], aliases: [], extractable: true, expert_routing: true },
{ name: 'paper', primitive: 'media', path_prefixes: ['papers/'], aliases: [], extractable: false, expert_routing: false },
],
link_types: [],
});
expect(inferTypeFromPack('researchers/alice.md', pack)).toBe('researcher');
expect(inferTypeFromPack('papers/smith-2024.md', pack)).toBe('paper');
// Paths NOT in the pack's prefixes default to 'concept'.
expect(inferTypeFromPack('people/alice.md', pack)).toBe('concept');
});
test('pack with empty page_types falls back to gbrain-base defaults', () => {
// E.g. a pack mid-construction with no page_types declared — should
// not crash, should match gbrain-base behavior.
const emptyPack = parseSchemaPackManifest({
api_version: 'gbrain-schema-pack-v1',
name: 'empty',
version: '0.1.0',
extends: null,
page_types: [],
link_types: [],
});
expect(inferTypeFromPack('people/alice.md', emptyPack)).toBe('person');
expect(inferTypeFromPack('media/foo.md', emptyPack)).toBe('media');
});
test('undefined filePath returns concept default', () => {
const pack = loadPackFromFile(GBRAIN_BASE_PATH);
expect(inferTypeFromPack(undefined, pack)).toBe('concept');
});
test('case-insensitive matching', () => {
const pack = loadPackFromFile(GBRAIN_BASE_PATH);
expect(inferTypeFromPack('PEOPLE/Alice.md', pack)).toBe('person');
expect(inferTypeFromPack('Companies/ACME.md', pack)).toBe('company');
});
});