Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Test compaction summary escaping against a realistic history
hh123456twfd766ed5ff3f
diff --git a/server/compaction-summary.test.ts b/server/compaction-summary.test.tsindex 848986b5a5..9039eebb69 100644--- a/server/compaction-summary.test.ts+++ b/server/compaction-summary.test.ts@@ -1,7 +1,7 @@import { expect, it, vi } from "vitest";import { EXCERPT_SUMMARY_HEADER } from "../shared/wire.ts";import { deterministicSummary, draftSummary, foldPoint, summaryPrompt } from "./compaction-summary.ts";-import { MAX_SUMMARY_BYTES, selectReplay, type ReplayEntry } from "./context-rebuild.ts";+import { MAX_SUMMARY_BYTES, selectReplay, type CompactionRecord, type ReplayEntry } from "./context-rebuild.ts";import { createSideJobs, type SideJobInstance, type SideJobsDeps } from "./side-jobs.ts";const history: ReplayEntry[] = [@@ -52,16 +52,16 @@ it("opens only the excerpt fallback with the header stored records are recogniseit("does not re-escape the previous summary across repeated manual compactions", () => {const longestBackslashRun = (text: string) => Math.max(0, ...(text.match(/\\+/g) ?? []).map(run => run.length));- let transcript: ReplayEntry[] = [{ id: "m1", role: "user", text: "Deploy to \"staging\"\nthen run the tests" }];+ const history: ReplayEntry[] = [{ id: "m1", role: "user", text: "Deploy to \"staging\"\nthen run the tests" }];+ let record: (CompactionRecord & { id: string }) | undefined;let summary = "";const runs: number[] = [];for (let round = 1; round <= 4; round++) {- summary = deterministicSummary(transcript.filter(entry => entry.id));+ const replay = selectReplay(history, { compaction: record });+ summary = deterministicSummary(replay.transcript.filter(entry => entry.id));runs.push(longestBackslashRun(summary));- const record = { id: `c${round}`, firstKeptId: "", foldedThroughId: `m${round}`, summary, tokensBefore: 0, by: "person" as const };- const next: ReplayEntry = { id: `m${round + 1}`, role: "user", text: `Round ${round + 1} note` };- const replay = selectReplay([...transcript.filter(entry => entry.id !== `c${round - 1}`), { id: record.foldedThroughId, role: "user", text: "x" }, { id: record.id, role: "assistant", text: "" }, next], { compaction: record });- transcript = replay.transcript;+ record = { id: `c${round}`, firstKeptId: "", foldedThroughId: history.at(-1)!.id, summary, tokensBefore: 0, by: "person" };+ history.push({ id: record.id, role: "assistant", text: "" }, { id: `m${round + 1}`, role: "user", text: `Round ${round + 1} note` });}expect(runs.every(run => run === runs[0])).toBe(true);expect(summary).toContain("Deploy to \\\"staging\\\"\\nthen run the tests");@@ -70,6 +70,32 @@ it("does not re-escape the previous summary across repeated manual compactions",expect(Buffer.byteLength(summary)).toBeLessThanOrEqual(MAX_SUMMARY_BYTES);});+it("falls back to quoting the entry when the prior summary is empty or blank", () => {+ for (const priorSummary of ["", " \n "]) {+ const summary = deterministicSummary([{ id: "c", role: "assistant", text: "[summary]", priorSummary }, { id: "m", role: "user", text: "next" }]);+ expect(summary).toContain('Assistant/record: "[summary]"');+ expect(summary).toContain('User message: "next"');+ }+});++it("keeps a model-written prior summary verbatim", () => {+ const priorSummary = 'The person said "ship it"\nand cancelled the rollback.';+ const summary = deterministicSummary([{ id: "c", role: "assistant", text: JSON.stringify(priorSummary), priorSummary }]);+ expect(summary).toBe(`${EXCERPT_SUMMARY_HEADER}\n${priorSummary}`);+});++it.each([512, 3_200, MAX_SUMMARY_BYTES])("keeps multibyte summaries valid and within a %i byte budget", maxBytes => {+ const priorSummary = "摘要🙂".repeat(580);+ expect(Buffer.byteLength(priorSummary)).toBeGreaterThan(5_500);+ const summary = deterministicSummary([+ { id: "c", role: "assistant", text: JSON.stringify(priorSummary), priorSummary },+ { id: "m", role: "user", text: "最新訊息🙂" },+ ], maxBytes);+ expect(Buffer.byteLength(summary)).toBeLessThanOrEqual(maxBytes);+ expect(summary).toContain("最新訊息🙂");+ expect(summary).not.toContain("�");+});+it("works without a model helper", async () => {expect(await draftSummary(history, { signal: new AbortController().signal })).toBe(deterministicSummary(history));});diff --git a/server/compaction-summary.ts b/server/compaction-summary.tsindex 4598556fcc..966d7bc0c9 100644--- a/server/compaction-summary.ts+++ b/server/compaction-summary.ts@@ -20,7 +20,6 @@ export function deterministicSummary(history: readonly ReplayEntry[], maxBytes =for (let i = history.length - 1; i >= 0; i--) {const entry = history[i]!;if (!entry.text.trim()) continue;- // Already escaped once: stringifying it again would double the escapes every round.const prior = entry.priorSummary?.replace(EXCERPT_SUMMARY_HEADER, (m, at) => at === 0 ? "" : m).trim() || undefined;const text = prior ?? `${entry.role === "user" ? "User message" : "Assistant/record"}: ${JSON.stringify(entry.text)}`;const line = boundedContextText(text, Math.min(prior ? MAX_SUMMARY_BYTES : 1_600, available));diff --git a/server/context-rebuild.ts b/server/context-rebuild.tsindex 64107e5896..81e304031f 100644--- a/server/context-rebuild.ts+++ b/server/context-rebuild.ts@@ -11,7 +11,6 @@ export interface ReplayEntry {id: string;role: "user" | "assistant";text: string;- /** On the entry standing for an earlier compaction summary: the summary's raw text, before it is JSON-quoted into `text`. */priorSummary?: string;}