Multiplayer agent harness for work.
Let the guidance tool read and set a channel's ambient-replies mode (#397)
Regan Bell4eaa1a7e6c5b
diff --git a/src/core/orchestrator/surface-tools.ts b/src/core/orchestrator/surface-tools.tsindex 42ced8cc9..2634b4954 100644--- a/src/core/orchestrator/surface-tools.ts+++ b/src/core/orchestrator/surface-tools.ts@@ -417,9 +417,14 @@ export function createSurfaceToolDeps(ctx: SurfaceToolsContext): SurfaceToolDepsif (conversation.kind === "dm" || !conversation.channelRef)return { ok: false, message: "standing orders are per-channel — there isn't one for a DM." };const p = await deps.channelPolicy.get(conversation.channelRef);- return { ok: true, orders: p?.orders ?? "", ...(p?.bots && Object.keys(p.bots).length ? { bots: p.bots } : {}) };+ return {+ ok: true,+ orders: p?.orders ?? "",+ ...(p?.bots && Object.keys(p.bots).length ? { bots: p.bots } : {}),+ ...(p?.ambientEnabled !== undefined ? { ambientEnabled: p.ambientEnabled } : {}),+ };},- setStandingOrder: async (orders: string, bots?: Record<string, BotPolicy>) => {+ setStandingOrder: async (orders: string, bots?: Record<string, BotPolicy>, ambientEnabled?: boolean | null) => {if (!deps.channelPolicy) return { ok: false, message: "standing orders aren't available on this turn" };if (conversation.kind === "dm" || !conversation.channelRef)return { ok: false, message: "standing orders are per-channel — you can only set one from inside a channel." };@@ -433,6 +438,7 @@ export function createSurfaceToolDeps(ctx: SurfaceToolsContext): SurfaceToolDepssetBy: actor.id,bots: parsedBots,sessionId: session.id,+ ambientEnabled,});deps.auditLog.record({at: Date.now(),@@ -441,7 +447,12 @@ export function createSurfaceToolDeps(ctx: SurfaceToolsContext): SurfaceToolDepsresource: conversation.channelRef,scopeLabel: scopeId,});- return { ok: true, orders, ...(p.bots && Object.keys(p.bots).length ? { bots: p.bots } : {}) };+ return {+ ok: true,+ orders,+ ...(p.bots && Object.keys(p.bots).length ? { bots: p.bots } : {}),+ ...(p.ambientEnabled !== undefined ? { ambientEnabled: p.ambientEnabled } : {}),+ };},staySilent: async (reason: string) => {spine.staySilentReason = reason;diff --git a/src/harness/pi-tools.ts b/src/harness/pi-tools.tsindex 2113d9cc0..256ae3c55 100644--- a/src/harness/pi-tools.ts+++ b/src/harness/pi-tools.ts@@ -1801,6 +1801,12 @@ export function createPiTools(ref: ToolContextRef, opts?: PiToolsOptions): ToolDaction: Type.Union([Type.Literal("read"), Type.Literal("write")]),scope: Type.Optional(Type.Union([Type.Literal("channel"), Type.Literal("conversation")])),content: Type.Optional(Type.String()),+ ambientEnabled: Type.Optional(+ Type.Union([Type.Boolean(), Type.Null()], {+ description:+ "Channel scope only. true judges every message for an unprompted reply, false responds only when addressed, and null uses the platform default.",+ }),+ ),bots: Type.Optional(Type.Record(Type.String(),@@ -1855,24 +1861,39 @@ export function createPiTools(ref: ToolContextRef, opts?: PiToolsOptions): ToolDconst note = convoExists? "\n\n(conversation-scope guidance also exists — read with scope=conversation)": "";- const body = (channel!.orders.trim() ? channel!.orders : "[no channel guidance set]") + ledger + note;+ let ambientState = "default";+ if (channel!.ambientEnabled !== undefined) ambientState = channel!.ambientEnabled ? "on" : "off";+ const ambient = `\n\nAmbient replies: ${ambientState}`;+ const body =+ (channel!.orders.trim() ? channel!.orders : "[no channel guidance set]") + ambient + ledger + note;return recordResult(callId, { tool: "guidance", scope, ok: true }, text(body));}- if (typeof params.content !== "string" && params.bots === undefined) {+ if (typeof params.content !== "string" && params.bots === undefined && params.ambientEnabled === undefined) {return recordResult(callId,- { tool: "guidance", scope, error: "content or bots required" },- text("[error] guidance write needs `content` (the full new channel guidance) and/or `bots`."),+ { tool: "guidance", scope, error: "content, bots, or ambientEnabled required" },+ text(+ "[error] guidance write needs `content` (the full new channel guidance), `bots`, and/or `ambientEnabled`.",+ ),true,);}const orders = typeof params.content === "string" ? params.content : channel!.orders;- const r = await tc.setStandingOrder(orders, params.bots);+ const r = await tc.setStandingOrder(orders, params.bots, params.ambientEnabled);if (!r.ok)return recordResult(callId, { tool: "guidance", scope, ok: false }, text(`[error] ${r.message}`), true);return recordResult(callId, { tool: "guidance", scope, ok: true }, text("[channel guidance updated]"));}+ if (params.ambientEnabled !== undefined) {+ return recordResult(+ callId,+ { tool: "guidance", scope, error: "ambientEnabled is channel scope only" },+ text("[error] `ambientEnabled` applies only to channel scope."),+ true,+ );+ }+if (params.action === "read") {const r = tc.soulRead();if (isUnavailable(r)) return unavailable(callId, "guidance");diff --git a/src/tools/primitives.ts b/src/tools/primitives.tsindex 43b6bc93b..596cc7935 100644--- a/src/tools/primitives.ts+++ b/src/tools/primitives.ts@@ -311,7 +311,8 @@ interface SurfaceFileResult {}type SurfaceStandingOrderResult =- { ok: true; orders: string; bots?: Record<string, BotPolicy> } | { ok: false; message: string };+ | { ok: true; orders: string; bots?: Record<string, BotPolicy>; ambientEnabled?: boolean }+ | { ok: false; message: string };export interface SurfaceToolDeps {post(text: string, opts?: SurfacePostOpts, files?: readonly string[]): Promise<SurfacePostResult>;@@ -325,7 +326,11 @@ export interface SurfaceToolDeps {readMembers(): Promise<SurfaceMembersResult>;readFile(ref: string): Promise<SurfaceFileResult>;getStandingOrder(): Promise<SurfaceStandingOrderResult>;- setStandingOrder(orders: string, bots?: Record<string, BotPolicy>): Promise<SurfaceStandingOrderResult>;+ setStandingOrder(+ orders: string,+ bots?: Record<string, BotPolicy>,+ ambientEnabled?: boolean | null,+ ): Promise<SurfaceStandingOrderResult>;staySilent(reason: string): Promise<{ ok: true; message: string }>;}@@ -961,7 +966,8 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {readMembers: () => surfaceOp((s) => s.readMembers()),readFile: (ref) => surfaceOp((s) => s.readFile(ref)),getStandingOrder: () => surfaceOp((s) => s.getStandingOrder()),- setStandingOrder: (orders, bots) => surfaceOp((s) => s.setStandingOrder(orders, bots)),+ setStandingOrder: (orders, bots, ambientEnabled) =>+ surfaceOp((s) => s.setStandingOrder(orders, bots, ambientEnabled)),staySilent: (reason) =>deps.surface? deps.surface.staySilent(reason)diff --git a/test/pi-tools.test.ts b/test/pi-tools.test.tsindex 1dfabc8cb..42e9159c7 100644--- a/test/pi-tools.test.ts+++ b/test/pi-tools.test.ts@@ -1801,7 +1801,7 @@ test("guidance conversation scope reads the effective SOUL; write requires conte});test("guidance defaults to channel scope when a channel is available, and rewrites the channel order", async () => {- assert.match(textOut(await call(tool("guidance"), { action: "read" })), /no channel guidance set/);+ assert.match(textOut(await call(tool("guidance"), { action: "read" })), /Ambient replies: default/);assert.match(textOut(await call(tool("guidance"), { action: "write", content: "reply piratey to tweets" })),/channel guidance updated/,@@ -1812,7 +1812,44 @@ test("guidance defaults to channel scope when a channel is available, and rewrit);assert.match(textOut(await call(tool("guidance"), { action: "write" })),- /\[error\].*needs `content`.*and\/or `bots`/,+ /\[error\].*needs `content`.*`bots`.*and\/or `ambientEnabled`/,+ );+});++test("guidance reads and writes channel ambient replies without changing omitted state", async () => {+ let ambientEnabled: boolean | undefined;+ const tc: ToolContext = {+ ...fakeToolContext(),+ async getStandingOrder() {+ return { ok: true, orders: "keep watch", ...(ambientEnabled === undefined ? {} : { ambientEnabled }) };+ },+ async setStandingOrder(orders, _bots, nextAmbientEnabled) {+ if (nextAmbientEnabled !== undefined) ambientEnabled = nextAmbientEnabled ?? undefined;+ return { ok: true, orders, ...(ambientEnabled === undefined ? {} : { ambientEnabled }) };+ },+ };++ assert.match(textOut(await call(tool("guidance", tc), { action: "write", ambientEnabled: true })), /updated/);+ assert.match(textOut(await call(tool("guidance", tc), { action: "read" })), /Ambient replies: on/);+ await call(tool("guidance", tc), { action: "write", content: "keep watching" });+ assert.match(textOut(await call(tool("guidance", tc), { action: "read" })), /Ambient replies: on/);+ await call(tool("guidance", tc), { action: "write", ambientEnabled: false });+ assert.match(textOut(await call(tool("guidance", tc), { action: "read" })), /Ambient replies: off/);+ await call(tool("guidance", tc), { action: "write", ambientEnabled: null });+ assert.match(textOut(await call(tool("guidance", tc), { action: "read" })), /Ambient replies: default/);+});++test("guidance rejects ambient replies at conversation scope", async () => {+ assert.match(+ textOut(+ await call(tool("guidance"), {+ action: "write",+ scope: "conversation",+ content: "Be terse.",+ ambientEnabled: true,+ }),+ ),+ /\[error\].*applies only to channel scope/,);});diff --git a/test/surface-post-files.test.ts b/test/surface-post-files.test.tsindex 67b950ada..325eafa0e 100644--- a/test/surface-post-files.test.ts+++ b/test/surface-post-files.test.ts@@ -7,6 +7,7 @@ import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";import { scopeId } from "../src/types.ts";import type { Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";+import { createMemoryChannelPolicyStore } from "../src/surface-cache/channel-policy-store.ts";function fakeSandbox(files: Record<string, Uint8Array>, outboxListing: string[]): Sandbox {return {@@ -102,6 +103,38 @@ test("surface post rejects traversal before provisioning or staging any attachmeassert.deepEqual(calls, { provision: 0, read: 0, put: 0, grant: 0 });});+test("surface standing orders preserve and reset the stored ambient reply policy", async () => {+ const channelPolicy = createMemoryChannelPolicyStore();+ const tools = createSurfaceToolDeps({+ deps: { deliveries: {}, channelPolicy, auditLog: { record() {} } },+ input: { surfaceTools: true },+ actor: { id: "U1" },+ conversation: { kind: "channel", channelRef: "C1" },+ session: { id: "S1" },+ scopeId: "channel:C1",+ defaultDestination: {},+ strictReadOnly: false,+ blobTransfer: {},+ fileRegistration: {},+ provision: async () => handle,+ postProvenance() {+ return {};+ },+ spine: { surfaceOutboundCount: 0, crossConversationPosts: 0 },+ } as unknown as SurfaceToolsContext)!;++ await tools.setStandingOrder("watch", undefined, true);+ assert.equal((await channelPolicy.get("C1"))?.ambientEnabled, true);+ const enabledOrder = await tools.getStandingOrder();+ assert.equal(enabledOrder.ok && enabledOrder.ambientEnabled, true);+ await tools.setStandingOrder("keep watching");+ assert.equal((await channelPolicy.get("C1"))?.ambientEnabled, true);+ await tools.setStandingOrder("keep watching", undefined, null);+ assert.equal((await channelPolicy.get("C1"))?.ambientEnabled, undefined);+ const defaultOrder = await tools.getStandingOrder();+ assert.equal(defaultOrder.ok && defaultOrder.ambientEnabled, undefined);+});+test("collectNamedOutbound: a missing/empty path is reported (so post can fail the WHOLE call)", async () => {const transfer = createMemoryBlobTransferStore();const sandbox = fakeSandbox({ "outbox/there.png": bytes("X"), "outbox/blank.txt": bytes("") }, []);