Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Card in-edges: order by link id so the EDGE_CAP cut and repeated reads are stable
garrytan46118d860bd5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d71cc2aa74..b8cc11cc1b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -26,6 +26,7 @@ identifiers and attribution are available in the pre-removal Git revision- **Frontmatter `since`/`until` dates only the exact target when several objects match.** `src/core/link-temporal-evidence.ts` still accepts a slug-prefix name (`Acme` for `acme-example`) when it is the only match, but when several objects in one field match one target only the exact name dates it, so `{name: MSA Example}` no longer dates `msa-example-renewal`.- Tests: `test/amends-link.test.ts` (PGLite) and `test/e2e/amends-link-postgres.test.ts` (Postgres) share `test/helpers/amends-ranking-scenarios.ts`: one final-output invariance row per link-counting consumer, each with a ranking-type control that crosses its threshold; amendments versus `supersedes` through final search scores and labels; frontmatter `since` per exact target; and a frozen-clock check that a future-`since` amends edge stays in `get_backlinks`, `traverse_graph` and card edges. `test/link-temporal-evidence.test.ts` adds the prefix-sharing target pair.- **Document status where agents look (F3): `page_status` on `context_pack` and `get_backlinks` rows, on by default.** A page's own frontmatter `status`, only when set to a string (trimmed, at most 40 characters), now rides `context_pack` cards (the card's page) and their `newer_mentions` rows, and every `get_backlinks` link row (the referring page) and `group: "page"` row, as the additive `page_status` that F1 record rows already carry. Nothing is inferred: no title, subject or first-line heuristics, and a draft is not treated as lesser evidence. The documented convention for a record not in force yet is `status: draft` or `status: unsigned`. Search rows carry the same value as raw `status` (unbounded); a link row's `status` stays the relationship's temporal state, and the `status` filter is unchanged. Private pages (untrusted callers) and quarantined pages (unless the caller may read them) show none; `context_pack` `text` is unchanged. New key `entity.row_page_status` (default `true`); `false` removes the field from these rows only, and record rows (`records[]`, and a record type's `get_backlinks` continuation) keep it. `src/core/mentions/referrers.ts` (`pageStatusValue`, `readPageStatusByRef`), `src/core/ops/backlinks-paged.ts` (`withLinkPageStatus`), `src/core/ops/facts.ts`. Docs: MEMORY_VERBS v1 "page_status on context_pack and get_backlinks rows", `docs/guides/entity-records.md`, `docs/mcp/TOOL_REFERENCE.md`. Tests: two shared scenarios in `test/entity-records.test.ts` and `test/e2e/entity-records-postgres.test.ts` (additive against the key off, bounded and trimmed, hidden pages none, schema-valid; F1 on with F3 off keeps `page_status` on names rows, the full card, `context_pack` records and the record continuation, and on no other row).+- **Card in-edges come back in link order.** The inbound-edge read behind `entity` and `context_pack` cards (`src/core/verbs/entity-card.ts`) had no `ORDER BY`, so which 10 edges survived the cap, and their order, followed the table's physical row order and could change between two identical reads once page or link rows were rewritten. It now orders by link id, like out-edges. Test: `cardInEdgesInLinkOrder` in the shared entity-records scenarios (rewrites the earliest referrers' rows between two reads; failed every run before the fix).- **Evidence delivery (F2 plumbing): long non-conversation pages can come back in a delivery unit, off by default.** New key `search.long_page_unit` (`off` default | `window` | `section` | `page`; `gbrain config set` refuses other values; `section` is the value under test). Under the implied `auto` on `search` and `query`, a hit on a page whose rendered body holds at least `search.long_page_min_chars` characters (new key, default 20,000) is widened to that unit inside the existing `search.return_budget_conversation` budget; every other hit keeps its ranked chunk, reserved first, and widened units fill only what is left. One precedence table (`docs/evidence-delivery.md`, "Long pages"): an explicit `return_unit`, then a non-`auto` `search.return_unit`, then this key; an explicit `token_budget` keeps it off. A widened row is exempt from `snippet_chars` and comes back whole, still inside `search.return_budget_conversation` and the long-page limits; `snippet_chars` keeps cutting every ranked chunk row, and `delivered {unit, reason}` explains the longer row. The policy is resolved at the search/query caller (`withLongPagePolicy`), so recall, think and `assemble_evidence` are byte-identical with it on. `selectLongPages` runs one character/byte-count preflight under the read scope, and delivery confirms the threshold on the rendered body; at most 5 long pages and 1,000,000 body bytes per call are fetched in their own bounded batched call under a 2,000 ms deadline, whole pages only; any long page it cannot deliver keeps its ranked chunk (`delivered.reason: "long_page_chunk"`, a `long_page_*` fallback code). Lean rows keep `delivered: {unit, reason}` on long-page rows (plus `truncated: true` when cut); every other lean row keeps `{truncated: true}` only. The off path is byte-identical (`test/evidence-delivery-golden.test.ts` unchanged); a second frozen golden pins the on path (`test/fixtures/goldens/evidence-delivery/long-page-on-path.json`). Tests: `test/evidence-long-page.test.ts` (PGLite: replay case, threshold key, cap and explicit-unit paths byte-identical with the key on, `snippet_chars` 300 survival of the widened row on the serialized lean MCP output), `test/e2e/evidence-long-page.test.ts` (Postgres). No passage selector is built; the unit is chosen later by the frozen-hit comparison.- **Served instructions (F4): the record in effect on the date asked about governs, not the newest one.** Instruction 7 now reads "The record whose terms are in effect on the date asked about governs: a page's `date`/`effective_date` is when it was written, not when its terms take effect, and a change not yet effective does not apply yet; drafts, proposals and agent-written notes do not override records." (`date` is named only where `entity` is callable). It replaces "prefer the newest governing source: a later correction, handoff or executed change outranks an older record". The rule now leads the answering clause and ends inside the first 2,048 characters capped harnesses read (starter 1,566–1,842, full 1,717–1,993; it started at 1,943 and 2,094). New key `mcp.instructions_governing_rule` (`in_effect` default, `newest` serves the earlier sentence for measurement). Budget: the sentence is 74 characters longer; 11 are paid by cutting "the best-ranked excerpts" to "top excerpts" in the same clause, and +63 is recorded in `test/mcp-schema-budget.test.ts` (5,108 for the maximal rendering). Tests: `test/mcp-instructions-governing-rule.test.ts`.- **Served instructions (d5b): the brief clause no longer recommends a `referenced_by` walk batch mode cannot do.** `src/mcp/instructions.ts`: the walk is on the single-name `entity` card, and the clause names what a compact `entity names` row carries on this brain (its `next` to the card while `entity.names_references` is on, the default; `records` by type once `entity.record_types` enrolls page types, empty by default). Every transport reads both keys at initialize through `readInstructionContent` (fail-closed to the defaults); the static contract, bootstrap's instructions file and the SDK constructor fallback are the defaults rendering. One reader module, `src/core/entity-references-config.ts`, serves both keys. Budget: +70 characters for the maximal rendering (+39 at the default), recorded in `test/mcp-schema-budget.test.ts` and `test/mcp-initialize-instructions.test.ts`. Tests: `test/mcp-instructions-references.test.ts`.diff --git a/src/core/verbs/entity-card.ts b/src/core/verbs/entity-card.tsindex 1757277695..650cc1dce9 100644--- a/src/core/verbs/entity-card.ts+++ b/src/core/verbs/entity-card.ts@@ -492,7 +492,8 @@ async function assembleCard(JOIN pages t ON t.id = l.to_page_id${temporalLinkJoinSql('l', excludePrivate)}WHERE t.slug = $1 AND t.source_id = $2 AND f.source_id = $2 AND f.deleted_at IS NULL- AND COALESCE(l.link_source, '') <> 'mentions'${inboundPrivacy}`,+ AND COALESCE(l.link_source, '') <> 'mentions'${inboundPrivacy}+ ORDER BY l.id`,[pageSlug, sourceId],).then(rs => rs.map(r => annotateTemporalRow(r)))diff --git a/test/e2e/entity-records-postgres.test.ts b/test/e2e/entity-records-postgres.test.tsindex aee1ec67d6..6251ba2039 100644--- a/test/e2e/entity-records-postgres.test.ts+++ b/test/e2e/entity-records-postgres.test.ts@@ -33,4 +33,5 @@ d('entity records (Postgres)', () => {test('F3 page_status on get_backlinks and context_pack rows: additive, bounded, hidden pages none; its key removes it there', () => S.pageStatusOnRows(engine), 60_000);test('F1 on, F3 off: record rows keep page_status on initial and continued outputs; other rows have none', () => S.recordsKeepPageStatusWithRowKeyOff(engine), 60_000);test('amends and supersedes targets outside the caller\'s sources never reach record rows', () => S.recordLinksStayInSources(engine), 60_000);+ test('card in-edges come back in link order, stable when page rows move', () => S.cardInEdgesInLinkOrder(engine), 60_000);});diff --git a/test/entity-records.test.ts b/test/entity-records.test.tsindex f393c611c2..79e8648af0 100644--- a/test/entity-records.test.ts+++ b/test/entity-records.test.ts@@ -33,4 +33,5 @@ describe('entity records (PGLite)', () => {test('F3 page_status on get_backlinks and context_pack rows: additive, bounded, hidden pages none; its key removes it there', () => S.pageStatusOnRows(engine), 60_000);test('F1 on, F3 off: record rows keep page_status on initial and continued outputs; other rows have none', () => S.recordsKeepPageStatusWithRowKeyOff(engine), 60_000);test('amends and supersedes targets outside the caller\'s sources never reach record rows', () => S.recordLinksStayInSources(engine), 60_000);+ test('card in-edges come back in link order, stable when page rows move', () => S.cardInEdgesInLinkOrder(engine), 60_000);});diff --git a/test/helpers/entity-records-scenarios.ts b/test/helpers/entity-records-scenarios.tsindex 57e3333166..7414368b5c 100644--- a/test/helpers/entity-records-scenarios.ts+++ b/test/helpers/entity-records-scenarios.ts@@ -478,3 +478,20 @@ export async function recordLinksStayInSources(engine: BrainEngine): Promise<voiexpect(v2.supersedes).toEqual([{ slug: 'contracts/acme-msa' }]);}}++/** Card in-edges come back in link order (like out-edges), so the EDGE_CAP cut and every repeated read are stable when page rows move on disk. */+export async function cardInEdgesInLinkOrder(engine: BrainEngine): Promise<void> {+ await seedRecordsWorld(engine);+ const inSlugs = async () => ((await call(engine, 'context_pack', { entities: 'crm/acme-example' })).body.cards[0].edges as Array<{ slug: string; direction: string }>)+ .filter(e => e.direction === 'in').map(e => e.slug);+ const linkOrder = (await engine.executeRaw<{ slug: string }>(+ `SELECT f.slug FROM links l JOIN pages f ON f.id = l.from_page_id JOIN pages t ON t.id = l.to_page_id+ WHERE t.slug = 'crm/acme-example' AND t.source_id = $1 AND f.slug <> 'contracts/acme-private' ORDER BY l.id`, [SRC])).map(r => r.slug);+ const before = await inSlugs();+ expect(before).toEqual(linkOrder.slice(0, before.length));+ // Rewrite the earliest referrers' rows so they move to the end of the heap; the card must not reorder.+ await engine.executeRaw(`UPDATE pages SET title = title WHERE source_id = $1 AND slug IN ('contracts/acme-msa', 'contracts/acme-amend-1')`, [SRC]);+ await engine.executeRaw(`UPDATE links SET context = context WHERE from_page_id IN (SELECT id FROM pages WHERE source_id = $1 AND slug IN ('contracts/acme-msa', 'contracts/acme-amend-1'))`, [SRC]);+ expect(await inSlugs()).toEqual(before);+}+