Multiplayer agent harness for work.
Clarify credential approval session and requested access (#1535)
Regan Bell2aeabf5e3061
diff --git a/src/credentials/keychain-approval.ts b/src/credentials/keychain-approval.tsindex e00848188..3b75000da 100644--- a/src/credentials/keychain-approval.ts+++ b/src/credentials/keychain-approval.ts@@ -60,7 +60,7 @@ export function createKeychainApprovals(deps: {ask,service: credential.service,...(credential.accountLabel ? { accountLabel: credential.accountLabel } : {}),- conversation: context?.name || "the requesting conversation",+ conversation: visible?.title || context?.name || "the requesting conversation",...(visible ? { sessionId: visible.id } : {}),...(ask.requesterSeq !== undefined && visible?.threadRef === ask.requesterThreadRef? { seq: ask.requesterSeq }diff --git a/src/credentials/keychain.ts b/src/credentials/keychain.tsindex 4e8254c47..a31618543 100644--- a/src/credentials/keychain.ts+++ b/src/credentials/keychain.ts@@ -1122,7 +1122,8 @@ export function createKeychain(deps: {async createAsk(input) {const purpose = input.purpose.trim();- if (!purpose) throw new KeychainError(400, "purpose required — record the requester's words verbatim");+ if (!purpose)+ throw new KeychainError(400, "purpose required — describe the specific task requiring this credential");const cred = await deps.creds.get(input.credentialId);if (!cred || cred.kind === "broker") throw new KeychainError(404, "unknown credential");const t = now();@@ -1777,7 +1778,7 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: number"3. Owner not here, or not answering? Offer to send them the ask. On a go-ahead from the requester:",' `curl -fsS -X POST "$AGENT_API_URL/v1/keychain/asks" ' +CAPABILITY_CURL_AUTH +- ' -H \'content-type: application/json\' -d \'{"credential":"<credential id>","purpose":"<the requester\'s words, verbatim>"}\'` (`"requestedMode":"standing"` only if they asked for that).',+ ' -H \'content-type: application/json\' -d \'{"credential":"<credential id>","purpose":"<specific task requiring this credential>"}\'` Set `requestedMode` to `"once"` for one credential use or `"standing"` when requesting recurring or repeated background use. Describe what this credential will do and why it is needed; do not just repeat the user\'s broad request. This proposes access for the owner to approve; it does not authorize it.'," Core DMs the owner a notice composed from the record, and wakes THIS conversation the moment they answer (or the ask expires, 24h). You may set yourself a one-shot follow-up cron as a timeout check. A relayed approval never mints anything — explain that and send a real ask instead.","4. On the turn where the owner speaks their approval (core verifies the speaker IS the owner), record it:",' `curl -fsS -X POST "$AGENT_API_URL/v1/keychain/grants" ' +diff --git a/src/slack/keychain-approvals.ts b/src/slack/keychain-approvals.tsindex b253f8354..d0a0494b2 100644--- a/src/slack/keychain-approvals.ts+++ b/src/slack/keychain-approvals.ts@@ -4,6 +4,7 @@ import type { Directory } from "./directory.ts";import { parseBlockAction, parseInteractionBody } from "./payloads.ts";import { updateSlackMessage } from "./messaging.ts";import { errMessage, swallowAs } from "../util/errors.ts";+import { parseScopeId } from "../types.ts";const ACTIONS = ["keychain_allow_once", "keychain_allow_always", "keychain_deny"] as const;const escape = (text: string): string => text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");@@ -18,14 +19,20 @@ export function keychainApprovalMessage(originUrl && /^https?:\/\//.test(originUrl)? `<${originUrl.replaceAll("|", "%7C").replaceAll(">", "%3E")}|${place}>`: place;- const summary = `Use your *${escape(view.service.slice(0, 150))}* credential ${origin}.`;const pending = ask.status === "pending";+ const { kind } = parseScopeId(ask.requesterScopeId);+ let audience = "this group";+ if (kind === "personal") audience = "your personal conversations";+ if (kind === "channel") audience = "this channel";+ const standing = `ongoing access across ${audience}`;+ const duration =+ pending && ask.requestedMode ? ` (${ask.requestedMode === "standing" ? standing : "one-time access"})` : "";+ const summary = `Use your *${escape(view.service.slice(0, 150))}* credential ${origin}${duration}.`;let status = "This request expired.";- if (ask.status === "approved")- status = `Approved — ${view.mode === "standing" ? "ongoing access for this conversation" : "one-time access"}.`;+ if (ask.status === "approved") status = `Approved — ${view.mode === "standing" ? standing : "one-time access"}.`;if (ask.status === "declined") status = "Denied.";const text = pending- ? `Approval needed: use your ${view.service} credential in ${view.conversation}. ${ask.purpose}`+ ? `Approval needed: use your ${view.service} credential in ${view.conversation}${duration}. ${ask.purpose}`: status;const blocks: Array<Record<string, unknown>> = [{@@ -37,15 +44,6 @@ export function keychainApprovalMessage(},];if (pending) {- blocks.push({- type: "context",- elements: [- {- type: "plain_text",- text: "Allow once permits one credential use. Allow always permits future use in this conversation until revoked.",- },- ],- });blocks.push({type: "actions",block_id: `keychain_ask:${ask.id}`,diff --git a/test/keychain-approval-card.test.ts b/test/keychain-approval-card.test.tsindex 8f27f08e2..e5b72935b 100644--- a/test/keychain-approval-card.test.ts+++ b/test/keychain-approval-card.test.ts@@ -61,6 +61,7 @@ async function fixture() {asks,ask,session,+ sessions,approvals,resumed,revokeMembership: () => {@@ -129,9 +130,20 @@ test("the card links the conversation inline and omits command-policy fields", aconst wire = JSON.stringify(card);assert.match(wire, /\|in the Reports conversation>/);assert.doesNotMatch(wire, /Command:|Flagged as:|Allow session|fixture-secret/);+ assert.match(wire, /ongoing access across your personal conversations/);+ for (const [scope, audience] of [+ ["channel:C1", "this channel"],+ ["group:project", "this group"],+ ] as const) {+ const scoped = keychainApprovalMessage({ ...view, ask: { ...view.ask, requesterScopeId: scope } }, origin);+ assert.ok(JSON.stringify(scoped).includes(`ongoing access across ${audience}`));+ }+ const once = keychainApprovalMessage({ ...view, ask: { ...view.ask, requestedMode: "once" } }, origin);+ assert.match(JSON.stringify(once), /one-time access/);assert.match(wire, /Allow once/);assert.match(wire, /Allow always/);assert.match(wire, /Deny/);+ assert.ok(card.blocks.every((block) => block.type !== "context"));const settled = keychainApprovalMessage({ ...view, ask: { ...view.ask, status: "approved" }, mode: "standing" },origin,@@ -245,3 +257,16 @@ test("an expired notification cannot hide an approval recovered after a failed wassert.equal((await f.keychain.unnotifiedResolvedAsks(Date.now())).length, 0);}});++test("approval labels use the owner's session title without exposing inaccessible titles", async () => {+ const f = await fixture();+ await f.sessions.updateTitle(f.session.id, "Nightly report");+ await f.sessions.updateParticipantView(f.session.id, "[email protected]", { title: "My nightly report" });+ const view = (await f.approvals.get(f.ask.id, "[email protected]"))!;+ assert.equal(view.conversation, "My nightly report");+ assert.match(JSON.stringify(keychainApprovalMessage(view, "https://qm.example/s/test")), /\|in My nightly report>/);+ f.sessions.getForParticipant = async () => null;+ const hidden = (await f.approvals.get(f.ask.id, "[email protected]"))!;+ assert.equal(hidden.conversation, "the Reports conversation");+ assert.equal(hidden.sessionId, undefined);+});