Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Preserve recent Slack context for human replies (#1271)

Regan Belld54c54c75e29
diff --git a/src/core/message-revisions.ts b/src/core/message-revisions.ts
index cc9ff9c5b..7ef83a102 100644
--- a/src/core/message-revisions.ts
+++ b/src/core/message-revisions.ts
@@ -16,9 +16,11 @@ export interface MessageRevisionPayload {
ts: string;
text?: string;
name?: string;
+ sourceRole?: "agent";
}
interface MessageRevisionSource {
+ self?: boolean;
ts: string;
deleted?: boolean;
text?: string;
@@ -44,6 +46,7 @@ export function messageRevision(e: Pick<SessionEntry, "type" | "payload">): Mess
export function renderMessageRevision(p: MessageRevisionPayload): string {
const attrs = [
`id="${xmlAttrEscape(p.ts)}"`,
+ ...(p.sourceRole ? [`from="${p.sourceRole}"`] : []),
...(p.name?.trim() ? [`author="${xmlAttrEscape(p.name.trim())}"`] : []),
...(isoFromTs(p.ts) ? [`sent-at="${isoFromTs(p.ts)}"`] : []),
].join(" ");
@@ -53,7 +56,7 @@ export function renderMessageRevision(p: MessageRevisionPayload): string {
}
function isRevisionEvent(e: IngestEvent): boolean {
- return !e.self && Boolean(e.ts) && (e.deleted === true || e.editedAt !== undefined);
+ return Boolean(e.ts) && (e.deleted === true || e.editedAt !== undefined);
}
export function hasRevisionEvents(events: readonly IngestEvent[]): boolean {
@@ -63,6 +66,7 @@ export function hasRevisionEvents(events: readonly IngestEvent[]): boolean {
interface OriginalMessage {
text: string;
name?: string;
+ sourceRole?: "agent";
}
function findOriginal(entries: readonly SessionEntry[], ts: string): OriginalMessage | null {
@@ -73,6 +77,7 @@ function findOriginal(entries: readonly SessionEntry[], ts: string): OriginalMes
ts?: unknown;
text?: unknown;
name?: unknown;
+ sourceRole?: unknown;
hidden?: unknown;
securityTainted?: unknown;
} | null;
@@ -80,6 +85,7 @@ function findOriginal(entries: readonly SessionEntry[], ts: string): OriginalMes
if (p.hidden === true || p.securityTainted === true) return null;
found = {
text: typeof p.text === "string" ? p.text : "",
+ ...(p.sourceRole === "agent" ? { sourceRole: "agent" as const } : {}),
...(typeof p.name === "string" && p.name.trim() ? { name: p.name.trim() } : {}),
};
}
@@ -91,13 +97,16 @@ function revisionToRecord(
source: MessageRevisionSource,
): MessageRevisionPayload | null {
const original = findOriginal(entries, source.ts);
- if (!original) return null;
+ if (!original || (source.self && original.sourceRole !== "agent")) return null;
let last: MessageRevisionPayload | null = null;
for (const e of entries) {
const r = messageRevision(e);
if (r && r.ts === source.ts) last = r;
}
- const named = original.name ? { name: original.name } : {};
+ const named = {
+ ...(original.name ? { name: original.name } : {}),
+ ...(original.sourceRole ? { sourceRole: original.sourceRole } : {}),
+ };
if (source.deleted) {
if (last?.action === "deleted") return null;
return { kind: "message_revision", action: "deleted", ts: source.ts, ...named };
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index 4275bbae7..395125af5 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -854,6 +854,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
entrySeq: imported.seq,
meta: {
overheard: true,
+ ...(overheard.sourceRole ? { sourceRole: overheard.sourceRole } : {}),
bareText: overheard.text,
ts: overheard.ts,
...(overheard.name ? { author: overheard.name } : {}),
@@ -2442,7 +2443,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
const importedOverheard: OverheardEntryPayload[] = [];
- if (!input.envelopeWrapped && input.overheard?.length) {
+ if ((!input.envelopeWrapped || humanTurn) && input.overheard?.length) {
const toImport = await transcripts
.forRender(session.id)
.then((read) => selectOverheardToImport(input.overheard!, recordedMessageTimestamps(read.entries)))
@@ -2476,6 +2477,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
entrySeq: imported.seq,
meta: {
overheard: true,
+ ...(p.sourceRole ? { sourceRole: p.sourceRole } : {}),
bareText: p.text,
ts: p.ts,
...(p.changeTime ? { changeTime: p.changeTime } : {}),
diff --git a/src/harness/context-compaction.ts b/src/harness/context-compaction.ts
index 43c1d7baa..2467f724c 100644
--- a/src/harness/context-compaction.ts
+++ b/src/harness/context-compaction.ts
@@ -135,6 +135,7 @@ export function compactTranscript(history: SessionEntry[]): string {
const op = entry.payload as {
text?: string;
overheard?: unknown;
+ sourceRole?: unknown;
name?: unknown;
files?: unknown;
isError?: unknown;
@@ -144,7 +145,7 @@ export function compactTranscript(history: SessionEntry[]): string {
const ovFiles = ov && Array.isArray(op?.files) ? (op!.files as unknown[]).map(String).filter(Boolean) : [];
const author = entry.type === "user" && typeof op?.name === "string" && op.name ? op.name : null;
const label = ov
- ? `overheard#${entry.seq}${stamp} (${author ?? "someone"})`
+ ? `overheard#${entry.seq}${stamp} (${op?.sourceRole === "agent" ? "agent" : (author ?? "someone")})`
: `${entry.type}#${entry.seq}${stamp}${author ? ` (${author})` : ""}`;
if (text) push(`${label}: ${text}${ovFiles.length ? ` (files: ${ovFiles.join(", ")})` : ""}`);
else if (ov && ovFiles.length) push(`${label}: (shared file) (files: ${ovFiles.join(", ")})`);
diff --git a/src/harness/replay.ts b/src/harness/replay.ts
index b6364eb8f..72b9ec53b 100644
--- a/src/harness/replay.ts
+++ b/src/harness/replay.ts
@@ -16,6 +16,7 @@ export interface SeededMessage {
export interface OverheardEntryPayload {
overheard: true;
+ sourceRole?: "agent";
ts: string;
changeTime?: string;
name?: string;
@@ -53,12 +54,15 @@ export function selectOverheardToImport(
return 0;
});
const out: OverheardEntryPayload[] = [];
+ const seen = new Set(imported);
for (const m of fresh) {
- if (m.role === "self") continue;
+ if (seen.has(m.ts!)) continue;
const text = String(m.text ?? "").trim();
if (!text && !m.files?.length) continue;
+ seen.add(m.ts!);
out.push({
overheard: true,
+ ...(m.role === "self" ? { sourceRole: "agent" as const } : {}),
ts: m.ts,
...(m.name ? { name: m.name } : {}),
text,
@@ -74,7 +78,7 @@ export function renderOverheard(p: OverheardEntryPayload): string {
return messageTag(
{
overheard: true,
- from: "human",
+ from: p.sourceRole ?? "human",
...(p.ts ? { id: p.ts, sentAt: isoFromTs(p.ts) } : {}),
...(p.name?.trim() ? { author: p.name.trim() } : {}),
...(p.mentions && Object.keys(p.mentions).length ? { mentions: p.mentions } : {}),
@@ -331,7 +335,7 @@ export function replayPreamble(history: SessionEntry[]): string {
else if (overheard) {
if (overheard.text.trim() || overheard.files?.length) {
lines.push(
- `Overheard (${overheard.name?.trim() || "someone"}): ${overheard.text}${overheard.files?.length ? ` (files: ${overheard.files.join(", ")})` : ""}`,
+ `Overheard (${overheard.sourceRole === "agent" ? "agent" : overheard.name?.trim() || "someone"}): ${overheard.text}${overheard.files?.length ? ` (files: ${overheard.files.join(", ")})` : ""}`,
);
}
} else if (e.type === "user" && textOf(e)) lines.push(`User: ${textOf(e)}`);
diff --git a/src/harness/tape-projection.ts b/src/harness/tape-projection.ts
index b6472e4eb..17ead3efb 100644
--- a/src/harness/tape-projection.ts
+++ b/src/harness/tape-projection.ts
@@ -126,6 +126,7 @@ function userDraft(row: TapeRecord, isTrigger: boolean): DraftEntry | null {
type: "user",
payload: {
overheard: true,
+ ...(meta.sourceRole ? { sourceRole: meta.sourceRole } : {}),
...(meta.ts ? { ts: meta.ts } : {}),
...(meta.changeTime ? { changeTime: meta.changeTime } : {}),
...(meta.author ? { name: meta.author } : {}),
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index cff7a0572..f5149a763 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -124,6 +124,7 @@ function rowToTape(r: Record<string, unknown>): TapeRecord {
...(r.change_time != null ? { changeTime: r.change_time as string } : {}),
...(r.hidden != null ? { hidden: Boolean(r.hidden) } : {}),
...(r.overheard != null ? { overheard: Boolean(r.overheard) } : {}),
+ ...(r.source_role === "agent" ? { sourceRole: "agent" as const } : {}),
...(r.author != null ? { author: r.author as string } : {}),
...(r.attachments != null ? { attachments: JSON.parse(r.attachments as string) as unknown[] } : {}),
...(r.display != null ? { display: r.display as string } : {}),
@@ -526,6 +527,13 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
ON CONFLICT (session_id, seq) DO NOTHING`,
],
},
+ {
+ id: "sessions/store/0016-tape-source-role",
+ statements: [
+ `SET LOCAL lock_timeout = '3s'`,
+ `ALTER TABLE session_tape ADD COLUMN IF NOT EXISTS source_role TEXT`,
+ ],
+ },
],
[
{
@@ -579,8 +587,8 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const stored = jsonbSafeStringify(rec.payload ?? null);
const createdAt = now();
await client.query(
- `INSERT INTO session_tape(session_id, seq, kind, harness, payload, scope_label, bare_text, ts, change_time, hidden, overheard, author, attachments, display, security_tainted, entry_created_at, entry_seq, covers_entry_seq, created_at)
- VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19)`,
+ `INSERT INTO session_tape(session_id, seq, kind, harness, payload, scope_label, bare_text, ts, change_time, hidden, overheard, author, attachments, display, security_tainted, entry_created_at, entry_seq, covers_entry_seq, created_at, source_role)
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20)`,
[
sessionId,
seq,
@@ -601,6 +609,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
rec.entrySeq ?? null,
rec.coversEntrySeq ?? null,
createdAt,
+ rec.meta?.sourceRole ?? null,
],
);
return { ...rec, payload: JSON.parse(stored), sessionId, seq, createdAt };
diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.ts
index 6c3c70dac..8109ed653 100644
--- a/src/sessions/session-store.ts
+++ b/src/sessions/session-store.ts
@@ -217,6 +217,7 @@ export interface TapeMeta {
changeTime?: string;
hidden?: boolean;
overheard?: boolean;
+ sourceRole?: "agent";
author?: string;
attachments?: unknown[];
display?: string;
diff --git a/src/slack/attachments.ts b/src/slack/attachments.ts
index b7eeb5a37..536f9aa63 100644
--- a/src/slack/attachments.ts
+++ b/src/slack/attachments.ts
@@ -1,7 +1,6 @@
import { sleep } from "./util.ts";
import { channelShareTs, parseUploadedFileIds, slackErrorCode } from "./payloads.ts";
import { BlobTooLargeError } from "../persistence/blob-transfer.ts";
-import { messageWithForwardedContent, type SlackMessageAttachment } from "./forwards.ts";
export interface IncomingAttachment {
name: string;
@@ -57,38 +56,6 @@ export function isOversize(file: Pick<SlackFile, "size">): boolean {
return typeof file.size === "number" && file.size > MAX_ATTACHMENT_BYTES;
}
-export interface ThreadMessage {
- user?: string;
- text?: string;
- ts?: string;
- bot_id?: string;
- subtype?: string;
- files?: SlackFile[];
- attachments?: SlackMessageAttachment[];
-}
-
-export function collectEarlierThreadFiles(
- messages: readonly ThreadMessage[],
- opts: { triggerTs: string; botUserId: string; ownBotId: string; have: readonly SlackFile[]; inThread: boolean },
-): SlackFile[] {
- if (!opts.inThread) return [];
- const seen = new Set<string>();
- for (const f of opts.have) if (f.id) seen.add(f.id);
- const out: SlackFile[] = [];
- for (let i = messages.length - 1; i >= 0; i--) {
- const m = messages[i]!;
- if (m.ts === opts.triggerTs) continue;
- const isBot = (m.user && m.user === opts.botUserId) || (opts.ownBotId !== "" && m.bot_id === opts.ownBotId);
- if (isBot) continue;
- for (const f of messageWithForwardedContent(m).files) {
- if (f.id && seen.has(f.id)) continue;
- if (f.id) seen.add(f.id);
- out.push(f.user || !m.user ? f : { ...f, user: m.user });
- }
- }
- return out;
-}
-
export function isTrustedSlackHost(url: string, extraHost?: string): boolean {
try {
const h = new URL(url).hostname.toLowerCase();
diff --git a/src/slack/conversation-view.ts b/src/slack/conversation-view.ts
index fe0b2a3e5..5e7abd180 100644
--- a/src/slack/conversation-view.ts
+++ b/src/slack/conversation-view.ts
@@ -9,7 +9,6 @@ import {
type SlackFile,
MAX_RECENT_MESSAGES,
MAX_TOP_LEVEL_CONTEXT_AGE_S,
- collectEarlierThreadFiles,
decodeSlackEntities,
isOversize,
recentWindow,
@@ -59,7 +58,7 @@ export interface ConversationSerializer {
kind?: SlackConversationKind;
slackIdsByPrincipal?: ReadonlyMap<string, string>;
},
- ): Promise<{ view: ConversationView; earlierFiles: SlackFile[] }>;
+ ): Promise<{ view: ConversationView }>;
recentMessageWindow: number;
}
@@ -173,7 +172,7 @@ export function createConversationSerializer(deps: {
kind?: SlackConversationKind;
slackIdsByPrincipal?: ReadonlyMap<string, string>;
},
- ): Promise<{ view: ConversationView; earlierFiles: SlackFile[] }> {
+ ): Promise<{ view: ConversationView }> {
const empty: ConversationView = {
channel: { kind: ctx.kind ?? inc.kind },
members: [],
@@ -182,8 +181,7 @@ export function createConversationSerializer(deps: {
files: [],
omittedFiles: [],
};
- if (ctx.audience.some((a) => a.isExternalGuest) && !(await externalParticipantsEnabled()))
- return { view: empty, earlierFiles: [] };
+ if (ctx.audience.some((a) => a.isExternalGuest) && !(await externalParticipantsEnabled())) return { view: empty };
const nameById = new Map<string, string>();
for (const a of ctx.audience) {
@@ -210,13 +208,6 @@ export function createConversationSerializer(deps: {
RECENT_MESSAGE_WINDOW,
inc.threadTs ? undefined : { triggerTs: inc.ts, maxAgeSeconds: MAX_TOP_LEVEL_CONTEXT_AGE_S },
);
- const earlierFiles = collectEarlierThreadFiles(raw, {
- triggerTs: inc.ts,
- botUserId: ids.botUserId,
- ownBotId: ids.ownBotId,
- have: inc.files,
- inThread: Boolean(inc.threadTs),
- });
const members =
ctx.audience.length > 0 && ctx.audience.length <= MEMBERS_SHOW_MAX
@@ -250,9 +241,8 @@ export function createConversationSerializer(deps: {
here = { kind: "top-level" };
}
- const allFiles = [...inc.files, ...earlierFiles];
- const files = allFiles.filter((f) => !isOversize(f)).map((f) => ({ name: slackFileName(f) }));
- const omittedFiles = allFiles
+ const files = inc.files.filter((f) => !isOversize(f)).map((f) => ({ name: slackFileName(f) }));
+ const omittedFiles = inc.files
.filter((f) => isOversize(f))
.map((f) => ({ name: slackFileName(f), reason: "too-big" as const }));
@@ -270,7 +260,7 @@ export function createConversationSerializer(deps: {
nameById,
...("note" in page && page.note ? { contextNote: page.note } : {}),
};
- return { view, earlierFiles };
+ return { view };
}
return { shapeRecentMessages, serializeSlackConversation, recentMessageWindow: RECENT_MESSAGE_WINDOW };
diff --git a/src/slack/forwards.ts b/src/slack/forwards.ts
index 2e8584b87..e3f6a2929 100644
--- a/src/slack/forwards.ts
+++ b/src/slack/forwards.ts
@@ -13,7 +13,7 @@ interface SlackMessageBlock {
message?: ForwardedMessage;
}
-export interface SlackMessageAttachment extends ForwardedMessage {
+interface SlackMessageAttachment extends ForwardedMessage {
fallback?: string;
author_name?: string;
author_subname?: string;
diff --git a/src/slack/lib.ts b/src/slack/lib.ts
index 386434e8c..fb294bed4 100644
--- a/src/slack/lib.ts
+++ b/src/slack/lib.ts
@@ -57,8 +57,6 @@ export {
hydrateSlackFiles,
MAX_ATTACHMENT_BYTES,
isOversize,
- type ThreadMessage,
- collectEarlierThreadFiles,
isTrustedSlackHost,
attachmentFromBytes,
MAX_ATTACHMENTS_PER_TURN,
diff --git a/src/slack/turn-handler.ts b/src/slack/turn-handler.ts
index 57db56064..450d7b623 100644
--- a/src/slack/turn-handler.ts
+++ b/src/slack/turn-handler.ts
@@ -431,7 +431,6 @@ export function createTurnHandler(deps: {
let overheard: OverheardMessage[] | undefined;
let detectContext: string | undefined;
let detectOpener: string | undefined;
- let earlierFiles: SlackFile[] = [];
if (inc.kind === "channel" || (inc.kind === "dm" && inc.threadTs)) {
const serialize = () =>
serializer.serializeSlackConversation(client, inc, {
@@ -450,7 +449,6 @@ export function createTurnHandler(deps: {
serialize,
)
: await serialize();
- earlierFiles = serialized.earlierFiles;
const rendered = renderConversationView(serialized.view);
if (rendered.header) conversationHeader = rendered.header;
if (rendered.priorTurns.length) priorTurns = rendered.priorTurns;
@@ -461,13 +459,10 @@ export function createTurnHandler(deps: {
}
const ownFiles = inc.files.map((f) => (f.user || !inc.userId ? f : { ...f, user: inc.userId }));
- const inboundFiles = await hydrateSlackFiles(
- earlierFiles.length ? [...ownFiles, ...earlierFiles] : ownFiles,
- async (id) => {
- const response = await client.files.info({ file: id });
- return response?.file as SlackFile | undefined;
- },
- );
+ const inboundFiles = await hydrateSlackFiles(ownFiles, async (id) => {
+ const response = await client.files.info({ file: id });
+ return response?.file as SlackFile | undefined;
+ });
const resolveFileAuthor = async (userId: string | undefined): Promise<string | undefined> =>
userId ? (await classifyUserCached(client, userId)).actor.displayName : undefined;
const { attachments, issues } = await processInboundFiles(
diff --git a/src/surface-cache/surface-cache.ts b/src/surface-cache/surface-cache.ts
index 307cdf324..cbf19a3d7 100644
--- a/src/surface-cache/surface-cache.ts
+++ b/src/surface-cache/surface-cache.ts
@@ -325,7 +325,6 @@ export function createPostgresSurfaceCache(
const conds = [
"org_id = $1",
"container = $2",
- "self = FALSE",
"((edited_at > 0 AND edited_at >= $3) OR (deleted_at > 0 AND deleted_at >= $3))",
];
const args: unknown[] = [orgId, container, since];
@@ -595,7 +594,7 @@ export function createMemorySurfaceCache(opts: { liveFallback?: LiveFallback } =
async revisedSince(container, since, o = {}) {
return [...containerMsgs(container).values()]
- .filter((m) => !m.self && revisedAt(m) > 0 && revisedAt(m) >= since)
+ .filter((m) => revisedAt(m) > 0 && revisedAt(m) >= since)
.filter((m) => !o.thread || m.sub === o.thread || m.ts === o.thread)
.sort((a, b) => revisedAt(b) - revisedAt(a) || compareTs(b, a))
.slice(0, REVISION_SCAN_LIMIT);
diff --git a/test/message-revisions.test.ts b/test/message-revisions.test.ts
index fc0553446..2760b2e08 100644
--- a/test/message-revisions.test.ts
+++ b/test/message-revisions.test.ts
@@ -429,3 +429,26 @@ test("an edit made before the message's own turn is still caught through the tri
assert.equal(slackTsToMs("t1"), undefined);
await sessions.releaseLease(lease!);
});
+
+test("imported agent posts receive attributed edits and deletions without creating assistant actions", async () => {
+ const sessions = createMemorySessionStore();
+ const session = await seedSession(sessions, `dm:${DM}`, "dm", [
+ { overheard: true, sourceRole: "agent", text: "Shall I restart it?", ts: "100.1" },
+ ]);
+ await recordMessageRevisions(sessions, [edit({ self: true, text: "Shall I restart the worker?" })]);
+ await recordMessageRevisions(sessions, [del({ self: true })]);
+ const entries = await sessions.getEntries(session.id);
+ const marks = revisions(entries);
+ assert.deepEqual(
+ marks.map((m) => [m.action, m.sourceRole]),
+ [
+ ["edited", "agent"],
+ ["deleted", "agent"],
+ ],
+ );
+ assert.match(renderMessageRevision(marks[0]!), /from="agent"/);
+ assert.equal(
+ reconstructMessagesFromHistory(entries).some((m) => m.role === "assistant"),
+ false,
+ );
+});
diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.ts
index 73bc59303..0d7646e74 100644
--- a/test/orchestrator.test.ts
+++ b/test/orchestrator.test.ts
@@ -1872,14 +1872,15 @@ test("overheard messages are imported ONCE into the durable log, author-labeled,
assert.equal(r1.status, "ok");
assert.match(r1.reply ?? "", /Alice@100\.001: I posted a cat photo \[cat\.jpg\]/);
assert.match(r1.reply ?? "", /Bob@100\.003: love it/);
- assert.doesNotMatch(r1.reply ?? "", /nice cat/);
+ assert.match(r1.reply ?? "", /nice cat/);
const s1 = await app.getSession(r1.sessionId!);
const ov1 = overheardEntries(s1!.entries);
assert.deepEqual(
ov1.map((e) => (e.payload as { ts: string }).ts),
- ["100.001", "100.003"],
+ ["100.001", "100.002", "100.003"],
);
+ assert.equal((ov1[1]!.payload as { sourceRole: string }).sourceRole, "agent");
assert.equal((ov1[0]!.payload as { name: string }).name, "Alice");
assert.deepEqual((ov1[0]!.payload as { files: string[] }).files, ["cat.jpg"]);
@@ -1887,6 +1888,7 @@ test("overheard messages are imported ONCE into the durable log, author-labeled,
channel("!overheard", {
overheard: [
{ ts: "100.001", role: "user", name: "Alice", text: "I posted a cat photo", files: ["cat.jpg"] },
+ { ts: "100.002", role: "self", text: "nice cat" },
{ ts: "100.003", role: "user", name: "Bob", text: "love it" },
{ ts: "100.004", role: "user", name: "Carol", text: "me too" },
],
@@ -1900,7 +1902,7 @@ test("overheard messages are imported ONCE into the durable log, author-labeled,
const ov2 = overheardEntries(s2!.entries);
assert.deepEqual(
ov2.map((e) => (e.payload as { ts: string }).ts),
- ["100.001", "100.003", "100.004"],
+ ["100.001", "100.002", "100.003", "100.004"],
"append-only: each message recorded exactly once",
);
});
diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.ts
index 97827e23d..dfcc40392 100644
--- a/test/postgres-store.test.ts
+++ b/test/postgres-store.test.ts
@@ -377,10 +377,11 @@ test("pg latestEntrySeq, participant windows, and tape meta attachments round-tr
payload: { role: "user", content: [{ type: "text", text: "one" }] },
scopeLabel: scope,
entrySeq: 0,
- meta: { bareText: "one", attachments },
+ meta: { bareText: "one", attachments, sourceRole: "agent" },
});
const rows = await s.getTape(session.id);
assert.deepEqual(rows[0]!.meta?.attachments, attachments);
+ assert.equal(rows[0]!.meta?.sourceRole, "agent");
});
test("pg participant tenure remains exact when every event shares a timestamp", { skip }, async () => {
diff --git a/test/postgres-surface-cache.test.ts b/test/postgres-surface-cache.test.ts
index bfdcb314b..3b298ed6b 100644
--- a/test/postgres-surface-cache.test.ts
+++ b/test/postgres-surface-cache.test.ts
@@ -252,7 +252,7 @@ test("pg surface-cache: revisedSince returns edits and deletions after the water
}
});
-test("pg surface-cache: revisedSince skips self edits and scopes to a thread", { skip }, async () => {
+test("pg surface-cache: revisedSince includes self edits and scopes to a thread", { skip }, async () => {
const cache = createPostgresSurfaceCache(URL!);
const container = `Cthr-${Date.now()}`;
try {
@@ -270,7 +270,7 @@ test("pg surface-cache: revisedSince skips self edits and scopes to a thread", {
]);
assert.deepEqual(
(await cache.revisedSince(container, 0)).map((m) => m.ts),
- ["2.0", "1.5", "1.0"],
+ ["2.5", "2.0", "1.5", "1.0"],
);
assert.deepEqual(
(await cache.revisedSince(container, 0, { thread: "1.0" })).map((m) => m.ts),
diff --git a/test/replay.test.ts b/test/replay.test.ts
index b3ce40b63..55a80f1d2 100644
--- a/test/replay.test.ts
+++ b/test/replay.test.ts
@@ -361,7 +361,7 @@ test("a message answered as a trigger is NOT re-imported as overheard next turn
);
});
-test("selectOverheardToImport keeps every not-yet-imported message, in order, dropping the bot's own", () => {
+test("selectOverheardToImport keeps every not-yet-imported message, in order, retaining agent authorship", () => {
const incoming: OverheardMessage[] = [
{ ts: "100.001", role: "user", name: "Alice", text: "already imported" },
{ ts: "100.005", role: "user", name: "Bob", text: "fresh from bob" },
@@ -371,10 +371,12 @@ test("selectOverheardToImport keeps every not-yet-imported message, in order, dr
const picked = selectOverheardToImport(incoming, new Set(["100.001", "100.003"]));
assert.deepEqual(
picked.map((p) => p.ts),
- ["100.005", "100.006"],
+ ["100.004", "100.005", "100.006"],
);
- assert.equal(picked[0]!.name, "Bob");
- assert.equal(picked[1]!.name, "Carol");
+ assert.equal(picked[0]!.sourceRole, "agent");
+ assert.match(renderOverheard(picked[0]!), /from="agent"/);
+ assert.equal(picked[1]!.name, "Bob");
+ assert.equal(picked[2]!.name, "Carol");
assert.ok(picked.every((p) => p.overheard === true));
});
@@ -516,3 +518,18 @@ test("reconstructMessagesFromHistory replays the environment note the user messa
{ type: "text", text: "what's on today?\n\n<environment>\nIt is Monday 9am\n</environment>" },
]);
});
+
+test("overheard reconciliation deduplicates IDs within a batch but preserves identical text at different IDs", () => {
+ const incoming: OverheardMessage[] = [
+ { ts: "100.001", role: "self", text: "Shall I proceed?" },
+ { ts: "100.001", role: "self", text: "Shall I proceed?" },
+ { ts: "100.002", role: "user", name: "Alex", text: "yes" },
+ { ts: "100.003", role: "user", name: "Blair", text: "yes" },
+ ];
+ const picked = selectOverheardToImport(incoming, new Set());
+ assert.deepEqual(
+ picked.map((p) => p.ts),
+ ["100.001", "100.002", "100.003"],
+ );
+ assert.deepEqual(selectOverheardToImport(incoming, new Set(picked.map((p) => p.ts))), []);
+});
diff --git a/test/slack-attachments.test.ts b/test/slack-attachments.test.ts
index 34ba40c53..2cc82cfa7 100644
--- a/test/slack-attachments.test.ts
+++ b/test/slack-attachments.test.ts
@@ -6,7 +6,6 @@ import {
isTrustedSlackHost,
downloadSlackFile,
processInboundFiles,
- collectEarlierThreadFiles,
uploadAttachments,
uploadFailureNote,
MAX_ATTACHMENT_BYTES,
@@ -176,51 +175,6 @@ test("processInboundFiles caps the number of files per turn", async () => {
assert.ok(r.issues.some((i) => /too many files/.test(i)));
});
-test("collectEarlierThreadFiles returns earlier files, recent-first, deduped and filtered", () => {
- const messages = [
- { ts: "1", user: "U1", files: [{ id: "Fa", name: "old.png" }] },
- { ts: "2", user: "BOT", bot_id: "B1", files: [{ id: "Fbot", name: "bot-made.png" }] },
- {
- ts: "3",
- user: "U2",
- files: [
- { id: "Fb", name: "graph.png" },
- { id: "Fdup", name: "dup.png" },
- ],
- },
- { ts: "4", user: "U1", files: [{ id: "Fdup", name: "dup.png" }] },
- { ts: "5", user: "U1", files: [{ id: "Ftrigger", name: "current.png" }] },
- ];
- const out = collectEarlierThreadFiles(messages, {
- triggerTs: "5",
- botUserId: "BOT",
- ownBotId: "B1",
- have: [{ id: "Ftrigger", name: "current.png" }],
- inThread: true,
- });
- assert.deepEqual(
- out.map((f) => f.id),
- ["Fdup", "Fb", "Fa"],
- );
-});
-
-test("collectEarlierThreadFiles stamps each file with its poster, preserving an explicit file.user", () => {
- const messages = [
- { ts: "1", user: "U1", files: [{ id: "Fa", name: "a.png" }] },
- { ts: "2", user: "U2", files: [{ id: "Fb", name: "b.png", user: "Uexplicit" }] },
- ];
- const out = collectEarlierThreadFiles(messages, {
- triggerTs: "9",
- botUserId: "BOT",
- ownBotId: "",
- have: [],
- inThread: true,
- });
- const userById = Object.fromEntries(out.map((f) => [f.id, f.user]));
- assert.equal(userById["Fa"], "U1");
- assert.equal(userById["Fb"], "Uexplicit");
-});
-
test("processInboundFiles attributes each file via resolveAuthor(file.user)", async () => {
const r = await processInboundFiles(
[{ name: "x.png", user: "Utay" }],
@@ -232,34 +186,6 @@ test("processInboundFiles attributes each file via resolveAuthor(file.user)", as
assert.equal(r.attachments[0]!.author, "taylor");
});
-test("collectEarlierThreadFiles returns nothing when only the trigger has files", () => {
- const messages = [{ ts: "9", user: "U1", files: [{ id: "Fx", name: "x.png" }] }];
- const out = collectEarlierThreadFiles(messages, {
- triggerTs: "9",
- botUserId: "BOT",
- ownBotId: "",
- have: [{ id: "Fx" }],
- inThread: true,
- });
- assert.equal(out.length, 0);
-});
-
-test("collectEarlierThreadFiles ignores channel backscroll for a top-level trigger", () => {
- const messages = [
- { ts: "1", user: "U1", files: [{ id: "Fold1", name: "screenshot1.png" }] },
- { ts: "2", user: "U2", files: [{ id: "Fold2", name: "logo.png" }] },
- { ts: "3", user: "U3", files: [{ id: "Ftrigger", name: "trigger.png" }] },
- ];
- const out = collectEarlierThreadFiles(messages, {
- triggerTs: "3",
- botUserId: "BOT",
- ownBotId: "",
- have: [],
- inThread: false,
- });
- assert.equal(out.length, 0);
-});
-
const noArtifacts = async (): Promise<Buffer> => {
throw new Error("no artifact fallback in this test");
};
diff --git a/test/slack-context-reconciliation.test.ts b/test/slack-context-reconciliation.test.ts
new file mode 100644
index 000000000..9d0dbc33a
--- /dev/null
+++ b/test/slack-context-reconciliation.test.ts
@@ -0,0 +1,95 @@
+import "./support/auto-fake-sprites.ts";
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { buildApp } from "../src/wiring.ts";
+import { testConfig } from "./support/test-config.ts";
+import type { TurnRequest } from "../src/types.ts";
+
+function reply(): Response {
+ const events = [
+ {
+ type: "message_start",
+ message: {
+ id: "msg_test",
+ type: "message",
+ role: "assistant",
+ content: [],
+ model: "claude-test",
+ stop_reason: null,
+ usage: { input_tokens: 5, output_tokens: 0 },
+ },
+ },
+ { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
+ { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Acknowledged." } },
+ { type: "content_block_stop", index: 0 },
+ { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } },
+ { type: "message_stop" },
+ ];
+ return new Response(events.map((e) => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(""), {
+ headers: { "content-type": "text/event-stream" },
+ });
+}
+
+for (const warm of [false, true]) {
+ test(`${warm ? "existing" : "fresh"} human thread includes the approval referent in the provider request and deduplicates after harness reset`, async () => {
+ const built = buildApp(
+ testConfig({ harness: "pi", anthropicApiKey: "sk-test", securityScreenBackend: "off", sessionTapeMode: "serve" }),
+ );
+ const requests: string[] = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {
+ requests.push(String(init?.body));
+ return reply();
+ }) as typeof fetch;
+ const base: TurnRequest = {
+ surface: "slack",
+ actor: { externalId: "U1" },
+ conversation: {
+ kind: "channel",
+ threadRef: `ch:C1:${warm ? "existing" : "fresh"}`,
+ channelRef: "C1",
+ audience: [{ externalId: "U1" }],
+ },
+ origin: { kind: "human" },
+ text: "go",
+ envelopeWrapped: true,
+ };
+ try {
+ if (warm) {
+ assert.equal((await built.app.turn({ ...base, text: "Can you check the worker?" })).status, "ok");
+ }
+ const input: TurnRequest = {
+ ...base,
+ overheard: [
+ { ts: "100.001", role: "user", name: "Alex", text: "The worker needs restarting." },
+ { ts: "100.002", role: "self", text: "Shall I restart the worker?" },
+ { ts: "100.003", role: "user", name: "Blair", text: "Alex, can you approve the restart above?" },
+ ],
+ };
+ const first = await built.app.turn(input);
+ assert.equal(first.status, "ok", first.reason);
+ const messages = JSON.parse(requests.at(-1)!).messages as Array<{ role: string; content: unknown }>;
+ const quoted = messages.filter((m) => JSON.stringify(m.content).includes("Shall I restart the worker?"));
+ assert.equal(quoted.length, 1);
+ assert.equal(quoted[0]!.role, "user");
+ assert.match(JSON.stringify(quoted[0]!.content), /from=\\"agent\\"/);
+ const requestText = JSON.stringify(messages);
+ assert.match(requestText, /The worker needs restarting/);
+ assert.match(requestText, /Alex, can you approve/);
+ await built.runtime.stop();
+ const second = await built.app.turn({ ...input, text: "yes, go ahead" });
+ assert.equal(second.status, "ok", second.reason);
+ const next = JSON.stringify(JSON.parse(requests.at(-1)!).messages);
+ assert.equal(next.split("Shall I restart the worker?").length - 1, 1);
+ const session = await built.app.getSession(first.sessionId!);
+ const imports = session!.entries.filter(
+ (e) => e.type === "user" && (e.payload as { overheard?: boolean }).overheard,
+ );
+ assert.equal(imports.length, 3);
+ assert.equal((imports[1]!.payload as { sourceRole?: string }).sourceRole, "agent");
+ } finally {
+ globalThis.fetch = originalFetch;
+ await built.runtime.stop();
+ }
+ });
+}
diff --git a/test/slack-mirror-context.test.ts b/test/slack-mirror-context.test.ts
index 4a9b10025..592f4e3fe 100644
--- a/test/slack-mirror-context.test.ts
+++ b/test/slack-mirror-context.test.ts
@@ -322,3 +322,81 @@ test("shared-app shadow compares equal pages while mirror reads retain stored hi
assert.equal(comparison.mirrorMessagesOutsideLiveWindow, 0);
assert.equal(comparison.mirrorHasMore, true);
});
+
+test("thread history keeps file references and authors but only current files are turn attachments", async () => {
+ const serializer = createConversationSerializer({
+ ids,
+ directory,
+ externalParticipantsEnabled: async () => false,
+ readHistory: async () => ({
+ hasMore: false,
+ raw: [
+ { ts: "1000.000001", user: "U1", text: "Review the notes", files: [{ id: "FOLD", name: "old-notes.txt" }] },
+ {
+ ts: "1000.000002",
+ thread_ts: "1000.000001",
+ user: "UBOT",
+ bot_id: "BBOT",
+ text: "Shall I summarize those notes?",
+ },
+ ],
+ }),
+ });
+ const { view } = await serializer.serializeSlackConversation(
+ {},
+ {
+ kind: "channel",
+ channel: "C1",
+ threadTs: "1000.000001",
+ ts: "1000.000003",
+ rawText: "yes",
+ userId: "U2",
+ files: [{ id: "FCURRENT", name: "current.txt" }],
+ },
+ { audience: [] },
+ );
+ assert.deepEqual(view.files, [{ name: "current.txt" }]);
+ const rendered = renderConversationView(view);
+ assert.equal(rendered.overheard[0]?.name, "U1");
+ assert.deepEqual(rendered.overheard[0]?.files, ["old-notes.txt"]);
+ assert.equal(rendered.overheard[1]?.role, "self");
+ assert.match(rendered.overheard[1]!.text, /Shall I summarize/);
+});
+
+test("automatic context keeps the thread parent and recent exchange but excludes old channel chatter", async () => {
+ const raw = [
+ { ts: "1.000000", user: "U1", text: "ancient unrelated message" },
+ { ts: "100000.000000", user: "U1", text: "thread parent" },
+ ...Array.from({ length: 50 }, (_, i) => ({
+ ts: `${100001 + i}.000000`,
+ user: "U2",
+ thread_ts: "100000.000000",
+ text: `reply ${i}`,
+ })),
+ ];
+ const serializer = createConversationSerializer({
+ ids,
+ directory,
+ externalParticipantsEnabled: async () => false,
+ readHistory: async () => ({ raw, hasMore: false }),
+ });
+ const { view } = await serializer.serializeSlackConversation(
+ {},
+ {
+ kind: "channel",
+ channel: "C1",
+ ts: "100060.000000",
+ rawText: "go",
+ userId: "U1",
+ files: [],
+ },
+ { audience: [] },
+ );
+ assert.equal(view.messages.length, serializer.recentMessageWindow + 1);
+ assert.equal(view.messages[0]?.text, "thread parent");
+ assert.equal(view.messages.at(-2)?.text, "reply 49");
+ assert.equal(
+ view.messages.some((m) => m.text === "ancient unrelated message" || m.text === "reply 0"),
+ false,
+ );
+});
diff --git a/test/slack-mirror-fidelity.test.ts b/test/slack-mirror-fidelity.test.ts
index e32417855..a23129d89 100644
--- a/test/slack-mirror-fidelity.test.ts
+++ b/test/slack-mirror-fidelity.test.ts
@@ -266,12 +266,28 @@ for (const postgres of [false, true]) {
{ audience: [] },
);
views.push(result.view);
+ const current = await serializer.serializeSlackConversation(
+ client,
+ {
+ kind: "channel",
+ channel: container,
+ threadTs: "1000",
+ ts: "1001",
+ rawText: "question",
+ userId: "U1",
+ files: raw.files,
+ },
+ { audience: [] },
+ );
+ assert.deepEqual(current.view.omittedFiles, [{ name: "Large report", reason: "too-big" }]);
}
assert.deepEqual(views[1]!.messages, views[0]!.messages);
assert.equal(views[1]!.messages[0]!.authorId, "BOTHER");
assert.equal(views[1]!.messages[0]!.name, "Report bot");
assert.deepEqual(views[1]!.files, views[0]!.files);
- assert.deepEqual(views[1]!.omittedFiles, [{ name: "Large report", reason: "too-big" }]);
+ assert.deepEqual(views[1]!.messages[0]!.files, ["Large report"]);
+ assert.deepEqual(views[1]!.files, []);
+ assert.deepEqual(views[1]!.omittedFiles, []);
await cache.ingest([{ container, ts: "1000", deleted: true }]);
await cache.ingest([{ ...event, botId: "BWRONG", editedAt: 30 }]);
assert.equal((await cache.readMessages(container, { includeDeleted: true }))[0]!.botId, "BOTHER");
diff --git a/test/surface-cache.test.ts b/test/surface-cache.test.ts
index 9d1fbfa4c..97dd87514 100644
--- a/test/surface-cache.test.ts
+++ b/test/surface-cache.test.ts
@@ -342,7 +342,7 @@ test("revisedSince returns messages edited or deleted after the watermark, stamp
);
});
-test("revisedSince skips the bot's own edits and can be scoped to one thread", async () => {
+test("revisedSince includes the bot's own edits and can be scoped to one thread", async () => {
const cache = createMemorySurfaceCache();
await cache.ingest([
{ container: "Ct", ts: "1.0", text: "root", createdAt: 1 },
@@ -358,7 +358,7 @@ test("revisedSince skips the bot's own edits and can be scoped to one thread", a
]);
assert.deepEqual(
(await cache.revisedSince("Ct", 0)).map((m) => m.ts),
- ["2.0", "1.5", "1.0"],
+ ["2.5", "2.0", "1.5", "1.0"],
);
assert.deepEqual(
(await cache.revisedSince("Ct", 0, { thread: "1.0" })).map((m) => m.ts),
@@ -366,7 +366,7 @@ test("revisedSince skips the bot's own edits and can be scoped to one thread", a
);
assert.deepEqual(
(await cache.revisedSince("Ct", 0, { thread: "2.0" })).map((m) => m.ts),
- ["2.0"],
+ ["2.5", "2.0"],
);
});
diff --git a/test/tape-projection.test.ts b/test/tape-projection.test.ts
index 892705e62..93a1a990c 100644
--- a/test/tape-projection.test.ts
+++ b/test/tape-projection.test.ts
@@ -349,6 +349,37 @@ test("overheard import and delivery note project to their entry shapes", async (
await assertParity(sim);
});
+test("agent-authored overheard import and delivery note project to their entry shapes", async () => {
+ const sim = await simSession();
+ const overheard: OverheardEntryPayload = {
+ overheard: true,
+ sourceRole: "agent",
+ ts: "1720000000.000050",
+ text: "Shall I restart the worker?",
+ };
+ const imported = await sim.store.append(sim.lease, { type: "user", payload: overheard, scopeLabel: scope });
+ await sim.store.appendTape(sim.lease, {
+ kind: "message",
+ payload: { role: "user", content: [{ type: "text", text: renderOverheard(overheard) }], timestamp: CLOCK },
+ scopeLabel: scope,
+ entrySeq: imported.seq,
+ meta: {
+ overheard: true,
+ sourceRole: "agent",
+ bareText: overheard.text,
+ ts: overheard.ts,
+ author: overheard.name,
+ entryCreatedAt: imported.createdAt,
+ },
+ });
+ await simTurn(sim, {
+ input: "yes, restart it",
+ reply: "Restarted.",
+ deliveryFiles: [{ name: "log.txt", mimetype: "text/plain", sizeBytes: 42, artifactId: "art_1" }],
+ });
+ await assertParity(sim);
+});
+
test("a security-flagged overheard import keeps its taint and text", async () => {
const sim = await simSession();
const overheard: OverheardEntryPayload = {