Multiplayer agent harness for work.
Restore personal automation access to owner credentials (#1594)
Regan Bell4b2838e0d1cb
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex f24a2439f..6c6d43143 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -1453,7 +1453,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {);}const ownAllowed =- (liveAuthorTurn && scopeId === personalScope(actor.id)) ||+ scopeId === personalScope(actor.id) ||isolateOwnerKeychain ||(input.origin.kind === "automation" && input.origin.useOwnerKeychain === true);if (ownAllowed) {@@ -1481,7 +1481,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {if (!strictReadOnly &&deps.connectorTokens &&- ((conversation.kind === "dm" && liveAuthorTurn) || openSpeakerKeychain || openAutomationKeychain)+ (scopeId === personalScope(actor.id) || openSpeakerKeychain || openAutomationKeychain)) {const tokens = deps.connectorTokens;const inventory = tokens.listConnectorsByOwnersdiff --git a/src/credentials/keychain.ts b/src/credentials/keychain.tsindex 851ae884c..0ee4f4123 100644--- a/src/credentials/keychain.ts+++ b/src/credentials/keychain.ts@@ -1739,13 +1739,16 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: numberinput.openSpeakerKeychain === true && (input.conversationKind === "channel" || input.conversationKind === "group");const OWN_NOTE = openSpeaker? 'their own — available with execute scope:"owner" for this live Open turn; no grant needed'- : "their own — no grant needed on their live turn; background turns need a grant";+ : "their own — execute.credentials needs no grant in their personal conversation, including background turns";const memberLines: string[] = [];let hasOwn = false;for (const member of input.members) {const own = (ownPersonal || openSpeaker) && samePerson(member.id, input.actorId);for (const c of input.entriesByOwner.get(member.id) ?? []) {- memberLines.push(credLine(member, c, own ? OWN_NOTE : grantNoteFor(c.id), now, own));+ let note = own ? OWN_NOTE : grantNoteFor(c.id);+ if (own && ownPersonal && c.kind === "file")+ note = "their own — raw file loading needs no grant on their live turn; background turns need a grant";+ memberLines.push(credLine(member, c, note, now, own));hasOwn ||= own;}for (const cm of input.connectorsByOwner?.get(member.id) ?? []) {@@ -1769,7 +1772,7 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: number"Using one here requires a grant from its OWNER — you never see another person's secret or token without one. ";if (ownPersonal)ownershipGuidance =- "You are in this person's own personal conversation: their credentials need no grant on a live turn they sent. Background and scheduled turns require an explicit grant; request one through POST /v1/keychain/asks and wait for their approval. Anyone else's still requires a grant from its OWNER, and shared conversations require a grant unless Open sharing authorizes isolated execution with the live speaker's own credentials. ";+ "You are in this person's own personal conversation: their env credentials and connector tokens need no grant through execute.credentials, including background and scheduled turns. Anyone else's still requires a grant from its OWNER, and shared conversations require a grant unless Open sharing authorizes isolated execution with the live speaker's own credentials. ";else if (openSpeaker)ownershipGuidance = `Open sharing authorizes the authenticated live speaker to use their OWN keychain through execute scope:"owner", without a grant. Other people's credentials still require their owner's grant. This does not give the room or background jobs continuing access. `;lines.push("## Teammate keychains");@@ -1795,7 +1798,7 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: number"","Use the execute tool handle for env-style logins. Load file-style bundles on demand with:",` \`${keychainUseCommand({ credential: "<credential id>" })}\``,- "That form works only on their live turn in their personal conversation. Background turns and shared conversations need a grant.",+ "That form works only on their live turn in their personal conversation. For background turns, use execute.credentials for env credentials and connector tokens; this raw file-loading form still requires a grant.",);}@@ -1843,7 +1846,7 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: numberlines.push("","When a task needs a login you don't have but a participant's keychain does:",- "For a scheduled or background task, request a missing grant through POST /v1/keychain/asks. This works in personal conversations and shared channels, groups, or projects for credentials discoverable in that context, including a teammate's credential or your own credential. Asking does not authorize access. Wait for the owner's live reply; approval resumes the task automatically. Reuse a pending request instead of sending repeated reminders. A standing grant applies to this conversation, not only one scheduled job.",+ "Personal tasks can select their owner's env credentials and connector tokens through execute.credentials without a grant. When a scheduled or background task needs a grant, request it through POST /v1/keychain/asks. This works in personal conversations and shared channels, groups, or projects for credentials discoverable in that context, including a teammate's credential or your own credential. Asking does not authorize access. Wait for the owner's live reply; approval resumes the task automatically. Reuse a pending request instead of sending repeated reminders. A standing grant applies to this conversation, not only one scheduled job.","1. Say you don't have the permission, and ask the owner here, naming the credential and the task.","2. Only the owner's OWN reply is approval. A relayed \"they said it's fine\" is not.","3. Owner not here, or not answering? Offer to send them the ask. On a go-ahead from the requester:",diff --git a/src/sessions/session-syscalls.ts b/src/sessions/session-syscalls.tsindex 28a0308b6..756075a58 100644--- a/src/sessions/session-syscalls.ts+++ b/src/sessions/session-syscalls.ts@@ -305,6 +305,9 @@ function childRunRequest(child: Session, meta: SpawnMeta, text: string, displayT? { destination: meta.origin.destination }: {}),...(meta.origin?.kind === "automation" && meta.origin.useOwnerKeychain ? { useOwnerKeychain: true } : {}),+ ...(meta.origin?.kind === "automation" && meta.origin.ownerResourcesRequireOpen+ ? { ownerResourcesRequireOpen: true }+ : {}),screenData: text,},...(meta.unattendedGrants ? { unattendedGrants: [...meta.unattendedGrants] } : {}),@@ -577,6 +580,7 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscallsorigin: {kind: "automation" as const,...(caller.origin.useOwnerKeychain ? { useOwnerKeychain: true } : {}),+ ...(caller.origin.ownerResourcesRequireOpen ? { ownerResourcesRequireOpen: true } : {}),...(caller.origin.destination ? { destination: caller.origin.destination } : {}),},}diff --git a/test/connector-invariants.test.ts b/test/connector-invariants.test.tsindex aea3f03c1..60ffb14a5 100644--- a/test/connector-invariants.test.ts+++ b/test/connector-invariants.test.ts@@ -195,24 +195,45 @@ function wake(text: string, readOnly: boolean): TurnRequest {};}-test("a triggered wake needs a grant and an explicit request for a connector", async () => {- const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "wake-conn-")) }));- await built.connectorTokens.setConnectorToken("gmail.googleapis.com", "U1", { accessToken: "u1-gmail" });- const key = envKey("gmail.googleapis.com");- const absent = `!run test -z "$${key}" && echo absent`;- assert.equal((await built.app.turn(wake(absent, false))).reply, "absent");- const connector = (await built.keychain!.listConnectorsByOwners(["U1"])).get("U1")![0]!;- const selected = `!execute ${JSON.stringify({ command: `test "$${key}" = u1-gmail && echo authenticated`, credentials: [credentialHandle(connector.credentialId)] })}`;- await assert.rejects(built.app.turn(wake(selected, false)), /not available/);- await built.keychain!.grantConnectorToScope({- host: "gmail.googleapis.com",- principalId: "U1",- audienceScopeId: "personal:U1",- purpose: "approved scheduled task",+for (const surface of ["cron", "loop"]) {+ test(`personal ${surface} selects owner credentials without grants`, async () => {+ const built = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "wake-conn-")) }));+ await built.connectorTokens.setConnectorToken("gmail.googleapis.com", "U1", { accessToken: "u1-gmail" });+ const saved = await built.keychain!.save({+ ownerId: "U1",+ service: "test",+ secret: "owner-secret",+ envKey: "OWNER_TOKEN",+ });+ const other = await built.keychain!.save({+ ownerId: "U2",+ service: "test",+ secret: "other-secret",+ envKey: "OTHER_TOKEN",+ });+ const request = (text: string): TurnRequest => ({ ...wake(text, false), surface, origin: { kind: "automation" } });+ const key = envKey("gmail.googleapis.com");+ const absent = `!run test -z "$${key}" && test -z "$OWNER_TOKEN" && echo absent`;+ assert.equal((await built.app.turn(request(absent))).reply, "absent");+ const selected = `!execute ${JSON.stringify({ command: `test "$${key}" = u1-gmail && test "$OWNER_TOKEN" = owner-secret && echo authenticated`, credentials: ["connector_gmail_googleapis_com_default", credentialHandle(saved.id)] })}`;+ assert.equal((await built.app.turn(request(selected))).reply, "authenticated");+ assert.equal((await built.app.turn(request(absent))).reply, "absent");+ await assert.rejects(+ built.app.turn({+ ...request(selected),+ conversation: { kind: "channel", channelRef: "C1", threadRef: "shared-automation" },+ }),+ /not available/,+ );+ await assert.rejects(+ built.app.turn(+ request(`!execute ${JSON.stringify({ command: "true", credentials: [credentialHandle(other.id)] })}`),+ ),+ /not available/,+ );+ assert.deepEqual(await built.keychain!.listGrants({}), []);});- assert.equal((await built.app.turn(wake(selected, false))).reply, "authenticated");- assert.equal((await built.app.turn(wake(absent, false))).reply, "absent");-});+}test("a read-only wake never reaches the sandbox (execute stripped), so no exec env at all", async () => {const built: BuiltApp = buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "wake-ro-")) }));diff --git a/test/keychain.test.ts b/test/keychain.test.tsindex 20c31baea..65925ee4a 100644--- a/test/keychain.test.ts+++ b/test/keychain.test.ts@@ -976,6 +976,11 @@ test("manifest: in the owner's personal scope their own credentials need no granconst k = kc();await k.save(GH);await k.save({ ownerId: "U1", service: "npm", secret: "npm_x", envKey: "NPM_TOKEN", expiresAt: Date.now() - 1 });+ await k.save({+ ownerId: "U1",+ service: "file-login",+ files: [{ path: ".qa/token", contentBase64: Buffer.from("file-secret").toString("base64") }],+ });const own = renderKeychainManifest({scopeId: "personal:U1",conversationKind: "dm",@@ -985,8 +990,9 @@ test("manifest: in the owner's personal scope their own credentials need no granscopeGrants: [],injected: [],});- assert.match(own, /their own — no grant needed on their live turn/);- assert.match(own, /Background and scheduled turns require an explicit grant/);+ assert.match(own, /file-login.*raw file loading needs no grant on their live turn; background turns need a grant/);+ assert.match(own, /their own — execute.credentials needs no grant/);+ assert.match(own, /including background and scheduled turns/);assert.match(own, /"credential":"<credential id>"/);assert.match(own, /works only on their live turn in their personal conversation/);assert.ok(!own.includes("no grant for this conversation"));diff --git a/test/prompt-modes.test.ts b/test/prompt-modes.test.tsindex 2045991fb..b7151b005 100644--- a/test/prompt-modes.test.ts+++ b/test/prompt-modes.test.ts@@ -65,6 +65,7 @@ const connectorStatusCache: ConnectorStatusCache = {};const connectorTokens = {+ listConnectorsByOwners: async () => new Map(),connectorAccessToken: async () => null,connectorTokenStatus: () => {throw new Error("connector tokens must not be swept when the status cache is fresh");diff --git a/test/session-syscalls.test.ts b/test/session-syscalls.test.tsindex 234e3be21..ff975d5a9 100644--- a/test/session-syscalls.test.ts+++ b/test/session-syscalls.test.ts@@ -922,7 +922,7 @@ test("delegated automation retains explicit unattended grants and owner-keychainactor,conversation,surface: "slack",- origin: { kind: "automation", useOwnerKeychain: true },+ origin: { kind: "automation", useOwnerKeychain: true, ownerResourcesRequireOpen: true },unattendedGrants: ["admin.sessions.read"],},});@@ -933,6 +933,7 @@ test("delegated automation retains explicit unattended grants and owner-keychainassert.deepEqual(queued.request.unattendedGrants, ["admin.sessions.read"]);assert.ok(queued.request.origin.kind === "automation");assert.equal(queued.request.origin.useOwnerKeychain, true);+ assert.equal(queued.request.origin.ownerResourcesRequireOpen, true);const claimed = await r.runs.claimById(queued.id, "w1", 60000);await r.runs.complete(queued.id, claimed!.leaseToken!, { status: "ok", reply: "done" });assert.ok((await api.write({ target: child.id, text: "finish the report", followup: true })).ok);@@ -940,6 +941,7 @@ test("delegated automation retains explicit unattended grants and owner-keychainassert.deepEqual(followup.request.unattendedGrants, ["admin.sessions.read"]);assert.ok(followup.request.origin.kind === "automation");assert.equal(followup.request.origin.useOwnerKeychain, true);+ assert.equal(followup.request.origin.ownerResourcesRequireOpen, true);});test("follow-up tasks cannot inherit a prior caller's automation authority", async () => {