Multiplayer agent harness for work.
Expose indexed conversation search through sessions tool (#2204)
Regan Bellada7af76963a
diff --git a/src/api/app-search.ts b/src/api/app-search.tsindex c5d36aa6e..751f1a8ef 100644--- a/src/api/app-search.ts+++ b/src/api/app-search.ts@@ -126,18 +126,28 @@ function slackBackend(deps: AppDeps, app: App): SearchBackend | null {},};}-export function createSearchMethods(deps: AppDeps, app: App, helpers: AppHelpers): Pick<App, "search"> {+export function createSearchMethods(+ deps: AppDeps,+ app: App,+ helpers: AppHelpers,+): Pick<App, "search" | "searchConversations"> {const slack = slackBackend(deps, app);+ const options = {+ onBackendError: (backend: string, error: unknown) =>+ console.error(+ "%s",+ `[search] backend ${backend} failed:`,+ error instanceof Error ? error.message : String(error),+ ),+ };+ const conversationsBackend = conversationBackend(app);const core = createCoreSearch(- [conversationBackend(app), ...(slack ? [slack] : []), fileBackend(deps, app, helpers)],- {- onBackendError: (backend, error) =>- console.error(- "%s",- `[search] backend ${backend} failed:`,- error instanceof Error ? error.message : String(error),- ),- },+ [conversationsBackend, ...(slack ? [slack] : []), fileBackend(deps, app, helpers)],+ options,);- return { search: (query, principals, limit) => core.search({ query, principals, limit }) };+ const conversations = createCoreSearch([conversationsBackend], options);+ return {+ search: (query, principals, limit) => core.search({ query, principals, limit }),+ searchConversations: (query, principals, limit) => conversations.search({ query, principals, limit }),+ };}diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.tsindex 903ca60c5..b52b83606 100644--- a/src/api/app-sessions.ts+++ b/src/api/app-sessions.ts@@ -1,3 +1,4 @@+import { principalEntitledToScope } from "../resolution/context-filter.ts";import { createCurrentScopeMembers } from "../resolution/scope-membership.ts";import { notifyDeploymentShared } from "../deploy/share-notice.ts";import { deploymentShareScope } from "../deploy/email-access.ts";@@ -502,9 +503,19 @@ export function createSessionMethods(),),);+ const teamIds = [...new Set(hits.map((hit) => hit.entryScopeLabel))]+ .map(parseScopeId)+ .filter((label) => label.kind === "team")+ .map((label) => label.ref);+ const memberships = await Promise.all(+ teamIds.map(async (id) => ((await deps.teams?.isMember(principalId, id)) ? id : null)),+ );+ const principal = { ...deps.identity.classify(principalId), teamIds: memberships.filter((id) => id !== null) };const terms = searchTerms(query);return hits.flatMap((hit) => {if (!allowed.get(hit.scopeId)) return [];+ if (!principalEntitledToScope(principal, hit.entryScopeLabel, hit.scopeId, scopeId("org", orgIdOf())))+ return [];const session = hit;return [{diff --git a/src/api/app-types.ts b/src/api/app-types.tsindex 34e3f438c..bff15b8d0 100644--- a/src/api/app-types.ts+++ b/src/api/app-types.ts@@ -1,3 +1,4 @@+import type { TeamStore } from "../teams/teams.ts";import type { ExternalSlackPolicies } from "../resolution/external-slack.ts";import type { InviteMailer } from "../admin/invite-email.ts";import type { DeploymentInvitation } from "../deploy/email-access.ts";@@ -360,6 +361,7 @@ export interface App {principalId: string,query: string,): Promise<{ hits: ResourceSearchHit[]; failed: string[]; limited: string[] }>;+ searchConversations: App["search"];searchSessions(principalId: string, query: string, limit?: number): Promise<SessionSearchHit[]>;search(query: string,@@ -659,6 +661,7 @@ export interface AppDeps {webhooks: WebhookStore;deliveries: DeliveryStore;directory: DirectoryStore;+ teams?: Pick<TeamStore, "isMember">;emailAuthMembers?: DirectoryMember[];projects?: ProjectStore;deploy: DeployService;diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.tsindex 888a37e72..8c99bb198 100644--- a/src/harness/agent-tools.ts+++ b/src/harness/agent-tools.ts@@ -1695,7 +1695,8 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):name: "sessions",label: "sessions",description:- "Work with sessions: the conversations people see in the web sidebar, within this same context. Subagents are not sessions; use the subagents tool for them. " ++ "Work with sessions: the conversations people see in the web sidebar, with list/read scoped to this context. Subagents are not sessions; use the subagents tool for them. " ++ "`search` finds matching entries across conversations visible to everyone here, with sessionId and entry seq references; it does not expand read/send_message access. " +"`list` shows the sessions here that you can see. `read` shows a session's recent transcript. " +"`send_message` delivers a private note to another session without starting a turn; it arrives there as internal context, not as a request from a person. " +"`new` starts a clean session whose first message is `text`; it inherits nothing from this conversation. " +@@ -1704,11 +1705,13 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):parameters: Type.Object({action: Type.Union([Type.Literal("list"),+ Type.Literal("search"),Type.Literal("read"),Type.Literal("send_message"),Type.Literal("new"),Type.Literal("fork"),]),+ query: Type.Optional(Type.String({ description: "search: words to find across visible conversations." })),target: Type.Optional(Type.String({ description: "read and send_message: a sessionId from list." })),text: Type.Optional(Type.String({@@ -1717,12 +1720,15 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):}),),title: Type.Optional(Type.String({ description: "new and fork: optional sidebar title." })),- limit: Type.Optional(Type.Integer({ description: "read: max transcript entries to show (default 30)." })),+ limit: Type.Optional(+ Type.Integer({ description: "read: max entries (default 30). search: max matches (default 20, maximum 100)." }),+ ),}),async execute(callId, params) {const syscalls = ref.current?.sessionSyscalls;const p = params as {- action: "list" | "read" | "send_message" | "new" | "fork";+ action: "search" | "list" | "read" | "send_message" | "new" | "fork";+ query?: string;target?: string;text?: string;title?: string;@@ -1737,6 +1743,16 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):});const fail = (message: string) =>recordResult(callId, { tool: "sessions", action: p.action, error: message }, text(`[error] ${message}`), true);+ if (p.action === "search") {+ if (!p.query?.trim()) return fail("search requires `query`.");+ if (!syscalls?.search) return fail("session search isn't available on this turn.");+ const result = await syscalls.search({ query: p.query, ...(p.limit !== undefined ? { limit: p.limit } : {}) });+ return recordResult(+ callId,+ { tool: "sessions", action: "search", count: result.hits.length, failedBackends: result.failedBackends },+ text(JSON.stringify(result)),+ );+ }if (!syscalls?.list || !syscalls.start) return fail("sessions aren't available on this turn.");if (p.action === "list") {const result = await syscalls.list();diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.tsindex 8235353bb..31c4a0829 100644--- a/src/sessions/memory-session-store.ts+++ b/src/sessions/memory-session-store.ts@@ -612,9 +612,12 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStoreif (!session) continue;for (const row of indexed) {if (!entryWithinTenure(row, win)) continue;+ const entry = entries.get(sessionId)?.[row.seq];+ if (!entry) continue;if (!matchesSearchTerms(row.text, terms)) continue;hits.push({sessionId,+ entryScopeLabel: entry.scopeLabel,scopeId: session.scopeId,...((win.title ?? session.title) != null ? { title: win.title ?? session.title } : {}),...(session.channelName ? { channelName: session.channelName } : {}),diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.tsindex bade3dc5b..684d763ca 100644--- a/src/sessions/postgres-session-store.ts+++ b/src/sessions/postgres-session-store.ts@@ -1624,9 +1624,10 @@ export function createPostgresSessionStore(connectionString: string, opts: Storeconst ts = tsPrefixQuery(query);if (!ts) return [];const rows = await q(- `SELECT h.session_id, h.seq, h.type, h.author, h.text, h.created_at,+ `SELECT h.session_id, h.seq, h.type, h.author, h.text, h.created_at, e.scope_label,s.scope_id, COALESCE(p.title, s.title) AS title, s.channel_name, s.surface, p.archivedFROM session_entry_search h+ JOIN session_entries e ON e.session_id = h.session_id AND e.seq = h.seqJOIN participants p ON p.session_id = h.session_id AND p.principal_id = $1JOIN sessions s ON s.id = h.session_idWHERE h.search_tsv @@ to_tsquery('simple', $2)@@ -1642,6 +1643,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Storereturn [{sessionId: r.session_id as string,+ entryScopeLabel: r.scope_label as ScopeId,scopeId: r.scope_id as ScopeId,...(r.title != null ? { title: r.title as string } : {}),...(r.channel_name ? { channelName: r.channel_name as string } : {}),diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.tsindex b79c94455..d9f02bae3 100644--- a/src/sessions/session-store.ts+++ b/src/sessions/session-store.ts@@ -532,6 +532,7 @@ export interface DistinctScope {}export interface EntrySearchHit extends Pick<Session, "scopeId" | "title" | "channelName" | "surface" | "archived"> {+ entryScopeLabel: ScopeId;sessionId: string;seq: number;type: EntryType;diff --git a/src/sessions/session-syscalls.ts b/src/sessions/session-syscalls.tsindex ad628ff30..591ec4804 100644--- a/src/sessions/session-syscalls.ts+++ b/src/sessions/session-syscalls.ts@@ -12,6 +12,7 @@ import { filterHistoryForAudience } from "../resolution/context-filter.ts";import { randomUUID } from "node:crypto";import { hashId } from "../util/crypto.ts";import { canonicalJson } from "../util/objects.ts";+import type { CoreSearch } from "../search/core-search.ts";import type { ScopeId, Session, SessionEntry, SpawnMeta } from "../types.ts";import type { OrchestratorInput } from "../core/orchestrator/types.ts";import type { Run, RunStore } from "../runs/run-store.ts";@@ -212,6 +213,7 @@ export interface SessionSyscalls {open(input: SessionOpenInput): Promise<SessionOpenResult>;write(input: SessionWriteInput): Promise<SessionWriteResult>;read(input: SessionReadInput): Promise<SessionReadResult>;+ search?(input: { query: string; limit?: number }): ReturnType<CoreSearch["search"]>;list?(): Promise<SessionListResult>;start?(input: SessionStartInput): Promise<SessionStartResult>;}@@ -248,6 +250,7 @@ export interface SessionSyscallDeps {authorize?: (session: Session, actorId: string) => Promise<boolean>;validateRuntime?: (input: SessionOpenInput, scopeId: ScopeId) => Promise<void>;conversations?: {+ search?: CoreSearch["search"];list(actorId: string): Promise<Session[]>;start(actorId: string,@@ -942,6 +945,22 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscallsrendered: rendered || "[no readable entries yet]",};},+ async search(input) {+ const caller = await currentCaller();+ const refusal = sidebarRefusal();+ if (refusal) throw new Error(refusal);+ const search = deps.conversations?.search;+ if (!search) throw new Error("session search isn't available on this deployment.");+ const members = caller.conversation.publishMembers;+ if (caller.conversation.kind !== "dm" && !members?.length)+ throw new Error("search requires the complete principal set for a shared conversation");+ if (!input.query.trim()) throw new Error("search requires a query");+ return search({+ query: input.query,+ principals: [caller.actor, ...(members ?? caller.conversation.audience)],+ ...(input.limit !== undefined ? { limit: input.limit } : {}),+ });+ },async list() {await currentCaller();const conversations = deps.conversations;diff --git a/src/wiring.ts b/src/wiring.tsindex 4482cd602..d3d1f3f6c 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -1926,6 +1926,7 @@ export function buildApp(prepareRequest: prepareSessionRequest,authorize: (session, actorId) => canWriteScope(actorId, session.scopeId),conversations: {+ search: ({ query, principals, limit }) => app.searchConversations(query, principals, limit),list: (actorId) => app.listSessions(actorId),start: (actorId, input) => startSession(app, sessions, actorId, input),},@@ -2144,6 +2145,7 @@ export function buildApp(}): undefined;const app = createApp({+ teams,externalSlackPolicies: config.externalSlackPolicies,admittedWork,shutdown: () => inlineShutdown.signal,diff --git a/test/agent-tools.test.ts b/test/agent-tools.test.tsindex c4b8cb0c2..5d6e6bba5 100644--- a/test/agent-tools.test.ts+++ b/test/agent-tools.test.ts@@ -4286,6 +4286,38 @@ test("background list labels jobs by purpose and retains commands and legacy falassert.match(result, /old-job[^\n]*bg: npm test/);});+test("sessions search forwards a bounded query and preserves source references and failures", async () => {+ const tc = fakeToolContext();+ const seen: unknown[] = [];+ tc.sessionSyscalls = {+ open: async () => ({ ok: false, message: "unused" }),+ write: async () => ({ ok: false, message: "unused" }),+ read: async () => ({ ok: false, message: "unused" }),+ search: async (input) => {+ seen.push(input);+ return {+ hits: [+ {+ id: "source:7",+ type: "conversation",+ backend: "conversations",+ snippet: "memo",+ metadata: { sessionId: "source", seq: 7 },+ },+ ],+ failedBackends: ["conversations"],+ };+ },+ };+ const tool = createAgentTools({ current: tc }, { surfaceName: "web" }).find((t) => t.name === "sessions")!;+ assert.match(textOut(await call(tool, { action: "search" })), /requires `query`/);+ assert.deepEqual(seen, []);+ const output = JSON.parse(textOut(await call(tool, { action: "search", query: "memo", limit: 3 })));+ assert.deepEqual(seen, [{ query: "memo", limit: 3 }]);+ assert.deepEqual(output.hits[0].metadata, { sessionId: "source", seq: 7 });+ assert.deepEqual(output.failedBackends, ["conversations"]);+});+test("background read approval cannot authorize process input or termination", async () => {let mutations = 0;const ref: ToolContextRef = {diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.tsindex d4dff0802..bca359e6c 100644--- a/test/postgres-store.test.ts+++ b/test/postgres-store.test.ts@@ -2772,6 +2772,7 @@ test("pg search: full-text over entries with prefix match, window ACL, and typeawait s.releaseLease(lease);const hits = await s.searchEntries("USRCH", "memo boar");+ assert.ok(hits.every((hit) => hit.entryScopeLabel === scopeId("personal", "USRCH")));assert.equal(hits.length, 3, "prefix terms match user and assistant text; tool calls are ignored");assert.equal(hits[0]!.text, "memo board postscript", "newest first");assert.equal(hits[2]!.author, "josh", "user hits carry the author");diff --git a/test/session-search-tool.test.ts b/test/session-search-tool.test.tsnew file mode 100644index 000000000..6d3b800a0--- /dev/null+++ b/test/session-search-tool.test.ts@@ -0,0 +1,120 @@+import "./support/auto-fake-sprites.ts";+import { test } from "node:test";+import assert from "node:assert/strict";+import { mkdtempSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { buildApp } from "../src/wiring.ts";+import { testConfig } from "./support/test-config.ts";+import { createSessionSyscalls } from "../src/sessions/session-syscalls.ts";+import { createSessionMailbox, type SessionMessage } from "../src/sessions/session-mailbox.ts";+import { createMemoryMap } from "../src/persistence/durable-map.ts";+import { createMemoryRunStore } from "../src/runs/memory-run-store.ts";+import { createMemoryRunSignalStore } from "../src/runs/run-signal-store.ts";+import { scopeId, type Principal } from "../src/types.ts";++const alice: Principal = { id: "[email protected]", type: "internal" };+const bob: Principal = { id: "[email protected]", type: "internal" };++test("session search reuses indexed search with audience intersection, tenure windows and bounded references", async () => {+ const { app, sessions } = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "session-search-tool-")) }));+ const scope = scopeId("group", "room");+ const current = await sessions.getOrCreateByThread("web:room", "group", scope);+ const source = await sessions.getOrCreateByThread("web:source", "group", scopeId("group", "source"));+ await sessions.addParticipant(source.id, alice.id, undefined, { includeHistory: true });+ const { lease } = await sessions.acquireLease(source.id);+ assert.ok(lease);+ await sessions.append(lease, {+ type: "user",+ payload: { text: "memo before Bob joined" },+ scopeLabel: source.scopeId,+ });+ await sessions.addParticipant(source.id, bob.id);+ const shared = await sessions.append(lease, {+ type: "user",+ payload: { text: "memo shared" },+ scopeLabel: source.scopeId,+ });+ await sessions.append(lease, {+ type: "assistant",+ payload: { text: "memo private-only" },+ scopeLabel: scopeId("personal", alice.id),+ });+ await sessions.releaseLease(lease);+ const { runs } = createMemoryRunStore();+ let members: Principal[] | undefined;+ const factory = createSessionSyscalls({+ sessions,+ runs,+ signals: createMemoryRunSignalStore(),+ mailbox: createSessionMailbox(createMemoryMap<SessionMessage>()),+ maxAttempts: 1,+ prepareRequest: async (request) => ({+ ...request,+ conversation: { ...request.conversation, publishMembers: members },+ }),+ conversations: {+ list: async () => [],+ start: async () => ({ error: "unused" }),+ search: ({ query, principals, limit }) => app.searchConversations(query, principals, limit),+ },+ });+ const tools = factory.forTurn({+ session: current,+ scopeId: scope,+ request: {+ surface: "web",+ actor: alice,+ conversation: { kind: "group", threadRef: current.threadRef, audience: [alice], publishMembers: [alice] },+ },+ });+ await assert.rejects(() => tools.search!({ query: "memo" }), /complete principal set/);+ members = [bob];+ const result = await tools.search!({ query: "memo" });+ assert.deepEqual(result.failedBackends, []);+ assert.equal(result.hits.length, 1);+ assert.deepEqual(+ result.hits.map((h) => [h.metadata?.sessionId, h.metadata?.seq, h.snippet]),+ [[source.id, shared.seq, "memo shared"]],+ );+ assert.equal((await tools.search!({ query: "memo shared", limit: 1 })).hits.length, 1);+ assert.deepEqual((await app.search("private-only", [bob])).hits, []);+ assert.deepEqual(await app.searchSessions(bob.id, "private-only"), []);+ members = [alice];+ assert.equal((await tools.search!({ query: "memo" })).hits.length, 3);+ assert.equal((await tools.search!({ query: "memo", limit: 1 })).hits.length, 1);+ members = [bob, { id: "[email protected]", type: "internal" }];+ assert.deepEqual((await tools.search!({ query: "memo" })).hits, []);+});++test("shared session search resolves team labels against current membership", async () => {+ const { createSessionMethods } = await import("../src/api/app-sessions.ts");+ const { createMemorySessionStore } = await import("../src/sessions/memory-session-store.ts");+ const { createIdentityService } = await import("../src/identity/identity-service.ts");+ const sessions = createMemorySessionStore();+ const source = await sessions.getOrCreateByThread("web:team-search", "group", scopeId("group", "team-search"));+ await sessions.addParticipant(source.id, alice.id, undefined, { includeHistory: true });+ await sessions.addParticipant(source.id, bob.id, undefined, { includeHistory: true });+ const { lease } = await sessions.acquireLease(source.id);+ assert.ok(lease);+ await sessions.append(lease, {+ type: "assistant",+ payload: { text: "engineering memo" },+ scopeLabel: scopeId("team", "eng"),+ });+ await sessions.releaseLease(lease);+ let member = alice.id;+ const methods = createSessionMethods(+ {+ sessions,+ identity: createIdentityService(),+ teams: { isMember: async (id, team) => id === member && team === "eng" },+ } as import("../src/api/app-types.ts").AppDeps,+ { managedProjectMembership: async () => undefined } as unknown as import("../src/api/app-helpers.ts").AppHelpers,+ );+ assert.equal((await methods.searchSessions(alice.id, "engineering")).length, 1);+ assert.deepEqual(await methods.searchSessions(bob.id, "engineering"), []);+ member = bob.id;+ assert.deepEqual(await methods.searchSessions(alice.id, "engineering"), []);+ assert.equal((await methods.searchSessions(bob.id, "engineering")).length, 1);+});