Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
docs(graph): temporal edges guide, key-files, verbs and troubleshooting (GBRA-49 P1)
garrytan53588e3d59b4
diff --git a/docs/architecture/key-files/graph-and-facts.md b/docs/architecture/key-files/graph-and-facts.mdindex d6cea2fee8..e5f802ec41 100644--- a/docs/architecture/key-files/graph-and-facts.md+++ b/docs/architecture/key-files/graph-and-facts.md@@ -106,3 +106,7 @@ rejection of serve-only flags before a brain opens.- `src/core/timeline-dedup-repair.ts` — schema-drift self-heal for `idx_timeline_dedup`. A master-merge migration renumbering can leave a brain's version counter stamped past the index change while the index keeps an old shape — and every `addTimelineEntry` batch then fails its `ON CONFLICT` inference, silently breaking timeline writes brain-wide. The version counter can't detect this, so the repair is keyed off the actual index SHAPE. Canonical shape is `(page_id, date, md5(summary), source)` — md5-keyed (migration v138) because a raw long/incompressible summary would overflow the btree v4 row cap (~2704 bytes) and abort every timeline insert for that page; both engines' insert sites infer `ON CONFLICT` on the md5 tuple, so `EXPECTED_COLUMNS` MUST carry the md5 form (a raw-shape expectation would make the repair revert v138 on every migrate pass). `checkTimelineDedupIndex(engine)` returns `{tablePresent, indexPresent, columns, needsRepair}` (read-only; powers the `timeline_dedup_index` doctor check; the indexdef column parser is paren-depth-aware so `md5(summary)` — or any future expression column — parses as ONE column instead of flagging a correct index as drifted forever) and `repairTimelineDedupIndex(engine)` dedupes-then-rebuilds to the canonical shape (raw-summary grouping ⟺ md5 grouping modulo negligible collisions). `runMigrations` invokes the repair on every pass (including the no-pending early-return path); idempotent no-op when the index already matches. `gbrain apply-migrations --force-schema` triggers it on demand. Pinned by `test/timeline-dedup-repair.test.ts`.- `src/core/trajectory-format.ts` — shared `formatTrajectoryBlock(points, entitySlug, opts)` consumed by both `gbrain think` (production) and the LongMemEval harness (benchmark). Groups by `(metric ?? event_type)`, per-metric cap 20, total cap 100, knowledge_update intent annotates value-change rows with `(superseded prior)`. Emits a `<trajectory entity="...">` XML envelope — `INJECTION_PATTERNS` in `src/core/think/sanitize.ts` escapes `</trajectory>`, `<trajectory ...>` open tags, and attribute injection so adversarial fact text can't break out. Pinned by `test/trajectory-format.test.ts`.++- `src/core/link-validity.ts` + `src/core/link-relationships.ts` + `src/core/link-temporal-evidence.ts` + `src/core/link-temporal-apply.ts` + `src/core/ops/edge-temporal.ts` — temporal typed edges ([guide](../../guides/temporal-edges.md)). Relation semantics (state / event / reference), `buildRelationshipState` (order-invariant stints from `links` assertions with `assertion_tense` plus dated `link_transitions`; dated beats undated, same-page present beats past), `closeContradiction` (pure date arithmetic, relationship-specific starts only), and `relationshipFilterSql` / `annotateTemporalRow` / `edgeMatchesTemporal` (one liveness rule: a relationship with no `link_relationships` row reads live). `refreshRelationships` recomputes per-scope state (`all`, `world` ignores private-origin evidence) inside the writer's transaction; `replaceDerivedLinks` calls `applyTemporalEvidence`, and the engine link writers refresh touched temporal keys; the extract phase sweeps lagging or orphaned state. Graph ops read live relationships by default; default reads keep the established row shape, history reads (`status`, `as_of`, `during`) return `status`/`stints`/`recorded_at`/`retired_at`; hidden rows emit `former_relationships_hidden` + `gbrain.temporal` meta. `graph.edge_validity off` is a read policy only. Pinned by `test/link-validity.test.ts`, `test/link-temporal-evidence.test.ts`, `test/link-relationships.test.ts` (+ Postgres twin), `test/link-temporal-put-page.test.ts`, `test/link-temporal-reads.test.ts`.++- `src/core/cycle/edge-contradictions.ts` + `src/commands/edge-proposals.ts` — the `edge_contradictions` dream phase and `gbrain edge-proposals`. Candidates are subjects with ≥2 live same-type state relationships whose pair evidence hash has no proposal; one judge call per subject returns only conflict flags; `closeContradiction` picks the ending relationship and date (undated → `undated_unresolved`). Modes propose (default) / apply (certified models in `CERTIFIED_APPLY_MODELS`) / off; applying appends a `gbrain-dream (inferred)` "Ended <type> [[target]]" timeline line through the coordinated `add_timeline_entry` path and re-derives the page; a hand-deleted line becomes `reverted_by_user`. Budget through the cycle `BudgetMeter`. Pinned by `test/cycle-edge-contradictions.test.ts` and the managed phase matrix.diff --git a/docs/guides/temporal-edges.md b/docs/guides/temporal-edges.mdnew file mode 100644index 0000000000..2a3ce86c98--- /dev/null+++ b/docs/guides/temporal-edges.md@@ -0,0 +1,140 @@+# Temporal edges: relationships with dates++GBrain records when a relationship between two pages started and ended, and+graph reads return what is true today unless you ask for history. "Who works at+acme-example?" lists current employees; former employees stay one parameter+away.++**Say to your agent:**+- *"Who works at acme-example now?"*+- *"Where did alice-example work in 2022?"*+- *"Show me former employees of acme-example."*+- *"Mark that alice-example left acme-example on 2025-03-01."*++## What carries a date++Relationship types fall into three groups:++| Group | Types | Behavior |+|---|---|---|+| State | `works_at`, `advises`, `yc_partner` | starts and can end; may have several stints (left and rejoined) |+| Event | `founded`, `invested_in`, `led_round`, `attended`, `discussed_in`, `cited` | happened on a date and stays true; hidden in as-of reads before that date |+| Reference | `mentions`, `source`, `owes_to`, `awaiting_reply_from`, untyped links, … | no temporal state; always returned |++Open-loop edges (`owes_to`, `awaiting_reply_from`) keep their own lifecycle in+open loops.++## How dates get recorded (no model calls)++Every write re-reads the page and records dated evidence for the relationships+the page states:++- **Timeline lines with a cue.** `- **2025-03-01** | linkedin — Left [Acme](../companies/acme-example) to join [Widget](../companies/widget-co)`+ ends `works_at acme-example` and starts `works_at widget-co` on that date. Cues are+ relation-specific: leaving a job does not end an advisory role. A cue only dates a+ relationship the page already states; it never creates one.+- **The explicit line grammar**, inside a dated timeline entry. It names the relation+ and the target, so it works on its own, even after the old sentence is deleted:+ ```+ - **2025-03-01** | me — Ended works_at [[companies/acme-example]]+ - **2021-04-01** | me — Started advises [[companies/widget-co]]+ ```+- **Frontmatter `since` / `until`** on relationship objects:+ `company: [{ name: Acme, since: 2021-04, until: 2024-02-15 }]` (partial dates+ normalize to the first day of the month or year).+- **Past-tense prose.** "previously at", "former CTO of", "used to work at" mark the+ page's assertion as past. With no dated end, the relationship reads as ended at an+ unknown date. On one page, a present-tense mention wins ("previously at Acme, now+ runs Acme's EU team" stays live).+- **Manual edges:** `add_link` accepts `valid_from` and `valid_until` (YYYY-MM-DD).+ Re-run `add_link` with `valid_until` to record that a relationship ended.++Dated evidence beats undated evidence from any page: a closure on a person's timeline+ends the relationship even if the company page still lists them under `key_people`.++## Reading++| Parameter | Meaning |+|---|---|+| (none) | relationships true today (UTC) |+| `status: "all"` | every relationship, each with `status`, `stints`, `recorded_at`, `retired_at` |+| `status: "ended"` | former relationships only (`get_links`) |+| `as_of: "2022-06-30"` | what was true on that day |+| `during: "2022"` | true at any point in a period (`2022`, `2022-03`, `2021..2023-06`) |++`get_links` takes all of them plus `link_type`; `get_backlinks` and `traverse_graph`+take `status` (`live`, `all`) and `as_of`. When a default read leaves relationships+out, the response carries a `former_relationships_hidden` notice with the exact call+that shows them, and `gbrain.temporal` response metadata with the count.++Recipe for "where did alice-example work in 2022":++```+get_links { slug: "people/alice-example", link_type: "works_at", during: "2022" }+```++Statuses in history reads:++| Status | Meaning |+|---|---|+| `live` | true at the reference date |+| `ended` | ended on a recorded date at or before the reference date |+| `ended_unknown_date` | stated as over, with no end date (excluded from as-of answers) |+| `not_started` | starts after the reference date |+| `disputed` | one page says it is current, another says it is over, no dates; returned live today and excluded from as-of answers |+| `event` | an event that has happened |+| `reference` | a plain reference with no temporal state |++The relational search arm follows the question's tense: "who works at" reads live+relationships, "who worked at" / "used to" reads history, "former employees of" reads+ended ones. Entity cards and `context_pack` keep every edge with its status and add a+relationship note, for example+`now: works_at widget-co (since 2025-03-01); ended: works_at acme-example (2025-03-01); summary may be stale: it still names acme-example`.++## The nightly relationship check++The dream cycle's `edge_contradictions` phase looks at subjects with two or more live+relationships of the same state type (two current employers). A chat model judges only+whether they can both hold now; date arithmetic decides which one ended and when: the+relationship that started earlier ends on the date the other started. Relationships+without a dated start are never closed; the proposal asks for a date instead.++| Setting | Default |+|---|---|+| `dream.edge_contradictions.mode` | `propose` with a chat model; `apply` only for models that passed the held-out certification; `off` without a chat model |+| `models.dream.edge_contradictions` | utility tier |+| `dream.edge_contradictions.max_subjects` | 200 per cycle |+| `dream.edge_contradictions.max_usd` | $1.00 per cycle |++Proposals:++```+gbrain edge-proposals list # open proposals and undated pairs+gbrain edge-proposals accept 12 # writes the closure line, re-derives the page+gbrain edge-proposals reject 12+gbrain edge-proposals undo 12 # removes the line it wrote+gbrain edge-proposals undo --all-applied+gbrain edge-proposals date 14 2024-05-01 # record when the newer relationship started+```++An applied proposal is one timeline line on the subject page:++```+- **2024-05-01** | gbrain-dream (inferred) — Ended works_at [[companies/acme-example]] (superseded by works_at companies/widget-co)+```++Delete the line to reopen the relationship; the check records that and does not+propose it again until the evidence changes.++## Turning it off++- `gbrain config set graph.edge_validity off`: graph reads return every edge, as+ before. Dated evidence keeps being recorded, so turning it back on loses nothing.+- `gbrain config set dream.edge_contradictions.mode off`: no relationship checks.+ Lines it already wrote stay until `gbrain edge-proposals undo --all-applied`.++## Health++`gbrain doctor --only edge_validity` reports relationships by status, relationships+whose state lags their evidence (the extract cycle sweeps them), pages that still+state a relationship their own timeline ended, and open proposals.diff --git a/docs/guides/troubleshooting.md b/docs/guides/troubleshooting.mdindex 481549773a..23d0cb16eb 100644--- a/docs/guides/troubleshooting.md+++ b/docs/guides/troubleshooting.md@@ -13,6 +13,7 @@| Symptom | Next step | Who acts | Consent | Verify ||---|---|---|---|---|| A gbrain call failed with a code you don't recognize | follow `fix.next` ([protocol](../protocol/AGENT_OPERATOR_v1.md)); `gbrain errors <code>` | agent | as the fix's `consent` | the fix's `verify` |+| A graph read omits a relationship you know existed, or says someone still works somewhere they left | the default read returns relationships true today: repeat with `status: "all"` or `as_of`; to record an end, add a dated timeline line (`Ended works_at [[companies/x]]`) or `add_link ... valid_until` ([temporal edges](temporal-edges.md)) | agent | none | `gbrain doctor --only edge_validity --json` || A command exited 3 (`confirmation_required`) | relay `user_message`; run `fix.command` only after the user agrees | agent, after the user agrees | the payload's `effects` | the fix's `verify` || [Database unreachable or `GBRAIN_DB_ACCESS <reason>`](#database-unreachable) | `gbrain engine status --probe`, then `gbrain db-repair` (diagnose); `gbrain db-repair --yes` applies safe fixes | agent; `--yes` after the user agrees | none to diagnose; `--apply-rewrites` rewrites the config URL (undoable) | `gbrain engine status --probe --json` || [PGLite `RuntimeError: Aborted()` at startup](#pglite-aborted) | automatic repair on the next command; else `gbrain pglite-repair --dry-run`, then `gbrain pglite-repair --yes` | agent; `--yes` after the user agrees | `destructive` (a WAL backup is kept and the restore command printed) | `gbrain doctor --only connection --json` |diff --git a/docs/protocol/MEMORY_VERBS_v1.md b/docs/protocol/MEMORY_VERBS_v1.mdindex 548221f732..a96efd0d8c 100644--- a/docs/protocol/MEMORY_VERBS_v1.md+++ b/docs/protocol/MEMORY_VERBS_v1.md@@ -325,7 +325,7 @@ pending-decision loops — the ADDITIVE-FOREVER optional fields disambiguate:All five are absent on threads not backed by a loop row and on servers thatdo not implement them; a server that omits them still certifies. Same propagation to theper-entity cards and top-level `open_threads` of `context_pack`.-- `edges`: top ~10 typed edges, mentions excluded, out-edges first.+- `edges`: top ~10 typed edges, mentions excluded, out-edges first, live relationships first. Additive fields: `status` (`live`, `ended`, `ended_unknown_date`, `event`, …), `since` / `until` (latest stint). `relationship_note` (additive) summarizes current and ended relationships and flags a summary that still names an ended one ([temporal edges](../guides/temporal-edges.md)).- The p99 < 100ms promise is op-layer latency (transport excluded), CI-gatedon a 20K-page corpus. 200K validation recipe below.diff --git a/llms-full.txt b/llms-full.txtindex edf49a510b..ac1c1619b9 100644--- a/llms-full.txt+++ b/llms-full.txt@@ -8872,7 +8872,7 @@ pending-decision loops — the ADDITIVE-FOREVER optional fields disambiguate:All five are absent on threads not backed by a loop row and on servers thatdo not implement them; a server that omits them still certifies. Same propagation to theper-entity cards and top-level `open_threads` of `context_pack`.-- `edges`: top ~10 typed edges, mentions excluded, out-edges first.+- `edges`: top ~10 typed edges, mentions excluded, out-edges first, live relationships first. Additive fields: `status` (`live`, `ended`, `ended_unknown_date`, `event`, …), `since` / `until` (latest stint). `relationship_note` (additive) summarizes current and ended relationships and flags a summary that still names an ended one ([temporal edges](../guides/temporal-edges.md)).- The p99 < 100ms promise is op-layer latency (transport excluded), CI-gatedon a 20K-page corpus. 200K validation recipe below.@@ -9650,6 +9650,7 @@ Source: https://raw.githubusercontent.com/garrytan/gbrain/master/docs/guides/tro| Symptom | Next step | Who acts | Consent | Verify ||---|---|---|---|---|| A gbrain call failed with a code you don't recognize | follow `fix.next` ([protocol](../protocol/AGENT_OPERATOR_v1.md)); `gbrain errors <code>` | agent | as the fix's `consent` | the fix's `verify` |+| A graph read omits a relationship you know existed, or says someone still works somewhere they left | the default read returns relationships true today: repeat with `status: "all"` or `as_of`; to record an end, add a dated timeline line (`Ended works_at [[companies/x]]`) or `add_link ... valid_until` ([temporal edges](temporal-edges.md)) | agent | none | `gbrain doctor --only edge_validity --json` || A command exited 3 (`confirmation_required`) | relay `user_message`; run `fix.command` only after the user agrees | agent, after the user agrees | the payload's `effects` | the fix's `verify` || [Database unreachable or `GBRAIN_DB_ACCESS <reason>`](#database-unreachable) | `gbrain engine status --probe`, then `gbrain db-repair` (diagnose); `gbrain db-repair --yes` applies safe fixes | agent; `--yes` after the user agrees | none to diagnose; `--apply-rewrites` rewrites the config URL (undoable) | `gbrain engine status --probe --json` || [PGLite `RuntimeError: Aborted()` at startup](#pglite-aborted) | automatic repair on the next command; else `gbrain pglite-repair --dry-run`, then `gbrain pglite-repair --yes` | agent; `--yes` after the user agrees | `destructive` (a WAL backup is kept and the restore command printed) | `gbrain doctor --only connection --json` |