Multiplayer agent harness for work.
Measure immutable Slack shadow samples with actual mirror selection (#1244)
Regan Bellae1d8395aa37
diff --git a/src/slack/README.md b/src/slack/README.mdindex a82f4da72..c942ede5b 100644--- a/src/slack/README.md+++ b/src/slack/README.md@@ -251,7 +251,7 @@ your per-dev name + tokens live only in your local app and your gitignored `.env`SLACK_CONTEXT_SOURCE` controls conversation context and default Slack tool reads:- `live` (default) keeps Slack history as the source, including automatic channel thread expansion.-- `shadow` returns the same live context while comparing stored events in the background. It logs `slack_mirror_shadow` counts for missing messages, text differences, and missing thread parents. It does not log message text or channel identifiers, backfill history, or wait for comparison before returning context. At most one comparison runs per Slack client; concurrent reads are not all sampled.+- `shadow` returns the same live context while comparing stored events in the background. It logs `slack_mirror_shadow` counts for missing messages, text differences, and missing thread parents. It does not log message text or channel identifiers, backfill history, or wait for comparison before returning context. The live page is snapshotted before callers add trigger context, and shadow uses the same stored-message selection as mirror mode. Long threads may differ from Slack’s oldest page; these differences remain visible. Counts cover raw message fields, not complete rendered context or author/subtype fidelity. At most one comparison runs per Slack client; concurrent reads are not all sampled.- `mirror` explicitly enables stored context and mirror search, with the documented partial coverage and bounded live fallback. Keep this opt-in until shadow evidence and development qualification are satisfactory.Ingestion continues in every mode. Shadow mismatch counts describe the sampled live window, not complete channel coverage. Compare channel and thread samples, edits/deletes, and normal agent turns before enabling mirror reads. No automatic mode promotion occurs.diff --git a/src/slack/history.ts b/src/slack/history.tsindex 40d3b3860..442550074 100644--- a/src/slack/history.ts+++ b/src/slack/history.ts@@ -89,11 +89,9 @@ export function createSlackHistoryReader(deps: {if (threadTs) {const [parents, replies] = await Promise.all([read(channel, { at: threadTs, noFallback: true, ...(before ? { before } : {}) }),- read(channel, { ...options, sub: threadTs, oldestFirst: deps.source === "shadow" }),+ read(channel, { ...options, sub: threadTs }),]);- return deps.source === "shadow"- ? [...parents, ...replies].slice(0, 200)- : [...parents, ...replies.slice(-(200 - parents.length))];+ return [...parents, ...replies.slice(-(200 - parents.length))];}const roots = await read(channel, { ...options, channelHistory: true });if (!expandThreads) return roots;@@ -195,7 +193,7 @@ export function createSlackHistoryReader(deps: {const live = await liveHistory(client, channel, threadTs, before, expandThreads);if (deps.source === "shadow" && !shadowPending) {shadowPending = true;- void compareShadow(live, channel, threadTs, before, expandThreads).finally(() => {+ void compareShadow(structuredClone(live), channel, threadTs, before, expandThreads).finally(() => {shadowPending = false;});}diff --git a/test/slack-mirror-soak.test.ts b/test/slack-mirror-soak.test.tsindex d25849af0..2d8b7f683 100644--- a/test/slack-mirror-soak.test.ts+++ b/test/slack-mirror-soak.test.ts@@ -1,9 +1,11 @@import { test } from "node:test";+import { createMemorySurfaceCache } from "../src/surface-cache/surface-cache.ts";import assert from "node:assert/strict";import { createSlackHistoryReader } from "../src/slack/history.ts";import { parseSlackContextSource, slackAccountConfigsFromEnv } from "../src/slack/config.ts";import { createSurfaceToolDeps, type SurfaceToolsContext } from "../src/core/orchestrator/surface-tools.ts";import type { SlackCoreClient } from "../src/api/slack-core-client.ts";+import type { ReadMessagesOpts } from "../src/surface-cache/types.ts";import type { BotIdentity } from "../src/slack/directory.ts";const ids = { botUserId: "UBOT", ownBotId: "BBOT" } as BotIdentity;@@ -300,3 +302,84 @@ test("shadow never certifies failed or truncated live thread expansion as compleassert.equal(result.liveTruncatedExpansions, fail ? 0 : 1);}});++test("shadow measures the same recent long-thread selection as mirror without fallback", async (t) => {+ const logs: string[] = [];+ t.mock.method(console, "info", (line: string) => logs.push(line));+ const cache = createMemorySurfaceCache();+ const messages = Array.from({ length: 220 }, (_, index) => ({+ ts: `1000.${String(index).padStart(6, "0")}`,+ text: `message-${index}`,+ ...(index ? { thread_ts: "1000.000000" } : {}),+ }));+ await cache.ingest(+ messages.map((message) => ({+ container: "C1",+ ts: message.ts,+ text: message.text,+ ...(message.thread_ts ? { sub: message.thread_ts } : {}),+ })),+ );+ const core = {+ readSurfaceMessages: async (container: string, opts?: ReadMessagesOpts) => {+ assert.equal(opts?.noFallback, true);+ return cache.readMessages(container, opts);+ },+ rememberSurfaceHistory: async () => {+ throw new Error("must not backfill");+ },+ } as unknown as SlackCoreClient;+ const client = { conversations: { replies: async () => ({ messages: messages.slice(0, 200), has_more: true }) } };+ const mirrored = await createSlackHistoryReader({ core, ids, source: "mirror" })(client, "C1", "1000.000000");+ assert.deepEqual(+ mirrored.raw.map((message) => message.ts),+ [messages[0]!, ...messages.slice(-199)].map((message) => message.ts),+ );+ const live = await createSlackHistoryReader({ core, ids, source: "shadow" })(client, "C1", "1000.000000");+ assert.deepEqual(live.raw, messages.slice(0, 200));+ await new Promise((resolve) => setImmediate(resolve));+ const comparison = JSON.parse(logs[0]!);+ assert.equal(comparison.liveMessages, 200);+ assert.equal(comparison.mirroredMessages, 200);+ assert.equal(comparison.liveMessagesMissingFromMirror, 20);+ assert.equal(comparison.mirrorMessagesOutsideLiveWindow, 20);+ assert.equal(comparison.matchingMessages, 180);+ assert.equal(comparison.liveComplete, false);+ await cache.close();+});++test("shadow freezes live messages and attachments before callers modify their context", async (t) => {+ const logs: string[] = [];+ t.mock.method(console, "info", (line: string) => logs.push(line));+ let release!: () => void;+ const pending = new Promise<void>((resolve) => {+ release = resolve;+ });+ const stored = [{ container: "C1", ts: "1", text: "original", files: [{ fileId: "F1", name: "original.txt" }] }];+ const core = {+ readSurfaceMessages: async () => {+ await pending;+ return stored;+ },+ rememberSurfaceHistory: async () => {+ throw new Error("must not backfill");+ },+ } as unknown as SlackCoreClient;+ const client = {+ conversations: {+ history: async () => ({ messages: [{ ts: "1", text: "original", files: [{ id: "F1", name: "original.txt" }] }] }),+ },+ };+ const live = await createSlackHistoryReader({ core, ids, source: "shadow" })(client, "C1");+ live.raw.push({ ts: "2", text: "trigger appended by caller" });+ live.raw[0]!.text = "changed by caller";+ live.raw[0]!.files![0]!.name = "changed.txt";+ release();+ await new Promise((resolve) => setImmediate(resolve));+ const comparison = JSON.parse(logs[0]!);+ assert.equal(comparison.liveMessages, 1);+ assert.equal(comparison.matchingMessages, 1);+ assert.equal(comparison.textMismatches, 0);+ assert.equal(comparison.fileMismatches, 0);+ assert.equal(comparison.liveMessagesMissingFromStorage, 0);+});