Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

GST-359 D: memory_recall drops the '… N more' marker line; QM reads every line as a fact (contract item 15), truncation rides structuredContent.truncated

garrytan35bac8b13a80
diff --git a/docs/contracts/qm-memory-provider/v1.json b/docs/contracts/qm-memory-provider/v1.json
index 3c1827dffd..3051fda069 100644
--- a/docs/contracts/qm-memory-provider/v1.json
+++ b/docs/contracts/qm-memory-provider/v1.json
@@ -135,14 +135,13 @@
"arguments_checked": "query and max_chars are checked before any read, also for an unlinked person (invalid_params)",
"content": {
"line": "- <fact> (<YYYY-MM-DD>; <kind>)",
- "truncated_tail": "… <N> more",
"never": [
"page bodies",
"fence markers",
"heading markers",
"line breaks or control characters inside a fact"
],
- "bounded_by": "max_chars, cut at a line boundary"
+ "bounded_by": "max_chars: as many whole lines as fit; facts left out set structuredContent.truncated, with no marker line, because every line is one fact"
},
"order": [
"own facts from the last 24 hours, newest first",
diff --git a/docs/guides/qm.md b/docs/guides/qm.md
index d436171006..ea0cd0cce1 100644
--- a/docs/guides/qm.md
+++ b/docs/guides/qm.md
@@ -26,7 +26,8 @@ a repo exists, saved facts go through the same git-first writer door as `remembe
- `memory_recall {query, max_chars?}` answers plain text, one fact per line
(`- <fact> (<date>; <kind>)`): the person's own facts from the last 24 hours
first, then the facts they may read ranked by `query`. Never page bodies. The text
- is cut at a line with a final `… N more` line and never exceeds `max_chars`
+ holds as many whole lines as fit, with no marker line (every line is one fact;
+ `structuredContent.truncated` says some were left out), and never exceeds `max_chars`
(a whole number of UTF-16 code units, 200-12000, default 6000). Each fact is
one line: line breaks and control characters in it become spaces. Ranking is
keyword-only on a keyless brain.
diff --git a/scripts/build-qm-memory-contract.ts b/scripts/build-qm-memory-contract.ts
index 6941fcef4e..4229b61eb2 100644
--- a/scripts/build-qm-memory-contract.ts
+++ b/scripts/build-qm-memory-contract.ts
@@ -66,7 +66,7 @@ export function buildQmMemoryContract(): Record<string, unknown> {
memory_recall: {
limits: { max_chars: { ...MEMORY_RECALL_MAX_CHARS, whole_number: true, unit: 'UTF-16 code units (JavaScript string length)' }, query_chars_ranked: MEMORY_RECALL_QUERY_CHARS, own_recent_window_hours: OWN_RECENT_WINDOW_HOURS },
arguments_checked: 'query and max_chars are checked before any read, also for an unlinked person (invalid_params)',
- content: { line: '- <fact> (<YYYY-MM-DD>; <kind>)', truncated_tail: '… <N> more', never: ['page bodies', 'fence markers', 'heading markers', 'line breaks or control characters inside a fact'], bounded_by: 'max_chars, cut at a line boundary' },
+ content: { line: '- <fact> (<YYYY-MM-DD>; <kind>)', never: ['page bodies', 'fence markers', 'heading markers', 'line breaks or control characters inside a fact'], bounded_by: 'max_chars: as many whole lines as fit; facts left out set structuredContent.truncated, with no marker line, because every line is one fact' },
order: ['own facts from the last ' + OWN_RECENT_WINDOW_HOURS + ' hours, newest first', 'facts the person may read, ranked by query'],
ranking: 'keyword-only unless an embedding provider is configured (no embedding call on a keyless brain)',
structured_content: { facts: [{ id: 'string', text: 'string', date: 'YYYY-MM-DD', kind: 'string' }], truncated: 'boolean', code: 'actor_unlinked (only on unlinked_recall)' },
diff --git a/src/core/ops/qm-memory.ts b/src/core/ops/qm-memory.ts
index 9d629b7662..0fe2d209f0 100644
--- a/src/core/ops/qm-memory.ts
+++ b/src/core/ops/qm-memory.ts
@@ -4,8 +4,9 @@
*
* - memory_recall: plain-text facts, one per line (`- <fact> (<date>; <kind>)`), never page bodies. The caller's own
* facts from the last 24 hours on its member page come first, then the facts it may read ranked by the query
- * (keyword-only unless an embedding provider is configured), deduplicated by fact id, cut at a line boundary with a
- * final `… N more` line so the text never exceeds max_chars. Fence and heading markers are stripped from fact text.
+ * (keyword-only unless an embedding provider is configured), deduplicated by fact id, as many whole lines as fit in
+ * max_chars and no marker line (QM reads every line as a fact; `truncated` says some were left out). Fence and
+ * heading markers are stripped from fact text.
* - memory_write: 1-20 facts saved through remember's persistence path (the same writer door, git-first where a repo
* exists) onto the attested member's page, readable by that member alone, with provenance `qm:<actor id>; <caller's>`.
* Each fact's request id derives from uuidv5(member, idempotency_key), so a key never crosses members and a replay
@@ -60,20 +61,17 @@ export function plainFactText(text: string): string {
.trim();
}
-/** The lines that fit in maxChars: whole lines, then `… N more` when any were left out; never longer than maxChars. */
+/** The whole lines that fit in maxChars, in order, with no marker line: the first line that would cross the cap ends the list. */
export function boundFactLines(lines: readonly string[], maxChars: number): { kept: number; text: string } {
- const full = lines.join('\n');
- if (full.length <= maxChars) return { kept: lines.length, text: full };
let kept = 0;
let used = 0;
while (kept < lines.length) {
const next = used + (kept ? 1 : 0) + lines[kept]!.length;
- if (next + 1 + `… ${lines.length - kept - 1} more`.length > maxChars) break;
+ if (next > maxChars) break;
used = next;
kept++;
}
- const tail = `… ${lines.length - kept} more`;
- return { kept, text: kept ? `${lines.slice(0, kept).join('\n')}\n${tail}` : tail };
+ return { kept, text: lines.slice(0, kept).join('\n') };
}
/** YYYY-MM-DD of a stored time, read from its own fields: a database text value keeps its date as written (a non-ISO
diff --git a/test/qm-memory-tools.test.ts b/test/qm-memory-tools.test.ts
index 57d3908b9f..f0cd0cc506 100644
--- a/test/qm-memory-tools.test.ts
+++ b/test/qm-memory-tools.test.ts
@@ -6,8 +6,8 @@
* with request ids from uuidv5(member, idempotency_key): a replay returns the original receipt, the same key from
* two members is two writes, and a write still publishing answers pending with a pollable request_id.
* memory_recall answers the person's own recent facts first, then the facts they may read ranked by the query,
- * one plain line per fact (fence and heading markers stripped, never page bodies), cut at a line boundary with a
- * final "… N more" line and never longer than max_chars (200-12000, default 6000). Success codes ride
+ * one plain line per fact (fence and heading markers stripped, never page bodies), as many whole lines as fit and
+ * no marker line (QM reads every line as a fact), never longer than max_chars (200-12000, default 6000). Success codes ride
* structuredContent only; an unlinked actor gets an empty success with code actor_unlinked.
* 2. Fails when: a member write lands on a shared page or as a world row, a key replays across members, a write runs
* without an attested person, recall leaks another member's fact or a page body, own recent facts do not lead,
@@ -62,24 +62,21 @@ const keyedEmbeddings = (onCall?: () => void) => {
describe('memory_recall text bounds (pure)', () => {
const lines = Array.from({ length: 40 }, (_, i) => '- fact number ' + i + ' about the harbor ledger (2026-10-30; fact)');
- test('whole lines, then one "… N more" line, never past the cap, at every cap from the minimum up', () => {
+ test('only whole fact lines, no marker line, never past the cap, at every cap from the minimum up', () => {
for (let max = MEMORY_RECALL_MAX_CHARS.min; max < lines.join('\n').length; max += 7) {
const { kept, text } = boundFactLines(lines, max);
expect(text.length).toBeLessThanOrEqual(max);
- const out = text.split('\n');
- expect(out.slice(0, kept)).toEqual(lines.slice(0, kept));
+ expect(kept).toBeGreaterThan(0);
expect(kept).toBeLessThan(lines.length);
- expect(out).toHaveLength(kept + 1);
- expect(out[kept]).toBe('… ' + (lines.length - kept) + ' more');
- const oneMore = lines.slice(0, kept + 1).join('\n') + '\n… ' + (lines.length - kept - 1) + ' more';
- expect(oneMore.length).toBeGreaterThan(max);
+ expect(text.split('\n')).toEqual(lines.slice(0, kept));
+ expect(lines.slice(0, kept + 1).join('\n').length).toBeGreaterThan(max);
}
});
test('a list that fits is returned whole with no tail; an empty list is empty text', () => {
expect(boundFactLines(lines.slice(0, 3), 6000)).toEqual({ kept: 3, text: lines.slice(0, 3).join('\n') });
expect(boundFactLines([], 200)).toEqual({ kept: 0, text: '' });
- expect(boundFactLines(['x'.repeat(500)], 200)).toEqual({ kept: 0, text: '… 1 more' });
+ expect(boundFactLines(['x'.repeat(500)], 200)).toEqual({ kept: 0, text: '' });
});
test('fence, heading and rule markers are stripped and the fact is one line', () => {
@@ -361,11 +358,11 @@ for (const kind of testBackends()) {
expect(out.isError).toBe(false);
expect(out.text.length).toBeLessThanOrEqual(max);
expect(out.structured!.truncated).toBe(true);
- const lines = out.text.split('\n');
+ const lines = out.text ? out.text.split('\n') : [];
const kept = out.structured!.facts.length;
- expect(lines).toHaveLength(kept + 1);
- expect(lines[kept]).toMatch(/^… \d+ more$/);
- for (const line of lines.slice(0, kept)) expect(line).toMatch(/^- Carol harbor .+canarycarol \(\d{4}-\d{2}-\d{2}; fact\)$/);
+ if (max > MEMORY_RECALL_MAX_CHARS.min) expect(kept).toBeGreaterThan(0);
+ expect(lines).toHaveLength(kept);
+ for (const line of lines) expect(line).toMatch(/^- Carol harbor .+canarycarol \(\d{4}-\d{2}-\d{2}; fact\)$/);
}
for (const max_chars of [199, 12_001, 500.5, '600']) {
const out = await recall(carol, 'harbor ledger', { max_chars });