Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(hooks): a turn bound after its cleanup revoked the hooks bearer still gets its late-output window
Absterrg098e194b0eb7d
diff --git a/server/host/late-tool-results.test.ts b/server/host/late-tool-results.test.tsindex cb8ed1acc2..b9de3f95e9 100644--- a/server/host/late-tool-results.test.ts+++ b/server/host/late-tool-results.test.ts@@ -36,6 +36,18 @@ describe("late tool results", () => {expect(late.turnFor("Bearer other", "t1", "b1", "tool-z")).toBe("turn-2");});+ it("remembers a revoked generation's hooks bearer for a turn bound after its cleanup, a bounded few, until the conversation's bearers go", () => {+ const late = createLateToolResults(() => 0);+ const hooks = { threadId: "t1", botId: "b1", token: "secret" };+ late.generationRevoked("gen-1", hooks);+ expect(late.revokedHooks("gen-1")).toEqual(hooks);+ for (let i = 0; i < 64; i++) late.generationRevoked(`gen-${i + 2}`, { ...hooks, threadId: "t2" });+ expect(late.revokedHooks("gen-1")).toBeUndefined();+ expect(late.revokedHooks("gen-65")).toBeDefined();+ late.forgetThread("t2");+ expect(late.revokedHooks("gen-65")).toBeUndefined();+ });+it("ends with its window, when the conversation's bearers are revoked, or when nothing was left to save", () => {let now = 0;const late = createLateToolResults(() => now);diff --git a/server/host/late-tool-results.ts b/server/host/late-tool-results.tsindex f9d0499521..0ca55deb4a 100644--- a/server/host/late-tool-results.ts+++ b/server/host/late-tool-results.ts@@ -2,6 +2,7 @@ import { timingSafeEqual } from "node:crypto";/** How long after its turn ends a background `PostToolUse` may still save that turn's full tool output. */export const LATE_TOOL_RESULT_MS = 60_000;+const REVOKED_KEPT = 64;export interface EndedHookTurn {threadId: string;@@ -17,6 +18,7 @@ export interface EndedHookTurn {* tool calls, each once, until LATE_TOOL_RESULT_MS after the turn ended; revoking the thread's bearers ends it at once. */export function createLateToolResults(now: () => number = Date.now) {const ended: Array<Omit<EndedHookTurn, "toolUseIds"> & { toolUseIds: Set<string>; until: number }> = [];+ const revoked = new Map<string, { threadId: string; botId: string; token: string }>();const live = () => {const at = now();for (let i = ended.length - 1; i >= 0; i--) if (ended[i]!.until <= at || ended[i]!.toolUseIds.size === 0) ended.splice(i, 1);@@ -28,6 +30,14 @@ export function createLateToolResults(now: () => number = Date.now) {return got.length === expected.length && timingSafeEqual(got, expected);};return {+ /** A turn can complete before its generation is bound to it, after cleanup revoked the generation's hooks bearer. */+ generationRevoked(generation: string, hooks: { threadId: string; botId: string; token: string }): void {+ revoked.set(generation, hooks);+ for (const oldest of revoked.keys()) if (revoked.size > REVOKED_KEPT) revoked.delete(oldest); else break;+ },+ revokedHooks(generation: string): { threadId: string; botId: string; token: string } | undefined {+ return revoked.get(generation);+ },turnEnded(turn: EndedHookTurn): void {if (turn.toolUseIds.size) ended.push({ ...turn, toolUseIds: new Set(turn.toolUseIds), until: now() + LATE_TOOL_RESULT_MS });},@@ -45,9 +55,11 @@ export function createLateToolResults(now: () => number = Date.now) {},forgetThread(threadId: string): void {for (let i = ended.length - 1; i >= 0; i--) if (ended[i]!.threadId === threadId) ended.splice(i, 1);+ for (const [generation, hooks] of revoked) if (hooks.threadId === threadId) revoked.delete(generation);},clear(): void {ended.length = 0;+ revoked.clear();},};}diff --git a/server/index.ts b/server/index.tsindex 833f80f106..0612d95c5d 100644--- a/server/index.ts+++ b/server/index.ts@@ -1724,7 +1724,9 @@ function revokeInternalCapabilityGeneration(threadId: string, generation: stringendForeignTurns(threadId, generation);if (guestDrivenTurns.get(threadId) === generation) guestDrivenTurns.delete(threadId);for (const [token, capability] of internalCapabilities) {- if (capability.threadId === threadId && capability.generation === generation) internalCapabilities.delete(token);+ if (capability.threadId !== threadId || capability.generation !== generation) continue;+ if (capability.kind === "hooks") lateToolResults.generationRevoked(generation, { threadId, botId: capability.botId, token });+ internalCapabilities.delete(token);}if (activeInternalGenerationByThread.get(threadId) === generation) activeInternalGenerationByThread.delete(threadId);internalGenerationByProviderTurn.deleteGeneration(threadId, generation);@@ -1773,8 +1775,9 @@ function revokeInternalCapabilityForProviderEvent(event: RuntimeEvent): void {}function rememberLateToolResults(threadId: string, generation: string, turnId: string): void {- const hooks = [...internalCapabilities].find(([, capability]) => capability.kind === "hooks" && capability.threadId === threadId && capability.generation === generation);- if (hooks) lateToolResults.turnEnded({ threadId, botId: hooks[1].botId, turnId, token: hooks[0], toolUseIds: new Set(store.activePath(threadId)+ const live = [...internalCapabilities].find(([, capability]) => capability.kind === "hooks" && capability.threadId === threadId && capability.generation === generation);+ const hooks = live ? { botId: live[1].botId, token: live[0] } : lateToolResults.revokedHooks(generation);+ if (hooks) lateToolResults.turnEnded({ threadId, botId: hooks.botId, turnId, token: hooks.token, toolUseIds: new Set(store.activePath(threadId).flatMap((m) => m.kind === "activity" && m.turnId === turnId && m.tool?.itemId && m.tool.fullResult === undefined ? [m.tool.itemId] : [])) });}