Multiplayer agent harness for work.
Keep inbox items current from Gmail and Slack events (#2374)
Joshua Franceea15103f8f3e
diff --git a/docs/loop-ingestion.md b/docs/loop-ingestion.mdindex fa4e4f447..df4a9fcf4 100644--- a/docs/loop-ingestion.md+++ b/docs/loop-ingestion.md@@ -56,3 +56,17 @@ Keep background work enabled: ingestion maintenance runs alongside the schedulerThe Loop's Ingestion panel shows the last receipt and processing error. A 202 acknowledgment followed by an error means the event is still durable and will retry or remains represented by a queued/parked ledger item. A failed Loop may require operator attention under its existing governor and retry limits. Watch renewal errors retry after five minutes.Do not paste webhook signing secrets into chat, playbooks, or committed configuration. The secret is returned only on creation for generic webhooks and is omitted from later listing responses.++## Conversation events (inbox)++Every new message in a conversation already on an inbox ledger is an event, and+every event is judged by the Loop's work stage. Nothing resolves an item in code.++- **Sources.** QM's Slack bot, Gmail history (INBOX and SENT), the inbox open-time+ source check, and the scheduled sync (`POST /v1/loops/:id/events`).+- **Queue.** Events land durably in a per-Loop internal `conversation` ingress+ source, deduplicated by conversation and message time. They merge+ `sourcePayload.latestEvent` into the existing item and refresh `sourceAt`, which+ reopens a resolved item. Events never create items.+- **Decision.** The work stage returns a draft, or+ `{"resolution":{"kind":"replied"|"no_reply_needed"}}`, which dismisses the item.diff --git a/plugins/web-ui/src/inbox.ts b/plugins/web-ui/src/inbox.tsindex 59e567fbe..5972f4e02 100644--- a/plugins/web-ui/src/inbox.ts+++ b/plugins/web-ui/src/inbox.ts@@ -139,6 +139,7 @@ export interface InboxItem {dismissedAt?: number;repliedAt?: number;externalReplyText?: string;+ noReplyNeeded?: { reason?: string };reactions?: string[];probablyResolved?: boolean;automated?: boolean;@@ -460,6 +461,9 @@ export function toInboxItem(entry: LedgerItem): InboxItem {...(resolved === "dismissed" && entry.actedAt !== undefined ? { dismissedAt: entry.actedAt } : {}),...(resolved === "replied" && entry.actedAt !== undefined ? { repliedAt: entry.actedAt } : {}),...(resolved === "replied" && entry.actionResult ? { externalReplyText: entry.actionResult } : {}),+ ...(entry.actionKind === "no_reply_needed"+ ? { noReplyNeeded: entry.actionResult ? { reason: entry.actionResult } : {} }+ : {}),};}@@ -1454,7 +1458,7 @@ function reactionChipTpl(name: string): TemplateResult {function handledStateLabel(item: InboxItem): string {if (item.status === "sent") return "Sent";if (item.status === "replied") return "Replied";- return "Dismissed";+ return item.noReplyNeeded ? "No reply needed" : "Dismissed";}function itemSideMark(item: InboxItem, handled: boolean): TemplateResult | typeof nothing {@@ -1486,8 +1490,14 @@ export function handledNoteTpl(item: InboxItem): TemplateResult {${item.externalReplyText ? html`<div class="inbox-replied-text">${slackTextTpl(item, item.externalReplyText)}</div>` : nothing}</div>`;}+ const label = `${handledStateLabel(item)} ${item.dismissedAt ? relTime(item.dismissedAt) : ""}`;+ if (item.noReplyNeeded?.reason)+ return html`<div class="inbox-handled-note inbox-replied-note">+ <div class="inbox-handled-line">${icon(X, 13)}<span>${label}</span> ${reopenButtonTpl(item)}</div>+ <div class="inbox-replied-text">${item.noReplyNeeded.reason}</div>+ </div>`;return html`<div class="inbox-handled-note">- ${icon(X, 13)}<span>Dismissed ${item.dismissedAt ? relTime(item.dismissedAt) : ""}</span>+ ${icon(X, 13)}<span>${label}</span>${reopenButtonTpl(item)}</div>`;}diff --git a/src/api/agent-api-catalog.ts b/src/api/agent-api-catalog.tsindex e5d45812c..5a53f744a 100644--- a/src/api/agent-api-catalog.ts+++ b/src/api/agent-api-catalog.ts@@ -215,7 +215,7 @@ const FAMILIES: AgentApiFamily[] = [method: "POST",path: "/v1/loops/:id/items/:itemId/action",summary:- 'act on a held item — body {kind, args?}. kind "edit" replaces the proposal ({args:{proposal}}), "dismiss" drops the item, "reopen" un-dismisses it, "replied" ({args:{text}}) closes an item the person already answered themselves outside QM and never demotes one sent from here, and any action the loop\'s source declares (e.g. "send", or Slack\'s "react" with {args:{name}}, which annotates the item without closing it) executes it and records the outcome',+ 'act on a held item — body {kind, args?}. kind "edit" replaces the proposal ({args:{proposal}}), "dismiss" drops the item, "reopen" un-dismisses it, and any action the loop\'s source declares (e.g. "send", or Slack\'s "react" with {args:{name}}, which annotates the item without closing it) executes it and records the outcome',},{method: "POST",@@ -223,6 +223,12 @@ const FAMILIES: AgentApiFamily[] = [summary:"chat about one held item — body {message}; the reply lands on the item's thread and may revise its proposal",},+ {+ method: "POST",+ path: "/v1/loops/:id/events",+ summary:+ 'report new messages in conversations already on the ledger — body {events:[{sourceKey, author:"owner"|"other", at, text?, from?}]}; each event is judged by the loop\'s work stage, which may close or redraft the item',+ },],},{diff --git a/src/api/routes/loop-ingress.ts b/src/api/routes/loop-ingress.tsindex 8bc545f83..850abfc25 100644--- a/src/api/routes/loop-ingress.ts+++ b/src/api/routes/loop-ingress.ts@@ -1,11 +1,27 @@-import { scopeId } from "../../types.ts";+import { z } from "zod";+import { parseScopeId, scopeId } from "../../types.ts";import { sendJson, readRawBody, PayloadTooLargeError } from "../http.ts";import { loadAdministrable } from "./loops.ts";import { isObj } from "./shared.ts";import { withActor, type ApiCtx, type BaseCtx, type Route } from "./route.ts";-import type { LoopIngress, IngressSetup } from "../../loops/ingress.ts";+import { conversationEvent, type LoopIngress, type IngressSetup } from "../../loops/ingress.ts";import { errMessage } from "../../util/errors.ts";+const eventsSchema = z.object({+ events: z+ .array(+ z.object({+ sourceKey: z.string().min(1),+ author: z.enum(["owner", "other"]),+ at: z.number().finite().positive(),+ text: z.string().optional(),+ from: z.string().optional(),+ }),+ )+ .min(1)+ .max(20),+});+function view(source: Omit<LoopIngress, "secret">, publicUrl?: string) {const { secret: _secret, ...safe } = source as LoopIngress;const path = `/v1/loop-ingress/${source.kind === "gmail" ? "gmail" : source.id}`;@@ -65,6 +81,38 @@ async function change(ctx: ApiCtx) {}}+async function events(ctx: ApiCtx) {+ const loaded = await loadAdministrable(ctx);+ if (!loaded) return;+ const ingress = ctx.deps.loopIngress;+ if (!ingress) return sendJson(ctx.res, 404, { error: "unavailable" });+ if (loaded.loop.state === "archived") return sendJson(ctx.res, 409, { error: "archived" });+ if (+ ctx.capability &&+ parseScopeId(loaded.loop.ownerScopeId).kind === "personal" &&+ ctx.capability.privateScope !== true+ )+ return sendJson(ctx.res, 403, {+ error: "forbidden",+ message: "personal loop events come only from its owner's scope",+ });+ const parsed = eventsSchema.safeParse(ctx.body);+ if (!parsed.success)+ return sendJson(ctx.res, 400, {+ error: "bad_request",+ message: 'body needs {events: 1-20 of {sourceKey, author: "owner"|"other", at (ms), text?, from?}}',+ });+ const entries = parsed.data.events.map((event) =>+ conversationEvent({ loopId: loaded.loop.id, sourceKey: event.sourceKey }, event),+ );+ const queued = await ingress.enqueueConversation(loaded.loop, entries);+ return sendJson(+ ctx.res,+ queued ? 202 : 409,+ queued ? { ok: true, queued: entries.length } : { error: "not_accepting" },+ );+}+async function receive(ctx: BaseCtx) {if (!ctx.deps.loopIngress) {ctx.req.resume();@@ -87,6 +135,7 @@ export const loopIngressRoutes: ReadonlyArray<Route<ApiCtx>> = [{ method: "POST", path: "/v1/loops/:id/ingestion", auth: "source", handle: change },{ method: "PATCH", path: "/v1/loops/:id/ingestion/:sourceId", auth: "source", handle: change },]),+ { method: "POST", path: "/v1/loops/:id/events", auth: "either", handle: events },];export const loopIngressRawRoutes: ReadonlyArray<Route<BaseCtx>> = [diff --git a/src/api/routes/loop-items.ts b/src/api/routes/loop-items.tsindex 3f8fbed18..97f373bc2 100644--- a/src/api/routes/loop-items.ts+++ b/src/api/routes/loop-items.ts@@ -36,7 +36,6 @@ const MAX_ITEMS_PER_INGEST = 50;const MAX_SOURCE_PAYLOAD_BYTES = 64_000;const MAX_PROPOSAL_BYTES = 32_000;const MAX_FOLLOWUP_CHARS = 4_000;-const MAX_EXTERNAL_REPLY_CHARS = 500;function jsonSize(value: unknown): number {return JSON.stringify(value)?.length ?? 0;@@ -353,20 +352,6 @@ async function actOnItem(ctx: ApiCtx): Promise<void> {return sendJson(ctx.res, 200, { item: ledgerItemView(next, loop, !!ctx.capability) });}- if (kind === "replied") {- const text = typeof args.text === "string" ? args.text.trim().slice(0, MAX_EXTERNAL_REPLY_CHARS) : "";- const next = await deps.items.recordAction(item.id, {- kind,- outcome: "dismissed",- ...(typeof args.sourceAt === "number" ? { sourceAt: args.sourceAt } : {}),- ...(text ? { result: text } : {}),- });- if (!next) {- return sendJson(ctx.res, 409, { error: "conflict", message: "this item was already actioned from here" });- }- return sendJson(ctx.res, 200, { item: ledgerItemView(next, loop, !!ctx.capability) });- }-if (kind === "send" && item.status !== "ready") {return sendJson(ctx.res, 409, { error: "not_ready", message: "This item needs a fresh draft before sending" });}diff --git a/src/loops/gmail-push.ts b/src/loops/gmail-push.tsindex a84ca567e..56d9177e9 100644--- a/src/loops/gmail-push.ts+++ b/src/loops/gmail-push.ts@@ -1,6 +1,6 @@import { createRemoteJWKSet, jwtVerify } from "jose";import type { ConnectorTokenSource } from "./sources/adapter.ts";-import type { IngestEntryInput } from "./item-ledger.ts";+import { conversationEvent, type IngressEntry } from "./ingress.ts";import { GmailApiError, gmailApi } from "../connectors/gmail-api.ts";export interface GmailPushConfig {@@ -52,7 +52,7 @@ export function createGmailPushClient(tokens: ConnectorTokenSource, config: Gmaiconst request = await client(owner);const profile = await request<{ emailAddress: string }>("profile");const watch = await request<{ historyId: string; expiration: string }>("watch", {- body: { topicName: config.topic, labelIds: ["INBOX"], labelFilterBehavior: "include" },+ body: { topicName: config.topic, labelIds: ["INBOX", "SENT"], labelFilterBehavior: "include" },});if (!profile.emailAddress || !/^\d+$/.test(watch.historyId) || !Number.isFinite(Number(watch.expiration)))throw new Error("Gmail returned an invalid watch response");@@ -66,7 +66,7 @@ export function createGmailPushClient(tokens: ConnectorTokenSource, config: Gmaiowner: string,loopId: string,cursor: GmailCursor,- ingest: (entries: IngestEntryInput[]) => Promise<unknown>,+ ingest: (entries: IngressEntry[]) => Promise<unknown>,): Promise<string> {const request = await client(owner);const profile = await request<{ emailAddress: string; historyId: string }>("profile");@@ -81,11 +81,21 @@ export function createGmailPushClient(tokens: ConnectorTokenSource, config: Gmaiif (error instanceof GmailApiError && error.status === 404) continue;throw error;}- if (!message.labelIds?.includes("INBOX")) continue;+ const sent = message.labelIds?.includes("SENT") === true;+ if (!sent && !message.labelIds?.includes("INBOX")) continue;const headers = message.payload?.headers ?? [];const header = (name: string) => headers.find((entry) => entry.name.toLowerCase() === name)?.value;const at = Number(message.internalDate);if (!message.threadId || !Number.isFinite(at)) throw new Error("Gmail returned invalid message metadata");+ if (sent) {+ await ingest([+ conversationEvent(+ { loopId, sourceKey: message.threadId, source: "gmail" },+ { author: "owner", at, ...(message.snippet ? { text: message.snippet } : {}) },+ ),+ ]);+ continue;+ }await ingest([{loopId,diff --git a/src/loops/inbox-loop.ts b/src/loops/inbox-loop.tsindex 7006d25df..597ebde6c 100644--- a/src/loops/inbox-loop.ts+++ b/src/loops/inbox-loop.ts@@ -1,13 +1,15 @@-import type { Loop } from "../types.ts";+import type { Loop, LoopItem } from "../types.ts";+import type { LoopItemLedger } from "./item-ledger.ts";import { scopeId } from "../types.ts";import type { LoopStore } from "./loop-store.ts";-import { SOURCE_IDS } from "./sources/index.ts";+import { SOURCE_IDS, adapterForItem } from "./sources/index.ts";+import { EVENT_TEXT_CHARS, type LatestEvent } from "./ingress.ts";const INBOX_LOOP_NAME = "Inbox";const INBOX_LOOP_SURFACE = "inbox";-export const INBOX_SYNC_TASK_VERSION = 6;+export const INBOX_SYNC_TASK_VERSION = 7;export const INBOX_SYNC_DEFAULT_EVERY_MS = 15 * 60 * 1000;@@ -21,8 +23,8 @@ const INBOX_SUCCESS_CONDITION =export function renderInboxSyncTask(loopId: string): string {return `Inbox sync v${INBOX_SYNC_TASK_VERSION}. Keep the user's QM inbox loop current: every Slack message and email genuinely WAITING ON A REPLY FROM THEM gets a ledger item with a ready-to-send draft in their own voice. The inbox UI is the only delivery surface — never message the user, and NEVER send a reply yourself; you produce drafts only.-1. GET $AGENT_API_URL/v1/loops/${loopId}/items (agent capability header) — note existing items: skip complete items already tracked unless a NEWER inbound message has arrived since (compare sourceAt); re-post such items with the fresh message and a fresh draft. Refresh an agent draft when sourceAt is newer than proposal.at, even if the ledger already contains the latest message; realtime updates refresh message data before drafting. Preserve human-edited drafts. Repair incomplete open items first: an existing dedupeKey with missing sourcePayload, title, snippet, source metadata, or proposal is unfinished work, not a reason to skip it. Re-read its original conversation using the dedupeKey and summary, then ingest the complete item and draft under that same dedupeKey even when no newer message arrived. Never invent message content from a summary; if the source is unavailable, leave the item pending for retry.- - Closed-loop detection: BEFORE scanning for new items, re-read the exact source conversation of EVERY unresolved item (held, pending, processed, or failed), regardless of lastScanAt, age, read/unread state, or whether Gmail still labels it INBOX. The intake watermark is NOT a reconciliation watermark. If the LATEST message is FROM THE USER THEMSELVES — their own Slack userId on Slack, or their own email address in the From line on Gmail — they already replied outside QM, so POST $AGENT_API_URL/v1/loops/${loopId}/items/<itemId>/action with {"kind":"replied","args":{"text":"<their message, <=200 chars>","sourceAt":<reply timestamp in ms>}} to close it. Ignore Gmail drafts and recognize SENT messages, including send-as aliases. Only close when the reply is newer than the waiting message and there is no later inbound ask. If a source read fails or is incomplete, retain the item and retry later; do not treat it as resolved. This applies to both Gmail and Slack items. Do NOT reopen or redraft an item the user has already answered.+1. GET $AGENT_API_URL/v1/loops/${loopId}/items (agent capability header) — note existing items: skip complete items already tracked; a newer message in a tracked conversation is reported as an event (below), never re-posted or redrafted here. Preserve human-edited drafts. Repair incomplete open items first: an existing dedupeKey with missing sourcePayload, title, snippet, source metadata, or proposal is unfinished work, not a reason to skip it. Re-read its original conversation using the dedupeKey and summary, then ingest the complete item and draft under that same dedupeKey even when no newer message arrived. Never invent message content from a summary; if the source is unavailable, leave the item pending for retry.+ - Closed-loop detection: BEFORE scanning for new items, re-read the exact source conversation of EVERY unresolved item (held, pending, processed, or failed), regardless of lastScanAt, age, read/unread state, or whether Gmail still labels it INBOX. The intake watermark is NOT a reconciliation watermark. Whenever the LATEST message in that conversation is newer than the item's sourceAt, report it as an event and let the Loop decide: POST $AGENT_API_URL/v1/loops/${loopId}/events with {"events":[{"sourceKey":"<item sourceKey>","author":"owner"|"other","at":<message timestamp in ms>,"text":"<the message, <=${EVENT_TEXT_CHARS} chars>","from":"<sender name, for other>"}]} (up to 20 events per call). author is "owner" when the user wrote it themselves (their own Slack userId on Slack; their own address or a send-as alias in the From line, or the SENT label, on Gmail), else "other". Ignore Gmail drafts. Never resolve, dismiss, or redraft these items yourself: the event is judged by the Loop, which closes answered conversations and redrafts reopened ones. If a source read fails or is incomplete, retain the item and retry later; do not treat it as resolved. This applies to both Gmail and Slack items.2. Keep a lastScanAt watermark in a state file on your workspace (e.g. ~/workspace/inbox-sync/STATE.md); scan each connected source since that watermark (fall back to ~3 days on first run), and advance the watermark only after a successful pass, so outages and missed fires backfill instead of leaving silent gaps. Skip a source whose app is not connected. Judge "waiting on a reply" like a good chief of staff, with a deliberate asymmetry: (a) NEEDS REPLY: direct questions, requests, intros, and personal messages that expect acknowledgment; when in any doubt, this bucket. (b) PROBABLY RESOLVED: the transcript reads closed (a thanks, a "sounds good", a thread someone else already answered) but a reasonable person might still send a word; post the item anyway with "probablyResolved": true in the item body and a draft included; the UI shows these in a muted section. NEVER silently drop a human conversation you judged resolved; demote it instead. (c) SKIP entirely: only unmistakable machine noise (newsletters, receipts, calendar notices, bot chatter). Human words never skip; the lowest they go is probablyResolved.- Gmail (use the google-workspace skill): INBOX threads where the LAST message is inbound and the user is in To or Cc. sourceKey = the Gmail threadId. Record gmail metadata: threadId, messageId, the RFC-2822 Message-ID header (rfcMessageId), subject, and the reply recipients (to = reply-all sender+others minus the user; cc as appropriate).- Slack (use your Slack access as the user): unanswered DMs, @mentions, and thread replies addressed to the user. ONE ITEM PER CONVERSATION, never per message: for unthreaded DMs and group DMs sourceKey = the channelId alone (two people posting in the same group DM is ONE item, refreshed with the latest state and a single draft answering everything outstanding); for all threaded asks sourceKey = channelId:threadTs (channelId:ts of a top-level message anchoring its own thread). Record slack metadata: channelId, channelLabel ("#channel" or "DM with <name>"), ts (the LATEST waiting message), userId (the Slack user id of that message's sender), isDirectMessage from Slack conversations.info is_im/is_mpim (NEVER infer group DMs from C/G ID prefixes), and threadTs for every threaded ask. For a top-level CHANNEL mention, threadTs is the original mentioned message ts, even before anyone replies. Preserve that anchor forever: unrelated channel chatter must not replace, refresh, or resolve this item. Read conversations.replies for that root, not the channel tail. For mentions inside a thread, include the root and only that thread. Relevant messages immediately before/after a channel mention may be included in context with nearby:true; never label these as thread replies, and never include unrelated neighboring messages.@@ -61,6 +63,43 @@ export async function ensureDefaultInboxLoops(store: LoopStore, owner: string):return loops;}+export function isReplyLoop(loop: Loop, item: LoopItem): boolean {+ return Boolean(adapterForItem(item)) && loop.shipActions.length === 1 && loop.shipActions[0]?.action === "send";+}++export const REPLY_DECISION = [+ 'First decide whether the person still owes a reply in this conversation. sourcePayload.latestEvent, when present, is the newest message an event saw: author "owner" means the person wrote it themselves (in Gmail, Slack, or from QM).',+ 'If the person has already answered and nothing newer asks anything of them, end with {"resolution":{"kind":"replied","reason":"<one line>"}}. If the conversation needs nothing from them (closed out, a plain thanks, someone else answered, machine noise), end with {"resolution":{"kind":"no_reply_needed","reason":"<one line>"}}. Re-read the conversation from the source when the payload is not enough to tell. When in doubt, draft a reply.',+].join("\n");++export async function applyReplyResolution(+ items: Pick<LoopItemLedger, "recordAction">,+ loop: Loop,+ item: LoopItem,+ parsed: unknown,+): Promise<boolean> {+ const resolution = isReplyLoop(loop, item) ? parseResolution(parsed) : null;+ if (!resolution) return false;+ const latest = item.sourcePayload?.latestEvent as LatestEvent | undefined;+ const ownText = latest?.author === "owner" ? latest.text : undefined;+ const result = resolution.kind === "replied" ? ownText : resolution.reason;+ await items.recordAction(item.id, {+ kind: resolution.kind,+ outcome: "dismissed",+ ...(result ? { result } : {}),+ });+ return true;+}++function parseResolution(parsed: unknown): { kind: "replied" | "no_reply_needed"; reason: string } | null {+ if (!parsed || typeof parsed !== "object" || !("resolution" in parsed)) return null;+ const raw = (parsed as { resolution?: unknown }).resolution;+ if (!raw || typeof raw !== "object") return null;+ const { kind, reason } = raw as { kind?: unknown; reason?: unknown };+ if (kind !== "replied" && kind !== "no_reply_needed") return null;+ return { kind, reason: typeof reason === "string" ? reason.slice(0, 500) : "" };+}+export function renderSourceInboxTask(loopId: string, source: string): string {return `Source restriction: scan only ${source}. Never read or ingest another source. Keep a separate watermark for this Loop.\n${renderInboxSyncTask(loopId)}`;}diff --git a/src/loops/inbox-realtime.ts b/src/loops/inbox-realtime.tsindex 56f5a09ca..14c998f67 100644--- a/src/loops/inbox-realtime.ts+++ b/src/loops/inbox-realtime.ts@@ -1,29 +1,23 @@-import type { LoopItem } from "../types.ts";import type { LoopStore } from "./loop-store.ts";import type { LoopItemLedger } from "./item-ledger.ts";+import { conversationEvent, type IngressEntry } from "./ingress.ts";import type { ConversationEvent } from "./sources/adapter.ts";import { sourceAdapter } from "./sources/index.ts";-import { swallow } from "../util/errors.ts";+import { isResolved } from "./ledger-view.ts";export interface InboxRealtimeDeps {loops: Pick<LoopStore, "list">;- items: Pick<LoopItemLedger, "byLoop" | "recordAction" | "ingest">;- requestFire?: (loopId: string) => void;+ items: Pick<LoopItemLedger, "byLoop">;+ enqueue: (loop: { id: string; owner: string }, entries: IngressEntry[]) => Promise<unknown>;}-const OPEN_STATUSES = new Set(["queued", "in_progress", "ready", "failed"]);const LOOP_CACHE_TTL_MS = 60_000;-const FIRE_DEBOUNCE_MS = 30_000;-const MAX_REPLY_CHARS = 500;-const MAX_SUMMARY_CHARS = 200;-const MAX_SNIPPET_CHARS = 10_000;export function createInboxRealtime(deps: InboxRealtimeDeps): {onConversationEvent(event: ConversationEvent): Promise<void>;} {let cachedInboxLoops: Array<{ id: string; owner: string }> = [];let cacheAt = 0;- const lastFireAt = new Map<string, number>();async function inboxLoops(): Promise<Array<{ id: string; owner: string }>> {const now = Date.now();@@ -43,59 +37,24 @@ export function createInboxRealtime(deps: InboxRealtimeDeps): {const adapter = sourceAdapter(event.source);if (!adapter) return;for (const loop of await inboxLoops()) {- let matched: LoopItem[];- try {- const items = await deps.items.byLoop(loop.id);- matched = items.filter(- (item) =>- OPEN_STATUSES.has(item.status) &&- (item.source ?? item.sourcePayload?.source) === event.source &&- adapter.matchesEvent(item, event.conversationRef),- );- } catch (e) {- swallow("inbox realtime: read ledger", e);- continue;- }+ const owner = Boolean(event.senderEmail && event.senderEmail.toLowerCase() === loop.owner.toLowerCase());+ const matched = (await deps.items.byLoop(loop.id)).filter(+ (item) =>+ (item.source ?? item.sourcePayload?.source) === event.source &&+ adapter.matchesEvent(item, event.conversationRef) &&+ !(owner && isResolved(item)),+ );if (!matched.length) continue;- const ownerReplied = Boolean(event.senderEmail && event.senderEmail.toLowerCase() === loop.owner.toLowerCase());- for (const item of matched) {- try {- if (ownerReplied) {- await deps.items.recordAction(item.id, {- kind: "replied",- sourceAt: event.at,- outcome: "dismissed",- ...(event.text ? { result: event.text.slice(0, MAX_REPLY_CHARS) } : {}),- });- } else if (event.at > (item.sourceAt ?? 0)) {- const text = event.text?.trim();- const snippet = text?.slice(0, MAX_SNIPPET_CHARS);- await deps.items.ingest([- {- loopId: loop.id,- dedupeKey: item.sourceKey,- ...(item.source !== undefined ? { source: item.source } : {}),- ...(text ? { summary: text.slice(0, MAX_SUMMARY_CHARS) } : {}),- sourceAt: event.at,- sourcePayload: {- ...item.sourcePayload,- ...(snippet ? { snippet } : {}),- receivedAt: event.at,- },- },- ]);- }- } catch (e) {- swallow("inbox realtime: update item", e);- }- }- if (!ownerReplied && deps.requestFire) {- const last = lastFireAt.get(loop.id) ?? 0;- if (Date.now() - last > FIRE_DEBOUNCE_MS) {- lastFireAt.set(loop.id, Date.now());- deps.requestFire(loop.id);- }- }+ const latest = {+ author: owner ? ("owner" as const) : ("other" as const),+ at: event.at,+ ...(event.text ? { text: event.text } : {}),+ ...(event.senderEmail ? { from: event.senderEmail } : {}),+ };+ await deps.enqueue(+ loop,+ matched.map((item) => conversationEvent(item, latest)),+ );}},};diff --git a/src/loops/inbox-source-refresh.ts b/src/loops/inbox-source-refresh.tsindex 4bb3951e9..421fcb272 100644--- a/src/loops/inbox-source-refresh.ts+++ b/src/loops/inbox-source-refresh.ts@@ -5,6 +5,7 @@ import type { ErrorLog } from "../admin/error-log.ts";import { redactSecrets } from "../../plugins/chassis/src/redact-secrets.ts";import { errMessage } from "../util/errors.ts";import type { LoopItemLedger } from "./item-ledger.ts";+import { conversationEvent, type IngressEntry } from "./ingress.ts";import { isResolved } from "./ledger-view.ts";import { isObj, tokenFor, type ConnectorTokenSource } from "./sources/adapter.ts";import { normalizeSlackApiUrl } from "../slack/config.ts";@@ -17,6 +18,7 @@ interface RefreshDeps {directory: Pick<DirectoryStore, "getBySlackId">;featureFlags: Pick<FeatureFlagStore, "enabled">;errors?: ErrorLog;+ enqueue: (loop: { id: string; owner: string }, entries: IngressEntry[]) => Promise<unknown>;fetchImpl?: typeof fetch;slackApiUrl?: string;now?: () => number;@@ -184,6 +186,11 @@ export function createInboxSourceRefresh(deps: RefreshDeps): InboxSourceRefresh);}+ async function ownerReplied(owner: string, item: LoopItem, at: number, text: string): Promise<void> {+ if (!Number.isFinite(at) || at <= (item.sourceAt ?? 0)) return;+ await deps.enqueue({ id: item.loopId, owner }, [conversationEvent(item, { author: "owner", at, text })]);+ }+async function refresh(owner: string, item: LoopItem): Promise<void> {const source = item.source ?? item.sourcePayload?.source;if (source !== "gmail" && source !== "slack") return;@@ -201,12 +208,12 @@ export function createInboxSourceRefresh(deps: RefreshDeps): InboxSourceRefresh.sort((a, b) => Number(a.internalDate) - Number(b.internalDate)).at(-1);if (latest && Array.isArray(latest.labelIds) && latest.labelIds.includes("SENT")) {- await deps.items.recordAction(item.id, {- kind: "replied",- outcome: "dismissed",- sourceAt: Number(latest.internalDate),- ...(typeof latest.snippet === "string" ? { result: latest.snippet.slice(0, 500) } : {}),- });+ await ownerReplied(+ owner,+ item,+ Number(latest.internalDate),+ typeof latest.snippet === "string" ? latest.snippet : "",+ );}if (payload.sourceRefreshError)await deps.items.annotate(item.id, { sourceRefreshError: null }, { expectedSourceAt: item.sourceAt });@@ -312,12 +319,12 @@ export function createInboxSourceRefresh(deps: RefreshDeps): InboxSourceRefreshif ((!root || !partial) && latest && !isResolved(item)) {const identity = await slackInfo(owner, "auth.test", token);if (typeof identity.user_id === "string" && latest.user === identity.user_id) {- await deps.items.recordAction(item.id, {- kind: "replied",- outcome: "dismissed",- sourceAt: Math.round(Number(latest.ts) * 1000),- result: typeof latest.text === "string" ? latest.text.slice(0, 500) : "",- });+ await ownerReplied(+ owner,+ item,+ Math.round(Number(latest.ts) * 1000),+ typeof latest.text === "string" ? latest.text : "",+ );}}}diff --git a/src/loops/ingress.ts b/src/loops/ingress.tsindex 3bb0b7a46..19345700e 100644--- a/src/loops/ingress.ts+++ b/src/loops/ingress.ts@@ -1,10 +1,11 @@import { randomBytes, randomUUID } from "node:crypto";import type { DurableMap } from "../persistence/durable-map.ts";import type { AdvisoryLock } from "../persistence/advisory-lock.ts";-import type { Loop } from "../types.ts";+import type { Loop, LoopItem } from "../types.ts";import type { LoopStore } from "./loop-store.ts";import type { IngestEntryInput, LoopItemLedger } from "./item-ledger.ts";import type { LoopOutputStore } from "./output-store.ts";+import { isResolved } from "./ledger-view.ts";import type { LoopFireService } from "./loop-fire.ts";import { getVerifier, type VerifierInput } from "../webhooks/verifiers.ts";import { hashId } from "../util/crypto.ts";@@ -18,7 +19,7 @@ export interface LoopIngress {id: string;loopId: string;owner: string;- kind: "webhook" | "slack" | "gmail";+ kind: "webhook" | "slack" | "gmail" | "conversation";enabled: boolean;secret?: string;teamId?: string;@@ -32,12 +33,62 @@ export interface LoopIngress {nextWorkAt?: number;}+export type IngressEntry = IngestEntryInput & { merge?: boolean };++export interface LatestEvent {+ author: "owner" | "other";+ at: number;+ text?: string;+ from?: string;+}++export const EVENT_TEXT_CHARS = 2_000;++export function conversationEvent(+ item: { loopId: string; sourceKey: string; source?: string | undefined },+ event: LatestEvent,+): IngressEntry {+ const text = event.text?.trim().slice(0, EVENT_TEXT_CHARS);+ const other = event.author === "other";+ const from = other ? event.from?.slice(0, 300) : undefined;+ return {+ loopId: item.loopId,+ dedupeKey: item.sourceKey,+ ...(item.source !== undefined ? { source: item.source } : {}),+ sourceAt: event.at,+ merge: true,+ sourcePayload: {+ latestEvent: {+ author: event.author,+ at: event.at,+ ...(text && !other ? { text } : {}),+ ...(from ? { from } : {}),+ },+ ...(other ? { receivedAt: event.at, ...(text ? { snippet: text } : {}) } : {}),+ },+ };+}++function ledgerEntry(raw: IngressEntry, existing: LoopItem | undefined): IngestEntryInput | null {+ const { merge, ...base } = raw;+ if (!merge) return base;+ if (!existing) return null;+ if (isResolved(existing) && (base.sourcePayload?.latestEvent as LatestEvent | undefined)?.author === "owner")+ return null;+ return {+ ...base,+ ...(base.source === undefined && existing.source !== undefined ? { source: existing.source } : {}),+ ...(base.summary === undefined && existing.sourceSummary !== undefined ? { summary: existing.sourceSummary } : {}),+ sourcePayload: { ...existing.sourcePayload, ...base.sourcePayload },+ };+}+export interface IngressDelivery {id: string;ingressId: string;queueKey?: string;receivedAt: number;- entries?: IngestEntryInput[];+ entries?: IngressEntry[];historyId?: string;ingested?: boolean;completedAt?: number;@@ -98,9 +149,11 @@ export function createLoopIngress(deps: LoopIngressDeps) {.filter((delivery) => !delivery.completedAt && delivery.nextAttemptAt <= Date.now()).sort((a, b) => a.receivedAt - b.receivedAt).slice(0, 20);- const ingest = async (entries: IngestEntryInput[]) => {- for (const entry of entries) {- const existing = (await deps.items.byLoop(source.loopId)).find((item) => item.sourceKey === entry.dedupeKey);+ const ingest = async (entries: IngressEntry[]) => {+ for (const raw of entries) {+ const existing = (await deps.items.byLoop(source.loopId)).find((item) => item.sourceKey === raw.dedupeKey);+ const entry = ledgerEntry(raw, existing);+ if (!entry) continue;const refreshed = existing && (entry.sourceAt ?? 0) > (existing.sourceAt ?? 0);if (refreshed &&@@ -135,6 +188,7 @@ export function createLoopIngress(deps: LoopIngressDeps) {});await deps.sources.merge(source.id, { lastError: undefined });} catch (error) {+ swallow("Loop ingress delivery", error);await deps.sources.merge(source.id, { lastError: errMessage(error) });await deps.deliveries.merge(delivery.id, {nextAttemptAt: Date.now() + Math.min(3_600_000, 30_000 * 2 ** Math.min(attempt, 7)),@@ -181,10 +235,42 @@ export function createLoopIngress(deps: LoopIngressDeps) {});}+ async function conversationSource(loop: Pick<Loop, "id" | "owner">): Promise<LoopIngress> {+ const find = async () =>+ (await deps.sources.select({ where: { field: "loopId", anyOfFold: [loop.id] } })).find(+ (source) => source.kind === "conversation",+ );+ const found = await find();+ if (found) return found;+ return deps.lock.withLock(`loop-ingress-setup:${loop.id}`, async () => {+ const again = await find();+ if (again) return again;+ const source: LoopIngress = {+ id: randomUUID(),+ loopId: loop.id,+ owner: loop.owner,+ kind: "conversation",+ enabled: true,+ createdAt: Date.now(),+ };+ await deps.sources.put(source.id, source);+ return source;+ });+ }+return {gmailAvailable: Boolean(deps.gmailConfig && deps.gmailClient),+ async enqueueConversation(loop: Pick<Loop, "id" | "owner">, entries: IngressEntry[]): Promise<boolean> {+ if (!entries.length || !(await deps.enabledFor(loop.owner))) return false;+ const source = await conversationSource(loop);+ await enqueue(source, hashId(entries.map((entry) => `${entry.dedupeKey}:${entry.sourceAt}`)), { entries });+ void process(source.id).catch((error) => swallow("Loop conversation event", error));+ return true;+ },async list(loopId: string) {- return await deps.sources.select({ omit: ["secret"], where: { field: "loopId", anyOfFold: [loopId] } });+ return (await deps.sources.select({ omit: ["secret"], where: { field: "loopId", anyOfFold: [loopId] } })).filter(+ (source) => source.kind !== "conversation",+ );},async create(loop: Loop, input: IngressSetup): Promise<LoopIngress> {if (!(await deps.enabledFor(loop.owner))) throw new Error("Event ingestion is not enabled for this owner");@@ -287,7 +373,12 @@ export function createLoopIngress(deps: LoopIngressDeps) {return { status: 202, body: "Accepted" };}const source = await deps.sources.get(id);- if (!source?.enabled || !(await deps.enabledFor(source.owner)) || source.kind === "gmail")+ if (+ !source?.enabled ||+ !(await deps.enabledFor(source.owner)) ||+ source.kind === "gmail" ||+ source.kind === "conversation"+ )return { status: 404, body: "Not found" };const verifier = getVerifier(source.kind === "slack" ? "slack" : "hmac-sha256")!;const input = { ...request, secret: source.secret };diff --git a/src/loops/item-ledger.ts b/src/loops/item-ledger.tsindex ef857131b..51fdd5d67 100644--- a/src/loops/item-ledger.ts+++ b/src/loops/item-ledger.ts@@ -64,7 +64,6 @@ interface RecordActionInput {result?: string;actorId?: string;outcome: "actioned" | "dismissed";- sourceAt?: number;}export interface LoopItemLedger {@@ -490,18 +489,12 @@ export function createLoopItemLedger(let applied = false;const after = await update(id, (item) => {if (item.status === "shipped") return item;- if (- input.sourceAt !== undefined &&- (!Number.isFinite(input.sourceAt) || input.sourceAt <= (item.sourceAt ?? 0) || isResolved(item))- )- return item;applied = true;const now = Date.now();return {...item,status: input.outcome === "actioned" ? "shipped" : "skipped",actionKind: input.kind,- ...(input.sourceAt !== undefined ? { sourceAt: input.sourceAt } : {}),actedAt: now,claimedAt: undefined,claimToken: undefined,diff --git a/src/loops/loop-fire.ts b/src/loops/loop-fire.tsindex 1cc12c187..9c7eeb735 100644--- a/src/loops/loop-fire.ts+++ b/src/loops/loop-fire.ts@@ -1,5 +1,11 @@import { WorkAdmissionClosed, type AdmittedWork } from "../util/admitted-work.ts";-import { renderSourceInboxTask, renderInboxSyncTask } from "./inbox-loop.ts";+import {+ renderSourceInboxTask,+ renderInboxSyncTask,+ isReplyLoop,+ applyReplyResolution,+ REPLY_DECISION,+} from "./inbox-loop.ts";import { cronTriggerAuthority } from "../cron/authority.ts";import type { CronStore } from "../cron/cron-store.ts";import { boundLoopCron } from "./authority.ts";@@ -427,8 +433,11 @@ function workPrompt(loop: Loop, item: LoopItem, guidance?: string, similar: Loop"```",shipActionContract(loop),`The item's success condition: ${promptText(loop.successCondition)}`,- adapterForItem(item) && loop.shipActions.length === 1 && loop.shipActions[0]?.action === "send"- ? 'For a reply, end with a fenced JSON object {"proposal":{"body":"the draft reply","to":["email recipients if applicable"],"subject":"email subject if applicable"},"outputs":[]}. Save the draft for human review; never send it. If no reply is needed, return {"outputs":[]}.'+ isReplyLoop(loop, item)+ ? [+ REPLY_DECISION,+ 'For a reply, end with a fenced JSON object {"proposal":{"body":"the draft reply","to":["email recipients if applicable"],"subject":"email subject if applicable"},"outputs":[]}. Save the draft for human review; never send it.',+ ].join("\n"): 'End your reply with a fenced json block: {"outputs": [{"shipAction": "<declared action>", "label": "<optional grouping label>", "title": "<one line>", "externalRef": "<url or id if any>", "summary": "<one line>"}]}. List every externally-reviewable artifact you prepared; an empty outputs array means the item needed none.',"[End loop work]","",@@ -719,6 +728,8 @@ export function createLoopFireService(deps: LoopFireDeps): LoopFireService {const failure = stageFailure("work", outcome);if (failure) throw failure.error;const parsed = fencedJson(outcome.reply ?? "");+ if (await applyReplyResolution(deps.items, loop, item, parsed))+ return { runId: outcome.sessionId ?? `${threadRef}:work:${item.id}` };const proposal =parsed && typeof parsed === "object" && "proposal" in parsed? adapterForItem(item)?.parseProposal(parsed.proposal)diff --git a/src/wiring.ts b/src/wiring.tsindex b0d38647c..193faa90f 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -2314,10 +2314,7 @@ export function buildApp(const inboxRealtime = createInboxRealtime({loops: loopStore,items: loopItems,- requestFire: (loopId) =>- void loopFire- .fire(loopId, `loop:${loopId}:slack-event:${Date.now()}`)- .catch(swallowAs("inbox loop: slack event fire", undefined)),+ enqueue: (loop, entries) => loopIngress.enqueueConversation(loop, entries),});const keychainApprovals = keychain? createKeychainApprovals({@@ -3191,6 +3188,7 @@ export function serverDeps(featureFlags: built.featureFlags,items: built.loops.items,errors: built.errors,+ enqueue: (loop, entries) => built.loopIngress.enqueueConversation(loop, entries),tokens: built.keychain,directory: built.directory,...(config.slack?.apiUrl ? { slackApiUrl: config.slack.apiUrl } : {}),diff --git a/test/inbox-realtime.test.ts b/test/inbox-realtime.test.tsindex 36ef93f37..694301033 100644--- a/test/inbox-realtime.test.ts+++ b/test/inbox-realtime.test.ts@@ -6,8 +6,9 @@ import { renderSourceInboxTask } from "../src/loops/inbox-loop.ts";import { slackConversationRef } from "../src/loops/sources/slack.ts";import type { LedgerEvent } from "../src/loops/ledger-events.ts";import type { Loop } from "../src/types.ts";+import type { IngressEntry } from "../src/loops/ingress.ts";-const LOOP = { id: "loop-inbox", owner: "[email protected]", surface: "inbox" } as unknown as Loop;+const LOOP = { id: "loop-inbox", owner: "[email protected]", surface: "inbox" } as unknown as Loop;function slackEntry(over: Partial<IngestEntryInput> = {}): IngestEntryInput {return {@@ -17,8 +18,8 @@ function slackEntry(over: Partial<IngestEntryInput> = {}): IngestEntryInput {summary: "any update?",sourcePayload: {source: "slack",- title: "DM with Mark",- from: "Mark",+ title: "DM with Sam",+ from: "Sam",snippet: "any update?",slack: { channelId: "D123", ts: "1000.100", isDirectMessage: true },},@@ -45,15 +46,22 @@ function gmailEntry(over: Partial<IngestEntryInput> = {}): IngestEntryInput {};}-function realtime(items: ReturnType<typeof createLoopItemLedger>, requestFire?: (loopId: string) => void) {- return createInboxRealtime({+function realtime(items: ReturnType<typeof createLoopItemLedger>) {+ const calls: IngressEntry[][] = [];+ const rt = createInboxRealtime({loops: { list: async () => [LOOP] },items,- ...(requestFire ? { requestFire } : {}),+ enqueue: async (_loop, entries) => {+ calls.push(entries);+ },});+ return Object.assign(rt, { calls });}-test("the owner's own reply resolves the matching open card as replied", async () => {+const latest = (entries: IngressEntry[] | undefined) =>+ entries?.map((entry) => [entry.dedupeKey, entry.sourcePayload?.latestEvent]);++test("the owner's own reply is queued as an owner event for the card's conversation", async () => {const items = createLoopItemLedger();await items.ingest([slackEntry()]);const rt = realtime(items);@@ -62,41 +70,59 @@ test("the owner's own reply resolves the matching open card as replied", async (conversationRef: "D123",at: 2_000_000,text: "on it!",- senderEmail: "[email protected]",+ senderEmail: "[email protected]",});- const [item] = await items.byLoop(LOOP.id);- assert.equal(item!.status, "skipped");- assert.equal(item!.actionKind, "replied");- assert.equal(item!.actionResult, "on it!");+ assert.deepEqual(latest(rt.calls[0]), [["D123", { author: "owner", at: 2_000_000, text: "on it!" }]]);+ assert.equal(rt.calls[0]![0]!.merge, true);+ assert.equal(rt.calls[0]![0]!.sourceAt, 2_000_000);});-test("someone else's message bumps the card and requests a debounced fire", async () => {+test("someone else's messages are queued as other events with their sender", async () => {const items = createLoopItemLedger();- await items.ingest([slackEntry()]);- const fires: string[] = [];- const rt = realtime(items, (loopId) => fires.push(loopId));+ await items.ingest([slackEntry(), gmailEntry()]);+ const rt = realtime(items);await rt.onConversationEvent({source: "slack",conversationRef: "D123",at: 3_000_000,- text: "also, when works?",- senderEmail: "[email protected]",+ text: "ping",+ senderEmail: "[email protected]",});await rt.onConversationEvent({- source: "slack",- conversationRef: "D123",+ source: "gmail",+ conversationRef: "t-1",at: 3_001_000,- text: "ping",- senderEmail: "[email protected]",+ text: "any word?",+ senderEmail: "[email protected]",});- const [item] = await items.byLoop(LOOP.id);- assert.equal(item!.status, "queued");- assert.equal(item!.sourceAt, 3_001_000);- assert.equal(item!.sourceSummary, "ping");- assert.equal(fires.length, 1);+ assert.deepEqual(+ rt.calls.map((entries) => entries.map((entry) => [entry.dedupeKey, entry.sourcePayload])),+ [+ [+ [+ "D123",+ {+ latestEvent: { author: "other", at: 3_000_000, from: "[email protected]" },+ receivedAt: 3_000_000,+ snippet: "ping",+ },+ ],+ ],+ [+ [+ "gmail:t-1",+ {+ latestEvent: { author: "other", at: 3_001_000, from: "[email protected]" },+ receivedAt: 3_001_000,+ snippet: "any word?",+ },+ ],+ ],+ ],+ );});-test("events in unrelated conversations or other sources leave the ledger alone", async () => {+test("events in unrelated conversations or other sources queue nothing", async () => {const items = createLoopItemLedger();await items.ingest([slackEntry({@@ -109,21 +135,30 @@ test("events in unrelated conversations or other sources leave the ledger alone"slack: { channelId: "C9", ts: "100.1", threadTs: "100.1" },},}),+ slackEntry({+ dedupeKey: "D124",+ sourcePayload: { slack: { channelId: "D124", ts: "2.0", isDirectMessage: true } },+ }),gmailEntry(),]);const rt = realtime(items);-- await rt.onConversationEvent({ source: "slack", conversationRef: "C9:200.2", at: 2_000_000, text: "chatter" });- await rt.onConversationEvent({ source: "slack", conversationRef: "D999", at: 2_000_000, text: "other dm" });-- await rt.onConversationEvent({ source: "slack", conversationRef: "t-1", at: 2_000_000, text: "collide" });- for (const item of await items.byLoop(LOOP.id)) {- assert.equal(item.status, "queued");- assert.equal(item.sourceAt, 1_000_100);- }+ for (const [source, conversationRef] of [+ ["slack", "C9:200.2"],+ ["slack", "D999"],+ ["slack", "t-1"],+ ["gmail", "t-other"],+ ["slack", "D124:1.0"],+ ])+ await rt.onConversationEvent({+ source: source!,+ conversationRef: conversationRef!,+ at: 2_000_000,+ senderEmail: LOOP.owner,+ });+ assert.deepEqual(rt.calls, []);});-test("a thread reply resolves a channel card anchored at the thread root", async () => {+test("a thread reply reaches a channel card anchored at the thread root", async () => {const items = createLoopItemLedger();await items.ingest([slackEntry({@@ -143,54 +178,9 @@ test("a thread reply resolves a channel card anchored at the thread root", asyncconversationRef: "C9:100.1",at: 2_000_000,text: "done",- senderEmail: "[email protected]",- });- const [item] = await items.byLoop(LOOP.id);- assert.equal(item!.status, "skipped");- assert.equal(item!.actionKind, "replied");-});--test("a gmail event matches cards by threadId only", async () => {- const items = createLoopItemLedger();- await items.ingest([gmailEntry()]);- const rt = realtime(items);- await rt.onConversationEvent({- source: "gmail",- conversationRef: "t-other",- at: 2_000_000,- senderEmail: "[email protected]",- });- let [item] = await items.byLoop(LOOP.id);- assert.equal(item!.status, "queued");- await rt.onConversationEvent({- source: "gmail",- conversationRef: "t-1",- at: 2_000_000,- text: "sent, thanks",- senderEmail: "[email protected]",- });- [item] = await items.byLoop(LOOP.id);- assert.equal(item!.status, "skipped");- assert.equal(item!.actionResult, "sent, thanks");-});--test("someone else's gmail reply bumps the card for a redraft", async () => {- const items = createLoopItemLedger();- await items.ingest([gmailEntry()]);- const fires: string[] = [];- const rt = realtime(items, (loopId) => fires.push(loopId));- await rt.onConversationEvent({- source: "gmail",- conversationRef: "t-1",- at: 3_000_000,- text: "bumping this — any word?",- senderEmail: "[email protected]",+ senderEmail: LOOP.owner,});- const [item] = await items.byLoop(LOOP.id);- assert.equal(item!.status, "queued");- assert.equal(item!.sourceAt, 3_000_000);- assert.equal(item!.sourceSummary, "bumping this — any word?");- assert.deepEqual(fires, [LOOP.id]);+ assert.deepEqual(latest(rt.calls[0]), [["C9:100.1", { author: "owner", at: 2_000_000, text: "done" }]]);});test("slack conversation refs key DMs by channel and channel asks by thread root", () => {@@ -219,56 +209,6 @@ test("private-channel mentions stay anchored to their thread, not the channel taassert.equal(slackConversationRef("GPRIVATE", "200.2", "100.1", false), "GPRIVATE:100.1");});-test("an old own reply cannot close a newer unanswered message", async () => {- const items = createLoopItemLedger();- await items.ingest([gmailEntry({ sourceAt: 3_000_000 })]);- await realtime(items).onConversationEvent({- source: "gmail",- conversationRef: "t-1",- at: 2_000_000,- senderEmail: LOOP.owner,- });- assert.notEqual((await items.byLoop(LOOP.id))[0]!.status, "skipped");-});--test("reply closure advances the source watermark so an older scan cannot resurrect it", async () => {- const items = createLoopItemLedger();- await items.ingest([gmailEntry()]);- await realtime(items).onConversationEvent({- source: "gmail",- conversationRef: "t-1",- at: 3_000_000,- senderEmail: LOOP.owner,- });- await items.ingest([gmailEntry({ sourceAt: 2_000_000 })]);- assert.equal((await items.byLoop(LOOP.id))[0]!.status, "skipped");-});--test("replying in an unrelated DM thread cannot dismiss an unthreaded ask", async () => {- const items = createLoopItemLedger();- await items.ingest([- slackEntry({ sourcePayload: { slack: { channelId: "D123", ts: "2.0", isDirectMessage: true } } }),- ]);- await realtime(items).onConversationEvent({- source: "slack",- conversationRef: "D123:1.0",- at: 3_000_000,- senderEmail: LOOP.owner,- });- assert.notEqual((await items.byLoop(LOOP.id))[0]!.status, "skipped");-});--test("a new inbound message keeps its full text on the card", async () => {- const items = createLoopItemLedger();- await items.ingest([gmailEntry()]);- const rt = realtime(items);- const text = `Thanks for the demo. ${"Here is everything we tried last week. ".repeat(12)}The one feature that I needed was model routing.`;- await rt.onConversationEvent({ source: "gmail", conversationRef: "t-1", at: 2_000_000, text });- const [item] = await items.byLoop(LOOP.id);- assert.equal(item!.sourcePayload!.snippet, text);- assert.ok(item!.sourceSummary!.length <= 200);-});-test("the inbox sync task asks for the full waiting message", () => {const task = renderSourceInboxTask("loop-inbox", "gmail");assert.match(task, /snippet \(the full text of the waiting message/);diff --git a/test/inbox-selection.test.ts b/test/inbox-selection.test.tsindex 3c076ae58..d90215c05 100644--- a/test/inbox-selection.test.ts+++ b/test/inbox-selection.test.ts@@ -391,7 +391,7 @@ test("the inbox feed reconciles selected owner Gmail loops before counting summaassert.equal(owner, "alice");for (const item of items) {refreshed.push(item.id);- await w.deps.items.recordAction(item.id, { kind: "replied", outcome: "dismissed", sourceAt: 2000 });+ await w.deps.items.recordAction(item.id, { kind: "replied", outcome: "dismissed" });}});const loops = await ensureDefaultInboxLoops(w.deps.store, "alice");diff --git a/test/inbox-source-refresh.test.ts b/test/inbox-source-refresh.test.tsindex 020c6b916..a07560fb0 100644--- a/test/inbox-source-refresh.test.ts+++ b/test/inbox-source-refresh.test.ts@@ -1,7 +1,8 @@import assert from "node:assert/strict";import { test } from "node:test";-import { createInboxSourceRefresh } from "../src/loops/inbox-source-refresh.ts";+import { createInboxSourceRefresh as createRefresh } from "../src/loops/inbox-source-refresh.ts";import { createLoopItemLedger } from "../src/loops/item-ledger.ts";+import type { IngressEntry } from "../src/loops/ingress.ts";async function fixture(source = "gmail", meta: Record<string, unknown> = { threadId: "t1" }) {const items = createLoopItemLedger();@@ -18,6 +19,18 @@ async function fixture(source = "gmail", meta: Record<string, unknown> = { threareturn { items, item: (await items.byLoop("inbox"))[0]! };}const featureFlags = { enabled: async () => true };+const createInboxSourceRefresh = (+ deps: Omit<Parameters<typeof createRefresh>[0], "enqueue"> & {+ enqueue?: Parameters<typeof createRefresh>[0]["enqueue"];+ },+) => createRefresh({ enqueue: async () => undefined, ...deps });+function recorder() {+ const events: unknown[] = [];+ const enqueue = async (_loop: unknown, entries: IngressEntry[]) => {+ events.push(...entries.map((entry) => entry.sourcePayload?.latestEvent));+ };+ return { events, enqueue };+}const tokens = { connectorAccessToken: async () => "synthetic-token" };const json = (body: unknown) => Response.json(body);const ALEX = "https://avatars.slack-edge.com/alex.png";@@ -35,12 +48,14 @@ function directoryOf(}const directory = directoryOf({ U1: { displayName: "Alex", avatarUrl: ALEX }, U2: { displayName: "Taylor" } });-test("Gmail reply sent outside QM closes the card, ignores drafts and recognizes send-as aliases", async () => {+test("Gmail reply sent outside QM becomes an owner event, ignoring drafts and recognizing send-as aliases", async () => {const { items, item } = await fixture();let reads = 0;+ const queue = recorder();const refresh = createInboxSourceRefresh({featureFlags,items,+ enqueue: queue.enqueue,tokens,directory,fetchImpl: async (url) => {@@ -57,12 +72,8 @@ test("Gmail reply sent outside QM closes the card, ignores drafts and recognizes});await Promise.all([refresh("[email protected]", [item]), refresh("[email protected]", [item])]);await refresh("[email protected]", [item]);- const after = (await items.get(item.id))!;- assert.equal(after.status, "skipped");- assert.equal(after.actionKind, "replied");- assert.equal(after.sourceAt, 2000);- assert.equal(after.actionResult, "Handled elsewhere");- assert.equal(after.proposal?.data.body, "Keep my edit");+ assert.deepEqual(queue.events, [{ author: "owner", at: 2000, text: "Handled elsewhere" }]);+ assert.equal((await items.get(item.id))!.proposal?.data.body, "Keep my edit");assert.equal(reads, 1);});@@ -76,14 +87,16 @@ test("an old sent message or a newer inbound message does not close a Gmail item[{ internalDate: "2000", labelIds: ["DRAFT"] }],]) {const { items, item } = await fixture();+ const queue = recorder();await createInboxSourceRefresh({featureFlags,items,+ enqueue: queue.enqueue,tokens,directory,fetchImpl: async () => json({ messages }),})("owner", [item]);- assert.equal((await items.get(item.id))!.status, "ready");+ assert.deepEqual(queue.events, []);}});@@ -184,28 +197,32 @@ test("top-level/private-channel mentions fetch just their root and replies, nevetest("Slack detects group DMs by conversation metadata, including C-prefixed IDs", async () => {const { items, item } = await fixture("slack", { channelId: "C_GROUP_DM", ts: "1.0" });const calls: string[] = [];+ const queue = recorder();await createInboxSourceRefresh({featureFlags,items,+ enqueue: queue.enqueue,tokens,directory,fetchImpl: slackFetch(calls, { direct: true, own: true }),})("owner", [item]);assert.ok(calls.some((p) => p.includes("conversations.history")));- assert.equal((await items.get(item.id))!.actionKind, "replied");+ assert.deepEqual(queue.events, [{ author: "owner", at: 2000, text: "Thread reply" }]);});test("a partial Slack thread is displayed as partial but cannot resolve an item", async () => {const { items, item } = await fixture("slack", { channelId: "C1", ts: "1.0" });+ const queue = recorder();await createInboxSourceRefresh({featureFlags,items,+ enqueue: queue.enqueue,tokens,directory,fetchImpl: slackFetch([], { partial: true, own: true }),})("owner", [item]);const after = (await items.get(item.id))!;- assert.equal(after.status, "ready");+ assert.deepEqual(queue.events, []);assert.equal(after.sourcePayload!.sourceContextPartial, true);});diff --git a/test/loop-fire.test.ts b/test/loop-fire.test.tsindex d7c076b7c..ab0e21c54 100644--- a/test/loop-fire.test.ts+++ b/test/loop-fire.test.ts@@ -1162,3 +1162,56 @@ test("Slack work and follow-up prompts preserve message images without repeating}assert.match(JSON.stringify((await s.items.get(item!.id))!.sourcePayload), /avatar.png/);});++test("a reply Loop's work stage can settle an event as replied or no reply needed", async () => {+ for (const [kind, actionKind, actionResult] of [+ ["replied", "replied", "Sent it over"],+ ["no_reply_needed", "no_reply_needed", "Answered in Gmail"],+ ] as const) {+ let judged = false;+ const w = service((req) => {+ if (stage(req) === "intake") throw new Error("Event work must not enumerate");+ if (stage(req) === "work")+ return `Done.\n\`\`\`json\n{"resolution":{"kind":"${kind}","reason":"Answered in Gmail"}}\n\`\`\``;+ judged = true;+ return '```json\n{"outcome":"met","reason":"ok"}\n```';+ });+ const loop = await makeLoop(w.loops, { sources: ["gmail"], shipActions: [{ action: "send", gate: "hold" }] });+ await w.items.ingest([+ {+ loopId: loop.id,+ dedupeKey: "thread1",+ source: "gmail",+ sourceAt: 2000,+ sourcePayload: {+ source: "gmail",+ snippet: "Can you send the deck?",+ gmail: { threadId: "thread1" },+ latestEvent: { author: "owner", at: 2000, text: "Sent it over" },+ },+ },+ ]);+ const result = await w.fire.fire(loop.id, `push:${kind}`, undefined, { enumerate: false });+ const [item] = await w.items.byLoop(loop.id);+ assert.equal(item?.status, "skipped");+ assert.equal(item?.actionKind, actionKind);+ assert.equal(item?.actionResult, actionResult);+ assert.equal(item?.proposal, undefined);+ assert.equal(judged, false);+ assert.deepEqual(result.summary?.ready, []);+ assert.deepEqual(result.summary?.shipped, []);+ }+});++test("a resolution from a non-reply Loop is ignored", async () => {+ const w = service((req) =>+ stage(req) === "work"+ ? '```json\n{"resolution":{"kind":"replied","reason":"x"},"outputs":[]}\n```'+ : '```json\n{"outcome":"met","reason":"ok"}\n```',+ );+ const loop = await makeLoop(w.loops);+ await w.items.ingest([{ loopId: loop.id, dedupeKey: "k", sourceAt: 1, sourcePayload: {} }]);+ await w.fire.fire(loop.id, "push:generic", undefined, { enumerate: false });+ const [item] = await w.items.byLoop(loop.id);+ assert.notEqual(item?.status, "skipped");+});diff --git a/test/loop-ingress.test.ts b/test/loop-ingress.test.tsindex 65dc1e008..141ee1e96 100644--- a/test/loop-ingress.test.ts+++ b/test/loop-ingress.test.ts@@ -1,7 +1,7 @@import assert from "node:assert/strict";import { test } from "node:test";import { createHmac } from "node:crypto";-import { createLoopIngress, type LoopIngress, type IngressDelivery } from "../src/loops/ingress.ts";+import { conversationEvent, createLoopIngress, type LoopIngress, type IngressDelivery } from "../src/loops/ingress.ts";import { createMemoryMap } from "../src/persistence/durable-map.ts";import { createMemoryAdvisoryLock } from "../src/persistence/advisory-lock.ts";import { createLoopStore } from "../src/loops/loop-store.ts";@@ -447,3 +447,152 @@ test("revoking rollout blocks new deliveries and processing already queued work"await denied.setEnabled(w.loop.id, source.id, false);await assert.rejects(denied.setEnabled(w.loop.id, source.id, true), /not enabled/);});++test("conversation events reopen a known item for others' messages, skip the owner's on a closed one, and never invent one", async () => {+ const w = await world();+ await w.items.ingest([+ {+ loopId: w.loop.id,+ dedupeKey: "thread-1",+ source: "gmail",+ summary: "Can you send the deck?",+ sourceAt: 1000,+ sourcePayload: {+ source: "gmail",+ title: "Deck",+ snippet: "Can you send the deck?",+ gmail: { threadId: "thread-1" },+ },+ },+ ]);+ const [item] = await w.items.byLoop(w.loop.id);+ await w.items.recordAction(item!.id, { kind: "dismissed", outcome: "dismissed" });+ const event = (sourceKey: string, author: "owner" | "other", at: number) =>+ conversationEvent({ loopId: w.loop.id, sourceKey }, { author, at, text: author, from: "Bob" });+ const until = async (done: () => Promise<boolean>) => {+ for (let i = 0; i < 100 && !(await done()); i++) {+ await w.ingress.maintain();+ await new Promise((resolve) => setTimeout(resolve, 5));+ }+ };+ const settled = () => until(async () => (await w.deliveries.all()).every((d) => d.completedAt));+ assert.equal(+ await w.ingress.enqueueConversation(w.loop, [event("thread-1", "owner", 1500), event("unknown", "other", 1500)]),+ true,+ );+ await settled();+ assert.deepEqual(await w.ingress.list(w.loop.id), []);+ assert.equal((await w.items.byLoop(w.loop.id)).length, 1);+ assert.equal((await w.items.get(item!.id))!.status, "skipped");+ assert.equal(w.fires.length, 0);++ await w.ingress.enqueueConversation(w.loop, [event("thread-1", "other", 2000)]);+ await until(async () => w.fires.length > 0);+ const after = (await w.items.get(item!.id))!;+ assert.equal(after.sourceAt, 2000);+ assert.equal(after.sourceSummary, "Can you send the deck?");+ assert.equal(after.sourcePayload?.title, "Deck");+ assert.deepEqual(after.sourcePayload?.latestEvent, { author: "other", at: 2000, from: "Bob" });+ assert.equal(after.sourcePayload?.snippet, "other");+ assert.equal(w.fires.length, 1);++ await w.ingress.enqueueConversation(w.loop, [event("thread-1", "other", 2000)]);+ assert.equal((await w.deliveries.all()).length, 2);+ const [source] = (await w.sources.all()).filter((s) => s.kind === "conversation");+ assert.equal((await w.ingress.receive(source!.id, request("webhook", "x", {}))).status, 404);+});++test("conversation events are not queued for owners outside the rollout", async () => {+ const w = await world();+ const ingress = createLoopIngress({ ...w.deps, enabledFor: async () => false });+ assert.equal(+ await ingress.enqueueConversation(w.loop, [+ { loopId: w.loop.id, dedupeKey: "k", sourceAt: 1, merge: true, sourcePayload: {} },+ ]),+ false,+ );+ assert.equal((await w.deliveries.all()).length, 0);+});++test("Gmail sent mail becomes an owner event on known threads only", async () => {+ const fetchImpl: typeof fetch = async (input) => {+ const url = new URL(String(input));+ if (url.pathname.endsWith("profile")) return Response.json({ emailAddress: "[email protected]", historyId: "5" });+ if (url.pathname.endsWith("history"))+ return Response.json({+ historyId: "6",+ history: [+ { messagesAdded: [{ message: { id: "sent" } }, { message: { id: "draft" } }, { message: { id: "in" } }] },+ ],+ });+ const id = url.pathname.split("/").at(-1);+ const labels: Record<string, string[]> = { sent: ["SENT"], draft: ["DRAFT"], in: ["INBOX", "UNREAD"] };+ return Response.json({+ id,+ threadId: "thread-1",+ internalDate: id === "sent" ? "2000" : "3000",+ labelIds: labels[id!],+ snippet: id === "sent" ? "Here you go" : "Thanks!",+ payload: { headers: [{ name: "From", value: id === "sent" ? "[email protected]" : "Bob <[email protected]>" }] },+ });+ };+ const client = createGmailPushClient({ connectorAccessToken: async () => "token" }, gmailConfig, fetchImpl);+ const entries: any[] = [];+ await client.changes("alice", "loop", { email: "[email protected]", historyId: "1", expiresAt: 1 }, async (batch) => {+ entries.push(...batch);+ });+ assert.equal(entries.length, 2);+ assert.deepEqual(entries[0], {+ loopId: "loop",+ source: "gmail",+ dedupeKey: "thread-1",+ sourceAt: 2000,+ merge: true,+ sourcePayload: { latestEvent: { author: "owner", at: 2000, text: "Here you go" } },+ });+ assert.equal(entries[1].merge, undefined);+ assert.equal(entries[1].sourcePayload.snippet, "Thanks!");+});++test("the events route queues owner and other messages for known conversations and rejects outsiders", async () => {+ const w = await world();+ await w.items.ingest([+ { loopId: w.loop.id, dedupeKey: "t1", source: "gmail", sourceAt: 1000, sourcePayload: { snippet: "Ask" } },+ ]);+ const route = loopIngressRoutes.find((route) => "path" in route && route.path === "/v1/loops/:id/events")!;+ const call = async (actor: string | null, body: unknown) => {+ let status = 0;+ await route.handle({+ params: { id: w.loop.id },+ url: new URL("http://local?principalId=alice"),+ actor: actor ? { p: actor } : null,+ capability: null,+ body,+ deps: { loops: { store: w.loops }, loopIngress: w.ingress, featureFlags: { enabled: async () => true } },+ app: {+ samePerson: async (a: string, b: string) => a === b,+ membershipControlsScope: async () => false,+ managesScope: async () => false,+ },+ res: {+ getHeader: () => undefined,+ writeHead: (value: number) => {+ status = value;+ },+ end: () => {},+ },+ } as unknown as ApiCtx);+ return status;+ };+ const event = { sourceKey: "t1", author: "owner", at: 2000, text: "Done" };+ assert.equal(await call("mallory", { events: [event] }), 403);+ assert.equal(await call("alice", { events: [{ ...event, author: "bot" }] }), 400);+ assert.equal(await call("alice", { events: [] }), 400);+ assert.equal(await call("alice", { events: [event] }), 202);+ for (let i = 0; i < 100 && !w.fires.length; i++) await new Promise((resolve) => setTimeout(resolve, 5));+ const [item] = await w.items.byLoop(w.loop.id);+ assert.equal(item!.sourceAt, 2000);+ assert.equal(item!.sourcePayload?.snippet, "Ask");+ assert.deepEqual(item!.sourcePayload?.latestEvent, { author: "owner", at: 2000, text: "Done" });+ assert.equal(w.fires.length, 1);+});diff --git a/test/loop-item-routes.test.ts b/test/loop-item-routes.test.tsindex 5a827508e..530af6ddc 100644--- a/test/loop-item-routes.test.ts+++ b/test/loop-item-routes.test.ts@@ -499,71 +499,6 @@ test("a reaction slack refuses leaves the item untouched", async () => {assert.equal(stored?.sourcePayload?.reactions, undefined);});-test("replied closes an item the person answered outside QM and keeps their words", async () => {- const w = world();- const { loop, item } = await seed(w);- const out = await call(w, {- method: "POST",- path: `/v1/loops/${loop.id}/items/${item.id}/action`,- body: { kind: "replied", args: { text: " Answered in the thread. " } },- });- assert.equal(out.status, 200, JSON.stringify(out.body));- const view = (out.body as { item: LedgerItemView }).item;- assert.equal(view.state, "dismissed");- assert.equal(view.actionKind, "replied");- assert.equal(view.actionResult, "Answered in the thread.");- assert.equal(w.sent.length, 0, "closing a loop externally never touches the source");-});--test("replied reopens like any dismissal and never demotes what was sent from here", async () => {- const w = world();- const { loop, item } = await seed(w);- await call(w, {- method: "POST",- path: `/v1/loops/${loop.id}/items/${item.id}/action`,- body: { kind: "replied", args: { text: "handled it" } },- });- const reopened = await call(w, {- method: "POST",- path: `/v1/loops/${loop.id}/items/${item.id}/action`,- body: { kind: "reopen" },- });- const view = (reopened.body as { item: LedgerItemView }).item;- assert.equal(view.state, "held");- assert.equal(view.actionKind, undefined);- assert.equal(view.actionResult, undefined);-- await w.loops.items.recordAction(item.id, { kind: "send", outcome: "actioned" });- const late = await call(w, {- method: "POST",- path: `/v1/loops/${loop.id}/items/${item.id}/action`,- body: { kind: "replied", args: { text: "too late" } },- });- assert.equal(late.status, 409);- assert.equal((await w.loops.items.get(item.id))?.actionKind, "send");-});--test("a replied text longer than the cap is clipped, and an absent one still closes the item", async () => {- const w = world();- const { loop, item } = await seed(w);- const clipped = await call(w, {- method: "POST",- path: `/v1/loops/${loop.id}/items/${item.id}/action`,- body: { kind: "replied", args: { text: "x".repeat(900) } },- });- assert.equal((clipped.body as { item: LedgerItemView }).item.actionResult?.length, 500);-- const bare = await seed(world());- const out = await call(bare.w, {- method: "POST",- path: `/v1/loops/${bare.loop.id}/items/${bare.item.id}/action`,- body: { kind: "replied" },- });- const view = (out.body as { item: LedgerItemView }).item;- assert.equal(view.state, "dismissed");- assert.equal(view.actionResult, undefined);-});-test("an action kind the source does not know is handed to the agent", async () => {const w = world();const { loop, item } = await seed(w);@@ -866,15 +801,19 @@ test("a conversational send refuses a stale draft before starting an agent turn"assert.equal(w.followUps.length, 1);});-test("inbox list refreshes open Gmail items before returning counts, but not another owner's loop", async () => {+test("inbox list refreshes open Gmail items before returning, but not another owner's loop", async () => {const w = world();const loop = await ensureInboxLoop(w.loops.store, "josh");const refreshes: string[] = [];// Exercise the route with the real refresher via the same dependency used by wiring.const { createInboxSourceRefresh } = await import("../src/loops/inbox-source-refresh.ts");+ const queued: unknown[] = [];w.sourceRefresh = createInboxSourceRefresh({featureFlags: { enabled: async () => true },items: w.loops.items,+ enqueue: async (_loop, entries) => {+ queued.push(...entries.map((entry) => entry.sourcePayload?.latestEvent));+ },tokens: { connectorAccessToken: async () => "synthetic" },directory: { getBySlackId: async () => null },fetchImpl: async (url) => {@@ -902,11 +841,11 @@ test("inbox list refreshes open Gmail items before returning counts, but not anoassert.equal(refreshes.length, 0);const result = await call(w, { method: "GET", path: `/v1/loops/${loop.id}/items` });assert.equal(result.status, 200);- assert.equal((result.body as { counts: Record<string, number> }).counts.dismissed, 1);+ assert.deepEqual(queued, [{ author: "owner", at: 2000, text: "Already replied" }]);assert.equal(refreshes.length, 1);});-test("Slack thread keys reuse a legacy item's identity, edits, and replied watermark", async () => {+test("Slack thread keys reuse a legacy item's identity and edits", async () => {const w = world();const loop = await ensureInboxLoop(w.loops.store, "josh");await w.loops.items.ingest([@@ -939,10 +878,6 @@ test("Slack thread keys reuse a legacy item's identity, edits, and replied waterassert.equal((await ingest()).status, 200);assert.equal((await w.loops.items.byLoop(loop.id)).length, 1);assert.equal((await w.loops.items.get(original!.id))!.proposal!.data.body, "Keep this edit");- await w.loops.items.recordAction(original!.id, { kind: "replied", outcome: "dismissed", sourceAt: 3000 });- await ingest();- assert.equal((await w.loops.items.byLoop(loop.id)).length, 1);- assert.equal((await w.loops.items.get(original!.id))!.status, "skipped");});for (const migrated of [false, true])@@ -1201,6 +1136,7 @@ test("an owner cannot trigger Slack avatar enrichment through item detail when I};w.sourceRefresh = createInboxSourceRefresh({items: w.loops.items,+ enqueue: async () => undefined,featureFlags: { enabled },tokens: { connectorAccessToken: async () => assert.fail("disabled Inbox must not read credentials") },directory: { getBySlackId: async () => assert.fail("disabled Inbox must not read the directory") },