Skip to content

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

Context: drop a count the engine returns after the conversation moved on

luishron123fe8ec9dfa37a
diff --git a/server/routes/bot-messages.test.ts b/server/routes/bot-messages.test.ts
index 5cb25e9ad3..e4b2a74435 100644
--- a/server/routes/bot-messages.test.ts
+++ b/server/routes/bot-messages.test.ts
@@ -5,10 +5,12 @@ import { createServer, type Server } from "node:http";
import type { AddressInfo } from "node:net";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { ContextBreakdown } from "../../shared/context-breakdown.ts";
import { WIDGET_SAMPLES } from "../../shared/widget-samples.ts";
import { createAgentCard } from "../chat-widgets.ts";
import { DATA_DIR } from "../config.ts";
import type { ModelSelection } from "../contracts.ts";
+import { startFresh } from "../fresh-start.ts";
import { json, readBody } from "../harness/http.ts";
import { createRunOwners } from "../host/run-owner.ts";
import { promptWithReply } from "../replies.ts";
@@ -162,9 +164,47 @@ async function fixture(over: Partial<Record<keyof BotMessageRouteDeps, unknown>>
};
const row = (id: string, thread = threadId): Message => store.messagesFor(thread).find((message) => message.id === id)!;
const sent = (thread = threadId): Message[] => store.messagesFor(thread).filter((message) => message.role === "user");
- return { store, bot, threadId, state, post, edit, cancelQueued, show, row, sent, patches: vi.spyOn(store, "patchMessage") };
+ const context = async () => {
+ const response = await fetch(`${base}/api/bots/${bot.id}/context`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ threadId }) });
+ return { status: response.status, body: await response.json() as Record<string, any> };
+ };
+ return { store, bot, threadId, state, post, edit, cancelQueued, context, show, row, sent, patches: vi.spyOn(store, "patchMessage") };
}
+describe("POST /api/bots/:id/context", () => {
+ const counted: ContextBreakdown = { tokens: 6000, window: 200000, categories: [{ id: "messages", kind: "used", tokens: 6000 }] };
+ /** An engine that has been asked and answers only when the test lets it. */
+ function engine() {
+ let asked!: () => void;
+ let answer!: (breakdown: ContextBreakdown) => void;
+ const reached = new Promise<void>((resolve) => { asked = resolve; });
+ const reply = new Promise<ContextBreakdown>((resolve) => { answer = resolve; });
+ const registry = { get: () => ({ instanceId: "claude", driverKind: "claudeAgent", adapter: { contextBreakdown: () => { asked(); return reply; } } }) };
+ return { registry, reached, answer };
+ }
+
+ it("returns the engine's count for the conversation it still holds", async () => {
+ const { registry, reached, answer } = engine();
+ const { context, threadId } = await fixture({ registry });
+ const reading = context();
+ await reached;
+ answer(counted);
+ expect(await reading).toEqual({ status: 200, body: { threadId, breakdown: counted } });
+ });
+
+ it("drops a count the engine returns after the person started fresh while it was counting", async () => {
+ const { registry, reached, answer } = engine();
+ const { store, bot, context, threadId } = await fixture({ registry });
+ store.appendMessage(threadId, { role: "user", kind: "text", text: "hello" });
+ store.appendMessage(threadId, { role: "bot", kind: "text", text: "hi" });
+ const reading = context();
+ await reached;
+ expect(startFresh(store, bot.id, threadId)).not.toBeNull();
+ answer(counted);
+ expect(await reading).toEqual({ status: 200, body: { threadId, breakdown: null } });
+ });
+});
+
describe("editing a message with no AI", () => {
it("says what a Send says on a team, and starts no turn", async () => {
const line = "Olive needs to connect an AI before bots can answer. I've told them.";
diff --git a/server/routes/bot-messages.ts b/server/routes/bot-messages.ts
index c947efd23b..339b4308d0 100644
--- a/server/routes/bot-messages.ts
+++ b/server/routes/bot-messages.ts
@@ -503,11 +503,12 @@ export function createBotMessageRoutes(deps: BotMessageRouteDeps): RouteHandler
const bot = requestedTaskBot(m[1], body?.threadId);
const notYours = cloudThreadRefusal(auth, bot.threadId);
if (notYours) return json(res, 403, { error: notYours });
- // 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
+ // The engine process answers for the conversation it last read, which the next turn may be about to replace:
+ // already when asked, or by a Start fresh, rewind or summary that lands while it counts.
+ 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 });
+ return json(res, 200, { threadId: bot.threadId, breakdown: stale() ? null : breakdown });
}
m = path.match(/^\/api\/bots\/([\w-]+)\/messages\/([\w-]+)\/edit$/);