Skip to content

Commit 26308d1

Browse files
authored
Merge pull request #9 from codeasier/fix/hook-truncation
fix(hooks): stop the post-tool-use wire bound from collapsing escape-heavy outputs
2 parents df415c8 + e68983f commit 26308d1

5 files changed

Lines changed: 285 additions & 86 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1212

1313
### Fixed
1414

15+
- **The post-tool-use hook no longer collapses escape-heavy tool outputs to `{}`.** The client-side 32k wire bound sliced strings by raw character count, ignoring that JSON escaping (quotes, backslashes, control characters) can double or triple the serialized form. An oversized truncated string then failed the object accounting's cost check, and the loop's `break` discarded every remaining field — outputs like `{stdout: "<quote-heavy json>", file_path: ..., exit_code: 0}` arrived as `{}`, losing exactly the small fields the structure-preserving bound exists to keep. The hook's truncation (now in `src/hooks/truncate.ts`, shared by the built copies in `dist/hooks/` and `plugin/scripts/`) mirrors the server's `boundValue`: strings are cut by binary search on serialized length, object fields are taken smallest-first so a huge `stdout` cannot crowd out `file_path`/`exit_code`, and a single unfittable entry is skipped instead of discarding the rest. Field order of over-budget objects changes in transit only; the server re-bounds and re-orders anyway.
1516
- **Fresh installs are portable and persistent (aligned with upstream #892).** The bundled `iii-config.yaml` uses cwd-relative paths and the engine was spawned without a cwd, so on global and npx installs `./data/state_store.db` and `./data/stream_store` landed in whatever directory the user ran the CLI from, and the `iii-exec` supervision block never resolved — nothing respawned the worker after it died, which surfaced as "all data gone" reports against a live REST port. `agentmemory start` now regenerates a runtime config each boot (`~/.agentmemory/data/iii-config.runtime.yaml`) with absolute data paths and an absolute worker exec line, anchors the engine cwd at `~/.agentmemory`, and copies any legacy `./data` stores from the invocation directory on first run. Repo checkouts keep the cwd config and repo-root cwd, so dev behavior is unchanged; user overrides via env or `~/.agentmemory/iii-config.yaml` are passed through verbatim. The viewer port is now configurable (`III_VIEWER_PORT`, default REST + 2), worker pidfile paths honor `AGENTMEMORY_RUNTIME_DIR`, Docker installs gain uid/gid/chown controls, and `agentmemory remove` covers the generated runtime config. `agentmemory doctor` and startup-failure reporting were expanded accordingly.
1617
- **Observation capture no longer grows the persisted live-stream store.** Raw and compressed observation events use `stream::send` instead of `stream::set`; the viewer still receives live updates and REST remains the history source, but `data/stream_store` no longer accumulates a write-only second and third copy of every observation.
1718
- **Server-side capture payloads are bounded without flattening structured tool data.** `tool_input`, `tool_output`, prompts and the raw envelope are trimmed to configurable character budgets (`AGENTMEMORY_OBSERVE_*`); object fields such as `file_path` survive truncation, `0` disables an individual bound, and positive budgets below 2 are clamped to the smallest serialized string.

‎plugin/scripts/post-tool-use.mjs‎

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,62 @@ function hookCwd(data) {
3131
if (projectDir && projectDir.trim()) return projectDir;
3232
}
3333
//#endregion
34+
//#region src/hooks/truncate.ts
35+
const TRUNCATION_MARKER = "...[truncated]";
36+
function serializedLength(value) {
37+
return JSON.stringify(value)?.length ?? 0;
38+
}
39+
function truncate(value, max) {
40+
if (typeof value === "string") {
41+
if (serializedLength(value) <= max) return value;
42+
let keep = 0;
43+
let hi = value.length;
44+
while (keep < hi) {
45+
const mid = Math.ceil((keep + hi) / 2);
46+
if (serializedLength(value.slice(0, mid) + "...[truncated]") <= max) keep = mid;
47+
else hi = mid - 1;
48+
}
49+
return value.slice(0, keep) + TRUNCATION_MARKER;
50+
}
51+
if (Array.isArray(value)) {
52+
if (serializedLength(value) <= max) return value;
53+
const bounded = [];
54+
let remaining = Math.max(0, max - 2);
55+
for (const item of value) {
56+
const comma = bounded.length > 0 ? 1 : 0;
57+
if (remaining <= comma) break;
58+
const next = truncate(item, remaining - comma);
59+
const cost = comma + serializedLength(next);
60+
if (cost > remaining) continue;
61+
bounded.push(next);
62+
remaining -= cost;
63+
}
64+
return bounded;
65+
}
66+
if (typeof value === "object" && value !== null) {
67+
if (serializedLength(value) <= max) return value;
68+
const bounded = {};
69+
let remaining = Math.max(0, max - 2);
70+
const sized = Object.entries(value).map((entry) => ({
71+
entry,
72+
size: serializedLength(entry[1])
73+
}));
74+
sized.sort((a, b) => a.size - b.size);
75+
for (const { entry } of sized) {
76+
const [key, item] = entry;
77+
const overhead = (Object.keys(bounded).length > 0 ? 1 : 0) + JSON.stringify(key).length + 1;
78+
if (remaining <= overhead) continue;
79+
const next = truncate(item, remaining - overhead);
80+
const cost = overhead + serializedLength(next);
81+
if (cost > remaining) continue;
82+
bounded[key] = next;
83+
remaining -= cost;
84+
}
85+
return bounded;
86+
}
87+
return value;
88+
}
89+
//#endregion
3490
//#region src/hooks/post-tool-use.ts
3591
function isSdkChildContext(payload) {
3692
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
@@ -116,44 +172,6 @@ function extractImageData(output) {
116172
cleanOutput: output
117173
};
118174
}
119-
const TRUNCATION_MARKER = "...[truncated]";
120-
function truncate(value, max) {
121-
if (typeof value === "string") {
122-
if (value.length + 14 <= max) return value;
123-
return value.slice(0, Math.max(0, max - 14)) + TRUNCATION_MARKER;
124-
}
125-
if (Array.isArray(value)) {
126-
if (JSON.stringify(value).length <= max) return value;
127-
const bounded = [];
128-
let remaining = Math.max(0, max - 2);
129-
for (const item of value) {
130-
const comma = bounded.length > 0 ? 1 : 0;
131-
if (remaining <= comma) break;
132-
const next = truncate(item, remaining - comma);
133-
const cost = comma + JSON.stringify(next).length;
134-
if (cost > remaining) break;
135-
bounded.push(next);
136-
remaining -= cost;
137-
}
138-
return bounded;
139-
}
140-
if (typeof value === "object" && value !== null) {
141-
if (JSON.stringify(value).length <= max) return value;
142-
const bounded = {};
143-
let remaining = Math.max(0, max - 2);
144-
for (const [key, item] of Object.entries(value)) {
145-
const overhead = (Object.keys(bounded).length > 0 ? 1 : 0) + JSON.stringify(key).length + 1;
146-
if (remaining <= overhead) break;
147-
const next = truncate(item, remaining - overhead);
148-
const cost = overhead + JSON.stringify(next).length;
149-
if (cost > remaining) break;
150-
bounded[key] = next;
151-
remaining -= cost;
152-
}
153-
return bounded;
154-
}
155-
return value;
156-
}
157175
main().catch(() => process.exit(0));
158176
//#endregion
159177
export {};

‎src/hooks/post-tool-use.ts‎

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22
import { resolveProject, hookCwd } from "./_project.js";
3+
import { truncate } from "./truncate.js";
34

45
function isSdkChildContext(payload: unknown): boolean {
56
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
@@ -105,52 +106,4 @@ function extractImageData(output: unknown): { imageData: string | undefined; cle
105106
return { imageData: undefined, cleanOutput: output };
106107
}
107108

108-
const TRUNCATION_MARKER = "...[truncated]";
109-
110-
// Structure-preserving client-side bound. A flat JSON.stringify+slice kept
111-
// the wire small but destroyed object shape for outputs over the cap — the
112-
// largest payloads lost the very fields (file_path etc.) the server's
113-
// structure-preserving truncation exists to keep. Walk the value and keep as
114-
// many whole entries as fit the budget; strings get sliced. The server bound
115-
// (AGENTMEMORY_OBSERVE_TOOL_OUTPUT_CHARS) remains authoritative — this cap
116-
// only limits wire size and ingress parse memory.
117-
function truncate(value: unknown, max: number): unknown {
118-
if (typeof value === "string") {
119-
if (value.length + TRUNCATION_MARKER.length <= max) return value;
120-
return value.slice(0, Math.max(0, max - TRUNCATION_MARKER.length)) + TRUNCATION_MARKER;
121-
}
122-
if (Array.isArray(value)) {
123-
if (JSON.stringify(value).length <= max) return value;
124-
const bounded: unknown[] = [];
125-
let remaining = Math.max(0, max - 2);
126-
for (const item of value) {
127-
const comma = bounded.length > 0 ? 1 : 0;
128-
if (remaining <= comma) break;
129-
const next = truncate(item, remaining - comma);
130-
const cost = comma + JSON.stringify(next).length;
131-
if (cost > remaining) break;
132-
bounded.push(next);
133-
remaining -= cost;
134-
}
135-
return bounded;
136-
}
137-
if (typeof value === "object" && value !== null) {
138-
if (JSON.stringify(value).length <= max) return value;
139-
const bounded: Record<string, unknown> = {};
140-
let remaining = Math.max(0, max - 2);
141-
for (const [key, item] of Object.entries(value as Record<string, unknown>)) {
142-
const overhead =
143-
(Object.keys(bounded).length > 0 ? 1 : 0) + JSON.stringify(key).length + 1;
144-
if (remaining <= overhead) break;
145-
const next = truncate(item, remaining - overhead);
146-
const cost = overhead + JSON.stringify(next).length;
147-
if (cost > remaining) break;
148-
bounded[key] = next;
149-
remaining -= cost;
150-
}
151-
return bounded;
152-
}
153-
return value;
154-
}
155-
156109
main().catch(() => process.exit(0));

‎src/hooks/truncate.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Structure-preserving client-side bound for hook payloads. A flat
2+
// JSON.stringify+slice kept the wire small but destroyed object shape for
3+
// outputs over the cap — the largest payloads lost the very fields
4+
// (file_path etc.) the server's structure-preserving truncation exists to
5+
// keep. Walk the value and keep as many whole entries as fit the budget;
6+
// strings get sliced. The server bound
7+
// (AGENTMEMORY_OBSERVE_TOOL_OUTPUT_CHARS) remains authoritative — this cap
8+
// only limits wire size and ingress parse memory.
9+
10+
export const TRUNCATION_MARKER = "...[truncated]";
11+
12+
function serializedLength(value: unknown): number {
13+
return JSON.stringify(value)?.length ?? 0;
14+
}
15+
16+
export function truncate(value: unknown, max: number): unknown {
17+
if (typeof value === "string") {
18+
if (serializedLength(value) <= max) return value;
19+
// Escape expansion (quotes, backslashes, control characters) makes the
20+
// serialized form up to six times the raw character count, so slicing by
21+
// character count alone overshoots the budget — and inside an object that
22+
// overshoot made the caller's cost check reject the field and drop every
23+
// field after it, so escape-heavy tool outputs arrived as {}. Find the
24+
// longest prefix whose serialized form still fits by binary search, the
25+
// same way the server's boundString does.
26+
let keep = 0;
27+
let hi = value.length;
28+
while (keep < hi) {
29+
const mid = Math.ceil((keep + hi) / 2);
30+
const candidate = value.slice(0, mid) + TRUNCATION_MARKER;
31+
if (serializedLength(candidate) <= max) keep = mid;
32+
else hi = mid - 1;
33+
}
34+
return value.slice(0, keep) + TRUNCATION_MARKER;
35+
}
36+
if (Array.isArray(value)) {
37+
if (serializedLength(value) <= max) return value;
38+
const bounded: unknown[] = [];
39+
let remaining = Math.max(0, max - 2);
40+
for (const item of value) {
41+
const comma = bounded.length > 0 ? 1 : 0;
42+
if (remaining <= comma) break;
43+
const next = truncate(item, remaining - comma);
44+
const cost = comma + serializedLength(next);
45+
// Skip, not stop: one entry that cannot fit (long primitives, a
46+
// string against a sub-budget smaller than the marker) must not
47+
// discard later entries that still fit.
48+
if (cost > remaining) continue;
49+
bounded.push(next);
50+
remaining -= cost;
51+
}
52+
return bounded;
53+
}
54+
if (typeof value === "object" && value !== null) {
55+
if (serializedLength(value) <= max) return value;
56+
const bounded: Record<string, unknown> = {};
57+
let remaining = Math.max(0, max - 2);
58+
// Smallest fields first, mirroring the server's boundValue: a huge
59+
// stdout must not crowd out file_path / exit_code when the budget runs
60+
// out. Field order is not semantic for the wire — the server re-bounds
61+
// and re-orders anyway.
62+
const sized = Object.entries(value as Record<string, unknown>).map(
63+
(entry) => ({ entry, size: serializedLength(entry[1]) }),
64+
);
65+
sized.sort((a, b) => a.size - b.size);
66+
for (const { entry } of sized) {
67+
const [key, item] = entry;
68+
const overhead =
69+
(Object.keys(bounded).length > 0 ? 1 : 0) + JSON.stringify(key).length + 1;
70+
// Long keys can outlive the budget while later short keys still fit.
71+
if (remaining <= overhead) continue;
72+
const next = truncate(item, remaining - overhead);
73+
const cost = overhead + serializedLength(next);
74+
if (cost > remaining) continue;
75+
bounded[key] = next;
76+
remaining -= cost;
77+
}
78+
return bounded;
79+
}
80+
return value;
81+
}

0 commit comments

Comments
 (0)