Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Repair device login capture and explicit credential writeback (#2146)

Regan Bellfaee436450ac
diff --git a/cli/src/tool-descriptor.ts b/cli/src/tool-descriptor.ts
index 596d0ec5c..46becf0ad 100644
--- a/cli/src/tool-descriptor.ts
+++ b/cli/src/tool-descriptor.ts
@@ -94,7 +94,7 @@ const BUILT_IN_CREDENTIAL_PATHS: readonly ToolCredentialPath[] = builtInCredenti
const nested = (a: string, b: string): boolean => a.startsWith(`${b}/`);
-export function credentialPathError(path: string, kind: "file" | "directory"): string | undefined {
+function credentialPathError(path: string, kind: "file" | "directory"): string | undefined {
const segments = path.split("/");
if (
!path ||
diff --git a/docs/credential-refresh-handoff.md b/docs/credential-refresh-handoff.md
new file mode 100644
index 000000000..d7777aa0b
--- /dev/null
+++ b/docs/credential-refresh-handoff.md
@@ -0,0 +1,46 @@
+# Credential login and refresh
+
+## Design
+
+Logging in is a skill the agent follows (`skills-seed/interactive-login`):
+
+1. The agent runs the native login as an ordinary background job with `HOME` and the XDG
+ directories pointed at a fresh `mktemp -d` directory and relays the verification URL and
+ code.
+2. After the job exits, `scripts/save-login.mjs <service> <dir>` uploads only the files under
+ that directory through `POST /v1/keychain/credentials` and deletes the directory. Secrets
+ never enter model context. The keychain limits a login to 500 files and 8 MiB.
+3. Later commands get the login only by naming its handle in `execute.credentials`.
+
+Re-saving a service replaces the entry in place. The credential id is derived from owner,
+service and slot, so the handle, owner and grants are unchanged; `createdAt` is kept, while
+files, origin, label and expiry come from the new save.
+
+## Execution
+
+`src/credentials/execute-files.ts` stages requested bundles in a private
+`/tmp/qm-credentials.<random>/home`, points the CLI at it, captures changed files after the
+command and writes them back with `keychain.updateFiles`, which rejects stale writes. Only
+the owner's executions write back; a grant uses the saved files as they are. Each execution
+removes its own directory in `finally`.
+
+Background jobs accept the same `credentials` handles. The staging plan, credential ids,
+grant ids and baseline fingerprints (no secrets) are stored on the job's `process_sessions`
+row (`credential_files`). Any path that observes the exit (background poll/stop, monitor,
+reaper, reconcile on provision) re-checks the baseline against the keychain, writes
+refreshes back, clears the column and removes the directory. If a write fails for a reason
+other than a keychain rejection, the column and directory stay so a later observer or the
+next provision retries. A start that fails after the process launched leaves cleanup to
+these paths. The start call masks the job's env credential values in its initial output;
+later polls are unmasked because no secret values are stored.
+
+Directories older than two hours are swept when the next credentialed execution starts,
+which is longer than any execution or background job may live, so concurrent operations never
+remove each other's files.
+
+## Known gaps
+
+- The staging directory is protected by mode 0700 and a random name, not by a separate user.
+ Another process running as the same user during the command can read it.
+- No proactive refresh for idle AWS sessions; the CLI refreshes only when a command runs.
+- Refreshes made through a grant are not saved.
diff --git a/docs/skill-registry.md b/docs/skill-registry.md
index 0fd86f72a..35e501a4b 100644
--- a/docs/skill-registry.md
+++ b/docs/skill-registry.md
@@ -78,10 +78,11 @@ visible, screened published skill and returns its SKILL.md, or a relative manife
named by `path`, without creating or waking a sandbox. Unavailable skills and invalid
paths return no file.
-When a skill ships supporting files or pack bundles, the same call lays them into the
-turn's private directory under `.agent-turn/<conversation>/skills/<name>/` (packs under
-`.packs/<id>/` beside it) and reports that path; the body's own `skills/<name>/` references
-are rewritten to it. That directory is wiped when the turn ends and before the next one
-starts, so nothing reconciles, locks, or sweeps: plain commands and filesystem reads never
-wait on skill synchronization, and revoked or archived content cannot outlive the turn
-that loaded it. Publish changes through the skill API to update the source.
+When a skill ships supporting files or pack bundles, the same call writes them to
+`.agent-skills/<conversation>/<name>/` on the computer (packs under
+`.agent-skills/<conversation>/.packs/<id>/`) and reports that absolute path; the body's own
+`skills/<name>/` references are rewritten to it. The copy stays across turns and each read
+overwrites it with the published version. When a turn first prepares the computer, it
+removes every copy of a skill that turn's speaker cannot see, so archived, revoked, blocked,
+or another person's skills do not linger on a shared computer. Publish changes through the
+skill API to update the source.
diff --git a/skills-seed/browse/SKILL.md b/skills-seed/browse/SKILL.md
index 4e559db35..191ea3f5c 100644
--- a/skills-seed/browse/SKILL.md
+++ b/skills-seed/browse/SKILL.md
@@ -73,51 +73,42 @@ becoming the default):
nothing at another — never register one provider's profile under another's env key.
**Check your environment first**: on most deployments the org configures the browser provider key,
-so that key is already set in your shell — skip
-straight to the profile check below. Only when one is absent do the keys go through the
-keychain: materialize your grant (your keychain manifest shows the grant id), then source it:
+so that key is already set in every shell — skip straight to the profile check below. A
+person's own provider key and their profile name never arrive in your env by default: find
+their handles in your keychain manifest and name them in `credentials` on every `execute`
+and background start that needs them. Never echo the values.
-```bash
-curl -fsS -X POST "$AGENT_API_URL/v1/keychain/use" -H "x-agent-capability: $AGENT_API_TOKEN" \
- -H 'content-type: application/json' -d '{"grant":"<grantId>"}' -o /tmp/keychain.env && . /tmp/keychain.env
-```
-
-**If the browser provider key is still missing after sourcing, don't
-dead-end on "no grant"** — the keychain has a path for every case, and a person-typed turn
-can use all of them (asks and drops are refused only on trigger-fired turns). Never echo the
-values.
+**If the browser provider key is in neither your env nor your handles, don't dead-end on
+"no grant"** — the keychain has a path for every case, and a person-typed turn can use all
+of them (asks and drops are refused only on trigger-fired turns).
1. See whether the credential is already registered to a participant:
```bash
curl -fsS "$AGENT_API_URL/v1/keychain/credentials" -H "x-agent-capability: $AGENT_API_TOKEN"
```
-2. **Registered to the person themselves** → in their DM, load it directly with
- `POST /v1/keychain/use` and `{"credential":"<id>"}`, then re-source. Anywhere else, send an ask
- (step 3); they approve it on the card.
+2. **Registered to the person themselves** → in their DM, its handle is already usable in
+ `credentials`. Anywhere else, send an ask (step 3); they approve it on the card.
3. **Registered to someone else** (the provider key usually lives with whoever set it up) →
send an ask: `POST /v1/keychain/asks` with the credential id and the person's words as
the purpose. Core DMs the owner and wakes this conversation when they answer; tell the
- person whose approval you're waiting on.
+ person whose approval you're waiting on. The approved grant's handle then works in
+ `credentials`.
4. **Not registered anywhere** → the person can supply their own keys on the spot: mint a
drop link per key (`POST /v1/keychain/drops` with
`{"service":"<your provider doc's keychain service>","envKey":"<the provider key name>","purpose":"browse"}`)
and hand the link over — the secret lands in
their keychain, never in chat.
-**If the profile env key is missing from your env, do NOT jump to bootstrapping** — a
+**If the profile has no handle in your manifest, do NOT jump to bootstrapping** — a
duplicate profile silently orphans every sign-in saved in the real one. First check whether
the credential already exists:
```bash
curl -fsS "$AGENT_API_URL/v1/keychain/credentials" -H "x-agent-capability: $AGENT_API_TOKEN" \
- | python3 -c "import sys,json;print(json.dumps([c['id'] for c in json.load(sys.stdin)['credentials'] if c.get('envKey')==sys.argv[1]]))" "$PROFILE_ENV"
+ | python3 -c "import sys,json;print(json.dumps([c.get('credentialHandle') for c in json.load(sys.stdin)['credentials'] if c.get('envKey')==sys.argv[1]]))" "$PROFILE_ENV"
```
-- **Credential exists but wasn't in your sourced env** → in their DM, load it with
- `POST /v1/keychain/use` and `{"credential":"<id>"}` to a NEW file and fold it in
- (`-o /tmp/keychain2.env && . /tmp/keychain2.env && cat /tmp/keychain2.env >> /tmp/keychain.env`)
- so a later background re-source of `/tmp/keychain.env` still has everything.
- Never bootstrap in this case.
+- **Credential exists** → use that handle in `credentials`. Never bootstrap in this case.
- **No credential at all** → first-time setup below, with the person's OK.
**First-time profile setup (only when the check above found no credential).** Mint the
@@ -208,12 +199,10 @@ PY
gets into later commands as data instead of being pasted into shell source.)
For a task likely to run past a couple of minutes, launch it with the `background` tool and
-poll its output instead of blocking `execute`. The background shell does NOT inherit your
-`execute` shell's env — prefix the background command with
-`[ -f /tmp/keychain.env ] && . /tmp/keychain.env;` so keychain-granted keys are re-sourced
-there (org-configured keys are already in every shell's env; a run without its keys fails
-every step with "Could not resolve authentication method", which looks exactly like the
-flaky-auth race but isn't).
+poll its output instead of blocking `execute`. Pass the same `credentials` handles to the
+background start: org-configured keys are already in every shell's env, but a run without
+its keychain keys fails every step with "Could not resolve authentication method", which
+looks exactly like the flaky-auth race but isn't.
Browser-use 0.12.9
has two known warts you'll see in logs and should read past: a flaky per-step client-auth race
diff --git a/skills-seed/browse/providers/anchor.md b/skills-seed/browse/providers/anchor.md
index 6ac681ef9..b52cac85f 100644
--- a/skills-seed/browse/providers/anchor.md
+++ b/skills-seed/browse/providers/anchor.md
@@ -114,8 +114,8 @@ curl -fsS -X POST "$AGENT_API_URL/v1/keychain/drops" -H "x-agent-capability: $AG
Hand over the returned `url` with the consent stated plainly: _filling it stores this login
with Anchor (our managed-browser provider) so it can sign in and re-sign-in for you_ — that
storage is the point, and it needs their explicit OK. Redemption fires a wake back into
-this conversation with `EMAIL`/`PASSWORD` in your env (sign-ins are DM-only, so no grant
-dance). Then create the identity with Python building the body from env
+this conversation; name the new credential's handle in `credentials` to get
+`EMAIL`/`PASSWORD` for that command (sign-ins are DM-only, so no grant dance). Then create the identity with Python building the body from env
(`POST /v1/identities` with `{"source":"https://<domain>","name":...,"credentials":
[{"type":"username_password","username":...,"password":...}]}`), and register the returned
identity id in their keychain (service `anchor-identity`, envKey
diff --git a/skills-seed/browse/providers/kernel.md b/skills-seed/browse/providers/kernel.md
index d8babba45..b99f4c630 100644
--- a/skills-seed/browse/providers/kernel.md
+++ b/skills-seed/browse/providers/kernel.md
@@ -89,8 +89,8 @@ can re-auth automatically when the session lapses (only an OTP still needs the p
login with Kernel (our managed-browser provider) so it can sign in and re-sign-in for
you_ — that storage is the point, and it needs their explicit OK. Redemption fires a
wake back into this conversation, and since sign-ins are DM-only the person's own
- credential injects straight into your env on that next turn (`EMAIL`/`PASSWORD` set —
- no grant dance needed).
+ credential is usable on that next turn by naming its handle in `credentials` (which sets
+ `EMAIL`/`PASSWORD` for that command — no grant dance needed).
2. **Store the credential at Kernel, then create the connection WITH it linked.** Linking
only works at connection create — if an unlinked connection for the domain already
exists (the create 409s with `existing_id`), DELETE it first and re-create. Names must
diff --git a/skills-seed/cloud-cli/SKILL.md b/skills-seed/cloud-cli/SKILL.md
index b31f7c729..634c702b9 100644
--- a/skills-seed/cloud-cli/SKILL.md
+++ b/skills-seed/cloud-cli/SKILL.md
@@ -1,14 +1,13 @@
---
name: cloud-cli
-description: Sign the agent computer in to a cloud provider's CLI (AWS, Google Cloud, Azure, or another) with a device-code flow, then run that CLI as the requesting user. Covers checking the CLI is installed, the login that survives the browser round-trip, and the boundaries on cloud writes.
+description: Sign a cloud provider's CLI in (AWS, Google Cloud, Azure, or another) with a device-code flow, then run that CLI as the requesting user. Covers checking the CLI is installed, the login that survives the browser round-trip, and the boundaries on cloud writes.
---
# Cloud provider CLIs
-A resident-machine-auth connector. It adds NO new tool: a provider's CLI is a command on
-the agent computer and you drive it with the `execute` primitive. Tokens are cached on the
-machine across turns. Logins never enter workspace backups; the platform keeps an encrypted
-copy and restores it if the machine is replaced — if the token is still gone, log in again.
+It adds NO new tool: a provider's CLI is a command on the agent computer and you drive it
+with the `execute` primitive. Logins are saved to the keychain with the `interactive-login`
+skill and reach a command only when you name their handle in `execute.credentials`.
## First: is the CLI even here?
@@ -57,16 +56,10 @@ For AWS specifically, `--use-device-code` is mandatory rather than merely prefer
(≥ 2.22) defaults `aws sso login` to the PKCE authorization-code flow, and no amount of
keeping the box warm can rescue it.
-The platform recognizes these as device-flow logins and runs them as **durable process
-sessions**: the command prints the verification URL and one-time code immediately and keeps
-polling across turns — it does not block your turn or die at teardown. Give the user the URL
-and the code, tell them to approve and then say "done". See
-the `interactive-login` skill for how to drive that from the background tool.
-
-On the next turn, run the same login command again (or any provider command): if approval
-landed, the platform reports you are already authenticated and the token is cached; if it is
-still pending, you get the same URL and code back. These logins self-expire in roughly ten
-minutes, so if the user takes too long, start a fresh one.
+Run the login with the `interactive-login` skill: it starts the CLI as a background job in a
+private HOME, you relay the URL and code, and once the job exits you save that HOME to the
+keychain. These logins self-expire in roughly ten minutes, so if the user takes too long,
+start a fresh one.
## Boundaries
diff --git a/skills-seed/github-gitlab/SKILL.md b/skills-seed/github-gitlab/SKILL.md
index dcac128fa..770756e36 100644
--- a/skills-seed/github-gitlab/SKILL.md
+++ b/skills-seed/github-gitlab/SKILL.md
@@ -40,20 +40,10 @@ Use the native login only after an explicit authentication rejection. A TLS time
connection reset, rate limit, or GitHub server error is not evidence of logout or
permission expiry. Do not consume another credential grant to repair transport.
-For a confirmed missing or expired GitHub login:
-
-```bash
-gh auth login
-```
-
-The platform recognizes this device-flow login and runs it as a **durable process
-session** (ADR 0002): it prints the one-time code + verification URL immediately and keeps
-polling on the agent computer across turns — it does not block your turn or die at
-teardown. Give the user the code and URL, ask them to approve in the browser, then say
-"done". On the next turn, run `gh auth status` (or `gh auth login` again): the platform
-reports you're authenticated once approval completes, and the login self-expires if the
-user takes too long (just run `gh auth login` again to restart). GitLab is the same with
-`glab auth login`.
+For a confirmed missing or expired GitHub or GitLab login, follow the
+`interactive-login` skill: it runs `gh auth login` / `glab auth login` in a temporary
+home as a background job, relays the code and URL, and saves the login to the keychain
+so later commands use it through `execute.credentials`.
## Read-only work
diff --git a/skills-seed/interactive-login/SKILL.md b/skills-seed/interactive-login/SKILL.md
index b38f3dc59..02c46a2b4 100644
--- a/skills-seed/interactive-login/SKILL.md
+++ b/skills-seed/interactive-login/SKILL.md
@@ -1,84 +1,51 @@
---
name: interactive-login
-description: How to complete browser/interactive logins (aws / gh / glab / gcloud). The platform backgrounds the login poller so it survives the human's browser round-trip — and when that does NOT work.
+description: Log a CLI in with a browser/device-code flow (aws sso, gh, glab, gcloud, …) and save the result to the keychain so later commands can request it through execute.credentials.
---
-# Interactive login (and what backgrounding can and can't do)
+# Interactive login
-Some logins are interactive: the CLI prints a verification URL + one-time code, then
-**polls** the provider until the human approves in a browser, and only then writes the
-token to `$HOME`. Run that as a plain blocking `execute` and it either hangs your whole
-turn waiting for a human, or — worse — the turn's sandbox is torn down at turn end and
-the poller dies _before_ the human finishes, so nothing is ever saved. The next turn
-starts over and loops forever.
+Nothing captures logins for you. Every login runs in a fresh temporary home and is saved
+to the keychain with the script shipped with this skill. Never log in against the real
+`$HOME`, and never treat a token or config already in `$HOME` as a usable login: only a
+keychain handle is.
-You usually don't have to manage any of this. **Just run the native login command.**
+1. Make a fresh directory and start the login as a background job with every config
+ location pointed into it. Copy in any config the CLI needs first (for AWS, the
+ `[profile …]`/`[sso-session …]` blocks in `.aws/config`); everything under that
+ directory becomes the saved login.
-```bash
-aws sso login --use-device-code
-```
+ ```bash
+ d=$(mktemp -d) && echo "$d"
+ in_d() { env HOME="$d" XDG_CONFIG_HOME="$d/.config" XDG_DATA_HOME="$d/.local/share" XDG_STATE_HOME="$d/.local/state" XDG_CACHE_HOME="$d/.cache" "$@"; }
+ in_d aws sso login --profile work --use-device-code --no-browser
+ in_d gh auth login --hostname github.com --git-protocol https --web
+ in_d glab auth login --hostname gitlab.com --web
+ in_d gcloud auth login --no-launch-browser
+ ```
-> **AWS (and CLIs that log in through AWS SSO):** always pass `--use-device-code`
-> (`aws sso login` ≥ CLI v2 2.22). The bare form uses a PKCE flow
-> that redirects to `127.0.0.1` and only completes if the approving browser is on the
-> _same machine_ as the CLI. Since the user approves on _their_ computer, that flow can
-> never finish on the agent computer; `--use-device-code` forces the device-grant (URL +
-> code, completed by server-side polling) that works cross-device.
+ Use the CLI's device-code mode (AWS needs `--use-device-code`); a browser flow only
+ completes on the same machine.
-On the resident, per-scope agent computer (the production substrate), the platform
-recognizes these as interactive logins and runs each as a **durable process session**
-(ADR 0002): it returns the verification URL + code to you immediately instead of
-blocking, and keeps polling the provider **on the agent computer across turns**. (This
-recognition is wired only where the backend supports process sessions — on a per-turn
-local/docker sandbox nothing intercepts the command and it just blocks; see "When it
-will NOT work".) When the human approves, the poller writes the token into the
-durable `$HOME` and exits. Give the user the URL + code, tell them to approve, then on
-a later turn re-run the same command (or any command for that tool) — if approval
-landed, the platform reports you're already authenticated; if not, you get the same URL
+2. Relay the verification URL and code from the job output. Answer any prompt with
+ send_input. Watch the job; never stop it while the person is approving.
-- code back. Each pending login self-expires (~10–15 min); if it lapses, just run the
- login again.
+3. When it exits successfully, save the directory with the script under the absolute
+ directory the skill read reported:
-So the right pattern is: **run the native login, hand over the URL + code, continue,
-and re-check later.** You do not poll in a loop, you do not hold the turn open, and you
-do not hand-roll the exchange yourself.
+ ```bash
+ node <skill dir>/scripts/save-login.mjs aws "$d" --label "work (123456789012)"
+ ```
-## When backgrounding works
+ The script uploads only the files under that directory, removes the directory, and
+ prints the credential handle. It also removes a directory that holds no usable login.
+ Never remove the directory yourself; if the save fails, run the script again.
+ Saving the same service again replaces that entry in place: its id, handle, owner and
+ grants stay the same.
-- The agent computer is a **resident, per-scope machine** (the production substrate —
- `SANDBOX=fly`): it has durable process sessions and a durable `$HOME`, so a login
- started in one turn is still polling in the next and the token persists.
-- The command is one of the **recognized native flows**: `aws sso login`,
- `gh auth login`, `glab auth login`, `gcloud auth login`. The platform knows these
- poll-then-write and keeps them alive for you.
+4. Use the login only by naming the handle in `credentials` on `execute` or a background
+ start, including to check that it works. The files are staged in a private home for
+ that command or job, and refreshes the CLI writes are saved back when it ends.
-## When it will NOT work (don't rely on it)
-
-- **On a per-turn sandbox (local/docker dev).** Those backends have no process
- sessions and are torn down at the end of every turn — there is nothing to keep the
- poller alive across the human's approval. An interactive browser login cannot
- complete there. Use a **non-interactive** path instead: a token via env or
- `--with-token` / `--token --stdin`, a service-account / credentials file, or
- `--cred-file`. The platform deliberately leaves those non-interactive token forms to
- run normally (it only backgrounds the interactive login).
-- **For logins the platform doesn't recognize.** A CLI that prompts for a
- username/password at the TTY, or a bespoke OAuth flow that isn't in the list above,
- is **not** backgrounded — it runs as an ordinary blocking command and will hang and
- then die at turn teardown. Prefer that tool's non-interactive/token auth, or ask the
- user to provide a token you can configure.
-- **For arbitrary processes you spawn yourself.** Backgrounding with `&` / `nohup`
- does **not** make a process survive between turns. Only _declared_ session kinds are
- kept alive past teardown, and today only the interactive-login broker is wired in.
- A dev server, a long build, or a `sleep` you launch will be reaped when the turn
- ends — don't architect a task around "I'll leave it running in the background."
-
-## Boundaries
-
-- A backgrounded login session is **scope-keyed** to the requesting scope and lives on
- that scope's computer only — it never crosses the data boundary; another principal in
- the same thread cannot read your session or use the resulting auth.
-- A session is a **host for resident state, not an authority.** The login only
- completes the auth exchange; any side-effecting or destructive command you run
- afterward still goes through the normal approval path.
-- Never echo the token, one-time code reuse, client secret, or resulting credentials
- into the channel — hand over only the verification URL + code the human needs.
+Never print token files or paste their contents into the conversation. If the login fails
+or expires, start again from step 1 with a new directory.
diff --git a/skills-seed/interactive-login/scripts/save-login.mjs b/skills-seed/interactive-login/scripts/save-login.mjs
new file mode 100755
index 000000000..7776c2a2f
--- /dev/null
+++ b/skills-seed/interactive-login/scripts/save-login.mjs
@@ -0,0 +1,56 @@
+#!/usr/bin/env node
+import { lstatSync, readdirSync, readFileSync, rmSync } from "node:fs";
+import { join, relative } from "node:path";
+
+const [service, dir, ...rest] = process.argv.slice(2);
+const labelAt = rest.indexOf("--label");
+const label = labelAt >= 0 ? rest[labelAt + 1] : undefined;
+if (!service || !dir || (labelAt >= 0 && !label)) {
+ console.error("usage: save-login.mjs <service> <dir> [--label <account>]");
+ process.exit(2);
+}
+const { AGENT_API_URL, AGENT_API_TOKEN } = process.env;
+if (!AGENT_API_URL || !AGENT_API_TOKEN) {
+ console.error("AGENT_API_URL and AGENT_API_TOKEN must be set");
+ process.exit(2);
+}
+
+const files = [];
+const walk = (path) => {
+ for (const entry of readdirSync(path, { withFileTypes: true })) {
+ const full = join(path, entry.name);
+ if (entry.isDirectory() && entry.name !== "logs") walk(full);
+ else if (entry.isFile())
+ files.push({
+ path: relative(dir, full),
+ contentBase64: readFileSync(full).toString("base64"),
+ mode: lstatSync(full).mode & 0o777,
+ });
+ }
+};
+walk(dir);
+const discard = (message) => {
+ rmSync(dir, { recursive: true, force: true });
+ console.error(`${message}; ${dir} was removed`);
+ process.exit(1);
+};
+if (!files.length) discard(`${dir} contained no files, so the login did not complete`);
+
+const response = await fetch(`${AGENT_API_URL}/v1/keychain/credentials`, {
+ method: "POST",
+ headers: { "x-agent-capability": AGENT_API_TOKEN, "content-type": "application/json" },
+ body: JSON.stringify({ service, files, ...(label ? { accountLabel: label } : {}) }),
+}).catch((error) => {
+ console.error(`keychain save failed: ${error.message}; run this again to retry`);
+ process.exit(1);
+});
+const text = await response.text();
+if (response.status === 400 || response.status === 413)
+ discard(`keychain rejected the login (${response.status}): ${text}`);
+if (!response.ok) {
+ console.error(`keychain save failed (${response.status}): ${text}; run this again to retry`);
+ process.exit(1);
+}
+const { credential } = JSON.parse(text);
+rmSync(dir, { recursive: true, force: true });
+console.log(`saved ${credential.service} (${files.length} files) as ${credential.credentialHandle}`);
diff --git a/skills-seed/morning-digest/SKILL.md b/skills-seed/morning-digest/SKILL.md
index 35fa7933b..a3eccfd9e 100644
--- a/skills-seed/morning-digest/SKILL.md
+++ b/skills-seed/morning-digest/SKILL.md
@@ -17,7 +17,7 @@ and the available command tool for scripts.
## Sources
Use only what this deployment actually has, and say which sources you used. Check
-**Connected apps** and **Your logins** in your prompt — those blocks are the complete
+**Connected apps** and the keychain credentials in your prompt — those blocks are the complete
allowlist. Typically: mail and calendar (what lands today, what's unanswered), the issue
tracker (what moved, what's assigned to them), the repositories they work in (merged,
failing, waiting on their review), and any shared credential vended for a news or search
diff --git a/src/api/agent-api-catalog.ts b/src/api/agent-api-catalog.ts
index 0f8eac252..d4bfe9a1b 100644
--- a/src/api/agent-api-catalog.ts
+++ b/src/api/agent-api-catalog.ts
@@ -617,9 +617,9 @@ const FAMILIES: AgentApiFamily[] = [
(p === "/v1/keychain/grants" && m === "GET") ||
(m === "POST" && p.startsWith("/v1/keychain/grants/") && p.endsWith("/revoke")) ||
(p === "/v1/keychain/asks" && (m === "POST" || m === "GET")) ||
- (m === "POST" && p === "/v1/keychain/drops") ||
- (m === "POST" && p === "/v1/keychain/use"),
- guidance: "The keychain ask→card approval→use protocol is documented in your keychain manifest when one renders.",
+ (m === "POST" && p === "/v1/keychain/drops"),
+ guidance:
+ "The keychain ask→card approval→execute.credentials protocol is documented in your keychain manifest when one renders.",
routes: [
{
method: "POST|GET",
@@ -647,12 +647,6 @@ const FAMILIES: AgentApiFamily[] = [
summary:
'mint a single-use, expiring link for someone to drop a credential into the keychain via a browser (no secret in chat; hand the returned url over VERBATIM — it carries a link-bound token, so a reconstructed url will not work; declare the form inputs with fields[], e.g. [{key:"X_EMAIL",label:"Email",secret:false},{key:"X_PASSWORD",label:"Password"}] for a login, or omit for a single token; the link binds to the person who will paste the secret — pass onBehalfOf with their id when that is a teammate who spoke in this conversation rather than the person whose turn this is; refused on trigger-fired turns)',
},
- {
- method: "POST",
- path: "/v1/keychain/use",
- summary:
- "materialize an approved grant ({grant}) — or, in the owner's personal conversation, their own credential ({credential}) — into env vars for this turn",
- },
],
},
{
diff --git a/src/api/contract.ts b/src/api/contract.ts
index a1239a38f..8561e9d66 100644
--- a/src/api/contract.ts
+++ b/src/api/contract.ts
@@ -1,11 +1,3 @@
export const CAPABILITY_HEADER = "x-agent-capability";
export const CAPABILITY_CURL_AUTH = `-H "${CAPABILITY_HEADER}: $AGENT_API_TOKEN"`;
-
-export function keychainUseCommand(ref: { grant: string } | { credential: string }): string {
- const body = "grant" in ref ? `{"grant":"${ref.grant}"}` : `{"credential":"${ref.credential}"}`;
- return (
- `curl -fsS -X POST "$AGENT_API_URL/v1/keychain/use" ${CAPABILITY_CURL_AUTH} ` +
- `-H 'content-type: application/json' -d '${body}' -o /tmp/keychain.env && . /tmp/keychain.env`
- );
-}
diff --git a/src/api/deps.ts b/src/api/deps.ts
index 790fd56f7..5adc39242 100644
--- a/src/api/deps.ts
+++ b/src/api/deps.ts
@@ -49,7 +49,6 @@ import type { Scheduler } from "../cron/scheduler.ts";
import type { WebhookReceiver } from "../webhooks/webhook-receiver.ts";
import type { IdentityService } from "../identity/identity-service.ts";
import type { PrincipalLinkService } from "../identity/principal-links.ts";
-import type { DeviceFlowCutoverStore } from "../credentials/device-flow-cutover.ts";
import type { FeatureFlagStore } from "../feature-flags.ts";
import type {
ConnectorTokenStore,
@@ -121,7 +120,6 @@ export interface ServerDeps {
config?: ScopedConfigStore;
acl?: AclStore;
credentialUsage?: CredentialUsageSink;
- deviceFlowCutover?: DeviceFlowCutoverStore;
featureFlags?: FeatureFlagStore;
egressAudit?: EgressAuditSink;
brokerFetch?: BrokerFetch;
@@ -185,8 +183,6 @@ export interface ServerDeps {
deploymentLayer?: DeploymentLayerStore;
backgroundOwnership?: BackgroundOwnershipControl;
deploymentControlSecret?: string;
- credentialServices?: () => readonly string[];
- brokeredServices?: () => readonly string[];
deployDialTimeoutMs?: number;
deployAppsDomain?: string;
deployGateSecret?: string;
diff --git a/src/api/routes/admin-resources.ts b/src/api/routes/admin-resources.ts
index 0a26a0089..f93a62757 100644
--- a/src/api/routes/admin-resources.ts
+++ b/src/api/routes/admin-resources.ts
@@ -44,7 +44,6 @@ import {
} from "../../security/security-posture.ts";
import type { ApprovalGrantModes } from "../../types.ts";
import { parseEgressPolicy } from "../../resolution/egress-policy.ts";
-import { DEVICE_FLOW_CUTOVER_MODES, type DeviceFlowCutoverMode } from "../../credentials/device-flow-cutover.ts";
import { FEATURE_NAMES, type FeatureName } from "../../feature-flags.ts";
import { parseSharingPosture, SHARING_POSTURES, type SharingPosture } from "../../resolution/sharing-posture.ts";
@@ -137,17 +136,6 @@ const orgOnly = (scope: string, label: string): { error: string } | null =>
const boolBody = (body: unknown): { value: boolean } => ({ value: !!(body as { on?: unknown }).on });
-const credentialServices = async (
- deps: Pick<ServerDeps, "credentialServices" | "brokeredServices" | "deviceFlowCutover">,
- scope: ScopeId,
-): Promise<string[]> => [
- ...new Set([
- ...(deps.credentialServices?.() ?? []),
- ...(deps.brokeredServices?.() ?? []),
- ...((await deps.deviceFlowCutover?.listServices(scope)) ?? []),
- ]),
-];
-
const MAX_ORDERS_CHARS = 20_000;
const MAX_SOUL_CHARS = 100_000;
@@ -375,62 +363,6 @@ export const ADMIN_RESOURCES: readonly AdminResource[] = [
return { ok: true };
},
},
- {
- id: "device-flow-cutover",
- kind: "custom",
- target: "any",
- readKey: "deviceFlowCutover",
- label:
- "Credential-file migration by service. legacy restores resident files; prefer_ephemeral retains resident files without capturing replacements; ephemeral_only quarantines the stored legacy copy without deleting it; inherit clears a scope override.",
- get: async (deps, scope) => {
- if (!deps.deviceFlowCutover) return undefined;
- const out: Record<string, unknown> = {};
- for (const service of await credentialServices(deps, scope)) {
- out[service] = {
- configured: await deps.deviceFlowCutover.get(scope, service),
- effective: await deps.deviceFlowCutover.resolve(scope, service),
- };
- }
- return out;
- },
- apply: async (ctx, actor, scope) => {
- if (!ctx.deps.deviceFlowCutover) return { error: "not available on this deployment", status: 404 };
- const body = (ctx.body ?? {}) as { service?: unknown; mode?: unknown };
- if (typeof body.service !== "string" || !body.service.trim()) {
- return {
- error: "device-flow-cutover requires { service: string, mode: legacy | prefer_ephemeral | ephemeral_only }",
- };
- }
- const service = body.service;
- if (body.mode !== "inherit" && !(await credentialServices(ctx.deps, scope)).includes(service)) {
- return { error: `device-flow-cutover has no credential paths for service: ${service}` };
- }
- const beforeConfigured = await ctx.deps.deviceFlowCutover.get(scope, service);
- const beforeEffective = await ctx.deps.deviceFlowCutover.resolve(scope, service);
- if (body.mode === "inherit") {
- await ctx.deps.deviceFlowCutover.clear(scope, service);
- const effective = await ctx.deps.deviceFlowCutover.resolve(scope, service);
- audit(ctx.deps, {
- principalId: actor.id,
- action: "credential.cutover.update",
- resource: `${service}:${beforeConfigured?.mode ?? "inherit"}/${beforeEffective}->inherit/${effective}`,
- scopeLabel: scope,
- });
- return { ok: true };
- }
- if (typeof body.mode !== "string" || !(DEVICE_FLOW_CUTOVER_MODES as readonly string[]).includes(body.mode)) {
- return { error: `device-flow-cutover mode must be one of: ${DEVICE_FLOW_CUTOVER_MODES.join(", ")}, inherit` };
- }
- await ctx.deps.deviceFlowCutover.set(scope, service, body.mode as DeviceFlowCutoverMode, actor.id);
- audit(ctx.deps, {
- principalId: actor.id,
- action: "credential.cutover.update",
- resource: `${service}:${beforeConfigured?.mode ?? "inherit"}/${beforeEffective}->${body.mode}/${body.mode}`,
- scopeLabel: scope,
- });
- return { ok: true };
- },
- },
{
id: "unfulfilled-insights",
kind: "boolean",
diff --git a/src/api/routes/keychain.ts b/src/api/routes/keychain.ts
index 7a702a4b7..8c3cab74a 100644
--- a/src/api/routes/keychain.ts
+++ b/src/api/routes/keychain.ts
@@ -1,7 +1,6 @@
import {
KeychainError,
renderAskNotice,
- renderUseScript,
type CredentialFieldInput,
type CredentialFile,
type GrantMode,
@@ -289,45 +288,6 @@ async function handleKeychain(ctx: ApiCtx): Promise<void> {
);
return sendJson(res, 200, { asks });
}
-
- if (method === "POST" && pathname === "/v1/keychain/use") {
- const b = body as { grant?: unknown; credential?: unknown };
- if (typeof b.grant !== "string" && typeof b.credential !== "string") {
- return sendJson(res, 400, {
- error: "bad_request",
- message: "expected { grant } or { credential } (your own, personal conversation only)",
- });
- }
- let m;
- if (typeof b.grant === "string") {
- m = await kc.materialize(b.grant, capability.scopeId, actorId);
- } else {
- if (capability.liveActor !== true) {
- return sendJson(res, 403, {
- error: "forbidden",
- message:
- "own-credential use is implied only on a turn its owner themself sent live — this turn wasn't; use an existing grant or POST /v1/keychain/asks to request owner approval, then wait",
- });
- }
- m = await kc.materializeOwnById(actorId, b.credential as string, capability.scopeId);
- }
- deps.credentialUsage?.record({
- slug: `keychain:${m.service}:${m.credentialId}`,
- host: m.service,
- status: "materialized",
- scopeLabel: capability.scopeId,
- principalId: actorId,
- });
- audit(deps, {
- principalId: actorId,
- action: "keychain.use",
- resource: m.grantId ? `${m.credentialId} (grant ${m.grantId})` : `${m.credentialId} (own)`,
- scopeLabel: capability.scopeId,
- });
- res.writeHead(200, { "content-type": "text/plain; charset=utf-8" });
- res.end(renderUseScript(m));
- return;
- }
} catch (e) {
if (e instanceof KeychainError) return sendJson(res, e.status, { error: "keychain", message: e.message });
throw e;
@@ -363,6 +323,5 @@ export const keychainRoutes: ReadonlyArray<Route<ApiCtx>> = [
{ method: "POST", path: "/v1/keychain/grants/:id/revoke", auth: "either", handle: handleKeychain },
{ method: "POST", path: "/v1/keychain/asks", auth: "either", handle: handleKeychain },
{ method: "GET", path: "/v1/keychain/asks", auth: "either", handle: handleKeychain },
- { method: "POST", path: "/v1/keychain/use", auth: "either", handle: handleKeychain },
{ method: "POST", path: "/v1/keychain/approvals/:id", auth: "source", handle: handleApproval },
];
diff --git a/src/api/routes/secret-drop.ts b/src/api/routes/secret-drop.ts
index 320e439b8..aa8a3e462 100644
--- a/src/api/routes/secret-drop.ts
+++ b/src/api/routes/secret-drop.ts
@@ -378,6 +378,7 @@ async function redeemDrop(ctx: ApiCtx): Promise<void> {
...(drop.destination ? { destination: drop.destination } : {}),
...(drop.threadRef ? { threadRef: drop.threadRef } : {}),
...(grantId ? { grantId } : {}),
+ ...(meta.credentialHandle ? { credentialHandle: meta.credentialHandle } : {}),
granted: !!grantId,
...(pending.length ? { pendingSiblings: pending } : {}),
});
diff --git a/src/api/user-scoped-routes.ts b/src/api/user-scoped-routes.ts
index 2fd8d08ca..e89ced18b 100644
--- a/src/api/user-scoped-routes.ts
+++ b/src/api/user-scoped-routes.ts
@@ -160,7 +160,6 @@ const SYSTEM: Rule[] = [
pat("DELETE", "/v1/keychain/credentials/:id"),
pat("POST", "/v1/keychain/drops"),
pat("POST", "/v1/keychain/grants/:id/revoke"),
- pat("POST", "/v1/keychain/use"),
pat("POST", "/v1/surface-context"),
pat("POST", "/v1/surface-file"),
pat("POST", "/v1/triggers/:id/consent"),
diff --git a/src/connectors/background-exec-broker.ts b/src/connectors/background-exec-broker.ts
index fc0cb8a3f..846bd80fd 100644
--- a/src/connectors/background-exec-broker.ts
+++ b/src/connectors/background-exec-broker.ts
@@ -1,14 +1,18 @@
import type { ProcessSandbox, ProcessState, SandboxHandle } from "../sandbox/sandbox.ts";
import type { ProcessRegistry, ProcessStatus } from "../processes/process-registry.ts";
+import type { ProcessCredentialFiles } from "../credentials/execute-files.ts";
import { awaitProcessExit } from "../sandbox/await-process-exit.ts";
import { pollProcess, processIsGone } from "../sandbox/process-poll.ts";
import { redactCommand } from "../sandbox/exec-process-session.ts";
import { CONFIG_DEFAULTS } from "../config.ts";
+import { createExactSecretValueMasker } from "../security/secret-masking.ts";
+import { withCleanup } from "../util/errors.ts";
export interface BackgroundExecBrokerDeps {
sandbox: ProcessSandbox;
registry: ProcessRegistry;
provisionSandbox?: (id: string) => Promise<SandboxHandle>;
+ onExit?: (handle: SandboxHandle, processId: string) => Promise<void>;
scopeId: string;
sessionRef?: string;
ttlMs?: number;
@@ -54,9 +58,22 @@ export interface BackgroundWriteResult {
status: ProcessState;
}
+export interface BackgroundCredentials {
+ env: Record<string, string>;
+ secrets: readonly string[];
+ files?: ProcessCredentialFiles;
+ release?: () => Promise<void>;
+}
+
export interface BackgroundExecBroker {
handleFor?(processId: string): Promise<SandboxHandle | null>;
- start(handle: SandboxHandle, command: string, purpose: string, ttlMs?: number): Promise<BackgroundStartResult>;
+ start(
+ handle: SandboxHandle,
+ command: string,
+ purpose: string,
+ ttlMs?: number,
+ credentials?: BackgroundCredentials,
+ ): Promise<BackgroundStartResult>;
poll(
handle: SandboxHandle,
processId: string,
@@ -85,6 +102,10 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
const termGraceMs = deps.termGraceMs ?? DEFAULT_TERM_GRACE_MS;
const killGraceMs = deps.killGraceMs ?? DEFAULT_KILL_GRACE_MS;
+ const exited = async (handle: SandboxHandle, processId: string) => {
+ await deps.registry.markStatus(processId, "exited");
+ await deps.onExit?.(handle, processId);
+ };
return {
async handleFor(processId) {
const rec = await deps.registry.get(processId);
@@ -93,7 +114,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
if (!deps.provisionSandbox) throw new Error("background job sandbox is unavailable");
return deps.provisionSandbox(rec.sandboxId);
},
- async start(handle, command, purpose, ttlMs): Promise<BackgroundStartResult> {
+ async start(handle, command, purpose, ttlMs, credentials): Promise<BackgroundStartResult> {
if (!purpose?.trim()) throw new Error("background start requires a short purpose describing the job");
purpose = redactCommand(purpose.replace(/\s+/g, " ").trim(), handle.env);
const ttl = Math.min(ttlMs ?? defaultTtlMs, maxTtlMs);
@@ -102,7 +123,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
const redacted = redactCommand(normalized, handle.env);
let processId =
- redacted === normalized
+ redacted === normalized && !credentials
? ((await deps.registry.listByScope(deps.scopeId)).find(
(r) =>
r.kind === "background" &&
@@ -121,7 +142,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
waitMs: 0,
});
if (read.status.state === "exited") {
- await deps.registry.markStatus(existingProcessId, "exited");
+ await exited(handle, existingProcessId);
processId = null;
}
} catch (error) {
@@ -137,33 +158,55 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
maxBytes: DEFAULT_MAX_BYTES,
waitMs: 0,
});
- if (read.status.state === "exited") await deps.registry.markStatus(processId, "exited");
- return { processId, output: read.chunks, cursor: read.cursor, status: read.status, reattached: true };
+ if (read.status.state === "exited") await exited(handle, processId);
+ return {
+ processId,
+ output: read.chunks,
+ cursor: read.cursor,
+ status: read.status,
+ reattached: true,
+ };
}
- const register = async (id: string): Promise<void> => {
- await deps.registry.register({
- processId: id,
- scopeId: deps.scopeId,
- ...(handle.resourceId ? { sandboxId: handle.resourceId } : {}),
- kind: "background",
- command: redacted,
- purpose,
- ttlMs: ttl,
- ...(deps.sessionRef ? { sessionRef: deps.sessionRef } : {}),
+ let launched = false;
+ try {
+ const mask = createExactSecretValueMasker(credentials?.secrets ?? []);
+ const register = async (id: string): Promise<void> => {
+ launched = true;
+ await deps.registry.register({
+ processId: id,
+ scopeId: deps.scopeId,
+ ...(handle.resourceId ? { sandboxId: handle.resourceId } : {}),
+ kind: "background",
+ command: redacted,
+ purpose,
+ ttlMs: ttl,
+ ...(deps.sessionRef ? { sessionRef: deps.sessionRef } : {}),
+ ...(credentials?.files ? { credentialFiles: credentials.files } : {}),
+ });
+ };
+ const startOptions = { env: { PYTHONUNBUFFERED: "1", ...credentials?.env } };
+ if (deps.sandbox.startRegisteredProcess) {
+ ({ processId } = await deps.sandbox.startRegisteredProcess(handle, command, register, startOptions));
+ } else {
+ ({ processId } = await deps.sandbox.startProcess(handle, command, startOptions));
+ launched = true;
+ await register(processId);
+ }
+
+ const { output, cursor, status } = await pollProcess(deps.sandbox, handle, processId, {
+ deadlineMs: POLL_MS,
});
- };
- const startOptions = { env: { PYTHONUNBUFFERED: "1" } };
- if (deps.sandbox.startRegisteredProcess) {
- ({ processId } = await deps.sandbox.startRegisteredProcess(handle, command, register, startOptions));
- } else {
- ({ processId } = await deps.sandbox.startProcess(handle, command, startOptions));
- await register(processId);
+ if (status.state === "exited") await exited(handle, processId);
+ return { processId, output: mask(output), cursor, status, reattached: false };
+ } catch (error) {
+ return withCleanup(
+ () => Promise.reject(error),
+ async () => {
+ if (!launched) await credentials?.release?.();
+ },
+ );
}
-
- const { output, cursor, status } = await pollProcess(deps.sandbox, handle, processId, { deadlineMs: POLL_MS });
- if (status.state === "exited") await deps.registry.markStatus(processId, "exited");
- return { processId, output, cursor, status, reattached: false };
},
async poll(handle, processId, opts): Promise<BackgroundPollResult> {
@@ -180,7 +223,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
maxBytes: opts?.maxBytes ?? DEFAULT_MAX_BYTES,
waitMs: opts?.waitMs ?? 0,
});
- if (read.status.state === "exited") await deps.registry.markStatus(processId, "exited");
+ if (read.status.state === "exited") await exited(handle, processId);
return { processId, chunks: read.chunks, cursor: read.cursor, status: read.status };
},
@@ -195,7 +238,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
}
await deps.sandbox.writeStdin(handle, processId, data);
const read = await deps.sandbox.readProcess(handle, processId, { sinceCursor: 0, maxBytes: 1, waitMs: 0 });
- if (read.status.state === "exited") await deps.registry.markStatus(processId, "exited");
+ if (read.status.state === "exited") await exited(handle, processId);
return { processId, bytes: data.length, status: read.status };
},
@@ -214,7 +257,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrou
await deps.sandbox.signalProcess(handle, processId, "KILL");
status = await awaitProcessExit(deps.sandbox, handle, processId, killGraceMs);
}
- if (status.state === "exited") await deps.registry.markStatus(processId, "exited");
+ if (status.state === "exited") await exited(handle, processId);
return { processId, status, stopped: status.state === "exited" };
},
diff --git a/src/core/environment-facts.ts b/src/core/environment-facts.ts
index 3d86f6bb9..2f24e8a66 100644
--- a/src/core/environment-facts.ts
+++ b/src/core/environment-facts.ts
@@ -1,5 +1,4 @@
import type { AgentComputerSpec } from "../sandbox/sandbox.ts";
-import type { ResidentAuthConnector, ScopeLivenessRecord } from "../credentials/resident-auth.ts";
import { connectorLabel, type ConnectorStatusRecord } from "../credentials/connector-status.ts";
export interface WorkspaceLayoutInfo {
@@ -35,32 +34,6 @@ export function renderComputerBlock(spec: AgentComputerSpec | undefined, layout:
return lines.join("\n");
}
-export function renderResidentLoginsBlock(
- record: ScopeLivenessRecord | null,
- connectors: readonly ResidentAuthConnector[],
-): string {
- if (!record) return "";
- const present = connectors.filter((c) => {
- const s = record.connectors[c.id];
- return s === "active" || s === "inactive";
- });
- if (!present.length) return "";
- const lines = [
- "## Your logins",
- "Native logins on your computer (resident — each tool authenticates with its own; checked recently):",
- "Logins survive machine replacement automatically: the platform keeps an encrypted copy core-side and restores it onto a fresh machine; they are never written into workspace backups.",
- "To (re)log in, start the login command as a background process using the available process controls. A device-flow login prints a URL/code then waits for the person to approve. Relay the URL/code, then watch or poll the process until it exits; never kill it mid-flight. Capture into your keychain is automatic.",
- ];
- for (const c of present.sort((a, b) => a.id.localeCompare(b.id))) {
- if (record.connectors[c.id] === "active") {
- lines.push(`- ${c.label} — ✓ signed in`);
- } else {
- lines.push(`- ${c.label} — ✗ not signed in; to use it: start \`${c.reauth}\` as a background process`);
- }
- }
- return lines.join("\n");
-}
-
export function renderConnectedAppsBlock(
record: ConnectorStatusRecord | null,
availableProviders: readonly string[] = [],
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index 7410f4b78..1f13f67b6 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -59,13 +59,7 @@ import { createMonitorBroker, readBackgroundOutputTail } from "../monitors/monit
import { isPollSurface, isSilentPollReply } from "../triggers/run-trigger.ts";
import { envKey } from "../credentials/connector-token.ts";
import { credentialHandle, renderKeychainManifest, type PublicServiceCredential } from "../credentials/keychain.ts";
-import {
- captureDeviceFlowLogins,
- deviceFlowCredOwner,
- registerLoginPaths,
-} from "../credentials/device-flow-persist.ts";
-import type { CredentialPathSpec } from "../credentials/resident-paths.ts";
-import type { DeviceFlowCutoverMode } from "../credentials/device-flow-cutover.ts";
+import { finishProcessCredentials } from "../credentials/execute-files.ts";
import {
configuredConnectorProviders,
connectorStatusIsStale,
@@ -1455,24 +1449,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
return cleaned.length > 16_000 ? `${cleaned.slice(0, 16_000)}…` : cleaned;
};
const brokeredTools = external ? [] : (deps.brokeredTools ?? []);
- const credentialTools = external ? [] : (deps.credentialTools ?? brokeredTools);
- const credentialServices = [
- ...new Set([
- ...credentialTools.map((tool) => tool.service),
- ...brokeredTools.map((tool) => tool.service),
- ...(!external ? ((await deps.deviceFlowCutover?.listServices(memoryScopeId)) ?? []) : []),
- ]),
- ];
- const cutoverModes = new Map<string, DeviceFlowCutoverMode>();
- for (const service of credentialServices) {
- const policy = deps.deviceFlowCutover
- ? await deps.deviceFlowCutover.resolvePolicy(memoryScopeId, service)
- : null;
- cutoverModes.set(service, policy?.mode ?? "legacy");
- }
- const cutoverModeOf = (service: string): DeviceFlowCutoverMode => cutoverModes.get(service) ?? "legacy";
- const quarantinedServices = credentialServices.filter((service) => cutoverModeOf(service) === "ephemeral_only");
- const credentialCutoverServices = credentialServices.filter((service) => cutoverModeOf(service) !== "legacy");
const openSpeakerKeychain =
liveAuthorTurn && conversation.kind !== "dm" && sharingSources.includes(personalScope(actor.id));
const openAutomationKeychain =
@@ -1500,10 +1476,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
!external &&
(openSpeakerKeychain ||
(conversation.kind !== "dm" && input.origin.kind === "automation" && input.origin.useOwnerKeychain === true));
- let ownerAuthAvailable = isolateOwnerKeychain;
- if (brokeredTools.some((tool) => cutoverModeOf(tool.service) !== "legacy" && deps.layerBrokerFor?.(tool))) {
- ownerAuthAvailable = true;
- }
+ const ownerAuthAvailable = isolateOwnerKeychain;
const connectorEnv: Record<string, string> = {};
const credsStart = Date.now();
const commandCredentials: CommandCredential[] = [];
@@ -1520,8 +1493,21 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
};
const resolvedCredential = (materialized: import("../credentials/keychain.ts").MaterializedCred) => {
- if (materialized.kind !== "env") throw new Error("File credentials require the supervised execution route");
- return { env: materialized.env };
+ if (materialized.kind === "env") return { env: materialized.env };
+ return {
+ env: [],
+ files: {
+ files: materialized.files,
+ save: (files: import("../credentials/keychain.ts").CredentialFile[]) =>
+ deps.keychain!.updateFiles(materialized, files),
+ source: {
+ credentialId: materialized.credentialId,
+ ownerId: materialized.ownerId,
+ service: materialized.service,
+ ...(materialized.grantId ? { grantId: materialized.grantId } : {}),
+ },
+ },
+ };
};
const addCredentialToCatalog = (
credential: CommandCredential,
@@ -1543,7 +1529,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
if (external || strictReadOnly || !deps.keychain) return;
const keychain = deps.keychain;
for (const { grant, credential } of await keychain.grantsForScope(scopeId)) {
- if (credential.kind !== "env" || isBackendCredential(credential)) continue;
+ if (credential.kind === "broker" || isBackendCredential(credential)) continue;
addCredential(
{
handle: credentialHandle(credential.id),
@@ -1566,7 +1552,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
(input.origin.kind === "automation" && input.origin.useOwnerKeychain === true);
if (ownAllowed) {
for (const credential of await keychain.listByOwner(actor.id)) {
- if (credential.kind !== "env" || isBackendCredential(credential)) continue;
+ if (credential.kind === "broker" || isBackendCredential(credential)) continue;
addCredential(
{
handle: credentialHandle(credential.id),
@@ -1943,16 +1929,10 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
for (const tool of brokeredTools) {
const broker = deps.layerBrokerFor?.(tool);
if (!broker) continue;
- const brokerScope = cutoverModeOf(tool.service) === "legacy" ? "scoped" : "owner";
addCredentialToCatalog(
{
handle: `broker_${tool.service}`,
- scope: brokerScope,
resolve: async () => {
- const policy = await deps.deviceFlowCutover?.resolvePolicy(memoryScopeId, tool.service);
- const currentScope = !policy || policy.mode === "legacy" ? "scoped" : "owner";
- if (currentScope !== brokerScope)
- throw new Error(`Broker credential scope changed: ${tool.service}; retry on the next turn`);
let aws;
try {
aws = await broker.credsForActor(actor.id);
@@ -1960,7 +1940,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
deps.credentialUsage?.record({
slug: tool.service,
host: "sts.amazonaws.com",
- status: brokerScope === "owner" ? "ephemeral_failed_closed" : "legacy_unavailable",
+ status: "unavailable",
scopeLabel: scopeId,
principalId: actor.id,
});
@@ -1969,7 +1949,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
deps.credentialUsage?.record({
slug: tool.service,
host: "sts.amazonaws.com",
- status: brokerScope === "owner" ? "ephemeral_vended" : "legacy_vended",
+ status: "vended",
scopeLabel: scopeId,
principalId: actor.id,
});
@@ -2003,15 +1983,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
swallow("gap-work emit", e);
}
};
- const ephemeralOnlyTools = brokeredTools.filter((tool) => cutoverModeOf(tool.service) === "ephemeral_only");
- const ephemeralOnlyDenyRules = ephemeralOnlyTools.map((tool) => ({
- pattern: `(^|[\\s;&|()])${tool.binary.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}($|[\\s;&|()])`,
- decision: "deny" as const,
- reason: `credential-bearing service ${tool.service} requires execute with its broker credential and scope:owner`,
- }));
- const commandPolicy = ephemeralOnlyDenyRules.length
- ? { ...resolution.commandPolicy, rules: [...ephemeralOnlyDenyRules, ...resolution.commandPolicy.rules] }
- : resolution.commandPolicy;
+ const commandPolicy = resolution.commandPolicy;
const layerCommandRules = [...(deps.deploymentLayer?.commandRules ?? [])];
const reachAvailable = !!deps.reachExec && !!deps.directory && conversation.kind === "dm";
const turnSandboxResources = external
@@ -2038,7 +2010,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
scratchBox,
ownerAuthBox,
ownerAuthCommand,
- scopedCommand,
provision,
provisionScratch,
accessResource,
@@ -2063,20 +2034,13 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
connectorEnv,
egressTokenForTurn,
egressTokenForPolicy,
- isolateOwnerKeychain,
openSpeakerKeychain: openSpeakerKeychain || openAutomationKeychain,
openResourceAccess:
!external &&
(liveAuthorTurn || (input.origin.kind === "automation" && input.origin.useOwnerKeychain === true)),
ownerAuthAvailable,
- credentialTools,
- credentialServices,
- credentialCutoverServices,
- quarantinedServices,
- cutoverModeOf,
visibleSkillsForTurn,
emitGapWork,
- perf,
});
const leaseStart = Date.now();
const acquired = await acquireTurnLeaseOrRefuse({
@@ -2591,6 +2555,16 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
sandbox: deps.sandbox,
registry: deps.processes,
provisionSandbox: provisionResource,
+ onExit: (handle, processId) =>
+ finishProcessCredentials(
+ {
+ sandbox: deps.sandbox,
+ processes: deps.processes!,
+ ...(deps.keychain ? { keychain: deps.keychain } : {}),
+ },
+ handle,
+ processId,
+ ),
scopeId: memoryScopeId,
sessionRef: conversation.threadRef,
...(deps.backgroundJobTtlMs !== undefined ? { ttlMs: deps.backgroundJobTtlMs } : {}),
@@ -2772,7 +2746,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
accessSandboxResource: accessResource,
...(provisionOwnerAuth ? { provisionOwnerAuth } : {}),
...(ownerAuthCommand ? { ownerAuthCommand } : {}),
- ...(scopedCommand ? { scopedCommand } : {}),
useSkill,
...(reachAvailable
? {
@@ -2785,15 +2758,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
: {}),
layers: resolution.layers,
commandPolicy: () => commandPolicy,
- commandPolicyForCredentials: (handles, ownerAuth) => ({
- ...resolution.commandPolicy,
- rules: [
- ...ephemeralOnlyDenyRules.filter(
- (_, index) => !ownerAuth || !handles.includes(`broker_${ephemeralOnlyTools[index]!.service}`),
- ),
- ...resolution.commandPolicy.rules,
- ],
- }),
layerCommandRules: () => layerCommandRules,
authorizeCommand,
grantedHandles: resolution.grantedHandles,
@@ -2854,27 +2818,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
: {}),
...(strictReadOnly ? {} : { attach: attachStaging.attach }),
- ...(external || strictReadOnly || !deps.keychain
- ? {}
- : {
- registerLogin: async (service: string, paths: readonly CredentialPathSpec[]) =>
- registerLoginPaths({
- sandbox: deps.sandbox,
- handle: await provision(),
- keychain: deps.keychain!,
- ownerId: deviceFlowCredOwner(memoryScopeId, actor.id),
- service,
- paths: [...paths],
- onAnomaly: (svc, detail) =>
- deps.errors?.record({
- category: "keychain",
- code: "device_flow_capture_skipped",
- message: `register_login ${svc}: ${detail}`,
- scopeLabel: scopeId,
- sessionId: session.id,
- }),
- }),
- }),
memory: context.memory,
memoryCaptureMetadata: () => ({ sessionId: session.id, inheritedRecords: captureDependencies() }),
memoryScopeId,
@@ -4337,43 +4280,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
const tail = async (): Promise<void> => {
try {
- const writable = resolution.layers.find((l) => l.mode === "rw");
- const writtenHandle = box.used ? box.handle : null;
- if (writable && writtenHandle) {
- if (!external && deps.keychain) {
- try {
- await captureDeviceFlowLogins({
- sandbox: deps.sandbox,
- handle: writtenHandle,
- keychain: deps.keychain,
- ownerId: deviceFlowCredOwner(memoryScopeId, actor.id),
- ...(credentialCutoverServices.length ? { excludeServices: credentialCutoverServices } : {}),
- ...(deps.deploymentLayer?.credentialPaths.length
- ? { credentialPaths: deps.deploymentLayer.credentialPaths }
- : {}),
- onAnomaly: (service, detail) =>
- deps.errors?.record({
- category: "keychain",
- code: "device_flow_capture_skipped",
- message: `device-flow capture skipped ${service}: ${detail}`,
- scopeLabel: scopeId,
- sessionId: session.id,
- }),
- });
- } catch (e) {
- deps.errors?.record(
- {
- category: "keychain",
- code: "device_flow_capture_failed",
- message: errMessage(e),
- scopeLabel: scopeId,
- sessionId: session.id,
- },
- e,
- );
- }
- }
- }
if (!pausing && turnCompleted && !session.title && !fallbackTitleWrite) {
await titles.generateAndStore(session.id, scopeId, `User:\n${titleText}\n\nAssistant:\n${result.reply}`);
}
diff --git a/src/core/orchestrator/external-tools.ts b/src/core/orchestrator/external-tools.ts
index c4ff1be39..3cf344bc4 100644
--- a/src/core/orchestrator/external-tools.ts
+++ b/src/core/orchestrator/external-tools.ts
@@ -9,7 +9,7 @@ export function externalTools(tools: ToolContext, authorizeSandbox?: (id: string
const allowed = new Set(["attach", "history", "historyOpen", "commandCredentialHandles"]);
return new Proxy(tools, {
get(target, property, receiver) {
- if (property === "sessionSyscalls" || property === "runtime" || property === "registerLogin") return undefined;
+ if (property === "sessionSyscalls" || property === "runtime") return undefined;
if (property === "mcpToolDefs") return () => [];
if (property === "execute")
return async (command: string, opts?: Parameters<ToolContext["execute"]>[1]) => {
diff --git a/src/core/orchestrator/sandboxes.ts b/src/core/orchestrator/sandboxes.ts
index be20158a8..aea3ee182 100644
--- a/src/core/orchestrator/sandboxes.ts
+++ b/src/core/orchestrator/sandboxes.ts
@@ -6,20 +6,14 @@ import type { GapPhase } from "../../sessions/session-store.ts";
import { type SandboxHandle, supportsProcessSessions, SandboxProvisionCleanupError } from "../../sandbox/sandbox.ts";
import type { SandboxAccessPlan } from "../../sandbox/sandbox-resources.ts";
import { reconcileProcesses } from "../../processes/reconcile.ts";
-import {
- deviceFlowCredOwner,
- materializeDeviceFlowLogins,
- removeDeviceFlowLogins,
-} from "../../credentials/device-flow-persist.ts";
-import type { DeviceFlowCutoverMode } from "../../credentials/device-flow-cutover.ts";
-import { expandServiceAliases } from "../../credentials/resident-paths.ts";
+import { finishProcessCredentials } from "../../credentials/execute-files.ts";
import {
materializeSkillTree as laySkillTree,
packRoot,
rehomeSkillPaths,
renderSkillBody,
skillDir,
- SKILLS_DIR,
+ SKILLS_ROOT,
} from "../../skills/materialize.ts";
import { safeSkillFilePath, type SkillResolution } from "../../skills/skill-store.ts";
import { isSafeSkillName } from "../../skills/skill-name.ts";
@@ -46,18 +40,11 @@ export interface TurnSandboxContext {
connectorEnv: Record<string, string>;
egressTokenForTurn: string | undefined;
egressTokenForPolicy?: (policy: Resolution["egress"]) => Promise<string | undefined>;
- isolateOwnerKeychain: boolean;
openSpeakerKeychain?: boolean;
openResourceAccess?: boolean;
ownerAuthAvailable: boolean;
- credentialTools: readonly import("../../deployment/load-layer.ts").LayerCredentialTool[];
- credentialServices: string[];
- credentialCutoverServices: string[];
- quarantinedServices: string[];
- cutoverModeOf: (service: string) => DeviceFlowCutoverMode;
visibleSkillsForTurn: () => Promise<SkillResolution[]>;
emitGapWork: (phase: GapPhase, start: number, end: number) => void;
- perf: { credsMs: number };
}
export function createTurnSandboxes(ctx: TurnSandboxContext) {
@@ -75,18 +62,11 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
connectorEnv,
egressTokenForTurn,
egressTokenForPolicy,
- isolateOwnerKeychain,
openSpeakerKeychain,
openResourceAccess,
ownerAuthAvailable,
- credentialTools,
- credentialServices,
- credentialCutoverServices,
- quarantinedServices,
- cutoverModeOf,
visibleSkillsForTurn,
emitGapWork,
- perf,
} = ctx;
let ownerAuthCommand: ((command: string, env?: Record<string, string>) => string) | undefined;
@@ -101,9 +81,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
const keys = brokerEnvKeys.filter((key) => !(key in env));
return keys.length ? `unset ${keys.join(" ")}; ` : "";
};
- const scopedCommand = credentialCutoverServices.length
- ? (command: string, env = connectorEnv): string => `${unsetBrokerEnv(env)}${command}`
- : undefined;
if (ownerAuthAvailable) {
ownerAuthCommand = (command, env = {}) => {
if (openSpeakerKeychain)
@@ -173,22 +150,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
}
throw new AggregateError(errors, "Disposable sandbox destruction failed");
};
- const scrubOwnerAuthHandle = async (handle: SandboxHandle): Promise<void> => {
- if (!deps.keychain || !isolateOwnerKeychain) return;
- const services = (await deps.keychain.listByOwner(actor.id))
- .filter((record) => record.kind === "file")
- .map((record) => record.service);
- if (!services.length) return;
- await removeDeviceFlowLogins({
- sandbox: deps.sandbox,
- handle,
- keychain: deps.keychain,
- ownerId: actor.id,
- services,
- allOrigins: true,
- canonicalRoots: credentialTools.filter((tool) => services.includes(tool.service)).flatMap((tool) => tool.roots),
- });
- };
let sandboxStatusSeq = 2_000_000;
const onSandboxStatus =
input.runId && deps.runActivity
@@ -217,110 +178,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
});
return provisionInFlight;
};
- const prepareCredentials = async (
- handle: SandboxHandle,
- emit: typeof emitGapWork,
- credentialScopeId = memoryScopeId,
- ): Promise<void> => {
- if (!input.externalSlack && deps.keychain) {
- const deviceFlowStart = Date.now();
- const crossScope = credentialScopeId !== memoryScopeId;
- const services = crossScope
- ? [
- ...new Set([
- ...credentialServices,
- ...((await deps.deviceFlowCutover?.listServices(credentialScopeId)) ?? []),
- ]),
- ]
- : credentialServices;
- const targetModes = new Map<string, DeviceFlowCutoverMode>();
- if (crossScope) {
- for (const service of services) {
- const policy = await deps.deviceFlowCutover?.resolvePolicy(credentialScopeId, service);
- targetModes.set(service, policy?.mode ?? "legacy");
- }
- }
- const modeOf = (service: string): DeviceFlowCutoverMode => targetModes.get(service) ?? cutoverModeOf(service);
- const excludedServices = [
- ...new Set([...quarantinedServices, ...services.filter((service) => modeOf(service) === "ephemeral_only")]),
- ];
- const restoreOwnerId =
- !crossScope && input.origin.kind === "automation" && input.origin.useOwnerKeychain && !isolateOwnerKeychain
- ? actor.id
- : deviceFlowCredOwner(credentialScopeId, actor.id);
- const resetGenerations = new Map<string, string>();
- for (const service of services) {
- if (modeOf(service) !== "legacy") continue;
- const generation = await deps.deviceFlowCutover?.residentResetGeneration(
- credentialScopeId,
- service,
- handle.resourceId,
- );
- if (generation) resetGenerations.set(service, generation);
- }
- const owned = resetGenerations.size ? await deps.keychain.listByOwner(restoreOwnerId) : [];
- const resetServices = [...resetGenerations.keys()].filter((service) =>
- owned.some((record) => expandServiceAliases([service]).includes(record.service)),
- );
- const removeServices = [...new Set([...excludedServices, ...resetServices])];
- if (removeServices.length) {
- await removeDeviceFlowLogins({
- sandbox: deps.sandbox,
- handle,
- keychain: deps.keychain,
- ownerId: restoreOwnerId,
- ...(crossScope ? { allOrigins: true } : {}),
- services: removeServices,
- canonicalRoots: credentialTools
- .filter((tool) => removeServices.includes(tool.service))
- .flatMap((tool) => tool.roots),
- });
- }
- try {
- const restoredServices = await materializeDeviceFlowLogins({
- sandbox: deps.sandbox,
- handle,
- keychain: deps.keychain,
- ownerId: restoreOwnerId,
- ...(crossScope ? { allOrigins: true } : {}),
- ...(excludedServices.length ? { excludeServices: excludedServices } : {}),
- onAnomaly: (service, detail) =>
- deps.errors?.record({
- category: "keychain",
- code: "device_flow_restore_failed",
- message: `${service}: ${detail}`,
- scopeLabel: scopeId,
- sessionId: session.id,
- }),
- });
- for (const service of restoredServices) {
- deps.credentialUsage?.record({
- slug: `keychain:${service}`,
- host: "local",
- status: modeOf(service) === "prefer_ephemeral" ? "legacy_retained" : "legacy_restored",
- scopeLabel: scopeId,
- principalId: actor.id,
- });
- }
- for (const [service, generation] of resetGenerations) {
- await deps.deviceFlowCutover?.markResidentReset(credentialScopeId, service, generation, handle.resourceId);
- }
- } catch (err) {
- deps.errors?.record(
- {
- category: "keychain",
- code: "device_flow_restore_failed",
- message: errMessage(err),
- scopeLabel: scopeId,
- sessionId: session.id,
- },
- err,
- );
- }
- emit("creds", deviceFlowStart, Date.now());
- perf.credsMs += Date.now() - deviceFlowStart;
- }
- };
const doProvision = async (emit: typeof emitGapWork, eager: boolean): Promise<SandboxHandle> => {
const provisionStart = Date.now();
if (input.externalSlack) {
@@ -354,14 +211,23 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
eager,
}),
});
- await prepareCredentials(handle, emit);
const dirCleanupStart = Date.now();
await prepareTurnFiles(handle);
emit("dir_cleanup", dirCleanupStart, Date.now());
if (deps.processes && supportsProcessSessions(deps.sandbox)) {
const procReconcileStart = Date.now();
try {
- await reconcileProcesses(deps.sandbox, handle, deps.processes, memoryScopeId);
+ await reconcileProcesses(deps.sandbox, handle, deps.processes, memoryScopeId, (exited, processId) =>
+ finishProcessCredentials(
+ {
+ sandbox: deps.sandbox,
+ processes: deps.processes!,
+ ...(deps.keychain ? { keychain: deps.keychain } : {}),
+ },
+ exited,
+ processId,
+ ),
+ );
} catch (err) {
deps.errors?.record(
{
@@ -380,15 +246,15 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
box.handle = handle;
return handle;
};
- const skillsRoot = `${turnFilesDir}/${SKILLS_DIR}`;
+ const skillsRoot = () => `${SKILLS_ROOT}/${turnSessionDir.split("/").at(-1)}`;
const laidTrees = new Set<string>();
const materializeSkillTree = async (handle: SandboxHandle, r: SkillResolution, sandboxId?: string): Promise<void> => {
- const treeKey = `${sandboxId ?? "default"}:${skillDir(skillsRoot, r)}`;
+ const treeKey = `${sandboxId ?? "default"}:${skillDir(skillsRoot(), r)}`;
if (laidTrees.has(treeKey)) return;
const start = Date.now();
try {
const bundles = r.screenedBundles ?? (deps.skillBundles ? await loadActiveBundles(deps.skillBundles, [r]) : []);
- await laySkillTree(deps.sandbox, handle, skillsRoot, r, bundles);
+ await laySkillTree(deps.sandbox, handle, skillsRoot(), r, bundles);
laidTrees.add(treeKey);
} catch (err) {
deps.errors?.record(
@@ -424,22 +290,24 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
return false;
}
})?.content;
- let content: string | undefined;
- if (file === "SKILL.md") content = renderSkillBody(resolution, shipsFiles ? skillsRoot : undefined);
- else if (asset !== undefined) content = rehomeSkillPaths(resolution, asset, skillsRoot);
- if (content === undefined) return missing;
+ if (file !== "SKILL.md" && asset === undefined) return missing;
if (deps.skills)
void deps.skills.recordUse(resolution.skill.id).catch((e) => swallow("orchestrator: skill recordUse", e));
- if (!shipsFiles) return { content, sourceScopeId: resolution.skill.scopeId };
+ const render = (root?: string) =>
+ file === "SKILL.md"
+ ? renderSkillBody(resolution, root)
+ : rehomeSkillPaths(resolution, asset!, root ?? skillsRoot());
+ if (!shipsFiles) return { content: render(), sourceScopeId: resolution.skill.scopeId };
const access = sandboxId ? await accessResource(sandboxId) : undefined;
- if (access?.crossScope) return { content, sourceScopeId: resolution.skill.scopeId };
+ if (access?.crossScope) return { content: render(skillsRoot()), sourceScopeId: resolution.skill.scopeId };
const handle = access ? await provisionResource(access) : await provision();
await materializeSkillTree(handle, resolution, sandboxId);
- const pack = packRoot(skillsRoot, resolution);
+ const root = `${handle.rootDir}/${skillsRoot()}`;
+ const pack = packRoot(root, resolution);
return {
- content,
+ content: render(root),
sourceScopeId: resolution.skill.scopeId,
- dir: skillDir(skillsRoot, resolution),
+ dir: skillDir(root, resolution),
...(pack ? { packDir: pack } : {}),
};
};
@@ -496,7 +364,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
const policyKey = JSON.stringify({ egress, credentialScopeId });
const existing = resourceHandles.get(id);
if (existing && resourcePolicy.get(id) === policyKey) {
- if (credentialScopeId) await prepareCredentials(existing, emitGapWork, credentialScopeId);
return existing;
}
const provisioned = (async () => {
@@ -516,9 +383,7 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
...(egressToken ? { egressToken } : {}),
});
resourcePendingHandles.set(id, handle);
- if (credentialScopeId) await prepareCredentials(handle, emitGapWork, credentialScopeId);
if (!crossScope) {
- await prepareCredentials(handle, emitGapWork);
await prepareTurnFiles(handle);
}
resourceHandles.set(id, handle);
@@ -591,33 +456,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
);
ownerAuthBox.pending = handle;
ownerAuthBox.provisionMs = Date.now() - provisionStart;
- if (deps.keychain && isolateOwnerKeychain) {
- const restoredServices = await materializeDeviceFlowLogins({
- sandbox: deps.sandbox,
- handle,
- keychain: deps.keychain,
- ownerId: actor.id,
- ...(openSpeakerKeychain ? { allOrigins: true } : {}),
- ...(credentialCutoverServices.length ? { excludeServices: credentialCutoverServices } : {}),
- onAnomaly: (service, detail) =>
- deps.errors?.record({
- category: "keychain",
- code: "device_flow_restore_failed",
- message: `${service} (owner-auth box): ${detail}`,
- scopeLabel: scopeId,
- sessionId: session.id,
- }),
- });
- for (const service of restoredServices) {
- deps.auditLog.record({
- at: Date.now(),
- principalId: actor.id,
- action: "keychain.materialize",
- resource: `${service} (owner-auth box)`,
- scopeLabel: scopeId,
- });
- }
- }
ownerAuthBox.handle = handle;
return handle;
})().catch(async (err) => {
@@ -626,18 +464,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
const pendingHandle = ownerAuthBox.pending;
if (pendingHandle) {
try {
- await scrubOwnerAuthHandle(pendingHandle).catch((scrubErr) => {
- deps.errors?.record(
- {
- category: "sandbox",
- code: "owner_auth_scrub_failed",
- message: errMessage(scrubErr),
- scopeLabel: scopeId,
- sessionId: session.id,
- },
- scrubErr,
- );
- });
await destroyEphemeralHandle(pendingHandle);
if (ownerAuthBox.pending === pendingHandle) ownerAuthBox.pending = null;
} catch (cleanupErr) {
@@ -687,7 +513,26 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
}
}
};
- const prepareTurnFiles = async (handle: SandboxHandle): Promise<void> => {
+ const pruneSkills = async (handle: SandboxHandle): Promise<void> => {
+ const visible = (await visibleSkillsForTurn()).flatMap((r) => (r.skill ? [r.skill] : []));
+ const keep = new Set([
+ ...visible.map((skill) => skill.manifest.name),
+ ...visible.flatMap((skill) => (skill.pack ? [`.packs/${skill.pack.packId}`] : [])),
+ ]);
+ const stale = new Set<string>();
+ for (const path of await deps.sandbox.listDir(handle, SKILLS_ROOT)) {
+ const [root, conversation, name, sub] = path.split("/");
+ const key = name === ".packs" ? `${name}/${sub}` : name;
+ if (root === SKILLS_ROOT && conversation && key && !keep.has(key))
+ stale.add(`${SKILLS_ROOT}/${conversation}/${key}`);
+ }
+ await Promise.all(
+ [...stale].map((dir) =>
+ deps.sandbox.removeDir(handle, dir).catch(swallowAs("orchestrator: stale skill cleanup", undefined)),
+ ),
+ );
+ };
+ const clearStaleTurnFiles = async (handle: SandboxHandle): Promise<void> => {
const cutoff = Date.now() - TURN_FILES_MAX_AGE_MS;
const paths = deps.sandbox.removeDirAndList
? await deps.sandbox.removeDirAndList(handle, turnSessionDir, TURN_FILES_DIR)
@@ -709,6 +554,9 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
),
);
};
+ const prepareTurnFiles = async (handle: SandboxHandle): Promise<void> => {
+ await Promise.all([pruneSkills(handle), clearStaleTurnFiles(handle)]);
+ };
const hasLiveProcesses = async (handle: SandboxHandle, fallbackScope: ScopeId): Promise<boolean> => {
if (!deps.processes) return false;
if (!handle.resourceId) return (await deps.processes.liveByScope(fallbackScope)).length > 0;
@@ -749,18 +597,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
const ownerHandle = ownerAuthBox.handle ?? ownerAuthBox.pending;
if (ownerHandle) {
try {
- await scrubOwnerAuthHandle(ownerHandle).catch((scrubErr) => {
- deps.errors?.record(
- {
- category: "sandbox",
- code: "owner_auth_scrub_failed",
- message: errMessage(scrubErr),
- scopeLabel: scopeId,
- sessionId: session.id,
- },
- scrubErr,
- );
- });
await destroyEphemeralHandle(ownerHandle);
ownerAuthBox.handle = null;
ownerAuthBox.pending = null;
@@ -872,7 +708,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {
scratchBox,
ownerAuthBox,
ownerAuthCommand,
- scopedCommand,
provision,
provisionScratch,
accessResource,
diff --git a/src/core/orchestrator/types.ts b/src/core/orchestrator/types.ts
index 0891a82d7..8664fb334 100644
--- a/src/core/orchestrator/types.ts
+++ b/src/core/orchestrator/types.ts
@@ -31,10 +31,8 @@ import type { MonitorStore } from "../../monitors/monitor-store.ts";
import type { CronStore } from "../../cron/cron-store.ts";
import type { WebhookStore } from "../../webhooks/webhook-store.ts";
import type { ConnectorTokenStore, Keychain, ServiceCredentialStore } from "../../credentials/keychain.ts";
-import type { DeviceFlowCutoverStore } from "../../credentials/device-flow-cutover.ts";
import type { FeatureFlagStore } from "../../feature-flags.ts";
import type { CredentialUsageSink } from "../../admin/credential-usage-sink.ts";
-import type { LivenessCache } from "../../credentials/resident-auth.ts";
import type { ConnectorStatusCache } from "../../credentials/connector-status.ts";
import type { ModelGateway } from "../../model/model-gateway.ts";
import type { AuditLog } from "../../audit/audit-log.ts";
@@ -62,7 +60,7 @@ import type { AdvisoryLock } from "../../persistence/advisory-lock.ts";
import type { SkillStore } from "../../skills/skill-store.ts";
import type { OAuthClientResolver } from "../../connectors/oauth.ts";
import type { SkillBundleStore } from "../../skills/skill-bundle-store.ts";
-import type { BrokeredLayerTool, LayerCredentialTool, DeploymentLayerRuntime } from "../../deployment/load-layer.ts";
+import type { BrokeredLayerTool, DeploymentLayerRuntime } from "../../deployment/load-layer.ts";
import type { FileArtifactStore } from "../../files/file-artifact-store.ts";
import type { DeployService } from "../../deploy/deploy-service.ts";
import type { AclStore } from "../../acl/acl-store.ts";
@@ -188,12 +186,10 @@ export interface OrchestratorDeps {
choice: import("../../harness/harness.ts").RuntimeChoice,
purpose?: import("../../resolution/config-store.ts").RuntimePurpose,
) => Promise<string | null>;
- livenessCache?: LivenessCache;
connectorTokens?: ConnectorTokenStore;
connectorStatusCache?: ConnectorStatusCache;
resolveConnectorClient?: OAuthClientResolver;
scratchExec?: boolean;
- deviceFlowCutover?: DeviceFlowCutoverStore;
featureFlags?: FeatureFlagStore;
credentialUsage?: CredentialUsageSink;
keychain?: Keychain;
@@ -206,7 +202,6 @@ export interface OrchestratorDeps {
reachExec?: boolean;
eagerProvision?: boolean;
environments?: EnvironmentStore;
- credentialTools?: readonly LayerCredentialTool[];
layerBrokerFor?: (tool: BrokeredLayerTool) => AwsRoleBroker | undefined;
brokeredTools?: readonly BrokeredLayerTool[];
deploymentLayer?: DeploymentLayerRuntime;
diff --git a/src/credentials/device-flow-cutover.ts b/src/credentials/device-flow-cutover.ts
deleted file mode 100644
index 027f00b9d..000000000
--- a/src/credentials/device-flow-cutover.ts
+++ /dev/null
@@ -1,155 +0,0 @@
-import { orgId as configOrgId } from "../config.ts";
-import type { DurableMap } from "../persistence/durable-map.ts";
-import { scopeId, type ScopeId } from "../types.ts";
-import { randomUUID } from "node:crypto";
-
-export const DEVICE_FLOW_CUTOVER_MODES = ["legacy", "prefer_ephemeral", "ephemeral_only"] as const;
-
-export type DeviceFlowCutoverMode = (typeof DEVICE_FLOW_CUTOVER_MODES)[number];
-
-export interface DeviceFlowCutoverPolicy {
- scopeId: ScopeId;
- service: string;
- mode: DeviceFlowCutoverMode;
- updatedAt: number;
- updatedBy: string;
- resetResident?: boolean;
- resetGeneration?: string;
-}
-
-export interface DeviceFlowCutoverReset {
- generation: string;
-}
-
-export interface DeviceFlowCutoverStore {
- listServices(scope: ScopeId): Promise<string[]>;
- get(scope: ScopeId, service: string): Promise<DeviceFlowCutoverPolicy | null>;
- resolvePolicy(scope: ScopeId, service: string): Promise<DeviceFlowCutoverPolicy | null>;
- resolve(scope: ScopeId, service: string): Promise<DeviceFlowCutoverMode>;
- residentResetGeneration(scope: ScopeId, service: string, computerId?: string): Promise<string | null>;
- markResidentReset(scope: ScopeId, service: string, generation: string, computerId?: string): Promise<void>;
- set(
- scope: ScopeId,
- service: string,
- mode: DeviceFlowCutoverMode,
- updatedBy: string,
- ): Promise<DeviceFlowCutoverPolicy>;
- clear(scope: ScopeId, service: string): Promise<void>;
-}
-
-function normalizedService(service: string): string {
- const normalized = service.trim().toLowerCase();
- if (!normalized) throw new Error("device-flow cutover service must not be empty");
- return normalized;
-}
-
-function policyKey(scope: ScopeId, service: string): string {
- return `${encodeURIComponent(scope)}:${encodeURIComponent(normalizedService(service))}`;
-}
-
-function assertMode(mode: string): asserts mode is DeviceFlowCutoverMode {
- if (!(DEVICE_FLOW_CUTOVER_MODES as readonly string[]).includes(mode)) {
- throw new Error(`invalid device-flow cutover mode: ${mode}`);
- }
-}
-
-export function createDeviceFlowCutoverStore(
- backing: DurableMap<DeviceFlowCutoverPolicy>,
- opts: { now?: () => number; resetId?: () => string; resets?: DurableMap<DeviceFlowCutoverReset> } = {},
-): DeviceFlowCutoverStore {
- const orgScope = scopeId("org", configOrgId());
- const now = opts.now ?? Date.now;
- const resetId = opts.resetId ?? randomUUID;
- const resets = opts.resets;
- const volatileResets = new Map<string, DeviceFlowCutoverReset>();
- const resetKey = (kind: "request" | "complete", scope: ScopeId, service: string): string =>
- `${kind}:${policyKey(scope, service)}`;
- const completionKey = (scope: ScopeId, service: string, computerId?: string): string =>
- `${resetKey("complete", scope, service)}${computerId ? `:${encodeURIComponent(computerId)}` : ""}`;
- const getReset = (key: string): Promise<DeviceFlowCutoverReset | null> =>
- resets ? resets.get(key) : Promise.resolve(volatileResets.get(key) ?? null);
- const putReset = async (key: string, value: DeviceFlowCutoverReset): Promise<void> => {
- if (resets) await resets.put(key, value);
- else volatileResets.set(key, value);
- };
-
- const get = async (scope: ScopeId, service: string): Promise<DeviceFlowCutoverPolicy | null> => {
- const record = await backing.get(policyKey(scope, service));
- if (!record) return null;
- assertMode(record.mode);
- return record;
- };
- const resolvePolicy = async (scope: ScopeId, service: string): Promise<DeviceFlowCutoverPolicy | null> => {
- const exact = await get(scope, service);
- if (exact) return exact;
- return scope === orgScope ? null : get(orgScope, service);
- };
- const resolve = async (scope: ScopeId, service: string): Promise<DeviceFlowCutoverMode> =>
- (await resolvePolicy(scope, service))?.mode ?? "legacy";
-
- return {
- async listServices(scope) {
- const services = new Set(
- (await backing.all())
- .filter((record) => record.scopeId === scope || record.scopeId === orgScope)
- .map((record) => record.service),
- );
- const requests = resets ? await resets.entries() : [...volatileResets.entries()];
- for (const [key] of requests) {
- for (const target of new Set([scope, orgScope])) {
- const prefix = `request:${encodeURIComponent(target)}:`;
- if (key.startsWith(prefix)) services.add(decodeURIComponent(key.slice(prefix.length)));
- }
- }
- return [...services].sort();
- },
- get,
- resolvePolicy,
- resolve,
- async residentResetGeneration(scope, service, computerId) {
- const policy = await resolvePolicy(scope, service);
- if ((policy?.mode ?? "legacy") !== "legacy") return null;
- const [requested, orgRequested] = await Promise.all([
- getReset(resetKey("request", scope, service)),
- scope === orgScope ? null : getReset(resetKey("request", orgScope, service)),
- ]);
- const generation = [
- policy?.resetResident ? policy.resetGeneration : undefined,
- orgRequested?.generation,
- requested?.generation,
- ]
- .filter(Boolean)
- .join("|");
- if (!generation) return null;
- const complete = await getReset(completionKey(scope, service, computerId));
- return complete?.generation === generation ? null : generation;
- },
- async markResidentReset(scope, service, generation, computerId) {
- if (generation) await putReset(completionKey(scope, service, computerId), { generation });
- },
- async set(scope, service, mode, updatedBy) {
- assertMode(mode);
- const normalized = normalizedService(service);
- if (!updatedBy.trim()) throw new Error("device-flow cutover updater must not be empty");
- const previous = await resolve(scope, normalized);
- const record = {
- scopeId: scope,
- service: normalized,
- mode,
- updatedAt: now(),
- updatedBy,
- ...(mode === "legacy" && previous !== "legacy" ? { resetResident: true, resetGeneration: resetId() } : {}),
- };
- await backing.put(policyKey(scope, normalized), record);
- return record;
- },
- async clear(scope, service) {
- const previous = await resolve(scope, service);
- const inherited = scope === orgScope ? "legacy" : ((await get(orgScope, service))?.mode ?? "legacy");
- if (previous !== "legacy" && inherited === "legacy") {
- await putReset(resetKey("request", scope, service), { generation: resetId() });
- }
- await backing.delete(policyKey(scope, service));
- },
- };
-}
diff --git a/src/credentials/device-flow-persist.ts b/src/credentials/device-flow-persist.ts
deleted file mode 100644
index 501c58331..000000000
--- a/src/credentials/device-flow-persist.ts
+++ /dev/null
@@ -1,690 +0,0 @@
-import {
- DEVICE_FLOW_ORIGIN,
- KeychainError,
- fileCredentialFingerprint,
- restoredFileMode,
- type CredentialFile,
- type Keychain,
-} from "./keychain.ts";
-import { errMessage } from "../util/errors.ts";
-import { pathUnder } from "../util/paths.ts";
-import type { Sandbox, SandboxHandle } from "../sandbox/sandbox.ts";
-import { makeTar, parseTar } from "../sandbox/tar.ts";
-import { scopeId, type ScopeId } from "../types.ts";
-import { shq } from "../util/shell.ts";
-import { hashId } from "../util/crypto.ts";
-import { DISPLACED_DIR_REL, EPHEMERAL_CRED_DIR } from "./resident-paths.ts";
-import {
- CREDENTIAL_PATH_RE,
- builtInCredentialPaths,
- captureRootForTarget,
- configCredentialDirs,
- credentialServiceForPath,
- expandServiceAliases,
- type CredentialPathSpec,
-} from "./resident-paths.ts";
-import { credentialPathError } from "../deployment/deployment-layer.ts";
-import { homeRelativePath } from "./paths.ts";
-
-const CRED_DOTDIRS = [
- ".aws",
- ".ssh",
- ".docker",
- ".kube",
- ".azure",
- ".gnupg",
- ".fly",
- ".terraform.d",
- ".pulumi",
- ".railway",
- ".oci",
- ".m2",
- ".cargo",
- ".gem",
-];
-const CRED_DOTFILES = [
- ".netrc",
- ".git-credentials",
- ".pgpass",
- ".npmrc",
- ".terraformrc",
- ".pypirc",
- ".databrickscfg",
- ".my.cnf",
-];
-const PRUNE_NAMES = ["logs", ".cache", "__pycache__", "node_modules"];
-const PRUNE_PATHS = ["*/gcloud/cache", "*/gcloud/logs"];
-const MAX_SERVICE_BYTES = 4 * 1024 * 1024;
-const MAX_SERVICE_FILES = 500;
-const MAX_FILE_BYTES = 1024 * 1024;
-const MAX_REGISTERED_ROOTS = 64;
-
-export { DEVICE_FLOW_ORIGIN };
-
-export function deviceFlowCredOwner(computerScopeId: ScopeId, actorId: string): string {
- return computerScopeId === scopeId("personal", actorId) ? actorId : computerScopeId;
-}
-
-function fixedCoverage(credentialPaths: readonly CredentialPathSpec[]): { dirs: Set<string>; files: Set<string> } {
- const dirs = new Set<string>([...CRED_DOTDIRS, ...configCredentialDirs()]);
- const files = new Set<string>(CRED_DOTFILES);
- for (const entry of credentialPaths) {
- if (entry.kind === "directory") dirs.add(entry.path);
- else files.add(entry.path);
- }
- return { dirs, files };
-}
-
-function isCovered(root: CredentialPathSpec, fixed: { dirs: Set<string>; files: Set<string> }): boolean {
- if ([...fixed.dirs].some((d) => pathUnder(root.path, d))) return true;
- return root.kind === "file" && fixed.files.has(root.path);
-}
-
-const pruneExpr = (): string =>
- `\\( ${[...PRUNE_NAMES.map((n) => `-name ${shq(n)}`), ...PRUNE_PATHS.map((p) => `-path ${shq(p)}`)].join(" -o ")} \\) -prune`;
-
-interface SweepGroup {
- service: string;
- dirs: string[];
- files: string[];
-}
-
-const SWEEP_PATH_RE = CREDENTIAL_PATH_RE;
-const SERVICE_NAME_RE = /^[a-z0-9][a-z0-9._-]{0,63}$/;
-
-const CRED_WORK_DIR = DISPLACED_DIR_REL;
-const CAPTURE_STATE_REL = ".cred-state";
-const credTransientReaper = (excludeGlob?: string): string =>
- `find "$HOME/${CRED_WORK_DIR}" -maxdepth 1 -name '.cred-*'${excludeGlob ? ` ! -name ${shq(excludeGlob)}` : ""} -mmin +60 -exec rm -rf {} + 2>/dev/null; ` +
- `find "$HOME" -maxdepth 1 \\( -name '.agent-cred-*' -o \\( -name '${CAPTURE_STATE_REL}.w.*' -mmin +60 \\) \\) -exec rm -rf {} + 2>/dev/null`;
-const ensureCredWorkDir = `{ mkdir -p "$HOME/${CRED_WORK_DIR}" 2>/dev/null && [ ! -L "$HOME/${CRED_WORK_DIR}" ] && [ -d "$HOME/${CRED_WORK_DIR}" ]; }`;
-
-const groupHashLines = (group: SweepGroup): string[] => [
- `h=""`,
- ...group.dirs.map((d) => `[ -d ${shq(d)} ] && h="$h $(qmhd ${shq(d)})"`),
- ...group.files.map((f) => `[ -f ${shq(f)} ] && h="$h $(qmhf ${shq(f)})"`),
-];
-
-const captureScript = (groups: readonly SweepGroup[], gate: boolean): string => {
- const size = `-size -${MAX_FILE_BYTES}c`;
- const st = `"$HOME/${CAPTURE_STATE_REL}"`;
- const lines: string[] = [
- `if command -v sha256sum >/dev/null 2>&1; then QSH="sha256sum"; elif command -v shasum >/dev/null 2>&1; then QSH="shasum -a 256"; else QSH="cksum"; fi`,
- `if stat -c '%a' / >/dev/null 2>&1; then QST="stat -c %a:%n"; else QST="stat -f %Lp:%N"; fi`,
- `qmsha() { $QSH | cut -d" " -f1; }`,
- `qmhd() { ql=$(mktemp "$HOME/${CRED_WORK_DIR}/.cred-list.XXXXXX") || { printf 'ERR-%s-%s' "$$" "$(date +%s)"; return 0; }; ` +
- `find -L "$1" ${pruneExpr()} -o -type f ${size} -print0 2>/dev/null | LC_ALL=C sort -z > "$ql"; ` +
- `[ -s "$ql" ] || { rm -f "$ql"; return 0; }; ` +
- `{ xargs -0 $QSH < "$ql" 2>/dev/null; xargs -0 $QST < "$ql" 2>/dev/null; } | qmsha; rm -f "$ql"; }`,
- `qmhf() { [ -n "$(find -L "$1" -maxdepth 0 -type f ${size} 2>/dev/null)" ] || return 0; { find -L "$1" -maxdepth 0 -type f ${size} -print0 2>/dev/null | xargs -0 $QSH 2>/dev/null; $QST "$1" 2>/dev/null; } | qmsha; }`,
- `cd "$HOME" 2>/dev/null || { printf 'NOHOME\\tcd\\n'; exit 0; }`,
- `${ensureCredWorkDir} || { printf 'NOHOME\\tworkdir\\n'; exit 0; }`,
- `${credTransientReaper()}`,
- `grep '^!' ${st} 2>/dev/null | while IFS= read -r n; do printf 'ANOTE\\t%s\\n' "$n"; done; :`,
- `t=$(mktemp "$HOME/${CRED_WORK_DIR}/.cred-capture.XXXXXX") || { printf 'NOHOME\\tmktemp\\n'; exit 0; }`,
- `set --`,
- `cf=""`,
- ];
- groups.forEach((group, i) => {
- const svc = shq(group.service);
- lines.push(
- ...groupHashLines(group),
- `hs=$(printf '%s' "$h" | tr -d ' ')`,
- `if [ -n "$hs" ]; then printf 'PRE\\t%s\\t%s\\n' ${svc} "$h"; ` +
- (gate
- ? `if grep -qxF "${group.service}\t$h" ${st} 2>/dev/null; then printf 'KEEP\\t%s\\n' ${svc}; else `
- : "") +
- `printf 'SHIP\\t%s\\n' ${svc}; s${i}=1; p${i}="$h"; ` +
- group.dirs.map((d) => `{ [ -d ${shq(d)} ] && set -- "$@" ${shq(d)}; }; `).join("") +
- group.files.map((f) => `{ [ -f ${shq(f)} ] && cf="$cf ${f}"; }; `).join("") +
- (gate ? `fi; ` : "") +
- `fi`,
- );
- });
- lines.push(
- `{ [ "$#" -gt 0 ] && find -L "$@" ${pruneExpr()} -o -type f ${size} -print0; ` +
- `[ -n "$cf" ] && find -L $cf -maxdepth 0 -type f ${size} -print0; :; } ` +
- `2>/dev/null | LC_ALL=C sort -z | tar -h --null -T - -cf "$t" 2>/dev/null`,
- `[ -e "$t" ] || : > "$t"`,
- );
- groups.forEach((group, i) => {
- lines.push(
- `if [ -n "$s${i}" ]; then ${groupHashLines(group).join("; ")}; ` +
- `[ "$h" = "$p${i}" ] || printf 'VOLATILE\\t%s\\n' ${shq(group.service)}; fi`,
- );
- });
- lines.push(`printf 'TMP\\t%s\\n' "$t"`);
- return lines.join("\n");
-};
-
-export interface DeviceFlowPersistInput {
- sandbox: Sandbox;
- handle: SandboxHandle;
- keychain: Keychain;
- ownerId: string;
- onAnomaly?: (service: string, detail: string) => void;
- credentialPaths?: CredentialPathSpec[];
- groupAs?: { service: string; paths: readonly CredentialPathSpec[] };
- services?: readonly string[];
- excludeServices?: readonly string[];
-}
-
-function serviceSelected(
- input: Pick<DeviceFlowPersistInput, "services" | "excludeServices">,
- service: string,
-): boolean {
- if (input.services && !expandServiceAliases(input.services).includes(service)) return false;
- return !input.excludeServices || !expandServiceAliases(input.excludeServices).includes(service);
-}
-
-function rootedAt(handle: SandboxHandle, dir: string): SandboxHandle {
- return { ...handle, rootDir: dir };
-}
-function homeOf(handle: SandboxHandle): string {
- return handle.homeDir ?? "/root";
-}
-
-interface CapturedRecordShape {
- service: string;
- origin?: string;
- targets?: string[];
- target?: string;
- capturePaths?: CredentialPathSpec[];
-}
-
-function recordCaptureRoots(
- rec: Pick<CapturedRecordShape, "targets" | "target" | "capturePaths">,
-): CredentialPathSpec[] {
- const derived = (rec.targets ?? (rec.target ? [rec.target] : []))
- .map(captureRootForTarget)
- .filter((r): r is CredentialPathSpec => r !== undefined);
- return [...new Map([...(rec.capturePaths ?? []), ...derived].map((r) => [r.path, r] as const)).values()];
-}
-
-function buildSweepGroups(
- input: DeviceFlowPersistInput,
- existing: ReadonlyMap<string, CapturedRecordShape>,
-): { groups: SweepGroup[]; droppedServices: string[]; deferredAnomalies: Array<[string, string]> } {
- const groups = new Map<string, SweepGroup>();
- const deferredAnomalies: Array<[string, string]> = [];
- const addRoot = (service: string, root: CredentialPathSpec): void => {
- if (!serviceSelected(input, service)) return;
- if (!SERVICE_NAME_RE.test(service)) {
- deferredAnomalies.push([service.slice(0, 64), "service name cannot ride the capture sweep — not swept"]);
- return;
- }
- if (!SWEEP_PATH_RE.test(root.path)) {
- deferredAnomalies.push([
- service,
- `path ${JSON.stringify(root.path)} has characters the sweep cannot carry — not swept`,
- ]);
- return;
- }
- const group = groups.get(service) ?? { service, dirs: [], files: [] };
- const list = root.kind === "directory" ? group.dirs : group.files;
- if (!list.includes(root.path)) list.push(root.path);
- groups.set(service, group);
- };
- for (const dir of [...CRED_DOTDIRS, ...configCredentialDirs()]) {
- const service = credentialServiceForPath(dir);
- if (service) addRoot(service, { path: dir, kind: "directory" });
- }
- for (const file of CRED_DOTFILES) {
- const service = credentialServiceForPath(file);
- if (service) addRoot(service, { path: file, kind: "file" });
- }
- for (const entry of input.credentialPaths ?? []) {
- const service = credentialServiceForPath(entry.path);
- if (service) addRoot(service, entry);
- }
- if (input.groupAs) for (const entry of input.groupAs.paths) addRoot(input.groupAs.service, entry);
-
- const fixed = fixedCoverage(input.credentialPaths ?? []);
- const droppedServices: string[] = [];
- let registeredCount = 0;
- for (const rec of [...existing.values()].sort((a, b) => a.service.localeCompare(b.service))) {
- if (rec.origin !== DEVICE_FLOW_ORIGIN || !serviceSelected(input, rec.service)) continue;
- const uncovered = recordCaptureRoots(rec).filter((r) => !isCovered(r, fixed));
- const roots = uncovered.filter((r) => {
- if (SWEEP_PATH_RE.test(r.path)) return true;
- deferredAnomalies.push([
- rec.service,
- `stored path ${JSON.stringify(r.path)} has characters the sweep cannot carry — not swept`,
- ]);
- return false;
- });
- if (!roots.length) continue;
- if (registeredCount + roots.length > MAX_REGISTERED_ROOTS) {
- droppedServices.push(rec.service);
- continue;
- }
- registeredCount += roots.length;
- for (const root of roots) addRoot(rec.service, root);
- }
-
- const ordered = [...groups.values()].sort((a, b) => a.service.localeCompare(b.service));
- const claimed = new Map<string, string>();
- for (const group of ordered) {
- for (const root of [...group.dirs, ...group.files]) if (!claimed.has(root)) claimed.set(root, group.service);
- }
- for (const group of ordered) {
- const foreign = (p: string): boolean => {
- const owner = [...claimed].find(([root, service]) => service !== group.service && pathUnder(p, root));
- if (!owner) return false;
- deferredAnomalies.push([
- group.service,
- `path ${p} overlaps a root owned by ${owner[1]} — swept under ${owner[1]}, not ${group.service}`,
- ]);
- return true;
- };
- group.dirs = group.dirs.filter((d) => !foreign(d) && !group.dirs.some((o) => o !== d && pathUnder(d, o)));
- group.files = group.files.filter((f) => !foreign(f) && !group.dirs.some((d) => pathUnder(f, d)));
- }
- return { groups: ordered.filter((g) => g.dirs.length || g.files.length), droppedServices, deferredAnomalies };
-}
-
-export async function captureDeviceFlowLogins(input: DeviceFlowPersistInput): Promise<string[]> {
- const existing = new Map(
- (await input.keychain.listByOwner(input.ownerId)).filter((c) => c.kind === "file").map((c) => [c.service, c]),
- );
- const { groups, droppedServices, deferredAnomalies } = buildSweepGroups(input, existing);
- if (droppedServices.length) {
- input.onAnomaly?.(
- "capture-sweep",
- `${droppedServices.length} services past the ${MAX_REGISTERED_ROOTS}-root sweep cap were not captured: ${droppedServices.join(", ")}`,
- );
- }
- const droppedSet = new Set(droppedServices);
- const gate = !input.groupAs && !input.services;
- const home = homeOf(input.handle);
- const homeHandle = rootedAt(input.handle, home);
- const token = `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
- const scriptRel = `${CRED_WORK_DIR}/.cred-capture-${token}.sh`;
- await input.sandbox.writeFileBytes(homeHandle, scriptRel, Buffer.from(captureScript(groups, gate), "utf8"));
-
- let tmpRel = "";
- const pre = new Map<string, string>();
- const kept = new Set<string>();
- const shipped = new Set<string>();
- const volatile = new Set<string>();
- const priorNotes = new Set<string>();
- const cleanup = async (): Promise<void> => {
- await input.sandbox
- .run(
- input.handle,
- `cd "$HOME" 2>/dev/null || exit 0; rm -f ${shq(scriptRel)}${tmpRel ? ` ${shq(tmpRel)}` : ""}; :`,
- {
- timeoutMs: 30_000,
- },
- )
- .catch(() => {});
- };
- const tar = await (async () => {
- try {
- const built = await input.sandbox.run(input.handle, `sh "$HOME/${scriptRel}"`, { timeoutMs: 120_000 });
- if (built.code !== 0)
- throw new Error(`device-flow credential read failed: ${built.stderr || `exit ${built.code}`}`);
- let tmpAbs = "";
- let noHome = "";
- for (const line of built.stdout.split("\n")) {
- const [tag, a, b] = line.split("\t");
- if (tag === "PRE" && a && b !== undefined) pre.set(a, b);
- else if (tag === "KEEP" && a) kept.add(a);
- else if (tag === "SHIP" && a) shipped.add(a);
- else if (tag === "VOLATILE" && a) volatile.add(a);
- else if (tag === "TMP" && a) tmpAbs = a.trim();
- else if (tag === "ANOTE" && a !== undefined) priorNotes.add(a);
- else if (tag === "NOHOME") noHome = a || "unknown";
- }
- if (noHome === "cd") return null;
- if (noHome) throw new Error(`device-flow credential read failed: capture workspace unavailable (${noHome})`);
- if (!tmpAbs) throw new Error("device-flow credential read failed: capture produced no archive");
- tmpRel = `${CRED_WORK_DIR}/${tmpAbs.slice(tmpAbs.lastIndexOf("/") + 1)}`;
- const read = await input.sandbox.readFileBytes(homeHandle, tmpRel);
- if (read === null) throw new Error("device-flow credential read failed: capture tar unreadable");
- return read;
- } catch (err) {
- await cleanup();
- throw err;
- }
- })();
- if (tar === null) {
- await cleanup();
- return [];
- }
- for (const service of volatile) {
- input.onAnomaly?.(service, "files changed while being captured — skipped this turn, retrying next turn");
- }
- const noteSig = ([service, detail]: [string, string]): string => `!${hashId([service, detail])}`;
- const emitDeferred = (): void => {
- for (const entry of deferredAnomalies) {
- if (!priorNotes.has(noteSig(entry))) input.onAnomaly?.(entry[0], entry[1]);
- }
- };
-
- const saved: string[] = [];
- try {
- const groupFor = (path: string): string | undefined =>
- groups.find((g) => g.dirs.some((d) => pathUnder(path, d)) || g.files.some((f) => pathUnder(path, f)))?.service;
- const byService = new Map<string, CredentialFile[]>();
- for (const entry of await parseTar(tar)) {
- const path = entry.path.replace(/^\.\//, "");
- if (entry.data.length >= MAX_FILE_BYTES) continue;
- const service = groupFor(path) ?? credentialServiceForPath(path);
- if (!service || !serviceSelected(input, service)) continue;
- if (/[\n\r]/.test(path)) {
- input.onAnomaly?.(service, `captured file name contains a line break — dropped: ${JSON.stringify(path)}`);
- continue;
- }
- const files = byService.get(service) ?? [];
- files.push({
- path,
- contentBase64: entry.data.toString("base64"),
- ...(entry.mode !== undefined ? { mode: entry.mode } : {}),
- });
- byService.set(service, files);
- }
-
- const resolved = new Set<string>();
- for (const [service, files] of byService) {
- if (droppedSet.has(service) || volatile.has(service)) continue;
- const bytes = files.reduce((n, f) => n + Math.floor((f.contentBase64.length * 3) / 4), 0);
- if (files.length > MAX_SERVICE_FILES || bytes > MAX_SERVICE_BYTES) {
- input.onAnomaly?.(service, `${files.length} files / ${bytes} bytes exceeds capture caps`);
- resolved.add(service);
- continue;
- }
- files.sort((a, b) => a.path.localeCompare(b.path));
- if (existing.get(service)?.fingerprint === fileCredentialFingerprint(files)) {
- resolved.add(service);
- continue;
- }
- try {
- await input.keychain.save({
- ownerId: input.ownerId,
- service,
- files,
- origin: DEVICE_FLOW_ORIGIN,
- expectedOrigin: DEVICE_FLOW_ORIGIN,
- });
- } catch (e) {
- if (e instanceof KeychainError && e.status === 409) {
- resolved.add(service);
- continue;
- }
- if (e instanceof KeychainError && e.status === 503) {
- input.onAnomaly?.(service, "keychain write raced a concurrent save — retrying next turn");
- continue;
- }
- throw e;
- }
- resolved.add(service);
- saved.push(service);
- }
- for (const service of shipped) {
- if (byService.has(service) || volatile.has(service) || droppedSet.has(service)) continue;
- deferredAnomalies.push([service, "shipped by the sweep but produced no capturable files"]);
- }
-
- emitDeferred();
- if (gate) {
- const committed = [...pre]
- .filter(
- ([service, hash]) =>
- !hash.includes("ERR-") && (kept.has(service) || (shipped.has(service) && resolved.has(service))),
- )
- .map(([service, hash]) => `${service}\t${hash}\n`)
- .join("")
- .concat(deferredAnomalies.map((entry) => `${noteSig(entry)}\n`).join(""));
- try {
- const stateWriteRel = `${CAPTURE_STATE_REL}.w.${hashId([committed, String(Date.now()), String(Math.random())]).slice(0, 12)}`;
- await input.sandbox.writeFileBytes(homeHandle, stateWriteRel, Buffer.from(committed, "utf8"));
- const commit = await input.sandbox.run(
- input.handle,
- `cd "$HOME" 2>/dev/null || exit 1; mv -f ${shq(stateWriteRel)} ${shq(CAPTURE_STATE_REL)} && rm -f ${shq(scriptRel)} ${shq(tmpRel)}`,
- { timeoutMs: 30_000 },
- );
- if (commit.code !== 0) throw new Error(commit.stderr || `exit ${commit.code}`);
- } catch (e) {
- input.onAnomaly?.("capture-state", `state commit failed — next turn re-ships everything: ${errMessage(e)}`);
- await cleanup();
- }
- } else {
- await cleanup();
- }
- } catch (err) {
- await cleanup();
- throw err;
- }
- return saved;
-}
-
-const MAX_REGISTER_PATHS = 16;
-
-export interface RegisterLoginInput {
- sandbox: Sandbox;
- handle: SandboxHandle;
- keychain: Keychain;
- ownerId: string;
- service: string;
- paths: CredentialPathSpec[];
- onAnomaly?: (service: string, detail: string) => void;
-}
-
-export async function registerLoginPaths(input: RegisterLoginInput): Promise<{ service: string; captured: boolean }> {
- const service = input.service.trim().toLowerCase();
- if (!SERVICE_NAME_RE.test(service))
- throw new KeychainError(400, `invalid service name ${JSON.stringify(input.service)}`);
- if (!input.paths.length) throw new KeychainError(400, "register_login needs at least one path");
- if (input.paths.length > MAX_REGISTER_PATHS)
- throw new KeychainError(400, `register_login accepts at most ${MAX_REGISTER_PATHS} paths`);
- const records = (await input.keychain.listByOwner(input.ownerId)).filter((c) => c.kind === "file");
- const normalized: CredentialPathSpec[] = [];
- for (const entry of input.paths) {
- let path: string;
- try {
- path = homeRelativePath(entry.path);
- } catch {
- throw new KeychainError(400, `path ${JSON.stringify(entry.path)} must be under $HOME with no traversal`);
- }
- const err = credentialPathError(path, entry.kind);
- if (err) throw new KeychainError(400, err);
- if (path === CAPTURE_STATE_REL || path.startsWith(`${CAPTURE_STATE_REL}.`) || pathUnder(path, CRED_WORK_DIR))
- throw new KeychainError(400, `path ${JSON.stringify(path)} is capture bookkeeping, not a credential`);
- const coveredDir = [...builtInCredentialPaths().map((b) => b.path), ...CRED_DOTDIRS].find(
- (base) => pathUnder(path, base) || pathUnder(base, path),
- );
- if (coveredDir || CRED_DOTFILES.includes(path)) {
- const autoService = credentialServiceForPath(path);
- if (autoService && records.some((r) => expandServiceAliases([autoService]).includes(r.service)))
- throw new KeychainError(
- 409,
- `path ${JSON.stringify(path)} is already captured automatically as ${JSON.stringify(autoService)} — no registration needed`,
- );
- }
- normalized.push({ path, kind: entry.kind });
- }
-
- const existing = records.find((c) => c.service === service);
- if (existing && existing.origin !== DEVICE_FLOW_ORIGIN)
- throw new KeychainError(409, `a credential for ${service} already exists and was not created by a login capture`);
- for (const other of records) {
- if (other.service === service) continue;
- const otherRoots = recordCaptureRoots(other).map((r) => r.path);
- for (const entry of normalized) {
- const clash = otherRoots.find((root) => pathUnder(entry.path, root) || pathUnder(root, entry.path));
- if (clash)
- throw new KeychainError(
- 409,
- `path ${JSON.stringify(entry.path)} overlaps ${JSON.stringify(clash)}, already captured for ${other.service}`,
- );
- }
- }
-
- const check = await input.sandbox.run(
- input.handle,
- `cd "$HOME" 2>/dev/null || exit 3; hp=$(pwd -P); miss=1; for p in ${normalized.map((r) => shq(r.path)).join(" ")}; do ` +
- `rp=$(cd "$(dirname -- "$p")" 2>/dev/null && pwd -P) || continue; ` +
- `case "$rp/" in "$hp"/*|"$hp"/) ;; *) exit 4;; esac; ` +
- `[ -e "$p" ] && miss=0; done; exit $miss`,
- { timeoutMs: 30_000 },
- );
- if (check.code === 4) throw new KeychainError(400, "a registered path escapes $HOME via a symlink");
- if (check.code !== 0) throw new KeychainError(404, `nothing to capture at the registered paths for ${service}`);
-
- const anomalies: string[] = [];
- const saved = await captureDeviceFlowLogins({
- sandbox: input.sandbox,
- handle: input.handle,
- keychain: input.keychain,
- ownerId: input.ownerId,
- services: [service],
- credentialPaths: normalized,
- groupAs: { service, paths: normalized },
- onAnomaly: (svc, detail) => {
- anomalies.push(`${svc}: ${detail}`);
- input.onAnomaly?.(svc, detail);
- },
- });
- const recorded = await input.keychain.setCapturePaths(input.ownerId, service, normalized, DEVICE_FLOW_ORIGIN);
- if (!recorded)
- throw new KeychainError(
- 404,
- `nothing was captured at the registered paths for ${service}${anomalies.length ? ` (${anomalies.join("; ")})` : " — is the login complete?"}`,
- );
- return { service, captured: saved.includes(service) };
-}
-
-export async function materializeDeviceFlowLogins(
- input: DeviceFlowPersistInput & { allOrigins?: boolean },
-): Promise<string[]> {
- const bundles = (await input.keychain.materializeOwnFiles(input.ownerId)).filter(
- (b) => (input.allOrigins === true || b.origin === DEVICE_FLOW_ORIGIN) && serviceSelected(input, b.service),
- );
- if (!bundles.length) return [];
-
- const candidates = [...new Set(bundles.flatMap((b) => b.files.map((f) => f.path)))];
-
- const home = homeOf(input.handle);
- const probe = await input.sandbox.run(
- input.handle,
- `cd "$HOME" 2>/dev/null || exit 0; for p in ${candidates.map(shq).join(" ")}; do [ -e "$p" ] && printf '%s\\0' "$p"; done; :`,
- { timeoutMs: 60_000 },
- );
- if (probe.code !== 0)
- throw new Error(`device-flow credential restore failed: ${probe.stderr || `exit ${probe.code}`}`);
- const present = new Set(probe.stdout.split("\0").filter(Boolean));
-
- const toRestore = bundles
- .map((bundle) => ({
- service: bundle.service,
- files: bundle.files.filter((f) => {
- if (present.has(f.path)) return false;
- if (f.path.includes("\n")) {
- input.onAnomaly?.(bundle.service, `stored path with a newline cannot be restored: ${JSON.stringify(f.path)}`);
- return false;
- }
- return true;
- }),
- }))
- .filter((bundle) => bundle.files.length);
- if (!toRestore.length) return [];
-
- const tarBuf = await makeTar(
- toRestore.flatMap((bundle) =>
- bundle.files.map((f) => ({
- path: f.path,
- data: Buffer.from(f.contentBase64, "base64"),
- mode: restoredFileMode(f.mode),
- })),
- ),
- );
- const token = `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
- const blobRel = `${CRED_WORK_DIR}/.cred-restore-${token}.tar`;
- const scriptRel = `${CRED_WORK_DIR}/.cred-restore-${token}.sh`;
- const homeHandle = rootedAt(input.handle, home);
- try {
- await input.sandbox.writeFileBytes(homeHandle, blobRel, tarBuf);
-
- const bundleBlocks = toRestore.map(({ service, files }) =>
- [
- `mvd=$(mktemp "$stage/.moved.XXXXXX") || mvd=""`,
- `ok=1`,
- `moved=""`,
- `if [ -z "$mvd" ]; then printf 'FAIL\\t%s\\n' ${shq(service)}; ok=""; else ` +
- `for p in ${files.map((f) => shq(f.path)).join(" ")}; do ` +
- `[ -e "$p" ] && continue; [ -f "$stage/$p" ] || continue; ` +
- `if mkdir -p "$(dirname -- "$p")" && mv "$stage/$p" "$p"; then printf '%s\\n' "$p" >> "$mvd"; moved=1; ` +
- `else while IFS= read -r q; do rm -f "$q"; done < "$mvd"; printf 'FAIL\\t%s\\n' ${shq(service)}; ok=""; break; fi; done; ` +
- `rm -f "$mvd"; fi`,
- `[ -n "$ok" ] && [ -n "$moved" ] && printf 'OK\\t%s\\n' ${shq(service)}`,
- ].join("; "),
- );
- const restoreCmd = [
- `cd "$HOME" 2>/dev/null || exit 9`,
- `${ensureCredWorkDir} || exit 9`,
- `${credTransientReaper(`.cred-restore-${token}*`)}`,
- `stage=$(mktemp -d "$HOME/${CRED_WORK_DIR}/.cred-stage.XXXXXX") || exit 9`,
- `trap 'rm -rf "$stage"' EXIT`,
- `tar -xpf ${shq(blobRel)} -C "$stage" 2>/dev/null || { rm -rf "$stage" ${shq(blobRel)}; exit 9; }`,
- ...bundleBlocks,
- `rm -rf "$stage" ${shq(blobRel)}`,
- `:`,
- ].join("\n");
- await input.sandbox.writeFileBytes(homeHandle, scriptRel, Buffer.from(restoreCmd, "utf8"));
- const run = await input.sandbox.run(
- input.handle,
- `sh "$HOME/${scriptRel}"; rc=$?; rm -f "$HOME/${scriptRel}"; exit $rc`,
- { timeoutMs: 120_000 },
- );
- if (run.code !== 0) throw new Error(`device-flow credential restore failed: ${run.stderr || `exit ${run.code}`}`);
- const restored: string[] = [];
- for (const line of run.stdout.split("\n")) {
- const [tag, service] = line.split("\t");
- if (tag === "OK" && service) restored.push(service);
- else if (tag === "FAIL" && service)
- input.onAnomaly?.(service, "restore failed mid-bundle — that bundle's files were rolled back");
- }
- return restored;
- } catch (err) {
- await input.sandbox
- .run(input.handle, `cd "$HOME" 2>/dev/null || exit 0; rm -rf ${shq(blobRel)} ${shq(scriptRel)}; :`, {
- timeoutMs: 30_000,
- })
- .catch(() => {});
- throw err;
- }
-}
-
-export async function removeDeviceFlowLogins(
- input: Pick<DeviceFlowPersistInput, "sandbox" | "handle" | "keychain" | "ownerId" | "services"> & {
- allOrigins?: boolean;
- canonicalRoots?: readonly string[];
- },
-): Promise<string[]> {
- if (!input.services?.length) return [];
- const services = new Set(expandServiceAliases(input.services));
- const records = (await input.keychain.listByOwner(input.ownerId)).filter(
- (record) =>
- record.kind === "file" &&
- (input.allOrigins || record.origin === DEVICE_FLOW_ORIGIN) &&
- services.has(record.service),
- );
- const canonicalRoots = [...(input.canonicalRoots ?? [])];
- const paths = [
- ...new Set([
- ...canonicalRoots,
- ...records.flatMap((record) => record.targets ?? (record.target ? [record.target] : [])),
- ]),
- ];
- if (!paths.length) return [];
- const removed = await input.sandbox.run(
- input.handle,
- `cd "$HOME" 2>/dev/null || exit 0; for p in ${canonicalRoots.map(shq).join(" ")}; do if [ -L "$p" ]; then target="$(readlink -- "$p" 2>/dev/null || true)"; case "$target" in ${shq(`${EPHEMERAL_CRED_DIR}/`)}*) rm -rf -- "$target" ;; esac; fi; done; rm -rf -- ${paths.map(shq).join(" ")}`,
- { timeoutMs: 60_000 },
- );
- if (removed.code !== 0) {
- throw new Error(`device-flow credential quarantine failed: ${removed.stderr || `exit ${removed.code}`}`);
- }
- return paths;
-}
diff --git a/src/credentials/execute-files.ts b/src/credentials/execute-files.ts
new file mode 100644
index 000000000..1848dbfee
--- /dev/null
+++ b/src/credentials/execute-files.ts
@@ -0,0 +1,236 @@
+import type { Sandbox, SandboxHandle } from "../sandbox/sandbox.ts";
+import type { ProcessRegistry } from "../processes/process-registry.ts";
+import { parseTar } from "../sandbox/tar.ts";
+import { shq } from "../util/shell.ts";
+import { errMessage, withCleanup } from "../util/errors.ts";
+import { pathUnder } from "../util/paths.ts";
+import { homeRelativePath } from "./paths.ts";
+import { builtInCredentialPaths, type CredentialPathSpec } from "./resident-paths.ts";
+import {
+ credentialFilesFingerprint,
+ fileCredentialEnvironment,
+ KeychainError,
+ MAX_CREDENTIAL_BYTES,
+ MAX_CREDENTIAL_FILES,
+ restoredFileMode,
+ type CredentialFile,
+ type FileCredentialSource,
+ type Keychain,
+} from "./keychain.ts";
+
+export interface ExecutionFileCredential {
+ files: CredentialFile[];
+ save(files: CredentialFile[]): Promise<void>;
+ source?: FileCredentialSource;
+}
+
+export interface ExecutionFilePlan {
+ directory: string;
+ credentials: Array<{ paths: string[]; roots: CredentialPathSpec[] }>;
+}
+
+export interface ProcessCredentialFiles {
+ plan: ExecutionFilePlan;
+ sources: Array<FileCredentialSource & { fingerprint: string }>;
+}
+
+export function processCredentialFiles(
+ plan: ExecutionFilePlan,
+ credentials: readonly ExecutionFileCredential[],
+): ProcessCredentialFiles {
+ return {
+ plan,
+ sources: credentials.map((credential) => {
+ if (!credential.source) throw new Error("Background credentials need a keychain source");
+ return { ...credential.source, fingerprint: credentialFilesFingerprint(credential.files) };
+ }),
+ };
+}
+
+export async function finishProcessCredentials(
+ deps: {
+ sandbox: Sandbox;
+ processes: Pick<ProcessRegistry, "credentialFiles" | "setCredentialFiles">;
+ keychain?: Pick<Keychain, "writebackBaseline" | "updateFiles">;
+ },
+ handle: SandboxHandle,
+ processId: string,
+): Promise<void> {
+ const stored = await deps.processes.credentialFiles(processId);
+ if (!stored) return;
+ const keychain = deps.keychain;
+ await finishExecutionFiles(
+ deps.sandbox,
+ handle,
+ stored.plan,
+ stored.sources.map(({ fingerprint, ...source }) => async (files: CredentialFile[]) => {
+ if (!keychain) throw new Error("Refreshed credentials cannot be saved without a keychain");
+ await keychain.updateFiles(await keychain.writebackBaseline(source, fingerprint), files);
+ }),
+ (retry) =>
+ deps.processes.setCredentialFiles(
+ processId,
+ retry.length
+ ? {
+ plan: { ...stored.plan, credentials: retry.map((index) => stored.plan.credentials[index]!) },
+ sources: retry.map((index) => stored.sources[index]!),
+ }
+ : null,
+ ),
+ );
+}
+
+async function runOrThrow(sandbox: Sandbox, handle: SandboxHandle, command: string, failure: string) {
+ const result = await sandbox.run(handle, command);
+ if (result.code !== 0) throw new Error(`${failure} (exit ${result.code}): ${result.stderr.trim()}`);
+ return result;
+}
+
+const belongsTo = (path: string, roots: readonly CredentialPathSpec[]) =>
+ roots.some((root) => (root.kind === "directory" ? pathUnder(path, root.path) : path === root.path));
+
+export async function prepareExecutionFiles(
+ sandbox: Sandbox,
+ handle: SandboxHandle,
+ credentials: ExecutionFileCredential[],
+): Promise<{ env: Record<string, string>; plan: ExecutionFilePlan; finish(): Promise<void> }> {
+ const files = credentials
+ .flatMap((credential) => credential.files)
+ .map((file) => ({
+ ...file,
+ path: homeRelativePath(file.path),
+ }));
+ if (new Set(files.map((file) => file.path)).size !== files.length)
+ throw new Error("Requested credentials contain overlapping file paths");
+ const roots = credentials.map((credential) =>
+ credential.files.map((file) => {
+ const path = homeRelativePath(file.path);
+ return (
+ builtInCredentialPaths().find((root) => root.kind === "directory" && pathUnder(path, root.path)) ?? {
+ path,
+ kind: "file" as const,
+ }
+ );
+ }),
+ );
+ if (files.some((file) => roots.filter((group) => belongsTo(file.path, group)).length !== 1))
+ throw new Error("Requested credentials contain overlapping credential directories");
+ const prefix = "/tmp/qm-credentials.";
+ const created = await runOrThrow(
+ sandbox,
+ handle,
+ `find /tmp/ -maxdepth 1 -name ${shq(`${prefix.slice(5)}*`)} -mmin +120 -exec rm -rf -- {} + 2>/dev/null; umask 077; d=$(mktemp -d ${shq(`${prefix}XXXXXXXXXX`)}) && printf '%s\\n' "\${d##*.}"`,
+ "Could not create command credential directory",
+ );
+ const suffix = created.stdout.trim();
+ if (!/^[a-zA-Z0-9]+$/.test(suffix))
+ throw new Error(`Unexpected credential directory suffix: ${JSON.stringify(suffix)}`);
+ const directory = `${prefix}${suffix}`;
+ const home = `${directory}/home`;
+ const rooted = { ...handle, rootDir: home };
+ try {
+ await runOrThrow(
+ sandbox,
+ handle,
+ `umask 077; mkdir -p ${files.map((file) => shq(`${home}/${file.path.split("/").slice(0, -1).join("/")}`)).join(" ")}`,
+ "Could not prepare command credential files",
+ );
+ for (const file of files) {
+ await sandbox.writeFileBytes(rooted, file.path, Buffer.from(file.contentBase64, "base64"));
+ await runOrThrow(
+ sandbox,
+ handle,
+ `chmod ${restoredFileMode(file.mode).toString(8)} ${shq(`${home}/${file.path}`)}`,
+ "Could not protect command credential file",
+ );
+ }
+ } catch (error) {
+ return withCleanup(
+ () => Promise.reject(error),
+ () => removeDirectory(sandbox, handle, directory),
+ );
+ }
+ const plan = {
+ directory,
+ credentials: credentials.map((credential, index) => ({
+ paths: credential.files.map((file) => homeRelativePath(file.path)),
+ roots: roots[index]!,
+ })),
+ };
+ return {
+ env: fileCredentialEnvironment(files, home),
+ plan,
+ finish: () =>
+ finishExecutionFiles(
+ sandbox,
+ handle,
+ plan,
+ credentials.map((credential) => credential.save),
+ ),
+ };
+}
+
+async function removeDirectory(sandbox: Sandbox, handle: SandboxHandle, directory: string): Promise<void> {
+ await runOrThrow(sandbox, handle, `rm -rf -- ${shq(directory)}`, "Could not remove command credential directory");
+}
+
+async function finishExecutionFiles(
+ sandbox: Sandbox,
+ handle: SandboxHandle,
+ plan: ExecutionFilePlan,
+ saves: ReadonlyArray<(files: CredentialFile[]) => Promise<void>>,
+ settle?: (retry: number[]) => Promise<void>,
+): Promise<void> {
+ const { directory } = plan;
+ const home = `${directory}/home`;
+ let retry = settle ? plan.credentials.map((_, index) => index) : [];
+ const permanent = (message: string) => {
+ retry = [];
+ return new Error(message);
+ };
+ const work = async () => {
+ const captured = await sandbox.run(
+ handle,
+ `cd ${shq(home)} && [ ! -L ${shq(home)} ] && find . -type f -print0 > ${shq(`${directory}/paths`)} && ` +
+ `COPYFILE_DISABLE=1 tar --null -T ${shq(`${directory}/paths`)} -cf ${shq(`${directory}/files.tar`)} && ` +
+ `[ "$(wc -c < ${shq(`${directory}/files.tar`)})" -le ${MAX_CREDENTIAL_BYTES + MAX_CREDENTIAL_FILES * 1024} ]`,
+ );
+ if (captured.code !== 0)
+ throw permanent(
+ `Could not capture refreshed credential files (exit ${captured.code}): ${captured.stderr.trim()}`,
+ );
+ const archive = await sandbox.readFileBytes({ ...handle, rootDir: directory }, "files.tar");
+ if (!archive) throw permanent("Captured credential archive is missing");
+ const entries = await parseTar(archive);
+ const refreshed = entries.map((entry) => ({
+ path: homeRelativePath(entry.path),
+ contentBase64: entry.data.toString("base64"),
+ mode: restoredFileMode(entry.mode),
+ }));
+ if (plan.credentials.some(({ paths }) => paths.some((path) => !refreshed.some((entry) => entry.path === path))))
+ throw permanent("Credential files were removed or replaced with symlinks during execution");
+ const results = await Promise.allSettled(
+ plan.credentials.map(({ roots }, index) =>
+ saves[index]!(refreshed.filter((file) => belongsTo(file.path, roots))),
+ ),
+ );
+ const failures = results.flatMap((result, index) =>
+ result.status === "rejected" ? [{ index, reason: result.reason as unknown }] : [],
+ );
+ retry = retry.filter((index) =>
+ failures.some(
+ (failure) =>
+ failure.index === index && !(failure.reason instanceof KeychainError && failure.reason.status < 500),
+ ),
+ );
+ if (failures.length)
+ throw new AggregateError(
+ failures.map(({ reason }) => reason),
+ `${retry.length ? "Could not persist refreshed credentials; will retry" : "Could not persist refreshed credentials"}: ${failures.map(({ reason }) => errMessage(reason)).join("; ")}`,
+ );
+ };
+ await withCleanup(work, async () => {
+ await settle?.(retry);
+ if (!retry.length) await removeDirectory(sandbox, handle, directory);
+ });
+}
diff --git a/src/credentials/keychain.ts b/src/credentials/keychain.ts
index 108bc9af1..7f96ec51a 100644
--- a/src/credentials/keychain.ts
+++ b/src/credentials/keychain.ts
@@ -2,7 +2,7 @@ import { createMemoryAdvisoryLock, type AdvisoryLock } from "../persistence/advi
import { orgId as configOrgId } from "../config.ts";
import { randomBytes } from "node:crypto";
import { scopeId as toScopeId, parseScopeId, type Destination, type ScopeId } from "../types.ts";
-import { CAPABILITY_CURL_AUTH, keychainUseCommand } from "../api/contract.ts";
+import { CAPABILITY_CURL_AUTH } from "../api/contract.ts";
import type { DurableMap } from "../persistence/durable-map.ts";
import { encryptSecret, decryptSecret, type SecretKey } from "../connectors/connector-client-store.ts";
import { errMessage } from "../util/errors.ts";
@@ -11,7 +11,6 @@ import { cronIdOf } from "../sessions/session-store.ts";
import { hashId } from "../util/crypto.ts";
import { shq } from "../util/shell.ts";
import { homeRelativePath } from "./paths.ts";
-import type { CredentialPathSpec } from "./resident-paths.ts";
import { envKey } from "./connector-token.ts";
const COMPOSIO_ENV_KEY = "COMPOSIO_API_KEY";
@@ -103,7 +102,6 @@ export interface KeychainCredential {
envKey?: string;
target?: string;
targets?: string[];
- capturePaths?: CredentialPathSpec[];
host?: string;
accountLabel?: string;
fields?: CredentialFieldMeta[];
@@ -310,9 +308,7 @@ interface SaveCredentialInput {
files?: CredentialFile[];
host?: string;
accountLabel?: string;
- capturePaths?: CredentialPathSpec[];
origin?: string;
- expectedOrigin?: string;
expiresAt?: number;
}
@@ -375,8 +371,6 @@ interface MaterializedFileCred {
export type MaterializedCred = ({ kind: "env" } & MaterializedEnvCred) | ({ kind: "file" } & MaterializedFileCred);
-export const DEVICE_FLOW_ORIGIN = "device-flow-auto-capture";
-
export class KeychainError extends Error {
readonly status: number;
constructor(status: number, message: string) {
@@ -393,15 +387,14 @@ interface GrantListFilter {
export interface Keychain extends ServiceCredentialStore, ConnectorTokenStore {
save(input: SaveCredentialInput): Promise<KeychainCredentialMeta>;
+ updateFiles(materialized: Extract<MaterializedCred, { kind: "file" }>, files: CredentialFile[]): Promise<void>;
+ writebackBaseline(
+ source: FileCredentialSource,
+ fingerprint: string,
+ ): Promise<Extract<MaterializedCred, { kind: "file" }>>;
listAllMetadata(): Promise<KeychainCredentialMeta[]>;
listByOwner(ownerId: string): Promise<KeychainCredentialMeta[]>;
listByOwners(ownerIds: string[]): Promise<Map<string, KeychainCredentialMeta[]>>;
- setCapturePaths(
- ownerId: string,
- service: string,
- capturePaths: CredentialPathSpec[],
- expectedOrigin: string,
- ): Promise<boolean>;
listConnectorsByOwners(ownerIds: string[]): Promise<Map<string, ConnectorMeta[]>>;
getCredential(id: string): Promise<KeychainCredentialMeta | null>;
/** Decrypt an env credential the caller OWNS — no grant machinery, never someone else's. */
@@ -439,7 +432,6 @@ export interface Keychain extends ServiceCredentialStore, ConnectorTokenStore {
singleUse: boolean;
commit(): Promise<void>;
}>;
- materialize(grantId: string, scopeId: ScopeId, usedBy: string): Promise<MaterializedCred>;
materializeOwnById(ownerId: string, credentialId: string, scopeId: ScopeId): Promise<MaterializedCred>;
materializeOwn(ownerId: string): Promise<MaterializedEnvCred[]>;
materializeOwnFiles(ownerId: string): Promise<MaterializedFileCred[]>;
@@ -455,7 +447,22 @@ export function credentialHandle(credentialId: string): string {
return `kc_${credentialId.slice(0, 12)}`;
}
-export function fileCredentialFingerprint(files: CredentialFile[]): string {
+export interface FileCredentialSource {
+ credentialId: string;
+ ownerId: string;
+ service: string;
+ grantId?: string;
+}
+
+export function credentialFilesFingerprint(files: CredentialFile[]): string {
+ return fileCredentialFingerprint(
+ files
+ .map((file) => ({ ...file, mode: restoredFileMode(file.mode) }))
+ .sort((a, b) => homeRelativePath(a.path).localeCompare(homeRelativePath(b.path))),
+ );
+}
+
+function fileCredentialFingerprint(files: CredentialFile[]): string {
return fingerprintOf(JSON.stringify(files.map((f) => ({ ...f, path: homeRelativePath(f.path) }))));
}
@@ -500,7 +507,9 @@ function toMeta(rec: Omit<KeychainCredential, "secretEnc"> & { secretEnc?: strin
const { secretEnc: _, ...meta } = rec;
return {
...meta,
- ...(rec.kind === "env" && !isBackendCredential(rec) ? { credentialHandle: credentialHandle(rec.id) } : {}),
+ ...((rec.kind === "env" || rec.kind === "file") && !isBackendCredential(rec)
+ ? { credentialHandle: credentialHandle(rec.id) }
+ : {}),
};
}
@@ -860,7 +869,7 @@ export function createKeychain(deps: {
if (!service) throw new KeychainError(400, "service required");
let files: CredentialFile[] | undefined;
if (input.files?.length) {
- files = input.files.map((f) => ({ ...f, path: keychainFilePath(f.path) }));
+ files = checkedFiles(input.files.map((f) => ({ ...f, path: keychainFilePath(f.path) })));
} else if (input.target && input.secret) {
files = [
{
@@ -902,7 +911,6 @@ export function createKeychain(deps: {
const ownerId = canonicalPerson(input.ownerId);
const id = credId(ownerId, service, slot);
const buildRec = (prior?: KeychainCredential | null): KeychainCredential => {
- const carriedCapturePaths = input.capturePaths ?? prior?.capturePaths;
return {
id,
ownerId,
@@ -912,7 +920,6 @@ export function createKeychain(deps: {
...(envKey ? { envKey } : {}),
...(fieldsMeta ? { fields: fieldsMeta } : {}),
...(targets ? { targets } : {}),
- ...(carriedCapturePaths ? { capturePaths: carriedCapturePaths } : {}),
...(input.host ? { host: input.host } : {}),
...(input.accountLabel ? { accountLabel: input.accountLabel } : {}),
secretEnc: encryptSecret(secret, deps.key),
@@ -923,31 +930,9 @@ export function createKeychain(deps: {
updatedAt: t,
};
};
- const expectedOrigin =
- input.expectedOrigin ?? (input.origin === DEVICE_FLOW_ORIGIN ? DEVICE_FLOW_ORIGIN : undefined);
- if (expectedOrigin === undefined) {
- const prior = await deps.creds.get(id);
- const rec = buildRec(prior);
- await deps.creds.put(id, rec);
- return toMeta(rec);
- }
- if (!deps.creds.update || !deps.creds.insertIfAbsent)
- throw new Error("credential store does not support atomic origin-guarded saves");
- const guarded = (prior: KeychainCredential): KeychainCredential => {
- if (prior.origin !== expectedOrigin)
- throw new KeychainError(
- 409,
- `a ${prior.origin ?? "manually saved"} credential for ${service} already exists — not overwritten`,
- );
- return buildRec(prior);
- };
- for (let attempt = 0; attempt < 3; attempt++) {
- const updated = await deps.creds.update(id, guarded);
- if (updated) return toMeta(updated);
- const fresh = buildRec();
- if (await deps.creds.insertIfAbsent(id, fresh)) return toMeta(fresh);
- }
- throw new KeychainError(503, `credential for ${service} is being written concurrently — retry`);
+ const rec = buildRec(await deps.creds.get(id));
+ await deps.creds.put(id, rec);
+ return toMeta(rec);
}
async function materializeConnectorEnv(
@@ -1093,15 +1078,57 @@ export function createKeychain(deps: {
return {
save: saveCredential,
+ async updateFiles(materialized, files) {
+ if (materialized.grantId) return;
+ const normalized = checkedFiles(files.map((file) => ({ ...file, path: keychainFilePath(file.path) })));
+ if (!normalized.length) throw new KeychainError(409, "Refreshed credential files are missing");
+ const fingerprint = credentialFilesFingerprint;
+ if (fingerprint(normalized) === fingerprint(materialized.files)) return;
+
+ if (!deps.creds.update) throw new Error("credential store does not support atomic file refresh");
+ const updated = await deps.creds.update(materialized.credentialId, (current) => {
+ if (current.kind !== "file" || !samePerson(current.ownerId, materialized.ownerId))
+ throw new KeychainError(409, "Credential changed during execution");
+ const currentFiles = decryptToFiles(current).files;
+ if (fingerprint(currentFiles) === fingerprint(normalized)) return current;
+ if (fingerprint(currentFiles) !== fingerprint(materialized.files))
+ throw new KeychainError(409, "Credential changed during execution; refreshed files were not saved");
+ const secret = JSON.stringify(normalized);
+ return {
+ ...current,
+ targets: normalized.map((file) => file.path),
+ secretEnc: encryptSecret(secret, deps.key),
+ fingerprint: fingerprintOf(secret),
+ updatedAt: now(),
+ };
+ });
+ if (!updated) throw new KeychainError(410, "Credential was removed during execution");
+ },
+
+ async writebackBaseline(source, fingerprint) {
+ const current = await deps.creds.get(source.credentialId);
+ if (!current || current.kind !== "file" || !samePerson(current.ownerId, source.ownerId))
+ throw new KeychainError(410, "Credential was removed during execution");
+ const files = decryptToFiles(current).files;
+ if (credentialFilesFingerprint(files) !== fingerprint)
+ throw new KeychainError(409, "Credential changed during execution; refreshed files were not saved");
+ return {
+ kind: "file",
+ credentialId: current.id,
+ ownerId: current.ownerId,
+ service: current.service,
+ files,
+ ...(source.grantId ? { grantId: source.grantId } : {}),
+ };
+ },
+
async listAllMetadata() {
- return (await deps.creds.select({ omit: ["secretEnc"] }))
- .filter((c) => !c.managed && c.kind !== "broker")
- .map(toMeta);
+ return (await deps.creds.select({ omit: ["secretEnc"] })).filter(listed).map(toMeta);
},
async listByOwner(ownerId) {
return (await deps.creds.select({ omit: ["secretEnc"], where: byOwners([ownerId]) }))
- .filter((c) => samePerson(c.ownerId, ownerId) && !c.managed && c.kind !== "broker")
+ .filter((c) => samePerson(c.ownerId, ownerId) && listed(c))
.map(toMeta);
},
@@ -1109,22 +1136,11 @@ export function createKeychain(deps: {
return bucketByOwner(
await deps.creds.select({ omit: ["secretEnc"], where: byOwners(ownerIds) }),
ownerIds,
- (c) => !c.managed && c.kind !== "broker",
+ listed,
toMeta,
);
},
- async setCapturePaths(ownerId, service, capturePaths, expectedOrigin) {
- const id = credId(ownerId, service, "file");
- if (!deps.creds.update) throw new Error("credential store does not support atomic capture-path updates");
- const updated = await deps.creds.update(id, (rec) => {
- if (rec.origin !== expectedOrigin)
- throw new KeychainError(409, `credential for ${service} was not created by a login capture — not updated`);
- return { ...rec, capturePaths, updatedAt: now() };
- });
- return updated !== null;
- },
-
async getCredential(id) {
const rec = await deps.creds.get(id);
return rec ? toMeta(rec) : null;
@@ -1456,12 +1472,6 @@ export function createKeychain(deps: {
},
prepareMaterialize,
- async materialize(grantId, scopeId, usedBy) {
- const prepared = await prepareMaterialize(grantId, scopeId, usedBy);
- await prepared.commit();
- return prepared.materialized;
- },
-
async materializeOwnById(ownerId, credentialId, scopeId) {
if (scopeId !== toScopeId("personal", ownerId)) {
throw new KeychainError(
@@ -1496,7 +1506,7 @@ export function createKeychain(deps: {
async materializeOwnFiles(ownerId) {
return (await deps.creds.select({ where: byOwners([ownerId]) }))
- .filter((c) => samePerson(c.ownerId, ownerId) && c.kind === "file" && !c.managed)
+ .filter((c) => samePerson(c.ownerId, ownerId) && c.kind === "file" && listed(c))
.map((c) => tryDecrypt(c, decryptToFiles))
.filter((c): c is MaterializedFileCred => c !== null);
},
@@ -1530,65 +1540,27 @@ export function createKeychain(deps: {
};
}
-const tempCredentialPath = (rel: string): string =>
- /^[A-Za-z0-9._@+ /-]+$/.test(rel) ? `"$__kc_dir/${rel}"` : `"$__kc_dir"${shq(`/${rel}`)}`;
-
-const FILE_ENV_POINTERS: Array<[RegExp, (rel: string) => string]> = [
- [/(^|\/)\.aws\/credentials$/, (rel) => `export AWS_SHARED_CREDENTIALS_FILE=${tempCredentialPath(rel)}`],
- [/(^|\/)\.aws\/config$/, (rel) => `export AWS_CONFIG_FILE=${tempCredentialPath(rel)}`],
- [/(^|\/)\.kube\/config$/, (rel) => `export KUBECONFIG=${tempCredentialPath(rel)}`],
- [
- /(^|\/)\.config\/gh\/hosts\.yml$/,
- (rel) => `export GH_CONFIG_DIR=${tempCredentialPath(rel.replace(/\/hosts\.yml$/, ""))}`,
- ],
- [
- /(^|\/)(?:\.config\/(?:glab-cli|glab)|Library\/Application Support\/glab-cli)\/config\.yml$/,
- (rel) => `export GLAB_CONFIG_DIR=${tempCredentialPath(rel.replace(/\/config\.yml$/, ""))}`,
- ],
- [
- /(^|\/)\.docker\/config\.json$/,
- (rel) => `export DOCKER_CONFIG=${tempCredentialPath(rel.replace(/\/config\.json$/, ""))}`,
- ],
- [/(^|\/)\.npmrc$/, (rel) => `export NPM_CONFIG_USERCONFIG=${tempCredentialPath(rel)}`],
- [/(^|\/)\.netrc$/, (rel) => `export NETRC=${tempCredentialPath(rel)}`],
- [
- /(^|\/)\.ssh\/[^/]*(id_|key)[^/]*$/,
- (rel) =>
- /^[A-Za-z0-9._@+ /-]+$/.test(rel)
- ? `export GIT_SSH_COMMAND="ssh -i $__kc_dir/${rel} -o IdentitiesOnly=yes"`
- : `export GIT_SSH_COMMAND="ssh -i $__kc_dir"${shq(`/${rel}`)}" -o IdentitiesOnly=yes"`,
- ],
-];
-
-export function renderUseScript(m: MaterializedCred): string {
- if (m.kind === "env") return m.env.map((e) => `export ${e.key}=${shq(e.value)}`).join("\n") + "\n";
- const files = m.files.map((f) => ({ ...f, path: homeRelativePath(f.path) }));
- const lines = [`__kc_dir="$(mktemp -d "\${TMPDIR:-/tmp}/keychain.XXXXXX")"`, `umask 077`];
- for (const f of files) {
- const parent = f.path.includes("/") ? f.path.replace(/\/[^/]*$/, "") : "";
- if (parent) lines.push(`mkdir -p ${tempCredentialPath(parent)}`);
- const path = tempCredentialPath(f.path);
- lines.push(
- `printf '%s' ${shq(f.contentBase64)} | base64 -d > ${path}`,
- `chmod ${restoredFileMode(f.mode).toString(8)} ${path}`,
- );
- }
- const pointed = new Set<RegExp>();
- for (const f of files) {
- for (const [re, render] of FILE_ENV_POINTERS) {
- if (re.test(f.path) && !pointed.has(re)) {
- pointed.add(re);
- lines.push(render(f.path));
- }
+export function fileCredentialEnvironment(files: CredentialFile[], home: string): Record<string, string> {
+ const env: Record<string, string> = { HOME: home };
+ const pointers: Array<[RegExp, string, boolean?]> = [
+ [/^\.aws\/credentials$/, "AWS_SHARED_CREDENTIALS_FILE"],
+ [/^\.aws\/config$/, "AWS_CONFIG_FILE"],
+ [/^\.kube\/config$/, "KUBECONFIG"],
+ [/^\.config\/gh\/hosts\.yml$/, "GH_CONFIG_DIR", true],
+ [/^(?:\.config\/(?:glab-cli|glab)|Library\/Application Support\/glab-cli)\/config\.yml$/, "GLAB_CONFIG_DIR", true],
+ [/^\.docker\/config\.json$/, "DOCKER_CONFIG", true],
+ [/^\.npmrc$/, "NPM_CONFIG_USERCONFIG"],
+ [/^\.netrc$/, "NETRC"],
+ ];
+ for (const file of files) {
+ const rel = homeRelativePath(file.path);
+ for (const [pattern, key, directory] of pointers) {
+ if (pattern.test(rel) && !(key in env)) env[key] = `${home}/${directory ? rel.replace(/\/[^/]*$/, "") : rel}`;
}
+ if (/^\.ssh\/[^/]*(id_|key)[^/]*$/.test(rel) && !env.GIT_SSH_COMMAND)
+ env.GIT_SSH_COMMAND = `ssh -i ${shq(`${home}/${rel}`)} -o IdentitiesOnly=yes`;
}
- if (files.some((f) => !FILE_ENV_POINTERS.some(([re]) => re.test(f.path)))) {
- lines.push(
- `for __e in "$HOME"/.[!.]* "$HOME"/*; do [ -e "$__e" ] || continue; __b=\${__e##*/}; [ -e "$__kc_dir/$__b" ] || ln -s "$__e" "$__kc_dir/$__b"; done`,
- `export HOME="$__kc_dir"`,
- );
- }
- return lines.join("\n") + "\n";
+ return env;
}
function hoursLeft(expiresAt: number, now: number): number {
@@ -1640,16 +1612,14 @@ export interface KeychainManifestInput {
}
const SAVE_HINT =
- "Saving logins (the owner's own DM only). ALWAYS save a token-style login to the keychain right after it succeeds — " +
- "device-flow file logins (gh, glab, gcloud, aws, ~/.netrc) are captured automatically, but other logins on this " +
- "computer alone are not durable, and only keychain entries can be granted to other conversations. " +
- 'Token-style: `curl -fsS -X POST "$AGENT_API_URL/v1/keychain/credentials" ' +
+ "Saving logins (the owner's own DM only). Before running any CLI login, read the `interactive-login` skill and " +
+ "follow it from the first step: it logs in under a temporary home and saves the result. Never log in against this " +
+ "computer's $HOME; a token found there is not a saved login. Only keychain entries are durable and grantable. " +
+ 'Save a token-style key right after it works: `curl -fsS -X POST "$AGENT_API_URL/v1/keychain/credentials" ' +
CAPABILITY_CURL_AUTH +
' -H \'content-type: application/json\' -d \'{"service":"github","secret":"<token>","envKey":"GITHUB_TOKEN","accountLabel":"<who the service says they are>","expiresAt":<ms epoch, if the service reports one>}\'` — ' +
"verify first (e.g. `gh api user`) and pass what the service reports as `accountLabel`. " +
- "File-style (one bundle per service — e.g. ~/.aws/config + ~/.aws/credentials together): pass " +
- '`"files":[{"path":".aws/config","contentBase64":"<base64 of the file>"}, …]` instead of `secret`/`envKey`. ' +
- "Device-flow login bundles are captured and restored as-is, never renewed by the platform — when one expires, re-run the tool's interactive login.";
+ "Saved CLI logins are loaded only when requested through execute.credentials; updates made by the CLI are saved back after execution. If the provider can no longer refresh the session, re-run its interactive login.";
function expiryNote(c: KeychainCredentialMeta, now: number, own: boolean): string {
if (c.kind === "file" || typeof c.expiresAt !== "number") return "";
@@ -1701,17 +1671,34 @@ function connectorLine(
return `- ${who}: connected app ${cm.host}${account}${status} — credential id \`${cm.credentialId}\` — ${grantNote}`;
}
+export const MAX_CREDENTIAL_FILES = 500;
+export const MAX_CREDENTIAL_BYTES = 8 * 1024 * 1024;
+
+function checkedFiles(files: CredentialFile[]): CredentialFile[] {
+ const bytes = files.reduce((sum, file) => sum + Buffer.byteLength(file.contentBase64, "base64"), 0);
+ if (files.length > MAX_CREDENTIAL_FILES || bytes > MAX_CREDENTIAL_BYTES)
+ throw new KeychainError(
+ 413,
+ `A saved login is limited to ${MAX_CREDENTIAL_FILES} files and ${MAX_CREDENTIAL_BYTES} bytes`,
+ );
+ return files;
+}
+
+function listed(c: Pick<KeychainCredential, "managed" | "kind">): boolean {
+ return !c.managed && c.kind !== "broker";
+}
+
export function renderKeychainManifest(input: KeychainManifestInput, now: number = Date.now()): string {
const lines: string[] = [];
const byCred = new Map(input.scopeGrants.map((g) => [g.credential.id, g.grant]));
- const grantNoteFor = (id: string, file = false): string => {
+ const grantNoteFor = (id: string): string => {
const g = byCred.get(id);
if (!g) return "no grant for this conversation";
const note =
g.mode === "standing"
? `STANDING grant for this conversation (purpose: "${g.purpose}")`
: `one-time grant \`${g.id}\` available (purpose: "${g.purpose}")`;
- return file ? `${note} — load with \`${keychainUseCommand({ grant: g.id })}\`` : note;
+ return note;
};
const ownPersonal = input.scopeId === toScopeId("personal", input.actorId);
const openSpeaker =
@@ -1724,9 +1711,7 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: number
for (const member of input.members) {
const own = (ownPersonal || openSpeaker) && samePerson(member.id, input.actorId);
for (const c of input.entriesByOwner.get(member.id) ?? []) {
- let note = own ? OWN_NOTE : grantNoteFor(c.id, c.kind === "file");
- if (own && ownPersonal && c.kind === "file")
- note = "their own — raw file loading needs no grant on their live turn; background turns need a grant";
+ const note = own ? OWN_NOTE : grantNoteFor(c.id);
memberLines.push(credLine(member, c, note, now, own));
hasOwn ||= own;
}
@@ -1771,15 +1756,13 @@ export function renderKeychainManifest(input: KeychainManifestInput, now: number
if (hasOwn && openSpeaker) {
lines.push(
"",
- `Use execute with scope:"owner" for commands needing the speaker's logins or connected apps. Request env credentials and connector tokens explicitly using execute.credentials. Saved CLI logins are restored there separately. Do not load them through /v1/keychain/use on the shared computer.`,
+ `Use execute with scope:"owner" for commands needing the speaker's logins or connected apps. Request every credential, including saved CLI logins, explicitly using execute.credentials. Refreshed credential files are saved back after execution.`,
"This is a separate, disposable computer: it cannot see the shared workspace, and is destroyed at the end of this turn. Keep credential-using commands there; return only the results needed for the task. Never copy secrets into the shared workspace or pass them to background jobs. Normal command approvals still apply.",
);
} else if (hasOwn) {
lines.push(
"",
- "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. For background turns, use execute.credentials for env credentials and connector tokens; this raw file-loading form still requires a grant.",
+ "Request every credential, including saved CLI logins, explicitly using execute.credentials. Refreshed credential files are saved back after execution.",
);
}
diff --git a/src/credentials/resident-auth.ts b/src/credentials/resident-auth.ts
deleted file mode 100644
index 5fe7b9297..000000000
--- a/src/credentials/resident-auth.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import type { DurableMap } from "../persistence/durable-map.ts";
-import type { Sandbox, SandboxHandle } from "../sandbox/sandbox.ts";
-
-export interface ResidentAuthConnector {
- id: string;
- label: string;
- check: string;
- reauth: string;
-}
-
-export const RESIDENT_AUTH_CONNECTORS: readonly ResidentAuthConnector[] = [
- { id: "gh", label: "GitHub", check: "gh auth status", reauth: "gh auth login" },
- { id: "glab", label: "GitLab", check: "glab auth status", reauth: "glab auth login" },
- {
- id: "gcloud",
- label: "Google Cloud",
- check: "gcloud auth print-access-token",
- reauth: "gcloud auth login --no-launch-browser",
- },
-];
-
-type ResidentAuthState = "active" | "inactive" | "absent";
-
-export interface ScopeLivenessRecord {
- scopeId: string;
- checkedAt: number;
- connectors: Record<string, ResidentAuthState>;
-}
-
-export interface LivenessCache {
- get(scopeId: string): Promise<ScopeLivenessRecord | null>;
- put(record: ScopeLivenessRecord): Promise<void>;
-}
-
-export function createLivenessCache(map: DurableMap<ScopeLivenessRecord>): LivenessCache {
- return {
- get: (scopeId) => map.get(scopeId),
- put: (record) => map.put(record.scopeId, record),
- };
-}
-
-const RESIDENT_AUTH_PROBE_TTL_MS = 5 * 60_000;
-
-export function residentAuthProbeIsStale(
- record: ScopeLivenessRecord | null,
- now: number,
- ttlMs: number = RESIDENT_AUTH_PROBE_TTL_MS,
-): boolean {
- if (!record) return true;
- return now - record.checkedAt > ttlMs;
-}
-
-export function buildResidentAuthProbeScript(
- connectors: readonly ResidentAuthConnector[],
- perCheckSec: number,
-): string {
- const lines = connectors.map((c) => {
- const bin = c.check.split(/\s+/)[0];
- return (
- `( if command -v ${bin} >/dev/null 2>&1; then ` +
- `if timeout ${perCheckSec} ${c.check} >/dev/null 2>&1; then echo "${c.id}|active"; ` +
- `else echo "${c.id}|inactive"; fi; ` +
- `else echo "${c.id}|absent"; fi ) &`
- );
- });
- return `${lines.join("\n")}\nwait`;
-}
-
-function parseProbeOutput(stdout: string): Map<string, ResidentAuthState> {
- const out = new Map<string, ResidentAuthState>();
- for (const raw of stdout.split("\n")) {
- const [id, state] = raw.trim().split("|");
- if (id && (state === "active" || state === "inactive" || state === "absent")) {
- out.set(id, state);
- }
- }
- return out;
-}
-
-export interface ProbeResidentAuthOptions {
- sandbox: Sandbox;
- handle: SandboxHandle;
- cache: LivenessCache;
- scopeId: string;
- now: number;
- connectors?: readonly ResidentAuthConnector[];
- perCheckSec?: number;
- overallTimeoutMs?: number;
-}
-
-export async function probeResidentAuth(opts: ProbeResidentAuthOptions): Promise<void> {
- const connectors = opts.connectors ?? RESIDENT_AUTH_CONNECTORS;
- const script = buildResidentAuthProbeScript(connectors, opts.perCheckSec ?? 6);
- const res = await opts.sandbox.run(opts.handle, script, { timeoutMs: opts.overallTimeoutMs ?? 12_000 });
- const seen = parseProbeOutput(res.stdout);
- if (seen.size === 0) return;
- const prior = (await opts.cache.get(opts.scopeId))?.connectors ?? {};
- const connectorsOut: Record<string, ResidentAuthState> = { ...prior };
- for (const c of connectors) {
- const state = seen.get(c.id);
- if (state) connectorsOut[c.id] = state;
- }
- await opts.cache.put({ scopeId: opts.scopeId, checkedAt: opts.now, connectors: connectorsOut });
-}
diff --git a/src/credentials/resident-paths.ts b/src/credentials/resident-paths.ts
index f4d75ed74..0dff99269 100644
--- a/src/credentials/resident-paths.ts
+++ b/src/credentials/resident-paths.ts
@@ -1,7 +1,6 @@
import { posix } from "node:path";
import { shq } from "../util/shell.ts";
import { pathUnder } from "../util/paths.ts";
-import { homeRelativePath } from "./paths.ts";
import {
BASE_EPHEMERAL_CRED_LINKS,
DISPLACED_DIR_REL,
@@ -11,7 +10,6 @@ import {
export {
BASE_EPHEMERAL_CRED_LINKS,
- CREDENTIAL_PATH_RE,
DISPLACED_DIR_REL,
builtInCredentialPaths,
type CredentialPathSpec,
@@ -39,30 +37,6 @@ export function credentialServiceForPath(path: string): string | undefined {
return first.startsWith(".") && first.length > 1 ? first.slice(1) : undefined;
}
-export function captureRootForTarget(target: string): CredentialPathSpec | undefined {
- let normalized: string;
- try {
- normalized = homeRelativePath(target);
- } catch {
- return undefined;
- }
- if (!credentialServiceForPath(normalized)) return undefined;
- const segments = normalized.split("/");
- if (normalized.startsWith(".config/") && segments.length > 2)
- return { path: `.config/${segments[1]}`, kind: "directory" };
- return { path: normalized, kind: "file" };
-}
-
-export function configCredentialDirs(): string[] {
- return BASE_EPHEMERAL_CRED_LINKS.filter((l) => l.rel.startsWith(".config/")).map((l) => l.rel);
-}
-
-const SERVICE_ALIASES: Readonly<Record<string, readonly string[]>> = { glab: ["glab-cli"] };
-
-export function expandServiceAliases(services: readonly string[]): string[] {
- return [...new Set(services.flatMap((s) => [s, ...(SERVICE_ALIASES[s] ?? [])]))];
-}
-
export function displacedPruneGlobs(): string[] {
return [`./${DISPLACED_DIR_REL}`, `./${DISPLACED_DIR_REL}/*`, `./${DISPLACED_DIR_REL}.*`];
}
diff --git a/src/deployment/deployment-layer.ts b/src/deployment/deployment-layer.ts
index 18d681ff1..84c81c743 100644
--- a/src/deployment/deployment-layer.ts
+++ b/src/deployment/deployment-layer.ts
@@ -1,6 +1,5 @@
export {
compileApproval,
- credentialPathError,
parseToolDescriptor,
type ToolCredentialBroker,
type ToolCredentialPath,
diff --git a/src/deployment/load-layer.ts b/src/deployment/load-layer.ts
index 30ae6fa58..d07aac212 100644
--- a/src/deployment/load-layer.ts
+++ b/src/deployment/load-layer.ts
@@ -9,20 +9,17 @@ import {
type ToolInstallFile,
} from "./deployment-layer.ts";
import { credentialServiceForPath } from "../credentials/resident-paths.ts";
-import type { ResidentAuthConnector } from "../credentials/resident-auth.ts";
import type { CommandRule } from "../types.ts";
export interface DeploymentLayerRuntime {
dir: string;
tools: ToolDescriptor[];
- connectors: ResidentAuthConnector[];
advertisedTools: string[];
hints: string[];
credentialPaths: ToolCredentialPath[];
splitEnvTemplates: Record<string, string>[];
brokeredTools: BrokeredLayerTool[];
commandRules: CommandRule[];
- credentialTools: LayerCredentialTool[];
installFiles: LayerInstallFile[];
}
@@ -39,23 +36,16 @@ export interface LayerInstallFile {
content: string;
}
-export interface LayerCredentialTool {
- service: string;
- roots: string[];
-}
-
export function emptyDeploymentLayer(): DeploymentLayerRuntime {
return {
dir: "",
tools: [],
- connectors: [],
advertisedTools: [],
hints: [],
credentialPaths: [],
splitEnvTemplates: [],
brokeredTools: [],
commandRules: [],
- credentialTools: [],
installFiles: [],
};
}
@@ -116,13 +106,6 @@ function toolService(tool: ToolDescriptor, why: string): string {
);
}
-function toolServices(tool: ToolDescriptor): string[] {
- const services = new Set(
- (tool.auth?.credentialPaths ?? []).flatMap((entry) => credentialServiceForPath(entry.path) ?? []),
- );
- return services.has(tool.id) || services.size === 0 ? [tool.id] : [...services];
-}
-
export function resolvedDeploymentLayer(
dir: string,
tools: ToolDescriptor[],
@@ -140,14 +123,6 @@ export function resolvedDeploymentLayer(
return {
dir,
tools,
- connectors: withAuth
- .filter((t) => !t.auth!.broker)
- .map((t) => ({
- id: t.id,
- label: t.label ?? t.id,
- check: t.auth!.check,
- reauth: t.auth!.reauth,
- })),
advertisedTools: tools.flatMap((t) => (t.advertise ? [t.advertise] : [])),
hints: tools.flatMap((t) => t.hints ?? []),
credentialPaths: [
@@ -171,14 +146,6 @@ export function resolvedDeploymentLayer(
...(approval.reason ? { reason: approval.reason } : {}),
})),
),
- credentialTools: withAuth.flatMap((t) =>
- toolServices(t).map((service) => ({
- service,
- roots: (t.auth!.credentialPaths ?? []).flatMap((entry) =>
- credentialServiceForPath(entry.path) === service ? [entry.path] : [],
- ),
- })),
- ),
installFiles,
};
}
@@ -187,14 +154,12 @@ export function replaceDeploymentLayer(target: DeploymentLayerRuntime, source: D
target.dir = source.dir;
for (const key of [
"tools",
- "connectors",
"advertisedTools",
"hints",
"credentialPaths",
"splitEnvTemplates",
"brokeredTools",
"commandRules",
- "credentialTools",
"installFiles",
] as const) {
target[key].splice(0, target[key].length, ...(source[key] as never[]));
diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.ts
index a2b45b695..30404e7f7 100644
--- a/src/harness/agent-tools.ts
+++ b/src/harness/agent-tools.ts
@@ -649,7 +649,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
credentials: Type.Optional(
Type.Array(Type.String(), {
description:
- "Exact authorized credential handles to materialize for this command only. Newly granted handles are accepted.",
+ "Exact authorized credential handles to materialize for this command only (for a background job, until it exits). Newly granted handles are accepted.",
}),
),
};
@@ -785,12 +785,12 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
"a room like \"#project-alpha\" — a channel you and this person are both in; runs the command on THAT room's computer (read-only by etiquette: read, search, fetch — don't rearrange).";
const reachDescription =
"Run a shell command and return its stdout/stderr/exit code. Pick where it runs with `scope`:\n" +
- '- "scoped" (DEFAULT): this conversation\'s sandbox — its workspace files, turn-private inbox paths, shared-file handles, cached logins, and $AGENT_API_* tokens; working state is retained within provider recovery limits; publish durable code to git and artifacts to Files.\n' +
+ '- "scoped" (DEFAULT): this conversation\'s sandbox — its workspace files, turn-private inbox paths, shared-file handles, and $AGENT_API_* tokens; saved credentials require execute.credentials; working state is retained within provider recovery limits; publish durable code to git and artifacts to Files.\n' +
(scratchExec
- ? '- "scratch": a blank computer for this turn, with the same OS/tooling, read-only org-global files and skills, this conversation\'s scoped $AGENT_API_* capabilities (including Files), and only credentials explicitly requested for this execute call. It does not restore the resident workspace or cached CLI logins. Its local files are discarded after the turn. Use it for self-contained commands and API work, including credential-using work; publish any needed outputs to Files and verify success before finishing. Use scope:"scoped" when you need existing workspace files, installed state, cached logins, or local work that must continue later.\n'
+ ? '- "scratch": a blank computer for this turn, with the same OS/tooling, read-only org-global files and skills, this conversation\'s scoped $AGENT_API_* capabilities (including Files), and only credentials explicitly requested for this execute call. It does not restore the resident workspace or cached CLI logins. Its local files are discarded after the turn. Use it for self-contained commands and API work, including credential-using work; publish any needed outputs to Files and verify success before finishing. Use scope:"scoped" when you need existing workspace files, installed state, or local work that must continue later.\n'
: "") +
(ownerAuthExec
- ? "- \"owner\": available to the live speaker in Open shared conversations and to owner-authorized shared automation; this invocation-only auth box has org-global files plus the owner's credentials, no room workspace or $AGENT_API_* tokens, and is destroyed after the turn. Use for commands that need the owner's login without putting it on the shared computer.\n"
+ ? '- "owner": available to the live speaker in Open shared conversations and to owner-authorized shared automation; this invocation-only auth box has org-global files plus explicitly requested credentials, no room workspace or $AGENT_API_* tokens, and is destroyed after the turn. Use for commands that need the owner\'s login without putting it on the shared computer.\n'
: "") +
"- a room like \"#project-alpha\": a channel you and this person are both in — runs the command on THAT room's computer. Other rooms are places you VISIT: read, search, fetch (ls/grep/cat); don't rearrange. That box has none of this conversation's logins or capability tokens. Say where anything you bring back came from.\n" +
"If a file or piece of work isn't on this computer, don't declare it lost — check the rooms listed under 'Other computers you can reach'.\n" +
@@ -801,12 +801,12 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
EXECUTE_TIMEOUT_GUIDANCE;
const scopeDescription =
`Run a shell command and return its stdout/stderr/exit code. Pick a computer with \`scope\`:\n` +
- '- "scoped" (DEFAULT): this conversation\'s sandbox — its workspace files, turn-private inbox paths, shared-file handles, cached logins, and $AGENT_API_* tokens; working state is retained within provider recovery limits; publish durable code to git and artifacts to Files.\n' +
+ '- "scoped" (DEFAULT): this conversation\'s sandbox — its workspace files, turn-private inbox paths, shared-file handles, and $AGENT_API_* tokens; saved credentials require execute.credentials; working state is retained within provider recovery limits; publish durable code to git and artifacts to Files.\n' +
(ownerAuthExec
- ? '- "owner": available to the live speaker in Open shared conversations and to owner-authorized shared automation; this invocation-only auth box has org-global files plus the owner\'s credentials, no shared workspace or $AGENT_API_* tokens, and is destroyed after the turn. Use it for credential-using commands without putting personal logins on the shared computer.\n'
+ ? '- "owner": available to the live speaker in Open shared conversations and to owner-authorized shared automation; this invocation-only auth box has org-global files plus explicitly requested credentials, no shared workspace or $AGENT_API_* tokens, and is destroyed after the turn. Use it for credential-using commands without putting personal logins on the shared computer.\n'
: "") +
(scratchExec
- ? '- "scratch": a blank computer for this turn, with the same OS/tooling, read-only org-global files and skills, this conversation\'s scoped $AGENT_API_* capabilities (including Files), and only credentials explicitly requested for this execute call. It does not restore the resident workspace or cached CLI logins. Its local files are discarded after the turn. Use it for self-contained commands and API work, including credential-using work; publish any needed outputs to Files and verify success before finishing. Use scope:"scoped" when you need existing workspace files, installed state, cached logins, or local work that must continue later.\n'
+ ? '- "scratch": a blank computer for this turn, with the same OS/tooling, read-only org-global files and skills, this conversation\'s scoped $AGENT_API_* capabilities (including Files), and only credentials explicitly requested for this execute call. It does not restore the resident workspace or cached CLI logins. Its local files are discarded after the turn. Use it for self-contained commands and API work, including credential-using work; publish any needed outputs to Files and verify success before finishing. Use scope:"scoped" when you need existing workspace files, installed state, or local work that must continue later.\n'
: "") +
"`durable` defaults to true on scoped and false on invocation-only boxes; scoped cannot discard writes, and invocation-only boxes cannot be made durable.\n" +
FILE_SEND_GUIDANCE +
@@ -910,7 +910,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
],
{
description:
- 'Which computer runs this command: "scoped" (default — this conversation\'s sandbox: its working files and authorized logins; recovery depends on the provider) or "scratch" (blank filesystem for this turn, scoped API capabilities and explicitly requested credentials; no resident workspace or cached logins).',
+ 'Which computer runs this command: "scoped" (default — this conversation\'s sandbox: its working files and explicitly requested credentials; recovery depends on the provider) or "scratch" (blank filesystem for this turn, scoped API capabilities and explicitly requested credentials; no resident workspace or cached logins).',
},
),
),
@@ -1038,7 +1038,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
name: "skill",
label: "skill",
description:
- "Load a skill from the Skills index before relying on it. Returns its SKILL.md instructions (or the relative file named by `path`) straight from the published source, without starting a sandbox. When the skill ships scripts or supporting files, this call also syncs them into a directory that lives for this turn and reports it; run and read them there with execute and files action read, in this turn.",
+ "Load a skill from the Skills index before relying on it. Returns its SKILL.md instructions (or the relative file named by `path`) straight from the published source, without starting a sandbox. When the skill ships scripts or supporting files, this call also syncs them into a stable directory on the computer and reports it; run and read them there with execute and files action read. Reading the skill again refreshes that copy.",
parameters: Type.Object({
name: Type.String({ description: "Skill name exactly as listed in the Skills index." }),
path: Type.Optional(
@@ -1834,8 +1834,9 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
"URL/code from the returned output and relay it to the user, then `watch` (or `poll`) until the " +
"command exits — that's when the login is done. If a prompt needs an answer typed in, use " +
"action=send_input. Never run a login with `execute` (it blocks the whole turn) and never `stop`/kill a " +
- "login mid-flight — that throws away the pending approval and wedges it. The platform captures the " +
- "resulting credential into your keychain automatically; you don't save anything yourself.",
+ "login mid-flight — that throws away the pending approval and wedges it. Read the `interactive-login` skill " +
+ "before starting one and run it as the skill says: under a temporary home, never this computer's $HOME, then " +
+ "saved to the keychain.",
parameters: Type.Object({
action: Type.Union(
[
@@ -1911,6 +1912,12 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
description: "start only: exact sandbox to run on; later operations use the job’s saved target.",
}),
),
+ credentials: Type.Optional(
+ Type.Array(Type.String(), {
+ description:
+ "start only: exact authorized credential handles, as for execute. They stay staged until the job exits, then refreshes are saved back.",
+ }),
+ ),
timeout_seconds: Type.Optional(
Type.Integer({
minimum: 1,
@@ -1929,6 +1936,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
process_id: params.process_id,
...(params.sandbox_id ? { sandbox_id: params.sandbox_id } : {}),
...(params.monitor_id ? { monitor_id: params.monitor_id } : {}),
+ ...(params.credentials?.length ? { credentials: params.credentials } : {}),
});
try {
switch (params.action) {
@@ -1951,6 +1959,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
purpose: params.purpose,
...(params.timeout_seconds ? { ttlSeconds: params.timeout_seconds } : {}),
...(params.sandbox_id ? { sandboxId: params.sandbox_id } : {}),
+ ...(params.credentials?.length ? { credentials: params.credentials } : {}),
});
return recordResult(
callId,
@@ -2230,7 +2239,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
set_default: ["sandbox_id"],
retire: ["sandbox_id"],
exec: Object.keys(schemas(execute)),
- start_process: ["command", "sandbox_id", "timeout_seconds"],
+ start_process: ["command", "sandbox_id", "timeout_seconds", "credentials"],
read_process: ["process_id", "since_cursor", "wait_seconds", "max_bytes"],
write_stdin: ["process_id", "data"],
signal_process: ["process_id", "signal"],
@@ -3910,60 +3919,6 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
},
});
- const registerLogin = defineTool({
- name: "register_login",
- label: "register_login",
- description:
- "After you complete a browser/device-code login for a CLI whose files are NOT already backed up automatically " +
- "(the common ones — gh, glab, gcloud, aws, ssh — already are), call this so the login survives this machine being " +
- 'rebuilt. Pass the service name and the file(s) or directory it wrote under $HOME (e.g. { service: "kaggle", ' +
- 'paths: [{ path: ".kaggle/kaggle.json", kind: "file" }] }). The paths are captured immediately and re-captured on ' +
- "future turns so token rotations are kept current. Only paths under $HOME, disjoint from the built-in credential " +
- "paths, are accepted. A restored file is byte-faithful, but some providers invalidate sessions server-side " +
- "(npm login tokens expire in hours) — treat the CLI's own auth check as the truth after a rebuild.",
- parameters: Type.Object({
- service: Type.String(),
- paths: Type.Array(
- Type.Object({
- path: Type.String(),
- kind: Type.Union([Type.Literal("file"), Type.Literal("directory")]),
- }),
- { minItems: 1 },
- ),
- }),
- async execute(callId, params: { service: string; paths: { path: string; kind: "file" | "directory" }[] }) {
- const tc = ref.current;
- await recordCall(callId, { tool: "register_login", service: params.service, paths: params.paths });
- if (!tc?.registerLogin) {
- return recordResult(
- callId,
- { tool: "register_login", unavailable: true },
- text("[error] register_login is unavailable on this turn (no writable computer)"),
- true,
- );
- }
- try {
- const result = await tc.registerLogin(params.service, params.paths);
- return recordResult(
- callId,
- { tool: "register_login", ...result },
- text(
- result.captured
- ? `Registered ${result.service} and captured its login — it will survive a machine rebuild.`
- : `Registered ${result.service}. Nothing was captured yet; complete the login, then it is captured automatically next turn.`,
- ),
- );
- } catch (error) {
- return recordResult(
- callId,
- { tool: "register_login", failed: true, reason: errMessage(error) },
- text(`[error] ${errMessage(error)}`),
- true,
- );
- }
- },
- });
-
const GOAL_EVIDENCE_FILES = 5;
const GOAL_EVIDENCE_FILE_CHARS = 20_000;
async function goalEvidenceFiles(
@@ -4384,7 +4339,6 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
...(!opts?.sandboxResources && !delegateWork ? [background] : []),
...(opts?.sessionTools === false ? [] : [subagentTool, ...(sidebarSessions ? [sessionTool] : [])]),
sandbox,
- registerLogin,
...(controlTools
? [
resourceTool("cron", {
diff --git a/src/harness/mock-harness.ts b/src/harness/mock-harness.ts
index 62b9a399c..df7e3ef06 100644
--- a/src/harness/mock-harness.ts
+++ b/src/harness/mock-harness.ts
@@ -397,10 +397,12 @@ export function createMockHarness(): Harness {
command: string;
credentials?: string[];
ownerAuth?: boolean;
+ sandboxId?: string;
};
const result = await turn.tools.execute(params.command, {
credentials: params.credentials,
ownerAuth: params.ownerAuth,
+ sandboxId: params.sandboxId,
});
await turn.emit({ type: "tool_result", payload: result, scopeLabel: turn.scopeLabel });
turn.onProgress?.({ toolCalls: 1 });
diff --git a/src/monitors/monitor-poller.ts b/src/monitors/monitor-poller.ts
index 6cd667073..df096d45e 100644
--- a/src/monitors/monitor-poller.ts
+++ b/src/monitors/monitor-poller.ts
@@ -43,6 +43,7 @@ export interface MonitorPollerDeps {
now?: () => number;
maxFiresPerTick?: number;
leaderLease?: LeaderLease;
+ onProcessExit?: (handle: SandboxHandle, processId: string) => Promise<void>;
heartbeatMs?: number;
minFireIntervalMs?: number;
}
@@ -254,7 +255,10 @@ export function createMonitorPoller(deps: MonitorPollerDeps): MonitorPoller {
await deps.monitors.advance(m.id, { cursor: read.cursor, tail, firedAt: t });
if (exited || expired) await deps.monitors.setEnabled(m.id, false);
}
- if (exited) await deps.processes.markStatus(m.processId, "exited");
+ if (exited) {
+ await deps.processes.markStatus(m.processId, "exited");
+ await deps.onProcessExit?.(handle, m.processId).catch((e) => deps.monitors.recordError(m.id, errMessage(e)));
+ }
return true;
}
diff --git a/src/processes/process-reaper.ts b/src/processes/process-reaper.ts
index 881fd97f1..253d37432 100644
--- a/src/processes/process-reaper.ts
+++ b/src/processes/process-reaper.ts
@@ -3,7 +3,7 @@ import { createSweeper } from "../util/sweeper.ts";
import { createNoopLeaderLease, type LeaderLease } from "../persistence/leader-lease.ts";
import { awaitProcessExit } from "../sandbox/await-process-exit.ts";
import { processIsGone } from "../sandbox/process-poll.ts";
-import type { ProcessSandbox } from "../sandbox/sandbox.ts";
+import type { ProcessSandbox, SandboxHandle } from "../sandbox/sandbox.ts";
import { errMessage } from "../util/errors.ts";
const PROCESS_REAPER_LEASE_KEY = "processes:reaper";
@@ -11,6 +11,7 @@ const PROCESS_REAPER_LEASE_KEY = "processes:reaper";
export interface ReaperKillHookOptions {
termGraceMs?: number;
killGraceMs?: number;
+ onExit?: (handle: SandboxHandle, processId: string) => Promise<void>;
}
export function createReaperKillHook(
@@ -25,15 +26,26 @@ export function createReaperKillHook(
rec.sandboxId ? { sandboxId: rec.sandboxId } : undefined,
);
try {
- await sandbox.signalProcess(handle, rec.processId, "TERM");
- let status = await awaitProcessExit(sandbox, handle, rec.processId, termGraceMs);
- if (status.state !== "exited") {
- await sandbox.signalProcess(handle, rec.processId, "KILL");
- status = await awaitProcessExit(sandbox, handle, rec.processId, killGraceMs);
+ try {
+ await sandbox.signalProcess(handle, rec.processId, "TERM");
+ let status = await awaitProcessExit(sandbox, handle, rec.processId, termGraceMs);
+ if (status.state !== "exited") {
+ await sandbox.signalProcess(handle, rec.processId, "KILL");
+ status = await awaitProcessExit(sandbox, handle, rec.processId, killGraceMs);
+ }
+ if (status.state !== "exited") throw new Error(`process survived TERM+KILL: ${rec.processId}`);
+ } catch (e) {
+ if (!processIsGone(e)) throw e;
}
- if (status.state !== "exited") throw new Error(`process survived TERM+KILL: ${rec.processId}`);
- } catch (e) {
- if (!processIsGone(e)) throw e;
+ await opts
+ ?.onExit?.(handle, rec.processId)
+ .catch((e) =>
+ console.error(
+ "[process-reaper] credential writeback failed:",
+ errMessage(e),
+ ...(e instanceof AggregateError ? e.errors.map(errMessage) : []),
+ ),
+ );
} finally {
await sandbox.teardown(handle, { keepWarm: true });
}
diff --git a/src/processes/process-registry.ts b/src/processes/process-registry.ts
index 88752a404..baed529c9 100644
--- a/src/processes/process-registry.ts
+++ b/src/processes/process-registry.ts
@@ -1,3 +1,4 @@
+import type { ProcessCredentialFiles } from "../credentials/execute-files.ts";
import { createPgPool } from "../persistence/pg-pool.ts";
export type ProcessKind = "build" | "dev-server" | "background";
@@ -22,6 +23,7 @@ export interface ProcessRecord {
status: ProcessStatus;
sessionRef?: string;
runId?: string;
+ credentialsPending?: boolean;
}
interface NewProcessRecord {
@@ -34,6 +36,7 @@ interface NewProcessRecord {
ttlMs: number;
sessionRef?: string;
runId?: string;
+ credentialFiles?: ProcessCredentialFiles;
}
export interface ProcessRegistry {
@@ -43,6 +46,8 @@ export interface ProcessRegistry {
liveByScope(scopeId: string, now?: number): Promise<ProcessRecord[]>;
listLive(now?: number): Promise<ProcessRecord[]>;
markStatus(processId: string, status: ProcessStatus): Promise<boolean>;
+ credentialFiles(processId: string): Promise<ProcessCredentialFiles | null>;
+ setCredentialFiles(processId: string, files: ProcessCredentialFiles | null): Promise<void>;
listExpired(now?: number): Promise<ProcessRecord[]>;
delete(processId: string): Promise<void>;
close?(): void;
@@ -67,18 +72,31 @@ function newRecord(rec: NewProcessRecord, now: number): ProcessRecord {
export function createMemoryProcessRegistry(): ProcessRegistry {
const rows = new Map<string, ProcessRecord>();
+ const credentialFiles = new Map<string, ProcessCredentialFiles>();
+ const view = (row: ProcessRecord): ProcessRecord => ({
+ ...row,
+ ...(credentialFiles.has(row.processId) ? { credentialsPending: true } : {}),
+ });
return {
async register(rec) {
const row = newRecord(rec, Date.now());
rows.set(row.processId, row);
+ if (rec.credentialFiles) credentialFiles.set(row.processId, rec.credentialFiles);
return { ...row };
},
+ async credentialFiles(processId) {
+ return credentialFiles.get(processId) ?? null;
+ },
+ async setCredentialFiles(processId, files) {
+ if (!files) credentialFiles.delete(processId);
+ else if (rows.has(processId)) credentialFiles.set(processId, files);
+ },
async get(processId) {
const row = rows.get(processId);
- return row ? { ...row } : null;
+ return row ? view(row) : null;
},
async listByScope(scopeId) {
- return [...rows.values()].filter((r) => r.scopeId === scopeId).map((r) => ({ ...r }));
+ return [...rows.values()].filter((r) => r.scopeId === scopeId).map(view);
},
async liveByScope(scopeId, now = Date.now()) {
return [...rows.values()]
@@ -99,6 +117,7 @@ export function createMemoryProcessRegistry(): ProcessRegistry {
},
async delete(processId) {
rows.delete(processId);
+ credentialFiles.delete(processId);
},
};
}
@@ -116,6 +135,7 @@ function pgRowToRecord(r: Record<string, unknown>): ProcessRecord {
status: r.status as ProcessStatus,
...(r.session_ref ? { sessionRef: r.session_ref as string } : {}),
...(r.run_id ? { runId: r.run_id as string } : {}),
+ ...(r.credential_files != null ? { credentialsPending: true } : {}),
};
}
@@ -141,11 +161,17 @@ export function createPostgresProcessRegistry(connectionString: string): Process
statements: ["ALTER TABLE process_sessions ADD COLUMN IF NOT EXISTS purpose TEXT"],
};
pg.registerMigration(purposeMigration);
+ const credentialFilesMigration = {
+ id: "processes/registry/0004",
+ statements: ["ALTER TABLE process_sessions ADD COLUMN IF NOT EXISTS credential_files JSONB"],
+ };
+ pg.registerMigration(credentialFilesMigration);
let ready: Promise<void> | undefined;
const q: typeof pg.q = async (...args) => {
ready ??= pg
.migrate(migration)
.then(() => pg.migrate(purposeMigration))
+ .then(() => pg.migrate(credentialFilesMigration))
.catch((error) => {
ready = undefined;
throw error;
@@ -157,8 +183,8 @@ export function createPostgresProcessRegistry(connectionString: string): Process
async register(rec) {
const row = newRecord(rec, Date.now());
await q(
- `INSERT INTO process_sessions(process_id, scope_id, kind, command, started_at, expires_at, status, session_ref, run_id, sandbox_id, purpose)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
+ `INSERT INTO process_sessions(process_id, scope_id, kind, command, started_at, expires_at, status, session_ref, run_id, sandbox_id, purpose, credential_files)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
[
row.processId,
row.scopeId,
@@ -171,10 +197,21 @@ export function createPostgresProcessRegistry(connectionString: string): Process
row.runId ?? null,
row.sandboxId ?? null,
row.purpose ?? null,
+ rec.credentialFiles ? JSON.stringify(rec.credentialFiles) : null,
],
);
return row;
},
+ async credentialFiles(processId) {
+ const rows = await q("SELECT credential_files FROM process_sessions WHERE process_id = $1", [processId]);
+ return (rows[0]?.credential_files as ProcessCredentialFiles | null | undefined) ?? null;
+ },
+ async setCredentialFiles(processId, files) {
+ await q("UPDATE process_sessions SET credential_files = $2 WHERE process_id = $1", [
+ processId,
+ files ? JSON.stringify(files) : null,
+ ]);
+ },
async get(processId) {
const rows = await q("SELECT * FROM process_sessions WHERE process_id = $1", [processId]);
return rows.length ? pgRowToRecord(rows[0]!) : null;
diff --git a/src/processes/reconcile.ts b/src/processes/reconcile.ts
index eb223f727..8048b5480 100644
--- a/src/processes/reconcile.ts
+++ b/src/processes/reconcile.ts
@@ -6,17 +6,25 @@ export async function reconcileProcesses(
handle: SandboxHandle,
registry: ProcessRegistry,
scopeId: string,
+ onExit?: (handle: SandboxHandle, processId: string) => Promise<void>,
): Promise<void> {
- const records = await registry.listByScope(scopeId);
- const running = records.filter((r) => r.status === "running" && (!r.sandboxId || r.sandboxId === handle.resourceId));
- if (!running.length) return;
-
- const live = await sandbox.listProcesses(handle);
- const byId = new Map(live.map((s) => [s.processId, s]));
- for (const rec of running) {
- const backend = byId.get(rec.processId);
- if (!backend || backend.status.state === "exited") {
- await registry.markStatus(rec.processId, "exited");
+ const records = (await registry.listByScope(scopeId)).filter(
+ (r) => !r.sandboxId || r.sandboxId === handle.resourceId,
+ );
+ const running = records.filter((r) => r.status === "running");
+ const finished = records.filter((r) => r.status !== "running" && r.credentialsPending).map((r) => r.processId);
+ if (running.length) {
+ const byId = new Map((await sandbox.listProcesses(handle)).map((s) => [s.processId, s]));
+ for (const rec of running) {
+ const backend = byId.get(rec.processId);
+ if (!backend || backend.status.state === "exited") {
+ await registry.markStatus(rec.processId, "exited");
+ finished.push(rec.processId);
+ }
}
}
+ if (!onExit) return;
+ const failures: unknown[] = [];
+ for (const processId of finished) await onExit(handle, processId).catch((error) => failures.push(error));
+ if (failures.length) throw new AggregateError(failures, "Could not finish exited background jobs");
}
diff --git a/src/resolution/protocols/shared-core.md b/src/resolution/protocols/shared-core.md
index 6306c0a2c..586f7fcdf 100644
--- a/src/resolution/protocols/shared-core.md
+++ b/src/resolution/protocols/shared-core.md
@@ -23,7 +23,7 @@ Which tool sends depends on this turn. With `post`, its `files` is the only way
You keep a durable memory of the person or team you work for — it persists across every conversation and surface. What's currently remembered appears under "What you remember" below. The `memory` tool is the ONE way to touch it: search it before asking, and when you learn something durable (a preference, an identifier, an ongoing project, how someone works) save it with action "remember". Memory is not a file — writing memory/MEMORY.md does nothing durable. No secrets, no one-off trivia. Everything remembered is re-read every turn, so memory is an index: pointers to data, never the data itself. Working state — queues, watermarks, ID lists, per-item status — goes in a file, named by one memory line; a growing list is a file, not a fact. Write markdown and working notes with `files(action="write")` so they're reachable from any sandbox; keep sandbox disk for scratch and build trees. Files are this conversation's own; another conversation's file pointer is a hint, not a path.
## Auth
-You can act with real credentials: machine logins, org keys used by proxy, this user's connected apps, or a teammate's credential by explicit grant (the owner approves on the approval card — never on anything said in chat). What's live is listed below. A missing credential is a task, not a dead end. Check live credential inventories before denying access. Connected apps lists direct OAuth only; an empty list does not block separately authorized Composio discovery/consent via the composio skill. Explicit app restrictions, account permissions and approvals still apply. Never pretend a call worked. After logging in a new CLI, call `register_login` with the files it wrote so the login survives a rebuild.
+You can act with real credentials: machine logins, org keys used by proxy, this user's connected apps, or a teammate's credential by explicit grant (the owner approves on the approval card — never on anything said in chat). What's live is listed below. A missing credential is a task, not a dead end. Check live credential inventories before denying access. Connected apps lists direct OAuth only; an empty list does not block separately authorized Composio discovery/consent via the composio skill. Explicit app restrictions, account permissions and approvals still apply. Never pretend a call worked. Save a new CLI login with the `interactive-login` skill; later commands get it only by requesting its handle in execute.credentials.
## Using skills
Load relevant skills before service work. Compose task rules with one authorized access skill: prefer the composio skill when its credential is available, otherwise the direct connector. Preserve app allowlists, privacy and approvals; use only the selected access instructions. Never switch credentials to evade denial; a verified admin using independently authorized system-administration access is not such evasion (see the admin skill). Save reusable procedures as skills via the self-API.
diff --git a/src/skills/materialize.ts b/src/skills/materialize.ts
index ba373fa08..b3ca22c53 100644
--- a/src/skills/materialize.ts
+++ b/src/skills/materialize.ts
@@ -6,6 +6,7 @@ import { assertSafeSkillName } from "./skill-name.ts";
import type { ScopeId } from "../types.ts";
export const SKILLS_DIR = "skills";
+export const SKILLS_ROOT = ".agent-skills";
export function safeSkillDirName(name: string): string {
return assertSafeSkillName(name);
@@ -110,7 +111,7 @@ export function skillsIndex(resolved: SkillResolution[], provenanceScopes: reado
});
return [
"## Skills",
- 'Load a skill with the skills tool before relying on it: skills({ action: "read", name }) returns its instructions without starting a sandbox. When a skill ships scripts or supporting files, the same call syncs them into a directory that lives for this turn and names it; run and read them there.',
+ 'Load a skill with the skills tool before relying on it: skills({ action: "read", name }) returns its instructions without starting a sandbox. When a skill ships scripts or supporting files, the same call syncs them into a stable directory on the computer and names it; run and read them there. Reading the skill again refreshes that copy.',
...lines,
].join("\n");
}
diff --git a/src/tools/primitives.ts b/src/tools/primitives.ts
index 08b6df1d3..2ec484366 100644
--- a/src/tools/primitives.ts
+++ b/src/tools/primitives.ts
@@ -1,3 +1,8 @@
+import {
+ prepareExecutionFiles,
+ processCredentialFiles,
+ type ExecutionFileCredential,
+} from "../credentials/execute-files.ts";
import type { MemoryCaptureMetadata } from "../memory/records.ts";
import { disclosedMemory } from "../memory/disclosure.ts";
import { MaskedExecutionError, executionSecretEnv, createExactSecretValueMasker } from "../security/secret-masking.ts";
@@ -39,6 +44,7 @@ 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";
import type {
+ BackgroundCredentials,
BackgroundExecBroker,
BackgroundStartResult,
BackgroundPollResult,
@@ -53,7 +59,7 @@ import { carriesGitMetadata } from "../deploy/deploy-fs.ts";
import type { AclStore } from "../acl/acl-store.ts";
import type { AuditLog } from "../audit/audit-log.ts";
import { mimeFromName } from "../core/attachments.ts";
-import { swallow, errMessage } from "../util/errors.ts";
+import { swallow, errMessage, withCleanup } from "../util/errors.ts";
import { fileArtifactId, type FileArtifactStore } from "../files/file-artifact-store.ts";
import type { ScopedConfigStore } from "../resolution/config-store.ts";
import { MEMORY_FILE, type MemoryService } from "../memory/memory-service.ts";
@@ -178,6 +184,7 @@ export interface CommandCredential {
resolve?: () => Promise<{
commit?: () => Promise<void>;
singleUse?: boolean;
+ files?: ExecutionFileCredential;
env: Array<{ key: string; value: string; secret?: boolean }>;
}>;
}
@@ -200,10 +207,6 @@ export interface ToolContext extends SurfaceToolDeps {
/** True when a person's own message started this turn (not a cron, webhook, ambient or delegated wake). */
humanTurn?: boolean;
commandCredentialHandles?: readonly string[];
- registerLogin?(
- service: string,
- paths: readonly CredentialPathSpec[],
- ): Promise<{ service: string; captured: boolean }>;
execute(
command: string,
opts?: {
@@ -243,7 +246,7 @@ export interface ToolContext extends SurfaceToolDeps {
): Promise<ClientToolResult | "timeout" | "cancelled">;
backgroundStart(
command: string,
- opts: { purpose: string; ttlSeconds?: number; sandboxId?: string },
+ opts: { purpose: string; ttlSeconds?: number; sandboxId?: string; credentials?: string[] },
): Promise<BackgroundStartResult>;
backgroundPoll(
processId: string,
@@ -443,13 +446,8 @@ export const CONTROL_UNAVAILABLE: ControlUnavailable = {
export interface ToolContextDeps {
sandbox: Sandbox;
- registerLogin?: ToolContext["registerLogin"];
commandCredentials?: readonly CommandCredential[];
resolveCommandCredentials?: (handles: readonly string[]) => Promise<readonly CommandCredential[]>;
- commandPolicyForCredentials?: (
- handles: readonly string[],
- ownerAuth: boolean,
- ) => ReturnType<ToolContextDeps["commandPolicy"]>;
provision: () => Promise<SandboxHandle>;
provisionScratch?: () => Promise<SandboxHandle>;
provisionResource?: (
@@ -459,7 +457,6 @@ export interface ToolContextDeps {
accessSandboxResource?: (id: string) => Promise<SandboxAccessPlan>;
provisionOwnerAuth?: () => Promise<SandboxHandle>;
ownerAuthCommand?: (command: string, env?: Record<string, string>) => string;
- scopedCommand?: (command: string, env?: Record<string, string>) => string;
useSkill?: (name: string, path: string, sandboxId?: string) => Promise<SkillResult>;
reach?: {
resolveChannel(query: string): Promise<ReachResolution>;
@@ -632,8 +629,78 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
return cache ? once(call, cache) : call();
}
+ const selectCredentials = async (handles: readonly string[], ownerAuth: boolean) => {
+ const available = new Map(
+ (
+ (handles.length ? await deps.resolveCommandCredentials?.(handles) : undefined) ??
+ deps.commandCredentials ??
+ []
+ ).map((credential) => [credential.handle, credential] as const),
+ );
+ return handles.map((handle) => {
+ const credential = available.get(handle);
+ if (!credential) throw new Error(`credential handle is not available on this turn: ${handle}`);
+ if ((credential.scope ?? "scoped") !== (ownerAuth ? "owner" : "scoped"))
+ throw new Error(`credential ${handle} requires scope:${credential.scope ?? "scoped"}`);
+ return credential;
+ });
+ };
+
+ const stageCredentials = async (
+ requested: readonly CommandCredential[],
+ handle: SandboxHandle,
+ use: "command" | "background",
+ signal?: AbortSignal,
+ ) => {
+ const commandEnv: Record<string, string> = {};
+ const prepared: Array<{
+ commit?: () => Promise<void>;
+ singleUse?: boolean;
+ files?: ExecutionFileCredential;
+ env: Array<{ key: string; value: string; secret?: boolean }>;
+ }> = [];
+ for (const credential of new Set(requested)) {
+ const materialized = credential.resolve ? await credential.resolve() : { env: credential.env ?? [] };
+ prepared.push(materialized);
+ for (const { key, value } of materialized.env) {
+ if (key in commandEnv && commandEnv[key] !== value)
+ throw new Error(`requested credentials provide conflicting environment key: ${key}`);
+ commandEnv[key] = value;
+ }
+ deps.auditLog?.record({
+ at: Date.now(),
+ principalId: deps.createdBy,
+ action: "keychain.materialize",
+ resource: `${credential.handle} (${use})`,
+ scopeLabel: writableScopeId!,
+ });
+ }
+ signal?.throwIfAborted();
+ const singleUse = prepared.filter((credential) => credential.singleUse);
+ if (singleUse.length > 1) throw new Error("An execution may use at most one single-use grant");
+ for (const credential of prepared.filter((credential) => !credential.singleUse)) await credential.commit?.();
+ signal?.throwIfAborted();
+ for (const credential of singleUse) await credential.commit?.();
+ const fileCredentials = prepared.flatMap((credential) => (credential.files ? [credential.files] : []));
+ const fileExecution = fileCredentials.length
+ ? await prepareExecutionFiles(deps.sandbox, handle, fileCredentials)
+ : undefined;
+ for (const [key, value] of Object.entries(fileExecution?.env ?? {})) {
+ if (key in commandEnv && commandEnv[key] !== value) {
+ await fileExecution!.finish();
+ throw new Error(`requested credentials provide conflicting environment key: ${key}`);
+ }
+ commandEnv[key] = value;
+ }
+ return {
+ commandEnv,
+ secretValues: prepared.flatMap((credential) => credential.env),
+ fileExecution,
+ fileCredentials,
+ };
+ };
+
return {
- ...(deps.registerLogin ? { registerLogin: deps.registerLogin } : {}),
...(deps.commandCredentials?.length
? { commandCredentialHandles: deps.commandCredentials.map((credential) => credential.handle) }
: {}),
@@ -738,20 +805,7 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
if (requestedCredentials.length && (execOpts?.reachTarget !== undefined || !writableScopeId)) {
throw new Error("command credentials are available only on scoped, scratch, or owner computers");
}
- const availableCredentials = new Map(
- (
- (requestedCredentials.length ? await deps.resolveCommandCredentials?.(requestedCredentials) : undefined) ??
- deps.commandCredentials ??
- []
- ).map((credential) => [credential.handle, credential] as const),
- );
- const requested = requestedCredentials.map((handle) => {
- const credential = availableCredentials.get(handle);
- if (!credential) throw new Error(`credential handle is not available on this turn: ${handle}`);
- if ((credential.scope ?? "scoped") !== (ownerAuth ? "owner" : "scoped"))
- throw new Error(`credential ${handle} requires scope:${credential.scope ?? "scoped"}`);
- return credential;
- });
+ const requested = await selectCredentials(requestedCredentials, ownerAuth);
const reachTarget = execOpts?.reachTarget;
if (
[scratch, ownerAuth, reachTarget !== undefined, execOpts?.sandboxId !== undefined].filter(Boolean).length > 1
@@ -775,8 +829,7 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
if (target.kind === "error") throw new Error(target.message);
reached = { scopeId: target.scopeId, label: `#${target.channelName}` };
}
- const executionPolicy = () =>
- deps.commandPolicyForCredentials?.(requestedCredentials, ownerAuth) ?? deps.commandPolicy();
+ const executionPolicy = deps.commandPolicy;
let handle;
if (execOpts?.sandboxId) {
if (!deps.sandboxResources || !deps.provisionResource) throw new Error("sandbox inventory unavailable");
@@ -816,55 +869,34 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
}
return timed("exec", async () => {
execOpts?.signal?.throwIfAborted();
- const commandEnv: Record<string, string> = {};
- const prepared: Array<{
- commit?: () => Promise<void>;
- singleUse?: boolean;
- env: Array<{ key: string; value: string; secret?: boolean }>;
- }> = [];
- for (const credential of new Set(requested)) {
- const materialized = credential.resolve ? await credential.resolve() : { env: credential.env ?? [] };
- prepared.push(materialized);
- for (const { key, value } of materialized.env) {
- if (key in commandEnv && commandEnv[key] !== value)
- throw new Error(`requested credentials provide conflicting environment key: ${key}`);
- commandEnv[key] = value;
- }
- deps.auditLog?.record({
- at: Date.now(),
- principalId: deps.createdBy,
- action: "keychain.materialize",
- resource: `${credential.handle} (command)`,
- scopeLabel: writableScopeId!,
- });
- }
- execOpts?.signal?.throwIfAborted();
- const singleUse = prepared.filter((credential) => credential.singleUse);
- if (singleUse.length > 1) throw new Error("An execution may use at most one single-use grant");
- for (const credential of prepared.filter((credential) => !credential.singleUse)) await credential.commit?.();
- execOpts?.signal?.throwIfAborted();
- for (const credential of singleUse) await credential.commit?.();
- const sandboxCommand = ownerAuth
- ? (deps.ownerAuthCommand?.(command, commandEnv) ?? command)
- : (deps.scopedCommand?.(command, { ...handle.env, ...commandEnv }) ?? command);
- const commandHandle = Object.keys(commandEnv).length
- ? { ...handle, env: { ...handle.env, ...commandEnv } }
- : handle;
- const secretEnv = executionSecretEnv(
- commandHandle.env,
- prepared.flatMap((credential) => credential.env),
+ const { commandEnv, secretValues, fileExecution } = await stageCredentials(
+ requested,
+ handle,
+ "command",
+ execOpts?.signal,
+ );
+ const r = await withCleanup(
+ async (): Promise<ExecResult> => {
+ const sandboxCommand = ownerAuth ? (deps.ownerAuthCommand?.(command, commandEnv) ?? command) : command;
+ const commandHandle = Object.keys(commandEnv).length
+ ? { ...handle, env: { ...handle.env, ...commandEnv } }
+ : handle;
+ const secretEnv = executionSecretEnv(commandHandle.env, secretValues);
+ const mask = createExactSecretValueMasker(Object.values(secretEnv));
+ try {
+ const result = await deps.sandbox.run(commandHandle, sandboxCommand, opts);
+ return { ...result, stdout: mask(result.stdout), stderr: mask(result.stderr) };
+ } catch (error) {
+ const message = errMessage(error);
+ const masked = mask(message);
+ if (masked !== message) throw new MaskedExecutionError(error, mask, masked);
+ throw error;
+ }
+ },
+ async () => {
+ await fileExecution?.finish();
+ },
);
- const mask = createExactSecretValueMasker(Object.values(secretEnv));
- let r: ExecResult;
- try {
- const result = await deps.sandbox.run(commandHandle, sandboxCommand, opts);
- r = { ...result, stdout: mask(result.stdout), stderr: mask(result.stderr) };
- } catch (error) {
- const message = errMessage(error);
- const masked = mask(message);
- if (masked !== message) throw new MaskedExecutionError(error, mask, masked);
- throw error;
- }
return reached ? { ...r, reached } : r;
});
});
@@ -1190,26 +1222,54 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
async backgroundStart(
command: string,
- opts: { purpose: string; ttlSeconds?: number; sandboxId?: string },
+ opts: { purpose: string; ttlSeconds?: number; sandboxId?: string; credentials?: string[] },
): Promise<BackgroundStartResult> {
if (!deps.backgroundBroker) throw new Error(BACKGROUND_UNAVAILABLE_MESSAGE);
+ const requestedCredentials = opts.credentials ?? [];
+ if (requestedCredentials.length && !writableScopeId)
+ throw new Error("command credentials are available only on scoped computers");
+ const requested = await selectCredentials(requestedCredentials, false);
+ const policy = deps.commandPolicy;
let handle: SandboxHandle;
if (opts?.sandboxId) {
if (!deps.sandboxResources || !deps.provisionResource) throw new Error("sandbox inventory unavailable");
const access = await accessSandbox(opts.sandboxId);
- handle = await deps.provisionResource(access, (current) => authorizeExecution(command, current));
+ handle = await deps.provisionResource(access, (current) => {
+ if (current.crossScope && requestedCredentials.length)
+ throw new Error("command credentials cannot be copied to another scope's computer");
+ authorizeExecution(command, current, policy());
+ });
} else {
- authorizeExecution(command);
+ authorizeExecution(command, undefined, policy());
handle = await deps.provision();
}
return once(
- () =>
- deps.backgroundBroker!.start(
+ async () => {
+ const staged = requested.length ? await stageCredentials(requested, handle, "background") : undefined;
+ const release = async () => {
+ await staged?.fileExecution?.finish();
+ };
+ let credentials: BackgroundCredentials | undefined;
+ try {
+ credentials = staged && {
+ env: staged.commandEnv,
+ secrets: Object.values(executionSecretEnv(staged.commandEnv, staged.secretValues)),
+ ...(staged.fileExecution
+ ? { files: processCredentialFiles(staged.fileExecution.plan, staged.fileCredentials) }
+ : {}),
+ release,
+ };
+ } catch (error) {
+ return withCleanup(() => Promise.reject(error), release);
+ }
+ return deps.backgroundBroker!.start(
handle,
- deps.scopedCommand?.(command, handle.env) ?? command,
+ command,
opts.purpose,
opts?.ttlSeconds ? opts.ttlSeconds * 1000 : undefined,
- ),
+ credentials,
+ );
+ },
() => true,
);
},
diff --git a/src/triggers/keychain-ask.ts b/src/triggers/keychain-ask.ts
index 4edfe6ada..b04508160 100644
--- a/src/triggers/keychain-ask.ts
+++ b/src/triggers/keychain-ask.ts
@@ -7,7 +7,6 @@ import { principalDestination, withWebTranscriptText } from "../reach/reach.ts";
import { swallow } from "../util/errors.ts";
import { cronIdOf } from "../sessions/session-store.ts";
import { samePerson } from "../directory/person.ts";
-import { keychainUseCommand } from "../api/contract.ts";
function resolutionInput(ask: KeychainAsk, grant?: KeychainGrant): string {
if (ask.status === "approved") {
@@ -102,6 +101,7 @@ export interface DropResolution {
destination?: Destination;
threadRef?: string;
grantId?: string;
+ credentialHandle?: string;
granted: boolean;
pendingSiblings?: string[];
}
@@ -109,7 +109,7 @@ export interface DropResolution {
function dropResolutionInput(drop: DropResolution): string {
const waiting = !!drop.pendingSiblings?.length;
const where = drop.granted
- ? `now in the keychain and granted to this conversation — load it with \`${keychainUseCommand({ grant: String(drop.grantId) })}\`${waiting ? " when the task runs" : " and run the task in that same shell"}`
+ ? `now in the keychain and granted to this conversation — request \`${drop.credentialHandle}\` in execute.credentials${waiting ? " when the task runs" : " when resuming the task"}`
: `now in your keychain and available here`;
const next = waiting
? `Heads-up: other drop links from this conversation (${drop.pendingSiblings!.map((s) => `\`${s}\``).join(", ")}) haven't been filled yet — ` +
diff --git a/src/util/errors.ts b/src/util/errors.ts
index d9588c74a..379166e93 100644
--- a/src/util/errors.ts
+++ b/src/util/errors.ts
@@ -54,3 +54,17 @@ export async function httpFailure(res: Response, bodyChars = 200): Promise<strin
const body = (await res.text().catch(() => "")).slice(0, bodyChars);
return withRequestId(`http ${res.status} ${body}`, res.headers);
}
+
+export async function withCleanup<T>(work: () => Promise<T>, cleanup: () => Promise<void>): Promise<T> {
+ let result: T;
+ try {
+ result = await work();
+ } catch (error) {
+ await cleanup().catch((cleanupError: unknown) => {
+ throw new AggregateError([error, cleanupError], errMessage(error), { cause: error });
+ });
+ throw error;
+ }
+ await cleanup();
+ return result;
+}
diff --git a/src/wiring.ts b/src/wiring.ts
index 4c40340b5..063032ed1 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -265,14 +265,7 @@ import {
type DropResolution,
} from "./triggers/keychain-ask.ts";
import { createSecretDropStore, type SecretDropStore, type SecretDropRecord } from "./credentials/secret-drop.ts";
-import { createLivenessCache, type LivenessCache, type ScopeLivenessRecord } from "./credentials/resident-auth.ts";
import { createConnectorStatusCache, type ConnectorStatusRecord } from "./credentials/connector-status.ts";
-import {
- createDeviceFlowCutoverStore,
- type DeviceFlowCutoverPolicy,
- type DeviceFlowCutoverReset,
- type DeviceFlowCutoverStore,
-} from "./credentials/device-flow-cutover.ts";
import {
createFeatureFlagStore,
externalAppSharingAllowed,
@@ -354,7 +347,8 @@ import { createProcessReaper, createReaperKillHook, type ProcessReaper } from ".
import { createMonitorStore, type MonitorStore } from "./monitors/monitor-store.ts";
import { createMonitorPoller, type MonitorPoller } from "./monitors/monitor-poller.ts";
import { createSkillSyncEngine, type SkillSyncEngine } from "./skills/skill-sync-engine.ts";
-import { supportsProcessSessions } from "./sandbox/sandbox.ts";
+import { supportsProcessSessions, type SandboxHandle } from "./sandbox/sandbox.ts";
+import { finishProcessCredentials } from "./credentials/execute-files.ts";
import { createTurnStream } from "./runs/turn-stream.ts";
import { createMemorySessionStateBus, type SessionStateBus } from "./runs/session-state-bus.ts";
import { createPostgresSessionStateBus } from "./runs/postgres-session-state-bus.ts";
@@ -406,7 +400,6 @@ import { createMemoryReplayDedupe, createPostgresReplayDedupe, type ReplayDedupe
import {
emptyDeploymentLayer,
loadDeploymentLayer,
- type LayerCredentialTool,
type BrokeredLayerTool,
type DeploymentLayerRuntime,
} from "./deployment/load-layer.ts";
@@ -475,7 +468,6 @@ export interface BuiltApp {
app: App;
screenSecurity?: SecurityScreenProbe;
deploymentLayer: DeploymentLayerRuntime;
- credentialTools: readonly LayerCredentialTool[];
brokeredTools: readonly BrokeredLayerTool[];
deploymentLayerStore: DeploymentLayerStore;
deploymentLayerReady: Promise<unknown>;
@@ -541,8 +533,6 @@ export interface BuiltApp {
blobTransfer: BlobTransferStore;
files: FileArtifactStore;
fileUploads?: DirectFileUploads;
- livenessCache: LivenessCache;
- deviceFlowCutover: DeviceFlowCutoverStore;
featureFlags: FeatureFlagStore;
replayDedupe?: ReplayDedupe;
brokerSessions?: BrokerSessionStore;
@@ -705,10 +695,6 @@ export function buildApp(
});
const skillPacks = createSkillPackStore({ backing: artifactMap<SkillPack>("skill_packs") });
const skillBundles = createSkillBundleStore({ backing: artifactMap<SkillBundle>("skill_bundles") });
- const livenessCache = createLivenessCache(artifactMap<ScopeLivenessRecord>("credential_liveness"));
- const deviceFlowCutover = createDeviceFlowCutoverStore(artifactMap<DeviceFlowCutoverPolicy>("device_flow_cutover"), {
- resets: artifactMap<DeviceFlowCutoverReset>("device_flow_cutover_resets"),
- });
const featureFlags = createFeatureFlagStore(artifactMap<FeatureFlagRecord>("feature_flags"));
const connectorStatusCache = createConnectorStatusCache(artifactMap<ConnectorStatusRecord>("connector_status"));
const slackInstallation = createSlackInstallationStore(
@@ -720,7 +706,6 @@ export function buildApp(
? loadDeploymentLayer(config.deploymentLayerDir)
: emptyDeploymentLayer();
const layerSkillsDir = config.deploymentLayerDir ? resolve(deploymentLayer.dir, "skills") : undefined;
- const credentialTools = deploymentLayer.credentialTools;
const brokeredTools = deploymentLayer.brokeredTools;
const orgScope = scopeId("org", config.orgId);
const auditLog = config.databaseUrl ? createPostgresAuditLog(config.databaseUrl) : createAuditLog();
@@ -1194,6 +1179,15 @@ export function buildApp(
...deriveConnectorKey(keychainKeyMaterial ?? randomBytes(32), "keychain"),
...(legacyCredentialKey ? { fallbacks: [legacyCredentialKey] } : {}),
};
+ pgArtifactMap?.pool.registerMigration({
+ id: "durable-map/keychain_credentials/0002-delete-auto-captured-logins",
+ statements: [
+ "CREATE TABLE IF NOT EXISTS keychain_credentials (id TEXT PRIMARY KEY, json JSONB NOT NULL)",
+ "CREATE TABLE IF NOT EXISTS keychain_grants (id TEXT PRIMARY KEY, json JSONB NOT NULL)",
+ `DELETE FROM keychain_grants WHERE json ->> 'credentialId' IN (SELECT id FROM keychain_credentials WHERE json ->> 'origin' = 'device-flow-auto-capture')`,
+ `DELETE FROM keychain_credentials WHERE json ->> 'origin' = 'device-flow-auto-capture'`,
+ ],
+ });
const credentialStore: Keychain = createKeychain({
creds: artifactMap<KeychainCredential>("keychain_credentials", ["ownerId"]),
grants: artifactMap<KeychainGrant>("keychain_grants", ["ownerId"]),
@@ -1970,8 +1964,6 @@ export function buildApp(
runs,
tasks,
blobTransfer,
- livenessCache,
- deviceFlowCutover,
featureFlags,
credentialUsage,
connectorStatusCache,
@@ -1995,7 +1987,6 @@ export function buildApp(
...(config.surfaceDebugFooter ? { surfaceDebugFooter: true } : {}),
...(config.eagerProvisionEnabled ? { eagerProvision: true } : {}),
environments,
- credentialTools,
brokeredTools,
deploymentLayer,
layerBrokerFor,
@@ -2522,6 +2513,8 @@ export function buildApp(
},
app,
);
+ const finishCredentials = (handle: SandboxHandle, processId: string) =>
+ finishProcessCredentials({ sandbox, processes: processes!, ...(keychain ? { keychain } : {}) }, handle, processId);
const monitorPoller: MonitorPoller | null =
processes && supportsProcessSessions(sandbox)
? createMonitorPoller({
@@ -2537,6 +2530,7 @@ export function buildApp(
currentScopeMembers,
sessions,
leaderLease,
+ onProcessExit: finishCredentials,
heartbeatMs: config.monitorHeartbeatMs,
})
: null;
@@ -2587,7 +2581,9 @@ export function buildApp(
const processReaper: ProcessReaper | null = processes
? createProcessReaper(processes, {
intervalMs: config.processReaperIntervalMs,
- ...(supportsProcessSessions(sandbox) ? { kill: createReaperKillHook(sandbox) } : {}),
+ ...(supportsProcessSessions(sandbox)
+ ? { kill: createReaperKillHook(sandbox, { onExit: finishCredentials }) }
+ : {}),
leaderLease,
})
: null;
@@ -2793,7 +2789,6 @@ export function buildApp(
...(screenSecurity ? { screenSecurity } : {}),
deploymentLayer,
deploymentLayerStore,
- credentialTools,
brokeredTools,
deploymentLayerReady,
deploymentLayerRefresh,
@@ -2858,8 +2853,6 @@ export function buildApp(
blobTransfer,
files,
...(fileUploads ? { fileUploads } : {}),
- livenessCache,
- deviceFlowCutover,
featureFlags,
...(replayDedupe ? { replayDedupe } : {}),
...(brokerSessions ? { brokerSessions } : {}),
@@ -2963,7 +2956,6 @@ export function serverDeps(
rateLimiter: built.rateLimiter,
acl: built.acl,
credentialUsage: built.credentialUsage,
- deviceFlowCutover: built.deviceFlowCutover,
featureFlags: built.featureFlags,
egressAudit: built.egressAudit,
sessions: built.sessions,
@@ -2972,7 +2964,6 @@ export function serverDeps(
metrics: built.metrics,
crons: built.crons,
loops: built.loops,
- credentialServices: () => built.credentialTools.map((tool) => tool.service),
deploymentLayer: built.deploymentLayerStore,
deployDialTimeoutMs: config.deployDialTimeoutMs,
...(config.awsDeploy.appsDomain ? { deployAppsDomain: config.awsDeploy.appsDomain } : {}),
diff --git a/test/admin-resources.test.ts b/test/admin-resources.test.ts
index ec848efdb..57e866a67 100644
--- a/test/admin-resources.test.ts
+++ b/test/admin-resources.test.ts
@@ -44,9 +44,7 @@ function start(harnessId = "pi", withLayer = true): { base: string; built: Built
sessions: built.sessions,
acl: built.acl,
serviceCreds: built.serviceCreds,
- deviceFlowCutover: built.deviceFlowCutover,
featureFlags: built.featureFlags,
- credentialServices: () => built.credentialTools.map((tool) => tool.service),
channelPolicy: built.channelPolicy,
harnessId,
});
@@ -431,64 +429,6 @@ test("feature flag table changes one scope live without restart", async () => {
}
});
-test("device-flow cutover is scope-specific, audited, and reverses without deleting records", async () => {
- const srv = start();
- try {
- const scope = "channel:C1";
- const endpoint = `${srv.base}/v1/admin/scopes/${encodeURIComponent(scope)}/device-flow-cutover`;
- const prefer = await fetch(endpoint, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ service: "acmecli", mode: "prefer_ephemeral" }),
- });
- assert.equal(prefer.status, 200);
- assert.equal(await srv.built.deviceFlowCutover.resolve(scope, "acmecli"), "prefer_ephemeral");
-
- const read = await fetch(`${srv.base}/v1/admin/scopes/${encodeURIComponent(scope)}`, { headers: ADMIN });
- const body = (await read.json()) as {
- deviceFlowCutover: { acmecli: { effective: string; configured: { updatedBy: string } } };
- };
- assert.equal(body.deviceFlowCutover.acmecli.effective, "prefer_ephemeral");
- assert.equal(body.deviceFlowCutover.acmecli.configured.updatedBy, "admin-alice");
-
- const unsupported = await fetch(endpoint, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ service: "aws", mode: "ephemeral_only" }),
- });
- assert.equal(unsupported.status, 400);
- assert.match(await unsupported.text(), /no credential paths/);
-
- const rollback = await fetch(endpoint, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ service: "acmecli", mode: "legacy" }),
- });
- assert.equal(rollback.status, 200);
- assert.equal(await srv.built.deviceFlowCutover.resolve(scope, "acmecli"), "legacy");
-
- await srv.built.deviceFlowCutover.set("org:default-org", "acmecli", "prefer_ephemeral", "admin-alice");
- const inherit = await fetch(endpoint, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ service: "acmecli", mode: "inherit" }),
- });
- assert.equal(inherit.status, 200);
- assert.equal(await srv.built.deviceFlowCutover.get(scope, "acmecli"), null);
- assert.equal(await srv.built.deviceFlowCutover.resolve(scope, "acmecli"), "prefer_ephemeral");
- assert.ok((await srv.built.auditLog.events()).some((event) => event.action === "device-flow-cutover.update"));
- assert.ok(
- (await srv.built.auditLog.events()).some(
- (event) =>
- event.action === "credential.cutover.update" &&
- event.resource === "acmecli:legacy/legacy->inherit/prefer_ephemeral",
- ),
- );
- } finally {
- await srv.close();
- }
-});
-
test("security posture round-trips through durable scoped governance", async () => {
const srv = start();
try {
@@ -912,32 +852,6 @@ test("GET /v1/admin/slack-emoji surfaces 404 without a token, and serves the plu
}
});
-test("historical cutover policies remain visible and clearable without layer tools", async () => {
- const srv = start("pi", false);
- try {
- const scope = "channel:C1";
- await srv.built.deviceFlowCutover.set("org:default-org", "retired", "ephemeral_only", "admin");
- const read = await fetch(`${srv.base}/v1/admin/scopes/${encodeURIComponent(scope)}`, { headers: ADMIN });
- const body = (await read.json()) as { deviceFlowCutover: { retired: { effective: string } } };
- assert.equal(body.deviceFlowCutover.retired.effective, "ephemeral_only");
- const update = await fetch(`${srv.base}/v1/admin/scopes/${encodeURIComponent(scope)}/device-flow-cutover`, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ service: "retired", mode: "legacy" }),
- });
- assert.equal(update.status, 200);
- const clear = await fetch(`${srv.base}/v1/admin/scopes/${encodeURIComponent(scope)}/device-flow-cutover`, {
- method: "PUT",
- headers: ADMIN,
- body: JSON.stringify({ service: "retired", mode: "inherit" }),
- });
- assert.equal(clear.status, 200);
- assert.equal(await srv.built.deviceFlowCutover.resolve(scope, "retired"), "ephemeral_only");
- } finally {
- await srv.close();
- }
-});
-
test("org purpose runtimes round-trip, validate, and clear independently", async () => {
const srv = start();
const scopeUrl = `${srv.base}/v1/admin/scopes/org:default-org`;
diff --git a/test/aws-role-cutover.test.ts b/test/aws-role-cutover.test.ts
deleted file mode 100644
index 195664ae7..000000000
--- a/test/aws-role-cutover.test.ts
+++ /dev/null
@@ -1,625 +0,0 @@
-import { test, before, after, beforeEach } from "node:test";
-import assert from "node:assert/strict";
-import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
-import { tmpdir } from "node:os";
-import { join } from "node:path";
-import { buildApp } from "../src/wiring.ts";
-import { credentialHandle } from "../src/credentials/keychain.ts";
-import { DEVICE_FLOW_ORIGIN } from "../src/credentials/device-flow-persist.ts";
-import { scopeId } from "../src/types.ts";
-import { installGlobalFakeSprites, type FakeSprites } from "./support/fake-sprites.ts";
-import { testConfig } from "./support/test-config.ts";
-import { createAwsRoleBroker } from "../src/auth/aws-role-broker.ts";
-import { selectDefaultSandbox } from "./support/default-sandbox.ts";
-
-let ff: FakeSprites;
-before(() => {
- ff = installGlobalFakeSprites();
-});
-beforeEach(() => ff.reset());
-after(() => ff.cleanup());
-
-function acmecliBrokeredLayer(binary?: string, approvals?: Array<{ pattern: string; reason?: string }>): string {
- const dir = mkdtempSync(join(tmpdir(), "dfp-layer-"));
- mkdirSync(join(dir, "tools/acmecli"), { recursive: true });
- writeFileSync(
- join(dir, "tools/acmecli/tool.json"),
- JSON.stringify({
- id: "acmecli",
- ...(binary ? { install: { binary } } : {}),
- ...(approvals ? { approvals } : {}),
- auth: {
- check: "acmecli me",
- reauth: "acmecli login --use-device-code",
- credentialPaths: [{ path: ".acmecli", kind: "directory" }],
- broker: {
- kind: "aws-role",
- roleArnEnv: "TEST_BROKER_ROLE_ARN",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- },
- },
- }),
- );
- return dir;
-}
-
-test("a scope allow rule cannot override the ephemeral_only direct-execution deny", async () => {
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-credexec-scope-allow-")),
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliBrokeredLayer("env"),
- }),
- {
- credentialBrokers: {
- acmecli: createAwsRoleBroker({
- roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- assumeRole: async () => ({
- Credentials: {
- AccessKeyId: "AKIA_SCOPE_ALLOW",
- SecretAccessKey: "scope_allow_secret_value",
- SessionToken: "scope_allow_session_token",
- Expiration: new Date(Date.now() + 3_600_000),
- },
- }),
- }),
- },
- },
- );
- const personal = scopeId("personal", actor.externalId);
- built.config.setCommandPolicy(personal, {
- mode: "denylist",
- rules: [{ pattern: "\\benv\\b", decision: "allow" }],
- });
- const conversation = { kind: "dm" as const, threadRef: "dm:credexec-scope-allow", audience: [actor] };
- await built.deviceFlowCutover.set(personal, "acmecli", "ephemeral_only", "[email protected]");
-
- const direct = await built.app.turn({ surface: "slack", actor, conversation, text: "!run env" });
- assert.match(
- `${direct.reason ?? ""} ${direct.reply ?? ""}`,
- /requires execute with its broker credential and scope:owner/,
- );
-});
-
-const actor = { externalId: "U1" };
-
-test("shared ACMECLI cutover isolates brokered STS without shrinking the existing scopeShared owner union", async () => {
- const acmecliBroker = createAwsRoleBroker({
- roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- assumeRole: async ({ RoleSessionName }) => ({
- Credentials: {
- AccessKeyId: `AKIA_${RoleSessionName}`,
- SecretAccessKey: `secret_${RoleSessionName}`,
- SessionToken: `session_${RoleSessionName}`,
- Expiration: new Date(Date.now() + 3_600_000),
- },
- }),
- });
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-owner-box-")),
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliBrokeredLayer(),
- }),
- { credentialBrokers: { acmecli: acmecliBroker } },
- );
- const bob = { externalId: "BOB" };
- const alice = { externalId: "ALICE" };
- const room = scopeId("channel", "C-owner-auth");
- await selectDefaultSandbox(built, "BOB", room);
- const conversation = {
- kind: "channel" as const,
- threadRef: "ch:C-owner-auth:cron",
- channelRef: "C-owner-auth",
- isPrivate: true,
- audience: [bob, alice],
- };
- const npm = await built.keychain!.save({ ownerId: "BOB", service: "npm", secret: "npm_BOB", envKey: "NPM_TOKEN" });
- const aws = await built.keychain!.save({
- ownerId: "BOB",
- service: "aws",
- secret: "AKIA_BOB_GENERAL",
- envKey: "AWS_ACCESS_KEY_ID",
- });
- await built.keychain!.save({
- ownerId: "BOB",
- service: "acmecorp",
- files: [{ path: ".config/acmecorp/auth.json", contentBase64: Buffer.from("file_BOB").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- await built.keychain!.save({
- ownerId: room,
- service: "acmecli",
- files: [{ path: ".acmecli/session.json", contentBase64: Buffer.from("legacy_room_acmecli").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- await built.deviceFlowCutover.set(room, "acmecli", "prefer_ephemeral", "[email protected]");
- const ambientOwner = await built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:ambient" },
- text: '!owner printf "%s|%s" "${NPM_TOKEN-unset}" "${AWS_ACCESS_KEY_ID-unset}"',
- triggered: true,
- ownerKeychainUnion: true,
- });
- assert.equal(ambientOwner.reply, "unset|unset");
- const owner = await built.app.turn({
- surface: "cron",
- actor: bob,
- conversation,
- text: `!execute ${JSON.stringify({ command: `test "$NPM_TOKEN" = npm_BOB && test "$AWS_ACCESS_KEY_ID" = AKIA_BOB_GENERAL && printf 'selected|%s|%s' "$(cat ~/.config/acmecorp/auth.json)" "\${AGENT_API_TOKEN-unset}"; printf poisoned > ~/.config/acmecorp/auth.json`, credentials: [credentialHandle(npm.id), credentialHandle(aws.id)], ownerAuth: true })}`,
- triggered: true,
- ownerKeychainUnion: true,
- });
- assert.equal(owner.status, "ok", owner.reason);
- assert.equal(owner.reply, "selected|file_BOB|unset");
- assert.equal(
- ff.names().some((n) => n.includes("scratch")),
- false,
- "the owner-auth body is destroyed after success",
- );
-
- const brokeredAcmecli = await built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:brokered-acmecli" },
- text: `!execute ${JSON.stringify({ command: 'test -n "$AWS_ACCESS_KEY_ID" && test "$AWS_ACCESS_KEY_ID" != AKIA_BOB_GENERAL && test -z "${NPM_TOKEN-}" && test -z "${AGENT_API_TOKEN-}" && echo broker-only', credentials: ["broker_acmecli"], ownerAuth: true })}`,
- triggered: true,
- ownerKeychainUnion: true,
- });
- assert.equal(
- brokeredAcmecli.reply,
- "broker-only",
- "selected broker credentials replace ambient owner env and never carry a core API token",
- );
-
- const unpoisoned = await built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:unpoisoned" },
- text: "!owner cat ~/.config/acmecorp/auth.json",
- triggered: true,
- ownerKeychainUnion: true,
- });
- assert.equal(unpoisoned.reply, "file_BOB", "owner-box mutations never capture back into Bob's durable keychain");
- assert.deepEqual(await built.keychain!.grantsForScope(room), []);
- const ownerAudit = await built.auditLog.events();
- assert.ok(
- ownerAudit.some((event) => event.action === "keychain.materialize" && event.resource.includes("owner-auth box")),
- );
- assert.equal(
- ownerAudit.some((event) => event.action === "credential.materialize"),
- false,
- );
-
- const scoped = await built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:scoped" },
- text: '!run printf \'%s|%s|%s|%s\' "${NPM_TOKEN-unset}" "${AWS_ACCESS_KEY_ID-unset}" "$(test -e ~/.config/acmecorp/auth.json && echo found || echo absent)" "$(test -e ~/.acmecli/session.json && echo found || echo absent)"',
- triggered: true,
- ownerKeychainUnion: true,
- });
- assert.equal(scoped.status, "ok", scoped.reason);
- assert.equal(
- scoped.reply,
- "unset|unset|absent|found",
- "prefer-isolated keeps resident ACMECLI as a live fallback without placing Bob's private credentials on the room",
- );
-
- const poisoned = await built.app.turn({
- surface: "slack",
- actor: alice,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:poison" },
- text: "!run printf poisoned > ~/.acmecli/session.json",
- });
- assert.equal(poisoned.status, "ok", poisoned.reason);
-
- const aliceTurn = await built.app.turn({
- surface: "slack",
- actor: alice,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:alice" },
- text: '!run printf \'%s|%s|%s\' "${NPM_TOKEN-unset}" "${AWS_ACCESS_KEY_ID-unset}" "$(test -e ~/.config/acmecorp/auth.json && echo found || echo absent)"',
- });
- assert.equal(aliceTurn.status, "ok", aliceTurn.reason);
- assert.equal(aliceTurn.reply, "unset|unset|absent");
-
- const aliceAcmecli = await built.app.turn({
- surface: "slack",
- actor: alice,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:alice-acmecli" },
- text: '!owner mkdir -p /tmp/bin; printf \'%s\\n\' \'#!/bin/sh\' \'printf "%s" "$AWS_ACCESS_KEY_ID"\' > /tmp/bin/acmecli; chmod +x /tmp/bin/acmecli; export PATH="/tmp/bin:$PATH"; acmecli; printf \'|%s|%s\' "${NPM_TOKEN-unset}" "$(test -e ~/.config/acmecorp/auth.json && echo found || echo absent)"',
- });
- assert.equal(aliceAcmecli.status, "ok", aliceAcmecli.reason);
- assert.equal(
- aliceAcmecli.reply,
- "|unset|absent",
- "direct execution has no brokered identity and no access to Bob's keychain",
- );
- assert.equal(
- ff.names().some((n) => n.includes("scratch")),
- false,
- );
-
- await built.deviceFlowCutover.set(room, "acmecli", "legacy", "[email protected]");
- const rollback = await built.app.turn({
- surface: "slack",
- actor: alice,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:rollback" },
- text: "!run cat ~/.acmecli/session.json",
- });
- assert.equal(
- rollback.reply,
- "legacy_room_acmecli",
- "prefer-mode mutations never poison the encrypted rollback input",
- );
- const rollbackComputer = await built.sandbox.provision([{ scopeId: room, mountPath: "/", mode: "rw" }]);
- assert.equal(
- await built.deviceFlowCutover.residentResetGeneration(room, "acmecli", rollbackComputer.resourceId),
- null,
- "rollback reset is consumed after one verified restore",
- );
-
- await built.deviceFlowCutover.set(room, "acmecli", "ephemeral_only", "[email protected]");
- const requarantined = await built.app.turn({
- surface: "slack",
- actor: alice,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:requarantine" },
- text: "!run test -e ~/.acmecli/session.json && echo found || echo absent",
- });
- assert.equal(
- requarantined.reply,
- "absent",
- "ephemeral-only removes already-materialized legacy files without deleting the stored record",
- );
- const acmecliUsage = await built.credentialUsage.list({ slug: "acmecli" });
- assert.equal(
- acmecliUsage.some((row) => row.status === "ephemeral_vended"),
- true,
- );
- const legacyUsage = await built.credentialUsage.list({ slug: "keychain:acmecli" });
- assert.ok(
- legacyUsage.some((row) => row.status === "legacy_retained"),
- "prefer-isolated records that resident fallback remains present",
- );
-
- const realMaterializeOwnFiles = built.keychain!.materializeOwnFiles.bind(built.keychain!);
- built.keychain!.materializeOwnFiles = async () => {
- throw new Error("owner file materialization failed");
- };
- await assert.rejects(
- built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:init-failure" },
- text: "!owner true",
- triggered: true,
- ownerKeychainUnion: true,
- }),
- /owner file materialization failed/,
- );
- built.keychain!.materializeOwnFiles = realMaterializeOwnFiles;
- assert.equal(
- ff.names().some((n) => n.includes("scratch")),
- false,
- "failed owner-box initialization destroys its pending body",
- );
-
- const realTeardown = built.sandbox.teardown.bind(built.sandbox);
- let ownerDestroyAttempts = 0;
- built.sandbox.teardown = async (handle, opts) => {
- if (handle.scratch && opts?.destroy && ownerDestroyAttempts++ < 2)
- throw new Error("transient owner destroy failure");
- return realTeardown(handle, opts);
- };
- const retriedDestroy = await built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:destroy-retry" },
- text: "!owner true",
- triggered: true,
- ownerKeychainUnion: true,
- });
- built.sandbox.teardown = realTeardown;
- assert.equal(retriedDestroy.status, "ok", retriedDestroy.reason);
- assert.equal(ownerDestroyAttempts, 3, "credential-bearing owner bodies retry destruction before losing the handle");
- assert.equal(
- ff.names().some((n) => n.includes("scratch")),
- false,
- );
-
- let stranded: Parameters<typeof realTeardown>[0] | undefined;
- built.sandbox.teardown = async (handle, opts) => {
- if (handle.scratch && opts?.destroy) {
- stranded = handle;
- throw new Error("persistent control-plane deletion failure");
- }
- return realTeardown(handle, opts);
- };
- await assert.rejects(
- built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:destroy-failure-containment" },
- text: "!owner printf changed > ~/.config/acmecorp/auth.json",
- triggered: true,
- ownerKeychainUnion: true,
- }),
- (error: Error) => {
- assert.equal(error.message, "Disposable sandbox destruction failed");
- assert.ok(error instanceof AggregateError);
- assert.deepEqual(
- error.errors.map((attempt: Error) => attempt.message),
- Array(3).fill("persistent control-plane deletion failure"),
- );
- return true;
- },
- );
- built.sandbox.teardown = realTeardown;
- assert.ok(stranded);
- assert.equal(
- stranded.env?.NPM_TOKEN,
- undefined,
- "long-lived owner env credentials never enter machine configuration",
- );
- assert.equal(
- (await built.sandbox.run(stranded, "test ! -e ~/.config/acmecorp/auth.json")).code,
- 0,
- "owner files are scrubbed before remote deletion is attempted",
- );
- await realTeardown(stranded, { destroy: true });
-
- const realRun = built.sandbox.run.bind(built.sandbox);
- built.sandbox.run = async (handle, command, opts) => {
- if (handle.scratch && command.endsWith("explode-owner")) throw new Error("owner command exploded");
- return realRun(handle, command, opts);
- };
- await assert.rejects(
- built.app.turn({
- surface: "cron",
- actor: bob,
- conversation: { ...conversation, threadRef: "ch:C-owner-auth:throw" },
- text: "!owner explode-owner",
- triggered: true,
- ownerKeychainUnion: true,
- }),
- /owner command exploded/,
- );
- built.sandbox.run = realRun;
- assert.equal(
- ff.names().some((n) => n.includes("scratch")),
- false,
- "the owner-auth body is destroyed after a thrown turn",
- );
-});
-
-test("cutover policy retains legacy files only in prefer-ephemeral mode", async () => {
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-acmecli-fallback-")),
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliBrokeredLayer(),
- }),
- {
- credentialBrokers: {
- acmecli: createAwsRoleBroker({
- roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- assumeRole: async () => {
- throw new Error("STS unavailable");
- },
- }),
- },
- },
- );
- const room = scopeId("channel", "C-acmecli-fallback");
- await selectDefaultSandbox(built, actor.externalId, room);
- await built.keychain!.save({
- ownerId: room,
- service: "acmecli",
- files: [{ path: ".acmecli/session.json", contentBase64: Buffer.from("legacy_ok").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- await built.keychain!.save({
- ownerId: actor.externalId,
- service: "acmecli",
- files: [{ path: ".acmecli/session.json", contentBase64: Buffer.from("owner_legacy").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- const conversation = {
- kind: "channel" as const,
- channelRef: "C-acmecli-fallback",
- isPrivate: true,
- audience: [actor],
- };
-
- await built.deviceFlowCutover.set(room, "acmecli", "prefer_ephemeral", "[email protected]");
- const fallback = await built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-fallback:prefer" },
- text: "!run cat ~/.acmecli/session.json",
- });
- assert.equal(fallback.reply, "legacy_ok");
- await assert.rejects(
- built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-fallback:broker-prefer" },
- text: `!execute ${JSON.stringify({ command: "true", credentials: ["broker_acmecli"], ownerAuth: true })}`,
- }),
- /Could not vend credentials/,
- );
-
- await built.deviceFlowCutover.set(room, "acmecli", "ephemeral_only", "[email protected]");
- const closed = await built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-fallback:only" },
- text: '!run printf \'%s|%s\' "${AWS_ACCESS_KEY_ID-unset}" "$(test -e ~/.acmecli && echo found || echo absent)"',
- });
- assert.equal(closed.reply, "unset|absent");
- await assert.rejects(
- built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-fallback:broker-only" },
- text: `!execute ${JSON.stringify({ command: "true", credentials: ["broker_acmecli"], ownerAuth: true })}`,
- }),
- /Could not vend credentials/,
- );
- assert.ok(
- (await built.credentialUsage.list({ slug: "acmecli" })).some((row) => row.status === "ephemeral_failed_closed"),
- );
-
- const ownerClosed = await built.app.turn({
- surface: "cron",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-fallback:owner-only" },
- text: "!owner test -e ~/.acmecli && echo found || echo absent",
- triggered: true,
- ownerKeychainUnion: true,
- });
- assert.equal(
- ownerClosed.reply,
- "absent",
- "isolated-only never restores an owner's ambient ACMECLI after broker failure",
- );
-});
-
-test("a nonlegacy policy never places brokered STS on a shared room", async () => {
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-acmecli-flag-off-")),
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliBrokeredLayer(),
- }),
- {
- credentialBrokers: {
- acmecli: createAwsRoleBroker({
- roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- assumeRole: async () => ({
- Credentials: {
- AccessKeyId: "AKIA_SHOULD_NOT_REACH_ROOM",
- SecretAccessKey: "secret",
- SessionToken: "session",
- Expiration: new Date(Date.now() + 3_600_000),
- },
- }),
- }),
- },
- },
- );
- const room = scopeId("channel", "C-acmecli-flag-off");
- await selectDefaultSandbox(built, actor.externalId, room);
- await built.keychain!.save({
- ownerId: room,
- service: "acmecli",
- files: [{ path: ".acmecli/session.json", contentBase64: Buffer.from("legacy_ok").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- const conversation = {
- kind: "channel" as const,
- channelRef: "C-acmecli-flag-off",
- audience: [actor],
- };
-
- await built.deviceFlowCutover.set(room, "acmecli", "prefer_ephemeral", "[email protected]");
- const prefer = await built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-flag-off:prefer" },
- text: '!run printf \'%s|%s\' "${AWS_ACCESS_KEY_ID-unset}" "$(cat ~/.acmecli/session.json)"',
- });
- assert.equal(prefer.reply, "unset|legacy_ok");
-
- await built.deviceFlowCutover.set(room, "acmecli", "ephemeral_only", "[email protected]");
- const only = await built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "ch:C-acmecli-flag-off:only" },
- text: '!run printf \'%s|%s\' "${AWS_ACCESS_KEY_ID-unset}" "$(test -e ~/.acmecli && echo found || echo absent)"',
- });
- assert.equal(only.reply, "unset|absent");
-});
-
-test("selected broker execute honors deployment approval rules before vending credentials", async () => {
- let assumes = 0;
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-credexec-approval-")),
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliBrokeredLayer("env", [
- { pattern: "\\benv\\b\\s+tool\\b", reason: "mutating subcommand" },
- ]),
- }),
- {
- credentialBrokers: {
- acmecli: createAwsRoleBroker({
- roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- assumeRole: async () => {
- assumes++;
- return {
- Credentials: {
- AccessKeyId: "AKIA_APPROVAL_GATE",
- SecretAccessKey: "approval_gate_secret_value",
- SessionToken: "approval_gate_session_token",
- Expiration: new Date(Date.now() + 3_600_000),
- },
- };
- },
- }),
- },
- },
- );
- const personal = scopeId("personal", actor.externalId);
- const conversation = { kind: "dm" as const, threadRef: "dm:credexec-approval", audience: [actor] };
- await built.deviceFlowCutover.set(personal, "acmecli", "ephemeral_only", "[email protected]");
-
- const gated = await built.app.turn({
- surface: "slack",
- actor,
- conversation,
- text: `!execute ${JSON.stringify({ command: "env tool delete", credentials: ["broker_acmecli"], ownerAuth: true })}`,
- });
- assert.equal(gated.status, "pending_approval");
- assert.equal(assumes, 0, "no AssumeRole call happens for a blocked command");
- const pending = gated.pendingApprovals![0]!;
- assert.match(pending.reason, /mutating subcommand/);
-
- const approved = await built.app.turn({
- surface: "slack",
- actor,
- conversation,
- text: `!execute ${JSON.stringify({ command: "env tool delete", credentials: ["broker_acmecli"], ownerAuth: true })}`,
- approval: { requestId: pending.requestId, approved: true },
- });
- assert.equal(approved.status, "ok", approved.reason);
- assert.equal(assumes, 1, "approval unblocks exactly one vended invocation");
-
- const unrelated = await built.app.turn({
- surface: "slack",
- actor,
- conversation: { ...conversation, threadRef: "dm:credexec-approval-3" },
- text: `!execute ${JSON.stringify({ command: "env", credentials: ["broker_acmecli"], ownerAuth: true })}`,
- });
- assert.equal(unrelated.status, "ok", "subcommands without approval rules run without a grant");
- assert.equal(assumes, 1, "the broker's per-actor credential cache is reused within its TTL");
- assert.match(unrelated.reply ?? "", /<redacted:credential>/);
- const durable = JSON.stringify(await built.sessions.getEntries(unrelated.sessionId!));
- for (const value of ["AKIA_APPROVAL_GATE", "approval_gate_secret_value", "approval_gate_session_token"]) {
- assert.ok(!(unrelated.reply ?? "").includes(value));
- assert.ok(!durable.includes(value));
- }
- assert.deepEqual(await built.keychain!.grantsForScope(personal), []);
-});
diff --git a/test/background-tool-context.test.ts b/test/background-tool-context.test.ts
index fcaa580c6..17505e242 100644
--- a/test/background-tool-context.test.ts
+++ b/test/background-tool-context.test.ts
@@ -213,17 +213,3 @@ test("off-Fly degradation: with NO broker the four mutating methods throw the 'u
const list = await ctx.backgroundList();
assert.deepEqual(list, []);
});
-
-test("registerLogin passes through createToolContext to the tool layer", async () => {
- const calls: string[] = [];
- const ctx = ctxFor({
- registerLogin: async (service, paths) => {
- calls.push(`${service}:${paths.map((p) => p.path).join(",")}`);
- return { service, captured: true };
- },
- });
- assert.ok(ctx.registerLogin, "the callback survives context construction");
- const result = await ctx.registerLogin!("demotool", [{ path: ".demotool/credentials", kind: "file" }]);
- assert.deepEqual(result, { service: "demotool", captured: true });
- assert.deepEqual(calls, ["demotool:.demotool/credentials"]);
-});
diff --git a/test/broker-layer-compatibility.test.ts b/test/broker-layer-compatibility.test.ts
index cc91de170..9d742573b 100644
--- a/test/broker-layer-compatibility.test.ts
+++ b/test/broker-layer-compatibility.test.ts
@@ -43,9 +43,6 @@ test("loadDeploymentLayer derives the runtime shapes from tool descriptors", ()
layer.tools.map((t) => t.id),
["acmecli", "jq"],
);
- assert.deepEqual(layer.connectors, [
- { id: "acmecli", label: "Acme CLI", check: "acmecli me", reauth: "acmecli login --use-device-code" },
- ]);
assert.deepEqual(
layer.advertisedTools,
["acmecli (organization CLI)"],
@@ -114,9 +111,8 @@ test("a layer declaring brokers on two tools is rejected at load", () => {
);
});
-test("loadDeploymentLayer: an authless tool contributes no connector or paths", () => {
+test("loadDeploymentLayer: an authless tool contributes no paths", () => {
const layer = loadDeploymentLayer(layerDir({ helper: { id: "helper", advertise: "helper tool" } }));
- assert.deepEqual(layer.connectors, []);
assert.deepEqual(layer.credentialPaths, []);
assert.deepEqual(layer.splitEnvTemplates, []);
assert.deepEqual(layer.advertisedTools, ["helper tool"]);
diff --git a/test/deployment-layer-load.test.ts b/test/deployment-layer-load.test.ts
index a933d9952..484669eac 100644
--- a/test/deployment-layer-load.test.ts
+++ b/test/deployment-layer-load.test.ts
@@ -4,8 +4,6 @@ import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { emptyDeploymentLayer, loadDeploymentLayer, replaceDeploymentLayer } from "../src/deployment/load-layer.ts";
-import { buildApp } from "../src/wiring.ts";
-import { testConfig } from "./support/test-config.ts";
import { parseToolDescriptor } from "../src/deployment/deployment-layer.ts";
import { BASE_EPHEMERAL_CRED_LINKS, BASE_RESIDENT_AUTH_PATHS } from "../src/credentials/resident-paths.ts";
import { evaluateCommandWithLayer } from "../src/policy/command-policy.ts";
@@ -42,9 +40,6 @@ test("loadDeploymentLayer derives the runtime shapes from tool descriptors", ()
layer.tools.map((t) => t.id),
["acmecli", "jq"],
);
- assert.deepEqual(layer.connectors, [
- { id: "acmecli", label: "Acme CLI", check: "acmecli me", reauth: "acmecli login --use-device-code" },
- ]);
assert.deepEqual(
layer.advertisedTools,
["acmecli (organization CLI)"],
@@ -57,33 +52,8 @@ test("loadDeploymentLayer derives the runtime shapes from tool descriptors", ()
assert.deepEqual(layer.credentialPaths, [credentialDirectory(".acmecli"), credentialDirectory(".aws")]);
});
-test("credential tools resolve to service and quarantine roots", () => {
- const layer = loadDeploymentLayer(
- layerDir({
- acmecli: {
- ...ACMECLI_SHAPED,
- auth: {
- ...ACMECLI_SHAPED.auth,
- },
- },
- jq: { id: "jq" },
- }),
- );
- assert.deepEqual(
- layer.credentialTools,
- [
- {
- service: "acmecli",
- roots: [".acmecli"],
- },
- ],
- "the quarantine roots are only the paths mapping to the tool's own service — .aws stays",
- );
-});
-
-test("loadDeploymentLayer: an authless tool contributes no connector or paths", () => {
+test("loadDeploymentLayer: an authless tool contributes no paths", () => {
const layer = loadDeploymentLayer(layerDir({ helper: { id: "helper", advertise: "helper tool" } }));
- assert.deepEqual(layer.connectors, []);
assert.deepEqual(layer.credentialPaths, []);
assert.deepEqual(layer.advertisedTools, ["helper tool"]);
});
@@ -230,25 +200,8 @@ const BROKERED_ACME = {
},
};
-test("a layer installed after boot reaches the app that booted without one", () => {
- const built = buildApp(testConfig({ orgId: "acme" }));
- assert.equal(
- built.credentialTools.length,
- 0,
- "a deployment with no DEPLOYMENT_LAYER boots empty; the layer arrives over the API afterwards",
- );
- replaceDeploymentLayer(built.deploymentLayer, loadDeploymentLayer(layerDir({ acme: BROKERED_ACME })));
- assert.equal(
- built.credentialTools.length,
- 1,
- "credential vending reads this array; a copy taken at boot would stay empty forever",
- );
- assert.equal(built.credentialTools[0]?.service, "acme");
-});
-
test("replaceDeploymentLayer reaches every holder of the runtime arrays", () => {
const runtime = emptyDeploymentLayer();
- const credentialTools = runtime.credentialTools;
const commandRules = runtime.commandRules;
replaceDeploymentLayer(
runtime,
@@ -258,8 +211,6 @@ test("replaceDeploymentLayer reaches every holder of the runtime arrays", () =>
}),
),
);
- assert.equal(credentialTools.length, 1, "credential vending reads this array");
- assert.equal(credentialTools[0]?.service, "acme");
assert.equal(commandRules.length, 1, "the command policy reads this array, and already sees post-boot layers");
});
diff --git a/test/device-flow-cutover.test.ts b/test/device-flow-cutover.test.ts
deleted file mode 100644
index 83691df46..000000000
--- a/test/device-flow-cutover.test.ts
+++ /dev/null
@@ -1,118 +0,0 @@
-import assert from "node:assert/strict";
-import test from "node:test";
-import { createDeviceFlowCutoverStore } from "../src/credentials/device-flow-cutover.ts";
-import { createMemoryMap } from "../src/persistence/durable-map.ts";
-import { scopeId } from "../src/types.ts";
-
-test("device-flow cutover defaults every service and scope to legacy", async () => {
- const store = createDeviceFlowCutoverStore(createMemoryMap());
-
- assert.equal(await store.resolve(scopeId("channel", "C1"), "aws"), "legacy");
- assert.equal(await store.resolve(scopeId("personal", "U1"), "github"), "legacy");
-});
-
-test("exact-scope policy overrides the org service policy", async () => {
- const store = createDeviceFlowCutoverStore(createMemoryMap());
- const org = scopeId("org", "default-org");
- const channel = scopeId("channel", "C1");
- const other = scopeId("channel", "C2");
-
- await store.set(org, "AWS", "prefer_ephemeral", "[email protected]");
- await store.set(channel, "aws", "ephemeral_only", "[email protected]");
-
- assert.equal(await store.resolve(channel, "aws"), "ephemeral_only");
- assert.equal(await store.resolve(other, "aws"), "prefer_ephemeral");
- assert.equal(await store.resolve(channel, "github"), "legacy");
-});
-
-test("policies can be updated and cleared for immediate rollback", async () => {
- let now = 10;
- let reset = 0;
- const store = createDeviceFlowCutoverStore(createMemoryMap(), { now: () => now, resetId: () => `reset-${++reset}` });
- const org = scopeId("org", "default-org");
- const channel = scopeId("channel", "C1");
-
- await store.set(org, "aws", "prefer_ephemeral", "[email protected]");
- await store.set(channel, "aws", "ephemeral_only", "[email protected]");
- now = 20;
- await store.set(channel, "aws", "legacy", "[email protected]");
-
- assert.deepEqual(await store.get(channel, "aws"), {
- scopeId: channel,
- service: "aws",
- mode: "legacy",
- updatedAt: 20,
- updatedBy: "[email protected]",
- resetResident: true,
- resetGeneration: "reset-1",
- });
- assert.equal(await store.resolve(channel, "aws"), "legacy");
- const firstReset = await store.residentResetGeneration(channel, "aws");
- assert.ok(firstReset);
- await store.set(channel, "aws", "prefer_ephemeral", "[email protected]");
- await store.set(channel, "aws", "legacy", "[email protected]");
- await store.markResidentReset(channel, "aws", firstReset);
- const newerReset = await store.residentResetGeneration(channel, "aws");
- assert.ok(newerReset, "acknowledging an older reset generation cannot consume a concurrent newer rollback");
- await store.markResidentReset(channel, "aws", newerReset);
- assert.equal(await store.residentResetGeneration(channel, "aws"), null);
-
- await store.clear(channel, "aws");
- assert.equal(await store.resolve(channel, "aws"), "prefer_ephemeral");
-
- await store.set(org, "aws", "legacy", "[email protected]");
- assert.equal(await store.resolve(channel, "aws"), "legacy");
- assert.ok(
- await store.residentResetGeneration(channel, "aws"),
- "an inherited org rollback has a per-scope one-shot reset",
- );
-
- await store.set(channel, "aws", "ephemeral_only", "[email protected]");
- await store.clear(channel, "aws");
- assert.ok(
- await store.residentResetGeneration(channel, "aws"),
- "clearing a nonlegacy override to inherited legacy also requests reset",
- );
-
- await store.set(org, "aws", "prefer_ephemeral", "[email protected]");
- await store.clear(org, "aws");
- assert.ok(
- await store.residentResetGeneration(channel, "aws"),
- "clearing an org cutover to default legacy requests a reset for every child computer",
- );
-});
-
-test("stored policies and reset requests discover removed services across restart", async () => {
- const backing = createMemoryMap<import("../src/credentials/device-flow-cutover.ts").DeviceFlowCutoverPolicy>();
- const resets = createMemoryMap<import("../src/credentials/device-flow-cutover.ts").DeviceFlowCutoverReset>();
- const org = scopeId("org", "default-org");
- const scope = scopeId("personal", "U1");
- const first = createDeviceFlowCutoverStore(backing, { resets });
- await first.set(org, "shared-tool", "prefer_ephemeral", "admin");
- await first.set(scope, "retired-tool", "ephemeral_only", "admin");
- await first.set(scopeId("personal", "U2"), "unrelated", "ephemeral_only", "admin");
- const restarted = createDeviceFlowCutoverStore(backing, { resets });
- assert.deepEqual(await restarted.listServices(scope), ["retired-tool", "shared-tool"]);
- await restarted.clear(scope, "retired-tool");
- const afterClear = createDeviceFlowCutoverStore(backing, { resets });
- assert.deepEqual(await afterClear.listServices(scope), ["retired-tool", "shared-tool"]);
- assert.equal(await afterClear.resolve(scope, "retired-tool"), "legacy");
- assert.ok(await afterClear.residentResetGeneration(scope, "retired-tool"));
-});
-
-test("resident reset completion is durable per computer without changing scope policy", async () => {
- const backing = createMemoryMap<import("../src/credentials/device-flow-cutover.ts").DeviceFlowCutoverPolicy>();
- const resets = createMemoryMap<import("../src/credentials/device-flow-cutover.ts").DeviceFlowCutoverReset>();
- const scope = scopeId("channel", "team");
- const store = createDeviceFlowCutoverStore(backing, { resets });
- await store.set(scope, "aws", "ephemeral_only", "admin");
- await store.set(scope, "aws", "legacy", "admin");
- const generation = await store.residentResetGeneration(scope, "aws", "computer-a");
- assert.ok(generation);
- await store.markResidentReset(scope, "aws", generation, "computer-a");
- const restarted = createDeviceFlowCutoverStore(backing, { resets });
- assert.equal(await restarted.residentResetGeneration(scope, "aws", "computer-a"), null);
- assert.equal(await restarted.residentResetGeneration(scope, "aws", "computer-b"), generation);
- await restarted.markResidentReset(scope, "aws", generation, "computer-b");
- assert.equal(await restarted.residentResetGeneration(scope, "aws", "computer-b"), null);
-});
diff --git a/test/device-flow-persist.test.ts b/test/device-flow-persist.test.ts
deleted file mode 100644
index 0095bc082..000000000
--- a/test/device-flow-persist.test.ts
+++ /dev/null
@@ -1,1135 +0,0 @@
-import { test, before, after, beforeEach } from "node:test";
-import assert from "node:assert/strict";
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
-import { tmpdir } from "node:os";
-import { join } from "node:path";
-import { buildApp } from "../src/wiring.ts";
-import { createSpritesSandbox } from "../src/sandbox/sprites-sandbox.ts";
-import { createLocalWorkspaceStore } from "../src/workspace/workspace-store.ts";
-import { createKeychain, KeychainError, type Keychain } from "../src/credentials/keychain.ts";
-import { createMemoryMap } from "../src/persistence/durable-map.ts";
-import { deriveConnectorKey } from "../src/connectors/connector-client-store.ts";
-import {
- captureDeviceFlowLogins,
- deviceFlowCredOwner,
- materializeDeviceFlowLogins,
- registerLoginPaths,
- removeDeviceFlowLogins,
- DEVICE_FLOW_ORIGIN,
-} from "../src/credentials/device-flow-persist.ts";
-import { scopeId, type TurnRequest } from "../src/types.ts";
-import { installGlobalFakeSprites, type FakeSprites } from "./support/fake-sprites.ts";
-import { testConfig } from "./support/test-config.ts";
-import { selectDefaultSandbox } from "./support/default-sandbox.ts";
-
-let ff: FakeSprites;
-before(() => {
- ff = installGlobalFakeSprites();
-});
-beforeEach(() => ff.reset());
-after(() => ff.cleanup());
-
-const KEY = deriveConnectorKey("device-flow-test-key");
-
-function kc(): Keychain {
- return createKeychain({ creds: createMemoryMap(), grants: createMemoryMap(), asks: createMemoryMap(), key: KEY });
-}
-
-function sprites() {
- const dir = mkdtempSync(join(tmpdir(), "dfp-ws-"));
- return createSpritesSandbox(createLocalWorkspaceStore(dir), {
- token: "test-token",
- baseUrl: ff.baseUrl,
- });
-}
-const rw = (scope: string) => [{ scopeId: scope, mountPath: "", mode: "rw" as const }];
-
-function acmecliCredentialLayer(binary?: string, approvals?: Array<{ pattern: string; reason?: string }>): string {
- const dir = mkdtempSync(join(tmpdir(), "dfp-layer-"));
- mkdirSync(join(dir, "tools/acmecli"), { recursive: true });
- writeFileSync(
- join(dir, "tools/acmecli/tool.json"),
- JSON.stringify({
- id: "acmecli",
- ...(binary ? { install: { binary } } : {}),
- ...(approvals ? { approvals } : {}),
- auth: {
- check: "acmecli me",
- reauth: "acmecli login --use-device-code",
- credentialPaths: [{ path: ".acmecli", kind: "directory" }],
- },
- }),
- );
- return dir;
-}
-
-test("deviceFlowCredOwner: the person on their own personal box, the scope on a shared box", () => {
- assert.equal(deviceFlowCredOwner(scopeId("personal", "U1"), "U1"), "U1");
- assert.equal(deviceFlowCredOwner(scopeId("channel", "C1"), "U1"), scopeId("channel", "C1"));
- assert.equal(deviceFlowCredOwner(scopeId("personal", "U2"), "U1"), scopeId("personal", "U2"));
-});
-
-test("capture saves changed login bundles per service and fingerprint-skips unchanged ones", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- const login = await sb.run(
- h,
- "mkdir -p ~/.config/gh && printf 'oauth_token: gho_SECRET' > ~/.config/gh/hosts.yml && " +
- "printf 'machine x login y password z' > ~/.netrc",
- );
- assert.equal(login.code, 0, login.stderr);
-
- const input = { sandbox: sb, handle: h, keychain: k, ownerId: "U1" };
- assert.deepEqual((await captureDeviceFlowLogins(input)).sort(), ["gh", "netrc"]);
-
- const records = await k.listByOwner("U1");
- const gh = records.find((c) => c.service === "gh");
- assert.equal(gh?.kind, "file");
- assert.deepEqual(gh?.targets, [".config/gh/hosts.yml"]);
- assert.ok(!JSON.stringify(records).includes("gho_SECRET"), "listing carries metadata only");
-
- assert.deepEqual(await captureDeviceFlowLogins(input), []);
-
- await sb.run(h, "printf 'oauth_token: gho_ROTATED' > ~/.config/gh/hosts.yml");
- assert.deepEqual(await captureDeviceFlowLogins(input), ["gh"]);
-});
-
-test("capture grabs the AWS SSO token under .aws/sso/cache; gcloud's cache/logs bulk dirs are pruned", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- const setup = await sb.run(
- h,
- "mkdir -p ~/.aws/sso/cache ~/.aws/cli/cache ~/.config/gcloud/logs ~/.config/gcloud/cache && " +
- "printf '[profile acme]' > ~/.aws/config && " +
- 'printf \'{"accessToken":"sso_SECRET"}\' > ~/.aws/sso/cache/token.json && ' +
- 'printf \'{"Credentials":"role_SECRET"}\' > ~/.aws/cli/cache/role.json && ' +
- "printf 'gcloud_creds' > ~/.config/gcloud/credentials.db && " +
- "printf 'noise' > ~/.config/gcloud/logs/run.log && printf 'noise' > ~/.config/gcloud/cache/x.json",
- );
- assert.equal(setup.code, 0, setup.stderr);
-
- const input = { sandbox: sb, handle: h, keychain: k, ownerId: "U1" };
- assert.deepEqual((await captureDeviceFlowLogins(input)).sort(), ["aws", "gcloud"]);
-
- const records = await k.listByOwner("U1");
- const aws = records.find((c) => c.service === "aws");
- assert.deepEqual(
- aws?.targets?.slice().sort(),
- [".aws/cli/cache/role.json", ".aws/config", ".aws/sso/cache/token.json"],
- "the SSO token and assumed-role cache rode along, not just .aws/config",
- );
- const gcloud = records.find((c) => c.service === "gcloud");
- assert.deepEqual(
- gcloud?.targets,
- [".config/gcloud/credentials.db"],
- "the real gcloud cred is captured; its cache (discovery docs) and logs bulk dirs are pruned",
- );
-
- rmSync(ff.homeDir(h.id), { recursive: true, force: true });
- const h2 = await sb.provision(rw(scopeId("personal", "U1")));
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const back = await sb.run(h2, "cat ~/.aws/sso/cache/token.json");
- assert.equal(back.code, 0, back.stderr);
- assert.match(back.stdout, /sso_SECRET/, "auth survived machine replacement");
-});
-
-test("an unregistered ~/.config tool is neither swept nor stored (known services still are)", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(
- h,
- "mkdir -p ~/.config/gh ~/.config/acmecorp && printf 'oauth_token: gho_OK' > ~/.config/gh/hosts.yml && " +
- "printf 'tok_NEWTOOL' > ~/.config/acmecorp/auth.json",
- );
- const skipped: string[] = [];
- const saved = await captureDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- onAnomaly: (s) => skipped.push(s),
- });
- assert.deepEqual(saved, ["gh"], "the known service is captured; the unregistered one is not");
- assert.deepEqual(skipped, [], "an unregistered dir is not swept, so it never trips the caps warning");
- assert.ok(!(await k.listByOwner("U1")).some((c) => c.service === "acmecorp"), "acmecorp was never stored");
-});
-
-test("lossless migration only follows device-flow records, not operator-saved credentials", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/opsaved && printf 'operator_secret' > ~/.config/opsaved/auth.json");
- await k.save({
- ownerId: "U1",
- service: "opsaved",
- files: [{ path: ".config/opsaved/auth.json", contentBase64: Buffer.from("op_v0").toString("base64") }],
- origin: "agent-session:personal:U1",
- });
-
- assert.deepEqual(
- await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }),
- [],
- "an operator/API-saved credential is not adopted into the device-flow sweep and not overwritten",
- );
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "opsaved");
- assert.equal(rec?.origin, "agent-session:personal:U1", "its origin is untouched");
-});
-
-test("lossless migration: a service captured before keeps being swept via its record targets", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(h1, "mkdir -p ~/.config/acmecorp && printf 'tok_v1' > ~/.config/acmecorp/auth.json");
- await k.save({
- ownerId: "U1",
- service: "acmecorp",
- files: [{ path: ".config/acmecorp/auth.json", contentBase64: Buffer.from("tok_v0").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
-
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" }), [
- "acmecorp",
- ]);
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const back = await sb.run(h2, "cat ~/.config/acmecorp/auth.json");
- assert.match(back.stdout, /tok_v1/, "the previously-captured tool's rotated login still rides along");
-});
-
-test("pre-XDG holdout: a tool that keeps its login in ~/.<tool> (fly) is captured + restored", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(h1, "mkdir -p ~/.fly && printf 'access_token: fo1_SECRET' > ~/.fly/config.yml");
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" }), ["fly"]);
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const back = await sb.run(h2, "cat ~/.fly/config.yml");
- assert.match(back.stdout, /fo1_SECRET/, "a non-XDG holdout's login survived machine replacement");
-});
-
-test("large multi-file bundle round-trips intact (past the exec ~16KiB request + ~4MB response caps)", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- const setup =
- "mkdir -p ~/.config/bigtool && " +
- "for i in $(seq 0 29); do head -c 102400 /dev/zero | tr '\\0' 'X' > ~/.config/bigtool/part$i.dat; done && " +
- "printf 'TOKEN_SENTINEL_END' >> ~/.config/bigtool/part29.dat";
- assert.equal((await sb.run(h1, setup)).code, 0);
-
- const bigInput = {
- sandbox: sb,
- handle: h1,
- keychain: k,
- ownerId: "U1",
- credentialPaths: [{ path: ".config/bigtool", kind: "directory" as const }],
- };
- assert.deepEqual(await captureDeviceFlowLogins(bigInput), ["bigtool"]);
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "bigtool");
- assert.equal(rec?.targets?.length, 30, "every file in the large bundle was captured, none dropped to truncation");
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const check = await sb.run(
- h2,
- 'echo "size=$(wc -c < ~/.config/bigtool/part0.dat)"; echo "sentinel=$(tail -c 18 ~/.config/bigtool/part29.dat)"; echo "count=$(ls ~/.config/bigtool | wc -l)"',
- );
- assert.equal(check.code, 0, check.stderr);
- const fields = Object.fromEntries(
- check.stdout
- .trim()
- .split("\n")
- .map((l) => l.split("=").map((s) => s.trim())),
- );
- assert.equal(
- Number(fields.size),
- 100 * 1024,
- "a large file restored at full size, not truncated under the write cap",
- );
- assert.equal(fields.sentinel, "TOKEN_SENTINEL_END", "the last file's trailing bytes survived (no silent corruption)");
- assert.equal(Number(fields.count), 30, "every file came back");
-});
-
-test("shape backstop: a registered path that balloons past the caps is skipped loudly, not stored", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(
- h,
- "mkdir -p ~/.config/gh ~/.config/bloat && printf 'oauth_token: gho_OK' > ~/.config/gh/hosts.yml && " +
- "for i in $(seq 1 600); do printf x > ~/.config/bloat/f$i; done",
- );
- const skipped: string[] = [];
- const saved = await captureDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- credentialPaths: [{ path: ".config/bloat", kind: "directory" }],
- onAnomaly: (service) => skipped.push(service),
- });
- assert.deepEqual(saved, ["gh"], "the real login still got captured");
- assert.deepEqual(skipped, ["bloat"], "the registered but oversized path was skipped by the caps backstop");
- assert.ok(!(await k.listByOwner("U1")).some((c) => c.service === "bloat"), "and never stored");
-});
-
-test("a browser profile under ~/.config no longer trips the capture — neither swept nor warned", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- const setup = await sb.run(
- h,
- "mkdir -p ~/.config/gh ~/.config/chromium-headless/Default && " +
- "printf 'oauth_token: gho_OK' > ~/.config/gh/hosts.yml && " +
- "touch ~/.config/chromium-headless/'Local State' ~/.config/chromium-headless/SingletonLock && " +
- "for i in $(seq 1 700); do printf xxxx > ~/.config/chromium-headless/Default/blob$i; done",
- );
- assert.equal(setup.code, 0, setup.stderr);
- const files = await sb.run(h, "find ~/.config/chromium-headless -type f | wc -l");
- assert.ok(Number(files.stdout.trim()) >= 700, `the browser profile really exists (${files.stdout.trim()} files)`);
-
- const skipped: string[] = [];
- const saved = await captureDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- onAnomaly: (s) => skipped.push(s),
- });
- assert.deepEqual(saved, ["gh"], "only the real login is captured");
- assert.deepEqual(skipped, [], "no capture-skipped warning — the browser profile was never swept");
- assert.ok(!(await k.listByOwner("U1")).some((c) => c.service === "chromium-headless"), "and never stored");
-});
-
-test("materialize round-trip: login → machine replaced → files restored 0600 behind the symlinks", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(
- h1,
- "mkdir -p ~/.config/glab && printf 'token: glpat_SECRET' > ~/.config/glab/config.yml && chmod 600 ~/.config/glab/config.yml",
- );
- await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" });
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
-
- const restored = await sb.run(
- h2,
- "cat ~/.config/glab/config.yml && stat -c %a ~/.config/glab/config.yml 2>/dev/null || stat -f %Lp ~/.config/glab/config.yml",
- );
- assert.equal(restored.code, 0, restored.stderr);
- assert.match(restored.stdout, /glpat_SECRET/);
- assert.match(restored.stdout, /600/);
- const link = await sb.run(h2, "readlink ~/.config/glab >/dev/null && echo islink");
- assert.match(link.stdout, /islink/);
-});
-
-test("materialize never overwrites a file already on disk — the live machine's login wins", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/gh && printf 'oauth_token: gho_OLD' > ~/.config/gh/hosts.yml");
- await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" });
-
- await sb.run(h, "printf 'oauth_token: gho_NEWER' > ~/.config/gh/hosts.yml");
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" });
- const read = await sb.run(h, "cat ~/.config/gh/hosts.yml");
- assert.equal(read.stdout, "oauth_token: gho_NEWER");
-});
-
-test("a legacy bundle stamped with a past expiresAt (by the deleted refresher) still restores", async () => {
- const sb = sprites();
- const k = kc();
- await k.save({
- ownerId: "U1",
- service: "aws",
- files: [
- { path: ".aws/config", contentBase64: Buffer.from("[default]\nregion=us-west-2", "utf8").toString("base64") },
- ],
- origin: DEVICE_FLOW_ORIGIN,
- expiresAt: Date.now() - 3_600_000,
- });
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" });
- const read = await sb.run(h, "cat ~/.aws/config");
- assert.match(read.stdout, /us-west-2/, "file bundles are durability-only: a stale expiry stamp never blocks restore");
-});
-
-test("ACMECLI quarantine removes the canonical root even with no record or a stale partial record", async () => {
- const dir = mkdtempSync(join(tmpdir(), "dfp-ws-"));
- const sb = createSpritesSandbox(createLocalWorkspaceStore(dir), {
- token: "test-token",
- baseUrl: ff.baseUrl,
- credentialPaths: [{ path: ".acmecli", kind: "directory" }],
- });
- const k = kc();
- const h = await sb.provision(rw(scopeId("channel", "C1")));
- await sb.run(h, "mkdir -p ~/.acmecli && printf live > ~/.acmecli/unrecorded.json");
- await removeDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: scopeId("channel", "C1"),
- services: ["acmecli"],
- canonicalRoots: [".acmecli"],
- });
- assert.equal(
- (await sb.run(h, "test ! -e ~/.acmecli && test ! -e /tmp/agent-creds/.acmecli")).code,
- 0,
- "an uncaptured warm-machine login and its resolved ephemeral target are removed",
- );
-
- await k.save({
- ownerId: scopeId("channel", "C1"),
- service: "acmecli",
- files: [{ path: ".acmecli/known.json", contentBase64: Buffer.from("old").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- await sb.run(h, "mkdir -p ~/.acmecli && printf known > ~/.acmecli/known.json && printf newer > ~/.acmecli/new.json");
- await removeDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: scopeId("channel", "C1"),
- services: ["acmecli"],
- canonicalRoots: [".acmecli"],
- });
- assert.equal(
- (await sb.run(h, "test ! -e ~/.acmecli && test ! -e /tmp/agent-creds/.acmecli")).code,
- 0,
- "new files absent from the encrypted record are removed from the ephemeral target too",
- );
-});
-
-async function freshApp() {
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-app-")),
- signingSecret: "device-flow-test-secret",
- }),
- );
- await selectDefaultSandbox(built, "U1", scopeId("personal", "U1"), scopeId("channel", "C1"));
- return built;
-}
-
-const actor = { externalId: "U1" };
-
-function dm(text: string): TurnRequest {
- return { surface: "test", actor, conversation: { kind: "dm", threadRef: "dm:U1:t1" }, text };
-}
-
-function channel(text: string): TurnRequest {
- return {
- surface: "slack",
- actor,
- conversation: { kind: "channel", threadRef: "ch:C1:t1", channelRef: "C1", audience: [actor] },
- text,
- };
-}
-
-test("a DM turn auto-captures a device-flow login under the PERSON, and a fresh machine gets it back", async () => {
- const { app, keychain } = await freshApp();
- const res = await app.turn(
- dm("!run mkdir -p ~/.config/gh && printf 'oauth_token: gho_E2E' > ~/.config/gh/hosts.yml && echo done"),
- );
- assert.equal(res.status, "ok");
-
- const records = await keychain!.listByOwner("U1");
- const gh = records.find((c) => c.service === "gh");
- assert.ok(gh, "post-turn capture persisted the login under the actor");
- assert.equal(gh!.kind, "file");
-
- for (const name of ff.names()) rmSync(ff.homeDir(name), { recursive: true, force: true });
- const back = await app.turn(dm("!run cat ~/.config/gh/hosts.yml"));
- assert.equal(back.status, "ok");
- assert.match(back.reply ?? "", /gho_E2E/, "auth survived machine replacement");
-});
-
-test("a login performed on a shared channel box is keyed to the SCOPE, like its workspace", async () => {
- const { app, keychain } = await freshApp();
- const res = await app.turn(
- channel("!run mkdir -p ~/.config/glab && printf 'token: glpat_CH' > ~/.config/glab/config.yml && echo done"),
- );
- assert.equal(res.status, "ok", res.reason);
-
- assert.equal((await keychain!.listByOwner("U1")).length, 0, "no personal record from a channel turn");
- const scoped = await keychain!.listByOwner(scopeId("channel", "C1"));
- assert.equal(scoped.find((c) => c.service === "glab")?.kind, "file");
-});
-
-test("a capture failure is logged as an error event and does NOT fail the turn", async () => {
- const { app, keychain, errors } = await freshApp();
- const realSave = keychain!.save.bind(keychain!);
- keychain!.save = async () => {
- throw new Error("injected keychain outage");
- };
- const res = await app.turn(
- dm("!run mkdir -p ~/.config/gh && printf 'oauth_token: gho_X' > ~/.config/gh/hosts.yml && echo done"),
- );
- assert.equal(res.status, "ok", "capture is best-effort — the turn still succeeds");
- const logged = (await errors.list()).find((e) => e.code === "device_flow_capture_failed");
- assert.ok(logged, "the failure is durably visible to operators");
-
- keychain!.save = realSave;
- const retry = await app.turn(dm("!run echo retry"));
- assert.equal(retry.status, "ok");
- assert.ok((await keychain!.listByOwner("U1")).some((c) => c.service === "gh"));
-});
-
-test("removing platform credential vending preserves stored quarantine on personal and shared sandboxes", async () => {
- for (const shared of [false, true]) {
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-quarantine-")),
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliCredentialLayer(),
- }),
- );
- const request = shared ? channel("!run echo ready") : dm("!run echo ready");
- const ownerId = shared ? scopeId("channel", "C1") : "U1";
- const targetScope = shared ? scopeId("channel", "C1") : scopeId("personal", "U1");
- await selectDefaultSandbox(built, "U1", targetScope);
- await built.keychain!.save({
- ownerId,
- service: "acmecli",
- files: [{ path: ".acmecli/session.json", contentBase64: Buffer.from("stored-login").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- const read = "!run cat ~/.acmecli/session.json";
- assert.equal((await built.app.turn({ ...request, text: read })).reply, "stored-login");
- await built.deviceFlowCutover.set(targetScope, "acmecli", "ephemeral_only", "[email protected]");
- const hidden = await built.app.turn({
- ...request,
- text: "!run test -e ~/.acmecli/session.json && echo found || echo absent",
- });
- assert.equal(hidden.reply, "absent");
- assert.ok((await built.keychain!.listByOwner(ownerId)).some((record) => record.service === "acmecli"));
- await built.deviceFlowCutover.set(targetScope, "acmecli", "legacy", "[email protected]");
- assert.equal((await built.app.turn({ ...request, text: read })).reply, "stored-login");
- }
-});
-
-test("capture never clobbers an operator-saved record for a swept service", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/gh && printf 'oauth_token: gho_BOX' > ~/.config/gh/hosts.yml");
- await k.save({
- ownerId: "U1",
- service: "gh",
- files: [
- { path: ".config/gh/hosts.yml", contentBase64: Buffer.from("oauth_token: gho_OPERATOR").toString("base64") },
- ],
- origin: "agent-session:personal:U1",
- });
-
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }), []);
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "gh");
- assert.equal(rec?.origin, "agent-session:personal:U1", "the operator's record and origin survive capture");
-});
-
-test("a shared-box capture sweeps only the scope owner's roots, never another person's registrations", async () => {
- const sb = sprites();
- const k = kc();
- const shared = scopeId("channel", "C1");
- const h = await sb.provision(rw(shared));
- await sb.run(h, "mkdir -p ~/.config/acmecorp && printf 'tok_ROTATED' > ~/.config/acmecorp/auth.json");
- await k.save({
- ownerId: "U1",
- service: "acmecorp",
- files: [{ path: ".config/acmecorp/auth.json", contentBase64: Buffer.from("tok_v0").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
-
- const saved = await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: shared });
- assert.deepEqual(saved, [], "a person's private registration does not widen what a shared box exfiltrates");
- assert.ok(!(await k.listByOwner(shared)).some((c) => c.service === "acmecorp"), "nothing saved under the scope");
-});
-
-test("a registered file under an already-swept dotdir is not tarred twice", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, 'mkdir -p ~/.aws/sso/cache && printf \'{"accessToken":"sso_X"}\' > ~/.aws/sso/cache/token.json');
- await k.save({
- ownerId: "U1",
- service: "aws",
- files: [{ path: ".aws/sso/cache/token.json", contentBase64: Buffer.from("old").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
-
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }), ["aws"]);
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "aws");
- assert.deepEqual(rec?.targets, [".aws/sso/cache/token.json"], "one entry, not a find+file-loop duplicate");
-
- assert.deepEqual(
- await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }),
- [],
- "an unchanged box fingerprints clean on the very next capture — no duplicate-entry churn",
- );
-});
-
-test("keychain.save with expectedOrigin atomically refuses to overwrite a foreign-origin record", async () => {
- const k = kc();
- await k.save({
- ownerId: "U1",
- service: "tool",
- files: [{ path: ".config/tool/auth.json", contentBase64: Buffer.from("operator").toString("base64") }],
- origin: "agent-session:personal:U1",
- });
- await assert.rejects(
- k.save({
- ownerId: "U1",
- service: "tool",
- files: [{ path: ".config/tool/auth.json", contentBase64: Buffer.from("box").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- expectedOrigin: DEVICE_FLOW_ORIGIN,
- }),
- /already exists/,
- );
- await k.save({
- ownerId: "U1",
- service: "fresh",
- files: [{ path: ".config/fresh/auth.json", contentBase64: Buffer.from("v1").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- expectedOrigin: DEVICE_FLOW_ORIGIN,
- });
- await k.save({
- ownerId: "U1",
- service: "fresh",
- files: [{ path: ".config/fresh/auth.json", contentBase64: Buffer.from("v2").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- expectedOrigin: DEVICE_FLOW_ORIGIN,
- });
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "fresh");
- assert.equal(rec?.origin, DEVICE_FLOW_ORIGIN, "a device-flow record updates freely under the precondition");
-});
-
-test("registered roots beyond the cap are dropped with one anomaly, after collapsing covered roots", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- for (let i = 0; i < 70; i++) {
- await k.save({
- ownerId: "U1",
- service: `svc${i}`,
- files: [{ path: `.config/svc${i}/a/auth.json`, contentBase64: Buffer.from(`t${i}`).toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- }
- for (let i = 0; i < 5; i++) {
- await k.save({
- ownerId: "U1",
- service: "aws",
- files: [{ path: ".aws/sso/cache/token.json", contentBase64: Buffer.from("x").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- }
- const anomalies: string[] = [];
- await captureDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- onAnomaly: (s, d) => anomalies.push(`${s}: ${d}`),
- });
- assert.equal(anomalies.length, 1, "exactly one truncation anomaly");
- assert.match(anomalies[0]!, /capture-sweep: 6 services past the 64-root sweep cap were not captured/);
-});
-
-test("the cap drops whole services, never a subset of one record's files", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- const paths = Array.from({ length: 70 }, (_, i) => `.tool/f${i}.json`);
- await sb.run(h, "mkdir -p ~/.tool && " + paths.map((p) => `printf x > ~/${p}`).join(" && "));
- await k.save({
- ownerId: "U1",
- service: "tool",
- files: paths.map((p) => ({ path: p, contentBase64: Buffer.from("v0").toString("base64") })),
- origin: DEVICE_FLOW_ORIGIN,
- });
-
- const anomalies: string[] = [];
- const saved = await captureDeviceFlowLogins({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- onAnomaly: (s, d) => anomalies.push(`${s}: ${d}`),
- });
- assert.deepEqual(saved, [], "a single record whose files exceed the cap is skipped whole, not saved shrunk");
- assert.equal((await k.listByOwner("U1")).find((c) => c.service === "tool")?.targets?.length, 70, "record intact");
- assert.match(anomalies.join("\n"), /services past the 64-root sweep cap were not captured: tool/);
-});
-
-test("register_login captures a real CLI login and it survives a machine rebuild", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(h1, 'mkdir -p ~/.kaggle && printf \'{"username":"u","key":"kag_SECRET"}\' > ~/.kaggle/kaggle.json');
-
- const result = await registerLoginPaths({
- sandbox: sb,
- handle: h1,
- keychain: k,
- ownerId: "U1",
- service: "kaggle",
- paths: [{ path: ".kaggle/kaggle.json", kind: "file" }],
- });
- assert.deepEqual(result, { service: "kaggle", captured: true });
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "kaggle");
- assert.deepEqual(rec?.capturePaths, [{ path: ".kaggle/kaggle.json", kind: "file" }]);
- assert.ok(!JSON.stringify(await k.listByOwner("U1")).includes("kag_SECRET"), "metadata only in the listing");
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const back = await sb.run(h2, "cat ~/.kaggle/kaggle.json");
- assert.match(back.stdout, /kag_SECRET/, "the registered login is restored on a fresh machine");
-});
-
-test("a registered service keeps being swept — a later rotation is captured without re-registering", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/rotato && printf 'refresh_v1' > ~/.config/rotato/creds");
- await registerLoginPaths({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- service: "rotato",
- paths: [{ path: ".config/rotato", kind: "directory" }],
- });
-
- await sb.run(h, "printf 'refresh_v2_ROTATED' > ~/.config/rotato/creds");
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }), ["rotato"]);
-});
-
-test("register_login rejects paths outside the service, built-in overlaps, and missing files", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- const base = { sandbox: sb, handle: h, keychain: k, ownerId: "U1" };
-
- await assert.rejects(
- registerLoginPaths({ ...base, service: "aws", paths: [{ path: ".aws/sso/cache", kind: "directory" }] }),
- /overlaps the built-in/,
- );
- await assert.rejects(
- registerLoginPaths({ ...base, service: "ghost", paths: [{ path: "../escape", kind: "file" }] }),
- /under \$HOME/,
- );
- await sb.run(h, "true");
- await assert.rejects(
- registerLoginPaths({ ...base, service: "absent", paths: [{ path: ".absent/creds", kind: "file" }] }),
- /nothing to capture|nothing was captured/,
- );
-});
-
-test("register_login refuses to adopt a service already owned by an operator/API save", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.opstool && printf 'x' > ~/.opstool/creds");
- await k.save({
- ownerId: "U1",
- service: "opstool",
- files: [{ path: ".opstool/creds", contentBase64: Buffer.from("op").toString("base64") }],
- origin: "agent-session:personal:U1",
- });
- await assert.rejects(
- registerLoginPaths({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- service: "opstool",
- paths: [{ path: ".opstool/creds", kind: "file" }],
- }),
- /not created by a login capture/,
- );
-});
-
-test("register_login handles a .cache/<tool> layout and re-captures rotations under the declared service", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.cache/huggingface && printf 'hf_v1' > ~/.cache/huggingface/token");
- const reg = await registerLoginPaths({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- service: "huggingface",
- paths: [{ path: ".cache/huggingface/token", kind: "file" }],
- });
- assert.deepEqual(reg, { service: "huggingface", captured: true });
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "huggingface");
- assert.deepEqual(rec?.targets, [".cache/huggingface/token"], "filed under the declared service, not '.cache'");
- assert.ok(!(await k.listByOwner("U1")).some((c) => c.service === "cache"), "no bogus 'cache' service record");
-
- await sb.run(h, "printf 'hf_v2_ROTATED' > ~/.cache/huggingface/token");
- assert.deepEqual(
- await captureDeviceFlowLogins({ sandbox: sb, handle: k && h, keychain: k, ownerId: "U1" }),
- ["huggingface"],
- "a plain re-capture groups the rotation under the registered service via its stored capturePaths",
- );
- assert.ok(!(await k.listByOwner("U1")).some((c) => c.service === "cache"), "still no 'cache' record after rotation");
-});
-
-test("capture-on-change: an unchanged box ships nothing — gated by the box-side hash, not just the fingerprint", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/gh && printf 'oauth_token: gho_V1' > ~/.config/gh/hosts.yml");
- const input = { sandbox: sb, handle: h, keychain: k, ownerId: "U1" };
- assert.deepEqual(await captureDeviceFlowLogins(input), ["gh"]);
- const state = await sb.run(h, "cat ~/.cred-state");
- assert.equal(state.code, 0, "the committed per-service hash state exists after a successful capture");
- assert.match(state.stdout, /^gh\t/m);
-
- const rec = (await k.listByOwner("U1")).find((c) => c.service === "gh")!;
- await k.remove("U1", rec.id);
- assert.deepEqual(
- await captureDeviceFlowLogins(input),
- [],
- "unchanged box ships nothing even with the record gone — proof the gate is the box-side hash",
- );
-
- await sb.run(h, "printf 'oauth_token: gho_V2' > ~/.config/gh/hosts.yml");
- assert.deepEqual(await captureDeviceFlowLogins(input), ["gh"], "a real change ships and re-creates the record");
-});
-
-test("capture-on-change: a rebuilt box (no state file) re-ships everything", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(h1, "mkdir -p ~/.fly && printf 'access_token: fo1_X' > ~/.fly/config.yml");
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" }), ["fly"]);
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- await sb.run(h2, "printf 'access_token: fo1_ROTATED' > ~/.fly/config.yml");
- assert.deepEqual(
- await captureDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" }),
- ["fly"],
- "with no state file the fresh box ships, and the rotation lands",
- );
-});
-
-test("restore preserves each file's captured mode, defaulting old records to 0600", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(
- h1,
- "mkdir -p ~/.modes && printf 'secret' > ~/.modes/tight && chmod 600 ~/.modes/tight && " +
- "printf 'shared-config' > ~/.modes/open && chmod 644 ~/.modes/open && " +
- "printf '#!/bin/sh' > ~/.modes/hook && chmod 700 ~/.modes/hook",
- );
- await registerLoginPaths({
- sandbox: sb,
- handle: h1,
- keychain: k,
- ownerId: "U1",
- service: "modes",
- paths: [{ path: ".modes", kind: "directory" }],
- });
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const modes = await sb.run(h2, "stat -c '%a %n' ~/.modes/* 2>/dev/null || stat -f '%Lp %N' ~/.modes/*");
- assert.match(modes.stdout, /600 .*tight/, "the 0600 credential stays 0600 (deployctl/kaggle tamper checks)");
- assert.match(modes.stdout, /600 .*open/, "group/other bits are stripped on restore — the mode floor policy");
- assert.match(modes.stdout, /700 .*hook/, "owner-exec is preserved through the floor");
-});
-
-test("a failed mid-bundle restore rolls back that bundle's files instead of leaving a partial set", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(
- h1,
- "mkdir -p ~/.aws/sso/cache ~/.aws/cli/cache && printf 'a' > ~/.aws/sso/cache/a.json && " +
- "printf 'b' > ~/.aws/cli/cache/b.json && printf 'c' > ~/.aws/config",
- );
- await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" });
-
- await sb.run(h1, "mkdir -p ~/.config/gh && printf 'oauth_token: gho_OK' > ~/.config/gh/hosts.yml");
- await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" });
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await sb.run(h2, "mkdir -p ~/.aws && printf 'blocking' > ~/.aws/sso");
- const anomalies: string[] = [];
- const restored = await materializeDeviceFlowLogins({
- sandbox: sb,
- handle: h2,
- keychain: k,
- ownerId: "U1",
- onAnomaly: (s, d) => anomalies.push(`${s}: ${d}`),
- });
- assert.deepEqual(restored.slice().sort(), ["gh"], "the healthy bundle restores even when a sibling bundle fails");
- assert.match(anomalies.join("\n"), /aws: restore failed mid-bundle/);
- const leftover = await sb.run(h2, "find ~/.aws -type f 2>/dev/null | grep -v '/sso$' | wc -l");
- assert.equal(leftover.stdout.trim(), "0", "no partial multi-file set is left behind");
-
- await sb.run(h2, "rm -f ~/.aws/sso");
- const clean = await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- assert.deepEqual(clean, ["aws"], "a later healthy restore brings the whole set");
-});
-
-test("a root that mutates during the tar is reported volatile and its service is skipped this turn", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/gh && printf 'oauth_token: gho_MIDWRITE' > ~/.config/gh/hosts.yml");
- const volatileSb = {
- ...sb,
- run: async (handle: unknown, cmd: string, opts?: unknown) => {
- const result = await sb.run(handle as never, cmd, opts as never);
- if (cmd.includes(".cred-capture-")) return { ...result, stdout: `${result.stdout}VOLATILE\tgh\n` };
- return result;
- },
- };
- assert.deepEqual(
- await captureDeviceFlowLogins({ sandbox: volatileSb as never, handle: h, keychain: k, ownerId: "U1" }),
- [],
- "a torn snapshot is never saved",
- );
- assert.ok(!(await k.listByOwner("U1")).some((c) => c.service === "gh"));
-
- assert.deepEqual(
- await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }),
- ["gh"],
- "the next quiet turn captures it cleanly",
- );
-});
-
-test("~/.netrc: whole-file capture keeps every tool's entries, and a live machine's copy is never clobbered", async () => {
- const sb = sprites();
- const k = kc();
- const layers = rw(scopeId("personal", "U1"));
- const h1 = await sb.provision(layers);
- await sb.run(
- h1,
- "printf 'machine api.wandb.ai\\n login user\\n password wandb_KEY\\nmachine api.heroku.com\\n login u@x\\n password heroku_TOK\\n' > ~/.netrc && chmod 600 ~/.netrc",
- );
- assert.deepEqual(await captureDeviceFlowLogins({ sandbox: sb, handle: h1, keychain: k, ownerId: "U1" }), ["netrc"]);
-
- rmSync(ff.homeDir(h1.id), { recursive: true, force: true });
- const h2 = await sb.provision(layers);
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const restored = await sb.run(h2, "cat ~/.netrc");
- assert.match(restored.stdout, /wandb_KEY/, "wandb's entry survived the rebuild");
- assert.match(restored.stdout, /heroku_TOK/, "heroku's entry survived in the same file");
-
- await sb.run(h2, "printf 'machine api.wandb.ai\\n login user\\n password wandb_NEWER\\n' > ~/.netrc");
- await materializeDeviceFlowLogins({ sandbox: sb, handle: h2, keychain: k, ownerId: "U1" });
- const kept = await sb.run(h2, "cat ~/.netrc");
- assert.match(kept.stdout, /wandb_NEWER/, "a live machine's .netrc is never clobbered by restore");
- assert.doesNotMatch(kept.stdout, /heroku_TOK/, "restore did not merge stale entries over the live file");
-});
-
-test("a squatted quarantine dir makes capture fail LOUDLY, never silently skip", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(
- h,
- "rm -rf ~/.agent-displaced && printf squat > ~/.agent-displaced && mkdir -p ~/.config/gh && printf 'oauth_token: gho_X' > ~/.config/gh/hosts.yml",
- );
- await assert.rejects(
- captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" }),
- /workspace unavailable|write .* failed/,
- );
-});
-
-test("old-name debris (.agent-cred-*) is reaped by the next capture", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(
- h,
- "printf stale > ~/.agent-cred-state && printf leak > ~/.agent-cred-capture.abc123 && " +
- "mkdir -p ~/.config/gh && printf 'oauth_token: gho_X' > ~/.config/gh/hosts.yml",
- );
- await captureDeviceFlowLogins({ sandbox: sb, handle: h, keychain: k, ownerId: "U1" });
- const left = await sb.run(h, "ls ~/.agent-cred-* 2>/dev/null | wc -l");
- assert.equal(left.stdout.trim(), "0", "legacy scratch and state files are removed");
-});
-
-test("a static sweep anomaly fires once, not on every subsequent capture", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.config/gh && printf 'oauth_token: gho_X' > ~/.config/gh/hosts.yml");
- await k.save({
- ownerId: "U1",
- service: "weird",
- files: [{ path: ".weird/has space/tok", contentBase64: Buffer.from("x").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- const anomalies: string[] = [];
- const input = {
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- onAnomaly: (s: string, d: string) => anomalies.push(`${s}: ${d}`),
- };
- await captureDeviceFlowLogins(input);
- const afterFirst = anomalies.length;
- assert.ok(afterFirst >= 1, "the unsweepable stored path is reported");
- await captureDeviceFlowLogins(input);
- assert.equal(anomalies.length, afterFirst, "the identical static condition is not re-reported next turn");
-});
-
-test("register_login under a cap-blown fixed dotdir is allowed until the auto service actually persists", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.cargo && printf 'crates_TOKEN' > ~/.cargo/credentials.toml");
-
- const result = await registerLoginPaths({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- service: "crates",
- paths: [{ path: ".cargo/credentials.toml", kind: "file" }],
- });
- assert.deepEqual(
- result,
- { service: "crates", captured: true },
- "no cargo record exists, so the sweep is not covering it",
- );
-
- await k.save({
- ownerId: "U2",
- service: "cargo",
- files: [{ path: ".cargo/credentials.toml", contentBase64: Buffer.from("x").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- const h2 = await sb.provision(rw(scopeId("personal", "U2")));
- await sb.run(h2, "mkdir -p ~/.cargo && printf 'y' > ~/.cargo/credentials.toml");
- await assert.rejects(
- registerLoginPaths({
- sandbox: sb,
- handle: h2,
- keychain: k,
- ownerId: "U2",
- service: "crates",
- paths: [{ path: ".cargo/credentials.toml", kind: "file" }],
- }),
- /already captured automatically as "cargo"/,
- );
-});
-
-test("register_login refuses capture bookkeeping paths", async () => {
- const sb = sprites();
- const k = kc();
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "printf 'notacred' > ~/.cred-state");
- await assert.rejects(
- registerLoginPaths({
- sandbox: sb,
- handle: h,
- keychain: k,
- ownerId: "U1",
- service: "sneaky",
- paths: [{ path: ".cred-state", kind: "file" }],
- }),
- /capture bookkeeping/,
- );
-});
-
-test("a concurrent-save race skips that service and retries it on the next capture", async () => {
- const sb = sprites();
- const k = kc();
- const anomalies: string[] = [];
- const h = await sb.provision(rw(scopeId("personal", "U1")));
- await sb.run(h, "mkdir -p ~/.racy ~/.calm && printf 'r1' > ~/.racy/creds && printf 'c1' > ~/.calm/creds");
- const base = { sandbox: sb, handle: h, keychain: k, ownerId: "U1" };
- await registerLoginPaths({ ...base, service: "racy", paths: [{ path: ".racy/creds", kind: "file" }] });
- await registerLoginPaths({ ...base, service: "calm", paths: [{ path: ".calm/creds", kind: "file" }] });
-
- await sb.run(h, "printf 'r2_ROTATED' > ~/.racy/creds && printf 'c2_ROTATED' > ~/.calm/creds");
- const realSave = k.save.bind(k);
- let raced = false;
- k.save = async (input) => {
- if (input.service === "racy" && !raced) {
- raced = true;
- throw new KeychainError(503, "credential for racy is being written concurrently — retry");
- }
- return realSave(input);
- };
- const first = await captureDeviceFlowLogins({ ...base, onAnomaly: (s, d) => anomalies.push(`${s}: ${d}`) });
- assert.deepEqual(first, ["calm"], "the raced service is skipped, the rest of the sweep continues");
- assert.match(anomalies.join("\n"), /racy: keychain write raced a concurrent save/);
-
- const second = await captureDeviceFlowLogins(base);
- assert.deepEqual(second, ["racy"], "the raced service re-ships and saves on the next capture");
-});
-
-test("removed layer tools retain quarantine, capture exclusion and reset-to-legacy", async () => {
- for (const shared of [false, true]) {
- const built = buildApp(
- testConfig({ dataDir: mkdtempSync(join(tmpdir(), "dfp-removed-tool-")), signingSecret: "test" }),
- );
- assert.equal(built.credentialTools.length, 0);
- const request = shared ? channel("!run echo ready") : dm("!run echo ready");
- const ownerId = shared ? scopeId("channel", "C1") : "U1";
- const targetScope = shared ? scopeId("channel", "C1") : scopeId("personal", "U1");
- await selectDefaultSandbox(built, "U1", targetScope);
- await built.keychain!.save({
- ownerId,
- service: "retired",
- files: [{ path: ".retired/session", contentBase64: Buffer.from("stored-login").toString("base64") }],
- origin: DEVICE_FLOW_ORIGIN,
- });
- const read = "!run cat ~/.retired/session";
- assert.equal((await built.app.turn({ ...request, text: read })).reply, "stored-login");
- await built.deviceFlowCutover.set(targetScope, "retired", "ephemeral_only", "admin");
- assert.equal(
- (await built.app.turn({ ...request, text: "!run test -e ~/.retired/session && echo found || echo absent" }))
- .reply,
- "absent",
- );
- await built.app.turn({ ...request, text: "!run mkdir -p ~/.retired && printf tampered > ~/.retired/session" });
- const stored = (await built.keychain!.listByOwner(ownerId)).find((record) => record.service === "retired")!;
- const files = await built.keychain!.materializeOwn(ownerId);
- assert.equal(files.length, 0);
- assert.ok(stored);
- await built.deviceFlowCutover.set(targetScope, "retired", "prefer_ephemeral", "admin");
- assert.equal((await built.app.turn({ ...request, text: read })).reply, "tampered");
- await built.deviceFlowCutover.clear(targetScope, "retired");
- assert.equal((await built.app.turn({ ...request, text: read })).reply, "stored-login");
- await built.deviceFlowCutover.set(targetScope, "retired", "ephemeral_only", "admin");
- await built.deviceFlowCutover.set(targetScope, "retired", "legacy", "admin");
- assert.equal((await built.app.turn({ ...request, text: read })).reply, "stored-login");
- }
-});
diff --git a/test/environment-facts.test.ts b/test/environment-facts.test.ts
index ade5ba77d..9c95c5732 100644
--- a/test/environment-facts.test.ts
+++ b/test/environment-facts.test.ts
@@ -1,11 +1,6 @@
import { test } from "node:test";
import assert from "node:assert/strict";
-import {
- renderComputerBlock,
- renderResidentLoginsBlock,
- renderConnectedAppsBlock,
-} from "../src/core/environment-facts.ts";
-import { RESIDENT_AUTH_CONNECTORS, type ScopeLivenessRecord } from "../src/credentials/resident-auth.ts";
+import { renderComputerBlock, renderConnectedAppsBlock } from "../src/core/environment-facts.ts";
import type { ConnectorStatusRecord } from "../src/credentials/connector-status.ts";
import type { AgentComputerSpec } from "../src/sandbox/sandbox.ts";
@@ -57,33 +52,6 @@ test("computer block: partial spec omits the fields it lacks (no empty fragments
assert.doesNotMatch(out, /NOT installed/);
});
-test("logins block: null record renders nothing (first turn, no probe yet)", () => {
- assert.equal(renderResidentLoginsBlock(null, RESIDENT_AUTH_CONNECTORS), "");
-});
-
-test("logins block: all-absent record renders nothing (no tools present)", () => {
- const record: ScopeLivenessRecord = {
- scopeId: "personal:U1",
- checkedAt: 1,
- connectors: Object.fromEntries(RESIDENT_AUTH_CONNECTORS.map((c) => [c.id, "absent" as const])),
- };
- assert.equal(renderResidentLoginsBlock(record, RESIDENT_AUTH_CONNECTORS), "");
-});
-
-test("logins block: shows active with a check, inactive with its exact reauth command, omits absent", () => {
- const record: ScopeLivenessRecord = {
- scopeId: "personal:U1",
- checkedAt: 1,
- connectors: { gh: "active", glab: "inactive", gcloud: "absent" },
- };
- const out = renderResidentLoginsBlock(record, RESIDENT_AUTH_CONNECTORS);
- assert.match(out, /## Your logins/);
- assert.match(out, /GitHub — ✓ signed in/);
- assert.match(out, /GitLab — ✗ not signed in.*`glab auth login`/);
- assert.doesNotMatch(out, /Google Cloud/);
- assert.doesNotMatch(out, /AWS/);
-});
-
test("connected-apps block: lists only admin-configured providers and the exact connection URL", () => {
const url = "https://qm.example/keychain";
const unavailable = renderConnectedAppsBlock(null, [], url);
diff --git a/test/execute-env-credentials.test.ts b/test/execute-env-credentials.test.ts
index 07431793f..11b072d8d 100644
--- a/test/execute-env-credentials.test.ts
+++ b/test/execute-env-credentials.test.ts
@@ -43,61 +43,7 @@ function acmecliBrokeredLayer(binary?: string, approvals?: Array<{ pattern: stri
}
const actor = { externalId: "U_ENV_BROKER" };
-test("selected broker credential uses isolated execute with policy before vending", async () => {
- let assumes = 0;
- const sentinels = {
- access: "AKIA_CREDENTIAL_EXEC_SENTINEL",
- secret: "credential_exec_secret_sentinel",
- token: "credential_exec_session_sentinel",
- };
- const built = buildApp(
- testConfig({
- dataDir: mkdtempSync(join(tmpdir(), "dfp-credential-exec-")),
- maxAttempts: 1,
- signingSecret: "device-flow-test-secret",
- deploymentLayerDir: acmecliBrokeredLayer("env"),
- }),
- {
- credentialBrokers: {
- acmecli: createAwsRoleBroker({
- roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
- region: "us-west-2",
- sessionActions: ["execute-api:Invoke"],
- assumeRole: async () => {
- assumes++;
- return {
- Credentials: {
- AccessKeyId: sentinels.access,
- SecretAccessKey: sentinels.secret,
- SessionToken: sentinels.token,
- Expiration: new Date(Date.now() + 3_600_000),
- },
- };
- },
- }),
- },
- },
- );
-
- const personal = scopeId("personal", actor.externalId);
- const conversation = { kind: "dm" as const, threadRef: "dm:env-broker", audience: [actor] };
- await built.deviceFlowCutover.set(personal, "acmecli", "ephemeral_only", "[email protected]");
- const run = (params: object) =>
- built.app.turn({ surface: "slack", actor, conversation, text: `!execute ${JSON.stringify(params)}` });
- await assert.rejects(run({ command: "env", credentials: ["broker_acmecli"] }), /requires scope:owner/);
- assert.equal(assumes, 0);
- const selected = await run({
- command:
- 'test "$AWS_ACCESS_KEY_ID" = AKIA_CREDENTIAL_EXEC_SENTINEL && test -z "${AGENT_API_TOKEN-}" && echo selected',
- credentials: ["broker_acmecli"],
- ownerAuth: true,
- });
- assert.equal(selected.reply, "selected");
- assert.equal(assumes, 1);
- assert.ok(ff.names().every((name) => !name.includes("credential-exec")));
-});
-
-test("legacy role broker vends only for explicitly selected scoped execution", async () => {
+test("role broker vends only for explicitly selected scoped execution", async () => {
let assumes = 0;
const built = buildApp(
testConfig({
@@ -150,3 +96,76 @@ test("legacy role broker vends only for explicitly selected scoped execution", a
assert.equal((await run('!run test -z "${AWS_ACCESS_KEY_ID-}" && echo absent')).reply, "absent");
assert.equal(assumes, 1);
});
+
+test("selected broker execute honors deployment approval rules before vending credentials", async () => {
+ let assumes = 0;
+ const built = buildApp(
+ testConfig({
+ dataDir: mkdtempSync(join(tmpdir(), "dfp-credexec-approval-")),
+ signingSecret: "device-flow-test-secret",
+ deploymentLayerDir: acmecliBrokeredLayer("env", [
+ { pattern: "\\benv\\b\\s+tool\\b", reason: "mutating subcommand" },
+ ]),
+ }),
+ {
+ credentialBrokers: {
+ acmecli: createAwsRoleBroker({
+ roleArn: "arn:aws:iam::123456789012:role/acmecli-broker",
+ region: "us-west-2",
+ sessionActions: ["execute-api:Invoke"],
+ assumeRole: async () => {
+ assumes++;
+ return {
+ Credentials: {
+ AccessKeyId: "AKIA_APPROVAL_GATE",
+ SecretAccessKey: "approval_gate_secret_value",
+ SessionToken: "approval_gate_session_token",
+ Expiration: new Date(Date.now() + 3_600_000),
+ },
+ };
+ },
+ }),
+ },
+ },
+ );
+ const personal = scopeId("personal", actor.externalId);
+ const conversation = { kind: "dm" as const, threadRef: "dm:credexec-approval", audience: [actor] };
+ await selectDefaultSandbox(built, actor.externalId, personal);
+
+ const gated = await built.app.turn({
+ surface: "slack",
+ actor,
+ conversation,
+ text: `!execute ${JSON.stringify({ command: "env tool delete", credentials: ["broker_acmecli"] })}`,
+ });
+ assert.equal(gated.status, "pending_approval");
+ assert.equal(assumes, 0, "no AssumeRole call happens for a blocked command");
+ const pending = gated.pendingApprovals![0]!;
+ assert.match(pending.reason, /mutating subcommand/);
+
+ const approved = await built.app.turn({
+ surface: "slack",
+ actor,
+ conversation,
+ text: `!execute ${JSON.stringify({ command: "env tool delete", credentials: ["broker_acmecli"] })}`,
+ approval: { requestId: pending.requestId, approved: true },
+ });
+ assert.equal(approved.status, "ok", approved.reason);
+ assert.equal(assumes, 1, "approval unblocks exactly one vended invocation");
+
+ const unrelated = await built.app.turn({
+ surface: "slack",
+ actor,
+ conversation: { ...conversation, threadRef: "dm:credexec-approval-3" },
+ text: `!execute ${JSON.stringify({ command: "env", credentials: ["broker_acmecli"] })}`,
+ });
+ assert.equal(unrelated.status, "ok", "subcommands without approval rules run without a grant");
+ assert.equal(assumes, 1, "the broker's per-actor credential cache is reused within its TTL");
+ assert.match(unrelated.reply ?? "", /<redacted:credential>/);
+ const durable = JSON.stringify(await built.sessions.getEntries(unrelated.sessionId!));
+ for (const value of ["AKIA_APPROVAL_GATE", "approval_gate_secret_value", "approval_gate_session_token"]) {
+ assert.ok(!(unrelated.reply ?? "").includes(value));
+ assert.ok(!durable.includes(value));
+ }
+ assert.deepEqual(await built.keychain!.grantsForScope(personal), []);
+});
diff --git a/test/execute-file-credentials.test.ts b/test/execute-file-credentials.test.ts
new file mode 100644
index 000000000..54429d1e4
--- /dev/null
+++ b/test/execute-file-credentials.test.ts
@@ -0,0 +1,418 @@
+import { finishProcessCredentials, prepareExecutionFiles } from "../src/credentials/execute-files.ts";
+import { createBackgroundBroker } from "../src/connectors/background-exec-broker.ts";
+import { createMemoryProcessRegistry } from "../src/processes/process-registry.ts";
+import assert from "node:assert/strict";
+import test, { type TestContext } from "node:test";
+import { execFile, spawn, type ChildProcess } from "node:child_process";
+import { promisify } from "node:util";
+import { mkdtemp, mkdir, readFile, writeFile, rm, access } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { createKeychain, type CredentialFile, type Keychain } from "../src/credentials/keychain.ts";
+import { reconcileProcesses } from "../src/processes/reconcile.ts";
+import { createMemoryMap } from "../src/persistence/durable-map.ts";
+import { deriveConnectorKey } from "../src/connectors/connector-client-store.ts";
+import { createToolContext, type ToolContextDeps } from "../src/tools/primitives.ts";
+import type { ProcessSandbox, ProcessState, Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";
+import { scopeId } from "../src/types.ts";
+
+const run = promisify(execFile);
+const SECRET_TOKEN = "synthetic-background-token-123";
+const owner = "file-execution-test";
+const scope = scopeId("personal", owner);
+const files = (value: string): CredentialFile[] => [
+ { path: ".aws/sso/cache/session.json", contentBase64: Buffer.from(value).toString("base64") },
+];
+
+async function fixture(t: TestContext) {
+ const root = await mkdtemp(join(tmpdir(), "qm-file-execution-test-"));
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const keychain = createKeychain({
+ creds: createMemoryMap(),
+ grants: createMemoryMap(),
+ asks: createMemoryMap(),
+ key: deriveConnectorKey("synthetic-test-key"),
+ });
+ const credential = await keychain.save({
+ ownerId: owner,
+ service: "aws",
+ files: files("original"),
+ origin: "agent-session:original",
+ accountLabel: "original owner",
+ });
+ const handle: SandboxHandle = { id: "default", rootDir: root, homeDir: root };
+ const commands: Array<{ handle: SandboxHandle; command: string }> = [];
+ const sandbox = {
+ async run(h: SandboxHandle, command: string) {
+ commands.push({ handle: h, command });
+ try {
+ const result = await run("/bin/sh", ["-c", command], {
+ cwd: h.rootDir,
+ env: { PATH: process.env.PATH, HOME: h.homeDir, ...h.env },
+ maxBuffer: 1024 * 1024,
+ });
+ return { ...result, code: 0, timedOut: false };
+ } catch (error) {
+ const result = error as Error & { stdout: string; stderr: string; code: number };
+ return { stdout: result.stdout, stderr: result.stderr, code: result.code, timedOut: false };
+ }
+ },
+ async writeFileBytes(h: SandboxHandle, path: string, bytes: Uint8Array) {
+ await mkdir(join(h.rootDir, path, ".."), { recursive: true });
+ await writeFile(join(h.rootDir, path), bytes);
+ },
+ async readFile(h: SandboxHandle, path: string) {
+ try {
+ return await readFile(path.startsWith("/") ? path : join(h.rootDir, path), "utf8");
+ } catch {
+ return null;
+ }
+ },
+ async readFileBytes(h: SandboxHandle, path: string) {
+ return readFile(join(h.rootDir, path));
+ },
+ } as unknown as Sandbox;
+ let resolutions = 0;
+ const context = (extra: Partial<ToolContextDeps> = {}, ownerScope = false) =>
+ createToolContext({
+ sandbox,
+ provision: async () => handle,
+ provisionOwnerAuth: async () => ({ ...handle, id: "owner" }),
+ provisionScratch: async () => ({ ...handle, id: "scratch" }),
+ layers: [{ scopeId: scope, mountPath: "", mode: "rw" }],
+ commandPolicy: () => ({ mode: "denylist", rules: [] }),
+ authorizeCommand: () => false,
+ commandCredentials: [
+ {
+ handle: "aws",
+ scope: ownerScope ? "owner" : "scoped",
+ async resolve() {
+ resolutions++;
+ const materialized = await keychain.materializeOwnById(owner, credential.id, scope);
+ assert.equal(materialized.kind, "file");
+ if (materialized.kind !== "file") throw new Error("Expected files");
+ return {
+ env: [],
+ files: {
+ files: materialized.files,
+ save: (updated: CredentialFile[]) => keychain.updateFiles(materialized, updated),
+ source: { credentialId: materialized.credentialId, ownerId: owner, service: "aws" },
+ },
+ };
+ },
+ },
+ {
+ handle: "token",
+ scope: "scoped",
+ async resolve() {
+ return { env: [{ key: "API_TOKEN", value: SECRET_TOKEN }] };
+ },
+ },
+ ],
+ grantedHandles: [],
+ workspace: {} as never,
+ deploy: {} as never,
+ acl: {} as never,
+ createdBy: owner,
+ ...extra,
+ });
+ return { keychain, credential, handle, sandbox, commands, context, resolutions: () => resolutions };
+}
+
+for (const target of ["default", "owner", "scratch", "selected"] as const) {
+ test(`${target} execution persists refreshed files and cleans its temporary home`, async (t) => {
+ const f = await fixture(t);
+ const selected = { ...f.handle, id: "selected" };
+ const resource = { id: "selected", ownerScopeId: scope };
+ const ctx = f.context(
+ target === "selected"
+ ? {
+ sandboxResources: {} as never,
+ accessSandboxResource: async () =>
+ ({ resource, crossScope: false, egress: { mode: "allowlist", hosts: [] }, commandPolicy: null }) as never,
+ provisionResource: async (_access, authorize) => {
+ authorize?.({ resource, crossScope: false, egress: {} } as never);
+ return selected;
+ },
+ }
+ : {},
+ target === "owner",
+ );
+ const result = await ctx.execute('printf refreshed > "$HOME/.aws/sso/cache/session.json"; printf "%s" "$HOME"', {
+ credentials: ["aws"],
+ ...(target === "owner" ? { ownerAuth: true } : {}),
+ ...(target === "scratch" ? { scratch: true } : {}),
+ ...(target === "selected" ? { sandboxId: "selected" } : {}),
+ });
+ assert.equal(result.code, 0, result.stderr);
+ assert.equal(f.commands.find((entry) => entry.command.startsWith("printf refreshed"))?.handle.id, target);
+ await assert.rejects(access(result.stdout));
+ const next = await f.keychain.materializeOwnById(owner, f.credential.id, scope);
+ assert.equal(next.kind, "file");
+ if (next.kind === "file") assert.equal(Buffer.from(next.files[0]!.contentBase64, "base64").toString(), "refreshed");
+ const meta = await f.keychain.getCredential(f.credential.id);
+ assert.equal(meta?.origin, "agent-session:original");
+ assert.equal(meta?.accountLabel, "original owner");
+ const second = await f.context().execute('cat "$HOME/.aws/sso/cache/session.json"', { credentials: ["aws"] });
+ assert.equal(second.stdout, "refreshed");
+ });
+}
+
+test("unrequested credentials are never materialized", async (t) => {
+ const f = await fixture(t);
+ const result = await f.context().execute('test ! -e "$HOME/.aws/sso/cache/session.json"');
+ assert.equal(result.code, 0);
+ assert.equal(f.resolutions(), 0);
+});
+
+test("failed commands still persist refreshes", async (t) => {
+ const f = await fixture(t);
+ const result = await f
+ .context()
+ .execute('printf refreshed > "$HOME/.aws/sso/cache/session.json"; exit 7', { credentials: ["aws"] });
+ assert.equal(result.code, 7);
+ const second = await f.context().execute('cat "$HOME/.aws/sso/cache/session.json"', { credentials: ["aws"] });
+ assert.equal(second.stdout, "refreshed");
+});
+
+test("concurrent file updates cannot overwrite a newer credential or its grants", async (t) => {
+ const f = await fixture(t);
+ const grant = await f.keychain.createGrant({
+ credentialId: f.credential.id,
+ ownerId: owner,
+ audienceScopeId: scopeId("channel", "test"),
+ mode: "standing",
+ purpose: "test",
+ });
+ const a = await f.keychain.materializeOwnById(owner, f.credential.id, scope);
+ const b = await f.keychain.materializeOwnById(owner, f.credential.id, scope);
+ if (a.kind !== "file" || b.kind !== "file") throw new Error("Expected files");
+ await f.keychain.updateFiles(a, files("newer"));
+ await assert.rejects(f.keychain.updateFiles(b, files("stale")), /changed during execution/);
+ assert.equal((await f.keychain.getGrant(grant.id))?.status, "active");
+});
+
+test("symlink replacement is rejected and the ephemeral home is removed", async (t) => {
+ const f = await fixture(t);
+ await assert.rejects(
+ f
+ .context()
+ .execute('rm "$HOME/.aws/sso/cache/session.json"; ln -s /etc/passwd "$HOME/.aws/sso/cache/session.json"', {
+ credentials: ["aws"],
+ }),
+ /symlinks/,
+ );
+ const execution = f.commands.find((entry) => entry.command.startsWith('rm "$HOME'))!;
+ await assert.rejects(access(execution.handle.env!.HOME!));
+});
+
+test("refreshes made through a use grant never change the owner's saved login", async (t) => {
+ const f = await fixture(t);
+ const grant = await f.keychain.createGrant({
+ credentialId: f.credential.id,
+ ownerId: owner,
+ audienceScopeId: scope,
+ mode: "standing",
+ purpose: "test grant writeback",
+ });
+ const prepared = await f.keychain.prepareMaterialize(grant.id, scope, owner);
+ if (prepared.materialized.kind !== "file") throw new Error("Expected files");
+ await prepared.commit();
+ const before = prepared.materialized.files;
+ await f.keychain.updateFiles(prepared.materialized, [
+ { path: ".aws/config", contentBase64: Buffer.from("credential_process=malicious").toString("base64") },
+ ]);
+ const saved = await f.keychain.materializeOwnById(owner, f.credential.id, scope);
+ if (saved.kind !== "file") throw new Error("Expected files");
+ assert.deepEqual(saved.files, before);
+});
+
+test("an execution sweeps only stale credential directories", async (t) => {
+ const f = await fixture(t);
+ const stale = await prepareExecutionFiles(f.sandbox, f.handle, [{ files: files("stale"), save: async () => {} }]);
+ const live = await prepareExecutionFiles(f.sandbox, f.handle, [{ files: files("live"), save: async () => {} }]);
+ const staleDir = stale.env.HOME!.replace(/\/home$/, "");
+ await f.sandbox.run(f.handle, `touch -d '3 hours ago' ${staleDir}`);
+ await f.context().execute("true", { credentials: ["aws"] });
+ await assert.rejects(access(stale.env.HOME!));
+ await access(`${live.env.HOME}/.aws/sso/cache/session.json`);
+ await live.finish();
+});
+
+test("concurrent executions keep their own credential directories", async (t) => {
+ const f = await fixture(t);
+ const entered = Promise.withResolvers<string>();
+ const release = Promise.withResolvers<void>();
+ const sandbox = {
+ ...f.sandbox,
+ async run(handle: SandboxHandle, command: string, opts: Parameters<Sandbox["run"]>[2]) {
+ if (command.startsWith("credential-hold")) {
+ entered.resolve(handle.env!.HOME!);
+ await release.promise;
+ }
+ return f.sandbox.run(handle, command, opts);
+ },
+ };
+ const ctx = f.context({ sandbox });
+ const held = ctx.execute("credential-hold 2>/dev/null; cat ~/.aws/sso/cache/session.json", { credentials: ["aws"] });
+ const home = await entered.promise;
+ assert.equal((await ctx.execute("echo unrelated")).stdout.trim(), "unrelated");
+ await ctx.execute("true", { credentials: ["aws"] });
+ await access(`${home}/.aws/sso/cache/session.json`);
+ release.resolve();
+ assert.equal((await held).stdout, "original");
+ await assert.rejects(access(home));
+});
+
+function background(
+ f: Awaited<ReturnType<typeof fixture>>,
+ opts: {
+ keychain?: Pick<Keychain, "writebackBaseline" | "updateFiles">;
+ readFailures?: number;
+ startFailure?: boolean;
+ } = {},
+) {
+ const registry = createMemoryProcessRegistry();
+ const jobs = new Map<string, { output: string; code?: number; child: ChildProcess }>();
+ let readFailures = opts.readFailures ?? 0;
+ const sandbox = {
+ ...f.sandbox,
+ profile: { processSessions: true },
+ async startProcess(h: SandboxHandle, command: string, startOpts?: { env?: Record<string, string> }) {
+ if (opts.startFailure) throw new Error("sandbox refused the process");
+ const processId = `job-${jobs.size}`;
+ const child = spawn("/bin/sh", ["-c", command], {
+ cwd: h.rootDir,
+ env: { PATH: process.env.PATH, HOME: h.homeDir, ...h.env, ...startOpts?.env },
+ });
+ const job: { output: string; code?: number; child: ChildProcess } = { output: "", child };
+ child.stdout!.on("data", (chunk) => (job.output += chunk));
+ child.on("exit", (code) => (job.code = code ?? 143));
+ jobs.set(processId, job);
+ return { processId };
+ },
+ async readProcess(_h: SandboxHandle, processId: string, readOpts?: { sinceCursor?: number; waitMs?: number }) {
+ if (readFailures-- > 0) throw new Error("transient read failure");
+ const job = jobs.get(processId)!;
+ if (job.code === undefined && readOpts?.waitMs)
+ await new Promise((resolve) => setTimeout(resolve, Math.min(readOpts.waitMs!, 50)));
+ return {
+ chunks: job.output.slice(readOpts?.sinceCursor ?? 0),
+ cursor: job.output.length,
+ status: job.code === undefined ? { state: "running" as const } : { state: "exited" as const, code: job.code },
+ };
+ },
+ async writeStdin() {},
+ async signalProcess(_h: SandboxHandle, processId: string) {
+ jobs.get(processId)!.child.kill();
+ },
+ async listProcesses() {
+ return [...jobs].map(([processId, job]) => ({
+ processId,
+ status: job.code === undefined ? { state: "running" as const } : { state: "exited" as const, code: job.code },
+ }));
+ },
+ } as unknown as ProcessSandbox;
+ const finish = (h: SandboxHandle, processId: string, keychain = opts.keychain ?? f.keychain) =>
+ finishProcessCredentials({ sandbox, processes: registry, keychain }, h, processId);
+ const broker = () =>
+ createBackgroundBroker({
+ sandbox,
+ registry,
+ scopeId: scope,
+ pollMs: 10,
+ onExit: (h, processId) => finish(h, processId),
+ });
+ const ctx = (b = broker()) => f.context({ sandbox, backgroundBroker: b });
+ const waitForExit = async (processId: string) => {
+ while (jobs.get(processId)?.code === undefined) await new Promise((resolve) => setTimeout(resolve, 20));
+ };
+ return { registry, sandbox, broker, ctx, finish, waitForExit };
+}
+
+const savedValue = async (f: Awaited<ReturnType<typeof fixture>>) => {
+ const saved = await f.keychain.materializeOwnById(owner, f.credential.id, scope);
+ assert.equal(saved.kind, "file");
+ return saved.kind === "file" ? Buffer.from(saved.files[0]!.contentBase64, "base64").toString() : "";
+};
+
+const refresh =
+ 'printf %s "$HOME" > staged-home; sleep 0.5; cat "$HOME/.aws/sso/cache/session.json" > seen; printf background > "$HOME/.aws/sso/cache/session.json"';
+
+test("background credentials stay staged until the job exits, then refreshes are saved", async (t) => {
+ const f = await fixture(t);
+ const bg = background(f);
+ const ctx = bg.ctx();
+ const started = await ctx.backgroundStart(refresh, { purpose: "Refresh a background login", credentials: ["aws"] });
+ assert.equal(started.status.state, "running");
+ assert.equal((await ctx.execute("echo unrelated")).stdout.trim(), "unrelated");
+ let status: ProcessState = started.status;
+ while (status.state !== "exited") status = (await ctx.backgroundPoll(started.processId, { waitSeconds: 1 })).status;
+ assert.equal(await readFile(join(f.handle.rootDir, "seen"), "utf8"), "original");
+ assert.equal(await savedValue(f), "background");
+ await assert.rejects(access(await readFile(join(f.handle.rootDir, "staged-home"), "utf8")));
+ assert.equal(await bg.registry.credentialFiles(started.processId), null);
+});
+
+test("background start output masks the command's credential values", async (t) => {
+ const f = await fixture(t);
+ const bg = background(f);
+ const started = await bg.ctx().backgroundStart('echo "early $API_TOKEN"; sleep 0.3', {
+ purpose: "Print a token",
+ credentials: ["token"],
+ });
+ assert.match(started.output, /early <redacted:credential>/);
+ assert.ok(!started.output.includes(SECRET_TOKEN));
+ assert.ok(!JSON.stringify(await bg.registry.get(started.processId)).includes(SECRET_TOKEN));
+});
+
+test("a failed background writeback keeps the refresh for a later retry", async (t) => {
+ const f = await fixture(t);
+ let outage = true;
+ const keychain: Pick<Keychain, "writebackBaseline" | "updateFiles"> = {
+ writebackBaseline: (source, fingerprint) => f.keychain.writebackBaseline(source, fingerprint),
+ async updateFiles(materialized, updated) {
+ if (outage) throw new Error("keychain database unavailable");
+ await f.keychain.updateFiles(materialized, updated);
+ },
+ };
+ const bg = background(f, { keychain });
+ const started = await bg.ctx().backgroundStart(refresh, { purpose: "Refresh a login", credentials: ["aws"] });
+ await bg.waitForExit(started.processId);
+ await assert.rejects(bg.ctx().backgroundPoll(started.processId), /will retry/);
+ const staged = await readFile(join(f.handle.rootDir, "staged-home"), "utf8");
+ await access(staged);
+ assert.equal(await savedValue(f), "original");
+ assert.equal((await bg.registry.get(started.processId))?.credentialsPending, true);
+ outage = false;
+ await reconcileProcesses(bg.sandbox, f.handle, bg.registry, scope, (h, processId) => bg.finish(h, processId));
+ assert.equal(await savedValue(f), "background");
+ await assert.rejects(access(staged));
+ assert.equal(await bg.registry.credentialFiles(started.processId), null);
+});
+
+test("a background start that fails after launch leaves staging to the exit hook", async (t) => {
+ const f = await fixture(t);
+ const bg = background(f, { readFailures: 1 });
+ await assert.rejects(
+ bg.ctx().backgroundStart(refresh, { purpose: "Refresh a login", credentials: ["aws"] }),
+ /transient read failure/,
+ );
+ const [record] = await bg.registry.listByScope(scope);
+ await bg.waitForExit(record!.processId);
+ assert.equal(await readFile(join(f.handle.rootDir, "seen"), "utf8"), "original");
+ await bg.ctx().backgroundPoll(record!.processId);
+ assert.equal(await savedValue(f), "background");
+ await assert.rejects(access(await readFile(join(f.handle.rootDir, "staged-home"), "utf8")));
+});
+
+test("a background start that fails before launch removes its staging", async (t) => {
+ const f = await fixture(t);
+ const bg = background(f, { startFailure: true });
+ const before = (await run("sh", ["-c", "ls -d /tmp/qm-credentials.* 2>/dev/null | wc -l"])).stdout;
+ await assert.rejects(
+ bg.ctx().backgroundStart(refresh, { purpose: "Refresh a login", credentials: ["aws"] }),
+ /sandbox refused/,
+ );
+ assert.equal((await run("sh", ["-c", "ls -d /tmp/qm-credentials.* 2>/dev/null | wc -l"])).stdout, before);
+});
diff --git a/test/execute-scratch.test.ts b/test/execute-scratch.test.ts
index ade03dbfd..cda17309f 100644
--- a/test/execute-scratch.test.ts
+++ b/test/execute-scratch.test.ts
@@ -1,5 +1,4 @@
import { SandboxProvisionCleanupError, cleanupFailedProvision } from "../src/sandbox/sandbox.ts";
-import { execFileSync } from "node:child_process";
import { createTurnSandboxes, type TurnSandboxContext } from "../src/core/orchestrator/sandboxes.ts";
import { fakeSprites } from "./support/auto-fake-sprites.ts";
import { test } from "node:test";
@@ -453,34 +452,6 @@ test("owner execute masks selected credentials and inherited proxy credentials",
);
});
-test("scoped command wrappers preserve selected AWS credentials and clear unselected ambient keys", async () => {
- const boxes = createTurnSandboxes({
- deps: {},
- input: {},
- connectorEnv: {},
- credentialCutoverServices: ["role-service"],
- resolution: { layers: [] },
- } as unknown as TurnSandboxContext);
- const { ctx } = routingCtx({
- scopedCommand: boxes.scopedCommand,
- commandCredentials: [{ handle: "selected-aws", env: [{ key: "AWS_ACCESS_KEY_ID", value: "selected-key" }] }],
- sandbox: {
- async run(handle: SandboxHandle, command: string) {
- const stdout = execFileSync("/bin/sh", ["-c", command], {
- env: { AWS_SECRET_ACCESS_KEY: "stale", ...handle.env },
- encoding: "utf8",
- });
- return { stdout, stderr: "", code: 0, timedOut: false };
- },
- } as unknown as Sandbox,
- });
- const result = await ctx.execute(
- 'test "$AWS_ACCESS_KEY_ID" = selected-key && test "${AWS_SECRET_ACCESS_KEY-unset}" = unset && printf passed',
- { credentials: ["selected-aws"] },
- );
- assert.equal(result.stdout, "passed");
-});
-
test("scratch credentials are selected per command and masked before returning", async () => {
const environments: Array<Record<string, string> | undefined> = [];
const { ctx } = routingCtx({
@@ -522,7 +493,6 @@ function turnBoxes(sandbox: Partial<Sandbox>, transferId = "turn-a") {
scopeId: scopeId("channel", "C1"),
memoryScopeId: scopeId("channel", "C1"),
connectorEnv: { AGENT_API_TOKEN: "scope-capability" },
- credentialCutoverServices: [],
resolution: {
layers: [
{ scopeId: scopeId("channel", "C1"), mode: "rw", mountPath: "" },
diff --git a/test/keychain-ask.test.ts b/test/keychain-ask.test.ts
index ec9d0d024..bb8600bf4 100644
--- a/test/keychain-ask.test.ts
+++ b/test/keychain-ask.test.ts
@@ -35,6 +35,7 @@ import { scopeId, type TurnRequest, type TurnResult } from "../src/types.ts";
import { fakeSprites } from "./support/auto-fake-sprites.ts";
import { testConfig } from "./support/test-config.ts";
import { selectDefaultSandbox } from "./support/default-sandbox.ts";
+import { materializeGrant } from "./support/materialize-grant.ts";
const KEY = deriveConnectorKey("keychain-ask-test-key");
const SECRET = "keychain-ask-route-secret".repeat(3);
@@ -222,13 +223,13 @@ test("approveAsk: same createGrant owner gate, audience from the record, single
"re-approve is a replay",
);
- await assert.rejects(k.materialize(grant.id, "channel:OTHER", "U_BOB"), (e: KeychainError) => e.status === 403);
- const m = await k.materialize(grant.id, "channel:C1", "U_BOB");
+ await assert.rejects(materializeGrant(k, grant.id, "channel:OTHER", "U_BOB"), (e: KeychainError) => e.status === 403);
+ const m = await materializeGrant(k, grant.id, "channel:C1", "U_BOB");
assert.ok(m.kind === "env" && m.env[0]!.value === "ghp_alice");
const used = (await k.getGrant(grant.id))!;
assert.equal(used.status, "used");
assert.equal(used.usedAt, 1_000_000);
- await assert.rejects(k.materialize(grant.id, "channel:C1", "U_BOB"), (e: KeychainError) => e.status === 410);
+ await assert.rejects(materializeGrant(k, grant.id, "channel:C1", "U_BOB"), (e: KeychainError) => e.status === 410);
});
test("declineAsk is owner-gated and single-resolution", async () => {
@@ -872,15 +873,17 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
await built.fireAskResolution!({ ...view.ask } as KeychainAsk, grant);
assert.equal((await resumeTurns(session!.id, /The credential owner approved access/)).length, 1);
- assert.equal(
- (await post("/v1/keychain/use", { grant: grant.id }, await capFor("U_EVE", "channel:OTHER"))).status,
- 403,
+ await assert.rejects(
+ built.keychain!.prepareMaterialize(grant.id, "channel:OTHER", "U_EVE"),
+ (e: KeychainError) => e.status === 403,
+ );
+ const prepared = await built.keychain!.prepareMaterialize(grant.id, "channel:C_INFRA", "U_BOB");
+ await prepared.commit();
+ await assert.rejects(
+ built.keychain!.prepareMaterialize(grant.id, "channel:C_INFRA", "U_BOB"),
+ (e: KeychainError) => e.status === 410,
+ "a child or retry needs a new approval after consumption",
);
- const used = await post("/v1/keychain/use", { grant: grant.id }, await bobInInfra());
- assert.equal(used.status, 200);
- assert.equal(await used.text(), "export GITHUB_TOKEN='ghp_alice'\n");
- const rerun = await post("/v1/keychain/use", { grant: grant.id }, await bobInInfra());
- assert.equal(rerun.status, 410, "a child or retry needs a new approval after consumption");
});
it("deny on the card flips the ask and fires exactly one resolution turn into the asking channel", async () => {
@@ -898,7 +901,6 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
const resolution = await waitFor(() => resumeTurns(session!.id, /declined the access request/));
assert.equal(resolution.length, 1);
assert.equal((await decide(made.ask.id, "U_ALICE", "once")).ask.status, "declined", "a decided card stays decided");
- assert.equal((await post("/v1/keychain/use", { credential: npm.id }, await bobInInfra())).status, 403);
});
it("the removed chat-approval routes are gone; background requests may still ask", async () => {
@@ -981,7 +983,6 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
assert.equal(seed.status, "ok");
const session = await built.sessions.getByThread(threadRef);
assert.ok(session);
- assert.equal((await post("/v1/keychain/use", { credential: credential.id }, token)).status, 403);
const made = await post(
"/v1/keychain/asks",
{ credential: credential.id, purpose: "run the synthetic shared check" },
@@ -1007,9 +1008,6 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
assert.equal(grant.audienceScopeId, scope);
const resumed = await waitFor(() => resumeTurns(session.id, /The credential owner approved access/));
assert.equal(resumed.length, 1);
- assert.equal((await post("/v1/keychain/use", { grant: grant.id }, await capFor(requesterId))).status, 403);
- assert.equal((await post("/v1/keychain/use", { grant: grant.id }, token)).status, 200);
- assert.equal((await post("/v1/keychain/use", { grant: grant.id }, token)).status, 410);
});
}
}
@@ -1091,9 +1089,6 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
assert.equal(seed.status, "ok");
const session = await built.sessions.getByThread(threadRef);
assert.ok(session);
- const denied = await post("/v1/keychain/use", { credential: credential.id }, token);
- assert.equal(denied.status, 403);
- assert.match(((await denied.json()) as any).message, /keychain\/asks/);
const requested = await post(
"/v1/keychain/asks",
{ credential: credential.id, purpose: "read-only dummy scheduled check", requestedMode: mode },
@@ -1127,19 +1122,19 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
assert.equal(approved.grant.audienceScopeId, personal);
const resumed = await waitFor(() => resumeTurns(session.id, /The credential owner approved access/));
assert.equal(resumed.length, 1);
- assert.equal((await post("/v1/keychain/use", { grant: approved.grant.id }, await capFor("U_BOB"))).status, 403);
- assert.equal((await post("/v1/keychain/use", { grant: approved.grant.id }, token)).status, 200);
- assert.equal(
- (await post("/v1/keychain/use", { grant: approved.grant.id }, token)).status,
- mode === "once" ? 410 : 200,
- );
+ const prepared = await built.keychain!.prepareMaterialize(approved.grant.id, personal, "U_ALICE");
+ await prepared.commit();
+ if (mode === "once")
+ await assert.rejects(
+ built.keychain!.prepareMaterialize(approved.grant.id, personal, "U_ALICE"),
+ (e: KeychainError) => e.status === 410,
+ );
const after = (await built.keychain!.getGrant(approved.grant.id))!;
assert.equal(after.status, mode === "once" ? "used" : "active");
if (mode === "once") assert.ok(after.usedAt !== undefined);
assert.equal(after.expiresAt, approved.grant.expiresAt);
if (mode === "standing") {
assert.equal((await post(`/v1/keychain/grants/${approved.grant.id}/revoke`, {}, live)).status, 200);
- assert.equal((await post("/v1/keychain/use", { grant: approved.grant.id }, token)).status, 410);
}
});
}
@@ -1160,7 +1155,6 @@ describe("/v1/keychain/asks — card approval end to end", async () => {
await assert.rejects(decide(ask.id, "U_BOB", "deny"), (e: KeychainError) => e.status === 403);
assert.equal((await decide(ask.id, "U_ALICE", "deny")).ask.status, "declined");
assert.equal((await built.keychain!.getAsk(ask.id))?.status, "declined");
- assert.equal((await post("/v1/keychain/use", { credential: credential.id }, token)).status, 403);
assert.equal(
(
await post(
diff --git a/test/keychain.test.ts b/test/keychain.test.ts
index af744371c..4af3f8ce6 100644
--- a/test/keychain.test.ts
+++ b/test/keychain.test.ts
@@ -5,7 +5,6 @@ import assert from "node:assert/strict";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { execFileSync } from "node:child_process";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
import { buildApp, type BuiltApp } from "../src/wiring.ts";
@@ -14,7 +13,7 @@ import {
createKeychain,
credentialHandle,
renderKeychainManifest,
- renderUseScript,
+ fileCredentialEnvironment,
KeychainError,
type Keychain,
type KeychainGrant,
@@ -27,6 +26,7 @@ import { scopeId, type TurnRequest } from "../src/types.ts";
import { fakeSprites } from "./support/auto-fake-sprites.ts";
import { testConfig } from "./support/test-config.ts";
import { selectDefaultSandbox } from "./support/default-sandbox.ts";
+import { materializeGrant } from "./support/materialize-grant.ts";
const KEY = deriveConnectorKey("keychain-test-key");
@@ -168,12 +168,12 @@ test("only the owner can grant; materialize is scope-checked; once-grants are co
});
await assert.rejects(
- k.materialize(grant.id, "channel:OTHER", "U2"),
+ materializeGrant(k, grant.id, "channel:OTHER", "U2"),
(e: KeychainError) => e.status === 403,
"grant is audience-bound",
);
- const m = await k.materialize(grant.id, "channel:C1", "U2");
+ const m = await materializeGrant(k, grant.id, "channel:C1", "U2");
assert.equal(m.kind, "env");
assert.ok(
m.kind === "env" && m.env.length === 1 && m.env[0]!.value === "ghp_secret" && m.env[0]!.key === "GITHUB_TOKEN",
@@ -181,7 +181,7 @@ test("only the owner can grant; materialize is scope-checked; once-grants are co
assert.equal(m.purpose, "use my gh to clone the repo");
await assert.rejects(
- k.materialize(grant.id, "channel:C1", "U2"),
+ materializeGrant(k, grant.id, "channel:C1", "U2"),
(e: KeychainError) => e.status === 410,
"a once-grant is single-use",
);
@@ -198,9 +198,9 @@ test("concurrent materialization consumes a once grant exactly once", async () =
purpose: "single use",
});
const results = await Promise.allSettled([
- k.materialize(grant.id, "channel:C1", "U2"),
- k.materialize(grant.id, "channel:C1", "U2"),
- k.materialize(grant.id, "channel:C1", "U3"),
+ materializeGrant(k, grant.id, "channel:C1", "U2"),
+ materializeGrant(k, grant.id, "channel:C1", "U2"),
+ materializeGrant(k, grant.id, "channel:C1", "U3"),
]);
assert.equal(results.filter((result) => result.status === "fulfilled").length, 1);
assert.equal(
@@ -227,7 +227,7 @@ test("legacy retry grants remain consumed and invalidate prepared uses", async (
await grants.put(grant.id, { ...grant, status: "active", usedAt: Date.now(), usedBy: "U2" });
assert.equal((await k.grantsForScope("channel:C1")).length, 0);
for (const actor of ["U2", "U3"])
- await assert.rejects(k.materialize(grant.id, "channel:C1", actor), (e: KeychainError) => e.status === 410);
+ await assert.rejects(materializeGrant(k, grant.id, "channel:C1", actor), (e: KeychainError) => e.status === 410);
await assert.rejects(prepared.commit(), (e: KeychainError) => e.status === 410);
});
@@ -291,7 +291,7 @@ test("a grant row pointing at a broker credential cannot materialize (defense in
createdAt: Date.now(),
});
await assert.rejects(
- k.materialize("g-broker", scopeId("channel", "C1"), "U2"),
+ materializeGrant(k, "g-broker", scopeId("channel", "C1"), "U2"),
(e: KeychainError) => e.status === 403,
);
await assert.rejects(
@@ -351,14 +351,13 @@ describe("connectors are grantable like any keychain record", () => {
mode: "once",
purpose: "check my signature emails",
});
- const m = await k.materialize(grant.id, G1, "carol@x");
+ const m = await materializeGrant(k, grant.id, G1, "carol@x");
assert.equal(m.kind, "env");
assert.equal(envKey(GMAIL), "VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM");
assert.deepEqual(m.kind === "env" ? m.env : null, [
{ key: "VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM", value: "ya29.alex" },
]);
- assert.match(renderUseScript(m), /export VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM=/);
- await assert.rejects(k.materialize(grant.id, G1, "carol@x"), (e: KeychainError) => e.status === 410);
+ await assert.rejects(materializeGrant(k, grant.id, G1, "carol@x"), (e: KeychainError) => e.status === 410);
});
it("an expired connector token is refreshed on materialize; with no refresh it 410s for reconnect", async () => {
@@ -373,7 +372,7 @@ describe("connectors are grantable like any keychain record", () => {
mode: "standing",
purpose: "p",
});
- const m = await k.materialize(grant.id, G1, "carol@x");
+ const m = await materializeGrant(k, grant.id, G1, "carol@x");
assert.deepEqual(m.kind === "env" ? m.env : null, [
{ key: "VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM", value: "ya29.fresh" },
]);
@@ -388,7 +387,7 @@ describe("connectors are grantable like any keychain record", () => {
mode: "once",
purpose: "p",
});
- await assert.rejects(k2.materialize(g2.id, G1, "carol@x"), (e: KeychainError) => e.status === 410);
+ await assert.rejects(materializeGrant(k2, g2.id, G1, "carol@x"), (e: KeychainError) => e.status === 410);
});
it("a still-valid token within the refresh margin is refreshed; one with ample life is reused", async () => {
@@ -405,7 +404,7 @@ describe("connectors are grantable like any keychain record", () => {
mode: "standing",
purpose: "p",
});
- const m = await k.materialize(g.id, G1, "[email protected]");
+ const m = await materializeGrant(k, g.id, G1, "[email protected]");
assert.deepEqual(m.kind === "env" ? m.env : null, [
{ key: "VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM", value: "ya29.fresh" },
]);
@@ -423,7 +422,7 @@ describe("connectors are grantable like any keychain record", () => {
mode: "standing",
purpose: "p",
});
- const m2 = await k2.materialize(g2.id, G1, "[email protected]");
+ const m2 = await materializeGrant(k2, g2.id, G1, "[email protected]");
assert.deepEqual(m2.kind === "env" ? m2.env : null, [
{ key: "VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM", value: "ya29.plenty" },
]);
@@ -490,13 +489,13 @@ test("standing grants are reusable, revocable, and die with the credential", asy
assert.equal((await k.materializeStanding("channel:C1")).length, 1);
assert.equal((await k.materializeStanding("channel:C2")).length, 0);
- await k.materialize(grant.id, "channel:C1", "U2");
- await k.materialize(grant.id, "channel:C1", "U3");
+ await materializeGrant(k, grant.id, "channel:C1", "U2");
+ await materializeGrant(k, grant.id, "channel:C1", "U3");
assert.equal(await k.revokeGrant("U2", grant.id), false, "only the owner can revoke");
assert.equal(await k.revokeGrant("U1", grant.id), true);
assert.equal((await k.materializeStanding("channel:C1")).length, 0);
- await assert.rejects(k.materialize(grant.id, "channel:C1", "U2"), (e: KeychainError) => e.status === 410);
+ await assert.rejects(materializeGrant(k, grant.id, "channel:C1", "U2"), (e: KeychainError) => e.status === 410);
const grant2 = await k.createGrant({
credentialId: cred.id,
@@ -587,31 +586,6 @@ test("file bundles: one item per service, materialize to a /tmp script with env
files: [{ path: "$(echo injected)", contentBase64: b64("x") }],
});
assert.deepEqual(unusual.targets, ["$(echo injected)"]);
- const unusualScript = renderUseScript({
- kind: "file",
- credentialId: "legacy-unusual",
- ownerId: "U1",
- service: "unusual",
- files: [{ path: "$(echo injected)", contentBase64: b64("x") }],
- });
- const renderedPath = execFileSync("sh", ["-c", `${unusualScript}\nfind "$HOME" -maxdepth 1 -type f -print`], {
- encoding: "utf8",
- });
- assert.match(renderedPath, /\/\$\(echo injected\)$/m);
- const pointerScript = renderUseScript({
- kind: "file",
- credentialId: "legacy-pointer",
- ownerId: "U1",
- service: "unusual-pointer",
- files: [{ path: "$(echo injected)/.aws/config", contentBase64: b64("x") }],
- });
- const pointerPath = execFileSync(
- "sh",
- ["-c", `${pointerScript}\nprintf '%s\\n' "$AWS_CONFIG_FILE"\nfind "$__kc_dir" -type f -print`],
- { encoding: "utf8" },
- );
- assert.match(pointerPath, /\/\$\(echo injected\)\/\.aws\/config$/m);
-
const grant = await k.createGrant({
credentialId: cred.id,
ownerId: "U1",
@@ -619,110 +593,38 @@ test("file bundles: one item per service, materialize to a /tmp script with env
mode: "once",
purpose: "use my aws for the deploy",
});
- const m = await k.materialize(grant.id, "channel:C1", "U2");
+ const m = await materializeGrant(k, grant.id, "channel:C1", "U2");
assert.equal(m.kind, "file");
- const script = renderUseScript(m);
- assert.match(script, /mktemp -d/);
- assert.match(script, /chmod 600/);
- assert.match(script, /export AWS_SHARED_CREDENTIALS_FILE="\$__kc_dir\/.aws\/credentials"/);
- assert.ok(
- !script.includes("/root/") && !script.includes("$HOME"),
- "files land on ephemeral /tmp, not the durable home",
- );
});
-test("file bundle materialization exports the env pointers CLIs honor from ephemeral /tmp paths", () => {
- const b64 = (s: string) => Buffer.from(s).toString("base64");
- const script = renderUseScript({
- kind: "file",
- credentialId: "cred-cli",
- ownerId: "U1",
- service: "cli-bundle",
- files: [
- { path: ".aws/config", contentBase64: b64("[default]\nregion=us-west-2\n") },
- { path: ".aws/credentials", contentBase64: b64("[default]\naws_access_key_id=AKIA\n") },
- { path: ".config/gh/hosts.yml", contentBase64: b64("github.com:\n oauth_token: gho_x\n") },
- { path: ".config/glab-cli/config.yml", contentBase64: b64("hosts:\n gitlab.com:\n token: glpat_x\n") },
- { path: ".kube/config", contentBase64: b64("apiVersion: v1\n") },
- { path: ".docker/config.json", contentBase64: b64("{}\n") },
- { path: ".npmrc", contentBase64: b64("//registry.npmjs.org/:_authToken=npm_x\n") },
- { path: ".netrc", contentBase64: b64("machine example.com login u password p\n") },
- { path: ".ssh/id_ed25519", contentBase64: b64("PRIVATE KEY\n") },
- ],
- });
-
- assert.match(script, /\$\{TMPDIR:-\/tmp\}\/keychain\.XXXXXX/, "materialized files are rooted in ephemeral /tmp");
- assert.ok(!script.includes("/root/") && !script.includes("$HOME"), "no materialized file targets the durable home");
- assert.match(script, /export AWS_CONFIG_FILE="\$__kc_dir\/.aws\/config"/);
- assert.match(script, /export AWS_SHARED_CREDENTIALS_FILE="\$__kc_dir\/.aws\/credentials"/);
- assert.match(script, /export GH_CONFIG_DIR="\$__kc_dir\/.config\/gh"/);
- assert.match(script, /export GLAB_CONFIG_DIR="\$__kc_dir\/.config\/glab-cli"/);
- assert.match(script, /export KUBECONFIG="\$__kc_dir\/.kube\/config"/);
- assert.match(script, /export DOCKER_CONFIG="\$__kc_dir\/.docker"/);
- assert.match(script, /export NPM_CONFIG_USERCONFIG="\$__kc_dir\/.npmrc"/);
- assert.match(script, /export NETRC="\$__kc_dir\/.netrc"/);
- assert.match(script, /export GIT_SSH_COMMAND="ssh -i \$__kc_dir\/.ssh\/id_ed25519 -o IdentitiesOnly=yes"/);
-});
-
-test("glab file bundles use GLAB_CONFIG_DIR for current and legacy config locations", () => {
- const b64 = Buffer.from("token: glpat_x\n").toString("base64");
- const cases: Array<[string, string]> = [
- [".config/glab-cli/config.yml", ".config/glab-cli"],
- [".config/glab/config.yml", ".config/glab"],
- ["Library/Application Support/glab-cli/config.yml", "Library/Application Support/glab-cli"],
- ];
- for (const [path, dir] of cases) {
- const script = renderUseScript({
- kind: "file",
- credentialId: `cred-${path}`,
- ownerId: "U1",
- service: "glab",
- files: [{ path, contentBase64: b64 }],
- });
- assert.match(
- script,
- new RegExp(`export GLAB_CONFIG_DIR="\\$__kc_dir/${dir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}"`),
- );
- }
-});
-
-test("a non-redirectable cred (AWS SSO token cache) overlays HOME; redirectable bundles leave it untouched", () => {
- const b64 = (s: string) => Buffer.from(s).toString("base64");
- const sso = renderUseScript({
- kind: "file",
- credentialId: "cred-sso",
- ownerId: "U1",
- service: "aws",
- files: [
- { path: ".aws/config", contentBase64: b64("[default]\nsso_session=acme\n") },
- { path: ".aws/sso/cache/abc123.json", contentBase64: b64('{"accessToken":"sso_x"}') },
- ],
- });
- assert.match(sso, /export HOME="\$__kc_dir"/, "HOME points at the bundle so ~/.aws/sso/cache resolves");
- assert.match(
- sso,
- /ln -s "\$__e" "\$__kc_dir\/\$__b"/,
- "the box's own home is mirrored in, so unrelated ~ lookups still resolve",
- );
- assert.match(
- sso,
- /export AWS_CONFIG_FILE="\$__kc_dir\/.aws\/config"/,
- "env pointers are still emitted alongside the HOME overlay",
- );
- const writes = [...sso.matchAll(/base64 -d > "([^"]+)"/g)].map((x) => x[1]!);
- assert.ok(
- writes.length > 0 && writes.every((w) => w.startsWith("$__kc_dir/")),
- "every materialized file lands under $__kc_dir, not durable home",
+test("file credentials point CLIs at their staged home", () => {
+ const b64 = (value: string) => Buffer.from(value).toString("base64");
+ const env = fileCredentialEnvironment(
+ [
+ ".aws/config",
+ ".aws/credentials",
+ ".config/gh/hosts.yml",
+ ".config/glab/config.yml",
+ ".kube/config",
+ ".docker/config.json",
+ ".npmrc",
+ ".netrc",
+ ".ssh/id_ed25519",
+ ].map((path) => ({ path, contentBase64: b64("x") })),
+ "/tmp/staged",
);
-
- const gh = renderUseScript({
- kind: "file",
- credentialId: "cred-gh",
- ownerId: "U1",
- service: "gh",
- files: [{ path: ".config/gh/hosts.yml", contentBase64: b64("github.com:\n oauth_token: gho_x\n") }],
+ assert.deepEqual(env, {
+ HOME: "/tmp/staged",
+ AWS_CONFIG_FILE: "/tmp/staged/.aws/config",
+ AWS_SHARED_CREDENTIALS_FILE: "/tmp/staged/.aws/credentials",
+ GH_CONFIG_DIR: "/tmp/staged/.config/gh",
+ GLAB_CONFIG_DIR: "/tmp/staged/.config/glab",
+ KUBECONFIG: "/tmp/staged/.kube/config",
+ DOCKER_CONFIG: "/tmp/staged/.docker",
+ NPM_CONFIG_USERCONFIG: "/tmp/staged/.npmrc",
+ NETRC: "/tmp/staged/.netrc",
+ GIT_SSH_COMMAND: "ssh -i '/tmp/staged/.ssh/id_ed25519' -o IdentitiesOnly=yes",
});
- assert.ok(!gh.includes("export HOME=") && !gh.includes("$HOME"), "redirectable creds leave HOME untouched");
});
test("standing file grants are not auto-injected, but remain re-fetchable by use", async () => {
@@ -748,11 +650,10 @@ test("standing file grants are not auto-injected, but remain re-fetchable by use
[],
"standing file grants are not injected into provision env",
);
- const first = await k.materialize(grant.id, "channel:C1", "U2");
- const second = await k.materialize(grant.id, "channel:C1", "U2");
+ const first = await materializeGrant(k, grant.id, "channel:C1", "U2");
+ const second = await materializeGrant(k, grant.id, "channel:C1", "U2");
assert.equal(first.kind, "file");
- assert.equal(second.kind, "file", "standing file grants stay reusable for explicit /use re-fetches");
- assert.match(renderUseScript(first), /export GLAB_CONFIG_DIR="\$__kc_dir\/.config\/glab-cli"/);
+ assert.equal(second.kind, "file", "standing file grants stay reusable for explicit re-fetches");
});
test("materializeOwn round-trips the owner's env creds; expired creds are skipped", async () => {
@@ -801,7 +702,7 @@ test("single-grant materialize surfaces a 422 (not a raw crypto 500) when the ro
purpose: "use it",
});
await assert.rejects(
- reader.materialize(grant.id, "channel:C1", "U2"),
+ materializeGrant(reader, grant.id, "channel:C1", "U2"),
(e: KeychainError) => e.status === 422,
"an undecryptable row yields a clean 422, not a crash",
);
@@ -832,8 +733,6 @@ test("multi-input login: each field becomes its own env var, values never in met
{ key: "DOORDASH_EMAIL", value: "[email protected]", secret: false },
{ key: "DOORDASH_PASSWORD", value: "hunter2", secret: true },
]);
- const script = renderUseScript({ kind: "env", ...own! });
- assert.match(script, /export DOORDASH_EMAIL='[email protected]'\nexport DOORDASH_PASSWORD='hunter2'/);
});
test("multi-input login: re-save upserts the same slot, and unique/non-empty fields are enforced", async () => {
@@ -998,7 +897,7 @@ test("manifest: explains itself in a bare channel, lists credentials + protocol
assert.ok(!block.includes("ghp_secret"), "the manifest is metadata-only");
});
-test("manifest: in the owner's personal scope their own credentials need no grant and load by id", async () => {
+test("manifest: own credentials need no grant but require explicit execution requests", async () => {
const k = kc();
await k.save(GH);
await k.save({ ownerId: "U1", service: "npm", secret: "npm_x", envKey: "NPM_TOKEN", expiresAt: Date.now() - 1 });
@@ -1016,11 +915,10 @@ test("manifest: in the owner's personal scope their own credentials need no gran
scopeGrants: [],
injected: [],
});
- assert.match(own, /file-login.*raw file loading needs no grant on their live turn; background turns need a grant/);
+ assert.match(own, /file-login.*execute.credentials needs no 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.match(own, /Request every credential, including saved CLI logins, explicitly using execute.credentials/);
assert.ok(!own.includes("no grant for this conversation"));
assert.match(
own,
@@ -1185,9 +1083,10 @@ describe("/v1/keychain routes (capability-authed)", () => {
const cid = (await built.keychain!.listConnectorsByOwners(["alex@conn"])).get("alex@conn")![0]!.credentialId;
const grant = await grantHere(cid, "alex@conn", GROUP, "once", "check my signature emails");
- const used = await post("/v1/keychain/use", { grant: grant.id }, await capFor("carol@conn", GROUP));
- assert.equal(used.status, 200);
- assert.equal(await used.text(), "export VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM='ya29.alex'\n");
+ assert.equal((await built.keychain!.getGrant(grant.id))?.status, "active");
+ const prepared = await built.keychain!.prepareMaterialize(grant.id, GROUP, "carol@conn");
+ assert.equal(prepared.materialized.kind, "env");
+ if (prepared.materialized.kind === "env") assert.equal(prepared.materialized.env[0]?.value, "ya29.alex");
});
it("normalizes keychain credential expiry seconds to milliseconds before storing", async () => {
@@ -1202,8 +1101,7 @@ describe("/v1/keychain routes (capability-authed)", () => {
assert.equal(credential.expiresAt, expiresAt * 1000);
const grant = await grantHere(credential.id, "U_SECONDS", "channel:C_SECONDS", "once", "seconds check");
- const used = await post("/v1/keychain/use", { grant: grant.id }, await capFor("U_SECONDS", "channel:C_SECONDS"));
- assert.equal(used.status, 200, "seconds input must not make the credential look expired immediately");
+ await built.keychain!.prepareMaterialize(grant.id, "channel:C_SECONDS", "U_SECONDS");
});
it("rejects invalid keychain credential expiry at the route boundary", async () => {
@@ -1247,33 +1145,23 @@ describe("/v1/keychain routes (capability-authed)", () => {
assert.notEqual(res.status, 200);
});
- it("use is scope-bound, returns sourceable env text, and a once grant cannot be reused", async () => {
- const { credential } = (await (
- await post(
- "/v1/keychain/credentials",
- { service: "github", secret: "ghp_grant", envKey: "GH_GRANT" },
- await capFor("OWNER"),
- )
- ).json()) as any;
-
- const g = { grant: await grantHere(credential.id, "OWNER", "channel:C7", "once", "clone the repo") };
-
- const elsewhere = await post("/v1/keychain/use", { grant: g.grant.id }, await capFor("U3", "channel:OTHER"));
- assert.equal(elsewhere.status, 403);
-
- const used = await post("/v1/keychain/use", { grant: g.grant.id }, await capFor("U3", "channel:C7"));
- assert.equal(used.status, 200);
- assert.match(used.headers.get("content-type") ?? "", /text\/plain/);
- assert.equal(await used.text(), "export GH_GRANT='ghp_grant'\n");
-
- const rerun = await post("/v1/keychain/use", { grant: g.grant.id }, await capFor("U3", "channel:C7"));
- assert.equal(rerun.status, 410, "a consumed grant cannot authorize another credential load");
-
- const usage = await built.credentialUsage.list({});
- assert.ok(
- usage.some(
- (u) => u.slug === `keychain:github:${credential.id}` && u.principalId === "U3" && u.scopeLabel === "channel:C7",
- ),
+ it("one-time grants materialize only in their scope, once", async () => {
+ const credential = await built.keychain!.save({
+ ownerId: "OWNER",
+ service: "github",
+ secret: "ghp_grant",
+ envKey: "GH_GRANT",
+ });
+ const grant = await grantHere(credential.id, "OWNER", "channel:C7", "once", "clone the repo");
+ await assert.rejects(
+ built.keychain!.prepareMaterialize(grant.id, "channel:OTHER", "U3"),
+ (e: KeychainError) => e.status === 403,
+ );
+ const prepared = await built.keychain!.prepareMaterialize(grant.id, "channel:C7", "U3");
+ await prepared.commit();
+ await assert.rejects(
+ built.keychain!.prepareMaterialize(grant.id, "channel:C7", "U3"),
+ (e: KeychainError) => e.status === 410,
);
});
@@ -1286,8 +1174,7 @@ describe("/v1/keychain routes (capability-authed)", () => {
await capFor(owner),
)
).json()) as any;
- const grant = await grantHere(credential.id, owner, "channel:C_OVERVIEW", "standing", "deploy the reporting app");
- await post("/v1/keychain/use", { grant: grant.id }, await capFor(owner, "channel:C_OVERVIEW"));
+ await grantHere(credential.id, owner, "channel:C_OVERVIEW", "standing", "deploy the reporting app");
const usage = t.mock.method(built.credentialUsage, "list", async () => {
throw new Error("usage history is unavailable");
@@ -1340,121 +1227,6 @@ describe("/v1/keychain routes (capability-authed)", () => {
assert.equal(body.grants.find((entry: any) => entry.id === grant!.id)?.status, "active");
assert.ok(!JSON.stringify(body).includes("never-return-this-connector"));
});
-
- it("standing file grants are not auto-injected, but /v1/keychain/use re-fetches them into /tmp with glab env pointers", async () => {
- const content = "hosts:\n gitlab.com:\n token: glpat_route\n";
- const { credential } = (await (
- await post(
- "/v1/keychain/credentials",
- {
- service: "glab",
- files: [{ path: ".config/glab-cli/config.yml", contentBase64: Buffer.from(content).toString("base64") }],
- },
- await capFor("OWNER"),
- )
- ).json()) as any;
-
- const g = { grant: await grantHere(credential.id, "OWNER", "channel:C8", "standing", "use my glab here") };
- assert.deepEqual(
- await built.keychain!.materializeStanding("channel:C8"),
- [],
- "standing file grants do not ride provision env injection",
- );
-
- for (let i = 0; i < 2; i++) {
- const used = await post("/v1/keychain/use", { grant: g.grant.id }, await capFor("U3", "channel:C8"));
- assert.equal(used.status, 200);
- const script = await used.text();
- assert.match(script, /\$\{TMPDIR:-\/tmp\}\/keychain\.XXXXXX/);
- assert.match(script, /export GLAB_CONFIG_DIR="\$__kc_dir\/.config\/glab-cli"/);
- assert.ok(
- !script.includes("/root/") && !script.includes("$HOME"),
- "file grant materialization must stay off durable home",
- );
- assert.ok(!script.includes("glpat_route"), "raw file contents stay base64-encoded in the sourceable script");
- }
- });
-
- const liveOwn = async (actorId: string) => await capFor(actorId, scopeId("personal", actorId), { liveActor: true });
-
- it("use by credential id: the caller's own, in their personal conversation only — no grant needed", async () => {
- const { credential } = (await (
- await post(
- "/v1/keychain/credentials",
- { service: "npm", secret: "npm_own", envKey: "NPM_OWN" },
- await capFor("U_OWN"),
- )
- ).json()) as any;
-
- const used = await post("/v1/keychain/use", { credential: credential.id }, await liveOwn("U_OWN"));
- assert.equal(used.status, 200);
- assert.equal(await used.text(), "export NPM_OWN='npm_own'\n");
- assert.equal(
- (await post("/v1/keychain/use", { credential: credential.id }, await liveOwn("U_OWN"))).status,
- 200,
- "nothing is consumed",
- );
-
- assert.equal(
- (
- await post(
- "/v1/keychain/use",
- { credential: credential.id },
- await capFor("U_OWN", "channel:C9", { liveActor: true }),
- )
- ).status,
- 403,
- "own-use never crosses into a shared scope, even on a live turn",
- );
- assert.equal(
- (await post("/v1/keychain/use", { credential: credential.id }, await capFor("U_OWN"))).status,
- 403,
- "a non-live personal turn (unprompted/detection — no liveActor) cannot pull own credentials by id",
- );
- assert.equal(
- (
- await post(
- "/v1/keychain/use",
- { credential: credential.id },
- await capFor("U_OWN", "personal:U_OWN", { triggered: true }),
- )
- ).status,
- 403,
- "a trigger-fired turn runs as the owner without them speaking — refused",
- );
- assert.equal(
- (await post("/v1/keychain/use", { credential: credential.id }, await liveOwn("U_ELSE"))).status,
- 404,
- "someone else's credential id reads as unknown",
- );
-
- const usage = await built.credentialUsage.list({});
- assert.ok(
- usage.some(
- (u) =>
- u.slug === `keychain:npm:${credential.id}` && u.principalId === "U_OWN" && u.scopeLabel === "personal:U_OWN",
- ),
- );
- });
-
- it("use by credential id loads the caller's own file bundle into /tmp with env pointers", async () => {
- const content = "hosts:\n gitlab.com:\n token: glpat_own\n";
- const { credential } = (await (
- await post(
- "/v1/keychain/credentials",
- {
- service: "glab",
- files: [{ path: ".config/glab-cli/config.yml", contentBase64: Buffer.from(content).toString("base64") }],
- },
- await capFor("U_OWN_FILE"),
- )
- ).json()) as any;
- const used = await post("/v1/keychain/use", { credential: credential.id }, await liveOwn("U_OWN_FILE"));
- assert.equal(used.status, 200);
- const script = await used.text();
- assert.match(script, /export GLAB_CONFIG_DIR="\$__kc_dir\/.config\/glab-cli"/);
- assert.ok(!script.includes("glpat_own"), "raw file contents stay base64-encoded in the sourceable script");
- });
});
function channelTurn(
@@ -1637,7 +1409,7 @@ test("Composio keys remain backend-only across all materialization paths includi
assert.deepEqual(await k.materializeOwn("U1"), []);
assert.deepEqual(await k.materializeStanding("channel:C1"), []);
await assert.rejects(k.materializeOwnById("U1", c.id, "personal:U1"), /keys stay in the backend/);
- await assert.rejects(k.materialize(grant.id, "channel:C1", "U1"), /keys stay in the backend/);
+ await assert.rejects(materializeGrant(k, grant.id, "channel:C1", "U1"), /keys stay in the backend/);
assert.equal(await k.composioKey("U1", c.id), "project-key");
assert.equal(await k.composioKey("U2", c.id), null);
}
diff --git a/test/open-speaker-keychain.test.ts b/test/open-speaker-keychain.test.ts
index 3e074bf32..d106291fa 100644
--- a/test/open-speaker-keychain.test.ts
+++ b/test/open-speaker-keychain.test.ts
@@ -43,9 +43,7 @@ test("Open owner computer rechecks revocation before reuse and is destroyed, nev
openSpeakerKeychain: true,
ownerAuthAvailable: true,
connectorEnv: {},
- credentialTools: [],
credentialServices: [],
- credentialCutoverServices: [],
quarantinedServices: [],
} as unknown as TurnSandboxContext;
const boxes = createTurnSandboxes(context);
diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.ts
index 75e83258a..898f27961 100644
--- a/test/orchestrator.test.ts
+++ b/test/orchestrator.test.ts
@@ -4305,24 +4305,6 @@ test("default screening does not invoke a model for inbound data or tool results
assert.equal(built.modelGateway.audit().filter((rec) => rec.model === "mock-security").length, 0);
});
-test("ordinary turns neither probe native logins nor advertise cached login state", async () => {
- const built = freshApp();
- const checkedAt = 1;
- await built.livenessCache.put({ scopeId: scopeId("personal", "U1"), checkedAt, connectors: { gh: "active" } });
- const commands: string[] = [];
- const run = built.sandbox.run.bind(built.sandbox);
- built.sandbox.run = async (handle, command, opts) => {
- commands.push(command);
- return run(handle, command, opts);
- };
- const prompt = await built.app.turn(dm("!sysprompt"));
- assert.doesNotMatch(prompt.reply ?? "", /## Your logins|GitHub — ✓ signed in/);
- await built.app.turn(dm("!run printf ready"));
- assert.ok(commands.some((c) => c.includes("printf ready")));
- assert.ok(commands.every((c) => !c.includes("gh auth status") && !c.includes("gcloud auth print-access-token")));
- assert.equal((await built.livenessCache.get(scopeId("personal", "U1")))?.checkedAt, checkedAt);
-});
-
for (const combined of [true, false]) {
test(`turn cleanup retains recent and malformed paths and removes stale files (combined=${combined})`, async () => {
const built = freshApp();
diff --git a/test/own-credential-automation.test.ts b/test/own-credential-automation.test.ts
new file mode 100644
index 000000000..8bad6bcdf
--- /dev/null
+++ b/test/own-credential-automation.test.ts
@@ -0,0 +1,90 @@
+import { test, before, after, beforeEach } 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 { credentialHandle } from "../src/credentials/keychain.ts";
+import { scopeId, type TurnRequest } from "../src/types.ts";
+import { installGlobalFakeSprites, type FakeSprites } from "./support/fake-sprites.ts";
+import { testConfig } from "./support/test-config.ts";
+import { selectDefaultSandbox } from "./support/default-sandbox.ts";
+
+let ff: FakeSprites;
+before(() => {
+ ff = installGlobalFakeSprites();
+});
+beforeEach(() => ff.reset());
+after(() => ff.cleanup());
+
+const actor = { externalId: "U1" };
+
+async function setup() {
+ const built = buildApp(
+ testConfig({ dataDir: mkdtempSync(join(tmpdir(), "own-cred-automation-")), signingSecret: "own-cred-test" }),
+ );
+ await selectDefaultSandbox(built, "U1", scopeId("personal", "U1"), scopeId("channel", "C1"));
+ const credential = await built.keychain!.save({
+ ownerId: "U1",
+ service: "aws",
+ files: [{ path: ".aws/sso/cache/session.json", contentBase64: Buffer.from("own-login").toString("base64") }],
+ });
+ const read = `!execute ${JSON.stringify({
+ command: 'cat "$HOME/.aws/sso/cache/session.json"',
+ credentials: [credentialHandle(credential.id)],
+ })}`;
+ return { built, read };
+}
+
+test("a scheduled turn in the owner's DM uses the owner's credential without a grant", async () => {
+ const { built, read } = await setup();
+ const fired = await built.app.turn({
+ surface: "cron",
+ actor,
+ conversation: { kind: "dm", threadRef: "dm:U1:cron" },
+ text: read,
+ triggered: true,
+ } as TurnRequest);
+ assert.equal(fired.status, "ok", fired.reason);
+ assert.match(fired.reply ?? "", /own-login/);
+ assert.equal((await built.keychain!.listGrants({ ownerId: "U1" })).length, 0);
+});
+
+test("a subagent of the owner's DM uses the owner's credential without a grant", async () => {
+ const { built, read } = await setup();
+ const personal = scopeId("personal", "U1");
+ const parent = await built.sessions.getOrCreateByThread("dm:U1:parent", "dm", personal);
+ const child = await built.sessions.getOrCreateByThread("agent:main:subagent:own-cred", "dm", personal);
+ const conversation = {
+ kind: "dm" as const,
+ threadRef: "dm:U1:parent",
+ audience: [{ id: "U1", type: "internal" as const }],
+ };
+ await built.sessions.setParentSession(child.id, parent.id);
+ await built.sessions.addParticipant(child.id, "U1");
+ await built.sessions.setSpawnMeta(child.id, { surface: "web", actor: { id: "U1", type: "internal" }, conversation });
+ const result = await built.app.turn({
+ surface: "web",
+ actor,
+ conversation: { kind: "dm", threadRef: child.threadRef },
+ text: read,
+ triggered: true,
+ } as TurnRequest);
+ assert.equal(result.status, "ok", result.reason);
+ assert.match(result.reply ?? "", /own-login/);
+ assert.equal((await built.keychain!.listGrants({ ownerId: "U1" })).length, 0);
+});
+
+test("a scheduled turn in a shared channel cannot use the owner's credential without a grant", async () => {
+ const { built, read } = await setup();
+ await assert.rejects(
+ built.app.turn({
+ surface: "cron",
+ actor,
+ conversation: { kind: "channel", threadRef: "ch:C1:cron", channelRef: "C1", audience: [actor] },
+ text: read,
+ triggered: true,
+ } as TurnRequest),
+ /credential handle is not available/,
+ );
+});
diff --git a/test/postgres-map.test.ts b/test/postgres-map.test.ts
index dd4dc7358..5af7d6017 100644
--- a/test/postgres-map.test.ts
+++ b/test/postgres-map.test.ts
@@ -14,6 +14,7 @@ import {
} from "../src/credentials/keychain.ts";
import { deriveConnectorKey } from "../src/connectors/connector-client-store.ts";
import { applyPgMigrations, definePgMigration } from "../src/persistence/pg-pool.ts";
+import { materializeGrant } from "./support/materialize-grant.ts";
const URL = process.env.DATABASE_URL;
const skip = URL ? false : "set DATABASE_URL (a Postgres) to run the Postgres map tests";
@@ -368,8 +369,8 @@ test("pg map: concurrent keychain instances claim a once grant exactly once", {
purpose: "single use",
});
const results = await Promise.allSettled([
- owner.materialize(grant.id, scopeId("channel", "C1"), "U2"),
- peer.materialize(grant.id, scopeId("channel", "C1"), "U3"),
+ materializeGrant(owner, grant.id, scopeId("channel", "C1"), "U2"),
+ materializeGrant(peer, grant.id, scopeId("channel", "C1"), "U3"),
]);
assert.equal(results.filter((result) => result.status === "fulfilled").length, 1);
assert.equal(
diff --git a/test/process-registry.test.ts b/test/process-registry.test.ts
index 8be08d40f..e8fbedd4c 100644
--- a/test/process-registry.test.ts
+++ b/test/process-registry.test.ts
@@ -129,6 +129,40 @@ test("[postgres] honors the registry contract (durable across a restart)", { ski
}
});
+for (const [name, make] of [
+ ["memory", () => createMemoryProcessRegistry()],
+ ...(PG_URL ? [["postgres", () => createPostgresProcessRegistry(PG_URL)] as const] : []),
+] as const) {
+ test(`[${name}] background credential files survive a new registry until settled`, async () => {
+ const reg = make();
+ const credentialFiles = {
+ plan: { directory: "/tmp/qm-credentials.abc", credentials: [{ paths: [".aws/config"], roots: [] }] },
+ sources: [{ credentialId: "c1", ownerId: "U1", service: "aws", fingerprint: "f1" }],
+ };
+ const processId = id(Date.now() % 1_000_000);
+ try {
+ await reg.register({
+ processId,
+ scopeId: "personal:U1",
+ kind: "background",
+ command: "x",
+ ttlMs: 60_000,
+ credentialFiles,
+ });
+ const reopened = name === "postgres" ? make() : reg;
+ assert.deepEqual(await reopened.credentialFiles(processId), credentialFiles);
+ assert.equal((await reopened.get(processId))?.credentialsPending, true);
+ await reopened.setCredentialFiles(processId, null);
+ assert.equal(await reg.credentialFiles(processId), null);
+ assert.equal((await reg.get(processId))?.credentialsPending, undefined);
+ if (reopened !== reg) reopened.close?.();
+ } finally {
+ await reg.delete(processId);
+ reg.close?.();
+ }
+ });
+}
+
function id(n: number): string {
const s = n.toString(16).padStart(12, "0");
return `00000000-0000-0000-0000-${s}`;
diff --git a/test/prompt-inventory-order.test.ts b/test/prompt-inventory-order.test.ts
index 5477d7ab7..1cd00f6a8 100644
--- a/test/prompt-inventory-order.test.ts
+++ b/test/prompt-inventory-order.test.ts
@@ -1,6 +1,6 @@
import { test } from "node:test";
import assert from "node:assert/strict";
-import { renderComputerBlock, renderResidentLoginsBlock } from "../src/core/environment-facts.ts";
+import { renderComputerBlock } from "../src/core/environment-facts.ts";
import { renderGatewayContext } from "../src/core/gateway-context.ts";
import { sharedManifest } from "../src/core/attachments.ts";
import { renderSharingPosturePrompt } from "../src/resolution/sharing-posture.ts";
@@ -10,7 +10,6 @@ import {
renderReachRoster,
renderStandingObligations,
} from "../src/core/orchestrator/prompt-blocks.ts";
-import { RESIDENT_AUTH_CONNECTORS } from "../src/credentials/resident-auth.ts";
import type { Cron, Webhook, Monitor, GrantedHandle, CandidateDestination } from "../src/types.ts";
import type { DirectoryMember, DirectoryChannel } from "../src/directory/directory-store.ts";
@@ -75,16 +74,6 @@ test("environment and location facts are independent of discovery order", () =>
{ hasGlobal: false, teamCount: 0 },
),
);
- checkOrder([...RESIDENT_AUTH_CONNECTORS], (items) =>
- renderResidentLoginsBlock(
- {
- scopeId: "personal:U1",
- checkedAt: 1,
- connectors: Object.fromEntries(items.map((c) => [c.id, "active" as const])),
- },
- items,
- ),
- );
checkOrder(["channel:C2" as const, "team:T1" as const, "personal:U1" as const, "channel:C1" as const], (items) =>
renderSharingPosturePrompt({ id: "U1", type: "internal" }, items),
);
diff --git a/test/prompt-modes.test.ts b/test/prompt-modes.test.ts
index 917a9aeb2..70498c617 100644
--- a/test/prompt-modes.test.ts
+++ b/test/prompt-modes.test.ts
@@ -21,7 +21,6 @@ import { createDeployService } from "../src/deploy/deploy-service.ts";
import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
import type { Sandbox } from "../src/sandbox/sandbox.ts";
-import type { LivenessCache } from "../src/credentials/resident-auth.ts";
import type { ConnectorStatusCache } from "../src/credentials/connector-status.ts";
import type { ConnectorTokenStore } from "../src/credentials/keychain.ts";
import type { SkillStore } from "../src/skills/skill-store.ts";
@@ -54,11 +53,6 @@ function fakeSandbox(): Sandbox {
};
}
-const livenessCache: LivenessCache = {
- get: async () => ({ scopeId: "x", checkedAt: Date.now(), connectors: {} }),
- put: async () => {},
-};
-
const connectorStatusCache: ConnectorStatusCache = {
get: async () => ({ principalId: actor.id, checkedAt: Date.now(), providers: {} }),
put: async () => {},
@@ -123,7 +117,6 @@ function buildOrchestrator(
config,
...(opts.brandingDefault ? { brandingDefault: opts.brandingDefault } : {}),
skills,
- livenessCache,
connectorTokens,
connectorStatusCache,
signingSecret: "test-signing-secret",
diff --git a/test/resident-auth.test.ts b/test/resident-auth.test.ts
deleted file mode 100644
index ed8ef0bfe..000000000
--- a/test/resident-auth.test.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import { createMemoryMap } from "../src/persistence/durable-map.ts";
-import {
- buildResidentAuthProbeScript,
- createLivenessCache,
- probeResidentAuth,
- residentAuthProbeIsStale,
- RESIDENT_AUTH_CONNECTORS,
- type ScopeLivenessRecord,
-} from "../src/credentials/resident-auth.ts";
-import type { ExecResult, Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";
-
-const HANDLE = {} as SandboxHandle;
-
-function fakeSandbox(run: (cmd: string) => ExecResult): Sandbox {
- return { run: async (_h: SandboxHandle, cmd: string) => run(cmd) } as unknown as Sandbox;
-}
-
-test("liveness cache: put/get round-trips a scope record", async () => {
- const cache = createLivenessCache(createMemoryMap<ScopeLivenessRecord>());
- assert.equal(await cache.get("personal:U1"), null);
- const rec: ScopeLivenessRecord = { scopeId: "personal:U1", checkedAt: 100, connectors: { "aws-sso": "active" } };
- await cache.put(rec);
- assert.deepEqual(await cache.get("personal:U1"), rec);
- assert.equal(await cache.get("personal:U2"), null);
-});
-
-test("staleness: null is stale, fresh is fresh, past-TTL is stale", () => {
- const now = 1_000_000;
- assert.equal(residentAuthProbeIsStale(null, now), true);
- const fresh: ScopeLivenessRecord = { scopeId: "s", checkedAt: now - 1000, connectors: {} };
- assert.equal(residentAuthProbeIsStale(fresh, now, 5000), false);
- const old: ScopeLivenessRecord = { scopeId: "s", checkedAt: now - 6000, connectors: {} };
- assert.equal(residentAuthProbeIsStale(old, now, 5000), true);
-});
-
-test("probe script: command -v guards each check, bounds with timeout, emits id|state", () => {
- const script = buildResidentAuthProbeScript(RESIDENT_AUTH_CONNECTORS, 6);
- assert.match(script, /command -v gh/);
- assert.match(script, /timeout 6 gh auth status/);
- assert.match(script, /echo "gh\|active"/);
- assert.match(script, /echo "gcloud\|absent"/);
- assert.match(script, /\nwait$/);
-});
-
-test("probe: parses id|state output and writes the cache", async () => {
- const cache = createLivenessCache(createMemoryMap<ScopeLivenessRecord>());
- const sandbox = fakeSandbox(() => ({
- stdout: "gh|active\nglab|inactive\ngcloud|absent\n",
- stderr: "",
- code: 0,
- timedOut: false,
- }));
- await probeResidentAuth({ sandbox, handle: HANDLE, cache, scopeId: "personal:U1", now: 500 });
- const rec = await cache.get("personal:U1");
- assert.equal(rec?.checkedAt, 500);
- assert.equal(rec?.connectors["gh"], "active");
- assert.equal(rec?.connectors["glab"], "inactive");
- assert.equal(rec?.connectors["gcloud"], "absent");
-});
-
-test("probe: a connector the probe didn't report keeps its prior cached state", async () => {
- const cache = createLivenessCache(createMemoryMap<ScopeLivenessRecord>());
- await cache.put({ scopeId: "personal:U1", checkedAt: 1, connectors: { gh: "active", glab: "active" } });
- const sandbox = fakeSandbox(() => ({ stdout: "gh|inactive\n", stderr: "", code: 0, timedOut: false }));
- await probeResidentAuth({ sandbox, handle: HANDLE, cache, scopeId: "personal:U1", now: 2 });
- const rec = await cache.get("personal:U1");
- assert.equal(rec?.connectors["gh"], "inactive");
- assert.equal(rec?.connectors["glab"], "active");
-});
-
-test("probe: an empty/timed-out result does NOT reset the staleness window", async () => {
- const cache = createLivenessCache(createMemoryMap<ScopeLivenessRecord>());
- await cache.put({ scopeId: "personal:U1", checkedAt: 1, connectors: { "aws-sso": "active" } });
- const sandbox = fakeSandbox(() => ({ stdout: "", stderr: "", code: 124, timedOut: true }));
- await probeResidentAuth({ sandbox, handle: HANDLE, cache, scopeId: "personal:U1", now: 9999 });
- const rec = await cache.get("personal:U1");
- assert.equal(rec?.checkedAt, 1);
- assert.equal(rec?.connectors["aws-sso"], "active");
-});
-
-test("catalog: includes the known resident connectors with check + reauth commands", () => {
- const ids = RESIDENT_AUTH_CONNECTORS.map((c) => c.id);
- assert.deepEqual([...ids].sort(), ["gcloud", "gh", "glab"]);
- const gh = RESIDENT_AUTH_CONNECTORS.find((c) => c.id === "gh");
- assert.equal(gh?.check, "gh auth status");
- assert.match(gh?.reauth ?? "", /gh auth login/);
- assert.equal(
- RESIDENT_AUTH_CONNECTORS.find((c) => c.id === "acmecli"),
- undefined,
- );
- assert.equal(
- RESIDENT_AUTH_CONNECTORS.find((c) => c.id === "aws-sso"),
- undefined,
- );
-});
diff --git a/test/resident-paths.test.ts b/test/resident-paths.test.ts
index 07e2dce3b..171c71422 100644
--- a/test/resident-paths.test.ts
+++ b/test/resident-paths.test.ts
@@ -21,8 +21,6 @@ import { createExecExport, defaultExcludeAgentComputerExport } from "../src/sand
import {
DISPLACED_DIR_REL,
EPHEMERAL_CRED_DIR,
- captureRootForTarget,
- configCredentialDirs,
credentialServiceForPath,
ephemeralCredLinkPaths,
residentAuthPaths,
@@ -466,48 +464,3 @@ test("prep contains no deletion that can reach $HOME outside the quarantine", ()
assert.match(m[2]!, /-mindepth 1\b/, `find -exec rm at ${root} would also delete its own root`);
}
});
-
-test("captureRootForTarget: .config logins re-sweep the service dir; others their immediate parent", () => {
- assert.deepEqual(captureRootForTarget(".config/gh/hosts.yml"), { path: ".config/gh", kind: "directory" });
- assert.deepEqual(captureRootForTarget(".config/acmecorp/auth.json"), {
- path: ".config/acmecorp",
- kind: "directory",
- });
- assert.deepEqual(
- captureRootForTarget(".config/standalone"),
- { path: ".config/standalone", kind: "file" },
- "a bare file directly in .config is a file, not a bogus directory",
- );
- assert.deepEqual(captureRootForTarget(".aws/sso/cache/token.json"), {
- path: ".aws/sso/cache/token.json",
- kind: "file",
- });
- assert.deepEqual(captureRootForTarget(".netrc"), { path: ".netrc", kind: "file" });
- assert.deepEqual(
- captureRootForTarget(".local/share/foo/token"),
- { path: ".local/share/foo/token", kind: "file" },
- "a deep non-.config path never widens to a directory walk",
- );
- assert.deepEqual(
- captureRootForTarget("~/.fly/config.yml"),
- { path: ".fly/config.yml", kind: "file" },
- "legacy ~/-prefixed targets normalize instead of silently leaving the sweep",
- );
- assert.equal(captureRootForTarget("../escape"), undefined);
- assert.equal(captureRootForTarget("notdot/x"), undefined);
- assert.equal(captureRootForTarget(".config/"), undefined);
-});
-
-test("configCredentialDirs stays in lockstep with the .config links the prep layer projects", () => {
- const fromLinks = ephemeralCredLinkPaths()
- .filter((l) => l.rel.startsWith(".config/"))
- .map((l) => l.rel);
- for (const rel of fromLinks) {
- assert.ok(configCredentialDirs().includes(rel), `${rel} is linked by prep, so it must be swept`);
- }
- assert.ok(
- configCredentialDirs().includes(".config/glab-cli"),
- "real glab writes .config/glab-cli — a fresh login must be swept without registration",
- );
- assert.ok(configCredentialDirs().includes(".config/glab"), "glab is swept, not just validated");
-});
diff --git a/test/sandbox-open-continuity.test.ts b/test/sandbox-open-continuity.test.ts
index fb46f50f9..34b66e78c 100644
--- a/test/sandbox-open-continuity.test.ts
+++ b/test/sandbox-open-continuity.test.ts
@@ -15,7 +15,6 @@ function fixture(
open: true,
member: true,
allowed: true,
- cutoverMode: "legacy",
liveJobs: [] as Array<{ sandboxId: string; scopeId: string }>,
targetPolicy: { mode: "denylist", rules: [] } as import("../src/types.ts").CommandPolicy,
targetEgress: { allowedHosts: [], deniedHosts: [] } as import("../src/types.ts").EgressPolicy,
@@ -27,8 +26,6 @@ function fixture(
const runs: SandboxHandle[] = [];
const commands: string[] = [];
const released: unknown[][] = [];
- const credentialOwners: string[] = [];
- const restored: Uint8Array[] = [];
const starts: SandboxHandle[] = [];
const resource = { id: "personal-box", ownerScopeId: target };
const commandUses = new Map<string, number>();
@@ -64,9 +61,6 @@ function fixture(
teardown: async (...args: unknown[]) => {
released.push(args);
},
- writeFileBytes: async (_handle: unknown, _path: string, bytes: Uint8Array) => {
- restored.push(bytes);
- },
};
const roomSkill = {
skill: {
@@ -104,28 +98,6 @@ function fixture(
sandbox,
sandboxResources: resources,
processes: { listLive: async () => state.liveJobs },
- deviceFlowCutover: {
- listServices: async () => ["custom-login"],
- resolvePolicy: async () => ({ mode: state.cutoverMode }),
- residentResetGeneration: async () => null,
- },
- keychain: {
- listByOwner: async () => [
- { kind: "file", service: "custom-login", origin: "manual", targets: [".custom-login/token"] },
- ],
- materializeOwnFiles: async (owner: string) => {
- credentialOwners.push(owner);
- return [
- {
- service: "custom-login",
- origin: "manual",
- files: [
- { path: ".custom-login/token", contentBase64: Buffer.from("synthetic-own-file").toString("base64") },
- ],
- },
- ];
- },
- },
config,
isCurrentSharedScopeMember: async () => state.member,
},
@@ -143,17 +115,12 @@ function fixture(
scopeId: source,
memoryScopeId: source,
openResourceAccess: authority,
- credentialServices: [],
- credentialTools: [],
- quarantinedServices: [],
- cutoverModeOf: () => "legacy",
egressTokenForTurn: "synthetic-source-egress",
egressTokenForPolicy: async (policy: unknown) => {
egressClaims.push(policy);
return "synthetic-narrow-egress";
},
connectorEnv: { AGENT_API_TOKEN: "synthetic-room-capability", SHARED_SECRET: "synthetic-room-secret" },
- credentialCutoverServices: [],
ownerAuthAvailable: false,
turnSessionDir: "turn/session",
turnFilesDir: "turn/session/fire",
@@ -202,8 +169,6 @@ function fixture(
writes,
runs,
starts,
- credentialOwners,
- restored,
egressClaims,
approvalCalls,
released,
@@ -220,9 +185,7 @@ test("Open shared requests execute on the owner's personal machine without movin
assert.equal(f.provisions.length, 1);
assert.equal(JSON.stringify(f.provisions).includes("synthetic-room"), false);
assert.deepEqual(f.writes, []);
- assert.deepEqual(f.credentialOwners, ["alice"]);
assert.equal(f.runs.at(-1)?.env?.OWN_SECRET, undefined);
- assert.ok(f.restored.some((bytes) => Buffer.from(bytes).includes("synthetic-own-file")));
assert.equal(JSON.stringify(f.provisions).includes("team:private"), false);
const skill = await f.tools.skill("room-tool", { sandboxId: "personal-box" });
assert.match(skill.content ?? "", /synthetic-room-skill/);
@@ -307,8 +270,6 @@ test("Open personal credentials never flow into another shared computer", async
await f.tools.execute("pwd", { sandboxId: "personal-box" });
assert.equal(JSON.stringify(f.provisions).includes("synthetic-own"), false);
assert.equal(JSON.stringify(f.provisions).includes("synthetic-room"), false);
- assert.deepEqual(f.credentialOwners, []);
- assert.deepEqual(f.restored, []);
});
test("background starts use the authorized personal target and recheck revocation", async () => {
@@ -367,18 +328,6 @@ test("cross-target egress policies narrow both provider policy and minted proxy
});
});
-test("cached personal access reapplies device-flow quarantine without injecting environment credentials", async () => {
- const f = fixture();
- const first = await f.turn.provisionResource("personal-box");
- const firstRestores = f.restored.length;
- f.state.cutoverMode = "ephemeral_only";
- const second = await f.turn.provisionResource("personal-box");
- assert.equal(second, first);
- assert.ok(f.commands.some((command) => command.includes("rm -rf -- '.custom-login/token'")));
- assert.equal(f.restored.length, firstRestores);
- assert.equal(second.env, undefined);
-});
-
test("cross-scope teardown preserves live jobs regardless of which conversation started them", async () => {
for (const jobScope of ["personal:alice", "group:project", "group:other"]) {
const f = fixture();
@@ -467,22 +416,17 @@ test("cross-scope credential handles are refused without resolving or consuming
assert.deepEqual(f.provisions, []);
});
-test("credential-specific source policy still restricts an explicit cross-scope target", async () => {
- const calls: unknown[] = [];
+test("source policy still restricts an explicit cross-scope target", async () => {
const f = fixture("group:project", "personal:alice", true, {
- commandPolicyForCredentials: (handles, ownerAuth) => {
- calls.push([handles, ownerAuth]);
- return { mode: "denylist", rules: [{ pattern: "protected", decision: "deny" }] };
- },
+ commandPolicy: () => ({ mode: "denylist", rules: [{ pattern: "protected", decision: "deny" }] }),
});
await assert.rejects(f.tools.execute("protected", { sandboxId: "personal-box" }), /denied/);
- assert.deepEqual(calls, [[[], false]]);
assert.deepEqual(f.provisions, []);
});
-test("credential-specific source approval intersects target policy without consuming approval on target denial", async () => {
+test("source approval intersects target policy without consuming approval on target denial", async () => {
const f = fixture("group:project", "personal:alice", true, {
- commandPolicyForCredentials: () => ({
+ commandPolicy: () => ({
mode: "denylist",
rules: [{ pattern: "protected", decision: "require_approval" }],
}),
@@ -505,12 +449,10 @@ test("credential-specific source approval intersects target policy without consu
await assert.rejects(f.tools.execute("protected command", { sandboxId: "personal-box" }), NeedsApproval);
});
-test("same-scope explicit execution prepares credentials only after provisioning and wraps the effective env", async () => {
+test("same-scope explicit execution prepares credentials only after provisioning", async () => {
const events: string[] = [];
const f: ReturnType<typeof fixture> = fixture("group:project", "group:project", true, {
- commandPolicyForCredentials: (handles, ownerAuth) => {
- assert.deepEqual(handles, ["lazy"]);
- assert.equal(ownerAuth, false);
+ commandPolicy: () => {
events.push("policy");
return { mode: "denylist", rules: [] };
},
@@ -530,14 +472,8 @@ test("same-scope explicit execution prepares credentials only after provisioning
},
},
],
- scopedCommand: (command, env) => {
- events.push("wrap");
- assert.equal(env?.TOKEN, "synthetic-command-secret");
- assert.equal(env?.SHARED_SECRET, "synthetic-room-secret");
- return command;
- },
});
await f.tools.execute("pwd", { sandboxId: "personal-box", credentials: ["lazy"] });
- assert.deepEqual(events, ["policy", "resolve", "commit", "wrap"]);
+ assert.deepEqual(events, ["policy", "resolve", "commit"]);
assert.equal(f.runs.at(-1)?.env?.TOKEN, "synthetic-command-secret");
});
diff --git a/test/sandbox-resources.test.ts b/test/sandbox-resources.test.ts
index 61c879fb9..d896fdd8e 100644
--- a/test/sandbox-resources.test.ts
+++ b/test/sandbox-resources.test.ts
@@ -1,7 +1,6 @@
import { createBackgroundBroker } from "../src/connectors/background-exec-broker.ts";
import { createMemoryProcessRegistry } from "../src/processes/process-registry.ts";
import { supportsProcessSessions } from "../src/sandbox/sandbox.ts";
-import { createDeviceFlowCutoverStore } from "../src/credentials/device-flow-cutover.ts";
import { createTurnSandboxes, type TurnSandboxContext } from "../src/core/orchestrator/sandboxes.ts";
import { test } from "node:test";
import assert from "node:assert/strict";
@@ -226,7 +225,6 @@ test("turn default changes invalidate cached provisioning while explicit calls d
turnFilesDir: "turn/s/t",
connectorEnv: { AGENT_API_TOKEN: "scope-token" },
ownerAuthAvailable: false,
- credentialCutoverServices: [],
visibleSkills: [],
visibleSkillsForTurn: async () => [],
emitGapWork: () => {},
@@ -372,131 +370,6 @@ for (const fail of [false, true])
);
});
-for (const shared of [false, true])
- test(`explicit target receives the same credential cleanup and restore as default (${shared ? "isolated shared automation" : "personal"})`, async () => {
- const scripts: Array<{ id: string; script: string }> = [];
- const restoredTars: Array<{ id: string; bytes: Uint8Array }> = [];
- const scope = shared ? "channel:team" : "personal:alice";
- const owner = shared ? scope : "alice";
- let failCleanupFor: string | undefined;
- const { resources, router } = fixture(
- (backend) => {
- backend.run = async (handle, script) => {
- scripts.push({ id: handle.id, script });
- if (handle.id === failCleanupFor && script.includes("rm -rf --")) {
- failCleanupFor = undefined;
- return { stdout: "", stderr: "cleanup failed", code: 1, timedOut: false };
- }
- return { stdout: "", stderr: "", code: 0, timedOut: false };
- };
- backend.writeFileBytes = async (handle, path, bytes) => {
- if (path.endsWith(".tar")) restoredTars.push({ id: handle.id, bytes });
- };
- },
- [scope],
- );
- const record = await resources.create("admin", scope, "local");
- const owners: string[] = [];
- const resetMarks: unknown[][] = [];
- const cutover = createDeviceFlowCutoverStore(createMemoryMap(), { resets: createMemoryMap() });
- await cutover.set(scope, "aws", "ephemeral_only", "admin");
- await cutover.set(scope, "aws", "legacy", "admin");
- const turn = createTurnSandboxes({
- deps: {
- sandbox: router,
- sandboxResources: resources,
- keychain: {
- listByOwner: async (id: string) => {
- owners.push(id);
- return [
- { kind: "file", service: "aws", origin: "device-flow-auto-capture", targets: [".aws/config"] },
- { kind: "file", service: "gh", origin: "device-flow-auto-capture", targets: [".config/gh/hosts.yml"] },
- ];
- },
- materializeOwnFiles: async (id: string) => {
- owners.push(id);
- return [
- {
- service: "aws",
- origin: "device-flow-auto-capture",
- files: [{ path: ".aws/config", contentBase64: Buffer.from("allowed-token").toString("base64") }],
- },
- {
- service: "gh",
- origin: "device-flow-auto-capture",
- files: [
- {
- path: ".config/gh/hosts.yml",
- contentBase64: Buffer.from("quarantined-token").toString("base64"),
- },
- ],
- },
- ];
- },
- },
- deviceFlowCutover: {
- ...cutover,
- markResidentReset: async (...args: Parameters<typeof cutover.markResidentReset>) => {
- resetMarks.push(args);
- await cutover.markResidentReset(...args);
- },
- },
- },
- input: { origin: shared ? { kind: "automation", useOwnerKeychain: true } : { kind: "user" } },
- actor: { id: "alice", type: "internal" },
- session: { id: "s" },
- resolution: { layers: [{ scopeId: scope, mode: "rw", mountPath: "/" }] },
- scopeId: scope,
- memoryScopeId: scope,
- transferId: "t",
- turnSessionDir: "turn/s",
- turnFilesDir: "turn/s/t",
- connectorEnv: {},
- isolateOwnerKeychain: shared,
- ownerAuthAvailable: false,
- credentialTools: [
- { service: "aws", roots: [".aws"] },
- { service: "gh", roots: [".config/gh"] },
- ],
- credentialServices: ["aws"],
- credentialCutoverServices: [],
- quarantinedServices: ["gh"],
- cutoverModeOf: () => "legacy",
- visibleSkills: [],
- visibleSkillsForTurn: async () => [],
- emitGapWork: () => {},
- perf: { credsMs: 0 },
- } as unknown as TurnSandboxContext);
- if (shared) {
- failCleanupFor = scope;
- await assert.rejects(turn.provision(), /quarantine failed/);
- turn.invalidateProvision();
- }
- const legacyId = (await resources.list("admin", scope)).defaultSandboxId;
- const defaultHandle = shared ? await turn.provisionResource(legacyId!) : await turn.provision();
- failCleanupFor = record.backingScopeId;
- await assert.rejects(turn.provisionResource(record.id), /quarantine failed/);
- const explicit = await turn.provisionResource(record.id);
- assert.ok(owners.length >= 6);
- assert.ok(owners.every((id) => id === owner));
- assert.equal(resetMarks.length, 2);
- for (const handle of [defaultHandle, explicit]) {
- assert.ok(
- scripts.some(
- ({ id, script }) =>
- id === handle.id &&
- script.includes("rm -rf --") &&
- script.includes(".config/gh") &&
- script.includes(".aws"),
- ),
- );
- const tar = restoredTars.find(({ id }) => id === handle.id);
- assert.ok(tar);
- assert.ok(Buffer.from(tar.bytes).includes(Buffer.from("allowed-token")));
- assert.ok(!Buffer.from(tar.bytes).includes(Buffer.from("quarantined-token")));
- }
- });
-
test("activation preserves routes, cold identities and explicit nulls without calling a provider", async () => {
const { options, records, defaults, routes, marker, provisioned, upgrade } = fixture(undefined, []);
await routes.put("personal:routed", { backend: "modal" });
diff --git a/test/save-login.test.ts b/test/save-login.test.ts
new file mode 100644
index 000000000..f52975b6c
--- /dev/null
+++ b/test/save-login.test.ts
@@ -0,0 +1,72 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { execFile } from "node:child_process";
+import { createServer } from "node:http";
+import { mkdir, mkdtemp, rm, writeFile, access } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { promisify } from "node:util";
+import { fileURLToPath } from "node:url";
+import { createKeychain } from "../src/credentials/keychain.ts";
+import { createMemoryMap } from "../src/persistence/durable-map.ts";
+import { deriveConnectorKey } from "../src/connectors/connector-client-store.ts";
+import { scopeId } from "../src/types.ts";
+
+const run = promisify(execFile);
+const script = fileURLToPath(new URL("../skills-seed/interactive-login/scripts/save-login.mjs", import.meta.url));
+
+test("save-login uploads a private login home and re-saving keeps the entry and its grants", async (t) => {
+ const keychain = createKeychain({
+ creds: createMemoryMap(),
+ grants: createMemoryMap(),
+ asks: createMemoryMap(),
+ key: deriveConnectorKey("synthetic-test-key"),
+ });
+ const server = createServer(async (req, res) => {
+ let body = "";
+ for await (const chunk of req) body += chunk;
+ if (req.headers["x-agent-capability"] !== "synthetic-token") return res.writeHead(401).end();
+ const input = JSON.parse(body);
+ const credential = await keychain.save({ ownerId: "U1", ...input, origin: "agent-session:test" });
+ res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ credential }));
+ });
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ t.after(() => server.close());
+ const { port } = server.address() as { port: number };
+ const env = { ...process.env, AGENT_API_URL: `http://127.0.0.1:${port}`, AGENT_API_TOKEN: "synthetic-token" };
+
+ const login = async (token: string) => {
+ const home = await mkdtemp(join(tmpdir(), "login-home-"));
+ t.after(() => rm(home, { recursive: true, force: true }));
+ await mkdir(join(home, ".aws/sso/cache"), { recursive: true });
+ await mkdir(join(home, ".aws/logs"), { recursive: true });
+ await writeFile(join(home, ".aws/config"), "[profile work]\nsso_session = work\n");
+ await writeFile(join(home, ".aws/sso/cache/token.json"), token, { mode: 0o600 });
+ await writeFile(join(home, ".aws/logs/cli.log"), "noise");
+ const { stdout } = await run("node", [script, "aws", home, "--label", "work"], { env });
+ await assert.rejects(access(home));
+ return stdout;
+ };
+
+ assert.match(await login("first"), /^saved aws \(2 files\) as kc_/);
+ const [saved] = await keychain.listByOwner("U1");
+ const grant = await keychain.createGrant({
+ credentialId: saved!.id,
+ ownerId: "U1",
+ audienceScopeId: scopeId("channel", "C1"),
+ mode: "standing",
+ purpose: "test",
+ });
+ await login("second");
+ const after = await keychain.listByOwner("U1");
+ assert.equal(after.length, 1);
+ assert.equal(after[0]!.id, saved!.id);
+ assert.equal(after[0]!.accountLabel, "work");
+ assert.equal((await keychain.getGrant(grant.id))?.status, "active");
+ const loaded = await keychain.materializeOwnById("U1", saved!.id, scopeId("personal", "U1"));
+ assert.equal(loaded.kind, "file");
+ if (loaded.kind !== "file") return;
+ const token = loaded.files.find((file) => file.path === ".aws/sso/cache/token.json")!;
+ assert.equal(Buffer.from(token.contentBase64, "base64").toString(), "second");
+ assert.equal(token.mode, 0o600);
+});
diff --git a/test/scope-reach.test.ts b/test/scope-reach.test.ts
index 54bd2a83b..6f6f49e2d 100644
--- a/test/scope-reach.test.ts
+++ b/test/scope-reach.test.ts
@@ -389,7 +389,6 @@ test("reach preserves a durable workspace even without a login-probe cache", asy
test("reach teardown keeps (does not destroy) a room with its own computer", async () => {
const built = await roomApp();
- await built.livenessCache.put({ scopeId: scopeId("channel", "C-ph"), checkedAt: 1, connectors: {} });
await built.app.turn(dm("!reach #project-alpha echo hi"));
assert.ok(
fakeSprites.names().some((n) => n.startsWith("qm-sandbox-room-ph-")),
diff --git a/test/secret-drop.test.ts b/test/secret-drop.test.ts
index 024cba539..7d33bcdae 100644
--- a/test/secret-drop.test.ts
+++ b/test/secret-drop.test.ts
@@ -25,6 +25,7 @@ import {
import { signedRequestHeaders } from "../plugins/chassis/src/source-auth-sign.ts";
import { scopeId, type TurnRequest, type TurnResult } from "../src/types.ts";
import { testConfig } from "./support/test-config.ts";
+import { materializeGrant } from "./support/materialize-grant.ts";
const SECRET = "secret-drop-test-secret".repeat(3);
@@ -96,7 +97,7 @@ describe("fireDropResolution", () => {
run,
});
- it("resumes the channel as the owner with the grant use command, exactly once", async () => {
+ it("resumes the channel as the owner with the credential handle, exactly once", async () => {
const seen: TurnRequest[] = [];
const deps = depsWith(async (req) => {
seen.push(req);
@@ -111,13 +112,14 @@ describe("fireDropResolution", () => {
threadRef: "ch:C1-t",
destination: { type: "slack", target: "C1", audienceScopeId: scopeId("channel", "C1") },
grantId: "g1",
+ credentialHandle: "kc_stripe",
granted: true,
};
await fireDropResolution(deps, drop);
assert.equal(seen.length, 1);
assert.equal(seen[0]!.conversation.kind, "channel");
assert.equal(seen[0]!.actor.externalId, "U_A", "the resume runs as the owner who supplied the key");
- assert.match(seen[0]!.text, /g1/, "the channel resume hands over the grant's use command");
+ assert.match(seen[0]!.text, /kc_stripe/, "the channel resume hands over the credential handle");
await fireDropResolution(deps, drop);
assert.equal(seen.length, 1, "the drop:<id> fireKey dedupes a double fire");
@@ -158,6 +160,7 @@ describe("fireDropResolution", () => {
audienceScopeId: scopeId("channel", "C1"),
destination,
grantId: "g1",
+ credentialHandle: "kc_stripe",
granted: true,
});
const fb = (await deliveries.pending("slack")).filter((d) => d.idempotencyKey === "drop:dropF:fallback");
@@ -179,11 +182,12 @@ describe("fireDropResolution", () => {
audienceScopeId: scopeId("channel", "C1"),
destination: { type: "slack", target: "C1", audienceScopeId: scopeId("channel", "C1") },
grantId: "gA",
+ credentialHandle: "kc_alpha",
granted: true,
pendingSiblings: ["betasvc"],
});
assert.equal(seen.length, 1, "every redeem wakes the conversation — it must hear each grant's load command");
- assert.match(seen[0]!.text, /gA/, "the intermediate wake still hands over its own grant");
+ assert.match(seen[0]!.text, /kc_alpha/, "the intermediate wake still hands over its own handle");
assert.match(seen[0]!.text, /`betasvc`.*haven't been filled/, "the wake names the outstanding sibling links");
assert.match(seen[0]!.text, /keep waiting/);
assert.doesNotMatch(seen[0]!.text, /Pick the task back up/, "no resume order while credentials are outstanding");
@@ -337,7 +341,7 @@ describe("/v1/keychain/drops — mint, form, redeem", async () => {
const grants = await built.keychain!.grantsForScope(scopeId("channel", "C1"));
const g = grants.find((x) => x.credential.id === credential.id);
assert.ok(g, "a standing grant for the asking channel was minted on redeem");
- const m = await built.keychain!.materialize(g!.grant.id, scopeId("channel", "C1"), "U_A");
+ const m = await materializeGrant(built.keychain!, g!.grant.id, scopeId("channel", "C1"), "U_A");
assert.ok(m.kind === "env" && m.env[0]!.value === "sk_test_alice");
assert.equal((await redeem(dropId, { secret: "sk_test_again" }, "U_A", t)).status, 404);
@@ -431,7 +435,7 @@ describe("/v1/keychain/drops — mint, form, redeem", async () => {
const grants = await built.keychain!.grantsForScope(scopeId("channel", "C1"));
const g = grants.find((x) => x.credential.id === credential.id);
- const m = await built.keychain!.materialize(g!.grant.id, scopeId("channel", "C1"), "U_A");
+ const m = await materializeGrant(built.keychain!, g!.grant.id, scopeId("channel", "C1"), "U_A");
assert.ok(m.kind === "env");
assert.deepEqual(m.kind === "env" ? m.env : [], [
{ key: "DOORDASH_EMAIL", value: "[email protected]", secret: false },
diff --git a/test/shared-credential-resumption.test.ts b/test/shared-credential-resumption.test.ts
index cec240d10..cdf362907 100644
--- a/test/shared-credential-resumption.test.ts
+++ b/test/shared-credential-resumption.test.ts
@@ -10,6 +10,7 @@ import { createMemoryMap } from "../src/persistence/durable-map.ts";
import { fireAskResolution, type AskResolutionDeps } from "../src/triggers/keychain-ask.ts";
import { runTrigger } from "../src/triggers/run-trigger.ts";
import type { Principal, ScopeId, TurnRequest } from "../src/types.ts";
+import { materializeGrant } from "./support/materialize-grant.ts";
function keychain(now = Date.now) {
return createKeychain({
@@ -118,13 +119,13 @@ for (const scope of ["channel:C_SHARED", "group:G_SHARED", "group:web-project-sh
assert.equal(f.grant.audienceScopeId, scope);
assert.equal(f.grant.ownerId, "U_CREDENTIAL_OWNER");
await assert.rejects(
- f.k.materialize(f.grant.id, "channel:C_OTHER", request.actor.externalId),
+ materializeGrant(f.k, f.grant.id, "channel:C_OTHER", request.actor.externalId),
(error: KeychainError) => error.status === 403,
);
- const materialized = await f.k.materialize(f.grant.id, scope, request.actor.externalId);
+ const materialized = await materializeGrant(f.k, f.grant.id, scope, request.actor.externalId);
assert.ok(materialized.kind === "env" && materialized.env[0]!.value === "dummy-only-secret");
await assert.rejects(
- f.k.materialize(f.grant.id, scope, request.actor.externalId),
+ materializeGrant(f.k, f.grant.id, scope, request.actor.externalId),
(error: KeychainError) => error.status === 410,
);
const pending = await f.deliveries.pending("principal");
diff --git a/test/sharing-context-e2e.test.ts b/test/sharing-context-e2e.test.ts
index 0564e8c96..ddba4f443 100644
--- a/test/sharing-context-e2e.test.ts
+++ b/test/sharing-context-e2e.test.ts
@@ -438,15 +438,15 @@ test("Open speaker keychain uses a disposable computer, follows the speaker, and
maxAttempts: 1,
});
assert.ok(b.keychain);
- const handles = new Map<string, string>();
+ const handles = new Map<string, string[]>();
for (const id of ["U1", "U2"]) {
const credential = await b.keychain.save({ ownerId: id, service: "npm", secret: `npm_${id}`, envKey: "NPM_TOKEN" });
- handles.set(id, credentialHandle(credential.id));
- await b.keychain.save({
+ const login = await b.keychain.save({
ownerId: id,
service: "custom-cli",
files: [{ path: ".custom-cli/auth", contentBase64: Buffer.from(`file_${id}`).toString("base64") }],
});
+ handles.set(id, [credentialHandle(credential.id), credentialHandle(login.id)]);
await b.keychain.setConnectorToken("gmail.googleapis.com", id, {
accessToken: `gmail_${id}`,
expiresAt: Date.now() + 3600000,
@@ -460,7 +460,7 @@ test("Open speaker keychain uses a disposable computer, follows the speaker, and
await b.workspace.write("channel:C1", "room-only.txt", "room_data");
const probe = `python3 -c 'import os,pathlib; p=pathlib.Path.home()/".custom-cli/auth"; print("|".join([os.getenv("NPM_TOKEN","unset"),os.getenv("VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM","unset"),p.read_text() if p.exists() else "absent",os.getenv("AGENT_API_TOKEN","unset"),"room" if pathlib.Path("room-only.txt").exists() else "isolated"]))'`;
const selected = (id = "U1") =>
- `!execute ${JSON.stringify({ command: probe.replace('os.getenv("NPM_TOKEN","unset")', `str(os.getenv("NPM_TOKEN") == "npm_${id}")`).replace('os.getenv("VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM","unset")', `str(os.getenv("VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM") == "gmail_${id}")`), ownerAuth: true, credentials: [handles.get(id), "connector_gmail_googleapis_com_default"] })}`;
+ `!execute ${JSON.stringify({ command: probe.replace('os.getenv("NPM_TOKEN","unset")', `str(os.getenv("NPM_TOKEN") == "npm_${id}")`).replace('os.getenv("VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM","unset")', `str(os.getenv("VAULT_TOKEN_GMAIL_GOOGLEAPIS_COM") == "gmail_${id}")`), ownerAuth: true, credentials: [...handles.get(id)!, "connector_gmail_googleapis_com_default"] })}`;
assert.equal(await b.turn(selected(), true), "True|True|file_U1|unset|isolated");
assert.equal(await b.turn(selected("U2"), true, "U2"), "True|True|file_U2|unset|isolated");
for (const id of ["U1", "U2", "U1"]) {
diff --git a/test/skills-callable.test.ts b/test/skills-callable.test.ts
index 4c3eb542d..70e6f898a 100644
--- a/test/skills-callable.test.ts
+++ b/test/skills-callable.test.ts
@@ -120,7 +120,7 @@ async function publishFileSkill(skills: ReturnType<typeof buildApp>["skills"], n
return sk;
}
-test("skill files live only for the turn that loaded them", async () => {
+test("skill files stay on the computer across turns", async () => {
const { app, skills } = await withDefaultComputer(freshApp());
const first = await publishFileSkill(skills, "helper");
const request = {
@@ -131,7 +131,7 @@ test("skill files live only for the turn that loaded them", async () => {
const ran = await app.turn({ ...request, text: "!skill-run helper cat {dir}/scripts/run.sh" } as TurnRequest);
assert.equal(ran.reply, "printf helper");
const next = await app.turn({ ...request, text: "!run find . -name run.sh | wc -l | tr -d ' '" } as TurnRequest);
- assert.equal(next.reply, "0");
+ assert.equal(next.reply, "1");
await skills.archive(first.id);
assert.match((await app.turn({ ...request, text: "!skill helper" } as TurnRequest)).reply ?? "", /no skill file/);
});
diff --git a/test/skills-source-read.test.ts b/test/skills-source-read.test.ts
index 81feeb871..62b2830aa 100644
--- a/test/skills-source-read.test.ts
+++ b/test/skills-source-read.test.ts
@@ -57,10 +57,7 @@ test("published skill sources and assets read without a sandbox, respecting scop
const b = await fixture(t);
const skill = await b.publish("personal:U1", "PUBLISHED_BODY");
assert.match(await b.turn("!sysprompt"), /\*\*source-helper\*\*/);
- assert.match(
- await b.turn("!skill source-helper"),
- /^\.agent-turn\/\w+\/[\w-]+\/skills\/source-helper\nPUBLISHED_BODY$/,
- );
+ assert.match(await b.turn("!skill source-helper"), /^\/.+\/\.agent-skills\/\w+\/source-helper\nPUBLISHED_BODY$/);
assert.match(await b.turn("!skill source-helper references/example.txt"), /\nPUBLISHED_ASSET$/);
assert.equal(await b.turn("!skill-run source-helper cat {dir}/references/example.txt"), "PUBLISHED_ASSET");
assert.match(await b.turn("!skill source-helper", "U2"), /no skill file/);
@@ -112,7 +109,7 @@ test("published source follows scope shadowing and preserves sandbox-authored wo
assert.match(await b.turn("!skill source-helper"), /\nORG_BODY$/);
});
-test("a body read avoids sandbox work and a file request materializes that skill under the turn directory", async () => {
+test("a body read avoids sandbox work and a file request materializes that skill under the skills root", async () => {
const { createTurnSandboxes } = await import("../src/core/orchestrator/sandboxes.ts");
type TurnSandboxContext = import("../src/core/orchestrator/sandboxes.ts").TurnSandboxContext;
type SkillResolution = import("../src/skills/skill-store.ts").SkillResolution;
@@ -164,7 +161,6 @@ test("a body read avoids sandbox work and a file request materializes that skill
turnSessionDir: "turn/s",
turnFilesDir: "turn/s/t",
connectorEnv: {},
- credentialCutoverServices: [],
visibleSkillsForTurn: async () => visible,
emitGapWork: () => {},
perf: { credsMs: 0 },
@@ -184,12 +180,12 @@ test("a body read avoids sandbox work and a file request materializes that skill
visible[0] = resolution;
const loaded = await turn.useSkill("source-helper", "references/example.txt");
assert.equal(loaded.content, "v2");
- assert.equal(loaded.dir, "turn/s/t/skills/source-helper");
+ assert.equal(loaded.dir, "/workspace/.agent-skills/s/source-helper");
assert.equal(provisions, 1);
- assert.equal(files.get("turn/s/t/skills/source-helper/references/example.txt"), "v2");
- files.set("turn/s/t/skills/source-helper/references/example.txt", "local edit");
+ assert.equal(files.get(".agent-skills/s/source-helper/references/example.txt"), "v2");
+ files.set(".agent-skills/s/source-helper/references/example.txt", "local edit");
await turn.useSkill("source-helper", "SKILL.md");
- assert.equal(files.get("turn/s/t/skills/source-helper/references/example.txt"), "local edit");
+ assert.equal(files.get(".agent-skills/s/source-helper/references/example.txt"), "local edit");
resolution.skill!.pack = { packId: "pack", commit: "c", upstreamName: "source-helper" };
resolution.screenedBundles = [
{ packId: "pack", commit: "c", hash: "pack-hash", files: [{ path: "lib.txt", content: "PACK_RESOURCE" }] },
@@ -198,25 +194,25 @@ test("a body read avoids sandbox work and a file request materializes that skill
await turn.provisionResource("resource-1");
assert.deepEqual(files, beforeResource);
const onResource = await turn.useSkill("source-helper", "SKILL.md", "resource-1");
- assert.equal(onResource.packDir, "turn/s/t/skills/.packs/pack");
+ assert.equal(onResource.packDir, "/workspace/.agent-skills/s/.packs/pack");
assert.deepEqual(sandboxIds, [undefined, "resource-1"]);
- assert.equal(files.get("turn/s/t/skills/.packs/pack/lib.txt"), "PACK_RESOURCE");
+ assert.equal(files.get(".agent-skills/s/.packs/pack/lib.txt"), "PACK_RESOURCE");
assert.equal(
[...files.keys()].some((path) => path.includes("/unrelated/")),
false,
);
});
-test("pack assets run from the turn directory and vanish with it", async (t) => {
+test("pack assets run from the skills root and stay across turns", async (t) => {
const { computeBundleHash } = await import("../src/skills/skill-bundle-store.ts");
const b = await fixture(t);
const skill = await b.publish("personal:U1", "PACK_BODY", "source-pack");
const files = [{ path: "example.sh", content: "printf PACK_ASSET" }];
await b.skillBundles.put({ packId: "source-pack", commit: "c", files, hash: computeBundleHash(files) });
- assert.match(await b.turn("!skill source-helper"), /skills\/\.packs\/source-pack/);
+ assert.match(await b.turn("!skill source-helper"), /\.agent-skills\/\w+\/\.packs\/source-pack/);
assert.equal(b.provisions(), 1);
assert.equal(await b.turn("!skill-run source-helper sh {dir}/../.packs/source-pack/example.sh"), "PACK_ASSET");
- assert.equal(await b.turn("!run find . -name example.sh | wc -l | tr -d ' '"), "0");
+ assert.equal(await b.turn("!run find . -name example.sh | wc -l | tr -d ' '"), "1");
await b.skills.archive(skill.id);
assert.match(await b.turn("!skill source-helper"), /no skill file/);
});
diff --git a/test/support/materialize-grant.ts b/test/support/materialize-grant.ts
new file mode 100644
index 000000000..88aa81490
--- /dev/null
+++ b/test/support/materialize-grant.ts
@@ -0,0 +1,12 @@
+import type { Keychain, MaterializedCred } from "../../src/credentials/keychain.ts";
+
+export async function materializeGrant(
+ keychain: Pick<Keychain, "prepareMaterialize">,
+ grantId: string,
+ scopeId: Parameters<Keychain["prepareMaterialize"]>[1],
+ usedBy: string,
+): Promise<MaterializedCred> {
+ const prepared = await keychain.prepareMaterialize(grantId, scopeId, usedBy);
+ await prepared.commit();
+ return prepared.materialized;
+}
diff --git a/test/swarms.test.ts b/test/swarms.test.ts
index e0d5f2c07..2d12adcc3 100644
--- a/test/swarms.test.ts
+++ b/test/swarms.test.ts
@@ -885,9 +885,7 @@ test("the orchestrator sandbox path selects worker storage without changing auth
connectorEnv: {},
isolateOwnerKeychain: false,
ownerAuthAvailable: false,
- credentialTools: [],
credentialServices: [],
- credentialCutoverServices: [],
quarantinedServices: [],
visibleSkills: [],
visibleSkillsForTurn: async () => [],
diff --git a/test/system-prompt-order.test.ts b/test/system-prompt-order.test.ts
index 29951632d..0b7b6ac3f 100644
--- a/test/system-prompt-order.test.ts
+++ b/test/system-prompt-order.test.ts
@@ -27,7 +27,6 @@ import { createDeployService } from "../src/deploy/deploy-service.ts";
import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
import type { Sandbox } from "../src/sandbox/sandbox.ts";
-import type { LivenessCache } from "../src/credentials/resident-auth.ts";
import type { ConnectorStatusCache } from "../src/credentials/connector-status.ts";
import type { ConnectorTokenStore } from "../src/credentials/keychain.ts";
import { createSkillStore, type SkillStore } from "../src/skills/skill-store.ts";
@@ -70,11 +69,6 @@ function readSandbox(): Sandbox {
};
}
-const livenessCache: LivenessCache = {
- get: async () => ({ scopeId: "x", checkedAt: Date.now(), connectors: { gh: "active" } }),
- put: async () => {},
-};
-
const connectorStatusCache: ConnectorStatusCache = {
get: async () => ({ principalId: actor.id, checkedAt: Date.now(), providers: { google: { connected: true } } }),
put: async () => {},
@@ -136,7 +130,6 @@ function buildOrchestrator(
acl,
config,
skills,
- livenessCache,
connectorTokens,
connectorStatusCache,
resolveConnectorClient: async (provider) => {