Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Context: no breakdown between a summary and the turn that applies it
luishron12339d34ecb2a2c
diff --git a/server/context-breakdown.e2e.test.ts b/server/context-breakdown.e2e.test.tsindex 5a9c040fe7..50e56c8d26 100644--- a/server/context-breakdown.e2e.test.ts+++ b/server/context-breakdown.e2e.test.ts@@ -27,8 +27,10 @@ async function setup(extraEnv: NodeJS.ProcessEnv) {await control("send", "--bot", bot.id, "--task", thread, "--text", text);expect((await control("wait", "--bot", bot.id, "--task", thread, "--timeout", "30")).status).toBe("settled");};+ const settled = async () => expect((await control("wait", "--bot", bot.id, "--task", thread, "--timeout", "30")).status).toBe("settled");+ const records = () => (request(`/api/threads/${thread}/messages`, {}, fixture.info.url) as Promise<any>).then((r) => r.messages.filter((m: any) => m.kind === "compaction"));const close = async () => { try { await fixture.close(); } finally { expect(existsSync(fixture.info.dataDir)).toBe(false); } };- return { thread, post, context, send, close };+ return { thread, post, context, send, settled, records, close };}it("answers with the engine's own count while its process holds the conversation, and with nothing otherwise", async () => {@@ -58,6 +60,25 @@ it("answers with the engine's own count while its process holds the conversation}}, 60_000);+it("has no breakdown between a summary of the conversation and the turn that starts the engine on it", async () => {+ const f = await setup({ FAKE_CLAUDE_CONTEXT_USAGE: JSON.stringify(answer) });+ try {+ await f.send("hello");+ expect((await f.context()).breakdown?.tokens).toBe(6000);++ await f.post("compact");+ await f.settled();+ expect(await f.records()).toHaveLength(1);+ // The engine process that read the whole conversation is still there and still answers 6,000.+ expect(await f.context()).toEqual({ threadId: f.thread, breakdown: null });++ await f.send("and now?");+ expect((await f.context()).breakdown?.tokens).toBe(6000);+ } finally {+ await f.close();+ }+}, 60_000);+it("has no breakdown from an engine that does not count its window by category", async () => {const f = await setup({});try {diff --git a/server/fresh-start.ts b/server/fresh-start.tsindex cbc0bc8b45..eaefb9943f 100644--- a/server/fresh-start.ts+++ b/server/fresh-start.ts@@ -31,3 +31,12 @@ export function startFresh(store: FreshStartStore, botId: string, threadId: stricompaction: { summary: "", firstKeptId: "", foldedThroughId: newest.id, tokensBefore: 0, by: "person", fresh: true },});}++/** Whether the engine process kept for a conversation still holds what the next turn will replace: the conversation the+ * person left with Start fresh or a rewind, or the full history a summary written since its last turn stands in for. */+export function engineSessionOutdated(store: Pick<Store, "activePath" | "taskByThread">, botId: string, threadId: string): boolean {+ const task = store.taskByThread(botId, threadId);+ if (!task || task.rewound === true) return Boolean(task);+ const summary = sinceFreshStart(store.activePath(threadId)).findLast((message) => message.kind === "compaction" && message.compaction);+ return summary !== undefined && summary.id !== task.appliedCompactionId;+}diff --git a/server/routes/bot-messages.ts b/server/routes/bot-messages.tsindex 8a982e4db6..5a6e97c864 100644--- a/server/routes/bot-messages.ts+++ b/server/routes/bot-messages.ts@@ -36,7 +36,7 @@ import { extractSteerImages } from "../turn-images.ts";import { followupFacts, helperFollowup, helperFollowupBody, mayFollowUp } from "../helper-followup.ts";import type { UsageTrigger } from "../usage-ledger.ts";import { noteTypedSend } from "../typed-sends.ts";-import { startFresh } from "../fresh-start.ts";+import { engineSessionOutdated, startFresh } from "../fresh-start.ts";import { parseMentionPicks } from "../../shared/room-mentions.ts";import { pinnedClientThreadGuard } from "./pinned-client-thread.ts";import { refusalBody, userError } from "../user-errors.ts";@@ -503,8 +503,8 @@ export function createBotMessageRoutes(deps: BotMessageRouteDeps): RouteHandlerconst bot = requestedTaskBot(m[1], body?.threadId);const notYours = cloudThreadRefusal(auth, bot.threadId);if (notYours) return json(res, 403, { error: notYours });- // After Start fresh the engine process still holds the conversation the person left, until the next turn replaces it.- const stale = store.taskByThread(bot.id, bot.threadId)?.rewound === true;+ // The engine process answers for the conversation it last read, which the next turn may be about to replace.+ const stale = engineSessionOutdated(store, bot.id, bot.threadId);const breakdown = stale ? null : await registry.get(bot.modelSelection.instanceId)?.adapter.contextBreakdown?.(bot.threadId, { signal: AbortSignal.timeout(CONTEXT_BREAKDOWN_WAIT_MS) }) ?? null;return json(res, 200, { threadId: bot.threadId, breakdown });