Multiplayer agent harness for work.
Keep the full waiting message on inbox cards (#1746)
Joshua Francef10366eb74a4
diff --git a/src/loops/inbox-loop.ts b/src/loops/inbox-loop.tsindex 17044e2f6..374df55f0 100644--- a/src/loops/inbox-loop.ts+++ b/src/loops/inbox-loop.ts@@ -7,7 +7,7 @@ const INBOX_LOOP_NAME = "Inbox";const INBOX_LOOP_SURFACE = "inbox";-export const INBOX_SYNC_TASK_VERSION = 4;+export const INBOX_SYNC_TASK_VERSION = 5;export const INBOX_SYNC_DEFAULT_EVERY_MS = 15 * 60 * 1000;@@ -27,7 +27,7 @@ export function renderInboxSyncTask(loopId: string): string {- 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), 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.3. For each new or refreshed item, investigate before drafting: read the whole thread, and check whatever context makes the reply substantive (calendar for scheduling asks, earlier email/Slack history for open questions). Draft the reply in the user's own voice — for email follow the email-draft-in-voice skill (build the voice profile first if it is missing); for Slack match how the user actually writes in Slack (check their recent messages: register, length, punctuation). Punctuation hard rule: never use em dashes or en dashes in a draft; use a comma, period, or "..." instead, and re-read every draft before posting to strip any that slipped in.-4. POST $AGENT_API_URL/v1/loops/${loopId}/items with {"items": [...]}. Each item: source ("gmail"|"slack"), sourceKey, title (subject or channel label), from (display name), fromDetail (address or @handle), snippet (the waiting message, <=200 chars), context (up to 6 prior thread messages as {author, at, text}), receivedAt (ms epoch of the waiting message), externalUrl (deep link to the original in Gmail/Slack), draft ({to, cc, subject, body} for gmail — body plain text; {body} for slack), and the gmail/slack metadata block from step 2. IMAGES: when a thread message carries image attachments, include an "images" array of their https URLs (Slack: url_private; up to 4) on the matching context entry, and on the item itself for the waiting message — the UI proxies and renders them inline as the user's own eyes would see them in Slack/Gmail.+4. POST $AGENT_API_URL/v1/loops/${loopId}/items with {"items": [...]}. Each item: source ("gmail"|"slack"), sourceKey, title (subject or channel label), from (display name), fromDetail (address or @handle), snippet (the full text of the waiting message, never shortened or ellipsized), context (up to 6 prior thread messages as {author, at, text}), receivedAt (ms epoch of the waiting message), externalUrl (deep link to the original in Gmail/Slack), draft ({to, cc, subject, body} for gmail — body plain text; {body} for slack), and the gmail/slack metadata block from step 2. IMAGES: when a thread message carries image attachments, include an "images" array of their https URLs (Slack: url_private; up to 4) on the matching context entry, and on the item itself for the waiting message — the UI proxies and renders them inline as the user's own eyes would see them in Slack/Gmail.5. Found nothing new and changed nothing? Finish silently. Only raise your voice on a real fault (a connector that errors repeatedly) — and then only briefly.`;}diff --git a/src/loops/inbox-realtime.ts b/src/loops/inbox-realtime.tsindex 781d4e267..56f5a09ca 100644--- a/src/loops/inbox-realtime.ts+++ b/src/loops/inbox-realtime.ts@@ -15,7 +15,8 @@ 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_SNIPPET_CHARS = 200;+const MAX_SUMMARY_CHARS = 200;+const MAX_SNIPPET_CHARS = 10_000;export function createInboxRealtime(deps: InboxRealtimeDeps): {onConversationEvent(event: ConversationEvent): Promise<void>;@@ -67,13 +68,14 @@ export function createInboxRealtime(deps: InboxRealtimeDeps): {...(event.text ? { result: event.text.slice(0, MAX_REPLY_CHARS) } : {}),});} else if (event.at > (item.sourceAt ?? 0)) {- const snippet = event.text?.trim().slice(0, MAX_SNIPPET_CHARS);+ 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 } : {}),- ...(snippet ? { summary: snippet } : {}),+ ...(text ? { summary: text.slice(0, MAX_SUMMARY_CHARS) } : {}),sourceAt: event.at,sourcePayload: {...item.sourcePayload,diff --git a/test/inbox-realtime.test.ts b/test/inbox-realtime.test.tsindex 3148dbcb5..819eadf58 100644--- a/test/inbox-realtime.test.ts+++ b/test/inbox-realtime.test.ts@@ -2,6 +2,7 @@ import assert from "node:assert/strict";import { test } from "node:test";import { createLoopItemLedger, type IngestEntryInput } from "../src/loops/item-ledger.ts";import { createInboxRealtime } from "../src/loops/inbox-realtime.ts";+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";@@ -256,3 +257,20 @@ test("replying in an unrelated DM thread cannot dismiss an unthreaded ask", asyn});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/);+ assert.doesNotMatch(task, /snippet \(the waiting message, <=200 chars\)/);+});