Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Fix sessions tool; subagents inherit their parent's capabilities (#2155)

Regan Bell81a08e00eb10
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index e432f92b9..c3102db01 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -549,15 +549,15 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
deps.identity.audienceIsAllInternal(conversation.audience) &&
(conversation.kind === "dm" ||
(!!conversation.publishMembers?.length && conversation.publishMembers.every((p) => p.type === "internal")));
- const liveTurn = humanTurn && allInternal && !external;
const authoredDetection =
input.origin.kind === "ambient" && input.origin.live === true && conversation.kind !== "dm";
const delegationEnabled =
!external && (await deps.featureFlags?.enabled("responsive_spine", `personal:${actor.id}` as ScopeId)) === true;
const delegatedOrigin =
- delegationEnabled && deps.runs
+ !external && deps.runs
? await delegatedAuthorizationOrigin(input, { runs: deps.runs, sessions: deps.sessions })
: undefined;
+ const liveTurn = (humanTurn || delegatedOrigin?.kind === "human") && allInternal && !external;
const liveAuthorTurn =
!external && (humanTurn || authoredDetection || delegatedOrigin !== undefined) && allInternal;
const messageTs = input.origin.kind === "human" ? input.origin.messageTs : undefined;
diff --git a/src/sessions/session-syscalls.ts b/src/sessions/session-syscalls.ts
index 114ce840a..8b8e27e2e 100644
--- a/src/sessions/session-syscalls.ts
+++ b/src/sessions/session-syscalls.ts
@@ -108,7 +108,7 @@ const READ_MAX_CHARS_CEILING = 20_000;
const MAIL_ERROR_CAP = 1_000;
const LAST_SAID_SCAN = 40;
-export interface SessionOpenInput {
+interface SessionOpenInput {
requestId?: string;
task: string;
name?: string;
@@ -122,7 +122,7 @@ export interface SessionOpenInput {
type SessionOpenResult =
{ ok: true; sessionId: string; title: string; liveRunsRemaining: number } | { ok: false; message: string };
-export interface SessionWriteInput {
+interface SessionWriteInput {
target: string;
peer?: boolean;
text?: string;
@@ -140,7 +140,7 @@ type SessionWriteResult =
}
| { ok: false; message: string };
-export interface SessionReadInput {
+interface SessionReadInput {
target?: string;
peer?: boolean;
limit?: number;
@@ -520,8 +520,7 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscalls
function sidebarRefusal(): string | null {
if (binding.request.surface !== "web") return WEB_ONLY;
- if (binding.request.swarm || binding.session.parentSessionId || isSubagentThreadRef(binding.session.threadRef))
- return "sessions belong to the person's sidebar; a subagent reports to its parent instead.";
+ if (binding.request.swarm) return "swarm workers coordinate through the swarm API.";
return null;
}
diff --git a/src/tools/primitives.ts b/src/tools/primitives.ts
index 2ec484366..f6fe7ea6b 100644
--- a/src/tools/primitives.ts
+++ b/src/tools/primitives.ts
@@ -34,12 +34,7 @@ import { type PublishAudienceKind } from "../resolution/publish-audience.ts";
import type { WorkspaceStore } from "../workspace/workspace-store.ts";
import type { BotPolicy } from "../surface-cache/channel-policy-store.ts";
import type { GapPhase, GapWork } from "../sessions/session-store.ts";
-import type {
- SessionOpenInput,
- SessionReadInput,
- SessionSyscalls,
- SessionWriteInput,
-} from "../sessions/session-syscalls.ts";
+import type { SessionSyscalls } from "../sessions/session-syscalls.ts";
import { evaluateCommandWithLayer } from "../policy/command-policy.ts";
import { createNullLedger, type ToolLedger } from "../runs/tool-ledger.ts";
import { waitForClientResult, type RunSignalStore } from "../runs/run-signal-store.ts";
@@ -1184,17 +1179,7 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
},
...(deps.humanTurn ? { humanTurn: true } : {}),
- ...(deps.sessionSyscalls
- ? {
- sessionSyscalls: {
- receive: (timeoutMs?: number) => deps.sessionSyscalls!.receive?.(timeoutMs) ?? Promise.resolve([]),
- acknowledge: (ids: string[]) => deps.sessionSyscalls!.acknowledge?.(ids) ?? Promise.resolve(),
- open: (input: SessionOpenInput) => once(() => deps.sessionSyscalls!.open(input)),
- write: (input: SessionWriteInput) => once(() => deps.sessionSyscalls!.write(input)),
- read: (input: SessionReadInput) => deps.sessionSyscalls!.read(input),
- },
- }
- : {}),
+ ...(deps.sessionSyscalls ? { sessionSyscalls: deps.sessionSyscalls } : {}),
async history(q: string, limit?: number): Promise<string[]> {
if (!deps.sessionHistory) return [];
diff --git a/test/control-tool-context.test.ts b/test/control-tool-context.test.ts
index 48f26d991..d983294ea 100644
--- a/test/control-tool-context.test.ts
+++ b/test/control-tool-context.test.ts
@@ -287,34 +287,3 @@ test("without control/controlClaims wired, every control method returns CONTROL_
assert.deepEqual(await ctx.webhookList(), CONTROL_UNAVAILABLE);
assert.deepEqual(ctx.soulRead(), CONTROL_UNAVAILABLE);
});
-
-test("concurrent session calls retain distinct ledger receipts", async () => {
- const { ledger } = memoryLedger();
- const ctx = ctxFor({
- ledger,
- runId: "parallel-session-calls",
- attempt: 1,
- sessionSyscalls: {
- async open(input) {
- await Promise.resolve();
- return { ok: true, sessionId: input.task, title: input.task, liveRunsRemaining: 8 };
- },
- async write() {
- return { ok: false, message: "unused" };
- },
- async read() {
- return { ok: true, mode: "children", children: [] };
- },
- },
- });
- const results = await Promise.all([
- ctx.sessionSyscalls!.open({ task: "first" }),
- ctx.sessionSyscalls!.open({ task: "second" }),
- ]);
- const first = await ledger.begin("parallel-session-calls", 1, 0);
- const second = await ledger.begin("parallel-session-calls", 1, 1);
- assert.equal(first.cached, true);
- assert.equal(second.cached, true);
- assert.deepEqual(JSON.parse(first.output!), results[0]);
- assert.deepEqual(JSON.parse(second.output!), results[1]);
-});
diff --git a/test/session-syscalls.test.ts b/test/session-syscalls.test.ts
index e80eb6010..d004f78b4 100644
--- a/test/session-syscalls.test.ts
+++ b/test/session-syscalls.test.ts
@@ -1808,7 +1808,8 @@ test("sessions list shows only sidebar sessions the whole audience can see and c
origin: { kind: "human" },
} as Parameters<typeof factory.forTurn>[0]["request"],
});
- for (const out of [await fromChild.list!(), await fromChild.start!({ fork: true })]) assert.equal(out.ok, false);
+ for (const out of [await fromChild.list!(), await fromChild.start!({ fork: true })])
+ assert.equal(out.ok, true, JSON.stringify(out));
const unshareable =
"this conversation's earlier history was shared with a different audience, so it can't be forked from here.";
const checkpoint = async (audience: string, throughSeq: number) => {
@@ -1852,5 +1853,5 @@ test("sessions list shows only sidebar sessions the whole audience can see and c
const out = await blocked.start!({ fork: true });
assert.equal(out.ok, false);
}
- assert.equal(started.length, 3);
+ assert.equal(started.length, 4);
});
diff --git a/test/sharing-context-e2e.test.ts b/test/sharing-context-e2e.test.ts
index ddba4f443..4144966bf 100644
--- a/test/sharing-context-e2e.test.ts
+++ b/test/sharing-context-e2e.test.ts
@@ -111,7 +111,7 @@ test("sharing e2e: personal files and memories follow the speaker, opt-out, and
);
});
-test("sharing e2e: delegated live sharing requires the current pilot flag and current sharing permission", async (t) => {
+test("sharing e2e: a delegated turn shares its live parent's access under current sharing permission", async (t) => {
const b = await fixture(t, { apiBaseUrl: "https://core.example.com", signingSecret: "test-ingress-secret" });
await b.workspace.write("personal:U1", "notes.txt", "DELEGATED_PERSONAL_FILE");
await b.turn("inspect my personal notes", true);
@@ -163,26 +163,16 @@ test("sharing e2e: delegated live sharing requires the current pilot flag and cu
}
};
const read = "!read shared/open-personal-U1/notes.txt";
- assert.match(await childTurn(read), /no file/);
- await b.featureFlags.setEnabled("responsive_spine", "personal:U1", true, "U1");
assert.equal(await childTurn(read), "DELEGATED_PERSONAL_FILE");
assert.equal(await childTurn("!run echo delegated"), "delegated");
assert.ok(token);
const delegatedClaims = await verifyCapabilityToken(token, TEST_CAPABILITY_SECRET);
assert.equal(delegatedClaims?.liveAuthor, true);
- assert.notEqual(delegatedClaims?.liveActor, true);
+ assert.equal(delegatedClaims?.liveActor, true);
assert.equal(delegatedClaims?.triggered, true);
await b.config.setSharingPosture("personal:U1", "isolated");
assert.match(await childTurn(read), /no file/);
await b.config.clearSharingPosture("personal:U1");
- await b.featureFlags.setEnabled("responsive_spine", "personal:U1", false, "U1");
- assert.match(await childTurn(read), /no file/);
- assert.equal(await childTurn("!run echo unprivileged"), "unprivileged");
- assert.ok(token);
- const unprivilegedClaims = await verifyCapabilityToken(token, TEST_CAPABILITY_SECRET);
- assert.notEqual(unprivilegedClaims?.liveAuthor, true);
- assert.notEqual(unprivilegedClaims?.liveActor, true);
- await b.featureFlags.setEnabled("responsive_spine", "personal:U1", true, "U1");
await b.remove("U1");
assert.match(await childTurn(read, "refused"), /access is no longer current/);
});
diff --git a/test/subagent-parity.test.ts b/test/subagent-parity.test.ts
new file mode 100644
index 000000000..389d0a7b5
--- /dev/null
+++ b/test/subagent-parity.test.ts
@@ -0,0 +1,215 @@
+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 { createOrchestrator, type OrchestratorInput } from "../src/core/orchestrator.ts";
+import { createIdentityService } from "../src/identity/identity-service.ts";
+import { createMemoryConfigStore } from "../src/resolution/config-store.ts";
+import { createAclStore } from "../src/acl/acl-store.ts";
+import { createResolutionService } from "../src/resolution/resolution-service.ts";
+import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
+import { createMemoryRunStore } from "../src/runs/memory-run-store.ts";
+import { createLocalWorkspaceStore } from "../src/workspace/workspace-store.ts";
+import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
+import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
+import { createMemoryService } from "../src/memory/memory-service.ts";
+import { createModelGateway } from "../src/model/model-gateway.ts";
+import { createAuditLog } from "../src/audit/audit-log.ts";
+import { createRateLimiter } from "../src/ratelimit/rate-limiter.ts";
+import { defineHarness } from "../src/harness/harness.ts";
+import { harnessToolOptions } from "../src/harness/harness-shared.ts";
+import { createAgentTools, type AgentToolsOptions } from "../src/harness/agent-tools.ts";
+import { createDeployStore } from "../src/deploy/deploy-store.ts";
+import { createDockerDeployProvider } from "../src/deploy/docker-deploy-provider.ts";
+import { createDeployService } from "../src/deploy/deploy-service.ts";
+import { createDeliveryStore } from "../src/delivery/delivery-store.ts";
+import { createMemoryMap } from "../src/persistence/durable-map.ts";
+import { createFeatureFlagStore, type FeatureName } from "../src/feature-flags.ts";
+import { createSessionMailbox, type SessionMessage } from "../src/sessions/session-mailbox.ts";
+import { createSessionSyscalls } from "../src/sessions/session-syscalls.ts";
+import { createMemoryRunSignalStore } from "../src/runs/run-signal-store.ts";
+import { scopeId, type Principal, type Session } from "../src/types.ts";
+import type { Sandbox } from "../src/sandbox/sandbox.ts";
+import type { Run } from "../src/runs/run-store.ts";
+
+type Call = { tool: string; args: Record<string, unknown> };
+type Turn = {
+ ref: Parameters<typeof createAgentTools>[0];
+ options: AgentToolsOptions;
+ tools: string[];
+ results: Array<{ isError?: unknown }>;
+ started: Session[];
+};
+
+const actor: Principal = { id: "U1", type: "internal" };
+const scope = scopeId("personal", "U1");
+const SUBAGENT_EXCEPTIONS = {
+ surfaceTools: "a subagent's result returns to its parent, which owns posting to the conversation",
+ delegateWork: "a subagent is the delegate, so it does the work instead of delegating again",
+} satisfies Partial<Record<keyof AgentToolsOptions, string>>;
+const exceptions = Object.keys(SUBAGENT_EXCEPTIONS) as (keyof AgentToolsOptions)[];
+
+async function world(flags: FeatureName[], next: (turn: Turn, subagent: boolean) => Call | undefined) {
+ const sessions = createMemorySessionStore();
+ const { runs } = createMemoryRunStore();
+ const featureFlags = createFeatureFlagStore(createMemoryMap());
+ for (const flag of flags) await featureFlags.setEnabled(flag, scope, true, "test");
+ const sidebar: Session[] = [];
+ const sessionSyscalls = createSessionSyscalls({
+ sessions,
+ runs,
+ mailbox: createSessionMailbox(createMemoryMap<SessionMessage>()),
+ signals: createMemoryRunSignalStore(),
+ maxAttempts: 1,
+ conversations: {
+ list: async () => sidebar,
+ start: async (_actorId, input) => {
+ const session = await sessions.getOrCreateByThread(`web:U1:started-${sidebar.length}`, "dm", input.scopeId);
+ await sessions.addParticipant(session.id, actor.id);
+ await sessions.setSpawnMeta(session.id, {
+ surface: "web",
+ actor,
+ conversation: { kind: "dm", threadRef: session.threadRef, audience: [actor] },
+ });
+ sidebar.push(session);
+ return { session };
+ },
+ },
+ });
+ const turns = new Map<string, Turn>();
+ const harness = defineHarness(
+ {
+ id: "pi",
+ controlTransport: "in-process",
+ toolTransport: "in-process",
+ transcriptFormat: "pi",
+ capabilities: new Set(),
+ },
+ {
+ async runTurn(turn) {
+ const results: Turn["results"] = [];
+ const ref = {
+ current: turn.tools,
+ scopeLabel: turn.scopeLabel,
+ emit: async (entry: Parameters<typeof turn.emit>[0]) => {
+ if (entry.type === "tool_result") results.push(entry.payload as Turn["results"][number]);
+ return turn.emit(entry);
+ },
+ screenToolResult: turn.screenToolResult,
+ pendingApprovals: [],
+ };
+ const options = harnessToolOptions({}, turn);
+ const tools = createAgentTools(ref, options);
+ const record: Turn = { ref, options, tools: tools.map((t) => t.name).sort(), results, started: sidebar };
+ turns.set(turn.session.threadRef, record);
+ for (
+ let call = next(record, !!turn.session.parentSessionId);
+ call;
+ call = next(record, !!turn.session.parentSessionId)
+ ) {
+ const execute = tools.find((t) => t.name === call.tool)!.execute as unknown as (
+ id: string,
+ input: unknown,
+ ) => Promise<unknown>;
+ await execute(`call-${results.length}`, call.args);
+ }
+ return { reply: "done", modelCalls: 1 };
+ },
+ },
+ );
+ const acl = createAclStore();
+ const auditLog = createAuditLog();
+ const workspace = createLocalWorkspaceStore(mkdtempSync(join(tmpdir(), "subagent-parity-")));
+ const orchestrator = createOrchestrator({
+ identity: createIdentityService(),
+ resolution: createResolutionService("org", createMemoryConfigStore("org"), acl),
+ sessions,
+ runs,
+ workspace,
+ files: createMemoryFileArtifactStore(createMemoryDurableByteStore()),
+ sandbox: { profile: { backend: "fake", writablePersistence: "snapshot_to_workspace" } } as Sandbox,
+ modelGateway: createModelGateway(),
+ auditLog,
+ rateLimiter: createRateLimiter({ maxPerWindow: 100, windowMs: 60_000 }),
+ harness,
+ memory: createMemoryService(workspace),
+ deploy: createDeployService({
+ deployStore: createDeployStore(),
+ provider: createDockerDeployProvider(),
+ deployDir: join(tmpdir(), "subagent-parity-deploy"),
+ auditLog,
+ acl,
+ }),
+ acl,
+ deliveries: createDeliveryStore(),
+ featureFlags,
+ sessionSyscalls,
+ });
+ const execute = async (run: Run) => {
+ const result = await orchestrator.handleTurn({ ...run.request, runId: run.id });
+ assert.equal(result.status, "ok", result.reply);
+ };
+ const parentTurn = async (surface: string, threadRef: string) => {
+ const conversation = { kind: "dm" as const, threadRef, audience: [actor] };
+ const request: OrchestratorInput = { surface, actor, conversation, origin: { kind: "human" }, text: "go" };
+ await execute((await runs.enqueue({ sessionId: threadRef, request, maxAttempts: 1 })).run);
+ const parent = (await sessions.getByThread(threadRef))!;
+ const [child] = await sessions.childrenOf(parent.id);
+ assert.ok(child, "the parent opened a subagent");
+ await execute((await runs.latestForThread(child.threadRef))!);
+ return { parent: turns.get(threadRef)!, child: turns.get(child.threadRef)! };
+ };
+ return { parentTurn };
+}
+
+const failed = (result: Turn["results"][number]) => result.isError === true;
+
+function script(turn: Turn, subagent: boolean): Call | undefined {
+ const calls: Call[] = [
+ ...(turn.tools.includes("sessions")
+ ? [
+ { tool: "sessions", args: { action: "list" } },
+ { tool: "sessions", args: { action: "new", text: "draft the memo", title: "Memo" } },
+ {
+ tool: "sessions",
+ args: { action: "send_message", target: turn.started.at(-1)?.id, text: "add a summary" },
+ },
+ ]
+ : []),
+ ...(subagent ? [] : [{ tool: "subagents", args: { action: "open", task: "repeat what your parent did" } }]),
+ ];
+ return calls[turn.results.length];
+}
+
+test("the sessions tool lists, starts and messages sessions through the real tool context", async () => {
+ const w = await world(["persistent_subagents"], script);
+ const { parent } = await w.parentTurn("web", "web:U1:sessions");
+ assert.equal(parent.results.length, 4);
+ assert.deepEqual(parent.results.map(failed), [false, false, false, false], JSON.stringify(parent.results));
+});
+
+for (const [surface, flags] of [
+ ["web", ["persistent_subagents"]],
+ ["slack", ["responsive_spine"]],
+] as const) {
+ test(`a ${surface} subagent can do what its parent could, except SUBAGENT_EXCEPTIONS`, async () => {
+ const w = await world([...flags], script);
+ const { parent, child } = await w.parentTurn(surface, `${surface}:U1:parity`);
+ const omit = (options: AgentToolsOptions) =>
+ Object.fromEntries(Object.entries(options).filter(([key]) => !(key in SUBAGENT_EXCEPTIONS)));
+ assert.deepEqual(omit(child.options), omit(parent.options));
+ const asParent = { ...child.options, ...Object.fromEntries(exceptions.map((key) => [key, parent.options[key]])) };
+ assert.deepEqual(
+ createAgentTools(child.ref, asParent)
+ .map((t) => t.name)
+ .sort(),
+ parent.tools,
+ );
+ assert.deepEqual(
+ child.results.map(failed),
+ parent.results.slice(0, child.results.length).map(failed),
+ JSON.stringify(child.results),
+ );
+ });
+}