Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(hooks): a digest written before its turn's late outputs arrived is marked full once they complete it

Absterrg0333c7270dc5d
diff --git a/docs/verification/digests.md b/docs/verification/digests.md
index 69038ba4f1..63bb08b142 100644
--- a/docs/verification/digests.md
+++ b/docs/verification/digests.md
@@ -37,8 +37,9 @@ does: the hint for a timed-out Bash call (`PostToolUseFailure`, matcher
a 60 s timeout; there is no `Stop` hook. A `PostToolUse` can land after its
turn has ended. For 60 s after that, the ended turn's hook token is honoured
for one thing only: saving the full output of that turn's own tool calls not
-yet saved, each once. The digest was written at the turn's end, so it still
-says "from tool previews". `server/hooks.e2e.test.ts` plays this with
+yet saved, each once. The digest is written at the turn's end and says "from
+tool previews" until those outputs complete the turn's evidence; then it is
+marked full again. `server/hooks.e2e.test.ts` plays this with
`FAKE_CLAUDE_HOOKS_AFTER_TURN_MS`.
To see the difference with the real CLI, run `claude -p` in an empty folder
diff --git a/server/digest.test.ts b/server/digest.test.ts
index c4126fd681..d26635275b 100644
--- a/server/digest.test.ts
+++ b/server/digest.test.ts
@@ -217,6 +217,22 @@ describe("coverageForDriver", () => {
});
});
+describe("digestCompletedLate", () => {
+ it("marks a preview digest full once every tool row of its turn has its full result, and leaves it otherwise", async () => {
+ const { digestCompletedLate } = await import("./digest.ts");
+ const preview = activity("Bash", true, "ls");
+ const full = { ...preview, tool: { ...preview.tool!, fullResult: true } };
+ const digest = buildTurnDigest({ ...base, activities: [preview], memory: [] });
+ const row: Message = { id: "d1", role: "bot", kind: "digest", at: 2, turnId: "turn-1", text: renderDigest(digest), digest };
+ expect(renderDigest(digest)).toContain("from tool previews");
+ expect(digestCompletedLate([preview, row], "turn-1")).toBeNull();
+ const completed = digestCompletedLate([full, row], "turn-1");
+ expect(completed).toMatchObject({ id: "d1", digest: { hookCoverage: "full" } });
+ expect(completed!.text).not.toContain("from tool previews");
+ expect(digestCompletedLate([full, { ...row, digest: completed!.digest }], "turn-1")).toBeNull();
+ });
+});
+
describe("toolEvidence", () => {
it("is full only when every tool row of the turn carries a delivered result", async () => {
const { toolEvidence } = await import("./digest.ts");
diff --git a/server/digest.ts b/server/digest.ts
index c3a771510b..d285e4d800 100644
--- a/server/digest.ts
+++ b/server/digest.ts
@@ -18,6 +18,14 @@ export function toolEvidence(activities: readonly Message[], turnId: string): bo
return rows.length > 0 && rows.every((m) => m.tool?.fullResult === true);
}
+/** A turn's digest marked "preview" that full results arriving after it have since completed; null when none did. */
+export function digestCompletedLate(activities: readonly Message[], turnId: string): { id: string; digest: TurnDigest; text: string } | null {
+ const message = activities.findLast((m) => m.kind === "digest" && m.turnId === turnId && m.digest?.hookCoverage === "preview");
+ if (!message?.digest || !toolEvidence(activities, turnId)) return null;
+ const digest: TurnDigest = { ...message.digest, hookCoverage: "full" };
+ return { id: message.id, digest, text: renderDigest(digest) };
+}
+
export const MAX_TOOLS = 8;
export const MAX_FILES = 20;
export const MAX_MEMORY = 20;
diff --git a/server/hooks.e2e.test.ts b/server/hooks.e2e.test.ts
index d538b452b8..5abcffdce6 100644
--- a/server/hooks.e2e.test.ts
+++ b/server/hooks.e2e.test.ts
@@ -151,12 +151,15 @@ posixOnly("engine hooks e2e: full tool output that arrives after its turn ended"
expect(bot.messages.find((m: Msg) => m.kind === "digest").digest?.hookCoverage).toBe("preview");
const rows = async (): Promise<Msg[]> => (await h.getBot(bot.id)).messages.filter((m: Msg) => m.kind === "activity" && m.tool?.itemId);
await h.waitFor(async () => (await rows()).every((m) => m.tool!.fullResult === true), "the late hooks to save both outputs");
+ const digest = (await h.getBot(bot.id)).messages.find((m: Msg) => m.kind === "digest");
+ expect(digest.digest?.hookCoverage).toBe("full");
+ expect(digest.text).not.toContain("from tool previews");
const root = join(h.home(), ".openmausbot", "tool-results");
const spilled = readdirSync(join(root, readdirSync(root)[0]!)).map((name) => readFileSync(join(root, readdirSync(root)[0]!, name), "utf8"));
expect(spilled.some((text) => text.includes(BIG.slice(0, 200)))).toBe(true);
- const digest = createHash("sha256").update(`${bot.id}\0${bot.threadId}`).digest("hex").slice(0, 24);
- const bearer = { authorization: `Bearer ${readFileSync(join(h.home(), ".openmausbot", "hook-tokens", digest, "token"), "utf8").trim()}` };
+ const tokenDir = createHash("sha256").update(`${bot.id}\0${bot.threadId}`).digest("hex").slice(0, 24);
+ const bearer = { authorization: `Bearer ${readFileSync(join(h.home(), ".openmausbot", "hook-tokens", tokenDir, "token"), "utf8").trim()}` };
const [first] = await rows();
const again = await h.api("POST", "/api/internal/hook", { event: "PostToolUse", payload: { tool_use_id: first!.tool!.itemId, tool_response: "replaced" } }, bearer);
expect(again.status).toBe(401);
diff --git a/server/index.ts b/server/index.ts
index 0612d95c5d..1e8ed29dfc 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -62,7 +62,7 @@ import {
} from "./browser-lifecycle-cleanup.ts";
import * as checkpoints from "./checkpoints.ts";
import { commandReceipt } from "./commands.ts"; import { moveSavedGrants } from "./mcp-grant-spec.ts";
-import { buildTurnDigest, coverageForDriver, digestPromptLine, renderDigest, toolEvidence } from "./digest.ts";
+import { buildTurnDigest, coverageForDriver, digestCompletedLate, digestPromptLine, renderDigest, toolEvidence } from "./digest.ts";
import { appendDecision, bindDecisionRetention, decisionRetentionDays, flushDecisionLog, pruneDecisions, withDecisionActor, type DecisionActor, type DecisionVia } from "./decision-log.ts";
import { folderToFollow, inMyFolderCwd, validateBotCwd } from "./bot-cwd.ts";
import { ATTACHMENTS_DIR, attachmentExists, deleteAttachment, keptAttachmentNames, offerAttachmentClear, saveAudio, saveImage } from "./attachments.ts";
@@ -5474,7 +5474,11 @@ function ingestEngineHook(capability: Pick<InternalCapability, "botId" | "thread
const fullResult = spillToolResult(dir, key, redacted, TOOL_RESULT_SPILL_MAX);
store.patchMessage(threadId, row.id, { tool: { ...row.tool, fullResult } }, { kind: "hook.ingest", key });
}
- if (late) lateToolResults.saved(threadId, late, toolUseId);
+ if (late) {
+ lateToolResults.saved(threadId, late, toolUseId);
+ const completed = digestCompletedLate(store.activePath(threadId), late);
+ if (completed) store.patchMessage(threadId, completed.id, { digest: completed.digest, text: completed.text });
+ }
return { ok: true };
}
diff --git a/server/testing/fake-claude-cli.ts b/server/testing/fake-claude-cli.ts
index 0812c06938..ea3ffdc357 100755
--- a/server/testing/fake-claude-cli.ts
+++ b/server/testing/fake-claude-cli.ts
@@ -284,7 +284,7 @@ function runHooks(event: FakeHookEvent, payload: Record<string, unknown>): strin
hooksAfterTurn.push(() => new Promise((settled) => {
const child = spawn(hook.command!, hook.args ?? [], { ...options, stdio: ["pipe", "ignore", "ignore"] });
child.on("error", settled).on("close", settled);
- child.stdin!.end(input);
+ child.stdin!.on("error", () => {}).end(input);
}));
} else stdout += spawnSync(hook.command, hook.args ?? [], { ...options, input, stdio: ["pipe", "pipe", "pipe"], encoding: "utf8" }).stdout ?? "";
}