A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, performance] perf(coding-agent): fingerprint a bounded turn-body subset for semantic-edge retry identity (#2476)
Sebastian Müller514b4b871476
diff --git a/packages/coding-agent/.changes/edge-hash-subset.md b/packages/coding-agent/.changes/edge-hash-subset.mdnew file mode 100644index 0000000000..10e0511e2d--- /dev/null+++ b/packages/coding-agent/.changes/edge-hash-subset.md@@ -0,0 +1 @@+- Reduced per-model-request CPU cost by fingerprinting a bounded turn-body subset (model identity, shaping options, system prompt, message count, last message, tool schemas) instead of re-serializing the full request body, with the tool-schema digest memoized per session.diff --git a/packages/coding-agent/src/core/semantic-edges.ts b/packages/coding-agent/src/core/semantic-edges.tsindex 198ea40721..48d50b318d 100644--- a/packages/coding-agent/src/core/semantic-edges.ts+++ b/packages/coding-agent/src/core/semantic-edges.ts@@ -114,7 +114,26 @@ function mintId(): string {return randomUUID().replaceAll("-", "");}-/** Content hash of one turn call body; Idempotency-Key reuse is only safe for a byte-identical retry. */+function digestRaw(value: string): string {+ return createHash("sha256").update(value).digest("hex");+}++function digestJson(value: unknown): string {+ return digestRaw(JSON.stringify(value));+}++/** Bump when the fingerprinted material or its serialization changes, so fingerprints never compare across versions. */+const TURN_BODY_FINGERPRINT_VERSION = 1;++/**+ * Fingerprint of one turn call body: the recorder reuses a parked retry's Idempotency-Key only for an equal+ * fingerprint. It hashes a bounded subset (model, options, system-prompt digest, message count, last-message+ * digest, tools digest) so the full body (0.1-1+ MB near the context window) is never re-serialized per request;+ * earlier messages are deliberately not covered — the fingerprint only gates parked-retry ID reuse within one+ * retry window, where steering appends change the message count and compaction changes the recorder epoch.+ *+ * @param toolsDigest memoized digest of JSON.stringify(context.tools), computed here when omitted.+ */export function hashTurnBody(model: { provider: string; id: string },context: {@@ -129,20 +148,24 @@ export function hashTurnBody(maxTokens?: number;serviceTier?: unknown;},+ toolsDigest?: string,): string {+ const lastMessage = context.messages.length > 0 ? context.messages[context.messages.length - 1] : undefined;return createHash("sha256").update(JSON.stringify({+ version: TURN_BODY_FINGERPRINT_VERSION,provider: model.provider,model: model.id,- systemPrompt: context.systemPrompt,- messages: context.messages,- tools: context.tools,reasoning: options?.reasoning,thinkingBudgets: options?.thinkingBudgets,temperature: options?.temperature,maxTokens: options?.maxTokens,serviceTier: options?.serviceTier,+ systemPromptDigest: context.systemPrompt === undefined ? undefined : digestRaw(context.systemPrompt),+ messageCount: context.messages.length,+ lastMessageDigest: lastMessage === undefined ? undefined : digestJson(lastMessage),+ toolsDigest: context.tools === undefined ? undefined : (toolsDigest ?? digestJson(context.tools)),}),).digest("hex");@@ -538,8 +561,26 @@ export function unwrapSemanticEdgeStreamFn(streamFn: StreamFn): StreamFn {*/export function wrapStreamFnWithSemanticEdges(streamFn: StreamFn, recorder: SemanticEdgeRecorder): StreamFn {const inner = unwrapSemanticEdgeStreamFn(streamFn);+ // Tool schemas are usually stable across a session's requests, so the digest is memoized by element identity;+ // a tool mutated in place keeps the stale digest — replace the object to change tools.+ let memoizedTools: { elements: unknown[]; digest: string } | undefined;+ const toolsDigestFor = (tools: unknown[] | undefined): string | undefined => {+ if (tools === undefined) {+ return undefined;+ }+ if (+ memoizedTools &&+ memoizedTools.elements.length === tools.length &&+ memoizedTools.elements.every((tool, index) => tool === tools[index])+ ) {+ return memoizedTools.digest;+ }+ const digest = digestJson(tools);+ memoizedTools = { elements: tools, digest };+ return digest;+ };const wrapped: StreamFn = (model, context, options) => {- const requestId = recorder.startTurnRequest(hashTurnBody(model, context, options));+ const requestId = recorder.startTurnRequest(hashTurnBody(model, context, options, toolsDigestFor(context.tools)));if (requestId === undefined) {return inner(model, context, options);}diff --git a/packages/coding-agent/test/semantic-edges.test.ts b/packages/coding-agent/test/semantic-edges.test.tsindex f6116f3968..05e3cc4ecd 100644--- a/packages/coding-agent/test/semantic-edges.test.ts+++ b/packages/coding-agent/test/semantic-edges.test.ts@@ -64,18 +64,16 @@ describe("SemanticEdgeRecorder", () => {expect(recorder.lastCommittedRequestId).toBe(requestId);});- it("reuses a parked retry ID only for a byte-identical body and re-logs the start", () => {+ it("reuses a parked retry ID only for the same turn fingerprint and re-logs the start", () => {const recorder = createRecorder();const body = hashTurnBody({ provider: "p", id: "m" }, { messages: [{ role: "user", content: "hi" }] });const failedId = recorder.startTurnRequest(body);recorder.failRequest(failedId);recorder.prepareTurnRetry();- // Same message count and roles but different content must not steal the key.- const sideBody = hashTurnBody(- { provider: "p", id: "m" },- { messages: [{ role: "user", content: "side question" }] },- );+ // One queued steering message before the identical tail: only the count differs.+ const queued = { role: "user", content: "queued" };+ const sideBody = hashTurnBody({ provider: "p", id: "m" }, { messages: [queued, ...MESSAGES] });const sideId = recorder.startTurnRequest(sideBody);expect(sideId).not.toBe(failedId);@@ -122,6 +120,12 @@ describe("SemanticEdgeRecorder", () => {expect(recorder.startTurnRequest(retryBody())).not.toBe(failedId);});+ it("does not fingerprint earlier messages beyond the shared tail", () => {+ const hashFor = (earlier: string) =>+ hashTurnBody({ provider: "p", id: "m" }, { messages: [{ role: "user", content: earlier }, ...MESSAGES] });+ expect(hashFor("old")).toBe(hashFor("rewritten"));+ });+it("hashes the body at request time, not at park time", () => {const recorder = createRecorder();const messages: Array<{ role: string; content: Array<{ type: string; text: string }> }> = [@@ -650,6 +654,23 @@ describe("wrapStreamFnWithSemanticEdges", () => {expect(headers[MODEL_REQUEST_ID_HEADER]).toBe(recorder.lastTurnRequestId);});+ it("memoizes the tools digest by element identity across calls", () => {+ const recorder = recorderIn("tools-memo.jsonl");+ const wrapped = wrapStreamFnWithSemanticEdges(() => createAssistantMessageEventStream(), recorder);+ const call = (tools: unknown[]) => {+ wrapped(model, { ...context, tools } as unknown as Parameters<StreamFn>[1], undefined);+ return recorder.lastTurnRequestId;+ };+ const tools = [{ name: "bash", description: "" }];+ const firstId = call(tools);+ recorder.prepareTurnRetry();+ // Mutated in place: identity keys the memo, so the parked ID is reused.+ tools[0]!.description = "mutated in place";+ expect(call(tools)).toBe(firstId);+ recorder.prepareTurnRetry();+ expect(call([{ name: "bash", description: "x" }])).not.toBe(firstId);+ });+function outcomeHarness(name: string) {const recorder = recorderIn(name);const streams: Array<ReturnType<typeof createAssistantMessageEventStream>> = [];