Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(runtime): mask secrets before clipping an over-long answer
AdityaUmale154a4bedbf55
diff --git a/server/runtime/envelope.test.ts b/server/runtime/envelope.test.tsindex 822a1830ce..d8525f2173 100644--- a/server/runtime/envelope.test.ts+++ b/server/runtime/envelope.test.ts@@ -514,21 +514,6 @@ describe("composeEnvelope at Full access: nothing a bot can settle waits on theremoveScrubber();}});- it("drops a held secret cut in half where the stored answer was clipped", () => {- const secret = "Zq8-held-PASSWORD-value";- const removeScrubber = addHeldSecretScrubber((text) => text.replaceAll(secret, HELD_SECRET_MASK));- try {- const lead = `Done. ${"word ".repeat(2_398)}`;- const answer = `${lead}${secret}`.slice(0, 12_000);- expect(answer.length).toBe(12_000);- expect(answer.endsWith(secret)).toBe(false);- const envelope = composeEnvelope(assignment({ question: "Done?", answer, answeredBy: { kind: "bot", botId: "chief" } }), { ...names, answerer: "Chief" });- expect(envelope).not.toContain("Zq8");- expect(envelope).toContain(`word …".`);- } finally {- removeScrubber();- }- });});describe("composeResume", () => {diff --git a/server/runtime/envelope.ts b/server/runtime/envelope.tsindex 10ffd9ce27..377c51bf4d 100644--- a/server/runtime/envelope.ts+++ b/server/runtime/envelope.ts@@ -107,22 +107,13 @@ function replyLine(a: Assignment, requester: string, parent?: Assignment): strinreturn `Ownership is yours: results, failures and questions stay in this thread; ${requester} is not waiting and will not be resumed.${answerBack}`;}-/** runtime.ts stores an answer clipped at 12 000 characters (BRIEF_MAX). */-const STORED_ANSWER_MAX = 12_000;--/** A stored answer cut at the limit may end mid-secret, which redaction can no longer match: drop that last partial word. */-function dropClippedTail(text: string | undefined): string | undefined {- if (!text || text.length < STORED_ANSWER_MAX) return text;- return `${text.replace(/\S+$/, "").trimEnd()} …`;-}-/** Harness-quoted, so the person's "yes" never travels as pasted text, and a bot's answer never passes for theirs. */function answerLine(a: Assignment, names: EnvelopeNames): string {const question = quote(a.question, QUOTE_MAX);const asked = question ? `Your question was: "${question}". ` : "";// Only a bot's or the person's answer travels whole; a default pick keeps the short quote it always had.const isDefault = a.answeredBy?.kind === "default";- const answer = isDefault ? quote(a.answer, QUOTE_MAX) : quote(dropClippedTail(a.answer), Number.POSITIVE_INFINITY);+ const answer = isDefault ? quote(a.answer, QUOTE_MAX) : quote(a.answer, Number.POSITIVE_INFINITY);if (isDefault) {return `${asked}Nobody was asked, at this level: ${answer ? `go on with your default, "${answer}"` : "pick the sensible default yourself"}, and say in one plain line what you chose.`;}diff --git a/server/runtime/runtime.test.ts b/server/runtime/runtime.test.tsindex d5bce94a7f..09fcf73e13 100644--- a/server/runtime/runtime.test.ts+++ b/server/runtime/runtime.test.ts@@ -1,4 +1,5 @@import { describe, expect, it } from "vitest";+import { addHeldSecretScrubber, HELD_SECRET_MASK } from "../../shared/redact.ts";import { checkInvariants } from "./invariants.ts";import { Runtime, type BeginRequest } from "./runtime.ts";@@ -286,6 +287,27 @@ describe("Runtime: one tree, one lane", () => {invariants(w);});+ it("masks a held secret, even one with spaces, before an over-long answer is clipped", async () => {+ const secret = "alpha bravo charlie";+ const removeScrubber = addHeldSecretScrubber((text) => text.replaceAll(secret, HELD_SECRET_MASK));+ try {+ const w = world();+ w.runner.script = a => a.assignee.botId === "lead" && a.executions === 1 ? { kind: "needs_input", question: "Done?" } : undefined;+ const root = w.runtime.begin(beginReq());+ const lead = ok(w.runtime.enqueue(toBot("lead", root.id)));+ w.runtime.tick(); await flush();+ const leadIn = "x".repeat(11_990);+ expect(w.runtime.answer(lead.id, `${leadIn}${secret} and more`)).toBe(true);+ const stored = w.store.get(lead.id)?.answer ?? "";+ expect(stored.length).toBeLessThanOrEqual(12_000);+ expect(stored).not.toContain("alpha");+ expect(stored).toContain(HELD_SECRET_MASK);+ expect(w.runtime.answer(lead.id, "short")).toBe(false);+ } finally {+ removeScrubber();+ }+ });+it("stalls a turn with no activity on the monotonic clock, and a tree with no progress, but never a parked tree", async () => {const w = world(() => "hold", { turnStallMs: 1_000, stallMs: 10_000 });const root = w.runtime.begin(beginReq());diff --git a/server/runtime/runtime.ts b/server/runtime/runtime.tsindex dff670d019..21ab39a370 100644--- a/server/runtime/runtime.ts+++ b/server/runtime/runtime.ts@@ -1,5 +1,6 @@// The store is the only state: tick() is the only place work is admitted or dispatched, and no timer lives here.import { randomUUID } from "node:crypto";+import { redactSecretsInText } from "../../shared/redact.ts";import { composeEnvelope } from "./envelope.ts";import { assignmentLevel } from "./level.ts";@@ -546,7 +547,8 @@ export class Runtime {const n = this.store.get(id);if (!n || n.status !== "needs_input") return false;const now = this.clock.now();- n.answer = clip(text, BRIEF_MAX); delete n.question; delete n.decide; n.status = "queued"; n.lastActivityAt = now;+ // Mask secrets before an over-long answer is clipped, so the cut can never leave part of one that redaction no longer matches.+ n.answer = clip(text.length > BRIEF_MAX ? redactSecretsInText(text) : text, BRIEF_MAX); delete n.question; delete n.decide; n.status = "queued"; n.lastActivityAt = now;if (by) n.answeredBy = by; else delete n.answeredBy;const root = n.parentId ? this.store.get(n.rootId) : undefined;this.store.transaction(() => {