Compare commits

..
Author SHA1 Message Date
shadowdaoandClaude Opus 5.5 0a325cb7f2 fix(aws): Host settings merge saves against the latest config and cache the environment's lifetime cap
Build App (Preview) / compute-version (pull_request) Successful in 38s
Secret Scan / scan (push) Successful in 17s
Secret Scan / scan (pull_request) Successful in 11s
Build App (Preview) / create-release (pull_request) Successful in 7s
Build App (Preview) / test (pull_request) Successful in 8m14s
Build App (Preview) / build-macos (pull_request) Successful in 6m4s
Build App (Preview) / build-windows (pull_request) Successful in 10m43s
Build Container / build-container (pull_request) Successful in 19m45s
Build App (Preview) / build-linux (pull_request) Successful in 16m22s
Build App (Preview) / prune-previews (pull_request) Successful in 9s
A save right after the setup script's blur (a toggle) spread the render-time
config and reverted the script; saves now start from the stored config plus
every patch saved since. aws_environment_max_hours is cached per connection,
region and environment for 10 minutes, so re-mounting the section does not
ask AWS again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:10:37 -08:00
shadowdaoandClaude Opus 5.5 ea37f8434f fix(aws): retry the copy-at-connect secret read while the secret is pending deletion
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:46 -08:00
shadowdaoandClaude Opus 5.5 544409c554 fix(aws): no Pricing client counts as a failed fetch, so the 5-minute back-off applies
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:46 -08:00
shadowdaoandClaude Opus 5.5 be96b5f68f fix(aws): the copy-at-connect warning gets its own toast topic
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:46 -08:00
shadowdaoandClaude Opus 5.5 ff5ae9862b fix(aws): a waiting clone retry no longer holds the retry lock, so a later connect cannot strand it
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:07:14 -08:00
shadowdaoandClaude Opus 5.5 9e1e72d2a4 feat(aws): setup script editor
Build App (Preview) / compute-version (pull_request) Successful in 6s
Secret Scan / scan (push) Successful in 7s
Secret Scan / scan (pull_request) Successful in 14s
Build App (Preview) / create-release (pull_request) Successful in 6s
Build App (Preview) / build-macos (pull_request) Successful in 6m3s
Build App (Preview) / test (pull_request) Successful in 8m39s
Build App (Preview) / build-windows (pull_request) Successful in 11m38s
Build Container / build-container (pull_request) Successful in 17m24s
Build App (Preview) / build-linux (pull_request) Failing after 21m19s
Build App (Preview) / prune-previews (pull_request) Skipped
HostSection gets a monospace "Setup script" textarea (saved on blur, blank
as none) with the 32 KiB UTF-8 limit checked using the backend's message,
and notes on when it runs (as claude, bash -e, after the clone, every start,
30 minutes, output in the Overview card) and what it gets under
copy-at-connect.

Also in Host settings: the environment's advisory lifetime cap (Q8), read
by a new aws_environment_max_hours command, and the copy-at-connect limits
(no CA before the first connect, removed credentials linger in a running
task, no setup re-run after a re-clone), documented in CLAUDE.md too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:59:37 -08:00
shadowdaoandClaude Opus 5.5 5388db487e fix(aws): never run two workspace-status polls at once
A read in flight blocks the visibilitychange restart, so hiding and showing
the window mid-read no longer starts a second poll chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:59:37 -08:00
shadowdaoandClaude Opus 5.5 3bc2b41b70 test(aws): pin the 168-hour lifetime cap across Rust, TypeScript and HCL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:27 -08:00
shadowdaoandClaude Opus 5.5 f3d542ba60 fix(aws): build the pricing client inside the cost estimate's 8 s bound
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:27 -08:00
shadowdaoandClaude Opus 5.5 66a9993e69 fix(aws): retry a secret write while its deletion finishes
After a timed-out replace (secret force-deleted, not made again) the next
start retries the create/put for up to 30 s, then says to start again in a
minute; no more advice to restore the secret in the console.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:26 -08:00
shadowdaoandClaude Opus 5.5 56acab1bf2 fix(aws): register the workspace follower before spawning it
begin_follow runs in spawn_follow and the token is passed in, so a quick
Stop→Start can't leave the old follower registered last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:26 -08:00
shadowdaoandClaude Opus 5.5 f87edbc5df fix(aws): build the gateway sidecar payload only when preparing a task
Attaching to a running task no longer needs the provider key or gateway
models; the payload is built lazily on the prepare path only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:56:26 -08:00
shadowdaoandClaude Opus 5.5 34f8a4161d fix(container): copy-at-connect setup waits for the connect; distinct WIP refs; atomic CA swap
- start_clone_repos waits for the first connect when a setup script exists,
  not only when there are repositories, so the script sees the CA, SSH keys
  and Git credentials.
- wip_ref appends 8 hex of sha256(raw folder) when sanitising changed the
  name, so `a b` and `a_b` never share a WIP branch.
- triple-c-apply-connect moves new CA files to a temporary name and swaps
  them in; a failed move keeps the CA installed before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:50:55 -08:00
shadowdaoandClaude Sonnet 5.5 d63f484edc docs(aws): restore spec sections lost in the phase 6 docs edit
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 10:38:25 -08:00
shadowdaoandClaude Sonnet 5.5 9f0c482222 docs(aws): phase 6
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 10:37:21 -08:00
shadowdaoandClaude Opus 5.5 3cd8542670 test(aws): live phase 6 checks
a_phase6_workspace_round_trips: one environment (max_workspace_hours 1)
and one project with EFS storage, a setup script, copy-at-connect with a
CA file, a port mapping and the gateway sidecar, two task starts, then
removal with discard. Passed live in us-west-2 (results in aws/README.md
and the spec). The phase-5 setup moves into deploy_for_workspace_user;
aws_host::ensure_started_with takes the sidecar's provider key from the
caller (the live tests have no keychain). scripts/aws-e2e-workspace.sh
runs TRIPLEC_E2E_TEST when set. The guard's task-definition retirement
listed nothing (ECS familyPrefix is a whole family name); it now filters
the full list by prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:34:46 -08:00
shadowdaoandClaude Opus 5.5 904547b2f1 feat(aws): estimated hourly cost per workspace
Q5: aws/pricing.rs reads on-demand Fargate rates (x86 and ARM vCPU-hours,
GB-hours, ephemeral storage GB-hours) from the Price List API in us-east-1
(AmazonECS; regionCode, termType=OnDemand, usagetype CONTAINS Fargate),
caches them per region for 24 h under <data_dir>/triple-c/aws-pricing/,
and estimates a config's hourly cost (ephemeral storage billed above
20 GiB; the gateway sidecar adds nothing). aws_cost_estimate(connection_id,
region, config) never errors: denied, offline or slow (8 s) is None,
shown as "Estimate unavailable"; a failed region is not re-asked for
5 minutes and a stale cache still answers.

pricing:GetProducts on * joins workspace-user-tasks.json with its check,
a policy test, the README and a live test (passed). HostSection shows the
estimate while configuring, Overview while running, with the Spot and EFS
notes. Fixture: a recorded us-west-2 GetProducts answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:14:06 -08:00
shadowdaoandClaude Opus 5.5 2857b4ae7d feat(aws): optional LLM gateway sidecar
A non-essential 'gateway' container runs the digest-pinned LiteLLM image
next to the workspace (Q7): its secrets are exactly the three
TRIPLEC_GATEWAY_* keys, the config uses os.environ/ references (no key
in it), /bin/sh writes it to a 0600 file and execs LiteLLM on
127.0.0.1:4000. The workspace's ANTHROPIC_BASE_URL/AUTH_TOKEN point at
it with a master key minted per task start; under copy-at-connect that
token and the gateway keys stay in the secret (R1). Needs the
OpenAI-compatible backend and a gateway model (validated on save, start
and in the UI). Requests with the toggle off are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:57:07 -08:00
shadowdaoandClaude Opus 5.5 45aef8da7d fix(aws): serialise and diff port-mapping forwards; refuse the gateway sidecar's port
Opens hold a per-project lock and re-read the stored mappings, so
overlapping saves converge on the last one without false 'in use' errors.
Unchanged forwards keep their live connections. The sidecar's 4000 is
refused while it runs (mappings reach workspace loopback). Stored rows
that would be refused no longer block unrelated saves; they are skipped
with a warning at connect. Port-mapping logs get their own prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:45:37 -08:00
shadowdaoandClaude Opus 5.5 08c779ad36 feat(aws): forward port mappings to AWS workspaces
Each TCP mapping of an AWS project becomes a loopback-only listener
carried into the task over socat-over-attach (the auth bridge's
PortForward, generalised to separate host and container ports; mappings
skip the bridge's first-byte/web gate). One set per project, opened after
every connect and on a save while connected, closed with the tunnel
(stop, remove, task death, exit). A failing mapping is a workspace-warning;
UDP and reserved ports are refused on save. The Config tab offers the
editor on AWS, TCP only, "Reachable only from this machine (localhost)".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:00:56 -08:00
shadowdaoandClaude Opus 5.5 262ea041c8 fix(aws): create the secret before reading it; one staging dir per connect
- Copy-at-connect tries CreateSecret first and reads the current value only
  when the secret exists, so a first start never reads a missing secret
  (which the owner-tag condition may refuse).
- triple-c-apply-connect: --prepare makes and prints a per-connect nonce
  dir; the app uploads into it and applies exactly that dir, so concurrent
  connects never mix files. CA files move to a fixed path so an unchanged
  CA is not reinstalled.
- connect.env parsing kept a trailing '=' (base64 padding) — it was dropped.
- Pending-deletion errors from GetSecretValue are reported as such; message
  and doc-comment fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:51:14 -08:00
shadowdaoandClaude Opus 5.5 820bfab25c fix(aws): copy-at-connect review fixes — lock, ~/.ssh off EFS, secret history
- triple-c-apply-connect: the background re-clone closes the apply lock's
  fd, so a later connect never waits on it; --prepare takes the lock.
- R6: under copy-at-connect Hybrid/EFS no longer link ~/.ssh to EFS; the
  copied keys vanish with the task (UI says so, and that old keys on EFS
  stay until removed).
- R7: a copy-at-connect start that finds credentials still in the secret
  force-deletes it and creates it again (retrying ~30 s while AWS finishes),
  so no AWSPREVIOUS version keeps them.
- TRIPLE_C_CUSTOM_ENV (unsalted fingerprint) left out of the secret.
- Terminals get only the credential pairs (no file reads; keychain off the
  runtime), so a bad SSH/CA file never drops the API keys.
- ConnectBundle values are Zeroizing; UI copy updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:43:12 -08:00
shadowdaoandClaude Opus 5.5 8510797c22 feat(aws): copy credentials at connect instead of storing them
Copy-at-connect (phase 6, Q2): with secrets_delivery copy_at_connect the
project secret keeps settings, repos and the agent token but drops
TRIPLEC_FILES and every credential pair (SECRET_ENV_KEYS and all custom
variables), marking TRIPLEC_CREDENTIALS=connect. After every connect (start,
attach, background re-attach) the app uploads the SSH/CA files and a
connect.env as root into /dev/shm/triple-c and runs the new
triple-c-apply-connect: CA install as root, SSH merge and git credential
helper as claude, a `connected` marker the first clone waits for, and a
background re-clone of failed repos only. Terminals add the pairs to their
own ExecSpec env. A failed push is a workspace-warning.

The entrypoint's CA, SSH, git-credential, Hybrid-home and clone functions
move to container/entrypoint-lib.sh (sourced first; defines only), so both
scripts run the same code; Docker behaviour is unchanged. HostSection gets
the Credentials choice with its warnings (and the gateway note, R1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:32:09 -08:00
shadowdaoandClaude Opus 5.5 9c888caf13 fix(aws): follow clone and setup in the background, after the start
Ruling R5: the start ends after Connecting (Running, lock released) and a
background follower (cancelled on Stop, Remove, tunnel close, app exit)
reports only a failure it saw or one on a task this start ran. A stale
Starting/Stopping AWS project loads as Error, and reconcile also checks a
Stopped one that still has a task ARN, so a crash never hides a live task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:09:51 -08:00
shadowdaoandClaude Opus 5.5 6b62a2a241 feat(aws): follow clone and setup when a workspace starts
The payload carries TRIPLEC_SETUP_SCRIPT and TRIPLEC_MAX_LIFETIME_MINUTES
(environment hours x 60 when capped); the 64 KiB error names the largest
parts. After Connecting, the start follows triple-c-workspace-status through
the agent (background exec, 2 s, progress on change, 35 min cap, quiet stop
when the tunnel goes). A failed clone/setup is a workspace-warning event and
a warning toast; Overview shows an AWS status card via aws_workspace_status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:01:10 -08:00
shadowdaoandClaude Opus 5.5 431cdc83cb fix(container): one WIP branch per run and folder, setup chain records failed, lifetime read as decimal
Two folders sharing a remote no longer force-push over each other's
triple-c/wip/<run-id>; the folder is part of the branch name (sanitised,
check-ref-format validated, hash fallback). A setup chain that dies with
the setup still running records failed so the idle stop is not blocked.
TRIPLEC_MAX_LIFETIME_MINUTES 00 means no cap and 08 means 8. The run record
is written after the agent-token block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 07:52:12 -08:00
shadowdaoandClaude Opus 5.5 618cac1a49 feat(container): setup script, lifetime cap and one WIP branch per run
TRIPLEC_SETUP_SCRIPT runs as claude after the background clone (bash -e,
stdin /dev/null, 30 min timeout) with status/log in /run/triple-c-setup;
the idle watcher counts it as busy and enforces TRIPLEC_MAX_LIFETIME_MINUTES
(best-effort WIP push, marker lifetime, exit 76 = StopKind::LifetimeCap).
WIP pushes go to one triple-c/wip/<run-id> branch per task run, skipping an
unchanged tree. New triple-c-workspace-status prints clone + setup JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 07:44:20 -08:00
shadowdaoandClaude Opus 5.5 ad447555d9 fix(aws): clone through scratch and never delete a final path
clone_repos now clones into /workspace/.triple-c-clone/<folder> and renames
into place only on success, emptying scratch at each start; nothing at a
final path is ever deleted by the clone code. Replaces the in-repo
triplec.cloning marker, whose failed unset could get a working repository
deleted on the next start. triple-c-wip-push skips the scratch folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:49:57 -08:00
shadowdaoandClaude Opus 5.5 5477c2cea5 feat(aws): EFS storage mode
EFS storage is Hybrid plus /workspace on a second access point of the
user's own file system, rooted at /triple-c-workspaces/<owner_key>/<project>
(a sibling of the home roots) and mounted at /workspace. The workspace
access point is found by root + owner + project tags or created, waited
for before RunTask, recorded store-owned, kept when switching to Hybrid or
Ephemeral, and deleted with the home one by a discarding removal. Hybrid
and Ephemeral task definitions are byte-identical to before (pinned
fingerprints).

The entrypoint links home state for efs as for hybrid, warns when
/workspace is not mounted, and marks a clone in progress in its own
.git/config so a clone a killed task cut short is redone on the next start
while existing repositories are still skipped.

The EFS segment is enabled in HostSection, with a note when /workspace
files remain on an access point after switching away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:44:17 -08:00
shadowdaoandClaude Sonnet 5.5 cbaf6209fd feat(aws): environments can cap workspace lifetime
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 21:33:54 -08:00
shadowdaoandClaude Opus 5.5 34a6edd413 feat(aws): phase 6 project settings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:29:29 -08:00
shadowdaoandClaude Opus 5.5 6497dd84e9 docs(aws): phase 6 plan — lifetime cap enforced inside the workspace
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:24:51 -08:00
shadowdaoandClaude Opus 5.5 fd0d51219f docs(aws): phase 6 plan
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:56:25 -08:00
jknapp 03f62b093f Merge pull request 'AWS host phase 5: Fargate workspaces' (#82) from feat/aws-fargate-workspaces into main
Secret Scan / scan (push) Successful in 5s
Build Container / build-container (push) Successful in 1m49s
2026-11-15 21:51:12 +00:00
102 changed files with 11938 additions and 998 deletions

No files matched your search

+103 -8
View File
@@ -284,7 +284,7 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
users have migrated.
- **`host/`** — Where a project's container runs, and the only way into it. `ContainerTarget`
(`Docker`, and `Agent` — reached through the in-container `triple-c-agent` by `agent.rs`, per
`docs/superpowers/specs/2026-11-13-triple-c-agent-protocol.md`; AWS planned —
`docs/superpowers/specs/2026-11-13-triple-c-agent-protocol.md`; AWS host, designed in
`docs/superpowers/specs/2026-11-13-aws-ecs-host-design.md`)
wraps execs, file transfer and `is_running`; `sessions.rs` holds `ExecSessionManager`.
`ssm.rs` is the AWS transport to an `Agent` target: one SSM port-forward per task, run through
@@ -300,6 +300,17 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
every tunnel (`for_project` is `None` while none is open; `for_project_waiting` covers the
background re-attach after an app restart). AWS SDK calls stay in `aws/` (`runtime.rs`,
`tasks.rs`, `workspace.rs`).
`port_forwards.rs` is an AWS project's port mappings: each TCP mapping is a **loopback-only**
auth-bridge `PortForward::bind_mapping` over socat-over-attach. Two deliberate differences from
Docker: it reaches the workspace's **loopback** (Docker publishes to eth0), so the agent port,
the browser-view ranges and — with `gateway_sidecar` on — the sidecar's 4000 are refused; and it
has **no gate** (no first-byte wait, no cross-site check, unlike the bridge). Validated on save
(rows the save adds/edits; everything when newly on AWS or the sidecar is switched); a stored row
that would be refused is skipped with a warning at connect. Opens are serialised per project and
re-read the stored mappings under that lock; unchanged forwards are kept with their connections.
Opened after every connect and on a save while connected; closed by every `tunnels::close*`
path. A failing mapping is a `workspace-warning` (`topic: "ports"`). An idle listener makes no
agent request; a connection carrying traffic is activity (keeps the workspace from idling).
`agent.rs` reuses `docker/exec.rs`'s `OneshotOutput`, host-file reader and uid probe so the two
variants cannot drift; an `Agent` target's token never appears in `Debug` or error text.
**Container-interior code must go through `ContainerTarget`, never `docker::exec`, bollard or
@@ -355,9 +366,24 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
through the agent, closes the project's terminals and auth bridge (so nothing is written after the
push's snapshot), may push first (under an in-container `timeout`, since the agent does not kill an
exec the client gave up on), then writes `pushed` or `discard` to `/run/triple-c/stop-reason`
(root-owned; the entrypoint's TERM hook reads it, and the idle watcher writes `idle`) so the hook
(root-owned; the entrypoint's TERM hook reads it, and the idle watcher writes `idle` or `lifetime`) so the hook
doesn't push twice; the marker is cleared if StopTask fails. Claude Code's sandbox is forced off in
the generated settings (bubblewrap can't create namespaces on Fargate, verified live).
**LLM gateway sidecar** (`gateway_sidecar`, Q7): a second, `essential: false` container `gateway`
(`docker::gateway::registry_image_ref`, digest-pinned, amd64+arm64; `memoryReservation` 512; no
`environment`, no `dependsOn`) whose `secrets` are exactly `TRIPLEC_GATEWAY_{CONFIG,API_KEY,MASTER_KEY}`
— the workspace container gets every key but those. The config is `sidecar_config` (Docker's
`render_config` with `os.environ/…` key references, so it holds no key); `/bin/sh -c` writes it to a
0600 `/tmp/litellm.yaml` and `exec`s LiteLLM on **127.0.0.1:4000** only (the image's own entrypoint
can't write a file). The master key is **random per task start** (the Docker gateway's never leaves
the machine); the payload overrides `ANTHROPIC_BASE_URL`/`ANTHROPIC_AUTH_TOKEN` with the sidecar's,
and under copy-at-connect that token stays in the secret too (R1; terminals don't override it).
Log streams: `<prefix>/gateway/<task-id>` (the tail reads `workspace` only). Needs the
OpenAI-compatible backend, a gateway model and ≥ 1 vCPU / 2 GiB; off = the request is unchanged.
**Cost estimate** (`pricing.rs`, Q5): `pricing:GetProducts` (us-east-1, `AmazonECS`, filters in
`FILTERS`) → per-region x86/ARM rates cached at `<data_dir>/triple-c/aws-pricing/<region>.json` for
24 h; `aws_cost_estimate(connection_id, region, config)` never errors — denied/offline/slow (8 s) is
`None` ("Estimate unavailable"), a failed region isn't re-asked for 5 min, a stale cache still answers.
**Environments are OpenTofu modules** (`aws/terraform/{environment,user-onboard,permissions-boundary}`;
spec D2–D10). `tofu_modules.rs` embeds them and is the **only** place the shared names are built
(environment, `/triple-c/environments/<env>` record, owner key — `sha256("<region>:<aws:userid>")`,
@@ -396,20 +422,89 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
every terminal's env and the files hold private keys). Repos clone in the background after the agent
starts (terminals are usable before they arrive). Hybrid storage mounts EFS at `/mnt/triple-c-home`
and symlinks `~/.claude`, `~/.claude.json`, `~/.ssh` into it (mounting over `/home/claude` would hide
the image's tooling); root never follows those links outside the mount (`aws_hybrid_ssh_dir`). On
TERM the hook pushes unsaved work to a `triple-c/wip/<stamp>` branch via a temporary index (the
user's index is untouched; needs the task definition's `stopTimeout` raised, 120 s) unless the stop
marker says the app handled it. The idle watcher runs as root under a supervisor and signals the
the image's tooling); root never follows those links outside the mount (`aws_hybrid_ssh_dir`). EFS storage
is Hybrid plus a second access point (root `/triple-c-workspaces/<owner_key>/<project>`, a sibling of
the home roots) mounted straight at `/workspace`; the entrypoint links home state the same way
(`aws_storage_links_home`) and never creates or chowns `/workspace`. Because `/workspace` then
outlives the task, `clone_repos` clones into `/workspace/.triple-c-clone/<folder>` and renames
into place only on success; it never deletes anything at a final path, empties scratch at each
start, and `triple-c-wip-push` skips scratch.
Switching EFS → Hybrid/Ephemeral keeps `workspace_access_point_id` (store-owned) until "Remove AWS
resources" with discard deletes both access points. On
TERM the hook pushes unsaved work to the run's `triple-c/wip/<run-id>-<folder>` branch via a
temporary index (the user's index is untouched; needs the task definition's `stopTimeout` raised,
120 s) unless the stop marker says the app handled it.
**One WIP branch per task run and folder:** `aws_prepare_run` (right after the agent-token block)
writes `/run/triple-c/run-id` (`<UTC stamp>-<task id 8 | local>`, task id from ECS task metadata)
and `start-epoch`; `/run/triple-c` is root 0711 (the stop guard keeps that mode — at 0700 `claude`
could not read the run id). The folder is sanitised to `[A-Za-z0-9._-]` (a name that changed gets
`-<8 hex of sha256(raw name)>`, so `a b` and `a_b` never share a ref) and the ref checked with
`git check-ref-format`; later pushes force (`+`) only that run's own branch, and a tree equal to
`/run/triple-c/wip-last/<folder>` (claude-owned dir, written after a successful push) is skipped.
No usable run id (Docker) keeps a per-push `<stamp>-<folder>`, never forced.
**Setup script** (`TRIPLEC_SETUP_SCRIPT`, unset before the snapshot like `TRIPLEC_FILES`): written
to `/run/triple-c-setup/script` (root 0755 dir; root opens `log` and hands the descriptor over, so
claude writes nothing there), run as `claude`, `bash -e`, stdin `/dev/null`,
`timeout -k 10 1800`, chained after the background clone's `done` in the same background
subshell; `status` is `running` from the start until `ok` / `failed <code>` (a chain that dies
early writes `failed`, so the idle stop is never blocked). `triple-c-workspace-status` (python,
no secrets) prints `{clone:{done,repos},setup:{state,code,tail}}`. Edited in HostSection's
"Setup script" textarea (saved on blur, blank → `null`; the 32 KiB UTF-8 limit is checked in TS
with Rust's own message, `setupScriptProblem`).
**The start does not wait for them** (it ends after "Connecting…": Running, lock released);
`aws_host::spawn_follow` follows in a background task (`host/aws_host/status.rs`: background exec
every 2 s, at most 35 min, holds no project lock; ended by `tunnels::cancel_follow` on Stop/Remove
and by any tunnel close or app exit). Its only output is a `workspace-warning` event (warning
toast) for a failure it saw happen or one on a task this start ran; progress shows in Project
Home's Overview card, which polls `aws_workspace_status` every 5 s while Running and visible.
At load, a Starting/Stopping AWS project becomes Error (not Stopped — a crash mid-start may have
left a billed task), and `reconcile_aws_projects` also checks a Stopped one still holding a task ARN.
**Lifetime cap** (`TRIPLEC_MAX_LIFETIME_MINUTES`, advisory — sudo can kill it): the idle watcher
(started when idle stop *or* a non-zero cap is set) pushes best effort, records `lifetime` and
sends TERM; the hook exits 76 (`aws::tasks::LIFETIME_EXIT_CODE` → `StopKind::LifetimeCap` →
`lifetime` toast). HostSection shows a non-zero cap ("stops workspaces after N h (advisory)") from
`aws_environment_max_hours` (reads the environment record; `None` when it can't, never an error).
The idle watcher runs as root under a supervisor and signals the
entrypoint's own PID (not PID 1, which is docker-init), so idle and ECS stops share one trap.
Idle = the agent's `last_activity_unix` older than N minutes (open-but-silent attaches/tunnels
don't count; requests with `X-Triple-C-Background: 1` — the auth bridge's and the file viewer's polls — never do),
no `claude`, no scheduler job, no running clone, and no unsaved git work: with the WIP push on the
no `claude`, no scheduler job, no running clone or setup script, and no unsaved git work: with the WIP push on the
watcher pushes *before* signalling and stays up if that fails; with it off, dirty repos keep the
workspace up; activity during the watcher's push sends it back to watching. A repo the background
clone is still writing (`/run/triple-c-clone/repos` last says `cloning`, no `done`) is skipped by
`triple-c-wip-push` (dry run: `"cloning": true`). The stop trap is set before any background work
starts. An idle stop exits 75 (`aws::tasks::IDLE_EXIT_CODE`) so the death watch can tell it
from a crash (`classify_stop` → the `ContainerExited` toast).
**Copy-at-connect** (`secrets_delivery: copy_at_connect`, Q2): the secret keeps settings, repos and
the agent token but no `TRIPLEC_FILES` and no credential pairs (`SECRET_ENV_KEYS` + every custom
variable; `TRIPLEC_CREDENTIALS=connect` instead — the gateway sidecar's `TRIPLEC_GATEWAY_*` keys
stay, R1). After every connect (start, attach, background re-attach) `aws_host::connect`
uploads the files and `connect.env` (`KEY=<base64>` lines) as root into this connect's own
`/dev/shm/triple-c/connect/<nonce>/` (root 0700 tmpfs; `--prepare` prints the nonce, so two
connects at once never mix) and runs `triple-c-apply-connect <nonce>`: CA install as root, SSH merge and git
credential helper **as `claude`** (root never walks a live `~/.ssh`), `/run/triple-c/connected`,
then a background re-clone of `failed` repos only (GIT_TOKEN in that clone's env only). The
first clone waits up to 5 min for `connected` (`aws_wait_connect`) — when there are repos **or a
setup script** (the script runs after the clone and needs the CA, SSH and Git credentials). The
CA files are swapped in (`connect-ca.new` → `connect-ca`, old removed last), so a failed move
keeps the CA installed before. Terminals the app opens add
the pairs to their own `ExecSpec.env` (`aws_host::terminal_env`, pairs only — no file read);
nothing reaches the agent's snapshot, so cron/scheduled jobs and the setup script never get them.
A failed push is a `workspace-warning`. Hybrid/EFS do **not** link `~/.ssh` to EFS under
copy-at-connect (R6: keys vanish with the task). When a copy-at-connect start finds the secret
already there (CreateSecret's ResourceExists; a new project's secret is never read first) and it
`holds_credentials`, it is force-deleted and created again (R7: `PutSecretValue` would keep the
old values as AWSPREVIOUS); `TRIPLE_C_CUSTOM_ENV` is left out too. A replace whose re-create
times out (AWS still finishing the forced delete) leaves no secret; every secret write retries
while the name is pending deletion (≤ 30 s), then says to start again in a minute.
**Copy-at-connect limits** (also listed in HostSection's notes): there is no corporate CA until
the first connect (Claude Code's self-update may fail behind a TLS-inspecting proxy until then);
SSH keys, a Git token or variables removed from the project stay in the running task until it
stops (a connect only adds/overwrites them; a removed CA does go at the next connect); the setup script is not re-run
after a later connect's re-clone of a failed repo.
The re-clone closes the apply lock's fd (a later connect never waits for it); `--prepare` takes it.
The functions both scripts use live in `container/entrypoint-lib.sh`
(`/usr/local/lib/triple-c/`), sourced by the entrypoint first; it only defines things.
**The `TRIPLEC_` env prefix is reserved** on both hosts (`RESERVED_ENV_PREFIXES`; `workspace.rs`
drops custom variables in it): the entrypoint's settings live there.
- **Browser runtime libraries are baked in; browser *binaries* are not.** A layer runs
@@ -431,7 +526,7 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
approach converges on the worst state: a 400 MB browser present with its libraries gone.
- The layer sits immediately after Node (npx is its only prerequisite) and well above the shim
`COPY`s, so editing a shim does not re-run a multi-hundred-megabyte apt install.
- **`entrypoint.sh`** — UID/GID remapping to match host user, SSH key setup (`sync_host_ssh` *merges* the host dir into `~/.ssh`; never wipe it — container-made keys live there on the home volume), git config, docker socket permissions, Claude Code settings.json injection, then stays resident **as root** waiting on a root-owned
- **`entrypoint.sh`** (+ `entrypoint-lib.sh`, the CA/SSH/git-credential/clone functions it sources) — UID/GID remapping to match host user, SSH key setup (`sync_host_ssh` *merges* the host dir into `~/.ssh`; never wipe it — container-made keys live there on the home volume), git config, docker socket permissions, Claude Code settings.json injection, then stays resident **as root** waiting on a root-owned
`triple-c-keepalive` sleep (respawned if killed; SIGTERM trap so `docker stop` is immediate).
**Never hand the container's lifetime to a process the `claude` user owns** — it used to end in
`exec su … claude -c "exec sleep infinity"`, and any `pkill sleep` / `kill -9 -1` a Claude
+26
View File
@@ -488,6 +488,31 @@ dependencies = [
"tracing",
]
[[package]]
name = "aws-sdk-pricing"
version = "1.125.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bb09af014cbcee1ad1753d54d70c4afd7565dfd7ca4d84d8b702292462fe80f"
dependencies = [
"arc-swap",
"aws-credential-types",
"aws-runtime",
"aws-smithy-async",
"aws-smithy-http",
"aws-smithy-json",
"aws-smithy-observability",
"aws-smithy-runtime",
"aws-smithy-runtime-api",
"aws-smithy-schema",
"aws-smithy-types",
"aws-types",
"bytes",
"fastrand",
"http",
"regex-lite",
"tracing",
]
[[package]]
name = "aws-sdk-s3"
version = "1.159.0"
@@ -7446,6 +7471,7 @@ dependencies = [
"aws-sdk-efs",
"aws-sdk-iam",
"aws-sdk-kms",
"aws-sdk-pricing",
"aws-sdk-s3",
"aws-sdk-secretsmanager",
"aws-sdk-ssm",
+2
View File
@@ -70,6 +70,8 @@ aws-sdk-iam = { version = "1.136.0", default-features = false, features = ["defa
aws-sdk-secretsmanager = { version = "1.128.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-efs = { version = "1.125.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
aws-sdk-cloudwatchlogs = { version = "1.165.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
# aws/pricing.rs: the Fargate rates behind the hourly cost estimate (Q5).
aws-sdk-pricing = { version = "1.125.0", default-features = false, features = ["default-https-client", "rt-tokio", "behavior-version-latest"] }
# Named by src/aws/connection.rs: the credentials-provider trait (its profile
# wrapper) and the profile-file set (aws-config's alias for it is deprecated).
# Both are the versions aws-config already resolves.
+3
View File
@@ -20,6 +20,8 @@
"allow-stop-project-container",
"allow-check-unsaved-work",
"allow-aws-task-log",
"allow-aws-workspace-status",
"allow-aws-environment-max-hours",
"allow-remove-aws-resources",
"allow-rebuild-project-container",
"allow-reconcile-project-statuses",
@@ -69,6 +71,7 @@
"allow-aws-connection-sso-login",
"allow-check-aws-permissions",
"allow-aws-policy-document",
"allow-aws-cost-estimate",
"allow-detect-aws-environments",
"allow-deploy-aws-environment",
"allow-update-aws-environment",
+1 -1
View File
@@ -1 +1 @@
{"__app-acl__":{"default_permission":null,"permissions":{"allow-acquire-claude-token":{"identifier":"allow-acquire-claude-token","description":"Enables the acquire_claude_token command without any pre-configured scope.","commands":{"allow":["acquire_claude_token"],"deny":[]}},"allow-add-aws-keys-connection":{"identifier":"allow-add-aws-keys-connection","description":"Enables the add_aws_keys_connection command without any pre-configured scope.","commands":{"allow":["add_aws_keys_connection"],"deny":[]}},"allow-add-aws-profile-connection":{"identifier":"allow-add-aws-profile-connection","description":"Enables the add_aws_profile_connection command without any pre-configured scope.","commands":{"allow":["add_aws_profile_connection"],"deny":[]}},"allow-add-marketplace":{"identifier":"allow-add-marketplace","description":"Enables the add_marketplace command without any pre-configured scope.","commands":{"allow":["add_marketplace"],"deny":[]}},"allow-add-marketplace-gh-host-account":{"identifier":"allow-add-marketplace-gh-host-account","description":"Enables the add_marketplace_gh_host_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_gh_host_account"],"deny":[]}},"allow-add-marketplace-token-account":{"identifier":"allow-add-marketplace-token-account","description":"Enables the add_marketplace_token_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_token_account"],"deny":[]}},"allow-add-project":{"identifier":"allow-add-project","description":"Enables the add_project command without any pre-configured scope.","commands":{"allow":["add_project"],"deny":[]}},"allow-add-scheduled-task":{"identifier":"allow-add-scheduled-task","description":"Enables the add_scheduled_task command without any pre-configured scope.","commands":{"allow":["add_scheduled_task"],"deny":[]}},"allow-apply-marketplace-now":{"identifier":"allow-apply-marketplace-now","description":"Enables the apply_marketplace_now command without any pre-configured scope.","commands":{"allow":["apply_marketplace_now"],"deny":[]}},"allow-apply-settings-import":{"identifier":"allow-apply-settings-import","description":"Enables the apply_settings_import command without any pre-configured scope.","commands":{"allow":["apply_settings_import"],"deny":[]}},"allow-aws-connection-identity":{"identifier":"allow-aws-connection-identity","description":"Enables the aws_connection_identity command without any pre-configured scope.","commands":{"allow":["aws_connection_identity"],"deny":[]}},"allow-aws-connection-sso-login":{"identifier":"allow-aws-connection-sso-login","description":"Enables the aws_connection_sso_login command without any pre-configured scope.","commands":{"allow":["aws_connection_sso_login"],"deny":[]}},"allow-aws-policy-document":{"identifier":"allow-aws-policy-document","description":"Enables the aws_policy_document command without any pre-configured scope.","commands":{"allow":["aws_policy_document"],"deny":[]}},"allow-aws-sso-refresh":{"identifier":"allow-aws-sso-refresh","description":"Enables the aws_sso_refresh command without any pre-configured scope.","commands":{"allow":["aws_sso_refresh"],"deny":[]}},"allow-aws-task-log":{"identifier":"allow-aws-task-log","description":"Enables the aws_task_log command without any pre-configured scope.","commands":{"allow":["aws_task_log"],"deny":[]}},"allow-build-gateway-image":{"identifier":"allow-build-gateway-image","description":"Enables the build_gateway_image command without any pre-configured scope.","commands":{"allow":["build_gateway_image"],"deny":[]}},"allow-build-image":{"identifier":"allow-build-image","description":"Enables the build_image command without any pre-configured scope.","commands":{"allow":["build_image"],"deny":[]}},"allow-build-stt-image":{"identifier":"allow-build-stt-image","description":"Enables the build_stt_image command without any pre-configured scope.","commands":{"allow":["build_stt_image"],"deny":[]}},"allow-cancel-aws-deployment":{"identifier":"allow-cancel-aws-deployment","description":"Enables the cancel_aws_deployment command without any pre-configured scope.","commands":{"allow":["cancel_aws_deployment"],"deny":[]}},"allow-cancel-claude-token":{"identifier":"allow-cancel-claude-token","description":"Enables the cancel_claude_token command without any pre-configured scope.","commands":{"allow":["cancel_claude_token"],"deny":[]}},"allow-cancel-marketplace-gh-login":{"identifier":"allow-cancel-marketplace-gh-login","description":"Enables the cancel_marketplace_gh_login command without any pre-configured scope.","commands":{"allow":["cancel_marketplace_gh_login"],"deny":[]}},"allow-check-aws-permissions":{"identifier":"allow-check-aws-permissions","description":"Enables the check_aws_permissions command without any pre-configured scope.","commands":{"allow":["check_aws_permissions"],"deny":[]}},"allow-check-browser-view-support":{"identifier":"allow-check-browser-view-support","description":Line truncated
{"__app-acl__":{"default_permission":null,"permissions":{"allow-acquire-claude-token":{"identifier":"allow-acquire-claude-token","description":"Enables the acquire_claude_token command without any pre-configured scope.","commands":{"allow":["acquire_claude_token"],"deny":[]}},"allow-add-aws-keys-connection":{"identifier":"allow-add-aws-keys-connection","description":"Enables the add_aws_keys_connection command without any pre-configured scope.","commands":{"allow":["add_aws_keys_connection"],"deny":[]}},"allow-add-aws-profile-connection":{"identifier":"allow-add-aws-profile-connection","description":"Enables the add_aws_profile_connection command without any pre-configured scope.","commands":{"allow":["add_aws_profile_connection"],"deny":[]}},"allow-add-marketplace":{"identifier":"allow-add-marketplace","description":"Enables the add_marketplace command without any pre-configured scope.","commands":{"allow":["add_marketplace"],"deny":[]}},"allow-add-marketplace-gh-host-account":{"identifier":"allow-add-marketplace-gh-host-account","description":"Enables the add_marketplace_gh_host_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_gh_host_account"],"deny":[]}},"allow-add-marketplace-token-account":{"identifier":"allow-add-marketplace-token-account","description":"Enables the add_marketplace_token_account command without any pre-configured scope.","commands":{"allow":["add_marketplace_token_account"],"deny":[]}},"allow-add-project":{"identifier":"allow-add-project","description":"Enables the add_project command without any pre-configured scope.","commands":{"allow":["add_project"],"deny":[]}},"allow-add-scheduled-task":{"identifier":"allow-add-scheduled-task","description":"Enables the add_scheduled_task command without any pre-configured scope.","commands":{"allow":["add_scheduled_task"],"deny":[]}},"allow-apply-marketplace-now":{"identifier":"allow-apply-marketplace-now","description":"Enables the apply_marketplace_now command without any pre-configured scope.","commands":{"allow":["apply_marketplace_now"],"deny":[]}},"allow-apply-settings-import":{"identifier":"allow-apply-settings-import","description":"Enables the apply_settings_import command without any pre-configured scope.","commands":{"allow":["apply_settings_import"],"deny":[]}},"allow-aws-connection-identity":{"identifier":"allow-aws-connection-identity","description":"Enables the aws_connection_identity command without any pre-configured scope.","commands":{"allow":["aws_connection_identity"],"deny":[]}},"allow-aws-connection-sso-login":{"identifier":"allow-aws-connection-sso-login","description":"Enables the aws_connection_sso_login command without any pre-configured scope.","commands":{"allow":["aws_connection_sso_login"],"deny":[]}},"allow-aws-cost-estimate":{"identifier":"allow-aws-cost-estimate","description":"Enables the aws_cost_estimate command without any pre-configured scope.","commands":{"allow":["aws_cost_estimate"],"deny":[]}},"allow-aws-environment-max-hours":{"identifier":"allow-aws-environment-max-hours","description":"Enables the aws_environment_max_hours command without any pre-configured scope.","commands":{"allow":["aws_environment_max_hours"],"deny":[]}},"allow-aws-policy-document":{"identifier":"allow-aws-policy-document","description":"Enables the aws_policy_document command without any pre-configured scope.","commands":{"allow":["aws_policy_document"],"deny":[]}},"allow-aws-sso-refresh":{"identifier":"allow-aws-sso-refresh","description":"Enables the aws_sso_refresh command without any pre-configured scope.","commands":{"allow":["aws_sso_refresh"],"deny":[]}},"allow-aws-task-log":{"identifier":"allow-aws-task-log","description":"Enables the aws_task_log command without any pre-configured scope.","commands":{"allow":["aws_task_log"],"deny":[]}},"allow-aws-workspace-status":{"identifier":"allow-aws-workspace-status","description":"Enables the aws_workspace_status command without any pre-configured scope.","commands":{"allow":["aws_workspace_status"],"deny":[]}},"allow-build-gateway-image":{"identifier":"allow-build-gateway-image","description":"Enables the build_gateway_image command without any pre-configured scope.","commands":{"allow":["build_gateway_image"],"deny":[]}},"allow-build-image":{"identifier":"allow-build-image","description":"Enables the build_image command without any pre-configured scope.","commands":{"allow":["build_image"],"deny":[]}},"allow-build-stt-image":{"identifier":"allow-build-stt-image","description":"Enables the build_stt_image command without any pre-configured scope.","commands":{"allow":["build_stt_image"],"deny":[]}},"allow-cancel-aws-deployment":{"identifier":"allow-cancel-aws-deployment","description":"Enables the cancel_aws_deployment command without any pre-configured scope.","commands":{"allow":["cancel_aws_deployment"],"deny":[]}},"allow-cancel-claude-token":{"identifier":"allow-cancel-claude-token","description":"Enables the cancel_claude_token cLine truncated
+1 -1
View File
@@ -1 +1 @@
{"default":{"identifier":"default","description":"Default capabilities for Triple-C. Every entry here is an IPC command a compromised webview can call directly, so the set is an enumeration of what `app/src` actually invokes — plugin and core grants verified against tauri 2.11.0's `PLUGINS` table in tauri's own `build.rs` rather than assumed from a plugin's `default` set, app-command grants (the bare `allow-*` entries) cross-checked by this crate's `build.rs` against `generate_handler!`. `core:default` in particular is NOT used: it is an alias for `core:{path,event,window,webview,app,image,resources,menu,tray}:default`, and `core:image:default` carries `allow-from-path`, whose handler (`tauri-2.11.0/src/image/plugin.rs:41` → `src/image/mod.rs:96`) is a bare `std::fs::read(path)` with no scope mechanism of any kind. Nothing imports `@tauri-apps/api/image`, so the whole plugin is dropped rather than scoped — there is nothing to scope it with. `core:menu` and `core:tray` are dropped for the same reason (no menu, no tray icon); `core:window` and `core:path` because nothing imports them; `core:resources:allow-close` because no frontend value is a `Resource`; and `core:event`'s `allow-emit`/`allow-emit-to` because the frontend only ever *listens* — every emit in this app originates in Rust. Three notes on what is deliberately kept or accepted: (1) `core:webview:allow-internal-toggle-devtools` is not called by `app/src` at all — it is called by Tauri's own injected `toggle-devtools.js`, which binds Ctrl/Cmd+Shift+I. Both that script and the command behind it are `#[cfg(any(debug_assertions, feature = \"devtools\"))]`, so this grant is a `tauri dev` convenience that does not exist in a release bundle. (2) `opener:allow-open-url` is **gone**. It could not be narrowed by host — `TerminalView`'s `WebLinksAddon` opens links Claude printed inside the container, which are arbitrary by construction, so a host allowlist would have deleted the feature rather than bounded it — and it was carried here as an accepted residual risk: a compromised webview could make the OS open an attacker-chosen http(s) URL, an outbound channel. That risk is now closed rather than recorded. Every host-browser open in the app goes through the `open_url_external` command in `url_open.rs`, which exists because the AppImage environment leaks into a cold-launched browser on Linux (triple-c#34) and which re-validates the URL in Rust — scheme allowlist, no embedded credentials, no control characters, length cap, ASCII asserted before `execvp`. On macOS and Windows that command reaches the same plugin as before, via `OpenerExt::open_url`, whose desktop implementation calls `crate::open::open` directly and is therefore not gated by this file at all (`tauri-plugin-opener-2.5.3/src/lib.rs:60`). The plugin stays a dependency for exactly that reason; what is removed is the webview's ability to reach it without passing the Rust validation. (3) `drag:allow-start-drag` is **gone**, together with the OS drag-out it existed for. It could not be scoped — `tauri-plugin-drag` takes the item paths from the caller and has no scope mechanism, so a compromised webview could call `startDrag({ item: ['~/.ssh/id_rsa'] })` against any host path the user can read — and it was carried as an accepted residual risk for one gesture. Drag-out was held back for separate hardening (see branch `hold/disk-and-dragout`) and the plugin is no longer a dependency. Getting a file *out* of a container is either \"Back up container\" on the project's Overview tab, which archives a tree through the Docker API, or the Files tab's per-row \"Save to host…\", which copies one file; getting one *in* is a drop on the Terminal or the Files tab's \"Upload…\". None of the four touches this permission. The Files tab's two are worth separating out here, because they are the only host-path commands in the app whose dialog is opened by **Rust** rather than by the webview — `pick_save_path` and `pick_files_to_upload` in `commands/file_commands.rs` drive `tauri-plugin-dialog` from the backend, so a compromised webview can ask for a picker and nothing more: it cannot name a host path as an *input* to either command. Be precise about the limit of that claim — host paths do still travel outward in error text (`Failed to create /home/j/Documents/x.txt.triple-c-part-1a2b3c4d: Permission denied`), including canonicalized ones, which disclose symlink targets. That is accepted; the app already hands the webview the project paths. What is closed is the direction that mattered — the webview naming where bytes go. That is the shape an earlier revision of this file named as the honest one if the Files tab ever regained host I/O, and it is the shape it regained it in. The `dialog:allow-open` / `dialog:allow-save` grants below are therefore *not* what those two use; they remain for the frontend pickers in Add Project, the Config tab's workspace and access sections, the CA-certificate fielLine truncated
{"default":{"identifier":"default","description":"Default capabilities for Triple-C. Every entry here is an IPC command a compromised webview can call directly, so the set is an enumeration of what `app/src` actually invokes — plugin and core grants verified against tauri 2.11.0's `PLUGINS` table in tauri's own `build.rs` rather than assumed from a plugin's `default` set, app-command grants (the bare `allow-*` entries) cross-checked by this crate's `build.rs` against `generate_handler!`. `core:default` in particular is NOT used: it is an alias for `core:{path,event,window,webview,app,image,resources,menu,tray}:default`, and `core:image:default` carries `allow-from-path`, whose handler (`tauri-2.11.0/src/image/plugin.rs:41` → `src/image/mod.rs:96`) is a bare `std::fs::read(path)` with no scope mechanism of any kind. Nothing imports `@tauri-apps/api/image`, so the whole plugin is dropped rather than scoped — there is nothing to scope it with. `core:menu` and `core:tray` are dropped for the same reason (no menu, no tray icon); `core:window` and `core:path` because nothing imports them; `core:resources:allow-close` because no frontend value is a `Resource`; and `core:event`'s `allow-emit`/`allow-emit-to` because the frontend only ever *listens* — every emit in this app originates in Rust. Three notes on what is deliberately kept or accepted: (1) `core:webview:allow-internal-toggle-devtools` is not called by `app/src` at all — it is called by Tauri's own injected `toggle-devtools.js`, which binds Ctrl/Cmd+Shift+I. Both that script and the command behind it are `#[cfg(any(debug_assertions, feature = \"devtools\"))]`, so this grant is a `tauri dev` convenience that does not exist in a release bundle. (2) `opener:allow-open-url` is **gone**. It could not be narrowed by host — `TerminalView`'s `WebLinksAddon` opens links Claude printed inside the container, which are arbitrary by construction, so a host allowlist would have deleted the feature rather than bounded it — and it was carried here as an accepted residual risk: a compromised webview could make the OS open an attacker-chosen http(s) URL, an outbound channel. That risk is now closed rather than recorded. Every host-browser open in the app goes through the `open_url_external` command in `url_open.rs`, which exists because the AppImage environment leaks into a cold-launched browser on Linux (triple-c#34) and which re-validates the URL in Rust — scheme allowlist, no embedded credentials, no control characters, length cap, ASCII asserted before `execvp`. On macOS and Windows that command reaches the same plugin as before, via `OpenerExt::open_url`, whose desktop implementation calls `crate::open::open` directly and is therefore not gated by this file at all (`tauri-plugin-opener-2.5.3/src/lib.rs:60`). The plugin stays a dependency for exactly that reason; what is removed is the webview's ability to reach it without passing the Rust validation. (3) `drag:allow-start-drag` is **gone**, together with the OS drag-out it existed for. It could not be scoped — `tauri-plugin-drag` takes the item paths from the caller and has no scope mechanism, so a compromised webview could call `startDrag({ item: ['~/.ssh/id_rsa'] })` against any host path the user can read — and it was carried as an accepted residual risk for one gesture. Drag-out was held back for separate hardening (see branch `hold/disk-and-dragout`) and the plugin is no longer a dependency. Getting a file *out* of a container is either \"Back up container\" on the project's Overview tab, which archives a tree through the Docker API, or the Files tab's per-row \"Save to host…\", which copies one file; getting one *in* is a drop on the Terminal or the Files tab's \"Upload…\". None of the four touches this permission. The Files tab's two are worth separating out here, because they are the only host-path commands in the app whose dialog is opened by **Rust** rather than by the webview — `pick_save_path` and `pick_files_to_upload` in `commands/file_commands.rs` drive `tauri-plugin-dialog` from the backend, so a compromised webview can ask for a picker and nothing more: it cannot name a host path as an *input* to either command. Be precise about the limit of that claim — host paths do still travel outward in error text (`Failed to create /home/j/Documents/x.txt.triple-c-part-1a2b3c4d: Permission denied`), including canonicalized ones, which disclose symlink targets. That is accepted; the app already hands the webview the project paths. What is closed is the direction that mattered — the webview naming where bytes go. That is the shape an earlier revision of this file named as the honest one if the Files tab ever regained host I/O, and it is the shape it regained it in. The `dialog:allow-open` / `dialog:allow-save` grants below are therefore *not* what those two use; they remain for the frontend pickers in Add Project, the Config tab's workspace and access sections, the CA-certificate fielLine truncated
@@ -416,6 +416,18 @@
"const": "allow-aws-connection-sso-login",
"markdownDescription": "Enables the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Enables the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-cost-estimate",
"markdownDescription": "Enables the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Enables the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-environment-max-hours",
"markdownDescription": "Enables the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Enables the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -434,6 +446,12 @@
"const": "allow-aws-task-log",
"markdownDescription": "Enables the aws_task_log command without any pre-configured scope."
},
{
"description": "Enables the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-workspace-status",
"markdownDescription": "Enables the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Enables the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -1370,6 +1388,18 @@
"const": "deny-aws-connection-sso-login",
"markdownDescription": "Denies the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Denies the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-cost-estimate",
"markdownDescription": "Denies the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Denies the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-environment-max-hours",
"markdownDescription": "Denies the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Denies the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -1388,6 +1418,12 @@
"const": "deny-aws-task-log",
"markdownDescription": "Denies the aws_task_log command without any pre-configured scope."
},
{
"description": "Denies the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-workspace-status",
"markdownDescription": "Denies the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Denies the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -416,6 +416,18 @@
"const": "allow-aws-connection-sso-login",
"markdownDescription": "Enables the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Enables the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-cost-estimate",
"markdownDescription": "Enables the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Enables the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-environment-max-hours",
"markdownDescription": "Enables the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Enables the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -434,6 +446,12 @@
"const": "allow-aws-task-log",
"markdownDescription": "Enables the aws_task_log command without any pre-configured scope."
},
{
"description": "Enables the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "allow-aws-workspace-status",
"markdownDescription": "Enables the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Enables the build_gateway_image command without any pre-configured scope.",
"type": "string",
@@ -1370,6 +1388,18 @@
"const": "deny-aws-connection-sso-login",
"markdownDescription": "Denies the aws_connection_sso_login command without any pre-configured scope."
},
{
"description": "Denies the aws_cost_estimate command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-cost-estimate",
"markdownDescription": "Denies the aws_cost_estimate command without any pre-configured scope."
},
{
"description": "Denies the aws_environment_max_hours command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-environment-max-hours",
"markdownDescription": "Denies the aws_environment_max_hours command without any pre-configured scope."
},
{
"description": "Denies the aws_policy_document command without any pre-configured scope.",
"type": "string",
@@ -1388,6 +1418,12 @@
"const": "deny-aws-task-log",
"markdownDescription": "Denies the aws_task_log command without any pre-configured scope."
},
{
"description": "Denies the aws_workspace_status command without any pre-configured scope.",
"type": "string",
"const": "deny-aws-workspace-status",
"markdownDescription": "Denies the aws_workspace_status command without any pre-configured scope."
},
{
"description": "Denies the build_gateway_image command without any pre-configured scope.",
"type": "string",
+1 -1
View File
@@ -583,7 +583,7 @@ async fn reconcile(
);
continue;
}
match PortForward::bind(container.clone(), port, family).await {
match PortForward::bind(container.clone(), port, port, family).await {
Ok(forward) => {
if st.conflicts.remove(&port).is_some() {
log::info!("Auth bridge: host port {} became available", port);
+97 -20
View File
@@ -119,7 +119,11 @@ impl Drop for AbortOnDrop {
/// also awaiting the aborted task, which guarantees the socket is closed before
/// the caller proceeds (important when a port is rebound right after).
pub struct PortForward {
/// The host loopback port.
pub port: u16,
/// The container loopback port it reaches. The auth bridge mirrors a
/// port onto the same number; a project's port mapping may not.
pub container_port: u16,
pub family: PortFamily,
pub bridged_at: String,
/// Why `[::1]` could not be taken alongside `127.0.0.1`, if it could not.
@@ -141,15 +145,40 @@ impl Drop for PortForward {
}
impl PortForward {
/// Bind `port` on the host loopback and start proxying into `container`.
/// Bind `host_port` on the host loopback and start proxying into
/// `container_port` on the container's loopback — the auth bridge's
/// forward, gated (see [`Gate::SignInCallback`]).
///
/// The bind happens before the task is spawned, so an already-taken port is
/// reported to the caller as an error rather than disappearing into a
/// background task.
pub async fn bind(
container: ContainerTarget,
port: u16,
host_port: u16,
container_port: u16,
family: PortFamily,
) -> Result<Self, std::io::Error> {
Self::bind_with(container, host_port, container_port, family, Gate::SignInCallback).await
}
/// As [`bind`](Self::bind), for a project's port mapping on a host with
/// no Docker port publishing (AWS): every connection is carried as is
/// (see [`Gate::None`]).
pub async fn bind_mapping(
container: ContainerTarget,
host_port: u16,
container_port: u16,
family: PortFamily,
) -> Result<Self, std::io::Error> {
Self::bind_with(container, host_port, container_port, family, Gate::None).await
}
async fn bind_with(
container: ContainerTarget,
port: u16,
container_port: u16,
family: PortFamily,
gate: Gate,
) -> Result<Self, std::io::Error> {
// SECURITY BOUNDARY: the host side binds loopback ONLY — 127.0.0.1 and
// ::1, never 0.0.0.0 / ::. Everything reachable through this socket is
@@ -176,8 +205,8 @@ impl PortForward {
// bridge reports itself healthy — a silent failure with no
// thread back to this line.
log::warn!(
"Auth bridge: bound 127.0.0.1:{} but not [::1]:{} ({}) — continuing with IPv4 only; \
a client that resolves localhost to ::1 without falling back will not reach it",
"{}: bound 127.0.0.1:{} but not [::1]:{} ({}) — continuing with IPv4 only; \
a client that resolves localhost to ::1 without falling back will not reach it", gate.label(),
port,
port,
e
@@ -193,11 +222,12 @@ impl PortForward {
}
};
let target = family.socat_target(port);
let task = tokio::spawn(accept_loop(container, port, target, v4, v6));
let target = family.socat_target(container_port);
let task = tokio::spawn(accept_loop(container, port, target, v4, v6, gate));
Ok(Self {
port,
container_port,
family,
bridged_at: chrono::Utc::now().to_rfc3339(),
ipv6_warning,
@@ -214,6 +244,30 @@ impl PortForward {
}
}
/// What an accepted connection must show before it is carried in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Gate {
/// The auth bridge: a client must speak within [`FIRST_BYTE_TIMEOUT`],
/// and a cross-site sub-resource request is refused
/// ([`web_request_verdict`]).
SignInCallback,
/// A port mapping the user configured: carried as is, like Docker's own
/// port publishing (which has no gate either) — a server-speaks-first
/// protocol such as MySQL or SMTP must work, so nothing waits for the
/// client's first byte.
None,
}
impl Gate {
/// The log prefix of a forward of this kind.
fn label(self) -> &'static str {
match self {
Gate::SignInCallback => "Auth bridge",
Gate::None => "Port mapping",
}
}
}
/// Accept on both loopback listeners until aborted. Dropping this future drops
/// the listeners (freeing the port) and the `JoinSet` (aborting live tunnels).
async fn accept_loop(
@@ -222,6 +276,7 @@ async fn accept_loop(
target: String,
v4: TcpListener,
v6: Option<TcpListener>,
gate: Gate,
) {
let mut conns: JoinSet<()> = JoinSet::new();
@@ -245,25 +300,35 @@ async fn accept_loop(
// client side is whatever can dial loopback, so a queue is
// just a slower way to run out of execs.
log::warn!(
"Auth bridge: refusing connection from {} to bridged port {} — \
{} concurrent connections already open on it",
"{}: refusing connection from {} to bridged port {} — \
{} concurrent connections already open on it", gate.label(),
peer,
port,
MAX_CONNECTIONS
);
continue;
}
log::debug!("Auth bridge: connection from {} to bridged port {}", peer, port);
log::debug!("{}: connection from {} to bridged port {}", gate.label(), peer, port);
let _ = stream.set_nodelay(true);
conns.spawn(tunnel_connection(
container.clone(),
target.clone(),
stream,
port,
));
match gate {
Gate::SignInCallback => conns.spawn(tunnel_connection(
container.clone(),
target.clone(),
stream,
port,
)),
Gate::None => conns.spawn(tunnel_labelled(
gate.label(),
container.clone(),
target.clone(),
stream,
port,
Vec::new(),
)),
};
}
Err(e) => {
log::warn!("Auth bridge: accept failed on port {}: {} — stopping listener", port, e);
log::warn!("{}: accept failed on port {}: {} — stopping listener", gate.label(), port, e);
return;
}
}
@@ -520,6 +585,18 @@ pub async fn tunnel_connection_with_prelude(
stream: TcpStream,
port: u16,
prelude: Vec<u8>,
) {
tunnel_labelled("Auth bridge", container, target, stream, port, prelude).await
}
/// [`tunnel_connection_with_prelude`], logging under `label`.
async fn tunnel_labelled(
label: &'static str,
container: ContainerTarget,
target: String,
stream: TcpStream,
port: u16,
prelude: Vec<u8>,
) {
let cmd = vec!["socat".to_string(), "-".to_string(), target.clone()];
@@ -531,7 +608,7 @@ pub async fn tunnel_connection_with_prelude(
Ok(e) => e,
Err(e) => {
log::warn!(
"Auth bridge: failed to open tunnel exec for port {} ({}): {}",
"{}: failed to open tunnel exec for port {} ({}): {}", label,
port,
target,
e
@@ -579,7 +656,7 @@ pub async fn tunnel_connection_with_prelude(
Ok(chunk) => chunk,
Err(_) => {
log::debug!(
"Auth bridge: bridged port {} idle for {}s — closing the tunnel",
"{}: bridged port {} idle for {}s — closing the tunnel", label,
port,
IDLE_TIMEOUT.as_secs()
);
@@ -597,14 +674,14 @@ pub async fn tunnel_connection_with_prelude(
}
Ok(ExecChunk::StdErr(message)) => {
log::debug!(
"Auth bridge: socat stderr for port {}: {}",
"{}: socat stderr for port {}: {}", label,
port,
String::from_utf8_lossy(&message).trim()
);
}
Ok(_) => {}
Err(e) => {
log::debug!("Auth bridge: tunnel stream error on port {}: {}", port, e);
log::debug!("{}: tunnel stream error on port {}: {}", label, port, e);
break;
}
}
+14
View File
@@ -75,6 +75,10 @@ pub struct EnvironmentInfo {
pub log_group: String,
pub network_mode: String,
pub tenancy: String,
/// Advisory lifetime cap for workspaces, in hours; 0 = none. Absent in
/// records written by module version 1.
#[serde(default)]
pub max_workspace_hours: u32,
}
fn field<'a>(obj: &'a Map<String, Value>, name: &str) -> Result<&'a Value, String> {
@@ -187,6 +191,15 @@ pub fn parse_environment_record(
return Err("tenancy is not strict".into());
}
let max_workspace_hours = match obj.get("max_workspace_hours") {
None => 0,
Some(v) => v
.as_u64()
.and_then(|v| u32::try_from(v).ok())
.filter(|v| *v <= super::tofu_modules::MAX_WORKSPACE_HOURS)
.ok_or_else(|| "max_workspace_hours is not a number from 0 to 168".to_string())?,
};
Ok(EnvironmentInfo {
schema: RECORD_SCHEMA as u32,
name: name.clone(),
@@ -199,6 +212,7 @@ pub fn parse_environment_record(
log_group: log_group.to_string(),
network_mode: network_mode.to_string(),
tenancy: "strict".to_string(),
max_workspace_hours,
})
}
@@ -44,6 +44,22 @@ fn without(name: &str, field: &str) -> String {
r.to_string()
}
#[test]
fn the_lifetime_cap_is_read_when_present() {
for hours in [0, 8, 168] {
let rec = with("dev", "max_workspace_hours", json!(hours));
let info = parse_environment_record("dev", &rec, ACCOUNT, REGION).unwrap();
assert_eq!(info.max_workspace_hours, hours);
}
}
#[test]
fn a_version_1_record_reads_as_update_available() {
use crate::aws::tofu_modules::ENVIRONMENT_MODULE_VERSION;
let v1 = parsed("dev", 1);
assert!(v1.template_version < ENVIRONMENT_MODULE_VERSION);
}
#[test]
fn untrusted_environment_records_are_rejected() {
let valid = parsed("dev", 1);
@@ -52,6 +68,7 @@ fn untrusted_environment_records_are_rejected() {
assert_eq!(valid.subnet_ids.len(), 2);
assert!(valid.assign_public_ip);
assert_eq!(valid.tenancy, "strict");
assert_eq!(valid.max_workspace_hours, 0, "a v1 record has no cap");
let other_account = "999999999999";
let secret = "s3cr3t-value-never-echoed";
@@ -182,6 +199,21 @@ fn untrusted_environment_records_are_rejected() {
with("dev", "template_version", json!(secret)),
"template_version",
),
(
"cap above a week",
with("dev", "max_workspace_hours", json!(169)),
"max_workspace_hours",
),
(
"cap not a number",
with("dev", "max_workspace_hours", json!(secret)),
"max_workspace_hours",
),
(
"negative cap",
with("dev", "max_workspace_hours", json!(-1)),
"max_workspace_hours",
),
];
for (what, value, field) in cases {
let err = parse_environment_record("dev", &value, ACCOUNT, REGION)
+580 -97
View File
@@ -624,6 +624,35 @@ async fn an_assumed_workspace_role_checks_itself_by_simulation() {
.await;
}
/// The cost estimate's call (Q5), made for real: a session holding only the
/// workspace files reads the region's Fargate rates from us-east-1 (the
/// parser must find both architectures), and one holding nothing is denied.
#[tokio::test]
#[ignore]
async fn a_workspace_role_reads_fargate_prices_and_a_bare_role_cannot() {
use super::pricing::{fetch_rates, SdkPricingApi, PRICING_REGION};
e2e(|e| async move {
let ws = e.tier_role("price", &[Tier::WorkspaceUser]).await;
let cfg = sdk_config_for_keys(&e.assume_keys(&ws).await, PRICING_REGION);
let rates = fetch_rates(&SdkPricingApi::new(&cfg), &e.region)
.await
.expect("workspace role reads prices");
let x86 = rates.x86_64.expect("x86 rates");
let arm = rates.arm64.expect("ARM rates");
eprintln!("{}: x86 {x86:?}, ARM {arm:?}", e.region);
assert!(x86.vcpu_hour > 0.0 && x86.gb_hour > 0.0 && x86.storage_gb_hour > 0.0);
assert!(arm.vcpu_hour < x86.vcpu_hour, "ARM is cheaper per vCPU");
let bare = e.role("noprice", None, &[]).await;
let cfg = sdk_config_for_keys(&e.assume_keys(&bare).await, PRICING_REGION);
let err = fetch_rates(&SdkPricingApi::new(&cfg), &e.region)
.await
.expect_err("a role without pricing:GetProducts");
assert!(err.contains("pricing:GetProducts"), "{err}");
})
.await;
}
/// The e2e profile itself (an account administrator): every check allowed.
/// For an SSO profile this resolves the `/aws-reserved/sso.amazonaws.com/`
/// role path through `iam:GetRole`.
@@ -1477,7 +1506,7 @@ mod environment {
deploy_refusal(&found, &name).expect("deploy is offered");
// Deploy, public network, as the environment admin only.
let vars = environment_vars(&name, &t.e.region, &NetworkChoice::Public).unwrap();
let vars = environment_vars(&name, &t.e.region, &NetworkChoice::Public, 0).unwrap();
let (_, outcome, denied) = t
.run(
ModuleKind::Environment,
@@ -1590,7 +1619,7 @@ mod environment {
// Update: the recorded network, the same module version: nothing
// to change.
let network = network_from_record(&info, None).expect("network from record");
let vars = environment_vars(&name, &t.e.region, &network).unwrap();
let vars = environment_vars(&name, &t.e.region, &network, 0).unwrap();
let (_, outcome, denied) = t
.run(
ModuleKind::Environment,
@@ -1685,7 +1714,7 @@ mod environment {
let (keys, cfg, who) = t.session(&admin).await;
t.ensure_bucket(&cfg, &who).await;
let name = format!("e2e{}", &uuid::Uuid::new_v4().simple().to_string()[..6]);
let vars = environment_vars(&name, &t.e.region, &NetworkChoice::PrivateNat).unwrap();
let vars = environment_vars(&name, &t.e.region, &NetworkChoice::PrivateNat, 0).unwrap();
let (_, outcome, denied) = t
.run(
ModuleKind::Environment,
@@ -1800,9 +1829,10 @@ mod environment {
for status in [aws_sdk_ecs::types::TaskDefinitionStatus::Active, aws_sdk_ecs::types::TaskDefinitionStatus::Inactive] {
let mut next = None;
loop {
// No `family_prefix`: ECS matches it as a whole family
// name, so the test's `triple-c-<env>-` matched nothing.
let Ok(out) = ecs
.list_task_definitions()
.family_prefix(prefix)
.status(status.clone())
.set_next_token(next.take())
.send()
@@ -1810,7 +1840,8 @@ mod environment {
else {
break;
};
defs.extend(out.task_definition_arns().iter().cloned());
let ours = format!(":task-definition/{prefix}");
defs.extend(out.task_definition_arns().iter().filter(|a| a.contains(&ours)).cloned());
next = out.next_token().map(str::to_string);
if next.is_none() {
break;
@@ -1910,6 +1941,124 @@ mod environment {
.unwrap_or_else(|err| panic!("exec `{script}`: {err}"))
}
/// What [`deploy_for_workspace_user`] set up.
struct Deployed {
region: String,
/// The environment's name.
name: String,
/// The workspace user's `aws:userid`.
owner: String,
okey: String,
fs_token: String,
/// The workspace user's profile connection.
conn: HostConnection,
ws_cfg: SdkConfig,
ws: CallerIdentity,
}
/// The workspace tests' common start: the environment (with
/// `max_workspace_hours`) deployed by the environment admin, a role
/// holding only the workspace-user files onboarded to it, the test-only
/// ECR pull policy for `image`, and that role's profile connection
/// (`AWS_CONFIG_FILE` pointed at a config file this writes). Everything
/// made is recorded in `p5` / the fixture for the guards.
async fn deploy_for_workspace_user(
t: &Env,
p5: &std::sync::Mutex<Phase5>,
image: &str,
max_workspace_hours: u32,
) -> Deployed {
use crate::aws::tofu_modules::{environment_cluster_name, user_file_system_token};
let region = t.e.region.clone();
let admin = t.admin().await;
let (keys, cfg, who) = t.session(&admin).await;
t.ensure_bucket(&cfg, &who).await;
t.ensure_boundary().await;
// ── the environment, deployed by the environment admin ──
let name = format!("e2e{}", &uuid::Uuid::new_v4().simple().to_string()[..6]);
let (cluster, families) = (environment_cluster_name(&name), format!("triple-c-{name}-"));
{
let mut g = p5_lock(p5);
g.cluster = Some(cluster);
g.families.push(families);
}
let vars = environment_vars(&name, &region, &NetworkChoice::Public, max_workspace_hours).unwrap();
let (_, outcome, denied) = t
.run(ModuleKind::Environment, Deployment::Environment(&name), &vars, TofuOp::Apply, t.keys_env(&keys))
.await;
assert_ok("deploy", &outcome, &denied);
// ── the workspace user: only the workspace files ──
let user = t.e.tier_role("wsuser", &[Tier::WorkspaceUser]).await;
let owner = validate_owner_id(&user.session_user_id()).expect("owner id");
let okey = owner_key(&region, &owner);
let fs_token = user_file_system_token(&name, &okey);
p5_lock(p5).fs_token = Some(fs_token.clone());
// Onboarded by the admin (an onboarding from an admin bundle).
let (keys, _, _) = t.session(&admin).await;
let (_, outcome, denied) = t
.run(
ModuleKind::UserOnboard,
Deployment::User { env: &name, owner_key: &okey },
&onboarding_vars(&name, &region, &owner),
TofuOp::Apply,
t.keys_env(&keys),
)
.await;
assert_ok("onboard the workspace user", &outcome, &denied);
let (_, exec_role) = user_role_names(&name, &okey);
// Test-only: pull from the throwaway private ECR repository.
if let Some((registry, rest)) = image.split_once('/') {
if registry.contains(".dkr.ecr.") {
let repo = rest.split([':', '@']).next().unwrap();
let doc = format!(
r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow","Action":"ecr:GetAuthorizationToken","Resource":"*"}},{{"Effect":"Allow","Action":["ecr:BatchGetImage","ecr:GetDownloadUrlForLayer","ecr:BatchCheckLayerAvailability"],"Resource":"arn:aws:ecr:{region}:{}:repository/{repo}"}}]}}"#,
t.e.account
);
p5_lock(p5).exec_role = Some(exec_role.clone());
ok(
"put the test-only ECR policy",
t.e.iam.put_role_policy().role_name(&exec_role).policy_name(ECR_POLICY).policy_document(doc).send().await,
);
eprintln!("test-only: {ECR_POLICY} on {exec_role} for {repo}");
}
}
// The workspace user's profile connection.
let profile = format!("{}-wsuser", t.e.prefix);
let cfg_file = t.root.join(format!("{}-config", t.e.prefix));
std::fs::write(
&cfg_file,
format!(
"[profile {profile}]\nrole_arn = {}\nsource_profile = {}\nrole_session_name = {}\n",
user.arn,
super::var("TRIPLEC_AWS_E2E_PROFILE"),
super::SESSION
),
)
.unwrap();
let before = std::env::var_os("AWS_CONFIG_FILE");
p5_lock(p5).config_file_before = Some(before);
std::env::set_var("AWS_CONFIG_FILE", &cfg_file);
let conn = HostConnection {
id: "e2e-wsuser".into(),
name: "e2e-wsuser".into(),
kind: HostKind::Aws {
auth: AwsAuth::Profile { name: profile.clone() },
default_region: region.clone(),
},
};
let ws_cfg = crate::aws::sdk_config(&conn, &region).await.expect("workspace user config");
let ws = caller_identity(&ws_cfg).await.expect("workspace user identity");
assert_eq!(ws.user_id, owner, "the profile session is the onboarded owner");
eprintln!("workspace user: {}", ws.arn);
Deployed { region, name, owner, okey, fs_token, conn, ws_cfg, ws }
}
/// Phase 5: a Fargate workspace's whole life through `ProjectHost::Aws`,
/// as a role holding **only** the workspace-user files, in an
/// environment the environment admin deployed and onboarded it to.
@@ -1949,9 +2098,7 @@ mod environment {
use crate::aws::runtime::SdkRuntimeApi;
use crate::aws::tasks::{run_task_request, task_log_stream, SdkTaskApi, TaskApi};
use crate::aws::tofu_modules::{
environment_cluster_name, log_stream_prefix, project_secret_name, task_family,
user_file_system_token,
};
log_stream_prefix, project_secret_name, task_family};
use crate::aws::workspace::workspace_tags;
use crate::host::{lifecycle::ProjectHost, tunnels};
use crate::models::app_settings::ImageSource;
@@ -1966,92 +2113,8 @@ mod environment {
// Boxed: the body's state machine is far larger than a test
// thread's 2 MiB stack once it is moved into the timeout.
let inner = Box::pin(async {
let region = t.e.region.clone();
let admin = t.admin().await;
let (keys, cfg, who) = t.session(&admin).await;
t.ensure_bucket(&cfg, &who).await;
t.ensure_boundary().await;
// ── the environment, deployed by the environment admin ──
let name = format!("e2e{}", &uuid::Uuid::new_v4().simple().to_string()[..6]);
let (cluster, families) = (environment_cluster_name(&name), format!("triple-c-{name}-"));
{
let mut g = p5_lock(&p5);
g.cluster = Some(cluster);
g.families.push(families);
}
let vars = environment_vars(&name, &region, &NetworkChoice::Public).unwrap();
let (_, outcome, denied) = t
.run(ModuleKind::Environment, Deployment::Environment(&name), &vars, TofuOp::Apply, t.keys_env(&keys))
.await;
assert_ok("deploy", &outcome, &denied);
// ── the workspace user: only the workspace files ──
let user = t.e.tier_role("wsuser", &[Tier::WorkspaceUser]).await;
let owner = validate_owner_id(&user.session_user_id()).expect("owner id");
let okey = owner_key(&region, &owner);
let fs_token = user_file_system_token(&name, &okey);
p5_lock(&p5).fs_token = Some(fs_token.clone());
// Onboarded by the admin (an onboarding from an admin bundle).
let (keys, _, _) = t.session(&admin).await;
let (_, outcome, denied) = t
.run(
ModuleKind::UserOnboard,
Deployment::User { env: &name, owner_key: &okey },
&onboarding_vars(&name, &region, &owner),
TofuOp::Apply,
t.keys_env(&keys),
)
.await;
assert_ok("onboard the workspace user", &outcome, &denied);
let (_, exec_role) = user_role_names(&name, &okey);
// Test-only: pull from the throwaway private ECR repository.
if let Some((registry, rest)) = image.split_once('/') {
if registry.contains(".dkr.ecr.") {
let repo = rest.split([':', '@']).next().unwrap();
let doc = format!(
r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow","Action":"ecr:GetAuthorizationToken","Resource":"*"}},{{"Effect":"Allow","Action":["ecr:BatchGetImage","ecr:GetDownloadUrlForLayer","ecr:BatchCheckLayerAvailability"],"Resource":"arn:aws:ecr:{region}:{}:repository/{repo}"}}]}}"#,
t.e.account
);
p5_lock(&p5).exec_role = Some(exec_role.clone());
ok(
"put the test-only ECR policy",
t.e.iam.put_role_policy().role_name(&exec_role).policy_name(ECR_POLICY).policy_document(doc).send().await,
);
eprintln!("test-only: {ECR_POLICY} on {exec_role} for {repo}");
}
}
// The workspace user's profile connection.
let profile = format!("{}-wsuser", t.e.prefix);
let cfg_file = t.root.join(format!("{}-config", t.e.prefix));
std::fs::write(
&cfg_file,
format!(
"[profile {profile}]\nrole_arn = {}\nsource_profile = {}\nrole_session_name = {}\n",
user.arn,
super::var("TRIPLEC_AWS_E2E_PROFILE"),
super::SESSION
),
)
.unwrap();
let before = std::env::var_os("AWS_CONFIG_FILE");
p5_lock(&p5).config_file_before = Some(before);
std::env::set_var("AWS_CONFIG_FILE", &cfg_file);
let conn = HostConnection {
id: "e2e-wsuser".into(),
name: "e2e-wsuser".into(),
kind: HostKind::Aws {
auth: AwsAuth::Profile { name: profile.clone() },
default_region: region.clone(),
},
};
let ws_cfg = crate::aws::sdk_config(&conn, &region).await.expect("workspace user config");
let ws = caller_identity(&ws_cfg).await.expect("workspace user identity");
assert_eq!(ws.user_id, owner, "the profile session is the onboarded owner");
eprintln!("workspace user: {}", ws.arn);
let Deployed { region, name, owner, okey, fs_token, conn, ws_cfg, ws } =
deploy_for_workspace_user(&t, &p5, &image, 0).await;
let mut settings = AppSettings::default();
settings.hosts = vec![conn];
@@ -2304,7 +2367,7 @@ mod environment {
let tail1: Vec<&String> = log1.iter().filter(|l| l.contains("entrypoint: stopping") || l.contains("wip") || l.contains("WIP")).collect();
eprintln!("RESULT task 1 stop log lines: {tail1:#?}");
assert!(log1.iter().any(|l| l.contains("entrypoint: stopping (sigterm) — pushing work in progress")), "{log1:#?}");
store.set_aws_runtime(&id, None, current(&store).aws.unwrap().access_point_id, Some(revision1.clone())).unwrap();
store.set_aws_runtime(&id, None, current(&store).aws.unwrap().access_point_id, Some(revision1.clone()), None).unwrap();
// ── start 2, unchanged; idle stop is at its minimum ──
let t3 = std::time::Instant::now();
@@ -2388,7 +2451,7 @@ mod environment {
// ── remove: the task, the secret and (data discarded) the access
// point; the task definition is left (R8) ──
store.set_aws_runtime(&id, None, aws2.access_point_id.clone(), aws2.task_definition_arn.clone()).unwrap();
store.set_aws_runtime(&id, None, aws2.access_point_id.clone(), aws2.task_definition_arn.clone(), aws2.workspace_access_point_id.clone()).unwrap();
// Every step runs even when one fails (the failures are
// reported together), so check them all before the result.
let removed = host.remove_aws_resources(&current(&store), &settings, true).await;
@@ -2437,5 +2500,425 @@ mod environment {
})
.await;
}
}
// ─────────────────────────────────────────────── phase 6: one workspace, every option
/// The keys of the project secret, as the workspace user (values are
/// never printed), and the two gateway key values for the log check.
async fn secret_keys(
ws_cfg: &SdkConfig,
secret: &str,
) -> (Vec<String>, serde_json::Map<String, serde_json::Value>) {
let out = ok(
"GetSecretValue on the project's own secret, as the workspace user",
aws_sdk_secretsmanager::Client::new(ws_cfg).get_secret_value().secret_id(secret).send().await,
);
let map: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(out.secret_string().expect("a string secret")).expect("a JSON payload");
let mut keys: Vec<String> = map.keys().cloned().collect();
keys.sort();
(keys, map)
}
/// A free loopback port on this machine.
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
/// `GET /` on 127.0.0.1:`port`: the status line.
async fn http_get_status(port: u16) -> String {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let mut s = tokio::net::TcpStream::connect(("127.0.0.1", port)).await.expect("connect to the mapped port");
s.write_all(b"GET / HTTP/1.0\r\nHost: localhost\r\n\r\n").await.unwrap();
let mut buf = Vec::new();
let _ = tokio::time::timeout(Duration::from_secs(30), s.read_to_end(&mut buf)).await;
String::from_utf8_lossy(&buf).lines().next().unwrap_or_default().to_string()
}
/// Phase 6 in one project, two task starts: an environment (module v2)
/// with `max_workspace_hours = 1`, and a project with EFS storage, a
/// setup script, copy-at-connect credentials (a CA file, a git token and
/// a custom variable), a port mapping and the LLM gateway sidecar (a
/// dummy provider key). Run like the phase-5 test:
///
/// ```sh
/// TRIPLEC_E2E_TEST=aws::live_tests::environment::a_phase6_workspace_round_trips \
/// TRIPLEC_AWS_E2E_PROFILE=… TRIPLEC_AWS_E2E_REGION=us-west-2 scripts/aws-e2e-workspace.sh
/// ```
///
/// Not exercised: the lifetime cap firing (unit-tested; 60 minutes),
/// a v1 → v2 environment update (the fixture applies only the embedded
/// modules), a private clone retried after connect (no credentials).
/// About 25 Fargate task-minutes at 1 vCPU / 2 GB.
#[tokio::test]
#[ignore]
async fn a_phase6_workspace_round_trips() {
use crate::aws::runtime::SdkRuntimeApi;
use crate::aws::tasks::{task_log_stream, SdkTaskApi, TaskApi};
use crate::aws::tofu_modules::{log_stream_prefix, project_secret_name, task_family};
use crate::host::aws_host::{self, connect};
use crate::host::{lifecycle::ProjectHost, port_forwards, ExecSpec};
use crate::models::app_settings::ImageSource;
use crate::models::gateway_settings::{GatewayModel, GatewaySettings};
use crate::models::project::{
AwsPlacement, AwsWorkspaceConfig, Backend, EnvVar, OpenAiCompatibleConfig, PortMapping,
RepoSource, SecretsDelivery, StorageMode,
};
use crate::models::{AppSettings, Project};
use crate::storage::projects_store::ProjectsStore;
let image = super::var("TRIPLEC_E2E_IMAGE");
let ecr_repo = std::env::var("TRIPLEC_E2E_ECR_REPO").ok().filter(|r| !r.is_empty());
env_e2e(|t| async move {
let p5 = std::sync::Mutex::new(Phase5 { ecr_repo, ..Default::default() });
let inner = Box::pin(async {
// ── 1. the environment carries the lifetime cap ──
let Deployed { region, name, owner: _, okey, fs_token: _, conn, ws_cfg, ws } =
deploy_for_workspace_user(&t, &p5, &image, 1).await;
let env = crate::aws::runtime::read_environment(&SdkRuntimeApi::new(&ws_cfg), &name, &ws.account, &region)
.await
.expect("the environment record, as the workspace user");
eprintln!(
"RESULT environment record: template_version {}, max_workspace_hours {}",
env.template_version, env.max_workspace_hours
);
assert_eq!(env.template_version, crate::aws::tofu_modules::ENVIRONMENT_MODULE_VERSION);
assert_eq!(env.max_workspace_hours, 1);
// Dummy values: none is a real credential; each must stay out
// of the logs and (credentials) out of the secret.
let tag = uuid::Uuid::new_v4().simple().to_string();
let provider_key = format!("sk-e2e-provider-{tag}");
let git_token = format!("e2e-git-token-{tag}");
let custom_value = format!("e2e-custom-{tag}");
let dir = tempfile::tempdir().unwrap();
let ca = dir.path().join("e2e-ca.pem");
let made = std::process::Command::new("openssl")
.args(["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1", "-subj", "/CN=triple-c-e2e-ca"])
.arg("-keyout")
.arg(dir.path().join("e2e-ca.key"))
.arg("-out")
.arg(&ca)
.output()
.expect("openssl");
assert!(made.status.success(), "openssl: {}", String::from_utf8_lossy(&made.stderr));
let mut settings = AppSettings::default();
settings.hosts = vec![conn];
settings.image_source = ImageSource::Custom;
settings.custom_image_name = Some(image.clone());
settings.gateway = GatewaySettings {
provider: "openai".into(),
models: vec![GatewayModel { name: "e2e-model".into(), model_id: "gpt-4o-mini".into() }],
..Default::default()
};
let http_port: u16 = 8099;
let host_port = free_port();
let mut project = Project::new("e2e-phase6".into(), Vec::new());
project.host_id = Some("e2e-wsuser".into());
project.aws_placement = Some(AwsPlacement { region: region.clone(), environment: name.clone() });
project.backend = Backend::OpenAiCompatible;
project.openai_compatible_config = Some(OpenAiCompatibleConfig {
base_url: "https://example.invalid".into(),
api_key: Some(format!("e2e-oc-key-{tag}")),
model_id: Some("e2e-model".into()),
haiku_model_id: None,
});
project.ca_cert_path = Some(ca.to_string_lossy().into_owned());
project.git_token = Some(git_token.clone());
project.custom_env_vars = vec![EnvVar { key: "E2E_CUSTOM_TOKEN".into(), value: custom_value.clone() }];
project.port_mappings = vec![PortMapping { host_port, container_port: http_port, protocol: "tcp".into() }];
project.aws = Some(AwsWorkspaceConfig {
cpu: 1024,
memory_mib: 2048,
storage: StorageMode::Efs,
repos: vec![RepoSource {
url: "https://github.com/octocat/Hello-World.git".into(),
branch: None,
folder: "Hello-World".into(),
}],
wip_push_on_stop: false,
idle_stop_minutes: None,
secrets_delivery: SecretsDelivery::CopyAtConnect,
setup_script: Some(
"sudo apt-get install -y jq\njq --version\necho ok > /workspace/setup-marker\n".into(),
),
gateway_sidecar: true,
..Default::default()
});
assert_eq!(crate::host::port_forwards::validate_aws_port_mappings(&project.port_mappings, true), Ok(()));
let id = project.id.clone();
let secret_name = project_secret_name(&name, &okey, &id);
{
let mut g = p5_lock(&p5);
g.project_id = Some(id.clone());
g.secrets.push(secret_name.clone());
}
let store_dir = tempfile::tempdir().unwrap();
let store = ProjectsStore::over_file_for_test(store_dir.path().join("projects.json"), vec![project.clone()]);
assert_eq!(ProjectHost::for_project(&project), ProjectHost::Aws);
let host = ProjectHost::Aws;
let progress = |m: &str| eprintln!(" progress: {m}");
let api = SdkTaskApi::new(&ws_cfg);
let family = task_family(&name, &okey, &id);
let stream_prefix = log_stream_prefix(&okey, &id);
let current = |store: &ProjectsStore| store.get(&id).unwrap();
let key_reader = {
let k = provider_key.clone();
move || Ok(Some(k))
};
let connect_now = |target: crate::host::ContainerTarget| {
let (p, s) = (current(&store), settings.clone());
async move {
let bundle = connect::connect_credentials(&p, &s).expect("the connect bundle");
eprintln!(" connect bundle: {bundle:?}");
let t0 = std::time::Instant::now();
connect::push_connect_bundle(&target, &bundle).await.expect("push the connect bundle");
eprintln!("RESULT copy-at-connect push: {:?}", t0.elapsed());
}
};
// ── start 1 ──
let t0 = std::time::Instant::now();
let target = aws_host::ensure_started_with(&current(&store), &settings, &store, &progress, key_reader.clone())
.await
.expect("start 1");
let aws1 = current(&store).aws.unwrap();
let task1 = aws1.task_arn.clone().unwrap();
eprintln!("RESULT start 1 (cold, EFS + sidecar): {:?}; task {task1}", t0.elapsed());
assert!(aws1.access_point_id.is_some() && aws1.workspace_access_point_id.is_some(), "EFS: two access points {aws1:?}");
assert!(aws1.task_definition_arn.as_deref().unwrap().contains(&format!(":task-definition/{family}:")));
connect_now(target.clone()).await;
// ── 6. the secret holds no credentials; 1. the cap reached the task ──
let (keys, map) = secret_keys(&ws_cfg, &secret_name).await;
eprintln!("RESULT project secret keys (copy-at-connect + sidecar): {keys:?}");
eprintln!("RESULT TRIPLEC_MAX_LIFETIME_MINUTES in the secret: {:?}", map.get("TRIPLEC_MAX_LIFETIME_MINUTES"));
assert_eq!(map.get("TRIPLEC_MAX_LIFETIME_MINUTES").and_then(|v| v.as_str()), Some("60"));
for k in ["TRIPLEC_FILES", "GIT_TOKEN", "E2E_CUSTOM_TOKEN", "TRIPLE_C_CUSTOM_ENV"] {
assert!(!keys.iter().any(|x| x == k), "{k} is in the copy-at-connect secret");
}
let secret_text = serde_json::to_string(&map).unwrap();
for (what, v) in [("git token", &git_token), ("custom value", &custom_value)] {
assert!(!secret_text.contains(v.as_str()), "the {what} is in the secret");
}
assert_eq!(map.get("TRIPLEC_GATEWAY_API_KEY").and_then(|v| v.as_str()), Some(provider_key.as_str()), "R1: the sidecar's key stays");
let master1 = map.get("TRIPLEC_GATEWAY_MASTER_KEY").and_then(|v| v.as_str()).expect("a master key").to_string();
let (env_line, code) = sh(
&target,
"root",
"tr '\\0' '\\n' < /proc/1/environ | grep '^TRIPLEC_MAX_LIFETIME_MINUTES='; \
ps -eo args | grep -c '[t]riple-c-idle-watch' || true",
)
.await;
eprintln!("RESULT task environment (PID 1), lifetime + idle watcher count:\n{}", env_line.trim());
assert_eq!(code, 0);
assert!(env_line.contains("TRIPLEC_MAX_LIFETIME_MINUTES=60"), "{env_line}");
// ── 3. clone + setup, as the app's status follower sees it ──
let settled = poll(Duration::from_secs(600), Duration::from_secs(5), || async {
let s = aws_host::workspace_status(&current(&store)).await.ok().flatten()?;
s.settled().then_some(s)
})
.await
.expect("clone + setup never settled");
eprintln!("RESULT workspace status after start 1: {settled:?}");
assert_eq!(settled.setup.state, aws_host::status::SetupState::Ok, "{settled:?}");
assert!(settled.failed_repos().is_empty(), "{settled:?}");
// ── 2. EFS /workspace, Hybrid links; 6. CA installed, ~/.ssh local ──
let marker = uuid::Uuid::new_v4().to_string();
let (fs, code) = sh(
&target,
"claude",
&format!(
"grep ' /workspace ' /proc/mounts; cat /workspace/setup-marker; \
git -C /workspace/Hello-World log -1 --format=%H; \
printf %s {marker} > /workspace/e2e-efs-marker && echo wrote; \
for f in ~/.claude ~/.claude.json ~/.ssh; do printf '%s -> %s\\n' \"$f\" \"$(readlink \"$f\")\"; done; \
test -L ~/.ssh && echo SSH-IS-LINK || echo ssh-local; \
ls /usr/local/share/ca-certificates/; \
for c in /usr/local/share/ca-certificates/triple-c-*.crt; do openssl x509 -in \"$c\" -noout -subject; done; \
git config --global --get credential.helper >/dev/null && echo git-helper-set"
),
)
.await;
eprintln!("RESULT start 1 workspace (exit {code}):\n{}", fs.trim());
assert!(fs.contains(" /workspace nfs"), "{fs}");
assert!(fs.lines().any(|l| l.trim() == "ok"), "setup marker: {fs}");
assert!(fs.contains("wrote"), "{fs}");
assert!(fs.contains(".claude -> /mnt/triple-c-home/"), "{fs}");
assert!(fs.contains("ssh-local"), "R6: ~/.ssh is not on EFS under copy-at-connect: {fs}");
assert!(fs.contains("CN = triple-c-e2e-ca") || fs.contains("CN=triple-c-e2e-ca"), "CA installed: {fs}");
// ── 4. a port mapping ──
let exec = target
.exec_detached(
ExecSpec::new(vec!["python3".into(), "-m".into(), "http.server".into(), http_port.to_string(), "--bind".into(), "127.0.0.1".into()])
.working_dir(Some("/tmp"))
.no_stdin(),
)
.await
.expect("start the HTTP server");
eprintln!(" http.server exec {exec}");
let up = poll(Duration::from_secs(60), Duration::from_secs(2), || async {
let (_, c) = sh(&target, "claude", &format!("curl -sf -o /dev/null http://127.0.0.1:{http_port}/")).await;
(c == 0).then_some(())
})
.await;
assert!(up.is_some(), "the HTTP server never answered inside the workspace");
let opened = port_forwards::open_for(&id, &target, &current(&store).port_mappings).await;
assert!(opened.iter().all(|r| r.error.is_none()), "{opened:?}");
let status_line = http_get_status(host_port).await;
eprintln!("RESULT port mapping 127.0.0.1:{host_port} -> workspace :{http_port}: {status_line}");
assert!(status_line.contains(" 200 "), "{status_line}");
port_forwards::close(&id).await;
// ── 5. the gateway sidecar ──
let mk_env = vec![format!("MK={master1}")];
let health = poll(Duration::from_secs(420), Duration::from_secs(10), || async {
let (out, _) = sh(&target, "claude", "curl -s -o /dev/null -w '%{http_code}' --max-time 5 http://127.0.0.1:4000/health/liveliness").await;
(out.trim() == "200").then_some(out)
})
.await;
eprintln!("RESULT gateway /health/liveliness from the workspace: {health:?} after {:?} from RunTask", t0.elapsed());
assert!(health.is_some(), "the gateway sidecar never answered");
let exec_with = |script: &'static str| {
let t = target.clone();
let env = mk_env.clone();
async move {
tokio::time::timeout(SH_TIMEOUT, t.exec_oneshot_as("claude", vec!["sh".into(), "-c".into(), script.into()], env))
.await
.expect("exec timed out")
.expect("exec")
}
};
let (models, _) = exec_with(
"curl -s -w ' HTTP%{http_code}' -H \"Authorization: Bearer $MK\" http://127.0.0.1:4000/v1/models; echo; \
curl -s -o /dev/null -w 'no key: HTTP%{http_code}' http://127.0.0.1:4000/v1/models",
)
.await;
eprintln!("RESULT gateway /v1/models:\n{}", models.trim());
assert!(models.contains("e2e-model") && models.contains("HTTP200"), "{models}");
let (chat, _) = exec_with(
"curl -s --max-time 60 -w ' HTTP%{http_code}' -H \"Authorization: Bearer $MK\" -H 'content-type: application/json' \
-d '{\"model\":\"e2e-model\",\"messages\":[{\"role\":\"user\",\"content\":\"hi\"}],\"max_tokens\":5}' \
http://127.0.0.1:4000/v1/chat/completions",
)
.await;
let chat_short: String = chat.chars().take(400).collect();
eprintln!("RESULT failed provider call (dummy key):\n{chat_short}");
assert!(!chat.contains(&provider_key), "the provider key came back in the response");
assert!(!chat.contains(" HTTP200"), "a dummy key got an answer?");
let (claude_env, _) = sh(&target, "claude", "bash -lc 'printf \"%s\\n\" \"$ANTHROPIC_BASE_URL\"; test -n \"$ANTHROPIC_AUTH_TOKEN\" && echo token-set'").await;
eprintln!("RESULT claude's environment: {}", claude_env.trim());
// ── 7. a cost estimate for this configuration ──
let pricing_cfg = crate::aws::sdk_config(&settings.hosts[0], crate::aws::pricing::PRICING_REGION).await.expect("pricing config");
let rates = crate::aws::pricing::fetch_rates(&crate::aws::pricing::SdkPricingApi::new(&pricing_cfg), &region)
.await
.expect("fetch Fargate rates as the workspace user");
let est = crate::aws::pricing::estimate(
current(&store).aws.as_ref().unwrap(),
&rates.for_arch(crate::models::project::Arch::X86_64).expect("x86 rates"),
);
eprintln!("RESULT cost estimate for 1 vCPU / 2 GiB / EFS: {est:?}");
assert!(est.usd_per_hour > 0.0 && est.efs_billed_separately);
// ── stop 1 ──
host.stop_project(&current(&store), &settings).await.expect("stop 1");
let stopped = poll(Duration::from_secs(240), Duration::from_secs(5), || async {
let d = api.describe(&env.cluster_arn, std::slice::from_ref(&task1)).await.ok()?;
let t = d.into_iter().next()?;
(t.last_status == "STOPPED").then_some(t)
})
.await
.expect("task 1 never stopped");
eprintln!("RESULT stop 1: code {:?}, exit {:?}, kind {:?}", stopped.stop_code, stopped.exit_code, crate::aws::tasks::classify_stop(&stopped));
tokio::time::sleep(Duration::from_secs(20)).await;
// ── 5. neither key in the logs ──
let task_id = task1.rsplit('/').next().unwrap();
let gw_stream = format!("{stream_prefix}/gateway/{task_id}");
let gw_log = api.log_tail(&env.log_group, &gw_stream, 2000).await.unwrap();
let ws_log = api.log_tail(&env.log_group, &task_log_stream(&stream_prefix, &task1), 2000).await.unwrap();
eprintln!("RESULT gateway log: {} lines; workspace log: {} lines", gw_log.len(), ws_log.len());
let short = |l: &String| l.chars().take(240).collect::<String>();
for l in gw_log.iter().filter(|l| l.contains("rror") || l.contains("uthoriz") || l.contains("401") || l.to_lowercase().contains("header")).take(12) {
eprintln!(" gateway log: {}", short(l));
}
let prefix8: String = provider_key.chars().take(16).collect();
let partial = gw_log.iter().filter(|l| l.contains(&prefix8)).count();
eprintln!("RESULT gateway log lines holding the provider key's first 16 chars (masked echo): {partial}");
for (what, v) in [("provider key", provider_key.as_str()), ("master key", master1.as_str()), ("git token", git_token.as_str()), ("custom value", custom_value.as_str())] {
let gw = gw_log.iter().filter(|l| l.contains(v)).count();
let wsn = ws_log.iter().filter(|l| l.contains(v)).count();
eprintln!("RESULT {what} in logs: gateway {gw} line(s), workspace {wsn} line(s)");
assert_eq!((gw, wsn), (0, 0), "the {what} is in the task's logs");
}
assert!(!gw_log.is_empty(), "the gateway stream is empty: {gw_stream}");
store.set_aws_runtime(&id, None, aws1.access_point_id.clone(), aws1.task_definition_arn.clone(), aws1.workspace_access_point_id.clone()).unwrap();
// ── start 2: /workspace survived ──
let t3 = std::time::Instant::now();
let target2 = aws_host::ensure_started_with(&current(&store), &settings, &store, &progress, key_reader.clone())
.await
.expect("start 2");
let aws2 = current(&store).aws.unwrap();
let task2 = aws2.task_arn.clone().unwrap();
eprintln!("RESULT start 2: {:?}; task {task2}", t3.elapsed());
assert_ne!(task2, task1);
assert_eq!(aws2.workspace_access_point_id, aws1.workspace_access_point_id, "the same /workspace access point");
connect_now(target2.clone()).await;
let (_, map2) = secret_keys(&ws_cfg, &secret_name).await;
let master2 = map2.get("TRIPLEC_GATEWAY_MASTER_KEY").and_then(|v| v.as_str()).unwrap_or_default();
eprintln!("RESULT sidecar master key new per start: {}", master2 != master1);
assert_ne!(master2, master1.as_str());
let settled2 = poll(Duration::from_secs(600), Duration::from_secs(5), || async {
let s = aws_host::workspace_status(&current(&store)).await.ok().flatten()?;
s.settled().then_some(s)
})
.await
.expect("start 2 never settled");
eprintln!("RESULT workspace status after start 2: {settled2:?}");
let (fs2, _) = sh(&target2, "claude", "cat /workspace/e2e-efs-marker; echo; ls /workspace; git -C /workspace/Hello-World status --short | head -3; ls /usr/local/share/ca-certificates/").await;
eprintln!("RESULT start 2 /workspace:\n{}", fs2.trim());
assert!(fs2.contains(&marker), "the EFS file survived the stop: {fs2}");
assert!(settled2.clone.repos.iter().any(|r| r.folder == "Hello-World" && r.state == "exists"), "{settled2:?}");
// ── stop 2, then remove with discard ──
host.stop_project(&current(&store), &settings).await.expect("stop 2");
poll(Duration::from_secs(240), Duration::from_secs(5), || async {
let d = api.describe(&env.cluster_arn, std::slice::from_ref(&task2)).await.ok()?;
(d.into_iter().next()?.last_status == "STOPPED").then_some(())
})
.await
.expect("task 2 never stopped");
store.set_aws_runtime(&id, None, aws2.access_point_id.clone(), aws2.task_definition_arn.clone(), aws2.workspace_access_point_id.clone()).unwrap();
let removed = host.remove_aws_resources(&current(&store), &settings, true).await;
eprintln!("RESULT remove_aws_resources (discard): {:?}", removed.as_ref().map_err(|e| e.chars().take(400).collect::<String>()));
let efs = aws_sdk_efs::Client::new(&t.e.cfg);
for ap in [aws2.access_point_id.clone().unwrap(), aws2.workspace_access_point_id.clone().unwrap()] {
let gone = efs.describe_access_points().access_point_id(&ap).send().await;
let live = gone.as_ref().map(|o| o.access_points().iter().any(|a| a.life_cycle_state() == Some(&aws_sdk_efs::types::LifeCycleState::Available))).unwrap_or(false);
eprintln!("RESULT access point {ap} after remove: available {live}");
assert!(!live, "{ap} is still there");
}
removed.expect("remove the project's resources");
});
let body = async {
if tokio::time::timeout(PHASE5_TIMEOUT, inner).await.is_err() {
panic!("the phase-6 body did not finish in {PHASE5_TIMEOUT:?}");
}
};
let result = std::panic::AssertUnwindSafe(body).catch_unwind().await;
let guard = std::mem::take(&mut *p5_lock(&p5));
phase5_cleanup(&t.e, &guard).await;
if let Err(panic) = result {
std::panic::resume_unwind(panic);
}
})
.await;
}
}
+1
View File
@@ -11,6 +11,7 @@ mod identity;
mod live_tests;
pub mod permissions;
pub mod policies;
pub mod pricing;
#[cfg(test)]
mod policy_eval;
pub mod runtime;
+29
View File
@@ -421,6 +421,9 @@ const WORKSPACE_USER_CHECKS: &[Check] = &[
),
read("logs:FilterLogEvents", PROBE_LOG_GROUP, &[]),
read("sts:GetCallerIdentity", "*", &[]),
// `workspace-user-tasks.json`: the hourly cost estimate (Q5). The Price
// List API has no resource-level permissions, and prices are public.
read("pricing:GetProducts", "*", &[]),
];
/// The admin tier's checks, across all its files. `tofu_modules`' tests hold
@@ -1224,6 +1227,32 @@ mod tests {
}
}
/// The cost estimate's one call (Q5), in the second workspace file: only
/// `pricing:GetProducts`, on `*` (the API has no resource types), with no
/// other Pricing action riding along.
#[test]
fn the_workspace_tier_reads_prices_and_nothing_else_of_pricing() {
let (_, doc) = policies(Tier::WorkspaceUser)
.into_iter()
.find(|(name, _)| *name == "workspace-user-tasks.json")
.unwrap();
let pricing: Vec<Value> = statements_of(&doc)
.into_iter()
.filter(|s| actions(s).iter().any(|a| a.starts_with("pricing:")))
.collect();
assert_eq!(pricing.len(), 1);
assert_eq!(actions(&pricing[0]), ["pricing:GetProducts"]);
assert_eq!(resources(&pricing[0]), ["*"]);
assert!(pricing[0].get("Condition").is_none());
let c = ws_check("pricing:GetProducts", "*");
assert!(!c.write);
assert!(allowed(Tier::WorkspaceUser, c, ME, SOMEONE_ELSE));
for action in ["pricing:DescribeServices", "pricing:GetPriceListFileUrl"] {
let other = Check { action, ..*c };
assert!(!allowed(Tier::WorkspaceUser, &other, ME, ME), "{action}");
}
}
/// RegisterTaskDefinition with a task or execution role also authorizes
/// `iam:PassRole` on each; `PassOwnTaskRoles` (owner-tagged, to ECS
/// tasks, `triple-c-*` roles) must cover both and nothing else.
+405
View File
@@ -0,0 +1,405 @@
//! The estimated hourly cost of a Fargate workspace (Q5): on-demand rates
//! from the Price List API (`pricing:GetProducts`, service `AmazonECS`,
//! served from us-east-1 only), cached on disk per region for a day.
//!
//! Everything here is best effort. A denied call, no network, a slow answer
//! or a price list we can't read all end in "no estimate" (`None`), never an
//! error: the estimate is a hint next to the settings, not something a start
//! depends on.
use std::collections::HashMap;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::models::project::{Arch, AwsWorkspaceConfig, StorageMode};
/// The Price List API's endpoint region (it also has ap-south-1 and
/// eu-central-1; any of them serves every region's prices).
pub const PRICING_REGION: &str = "us-east-1";
pub const SERVICE_CODE: &str = "AmazonECS";
/// How long a region's rates are used before they are fetched again.
pub const CACHE_TTL: Duration = Duration::from_secs(24 * 60 * 60);
/// The whole fetch (every page), so the command answers quickly offline.
pub const FETCH_TIMEOUT: Duration = Duration::from_secs(8);
/// After a failed fetch, the region is not asked again for this long (the
/// settings call the estimate on every change).
pub const RETRY_AFTER_FAILURE: Duration = Duration::from_secs(5 * 60);
/// Fargate includes 20 GiB of ephemeral storage; only the rest is billed.
pub const FREE_EPHEMERAL_GIB: u32 = 20;
/// The filtered list is 8 rows (one page); more pages than this is not a
/// price list we understand.
const MAX_PAGES: usize = 5;
/// `GetProducts` filters as (type, field, value); `{region}` is the
/// workspace's region. Verified against the live API (2026-11-16): eight
/// rows per region — Linux x86 and ARM vCPU-hours and GB-hours, ephemeral
/// storage GB-hours and three Windows rows, which the parser skips.
pub const FILTERS: &[(&str, &str, &str)] = &[
("TERM_MATCH", "regionCode", "{region}"),
("TERM_MATCH", "termType", "OnDemand"),
("CONTAINS", "usagetype", "Fargate"),
];
/// One architecture's on-demand rates in a region, in USD.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct FargateRates {
pub vcpu_hour: f64,
pub gb_hour: f64,
pub storage_gb_hour: f64,
}
/// A region's rates per architecture. An architecture the price list did
/// not fully cover is `None`.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct RegionRates {
pub x86_64: Option<FargateRates>,
pub arm64: Option<FargateRates>,
}
impl RegionRates {
pub fn for_arch(&self, arch: Arch) -> Option<FargateRates> {
match arch {
Arch::X86_64 => self.x86_64,
Arch::Arm64 => self.arm64,
}
}
}
/// What the UI shows: the on-demand price per hour of the task while it
/// runs. A gateway sidecar shares the task's CPU and memory, so it adds
/// nothing; Spot isn't in the Price List API (`note_spot`: say it is
/// usually cheaper); EFS is billed per GB-month, not per hour
/// (`efs_billed_separately`).
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct HourlyEstimate {
pub usd_per_hour: f64,
pub note_spot: bool,
pub efs_billed_separately: bool,
}
pub fn estimate(cfg: &AwsWorkspaceConfig, rates: &FargateRates) -> HourlyEstimate {
let vcpu = f64::from(cfg.cpu) / 1024.0;
let gb = f64::from(cfg.memory_mib) / 1024.0;
let billed_storage = f64::from(cfg.ephemeral_gib.saturating_sub(FREE_EPHEMERAL_GIB));
HourlyEstimate {
usd_per_hour: vcpu * rates.vcpu_hour
+ gb * rates.gb_hour
+ billed_storage * rates.storage_gb_hour,
note_spot: cfg.spot,
efs_billed_separately: cfg.storage != StorageMode::Ephemeral,
}
}
/// The rates in a `GetProducts` price list (each item one product's JSON)
/// for `region`. `None` when no architecture is complete.
pub fn parse_price_list(region: &str, items: &[String]) -> Option<RegionRates> {
#[derive(Default)]
struct Partial {
vcpu: Option<f64>,
gb: Option<f64>,
}
let (mut x86, mut arm, mut storage) = (Partial::default(), Partial::default(), None);
for item in items {
let Ok(product) = serde_json::from_str::<Value>(item) else {
continue;
};
let attrs = &product["product"]["attributes"];
if attrs["regionCode"].as_str() != Some(region) {
continue;
}
if attrs["operatingSystem"]
.as_str()
.is_some_and(|os| os != "Linux")
{
continue;
}
let Some(usage) = attrs["usagetype"].as_str() else {
continue;
};
// `USW2-Fargate-ARM-vCPU-Hours:perCPU`; us-east-1's prefix is
// `USE1-`, Ireland's `EU-`.
let Some((_, kind)) = usage.split_once("Fargate-") else {
continue;
};
let slot = match kind {
"vCPU-Hours:perCPU" => &mut x86.vcpu,
"GB-Hours" => &mut x86.gb,
"ARM-vCPU-Hours:perCPU" => &mut arm.vcpu,
"ARM-GB-Hours" => &mut arm.gb,
"EphemeralStorage-GB-Hours" => &mut storage,
_ => continue,
};
if slot.is_none() {
*slot = on_demand_usd(&product);
}
}
let storage = storage?;
let complete = |p: Partial| {
Some(FargateRates {
vcpu_hour: p.vcpu?,
gb_hour: p.gb?,
storage_gb_hour: storage,
})
};
let rates = RegionRates {
x86_64: complete(x86),
arm64: complete(arm),
};
(rates.x86_64.is_some() || rates.arm64.is_some()).then_some(rates)
}
/// The product's on-demand USD price per unit. A price list gives one term
/// with one dimension for these rows; anything not a sane non-negative
/// number is ignored.
fn on_demand_usd(product: &Value) -> Option<f64> {
product["terms"]["OnDemand"]
.as_object()?
.values()
.filter_map(|term| term["priceDimensions"].as_object())
.flat_map(|dims| dims.values())
.filter_map(|d| d["pricePerUnit"]["USD"].as_str()?.parse::<f64>().ok())
.find(|p| p.is_finite() && (0.0..100.0).contains(p))
}
/// One page of `GetProducts`.
pub struct Page {
pub items: Vec<String>,
pub next_token: Option<String>,
}
/// The Price List API, faked in tests.
pub trait PricingApi {
/// One page of `AmazonECS` products matching [`FILTERS`] for `region`.
fn get_products(
&self,
region: &str,
next_token: Option<String>,
) -> impl Future<Output = Result<Page, String>> + Send;
}
/// The real thing: a client in [`PRICING_REGION`] from a connection's
/// `SdkConfig` (`aws::sdk_config(conn, PRICING_REGION)`).
pub struct SdkPricingApi {
client: aws_sdk_pricing::Client,
}
impl SdkPricingApi {
pub fn new(cfg: &aws_config::SdkConfig) -> Self {
Self {
client: aws_sdk_pricing::Client::new(cfg),
}
}
}
impl PricingApi for SdkPricingApi {
async fn get_products(&self, region: &str, next_token: Option<String>) -> Result<Page, String> {
use aws_sdk_pricing::types::{Filter, FilterType};
let mut req = self
.client
.get_products()
.service_code(SERVICE_CODE)
.format_version("aws_v1")
.max_results(100)
.set_next_token(next_token);
for (kind, field, value) in FILTERS {
let filter = Filter::builder()
.r#type(FilterType::from(*kind))
.field(*field)
.value(value.replace("{region}", region))
.build()
.map_err(|e| e.to_string())?;
req = req.filters(filter);
}
let out = req
.send()
.await
.map_err(|e| super::classify_sdk_error("pricing:GetProducts", &e).to_string())?;
Ok(Page {
items: out.price_list().to_vec(),
next_token: out.next_token().map(str::to_string),
})
}
}
/// Every page for `region`, parsed.
pub async fn fetch_rates<A: PricingApi>(api: &A, region: &str) -> Result<RegionRates, String> {
let mut items = Vec::new();
let mut token = None;
for _ in 0..MAX_PAGES {
let page = api.get_products(region, token).await?;
items.extend(page.items);
token = page.next_token.filter(|t| !t.is_empty());
if token.is_none() {
return parse_price_list(region, &items)
.ok_or_else(|| format!("no Fargate rates for {region} in the price list"));
}
}
Err(format!(
"the {region} price list has more than {MAX_PAGES} pages"
))
}
/// `<dir>/<region>.json`.
#[derive(Debug, Serialize, Deserialize)]
struct CacheFile {
region: String,
/// Unix seconds.
fetched_at: u64,
rates: RegionRates,
}
/// The per-region rate files and the memory of recent failures.
pub struct RatesCache {
dir: PathBuf,
failed_at: Mutex<HashMap<String, SystemTime>>,
}
/// `<data dir>/triple-c/aws-pricing/`.
pub fn default_cache_dir() -> Option<PathBuf> {
Some(dirs::data_dir()?.join("triple-c").join("aws-pricing"))
}
fn unix_secs(t: SystemTime) -> u64 {
t.duration_since(UNIX_EPOCH).map_or(0, |d| d.as_secs())
}
impl RatesCache {
pub fn new(dir: PathBuf) -> Self {
Self {
dir,
failed_at: Mutex::new(HashMap::new()),
}
}
#[cfg(test)]
pub fn dir(&self) -> &Path {
&self.dir
}
/// The region must already be validated (it names a file).
fn path(&self, region: &str) -> PathBuf {
self.dir.join(format!("{region}.json"))
}
/// The cached rates and whether they are still fresh at `now`. A
/// missing, unreadable or corrupt file, or one for another region, is
/// no cache.
pub fn read(&self, region: &str, now: SystemTime) -> Option<(RegionRates, bool)> {
let text = std::fs::read_to_string(self.path(region)).ok()?;
let file: CacheFile = serde_json::from_str(&text).ok()?;
if file.region != region {
return None;
}
let now = unix_secs(now);
// A timestamp from the future (a clock change) counts as stale.
let fresh = file.fetched_at <= now && now - file.fetched_at < CACHE_TTL.as_secs();
Some((file.rates, fresh))
}
pub fn write(&self, region: &str, rates: &RegionRates, now: SystemTime) -> Result<(), String> {
std::fs::create_dir_all(&self.dir).map_err(|e| e.to_string())?;
let file = CacheFile {
region: region.to_string(),
fetched_at: unix_secs(now),
rates: *rates,
};
let text = serde_json::to_string_pretty(&file).map_err(|e| e.to_string())?;
// Write then rename, so a concurrent reader never sees half a file.
let tmp = self
.dir
.join(format!(".{region}.json.tmp-{}", std::process::id()));
std::fs::write(&tmp, text).map_err(|e| e.to_string())?;
std::fs::rename(&tmp, self.path(region)).map_err(|e| {
let _ = std::fs::remove_file(&tmp);
e.to_string()
})
}
fn recently_failed(&self, region: &str, now: SystemTime) -> bool {
let map = self.failed_at.lock().unwrap_or_else(|e| e.into_inner());
map.get(region).is_some_and(|t| {
now.duration_since(*t)
.is_ok_and(|age| age < RETRY_AFTER_FAILURE)
})
}
fn note_failure(&self, region: &str, now: SystemTime, failed: bool) {
let mut map = self.failed_at.lock().unwrap_or_else(|e| e.into_inner());
if failed {
map.insert(region.to_string(), now);
} else {
map.remove(region);
}
}
/// Fresh cached rates, else a fetch (bounded by [`FETCH_TIMEOUT`])
/// that refreshes the cache, else the stale cache, else `None`. `api`
/// is `None` when no client could be built (the stale cache still
/// answers). A region whose fetch failed is not asked again for
/// [`RETRY_AFTER_FAILURE`].
pub async fn rates<A: PricingApi>(
&self,
api: Option<&A>,
region: &str,
now: SystemTime,
) -> Option<RegionRates> {
self.rates_with::<A, _>(async { api }, region, now).await
}
/// [`Self::rates`] with the client built by `api`, which is awaited only
/// when the cache can't answer, and within the same [`FETCH_TIMEOUT`] as
/// the fetch (building one reads credentials, which can hang too).
pub async fn rates_with<A: PricingApi, B: std::borrow::Borrow<A>>(
&self,
api: impl Future<Output = Option<B>>,
region: &str,
now: SystemTime,
) -> Option<RegionRates> {
let cached = self.read(region, now);
if let Some((rates, true)) = cached {
return Some(rates);
}
let stale = cached.map(|(rates, _)| rates);
if self.recently_failed(region, now) {
return stale;
}
let fetch = async {
let api = api.await?;
Some(fetch_rates(api.borrow(), region).await)
};
let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch).await {
Ok(Some(r)) => r,
// No client could be built: a failure like any other (building
// one reads credentials, which can be slow), and the stale cache
// answers.
Ok(None) => {
log::info!("No Fargate rates for {region}: no Pricing client could be built");
self.note_failure(region, now, true);
return stale;
}
Err(_) => Err(format!("timed out after {}s", FETCH_TIMEOUT.as_secs())),
};
match fetched {
Ok(rates) => {
self.note_failure(region, now, false);
if let Err(e) = self.write(region, &rates, now) {
log::warn!("Could not cache the {region} Fargate rates: {e}");
}
Some(rates)
}
Err(e) => {
log::info!("No Fargate rates for {region}: {e}");
self.note_failure(region, now, true);
stale
}
}
}
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,13 @@
{
"FormatVersion": "aws_v1",
"PriceList": [
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per vCPU per hour\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-Windows-vCPU-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU\",\"operatingSystem\":\"Windows\",\"operation\":\"\"},\"sku\":\"58AWBVRMPKQ3HVG6\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"58AWBVRMPKQ3HVG6.JRTCKXETXF\":{\"priceDimensions\":{\"58AWBVRMPKQ3HVG6.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Windows - vCPU - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"58AWBVRMPKQ3HVG6.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0465520000\"}}},\"sku\":\"58AWBVRMPKQ3HVG6\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per vCPU per hour\",\"cpuArchitecture\":\"ARM\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-ARM-vCPU-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU\",\"operation\":\"\"},\"sku\":\"5UAMYEN99PSY23D6\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"5UAMYEN99PSY23D6.JRTCKXETXF\":{\"priceDimensions\":{\"5UAMYEN99PSY23D6.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - ARM - vCPU - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"5UAMYEN99PSY23D6.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0323800000\"}}},\"sku\":\"5UAMYEN99PSY23D6\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-vCPU-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU\",\"operation\":\"\"},\"sku\":\"EKCCQUSFCBDDTKTS\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"EKCCQUSFCBDDTKTS.JRTCKXETXF\":{\"priceDimensions\":{\"EKCCQUSFCBDDTKTS.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - vCPU - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"EKCCQUSFCBDDTKTS.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0404800000\"}}},\"sku\":\"EKCCQUSFCBDDTKTS\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"OS License Fee per vCPU per hour\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-Windows-OS-Hours:perCPU\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"cputype\":\"perCPU OS License Fee\",\"operatingSystem\":\"Windows\",\"operation\":\"\"},\"sku\":\"HTBGRCKVHYXUYEAC\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"HTBGRCKVHYXUYEAC.JRTCKXETXF\":{\"priceDimensions\":{\"HTBGRCKVHYXUYEAC.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Windows - OS - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"HTBGRCKVHYXUYEAC.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0460000000\"}}},\"sku\":\"HTBGRCKVHYXUYEAC\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"memorytype\":\"perGB\",\"operation\":\"\"},\"sku\":\"NTC2QZFJW79R5XVK\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"NTC2QZFJW79R5XVK.JRTCKXETXF\":{\"priceDimensions\":{\"NTC2QZFJW79R5XVK.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Memory - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"NTC2QZFJW79R5XVK.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0044450000\"}}},\"sku\":\"NTC2QZFJW79R5XVK\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per GB per hour\",\"cpuArchitecture\":\"ARM\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-ARM-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"memorytype\":\"perGB\",\"operation\":\"\"},\"sku\":\"TAE28FJERF797NWS\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"TAE28FJERF797NWS.JRTCKXETXF\":{\"priceDimensions\":{\"TAE28FJERF797NWS.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - ARM - Memory - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"TAE28FJERF797NWS.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0035600000\"}}},\"sku\":\"TAE28FJERF797NWS\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"usagetype\":\"USW2-Fargate-EphemeralStorage-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"operation\":\"\",\"storagetype\":\"default\"},\"sku\":\"TTWDU5N74ZQ26XMP\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"TTWDU5N74ZQ26XMP.JRTCKXETXF\":{\"priceDimensions\":{\"TTWDU5N74ZQ26XMP.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"GB-Hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Ephemeral Storage - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"TTWDU5N74ZQ26XMP.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0001110000\"}}},\"sku\":\"TTWDU5N74ZQ26XMP\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}",
"{\"product\":{\"productFamily\":\"Compute\",\"attributes\":{\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonECS\",\"resource\":\"per GB per hour\",\"tenancy\":\"Shared\",\"usagetype\":\"USW2-Fargate-Windows-GB-Hours\",\"locationType\":\"AWS Region\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Container Service\",\"memorytype\":\"perGB\",\"operatingSystem\":\"Windows\",\"operation\":\"\"},\"sku\":\"ZPUB3WBJWT6K7RSF\"},\"serviceCode\":\"AmazonECS\",\"terms\":{\"OnDemand\":{\"ZPUB3WBJWT6K7RSF.JRTCKXETXF\":{\"priceDimensions\":{\"ZPUB3WBJWT6K7RSF.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"hours\",\"endRange\":\"Inf\",\"description\":\"AWS Fargate - Windows - Memory - US West 2 (Oregon)\",\"appliesTo\":[],\"rateCode\":\"ZPUB3WBJWT6K7RSF.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0051117500\"}}},\"sku\":\"ZPUB3WBJWT6K7RSF\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261111234623\",\"publicationDate\":\"2026-11-11T23:46:23Z\"}"
]
}
+418
View File
@@ -0,0 +1,418 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use super::*;
use crate::models::project::{Arch, AwsWorkspaceConfig, StorageMode};
/// A real `GetProducts` answer for us-west-2 with [`FILTERS`] (recorded
/// 2026-11-16): the eight Fargate rows, Windows ones included.
const FIXTURE: &str = include_str!("fixtures/get-products-us-west-2.json");
fn fixture_items() -> Vec<String> {
let v: Value = serde_json::from_str(FIXTURE).unwrap();
v["PriceList"]
.as_array()
.unwrap()
.iter()
.map(|s| s.as_str().unwrap().to_string())
.collect()
}
const X86: FargateRates = FargateRates {
vcpu_hour: 0.04048,
gb_hour: 0.004445,
storage_gb_hour: 0.000111,
};
const ARM: FargateRates = FargateRates {
vcpu_hour: 0.03238,
gb_hour: 0.00356,
storage_gb_hour: 0.000111,
};
const RATES: RegionRates = RegionRates {
x86_64: Some(X86),
arm64: Some(ARM),
};
fn close(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-9
}
fn cfg(cpu: u32, memory_mib: u32, ephemeral_gib: u32) -> AwsWorkspaceConfig {
AwsWorkspaceConfig {
cpu,
memory_mib,
ephemeral_gib,
..Default::default()
}
}
// ---- estimate ----
#[test]
fn estimate_is_vcpus_plus_gbs_at_the_pinned_rates() {
// 1 vCPU, 4 GiB, 21 GiB disk (1 billed).
let e = estimate(&cfg(1024, 4096, 21), &X86);
assert!(
close(e.usd_per_hour, 0.04048 + 4.0 * 0.004445 + 0.000111),
"{e:?}"
);
// 0.25 vCPU, 0.5 GiB, the free 20 GiB only.
let e = estimate(&cfg(256, 512, 20), &X86);
assert!(
close(e.usd_per_hour, 0.25 * 0.04048 + 0.5 * 0.004445),
"{e:?}"
);
// 4 vCPU, 16 GiB on ARM, 200 GiB disk (180 billed).
let e = estimate(&cfg(4096, 16384, 200), &ARM);
assert!(
close(
e.usd_per_hour,
4.0 * 0.03238 + 16.0 * 0.00356 + 180.0 * 0.000111
),
"{e:?}"
);
}
#[test]
fn ephemeral_storage_is_billed_only_above_twenty_gib() {
let free = estimate(&cfg(1024, 2048, 20), &X86).usd_per_hour;
let one = estimate(&cfg(1024, 2048, 21), &X86).usd_per_hour;
let fifty = estimate(&cfg(1024, 2048, 50), &X86).usd_per_hour;
assert!(close(one - free, 0.000111));
assert!(close(fifty - free, 30.0 * 0.000111));
}
#[test]
fn notes_follow_spot_and_storage_and_the_sidecar_adds_nothing() {
let mut c = cfg(1024, 4096, 21);
c.storage = StorageMode::Ephemeral;
let base = estimate(&c, &X86);
assert!(!base.note_spot && !base.efs_billed_separately);
c.spot = true;
assert!(estimate(&c, &X86).note_spot);
for storage in [StorageMode::Hybrid, StorageMode::Efs] {
c.storage = storage;
assert!(estimate(&c, &X86).efs_billed_separately, "{storage:?}");
}
c.gateway_sidecar = true;
assert!(close(estimate(&c, &X86).usd_per_hour, base.usd_per_hour));
}
#[test]
fn rates_are_picked_by_arch() {
assert_eq!(RATES.for_arch(Arch::X86_64), Some(X86));
assert_eq!(RATES.for_arch(Arch::Arm64), Some(ARM));
}
// ---- parsing ----
#[test]
fn the_recorded_price_list_parses_to_x86_and_arm_rates() {
let rates = parse_price_list("us-west-2", &fixture_items()).expect("rates");
assert_eq!(rates, RATES);
}
#[test]
fn a_price_list_for_another_region_gives_nothing() {
assert_eq!(parse_price_list("eu-west-1", &fixture_items()), None);
}
#[test]
fn windows_rows_never_count() {
let items: Vec<String> = fixture_items()
.into_iter()
.filter(|s| s.contains("Windows"))
.collect();
assert_eq!(items.len(), 3);
assert_eq!(parse_price_list("us-west-2", &items), None);
}
#[test]
fn an_incomplete_architecture_is_none_and_storage_is_required() {
let no_arm: Vec<String> = fixture_items()
.into_iter()
.filter(|s| !s.contains("Fargate-ARM-"))
.collect();
let rates = parse_price_list("us-west-2", &no_arm).unwrap();
assert_eq!(rates.x86_64, Some(X86));
assert_eq!(rates.arm64, None);
let no_storage: Vec<String> = fixture_items()
.into_iter()
.filter(|s| !s.contains("EphemeralStorage"))
.collect();
assert_eq!(parse_price_list("us-west-2", &no_storage), None);
}
#[test]
fn garbage_and_bad_prices_are_skipped() {
let mut items = vec!["not json".to_string(), "{}".to_string()];
// A negative x86 vCPU price ahead of the real one is ignored.
let bad = fixture_items()
.into_iter()
.find(|s| s.contains("USW2-Fargate-vCPU-Hours"))
.unwrap()
.replace("0.0404800000", "-1");
items.push(bad);
items.extend(fixture_items());
assert_eq!(parse_price_list("us-west-2", &items), Some(RATES));
}
#[test]
fn the_filters_name_the_region_on_demand_and_fargate() {
assert_eq!(PRICING_REGION, "us-east-1");
assert_eq!(SERVICE_CODE, "AmazonECS");
assert!(FILTERS.contains(&("TERM_MATCH", "regionCode", "{region}")));
assert!(FILTERS.contains(&("TERM_MATCH", "termType", "OnDemand")));
assert!(FILTERS.contains(&("CONTAINS", "usagetype", "Fargate")));
}
// ---- the API, faked ----
struct FakeApi {
pages: Mutex<Vec<Result<Page, String>>>,
calls: AtomicUsize,
hang: bool,
}
impl FakeApi {
fn answering(pages: Vec<Result<Page, String>>) -> Self {
Self {
pages: Mutex::new(pages),
calls: AtomicUsize::new(0),
hang: false,
}
}
fn fixture() -> Self {
Self::answering(vec![Ok(Page {
items: fixture_items(),
next_token: None,
})])
}
fn denied() -> Self {
Self::answering(vec![Err("Access denied: pricing:GetProducts".to_string())])
}
fn hanging() -> Self {
Self {
hang: true,
..Self::answering(vec![])
}
}
fn calls(&self) -> usize {
self.calls.load(Ordering::SeqCst)
}
}
impl PricingApi for FakeApi {
async fn get_products(&self, region: &str, _token: Option<String>) -> Result<Page, String> {
assert_eq!(region, "us-west-2");
self.calls.fetch_add(1, Ordering::SeqCst);
if self.hang {
std::future::pending::<()>().await;
}
let mut pages = self.pages.lock().unwrap();
if pages.is_empty() {
return Err("no more pages".to_string());
}
pages.remove(0)
}
}
#[tokio::test]
async fn pages_are_joined() {
let items = fixture_items();
let (a, b) = items.split_at(4);
let api = FakeApi::answering(vec![
Ok(Page {
items: a.to_vec(),
next_token: Some("t".to_string()),
}),
Ok(Page {
items: b.to_vec(),
next_token: None,
}),
]);
assert_eq!(fetch_rates(&api, "us-west-2").await, Ok(RATES));
assert_eq!(api.calls(), 2);
}
// ---- the cache ----
fn at(secs: u64) -> SystemTime {
UNIX_EPOCH + Duration::from_secs(secs)
}
const T0: u64 = 1_800_000_000;
const DAY: u64 = 24 * 60 * 60;
fn cache() -> (tempfile::TempDir, RatesCache) {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().join("aws-pricing"));
(dir, cache)
}
#[test]
fn a_cache_file_is_fresh_for_a_day() {
let (_d, c) = cache();
assert_eq!(c.read("us-west-2", at(T0)), None);
c.write("us-west-2", &RATES, at(T0)).unwrap();
assert!(c.dir().join("us-west-2.json").is_file());
assert_eq!(c.read("us-west-2", at(T0 + DAY - 1)), Some((RATES, true)));
assert_eq!(c.read("us-west-2", at(T0 + DAY)), Some((RATES, false)));
// From the future (the clock went back): stale.
assert_eq!(c.read("us-west-2", at(T0 - 10)), Some((RATES, false)));
// Another region's file is not this one's.
assert_eq!(c.read("eu-west-1", at(T0)), None);
}
#[test]
fn a_corrupt_or_mislabelled_cache_file_is_no_cache() {
let (_d, c) = cache();
std::fs::create_dir_all(c.dir()).unwrap();
std::fs::write(c.dir().join("us-west-2.json"), "{ not json").unwrap();
assert_eq!(c.read("us-west-2", at(T0)), None);
c.write("eu-west-1", &RATES, at(T0)).unwrap();
std::fs::copy(
c.dir().join("eu-west-1.json"),
c.dir().join("us-west-2.json"),
)
.unwrap();
assert_eq!(c.read("us-west-2", at(T0)), None);
}
#[tokio::test]
async fn a_fresh_cache_answers_without_a_call() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let api = FakeApi::denied();
assert_eq!(
c.rates(Some(&api), "us-west-2", at(T0 + 60)).await,
Some(RATES)
);
assert_eq!(api.calls(), 0);
}
#[tokio::test]
async fn a_miss_fetches_and_writes_the_cache() {
let (_d, c) = cache();
let api = FakeApi::fixture();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, Some(RATES));
assert_eq!(api.calls(), 1);
assert_eq!(c.read("us-west-2", at(T0 + 1)), Some((RATES, true)));
}
#[tokio::test]
async fn an_expired_cache_is_fetched_again() {
let (_d, c) = cache();
let old = RegionRates {
x86_64: Some(FargateRates {
vcpu_hour: 1.0,
..X86
}),
arm64: None,
};
c.write("us-west-2", &old, at(T0)).unwrap();
let api = FakeApi::fixture();
assert_eq!(
c.rates(Some(&api), "us-west-2", at(T0 + DAY)).await,
Some(RATES)
);
assert_eq!(api.calls(), 1);
assert_eq!(c.read("us-west-2", at(T0 + DAY)), Some((RATES, true)));
}
#[tokio::test]
async fn a_corrupt_cache_is_fetched_again() {
let (_d, c) = cache();
std::fs::create_dir_all(c.dir()).unwrap();
std::fs::write(c.dir().join("us-west-2.json"), "garbage").unwrap();
let api = FakeApi::fixture();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, Some(RATES));
assert_eq!(c.read("us-west-2", at(T0)), Some((RATES, true)));
}
#[tokio::test]
async fn denied_is_none_and_is_not_asked_again_for_a_while() {
let (_d, c) = cache();
let api = FakeApi::denied();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, None);
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0 + 60)).await, None);
assert_eq!(api.calls(), 1, "a recent failure is not retried");
let later = T0 + RETRY_AFTER_FAILURE.as_secs();
assert_eq!(c.rates(Some(&api), "us-west-2", at(later)).await, None);
assert_eq!(api.calls(), 2);
assert!(!c.dir().join("us-west-2.json").exists());
}
#[tokio::test]
async fn a_failed_refresh_falls_back_to_the_stale_cache() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let api = FakeApi::denied();
assert_eq!(
c.rates(Some(&api), "us-west-2", at(T0 + 2 * DAY)).await,
Some(RATES)
);
assert_eq!(api.calls(), 1);
// No client at all (e.g. the connection's keys are gone): stale still answers.
assert_eq!(
c.rates(None::<&FakeApi>, "us-west-2", at(T0 + 2 * DAY))
.await,
Some(RATES)
);
}
#[tokio::test(start_paused = true)]
async fn a_hanging_api_times_out_to_none() {
let (_d, c) = cache();
let api = FakeApi::hanging();
let started = tokio::time::Instant::now();
assert_eq!(c.rates(Some(&api), "us-west-2", at(T0)).await, None);
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
assert_eq!(api.calls(), 1);
}
#[tokio::test(start_paused = true)]
async fn building_the_client_counts_against_the_same_timeout() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let started = tokio::time::Instant::now();
// The client never gets built (credentials that hang): stale answers at
// the fetch's own bound, not later.
let r = c
.rates_with::<FakeApi, FakeApi>(std::future::pending(), "us-west-2", at(T0 + 2 * DAY))
.await;
assert_eq!(r, Some(RATES));
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
// A fresh cache never builds a client at all.
let fresh = c
.rates_with::<FakeApi, FakeApi>(
async { panic!("no client is built for a fresh cache") },
"us-west-2",
at(T0 + 60),
)
.await;
assert_eq!(fresh, Some(RATES));
}
#[tokio::test]
async fn no_client_counts_as_a_failure_and_is_not_asked_again_for_a_while() {
let (_d, c) = cache();
let none = || async { None::<FakeApi> };
assert_eq!(c.rates_with::<FakeApi, FakeApi>(none(), "us-west-2", at(T0)).await, None);
// Within the back-off the client is not even built.
let r = c
.rates_with::<FakeApi, FakeApi>(
async { panic!("no client is built within the back-off") },
"us-west-2",
at(T0 + 60),
)
.await;
assert_eq!(r, None);
// After it, a working client is asked and clears the failure.
let api = FakeApi::fixture();
let later = T0 + RETRY_AFTER_FAILURE.as_secs();
assert_eq!(c.rates(Some(&api), "us-west-2", at(later)).await, Some(RATES));
assert_eq!(api.calls(), 1);
}
+20 -10
View File
@@ -18,7 +18,7 @@ use super::environments::{knowable, parse_environment_record, EnvironmentInfo, K
use super::error::{classify_sdk_error, AwsError};
use super::tasks::{find_project_task, TaskApi, STOP_REASON};
use super::tofu_modules::{
access_point_root, environment_cluster_name, environment_parameter_name, project_secret_name, task_family,
access_point_root, workspace_access_point_root, environment_cluster_name, environment_parameter_name, project_secret_name, task_family,
user_file_system_token, user_role_names,
};
use super::workspace::WorkspaceApi;
@@ -234,15 +234,18 @@ pub struct RemoveRequest<'a> {
/// missed it).
pub stored_task_arn: Option<&'a str>,
pub stored_access_point_id: Option<&'a str>,
/// Delete the EFS access point (ruling R9). The files stay on the
/// user's own file system (only their task role can read it) until
/// offboarding destroys it; a later start of the same project gets a new
/// access point onto the same directory.
/// The EFS-mode `/workspace` access point the store has (kept after a
/// switch to Hybrid or Ephemeral, Q1).
pub stored_workspace_access_point_id: Option<&'a str>,
/// Delete the EFS access points, home and `/workspace` (ruling R9). The
/// files stay on the user's own file system (only their task role can
/// read it) until offboarding destroys it; a later start of the same
/// project gets new access points onto the same directories.
pub discard_data: bool,
}
/// Stop the project's task, delete its secret (no recovery window, R10) and,
/// with `discard_data`, delete its access point (not the files, R9). Task definition revisions
/// with `discard_data`, delete its access points (not the files, R9). Task definition revisions
/// are left registered (R8). A task that can't be stopped stops the removal (nothing
/// billed may be left running); the rest is attempted in full and every
/// failure reported.
@@ -288,20 +291,27 @@ pub async fn remove_workspace_resources<T: TaskApi, W: WorkspaceApi, R: RuntimeA
failures.push(format!("secret {secret}: {e}"));
}
// 4. The access point, only when asked.
// 4. The access points (home, and EFS mode's `/workspace`), only when asked.
if req.discard_data {
let mut ids: Vec<String> = req.stored_access_point_id.map(String::from).into_iter().collect();
let mut ids: Vec<String> = [req.stored_access_point_id, req.stored_workspace_access_point_id]
.into_iter()
.flatten()
.map(String::from)
.collect();
let token = user_file_system_token(req.env, req.owner_key);
match work.find_file_system(&token).await {
Ok(Some(fs)) if fs.owner.as_deref() == Some(req.owner_user_id) => {
let root = access_point_root(req.owner_key, req.project_id);
let roots = [
access_point_root(req.owner_key, req.project_id),
workspace_access_point_root(req.owner_key, req.project_id),
];
match work.access_points(&fs.id).await {
Ok(points) => ids.extend(
points
.into_iter()
.filter(|a| {
a.file_system_id == fs.id
&& a.root_path.as_deref() == Some(root.as_str())
&& a.root_path.as_ref().is_some_and(|r| roots.contains(r))
&& a.owner.as_deref() == Some(req.owner_user_id)
&& a.project.as_deref() == Some(req.project_id)
})
+74 -1
View File
@@ -5,7 +5,7 @@ use aws_sdk_ecs::operation::run_task::RunTaskInput;
use super::*;
use crate::aws::tasks::TaskInfo;
use crate::aws::tofu_modules::owner_key;
use crate::aws::tofu_modules::{owner_key, workspace_access_point_root};
use crate::aws::workspace::{AccessPointRef, FileSystemRef, TaskDefinitionRef};
const REGION: &str = "eu-west-1";
@@ -112,6 +112,22 @@ impl WorkspaceApi for Fake {
) -> Result<String, AwsError> {
unreachable!()
}
async fn project_secret_value(&self, _n: &str) -> Result<Option<String>, AwsError> {
unreachable!()
}
async fn create_project_secret(
&self,
n: &str,
k: &str,
j: &str,
t: &[(String, String)],
) -> Result<Option<String>, AwsError> {
let _ = (n, k, j, t);
unreachable!()
}
async fn delete_project_secret(&self, _n: &str) -> Result<(), AwsError> {
unreachable!()
}
async fn put_project_secret(
&self,
_n: &str,
@@ -288,6 +304,7 @@ async fn removal_stops_the_task_leaves_the_revisions_and_schedules_the_secret()
project_id: PROJECT,
stored_task_arn: Some(&t0),
stored_access_point_id: Some("fsap-stored"),
stored_workspace_access_point_id: None,
discard_data: false,
};
remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap();
@@ -323,9 +340,65 @@ async fn a_failed_step_is_reported_but_the_rest_still_runs() {
project_id: PROJECT,
stored_task_arn: None,
stored_access_point_id: Some("fsap-1"),
stored_workspace_access_point_id: None,
discard_data: true,
};
let e = remove_workspace_resources(&fake, &fake, &fake, &r).await.unwrap_err();
assert!(e.to_string().contains("secret"), "{e}");
assert!(fake.calls().contains(&"delete-ap fsap-1".to_string()));
}
#[tokio::test]
async fn removal_with_discard_deletes_both_access_points_and_without_keeps_both() {
let ap = |id: &str, root: String| AccessPointRef {
id: id.into(),
file_system_id: "fs-1".into(),
root_path: Some(root),
owner: Some(USER.into()),
project: Some(PROJECT.into()),
state: Some("available".into()),
};
let points = vec![
ap("fsap-home", access_point_root(&ok(), PROJECT)),
ap("fsap-ws", workspace_access_point_root(&ok(), PROJECT)),
AccessPointRef {
owner: Some("AROAOTHER:mallory".into()),
..ap("fsap-foreign", workspace_access_point_root(&ok(), PROJECT))
},
AccessPointRef {
project: Some("another".into()),
..ap("fsap-other-project", workspace_access_point_root(&ok(), PROJECT))
},
];
let key = ok();
let mut r = RemoveRequest {
env: "dev",
region: REGION,
owner_user_id: USER,
owner_key: &key,
project_id: PROJECT,
stored_task_arn: None,
stored_access_point_id: Some("fsap-home"),
// Kept in the store after switching EFS -> Hybrid.
stored_workspace_access_point_id: Some("fsap-ws-stored"),
discard_data: false,
};
let keep = Fake { access_points: points.clone(), ..Default::default() };
remove_workspace_resources(&keep, &keep, &keep, &r).await.unwrap();
assert!(
keep.calls().iter().all(|c| !c.starts_with("delete-ap")),
"{:?}",
keep.calls()
);
r.discard_data = true;
let discard = Fake { access_points: points, ..Default::default() };
remove_workspace_resources(&discard, &discard, &discard, &r).await.unwrap();
let mut deleted: Vec<String> = discard
.calls()
.into_iter()
.filter_map(|c| c.strip_prefix("delete-ap ").map(String::from))
.collect();
deleted.sort();
assert_eq!(deleted, vec!["fsap-home", "fsap-ws", "fsap-ws-stored"]);
}
+10
View File
@@ -483,6 +483,11 @@ async fn stopped_error<A: TaskApi>(api: &A, logs: &TaskLogs, t: &TaskInfo) -> Aw
/// so an idle stop (`EssentialContainerExited`) is not mistaken for a crash.
pub const IDLE_EXIT_CODE: i32 = 75;
/// The entrypoint's exit status after the environment's lifetime cap stopped
/// the workspace (`TRIPLEC_MAX_LIFETIME_MINUTES`, enforced by the in-container
/// idle watcher — advisory, see the phase 6 constraints, Q8).
pub const LIFETIME_EXIT_CODE: i32 = 76;
/// Why a task stopped, as the app reports it (spec, "Lifecycle and status").
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopKind {
@@ -491,6 +496,8 @@ pub enum StopKind {
Exited(Option<String>),
/// The workspace's own idle stop ([`IDLE_EXIT_CODE`]).
Idle,
/// The environment's maximum workspace lifetime ([`LIFETIME_EXIT_CODE`]).
LifetimeCap,
/// A Fargate Spot interruption (P8).
Spot,
/// Never got going: the project goes to Error with this reason.
@@ -510,6 +517,9 @@ pub fn classify_stop(info: &TaskInfo) -> StopKind {
),
Some("UserInitiated") => StopKind::UserInitiated,
Some("EssentialContainerExited") if info.exit_code == Some(IDLE_EXIT_CODE) => StopKind::Idle,
Some("EssentialContainerExited") if info.exit_code == Some(LIFETIME_EXIT_CODE) => {
StopKind::LifetimeCap
}
_ => StopKind::Exited(info.stopped_reason.clone().filter(|r| !r.trim().is_empty())),
}
}
+6
View File
@@ -41,6 +41,7 @@ fn env_info() -> EnvironmentInfo {
log_group: "/triple-c/dev".into(),
network_mode: "public".into(),
tenancy: "strict".into(),
max_workspace_hours: 0,
}
}
@@ -651,6 +652,11 @@ fn classify_stop_follows_the_spec_table() {
// The entrypoint's idle stop exits with its own status.
let idle = TaskInfo { exit_code: Some(IDLE_EXIT_CODE), ..with(Some("EssentialContainerExited"), None) };
assert_eq!(classify_stop(&idle), StopKind::Idle);
// So does its lifetime-cap stop.
let capped = TaskInfo { exit_code: Some(LIFETIME_EXIT_CODE), ..with(Some("EssentialContainerExited"), None) };
assert_eq!(classify_stop(&capped), StopKind::LifetimeCap);
let user_capped = TaskInfo { exit_code: Some(LIFETIME_EXIT_CODE), ..with(Some("UserInitiated"), None) };
assert_eq!(classify_stop(&user_capped), StopKind::UserInitiated);
let crashed = TaskInfo { exit_code: Some(137), ..with(Some("EssentialContainerExited"), None) };
assert_eq!(classify_stop(&crashed), StopKind::Exited(None));
// Only with that stop code: a user stop of a task that exits 75 is a user stop.
+22 -1
View File
@@ -15,7 +15,7 @@ use crate::models::host_connection::validate_supported_region;
/// `locals.template_version` (and the `triple-c:template-version` default
/// tag) of `aws/terraform/environment`.
pub const ENVIRONMENT_MODULE_VERSION: u32 = 1;
pub const ENVIRONMENT_MODULE_VERSION: u32 = 2;
/// The same for the user-onboarding module.
#[allow(dead_code)] // the onboarding module's version; checked against its HCL by a test
pub const ONBOARD_MODULE_VERSION: u32 = 1;
@@ -202,6 +202,17 @@ pub fn access_point_root(owner_key: &str, project_id: &str) -> String {
format!("/triple-c/{owner_key}/{}", project_component(project_id))
}
/// The root of the project's EFS-mode `/workspace` access point (Q1):
/// `/triple-c-workspaces/<owner_key>/<project>`, a sibling tree of the home
/// roots (under [`access_point_root`] it would sit inside the home access
/// point). At most 100 characters.
pub fn workspace_access_point_root(owner_key: &str, project_id: &str) -> String {
format!(
"/triple-c-workspaces/{owner_key}/{}",
project_component(project_id)
)
}
/// The container's `awslogs-stream-prefix`; ECS names the stream
/// `<prefix>/workspace/<task id>`.
pub fn log_stream_prefix(owner_key: &str, project_id: &str) -> String {
@@ -274,14 +285,23 @@ fn is_aws_id(id: &str, prefix: &str) -> bool {
})
}
/// The longest lifetime cap an environment can carry, in hours (a week).
pub const MAX_WORKSPACE_HOURS: u32 = 168;
/// The environment module's `terraform.tfvars.json`. Applies the same rules
/// as the module's variable validations, so a bad value fails here first.
pub fn environment_vars(
env: &str,
region: &str,
network: &NetworkChoice,
max_workspace_hours: u32,
) -> Result<serde_json::Value, String> {
let env = validate_environment_name(env)?;
if max_workspace_hours > MAX_WORKSPACE_HOURS {
return Err(format!(
"The maximum workspace lifetime is at most {MAX_WORKSPACE_HOURS} hours (0 = no cap)"
));
}
let region = validate_supported_region(region)?;
let (mode, vpc_id, subnet_ids, assign_public_ip) = match network {
NetworkChoice::Public => ("public", String::new(), Vec::new(), false),
@@ -322,6 +342,7 @@ pub fn environment_vars(
"vpc_id": vpc_id,
"subnet_ids": subnet_ids,
"assign_public_ip": assign_public_ip,
"max_workspace_hours": max_workspace_hours,
}))
}
+42 -10
View File
@@ -7,7 +7,7 @@ use serde_json::json;
use super::*;
use crate::aws::policies::PERMISSIONS_BOUNDARY_NAME;
const RECORD_FIELDS: [&str; 11] = [
const RECORD_FIELDS: [&str; 12] = [
"schema",
"name",
"template_version",
@@ -19,9 +19,10 @@ const RECORD_FIELDS: [&str; 11] = [
"log_group",
"network_mode",
"tenancy",
"max_workspace_hours",
];
const VARIABLES: [&str; 7] = [
const VARIABLES: [&str; 8] = [
"environment_name",
"region",
"network_mode",
@@ -29,6 +30,7 @@ const VARIABLES: [&str; 7] = [
"subnet_ids",
"assign_public_ip",
"log_retention_days",
"max_workspace_hours",
];
/// Every `.tf` file of `kind`, parsed and merged into one body.
@@ -345,7 +347,7 @@ fn the_onboarding_completion_output_waits_for_every_resource() {
#[test]
fn the_module_versions_agree() {
assert_eq!(ENVIRONMENT_MODULE_VERSION, 1);
assert_eq!(ENVIRONMENT_MODULE_VERSION, 2);
assert_eq!(ONBOARD_MODULE_VERSION, 1);
let body = parsed(ModuleKind::Environment);
let locals: Vec<_> = top_blocks(&body, "locals")
@@ -591,7 +593,7 @@ fn module_files_ship_the_lock_file() {
#[test]
fn environment_vars_cover_every_network_mode() {
let vars = environment_vars("dev", "us-west-2", &NetworkChoice::Public).unwrap();
let vars = environment_vars("dev", "us-west-2", &NetworkChoice::Public, 0).unwrap();
assert_eq!(
vars,
json!({
@@ -601,9 +603,11 @@ fn environment_vars_cover_every_network_mode() {
"vpc_id": "",
"subnet_ids": [],
"assign_public_ip": false,
"max_workspace_hours": 0,
})
);
let vars = environment_vars("dev", "us-west-2", &NetworkChoice::PrivateNat).unwrap();
let vars = environment_vars("dev", "us-west-2", &NetworkChoice::PrivateNat, 8).unwrap();
assert_eq!(vars["max_workspace_hours"], 8);
assert_eq!(vars["network_mode"], "private-nat");
assert_eq!(vars["subnet_ids"], json!([]));
@@ -613,7 +617,8 @@ fn environment_vars_cover_every_network_mode() {
assign_public_ip: true,
};
let two = ["subnet-0123456789abcdef0", "subnet-89abcdef"];
let vars = environment_vars("dev", "eu-central-1", &existing("vpc-0a1b2c3d", &two)).unwrap();
let vars =
environment_vars("dev", "eu-central-1", &existing("vpc-0a1b2c3d", &two), 168).unwrap();
assert_eq!(
vars,
json!({
@@ -623,6 +628,7 @@ fn environment_vars_cover_every_network_mode() {
"vpc_id": "vpc-0a1b2c3d",
"subnet_ids": two,
"assign_public_ip": true,
"max_workspace_hours": 168,
})
);
// Every variable the app sets is one the module declares.
@@ -648,13 +654,14 @@ fn environment_vars_cover_every_network_mode() {
existing("vpc-0a1b2c3d", &["subnet-0123456789abcdef0", "sg-89abcdef"]),
] {
assert!(
environment_vars("dev", "us-west-2", &bad).is_err(),
environment_vars("dev", "us-west-2", &bad, 0).is_err(),
"{bad:?} accepted"
);
}
assert!(environment_vars("Dev", "us-west-2", &NetworkChoice::Public).is_err());
assert!(environment_vars("dev", "us-west-2;", &NetworkChoice::Public).is_err());
assert!(environment_vars("dev", "cn-north-1", &NetworkChoice::Public).is_err());
assert!(environment_vars("Dev", "us-west-2", &NetworkChoice::Public, 0).is_err());
assert!(environment_vars("dev", "us-west-2;", &NetworkChoice::Public, 0).is_err());
assert!(environment_vars("dev", "cn-north-1", &NetworkChoice::Public, 0).is_err());
assert!(environment_vars("dev", "us-west-2", &NetworkChoice::Public, 169).is_err());
// The command layer receives the choice as tagged JSON.
let parsed: NetworkChoice = serde_json::from_value(json!({
@@ -2121,3 +2128,28 @@ fn every_module_write_has_an_admin_check_and_vice_versa() {
.collect();
assert!(stale.is_empty(), "APP_ONLY_WRITES is stale: {stale:?}");
}
#[test]
fn the_lifetime_cap_maximum_is_the_same_in_rust_typescript_and_hcl() {
let max = MAX_WORKSPACE_HOURS.to_string();
let vars = module_files(ModuleKind::Environment)
.iter()
.find(|(f, _)| *f == "variables.tf")
.unwrap()
.1;
let block = &vars[vars.find("variable \"max_workspace_hours\"").expect("the variable")..];
let block = &block[..block.find("\n}\n").unwrap()];
assert!(
block.contains(&format!("var.max_workspace_hours <= {max}")),
"the HCL validation's maximum: {block}"
);
assert!(block.contains(&format!("from 0 to {max}")), "{block}");
let ts = include_str!("../../../../src/components/settings/hosts/LifetimeCapField.tsx");
assert!(
ts.contains(&format!("export const MAX_WORKSPACE_HOURS = {max};")),
"LifetimeCapField.tsx's MAX_WORKSPACE_HOURS"
);
// And the record parser's message.
let envs = include_str!("../environments.rs");
assert!(envs.contains(&format!("from 0 to {max}")));
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -178,8 +178,9 @@ pub(crate) fn network_from_record(
subnet_ids: info.subnet_ids.clone(),
assign_public_ip: info.assign_public_ip,
};
// The same checks as the apply's variables.
environment_vars(&info.name, "us-east-1", &choice)?;
// The same checks as the apply's variables. Only the network is
// checked here, so the lifetime cap is a valid placeholder (0).
environment_vars(&info.name, "us-east-1", &choice, 0)?;
Ok(choice)
}
_ => Err("The environment record names an unknown network mode".into()),
@@ -1034,13 +1035,20 @@ pub async fn deploy_aws_environment(
region: String,
name: String,
network: NetworkChoice,
max_workspace_hours: Option<u32>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
let ctx = context(&state, &connection_id, &region).await?;
let found = detect(&ctx).await?;
deploy_refusal(&found, &name).map_err(other)?;
let vars = environment_vars(&name, &ctx.region, &network).map_err(other)?;
let vars = environment_vars(
&name,
&ctx.region,
&network,
max_workspace_hours.unwrap_or(0),
)
.map_err(other)?;
let key = DeploymentKey::Environment(name);
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
@@ -1052,6 +1060,7 @@ pub async fn update_aws_environment(
connection_id: String,
region: String,
name: String,
max_workspace_hours: Option<u32>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
@@ -1063,7 +1072,9 @@ pub async fn update_aws_environment(
.as_ref()
.ok_or_else(|| other("The environment has no readable record"))?;
let network = recorded_network(&ctx, info).await?;
let vars = environment_vars(&env.name, &ctx.region, &network).map_err(other)?;
// The recorded cap stays unless the caller changes it.
let hours = max_workspace_hours.unwrap_or(info.max_workspace_hours);
let vars = environment_vars(&env.name, &ctx.region, &network, hours).map_err(other)?;
let key = DeploymentKey::Environment(env.name.clone());
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
@@ -1077,6 +1088,7 @@ pub async fn repair_aws_environment(
region: String,
name: String,
network: Option<NetworkChoice>,
max_workspace_hours: Option<u32>,
app: AppHandle,
state: State<'_, AppState>,
) -> Result<String, AwsError> {
@@ -1092,7 +1104,11 @@ pub async fn repair_aws_environment(
))
}
};
let vars = environment_vars(&env.name, &ctx.region, &network).map_err(other)?;
// A valid record's cap stays unless the caller changes it.
let hours = max_workspace_hours
.or(env.info.as_ref().map(|i| i.max_workspace_hours))
.unwrap_or(0);
let vars = environment_vars(&env.name, &ctx.region, &network, hours).map_err(other)?;
let key = DeploymentKey::Environment(env.name.clone());
opened(launch(app, &ctx, &key, &vars, TofuOp::Apply).await)
}
@@ -1113,7 +1129,8 @@ pub async fn destroy_incomplete_aws_environment(
let env = destroy_refusal(&found, &name).map_err(other)?;
// A destroy plans the removal of everything in state whatever the
// network variables say; they only have to be valid.
let vars = environment_vars(&env.name, &ctx.region, &NetworkChoice::Public).map_err(other)?;
let vars =
environment_vars(&env.name, &ctx.region, &NetworkChoice::Public, 0).map_err(other)?;
let key = DeploymentKey::Environment(env.name.clone());
opened(launch(app, &ctx, &key, &vars, TofuOp::Destroy).await)
}
@@ -1372,6 +1389,7 @@ pub async fn save_aws_admin_bundle(
environment: Option<String>,
owner_id: Option<String>,
network: Option<NetworkChoice>,
max_workspace_hours: Option<u32>,
window: tauri::Window,
state: State<'_, AppState>,
) -> Result<Option<String>, AwsError> {
@@ -1386,8 +1404,13 @@ pub async fn save_aws_admin_bundle(
let (key, vars) = match kind {
ModuleKind::Environment => {
let env = need_env()?;
let vars = environment_vars(&env, &region_ok, &network.unwrap_or(NetworkChoice::Public))
.map_err(other)?;
let vars = environment_vars(
&env,
&region_ok,
&network.unwrap_or(NetworkChoice::Public),
max_workspace_hours.unwrap_or(0),
)
.map_err(other)?;
(DeploymentKey::Environment(env), vars)
}
ModuleKind::UserOnboard => {
@@ -32,6 +32,7 @@ fn info(name: &str, mode: &str, assign_public_ip: bool) -> EnvironmentInfo {
log_group: format!("/triple-c/{name}"),
network_mode: mode.into(),
tenancy: "strict".into(),
max_workspace_hours: 0,
}
}
@@ -165,6 +166,7 @@ fn update_reuses_the_recorded_network() {
"dev",
REGION,
&network_from_record(&existing, Some("vpc-0123456789abcdef0")).unwrap(),
0,
)
.unwrap();
assert_eq!(vars["network_mode"], "existing");
@@ -266,7 +268,7 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
#[test]
fn a_us_east_1_bundle_creates_its_bucket_without_a_location_constraint() {
let dir = tempfile::tempdir().unwrap();
let vars = environment_vars("dev", "us-east-1", &NetworkChoice::Public).unwrap();
let vars = environment_vars("dev", "us-east-1", &NetworkChoice::Public, 0).unwrap();
write_bundle(dir.path(), Deployment::Environment("dev"), ACCOUNT, "us-east-1", &vars).unwrap();
let readme = fs::read_to_string(dir.path().join("README.txt")).unwrap();
assert!(readme.contains("aws s3api create-bucket"), "{readme}");
+118 -1
View File
@@ -2,15 +2,18 @@
//! sign-in and the permission check.
use std::future::Future;
use std::sync::OnceLock;
use std::time::SystemTime;
use tauri::State;
use crate::aws::permissions::{check_permissions, PermissionReport, SdkPermissionApi};
use crate::aws::policies::{policy_files, Tier};
use crate::aws::pricing::{self, HourlyEstimate, RatesCache, SdkPricingApi};
use crate::aws::{caller_identity, sdk_config, sdk_config_for_keys, AwsError, CallerIdentity};
use crate::models::{
validate_connection_name, validate_profile_name, validate_supported_region, AppSettings, AwsAuth,
HostConnection, HostKind,
AwsWorkspaceConfig, HostConnection, HostKind,
};
use crate::storage::secure::{self, AwsKeys};
use crate::storage::settings_store::SettingsStore;
@@ -301,6 +304,65 @@ pub fn aws_policy_document(tier: Tier) -> Vec<PolicyFile> {
.collect()
}
/// The cost estimate's rate cache, `<data dir>/triple-c/aws-pricing/`, shared
/// by every call (it also remembers recent failures). `None` without a data
/// folder.
fn pricing_cache() -> Option<&'static RatesCache> {
static CACHE: OnceLock<Option<RatesCache>> = OnceLock::new();
CACHE
.get_or_init(|| pricing::default_cache_dir().map(RatesCache::new))
.as_ref()
}
/// The estimated on-demand hourly cost of `config` as a Fargate task in
/// `region` (Q5). It takes the config rather than a project id because
/// Project Home's Host settings ask while the user edits, and the Overview
/// asks for the saved one; `connection_id` is the project's connection, whose
/// credentials sign the Price List call (made in us-east-1). **Never an
/// error:** a bad region, a missing connection, a denied or slow call (8 s
/// bound) or an unreadable price list is `None`, shown as "Estimate
/// unavailable". Rates are cached a day per region.
#[tauri::command]
pub async fn aws_cost_estimate(
connection_id: String,
region: String,
config: AwsWorkspaceConfig,
state: State<'_, AppState>,
) -> Result<Option<HourlyEstimate>, String> {
let Some(cache) = pricing_cache() else {
return Ok(None);
};
let settings = state.settings_store.get();
Ok(cost_estimate(cache, &connection_id, &region, &config, &settings, SystemTime::now()).await)
}
async fn cost_estimate(
cache: &RatesCache,
connection_id: &str,
region: &str,
config: &AwsWorkspaceConfig,
settings: &AppSettings,
now: SystemTime,
) -> Option<HourlyEstimate> {
let region = validate_supported_region(region).ok()?;
let rates = match cache.read(&region, now) {
Some((rates, true)) => Some(rates),
_ => {
// Only now touch the connection (a keys connection reads the
// keychain); without one, a stale cache still answers.
// Built inside the fetch's 8 s bound: reading a profile's
// credentials can hang as well (an SSO or process provider).
let api = async {
let conn = aws_conn(connection_id, settings).ok()?;
let cfg = sdk_config(&conn, pricing::PRICING_REGION).await.ok()?;
Some(SdkPricingApi::new(&cfg))
};
cache.rates_with::<SdkPricingApi, _>(api, &region, now).await
}
}?;
Some(pricing::estimate(config, &rates.for_arch(config.arch)?))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -557,4 +619,59 @@ mod tests {
"workspace-user.json"
);
}
fn sample_rates() -> pricing::RegionRates {
let x86 = pricing::FargateRates {
vcpu_hour: 0.04,
gb_hour: 0.004,
storage_gb_hour: 0.0001,
};
pricing::RegionRates {
x86_64: Some(x86),
arm64: None,
}
}
#[tokio::test]
async fn the_cost_estimate_answers_from_a_fresh_cache_without_a_connection() {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().to_path_buf());
let now = SystemTime::now();
cache.write("us-west-2", &sample_rates(), now).unwrap();
let settings = AppSettings::default();
let config = AwsWorkspaceConfig {
cpu: 1024,
memory_mib: 2048,
ephemeral_gib: 20,
spot: true,
..Default::default()
};
let e = cost_estimate(&cache, "gone", "us-west-2", &config, &settings, now)
.await
.expect("estimate");
assert!((e.usd_per_hour - (0.04 + 2.0 * 0.004)).abs() < 1e-9);
assert!(e.note_spot && e.efs_billed_separately);
// An architecture the price list lacked has no estimate.
let arm = AwsWorkspaceConfig {
arch: crate::models::Arch::Arm64,
..config.clone()
};
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &arm, &settings, now).await, None);
}
#[tokio::test]
async fn the_cost_estimate_is_none_never_an_error() {
let dir = tempfile::tempdir().unwrap();
let cache = RatesCache::new(dir.path().to_path_buf());
let settings = AppSettings::default();
let config = AwsWorkspaceConfig::default();
let now = SystemTime::now();
// No cache and no such connection.
assert_eq!(cost_estimate(&cache, "gone", "us-west-2", &config, &settings, now).await, None);
// A region that isn't one (it would name the cache file).
cache.write("us-west-2", &sample_rates(), now).unwrap();
for bad in ["../us-west-2", "", "us-west-2/x"] {
assert_eq!(cost_estimate(&cache, "gone", bad, &config, &settings, now).await, None);
}
}
}
+419 -28
View File
@@ -641,7 +641,9 @@ pub async fn add_project(
aws: Option<AwsWorkspaceConfig>,
state: State<'_, AppState>,
) -> Result<Project, String> {
let project = new_project(name, paths, host_id, aws_placement, aws, &state.settings_store.get().hosts)?;
let settings = state.settings_store.get();
let project = new_project(name, paths, host_id, aws_placement, aws, &settings.hosts)?;
validate_gateway_sidecar_update(&Project::new(String::new(), Vec::new()), &project, &settings.gateway)?;
// Nothing can have been blanked on a project that did not exist a line ago.
store_secrets_for_project(&project, &[])?;
state.projects_store.add(project)
@@ -715,7 +717,7 @@ pub async fn remove_project(
// An AWS project's task and resources go first, and a failure keeps the
// project: its record is the only handle on a billed task and its
// secret. Its EFS access point goes only when asked (the files stay on the
// secret. Its EFS access points go only when asked (the files stay on the
// user's own file system until offboarding).
if let Some(project) = state.projects_store.get(&project_id).filter(|p| p.is_aws()) {
let settings = state.settings_store.get();
@@ -1061,6 +1063,7 @@ fn pending_cleanup_is_stale(recorded_at: &str, now: chrono::DateTime<chrono::Utc
#[tauri::command]
pub async fn update_project(
project: serde_json::Value,
app_handle: tauri::AppHandle,
state: State<'_, AppState>,
) -> Result<Project, String> {
// Taken as raw JSON, then deserialised, for one reason: a secret field that
@@ -1122,7 +1125,11 @@ pub async fn update_project(
// [`crate::models::validate_env_vars_update`].
crate::models::validate_env_vars_update(&stored.custom_env_vars, &project.custom_env_vars)?;
validate_aws_update(&stored, &project, &state.settings_store.get().hosts)?;
{
let settings = state.settings_store.get();
validate_aws_update(&stored, &project, &settings.hosts)?;
validate_gateway_sidecar_update(&stored, &project, &settings.gateway)?;
}
restore_store_owned_fields(&mut project, &stored);
project.updated_at = chrono::Utc::now().to_rfc3339();
@@ -1144,9 +1151,19 @@ pub async fn update_project(
// lock against the record as it is *now*, so a marketplace install, a
// status change or a container id landing since `stored` was read is kept
// rather than written over.
state
let mappings_changed = stored.port_mappings != project.port_mappings;
let saved = state
.projects_store
.update_restoring(project, restore_store_owned_fields)
.update_restoring(project, restore_store_owned_fields)?;
// AWS (Q6): a connected workspace's forwards follow the saved mappings
// now — there is no container to recreate. Nothing when not connected.
if saved.is_aws() && mappings_changed {
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &saved.id).await {
emit_ports_warning(&app_handle, &saved.id, &saved.name, &detail);
}
}
Ok(saved)
}
/// Restore onto `project` the fields whose value belongs to the store rather
@@ -1191,9 +1208,30 @@ fn restore_store_owned_fields(project: &mut Project, stored: &Project) {
aws.task_arn = s.and_then(|a| a.task_arn.clone());
aws.access_point_id = s.and_then(|a| a.access_point_id.clone());
aws.task_definition_arn = s.and_then(|a| a.task_definition_arn.clone());
aws.workspace_access_point_id = s.and_then(|a| a.workspace_access_point_id.clone());
}
}
/// The LLM gateway sidecar (Q7) needs the OpenAI-compatible backend and a
/// model in Settings → Model Gateway. A save is refused when the sidecar is
/// on without that backend, or is being turned on with no gateway model; a
/// project that already runs it still saves after the models were removed
/// from Settings (its start says what is missing).
fn validate_gateway_sidecar_update(
stored: &Project,
project: &Project,
gateway: &crate::models::gateway_settings::GatewaySettings,
) -> Result<(), String> {
let Some(problem) = crate::aws::workspace::gateway_sidecar_problem(project, gateway) else {
return Ok(());
};
let was_on = stored.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
if was_on && project.backend == crate::models::project::Backend::OpenAiCompatible {
return Ok(());
}
Err(problem)
}
/// Checks an incoming save's host placement against what is stored and the
/// configured connections.
fn validate_aws_update(
@@ -1210,6 +1248,13 @@ fn validate_aws_update(
crate::models::validate_region(&place.region)?;
crate::aws::tofu_modules::validate_environment_name(&place.environment)?;
crate::models::validate_aws_workspace(cfg)?;
// Q6: forwarded over loopback, TCP only, never a reserved port
// (rows the save adds or edits; everything when newly on AWS).
crate::host::port_forwards::validate_aws_port_mappings_update(
stored.aws.as_ref().map(|a| (stored.port_mappings.as_slice(), a.gateway_sidecar)),
&project.port_mappings,
cfg.gateway_sidecar,
)?;
}
_ => {
return Err(
@@ -1226,7 +1271,10 @@ fn validate_aws_update(
// connection's account and region; moving the project would orphan them.
if moved
&& stored.aws.as_ref().is_some_and(|a| {
a.task_arn.is_some() || a.access_point_id.is_some() || a.task_definition_arn.is_some()
a.task_arn.is_some()
|| a.access_point_id.is_some()
|| a.workspace_access_point_id.is_some()
|| a.task_definition_arn.is_some()
})
{
return Err(
@@ -1459,7 +1507,7 @@ async fn start_project_container_locked(
// id stored above is the tunnel's endpoint URL, which is what the death
// watch reports a stopped task as.
if project.is_aws() {
if let Some((task_arn, access_point_id, task_definition_arn)) =
if let Some((task_arn, access_point_id, task_definition_arn, workspace_access_point_id)) =
crate::host::tunnels::runtime(&project_id)
{
state.projects_store.set_aws_runtime(
@@ -1467,6 +1515,7 @@ async fn start_project_container_locked(
Some(task_arn),
access_point_id,
Some(task_definition_arn),
workspace_access_point_id,
)?;
}
}
@@ -1486,11 +1535,117 @@ async fn start_project_container_locked(
.await;
}
// AWS (Q6): the project's port mappings become loopback forwards into
// the workspace; a mapping that can't open is a warning, the rest work.
if project.is_aws() {
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &project_id).await {
emit_ports_warning(&app_handle, &project_id, &project.name, &detail);
}
}
// AWS, copy-at-connect (Q2): every connect copies the credentials in,
// in the background (the clone waits for them); a failure is a warning.
if project.is_aws() {
let (handle, id, name) = (app_handle.clone(), project_id.clone(), project.name.clone());
crate::host::aws_host::spawn_connect_push(&project, &settings, move |detail| {
emit_connect_warning(&handle, &id, &name, &detail)
});
}
// AWS: the start is done (Running, lock released when this returns);
// the background clone and setup script are followed by a background
// task that only reports a failure (ruling R5). Never for Docker.
if project.is_aws() {
let (handle, id, name) = (app_handle.clone(), project_id.clone(), project.name.clone());
crate::host::aws_host::spawn_follow(&project, move |detail| {
emit_workspace_warning(&handle, &id, &name, &detail)
});
}
project.container_id = Some(container_id);
project.status = ProjectStatus::Running;
Ok(project)
}
/// The follower's warning channel (phase 6, Q4/R5): a started workspace
/// whose clone or setup script failed (or still ran after 35 minutes) is
/// reported as a `workspace-warning` event `{project_id, message, detail}`;
/// the frontend shows it as a warning toast.
fn emit_workspace_warning(app_handle: &tauri::AppHandle, project_id: &str, name: &str, detail: &str) {
let _ = app_handle.emit(
"workspace-warning",
serde_json::json!({
"project_id": project_id,
"message": format!("“{name}” started, but not everything went well"),
"detail": detail,
}),
);
}
/// Copy-at-connect's warning (Q2): the credentials could not be copied into
/// a connected workspace. Same `workspace-warning` event as the follower's,
/// with its own `topic` so neither replaces the other's toast.
fn emit_connect_warning(app_handle: &tauri::AppHandle, project_id: &str, name: &str, detail: &str) {
let _ = app_handle.emit("workspace-warning", connect_warning_payload(project_id, name, detail));
}
fn connect_warning_payload(project_id: &str, name: &str, detail: &str) -> serde_json::Value {
serde_json::json!({
"project_id": project_id,
"message": format!("“{name}”: the credentials could not be copied into the workspace"),
"detail": detail,
"topic": "connect",
})
}
/// Port mappings that could not be forwarded to a connected AWS workspace
/// (Q6): a busy host port, a host port mapped twice. Same `workspace-warning`
/// event, with its own `topic` so it does not replace the other warnings.
fn emit_ports_warning(app_handle: &tauri::AppHandle, project_id: &str, name: &str, detail: &str) {
let _ = app_handle.emit(
"workspace-warning",
serde_json::json!({
"project_id": project_id,
"message": format!("“{name}”: some port mappings could not be opened"),
"detail": detail,
"topic": "ports",
}),
);
}
/// An AWS workspace's clone and setup-script status for Project Home
/// (background exec: never activity for idle stop). `None` for a local
/// project, a workspace with no tunnel, or an image without the reader.
#[tauri::command]
pub async fn aws_workspace_status(
project_id: String,
state: State<'_, AppState>,
) -> Result<Option<crate::host::aws_host::WorkspaceStatus>, String> {
let project = state
.projects_store
.get(&project_id)
.ok_or_else(|| format!("Project {} not found", project_id))?;
crate::host::aws_host::workspace_status(&project).await
}
/// The lifetime cap (hours; 0 = none) of an AWS project's environment, read
/// from its record, for Host settings. `None` for a local project or when the
/// record can't be read; never an error.
#[tauri::command]
pub async fn aws_environment_max_hours(
project_id: String,
state: State<'_, AppState>,
) -> Result<Option<u32>, String> {
let Some(project) = state.projects_store.get(&project_id) else {
return Ok(None);
};
if !project.is_aws() {
return Ok(None);
}
let settings = state.settings_store.get();
Ok(crate::host::aws_host::environment_max_hours(&project, &settings).await)
}
#[tauri::command]
pub async fn stop_project_container(
project_id: String,
@@ -1518,6 +1673,9 @@ pub async fn stop_project_container(
}
state.projects_store.update_status(&project_id, ProjectStatus::Stopping)?;
// AWS: the background clone/setup follower ends now (it holds no lock;
// closing the tunnel would end it too).
crate::host::tunnels::cancel_follow(&project_id);
// AWS: the terminals and the auth bridge close *before* the stop guard's
// push, so nothing written after its snapshot is silently dropped (the
@@ -1585,6 +1743,7 @@ pub async fn stop_project_container(
None,
a.access_point_id.clone(),
a.task_definition_arn.clone(),
a.workspace_access_point_id.clone(),
)?;
}
state.projects_store.set_container_id(&project_id, None)?;
@@ -1693,24 +1852,37 @@ pub async fn remove_aws_resources(
ProjectHost::for_project(&project)
.remove_aws_resources(&project, &settings, discard_data)
.await?;
state
.projects_store
.set_aws_runtime(&project_id, None, access_point_after_removal(&project, discard_data), None)?;
let (access_point_id, workspace_access_point_id) =
access_points_after_removal(&project, discard_data);
state.projects_store.set_aws_runtime(
&project_id,
None,
access_point_id,
None,
workspace_access_point_id,
)?;
state
.projects_store
.get(&project_id)
.ok_or_else(|| format!("Project {} not found", project_id))
}
/// The access point a removal leaves stored: kept when the saved files were
/// kept (it is still there, a later start reuses it and a later removal
/// with `discard_data` deletes it; and while it is stored the project can't
/// move away from it — [`validate_aws_update`]), gone when they were not.
fn access_point_after_removal(project: &Project, discard_data: bool) -> Option<String> {
if discard_data {
return None;
/// The access points (home, EFS `/workspace`) a removal leaves stored: kept
/// when the saved files were kept (they are still there, a later start
/// reuses them and a later removal with `discard_data` deletes them; and
/// while one is stored the project can't move away from it —
/// [`validate_aws_update`]), gone when they were not.
fn access_points_after_removal(
project: &Project,
discard_data: bool,
) -> (Option<String>, Option<String>) {
match project.aws.as_ref() {
Some(a) if !discard_data => (
a.access_point_id.clone(),
a.workspace_access_point_id.clone(),
),
_ => (None, None),
}
project.aws.as_ref().and_then(|a| a.access_point_id.clone())
}
fn check_aws_resources_removable(project: &Project) -> Result<(), String> {
@@ -1949,12 +2121,26 @@ pub async fn reconcile_project_statuses(
/// How many AWS projects reconcile at once.
const AWS_RECONCILE_CONCURRENCY: usize = 4;
/// Whether an AWS project's status must be checked against ECS at startup:
/// anything but a clean Stopped — and a Stopped one that still has a task
/// ARN (Stop clears it, so the stored ARN means a start or stop never
/// finished and a billed task may be running).
pub(crate) fn needs_aws_reconcile(p: &Project) -> bool {
match p.status {
ProjectStatus::Running | ProjectStatus::Error | ProjectStatus::Starting | ProjectStatus::Stopping => true,
ProjectStatus::Stopped => p.aws.as_ref().is_some_and(|a| a.task_arn.is_some()),
}
}
/// Reconcile every AWS project against ECS (the authority), independently of
/// Docker: an app started without Docker still learns which tasks run and
/// re-opens their tunnels. One bounded DescribeTasks per project, a few at a
/// time; a live task's tunnel is re-opened in the background.
#[tauri::command]
pub async fn reconcile_aws_projects(state: State<'_, AppState>) -> Result<Vec<Project>, String> {
pub async fn reconcile_aws_projects(
app_handle: tauri::AppHandle,
state: State<'_, AppState>,
) -> Result<Vec<Project>, String> {
use futures_util::StreamExt;
let projects: Vec<Project> = state.projects_store.list().into_iter().filter(|p| p.is_aws()).collect();
@@ -1972,20 +2158,32 @@ pub async fn reconcile_aws_projects(state: State<'_, AppState>) -> Result<Vec<Pr
let settings = state.settings_store.get();
let store = state.projects_store.clone();
futures_util::stream::iter(projects.into_iter().filter(|p| {
matches!(
p.status,
ProjectStatus::Running | ProjectStatus::Error | ProjectStatus::Starting | ProjectStatus::Stopping
) && crate::project_lock::held(&p.id).is_none()
needs_aws_reconcile(p) && crate::project_lock::held(&p.id).is_none()
}))
.for_each_concurrent(AWS_RECONCILE_CONCURRENCY, |project| {
let settings = settings.clone();
let store = store.clone();
let handle = app_handle.clone();
async move {
// The store's current copy: the id above may have changed.
let project = store.get(&project.id).unwrap_or(project);
let (id, name) = (project.id.clone(), project.name.clone());
let ports_handle = handle.clone();
let (ports_id, ports_name) = (id.clone(), name.clone());
let on_ports_warning = move |detail: String| {
emit_ports_warning(&ports_handle, &ports_id, &ports_name, &detail)
};
let on_connect_warning =
move |detail: String| emit_connect_warning(&handle, &id, &name, &detail);
if tokio::time::timeout(
AWS_RECONCILE_TIMEOUT,
crate::host::aws_host::reconcile(&project, &settings, store),
crate::host::aws_host::reconcile(
&project,
&settings,
store,
on_connect_warning,
on_ports_warning,
),
)
.await
.is_err()
@@ -2004,6 +2202,15 @@ pub async fn reconcile_aws_projects(state: State<'_, AppState>) -> Result<Vec<Pr
#[cfg(test)]
mod tests {
#[test]
fn the_connect_warning_has_its_own_toast_topic() {
let v = super::connect_warning_payload("p1", "demo", "upload failed");
assert_eq!(v["topic"], "connect");
assert_eq!(v["project_id"], "p1");
assert_eq!(v["detail"], "upload failed");
assert!(v["message"].as_str().unwrap().contains("demo"));
}
use super::*;
// ── Pending-cleanup aging ────────────────────────────────────────────
@@ -2616,12 +2823,14 @@ mod tests {
sc.task_arn = Some("arn:task".into());
sc.access_point_id = Some("fsap-1".into());
sc.task_definition_arn = Some("arn:td".into());
sc.workspace_access_point_id = Some("fsap-w".into());
stored.aws = Some(sc.clone());
let mut pc = crate::models::AwsWorkspaceConfig::default();
pc.cpu = 2048;
pc.memory_mib = 8192;
pc.task_arn = Some("evil".into());
pc.access_point_id = None;
pc.workspace_access_point_id = None;
payload.aws = Some(pc);
restore_store_owned_fields(&mut payload, &stored);
@@ -2630,6 +2839,7 @@ mod tests {
assert_eq!(a.task_arn, sc.task_arn);
assert_eq!(a.access_point_id, sc.access_point_id);
assert_eq!(a.task_definition_arn, sc.task_definition_arn);
assert_eq!(a.workspace_access_point_id, Some("fsap-w".into()));
assert_eq!(a.cpu, 2048, "the edit itself goes through");
}
@@ -2653,6 +2863,51 @@ mod tests {
p
}
#[test]
fn the_gateway_sidecar_needs_the_openai_backend_and_a_gateway_model() {
use crate::models::gateway_settings::{GatewayModel, GatewaySettings};
use crate::models::project::Backend;
let with_models = GatewaySettings {
models: vec![GatewayModel { name: "gpt".into(), model_id: "gpt-5".into() }],
..Default::default()
};
let half = GatewaySettings {
models: vec![GatewayModel { name: "gpt".into(), model_id: " ".into() }],
..Default::default()
};
let off = aws_project();
let mut on = off.clone();
on.aws.as_mut().unwrap().gateway_sidecar = true;
// Wrong backend: always refused.
on.backend = Backend::Anthropic;
let e = validate_gateway_sidecar_update(&off, &on, &with_models).unwrap_err();
assert!(e.contains("OpenAI Compatible"), "{e}");
assert!(validate_gateway_sidecar_update(&on, &on, &with_models).is_err());
// Turning it on without a complete gateway model row: refused.
on.backend = Backend::OpenAiCompatible;
let e = validate_gateway_sidecar_update(&off, &on, &half).unwrap_err();
assert!(e.contains("Model Gateway"), "{e}");
assert!(validate_gateway_sidecar_update(&off, &on, &with_models).is_ok());
// Already on: an unrelated save still goes through.
assert!(validate_gateway_sidecar_update(&on, &on, &half).is_ok());
// Off: nothing to check.
assert!(validate_gateway_sidecar_update(&off, &off, &half).is_ok());
}
#[test]
fn a_stored_workspace_access_point_blocks_moving_the_host() {
let hosts = vec![host("h1"), host("h2")];
let mut stored = aws_project();
stored.aws.as_mut().unwrap().workspace_access_point_id = Some("fsap-w".into());
let mut moved = stored.clone();
moved.host_id = Some("h2".into());
assert!(validate_aws_update(&stored, &moved, &hosts).unwrap_err().contains("AWS resources"));
stored.aws.as_mut().unwrap().workspace_access_point_id = None;
assert!(validate_aws_update(&stored, &moved, &hosts).is_ok());
}
#[test]
fn host_update_rules() {
let hosts = vec![host("h1")];
@@ -2691,14 +2946,14 @@ mod tests {
let hosts = vec![host("h1")];
let mut stored = aws_project();
stored.aws.as_mut().unwrap().access_point_id = Some("fsap-1".into());
assert_eq!(access_point_after_removal(&stored, false), Some("fsap-1".into()));
assert_eq!(access_point_after_removal(&stored, true), None);
assert_eq!(access_points_after_removal(&stored, false), (Some("fsap-1".into()), None));
assert_eq!(access_points_after_removal(&stored, true), (None, None));
// What the store holds after a keep-the-files removal still blocks a move.
let mut after = stored.clone();
let a = after.aws.as_mut().unwrap();
a.task_arn = None;
a.task_definition_arn = None;
a.access_point_id = access_point_after_removal(&stored, false);
a.access_point_id = access_points_after_removal(&stored, false).0;
let mut local = after.clone();
local.host_id = None;
local.aws_placement = None;
@@ -2706,10 +2961,33 @@ mod tests {
let e = validate_aws_update(&after, &local, &hosts).unwrap_err();
assert!(e.contains("access point"), "{e}");
// After a discarding removal it can move.
after.aws.as_mut().unwrap().access_point_id = access_point_after_removal(&stored, true);
after.aws.as_mut().unwrap().access_point_id = access_points_after_removal(&stored, true).0;
assert!(validate_aws_update(&after, &local, &hosts).is_ok());
}
#[test]
fn a_removal_keeps_or_drops_both_access_points_together() {
let hosts = vec![host("h1")];
// EFS switched to Hybrid: the /workspace access point is still stored.
let mut stored = aws_project();
let a = stored.aws.as_mut().unwrap();
a.access_point_id = Some("fsap-1".into());
a.workspace_access_point_id = Some("fsap-w".into());
assert_eq!(
access_points_after_removal(&stored, false),
(Some("fsap-1".into()), Some("fsap-w".into()))
);
assert_eq!(access_points_after_removal(&stored, true), (None, None));
// Kept: the stored /workspace access point alone still blocks a move.
let mut after = stored.clone();
after.aws.as_mut().unwrap().access_point_id = None;
let mut local = after.clone();
local.host_id = None;
local.aws_placement = None;
local.aws = None;
assert!(validate_aws_update(&after, &local, &hosts).is_err());
}
#[test]
fn a_project_with_aws_resources_cannot_be_moved_off_its_host() {
let hosts = vec![host("h1"), host("h2")];
@@ -2880,4 +3158,117 @@ mod tests {
local.host_id = None;
assert!(check_aws_resources_removable(&local).is_err());
}
/// The text of `fn name` in this file, up to the next top-level item.
fn fn_body<'a>(src: &'a str, name: &str) -> &'a str {
let start = src.find(&format!("fn {name}(")).unwrap_or_else(|| panic!("{name} missing"));
let rest = &src[start..];
let end = rest.find("\n}\n").unwrap();
&rest[..end]
}
#[test]
fn the_workspace_follower_is_spawned_after_the_start_succeeds_and_only_for_aws() {
// Ruling R5: the start ends after "Connecting…" (Running, lock
// released when the command returns); the clone/setup follow runs
// in the background and never inside the start.
let src = include_str!("project_commands.rs");
let start = fn_body(src, "start_project_container_locked");
let running = start
.find("update_status(&project_id, ProjectStatus::Running)")
.expect("the start sets Running");
let spawn = start.find("aws_host::spawn_follow(").expect("the start spawns the follower");
assert!(spawn > running, "the follower is spawned only after the project is Running");
assert_eq!(start.matches("spawn_follow(").count(), 1);
let guard = start[..spawn].rfind("if project.is_aws() {").expect("AWS-only");
assert!(start[guard..spawn].lines().count() <= 4, "the spawn sits right in the AWS guard");
assert!(!start.contains("follow_workspace_status"), "no follow inside the start");
// Docker: `spawn_follow` itself also refuses a local project
// (`status::tests::a_local_project_has_no_workspace_status_and_no_follower`).
}
#[test]
fn every_connect_of_a_copy_at_connect_project_copies_the_credentials_in() {
// Q2: a start (new task or attach) and the background re-attach at
// app start both push; a failure is a `workspace-warning`.
let src = include_str!("project_commands.rs");
let start = fn_body(src, "start_project_container_locked");
let running = start.find("update_status(&project_id, ProjectStatus::Running)").unwrap();
let push = start.find("aws_host::spawn_connect_push(").expect("the start pushes");
assert!(push > running, "only once the workspace is connected and Running");
let guard = start[..push].rfind("if project.is_aws() {").expect("AWS-only");
assert!(start[guard..push].lines().count() <= 4);
assert!(start[push..].starts_with("aws_host::spawn_connect_push(&project, &settings, move |detail| {\n emit_connect_warning("));
let reconcile = fn_body(src, "reconcile_aws_projects");
assert!(reconcile.contains("emit_connect_warning(&handle, &id, &name, &detail)"));
assert!(reconcile.contains("aws_host::reconcile(\n &project,\n &settings,\n store,\n on_connect_warning,"));
}
#[test]
fn an_aws_project_refuses_new_udp_and_reserved_mappings_and_a_local_one_does_not() {
let hosts = vec![host("h1")];
let udp = crate::models::PortMapping {
host_port: 5353,
container_port: 5353,
protocol: "udp".into(),
};
let stored = aws_project();
let mut aws = aws_project();
aws.port_mappings = vec![udp.clone()];
let e = validate_aws_update(&stored, &aws, &hosts).unwrap_err();
assert!(e.contains("only TCP"), "{e}");
// A stored UDP row does not block an unrelated save (it is skipped,
// with a warning, at connect).
assert!(validate_aws_update(&aws, &aws, &hosts).is_ok());
// Moving a local project with one to AWS checks everything.
let (mut local, _) = stored_and_stale_payload();
local.port_mappings = vec![udp.clone()];
assert!(validate_aws_update(&local, &aws, &hosts).unwrap_err().contains("only TCP"));
aws.port_mappings[0].container_port = crate::host::ssm::AGENT_PORT;
aws.port_mappings[0].protocol = "tcp".into();
assert!(validate_aws_update(&stored, &aws, &hosts).is_err());
aws.port_mappings[0].container_port = 3000;
assert!(validate_aws_update(&stored, &aws, &hosts).is_ok());
// Docker publishes UDP itself: a local project is not checked.
assert!(validate_aws_update(&local, &local, &hosts).is_ok());
}
#[test]
fn every_connect_of_an_aws_project_opens_its_port_mappings() {
// Q6: the start (after Running), the background re-attach, and a save
// of the mappings while connected; a failure is a `workspace-warning`.
let src = include_str!("project_commands.rs");
let start = fn_body(src, "start_project_container_locked");
let running = start.find("update_status(&project_id, ProjectStatus::Running)").unwrap();
let open = start
.find("port_forwards::open_for_project(&state.projects_store, &project_id)")
.expect("the start opens them");
assert!(open > running);
let guard = start[..open].rfind("if project.is_aws() {").expect("AWS-only");
assert!(start[guard..open].lines().count() <= 2);
assert!(start[open..].lines().nth(1).unwrap().contains("emit_ports_warning("));
let update = fn_body(src, "update_project");
assert!(update.contains("port_forwards::open_for_project(&state.projects_store, &saved.id)"));
let reconcile = fn_body(src, "reconcile_aws_projects");
assert!(reconcile.contains("on_ports_warning,"));
assert!(reconcile.contains("emit_ports_warning(&ports_handle, &ports_id, &ports_name, &detail)"));
let aws_host = include_str!("../host/aws_host.rs");
let reattached = aws_host.find("Ok(true) => {").unwrap();
let opened = aws_host.find("port_forwards::open_for_project(&store, &project.id)").unwrap();
let pushed = aws_host[reattached..].find("connect::apply_connect(").unwrap() + reattached;
assert!(reattached < opened && opened < pushed);
}
#[test]
fn stop_ends_the_follower_before_it_stops_the_task() {
let src = include_str!("project_commands.rs");
let stop = fn_body(src, "stop_project_container");
let cancel = stop.find("tunnels::cancel_follow(&project_id)").expect("stop cancels the follower");
let task = stop.find(".stop_project(&project, &settings)").expect("stop stops the task");
assert!(cancel < task);
// Stop's own claim never waits on the follower: it holds none
// (`status::tests::the_follower_takes_no_project_lock`).
}
}
+4 -2
View File
@@ -115,13 +115,15 @@ pub async fn forget_choice<R: WipRunner>(runner: Option<&R>, choice: Option<WipC
}
/// The root command that records `marker`; the value travels as `$1`, never
/// inside the script.
/// inside the script. The directory stays 0711 (the entrypoint's mode): the
/// `claude` user reads the run id and its WIP record in it by name (the
/// marker itself is 0600).
pub fn stop_reason_command(marker: &str) -> Vec<String> {
vec![
"sh".into(),
"-c".into(),
format!(
"umask 077 && mkdir -p /run/triple-c && chmod 700 /run/triple-c && printf '%s\\n' \"$1\" > {STOP_REASON_FILE}"
"umask 077 && mkdir -p /run/triple-c && chmod 711 /run/triple-c && printf '%s\\n' \"$1\" > {STOP_REASON_FILE}"
),
"sh".into(),
marker.into(),
@@ -182,6 +182,10 @@ pub async fn open_terminal_session(
),
};
// AWS copy-at-connect: the credentials go into this terminal's own
// environment (never the agent's). Empty for everything else.
let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await;
let output_event = format!("terminal-output-{}", session_id);
let exit_event = format!("terminal-exit-{}", session_id);
let app_handle_output = app_handle.clone();
@@ -193,6 +197,7 @@ pub async fn open_terminal_session(
&target,
&session_id,
cmd,
env,
move |data| {
let _ = app_handle_output.emit(&output_event, data);
},
+4 -1
View File
@@ -77,7 +77,7 @@ pub struct ContainerExited<'a> {
/// Why an AWS task stopped, for the UI's toast.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StopNote {
/// `spot`, `idle`, `failed` (the project goes to Error), `user` (someone
/// `spot`, `idle`, `lifetime`, `failed` (the project goes to Error), `user` (someone
/// stopped it: no toast) or `exited` (on its own).
pub kind: &'static str,
/// ECS's stopped reason, or a fixed text.
@@ -89,6 +89,7 @@ impl StopNote {
let (kind, message) = match kind {
StopKind::Spot => ("spot", Some("Spot interruption".to_string())),
StopKind::Idle => ("idle", None),
StopKind::LifetimeCap => ("lifetime", None),
StopKind::FailedToStart(r) => ("failed", Some(r.clone())),
StopKind::UserInitiated => ("user", None),
StopKind::Exited(r) => ("exited", r.clone()),
@@ -276,6 +277,7 @@ pub fn spawn(
None,
a.access_point_id.clone(),
a.task_definition_arn.clone(),
a.workspace_access_point_id.clone(),
);
}
let id = p.id.clone();
@@ -331,6 +333,7 @@ mod tests {
let cases = [
(StopKind::Spot, "spot", Some("Spot interruption")),
(StopKind::Idle, "idle", None),
(StopKind::LifetimeCap, "lifetime", None),
(StopKind::FailedToStart("CannotPull".into()), "failed", Some("CannotPull")),
(StopKind::UserInitiated, "user", None),
(StopKind::Exited(Some("OOM".into())), "exited", Some("OOM")),
File diff suppressed because it is too large. Load diff
+2 -1
View File
@@ -108,7 +108,8 @@ fn sha256_hex(input: &str) -> String {
/// loops that walk the store, and leading dots are stripped so a hidden file
/// does not stay hidden.
///
/// `entrypoint.sh` reimplements exactly this in a few lines of shell (it has to
/// `entrypoint-lib.sh` (`ca_normalise_name`, sourced by `entrypoint.sh`)
/// reimplements exactly this in a few lines of shell (it has to
/// rename the files inside the container); the two must agree, which is what
/// the unit tests below pin down.
pub fn container_cert_name(file_name: &str) -> String {
+37 -1
View File
@@ -58,10 +58,30 @@ const GATEWAY_LITELLM_VERSION: &str = "v1.106.6";
const GATEWAY_REGISTRY_DIGEST: &str =
"sha256:37e2dafb088a7723beb998590682957386dfc83ffea1848486f8e0e5cfac0b06";
fn registry_image_ref() -> String {
/// The digest-pinned registry image. The AWS gateway sidecar runs it too
/// (`aws::workspace`); the index covers linux/amd64 and linux/arm64.
pub(crate) fn registry_image_ref() -> String {
format!("{}@{}", GATEWAY_REGISTRY_REPO, GATEWAY_REGISTRY_DIGEST)
}
/// The environment variables the AWS gateway sidecar gets its two keys in
/// (from the project secret). [`sidecar_config`] names them with LiteLLM's
/// `os.environ/<NAME>` references, so the config itself holds no key.
pub(crate) const SIDECAR_API_KEY_ENV: &str = "TRIPLEC_GATEWAY_API_KEY";
pub(crate) const SIDECAR_MASTER_KEY_ENV: &str = "TRIPLEC_GATEWAY_MASTER_KEY";
/// The LiteLLM config for the AWS gateway sidecar: [`render_config`]'s, with
/// the provider key and master key as `os.environ/…` references (LiteLLM
/// resolves both: `litellm_params.api_key` and `general_settings.master_key`).
/// Holds no secret, so it can be sized and shown in errors.
pub(crate) fn sidecar_config(settings: &GatewaySettings) -> String {
render_config(
settings,
&format!("os.environ/{SIDECAR_API_KEY_ENV}"),
&format!("os.environ/{SIDECAR_MASTER_KEY_ENV}"),
)
}
/// Local builds are tagged with the pin plus a hash of the embedded build
/// context (`triple-c-gateway:v1.106.6-1a2b3c4d`), never a floating tag. A
/// build made by an app version with a different pin *or* a different
@@ -1175,6 +1195,22 @@ mod tests {
assert!(!render_config(&s, "k", "m").contains("api_base"));
}
#[test]
fn sidecar_config_references_the_keys_and_holds_none() {
let yaml = sidecar_config(&settings());
assert!(yaml.contains("api_key: \"os.environ/TRIPLEC_GATEWAY_API_KEY\""));
assert!(yaml.contains("master_key: \"os.environ/TRIPLEC_GATEWAY_MASTER_KEY\""));
// Otherwise exactly what Docker's gateway gets.
assert_eq!(
yaml,
render_config(
&settings(),
"os.environ/TRIPLEC_GATEWAY_API_KEY",
"os.environ/TRIPLEC_GATEWAY_MASTER_KEY"
)
);
}
#[test]
fn yaml_str_escapes_injection_attempts() {
let hostile = "a\"\nmaster_key: \"pwned";
+20 -6
View File
@@ -10,6 +10,10 @@ use crate::models::container_config;
const DOCKERFILE: &str = include_str!("../../../../container/Dockerfile");
const ENTRYPOINT: &str = include_str!("../../../../container/entrypoint.sh");
/// Sourced by the entrypoint and by `triple-c-apply-connect`; holds the
/// `sync_host_ssh` block the tests below run.
const ENTRYPOINT_LIB: &str = include_str!("../../../../container/entrypoint-lib.sh");
const APPLY_CONNECT: &str = include_str!("../../../../container/triple-c-apply-connect");
const SCHEDULER: &str = include_str!("../../../../container/triple-c-scheduler");
const TASK_RUNNER: &str = include_str!("../../../../container/triple-c-task-runner");
const OSC52_CLIPBOARD: &str = include_str!("../../../../container/osc52-clipboard");
@@ -19,6 +23,7 @@ const TRIPLE_C_OPEN: &str = include_str!("../../../../container/triple-c-open");
const PLAYWRIGHT_HEAL: &str = include_str!("../../../../container/triple-c-playwright-heal");
const WIP_PUSH: &str = include_str!("../../../../container/triple-c-wip-push");
const IDLE_WATCH: &str = include_str!("../../../../container/triple-c-idle-watch");
const WORKSPACE_STATUS: &str = include_str!("../../../../container/triple-c-workspace-status");
static MISSION_CONTROL_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../../container/mission-control");
static SKILLS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../../container/skills");
@@ -240,6 +245,8 @@ fn create_build_context() -> Result<Vec<u8>, std::io::Error> {
append_file_to_archive(&mut archive, "Dockerfile", DOCKERFILE.as_bytes(), 0o644)?;
append_file_to_archive(&mut archive, "entrypoint.sh", ENTRYPOINT.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "entrypoint-lib.sh", ENTRYPOINT_LIB.as_bytes(), 0o644)?;
append_file_to_archive(&mut archive, "triple-c-apply-connect", APPLY_CONNECT.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-scheduler", SCHEDULER.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-task-runner", TASK_RUNNER.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "osc52-clipboard", OSC52_CLIPBOARD.as_bytes(), 0o755)?;
@@ -249,6 +256,12 @@ fn create_build_context() -> Result<Vec<u8>, std::io::Error> {
append_file_to_archive(&mut archive, "triple-c-playwright-heal", PLAYWRIGHT_HEAL.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-wip-push", WIP_PUSH.as_bytes(), 0o755)?;
append_file_to_archive(&mut archive, "triple-c-idle-watch", IDLE_WATCH.as_bytes(), 0o755)?;
append_file_to_archive(
&mut archive,
"triple-c-workspace-status",
WORKSPACE_STATUS.as_bytes(),
0o755,
)?;
append_embedded_dir(&mut archive, &MISSION_CONTROL_DIR, "mission-control")?;
append_embedded_dir(&mut archive, &SKILLS_DIR, "skills")?;
@@ -305,17 +318,18 @@ mod build_context_tests {
#[cfg(all(test, unix))]
mod tests {
use super::ENTRYPOINT;
use super::ENTRYPOINT_LIB;
use std::fs;
use std::path::Path;
use std::process::Command;
/// The `sync_host_ssh` function from `entrypoint.sh`, cut out between its
/// markers so it can be run against temp dirs instead of the real ~/.ssh.
/// The `sync_host_ssh` function from `entrypoint-lib.sh` (which
/// `entrypoint.sh` sources), cut out between its markers so it can be run
/// against temp dirs instead of the real ~/.ssh.
fn sync_fn() -> &'static str {
let start = ENTRYPOINT.find("# >>> sync_host_ssh").expect("start marker");
let end = ENTRYPOINT.find("# <<< sync_host_ssh").expect("end marker");
&ENTRYPOINT[start..end]
let start = ENTRYPOINT_LIB.find("# >>> sync_host_ssh").expect("start marker");
let end = ENTRYPOINT_LIB.find("# <<< sync_host_ssh").expect("end marker");
&ENTRYPOINT_LIB[start..end]
}
fn sync(src: &Path, dest: &Path) -> String {
+32 -5
View File
@@ -420,6 +420,20 @@ pub(crate) fn workspace_env(
}
/// The names of the custom variables [`workspace_env`] emits for `project`
/// (global + per-project, trimmed, reserved names skipped): the ones
/// copy-at-connect treats as credentials.
pub(crate) fn custom_env_keys(project: &Project, global_custom_env_vars: &[EnvVar]) -> Vec<String> {
let mut keys: Vec<String> = merge_custom_env_vars(global_custom_env_vars, &project.custom_env_vars)
.iter()
.map(|e| e.key.trim().to_string())
.filter(|k| !k.is_empty() && !is_reserved_env_key(k))
.collect();
keys.sort();
keys.dedup();
keys
}
/// [`workspace_env`] with its inputs resolved from `settings` exactly as
/// `create_container` resolves them (the CA from disk, the shared Claude
/// token from the keychain), for a host that does not create a Docker
@@ -428,11 +442,25 @@ pub(crate) fn workspace_env_from_settings(
project: &Project,
settings: &crate::models::AppSettings,
) -> Result<crate::host::aws_host::AwsEnv, String> {
let shared_claude = super::container::shared_claude_auth(project);
let effective_ca_path =
resolve_with_global(project.ca_cert_path.as_deref(), settings.ca_cert_path.as_deref());
let resolved_ca = ca_certs::resolve(effective_ca_path)?;
let pairs = workspace_env(
let pairs = workspace_env_with_ca(project, settings, resolved_ca.as_ref());
Ok((pairs, resolved_ca))
}
/// [`workspace_env`] resolved from `settings` like
/// [`workspace_env_from_settings`] but with the CA given (`None`: no CA
/// variables) — so nothing is read from disk. The keychain is (the shared
/// Claude token). Copy-at-connect's terminal environment uses it: the
/// credential pairs never depend on the CA.
pub(crate) fn workspace_env_with_ca(
project: &Project,
settings: &crate::models::AppSettings,
resolved_ca: Option<&ResolvedCa>,
) -> Vec<(String, String)> {
let shared_claude = super::container::shared_claude_auth(project);
workspace_env(
project,
&WorkspaceEnvInputs {
global_aws: &settings.global_aws,
@@ -445,11 +473,10 @@ pub(crate) fn workspace_env_from_settings(
global_claude_code_settings: settings.global_claude_code_settings.as_ref(),
default_git_user_name: settings.default_git_user_name.as_deref(),
default_git_user_email: settings.default_git_user_email.as_deref(),
resolved_ca: resolved_ca.as_ref(),
resolved_ca,
shared_claude_token: shared_claude.as_ref().map(|(token, _)| token.as_str()),
},
);
Ok((pairs, resolved_ca))
)
}
#[cfg(test)]
+192 -16
View File
@@ -40,7 +40,10 @@ use crate::aws::tasks::{
use crate::aws::tofu_modules::{
environment_cluster_name, log_stream_prefix, owner_key, task_family,
};
use crate::aws::workspace::{prepare_workspace, SdkWorkspaceApi, WorkspaceApi};
use crate::aws::workspace::{
gateway_sidecar_problem, new_agent_token, prepare_workspace, GatewayPayload, SdkWorkspaceApi,
WorkspaceApi,
};
use crate::aws::{caller_identity, sdk_config, CallerIdentity};
use crate::docker::ca_certs::{container_cert_name, ResolvedCa};
use crate::models::app_settings::ImageSource;
@@ -81,6 +84,11 @@ const WATCH_CALL_TIMEOUT: Duration = Duration::from_secs(60);
/// reported as failed; the SDK's own bound is per call.
const STOP_TIMEOUT: Duration = Duration::from_secs(45);
pub mod connect;
pub mod status;
pub use connect::{spawn_connect_push, terminal_env};
pub use status::{spawn_follow, workspace_status, WorkspaceStatus};
fn s<E: std::fmt::Display>(e: E) -> String {
e.to_string()
}
@@ -146,6 +154,55 @@ pub fn aws_env(project: &Project, settings: &AppSettings) -> Result<AwsEnv, Stri
crate::docker::workspace_env::workspace_env_from_settings(&p, settings)
}
/// The gateway sidecar's payload (Q7), owned: its key-free config, the
/// provider key and a fresh master key.
pub struct SidecarSecrets {
config: String,
api_key: zeroize::Zeroizing<String>,
master_key: zeroize::Zeroizing<String>,
}
impl SidecarSecrets {
pub fn payload(&self) -> GatewayPayload<'_> {
GatewayPayload {
config: &self.config,
api_key: &self.api_key,
master_key: &self.master_key,
}
}
}
/// What the gateway sidecar needs for this start, or `None` when the project
/// doesn't run it. Refuses a project the sidecar can't serve
/// ([`gateway_sidecar_problem`]) and a missing provider key (`read_api_key`:
/// the keychain's, Settings → Model Gateway). The master key is new every
/// task start — only this task's sidecar and workspace know it — so the
/// Docker gateway's own master key never leaves this machine.
pub fn gateway_sidecar(
project: &Project,
settings: &AppSettings,
read_api_key: impl FnOnce() -> Result<Option<String>, String>,
) -> Result<Option<SidecarSecrets>, String> {
if !project.aws.as_ref().is_some_and(|a| a.gateway_sidecar) {
return Ok(None);
}
if let Some(problem) = gateway_sidecar_problem(project, &settings.gateway) {
return Err(problem);
}
let api_key = read_api_key()?
.filter(|k| !k.trim().is_empty())
.ok_or_else(|| {
"The LLM gateway sidecar needs the provider API key: set it in Settings → Model \
Gateway"
.to_string()
})?;
Ok(Some(SidecarSecrets {
config: crate::docker::gateway::sidecar_config(&settings.gateway),
api_key: zeroize::Zeroizing::new(api_key),
master_key: zeroize::Zeroizing::new(new_agent_token()),
}))
}
/// One file read for the secret, refusing anything over [`MAX_FILE_BYTES`]
/// (enforced on the bytes read, not on a size the file could change after).
fn read_capped(path: &Path, shown: &str) -> Result<Vec<u8>, String> {
@@ -345,7 +402,13 @@ pub struct StartInputs<'a> {
pub who: &'a CallerIdentity,
pub env_pairs: &'a [(String, String)],
pub files: &'a [(String, Vec<u8>)],
/// The custom variables' names: credentials under copy-at-connect.
pub custom_env_keys: &'a [String],
pub image: &'a str,
/// The gateway sidecar's payload, when the project runs it
/// ([`gateway_sidecar`]). Called only when the start prepares a new task:
/// attaching to a running one never needs the provider key or models.
pub gateway: &'a (dyn Fn() -> Result<Option<SidecarSecrets>, String> + Send + Sync),
pub wait: WaitTiming,
pub access_point_wait: PollTiming,
/// Called as soon as the project has a task (found, or RunTask
@@ -362,6 +425,8 @@ pub struct TaskRecord {
pub task_arn: String,
pub access_point_id: Option<String>,
pub task_definition_arn: Option<String>,
/// EFS storage's `/workspace` access point.
pub workspace_access_point_id: Option<String>,
}
/// A connected task.
@@ -373,6 +438,8 @@ pub struct Started {
/// The access point the task mounts (Hybrid). `None` when an existing
/// task was attached to: the store's value stands.
pub access_point_id: Option<String>,
/// The `/workspace` access point (EFS), like `access_point_id`.
pub workspace_access_point_id: Option<String>,
/// This start ran the task (rather than attaching to a running one).
pub created: bool,
}
@@ -433,13 +500,14 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
.await
.map_err(s)?;
let (task, token, access_point_id, created) = if let Some(task) = existing {
let (task, token, access_point_id, workspace_access_point_id, created) = if let Some(task) = existing {
// Attach: never rotate a running task's token (P2).
log::info!("Project {}: attaching to its running task {}", project.id, task.arn);
(inp.on_task)(Some(&TaskRecord {
task_arn: task.arn.clone(),
access_point_id: None,
task_definition_arn: Some(task.task_definition_arn.clone()),
workspace_access_point_id: None,
}));
let token = read_agent_token(rt, &env.name, &ok, &project.id).await.map_err(s)?;
progress(PROGRESS_WAITING);
@@ -447,9 +515,10 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
Ok(t) => t,
Err(e) => return Err(with_log_tail(tasks, &logs, &task.arn, e.to_string()).await),
};
(task, token, None, false)
(task, token, None, None, false)
} else {
progress(PROGRESS_PREPARING);
let sidecar = (inp.gateway)()?;
let prepared = prepare_workspace(
work,
inp.who,
@@ -457,11 +526,16 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
project,
inp.env_pairs,
inp.files,
inp.custom_env_keys,
inp.image,
sidecar.as_ref().map(SidecarSecrets::payload),
)
.await
.map_err(s)?;
if let Some(ap) = &prepared.access_point_id {
for ap in [&prepared.access_point_id, &prepared.workspace_access_point_id]
.into_iter()
.flatten()
{
wait_access_point(rt, ap, inp.access_point_wait).await.map_err(s)?;
}
progress(PROGRESS_STARTING);
@@ -482,6 +556,7 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
task_arn: start.task.arn.clone(),
access_point_id: prepared.access_point_id.clone(),
task_definition_arn: Some(start.task.task_definition_arn.clone()),
workspace_access_point_id: prepared.workspace_access_point_id.clone(),
}));
return Err(START_RACE.into());
}
@@ -490,6 +565,7 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
task_arn: arn.clone(),
access_point_id: prepared.access_point_id.clone(),
task_definition_arn: Some(prepared.task_definition_arn.clone()),
workspace_access_point_id: prepared.workspace_access_point_id.clone(),
}));
progress(PROGRESS_WAITING);
let task = match wait_ready(tasks, &env.cluster_arn, &arn, &logs, inp.wait, |_| {}).await {
@@ -502,7 +578,13 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
return Err(msg);
}
};
(task, prepared.agent_token, prepared.access_point_id, true)
(
task,
prepared.agent_token,
prepared.access_point_id,
prepared.workspace_access_point_id,
true,
)
};
progress(PROGRESS_CONNECTING);
@@ -530,6 +612,7 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
forward,
endpoint,
access_point_id,
workspace_access_point_id,
created,
})
}
@@ -555,12 +638,47 @@ async fn project_sdk(project: &Project, settings: &AppSettings) -> Result<(Strin
Ok((host_id.to_string(), region, sdk))
}
/// The lifetime cap, in hours (0 = none), of the environment `project` runs
/// in, from its record (Q8: Host settings show it). `None` when it can't be
/// read — offline, denied, slow, or the environment is gone.
pub async fn environment_max_hours(project: &Project, settings: &AppSettings) -> Option<u32> {
let placement = project.aws_placement.as_ref()?;
let read = async {
let (_, region, sdk) = project_sdk(project, settings).await.ok()?;
let who = caller_identity(&sdk).await.ok()?;
read_environment(&SdkRuntimeApi::new(&sdk), &placement.environment, &who.account, &region)
.await
.ok()
};
let info = tokio::time::timeout(std::time::Duration::from_secs(10), read).await.ok()??;
Some(info.max_workspace_hours)
}
/// `ProjectHost::Aws::ensure_started`.
pub async fn ensure_started(
project: &Project,
settings: &AppSettings,
store: &ProjectsStore,
progress: &(dyn Fn(&str) + Send + Sync),
) -> Result<ContainerTarget, String> {
ensure_started_with(
project,
settings,
store,
progress,
crate::storage::secure::get_gateway_api_key,
)
.await
}
/// [`ensure_started`] with the gateway sidecar's provider key read by
/// `read_gateway_key` instead of the keychain (the live tests have none).
pub async fn ensure_started_with(
project: &Project,
settings: &AppSettings,
store: &ProjectsStore,
progress: &(dyn Fn(&str) + Send + Sync),
read_gateway_key: impl FnOnce() -> Result<Option<String>, String> + Send,
) -> Result<ContainerTarget, String> {
// A background re-attach (after a restart) may be opening a tunnel:
// cancel it, this start attaches itself. Its tunnel, if it gets one, is
@@ -569,6 +687,7 @@ pub async fn ensure_started(
// Already connected and answering: nothing to do.
if let Some(ep) = tunnels::get_endpoint(&project.id) {
if matches!(agent::is_running(&ep).await, Ok(true)) {
tunnels::mark_attached(&project.id);
return Ok(ContainerTarget::Agent(ep));
}
tunnels::close(&project.id).await;
@@ -582,6 +701,17 @@ pub async fn ensure_started(
.as_deref()
.or(settings.default_ssh_key_path.as_deref());
let files = workspace_files(ssh, ca.as_ref())?;
let custom_env_keys =
crate::docker::workspace_env::custom_env_keys(project, &settings.global_custom_env_vars);
// Read lazily: only a start that prepares a new task needs it (an attach
// must work with the provider key gone from the keychain).
let read_gateway_key = std::sync::Mutex::new(Some(read_gateway_key));
let sidecar = || {
gateway_sidecar(project, settings, || match read_gateway_key.lock().unwrap().take() {
Some(read) => read(),
None => Ok(None),
})
};
let (connection_id, region, sdk) = project_sdk(project, settings).await?;
let who = caller_identity(&sdk).await.map_err(s)?;
@@ -596,7 +726,9 @@ pub async fn ensure_started(
who: &who,
env_pairs: &env_pairs,
files: &files,
custom_env_keys: &custom_env_keys,
image: &image,
gateway: &sidecar,
wait: WaitTiming::default(),
access_point_wait: PollTiming::default(),
on_task: &on_task,
@@ -615,6 +747,9 @@ pub async fn ensure_started(
let access_point_id = started
.access_point_id
.or_else(|| project.aws.as_ref().and_then(|a| a.access_point_id.clone()));
let workspace_access_point_id = started
.workspace_access_point_id
.or_else(|| project.aws.as_ref().and_then(|a| a.workspace_access_point_id.clone()));
let ecs = ecs_task(&started.task)?;
if let Some(old) = tunnels::register(
&project.id,
@@ -628,7 +763,9 @@ pub async fn ensure_started(
forward: started.forward,
endpoint: endpoint.clone(),
access_point_id,
workspace_access_point_id,
task_definition_arn: started.task.task_definition_arn.clone(),
created: started.created,
},
) {
old.forward.close().await;
@@ -651,12 +788,14 @@ pub fn record_task(store: &ProjectsStore, project_id: &str, rec: Option<&TaskRec
Some(r.task_arn.clone()),
r.access_point_id.clone().or(stored.access_point_id),
r.task_definition_arn.clone().or(stored.task_definition_arn),
r.workspace_access_point_id.clone().or(stored.workspace_access_point_id),
),
None => store.set_aws_runtime(
project_id,
None,
stored.access_point_id,
stored.task_definition_arn,
stored.workspace_access_point_id,
),
};
if let Err(e) = res {
@@ -808,6 +947,7 @@ async fn task_arn_or_find<T: TaskApi>(
/// the stored task ARN, else whatever live task the project has (a crash
/// between RunTask and the store write). Closes the tunnel.
pub async fn stop_project(project: &Project, settings: &AppSettings) -> Result<(), String> {
tunnels::cancel_follow(&project.id);
if let Some(ep) = tunnels::get_endpoint(&project.id) {
if let Some(res) = stop_endpoint(&ep).await {
return res;
@@ -841,6 +981,7 @@ pub async fn remove_resources(
settings: &AppSettings,
discard_data: bool,
) -> Result<(), String> {
tunnels::cancel_follow(&project.id);
let (_, region, sdk) = project_sdk(project, settings).await?;
let who = caller_identity(&sdk).await.map_err(s)?;
let ok = owner_key(&region, &who.user_id);
@@ -858,6 +999,8 @@ pub async fn remove_resources(
project_id: &project.id,
stored_task_arn: aws.and_then(|a| a.task_arn.as_deref()),
stored_access_point_id: aws.and_then(|a| a.access_point_id.as_deref()),
stored_workspace_access_point_id: aws
.and_then(|a| a.workspace_access_point_id.as_deref()),
discard_data,
},
)
@@ -972,6 +1115,7 @@ pub async fn reconcile_project<T: TaskApi>(
task_arn: t.arn.clone(),
access_point_id: None,
task_definition_arn: Some(t.task_definition_arn.clone()),
workspace_access_point_id: None,
}),
);
}
@@ -985,8 +1129,17 @@ pub async fn reconcile_project<T: TaskApi>(
/// `reconcile_project_statuses` for an AWS project: decide, and when the
/// task is live re-open its tunnel in the background (terminals wait for
/// it through [`tunnels::wait_endpoint`]). Never blocks on the tunnel.
pub async fn reconcile(project: &Project, settings: &AppSettings, store: Arc<ProjectsStore>) {
/// it through [`tunnels::wait_endpoint`]), then — copy-at-connect — copy the
/// credentials in, calling `on_connect_warning` if that fails — after
/// opening its port mappings (`on_ports_warning` for any that fail). Never
/// blocks on the tunnel.
pub async fn reconcile(
project: &Project,
settings: &AppSettings,
store: Arc<ProjectsStore>,
on_connect_warning: impl FnOnce(String) + Send + 'static,
on_ports_warning: impl FnOnce(String) + Send + 'static,
) {
let (connection_id, region, sdk) = match project_sdk(project, settings).await {
Ok(v) => v,
Err(e) => {
@@ -1006,12 +1159,29 @@ pub async fn reconcile(project: &Project, settings: &AppSettings, store: Arc<Pro
match reconcile_project(&api, &store, project, &find).await {
Ok(Reconciled::Live(task)) => {
let generation = tunnels::begin_reattach(&project.id);
let project = project.clone();
let (project, settings) = (project.clone(), settings.clone());
tauri::async_runtime::spawn(async move {
let ctx = Reattach { generation, connection_id, region, sdk, who };
if let Err(e) = reattach(&project, *task, ctx, &store).await {
log::warn!("Project {}: could not re-open the tunnel to its task: {e}", project.id);
tunnels::end_reattach(&project.id, generation);
match reattach(&project, *task, ctx, &store).await {
// Re-attached: a connect like any other (Q2).
Ok(true) => {
// Q6: the port mappings' forwards, before the
// (slower) credential copy.
if let Some(detail) = super::port_forwards::open_for_project(&store, &project.id).await {
on_ports_warning(detail);
}
if let Err(e) = connect::apply_connect(&project, &settings).await {
log::warn!("Project {}: could not copy the credentials in: {e}", project.id);
if tunnels::get_endpoint(&project.id).is_some() {
on_connect_warning(e);
}
}
}
Ok(false) => {}
Err(e) => {
log::warn!("Project {}: could not re-open the tunnel to its task: {e}", project.id);
tunnels::end_reattach(&project.id, generation);
}
}
});
}
@@ -1040,13 +1210,14 @@ pub fn reattach_still_wanted(store: &ProjectsStore, project_id: &str, task_arn:
})
}
/// Re-open the tunnel to a live task found at startup.
/// Re-open the tunnel to a live task found at startup. `Ok(true)` once it
/// is registered; `Ok(false)` when it was no longer wanted.
async fn reattach(
project: &Project,
task: TaskInfo,
ctx: Reattach,
store: &ProjectsStore,
) -> Result<(), String> {
) -> Result<bool, String> {
let placement = project.aws_placement.as_ref().ok_or("no placement")?;
let ok = owner_key(&ctx.region, &ctx.who.user_id);
let rt = SdkRuntimeApi::new(&ctx.sdk);
@@ -1070,13 +1241,18 @@ async fn reattach(
forward,
endpoint: endpoint.clone(),
access_point_id: project.aws.as_ref().and_then(|a| a.access_point_id.clone()),
workspace_access_point_id: project
.aws
.as_ref()
.and_then(|a| a.workspace_access_point_id.clone()),
task_definition_arn: task.task_definition_arn.clone(),
created: false,
};
if !reattach_still_wanted(store, &project.id, &task.arn) {
tunnel.forward.close().await;
tunnels::end_reattach(&project.id, ctx.generation);
log::info!("Project {}: stopped or removed while re-attaching; tunnel closed", project.id);
return Ok(());
return Ok(false);
}
match tunnels::register_if_current(&project.id, ctx.generation, tunnel) {
Ok(old) => {
@@ -1087,13 +1263,13 @@ async fn reattach(
Err(tunnel) => {
tunnel.forward.close().await;
log::info!("Project {}: stopped or removed while re-attaching; tunnel closed", project.id);
return Ok(());
return Ok(false);
}
}
// The local port is new, so the stored target id is too.
store.set_container_id(&project.id, Some(endpoint.base_url.to_string()))?;
log::info!("Project {}: tunnel to task {} re-opened", project.id, task.arn);
Ok(())
Ok(true)
}
// ─────────────────────────────────────────────────────────────── watch
+299
View File
@@ -0,0 +1,299 @@
//! Copy-at-connect (phase 6, ruling Q2): an AWS project whose credentials
//! are "copied when you connect" keeps them out of its Secrets Manager
//! secret (`aws::workspace`, `PayloadExtras::connect_only`) and brings them
//! in after every connect instead — a start, an attach, the background
//! re-attach when the app starts.
//!
//! The credentials are [`ConnectBundle`]: the SSH and CA files (the same
//! [`workspace_files`](super::workspace_files) the secret would carry) and
//! the credential environment pairs (`SECRET_ENV_KEYS` and every custom
//! variable, `aws::workspace::credential_pairs`). [`push_connect_bundle`]
//! uploads them through the agent, as root, into this connect's own root-only
//! staging dir on the task's `/dev/shm` tmpfs and runs `triple-c-apply-connect
//! <nonce>` (container), which
//! installs the CA, merges the SSH files, writes the git credential helper
//! and re-clones what failed. The terminals the app opens get the pairs in
//! their own `ExecSpec.env` ([`terminal_env`]); nothing puts them into the
//! agent's environment, so other execs, cron jobs and the scheduler never
//! see them. **Values are never logged**: errors carry names and the
//! script's own (value-free) output only.
use std::time::Duration;
use zeroize::Zeroizing;
use super::super::{tunnels, ContainerTarget};
use crate::aws::workspace::{credential_pairs, validate_file_key};
use crate::models::project::SecretsDelivery;
use crate::models::{AppSettings, Project};
/// The in-container half (container/triple-c-apply-connect).
pub const APPLY_CONNECT: &str = "/usr/local/bin/triple-c-apply-connect";
/// Staging (root 0700, on the tmpfs the entrypoint made for the agent
/// token): each connect gets `<nonce>/` with `ssh/`, `ca/` and `connect.env`.
pub const CONNECT_STAGE: &str = "/dev/shm/triple-c/connect";
pub const CONNECT_ENV_FILE: &str = "connect.env";
/// Every staged file is root 0600.
const STAGED_MODE: u32 = 0o600;
/// The whole push (a few uploads and two execs through the tunnel).
pub const PUSH_TIMEOUT: Duration = Duration::from_secs(120);
/// The most of the script's output a warning carries.
const OUTPUT_TAIL_LINES: usize = 10;
/// A credential value, zeroed when dropped (the settings export's
/// precedent). It narrows, not closes, the window: the values were read from
/// the keychain into a `Project`, and the copies an `ExecSpec` or an upload
/// body needs are plain buffers owned by those layers.
pub type SecretBytes = Zeroizing<Vec<u8>>;
pub type SecretString = Zeroizing<String>;
/// What copy-at-connect copies in.
#[derive(Clone, Default)]
pub struct ConnectBundle {
/// `ssh/<file>` / `ca/<file>`, as in the secret's `TRIPLEC_FILES`.
pub files: Vec<(String, SecretBytes)>,
/// The credential environment pairs.
pub env: Vec<(String, SecretString)>,
}
/// Names only: a bundle holds keys and tokens.
impl std::fmt::Debug for ConnectBundle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConnectBundle")
.field("files", &self.files.iter().map(|(k, _)| k.as_str()).collect::<Vec<_>>())
.field("env", &self.env.iter().map(|(k, _)| k.as_str()).collect::<Vec<_>>())
.finish()
}
}
/// Whether `project` copies its credentials in at connect.
pub fn copies_at_connect(project: &Project) -> bool {
project.is_aws()
&& project
.aws
.as_ref()
.is_some_and(|a| a.secrets_delivery == SecretsDelivery::CopyAtConnect)
}
/// The bundle for `project`, whose keychain secrets must already be loaded
/// (`load_secrets_for_project`): the same sources the secret would use.
pub fn connect_credentials(project: &Project, settings: &AppSettings) -> Result<ConnectBundle, String> {
let (_, ca) = super::aws_env(project, settings)?;
let ssh = project
.ssh_key_path
.as_deref()
.or(settings.default_ssh_key_path.as_deref());
let files = super::workspace_files(ssh, ca.as_ref())?
.into_iter()
.map(|(k, v)| (k, Zeroizing::new(v)))
.collect();
Ok(ConnectBundle { files, env: connect_env(project, settings) })
}
/// Only the credential pairs of `project` (secrets loaded): no file is read,
/// so a missing SSH or CA file never keeps the API keys from a terminal.
pub fn connect_env(project: &Project, settings: &AppSettings) -> Vec<(String, SecretString)> {
let pairs = crate::docker::workspace_env::workspace_env_with_ca(project, settings, None);
let custom =
crate::docker::workspace_env::custom_env_keys(project, &settings.global_custom_env_vars);
// With the gateway sidecar, the task's own ANTHROPIC_AUTH_TOKEN (the
// sidecar's master key, kept in the secret) is the right one; the
// OpenAI-compatible key would override it with one the sidecar refuses.
let sidecar = project.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
credential_pairs(&pairs, &custom)
.into_iter()
.filter(|(k, _)| !(sidecar && k == "ANTHROPIC_AUTH_TOKEN"))
.map(|(k, v)| (k, Zeroizing::new(v)))
.collect()
}
/// `project` as the store holds it, with its keychain secrets read in.
fn with_secrets(project: &Project) -> Project {
let mut p = project.clone();
crate::commands::project_commands::load_secrets_for_project(&mut p);
p
}
/// `connect.env`: one `KEY=<base64 of the value>` line per pair, so a value
/// with a newline or `=` reads back whole and nothing has to be quoted.
pub fn connect_env_file(env: &[(String, SecretString)]) -> SecretBytes {
use base64::Engine;
let b64 = base64::engine::general_purpose::STANDARD;
let mut out = Zeroizing::new(String::new());
for (k, v) in env {
out.push_str(k);
out.push('=');
b64.encode_string(v.as_bytes(), &mut out);
out.push('\n');
}
Zeroizing::new(out.as_bytes().to_vec())
}
/// `KEY=VALUE` strings for an `ExecSpec`.
pub fn exec_env(env: &[(String, SecretString)]) -> Vec<String> {
env.iter().map(|(k, v)| format!("{k}={}", v.as_str())).collect()
}
/// What a push needs from the container: root execs and root uploads.
pub trait ConnectTarget: Send + Sync {
fn exec_root(
&self,
cmd: Vec<String>,
) -> impl std::future::Future<Output = Result<(String, i64), String>> + Send;
fn upload_root(
&self,
dir: &str,
name: &str,
data: &[u8],
mode: u32,
) -> impl std::future::Future<Output = Result<(), String>> + Send;
}
impl ConnectTarget for ContainerTarget {
async fn exec_root(&self, cmd: Vec<String>) -> Result<(String, i64), String> {
self.exec_oneshot_as("root", cmd, Vec::new()).await
}
async fn upload_root(&self, dir: &str, name: &str, data: &[u8], mode: u32) -> Result<(), String> {
self.upload_bytes_with_ids(dir, name, data, mode, (0, 0)).await.map(|_| ())
}
}
fn tail(output: &str) -> String {
let lines: Vec<&str> = output.lines().filter(|l| !l.trim().is_empty()).collect();
lines[lines.len().saturating_sub(OUTPUT_TAIL_LINES)..].join("\n")
}
async fn run_apply<T: ConnectTarget>(t: &T, args: &[&str]) -> Result<String, String> {
let mut cmd = vec![APPLY_CONNECT.to_string()];
cmd.extend(args.iter().map(|a| a.to_string()));
let (out, code) = t.exec_root(cmd).await?;
match code {
0 => Ok(out),
// `exec` of a missing file: an image from before copy-at-connect.
126 | 127 => Err(
"This workspace's image has no triple-c-apply-connect (it predates copying \
credentials at connect); use a newer image, or store the credentials in AWS"
.into(),
),
_ => Err(format!("triple-c-apply-connect failed ({code}):\n{}", tail(&out))),
}
}
/// `--prepare`'s answer: this connect's nonce, 16 hex digits on the last line.
pub(crate) fn parse_nonce(out: &str) -> Result<String, String> {
let last = out.lines().rev().map(str::trim).find(|l| !l.is_empty()).unwrap_or_default();
if last.len() == 16 && last.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) {
Ok(last.to_string())
} else {
Err(format!("triple-c-apply-connect --prepare gave no connect id:\n{}", tail(out)))
}
}
/// Upload `bundle` and apply it (see the module docs): `--prepare` (which
/// names this connect's own staging dir), the files and `connect.env` (root
/// 0600) into that dir, then the apply of exactly that dir — so two
/// connects at once (two machines) never mix their files.
pub async fn push_connect_bundle<T: ConnectTarget>(t: &T, bundle: &ConnectBundle) -> Result<(), String> {
let nonce = parse_nonce(&run_apply(t, &["--prepare"]).await?)?;
let dir = format!("{CONNECT_STAGE}/{nonce}");
for (key, data) in &bundle.files {
validate_file_key(key)?;
let (kind, name) = key.split_once('/').expect("validated");
t.upload_root(&format!("{dir}/{kind}"), name, data, STAGED_MODE)
.await
.map_err(|e| format!("Could not copy {key} into the workspace: {e}"))?;
}
t.upload_root(&dir, CONNECT_ENV_FILE, &connect_env_file(&bundle.env), STAGED_MODE)
.await
.map_err(|e| format!("Could not copy the credential variables into the workspace: {e}"))?;
let out = run_apply(t, &[&nonce]).await?;
for line in out.lines().filter(|l| l.contains("warning")) {
log::warn!("{line}");
}
Ok(())
}
/// Copy `project`'s credentials into its connected workspace. `Ok(false)`
/// when there is nothing to do (not copy-at-connect, or no tunnel).
pub async fn apply_connect(project: &Project, settings: &AppSettings) -> Result<bool, String> {
if !copies_at_connect(project) {
return Ok(false);
}
let Some(ep) = tunnels::get_endpoint(&project.id) else {
return Ok(false);
};
let (p, s) = (project.clone(), settings.clone());
let bundle = tokio::task::spawn_blocking(move || connect_credentials(&with_secrets(&p), &s))
.await
.map_err(|e| format!("Could not gather the credentials: {e}"))??;
let target = ContainerTarget::Agent(ep);
tokio::time::timeout(PUSH_TIMEOUT, push_connect_bundle(&target, &bundle))
.await
.map_err(|_| format!("Copying the credentials took more than {} s", PUSH_TIMEOUT.as_secs()))??;
log::info!(
"Project {}: credentials copied into the workspace ({} file(s), {} variable(s))",
project.id,
bundle.files.len(),
bundle.env.len()
);
Ok(true)
}
/// After a connect: [`apply_connect`] in the background, `on_warning` on a
/// failure while the workspace is still connected (a Stop meanwhile is not
/// worth a warning). `false` (nothing spawned) unless the project copies at
/// connect and is connected.
pub fn spawn_connect_push(
project: &Project,
settings: &AppSettings,
on_warning: impl FnOnce(String) + Send + 'static,
) -> bool {
if !copies_at_connect(project) || tunnels::get_endpoint(&project.id).is_none() {
return false;
}
let (project, settings) = (project.clone(), settings.clone());
tauri::async_runtime::spawn(async move {
if let Err(e) = apply_connect(&project, &settings).await {
log::warn!("Project {}: could not copy the credentials in: {e}", project.id);
if tunnels::get_endpoint(&project.id).is_some() {
on_warning(e);
}
}
});
true
}
/// The environment a terminal the app opens adds to its exec: the
/// credential pairs for a copy-at-connect project, nothing otherwise (they
/// are already in the task's environment). Only the pairs ([`connect_env`]:
/// no SSH or CA file is read); the keychain is read off the async runtime.
pub async fn terminal_env(project: &Project, settings: &AppSettings) -> Vec<String> {
if !copies_at_connect(project) {
return Vec::new();
}
let (p, s) = (project.clone(), settings.clone());
let gathered = tokio::task::spawn_blocking(move || connect_env(&with_secrets(&p), &s))
.await
.map_err(|e| format!("could not gather them: {e}"));
terminal_env_with(project, || gathered)
}
/// [`terminal_env`] over a given source of the pairs.
pub fn terminal_env_with(
project: &Project,
credentials: impl FnOnce() -> Result<Vec<(String, SecretString)>, String>,
) -> Vec<String> {
if !copies_at_connect(project) {
return Vec::new();
}
match credentials() {
Ok(env) => exec_env(&env),
Err(e) => {
log::warn!(
"Project {}: the terminal opens without the project's credentials: {e}",
project.id
);
Vec::new()
}
}
}
+455
View File
@@ -0,0 +1,455 @@
//! An AWS workspace's clone and setup script, as the start follows them and
//! Project Home shows them (phase 6, Q4).
//!
//! The entrypoint clones in the background and then runs the setup script;
//! `triple-c-workspace-status` (container) prints both as one JSON line,
//! mirrored by [`WorkspaceStatus`].
//!
//! **The start does not wait for them** (ruling R5, amending Q4): it ends
//! after "Connecting…" as in phase 5 — project Running, project lock
//! released, terminals and Stop usable. [`spawn_follow`] then follows in the
//! background: a read through the agent every [`FOLLOW_INTERVAL`] as a
//! **background** exec (never activity for idle stop), a log line only when
//! it changes, until the clone is done and no setup runs, [`FOLLOW_LIMIT`]
//! passes, the agent is gone (tunnel dropped, task died, image without the
//! script) or its token is cancelled (`tunnels::cancel_follow` on Stop and
//! Remove; any tunnel close; app exit). Its only output is the final warning
//! ([`follow_warning`]); Project Home's Overview card polls the status itself
//! while the project is Running, so progress shows there.
use std::future::Future;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use tokio_util::sync::CancellationToken;
use super::super::{tunnels, ContainerTarget};
use crate::models::Project;
/// The in-container status reader (Task 4).
pub const STATUS_SCRIPT: &str = "/usr/local/bin/triple-c-workspace-status";
pub const FOLLOW_INTERVAL: Duration = Duration::from_secs(2);
/// The setup script's own timeout is 30 minutes, after the clone.
pub const FOLLOW_LIMIT: Duration = Duration::from_secs(35 * 60);
/// One status read through the agent.
const READ_TIMEOUT: Duration = Duration::from_secs(20);
/// Reads failing in a row before the follow gives up (quietly).
const MAX_READ_FAILURES: u32 = 5;
/// The script prints at most ~20 KB (20 tail lines of ≤ 1000 characters).
const STATUS_OUTPUT_LIMIT: usize = 256 * 1024;
/// A setup log line shown as progress is cut to this many characters.
const MAX_PROGRESS_LINE: usize = 200;
/// Setup log lines a warning carries.
const WARNING_TAIL_LINES: usize = 10;
pub const PROGRESS_CLONING: &str = "Cloning repos…";
pub const PROGRESS_SETUP: &str = "Running setup script…";
// ──────────────────────────────────────────────────────────────── model
/// `triple-c-workspace-status`'s JSON. Unknown fields are ignored and
/// missing ones default, so an older or newer image never breaks a read.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct WorkspaceStatus {
pub clone: CloneStatus,
pub setup: SetupStatus,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CloneStatus {
/// Every repo has been tried.
pub done: bool,
/// The repos tried so far, in order.
pub repos: Vec<RepoProgress>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct RepoProgress {
pub folder: String,
/// `cloning`, `cloned`, `failed` or `exists`.
pub state: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct SetupStatus {
pub state: SetupState,
/// The script's exit code: 0 when `ok`, the code when `failed` (if known).
pub code: Option<i64>,
/// The setup log's last lines.
pub tail: Vec<String>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SetupState {
/// No setup script.
#[default]
None,
Running,
Ok,
/// Also any state this app does not know.
#[serde(other)]
Failed,
}
impl RepoProgress {
fn finished(&self) -> bool {
matches!(self.state.as_str(), "cloned" | "failed" | "exists")
}
}
impl WorkspaceStatus {
/// Nothing more will change: the clone is done and no setup is running.
pub fn settled(&self) -> bool {
self.clone.done && self.setup.state != SetupState::Running
}
pub fn failed_repos(&self) -> Vec<&str> {
self.clone
.repos
.iter()
.filter(|r| r.state == "failed")
.map(|r| r.folder.as_str())
.collect()
}
/// The start's progress line for this state (`None` once settled).
/// `total` is how many repos the project configures; the status only
/// lists the ones started so far.
pub fn progress_line(&self, total: usize) -> Option<String> {
if !self.clone.done {
let n = total.max(self.clone.repos.len());
if n == 0 {
return Some(PROGRESS_CLONING.into());
}
let finished = self.clone.repos.iter().filter(|r| r.finished()).count();
return Some(format!("{PROGRESS_CLONING} ({}/{n})", (finished + 1).min(n)));
}
(self.setup.state == SetupState::Running).then(|| PROGRESS_SETUP.into())
}
/// The setup log's last non-blank line, cleaned for a progress line.
fn last_log_line(&self) -> Option<String> {
self.setup
.tail
.iter()
.rev()
.map(|l| clean_line(l))
.find(|l| !l.is_empty())
}
/// What went wrong, for the user; `None` when nothing did.
pub fn problems(&self) -> Option<String> {
let mut parts = Vec::new();
let failed = self.failed_repos();
if !failed.is_empty() {
parts.push(format!(
"Cloning failed for {}: {}. The workspace started without {}.",
if failed.len() == 1 { "a repo" } else { "some repos" },
failed.join(", "),
if failed.len() == 1 { "it" } else { "them" },
));
}
if self.setup.state == SetupState::Failed {
let code = self.setup.code.map(|c| format!(" (exit {c})")).unwrap_or_default();
let mut s = format!("The setup script failed{code}.");
let tail: Vec<&String> = self.setup.tail.iter().rev().take(WARNING_TAIL_LINES).collect();
if !tail.is_empty() {
s.push_str("\nLast lines:\n");
s.push_str(
&tail.into_iter().rev().map(String::as_str).collect::<Vec<_>>().join("\n"),
);
}
parts.push(s);
}
(!parts.is_empty()).then(|| parts.join("\n\n"))
}
}
/// One line of the setup log as a progress message: control characters
/// dropped, trimmed, at most [`MAX_PROGRESS_LINE`] characters.
fn clean_line(line: &str) -> String {
let cleaned: String = line
.chars()
.map(|c| if c == '\t' { ' ' } else { c })
.filter(|c| !c.is_control())
.collect();
let t = cleaned.trim();
if t.chars().count() > MAX_PROGRESS_LINE {
let mut s: String = t.chars().take(MAX_PROGRESS_LINE - 1).collect();
s.push('…');
s
} else {
t.to_string()
}
}
/// Parse the script's output: its last non-blank line is the JSON (anything
/// before it — a stray warning — is ignored).
pub fn parse_status(output: &str) -> Result<WorkspaceStatus, String> {
let line = output
.lines()
.rev()
.find(|l| !l.trim().is_empty())
.ok_or("The workspace status was empty")?;
serde_json::from_str(line.trim()).map_err(|e| format!("The workspace status could not be read: {e}"))
}
// ───────────────────────────────────────────────────────────── reading
/// One read of the status.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatusRead {
Status(WorkspaceStatus),
/// No agent to ask (tunnel gone), or an image without the script.
Gone,
/// A read that failed; may be retried.
Failed(String),
}
pub trait StatusSource: Send + Sync {
fn read(&self) -> impl Future<Output = StatusRead> + Send;
}
/// The command that runs the script; exit 127 when the image predates it.
fn status_command() -> Vec<String> {
vec![
"sh".into(),
"-c".into(),
format!("[ -x {STATUS_SCRIPT} ] || exit 127; exec {STATUS_SCRIPT}"),
]
}
/// Read the status through `target`, as a background exec.
pub async fn read_status(target: &ContainerTarget) -> StatusRead {
let res = tokio::time::timeout(
READ_TIMEOUT,
target.exec_oneshot_background("claude", status_command(), STATUS_OUTPUT_LIMIT),
)
.await;
match res {
Err(_) => StatusRead::Failed("The workspace status read timed out".into()),
Ok(Err(e)) => StatusRead::Failed(e),
Ok(Ok((out, 0))) => match parse_status(&out) {
Ok(s) => StatusRead::Status(s),
Err(e) => StatusRead::Failed(e),
},
Ok(Ok((_, 126 | 127))) => StatusRead::Gone,
Ok(Ok((_, code))) => StatusRead::Failed(format!("The workspace status exited with {code}")),
}
}
/// The project's registered tunnel, looked up on every read: a stop or a
/// dropped tunnel unregisters it, and the follow then ends.
struct TunnelStatusSource<'a> {
project_id: &'a str,
}
impl StatusSource for TunnelStatusSource<'_> {
async fn read(&self) -> StatusRead {
match tunnels::get_endpoint(self.project_id) {
Some(ep) => read_status(&ContainerTarget::Agent(ep)).await,
None => StatusRead::Gone,
}
}
}
/// The status of an AWS project's workspace for Project Home: `None` for a
/// local project, one with no tunnel, or an image without the script.
pub async fn workspace_status(project: &Project) -> Result<Option<WorkspaceStatus>, String> {
if !project.is_aws() {
return Ok(None);
}
match (TunnelStatusSource { project_id: &project.id }).read().await {
StatusRead::Status(s) => Ok(Some(s)),
StatusRead::Gone => Ok(None),
StatusRead::Failed(e) => Err(e),
}
}
// ──────────────────────────────────────────────────────────── following
#[derive(Debug, Clone, Copy)]
pub struct FollowTiming {
pub interval: Duration,
pub limit: Duration,
}
impl Default for FollowTiming {
fn default() -> Self {
Self { interval: FOLLOW_INTERVAL, limit: FOLLOW_LIMIT }
}
}
/// How a follow ended.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FollowEnd {
Settled {
status: WorkspaceStatus,
/// The very first read was already settled: nothing happened in
/// this follow (an attach to a workspace that finished long ago).
first_read: bool,
},
/// [`FollowTiming::limit`] passed; the last status read, if any.
TimedOut(Option<WorkspaceStatus>),
/// The agent went away, kept failing, or the follow was cancelled:
/// nothing to say.
Lost,
}
/// Follow `src` until it settles, the limit passes, it is lost or `cancel`
/// fires, calling `progress` with each new progress line and each new setup
/// log line.
pub async fn follow_workspace_status<S: StatusSource>(
src: &S,
total_repos: usize,
timing: FollowTiming,
cancel: &CancellationToken,
progress: &(dyn Fn(&str) + Send + Sync),
) -> FollowEnd {
tokio::select! {
biased;
_ = cancel.cancelled() => FollowEnd::Lost,
end = follow_inner(src, total_repos, timing, progress) => end,
}
}
async fn follow_inner<S: StatusSource>(
src: &S,
total_repos: usize,
timing: FollowTiming,
progress: &(dyn Fn(&str) + Send + Sync),
) -> FollowEnd {
let deadline = tokio::time::Instant::now() + timing.limit;
let mut last_line: Option<String> = None;
let mut last_log: Option<String> = None;
let mut last_status: Option<WorkspaceStatus> = None;
let mut failures = 0u32;
let mut reads = 0u32;
loop {
match src.read().await {
StatusRead::Status(s) => {
failures = 0;
reads += 1;
if s.settled() {
return FollowEnd::Settled { status: s, first_read: reads == 1 };
}
if let Some(line) = s.progress_line(total_repos) {
if last_line.as_deref() != Some(line.as_str()) {
progress(&line);
last_line = Some(line);
}
}
if s.setup.state == SetupState::Running {
if let Some(log) = s.last_log_line() {
if last_log.as_deref() != Some(log.as_str()) {
progress(&log);
last_log = Some(log);
}
}
}
last_status = Some(s);
}
StatusRead::Gone => return FollowEnd::Lost,
StatusRead::Failed(e) => {
failures += 1;
log::debug!("Workspace status read failed ({failures}): {e}");
if failures >= MAX_READ_FAILURES {
log::info!("Stopped following the workspace's start: {e}");
return FollowEnd::Lost;
}
}
}
if tokio::time::Instant::now() + timing.interval > deadline {
return FollowEnd::TimedOut(last_status);
}
tokio::time::sleep(timing.interval).await;
}
}
/// What the user is told after a follow; `None` when all went well, the
/// follow was lost, or the workspace had settled before this start —
/// a failure is reported only when this follow saw it happen, or the
/// task is one this start ran (`created`). Project Home still shows an old
/// failure on demand.
pub fn follow_warning(end: &FollowEnd, limit: Duration, created: bool) -> Option<String> {
match end {
FollowEnd::Settled { status, first_read } => {
if *first_read && !created {
None
} else {
status.problems()
}
}
FollowEnd::TimedOut(s) => {
let what = match s {
Some(s) if !s.clone.done => "still cloning its repos",
_ => "still running its setup script",
};
let mut msg = format!(
"The workspace is {what} after {} minutes. It is running; Project Home shows the \
progress.",
limit.as_secs() / 60
);
if let Some(p) = s.as_ref().and_then(WorkspaceStatus::problems) {
msg.push_str("\n\n");
msg.push_str(&p);
}
Some(msg)
}
FollowEnd::Lost => None,
}
}
/// The follow of one project's workspace. `follower` is its registration
/// with the tunnel registry (`tunnels::begin_follow`), made by the caller
/// before the follow is spawned so registrations keep the order of the starts
/// (a quick Stop→Start can't leave the old follower registered last).
/// `on_warning` gets the warning, if any.
pub async fn run_follow<S: StatusSource>(
src: &S,
project_id: &str,
follower: (u64, CancellationToken),
total_repos: usize,
created: bool,
timing: FollowTiming,
on_warning: impl FnOnce(String),
) {
let (id, cancel) = follower;
let log_line = |m: &str| log::debug!("Project {project_id}: {m}");
let end = follow_workspace_status(src, total_repos, timing, &cancel, &log_line).await;
tunnels::end_follow(project_id, id);
if let Some(w) = follow_warning(&end, timing.limit, created) {
on_warning(w);
}
}
/// After a successful start of an AWS project: follow its clone and setup
/// script in the background (see the module docs). `false` (nothing
/// spawned) for a local project or one with no tunnel.
pub fn spawn_follow(
project: &Project,
on_warning: impl FnOnce(String) + Send + 'static,
) -> bool {
if !project.is_aws() || tunnels::get_endpoint(&project.id).is_none() {
return false;
}
let project_id = project.id.clone();
let total = project.aws.as_ref().map_or(0, |a| a.repos.len());
let created = tunnels::ran_task(&project_id);
// Registered now, not in the spawned task: see `run_follow`.
let follower = tunnels::begin_follow(&project_id);
tauri::async_runtime::spawn(async move {
let src = TunnelStatusSource { project_id: &project_id };
run_follow(&src, &project_id, follower, total, created, FollowTiming::default(), on_warning)
.await;
});
true
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,285 @@
use std::sync::Mutex;
use super::*;
use crate::host::tunnels;
/// Returns its scripted reads in order, then repeats the last one.
struct Scripted {
reads: Mutex<Vec<StatusRead>>,
count: Mutex<usize>,
}
impl Scripted {
fn new(reads: Vec<StatusRead>) -> Self {
Self { reads: Mutex::new(reads), count: Mutex::new(0) }
}
fn count(&self) -> usize {
*self.count.lock().unwrap()
}
}
impl StatusSource for Scripted {
async fn read(&self) -> StatusRead {
*self.count.lock().unwrap() += 1;
let mut r = self.reads.lock().unwrap();
if r.len() > 1 {
r.remove(0)
} else {
r[0].clone()
}
}
}
fn st(json: &str) -> StatusRead {
StatusRead::Status(parse_status(json).unwrap())
}
async fn follow(src: &Scripted, total: usize) -> (FollowEnd, Vec<String>) {
let lines = Mutex::new(Vec::new());
let progress = |m: &str| lines.lock().unwrap().push(m.to_string());
let cancel = CancellationToken::new();
let end = follow_workspace_status(src, total, FollowTiming::default(), &cancel, &progress).await;
(end, lines.into_inner().unwrap())
}
const NOT_STARTED: &str = r#"{"clone":{"done":false,"repos":[]},"setup":{"state":"running","code":null,"tail":[]}}"#;
const CLONING_1: &str = r#"{"clone":{"done":false,"repos":[{"folder":"a","state":"cloning"}]},"setup":{"state":"running","code":null,"tail":[]}}"#;
const CLONING_2: &str = r#"{"clone":{"done":false,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"cloning"}]},"setup":{"state":"running","code":null,"tail":[]}}"#;
const SETUP_1: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"failed"}]},"setup":{"state":"running","code":null,"tail":["Reading package lists...",""," Setting up jq "]}}"#;
const SETUP_2: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"failed"}]},"setup":{"state":"running","code":null,"tail":["Setting up jq","done"]}}"#;
const SETUP_FAILED: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"},{"folder":"b","state":"failed"}]},"setup":{"state":"failed","code":2,"tail":["Setting up jq","E: oops"]}}"#;
const ALL_OK: &str = r#"{"clone":{"done":true,"repos":[{"folder":"a","state":"cloned"}]},"setup":{"state":"none","code":null,"tail":[]}}"#;
#[tokio::test(start_paused = true)]
async fn follow_emits_each_change_once_and_stops_when_settled() {
let src = Scripted::new(vec![
st(NOT_STARTED),
st(CLONING_1),
st(CLONING_1),
st(CLONING_2),
st(SETUP_1),
st(SETUP_1),
st(SETUP_2),
st(SETUP_FAILED),
]);
let started = tokio::time::Instant::now();
let (end, lines) = follow(&src, 2).await;
assert_eq!(
lines,
[
"Cloning repos… (1/2)",
"Cloning repos… (2/2)",
"Running setup script…",
"Setting up jq",
"done",
]
);
assert_eq!(src.count(), 8);
// Seven waits of 2 s between eight reads.
assert_eq!(started.elapsed(), FOLLOW_INTERVAL * 7);
let FollowEnd::Settled { status: s, first_read: false } = &end else { panic!("{end:?}") };
// Seen failing in this follow: reported even on an attach.
let w = follow_warning(&end, FOLLOW_LIMIT, false).unwrap();
assert!(w.contains("Cloning failed for a repo: b."), "{w}");
assert!(w.contains("The setup script failed (exit 2)."), "{w}");
assert!(w.ends_with("Last lines:\nSetting up jq\nE: oops"), "{w}");
assert_eq!(s.failed_repos(), ["b"]);
}
#[tokio::test(start_paused = true)]
async fn a_workspace_already_settled_ends_at_once_without_a_warning() {
let src = Scripted::new(vec![st(ALL_OK)]);
let started = tokio::time::Instant::now();
let (end, lines) = follow(&src, 1).await;
assert!(lines.is_empty());
assert_eq!(started.elapsed(), std::time::Duration::ZERO);
assert_eq!(follow_warning(&end, FOLLOW_LIMIT, true), None);
}
#[tokio::test(start_paused = true)]
async fn an_old_failure_warns_only_for_a_task_this_start_ran() {
let src = Scripted::new(vec![st(SETUP_FAILED)]);
let (end, lines) = follow(&src, 2).await;
assert!(lines.is_empty());
assert!(matches!(end, FollowEnd::Settled { first_read: true, .. }), "{end:?}");
// Attached to a workspace that failed long ago: no toast.
assert_eq!(follow_warning(&end, FOLLOW_LIMIT, false), None);
// This start ran the task and it failed before the first read.
let w = follow_warning(&end, FOLLOW_LIMIT, true).unwrap();
assert!(w.contains("The setup script failed (exit 2)."), "{w}");
}
#[tokio::test(start_paused = true)]
async fn follow_times_out_after_35_minutes() {
let src = Scripted::new(vec![st(CLONING_1)]);
let started = tokio::time::Instant::now();
let (end, lines) = follow(&src, 3).await;
assert_eq!(lines, ["Cloning repos… (1/3)"]);
assert!(matches!(end, FollowEnd::TimedOut(Some(_))), "{end:?}");
assert!(started.elapsed() <= FOLLOW_LIMIT);
assert!(started.elapsed() > FOLLOW_LIMIT - FOLLOW_INTERVAL * 2);
let w = follow_warning(&end, FOLLOW_LIMIT, false).unwrap();
assert!(w.contains("still cloning its repos after 35 minutes"), "{w}");
}
#[tokio::test(start_paused = true)]
async fn a_timed_out_setup_says_so_and_carries_any_clone_failure() {
let src = Scripted::new(vec![st(SETUP_2)]);
let (end, _) = follow(&src, 2).await;
let w = follow_warning(&end, FOLLOW_LIMIT, false).unwrap();
assert!(w.contains("still running its setup script"), "{w}");
assert!(w.contains("Cloning failed for a repo: b."), "{w}");
}
#[tokio::test(start_paused = true)]
async fn a_lost_agent_ends_the_follow_quietly() {
let src = Scripted::new(vec![st(CLONING_1), StatusRead::Gone]);
let (end, lines) = follow(&src, 1).await;
assert_eq!(end, FollowEnd::Lost);
assert_eq!(lines, ["Cloning repos… (1/1)"]);
assert_eq!(follow_warning(&end, FOLLOW_LIMIT, false), None);
}
#[tokio::test(start_paused = true)]
async fn failing_reads_are_retried_then_give_up_quietly() {
let fail = || StatusRead::Failed("tunnel hiccup".into());
// Four failures, then a good read: the count resets.
let src = Scripted::new(vec![fail(), fail(), fail(), fail(), st(ALL_OK)]);
assert!(matches!(follow(&src, 1).await.0, FollowEnd::Settled { .. }));
let src = Scripted::new(vec![fail()]);
let (end, _) = follow(&src, 1).await;
assert_eq!(end, FollowEnd::Lost);
assert_eq!(src.count(), MAX_READ_FAILURES as usize);
}
#[test]
fn status_parses_the_scripts_output_and_tolerates_drift() {
let s = parse_status(&format!("a stray warning\n{SETUP_FAILED}\n\n")).unwrap();
assert_eq!(s.setup.state, SetupState::Failed);
assert_eq!(s.setup.code, Some(2));
assert!(s.clone.done);
// Unknown fields and states, missing fields.
let s = parse_status(r#"{"clone":{"done":true,"extra":1},"setup":{"state":"paused"},"new":{}}"#)
.unwrap();
assert!(s.clone.repos.is_empty());
assert_eq!(s.setup.state, SetupState::Failed);
assert_eq!(parse_status("{}").unwrap(), WorkspaceStatus::default());
assert!(parse_status("").is_err());
assert!(parse_status("not json").is_err());
}
#[test]
fn progress_lines_count_repos_and_clean_log_lines() {
let s = parse_status(NOT_STARTED).unwrap();
assert_eq!(s.progress_line(0).as_deref(), Some("Cloning repos…"));
assert_eq!(s.progress_line(3).as_deref(), Some("Cloning repos… (1/3)"));
let s = parse_status(ALL_OK).unwrap();
assert!(s.settled());
assert_eq!(s.progress_line(1), None);
let mut s = parse_status(SETUP_2).unwrap();
s.setup.tail = vec![format!("\x1b[31m{}\x1b[0m", "x".repeat(500))];
let l = s.last_log_line().unwrap();
assert_eq!(l.chars().count(), MAX_PROGRESS_LINE);
assert!(l.starts_with("[31mxxx") && l.ends_with('…'), "{l}");
assert!(!l.contains('\x1b'));
}
#[test]
fn status_serialises_in_the_scripts_shape() {
let s = parse_status(SETUP_FAILED).unwrap();
let v = serde_json::to_value(&s).unwrap();
assert_eq!(v, serde_json::from_str::<serde_json::Value>(SETUP_FAILED).unwrap());
}
#[test]
fn the_status_command_reports_a_missing_script_as_127() {
let cmd = status_command();
assert_eq!(cmd[..2], ["sh", "-c"]);
assert!(cmd[2].contains("|| exit 127") && cmd[2].contains(STATUS_SCRIPT));
}
#[tokio::test]
async fn a_local_project_has_no_workspace_status_and_no_follower() {
let p = Project::new("p".into(), vec![]);
assert_eq!(workspace_status(&p).await, Ok(None));
assert!(!spawn_follow(&p, |_| panic!("no warning for Docker")));
assert!(!tunnels::is_following(&p.id));
}
/// Never answers: a follow in progress until something ends it.
struct Hang;
impl StatusSource for Hang {
async fn read(&self) -> StatusRead {
std::future::pending().await
}
}
#[tokio::test(start_paused = true)]
async fn stop_ends_a_follow_in_progress_without_a_warning() {
let id = "follow-stop-test";
let task = tokio::spawn(async move {
let warned = std::sync::Arc::new(Mutex::new(false));
let w = warned.clone();
let follower = tunnels::begin_follow(id);
run_follow(&Hang, id, follower, 1, true, FollowTiming::default(), move |_| {
*w.lock().unwrap() = true
})
.await;
let v = *warned.lock().unwrap();
v
});
tokio::time::sleep(FOLLOW_INTERVAL * 3).await;
assert!(tunnels::is_following(id));
// What Stop and Remove call (`stop_project_container`, `stop_project`,
// `remove_resources`); closing the tunnel does the same.
tunnels::cancel_follow(id);
let warned = tokio::time::timeout(std::time::Duration::from_secs(1), task)
.await
.expect("the follow ends at once")
.unwrap();
assert!(!warned);
assert!(!tunnels::is_following(id));
}
#[tokio::test(start_paused = true)]
async fn a_new_follow_replaces_the_old_one() {
let id = "follow-replace-test";
let follower = tunnels::begin_follow(id);
let first = tokio::spawn(run_follow(&Hang, id, follower, 1, true, FollowTiming::default(), |_| {}));
tokio::time::sleep(FOLLOW_INTERVAL).await;
let (_, second) = tunnels::begin_follow(id);
tokio::time::timeout(std::time::Duration::from_secs(1), first).await.unwrap().unwrap();
// The old follower's end does not unregister the new one.
assert!(tunnels::is_following(id));
assert!(!second.is_cancelled());
tunnels::cancel_follow(id);
assert!(second.is_cancelled());
}
#[tokio::test(start_paused = true)]
async fn followers_registered_in_start_order_win_in_that_order_however_they_are_scheduled() {
// A quick Stop→Start: the old start's follower is registered first, the
// new one second, but the new one's task happens to run first.
let id = "follow-order-test";
let old = tunnels::begin_follow(id);
let new = tunnels::begin_follow(id);
let new_token = new.1.clone();
let new_task = tokio::spawn(run_follow(&Hang, id, new, 1, true, FollowTiming::default(), |_| {}));
tokio::time::sleep(FOLLOW_INTERVAL).await;
let old_task = tokio::spawn(run_follow(&Hang, id, old, 1, true, FollowTiming::default(), |_| {}));
// The old one was cancelled at registration: it ends at once and leaves
// the new one registered and running.
tokio::time::timeout(std::time::Duration::from_secs(1), old_task).await.unwrap().unwrap();
assert!(tunnels::is_following(id));
assert!(!new_token.is_cancelled());
assert!(!new_task.is_finished());
tunnels::cancel_follow(id);
tokio::time::timeout(std::time::Duration::from_secs(1), new_task).await.unwrap().unwrap();
}
#[test]
fn the_follower_takes_no_project_lock() {
// Stop must never wait for it (ruling R5).
let src = include_str!("../status.rs");
assert!(!src.contains("project_lock"));
}
+450 -4
View File
@@ -155,11 +155,26 @@ impl WorkspaceApi for Fake {
async fn create_access_point(
&self,
_fs: &str,
_root: &str,
root: &str,
_tags: &[(String, String)],
) -> Result<String, AwsError> {
self.log("create-ap");
Ok("fsap-new".into())
Ok(if root.starts_with("/triple-c-workspaces/") { "fsap-ws" } else { "fsap-new" }.into())
}
async fn project_secret_value(&self, _name: &str) -> Result<Option<String>, AwsError> {
Ok(None)
}
async fn create_project_secret(
&self,
n: &str,
k: &str,
j: &str,
t: &[(String, String)],
) -> Result<Option<String>, AwsError> {
self.put_project_secret(n, k, j, t).await.map(Some)
}
async fn delete_project_secret(&self, _name: &str) -> Result<(), AwsError> {
Ok(())
}
async fn put_project_secret(
&self,
@@ -247,6 +262,10 @@ impl Connector for Fake {
}
}
fn no_gateway() -> Result<Option<SidecarSecrets>, String> {
Ok(None)
}
fn inputs<'a>(
p: &'a Project,
who: &'a CallerIdentity,
@@ -258,7 +277,9 @@ fn inputs<'a>(
who,
env_pairs: &[],
files: &[],
custom_env_keys: &[],
image: REGISTRY_IMAGE,
gateway: &no_gateway,
wait: WaitTiming {
interval: Duration::from_millis(1),
timeout: Duration::from_millis(30),
@@ -309,6 +330,76 @@ async fn a_first_start_prepares_runs_waits_and_connects_in_that_order() {
);
}
#[tokio::test]
async fn an_efs_start_waits_for_both_access_points_and_records_them() {
let p = aws_project(StorageMode::Efs);
let fake = Fake { run: Some(task(&p, "new", "RUNNING", true)), ..Default::default() };
let w = who();
let records = Mutex::new(Vec::new());
let on_task = |r: Option<&TaskRecord>| records.lock().unwrap().push(r.cloned());
let started = start_core(&fake, &fake, &fake, &fake, &inputs(&p, &w, &on_task), &|_| {})
.await
.unwrap();
assert_eq!(started.access_point_id.as_deref(), Some("fsap-new"));
assert_eq!(started.workspace_access_point_id.as_deref(), Some("fsap-ws"));
let calls = fake.calls();
let pos = |c: &str| calls.iter().position(|x| x == c).unwrap_or_else(|| panic!("{c}: {calls:?}"));
assert!(pos("ap-state fsap-new") < pos("run"), "{calls:?}");
assert!(pos("ap-state fsap-ws") < pos("run"), "{calls:?}");
let recs = records.into_inner().unwrap();
let rec = recs[0].as_ref().unwrap();
assert_eq!(rec.access_point_id.as_deref(), Some("fsap-new"));
assert_eq!(rec.workspace_access_point_id.as_deref(), Some("fsap-ws"));
// Hybrid waits for one and records none for /workspace.
let p = aws_project(StorageMode::Hybrid);
let fake = Fake { run: Some(task(&p, "new", "RUNNING", true)), ..Default::default() };
let records = Mutex::new(Vec::new());
let on_task = |r: Option<&TaskRecord>| records.lock().unwrap().push(r.cloned());
let started = start_core(&fake, &fake, &fake, &fake, &inputs(&p, &w, &on_task), &|_| {})
.await
.unwrap();
assert_eq!(started.workspace_access_point_id, None);
assert!(!fake.called("ap-state fsap-ws"));
assert_eq!(records.into_inner().unwrap()[0].as_ref().unwrap().workspace_access_point_id, None);
}
#[test]
fn switching_efs_to_hybrid_keeps_the_stored_workspace_access_point() {
let mut p = aws_project(StorageMode::Hybrid);
p.aws.as_mut().unwrap().workspace_access_point_id = Some("fsap-ws".into());
let (_d, store) = store_with(&p);
// A Hybrid start knows no /workspace access point: the stored one stays.
record_task(
&store,
&p.id,
Some(&TaskRecord {
task_arn: "arn:t".into(),
access_point_id: Some("fsap-home".into()),
task_definition_arn: None,
workspace_access_point_id: None,
}),
);
let a = store.get(&p.id).unwrap().aws.unwrap();
assert_eq!(a.workspace_access_point_id.as_deref(), Some("fsap-ws"));
record_task(&store, &p.id, None);
let a = store.get(&p.id).unwrap().aws.unwrap();
assert_eq!(a.workspace_access_point_id.as_deref(), Some("fsap-ws"));
// An EFS start records its own.
record_task(
&store,
&p.id,
Some(&TaskRecord {
task_arn: "arn:t2".into(),
access_point_id: None,
task_definition_arn: None,
workspace_access_point_id: Some("fsap-ws2".into()),
}),
);
let a = store.get(&p.id).unwrap().aws.unwrap();
assert_eq!(a.workspace_access_point_id.as_deref(), Some("fsap-ws2"));
}
#[tokio::test]
async fn a_running_task_is_attached_with_its_own_token_and_nothing_is_rewritten() {
let p = aws_project(StorageMode::Hybrid);
@@ -327,6 +418,35 @@ async fn a_running_task_is_attached_with_its_own_token_and_nothing_is_rewritten(
assert_eq!(steps, vec![PROGRESS_READING, PROGRESS_WAITING, PROGRESS_CONNECTING]);
}
/// A gateway payload that cannot be built: the provider key is gone.
fn missing_gateway_key() -> Result<Option<SidecarSecrets>, String> {
Err("The LLM gateway sidecar needs the provider API key".into())
}
#[tokio::test]
async fn attaching_to_a_running_task_never_builds_the_gateway_payload() {
let mut p = aws_project(StorageMode::Hybrid);
p.aws.as_mut().unwrap().gateway_sidecar = true;
let live = task(&p, "live", "RUNNING", true);
let fake = Fake {
listed: Mutex::new(vec![vec![live.arn.clone()]]),
..Default::default()
}
.with_task(live);
let w = who();
let on_task = |_: Option<&TaskRecord>| {};
let inp = StartInputs { gateway: &missing_gateway_key, ..inputs(&p, &w, &on_task) };
let started = start_core(&fake, &fake, &fake, &fake, &inp, &|_| {}).await.unwrap();
assert!(!started.created);
// A start that prepares a new task does need it, and fails before RunTask.
let fake = Fake::default();
let inp = StartInputs { gateway: &missing_gateway_key, ..inputs(&p, &w, &on_task) };
let e = start_core(&fake, &fake, &fake, &fake, &inp, &|_| {}).await.err().unwrap();
assert!(e.contains("provider API key"), "{e}");
assert!(!fake.called("run"));
}
#[tokio::test]
async fn losing_the_run_race_is_a_clear_error_and_nothing_is_stopped() {
let p = aws_project(StorageMode::Ephemeral);
@@ -666,7 +786,12 @@ fn a_start_records_its_task_and_keeps_the_stored_handles_it_does_not_know() {
record_task(
&store,
&p.id,
Some(&TaskRecord { task_arn: "arn:t".into(), access_point_id: None, task_definition_arn: Some("arn:td:2".into()) }),
Some(&TaskRecord {
task_arn: "arn:t".into(),
access_point_id: None,
task_definition_arn: Some("arn:td:2".into()),
workspace_access_point_id: None,
}),
);
let a = store.get(&p.id).unwrap().aws.unwrap();
assert_eq!((a.task_arn.as_deref(), a.access_point_id.as_deref(), a.task_definition_arn.as_deref()),
@@ -720,7 +845,12 @@ fn the_log_task_is_the_stored_one_else_the_last_one_a_start_saw() {
record_task(
&store,
&p.id,
Some(&TaskRecord { task_arn: "arn:first".into(), access_point_id: None, task_definition_arn: None }),
Some(&TaskRecord {
task_arn: "arn:first".into(),
access_point_id: None,
task_definition_arn: None,
workspace_access_point_id: None,
}),
);
record_task(&store, &p.id, None);
assert_eq!(log_task_arn(&p).as_deref(), Some("arn:first"), "a cleared ARN still has a log");
@@ -795,3 +925,319 @@ async fn an_unattached_aws_project_gets_a_target_id_no_tunnel_can_have() {
let local = Project::new("l".into(), Vec::new());
assert_eq!(unattached_container_id(&local), None);
}
#[tokio::test]
async fn a_persisted_starting_aws_project_with_a_task_arn_is_reconciled_against_ecs_on_load() {
use crate::commands::project_commands::needs_aws_reconcile;
use crate::storage::projects_store::settle_stale_statuses;
// The app quit mid-start: Starting, the task's ARN already recorded.
let mut p = aws_project(StorageMode::Hybrid);
p.status = ProjectStatus::Starting;
p.aws.as_mut().unwrap().task_arn = Some(task_arn("mid"));
let mut docker = Project::new("local".into(), vec![]);
docker.status = ProjectStatus::Starting;
let mut loaded = vec![p.clone(), docker];
assert!(settle_stale_statuses(&mut loaded));
// Not Stopped (that would hide a billed task): Error, ARN kept.
assert_eq!(loaded[0].status, ProjectStatus::Error);
assert_eq!(loaded[0].aws.as_ref().unwrap().task_arn, Some(task_arn("mid")));
assert_eq!(loaded[1].status, ProjectStatus::Stopped, "Docker is unchanged");
assert!(needs_aws_reconcile(&loaded[0]));
// ECS says it runs: Running.
let p = loaded[0].clone();
let fake = Fake::default().with_task(task(&p, "mid", "RUNNING", true));
let (_d, store) = store_with(&p);
assert!(matches!(reconcile_project(&fake, &store, &p, &find(&p)).await.unwrap(), Reconciled::Live(_)));
assert_eq!(store.get(&p.id).unwrap().status, ProjectStatus::Running);
// ECS says it is gone: Stopped, ARN cleared.
let (_d, store) = store_with(&p);
assert_eq!(reconcile_project(&Fake::default(), &store, &p, &find(&p)).await.unwrap(), Reconciled::Gone);
let after = store.get(&p.id).unwrap();
assert_eq!(after.status, ProjectStatus::Stopped);
assert_eq!(after.aws.unwrap().task_arn, None);
}
#[test]
fn a_stopped_aws_project_is_reconciled_only_when_it_still_has_a_task_arn() {
use crate::commands::project_commands::needs_aws_reconcile;
let mut p = aws_project(StorageMode::Hybrid);
p.status = ProjectStatus::Stopped;
assert!(!needs_aws_reconcile(&p), "a clean stop clears the ARN");
p.aws.as_mut().unwrap().task_arn = Some(task_arn("left"));
assert!(needs_aws_reconcile(&p));
for s in [ProjectStatus::Running, ProjectStatus::Error, ProjectStatus::Starting, ProjectStatus::Stopping] {
p.status = s;
p.aws.as_mut().unwrap().task_arn = None;
assert!(needs_aws_reconcile(&p));
}
}
// ──────────────────────────────────────────── copy-at-connect (Q2)
mod copy_at_connect {
use super::*;
use crate::host::aws_host::connect::*;
use crate::models::project::SecretsDelivery;
use crate::models::EnvVar;
fn connect_project(delivery: SecretsDelivery) -> Project {
let mut p = aws_project(StorageMode::Hybrid);
p.aws.as_mut().unwrap().secrets_delivery = delivery;
p
}
/// `(dir, name, data, mode)`.
type Upload = (String, String, Vec<u8>, u32);
#[derive(Default)]
struct FakeTarget {
calls: Mutex<Vec<String>>,
uploads: Mutex<Vec<Upload>>,
apply_result: Mutex<Option<(String, i64)>>,
}
impl ConnectTarget for FakeTarget {
async fn exec_root(&self, cmd: Vec<String>) -> Result<(String, i64), String> {
self.calls.lock().unwrap().push(format!("exec {}", cmd.join(" ")));
if cmd.get(1).map(String::as_str) == Some("--prepare") {
return Ok(("0123456789abcdef\n".into(), 0));
}
if let Some(r) = self.apply_result.lock().unwrap().clone() {
return Ok(r);
}
Ok(("triple-c-apply-connect: credentials applied\n".into(), 0))
}
async fn upload_root(&self, dir: &str, name: &str, data: &[u8], mode: u32) -> Result<(), String> {
self.calls.lock().unwrap().push(format!("upload {dir}/{name}"));
self.uploads.lock().unwrap().push((dir.into(), name.into(), data.to_vec(), mode));
Ok(())
}
}
fn bundle() -> ConnectBundle {
ConnectBundle {
files: vec![
("ssh/id_ed25519".into(), b"PRIVATE".to_vec().into()),
("ca/corp.crt".into(), b"PEM".to_vec().into()),
],
env: vec![
("GIT_TOKEN".into(), String::from("ghp_x").into()),
("MULTI".into(), String::from("a=b\nc").into()),
],
}
}
#[tokio::test]
async fn a_push_prepares_uploads_root_only_then_applies() {
let t = FakeTarget::default();
push_connect_bundle(&t, &bundle()).await.unwrap();
assert_eq!(
*t.calls.lock().unwrap(),
vec![
"exec /usr/local/bin/triple-c-apply-connect --prepare",
"upload /dev/shm/triple-c/connect/0123456789abcdef/ssh/id_ed25519",
"upload /dev/shm/triple-c/connect/0123456789abcdef/ca/corp.crt",
"upload /dev/shm/triple-c/connect/0123456789abcdef/connect.env",
"exec /usr/local/bin/triple-c-apply-connect 0123456789abcdef",
]
);
let uploads = t.uploads.lock().unwrap();
assert!(uploads.iter().all(|u| u.3 == 0o600), "every staged file is 0600");
assert_eq!(uploads[0].2, b"PRIVATE");
// KEY=<base64> lines: a newline or `=` in a value reads back whole.
assert_eq!(
String::from_utf8(uploads[2].2.clone()).unwrap(),
"GIT_TOKEN=Z2hwX3g=\nMULTI=YT1iCmM=\n"
);
}
#[tokio::test]
async fn a_failed_apply_reports_its_output_and_an_old_image_says_so() {
let t = FakeTarget::default();
*t.apply_result.lock().unwrap() = Some(("line\ntriple-c-apply-connect: /x is missing\n".into(), 1));
let e = push_connect_bundle(&t, &bundle()).await.unwrap_err();
assert!(e.contains("failed (1)") && e.contains("/x is missing"), "{e}");
assert!(!e.contains("ghp_x"));
let t = FakeTarget::default();
*t.apply_result.lock().unwrap() = Some(("not found".into(), 127));
let e = push_connect_bundle(&t, &bundle()).await.unwrap_err();
assert!(e.contains("predates copying credentials"), "{e}");
// A bad file key is refused before anything is uploaded under it.
let t = FakeTarget::default();
let bad = ConnectBundle { files: vec![("ssh/../x".into(), Vec::new().into())], env: vec![] };
assert!(push_connect_bundle(&t, &bad).await.is_err());
assert!(t.uploads.lock().unwrap().is_empty());
}
#[test]
fn only_a_well_formed_connect_id_is_used_as_a_path() {
assert_eq!(parse_nonce("noise\n0123456789abcdef\n").unwrap(), "0123456789abcdef");
for bad in ["", "../../etc/passwd", "0123456789ABCDEF", "0123456789abcde", "0123456789abcdef0"] {
assert!(parse_nonce(bad).is_err(), "{bad}");
}
}
#[test]
fn a_bundle_never_shows_its_values() {
let shown = format!("{:?}", bundle());
assert!(shown.contains("GIT_TOKEN") && shown.contains("ssh/id_ed25519"), "{shown}");
assert!(!shown.contains("ghp_x") && !shown.contains("PRIVATE"), "{shown}");
}
#[test]
fn the_bundle_is_the_ssh_and_ca_files_and_the_credential_variables() {
let base = tempfile::tempdir().unwrap();
let ssh = base.path().join("ssh");
std::fs::create_dir(&ssh).unwrap();
std::fs::write(ssh.join("id_ed25519"), b"KEY").unwrap();
let mut p = connect_project(SecretsDelivery::CopyAtConnect);
p.ssh_key_path = Some(ssh.to_str().unwrap().into());
p.git_token = Some("ghp_tok".into());
p.custom_env_vars = vec![EnvVar { key: "MY_KEY".into(), value: "v1".into() }];
let settings = AppSettings {
global_custom_env_vars: vec![EnvVar { key: "GLOBAL_KEY".into(), value: "g".into() }],
..Default::default()
};
let b = connect_credentials(&p, &settings).unwrap();
assert_eq!(b.files.len(), 1);
assert_eq!((b.files[0].0.as_str(), b.files[0].1.as_slice()), ("ssh/id_ed25519", &b"KEY"[..]));
let env: HashMap<String, String> = b.env.iter().map(|(k, v)| (k.clone(), v.to_string())).collect();
assert_eq!(env["GIT_TOKEN"], "ghp_tok");
assert_eq!(env["MY_KEY"], "v1");
assert_eq!(env["GLOBAL_KEY"], "g");
assert!(!env.contains_key("TZ") && !env.contains_key("CLAUDE_CODE_SETTINGS_JSON"));
assert!(b.env.iter().all(|(k, _)| crate::aws::workspace::is_credential_key(
k,
&["MY_KEY".to_string(), "GLOBAL_KEY".to_string()]
)));
}
#[test]
fn a_terminal_gets_the_api_keys_even_when_a_file_is_unreadable() {
let mut p = connect_project(SecretsDelivery::CopyAtConnect);
p.ssh_key_path = Some("/nonexistent/ssh-dir".into());
p.ca_cert_path = Some("/nonexistent/ca.pem".into());
p.git_token = Some("ghp_tok".into());
let settings = AppSettings::default();
assert!(connect_credentials(&p, &settings).is_err(), "the push needs the files");
let env: HashMap<String, String> =
connect_env(&p, &settings).into_iter().map(|(k, v)| (k, v.to_string())).collect();
assert_eq!(env["GIT_TOKEN"], "ghp_tok");
}
#[test]
fn a_copy_at_connect_terminal_carries_the_credentials_and_a_secrets_manager_one_does_not() {
let creds = || Ok(bundle().env);
let p = connect_project(SecretsDelivery::CopyAtConnect);
let spec = crate::host::sessions::session_spec(
vec!["bash".into(), "-l".into()],
true,
terminal_env_with(&p, creds),
);
assert_eq!(spec.env, vec!["GIT_TOKEN=ghp_x".to_string(), "MULTI=a=b\nc".to_string()]);
assert!(spec.tty && spec.user == "claude");
assert_eq!(spec.working_dir.as_deref(), Some("/workspace"));
let p = connect_project(SecretsDelivery::SecretsManager);
let env = terminal_env_with(&p, || panic!("not gathered for Secrets Manager"));
assert!(crate::host::sessions::session_spec(vec!["bash".into()], true, env).env.is_empty());
// A local project never; a failure opens the terminal without them.
let mut local = connect_project(SecretsDelivery::CopyAtConnect);
local.host_id = None;
assert!(terminal_env_with(&local, || panic!("local")).is_empty());
let p = connect_project(SecretsDelivery::CopyAtConnect);
assert!(terminal_env_with(&p, || Err("no CA file".into())).is_empty());
}
#[test]
fn only_a_connected_copy_at_connect_project_gets_a_push() {
let p = connect_project(SecretsDelivery::SecretsManager);
assert!(!copies_at_connect(&p));
assert!(!spawn_connect_push(&p, &AppSettings::default(), |_| panic!("no push")));
let p = connect_project(SecretsDelivery::CopyAtConnect);
assert!(copies_at_connect(&p));
// No tunnel registered for it: nothing to push to.
assert!(!spawn_connect_push(&p, &AppSettings::default(), |_| panic!("no tunnel")));
}
#[test]
fn a_sidecar_terminal_keeps_the_tasks_own_token() {
use crate::models::project::{Backend, OpenAiCompatibleConfig};
let mut p = connect_project(SecretsDelivery::CopyAtConnect);
p.backend = Backend::OpenAiCompatible;
p.openai_compatible_config = Some(OpenAiCompatibleConfig {
base_url: "http://host.docker.internal:4000".into(),
api_key: Some("sk-local".into()),
model_id: Some("gpt".into()),
haiku_model_id: None,
});
p.git_token = Some("ghp_tok".into());
let settings = AppSettings::default();
let keys = |p: &Project| -> Vec<String> {
connect_env(p, &settings).into_iter().map(|(k, _)| k).collect()
};
assert!(keys(&p).contains(&"ANTHROPIC_AUTH_TOKEN".to_string()));
p.aws.as_mut().unwrap().gateway_sidecar = true;
let with_sidecar = keys(&p);
assert!(!with_sidecar.contains(&"ANTHROPIC_AUTH_TOKEN".to_string()), "{with_sidecar:?}");
assert!(with_sidecar.contains(&"GIT_TOKEN".to_string()));
}
}
// ──────────────────────────────────────────── gateway sidecar (Q7)
mod gateway_sidecar_inputs {
use super::*;
use crate::models::gateway_settings::GatewayModel;
use crate::models::project::Backend;
fn sidecar(on: bool) -> (Project, AppSettings) {
let mut p = aws_project(StorageMode::Ephemeral);
p.backend = Backend::OpenAiCompatible;
p.aws.as_mut().unwrap().gateway_sidecar = on;
let mut s = AppSettings::default();
s.gateway.models = vec![GatewayModel { name: "gpt".into(), model_id: "gpt-5".into() }];
(p, s)
}
#[test]
fn off_reads_no_key() {
let (p, s) = sidecar(false);
assert!(gateway_sidecar(&p, &s, || panic!("no keychain read")).unwrap().is_none());
}
#[test]
fn on_renders_a_key_free_config_and_a_fresh_master_key_each_start() {
let (p, s) = sidecar(true);
let a = gateway_sidecar(&p, &s, || Ok(Some("sk-provider".into()))).unwrap().unwrap();
let b = gateway_sidecar(&p, &s, || Ok(Some("sk-provider".into()))).unwrap().unwrap();
let (pa, pb) = (a.payload(), b.payload());
assert_eq!(pa.api_key, "sk-provider");
assert!(!pa.config.contains("sk-provider") && !pa.config.contains(pa.master_key));
assert!(pa.config.contains("os.environ/TRIPLEC_GATEWAY_API_KEY"));
assert!(pa.config.contains("model: \"openai/gpt-5\""));
assert_eq!(pa.master_key.len(), 64);
assert_ne!(pa.master_key, pb.master_key);
}
#[test]
fn on_refuses_a_missing_key_backend_or_model() {
let (p, s) = sidecar(true);
for key in [None, Some(" ".to_string())] {
let e = gateway_sidecar(&p, &s, || Ok(key.clone())).err().unwrap();
assert!(e.contains("provider API key"), "{e}");
}
let mut wrong = p.clone();
wrong.backend = Backend::Anthropic;
assert!(gateway_sidecar(&wrong, &s, || Ok(Some("k".into()))).err().unwrap().contains("OpenAI Compatible"));
let mut empty = s.clone();
empty.gateway.models.clear();
assert!(gateway_sidecar(&p, &empty, || Ok(Some("k".into()))).err().unwrap().contains("Model Gateway"));
}
}
+1 -1
View File
@@ -132,7 +132,7 @@ impl ProjectHost {
/// AWS: stop the task and remove the project's resources — task
/// definition revisions, the secret (deleted at once) and, only
/// with `discard_data`, the EFS access point (not its files). Docker: nothing (its state
/// with `discard_data`, the EFS access points, home and EFS-mode `/workspace` (not their files). Docker: nothing (its state
/// goes with [`Self::discard_project_state`]).
pub async fn remove_aws_resources(
&self,
+1
View File
@@ -30,6 +30,7 @@
pub mod agent;
pub mod aws_host;
pub mod lifecycle;
pub mod port_forwards;
pub mod sessions;
pub mod ssm;
pub mod tunnels;
+390
View File
@@ -0,0 +1,390 @@
//! A project's port mappings on a host with no Docker port publishing (AWS).
//!
//! Docker publishes a mapping itself (`0.0.0.0:<host>` → the container's
//! network interface). A Fargate task has no such thing, so each TCP mapping
//! becomes a host **loopback** listener (`127.0.0.1` and `::1`, never a
//! wildcard) carried into the task over `socat` on an attached exec — the
//! auth bridge's [`PortForward`], which works through the agent like any
//! other exec (constraints Q6). Nothing is published beyond this machine,
//! and the UI says so.
//!
//! ## Two differences from Docker, both deliberate
//!
//! * **It reaches the workspace's loopback, not its network interface**
//! (`socat … TCP:127.0.0.1:<port>` inside the task). So a mapping can
//! reach a service that bound loopback precisely to be private — which is
//! why the agent's port, the browser view's range and (when the project
//! runs it) the gateway sidecar's [`GATEWAY_SIDECAR_PORT`] are refused.
//! * **No gate** ([`PortForward::bind_mapping`]): unlike the auth bridge, a
//! client need not speak first and an HTTP request is not checked for a
//! cross-site origin — Docker's publishing has neither, and a
//! server-speaks-first protocol (MySQL, SMTP) must work. What a mapping
//! exposes is what the user chose to expose, to this machine only.
//!
//! The tunnel's bounds still apply: 16 concurrent connections per mapping,
//! and a connection the workspace sends nothing on for 600 s is closed.
//!
//! ## Lifetime
//!
//! One set per project, opened after every connect (the start, and the
//! background re-attach after an app restart) and on a save of the mappings
//! while connected. Opens are serialised per project and re-read the stored
//! mappings under that lock, so the last save wins; an open keeps the
//! forwards it already has for an unchanged mapping (with their live
//! connections) and rebinds everything when the tunnel changed. The set is
//! closed with the project's tunnel — `tunnels::close`, `close_if_task` (the
//! task died) and `close_all` (app exit) call [`close`] / [`close_all`] — so
//! Stop, Remove, a task death and exit need nothing of their own.
//!
//! ## Idle stop
//!
//! An open listener costs the workspace nothing: no agent request is made
//! until a client connects. A connection is an attach, which counts as
//! activity while bytes flow — a client using a mapping keeps the workspace
//! from its idle stop; one connected but silent does not.
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use crate::auth_bridge::proc_net::PortFamily;
use crate::auth_bridge::tunnel::PortForward;
use crate::auth_bridge::{RESERVED_CONTAINER_PORTS, RESERVED_HOST_PORTS};
use crate::models::PortMapping;
use crate::storage::projects_store::ProjectsStore;
use super::ContainerTarget;
/// What became of one mapping: `error` is `None` when its forward is open.
#[derive(Debug, Clone, PartialEq)]
pub struct PortForwardResult {
pub mapping: PortMapping,
pub error: Option<String>,
}
/// The container port the gateway sidecar (Task 8, `gateway_sidecar`)
/// listens on, on the workspace's loopback.
pub const GATEWAY_SIDECAR_PORT: u16 = 4000;
/// A project's open forwards and the target (tunnel endpoint) they lead to.
struct OpenSet {
target_id: String,
forwards: Vec<PortForward>,
}
type Registry = HashMap<String, OpenSet>;
fn registry() -> &'static Mutex<Registry> {
static R: std::sync::OnceLock<Mutex<Registry>> = std::sync::OnceLock::new();
R.get_or_init(Default::default)
}
fn lock() -> std::sync::MutexGuard<'static, Registry> {
registry().lock().unwrap_or_else(|e| e.into_inner())
}
/// The per-project lock every open and close holds, so two opens (a save on
/// every blur, a save during a start's open) never interleave.
fn project_lock(project_id: &str) -> Arc<tokio::sync::Mutex<()>> {
static L: std::sync::OnceLock<Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>> =
std::sync::OnceLock::new();
L.get_or_init(Default::default)
.lock()
.unwrap_or_else(|e| e.into_inner())
.entry(project_id.to_string())
.or_default()
.clone()
}
/// Why one mapping can't be forwarded to an AWS workspace, if it can't.
/// `None` when it is fine.
fn mapping_problem(m: &PortMapping, gateway_sidecar: bool) -> Option<String> {
if !m.protocol.eq_ignore_ascii_case("tcp") {
return Some(format!(
"{} → {} ({}): only TCP ports can be forwarded to an AWS workspace",
m.host_port,
m.container_port,
m.protocol.to_ascii_uppercase()
));
}
if RESERVED_CONTAINER_PORTS.contains(&m.container_port) {
return Some(format!(
"Container port {} is reserved for the browser view ({}–{})",
m.container_port,
RESERVED_CONTAINER_PORTS.start(),
RESERVED_CONTAINER_PORTS.end()
));
}
if m.container_port == super::ssm::AGENT_PORT {
return Some(format!(
"Container port {} is the workspace agent's own port",
super::ssm::AGENT_PORT
));
}
if gateway_sidecar && m.container_port == GATEWAY_SIDECAR_PORT {
return Some(format!(
"Container port {GATEWAY_SIDECAR_PORT} is the gateway sidecar's, which holds the \
model provider's key"
));
}
if RESERVED_HOST_PORTS.contains(&m.host_port) {
return Some(format!(
"Host port {} is reserved for the browser view ({}–{})",
m.host_port,
RESERVED_HOST_PORTS.start(),
RESERVED_HOST_PORTS.end()
));
}
None
}
/// A row the editor saved before both its ports were typed (the editor
/// saves on blur): nothing to check or forward yet, and nothing to say.
fn incomplete(m: &PortMapping) -> bool {
m.host_port == 0 || m.container_port == 0
}
/// An AWS project's port mappings, checked in full: TCP only, no reserved
/// port (the browser view's two ranges, the agent's port, the gateway
/// sidecar's when it runs), and no host port twice. The first problem,
/// worded for the user.
pub fn validate_aws_port_mappings(mappings: &[PortMapping], gateway_sidecar: bool) -> Result<(), String> {
validate_changed(mappings, gateway_sidecar, |_| true)
}
/// [`validate_aws_port_mappings`] on a save, against what is stored:
/// `stored` is `None` when the project was not on AWS before (everything is
/// checked). Otherwise only rows the save adds or edits are — a stored row
/// this check would refuse (a UDP mapping kept from before) does not block
/// every unrelated save; it is skipped, with a warning, at each connect.
/// Turning the gateway sidecar on checks everything again.
pub fn validate_aws_port_mappings_update(
stored: Option<(&[PortMapping], bool)>,
mappings: &[PortMapping],
gateway_sidecar: bool,
) -> Result<(), String> {
match stored {
Some((old, old_gateway)) if old_gateway == gateway_sidecar => {
validate_changed(mappings, gateway_sidecar, |m| !old.contains(m))
}
_ => validate_aws_port_mappings(mappings, gateway_sidecar),
}
}
fn validate_changed(
mappings: &[PortMapping],
gateway_sidecar: bool,
changed: impl Fn(&PortMapping) -> bool,
) -> Result<(), String> {
let mut seen: HashMap<u16, bool> = HashMap::new();
for m in mappings.iter().filter(|m| !incomplete(m)) {
let is_changed = changed(m);
if is_changed {
if let Some(problem) = mapping_problem(m, gateway_sidecar) {
return Err(problem);
}
}
match seen.get(&m.host_port) {
Some(&other_changed) if is_changed || other_changed => {
return Err(format!("Host port {} is mapped twice", m.host_port))
}
Some(_) => {}
None => {
seen.insert(m.host_port, is_changed);
}
}
}
Ok(())
}
/// Whether `target` is still the project's connected workspace.
fn still_connected(project_id: &str, target: &ContainerTarget) -> bool {
match target {
ContainerTarget::Agent(ep) => {
super::tunnels::get_endpoint(project_id).is_some_and(|e| e.base_url == ep.base_url)
}
ContainerTarget::Docker { .. } => false,
}
}
/// Open `mappings` for `project_id` into `target` (its connected workspace).
/// See [`open_latest`]; the gateway sidecar is taken as off. Test helper.
#[cfg(test)]
pub async fn open_for(
project_id: &str,
target: &ContainerTarget,
mappings: &[PortMapping],
) -> Vec<PortForwardResult> {
let mappings = mappings.to_vec();
open_latest(project_id, target, move || Some((mappings, false))).await
}
/// Open the project's mappings into `target`, replacing the set it had.
/// `read` gives the mappings and whether the gateway sidecar runs, and is
/// called **under the project's lock** — so the last of several overlapping
/// opens applies what is stored last. `None` from it closes the set.
///
/// One result per mapping, in order: a refused mapping, a host port mapped
/// twice or a busy host port is an error for that mapping only, and the
/// others still open. A mapping already open into the same target is kept,
/// live connections and all. If the project's tunnel is no longer `target`
/// once they are bound (a Stop or a newer connect raced this), nothing is
/// kept.
pub async fn open_latest(
project_id: &str,
target: &ContainerTarget,
read: impl FnOnce() -> Option<(Vec<PortMapping>, bool)> + Send,
) -> Vec<PortForwardResult> {
let plock = project_lock(project_id);
let _held = plock.lock().await;
let Some((mappings, gateway_sidecar)) = read() else {
close_unlocked(project_id).await;
return Vec::new();
};
// What can be forwarded, in order; the first row wins a host port.
let mut results: Vec<PortForwardResult> = Vec::new();
let mut wanted: Vec<(usize, u16, u16)> = Vec::new();
let mut taken = HashSet::new();
for m in mappings.iter().filter(|m| !incomplete(m)) {
let error = if let Some(problem) = mapping_problem(m, gateway_sidecar) {
Some(problem)
} else if !taken.insert(m.host_port) {
Some(format!(
"{} → {}: host port {} is already used by another mapping",
m.host_port, m.container_port, m.host_port
))
} else {
wanted.push((results.len(), m.host_port, m.container_port));
None
};
results.push(PortForwardResult { mapping: m.clone(), error });
}
// Keep what is already open into this target and still wanted; free
// every other host port first, so a changed mapping can rebind it.
let previous = lock().remove(project_id);
let mut kept: HashMap<(u16, u16), PortForward> = HashMap::new();
let mut dropped = Vec::new();
if let Some(prev) = previous {
let same_target = prev.target_id == target.id();
for f in prev.forwards {
let key = (f.port, f.container_port);
if same_target && wanted.iter().any(|&(_, h, c)| (h, c) == key) {
kept.insert(key, f);
} else {
dropped.push(f);
}
}
}
shutdown_all(dropped).await;
let mut opened = Vec::new();
for (i, host_port, container_port) in wanted {
if let Some(f) = kept.remove(&(host_port, container_port)) {
opened.push(f);
continue;
}
match PortForward::bind_mapping(target.clone(), host_port, container_port, PortFamily::V4).await {
Ok(fwd) => {
if let Some(w) = &fwd.ipv6_warning {
log::warn!("Project {project_id}: port mapping {host_port}: {w}");
}
log::info!(
"Project {project_id}: forwarding localhost:{host_port} → workspace port {container_port}"
);
opened.push(fwd);
}
Err(e) => {
results[i].error = Some(if e.kind() == std::io::ErrorKind::AddrInUse {
format!(
"{host_port} → {container_port}: host port {host_port} is already in use on this machine"
)
} else {
format!("{host_port} → {container_port}: could not listen on host port {host_port}: {e}")
});
}
}
}
lock().insert(
project_id.to_string(),
OpenSet { target_id: target.id().to_string(), forwards: opened },
);
// Checked *after* the set is registered: a tunnel close that ran before
// this sees it gone here; one that runs after removes it itself.
if !still_connected(project_id, target) {
close_unlocked(project_id).await;
for r in results.iter_mut().filter(|r| r.error.is_none()) {
r.error = Some("the workspace disconnected".into());
}
}
results
}
async fn shutdown_all(set: Vec<PortForward>) {
futures_util::future::join_all(set.into_iter().map(|mut f| async move { f.shutdown().await }))
.await;
}
/// Close the project's forwards (its ports are free when this returns).
/// Waits for an open in progress.
pub async fn close(project_id: &str) {
let plock = project_lock(project_id);
let _held = plock.lock().await;
close_unlocked(project_id).await;
}
async fn close_unlocked(project_id: &str) {
let set = lock().remove(project_id);
if let Some(set) = set {
shutdown_all(set.forwards).await;
}
}
/// Close every project's forwards. The shutdown path's step (via
/// `tunnels::close_all`). An open in progress finds the tunnel gone and
/// closes its own set.
pub async fn close_all() {
let all: Vec<PortForward> = lock().drain().flat_map(|(_, v)| v.forwards).collect();
shutdown_all(all).await;
}
/// After a connect (or a save of the mappings while connected): open an
/// AWS project's mappings, **as stored when the open runs**, into its
/// connected workspace. The warning detail for the mappings that failed, if
/// any did; `None` as well for a local project (Docker publishes its own) or
/// a workspace that is not (or no longer) connected.
pub async fn open_for_project(store: &ProjectsStore, project_id: &str) -> Option<String> {
let project = store.get(project_id).filter(|p| p.is_aws())?;
let target = ContainerTarget::Agent(super::tunnels::get_endpoint(&project.id)?);
let results = open_latest(project_id, &target, || {
store.get(project_id).filter(|p| p.is_aws()).map(|p| {
let gateway = p.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
(p.port_mappings, gateway)
})
})
.await;
if !still_connected(project_id, &target) {
return None;
}
failure_detail(&results)
}
/// The host ports open for the project.
#[cfg(test)]
pub fn open_ports(project_id: &str) -> Vec<u16> {
let mut ports: Vec<u16> =
lock().get(project_id).map(|v| v.forwards.iter().map(|f| f.port).collect()).unwrap_or_default();
ports.sort_unstable();
ports
}
/// The `workspace-warning` detail for the mappings that failed, if any did.
pub fn failure_detail(results: &[PortForwardResult]) -> Option<String> {
let lines: Vec<&str> = results.iter().filter_map(|r| r.error.as_deref()).collect();
(!lines.is_empty()).then(|| lines.join("\n"))
}
#[cfg(test)]
#[path = "port_forwards/tests.rs"]
mod tests;
@@ -0,0 +1,396 @@
//! Port mappings against real loopback sockets and a **fake** agent that
//! answers `/v1/attach` by echoing stdin back as stdout — the socat it was
//! asked to run is recorded, never run.
use super::*;
use crate::storage::projects_store::ProjectsStore;
use crate::host::agent::AgentEndpoint;
use crate::host::tunnels;
use axum::extract::ws::{Message as WsMessage, WebSocket, WebSocketUpgrade};
use axum::extract::State;
use axum::response::Response;
use axum::routing::get;
use axum::Router;
use futures_util::SinkExt;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
#[derive(Default)]
struct Fake {
/// Every request the agent saw, of any kind.
requests: AtomicUsize,
/// The command of each attach.
cmds: std::sync::Mutex<Vec<Vec<String>>>,
}
async fn attach_ws(State(f): State<Arc<Fake>>, ws: WebSocketUpgrade) -> Response {
f.requests.fetch_add(1, Ordering::SeqCst);
ws.on_upgrade(move |socket| echo(f, socket))
}
async fn echo(f: Arc<Fake>, mut ws: WebSocket) {
let Some(Ok(WsMessage::Text(hello))) = ws.recv().await else { return };
let hello: serde_json::Value = serde_json::from_str(hello.as_str()).unwrap();
let cmd = hello["cmd"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap().to_string())
.collect();
f.cmds.lock().unwrap().push(cmd);
let _ = ws.send(WsMessage::Text(r#"{"type":"started","exec_id":"a1"}"#.into())).await;
while let Some(Ok(msg)) = ws.recv().await {
match msg {
WsMessage::Binary(b) => {
let mut out = vec![1u8];
out.extend_from_slice(&b);
if ws.send(WsMessage::Binary(out.into())).await.is_err() {
return;
}
}
WsMessage::Text(t) if t.as_str().contains("close_stdin") => break,
_ => {}
}
}
let _ = ws.send(WsMessage::Text(r#"{"type":"exit","code":0}"#.into())).await;
let _ = ws.close().await;
}
async fn counted(State(f): State<Arc<Fake>>) -> &'static str {
f.requests.fetch_add(1, Ordering::SeqCst);
"{}"
}
/// A fake agent, registered as `project_id`'s connected tunnel; its target.
async fn connected_agent(project_id: &str) -> (ContainerTarget, Arc<Fake>, Arc<AtomicUsize>) {
let fake = Arc::new(Fake::default());
let app = Router::new()
.route("/v1/attach", get(attach_ws))
.fallback(counted)
.with_state(fake.clone());
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let closes = Arc::new(AtomicUsize::new(0));
tunnels::register(project_id, tunnels::tests::fake_tunnel(port, closes.clone()));
(ContainerTarget::Agent(AgentEndpoint::loopback(port, "tok")), fake, closes)
}
fn pid(tag: &str) -> String {
format!("port-forwards-test-{tag}-{}", uuid::Uuid::new_v4())
}
/// A host port nothing listens on right now.
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
fn tcp(host_port: u16, container_port: u16) -> PortMapping {
PortMapping { host_port, container_port, protocol: "tcp".into() }
}
async fn round_trip(port: u16, msg: &[u8]) -> Vec<u8> {
let mut c = TcpStream::connect(("127.0.0.1", port)).await.expect("connect");
c.write_all(msg).await.unwrap();
let mut buf = vec![0u8; msg.len()];
tokio::time::timeout(std::time::Duration::from_secs(5), c.read_exact(&mut buf))
.await
.expect("echo in time")
.expect("echo");
buf
}
fn port_is_free(port: u16) -> bool {
std::net::TcpListener::bind(("127.0.0.1", port)).is_ok()
}
#[tokio::test]
async fn a_busy_host_port_fails_its_mapping_alone_and_the_other_forwards() {
let p = pid("busy");
let (target, fake, _) = connected_agent(&p).await;
let busy = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let busy_port = busy.local_addr().unwrap().port();
let free = free_port();
let results = open_for(&p, &target, &[tcp(busy_port, 3000), tcp(free, 5432)]).await;
assert_eq!(results.len(), 2);
let e = results[0].error.as_deref().expect("the busy port fails");
assert!(e.contains("already in use"), "{e}");
assert!(results[1].error.is_none(), "{:?}", results[1].error);
assert_eq!(failure_detail(&results).as_deref(), Some(e));
assert_eq!(open_ports(&p), vec![free]);
// Bytes go through the agent's attach, to the *container* port.
assert_eq!(round_trip(free, b"ping").await, b"ping");
assert_eq!(
fake.cmds.lock().unwrap().as_slice(),
[vec!["socat".to_string(), "-".into(), "TCP:127.0.0.1:5432".into()]]
);
// Stop / Remove / task death all close the tunnel: the mappings go too.
tunnels::close(&p).await;
assert!(open_ports(&p).is_empty());
assert!(port_is_free(free));
}
#[tokio::test]
async fn a_server_speaks_first_protocol_is_not_held_back() {
// Unlike the auth bridge, a mapping forwards a client that has said
// nothing yet (MySQL, SMTP): the attach opens on connect.
let p = pid("silent");
let (target, fake, _) = connected_agent(&p).await;
let port = free_port();
assert!(open_for(&p, &target, &[tcp(port, 3306)]).await[0].error.is_none());
let _client = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(3);
while fake.cmds.lock().unwrap().is_empty() {
assert!(tokio::time::Instant::now() < deadline, "no attach for a silent client");
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
tunnels::close(&p).await;
}
#[tokio::test]
async fn an_open_listener_with_no_connection_makes_no_agent_request() {
// So it can never count as activity and keep a workspace from its idle stop.
let p = pid("idle");
let (target, fake, _) = connected_agent(&p).await;
let port = free_port();
assert!(open_for(&p, &target, &[tcp(port, 8080)]).await[0].error.is_none());
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
assert_eq!(fake.requests.load(Ordering::SeqCst), 0);
tunnels::close(&p).await;
}
#[tokio::test]
async fn udp_reserved_and_duplicate_mappings_are_refused_per_mapping() {
let p = pid("refused");
let (target, _, _) = connected_agent(&p).await;
let ok = free_port();
let udp = PortMapping { host_port: free_port(), container_port: 53, protocol: "udp".into() };
let results = open_for(
&p,
&target,
&[
udp,
tcp(ok, 8080),
tcp(ok, 9090),
tcp(free_port(), *RESERVED_CONTAINER_PORTS.start()),
tcp(free_port(), crate::host::ssm::AGENT_PORT),
tcp(*RESERVED_HOST_PORTS.start(), 8080),
tcp(0, 0),
],
)
.await;
let errors: Vec<Option<&str>> = results.iter().map(|r| r.error.as_deref()).collect();
assert_eq!(errors.len(), 6, "an unfinished row is skipped: {errors:?}");
assert!(errors[0].unwrap().contains("only TCP"), "{errors:?}");
assert!(errors[1].is_none());
assert!(errors[2].unwrap().contains("another mapping"), "{errors:?}");
assert!(errors[3].unwrap().contains("browser view"), "{errors:?}");
assert!(errors[4].unwrap().contains("agent"), "{errors:?}");
assert!(errors[5].unwrap().contains("browser view"), "{errors:?}");
assert_eq!(open_ports(&p), vec![ok]);
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_reconnect_replaces_the_set_on_the_same_host_ports() {
let p = pid("replace");
let (target, _, _) = connected_agent(&p).await;
let port = free_port();
assert!(open_for(&p, &target, &[tcp(port, 8080)]).await[0].error.is_none());
// A background re-attach registers a new tunnel and opens again.
let (target2, fake2, _) = connected_agent(&p).await;
let again = open_for(&p, &target2, &[tcp(port, 8080)]).await;
assert!(again[0].error.is_none(), "{:?}", again[0].error);
assert_eq!(round_trip(port, b"hi").await, b"hi");
assert_eq!(fake2.cmds.lock().unwrap().len(), 1, "the new set reaches the new tunnel");
tunnels::close(&p).await;
assert!(port_is_free(port));
}
#[tokio::test]
async fn nothing_is_kept_when_the_workspace_is_no_longer_connected() {
let p = pid("gone");
let (target, _, _) = connected_agent(&p).await;
tunnels::close(&p).await; // a Stop raced the connect
let port = free_port();
let results = open_for(&p, &target, &[tcp(port, 8080)]).await;
assert!(results[0].error.as_deref().unwrap().contains("disconnected"));
assert!(open_ports(&p).is_empty());
assert!(port_is_free(port));
}
#[tokio::test]
async fn a_task_death_closes_the_mappings_but_a_stale_report_does_not() {
let p = pid("death");
let (target, _, _) = connected_agent(&p).await;
let port = free_port();
open_for(&p, &target, &[tcp(port, 8080)]).await;
tunnels::close_if_task(&p, "arn:some-older-task").await;
assert_eq!(open_ports(&p), vec![port]);
let ContainerTarget::Agent(ep) = &target else { unreachable!() };
let (_, _, _, arn) = tunnels::task_of(ep).unwrap();
tunnels::close_if_task(&p, &arn).await;
assert!(open_ports(&p).is_empty());
assert!(port_is_free(port));
}
#[test]
fn an_aws_project_saves_only_tcp_unreserved_unique_mappings() {
let v = |m: &[PortMapping]| validate_aws_port_mappings(m, false);
assert!(v(&[tcp(3000, 3000), tcp(8081, 8080), tcp(0, 0), tcp(5000, 0), tcp(4001, 4000)]).is_ok());
let udp = PortMapping { host_port: 5353, container_port: 5353, protocol: "udp".into() };
let e = v(&[udp]).unwrap_err();
assert!(e.contains("only TCP") && e.contains("UDP"), "{e}");
assert!(v(&[tcp(3000, 3000), tcp(3000, 4000)]).unwrap_err().contains("mapped twice"));
assert!(v(&[tcp(4000, 39321)]).is_err());
assert!(v(&[tcp(4000, 47900)]).is_err());
assert!(v(&[tcp(47820, 4000)]).is_err());
}
#[test]
fn the_gateway_sidecars_port_is_refused_while_the_project_runs_it() {
// A mapping reaches the workspace's *loopback*, where the sidecar holds
// the provider key behind 127.0.0.1:4000.
let m = [tcp(4001, GATEWAY_SIDECAR_PORT)];
assert!(validate_aws_port_mappings(&m, false).is_ok());
let e = validate_aws_port_mappings(&m, true).unwrap_err();
assert!(e.contains("gateway sidecar"), "{e}");
// Turning the sidecar on re-checks rows that were already stored.
assert!(validate_aws_port_mappings_update(Some((&m, false)), &m, true).is_err());
assert!(validate_aws_port_mappings_update(Some((&m, false)), &m, false).is_ok());
}
#[test]
fn a_stored_row_that_would_be_refused_does_not_block_other_saves() {
let udp = PortMapping { host_port: 5353, container_port: 5353, protocol: "udp".into() };
let stored = vec![udp.clone(), tcp(3000, 3000)];
// Unchanged, plus a new valid row: fine.
let saved = vec![udp.clone(), tcp(3000, 3000), tcp(8080, 8080)];
assert!(validate_aws_port_mappings_update(Some((&stored, false)), &saved, false).is_ok());
// A new UDP row, or an edit to one, is refused.
let edited = PortMapping { host_port: 5354, ..udp.clone() };
let e = validate_aws_port_mappings_update(Some((&stored, false)), &[edited, tcp(3000, 3000)], false);
assert!(e.unwrap_err().contains("only TCP"));
// A new row taking a stored row's host port is refused.
let dup = vec![udp.clone(), tcp(3000, 3000), tcp(3000, 4000)];
assert!(validate_aws_port_mappings_update(Some((&stored, false)), &dup, false)
.unwrap_err()
.contains("mapped twice"));
// Newly on AWS: everything is checked.
assert!(validate_aws_port_mappings_update(None, &stored, false).is_err());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn overlapping_opens_converge_on_the_last_stored_set_without_false_errors() {
// The editor saves on every blur; each save opens. Each open re-reads
// the stored mappings under the project's lock, so overlapping opens
// neither fight over a host port nor leave an older save's set behind.
let p = pid("concurrent");
let (target, _, _) = connected_agent(&p).await;
let (a, b, c) = (free_port(), free_port(), free_port());
let sets = [
vec![tcp(a, 8080), tcp(b, 8081)],
vec![tcp(b, 8081), tcp(c, 8082)],
vec![tcp(a, 8085), tcp(c, 8082)],
vec![tcp(b, 8090), tcp(c, 8082)],
];
let stored = Arc::new(std::sync::Mutex::new(sets[0].clone()));
let go = Arc::new(tokio::sync::Barrier::new(8));
let mut opens = Vec::new();
for i in 0..8 {
let (p2, t2, s2, go2) = (p.clone(), target.clone(), stored.clone(), go.clone());
let mine = sets[i % sets.len()].clone();
opens.push(tokio::spawn(async move {
go2.wait().await;
open_latest(&p2, &t2, move || {
let mut s = s2.lock().unwrap();
*s = mine; // this save lands, then its open reads the store
Some((s.clone(), false))
})
.await
}));
}
for o in opens {
let results = o.await.unwrap();
assert!(results.iter().all(|r| r.error.is_none()), "{results:?}");
}
let last = stored.lock().unwrap().clone();
let mut want: Vec<u16> = last.iter().map(|m| m.host_port).collect();
want.sort_unstable();
assert_eq!(open_ports(&p), want, "the registry holds the last stored set");
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_save_keeps_unchanged_forwards_and_their_live_connections() {
let p = pid("diff");
let (target, fake, _) = connected_agent(&p).await;
let (a, b) = (free_port(), free_port());
open_for(&p, &target, &[tcp(a, 8080)]).await;
let mut client = TcpStream::connect(("127.0.0.1", a)).await.unwrap();
client.write_all(b"one").await.unwrap();
let mut buf = [0u8; 3];
client.read_exact(&mut buf).await.unwrap();
let r = open_for(&p, &target, &[tcp(a, 8080), tcp(b, 8081)]).await;
assert!(r.iter().all(|r| r.error.is_none()), "{r:?}");
// The same connection still carries bytes: its forward was kept.
client.write_all(b"two").await.unwrap();
client.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"two");
assert_eq!(fake.cmds.lock().unwrap().len(), 1, "no new attach for the kept connection");
// A changed container port rebinds the same host port.
let r = open_for(&p, &target, &[tcp(a, 9090)]).await;
assert!(r[0].error.is_none(), "{:?}", r[0].error);
assert_eq!(round_trip(a, b"x").await, b"x");
assert_eq!(fake.cmds.lock().unwrap().last().unwrap()[2], "TCP:127.0.0.1:9090");
assert!(port_is_free(b));
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_stored_project_is_read_when_the_open_runs() {
let p = pid("store");
let dir = tempfile::tempdir().unwrap();
let mut project = crate::models::Project::new("x".into(), vec![]);
project.id = p.clone();
project.host_id = Some("h1".into());
project.aws_placement =
Some(crate::models::AwsPlacement { region: "us-west-2".into(), environment: "dev".into() });
project.aws = Some(Default::default());
let (port, busy) = (free_port(), std::net::TcpListener::bind("127.0.0.1:0").unwrap());
let busy_port = busy.local_addr().unwrap().port();
project.port_mappings = vec![tcp(port, 8080), tcp(busy_port, 8081)];
let store = ProjectsStore::over_file_for_test(dir.path().join("projects.json"), vec![project]);
assert_eq!(open_for_project(&store, &p).await, None, "not connected: nothing, no warning");
let (_target, _, _) = connected_agent(&p).await;
let warning = open_for_project(&store, &p).await.expect("the busy port is reported");
assert!(warning.contains(&busy_port.to_string()) && warning.contains("already in use"), "{warning}");
assert_eq!(open_ports(&p), vec![port]);
tunnels::close(&p).await;
}
#[tokio::test]
async fn the_auth_bridges_forward_still_waits_for_a_client_to_speak() {
// The generalised `PortForward::bind` keeps the bridge's gate: a silent
// client costs no attach (contrast `a_server_speaks_first_protocol_…`).
let p = pid("bridge");
let (target, fake, _) = connected_agent(&p).await;
let port = free_port();
let mut fwd = PortForward::bind(target, port, port, PortFamily::V4).await.unwrap();
assert_eq!((fwd.port, fwd.container_port), (port, port));
let _client = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
assert_eq!(fake.requests.load(Ordering::SeqCst), 0);
fwd.shutdown().await;
tunnels::close(&p).await;
}
+28 -3
View File
@@ -9,7 +9,13 @@ use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use tokio::sync::{mpsc, Mutex};
use super::{AttachedExec, ContainerTarget};
use super::{AttachedExec, ContainerTarget, ExecSpec};
/// A session's exec: as `claude` in `/workspace` (what `attach_exec` runs),
/// with `env` added. Values in `env` are never logged.
pub fn session_spec(cmd: Vec<String>, tty: bool, env: Vec<String>) -> ExecSpec {
ExecSpec::new(cmd).tty(tty).env(env)
}
pub struct ExecSession {
pub exec_id: String,
@@ -46,18 +52,22 @@ impl ExecSessionManager {
}
}
/// A terminal: a tty exec of `cmd` with `env` added to the container's
/// environment (an AWS copy-at-connect project's credentials,
/// `aws_host::terminal_env`; empty otherwise).
pub async fn create_session<F>(
&self,
target: &ContainerTarget,
session_id: &str,
cmd: Vec<String>,
env: Vec<String>,
on_output: F,
on_exit: Box<dyn FnOnce() + Send>,
) -> Result<(), String>
where
F: Fn(Vec<u8>) + Send + 'static,
{
self.create_session_with_tty(target, session_id, cmd, true, on_output, on_exit)
self.create_session_spec(target, session_id, session_spec(cmd, true, env), on_output, on_exit)
.await
}
@@ -70,6 +80,21 @@ impl ExecSessionManager {
on_output: F,
on_exit: Box<dyn FnOnce() + Send>,
) -> Result<(), String>
where
F: Fn(Vec<u8>) + Send + 'static,
{
self.create_session_spec(target, session_id, session_spec(cmd, tty, Vec::new()), on_output, on_exit)
.await
}
async fn create_session_spec<F>(
&self,
target: &ContainerTarget,
session_id: &str,
spec: ExecSpec,
on_output: F,
on_exit: Box<dyn FnOnce() + Send>,
) -> Result<(), String>
where
F: Fn(Vec<u8>) + Send + 'static,
{
@@ -77,7 +102,7 @@ impl ExecSessionManager {
exec_id,
mut output,
mut input,
} = target.attach_exec(cmd, tty).await?;
} = target.attach(spec).await?;
let (input_tx, mut input_rx) = mpsc::unbounded_channel::<Vec<u8>>();
let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
+94 -4
View File
@@ -18,6 +18,7 @@ use std::time::Duration;
use aws_config::SdkConfig;
use futures_util::future::BoxFuture;
use tokio::sync::watch;
use tokio_util::sync::CancellationToken;
use super::agent::AgentEndpoint;
use crate::aws::tasks::StopKind;
@@ -51,6 +52,11 @@ pub struct LiveTunnel {
/// Runtime handles for the store (`set_aws_runtime`).
pub access_point_id: Option<String>,
pub task_definition_arn: String,
/// EFS storage's `/workspace` access point.
pub workspace_access_point_id: Option<String>,
/// The start that opened this tunnel ran the task (rather than
/// attaching to one already running, or re-attaching after a restart).
pub created: bool,
}
/// Where an endpoint's task is: `(connection, region, cluster, task ARN)`.
@@ -68,6 +74,12 @@ struct Registry {
/// start's own tunnel) and [`cancel_reattach`]: a background re-attach
/// that began under an older generation must never register.
generation: HashMap<String, u64>,
/// The background clone/setup follower per project (`aws_host::status`),
/// with the id [`begin_follow`] gave it. Cancelled by a new follower,
/// [`cancel_follow`] (Stop, Remove), and whenever the tunnel is taken
/// out or every tunnel closed.
followers: HashMap<String, (u64, CancellationToken)>,
next_follower: u64,
}
fn registry() -> &'static Mutex<Registry> {
@@ -123,13 +135,15 @@ pub fn get_endpoint(project_id: &str) -> Option<AgentEndpoint> {
lock().live.get(project_id).map(|t| t.endpoint.clone())
}
/// `(task ARN, access point id, task definition ARN)` of the project's tunnel.
pub fn runtime(project_id: &str) -> Option<(String, Option<String>, String)> {
/// `(task ARN, access point id, task definition ARN, workspace access point
/// id)` of the project's tunnel.
pub fn runtime(project_id: &str) -> Option<(String, Option<String>, String, Option<String>)> {
lock().live.get(project_id).map(|t| {
(
t.task_arn.clone(),
t.access_point_id.clone(),
t.task_definition_arn.clone(),
t.workspace_access_point_id.clone(),
)
})
}
@@ -200,13 +214,67 @@ pub async fn close_if_task(project_id: &str, task_arn: &str) {
take(&mut r, project_id)
};
notify();
// The project's port mappings lead into that task: they go with it.
super::port_forwards::close(project_id).await;
if let Some(t) = taken {
t.forward.close().await;
}
}
/// Whether the project's registered tunnel came from a start that ran its
/// task (see [`LiveTunnel::created`]).
pub fn ran_task(project_id: &str) -> bool {
lock().live.get(project_id).is_some_and(|t| t.created)
}
/// A later start found the tunnel already up: it ran no task itself.
pub fn mark_attached(project_id: &str) {
if let Some(t) = lock().live.get_mut(project_id) {
t.created = false;
}
}
/// Register a background follower for `project_id`, cancelling the one
/// before it. Returns its id (for [`end_follow`]) and its token.
pub fn begin_follow(project_id: &str) -> (u64, CancellationToken) {
let mut r = lock();
r.next_follower += 1;
let id = r.next_follower;
let token = CancellationToken::new();
if let Some((_, old)) = r.followers.insert(project_id.to_string(), (id, token.clone())) {
old.cancel();
}
(id, token)
}
/// A follower finished: forget it, unless a newer one has replaced it.
pub fn end_follow(project_id: &str, id: u64) {
let mut r = lock();
if r.followers.get(project_id).is_some_and(|(i, _)| *i == id) {
r.followers.remove(project_id);
}
}
/// End the project's follower, if one runs (Stop, Remove).
pub fn cancel_follow(project_id: &str) {
cancel_follow_locked(&mut lock(), project_id);
}
fn cancel_follow_locked(r: &mut Registry, project_id: &str) {
if let Some((_, t)) = r.followers.remove(project_id) {
t.cancel();
}
}
/// Whether a follower runs for the project.
#[cfg(test)]
pub fn is_following(project_id: &str) -> bool {
lock().followers.contains_key(project_id)
}
/// Under the lock: invalidate a re-attach in flight and take the tunnel out.
fn take(r: &mut Registry, project_id: &str) -> Option<LiveTunnel> {
cancel_follow_locked(r, project_id);
r.pending.remove(project_id);
*r.generation.entry(project_id.to_string()).or_default() += 1;
let t = r.live.remove(project_id);
@@ -239,11 +307,12 @@ pub fn end_reattach(project_id: &str, generation: u64) {
notify();
}
/// Take the project's tunnel out and close it, and invalidate a background
/// re-attach in flight.
/// Take the project's tunnel out and close it (and its port mappings'
/// forwards), and invalidate a background re-attach in flight.
pub async fn close(project_id: &str) {
let taken = take(&mut lock(), project_id);
notify();
super::port_forwards::close(project_id).await;
if let Some(t) = taken {
t.forward.close().await;
}
@@ -255,12 +324,16 @@ pub async fn close_all() {
let mut r = lock();
r.pending.clear();
r.stopped.clear();
for (_, (_, t)) in r.followers.drain() {
t.cancel();
}
for g in r.generation.values_mut() {
*g += 1;
}
r.live.drain().map(|(_, t)| t).collect()
};
notify();
super::port_forwards::close_all().await;
futures_util::future::join_all(all.into_iter().map(|t| t.forward.close())).await;
}
@@ -334,6 +407,8 @@ pub(crate) mod tests {
endpoint: AgentEndpoint::loopback(port, "tok"),
access_point_id: Some("fsap-1".into()),
task_definition_arn: "arn:td:1".into(),
workspace_access_point_id: None,
created: true,
}
}
@@ -359,6 +434,21 @@ pub(crate) mod tests {
assert!(get_endpoint(&p).is_none() && task_of(&ep).is_none());
}
#[tokio::test]
async fn closing_the_tunnel_ends_its_follower_and_an_attach_clears_created() {
let closes = Arc::new(AtomicUsize::new(0));
let p = pid("follow");
register(&p, fake_tunnel(61009, closes.clone()));
assert!(ran_task(&p));
mark_attached(&p);
assert!(!ran_task(&p));
let (_, token) = begin_follow(&p);
assert!(is_following(&p) && !token.is_cancelled());
close(&p).await;
assert!(token.is_cancelled());
assert!(!is_following(&p));
}
#[tokio::test]
async fn re_registering_hands_back_the_old_tunnel_and_clears_a_stale_stop() {
let closes = Arc::new(AtomicUsize::new(0));
+3
View File
@@ -553,6 +553,8 @@ pub fn run() {
commands::project_commands::stop_project_container,
commands::project_commands::check_unsaved_work,
commands::project_commands::aws_task_log,
commands::project_commands::aws_workspace_status,
commands::project_commands::aws_environment_max_hours,
commands::project_commands::remove_aws_resources,
commands::project_commands::rebuild_project_container,
commands::project_commands::reconcile_project_statuses,
@@ -631,6 +633,7 @@ pub fn run() {
commands::host_commands::aws_connection_sso_login,
commands::host_commands::check_aws_permissions,
commands::host_commands::aws_policy_document,
commands::host_commands::aws_cost_estimate,
commands::environment_commands::detect_aws_environments,
commands::environment_commands::deploy_aws_environment,
commands::environment_commands::update_aws_environment,
+84
View File
@@ -486,6 +486,19 @@ pub enum StorageMode {
Ephemeral,
#[default]
Hybrid,
/// Hybrid plus `/workspace` on EFS (a second access point).
Efs,
}
/// How an AWS project's credentials and files reach the workspace.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum SecretsDelivery {
/// In the project's Secrets Manager secret.
#[default]
SecretsManager,
/// Kept out of the secret; the app copies them in after each connect.
CopyAtConnect,
}
/// One git repository cloned into the workspace. `branch: None` means the
@@ -536,8 +549,22 @@ pub struct AwsWorkspaceConfig {
pub access_point_id: Option<String>,
#[serde(default)]
pub task_definition_arn: Option<String>,
/// Store-owned like `access_point_id`: the EFS-mode `/workspace` access point.
#[serde(default)]
pub workspace_access_point_id: Option<String>,
#[serde(default)]
pub secrets_delivery: SecretsDelivery,
/// Run after the clone, as `claude` (AWS only).
#[serde(default)]
pub setup_script: Option<String>,
/// Run the LiteLLM gateway as a sidecar container.
#[serde(default)]
pub gateway_sidecar: bool,
}
/// Largest setup script, in bytes.
pub const MAX_SETUP_SCRIPT_BYTES: usize = 32_768;
impl Default for AwsWorkspaceConfig {
fn default() -> Self {
Self {
@@ -553,6 +580,10 @@ impl Default for AwsWorkspaceConfig {
task_arn: None,
access_point_id: None,
task_definition_arn: None,
workspace_access_point_id: None,
secrets_delivery: SecretsDelivery::default(),
setup_script: None,
gateway_sidecar: false,
}
}
}
@@ -706,6 +737,21 @@ pub fn validate_aws_workspace(cfg: &AwsWorkspaceConfig) -> Result<(), String> {
return Err(format!("Repository folder \"{}\" is used twice", r.folder));
}
}
if let Some(script) = &cfg.setup_script {
if script.len() > MAX_SETUP_SCRIPT_BYTES {
return Err(format!(
"The setup script is {} bytes; the limit is {}",
script.len(),
MAX_SETUP_SCRIPT_BYTES
));
}
if script.contains('\0') {
return Err("The setup script must not contain NUL characters".into());
}
}
if cfg.gateway_sidecar && (cfg.cpu < 1024 || cfg.memory_mib < 2048) {
return Err("The LLM gateway sidecar needs at least 1 vCPU (1024) and 2 GiB (2048 MiB)".into());
}
Ok(())
}
@@ -1342,6 +1388,44 @@ mod tests {
assert!(!off.wip_push_on_stop);
}
#[test]
fn phase6_fields_default_for_old_json_and_efs_round_trips() {
let c: AwsWorkspaceConfig = serde_json::from_str(r#"{"storage":"hybrid","cpu":512}"#).unwrap();
assert_eq!(c.secrets_delivery, SecretsDelivery::SecretsManager);
assert!(!c.gateway_sidecar && c.setup_script.is_none() && c.workspace_access_point_id.is_none());
let e: AwsWorkspaceConfig = serde_json::from_str(r#"{"storage":"efs","secrets_delivery":"copy_at_connect"}"#).unwrap();
assert_eq!(e.storage, StorageMode::Efs);
assert_eq!(e.secrets_delivery, SecretsDelivery::CopyAtConnect);
assert_eq!(serde_json::to_string(&StorageMode::Efs).unwrap(), "\"efs\"");
assert_eq!(serde_json::to_string(&SecretsDelivery::CopyAtConnect).unwrap(), "\"copy_at_connect\"");
let back: AwsWorkspaceConfig = serde_json::from_str(&serde_json::to_string(&e).unwrap()).unwrap();
assert_eq!(back, e);
}
#[test]
fn setup_script_limits() {
let mut c = AwsWorkspaceConfig::default();
c.setup_script = Some("a".repeat(MAX_SETUP_SCRIPT_BYTES));
assert!(validate_aws_workspace(&c).is_ok());
c.setup_script = Some("a".repeat(MAX_SETUP_SCRIPT_BYTES + 1));
assert!(validate_aws_workspace(&c).unwrap_err().contains("limit"));
c.setup_script = Some("echo\0hi".into());
assert!(validate_aws_workspace(&c).unwrap_err().contains("NUL"));
}
#[test]
fn gateway_sidecar_needs_one_vcpu_and_two_gib() {
let mut c = cfg(512, 2048);
c.gateway_sidecar = true;
assert!(validate_aws_workspace(&c).is_err());
c = cfg(1024, 2048);
c.gateway_sidecar = true;
assert!(validate_aws_workspace(&c).is_ok());
c = cfg(256, 2048);
c.gateway_sidecar = true;
assert!(validate_aws_workspace(&c).is_err());
}
#[test]
fn enums_serialize_snake_case_and_is_aws_follows_host_id() {
assert_eq!(serde_json::to_string(&Arch::Arm64).unwrap(), "\"arm64\"");
+47 -25
View File
@@ -93,6 +93,46 @@ fn record_corrupt_load(file_path: &Path, now: &chrono::DateTime<chrono::Utc>) {
}
}
/// The load's rewrite of stale transient statuses; `true` when it changed
/// any.
pub(crate) fn settle_stale_statuses(projects: &mut [Project]) -> bool {
// Reconcile stale transient statuses: on a cold app start no Docker
// operations can be in flight, so Starting/Stopping are always stale.
// Running/Error are left as-is and reconciled against Docker later
// via the reconcile_project_statuses command.
//
// An AWS project goes to **Error**, not Stopped: an app that quit
// mid-start (or mid-stop) may have left a billed task running, with
// or without its ARN recorded yet. Error is what a failed start that
// left a task already uses; `reconcile_aws_projects` then asks ECS
// (by ARN, else by the project's tags) and settles it Running or
// Stopped. Stopped would hide the live task.
let mut changed = false;
for p in projects.iter_mut() {
match p.status {
crate::models::ProjectStatus::Starting | crate::models::ProjectStatus::Stopping => {
let to = if p.is_aws() {
crate::models::ProjectStatus::Error
} else {
crate::models::ProjectStatus::Stopped
};
log::warn!(
"Reconciling stale '{}' status for project '{}' ({}) → {:?}",
serde_json::to_string(&p.status).unwrap_or_default().trim_matches('"'),
p.name,
p.id,
to
);
p.status = to;
p.updated_at = chrono::Utc::now().to_rfc3339();
changed = true;
}
_ => {}
}
}
changed
}
pub struct ProjectsStore {
projects: Mutex<Vec<Project>>,
file_path: PathBuf,
@@ -155,28 +195,8 @@ impl ProjectsStore {
(Vec::new(), false)
};
// Reconcile stale transient statuses: on a cold app start no Docker
// operations can be in flight, so Starting/Stopping are always stale.
// Running/Error are left as-is and reconciled against Docker later
// via the reconcile_project_statuses command.
let mut projects = projects;
let mut needs_save = needs_save;
for p in projects.iter_mut() {
match p.status {
crate::models::ProjectStatus::Starting | crate::models::ProjectStatus::Stopping => {
log::warn!(
"Reconciling stale '{}' status for project '{}' ({}) → Stopped",
serde_json::to_string(&p.status).unwrap_or_default().trim_matches('"'),
p.name,
p.id
);
p.status = crate::models::ProjectStatus::Stopped;
p.updated_at = chrono::Utc::now().to_rfc3339();
needs_save = true;
}
_ => {}
}
}
let needs_save = settle_stale_statuses(&mut projects) || needs_save;
let store = Self {
projects: Mutex::new(projects),
@@ -448,6 +468,7 @@ impl ProjectsStore {
task_arn: Option<String>,
access_point_id: Option<String>,
task_definition_arn: Option<String>,
workspace_access_point_id: Option<String>,
) -> Result<(), String> {
let mut projects = self.lock();
let p = projects
@@ -461,6 +482,7 @@ impl ProjectsStore {
aws.task_arn = task_arn;
aws.access_point_id = access_point_id;
aws.task_definition_arn = task_definition_arn;
aws.workspace_access_point_id = workspace_access_point_id;
p.updated_at = chrono::Utc::now().to_rfc3339();
self.save(&projects)?;
Ok(())
@@ -885,7 +907,7 @@ mod tests {
let store = store_over(&dir, vec![aws, local]);
store
.set_aws_runtime(&aid, Some("t".into()), Some("ap".into()), Some("td".into()))
.set_aws_runtime(&aid, Some("t".into()), Some("ap".into()), Some("td".into()), Some("wap".into()))
.unwrap();
let a = store.get(&aid).unwrap().aws.unwrap();
assert_eq!(
@@ -896,10 +918,10 @@ mod tests {
serde_json::from_str(&fs::read_to_string(dir.join("projects.json")).unwrap()).unwrap();
assert_eq!(on_disk[0].aws.as_ref().unwrap().task_arn.as_deref(), Some("t"));
store.set_aws_runtime(&aid, None, None, None).unwrap();
store.set_aws_runtime(&aid, None, None, None, None).unwrap();
assert!(store.get(&aid).unwrap().aws.unwrap().task_arn.is_none());
assert!(store.set_aws_runtime(&lid, None, None, None).is_err());
assert!(store.set_aws_runtime("nope", None, None, None).is_err());
assert!(store.set_aws_runtime(&lid, None, None, None, None).is_err());
assert!(store.set_aws_runtime("nope", None, None, None, None).is_err());
fs::remove_dir_all(&dir).ok();
}
}
@@ -304,6 +304,10 @@ async fn handle_open(
_ => build_terminal_cmd(&project, &state.settings_store),
};
// AWS copy-at-connect: the credentials go into this terminal's own
// environment (never the agent's). Empty for everything else.
let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await;
let session_id = uuid::Uuid::new_v4().to_string();
let project_name = project.name.clone();
@@ -332,6 +336,7 @@ async fn handle_open(
&container,
&session_id,
cmd,
env,
on_output,
on_exit,
)
+2
View File
@@ -23,6 +23,7 @@ import { useContainerProgress } from "./hooks/useContainerProgress";
import { useKeyboardShortcuts } from "./hooks/useKeyboardShortcuts";
import { useMarketplaceSyncToasts } from "./hooks/useMarketplace";
import { useProjectStatusEvents } from "./hooks/useProjectStatusEvents";
import { useWorkspaceWarnings } from "./hooks/useWorkspaceWarnings";
import MarketplaceView from "./components/marketplace/MarketplaceView";
import { useAppState, isHomeTab, tabKeyId, homeTabKey, MARKETPLACE_TAB_KEY } from "./store/appState";
import { reconcileAwsProjects, reconcileProjectStatuses } from "./lib/tauri-commands";
@@ -77,6 +78,7 @@ export default function App() {
useKeyboardShortcuts();
useMarketplaceSyncToasts();
useProjectStatusEvents();
useWorkspaceWarnings();
// Initialize on mount
useEffect(() => {
@@ -14,6 +14,14 @@ describe("ConfirmRemoveModal", () => {
expect(onConfirm.mock.calls).toEqual([[{ discardData: false }], [{ discardData: true }]]);
});
it("says both access points go when the workspace kept /workspace on EFS", () => {
render(
<ConfirmRemoveModal projectName="api" aws savedFiles workspaceFiles onConfirm={vi.fn()} onCancel={vi.fn()} />,
);
expect(screen.getByRole("checkbox", { name: /file-system access points/ })).toBeInTheDocument();
expect(screen.getByText(/\/workspace/)).toBeInTheDocument();
});
it("offers no access-point choice for an Ephemeral AWS workspace", () => {
const onConfirm = vi.fn();
render(<ConfirmRemoveModal projectName="api" aws onConfirm={onConfirm} onCancel={vi.fn()} />);
@@ -8,6 +8,8 @@ interface Props {
aws?: boolean;
/** AWS: the workspace may have an EFS access point (offers removing it). */
savedFiles?: boolean;
/** AWS: `/workspace` may be on EFS too (EFS storage, now or before). */
workspaceFiles?: boolean;
onConfirm: (opts: { discardData: boolean }) => void;
onCancel: () => void;
}
@@ -19,6 +21,7 @@ export default function ConfirmRemoveModal({
projectName,
aws = false,
savedFiles = false,
workspaceFiles = false,
onConfirm,
onCancel,
}: Props) {
@@ -61,11 +64,12 @@ export default function ConfirmRemoveModal({
onChange={(e) => setDiscardData(e.target.checked)}
/>
<span>
Also remove the workspace&rsquo;s file-system access point
Also remove the workspace&rsquo;s file-system access points
<span className="block text-xs text-[var(--text-secondary)]">
The saved files themselves (Claude login, history, SSH keys) stay on your own
EFS file system, readable only by your workspace role, until an administrator
offboards you.
The saved files themselves (Claude login, history, SSH keys
{workspaceFiles ? ", and the /workspace files" : ""}) stay on your own EFS file
system, readable only by your workspace role, until an administrator offboards
you.
</span>
</span>
</label>
@@ -7,6 +7,8 @@ interface Props {
portMappings: PortMapping[];
disabled: boolean;
disabledReason?: string;
/** AWS: only TCP is forwarded (UDP is refused on save). */
tcpOnly?: boolean;
onSave: (mappings: PortMapping[]) => Promise<unknown>;
}
@@ -14,6 +16,7 @@ export default function PortMappingsEditor({
portMappings: initial,
disabled,
disabledReason,
tcpOnly = false,
onSave,
}: Props) {
const [mappings, setMappings] = useState<PortMapping[]>(initial);
@@ -93,7 +96,7 @@ export default function PortMappingsEditor({
className={`w-[22%] ${selectClass}`}
>
<option value="tcp">TCP</option>
<option value="udp">UDP</option>
{(!tcpOnly || pm.protocol === "udp") && <option value="udp">UDP</option>}
</select>
<Button
variant="danger"
@@ -0,0 +1,116 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { act, render, screen } from "@testing-library/react";
import type { AwsWorkspaceConfig, HourlyEstimate } from "../../../lib/types";
import { defaultAwsWorkspace } from "../../../lib/awsWorkspace";
const awsCostEstimate =
vi.fn<(id: string, region: string, config: AwsWorkspaceConfig) => Promise<HourlyEstimate | null>>();
vi.mock("../../../lib/tauri-commands", () => ({
awsCostEstimate: (id: string, region: string, config: AwsWorkspaceConfig) =>
awsCostEstimate(id, region, config),
}));
import AwsCostEstimate, { ESTIMATE_DEBOUNCE_MS, formatHourly } from "./AwsCostEstimate";
const config = (over: Partial<AwsWorkspaceConfig> = {}): AwsWorkspaceConfig => ({
...defaultAwsWorkspace(),
storage: "ephemeral",
...over,
});
async function settle() {
await act(async () => {
vi.advanceTimersByTime(ESTIMATE_DEBOUNCE_MS);
});
await act(async () => {});
}
beforeEach(() => {
vi.useFakeTimers();
awsCostEstimate.mockReset();
});
afterEach(() => vi.useRealTimers());
describe("AwsCostEstimate", () => {
it("formats small and larger figures", () => {
expect(formatHourly(0.05826)).toBe("$0.058");
expect(formatHourly(0.2468)).toBe("$0.25");
expect(formatHourly(2.5)).toBe("$2.50");
});
it("shows the on-demand hourly figure for the connection, region and config", async () => {
awsCostEstimate.mockResolvedValue({
usd_per_hour: 0.05826,
note_spot: false,
efs_billed_separately: false,
});
const c = config();
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={c} />);
expect(screen.getByText("Estimating…")).toBeInTheDocument();
await settle();
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "us-west-2", c);
expect(screen.getByText("≈ $0.058/hour")).toBeInTheDocument();
expect(screen.queryByText(/Spot/)).toBeNull();
expect(screen.queryByText(/EFS/)).toBeNull();
});
it("notes Spot, EFS and the gateway sidecar", async () => {
awsCostEstimate.mockResolvedValue({
usd_per_hour: 0.05,
note_spot: true,
efs_billed_separately: true,
});
render(
<AwsCostEstimate
connectionId="c1"
region="us-west-2"
config={config({ spot: true, storage: "hybrid", gateway_sidecar: true })}
/>,
);
await settle();
expect(
screen.getByText("≈ $0.050/hour (on-demand; Spot is usually up to 70% cheaper)"),
).toBeInTheDocument();
expect(screen.getByText(/EFS storage is billed separately/)).toBeInTheDocument();
expect(screen.getByText(/gateway sidecar shares the task's CPU and memory/)).toBeInTheDocument();
});
it("says the estimate is unavailable for null or a failed call, never an error", async () => {
awsCostEstimate.mockResolvedValueOnce(null);
const { unmount } = render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
await settle();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
unmount();
awsCostEstimate.mockRejectedValueOnce("boom");
render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
await settle();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
});
it("without a connection or region it does not ask", async () => {
render(<AwsCostEstimate connectionId={null} region="us-west-2" config={config()} />);
await settle();
expect(awsCostEstimate).not.toHaveBeenCalled();
expect(screen.getByText("Estimate unavailable")).toBeInTheDocument();
});
it("asks once per settled change of a cost field, not for unrelated edits", async () => {
awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1, note_spot: false, efs_billed_separately: false });
const { rerender } = render(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config()} />);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
// An unrelated field: no new call.
rerender(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ setup_script: "x" })} />);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(1);
// Two quick CPU changes: one call, with the last.
rerender(<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ cpu: 2048 })} />);
rerender(
<AwsCostEstimate connectionId="c1" region="us-west-2" config={config({ cpu: 4096, memory_mib: 8192 })} />,
);
await settle();
expect(awsCostEstimate).toHaveBeenCalledTimes(2);
expect(awsCostEstimate.mock.calls[1][2].cpu).toBe(4096);
});
});
@@ -0,0 +1,78 @@
import { useEffect, useState } from "react";
import type { AwsWorkspaceConfig, HourlyEstimate } from "../../../lib/types";
import { awsCostEstimate } from "../../../lib/tauri-commands";
/** Edits settle this long before the estimate is asked for again. */
export const ESTIMATE_DEBOUNCE_MS = 400;
/** "$0.058" below ten cents (two decimals would round most tasks to $0.05/$0.06), "$0.25" above. */
export function formatHourly(usd: number): string {
return `$${usd.toFixed(usd < 0.1 ? 3 : 2)}`;
}
interface Props {
/** The project's connection; null when it was removed. */
connectionId: string | null;
region: string | null | undefined;
config: AwsWorkspaceConfig;
}
type State = { kind: "loading" } | { kind: "done"; estimate: HourlyEstimate | null };
/**
* The estimated on-demand cost per hour of an AWS project's Fargate task (Q5), from the Price
* List API through `aws_cost_estimate` (cached a day per region). Best effort: a denied call, no
* network or no connection reads "Estimate unavailable" — never an error toast, and nothing waits
* on it. Re-asked only when a field that changes the price changes, after edits settle.
*/
export default function AwsCostEstimate({ connectionId, region, config }: Props) {
const [state, setState] = useState<State>({ kind: "loading" });
const { cpu, memory_mib, arch, ephemeral_gib, spot, storage } = config;
// Only what the backend prices or notes; the rest of the config is sent but changes nothing.
const key = JSON.stringify([connectionId, region, cpu, memory_mib, arch, ephemeral_gib, spot, storage]);
useEffect(() => {
if (!connectionId || !region) {
setState({ kind: "done", estimate: null });
return;
}
let cancelled = false;
setState({ kind: "loading" });
const timer = setTimeout(() => {
awsCostEstimate(connectionId, region, config)
.catch(() => null)
.then((estimate) => {
if (!cancelled) setState({ kind: "done", estimate });
});
}, ESTIMATE_DEBOUNCE_MS);
return () => {
cancelled = true;
clearTimeout(timer);
};
// `key` covers every field of `config` the estimate depends on.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key]);
const estimate = state.kind === "done" ? state.estimate : null;
return (
<div className="space-y-0.5 text-xs">
<p className="text-[var(--text-primary)]">
{state.kind === "loading"
? "Estimating…"
: estimate
? `≈ ${formatHourly(estimate.usd_per_hour)}/hour${
estimate.note_spot ? " (on-demand; Spot is usually up to 70% cheaper)" : ""
}`
: "Estimate unavailable"}
</p>
{estimate && (
<p className="text-[var(--text-secondary)]">
While the task runs; nothing while it is stopped.
{estimate.efs_billed_separately && " EFS storage is billed separately."}
{config.gateway_sidecar &&
" The gateway sidecar shares the task's CPU and memory, so it adds nothing."}
</p>
)}
</div>
);
}
@@ -0,0 +1,151 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { act, render, screen } from "@testing-library/react";
import type { Project, WorkspaceStatus } from "../../../lib/types";
const awsWorkspaceStatus = vi.fn<(id: string) => Promise<WorkspaceStatus | null>>();
vi.mock("../../../lib/tauri-commands", () => ({
awsWorkspaceStatus: (id: string) => awsWorkspaceStatus(id),
}));
import AwsWorkspaceStatusCard, { STATUS_POLL_MS } from "./AwsWorkspaceStatusCard";
const project = (over: Partial<Project> = {}): Project =>
({
id: "p1",
name: "demo",
status: "running",
host_id: "conn-1",
container_id: "http://127.0.0.1:1",
aws: { repos: [{}, {}, {}] },
...over,
}) as unknown as Project;
const CLONING: WorkspaceStatus = {
clone: {
done: false,
repos: [
{ folder: "api", state: "cloned" },
{ folder: "web", state: "cloning" },
],
},
setup: { state: "running", code: null, tail: [] },
};
const FAILED: WorkspaceStatus = {
clone: {
done: true,
repos: [
{ folder: "api", state: "cloned" },
{ folder: "web", state: "failed" },
],
},
setup: { state: "failed", code: 2, tail: ["Setting up jq", "E: Unable to locate package nope"] },
};
async function flush() {
await act(async () => {
await Promise.resolve();
await Promise.resolve();
});
}
describe("AwsWorkspaceStatusCard", () => {
beforeEach(() => {
vi.useFakeTimers();
awsWorkspaceStatus.mockReset();
});
afterEach(() => {
vi.useRealTimers();
});
it("shows a workspace still cloning, then a failed clone and setup", async () => {
awsWorkspaceStatus.mockResolvedValueOnce(CLONING).mockResolvedValue(FAILED);
render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(screen.getByText("Still cloning repos (1 of 3 done)")).toBeTruthy();
expect(screen.getByText("cloning…")).toBeTruthy();
expect(screen.getByText("Setup script waits for the clone")).toBeTruthy();
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2);
expect(screen.getByText("Clone failed for web")).toBeTruthy();
expect(screen.getByText("Setup script failed (exit 2)")).toBeTruthy();
expect(screen.getByText(/E: Unable to locate package nope/)).toBeTruthy();
});
it("renders nothing and does not poll for a stopped project", async () => {
awsWorkspaceStatus.mockResolvedValue(FAILED);
const { container } = render(<AwsWorkspaceStatusCard project={project({ status: "stopped" })} />);
await flush();
expect(awsWorkspaceStatus).not.toHaveBeenCalled();
expect(container.innerHTML).toBe("");
});
it("renders nothing when there is nothing to say", async () => {
awsWorkspaceStatus.mockResolvedValue({
clone: { done: true, repos: [] },
setup: { state: "none", code: null, tail: [] },
});
const { container } = render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(container.innerHTML).toBe("");
});
it("pauses while the window is hidden", async () => {
awsWorkspaceStatus.mockResolvedValue(CLONING);
render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
const state = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS * 3);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
state.mockReturnValue("visible");
await act(async () => {
document.dispatchEvent(new Event("visibilitychange"));
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2);
state.mockRestore();
});
it("never runs two polls at once when the window is hidden and shown mid-read", async () => {
let resolve: (s: WorkspaceStatus) => void = () => {};
awsWorkspaceStatus.mockImplementation(() => new Promise((r) => (resolve = r)));
render(<AwsWorkspaceStatusCard project={project()} />);
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
// Hidden and shown again while the first read is still out.
const state = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
await act(async () => {
document.dispatchEvent(new Event("visibilitychange"));
});
state.mockReturnValue("visible");
await act(async () => {
document.dispatchEvent(new Event("visibilitychange"));
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1);
// The read ends: one chain goes on, one read per interval.
await act(async () => resolve(CLONING));
await flush();
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2);
await act(async () => resolve(CLONING));
await flush();
await act(async () => {
vi.advanceTimersByTime(STATUS_POLL_MS);
});
await flush();
expect(awsWorkspaceStatus).toHaveBeenCalledTimes(3);
state.mockRestore();
});
});
@@ -0,0 +1,136 @@
import { useEffect, useState } from "react";
import type { Project, WorkspaceStatus } from "../../../lib/types";
import { awsWorkspaceStatus } from "../../../lib/tauri-commands";
/** How often the card re-reads the status while it is on screen. */
export const STATUS_POLL_MS = 5000;
const REPO_STATE: Record<string, string> = {
cloning: "cloning…",
cloned: "cloned",
failed: "failed",
exists: "already there",
};
/**
* An AWS workspace's background clone and setup script (phase 6, Q4/R5):
* the start ends at "Connecting…" and this card is where their progress
* shows, re-read every 5 s while the project is Running and the window is
* visible. Each read is a background exec, so the
* card never keeps an idle workspace up. Renders nothing for a local project,
* a workspace that cannot be asked, or one with no repos and no setup script.
*/
export default function AwsWorkspaceStatusCard({ project }: { project: Project }) {
const running = project.status === "running";
const [status, setStatus] = useState<WorkspaceStatus | null>(null);
useEffect(() => {
setStatus(null);
if (!running) return;
let cancelled = false;
let timer: ReturnType<typeof setTimeout> | null = null;
// A read in progress: hiding and showing the window during it must not
// start a second poll chain beside this one.
let inFlight = false;
const visible = () => document.visibilityState !== "hidden";
const poll = async () => {
timer = null;
if (!visible()) return; // resumed by visibilitychange
inFlight = true;
try {
const s = await awsWorkspaceStatus(project.id);
if (!cancelled) setStatus(s);
} catch {
// A failed read keeps the last status; the next poll tries again.
} finally {
inFlight = false;
}
if (!cancelled) timer = setTimeout(poll, STATUS_POLL_MS);
};
const onVisibility = () => {
if (visible() && timer === null && !inFlight && !cancelled) void poll();
};
void poll();
document.addEventListener("visibilitychange", onVisibility);
return () => {
cancelled = true;
if (timer !== null) clearTimeout(timer);
document.removeEventListener("visibilitychange", onVisibility);
};
}, [project.id, running, project.container_id]);
if (!running || !status) return null;
const { clone, setup } = status;
if (clone.done && clone.repos.length === 0 && setup.state === "none") return null;
const failedRepos = clone.repos.filter((r) => r.state === "failed");
const finished = clone.repos.filter((r) => r.state !== "cloning").length;
const total = Math.max(project.aws?.repos.length ?? 0, clone.repos.length);
const cloneLine = !clone.done
? total > 0
? `Still cloning repos (${finished} of ${total} done)`
: "Still cloning repos"
: failedRepos.length > 0
? `Clone failed for ${failedRepos.map((r) => r.folder).join(", ")}`
: clone.repos.length > 0
? "Repos cloned"
: null;
const setupLine =
setup.state === "running"
? clone.done
? "Setup script running…"
: "Setup script waits for the clone"
: setup.state === "ok"
? "Setup script finished"
: setup.state === "failed"
? `Setup script failed${setup.code != null ? ` (exit ${setup.code})` : ""}`
: null;
const problem = failedRepos.length > 0 || setup.state === "failed";
return (
<section
aria-label="Workspace start-up"
className="p-3 border rounded-[var(--radius-panel)] bg-[var(--bg-secondary)] text-xs space-y-2"
style={{ borderColor: problem ? "var(--warning)" : "var(--border-color)" }}
>
<h2 className="text-[11px] font-semibold uppercase tracking-wide text-[var(--text-secondary)]">
Workspace start-up
</h2>
{cloneLine && (
<p className="text-[var(--text-primary)]">
{failedRepos.length > 0 && (
<span aria-hidden="true" className="mr-1 text-[var(--warning)]">
▲
</span>
)}
{cloneLine}
</p>
)}
{clone.repos.length > 0 && (!clone.done || failedRepos.length > 0) && (
<ul className="space-y-0.5 text-[var(--text-secondary)]">
{clone.repos.map((r) => (
<li key={r.folder}>
<span className="font-mono text-[var(--text-primary)]">{r.folder}</span>{" "}
{REPO_STATE[r.state] ?? r.state}
</li>
))}
</ul>
)}
{setupLine && (
<p className="text-[var(--text-primary)]">
{setup.state === "failed" && (
<span aria-hidden="true" className="mr-1 text-[var(--warning)]">
▲
</span>
)}
{setupLine}
</p>
)}
{(setup.state === "running" || setup.state === "failed") && setup.tail.length > 0 && (
<pre className="max-h-40 overflow-auto whitespace-pre-wrap break-all p-2 rounded-[var(--radius-control)] bg-[var(--bg-primary)] text-[var(--text-secondary)] font-mono text-[11px]">
{(setup.state === "running" ? setup.tail.slice(-3) : setup.tail).join("\n")}
</pre>
)}
</section>
);
}
@@ -12,6 +12,9 @@ import PermissionModeControl, {
} from "../PermissionModeControl";
import CapabilityTiles from "./CapabilityTiles";
import ContainerMigrationBanner from "./ContainerMigrationBanner";
import AwsWorkspaceStatusCard from "./AwsWorkspaceStatusCard";
import AwsCostEstimate from "./AwsCostEstimate";
import { isAwsProject } from "../../../lib/awsWorkspace";
import type { ContainerMigration } from "../../../hooks/useContainerMigration";
import SaveIndicator from "../../ui/SaveIndicator";
import Button from "../../ui/Button";
@@ -132,6 +135,27 @@ export default function OverviewTab({
</div>
</section>
{/* AWS: the background clone and setup script (still cloning, setup
failed, …). Polls only while Running and visible. */}
{isAwsProject(project) && <AwsWorkspaceStatusCard project={project} />}
{/* AWS, while running: what the task costs per hour (Q5). */}
{isAwsProject(project) && project.status === "running" && project.aws && (
<section
aria-label="Estimated cost"
className="p-3 border rounded-[var(--radius-panel)] bg-[var(--bg-secondary)] border-[var(--border-color)] space-y-1"
>
<h2 className="text-[11px] font-semibold uppercase tracking-wide text-[var(--text-secondary)]">
Estimated cost
</h2>
<AwsCostEstimate
connectionId={project.host_id}
region={project.aws_placement?.region}
config={project.aws}
/>
</section>
)}
{/* A container missing socat and bwrap is a capability statement, so the
out-of-date warning sits directly above the capability inventory. */}
<ContainerMigrationBanner
@@ -11,7 +11,7 @@ import Button from "../../ui/Button";
import OverflowMenu from "../../ui/OverflowMenu";
import ConfirmRemoveModal, { ConfirmForgetAwsModal } from "../ConfirmRemoveModal";
import StopGuardModal from "../StopGuardModal";
import { hasSavedFiles, isAwsProject } from "../../../lib/awsWorkspace";
import { hasSavedFiles, hasWorkspaceFiles, isAwsProject } from "../../../lib/awsWorkspace";
import { AWS_CONNECTION_MISSING, type RemoveProjectOptions } from "../../../lib/types";
import ConfirmResetModal from "../ConfirmResetModal";
import MigrateContainerModal from "../MigrateContainerModal";
@@ -349,6 +349,7 @@ export default function ProjectHome({ projectId, active }: Props) {
projectName={project.name}
aws={aws}
savedFiles={hasSavedFiles(project)}
workspaceFiles={hasWorkspaceFiles(project)}
onCancel={() => setConfirmRemove(false)}
onConfirm={({ discardData }) => {
setConfirmRemove(false);
@@ -7,7 +7,7 @@ import Field, { ConfigGroup, inputClass } from "../../../ui/Field";
import CaCertPathInput from "../../../settings/CaCertPathInput";
import EnvVarsEditor from "../../EnvVarsEditor";
import PortMappingsEditor from "../../PortMappingsEditor";
import { NOT_ON_AWS, isAwsProject } from "../../../../lib/awsWorkspace";
import { AWS_PORTS_LOCALHOST_ONLY, isAwsProject } from "../../../../lib/awsWorkspace";
interface Props {
project: Project;
@@ -163,21 +163,24 @@ export default function AccessSection({
Port mappings
</span>
{isAwsProject(project) ? (
<p className="mt-0.5 text-xs text-[var(--text-disabled)] leading-snug">{NOT_ON_AWS}</p>
<p className="mt-0.5 mb-2 text-xs text-[var(--text-secondary)] leading-snug">
Forward workspace ports to this machine while it is connected. TCP only. A
connection carrying traffic counts as activity, so it keeps the workspace from its
idle stop. {AWS_PORTS_LOCALHOST_ONLY}.
</p>
) : (
<>
<p className="mt-0.5 mb-2 text-xs text-[var(--text-secondary)] leading-snug">
Expose container ports on the host so you can reach dev servers running inside
the sandbox.
</p>
<PortMappingsEditor
portMappings={project.port_mappings ?? []}
disabled={disabled}
disabledReason={disabledReason}
onSave={(mappings) => save({ port_mappings: mappings })}
/>
</>
<p className="mt-0.5 mb-2 text-xs text-[var(--text-secondary)] leading-snug">
Expose container ports on the host so you can reach dev servers running inside
the sandbox.
</p>
)}
<PortMappingsEditor
portMappings={project.port_mappings ?? []}
disabled={disabled}
disabledReason={disabledReason}
tcpOnly={isAwsProject(project)}
onSave={(mappings) => save({ port_mappings: mappings })}
/>
</div>
</ConfigGroup>
);
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor, within } from "@testing-library/react";
import { act, cleanup, render, screen, fireEvent, waitFor, within } from "@testing-library/react";
import RuntimeSection from "./RuntimeSection";
import AccessSection from "./AccessSection";
import WorkspaceSection from "./WorkspaceSection";
@@ -8,22 +8,30 @@ import HostSection from "./HostSection";
import { awsProject, localProject } from "../../../../test/awsFixtures";
import type { AppSettings, AuthBridgeStatus, Project } from "../../../../lib/types";
import { useAppState } from "../../../../store/appState";
import { clearEnvironmentMaxHoursCache } from "../../../../lib/environmentMaxHours";
const OFF_BRIDGE: AuthBridgeStatus = { enabled: false, active_ports: [], conflicts: [] };
const removeAwsResources = vi.fn();
const awsCostEstimate = vi.fn(async (..._args: unknown[]) => null as unknown);
const awsEnvironmentMaxHours = vi.fn(async (_id: string) => 0 as number | null);
vi.mock("../../../../lib/tauri-commands", () => ({
getAuthBridgeStatus: vi.fn(async () => OFF_BRIDGE),
setAuthBridgeEnabled: vi.fn(),
inspectCaCertPath: vi.fn(async () => null),
removeAwsResources: (id: string, discard: boolean) => removeAwsResources(id, discard),
awsCostEstimate: (...args: unknown[]) => awsCostEstimate(...args),
awsEnvironmentMaxHours: (id: string) => awsEnvironmentMaxHours(id),
}));
vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn(async () => () => {}) }));
vi.mock("@tauri-apps/plugin-dialog", () => ({ open: vi.fn(async () => null) }));
const save = vi.fn().mockResolvedValue(true);
beforeEach(() => vi.clearAllMocks());
beforeEach(() => {
vi.clearAllMocks();
clearEnvironmentMaxHoursCache();
});
function toggle(name: string) {
return screen.getByRole("switch", { name });
@@ -40,6 +48,14 @@ describe("AWS project config", () => {
expect(screen.getByText(/Off on AWS for now/)).toBeInTheDocument();
});
it("still offers UDP mappings for a local project", () => {
const project = localProject();
project.port_mappings = [{ host_port: 5353, container_port: 5353, protocol: "udp" }];
render(<AccessSection project={project} save={save} disabled={false} />);
expect(within(screen.getByLabelText("Protocol 1")).getByText("UDP")).toBeInTheDocument();
expect(screen.queryByText(/Reachable only from this machine/)).toBeNull();
});
it("leaves those options alone for a local project", () => {
render(<RuntimeSection project={localProject()} save={save} disabled={false} />);
expect(toggle("VPN support")).not.toBeDisabled();
@@ -47,10 +63,16 @@ describe("AWS project config", () => {
expect(screen.queryByText("Not available on AWS (Fargate)")).toBeNull();
});
it("replaces port mappings and folders on AWS", () => {
render(<AccessSection project={awsProject()} save={save} disabled={false} />);
expect(screen.getByText("Not available on AWS (Fargate)")).toBeInTheDocument();
it("offers TCP port mappings on AWS, localhost only, and replaces folders", () => {
const project = awsProject();
project.port_mappings = [{ host_port: 3000, container_port: 3000, protocol: "tcp" }];
render(<AccessSection project={project} save={save} disabled={false} />);
expect(screen.getByText(/Reachable only from this machine \(localhost\)/)).toBeInTheDocument();
expect(screen.queryByText(/Expose container ports/)).toBeNull();
const protocol = screen.getByLabelText("Protocol 1");
expect(within(protocol).queryByText("UDP")).toBeNull();
expect(within(protocol).getByText("TCP")).toBeInTheDocument();
cleanup();
render(<WorkspaceSection project={awsProject()} save={save} disabled={false} />);
expect(screen.queryByText("Folders")).toBeNull();
@@ -99,10 +121,154 @@ describe("AWS project config", () => {
});
});
describe("LLM gateway sidecar (ModelSection)", () => {
const SIDECAR = "Run Triple-C's LLM gateway in the workspace";
const withModels = (models: { name: string; model_id: string }[]) =>
useAppState.setState({
appSettings: { gateway: { enabled: false, port: 4000, models } } as unknown as AppSettings,
});
const oai = (over: Partial<Project> = {}) =>
awsProject({
backend: "open_ai_compatible",
openai_compatible_config: {
base_url: "http://host.docker.internal:4000",
api_key: null,
model_id: "gpt",
haiku_model_id: null,
} as never,
...over,
});
const renderModel = (p: Project) =>
render(<ModelSection project={p} save={save} disabled={false} />);
it("is offered only for an AWS project on the OpenAI-compatible backend", () => {
withModels([{ name: "gpt", model_id: "gpt-5" }]);
renderModel(awsProject({ backend: "ollama" }));
expect(screen.queryByRole("switch", { name: SIDECAR })).toBeNull();
cleanup();
render(
<ModelSection
project={localProjectOai()}
save={save}
disabled={false}
/>,
);
expect(screen.queryByRole("switch", { name: SIDECAR })).toBeNull();
useAppState.setState({ appSettings: null });
});
it("turns on when Settings has a gateway model, and says why not otherwise", async () => {
withModels([{ name: " ", model_id: "gpt-5" }]);
renderModel(oai());
expect(toggle(SIDECAR)).toBeDisabled();
expect(screen.getByRole("alert")).toHaveTextContent("Settings → Model Gateway");
cleanup();
withModels([{ name: "gpt", model_id: "gpt-5" }]);
renderModel(oai());
expect(toggle(SIDECAR)).not.toBeDisabled();
fireEvent.click(toggle(SIDECAR));
await waitFor(() => expect(save).toHaveBeenCalled());
expect(save.mock.calls[0][0].aws.gateway_sidecar).toBe(true);
useAppState.setState({ appSettings: null });
});
it("needs 1 vCPU and 2 GiB", () => {
withModels([{ name: "gpt", model_id: "gpt-5" }]);
const p = oai();
p.aws = { ...p.aws!, cpu: 512, memory_mib: 1024 };
renderModel(p);
expect(toggle(SIDECAR)).toBeDisabled();
expect(screen.getByRole("alert")).toHaveTextContent("1 vCPU and 2 GiB");
useAppState.setState({ appSettings: null });
});
it("when on, replaces the local-endpoint warning and can always be turned off", async () => {
withModels([]);
const p = oai();
p.aws = { ...p.aws!, gateway_sidecar: true };
renderModel(p);
expect(screen.queryByText(/host.docker.internal is this machine/)).toBeNull();
expect(screen.getByRole("note", { name: "LLM gateway sidecar" })).toHaveTextContent(
"http://127.0.0.1:4000",
);
expect(toggle(SIDECAR)).toHaveAttribute("aria-checked", "true");
expect(toggle(SIDECAR)).not.toBeDisabled();
fireEvent.click(toggle(SIDECAR));
await waitFor(() => expect(save).toHaveBeenCalled());
expect(save.mock.calls[0][0].aws.gateway_sidecar).toBe(false);
useAppState.setState({ appSettings: null });
});
it("is turned off with the backend it needs", async () => {
withModels([{ name: "gpt", model_id: "gpt-5" }]);
const p = oai();
p.aws = { ...p.aws!, gateway_sidecar: true };
renderModel(p);
fireEvent.change(screen.getByLabelText("Backend"), { target: { value: "anthropic" } });
await waitFor(() => expect(save).toHaveBeenCalled());
expect(save.mock.calls[0][0]).toMatchObject({ backend: "anthropic", aws: { gateway_sidecar: false } });
useAppState.setState({ appSettings: null });
});
});
function localProjectOai(): Project {
return localProject({
backend: "open_ai_compatible",
openai_compatible_config: {
base_url: "http://host.docker.internal:4000",
api_key: null,
model_id: null,
haiku_model_id: null,
} as never,
});
}
describe("HostSection", () => {
const renderHost = (p: Project = awsProject(), disabled = false) =>
render(<HostSection project={p} save={save} disabled={disabled} />);
it("shows the estimated hourly cost for the project's connection and region", async () => {
awsCostEstimate.mockResolvedValueOnce({
usd_per_hour: 0.05,
note_spot: true,
efs_billed_separately: true,
});
useAppState.setState({
appSettings: { hosts: [{ id: "c1", name: "lab" }] } as unknown as AppSettings,
});
const p = awsProject();
p.aws = { ...p.aws!, spot: true };
renderHost(p);
const card = screen.getByRole("region", { name: "Estimated cost" });
await waitFor(() =>
expect(within(card).getByText(/≈ \$0\.050\/hour \(on-demand; Spot is usually up to 70% cheaper\)/)).toBeInTheDocument(),
);
expect(awsCostEstimate).toHaveBeenCalledWith("c1", "eu-west-1", p.aws);
expect(within(card).getByText(/EFS storage is billed separately/)).toBeInTheDocument();
useAppState.setState({ appSettings: null });
});
it("says the estimate is unavailable when the connection is gone", async () => {
useAppState.setState({ appSettings: null });
renderHost();
const card = screen.getByRole("region", { name: "Estimated cost" });
await waitFor(() => expect(within(card).getByText("Estimate unavailable")).toBeInTheDocument());
expect(awsCostEstimate).not.toHaveBeenCalled();
});
it("offers no CPU below 1 vCPU while the gateway sidecar is on", () => {
const p = awsProject();
p.aws = { ...p.aws!, gateway_sidecar: true };
renderHost(p);
const cpus = within(screen.getByLabelText("CPU"))
.getAllByRole("option")
.map((o) => (o as HTMLOptionElement).value);
expect(cpus).not.toContain("256");
expect(cpus).not.toContain("512");
expect(cpus).toContain("1024");
});
it("saves a CPU change with a memory size valid for it", async () => {
renderHost(); // 1024 CPU / 4096 MiB
fireEvent.change(screen.getByLabelText("CPU"), { target: { value: "256" } });
@@ -131,7 +297,214 @@ describe("HostSection", () => {
p.aws = { ...p.aws!, storage: "ephemeral" };
renderHost(p);
expect(screen.getByText(/Claude login and history are lost when the workspace stops/)).toBeInTheDocument();
expect(screen.getByText(/EFS .* coming later/)).toBeInTheDocument();
expect(screen.queryByText(/coming later/)).toBeNull();
});
it("offers EFS storage with what it costs", async () => {
const p = awsProject();
renderHost(p);
const efs = screen.getByRole("radio", { name: "EFS" });
expect(efs).not.toBeDisabled();
fireEvent.click(efs);
await waitFor(() => expect(save).toHaveBeenCalled());
expect(save.mock.calls[0][0].aws.storage).toBe("efs");
p.aws = { ...p.aws!, storage: "efs" };
renderHost(p);
expect(
screen.getByText("Everything in /workspace survives a stop; git and builds are slower over NFS"),
).toBeInTheDocument();
});
it("chooses where the credentials live and warns what copy-at-connect means", async () => {
const p = awsProject();
renderHost(p);
expect(screen.getByRole("radio", { name: "Stored in AWS (Secrets Manager)" })).toBeChecked();
expect(screen.getByText(/kept in the project's Secrets Manager secret/)).toBeInTheDocument();
fireEvent.click(screen.getByRole("radio", { name: "Copied when you connect" }));
await waitFor(() => expect(save).toHaveBeenCalled());
expect(save.mock.calls[0][0].aws.secrets_delivery).toBe("copy_at_connect");
cleanup();
p.aws = { ...p.aws!, secrets_delivery: "copy_at_connect", gateway_sidecar: false };
renderHost(p);
const note = screen.getByRole("note", { name: "Copied when you connect" });
expect(note).toHaveTextContent(
"Private repositories clone, and the setup script runs, after you connect (or after 5 minutes without).",
);
expect(note).toHaveTextContent(/Claude Code's self-update may fail until then/);
expect(note).toHaveTextContent("SSH keys, a Git token or variables you remove from the project stay in a running workspace until it stops.");
expect(note).toHaveTextContent(/not run again when a later connect re-clones/);
expect(note).toHaveTextContent(
"Scheduled jobs and the setup script never get the API keys or custom variables; Git and SSH work once the app has connected.",
);
expect(note).toHaveTextContent("~/.ssh is not kept on EFS");
expect(note).toHaveTextContent("/mnt/triple-c-home/.ssh stay there until you remove them");
expect(note).not.toHaveTextContent(/gateway/);
expect(screen.getByText(/repositories and SSH keys included, starts fresh/)).toBeInTheDocument();
cleanup();
// Ephemeral: nothing is kept anyway, so no SSH note.
renderHost({ ...p, aws: { ...p.aws!, storage: "ephemeral" } });
expect(screen.getByRole("note", { name: "Copied when you connect" })).not.toHaveTextContent("~/.ssh");
cleanup();
p.aws = { ...p.aws!, gateway_sidecar: true };
renderHost(p);
expect(screen.getByRole("note", { name: "Copied when you connect" })).toHaveTextContent(
"The LLM gateway's keys are still stored in AWS",
);
});
it("saves the setup script on blur, and a blank one as none", async () => {
renderHost();
const box = screen.getByLabelText("Setup script");
expect(box.tagName).toBe("TEXTAREA");
expect(box.className).toContain("font-mono");
expect(box.className).not.toContain("focus:outline-none");
fireEvent.change(box, { target: { value: "sudo apt-get install -y jq\n" } });
expect(screen.getByText(/27 of 32,768 bytes/)).toBeInTheDocument();
fireEvent.blur(box);
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
expect(save.mock.calls[0][0].aws.setup_script).toBe("sudo apt-get install -y jq\n");
cleanup();
const p = awsProject();
p.aws = { ...p.aws!, setup_script: "echo hi" };
renderHost(p);
const again = screen.getByLabelText("Setup script");
expect(again).toHaveValue("echo hi");
fireEvent.change(again, { target: { value: " \n" } });
fireEvent.blur(again);
await waitFor(() => expect(save).toHaveBeenCalledTimes(2));
expect(save.mock.calls[1][0].aws.setup_script).toBeNull();
});
it("a toggle right after the setup script's blur keeps the script", async () => {
// The store has not caught up with the first save when the second comes.
const p = awsProject();
renderHost(p);
const box = screen.getByLabelText("Setup script");
fireEvent.change(box, { target: { value: "make deps\n" } });
fireEvent.blur(box);
fireEvent.click(toggle("Spot"));
await waitFor(() => expect(save).toHaveBeenCalledTimes(2));
expect(save.mock.calls[0][0].aws.setup_script).toBe("make deps\n");
expect(save.mock.calls[1][0].aws).toMatchObject({ setup_script: "make deps\n", spot: true });
});
it("starts again from the stored config after a refused save", async () => {
save.mockResolvedValueOnce(false);
renderHost();
const box = screen.getByLabelText("Setup script");
fireEvent.change(box, { target: { value: "make deps\n" } });
fireEvent.blur(box);
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
await act(async () => {});
fireEvent.click(toggle("Spot"));
await waitFor(() => expect(save).toHaveBeenCalledTimes(2));
expect(save.mock.calls[1][0].aws).toMatchObject({ setup_script: null, spot: true });
});
it("refuses a setup script over 32 KiB with the backend's message", () => {
renderHost();
const box = screen.getByLabelText("Setup script");
// 16,385 two-byte characters: 32,770 UTF-8 bytes.
fireEvent.change(box, { target: { value: "é".repeat(16_385) } });
fireEvent.blur(box);
expect(screen.getByRole("alert")).toHaveTextContent("The setup script is 32770 bytes; the limit is 32768");
expect(box).toHaveAttribute("aria-invalid", "true");
expect(save).not.toHaveBeenCalled();
});
it("says when and how the setup script runs", () => {
renderHost();
expect(screen.getByText(/Runs on every start, as claude, with bash -e/)).toBeInTheDocument();
expect(screen.getByText(/once the repositories are cloned/)).toBeInTheDocument();
expect(screen.getByText(/stopped after 30 minutes, and its output shows in the Overview/)).toBeInTheDocument();
expect(screen.queryByText(/never gets the API keys or custom variables/)).toBeNull();
cleanup();
const p = awsProject();
p.aws = { ...p.aws!, secrets_delivery: "copy_at_connect" };
renderHost(p);
expect(
screen.getByText(/the setup script waits for the app's first connect \(at most 5 minutes\) and never gets the API keys or custom variables/),
).toBeInTheDocument();
});
it("shows the environment's lifetime cap when it has one", async () => {
awsEnvironmentMaxHours.mockResolvedValueOnce(24);
renderHost();
expect(
await screen.findByText("This environment stops workspaces after 24 h (advisory)."),
).toBeInTheDocument();
expect(awsEnvironmentMaxHours).toHaveBeenCalledWith(awsProject().id);
cleanup();
for (const none of [0, null]) {
clearEnvironmentMaxHoursCache();
awsEnvironmentMaxHours.mockResolvedValueOnce(none);
renderHost();
await waitFor(() => expect(awsEnvironmentMaxHours).toHaveBeenCalled());
await act(async () => {});
expect(screen.queryByText(/stops workspaces after/)).toBeNull();
cleanup();
}
});
it("reads the lifetime cap once per environment across re-mounts", async () => {
awsEnvironmentMaxHours.mockResolvedValue(24);
renderHost();
await screen.findByText(/stops workspaces after 24 h/);
cleanup();
renderHost();
// Shown from the cache, without asking AWS again.
await screen.findByText(/stops workspaces after 24 h/);
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(1);
cleanup();
// Another environment is its own read.
renderHost(awsProject({ aws_placement: { region: "eu-west-1", environment: "prod" } }));
await waitFor(() => expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(2));
awsEnvironmentMaxHours.mockReset();
});
it("does not let the credentials setting change while the workspace runs", () => {
renderHost(awsProject(), true);
expect(screen.getByRole("radio", { name: "Copied when you connect" })).toBeDisabled();
});
it("says /workspace files stay on their access point after switching away from EFS", () => {
const p = awsProject();
p.aws = {
...p.aws!,
storage: "hybrid",
workspace_access_point_id: "fsap-w",
task_arn: null,
task_definition_arn: null,
access_point_id: null,
};
renderHost(p);
expect(screen.getByRole("note", { name: "EFS files kept" })).toHaveTextContent(
/\/workspace files .* stay on their access point until .*Remove AWS resources/,
);
// It is an AWS resource: removable, and it keeps the project in place.
expect(screen.getByRole("button", { name: "Run on this machine instead" })).toHaveAttribute(
"aria-disabled",
"true",
);
fireEvent.click(screen.getByRole("button", { name: "Remove AWS resources…" }));
const dialog = screen.getByRole("dialog");
expect(within(dialog).getByRole("checkbox", { name: /access points/ })).toBeInTheDocument();
expect(dialog).toHaveTextContent(/\/workspace/);
});
it("does not show the kept-files note on EFS or without a workspace access point", () => {
const p = awsProject();
p.aws = { ...p.aws!, storage: "efs", workspace_access_point_id: "fsap-w" };
renderHost(p);
expect(screen.queryByRole("note", { name: "EFS files kept" })).toBeNull();
});
it("saves repository edits on blur", async () => {
@@ -1,8 +1,20 @@
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import type { AwsWorkspaceConfig, HostConnection, Project, RepoSource } from "../../../../lib/types";
import * as commands from "../../../../lib/tauri-commands";
import { environmentMaxHours } from "../../../../lib/environmentMaxHours";
import { useAppState } from "../../../../store/appState";
import {
COPY_AT_CONNECT_GATEWAY_NOTE,
GATEWAY_SIDECAR_MIN_CPU,
COPY_AT_CONNECT_HINT,
COPY_AT_CONNECT_SSH_NOTE,
COPY_AT_CONNECT_WARNINGS,
SECRETS_MANAGER_HINT,
SETUP_SCRIPT_COPY_AT_CONNECT_NOTE,
SETUP_SCRIPT_HINT,
MAX_SETUP_SCRIPT_BYTES,
setupScriptProblem,
EFS_HINT,
EPHEMERAL_WARNING,
FARGATE_CPUS,
defaultAwsWorkspace,
@@ -10,6 +22,7 @@ import {
formatMemory,
memoryError,
hasSavedFiles,
hasWorkspaceFiles,
memoryOptions,
nearestMemory,
} from "../../../../lib/awsWorkspace";
@@ -17,8 +30,9 @@ import Button from "../../../ui/Button";
import Modal from "../../../ui/Modal";
import Toggle from "../../../ui/Toggle";
import SegmentedControl from "../../../ui/SegmentedControl";
import Field, { ConfigGroup, SwitchRow, inputClass, selectClass } from "../../../ui/Field";
import Field, { ConfigGroup, SwitchRow, inputClass, monoInputClass, selectClass } from "../../../ui/Field";
import RepoListEditor from "../../RepoListEditor";
import AwsCostEstimate from "../AwsCostEstimate";
interface Props {
project: Project;
@@ -48,6 +62,10 @@ export default function HostSection({ project, save, disabled }: Props) {
const [idle, setIdle] = useState(String(aws.idle_stop_minutes ?? DEFAULT_IDLE_MINUTES));
const [idleProblem, setIdleProblem] = useState<string | null>(null);
const [confirmRemoveResources, setConfirmRemoveResources] = useState(false);
const [setupScript, setSetupScript] = useState(aws.setup_script ?? "");
const [setupProblem, setSetupProblem] = useState<string | null>(null);
// The environment's advisory lifetime cap (Q8), from its record.
const [maxHours, setMaxHours] = useState<number | null>(null);
useEffect(() => {
setMemory(String(aws.memory_mib));
@@ -55,11 +73,71 @@ export default function HostSection({ project, save, disabled }: Props) {
setIdle(String(aws.idle_stop_minutes ?? DEFAULT_IDLE_MINUTES));
}, [aws.memory_mib, aws.ephemeral_gib, aws.idle_stop_minutes]);
const saveAws = (patch: Partial<AwsWorkspaceConfig>) => save({ aws: { ...aws, ...patch } });
useEffect(() => {
setSetupScript(aws.setup_script ?? "");
}, [aws.setup_script]);
const hasResources = !!(aws.task_arn || aws.access_point_id || aws.task_definition_arn);
// A removal that kept the saved files keeps their access point stored.
const onlyAccessPoint = !!aws.access_point_id && !aws.task_arn && !aws.task_definition_arn;
const region = project.aws_placement?.region;
const environment = project.aws_placement?.environment;
useEffect(() => {
setMaxHours(null);
if (!project.host_id || !region || !environment) return;
let cancelled = false;
// Best effort: an unreadable record just shows no cap line. Cached per
// environment, so re-opening this section doesn't ask AWS again.
environmentMaxHours(project.id, project.host_id, region, environment)
.then((h) => {
if (!cancelled) setMaxHours(h);
})
.catch(() => {});
return () => {
cancelled = true;
};
}, [project.id, project.host_id, region, environment]);
// The config the next save starts from: the stored one plus every patch
// saved since. A blur's save followed at once by a toggle's (before the
// store has the first back) must not spread the render-time `aws` and
// revert the first. Re-based on the prop when it changes with no save in
// flight.
const latest = useRef({ base: project.aws, value: aws, inFlight: 0 });
if (latest.current.base !== project.aws && latest.current.inFlight === 0) {
latest.current = { base: project.aws, value: aws, inFlight: 0 };
}
const saveAws = async (patch: Partial<AwsWorkspaceConfig>) => {
const l = latest.current;
const next = { ...l.value, ...patch };
l.value = next;
l.inFlight += 1;
// A refused save is not the config any more: start again from the store.
const refused = () => {
if (l.value === next) l.value = l.base ?? defaultAwsWorkspace();
};
try {
const ok = await save({ aws: next });
if (!ok) refused();
return ok;
} catch (e) {
refused();
throw e;
} finally {
l.inFlight -= 1;
}
};
const hasResources = !!(
aws.task_arn ||
aws.access_point_id ||
aws.workspace_access_point_id ||
aws.task_definition_arn
);
// A removal that kept the saved files keeps their access points stored.
const onlyAccessPoint =
!!(aws.access_point_id || aws.workspace_access_point_id) &&
!aws.task_arn &&
!aws.task_definition_arn;
// Switched away from EFS: its /workspace files are still on their access point.
const keptWorkspaceFiles = aws.storage !== "efs" && !!aws.workspace_access_point_id;
// Not `disabled` (which also allows Error): on AWS, Error can mean a live task.
const stopped = project.status === "stopped";
@@ -78,6 +156,16 @@ export default function HostSection({ project, save, disabled }: Props) {
if (ok && value !== aws.ephemeral_gib) void saveAws({ ephemeral_gib: value });
};
const commitSetupScript = () => {
const problem = setupScriptProblem(setupScript);
setSetupProblem(problem);
if (problem) return;
const value = setupScript.trim() === "" ? null : setupScript;
if (value !== (aws.setup_script ?? null)) void saveAws({ setup_script: value });
};
const setupBytes = new TextEncoder().encode(setupScript).length;
const commitIdle = () => {
const value = Number(idle);
const ok = Number.isInteger(value) && value >= 5 && value <= 1440;
@@ -106,6 +194,11 @@ export default function HostSection({ project, save, disabled }: Props) {
{project.aws_placement?.environment}
</dd>
</dl>
{maxHours !== null && maxHours > 0 && (
<p className="text-xs text-[var(--text-secondary)]">
This environment stops workspaces after {maxHours} h (advisory).
</p>
)}
<div className="flex flex-wrap gap-4">
<Field label="CPU">
@@ -124,7 +217,10 @@ export default function HostSection({ project, save, disabled }: Props) {
void saveAws({ cpu, memory_mib });
}}
>
{FARGATE_CPUS.map((c) => (
{FARGATE_CPUS.filter(
// The gateway sidecar needs 1 vCPU / 2 GiB (Rust validates it too).
(c) => !aws.gateway_sidecar || c >= GATEWAY_SIDECAR_MIN_CPU,
).map((c) => (
<option key={c} value={c}>
{formatCpu(c)}
</option>
@@ -196,11 +292,9 @@ export default function HostSection({ project, save, disabled }: Props) {
segments={[
{ value: "ephemeral", label: "Ephemeral", caution: true },
{ value: "hybrid", label: "Hybrid" },
{ value: "efs", label: "EFS" },
]}
/>
<span className="text-xs text-[var(--text-disabled)]">
EFS (everything persists) — coming later
</span>
</div>
{aws.storage === "ephemeral" ? (
<p
@@ -209,17 +303,38 @@ export default function HostSection({ project, save, disabled }: Props) {
>
{EPHEMERAL_WARNING} Push your work to a remote before stopping.
</p>
) : aws.storage === "efs" ? (
<p className="mt-1 text-xs text-[var(--text-secondary)]">
<span>{EFS_HINT}</span>. The Claude login and history
{aws.secrets_delivery === "copy_at_connect" ? "" : ", and SSH keys,"} are kept on EFS too.
</p>
) : (
<p className="mt-1 text-xs text-[var(--text-secondary)]">
The Claude login, history and SSH keys are kept on EFS between starts; everything
else, repositories included, starts fresh.
{aws.secrets_delivery === "copy_at_connect"
? "The Claude login and history are kept on EFS between starts; everything else, repositories and SSH keys included, starts fresh."
: "The Claude login, history and SSH keys are kept on EFS between starts; everything else, repositories included, starts fresh."}
</p>
)}
{keptWorkspaceFiles && (
<p
role="note"
aria-label="EFS files kept"
className="mt-2 px-2 py-1.5 text-xs text-[var(--text-secondary)] bg-[var(--bg-secondary)] border border-[var(--border-color)] rounded-[var(--radius-control)]"
>
The /workspace files from when this project used EFS storage stay on their access
point until &ldquo;Remove AWS resources&rdquo; removes it (with the access points
option checked). Switching back to EFS picks them up again.
</p>
)}
</div>
<Field
label="Ephemeral storage (GiB)"
hint="The task's local disk: 21 to 200 GiB. Repositories and builds live here."
hint={
aws.storage === "efs"
? "The task's local disk: 21 to 200 GiB. With EFS storage, /workspace is on EFS instead."
: "The task's local disk: 21 to 200 GiB. Repositories and builds live here."
}
>
{(id) => (
<input
@@ -257,6 +372,19 @@ export default function HostSection({ project, save, disabled }: Props) {
}
/>
<section aria-label="Estimated cost">
<h3 className="text-[13px] font-medium text-[var(--text-primary)]">Estimated cost</h3>
<p className="mt-0.5 mb-1 text-xs text-[var(--text-secondary)] leading-snug">
The on-demand Fargate price in this region for the CPU, memory, architecture and disk
above.
</p>
<AwsCostEstimate
connectionId={connection ? connection.id : null}
region={project.aws_placement?.region}
config={aws}
/>
</section>
<SwitchRow
label="Stop when idle"
hint={
@@ -310,6 +438,40 @@ export default function HostSection({ project, save, disabled }: Props) {
}
/>
<div>
<span className="block text-[13px] font-medium text-[var(--text-primary)] mb-1">
Credentials
</span>
<SegmentedControl
label="Credentials"
value={aws.secrets_delivery}
disabled={disabled}
onChange={(secrets_delivery) => void saveAws({ secrets_delivery })}
segments={[
{ value: "secrets_manager", label: "Stored in AWS (Secrets Manager)" },
{ value: "copy_at_connect", label: "Copied when you connect" },
]}
/>
{aws.secrets_delivery === "copy_at_connect" ? (
<>
<p className="mt-1 text-xs text-[var(--text-secondary)]">{COPY_AT_CONNECT_HINT}</p>
<ul
role="note"
aria-label="Copied when you connect"
className="mt-2 px-2 py-1.5 text-xs text-[var(--warning)] bg-[var(--warning-muted)] border border-[var(--warning)]/30 rounded-[var(--radius-control)] list-disc list-inside"
>
{COPY_AT_CONNECT_WARNINGS.map((w) => (
<li key={w}>{w}</li>
))}
{aws.storage !== "ephemeral" && <li>{COPY_AT_CONNECT_SSH_NOTE}</li>}
{aws.gateway_sidecar && <li>{COPY_AT_CONNECT_GATEWAY_NOTE}</li>}
</ul>
</>
) : (
<p className="mt-1 text-xs text-[var(--text-secondary)]">{SECRETS_MANAGER_HINT}</p>
)}
</div>
<div className="pt-2 border-t border-[var(--border-color)]">
<span className="block text-[13px] font-medium text-[var(--text-primary)]">
Repositories
@@ -325,6 +487,48 @@ export default function HostSection({ project, save, disabled }: Props) {
/>
</div>
<div className="pt-2 border-t border-[var(--border-color)]">
<Field
label="Setup script"
hint={
<>
{SETUP_SCRIPT_HINT}
{aws.secrets_delivery === "copy_at_connect" && <> {SETUP_SCRIPT_COPY_AT_CONNECT_NOTE}</>}
</>
}
>
{(id) => (
<>
<textarea
id={id}
value={setupScript}
disabled={disabled}
rows={8}
spellCheck={false}
placeholder={"sudo apt-get update\nsudo apt-get install -y jq"}
aria-invalid={setupProblem ? true : undefined}
aria-describedby={`${id}-size`}
onChange={(e) => setSetupScript(e.target.value)}
onBlur={commitSetupScript}
className={`${monoInputClass} min-h-[8rem] resize-y`}
/>
<p
id={`${id}-size`}
className={`mt-1 text-xs ${setupBytes > MAX_SETUP_SCRIPT_BYTES ? "text-[var(--error)]" : "text-[var(--text-secondary)]"}`}
>
{setupBytes.toLocaleString("en-US")} of {MAX_SETUP_SCRIPT_BYTES.toLocaleString("en-US")} bytes
(32 KiB, UTF-8)
</p>
</>
)}
</Field>
{setupProblem && (
<p role="alert" className="mt-1 text-xs text-[var(--error)]">
{setupProblem}
</p>
)}
</div>
<div className="pt-2 border-t border-[var(--border-color)] flex flex-wrap items-center gap-2">
<Button
size="md"
@@ -346,7 +550,7 @@ export default function HostSection({ project, save, disabled }: Props) {
!stopped
? STILL_RUNNING
: onlyAccessPoint
? "The saved files' access point is still on AWS. Remove the AWS resources with \u201cAlso remove the workspace\u2019s file-system access point\u201d first."
? "The saved files' access point is still on AWS. Remove the AWS resources with \u201cAlso remove the workspace\u2019s file-system access points\u201d first."
: "Remove the workspace's AWS resources first, so nothing is left running or billed."
}
onClick={() => void save({ host_id: null, aws_placement: null, aws: null })}
@@ -422,8 +626,11 @@ function RemoveAwsResourcesModal({ project, onClose }: { project: Project; onClo
onChange={(e) => setDiscardData(e.target.checked)}
/>
<span>
Also remove the workspace&rsquo;s file-system access point
Also remove the workspace&rsquo;s file-system access points
<span className="block text-xs text-[var(--text-secondary)]">
{hasWorkspaceFiles(project)
? "Both go: the one for the saved home files and the one for /workspace. "
: ""}
The files stay on your own EFS file system (only your workspace role can read
them) until an administrator offboards you. Left unchecked, the access point
stays too: the next start here reuses it, and the project can&rsquo;t move to
@@ -17,7 +17,14 @@ import Field, {
selectClass,
} from "../../../ui/Field";
import Toggle from "../../../ui/Toggle";
import { isAwsProject, localEndpointProblem } from "../../../../lib/awsWorkspace";
import {
GATEWAY_SIDECAR_HINT,
GATEWAY_SIDECAR_LABEL,
GATEWAY_SIDECAR_ON_NOTE,
gatewaySidecarProblem,
isAwsProject,
localEndpointProblem,
} from "../../../../lib/awsWorkspace";
import { useAppState } from "../../../../store/appState";
/** P6: an AWS workspace can reach a local model only over the network. */
@@ -201,6 +208,10 @@ export default function ModelSection({ project, save, disabled }: Props) {
: project.backend === "open_ai_compatible"
? (project.openai_compatible_config ?? DEFAULT_OPENAI_COMPATIBLE_CONFIG).base_url
: null;
// Q7: with the sidecar, Claude Code talks to the gateway in the task, so
// the typed base URL is not used on AWS and its warning would mislead.
const sidecarOn = aws && project.aws?.gateway_sidecar === true;
const sidecarProblem = aws ? gatewaySidecarProblem(project, gateway) : null;
const endpointProblem =
aws && savedBaseUrl
? localEndpointProblem(savedBaseUrl, gateway?.enabled ? gateway.port : null)
@@ -208,6 +219,9 @@ export default function ModelSection({ project, save, disabled }: Props) {
const handleBackendChange = (mode: Backend) => {
const patch: Partial<Project> = { backend: mode };
// The sidecar needs this backend: leaving it turns the sidecar off.
if (sidecarOn && mode !== "open_ai_compatible" && project.aws)
patch.aws = { ...project.aws, gateway_sidecar: false };
if (mode === "bedrock" && !project.bedrock_config)
patch.bedrock_config = DEFAULT_BEDROCK_CONFIG;
if (mode === "ollama" && !project.ollama_config)
@@ -241,7 +255,7 @@ export default function ModelSection({ project, save, disabled }: Props) {
</select>
)}
</Field>
{aws && CUSTOM_ENDPOINT_BACKENDS.includes(project.backend) && (
{aws && CUSTOM_ENDPOINT_BACKENDS.includes(project.backend) && !sidecarOn && (
<div
role="note"
className="px-2 py-1.5 space-y-1 text-xs text-[var(--warning)] bg-[var(--warning-muted)] border border-[var(--warning)]/30 rounded-[var(--radius-control)]"
@@ -250,6 +264,35 @@ export default function ModelSection({ project, save, disabled }: Props) {
{endpointProblem && <p>{endpointProblem}</p>}
</div>
)}
{aws && project.backend === "open_ai_compatible" && project.aws && (
<div className="space-y-2">
<SwitchRow
label={GATEWAY_SIDECAR_LABEL}
hint={GATEWAY_SIDECAR_HINT}
control={
<Toggle
label={GATEWAY_SIDECAR_LABEL}
checked={sidecarOn}
onChange={(value) =>
save({ aws: { ...project.aws!, gateway_sidecar: value } })
}
// Turning it off is always allowed; on only when it can run.
disabled={disabled || (!sidecarOn && sidecarProblem !== null)}
/>
}
/>
{sidecarProblem && (
<p role="alert" className="text-xs text-[var(--warning)]">
{sidecarProblem}
</p>
)}
{sidecarOn && (
<p role="note" aria-label="LLM gateway sidecar" className="text-xs text-[var(--text-secondary)]">
{GATEWAY_SIDECAR_ON_NOTE}
</p>
)}
</div>
)}
{/* Only Anthropic reads CLAUDE_CODE_OAUTH_TOKEN; the other backends
authenticate through their own credentials entirely. */}
@@ -57,10 +57,21 @@ describe("AdminBundleModal", () => {
environment: "team",
ownerId: null,
network: { mode: "private-nat" },
maxWorkspaceHours: 0,
}),
);
});
it("an environment bundle carries the lifetime cap", async () => {
invoke.mockResolvedValue("/tmp/env");
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="environment" onClose={vi.fn()} />);
fireEvent.change(screen.getByLabelText("Maximum workspace lifetime (hours, 0 = none)"), { target: { value: "24" } });
fireEvent.click(screen.getByRole("button", { name: "Save bundle to folder…" }));
await waitFor(() =>
expect(invoke).toHaveBeenCalledWith("save_aws_admin_bundle", expect.objectContaining({ maxWorkspaceHours: 24 })),
);
});
it("shows a save failure through the AWS error copy", async () => {
invoke.mockRejectedValue({ kind: "sso_expired", profile: "prod-sso" });
render(<AdminBundleModal connectionId="h1" region="eu-west-1" kind="permissions_boundary" onClose={vi.fn()} />);
@@ -5,6 +5,7 @@ import type { AwsError, ModuleKind, NetworkChoice } from "../../../lib/types";
import Button from "../../ui/Button";
import Field, { inputClass } from "../../ui/Field";
import Modal from "../../ui/Modal";
import LifetimeCapField, { parseCapHours } from "./LifetimeCapField";
import NetworkFields from "./NetworkFields";
import { DEFAULT_NETWORK, environmentNameError, networkFromDraft, type NetworkDraft } from "./environmentFormat";
@@ -48,6 +49,7 @@ export default function AdminBundleModal({
const askEnvironment = kind === "environment" && !environment;
const [name, setName] = useState("main");
const [draft, setDraft] = useState<NetworkDraft>(DEFAULT_NETWORK);
const [capText, setCapText] = useState("0");
const [saving, setSaving] = useState(false);
const [saved, setSaved] = useState<string | null>(null);
const [error, setError] = useState<AwsError | null>(null);
@@ -55,7 +57,8 @@ export default function AdminBundleModal({
const nameError = askEnvironment ? environmentNameError(name) : null;
const picked = askEnvironment ? networkFromDraft(draft) : null;
const ready = !nameError && (!picked || "network" in picked);
const capHours = parseCapHours(capText);
const ready = !nameError && (!picked || "network" in picked) && (!askEnvironment || capHours !== null);
const save = async () => {
setSaving(true);
@@ -65,6 +68,7 @@ export default function AdminBundleModal({
environment: askEnvironment ? name : environment,
ownerId,
network: picked && "network" in picked ? picked.network : network,
...(askEnvironment && capHours !== null ? { maxWorkspaceHours: capHours } : {}),
});
if (folder) {
setSaved(folder);
@@ -127,6 +131,7 @@ export default function AdminBundleModal({
{(id) => <input id={id} className={inputClass} value={name} onChange={(e) => setName(e.target.value)} />}
</Field>
<NetworkFields value={draft} onChange={setDraft} error={picked && "error" in picked ? picked.error : null} />
<LifetimeCapField value={capText} onChange={setCapText} />
</div>
)}
<Button size="md" variant="primary" onClick={save} disabled={saving || !ready}>
@@ -55,7 +55,7 @@ describe("DeployEnvironmentModal", () => {
fireEvent.click(screen.getByRole("radio", { name: "Public" }));
expect(screen.getByLabelText("Name")).toHaveValue("main");
fireEvent.click(screen.getByRole("button", { name: "Deploy" }));
expect(props.onDeploy).toHaveBeenCalledWith("main", { mode: "public" });
expect(props.onDeploy).toHaveBeenCalledWith("main", { mode: "public" }, 0);
await waitFor(() => expect(props.onClose).toHaveBeenCalled());
});
@@ -71,7 +71,7 @@ describe("DeployEnvironmentModal", () => {
fireEvent.change(screen.getByLabelText("Name"), { target: { value: "team-2" } });
expect(deploy).toBeEnabled();
fireEvent.click(deploy);
expect(props.onDeploy).toHaveBeenCalledWith("team-2", { mode: "public" });
expect(props.onDeploy).toHaveBeenCalledWith("team-2", { mode: "public" }, 0);
await waitFor(() => expect(props.onClose).toHaveBeenCalled());
});
@@ -92,7 +92,24 @@ describe("DeployEnvironmentModal", () => {
vpc_id: "vpc-0123abcd",
subnet_ids: ["subnet-0123abcd", "subnet-4567abcd"],
assign_public_ip: true,
});
}, 0);
await waitFor(() => expect(props.onClose).toHaveBeenCalled());
});
it("carries the maximum workspace lifetime and refuses more than a week", async () => {
const props = renderModal();
const cap = screen.getByLabelText("Maximum workspace lifetime (hours, 0 = none)");
expect(cap).toHaveValue("0");
const deploy = screen.getByRole("button", { name: "Deploy" });
for (const bad of ["169", "-1", "1.5", "", "abc"]) {
fireEvent.change(cap, { target: { value: bad } });
expect(deploy).toBeDisabled();
}
fireEvent.change(cap, { target: { value: "168" } });
expect(deploy).toBeEnabled();
fireEvent.change(cap, { target: { value: "12" } });
fireEvent.click(deploy);
expect(props.onDeploy).toHaveBeenCalledWith("main", { mode: "public" }, 12);
await waitFor(() => expect(props.onClose).toHaveBeenCalled());
});
@@ -177,7 +194,7 @@ describe("DeployEnvironmentModal", () => {
expect(screen.getByText(/different network than the one it was deployed with replaces/)).toBeInTheDocument();
fireEvent.click(screen.getByRole("radio", { name: "Private + NAT" }));
fireEvent.click(screen.getByRole("button", { name: "Repair" }));
expect(props.onDeploy).toHaveBeenCalledWith("dev", { mode: "private-nat" });
expect(props.onDeploy).toHaveBeenCalledWith("dev", { mode: "private-nat" }, 0);
await waitFor(() => expect(props.onClose).toHaveBeenCalled());
});
});
@@ -7,6 +7,7 @@ import Field, { inputClass } from "../../ui/Field";
import Modal from "../../ui/Modal";
import AdminBundleModal from "./AdminBundleModal";
import DeployProgress, { runKey, useDeployRuns } from "./DeployProgress";
import LifetimeCapField, { parseCapHours } from "./LifetimeCapField";
import NetworkFields from "./NetworkFields";
import {
BOUNDARY_STATE_KEY,
@@ -37,11 +38,12 @@ export default function DeployEnvironmentModal({
repair?: string;
onClose: () => void;
/** Starts the run; resolves to the command's error, or null once started. */
onDeploy: (name: string, network: NetworkChoice) => Promise<AwsError | null>;
onDeploy: (name: string, network: NetworkChoice, maxWorkspaceHours: number) => Promise<AwsError | null>;
onDetect: () => void;
}) {
const [name, setName] = useState("main");
const [draft, setDraft] = useState<NetworkDraft>(DEFAULT_NETWORK);
const [capText, setCapText] = useState("0");
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<AwsError | null>(null);
const [boundaryDone, setBoundaryDone] = useState(false);
@@ -73,7 +75,8 @@ export default function DeployEnvironmentModal({
: environmentNameError(name) ??
(scan.environments.some((e) => e.name === name) ? `An environment named ${name} already exists here.` : null);
const picked = networkFromDraft(draft);
const ready = !nameError && "network" in picked && !submitting;
const capHours = parseCapHours(capText);
const ready = !nameError && "network" in picked && capHours !== null && !submitting;
const createBoundary = async () => {
setBoundaryError(null);
@@ -85,10 +88,10 @@ export default function DeployEnvironmentModal({
};
const submit = async () => {
if (!("network" in picked)) return;
if (!("network" in picked) || capHours === null) return;
setSubmitting(true);
setError(null);
const err = await onDeploy(target, picked.network);
const err = await onDeploy(target, picked.network, capHours);
setSubmitting(false);
if (err) setError(err);
else onClose();
@@ -171,6 +174,7 @@ export default function DeployEnvironmentModal({
</Field>
)}
<NetworkFields value={draft} onChange={setDraft} error={"error" in picked ? picked.error : null} />
<LifetimeCapField value={capText} onChange={setCapText} />
<p className="text-xs text-[var(--text-secondary)] leading-snug">
First deploy downloads OpenTofu (~30 MB) and the AWS provider (~150 MB).
</p>
@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import EnvironmentsPanel from "./EnvironmentsPanel";
import type { DeployProgress, EnvironmentScan } from "../../../lib/types";
import { conn, env, progress, scan, tofuEvent } from "./testkit";
import { conn, env, info, progress, scan, tofuEvent } from "./testkit";
const invoke = vi.fn();
vi.mock("@tauri-apps/api/core", () => ({
@@ -301,6 +301,29 @@ describe("EnvironmentsPanel", () => {
);
});
it("Update pre-fills the environment's recorded cap and sends what is shown", async () => {
backend(
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["join", "update"], info: { ...info("dev"), max_workspace_hours: 24 }, onboarding: { state: "joined" } })] }),
async (cmd) => {
if (cmd === "update_aws_environment") return "u1";
throw new Error(`unexpected ${cmd}`);
},
);
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
await open();
fireEvent.click(await screen.findByRole("button", { name: "Update dev" }));
const dialog = screen.getByRole("dialog", { name: "Update dev" });
const cap = within(dialog).getByLabelText("Maximum workspace lifetime (hours, 0 = none)");
expect(cap).toHaveValue("24");
fireEvent.change(cap, { target: { value: "200" } });
expect(within(dialog).getByRole("button", { name: "Update" })).toBeDisabled();
fireEvent.change(cap, { target: { value: "24" } });
fireEvent.click(within(dialog).getByRole("button", { name: "Update" }));
await waitFor(() =>
expect(invoke).toHaveBeenCalledWith("update_aws_environment", { connectionId: "h1", region: "eu-west-1", name: "dev", maxWorkspaceHours: 24 }),
);
});
it("shows a failed run's errors in full", async () => {
backend(
() => scan({ environments: [env({ state: { state: "update_available" }, actions: ["join", "update"], onboarding: { state: "joined" } })] }),
@@ -312,7 +335,8 @@ describe("EnvironmentsPanel", () => {
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
await open();
fireEvent.click(await screen.findByRole("button", { name: "Update dev" }));
await waitFor(() => expect(invoke).toHaveBeenCalledWith("update_aws_environment", { connectionId: "h1", region: "eu-west-1", name: "dev" }));
fireEvent.click(within(screen.getByRole("dialog", { name: "Update dev" })).getByRole("button", { name: "Update" }));
await waitFor(() => expect(invoke).toHaveBeenCalledWith("update_aws_environment", { connectionId: "h1", region: "eu-west-1", name: "dev", maxWorkspaceHours: 0 }));
const long = "Error: creating IAM Role (triple-c-dev-task): AccessDenied: User is not authorized to perform iam:CreateRole\n with aws_iam_role.task";
emit(progress({ op_id: "u1", events: [tofuEvent(long, { level: "error", kind: "diagnostic" })] }));
emit(progress({ op_id: "u1", events: [], done: true, cancellable: false, result: { ok: false, exit_code: 1, summary: "Apply failed" } }));
@@ -362,8 +386,9 @@ describe("EnvironmentsPanel", () => {
resolveUs(scan({ region: "us-east-1", environments: [env({ state: { state: "update_available" }, actions: ["update"], onboarding: { state: "joined" } })] })),
);
fireEvent.click(await screen.findByRole("button", { name: "Update dev" }));
fireEvent.click(within(screen.getByRole("dialog", { name: "Update dev" })).getByRole("button", { name: "Update" }));
await waitFor(() =>
expect(invoke).toHaveBeenCalledWith("update_aws_environment", { connectionId: "h1", region: "us-east-1", name: "dev" }),
expect(invoke).toHaveBeenCalledWith("update_aws_environment", { connectionId: "h1", region: "us-east-1", name: "dev", maxWorkspaceHours: 0 }),
);
expect(await screen.findByText("Running")).toBeInTheDocument();
});
@@ -402,6 +427,7 @@ describe("EnvironmentsPanel", () => {
render(<EnvironmentsPanel connection={conn} tier="environment_admin" />);
await open();
fireEvent.click(await screen.findByRole("button", { name: "Update dev" }));
fireEvent.click(within(screen.getByRole("dialog", { name: "Update dev" })).getByRole("button", { name: "Update" }));
expect(await screen.findByText(/Finished: No changes\./)).toBeInTheDocument();
expect(screen.getByText("Refreshing state")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Cancel deployment" })).toBeNull();
@@ -27,6 +27,7 @@ import Field, { inputClass, selectClass } from "../../ui/Field";
import Modal from "../../ui/Modal";
import StatusIndicator from "../../ui/StatusIndicator";
import AdminBundleModal from "./AdminBundleModal";
import UpdateEnvironmentModal from "./UpdateEnvironmentModal";
import DeployEnvironmentModal from "./DeployEnvironmentModal";
import DeployProgress, { runKey, useDeployRuns, type DeployRun } from "./DeployProgress";
import PolicyFiles from "./PolicyFiles";
@@ -133,6 +134,7 @@ export default function EnvironmentsPanel({
const [waiting, setWaiting] = useState<Set<string>>(new Set());
const [deployOpen, setDeployOpen] = useState(false);
const [repairOf, setRepairOf] = useState<string | null>(null);
const [updateOf, setUpdateOf] = useState<{ name: string; region: string; hours: number } | null>(null);
const [destroyOf, setDestroyOf] = useState<{ name: string; region: string } | null>(null);
/** An Unlock waiting for its typed confirmation: an environment's lock or the caller's onboarding's. */
const [unlockOf, setUnlockOf] = useState<{
@@ -225,13 +227,18 @@ export default function EnvironmentsPanel({
const current = scan && scan.region === region ? scan : null;
const stale = detecting || !current;
const deploy = (runRegion: string, name: string, network: NetworkChoice) =>
const deploy = (runRegion: string, name: string, network: NetworkChoice, hours: number) =>
launch(runRegion, name, environmentStateKey(name), `Deploy of ${name}`, () =>
deployAwsEnvironment(connection.id, runRegion, name, network),
deployAwsEnvironment(connection.id, runRegion, name, network, hours),
);
const repair = (runRegion: string, name: string, network?: NetworkChoice) =>
// Without `hours`, the environment's recorded cap is kept.
const repair = (runRegion: string, name: string, network?: NetworkChoice, hours?: number) =>
launch(runRegion, name, environmentStateKey(name), `Repair of ${name}`, () =>
repairAwsEnvironment(connection.id, runRegion, name, network),
repairAwsEnvironment(connection.id, runRegion, name, network, hours),
);
const update = (runRegion: string, name: string, hours: number) =>
launch(runRegion, name, environmentStateKey(name), `Update of ${name}`, () =>
updateAwsEnvironment(connection.id, runRegion, name, hours),
);
const join = (found: EnvironmentScan, e: DetectedEnvironment) => {
@@ -422,11 +429,7 @@ export default function EnvironmentsPanel({
size="sm"
disabled={busy}
aria-label={`Update ${e.name}`}
onClick={() =>
void launch(at, e.name, environmentStateKey(e.name), `Update of ${e.name}`, () =>
updateAwsEnvironment(connection.id, at, e.name),
)
}
onClick={() => setUpdateOf({ name: e.name, region: at, hours: e.info?.max_workspace_hours ?? 0 })}
>
Update
</Button>
@@ -568,12 +571,21 @@ export default function EnvironmentsPanel({
setDeployOpen(false);
setRepairOf(null);
}}
onDeploy={(name, network) =>
repairOf ? repair(current.region, name, network) : deploy(current.region, name, network)
onDeploy={(name, network, hours) =>
repairOf ? repair(current.region, name, network, hours) : deploy(current.region, name, network, hours)
}
onDetect={() => void detect()}
/>
)}
{updateOf && (
<UpdateEnvironmentModal
name={updateOf.name}
region={updateOf.region}
currentHours={updateOf.hours}
onClose={() => setUpdateOf(null)}
onUpdate={(hours) => update(updateOf.region, updateOf.name, hours)}
/>
)}
{destroyOf && (
<TypedConfirmModal
title={`Destroy ${destroyOf.name}`}
@@ -0,0 +1,44 @@
import Field, { inputClass } from "../../ui/Field";
/** The longest cap the environment module accepts (`MAX_WORKSPACE_HOURS` in `aws/tofu_modules.rs`). */
export const MAX_WORKSPACE_HOURS = 168;
/** The hours in `text`, or null when it isn't a whole number from 0 to 168. */
export function parseCapHours(text: string): number | null {
if (!/^\d{1,3}$/.test(text.trim())) return null;
const n = Number(text);
return n <= MAX_WORKSPACE_HOURS ? n : null;
}
/** "Maximum workspace lifetime (hours, 0 = none)": the environment's advisory cap. */
export default function LifetimeCapField({
value,
onChange,
}: {
value: string;
onChange: (text: string) => void;
}) {
const bad = parseCapHours(value) === null;
return (
<Field
label="Maximum workspace lifetime (hours, 0 = none)"
hint={
bad
? `Enter a whole number of hours from 0 to ${MAX_WORKSPACE_HOURS}.`
: "A workspace stops once it has run this long (after a best-effort WIP push). Advisory: it is enforced inside the workspace, which its user controls."
}
>
{(id) => (
<input
id={id}
type="text"
inputMode="numeric"
className={inputClass}
value={value}
aria-invalid={bad}
onChange={(e) => onChange(e.target.value)}
/>
)}
</Field>
);
}
@@ -0,0 +1,69 @@
import { useState } from "react";
import { awsErrorMessage } from "../../../lib/aws-error";
import type { AwsError } from "../../../lib/types";
import Button from "../../ui/Button";
import Modal from "../../ui/Modal";
import LifetimeCapField, { parseCapHours } from "./LifetimeCapField";
/**
* Update an environment to the app's module version. The lifetime cap is
* pre-filled from the environment's record, so an update keeps it unless
* the admin changes it.
*/
export default function UpdateEnvironmentModal({
name,
region,
currentHours,
onClose,
onUpdate,
}: {
name: string;
region: string;
/** The cap in the environment's record (0 = none). */
currentHours: number;
onClose: () => void;
/** Starts the run; resolves to the command's error, or null once started. */
onUpdate: (hours: number) => Promise<AwsError | null>;
}) {
const [text, setText] = useState(String(currentHours));
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<AwsError | null>(null);
const hours = parseCapHours(text);
const submit = async () => {
if (hours === null) return;
setSubmitting(true);
setError(null);
const err = await onUpdate(hours);
setSubmitting(false);
if (err) setError(err);
else onClose();
};
return (
<Modal
title={`Update ${name}`}
description={`In ${region}.`}
onClose={onClose}
footer={
<>
<Button size="md" onClick={onClose}>
Cancel
</Button>
<Button size="md" variant="primary" onClick={submit} disabled={hours === null || submitting}>
{submitting ? "Updating…" : "Update"}
</Button>
</>
}
>
<div className="space-y-4">
<LifetimeCapField value={text} onChange={setText} />
{error && (
<p role="alert" className="text-xs text-[var(--error)] leading-snug">
{awsErrorMessage(error)}
</p>
)}
</div>
</Modal>
);
}
@@ -40,6 +40,7 @@ export function info(name = "dev"): EnvironmentInfo {
log_group: `/triple-c/${name}`,
network_mode: "public",
tenancy: "strict",
max_workspace_hours: 0,
};
}
+8 -2
View File
@@ -9,6 +9,12 @@ const TONE: Record<Toast["kind"], { border: string; bg: string; fg: string; glyp
fg: "var(--error)",
glyph: "▲",
},
warning: {
border: "var(--warning)",
bg: "var(--warning-muted)",
fg: "var(--warning)",
glyph: "▲",
},
success: {
border: "var(--success)",
bg: "var(--success-muted)",
@@ -27,9 +33,9 @@ function ToastCard({ toast, onDismiss }: { toast: Toast; onDismiss: () => void }
const [expanded, setExpanded] = useState(false);
const tone = TONE[toast.kind];
// Errors stay until dismissed; transient confirmations time out.
// Errors and warnings stay until dismissed; transient confirmations time out.
useEffect(() => {
if (toast.kind === "error") return;
if (toast.kind === "error" || toast.kind === "warning") return;
const timer = setTimeout(onDismiss, 6000);
return () => clearTimeout(timer);
}, [toast.kind, onDismiss]);
@@ -71,6 +71,10 @@ describe("useProjectStatusEvents", () => {
expect(t[0].kind).toBe("info");
expect(t[0].message).toContain("idle");
t = fire({ kind: "lifetime", message: null });
expect(t[0].kind).toBe("info");
expect(t[0].message).toContain("maximum workspace lifetime was reached");
expect(fire({ kind: "user", message: null })).toHaveLength(0);
// A task that never started is news even when found at startup.
+9 -1
View File
@@ -19,7 +19,7 @@ export interface ContainerExitedEvent {
/** `container_watch.rs`'s `StopNote`. */
export interface StopNote {
/** `user`: someone stopped it (console, CLI, another machine) — no toast. */
kind: "spot" | "idle" | "failed" | "user" | "exited";
kind: "spot" | "idle" | "lifetime" | "failed" | "user" | "exited";
message: string | null;
}
@@ -52,6 +52,14 @@ export function exitToast(
message: `“${name}” stopped after being idle`,
detail: "Its AWS workspace stopped itself to save cost. Start the project again to continue.",
};
case "lifetime":
return {
kind: "info",
message: `“${name}” stopped: the environment's maximum workspace lifetime was reached`,
detail:
"Its AWS environment caps how long a workspace runs. With the WIP push on, unsaved work " +
"was pushed (best effort) to a triple-c/wip/ branch. Start the project again to continue.",
};
default:
return {
kind: "error",
@@ -0,0 +1,75 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderHook } from "@testing-library/react";
import { useAppState } from "../store/appState";
let handler: ((e: { payload: unknown }) => void) | null = null;
vi.mock("@tauri-apps/api/event", () => ({
listen: async (name: string, cb: (e: { payload: unknown }) => void) => {
if (name === "workspace-warning") handler = cb;
return () => {};
},
}));
import { useWorkspaceWarnings } from "./useWorkspaceWarnings";
describe("useWorkspaceWarnings", () => {
beforeEach(() => {
handler = null;
useAppState.setState({ toasts: [] });
});
it("shows a start's clone/setup warning as a warning toast with the last lines", async () => {
renderHook(() => useWorkspaceWarnings());
await vi.waitFor(() => expect(handler).not.toBeNull());
const fire = () =>
handler!({
payload: {
project_id: "p1",
message: "“demo” started, but not everything went well",
detail: "The setup script failed (exit 2).\nLast lines:\nE: oops",
},
});
fire();
fire();
const { toasts } = useAppState.getState();
// A second start's warning replaces the first.
expect(toasts).toHaveLength(1);
expect(toasts[0].kind).toBe("warning");
expect(toasts[0].message).toContain("demo");
expect(toasts[0].detail).toContain("E: oops");
});
it("keeps a port-mapping warning beside the start's own warning", async () => {
renderHook(() => useWorkspaceWarnings());
await vi.waitFor(() => expect(handler).not.toBeNull());
handler!({ payload: { project_id: "p1", message: "started", detail: "clone failed" } });
handler!({
payload: {
project_id: "p1",
message: "“demo”: some port mappings could not be opened",
detail: "3000 → 3000: host port 3000 is already in use on this machine",
topic: "ports",
},
});
const { toasts } = useAppState.getState();
expect(toasts).toHaveLength(2);
expect(toasts[1].detail).toContain("already in use");
});
it("keeps a copy-at-connect warning beside the start's own warning", async () => {
renderHook(() => useWorkspaceWarnings());
await vi.waitFor(() => expect(handler).not.toBeNull());
handler!({ payload: { project_id: "p1", message: "started", detail: "clone failed" } });
handler!({
payload: {
project_id: "p1",
message: "“demo”: the credentials could not be copied into the workspace",
detail: "upload failed",
topic: "connect",
},
});
const { toasts } = useAppState.getState();
expect(toasts).toHaveLength(2);
expect(toasts.map((t) => t.detail)).toEqual(["clone failed", "upload failed"]);
});
});
+48
View File
@@ -0,0 +1,48 @@
import { useEffect } from "react";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { useAppState } from "../store/appState";
/** `project_commands.rs`'s `workspace-warning`: an AWS workspace started, but
* its clone or setup script failed (or was still running after 35 minutes).
* `detail` carries the failed repos and the setup log's last lines. */
export interface WorkspaceWarningEvent {
project_id: string;
message: string;
detail: string;
/** Set when the warning is about something else than the start's own
* follow (`"ports"`, `"connect"`): keeps it from replacing that toast. */
topic?: string;
}
/**
* App-wide: show `workspace-warning` as a warning toast. The start command
* returns the stored project, so the start's warning arrives as this event
* rather than in its result; one app-wide listener (not one per sidebar row)
* shows it once, whichever surface started the project.
*/
export function useWorkspaceWarnings() {
useEffect(() => {
let cancelled = false;
let unlisten: UnlistenFn | null = null;
listen<WorkspaceWarningEvent>("workspace-warning", (event) => {
const { project_id, message, detail, topic } = event.payload;
useAppState.getState().pushToast({
kind: "warning",
message,
detail,
dedupeKey: topic
? `workspace-warning-${project_id}-${topic}`
: `workspace-warning-${project_id}`,
});
})
.then((fn) => {
if (cancelled) fn();
else unlisten = fn;
})
.catch((e) => console.error("Failed to listen for workspace warnings:", e));
return () => {
cancelled = true;
unlisten?.();
};
}, []);
}
+46
View File
@@ -1,7 +1,11 @@
import { describe, it, expect } from "vitest";
import {
cleanRepos,
defaultAwsWorkspace,
hasSavedFiles,
hasWorkspaceFiles,
environmentSelectable,
gatewaySidecarProblem,
memoryError,
memoryOptions,
nearestMemory,
@@ -59,3 +63,45 @@ describe("awsWorkspace", () => {
expect(environmentSelectable(environment("a", "deploying", true))).toBe(false);
});
});
describe("phase 6 settings", () => {
it("defaults mirror Rust", () => {
const d = defaultAwsWorkspace();
expect([d.secrets_delivery, d.setup_script, d.gateway_sidecar, d.workspace_access_point_id]).toEqual([
"secrets_manager",
null,
false,
null,
]);
});
it("EFS mode and a leftover workspace access point count as saved files", () => {
const aws = defaultAwsWorkspace();
expect(hasSavedFiles({ aws: { ...aws, storage: "efs" } })).toBe(true);
expect(hasSavedFiles({ aws: { ...aws, storage: "ephemeral", workspace_access_point_id: "fsap-w" } })).toBe(true);
expect(hasSavedFiles({ aws: { ...aws, storage: "ephemeral" } })).toBe(false);
});
it("knows when /workspace files may be on EFS", () => {
const aws = defaultAwsWorkspace();
expect(hasWorkspaceFiles({ aws: { ...aws, storage: "efs" } })).toBe(true);
expect(hasWorkspaceFiles({ aws: { ...aws, storage: "hybrid", workspace_access_point_id: "fsap-w" } })).toBe(true);
expect(hasWorkspaceFiles({ aws: { ...aws, storage: "hybrid" } })).toBe(false);
expect(hasWorkspaceFiles({ aws: null })).toBe(false);
});
});
describe("gatewaySidecarProblem", () => {
const models = { models: [{ name: "gpt", model_id: "gpt-5" }] };
const aws = defaultAwsWorkspace();
it("needs the OpenAI-compatible backend, a complete model and 1 vCPU / 2 GiB", () => {
expect(gatewaySidecarProblem({ backend: "open_ai_compatible", aws }, models)).toBeNull();
expect(gatewaySidecarProblem({ backend: "anthropic", aws }, models)).toMatch(/OpenAI Compatible/);
expect(gatewaySidecarProblem({ backend: "open_ai_compatible", aws }, null)).toMatch(/Model Gateway/);
expect(
gatewaySidecarProblem({ backend: "open_ai_compatible", aws }, { models: [{ name: "gpt", model_id: " " }] }),
).toMatch(/Model Gateway/);
expect(
gatewaySidecarProblem({ backend: "open_ai_compatible", aws: { ...aws, cpu: 512, memory_mib: 2048 } }, models),
).toMatch(/1 vCPU and 2 GiB/);
});
});
+90 -1
View File
@@ -1,6 +1,7 @@
import type {
AwsWorkspaceConfig,
DetectedEnvironment,
GatewaySettings,
Project,
RepoSource,
UnsavedWork,
@@ -8,6 +9,8 @@ import type {
/** Why a Local-only option is off on an AWS project (P6). */
export const NOT_ON_AWS = "Not available on AWS (Fargate)";
/** Q6: an AWS project's port mappings bind loopback only (Docker's bind 0.0.0.0). */
export const AWS_PORTS_LOCALHOST_ONLY = "Reachable only from this machine (localhost)";
/** R4: the AWS host forces Claude Code's sandbox off until it is verified on Fargate. */
export const SANDBOX_OFF_ON_AWS = "Off on AWS for now";
export const EPHEMERAL_WARNING =
@@ -83,9 +86,13 @@ export function defaultAwsWorkspace(): AwsWorkspaceConfig {
idle_stop_minutes: null,
repos: [],
wip_push_on_stop: true,
secrets_delivery: "secrets_manager",
setup_script: null,
gateway_sidecar: false,
task_arn: null,
access_point_id: null,
task_definition_arn: null,
workspace_access_point_id: null,
};
}
@@ -121,9 +128,91 @@ export function environmentSelectable(env: DetectedEnvironment): boolean {
/** Whether an AWS project may have saved files on EFS (Hybrid now, or an
* access point left from when it was). Ephemeral-only projects have none. */
export function hasSavedFiles(p: Pick<Project, "aws">): boolean {
return p.aws?.storage === "hybrid" || !!p.aws?.access_point_id;
const a = p.aws;
return a?.storage === "hybrid" || a?.storage === "efs" || !!a?.access_point_id || !!a?.workspace_access_point_id;
}
/** Whether an AWS project may have `/workspace` files on EFS (EFS storage now,
* or its access point left from when it was). */
export function hasWorkspaceFiles(p: Pick<Project, "aws">): boolean {
const a = p.aws;
return a?.storage === "efs" || !!a?.workspace_access_point_id;
}
/** Shown with EFS storage selected. */
export const EFS_HINT = "Everything in /workspace survives a stop; git and builds are slower over NFS";
/** Copy-at-connect (phase 6 Q2): what it means, in HostSection. */
export const COPY_AT_CONNECT_HINT =
"API keys, the Git token, custom environment variables and the SSH and CA files are not stored in AWS: Triple-C copies them into the running workspace each time it connects.";
export const COPY_AT_CONNECT_WARNINGS = [
"Private repositories clone, and the setup script runs, after you connect (or after 5 minutes without).",
"Scheduled jobs and the setup script never get the API keys or custom variables; Git and SSH work once the app has connected.",
"Until the first connect the workspace has no corporate CA: behind a TLS-inspecting proxy, Claude Code's self-update may fail until then.",
"SSH keys, a Git token or variables you remove from the project stay in a running workspace until it stops.",
"The setup script is not run again when a later connect re-clones a repository that failed.",
];
/** Mirrors Rust `MAX_SETUP_SCRIPT_BYTES` (UTF-8 bytes). */
export const MAX_SETUP_SCRIPT_BYTES = 32_768;
/** Why `script` can't be saved, or null. Mirrors (and words like) Rust
* `validate_aws_workspace`'s setup-script checks. */
export function setupScriptProblem(script: string): string | null {
const bytes = new TextEncoder().encode(script).length;
if (bytes > MAX_SETUP_SCRIPT_BYTES) {
return `The setup script is ${bytes} bytes; the limit is ${MAX_SETUP_SCRIPT_BYTES}`;
}
if (script.includes("\0")) return "The setup script must not contain NUL characters";
return null;
}
/** What the setup script is, under its editor (phase 6 Q3). */
export const SETUP_SCRIPT_HINT =
"Runs on every start, as claude, with bash -e in /workspace once the repositories are cloned. It is stopped after 30 minutes, and its output shows in the Overview's start-up card. claude can use sudo (apt-get install …).";
export const SETUP_SCRIPT_COPY_AT_CONNECT_NOTE =
"With credentials copied when you connect, the setup script waits for the app's first connect (at most 5 minutes) and never gets the API keys or custom variables.";
/** Hybrid/EFS under copy-at-connect (ruling R6): ~/.ssh stays off EFS. */
export const COPY_AT_CONNECT_SSH_NOTE =
"~/.ssh is not kept on EFS: SSH keys copied in, or made in the workspace, are gone when it stops. Keys an earlier start left in /mnt/triple-c-home/.ssh stay there until you remove them.";
export const COPY_AT_CONNECT_GATEWAY_NOTE =
"The LLM gateway's keys are still stored in AWS: the gateway starts before Triple-C connects. So is Claude Code's token for it (ANTHROPIC_AUTH_TOKEN), which scheduled jobs need.";
/** The LLM gateway sidecar (phase 6 Q7), in ModelSection. */
export const GATEWAY_SIDECAR_LABEL = "Run Triple-C's LLM gateway in the workspace";
export const GATEWAY_SIDECAR_HINT =
"Runs the gateway from Settings → Model Gateway (same models, provider and API key) next to the workspace, on its own localhost:4000. Claude Code uses it instead of the base URL and API key below; set the model to one of the gateway's model names.";
export const GATEWAY_SIDECAR_ON_NOTE =
"Claude Code talks to the gateway in the workspace (http://127.0.0.1:4000) with a key made for each start. The provider API key is stored in the project's AWS secret, whichever way credentials are delivered.";
/** Mirrors Rust `validate_aws_workspace`'s sidecar minimum. */
export const GATEWAY_SIDECAR_MIN_CPU = 1024;
export const GATEWAY_SIDECAR_MIN_MEMORY_MIB = 2048;
/**
* Why `project` can't turn the gateway sidecar on, or null. Mirrors Rust
* `gateway_sidecar_problem` (OpenAI-compatible backend, a complete gateway
* model) plus the 1 vCPU / 2 GiB minimum.
*/
export function gatewaySidecarProblem(
project: Pick<Project, "backend" | "aws">,
gateway: Pick<GatewaySettings, "models"> | null,
): string | null {
if (project.backend !== "open_ai_compatible") {
return "The LLM gateway sidecar needs the OpenAI Compatible backend.";
}
const models = (gateway?.models ?? []).filter((m) => m.name.trim() && m.model_id.trim());
if (models.length === 0) {
return "Add at least one model in Settings → Model Gateway first (a name and a provider model id).";
}
const aws = project.aws;
if (aws && (aws.cpu < GATEWAY_SIDECAR_MIN_CPU || aws.memory_mib < GATEWAY_SIDECAR_MIN_MEMORY_MIB)) {
return "The LLM gateway sidecar needs at least 1 vCPU and 2 GiB: raise them in the Host section.";
}
return null;
}
export const SECRETS_MANAGER_HINT =
"API keys, the Git token, custom environment variables and the SSH and CA files are kept in the project's Secrets Manager secret, so the workspace has them from the moment it starts.";
/** Hosts that only mean something on this machine or its Docker bridge. */
const LOCAL_HOSTS = /^(localhost|127(\.\d{1,3}){3}|0\.0\.0\.0|\[?::1\]?|host\.docker\.internal|172\.17\.0\.1)$/i;
+48
View File
@@ -0,0 +1,48 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
const awsEnvironmentMaxHours = vi.fn(async (_id: string) => 24 as number | null);
vi.mock("./tauri-commands", () => ({
awsEnvironmentMaxHours: (id: string) => awsEnvironmentMaxHours(id),
}));
import {
MAX_HOURS_TTL_MS,
clearEnvironmentMaxHoursCache,
environmentMaxHours,
} from "./environmentMaxHours";
describe("environmentMaxHours", () => {
beforeEach(() => {
awsEnvironmentMaxHours.mockClear();
clearEnvironmentMaxHoursCache();
});
it("asks once per connection, region and environment until the TTL passes", async () => {
expect(await environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0)).toBe(24);
// Another project in the same environment shares the read.
expect(await environmentMaxHours("p2", "c1", "eu-west-1", "dev", 1000)).toBe(24);
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(1);
await environmentMaxHours("p1", "c2", "eu-west-1", "dev", 1000);
await environmentMaxHours("p1", "c1", "us-east-1", "dev", 1000);
await environmentMaxHours("p1", "c1", "eu-west-1", "prod", 1000);
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(4);
await environmentMaxHours("p1", "c1", "eu-west-1", "dev", MAX_HOURS_TTL_MS);
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(5);
});
it("shares a read in flight and keeps an unreadable record (null)", async () => {
awsEnvironmentMaxHours.mockResolvedValueOnce(null);
const a = environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0);
const b = environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0);
expect(await a).toBeNull();
expect(await b).toBeNull();
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(1);
});
it("does not keep a rejected read", async () => {
awsEnvironmentMaxHours.mockRejectedValueOnce(new Error("Project p1 not found"));
await expect(environmentMaxHours("p1", "c1", "eu-west-1", "dev", 0)).rejects.toThrow();
expect(await environmentMaxHours("p1", "c1", "eu-west-1", "dev", 1)).toBe(24);
expect(awsEnvironmentMaxHours).toHaveBeenCalledTimes(2);
});
});
+45
View File
@@ -0,0 +1,45 @@
import * as commands from "./tauri-commands";
/** How long an environment's lifetime cap is reused before AWS is asked again. */
export const MAX_HOURS_TTL_MS = 10 * 60 * 1000;
interface Entry {
at: number;
value: Promise<number | null>;
}
/** Keyed by connection, region and environment: the record is the
* environment's, so every project placed there shares one read. */
const cache = new Map<string, Entry>();
/**
* `aws_environment_max_hours`, cached per (connection, region, environment)
* for [`MAX_HOURS_TTL_MS`], so re-mounting Host settings does not read the
* environment record from AWS again. A read in flight is shared; a rejected
* one is not kept (the next mount asks again).
*/
export function environmentMaxHours(
projectId: string,
connectionId: string,
region: string,
environment: string,
now: number = Date.now(),
): Promise<number | null> {
const key = JSON.stringify([connectionId, region, environment]);
const hit = cache.get(key);
if (hit && now - hit.at < MAX_HOURS_TTL_MS) return hit.value;
const value = Promise.resolve()
.then(() => commands.awsEnvironmentMaxHours(projectId))
.then((h) => h ?? null);
const entry = { at: now, value };
cache.set(key, entry);
value.catch(() => {
if (cache.get(key) === entry) cache.delete(key);
});
return value;
}
/** For tests. */
export function clearEnvironmentMaxHoursCache() {
cache.clear();
}
+46 -9
View File
@@ -1,5 +1,5 @@
import { invoke } from "@tauri-apps/api/core";
import type { Project, ProjectPath, NewProjectHost, RemoveProjectOptions, WipChoice, UnsavedWork, ProjectRemovalReport, ProjectResetOutcome, ContainerInfo, AppSettings, SettingsImportPreview, SettingsImportOutcome, UpdateInfo, ImageUpdateInfo, FileEntry, FileContents, WebTerminalInfo, SttStatus, GatewayStatus, InstallOptions, ClaudeSession, ContainerCapabilities, ScheduledTask, ScheduledTaskInput, SchedulerNotification, AuthBridgeStatus, BrowserViewStatus, BrowserViewPopoutState, BrowserPageState, PlaywrightDetection, BrowserSetupOutcome, BrowserInstallTarget, ContainerStaleness, MigrationOptions, MigrationReport, MigrationState, ClearTokenOutcome, CaCertInfo, UploadOutcome, Note, ViewerFile, ViewerPoll, ViewerSaved, ViewerState, FileDiff, InstallScope, ItemUpdate, Marketplace, MarketplaceAccount, MarketplaceItemRef, MarketplaceSnapshot, ProjectSyncResult, SyncReport, HostConnection, CallerIdentity, PermissionReport, PolicyFile, Tier, EnvironmentScan, NetworkChoice, ModuleKind } from "./types";
import type { Project, ProjectPath, NewProjectHost, RemoveProjectOptions, WipChoice, UnsavedWork, WorkspaceStatus, ProjectRemovalReport, ProjectResetOutcome, ContainerInfo, AppSettings, SettingsImportPreview, SettingsImportOutcome, UpdateInfo, ImageUpdateInfo, FileEntry, FileContents, WebTerminalInfo, SttStatus, GatewayStatus, InstallOptions, ClaudeSession, ContainerCapabilities, ScheduledTask, ScheduledTaskInput, SchedulerNotification, AuthBridgeStatus, BrowserViewStatus, BrowserViewPopoutState, BrowserPageState, PlaywrightDetection, BrowserSetupOutcome, BrowserInstallTarget, ContainerStaleness, MigrationOptions, MigrationReport, MigrationState, ClearTokenOutcome, CaCertInfo, UploadOutcome, Note, ViewerFile, ViewerPoll, ViewerSaved, ViewerState, FileDiff, InstallScope, ItemUpdate, Marketplace, MarketplaceAccount, MarketplaceItemRef, MarketplaceSnapshot, ProjectSyncResult, SyncReport, HostConnection, CallerIdentity, PermissionReport, PolicyFile, HourlyEstimate, AwsWorkspaceConfig, Tier, EnvironmentScan, NetworkChoice, ModuleKind } from "./types";
// Docker
export const checkDocker = () => invoke<boolean>("check_docker");
@@ -37,6 +37,15 @@ export const checkUnsavedWork = (projectId: string) =>
invoke<UnsavedWork>("check_unsaved_work", { projectId });
export const awsTaskLog = (projectId: string, limit: number) =>
invoke<string[]>("aws_task_log", { projectId, limit });
/** An AWS workspace's clone/setup status; null for a local project, no
* tunnel, or an image without the reader. A background read (never keeps an
* idle workspace up). */
export const awsWorkspaceStatus = (projectId: string) =>
invoke<WorkspaceStatus | null>("aws_workspace_status", { projectId });
/** The project's environment's advisory lifetime cap in hours (0 = none);
* null when it can't be read. Never rejects for a missing project. */
export const awsEnvironmentMaxHours = (projectId: string) =>
invoke<number | null>("aws_environment_max_hours", { projectId });
export const removeAwsResources = (projectId: string, discardData: boolean) =>
invoke<Project>("remove_aws_resources", { projectId, discardData });
export const rebuildProjectContainer = (projectId: string) =>
@@ -100,6 +109,11 @@ export const checkAwsPermissions = (id: string, region?: string) =>
/** Every policy file `tier` needs, in order: attach all of them. */
export const awsPolicyDocument = (tier: Tier) =>
invoke<PolicyFile[]>("aws_policy_document", { tier });
/** The estimated hourly cost of `config` on Fargate in `region`, signed with the project's
* connection. Never rejects for AWS reasons: `null` means "Estimate unavailable" (denied, offline,
* no connection). Rates are cached a day on disk. */
export const awsCostEstimate = (connectionId: string, region: string, config: AwsWorkspaceConfig) =>
invoke<HourlyEstimate | null>("aws_cost_estimate", { connectionId, region, config });
// AWS environments (OpenTofu). Reject with an `AwsError`. Starting commands (and follow) resolve
// to an op id; progress arrives on the `AWS_DEPLOY_PROGRESS_EVENT` event (`DeployProgress`, in
@@ -109,13 +123,35 @@ export const awsPolicyDocument = (tier: Tier) =>
// after a reload) replays the whole log under a new op id.
export const detectAwsEnvironments = (connectionId: string, region: string) =>
invoke<EnvironmentScan>("detect_aws_environments", { connectionId, region });
export const deployAwsEnvironment = (connectionId: string, region: string, name: string, network: NetworkChoice) =>
invoke<string>("deploy_aws_environment", { connectionId, region, name, network });
export const updateAwsEnvironment = (connectionId: string, region: string, name: string) =>
invoke<string>("update_aws_environment", { connectionId, region, name });
/** `network` is required only when the environment has no record. */
export const repairAwsEnvironment = (connectionId: string, region: string, name: string, network?: NetworkChoice) =>
invoke<string>("repair_aws_environment", { connectionId, region, name, network: network ?? null });
export const deployAwsEnvironment = (
connectionId: string,
region: string,
name: string,
network: NetworkChoice,
maxWorkspaceHours = 0,
) => invoke<string>("deploy_aws_environment", { connectionId, region, name, network, maxWorkspaceHours });
/** Omit `maxWorkspaceHours` to keep the environment's recorded cap. */
export const updateAwsEnvironment = (connectionId: string, region: string, name: string, maxWorkspaceHours?: number) =>
invoke<string>(
"update_aws_environment",
maxWorkspaceHours === undefined
? { connectionId, region, name }
: { connectionId, region, name, maxWorkspaceHours },
);
/** `network` is required only when the environment has no record; a record's cap is kept unless `maxWorkspaceHours` is given. */
export const repairAwsEnvironment = (
connectionId: string,
region: string,
name: string,
network?: NetworkChoice,
maxWorkspaceHours?: number,
) =>
invoke<string>(
"repair_aws_environment",
maxWorkspaceHours === undefined
? { connectionId, region, name, network: network ?? null }
: { connectionId, region, name, network: network ?? null, maxWorkspaceHours },
);
export const destroyIncompleteAwsEnvironment = (connectionId: string, region: string, name: string) =>
invoke<string>("destroy_incomplete_aws_environment", { connectionId, region, name });
export const unlockAwsState = (connectionId: string, region: string, stateKey: string) =>
@@ -136,7 +172,7 @@ export const saveAwsAdminBundle = (
connectionId: string,
region: string,
kind: ModuleKind,
options: { environment?: string; ownerId?: string; network?: NetworkChoice } = {},
options: { environment?: string; ownerId?: string; network?: NetworkChoice; maxWorkspaceHours?: number } = {},
) =>
invoke<string | null>("save_aws_admin_bundle", {
connectionId,
@@ -145,6 +181,7 @@ export const saveAwsAdminBundle = (
environment: options.environment ?? null,
ownerId: options.ownerId ?? null,
network: options.network ?? null,
...(options.maxWorkspaceHours === undefined ? {} : { maxWorkspaceHours: options.maxWorkspaceHours }),
});
/** Check a corporate CA path and report what would be installed. Never
* rejects for a bad path — the reason comes back in `error`. */
+39 -1
View File
@@ -82,7 +82,8 @@ export interface AwsPlacement {
}
export type Arch = "x86_64" | "arm64";
export type StorageMode = "ephemeral" | "hybrid";
export type StorageMode = "ephemeral" | "hybrid" | "efs";
export type SecretsDelivery = "secrets_manager" | "copy_at_connect";
export interface RepoSource {
url: string;
@@ -101,10 +102,16 @@ export interface AwsWorkspaceConfig {
idle_stop_minutes: number | null;
repos: RepoSource[];
wip_push_on_stop: boolean;
secrets_delivery: SecretsDelivery;
/** Run as `claude` after the clone; null = none. At most 32768 bytes. */
setup_script: string | null;
/** Run the LLM gateway as a sidecar (needs 1 vCPU and 2 GiB). */
gateway_sidecar: boolean;
/** Store-owned runtime handles; a save cannot change them. */
task_arn: string | null;
access_point_id: string | null;
task_definition_arn: string | null;
workspace_access_point_id: string | null;
}
/** Where a new project runs: omitted (or all null) = local Docker. An AWS
@@ -148,6 +155,25 @@ export interface UnsavedWork {
error: string | null;
}
/** An AWS workspace's background clone and setup script (Rust
* `host::aws_host::WorkspaceStatus`, the shape `triple-c-workspace-status`
* prints). */
export interface WorkspaceStatus {
clone: {
/** Every repo has been tried. */
done: boolean;
/** The repos tried so far; `state` is cloning, cloned, failed or exists. */
repos: { folder: string; state: string }[];
};
setup: {
state: "none" | "running" | "ok" | "failed";
/** 0 when ok; the exit code when failed, if known. */
code: number | null;
/** The setup log's last lines. */
tail: string[];
};
}
export type ProjectStatus =
| "stopped"
| "starting"
@@ -1242,6 +1268,16 @@ export interface PolicyFile {
json: string;
}
/** `aws::pricing::HourlyEstimate`: the on-demand price of an AWS project's
* Fargate task per hour while it runs (a gateway sidecar adds nothing). */
export interface HourlyEstimate {
usd_per_hour: number;
/** Spot is on: the Price List has no Spot rates, so say it is usually cheaper. */
note_spot: boolean;
/** Hybrid or EFS storage: EFS is billed per GB-month, not in this figure. */
efs_billed_separately: boolean;
}
export type Verdict = "allowed" | "denied" | "unverified";
export interface ActionResult {
@@ -1292,6 +1328,8 @@ export interface EnvironmentInfo {
log_group: string;
network_mode: "public" | "private-nat" | "existing";
tenancy: string;
/** Advisory workspace lifetime cap in hours; 0 = none (absent in v1 records). */
max_workspace_hours: number;
}
/** A held state lock. `who`/`created`/`operation` are sanitized untrusted text. */
+1 -1
View File
@@ -98,7 +98,7 @@ function activation(activeTabKey: string | null) {
};
}
export type ToastKind = "error" | "success" | "info";
export type ToastKind = "error" | "warning" | "success" | "info";
export interface Toast {
id: string;
+66 -7
View File
@@ -7,7 +7,7 @@ tier the signed-in identity has under **Settings → Hosts → Check**.
| File | What it is |
|---|---|
| `policies/workspace-user.json` | The **workspace user** tier, part 1: create, start, stop, connect to and delete **their own** workspaces in an existing environment |
| `policies/workspace-user-tasks.json` | The **workspace user** tier, part 2: register and tag **their own** ECS task definitions, and read **their own** secrets (`workspace-user.json` has no room left) |
| `policies/workspace-user-tasks.json` | The **workspace user** tier, part 2: register and tag **their own** ECS task definitions, read **their own** secrets, and read Fargate prices for the cost estimate (`workspace-user.json` has no room left) |
| `policies/environment-admin.json` | The **environment admin** tier, part 1: apply and destroy the environment and onboarding modules (`terraform/`) with OpenTofu, and keep their state |
| `policies/environment-admin-network.json` | The **environment admin** tier, part 2: the network those modules create (VPC, subnets, gateways, routes, security groups) |
| `policies/permissions-boundary.json` | The **permissions boundary** every Triple-C role must carry. It is never attached to a person |
@@ -117,6 +117,12 @@ following:
roles, add those roles' ARNs or path prefixes (for example `arn:aws:iam::*:role/engineers/*`)
to `InspectOwnPrincipal` in both tier policies. Everything matched there can be read and
simulated by every holder of the policy, so keep it to the sign-in roles.
- **Read Fargate prices** (`workspace-user-tasks.json`, `ReadFargatePrices`). `pricing:GetProducts`
on `*`: the Price List API has no resource types or condition keys, and prices are public. The
app asks it (in us-east-1, service `AmazonECS`) for the workspace region's on-demand Fargate
vCPU, memory and ephemeral-storage rates, caches them for a day and shows an estimated hourly
cost in the project's Host settings and Overview. Without this permission the estimate reads
"Estimate unavailable"; nothing else depends on it.
- **Read environment information.** This covers ECS, the `/triple-c/*` SSM parameters (the
environment records), the secret list, EFS, and the `/triple-c/*` log groups. Listing is not a
secret; the app filters by owner for display. A workspace user sees only environments that have
@@ -283,9 +289,18 @@ it is unpacked), and an admin can run the very same files by hand from a bundle.
| Module | Creates | State key |
|---|---|---|
| `terraform/permissions-boundary` | the account's `triple-c-permissions-boundary` policy (an account administrator applies it; the admin tier cannot) | `boundary/terraform.tfstate` |
| `terraform/environment` | what every user shares: ECS cluster (Fargate, Fargate Spot), the tasks security group (no inbound rules), KMS key and alias, log group `/triple-c/<env>`, the network (`network_mode`: `public`, `private-nat`, `existing`) and, last, the record `/triple-c/environments/<env>` | `environments/<env>/terraform.tfstate` |
| `terraform/environment` | what every user shares: ECS cluster (Fargate, Fargate Spot), the tasks security group (no inbound rules), KMS key and alias, log group `/triple-c/<env>`, the network (`network_mode`: `public`, `private-nat`, `existing`) and, last, the record `/triple-c/environments/<env>` (which carries the advisory `max_workspace_hours` lifetime cap, 0 = none, at most 168) | `environments/<env>/terraform.tfstate` |
| `terraform/user-onboard` | one user's task and execution roles (with the boundary) and their own EFS file system, its `-efs` security group, two mount targets and a file-system policy that admits only their task role | `users/<env>/<owner key>/terraform.tfstate` |
**Workspace lifetime cap (advisory).** `max_workspace_hours` (module variable, 0 to 168, default 0 =
no cap) is stored in the environment record and passed to every workspace as
`TRIPLEC_MAX_LIFETIME_MINUTES`; the workspace's idle watcher then pushes work in progress (best
effort, 90 s) and stops the task, which ECS reports as exit code 76. It is **not a security
control**: a workspace user has sudo inside their own task and can switch the watcher off. To
enforce a limit, stop old tasks from outside (for example a scheduled Lambda; not built). Changing
the cap is a module update (template version 2), so environments deployed before it show "Update
available" in the app.
The **owner key** is the first 12 hex characters of `sha256("<region>:<aws:userid>")`; it names
the user's roles, `triple-c-<env>-<owner key>-task` and `-exec` (a `UserId` holds a `:`, which
role names reject). The region is in it because IAM role names are account-wide while
@@ -394,10 +409,10 @@ Create each file as a **customer managed policy** and attach it. In the console,
**IAM → Policies → Create policy → JSON**, paste the file, and attach the policy to a user, group
or role. Inline policies do not fit: a role's inline policies share 10,240 characters and an
environment admin needs every tier file (about 16,400), and a user's share 2,048. A managed policy
may hold 6,144 characters, not counting whitespace. Without whitespace, `workspace-user.json` is
about 6,040 (roughly 100 characters of headroom), `workspace-user-tasks.json` about 1,200,
`environment-admin.json` about 5,800 (340)
and `environment-admin-network.json` about 3,340; a unit test (`every_policy_fits_a_managed_policy`)
may hold 6,144 characters, not counting whitespace. Measured 2026-11-16 without whitespace, `workspace-user.json` is
6,036 (108 characters of headroom), `workspace-user-tasks.json` 1,293 (including `ReadFargatePrices`),
`environment-admin.json` 5,805 (339)
and `environment-admin-network.json` 3,340; a unit test (`every_policy_fits_a_managed_policy`)
fails before any file outgrows the limit. A tier that outgrows its files gains another file, which
the app then lists with the others. For IAM Identity Center, add the files as inline policies on the permission set
(the "up to 32,768 characters" limit is unverified) or reference the managed policies.
@@ -540,7 +555,7 @@ progress events) is not exercised.
| EFS file-system policy naming a just-created role | ✓ no invalid-principal error in any onboarding apply (two applies, each created in 4 s, right after the role; whether the provider retried internally is not visible without its debug log); no module change. If it ever fails, applying the onboarding again completes it |
| NAT address on destroy | ✗→fixed: the provider disassociates the address before releasing it, after the NAT gateway (and the association) are gone, and EC2 authorizes that call against the literal `*/*`. `DisassociateGoneAddresses` allows exactly `arn:aws:ec2:*:*:${*}/${*}`; CloudTrail shows the retried call reaching `InvalidAssociationID.NotFound` |
| Conditional unlock | ✓ on the versioned state bucket, `DeleteObject` with `If-Match` and a mismatched ETag gets 412 (the app's "lock changed" answer) and leaves the lock; with the current ETag it deletes it. A fresh foreign lock scans as Deploying with its `Who` |
| Update | ✓ re-applying with the network read back from the record: "0 added, 0 changed, 0 destroyed" |
| Update | ✓ re-applying with the network and lifetime cap read back from the record: "0 added, 0 changed, 0 destroyed" |
| After `tofu destroy` | ✗→fixed (app): the state object stays, empty, so a destroyed environment kept scanning as Incomplete (and a removed onboarding as Joined). Detection now reads small state objects (up to 2 KiB, at most 200 per scan) and treats one with no resources as absent; a read that fails counts as a deployment |
| Cleanup | ✓ nothing `triple-c` left in us-west-2 (roles, policies, log groups, clusters, VPCs, subnets, gateways, NAT gateways, addresses, security groups, file systems, parameters, aliases, buckets); five environment KMS keys pending deletion (7 days); the boundary, which the account did not have, deleted again |
@@ -601,3 +616,47 @@ and the secret and removes the ECR policy; then the fixture destroys the modules
| A revision per configuration | turning idle stop on adds a secret key, so the next start registers a new revision (by design) |
| Removal (R8) | ✓ final run: `remove_aws_resources` succeeds; the secret is scheduled for deletion, the access point deleted, the revision left ACTIVE. Since R10 the secret is deleted without a recovery window (the test now asserts it is gone) |
| Cleanup | ✓ nothing `triple-c` left in us-west-2; environment KMS keys pending deletion |
### Phase 6: the cost estimate's price reads (2026-11-16)
`cargo test aws::live_tests::a_ -- --ignored --test-threads=1` (the four permission-check tests
plus `a_workspace_role_reads_fargate_prices_and_a_bare_role_cannot`), us-west-2, all passed.
| Point | Result |
|---|---|
| `pricing:GetProducts` on `*` (`ReadFargatePrices`) | ✓ a role holding only the workspace files reads us-west-2's rates from the us-east-1 endpoint (`AmazonECS`; `TERM_MATCH regionCode`, `TERM_MATCH termType=OnDemand`, `CONTAINS usagetype=Fargate`: eight rows, one page). x86 $0.04048/vCPU-h, $0.004445/GB-h; ARM $0.03238, $0.00356; ephemeral storage $0.000111/GB-h |
| Without the permission | ✓ a role with no policy is denied (`AccessDeniedException` naming `pricing:GetProducts`); the app shows "Estimate unavailable" |
| The permission check | ✓ the simulator allows the new check for the workspace tier and denies it for a role with no policy |
### Phase 6: one workspace with every option (2026-11-16)
```sh
TRIPLEC_E2E_TEST=aws::live_tests::environment::a_phase6_workspace_round_trips \
TRIPLEC_AWS_E2E_PROFILE=<admin profile> TRIPLEC_AWS_E2E_REGION=us-west-2 scripts/aws-e2e-workspace.sh
```
Same fixture as phase 5 (`deploy_for_workspace_user`: environment admin deploys, a role holding
only the workspace files is onboarded and drives the app as a profile connection), with the
environment's `max_workspace_hours = 1`. One project, two task starts, 1 vCPU / 2 GiB x86: EFS
storage, one public repo, a setup script, copy-at-connect (a CA file, a git token and a custom
variable — all dummies), a port mapping 127.0.0.1:<free port> → 8099, the gateway sidecar
(OpenAI-compatible backend, one gateway model, a dummy provider key handed in instead of the
keychain via `aws_host::ensure_started_with`), idle stop off, WIP push off. Passed in 600 s,
spend under $0.05.
| Point | Result |
|---|---|
| Environment module v2 + cap | ✓ the record reads `template_version 2`, `max_workspace_hours 1` (as the workspace user); the secret has `TRIPLEC_MAX_LIFETIME_MINUTES=60`, PID 1's environment has it (root exec, that one line grepped), one `triple-c-idle-watch` runs with idle stop off |
| v1 → v2 Update | not exercised: the fixture applies only the embedded modules (unit-tested: an old record shows "Update available") |
| Lifetime cap firing | not exercised live (60 minutes; Task 4's unit tests) |
| Times | cold start (two access points, register, RunTask, pull of both images, agent, tunnel) 115 s; restart 112 s; copy-at-connect push 3 s; gateway `/health/liveliness` 200 at 126 s after the start began |
| EFS `/workspace` | ✓ `127.0.0.1:/ /workspace nfs4` (TLS tunnel); Hello-World cloned onto it; a marker written in task 1 was there in task 2; the clone reported `exists` the second time; both access point ids kept in the store across the restart |
| Hybrid links under EFS | ✓ `~/.claude` and `~/.claude.json` → `/mnt/triple-c-home/…`; `~/.ssh` is a local directory (R6, copy-at-connect) |
| Setup script | ✓ `sudo apt-get install -y jq` (already in the image: "jq is already the newest version"), `jq --version`, marker on `/workspace`; `aws_workspace_status`'s read (`triple-c-workspace-status` through the tunnel) settled with clone `cloned` + setup `ok` (code 0, log tail); same again on the restart |
| Copy-at-connect | ✓ secret keys listed (values never printed): no `TRIPLEC_FILES`, `GIT_TOKEN`, custom variable or `TRIPLE_C_CUSTOM_ENV`, `TRIPLEC_CREDENTIALS` present; neither dummy value anywhere in the secret; after the push `/usr/local/share/ca-certificates/triple-c-e2e-ca.crt` (`subject=CN = triple-c-e2e-ca`) and a git credential helper are set |
| Port mapping | ✓ `python3 -m http.server 8099 --bind 127.0.0.1` (detached exec); `port_forwards::open_for` → `GET /` on the host's loopback port: `HTTP/1.0 200 OK` |
| Gateway sidecar | ✓ from the workspace: `/health/liveliness` 200; `/v1/models` with the master key 200 listing `e2e-model`, without it 401; claude's login shell has `ANTHROPIC_BASE_URL=http://127.0.0.1:4000` and a token; the sidecar's `TRIPLEC_GATEWAY_*` stay in the secret (R1); a new master key each start |
| Keys in the logs | ✓ after a failed `/v1/chat/completions` (dummy key → OpenAI 401) and the successful calls: the provider key, the master key, the git token and the custom value are in **0** lines of the gateway stream (`<prefix>/gateway/<task-id>`, 107 lines) and of the workspace stream. LiteLLM logs the exception and traceback, not request headers; the provider's own error text (also in the HTTP response) carries the key **masked** by OpenAI (`sk-e2e-p…1249`: first 8 and last 4 characters) |
| Cost estimate | ✓ `fetch_rates` as the workspace user + `estimate`: $0.0495/hour for 1 vCPU / 2 GiB / 21 GiB x86, `efs_billed_separately` |
| Removal with discard | ✓ `remove_aws_resources(…, true)` Ok; both access points gone; the secret deleted |
| Cleanup | ✓ nothing `triple-c` left in us-west-2 (the run's environment KMS key pending deletion). The guard's task-definition retirement had never matched anything (ECS's `familyPrefix` is a whole family name); fixed, and 13 revisions earlier runs had left ACTIVE were deregistered and deleted |
+10
View File
@@ -61,6 +61,16 @@
}
}
},
{
"Sid": "ReadFargatePrices",
"Effect": "Allow",
"Action": [
"pricing:GetProducts"
],
"Resource": [
"*"
]
},
{
"Sid": "NeverReadEnvironmentSecrets",
"Effect": "Deny",
+13 -12
View File
@@ -3,7 +3,7 @@
locals {
# ENVIRONMENT_MODULE_VERSION in app/src-tauri/src/aws/tofu_modules.rs.
template_version = 1
template_version = 2
name = "triple-c-${var.environment_name}"
}
@@ -110,17 +110,18 @@ resource "aws_ssm_parameter" "environment" {
type = "String"
tier = "Standard"
value = jsonencode({
schema = 1
name = var.environment_name
template_version = local.template_version
cluster_arn = aws_ecs_cluster.this.arn
security_group_id = aws_security_group.tasks.id
subnet_ids = local.task_subnet_ids
assign_public_ip = local.assign_public_ip
kms_key_arn = aws_kms_key.this.arn
log_group = aws_cloudwatch_log_group.this.name
network_mode = var.network_mode
tenancy = "strict"
schema = 1
name = var.environment_name
template_version = local.template_version
cluster_arn = aws_ecs_cluster.this.arn
security_group_id = aws_security_group.tasks.id
subnet_ids = local.task_subnet_ids
assign_public_ip = local.assign_public_ip
kms_key_arn = aws_kms_key.this.arn
log_group = aws_cloudwatch_log_group.this.name
network_mode = var.network_mode
tenancy = "strict"
max_workspace_hours = var.max_workspace_hours
})
depends_on = [
+11
View File
@@ -94,3 +94,14 @@ variable "log_retention_days" {
error_message = "log_retention_days must be a CloudWatch Logs retention value (1, 3, 5, 7, 14, 30, 60, 90, ...)."
}
}
variable "max_workspace_hours" {
description = "Advisory cap on how long one workspace task may run, in hours (0 = no cap, at most 168). The app reads it from the environment record and enforces it inside the workspace."
type = number
default = 0
validation {
condition = var.max_workspace_hours == floor(var.max_workspace_hours) && var.max_workspace_hours >= 0 && var.max_workspace_hours <= 168
error_message = "max_workspace_hours must be a whole number from 0 to 168."
}
}
+12 -2
View File
@@ -475,6 +475,11 @@ COPY skills /opt/triple-c-skills
# without a script, which is a legitimate thing for a skill to do.
RUN find /opt/triple-c-skills -name '*.sh' -exec chmod +x {} +
# The entrypoint's shared functions (CA install, SSH merge, git credential
# helper, clone), sourced by it and by triple-c-apply-connect. Root-owned and
# not writable by the user: both run as root.
COPY entrypoint-lib.sh /usr/local/lib/triple-c/entrypoint-lib.sh
RUN chmod 755 /usr/local/lib/triple-c && chmod 644 /usr/local/lib/triple-c/entrypoint-lib.sh
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
COPY triple-c-scheduler /usr/local/bin/triple-c-scheduler
@@ -489,10 +494,15 @@ RUN chmod +x /usr/local/bin/triple-c-playwright-heal
COPY triple-c-task-runner /usr/local/bin/triple-c-task-runner
RUN chmod +x /usr/local/bin/triple-c-task-runner
# AWS workspaces only (entrypoint.sh, TRIPLEC_HOST=aws): the WIP push run on
# stop and the idle auto-stop watcher. Inert in a Docker container.
# stop, the idle auto-stop / lifetime-cap watcher, the start-up status
# (clone + setup script) the app polls and the copy-at-connect apply the app
# runs (as root) after each connect. Inert in a Docker container.
COPY triple-c-wip-push /usr/local/bin/triple-c-wip-push
COPY triple-c-idle-watch /usr/local/bin/triple-c-idle-watch
RUN chmod +x /usr/local/bin/triple-c-wip-push /usr/local/bin/triple-c-idle-watch
COPY triple-c-workspace-status /usr/local/bin/triple-c-workspace-status
COPY triple-c-apply-connect /usr/local/bin/triple-c-apply-connect
RUN chmod 755 /usr/local/bin/triple-c-wip-push /usr/local/bin/triple-c-idle-watch \
/usr/local/bin/triple-c-workspace-status /usr/local/bin/triple-c-apply-connect
# Root-owned in /usr/local/bin: entrypoint.sh starts it as root (only when a
# token is configured), and it rides the snapshot and migration like the other
# shims here.
+608
View File
@@ -0,0 +1,608 @@
#!/bin/bash
# Triple-C entrypoint library — sourced, never run.
#
# Installed at /usr/local/lib/triple-c/entrypoint-lib.sh (root, 0644) and
# sourced by both /usr/local/bin/entrypoint.sh (every container start, Docker
# and AWS) and /usr/local/bin/triple-c-apply-connect (an AWS workspace whose
# credentials are copied in when the app connects, phase 6 Q2), so the CA
# install, the SSH merge, the git credential helper and the clone are one
# piece of code for both. **Sourcing it only defines variables and functions;
# it runs nothing** — app/src-tauri/src/docker/aws_mode_tests.rs checks that.
#
# The `# >>> name` / `# <<< name` markers delimit blocks the Rust tests cut out
# and run under bash (aws_mode_tests.rs, image.rs's sync_host_ssh tests); keep
# each block self-contained.
# ── Shared settings and the drop to the container user ─────────────────────
# >>> aws_lib
triplec_run_as=claude
# Where Hybrid storage mounts the project's EFS access point.
triplec_home_mnt=/mnt/triple-c-home
# This run's record (aws_prepare_run in entrypoint.sh) and, under
# copy-at-connect, the marker triple-c-apply-connect leaves there.
triplec_run_dir=/run/triple-c
# Copy-at-connect: how long the background clone waits for the app's first
# connect to bring the credentials (aws_wait_connect).
triplec_connect_wait=300
# Runs a command as the container user (`claude`) when this script is root.
_triplec_as_user() {
if [ -n "${triplec_run_as:-}" ] && [ "$(id -u)" = 0 ]; then
su -s /bin/bash -c '"$@"' -- "$triplec_run_as" _ "$@"
else
"$@"
fi
}
# <<< aws_lib
# ── AWS Hybrid/EFS storage: persisted home state (plan ruling P4) ───────────
# EFS storage is Hybrid plus /workspace on a second access point (mounted by
# the task definition straight at /workspace), so it gets the same home links.
# A Hybrid workspace mounts its EFS access point (POSIX 1000:1000) at
# /mnt/triple-c-home. Mounting it over /home/claude would hide the image's
# Claude Code, cargo and uv — EFS has no Docker-volume "copy the image's
# contents on first mount" — so only user state is persisted, by symlink:
# ~/.claude, ~/.claude.json and ~/.ssh. On first use each is seeded from the
# image's copy (or created empty); from then on the persisted copy wins and
# the fresh image copy every new task starts with is replaced by the link —
# what a Docker home volume does by masking. Runs before the SSH merge below,
# so host keys land on EFS, and before anything writes ~/.claude.
# >>> aws_home
link_persistent_home() {
local home="$1" persist="$2" owner="$3" name link target names=".claude .claude.json .ssh"
# Copy-at-connect (ruling R6): ~/.ssh stays in the task's own storage,
# so the keys copied in at connect vanish with the task and never reach
# EFS. (Keys made in the workspace then don't persist either; a .ssh
# already on EFS from an earlier Secrets Manager period is left alone.)
[ "${triplec_connect:-0}" = 1 ] && names=".claude .claude.json"
if [ ! -d "$persist" ] || [ -L "$persist" ]; then
echo "entrypoint: warning — Hybrid storage, but $persist is not mounted; home state will not persist"
return 0
fi
# The access point already makes its root 1000:1000; said again here so
# claude (Claude Code's own atomic saves, write_claude_json) can always
# create files beside the persisted ones.
[ -z "$owner" ] || chown "$owner" "$persist" 2>/dev/null
for name in $names; do
link="$home/$name"
target="$persist/$name"
if [ -L "$link" ] && [ "$(readlink "$link")" = "$target" ]; then
continue
fi
if [ ! -e "$target" ] && [ ! -L "$target" ]; then
if [ -e "$link" ] || [ -L "$link" ]; then
# Staged then renamed: a copy cut short must never look seeded.
rm -rf "$target.triple-c-seed"
if ! cp -a "$link" "$target.triple-c-seed" || ! mv "$target.triple-c-seed" "$target"; then
rm -rf "$target.triple-c-seed"
echo "entrypoint: warning — could not seed $target; ~/$name stays unpersisted"
continue
fi
elif [ "$name" = .claude.json ]; then
( umask 077; printf '{}\n' > "$target" ) || {
echo "entrypoint: warning — could not create $target"; continue; }
else
mkdir -m 700 "$target" || {
echo "entrypoint: warning — could not create $target"; continue; }
fi
[ -z "$owner" ] || chown -R "$owner" "$target" 2>/dev/null
fi
rm -rf "$link"
ln -s "$target" "$link" || echo "entrypoint: warning — could not link ~/$name to $target"
[ -z "$owner" ] || chown -h "$owner" "$link" 2>/dev/null
done
if [ "${triplec_connect:-0}" = 1 ]; then
echo "entrypoint: Hybrid storage — ~/.claude and ~/.claude.json persist on $persist; ~/.ssh does not (credentials are copied in at connect)"
else
echo "entrypoint: Hybrid storage — ~/.claude, ~/.claude.json and ~/.ssh persist on $persist"
fi
}
# The real directory behind a Hybrid ~/.ssh link, printed only when the link
# is exactly the one link_persistent_home makes and neither it nor the mount
# is itself a symlink. Root chowns and chmods under the result, and the
# persisted directory is claude-writable, so root must never be pointed
# anywhere else by it.
aws_hybrid_ssh_dir() {
local home="$1" want="$triplec_home_mnt/.ssh"
[ "$(readlink "$home/.ssh")" = "$want" ] || return 1
[ ! -L "$triplec_home_mnt" ] && [ ! -L "$want" ] && [ -d "$want" ] || return 1
printf '%s' "$want"
}
# Whether storage mode $1 persists home state (Hybrid, and EFS = Hybrid +
# /workspace on EFS).
aws_storage_links_home() {
case "$1" in
hybrid|efs) return 0 ;;
esac
return 1
}
# EFS storage: /workspace is the access point itself (uid/gid 1000 enforced,
# root acts as 1000 there). Nothing here creates or chowns it; only says so
# when it is missing, since the files would then not survive a stop.
aws_efs_workspace_check() {
mountpoint -q "$1" 2>/dev/null \
|| echo "entrypoint: warning — EFS storage, but $1 is not mounted; its files will not persist"
return 0
}
# <<< aws_home
# ── Corporate CA certificates ───────────────────────────────────────────────
# The host's CA material is bind-mounted read-only at /tmp/.host-ca. Triple-C
# mounts a *directory* as-is and a *single file* as /tmp/.host-ca/<name>.crt,
# so this only ever has to deal with a directory (the file branch below is
# defensive).
#
# Runs before everything that touches the network — the git credential helper,
# ssh-keyscan, and especially the `claude update` at the bottom of entrypoint.sh,
# which is itself an HTTPS call that fails behind a TLS-terminating proxy
# without this.
#
# Two things are easy to get wrong here:
# 1. `update-ca-certificates` globs /usr/local/share/ca-certificates/*.crt
# case-sensitively. A `.pem` that is merely copied in is ignored in total
# silence, so certificates are *renamed*, not copied.
# 2. Chrome/Chromium read neither /etc/ssl nor $SSL_CERT_FILE; they have
# their own NSS database at ~/.pki/nssdb, seeded below with certutil.
#
# NODE_EXTRA_CA_CERTS / REQUESTS_CA_BUNDLE / SSL_CERT_FILE are deliberately NOT
# exported here. Every terminal is a separate `docker exec`, which inherits the
# container's configured env and sees nothing this script exported — the same
# reason $BROWSER had to become an image-level ENV. Triple-C sets them on the
# container at creation time instead. (entrypoint.sh forwards them into the cron
# environment file, because cron jobs start from a bare env.)
# >>> ca_install
CA_SRC="/tmp/.host-ca"
CA_STORE="/usr/local/share/ca-certificates"
CA_PREFIX="triple-c-"
CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"
CA_STAMP="/var/lib/triple-c/ca.stamp"
CA_NSSDB="/home/claude/.pki/nssdb"
# Mirror of `container_cert_name()` in app/src-tauri/src/docker/ca_certs.rs.
# The two must agree; the Rust side has the unit tests.
ca_normalise_name() {
local name stem
name=$(printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_')
while [ "${name#.}" != "$name" ]; do name="${name#.}"; done
stem="${name%.*}"
[ -z "$stem" ] && stem="corporate-ca"
printf '%s.crt' "$stem"
}
ca_source_files() {
if [ -d "$CA_SRC" ]; then
find "$CA_SRC" -maxdepth 1 -type f \
\( -iname '*.crt' -o -iname '*.pem' -o -iname '*.cer' \
-o -iname '*.cert' -o -iname '*.ca-bundle' \) 2>/dev/null | sort
elif [ -f "$CA_SRC" ]; then
printf '%s\n' "$CA_SRC"
fi
}
# Seed Chrome/Chromium's NSS database. Tolerant by design: a missing certutil
# or a broken profile must warn, never fail the container start.
# ~/.pki lives in the home volume, so this persists once done; the system store
# lives in the writable layer and is re-applied on every start.
ca_seed_nssdb() {
if ! command -v certutil >/dev/null 2>&1; then
echo "entrypoint: warning — certutil not found (install libnss3-tools); Chrome/Chromium in this container will not trust the corporate CA"
return 0
fi
su -s /bin/bash claude -c '
db="$HOME/.pki/nssdb"
mkdir -p "$db" || exit 1
if [ ! -f "$db/cert9.db" ]; then
certutil -d "sql:$db" -N --empty-password >/dev/null 2>&1 || exit 1
fi
for f in /usr/local/share/ca-certificates/triple-c-*.crt; do
[ -f "$f" ] || continue
nick="triple-c:$(basename "$f" .crt)"
# Delete first so re-running replaces rather than duplicates.
certutil -d "sql:$db" -D -n "$nick" >/dev/null 2>&1
certutil -d "sql:$db" -A -t "C,," -n "$nick" -i "$f" >/dev/null 2>&1 \
|| echo "entrypoint: warning — certutil could not add $nick"
done
' && echo "entrypoint: seeded Chrome/Chromium NSS database with the corporate CA" \
|| echo "entrypoint: warning — NSS database seeding failed (continuing)"
}
install_corporate_ca() {
local files fp stamp f base name count installed
files=$(ca_source_files)
if [ -z "$files" ]; then
# Nothing configured — but /usr/local/share is in the writable layer and
# `docker commit` bakes it into the project's snapshot image, so a cert
# installed by a previous configuration would ride that snapshot into
# every future container. Turning the setting off has to actively undo.
if ls "$CA_STORE/$CA_PREFIX"*.crt >/dev/null 2>&1; then
echo "entrypoint: removing previously installed corporate CA certificates"
rm -f "$CA_STORE/$CA_PREFIX"*.crt
update-ca-certificates --fresh >/dev/null 2>&1 \
|| echo "entrypoint: warning — update-ca-certificates failed while removing certificates"
rm -f "$CA_STAMP"
fi
if [ -e "$CA_SRC" ]; then
echo "entrypoint: warning — $CA_SRC holds no certificate files"
fi
return 0
fi
# Idempotent and cheap: the certs are already installed on a plain restart
# (the writable layer survives stop/start), so hash the sources and skip the
# work when nothing has moved. The NSS database is checked separately
# because it lives in the home volume and can be wiped independently.
fp=$(printf '%s\n' "$files" | xargs -d '\n' -r sha256sum 2>/dev/null | sha256sum | cut -d' ' -f1)
stamp=$(cat "$CA_STAMP" 2>/dev/null)
if [ -n "$fp" ] && [ "$fp" = "$stamp" ] && [ -s "$CA_BUNDLE" ]; then
if [ -f "$CA_NSSDB/cert9.db" ]; then
echo "entrypoint: corporate CA certificates already installed"
return 0
fi
ca_seed_nssdb
return 0
fi
mkdir -p "$CA_STORE" "$(dirname "$CA_STAMP")"
rm -f "$CA_STORE/$CA_PREFIX"*.crt
installed=0
while IFS= read -r f; do
[ -n "$f" ] || continue
base=$(basename "$f")
name="$CA_PREFIX$(ca_normalise_name "$base")"
count=$(grep -c -- '-----BEGIN CERTIFICATE-----' "$f" 2>/dev/null || true)
[ -n "$count" ] || count=0
if [ "$count" -gt 1 ]; then
# A corporate trust chain is usually delivered as one PEM holding
# root + intermediates. update-ca-certificates handles exactly one
# certificate per file, so split it.
awk -v out="$CA_STORE/${name%.crt}" '
/-----BEGIN CERTIFICATE-----/ { n++; f = out "-" n ".crt" }
n > 0 { print > f }
' "$f" && installed=$((installed + count))
elif [ "$count" -eq 1 ]; then
cp -f "$f" "$CA_STORE/$name" && installed=$((installed + 1))
else
echo "entrypoint: warning — $f holds no PEM certificate (DER is not supported), skipping"
fi
done <<< "$files"
chmod 644 "$CA_STORE/$CA_PREFIX"*.crt 2>/dev/null
if [ "$installed" -eq 0 ]; then
echo "entrypoint: warning — no usable certificates found under $CA_SRC"
return 0
fi
if update-ca-certificates >/dev/null 2>&1; then
echo "entrypoint: installed $installed corporate CA certificate(s) into the system trust store"
printf '%s' "$fp" > "$CA_STAMP"
else
echo "entrypoint: warning — update-ca-certificates failed; corporate certificates may not be trusted"
fi
ca_seed_nssdb
}
# <<< ca_install
# ── SSH key setup ──────────────────────────────────────────────────────────
# Host SSH dir is mounted read-only at /tmp/.host-ssh and merged into
# /home/claude/.ssh (which lives on the home volume) so we can fix permissions.
#
# This used to `rm -rf ~/.ssh` and copy the host dir over it on every start,
# which silently deleted any key generated or saved inside the container and
# overwrote container edits to config/known_hosts. Now it is a merge:
# - Files the container created are never touched, even if the host later
# gains a file of the same name (the host copy is skipped with a warning).
# - A file previously copied from the host is refreshed when the host copy
# changes, and removed when the host drops it — but only while it is still
# byte-identical to what we copied. Once edited in the container it is the
# container's.
# - known_hosts is unioned, never replaced (ssh appends to it at runtime).
# `.triple-c-host-files` records "<sha256>\t<relative path>" for every file we
# copied, which is how the two kinds of file are told apart. Without it (first
# start after upgrading from the wipe-and-copy entrypoint) a differing file is
# backed up to `<name>.triple-c-backup` before the host copy replaces it.
#
# The function body between the markers is executed by a unit test in
# app/src-tauri/src/docker/image.rs; keep it self-contained.
# >>> sync_host_ssh
sync_host_ssh() {
local src="$1" dest="$2"
local manifest="$dest/.triple-c-host-files"
local legacy=0 new_manifest f rel target src_sum cur_sum recorded
_ssh_sum() {
if [ -L "$1" ]; then
printf 'link:%s' "$(readlink "$1")" | sha256sum | cut -d' ' -f1
else
sha256sum < "$1" | cut -d' ' -f1
fi
}
mkdir -p "$dest"
[ -f "$manifest" ] || legacy=1
new_manifest=$(mktemp)
while IFS= read -r -d '' f; do
rel="${f#"$src"/}"
[ "$rel" = "known_hosts" ] && continue
target="$dest/$rel"
src_sum=$(_ssh_sum "$f")
if [ -e "$target" ] || [ -L "$target" ]; then
cur_sum=$(_ssh_sum "$target")
if [ "$cur_sum" != "$src_sum" ]; then
recorded=""
[ "$legacy" = 0 ] && recorded=$(awk -F'\t' -v r="$rel" '$2 == r { print $1; exit }' "$manifest")
if [ "$legacy" = 1 ]; then
cp -a "$target" "$target.triple-c-backup"
echo "entrypoint: ~/.ssh/$rel differs from host copy; kept old version as $rel.triple-c-backup"
elif [ -z "$recorded" ] || [ "$recorded" != "$cur_sum" ]; then
echo "entrypoint: keeping container's ~/.ssh/$rel (host copy not applied)"
continue
fi
rm -f "$target"
mkdir -p "$(dirname "$target")"
cp -a "$f" "$target"
fi
else
mkdir -p "$(dirname "$target")"
cp -a "$f" "$target"
fi
printf '%s\t%s\n' "$src_sum" "$rel" >> "$new_manifest"
done < <(find "$src" \( -type f -o -type l \) -print0)
# Drop files the host no longer provides, unless edited in the container.
if [ "$legacy" = 0 ]; then
while IFS=$'\t' read -r recorded rel; do
[ -n "$rel" ] || continue
if [ ! -e "$src/$rel" ] && [ ! -L "$src/$rel" ] \
&& { [ -e "$dest/$rel" ] || [ -L "$dest/$rel" ]; } \
&& [ "$(_ssh_sum "$dest/$rel")" = "$recorded" ]; then
rm -f "$dest/$rel"
fi
done < "$manifest"
fi
if [ -f "$src/known_hosts" ]; then
cat "$src/known_hosts" >> "$dest/known_hosts"
sort -u -o "$dest/known_hosts" "$dest/known_hosts"
fi
mv "$new_manifest" "$manifest"
unset -f _ssh_sum
}
# <<< sync_host_ssh
# The host SSH files in $1 merged into ~/.ssh (sync_host_ssh), then the
# modes ssh insists on. On AWS Hybrid ~/.ssh is a link into the EFS mount; the
# steps below then work on the real directory behind it, and only after
# aws_hybrid_ssh_dir has confirmed the link is the one Triple-C made. Any other
# link: nothing is merged. Docker: always /home/claude/.ssh, as before.
# The entrypoint runs this as root before any user process exists;
# triple-c-apply-connect runs it *as the user* (root never walks a ~/.ssh the
# user has been able to plant links in).
# >>> merge_host_ssh
merge_host_ssh() {
local src="$1" ssh_home_dir=/home/claude/.ssh
if [ "$triplec_aws" = 1 ] && [ -L "$ssh_home_dir" ]; then
ssh_home_dir=$(aws_hybrid_ssh_dir /home/claude) || {
echo "entrypoint: warning — ~/.ssh is a symlink Triple-C did not make; host SSH files not merged"
ssh_home_dir=""
}
fi
if [ -n "$ssh_home_dir" ] && [ -d "$src" ]; then
sync_host_ssh "$src" "$ssh_home_dir"
chown -R claude:claude "$ssh_home_dir"
chmod 700 "$ssh_home_dir"
find "$ssh_home_dir" -type f -name "id_*" ! -name "*.pub" -exec chmod 600 {} \;
find "$ssh_home_dir" -type f -name "*.triple-c-backup" -exec chmod 600 {} \;
find "$ssh_home_dir" -type f -name "*.pub" -exec chmod 644 {} \;
if [ -f "$ssh_home_dir/known_hosts" ]; then
chmod 644 "$ssh_home_dir/known_hosts"
fi
if [ -f "$ssh_home_dir/config" ]; then
chmod 600 "$ssh_home_dir/config"
fi
fi
}
# <<< merge_host_ssh
# ── Git credential helper (for HTTPS token) ─────────────────────────────────
# $1 is the token. Callers pass it as a function argument, never on a
# command line (argv is world-readable in /proc).
# >>> git_credentials
install_git_credentials() {
local token="$1" CRED_FILE="/home/claude/.git-credentials"
: > "$CRED_FILE"
chmod 600 "$CRED_FILE"
chown claude:claude "$CRED_FILE"
echo "https://oauth2:${token}@github.com" >> "$CRED_FILE"
echo "https://oauth2:${token}@gitlab.com" >> "$CRED_FILE"
echo "https://oauth2:${token}@bitbucket.org" >> "$CRED_FILE"
git config --file /home/claude/.gitconfig credential.helper "store --file=$CRED_FILE"
}
# <<< git_credentials
# ── AWS: clone the project's repositories ───────────────────────────────────
# An AWS workspace has no project folder to mount; its repositories are cloned
# into /workspace/<folder> once the credential helper, git identity and ~/.ssh
# are in place — as `claude`, with prompts off and a per-repo time limit. A
# folder that is already a repository is left alone. Failures are reported and
# never stop the start: a workspace with one repo missing is still a workspace.
#
# In the background, started after the agent (start_clone_repos below): the
# app gives a starting task's agent only seconds to answer, so a clone in the
# way of the agent would fail every start with a large repository. Terminals
# work while the repos arrive. Progress, for the app to show later, is in
# /run/triple-c-clone (world-readable, root-written): `repos` gets a
# "<folder>\tcloning" line as a clone begins and a
# "<folder>\t<cloned|failed|exists>" line when it ends (the last line for a
# folder is its state), and `done` appears when all are finished. The idle
# watcher counts an unfinished clone as activity; triple-c-wip-push skips a
# folder still `cloning`.
# A clone is made in a scratch folder on the same file system,
# /workspace/.triple-c-clone/<folder>, and renamed into /workspace/<folder>
# (atomic) only once it has succeeded. So nothing appears at a final path until
# it is a whole repository, and the clone code never deletes anything at a
# final path — not on failure, not on a later start. With EFS storage
# /workspace outlives the task: whatever is at a final path is kept (a
# repository is skipped as `exists`), and a clone a killed task left in scratch
# is thrown away at the next start (scratch is never the user's). Scratch is
# made, emptied and renamed from as the user; root never walks their tree.
# triple-c-wip-push skips the scratch folder.
# >>> aws_repos
# clone_repos json root [status_dir]
clone_repos() {
local json="$1" root="$2" status_dir="${3:-}" folder url branch dest tmp err rc
local limit="${triplec_clone_timeout:-600}"
local scratch="$root/.triple-c-clone"
[ -n "$json" ] || return 0
_clone_status() { [ -z "$status_dir" ] || printf '%s\t%s\n' "$1" "$2" >> "$status_dir/repos"; }
# Whatever a killed clone left behind.
_triplec_as_user rm -rf -- "$scratch"
_triplec_as_user mkdir -m 700 -- "$scratch" \
|| echo "entrypoint: warning — could not create $scratch; clones will fail"
while IFS= read -r -d '' folder && IFS= read -r -d '' url && IFS= read -r -d '' branch; do
dest="$root/$folder"
tmp="$scratch/$folder"
if [ -e "$dest/.git" ]; then
echo "entrypoint: /workspace/$folder is already a repository; not cloning"
_clone_status "$folder" exists
continue
fi
# An empty directory may be cloned over (the rename replaces it, as
# `git clone` into it did); anything else at the final path is kept.
if { [ -e "$dest" ] || [ -L "$dest" ]; } \
&& [ -z "$(_triplec_as_user find "$dest" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
echo "Triple-C: clone of $folder failed: /workspace/$folder already exists and is not a repository"
_clone_status "$folder" failed
continue
fi
local args=(clone --quiet)
[ -n "$branch" ] && args+=(--branch "$branch")
args+=(-- "$url" "$tmp")
echo "entrypoint: cloning $folder..."
_clone_status "$folder" cloning
# BatchMode is appended to whatever ssh command the user's git config
# names (core.sshCommand), read as the user, rather than replacing it.
err=$(_triplec_as_user sh -c '
limit=$1; shift
c=${GIT_SSH_COMMAND:-$(git config --get core.sshCommand)}
export GIT_SSH_COMMAND="${c:-ssh} -o BatchMode=yes" GIT_TERMINAL_PROMPT=0
exec timeout -k 10 "$limit" git "$@"' _ "$limit" "${args[@]}" 2>&1 </dev/null)
rc=$?
if [ "$rc" -eq 0 ]; then
# -T: never into a folder made at the final path meanwhile; a
# rename onto anything but an empty directory fails.
if err=$(_triplec_as_user mv -T -- "$tmp" "$dest" 2>&1); then
echo "entrypoint: cloned $folder"
_clone_status "$folder" cloned
continue
fi
else
[ "$rc" -eq 124 ] || [ "$rc" -eq 137 ] && err="timed out after ${limit}s"
fi
_triplec_as_user rm -rf -- "$tmp"
echo "Triple-C: clone of $folder failed: $(printf '%s' "$err" | tail -n 1)"
_clone_status "$folder" failed
done < <(printf '%s' "$json" | python3 -I -c '
import json, sys
def say(msg):
print("Triple-C: " + msg, file=sys.stderr)
try:
repos = json.load(sys.stdin)
except ValueError:
say("TRIPLEC_REPOS is not valid JSON; nothing cloned")
sys.exit(0)
if not isinstance(repos, list):
say("TRIPLEC_REPOS is not a JSON array; nothing cloned")
sys.exit(0)
def text(v):
if not isinstance(v, str) or "\0" in v or "\t" in v or "\n" in v:
return False
try:
v.encode("utf-8")
except UnicodeError:
return False
return True
for i, r in enumerate(repos):
if not isinstance(r, dict):
say("skipping repository %d: not an object" % i)
continue
url, branch, folder = r.get("url"), r.get("branch"), r.get("folder")
if not text(folder) or folder in ("", ".", "..", ".triple-c-clone") or "/" in folder:
say("skipping repository %d: bad folder %a" % (i, folder))
continue
if not text(url) or not url:
say("skipping %s: bad url" % folder)
continue
if branch is not None and not text(branch):
say("skipping %s: bad branch" % folder)
continue
sys.stdout.write("%s\0%s\0%s\0" % (folder, url, branch or ""))
')
unset -f _clone_status
return 0
}
# Copy-at-connect (triplec_connect=1): the credentials arrive with the app's
# first connect, when triple-c-apply-connect has installed them and left
# $triplec_run_dir/connected. The clone waits for that, at most
# triplec_connect_wait seconds (the app connects within a minute or two of
# the agent starting, unless it was closed meanwhile); after that it clones
# without them, says so, and leaves `no-credentials` in the status dir $1 so
# the next connect retries what failed once this clone is done.
aws_wait_connect() {
local status_dir="$1" waited=0
while [ ! -e "$triplec_run_dir/connected" ]; do
if [ "$waited" -ge "$triplec_connect_wait" ]; then
echo "entrypoint: Triple-C has not connected within ${triplec_connect_wait}s — going ahead without the project's credentials (the next connect retries a clone that fails)"
[ -z "$status_dir" ] || : > "$status_dir/no-credentials"
return 0
fi
sleep 1
waited=$((waited + 1))
done
}
# Runs clone_repos in the background, recording progress in $2, then the
# setup script (aws_run, which must see the repositories). The status dir is
# made even with no repositories, so `done` always ends up there. Under
# copy-at-connect the clone first waits for the credentials (above) when there
# are repositories or a setup script.
start_clone_repos() {
local json="$1" status_dir="$2"
rm -rf "$status_dir"
mkdir -m 755 "$status_dir" || status_dir=""
(
trap '' HUP
# Should this chain end early (an error, a signal) with the setup
# still marked `running`, say `failed`: the idle watcher counts a
# running setup as busy and would otherwise never stop the workspace.
trap 'aws_setup_abandoned' EXIT
trap 'exit 143' TERM
(
# `done` however the clone ends (an error, a signal), or the idle
# watcher would count it as still running forever.
[ -z "$status_dir" ] || trap ': > "$status_dir/done"' EXIT
# Wait when something needs the credentials: a repository to
# clone, or a setup script (it runs right after this and needs the
# CA, SSH keys and Git credentials the connect installs).
local needs_connect=0
case "$json" in ''|'[]') ;; *) needs_connect=1 ;; esac
[ -f "${triplec_setup_dir:-/nonexistent}/script" ] && needs_connect=1
if [ "${triplec_connect:-0}" = 1 ] && [ "$needs_connect" = 1 ]; then
aws_wait_connect "$status_dir"
fi
clone_repos "$json" /workspace "$status_dir"
)
aws_run_setup
) &
}
# <<< aws_repos
+170 -483
View File
@@ -13,6 +13,16 @@
# upgradable content in /usr/local/bin or /opt, or seed it from here.
# See "Container Lifecycle" in the repo's CLAUDE.md.
# ── Shared functions ────────────────────────────────────────────────────────
# The CA install, the SSH merge, the git credential helper, the Hybrid home
# links and the repository clone live in entrypoint-lib.sh, which
# triple-c-apply-connect sources too (an AWS workspace whose credentials are
# copied in when the app connects). Sourcing it only defines functions and
# variables; everything still runs from here, in the same order as before.
# The path is fixed, never taken from the environment: this runs as root.
. /usr/local/lib/triple-c/entrypoint-lib.sh \
|| echo "entrypoint: warning — /usr/local/lib/triple-c/entrypoint-lib.sh is missing; the container will be incomplete"
# ── AWS workspace (TRIPLEC_HOST=aws): project data off the environment ─────
# On AWS (an ECS Fargate task) there are no bind mounts and no Docker API.
# Everything project-shaped arrives as env vars injected from the project's
@@ -22,20 +32,27 @@
# Docker bind mounts use, so the SSH merge and the CA install
# below run unchanged;
# TRIPLEC_REPOS [{"url","branch"|null,"folder"}] — cloned by clone_repos
# once git credentials and ~/.ssh are in place.
# Both are unset here, *before* the agent-token block below snapshots the
# once git credentials and ~/.ssh are in place;
# TRIPLEC_SETUP_SCRIPT the project's setup script (≤ 32 KiB), written to
# /run/triple-c-setup/script by aws_prepare_run and run as
# `claude` after the clone (aws_run_setup);
# TRIPLEC_CREDENTIALS `connect` when the project copies its credentials in
# at connect instead (phase 6 Q2): the secret then has no
# TRIPLEC_FILES, API keys, GIT_TOKEN or custom variables, and
# the background clone waits (start_clone_repos) for
# triple-c-apply-connect to bring them. Not secret; left set.
# All three are unset here, *before* the agent-token block below snapshots the
# environment for the agent: that snapshot is every terminal's environment on
# AWS, and the files hold private keys. The repo list is kept only as an
# unexported shell variable, which no child process inherits.
# AWS, and the files hold private keys. The repo list and the setup script are
# kept only as unexported shell variables, which no child process inherits.
#
# Nothing in this block (or any other aws block) runs without TRIPLEC_HOST=aws;
# a Docker container sees the same script it always did.
# >>> aws_env
triplec_aws=0
triplec_run_as=claude
triplec_connect=0
triplec_repos_json=
# Where Hybrid storage mounts the project's EFS access point.
triplec_home_mnt=/mnt/triple-c-home
triplec_setup_script=
# stdin: the TRIPLEC_FILES JSON; $1 the ssh dir, $2 the CA dir. Names are
# validated (non-empty, no `/`, not `.`/`..`, no NUL) and anything else is
@@ -94,15 +111,6 @@ sys.exit(1 if bad else 0)
' "$1" "$2"
}
# Runs a command as the container user (`claude`) when this script is root.
_triplec_as_user() {
if [ -n "${triplec_run_as:-}" ] && [ "$(id -u)" = 0 ]; then
su -s /bin/bash -c '"$@"' -- "$triplec_run_as" _ "$@"
else
"$@"
fi
}
if [ "${TRIPLEC_HOST:-}" = aws ]; then
triplec_aws=1
if [ -n "${TRIPLEC_FILES+x}" ]; then
@@ -112,9 +120,121 @@ if [ "${TRIPLEC_HOST:-}" = aws ]; then
fi
triplec_repos_json="${TRIPLEC_REPOS:-}"
unset TRIPLEC_REPOS
triplec_setup_script="${TRIPLEC_SETUP_SCRIPT:-}"
unset TRIPLEC_SETUP_SCRIPT
# Copy-at-connect: no credentials in the secret; the clone waits for the
# app's first connect to bring them (triple-c-apply-connect).
[ "${TRIPLEC_CREDENTIALS:-}" = connect ] && triplec_connect=1
fi
# <<< aws_env
# ── AWS: this run's record, and the setup script's files ───────────────────
# aws_prepare_run, as root at the start of an AWS task (just after the
# agent-token block):
# /run/triple-c root, 0711 — users may open a file they know the
# name of, never list it (the stop marker in it is
# 0600; the stop guard and the idle watcher keep the
# mode);
# /run/triple-c/run-id `<UTC yyyymmdd-HHMMSS>-<task id, first 8>` (task id
# from the ECS task metadata, else `local`), 0644 —
# triple-c-wip-push names this run's WIP branches
# after it (triple-c/wip/<run-id>-<folder>);
# /run/triple-c/start-epoch the start in Unix seconds, for the idle watcher's
# lifetime cap (TRIPLEC_MAX_LIFETIME_MINUTES);
# /run/triple-c/wip-last 0700, owned by `claude`: triple-c-wip-push (run as
# claude) records there the tree it last pushed per
# folder and skips an unchanged one. Root makes it
# and never looks inside;
# /run/triple-c/repos.json the repository list (TRIPLEC_REPOS), root 0600:
# triple-c-apply-connect re-clones the ones that
# failed from it. `connected` (its marker) is
# cleared;
# /run/triple-c-setup root, 0755: `script` (0644) when the project has a
# setup script, `log` (0644; root opens it and hands
# the script the descriptor, so the user never needs
# write access to anything here and root never opens
# a path the user controls) and `status` — `running`
# from now until the script ends (waiting for the
# clone included), then `ok` or `failed <code>`. No
# status file: no setup script.
# aws_run_setup runs the script as `claude`, `bash -e`, stdin /dev/null, cwd
# /workspace, under `timeout -k 10 1800`, chained after the background clone
# (start_clone_repos) so the agent's start is never held up. A failure is
# recorded, never fatal. triple-c-workspace-status reads all of this back.
# >>> aws_run
triplec_setup_dir=/run/triple-c-setup
triplec_setup_timeout=1800
# The first 8 characters of this task's id, from the ECS task metadata
# endpoint ECS sets for every container; `local` without one or on any doubt.
aws_task_id8() {
local uri="${ECS_CONTAINER_METADATA_URI_V4:-}" arn id
if [ -n "$uri" ]; then
arn=$(curl -sf --max-time 3 "$uri/task" 2>/dev/null | jq -r '.TaskARN // empty' 2>/dev/null)
id=${arn##*/}
case "$id" in
''|*[!A-Za-z0-9]*) ;;
*) [ "${#id}" -ge 8 ] && { printf '%s\n' "${id:0:8}"; return; } ;;
esac
fi
echo local
}
aws_prepare_run() {
local now root=0
[ "$(id -u)" = 0 ] && root=1
now=$(date +%s)
if mkdir -p "$triplec_run_dir"; then
[ "$root" = 1 ] && chown root:root "$triplec_run_dir"
chmod 711 "$triplec_run_dir"
printf '%s-%s\n' "$(date -u -d "@$now" +%Y%m%d-%H%M%S)" "$(aws_task_id8)" > "$triplec_run_dir/run-id"
chmod 644 "$triplec_run_dir/run-id"
printf '%s\n' "$now" > "$triplec_run_dir/start-epoch"
rm -rf "$triplec_run_dir/wip-last"
if mkdir -m 700 "$triplec_run_dir/wip-last" && [ -n "${triplec_run_as:-}" ]; then
chown "$triplec_run_as:" "$triplec_run_dir/wip-last"
fi
rm -f "$triplec_run_dir/connected"
( umask 077; printf '%s' "$triplec_repos_json" > "$triplec_run_dir/repos.json" )
else
echo "entrypoint: warning — could not create $triplec_run_dir"
fi
rm -rf "$triplec_setup_dir"
mkdir -m 755 "$triplec_setup_dir" || { echo "entrypoint: warning — could not create $triplec_setup_dir; no setup"; return 0; }
[ "$root" = 1 ] && chown root:root "$triplec_setup_dir"
[ -n "$triplec_setup_script" ] || return 0
printf '%s' "$triplec_setup_script" > "$triplec_setup_dir/script"
chmod 644 "$triplec_setup_dir/script"
: > "$triplec_setup_dir/log"
chmod 644 "$triplec_setup_dir/log"
printf 'running\n' > "$triplec_setup_dir/status"
triplec_setup_script=
}
# The setup chain ended without a verdict: record a failure (no code).
aws_setup_abandoned() {
[ "$(head -n 1 "$triplec_setup_dir/status" 2>/dev/null)" = running ] || return 0
printf 'failed\n' > "$triplec_setup_dir/status.tmp" && mv -f "$triplec_setup_dir/status.tmp" "$triplec_setup_dir/status"
echo "entrypoint: the setup script did not finish — recorded as failed"
}
# Runs the setup script, if there is one (see above). Never fails.
aws_run_setup() {
local dir="$triplec_setup_dir" rc state
[ -f "$dir/script" ] || return 0
echo "entrypoint: running the setup script..."
(
cd /workspace 2>/dev/null || cd /
_triplec_as_user timeout -k 10 "$triplec_setup_timeout" bash -e "$dir/script"
) </dev/null >>"$dir/log" 2>&1
rc=$?
if [ "$rc" -eq 0 ]; then state=ok; else state="failed $rc"; fi
printf '%s\n' "$state" > "$dir/status.tmp" && mv -f "$dir/status.tmp" "$dir/status"
echo "entrypoint: setup script $state"
return 0
}
# <<< aws_run
# ── triple-c-agent token: off the environment before anything else runs ───
# The agent (started at the very end, see start_agent) authenticates every
# request with this token, and it runs as root, so the token is effectively
@@ -158,6 +278,9 @@ if [ -n "${TRIPLEC_AGENT_TOKEN_FILE:-}" ]; then
mapfile -d '' agent_env < <(env -u PWD -u OLDPWD -u SHLVL -u _ -0)
fi
# <<< agent_token
# After the token block, so nothing aws_prepare_run starts (the metadata curl)
# inherits TRIPLEC_AGENT_TOKEN; before anything that can push.
if [ "$triplec_aws" = 1 ]; then aws_prepare_run; fi
# ── UID/GID remapping ──────────────────────────────────────────────────────
# Match the container's claude user to the host user's UID/GID so that
@@ -213,349 +336,18 @@ remap_uid_gid() {
# ~/.claude volume.
find /home/claude \( ! -user claude -o ! -group claude \) -exec chown -h claude:claude {} +
# ── AWS Hybrid storage: persisted home state (plan ruling P4) ───────────────
# A Hybrid workspace mounts its EFS access point (POSIX 1000:1000) at
# /mnt/triple-c-home. Mounting it over /home/claude would hide the image's
# Claude Code, cargo and uv — EFS has no Docker-volume "copy the image's
# contents on first mount" — so only user state is persisted, by symlink:
# ~/.claude, ~/.claude.json and ~/.ssh. On first use each is seeded from the
# image's copy (or created empty); from then on the persisted copy wins and
# the fresh image copy every new task starts with is replaced by the link —
# what a Docker home volume does by masking. Runs before the SSH merge below,
# so host keys land on EFS, and before anything writes ~/.claude.
# >>> aws_home
link_persistent_home() {
local home="$1" persist="$2" owner="$3" name link target
if [ ! -d "$persist" ] || [ -L "$persist" ]; then
echo "entrypoint: warning — Hybrid storage, but $persist is not mounted; home state will not persist"
return 0
fi
# The access point already makes its root 1000:1000; said again here so
# claude (Claude Code's own atomic saves, write_claude_json) can always
# create files beside the persisted ones.
[ -z "$owner" ] || chown "$owner" "$persist" 2>/dev/null
for name in .claude .claude.json .ssh; do
link="$home/$name"
target="$persist/$name"
if [ -L "$link" ] && [ "$(readlink "$link")" = "$target" ]; then
continue
fi
if [ ! -e "$target" ] && [ ! -L "$target" ]; then
if [ -e "$link" ] || [ -L "$link" ]; then
# Staged then renamed: a copy cut short must never look seeded.
rm -rf "$target.triple-c-seed"
if ! cp -a "$link" "$target.triple-c-seed" || ! mv "$target.triple-c-seed" "$target"; then
rm -rf "$target.triple-c-seed"
echo "entrypoint: warning — could not seed $target; ~/$name stays unpersisted"
continue
fi
elif [ "$name" = .claude.json ]; then
( umask 077; printf '{}\n' > "$target" ) || {
echo "entrypoint: warning — could not create $target"; continue; }
else
mkdir -m 700 "$target" || {
echo "entrypoint: warning — could not create $target"; continue; }
fi
[ -z "$owner" ] || chown -R "$owner" "$target" 2>/dev/null
fi
rm -rf "$link"
ln -s "$target" "$link" || echo "entrypoint: warning — could not link ~/$name to $target"
[ -z "$owner" ] || chown -h "$owner" "$link" 2>/dev/null
done
echo "entrypoint: Hybrid storage — ~/.claude, ~/.claude.json and ~/.ssh persist on $persist"
}
# The real directory behind a Hybrid ~/.ssh link, printed only when the link
# is exactly the one link_persistent_home makes and neither it nor the mount
# is itself a symlink. Root chowns and chmods under the result, and the
# persisted directory is claude-writable, so root must never be pointed
# anywhere else by it.
aws_hybrid_ssh_dir() {
local home="$1" want="$triplec_home_mnt/.ssh"
[ "$(readlink "$home/.ssh")" = "$want" ] || return 1
[ ! -L "$triplec_home_mnt" ] && [ ! -L "$want" ] && [ -d "$want" ] || return 1
printf '%s' "$want"
}
# <<< aws_home
if [ "$triplec_aws" = 1 ] && [ "${TRIPLEC_STORAGE:-}" = hybrid ]; then
if [ "$triplec_aws" = 1 ] && aws_storage_links_home "${TRIPLEC_STORAGE:-}"; then
link_persistent_home /home/claude "$triplec_home_mnt" claude:claude
fi
if [ "$triplec_aws" = 1 ] && [ "${TRIPLEC_STORAGE:-}" = efs ]; then
aws_efs_workspace_check /workspace
fi
# ── Corporate CA certificates ───────────────────────────────────────────────
# The host's CA material is bind-mounted read-only at /tmp/.host-ca. Triple-C
# mounts a *directory* as-is and a *single file* as /tmp/.host-ca/<name>.crt,
# so this only ever has to deal with a directory (the file branch below is
# defensive).
#
# Runs before everything that touches the network — the git credential helper,
# ssh-keyscan, and especially the `claude update` at the bottom of this file,
# which is itself an HTTPS call that fails behind a TLS-terminating proxy
# without this.
#
# Two things are easy to get wrong here:
# 1. `update-ca-certificates` globs /usr/local/share/ca-certificates/*.crt
# case-sensitively. A `.pem` that is merely copied in is ignored in total
# silence, so certificates are *renamed*, not copied.
# 2. Chrome/Chromium read neither /etc/ssl nor $SSL_CERT_FILE; they have
# their own NSS database at ~/.pki/nssdb, seeded below with certutil.
#
# NODE_EXTRA_CA_CERTS / REQUESTS_CA_BUNDLE / SSL_CERT_FILE are deliberately NOT
# exported here. Every terminal is a separate `docker exec`, which inherits the
# container's configured env and sees nothing this script exported — the same
# reason $BROWSER had to become an image-level ENV. Triple-C sets them on the
# container at creation time instead. (They are forwarded into the cron
# environment file further down, because cron jobs start from a bare env.)
CA_SRC="/tmp/.host-ca"
CA_STORE="/usr/local/share/ca-certificates"
CA_PREFIX="triple-c-"
CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt"
CA_STAMP="/var/lib/triple-c/ca.stamp"
CA_NSSDB="/home/claude/.pki/nssdb"
# Mirror of `container_cert_name()` in app/src-tauri/src/docker/ca_certs.rs.
# The two must agree; the Rust side has the unit tests.
ca_normalise_name() {
local name stem
name=$(printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_')
while [ "${name#.}" != "$name" ]; do name="${name#.}"; done
stem="${name%.*}"
[ -z "$stem" ] && stem="corporate-ca"
printf '%s.crt' "$stem"
}
ca_source_files() {
if [ -d "$CA_SRC" ]; then
find "$CA_SRC" -maxdepth 1 -type f \
\( -iname '*.crt' -o -iname '*.pem' -o -iname '*.cer' \
-o -iname '*.cert' -o -iname '*.ca-bundle' \) 2>/dev/null | sort
elif [ -f "$CA_SRC" ]; then
printf '%s\n' "$CA_SRC"
fi
}
# Seed Chrome/Chromium's NSS database. Tolerant by design: a missing certutil
# or a broken profile must warn, never fail the container start.
# ~/.pki lives in the home volume, so this persists once done; the system store
# lives in the writable layer and is re-applied on every start.
ca_seed_nssdb() {
if ! command -v certutil >/dev/null 2>&1; then
echo "entrypoint: warning — certutil not found (install libnss3-tools); Chrome/Chromium in this container will not trust the corporate CA"
return 0
fi
su -s /bin/bash claude -c '
db="$HOME/.pki/nssdb"
mkdir -p "$db" || exit 1
if [ ! -f "$db/cert9.db" ]; then
certutil -d "sql:$db" -N --empty-password >/dev/null 2>&1 || exit 1
fi
for f in /usr/local/share/ca-certificates/triple-c-*.crt; do
[ -f "$f" ] || continue
nick="triple-c:$(basename "$f" .crt)"
# Delete first so re-running replaces rather than duplicates.
certutil -d "sql:$db" -D -n "$nick" >/dev/null 2>&1
certutil -d "sql:$db" -A -t "C,," -n "$nick" -i "$f" >/dev/null 2>&1 \
|| echo "entrypoint: warning — certutil could not add $nick"
done
' && echo "entrypoint: seeded Chrome/Chromium NSS database with the corporate CA" \
|| echo "entrypoint: warning — NSS database seeding failed (continuing)"
}
install_corporate_ca() {
local files fp stamp f base name count installed
files=$(ca_source_files)
if [ -z "$files" ]; then
# Nothing configured — but /usr/local/share is in the writable layer and
# `docker commit` bakes it into the project's snapshot image, so a cert
# installed by a previous configuration would ride that snapshot into
# every future container. Turning the setting off has to actively undo.
if ls "$CA_STORE/$CA_PREFIX"*.crt >/dev/null 2>&1; then
echo "entrypoint: removing previously installed corporate CA certificates"
rm -f "$CA_STORE/$CA_PREFIX"*.crt
update-ca-certificates --fresh >/dev/null 2>&1 \
|| echo "entrypoint: warning — update-ca-certificates failed while removing certificates"
rm -f "$CA_STAMP"
fi
if [ -e "$CA_SRC" ]; then
echo "entrypoint: warning — $CA_SRC holds no certificate files"
fi
return 0
fi
# Idempotent and cheap: the certs are already installed on a plain restart
# (the writable layer survives stop/start), so hash the sources and skip the
# work when nothing has moved. The NSS database is checked separately
# because it lives in the home volume and can be wiped independently.
fp=$(printf '%s\n' "$files" | xargs -d '\n' -r sha256sum 2>/dev/null | sha256sum | cut -d' ' -f1)
stamp=$(cat "$CA_STAMP" 2>/dev/null)
if [ -n "$fp" ] && [ "$fp" = "$stamp" ] && [ -s "$CA_BUNDLE" ]; then
if [ -f "$CA_NSSDB/cert9.db" ]; then
echo "entrypoint: corporate CA certificates already installed"
return 0
fi
ca_seed_nssdb
return 0
fi
mkdir -p "$CA_STORE" "$(dirname "$CA_STAMP")"
rm -f "$CA_STORE/$CA_PREFIX"*.crt
installed=0
while IFS= read -r f; do
[ -n "$f" ] || continue
base=$(basename "$f")
name="$CA_PREFIX$(ca_normalise_name "$base")"
count=$(grep -c -- '-----BEGIN CERTIFICATE-----' "$f" 2>/dev/null || true)
[ -n "$count" ] || count=0
if [ "$count" -gt 1 ]; then
# A corporate trust chain is usually delivered as one PEM holding
# root + intermediates. update-ca-certificates handles exactly one
# certificate per file, so split it.
awk -v out="$CA_STORE/${name%.crt}" '
/-----BEGIN CERTIFICATE-----/ { n++; f = out "-" n ".crt" }
n > 0 { print > f }
' "$f" && installed=$((installed + count))
elif [ "$count" -eq 1 ]; then
cp -f "$f" "$CA_STORE/$name" && installed=$((installed + 1))
else
echo "entrypoint: warning — $f holds no PEM certificate (DER is not supported), skipping"
fi
done <<< "$files"
chmod 644 "$CA_STORE/$CA_PREFIX"*.crt 2>/dev/null
if [ "$installed" -eq 0 ]; then
echo "entrypoint: warning — no usable certificates found under $CA_SRC"
return 0
fi
if update-ca-certificates >/dev/null 2>&1; then
echo "entrypoint: installed $installed corporate CA certificate(s) into the system trust store"
printf '%s' "$fp" > "$CA_STAMP"
else
echo "entrypoint: warning — update-ca-certificates failed; corporate certificates may not be trusted"
fi
ca_seed_nssdb
}
install_corporate_ca
# ── SSH key setup ──────────────────────────────────────────────────────────
# Host SSH dir is mounted read-only at /tmp/.host-ssh and merged into
# /home/claude/.ssh (which lives on the home volume) so we can fix permissions.
#
# This used to `rm -rf ~/.ssh` and copy the host dir over it on every start,
# which silently deleted any key generated or saved inside the container and
# overwrote container edits to config/known_hosts. Now it is a merge:
# - Files the container created are never touched, even if the host later
# gains a file of the same name (the host copy is skipped with a warning).
# - A file previously copied from the host is refreshed when the host copy
# changes, and removed when the host drops it — but only while it is still
# byte-identical to what we copied. Once edited in the container it is the
# container's.
# - known_hosts is unioned, never replaced (ssh appends to it at runtime).
# `.triple-c-host-files` records "<sha256>\t<relative path>" for every file we
# copied, which is how the two kinds of file are told apart. Without it (first
# start after upgrading from the wipe-and-copy entrypoint) a differing file is
# backed up to `<name>.triple-c-backup` before the host copy replaces it.
#
# The function body between the markers is executed by a unit test in
# app/src-tauri/src/docker/image.rs; keep it self-contained.
# >>> sync_host_ssh
sync_host_ssh() {
local src="$1" dest="$2"
local manifest="$dest/.triple-c-host-files"
local legacy=0 new_manifest f rel target src_sum cur_sum recorded
_ssh_sum() {
if [ -L "$1" ]; then
printf 'link:%s' "$(readlink "$1")" | sha256sum | cut -d' ' -f1
else
sha256sum < "$1" | cut -d' ' -f1
fi
}
mkdir -p "$dest"
[ -f "$manifest" ] || legacy=1
new_manifest=$(mktemp)
while IFS= read -r -d '' f; do
rel="${f#"$src"/}"
[ "$rel" = "known_hosts" ] && continue
target="$dest/$rel"
src_sum=$(_ssh_sum "$f")
if [ -e "$target" ] || [ -L "$target" ]; then
cur_sum=$(_ssh_sum "$target")
if [ "$cur_sum" != "$src_sum" ]; then
recorded=""
[ "$legacy" = 0 ] && recorded=$(awk -F'\t' -v r="$rel" '$2 == r { print $1; exit }' "$manifest")
if [ "$legacy" = 1 ]; then
cp -a "$target" "$target.triple-c-backup"
echo "entrypoint: ~/.ssh/$rel differs from host copy; kept old version as $rel.triple-c-backup"
elif [ -z "$recorded" ] || [ "$recorded" != "$cur_sum" ]; then
echo "entrypoint: keeping container's ~/.ssh/$rel (host copy not applied)"
continue
fi
rm -f "$target"
mkdir -p "$(dirname "$target")"
cp -a "$f" "$target"
fi
else
mkdir -p "$(dirname "$target")"
cp -a "$f" "$target"
fi
printf '%s\t%s\n' "$src_sum" "$rel" >> "$new_manifest"
done < <(find "$src" \( -type f -o -type l \) -print0)
# Drop files the host no longer provides, unless edited in the container.
if [ "$legacy" = 0 ]; then
while IFS=$'\t' read -r recorded rel; do
[ -n "$rel" ] || continue
if [ ! -e "$src/$rel" ] && [ ! -L "$src/$rel" ] \
&& { [ -e "$dest/$rel" ] || [ -L "$dest/$rel" ]; } \
&& [ "$(_ssh_sum "$dest/$rel")" = "$recorded" ]; then
rm -f "$dest/$rel"
fi
done < "$manifest"
fi
if [ -f "$src/known_hosts" ]; then
cat "$src/known_hosts" >> "$dest/known_hosts"
sort -u -o "$dest/known_hosts" "$dest/known_hosts"
fi
mv "$new_manifest" "$manifest"
unset -f _ssh_sum
}
# <<< sync_host_ssh
# On AWS Hybrid ~/.ssh is a link into the EFS mount; the root steps below then
# work on the real directory behind it, and only after aws_hybrid_ssh_dir has
# confirmed the link is the one Triple-C made. Any other link: nothing is
# merged. Docker: always /home/claude/.ssh, as before.
ssh_home_dir=/home/claude/.ssh
if [ "$triplec_aws" = 1 ] && [ -L "$ssh_home_dir" ]; then
ssh_home_dir=$(aws_hybrid_ssh_dir /home/claude) || {
echo "entrypoint: warning — ~/.ssh is a symlink Triple-C did not make; host SSH files not merged"
ssh_home_dir=""
}
fi
if [ -n "$ssh_home_dir" ] && [ -d /tmp/.host-ssh ]; then
sync_host_ssh /tmp/.host-ssh "$ssh_home_dir"
chown -R claude:claude "$ssh_home_dir"
chmod 700 "$ssh_home_dir"
find "$ssh_home_dir" -type f -name "id_*" ! -name "*.pub" -exec chmod 600 {} \;
find "$ssh_home_dir" -type f -name "*.triple-c-backup" -exec chmod 600 {} \;
find "$ssh_home_dir" -type f -name "*.pub" -exec chmod 644 {} \;
if [ -f "$ssh_home_dir/known_hosts" ]; then
chmod 644 "$ssh_home_dir/known_hosts"
fi
if [ -f "$ssh_home_dir/config" ]; then
chmod 600 "$ssh_home_dir/config"
fi
fi
merge_host_ssh /tmp/.host-ssh
# Append common host keys (avoid duplicates)
su -s /bin/bash claude -c '
@@ -604,14 +396,7 @@ fi
# ── Git credential helper (for HTTPS token) ─────────────────────────────────
if [ -n "$GIT_TOKEN" ]; then
CRED_FILE="/home/claude/.git-credentials"
: > "$CRED_FILE"
chmod 600 "$CRED_FILE"
chown claude:claude "$CRED_FILE"
echo "https://oauth2:${GIT_TOKEN}@github.com" >> "$CRED_FILE"
echo "https://oauth2:${GIT_TOKEN}@gitlab.com" >> "$CRED_FILE"
echo "https://oauth2:${GIT_TOKEN}@bitbucket.org" >> "$CRED_FILE"
git config --file /home/claude/.gitconfig credential.helper "store --file=$CRED_FILE"
install_git_credentials "$GIT_TOKEN"
unset GIT_TOKEN
fi
@@ -624,117 +409,6 @@ if [ -n "$GIT_USER_EMAIL" ]; then
fi
chown claude:claude /home/claude/.gitconfig 2>/dev/null || true
# ── AWS: clone the project's repositories ───────────────────────────────────
# An AWS workspace has no project folder to mount; its repositories are cloned
# into /workspace/<folder> once the credential helper, git identity and ~/.ssh
# are in place — as `claude`, with prompts off and a per-repo time limit. A
# folder that is already a repository is left alone. Failures are reported and
# never stop the start: a workspace with one repo missing is still a workspace.
#
# In the background, started after the agent (start_clone_repos below): the
# app gives a starting task's agent only seconds to answer, so a clone in the
# way of the agent would fail every start with a large repository. Terminals
# work while the repos arrive. Progress, for the app to show later, is in
# /run/triple-c-clone (world-readable, root-written): `repos` gets a
# "<folder>\tcloning" line as a clone begins and a
# "<folder>\t<cloned|failed|exists>" line when it ends (the last line for a
# folder is its state), and `done` appears when all are finished. The idle
# watcher counts an unfinished clone as activity; triple-c-wip-push skips a
# folder still `cloning`.
# >>> aws_repos
# clone_repos json root [status_dir]
clone_repos() {
local json="$1" root="$2" status_dir="${3:-}" folder url branch dest existed err rc
local limit="${triplec_clone_timeout:-600}"
[ -n "$json" ] || return 0
_clone_status() { [ -z "$status_dir" ] || printf '%s\t%s\n' "$1" "$2" >> "$status_dir/repos"; }
while IFS= read -r -d '' folder && IFS= read -r -d '' url && IFS= read -r -d '' branch; do
dest="$root/$folder"
if [ -e "$dest/.git" ]; then
echo "entrypoint: /workspace/$folder is already a repository; not cloning"
_clone_status "$folder" exists
continue
fi
existed=0
[ -e "$dest" ] && existed=1
local args=(clone --quiet)
[ -n "$branch" ] && args+=(--branch "$branch")
args+=(-- "$url" "$dest")
echo "entrypoint: cloning $folder..."
_clone_status "$folder" cloning
# BatchMode is appended to whatever ssh command the user's git config
# names (core.sshCommand), read as the user, rather than replacing it.
err=$(_triplec_as_user sh -c '
limit=$1; shift
c=${GIT_SSH_COMMAND:-$(git config --get core.sshCommand)}
export GIT_SSH_COMMAND="${c:-ssh} -o BatchMode=yes" GIT_TERMINAL_PROMPT=0
exec timeout -k 10 "$limit" git "$@"' _ "$limit" "${args[@]}" 2>&1 </dev/null)
rc=$?
if [ "$rc" -eq 0 ]; then
echo "entrypoint: cloned $folder"
_clone_status "$folder" cloned
continue
fi
[ "$rc" -eq 124 ] || [ "$rc" -eq 137 ] && err="timed out after ${limit}s"
# Don't leave a half-made clone that the next start would skip.
[ "$existed" = 1 ] || rm -rf "$dest"
echo "Triple-C: clone of $folder failed: $(printf '%s' "$err" | tail -n 1)"
_clone_status "$folder" failed
done < <(printf '%s' "$json" | python3 -I -c '
import json, sys
def say(msg):
print("Triple-C: " + msg, file=sys.stderr)
try:
repos = json.load(sys.stdin)
except ValueError:
say("TRIPLEC_REPOS is not valid JSON; nothing cloned")
sys.exit(0)
if not isinstance(repos, list):
say("TRIPLEC_REPOS is not a JSON array; nothing cloned")
sys.exit(0)
def text(v):
if not isinstance(v, str) or "\0" in v or "\t" in v or "\n" in v:
return False
try:
v.encode("utf-8")
except UnicodeError:
return False
return True
for i, r in enumerate(repos):
if not isinstance(r, dict):
say("skipping repository %d: not an object" % i)
continue
url, branch, folder = r.get("url"), r.get("branch"), r.get("folder")
if not text(folder) or folder in ("", ".", "..") or "/" in folder:
say("skipping repository %d: bad folder %a" % (i, folder))
continue
if not text(url) or not url:
say("skipping %s: bad url" % folder)
continue
if branch is not None and not text(branch):
say("skipping %s: bad branch" % folder)
continue
sys.stdout.write("%s\0%s\0%s\0" % (folder, url, branch or ""))
')
unset -f _clone_status
return 0
}
# Runs clone_repos in the background, recording progress in $2.
start_clone_repos() {
local json="$1" status_dir="$2"
[ -n "$json" ] || return 0
rm -rf "$status_dir"
mkdir -m 755 "$status_dir" || status_dir=""
(
trap '' HUP
# `done` however the clone ends (an error, a signal), or the idle
# watcher would count it as still running forever.
[ -z "$status_dir" ] || trap ': > "$status_dir/done"' EXIT
clone_repos "$json" /workspace "$status_dir"
) &
}
# <<< aws_repos
# ── Claude instructions ──────────────────────────────────────────────────────
if [ -n "$CLAUDE_INSTRUCTIONS" ]; then
@@ -1171,19 +845,21 @@ start_agent() {
# <<< start_agent
start_agent
# ── AWS: WIP push on stop, idle auto-stop ───────────────────────────────────
# ── AWS: WIP push on stop, idle auto-stop, lifetime cap ─────────────────────
# aws_before_stop runs from the TERM trap below, before the keepalive goes:
# with TRIPLEC_WIP_PUSH=1 it pushes every repo's unsaved work to a
# triple-c/wip/<stamp> branch (as `claude`, bounded at 90 s), unless the app's
# with TRIPLEC_WIP_PUSH=1 it pushes every repo's unsaved work to the run's
# triple-c/wip/<run-id> branch (as `claude`, bounded at 90 s), unless the app's
# stop guard recorded `pushed` or `discard` in /run/triple-c/stop-reason
# (written as root through the agent just before StopTask), or the idle
# watcher recorded `idle` (it pushes *before* signalling and only signals once
# nothing unsaved is left, so an idle stop never loses work). ECS gives a task
# nothing unsaved is left, so an idle stop never loses work) or `lifetime`
# (it tried its push, best effort, before signalling). ECS gives a task
# `stopTimeout` seconds between SIGTERM and SIGKILL — 30 by default, 120 at
# most on Fargate — so the task definition must raise it for the push to
# finish. Once only: a second TERM during the push changes nothing.
#
# The idle watcher (TRIPLEC_IDLE_STOP_MINUTES set) runs as root under a
# The idle watcher (TRIPLEC_IDLE_STOP_MINUTES and/or a non-zero
# TRIPLEC_MAX_LIFETIME_MINUTES set) runs as root under a
# supervisor loop like the agent's, so a claude-user `pkill` cannot switch
# auto-stop off. When the workspace is idle it records the reason "idle" and
# sends TERM to *this* script ($$) — not PID 1, which under ECS's
@@ -1191,10 +867,14 @@ start_agent
# this same trap and push at most once. An idle stop exits with status 75
# (triplec_idle_exit; Rust `aws::tasks::IDLE_EXIT_CODE`), so ECS reports it as
# EssentialContainerExited with that exit code and the app can say "stopped
# after being idle" rather than "crashed". Its environment is built from
# scratch: it needs none of the container's but TRIPLEC_WIP_PUSH.
# after being idle" rather than "crashed". A lifetime-cap stop (the run's
# start-epoch plus TRIPLEC_MAX_LIFETIME_MINUTES reached; advisory — a user
# with sudo can switch it off) exits 76 (triplec_lifetime_exit; Rust
# `aws::tasks::LIFETIME_EXIT_CODE`). Its environment is built from scratch: it
# needs none of the container's but TRIPLEC_WIP_PUSH and the lifetime cap.
# >>> aws_stop
triplec_idle_exit=75
triplec_lifetime_exit=76
aws_before_stop() {
[ "${TRIPLEC_HOST:-}" = aws ] || return 0
[ -z "${triplec_stopping:-}" ] || return 0
@@ -1207,6 +887,11 @@ aws_before_stop() {
triplec_exit_status=$triplec_idle_exit
echo "entrypoint: stopping (idle) — work in progress already handled by the idle watcher"
return 0 ;;
lifetime)
# The watcher tried its push (best effort) before signalling.
triplec_exit_status=$triplec_lifetime_exit
echo "entrypoint: stopping (lifetime) — the environment's maximum workspace lifetime was reached; work in progress already handled by the idle watcher"
return 0 ;;
# The app's stop guard already pushed, or the user chose to stop
# without pushing: honour that instead of pushing again.
pushed|discard)
@@ -1223,9 +908,9 @@ aws_before_stop() {
start_idle_watch() {
local minutes="$1" signal_pid="$2"
mkdir -p /run/triple-c && chmod 700 /run/triple-c
mkdir -p /run/triple-c && chmod 711 /run/triple-c
rm -f /run/triple-c/stop-reason
if [ -z "${TRIPLEC_AGENT_TOKEN_FILE:-}" ]; then
if [ -n "$minutes" ] && [ -z "${TRIPLEC_AGENT_TOKEN_FILE:-}" ]; then
echo "triple-c-idle-watch: no agent token, so no activity to read — idle stop will never fire" >&2
fi
(
@@ -1237,6 +922,7 @@ start_idle_watch() {
TRIPLEC_IDLE_SIGNAL_PID="$signal_pid" \
TRIPLEC_AGENT_TOKEN_FILE="${TRIPLEC_AGENT_TOKEN_FILE:-/dev/shm/triple-c/agent-token}" \
TRIPLEC_WIP_PUSH="${TRIPLEC_WIP_PUSH:-0}" \
TRIPLEC_MAX_LIFETIME_MINUTES="${TRIPLEC_MAX_LIFETIME_MINUTES:-}" \
/usr/local/bin/triple-c-idle-watch
rc=$?
# 0: it signalled the stop. 2: misconfigured, and a restart would
@@ -1259,10 +945,11 @@ stop_keepalive() {
exit "${triplec_exit_status:-0}"
}
trap stop_keepalive TERM INT HUP
if [ "$triplec_aws" = 1 ] && [ -n "${TRIPLEC_IDLE_STOP_MINUTES:-}" ]; then
start_idle_watch "$TRIPLEC_IDLE_STOP_MINUTES" "$$"
if [ "$triplec_aws" = 1 ] && { [ -n "${TRIPLEC_IDLE_STOP_MINUTES:-}" ] || [ -n "${TRIPLEC_MAX_LIFETIME_MINUTES//0/}" ]; }; then
start_idle_watch "${TRIPLEC_IDLE_STOP_MINUTES:-}" "$$"
fi
# Repos are cloned in the background from here on — see clone_repos.
# Repos are cloned in the background from here on — see clone_repos — and the
# setup script runs after them (aws_run_setup).
if [ "$triplec_aws" = 1 ]; then
start_clone_repos "$triplec_repos_json" /run/triple-c-clone
fi
+286
View File
@@ -0,0 +1,286 @@
#!/bin/bash
# triple-c-apply-connect — the workspace half of copy-at-connect (AWS only;
# phase 6, ruling Q2).
#
# A project whose credentials are "copied when you connect" keeps them out of
# its Secrets Manager secret: no TRIPLEC_FILES, no API keys, no GIT_TOKEN, no
# custom variables. Instead, after every connect (a start, an attach, the
# background re-attach when the app starts) the app, through the agent and as
# root:
# 1. runs `triple-c-apply-connect --prepare`, which makes this connect's
# own root-only staging dir on the /dev/shm tmpfs (never the disk),
# /dev/shm/triple-c/connect/<nonce>/{ssh,ca}, and prints <nonce>
# (16 hex digits);
# 2. uploads the SSH files to <nonce>/ssh/, the CA files to <nonce>/ca/ and
# the credential environment to <nonce>/connect.env (each root 0600;
# connect.env holds one `KEY=<base64 of the value>` line per variable);
# 3. runs `triple-c-apply-connect <nonce>`, which, under the apply lock,
# - installs the CA (install_corporate_ca, as root: its source and the
# system store are root-only paths),
# - merges the SSH files into ~/.ssh (merge_host_ssh, **as the user**:
# by now the user has been able to plant links in ~/.ssh, so root
# does not walk it; Hybrid/EFS's ~/.ssh link is checked the same way
# the entrypoint checks it),
# - writes the git credential helper when there is a GIT_TOKEN
# (install_git_credentials, as the user, the token on stdin),
# - leaves /run/triple-c/connected, which the waiting background clone
# of a fresh workspace starts on (start_clone_repos), and
# - re-clones, in the background and as the user with GIT_TOKEN in that
# clone's environment only, the repositories whose clone failed.
# The same functions the entrypoint runs, from entrypoint-lib.sh. The values
# are read into this script's own memory and never written anywhere else:
# connect.env is never sourced into, or exported to, anything but the clone,
# and the agent's environment (every terminal's) is untouched — the app adds
# the credential variables to the terminals it opens itself. Nothing here
# prints a value. Each connect stages and applies its own set, so two connects
# at once (two machines, a start racing a re-attach) never mix their files:
# their applies run one after the other, the later one's set wins. The apply
# removes its staging dir; the CA files move to /dev/shm/triple-c/connect-ca
# (a fixed path, so an unchanged CA is not reinstalled). Idempotent: a new
# connect with the same credentials converges on the same state.
# Re-cloning does not re-run the setup script.
set -u
# Fixed paths, never from the environment: this runs as root. (The tests
# substitute these lines.)
lib=/usr/local/lib/triple-c/entrypoint-lib.sh
shm=/dev/shm/triple-c
stage=/dev/shm/triple-c/connect
ca_dir=/dev/shm/triple-c/connect-ca
clone_status=/run/triple-c-clone
workspace=/workspace
# How long a retry waits for a clone that started without credentials.
retry_wait=2400
# The background re-clone's output goes to the container's log.
log_to=/proc/1/fd/1
# Where the SSH files are handed to the user (sticky, world-writable).
ssh_tmp_dir=/tmp
say() { echo "triple-c-apply-connect: $*"; }
if ! . "$lib"; then
say "$lib is missing; nothing applied"
exit 1
fi
triplec_aws=1
# The apply lock: applies run one at a time; --prepare takes it too, so its
# clean-up of stale staging dirs never races an apply.
take_lock() {
exec 9>"$shm/apply-connect.lock" && flock -w 120 9 \
|| say "warning — another apply is still running; going ahead"
}
prepare() {
local nonce
umask 077
if [ ! -d "$shm" ] || [ -L "$shm" ]; then
say "$shm is missing (the workspace has no agent token?); nothing prepared"
exit 1
fi
take_lock
if [ -L "$stage" ] || { [ -e "$stage" ] && [ ! -d "$stage" ]; }; then rm -f "$stage"; fi
mkdir -p -m 700 "$stage" && chmod 700 "$stage" || exit 1
# What a connect that never applied (the app gone mid-push) left.
find "$stage" -mindepth 1 -maxdepth 1 -mmin +10 -exec rm -rf {} + 2>/dev/null
nonce=$(od -An -tx1 -N8 /dev/urandom | tr -d ' \n')
case "$nonce" in [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ;; *) say "no random nonce"; exit 1 ;; esac
mkdir -m 700 "$stage/$nonce" "$stage/$nonce/ssh" "$stage/$nonce/ca" || exit 1
printf '%s\n' "$nonce"
}
# This connect's staging dir (set by apply).
dir=
# GIT_TOKEN, decoded from connect.env into this shell only.
connect_git_token=
read_git_token() {
local line key value
[ -f "$dir/connect.env" ] || return 0
# Split by hand at the first `=`: `read` with IFS='=' would drop a
# trailing `=`, which is base64 padding here.
while IFS= read -r line; do
key=${line%%=*}
value=${line#*=}
[ "$key" = GIT_TOKEN ] || continue
connect_git_token=$(printf '%s' "$value" | base64 -d 2>/dev/null) || connect_git_token=
done < "$dir/connect.env"
}
# The staged SSH files merged as the user: copied to a fresh root-made dir
# (mktemp in the sticky /tmp, never $TMPDIR: 0700, a new name nobody could
# have planted), handed to the user, merged by them, removed.
merge_ssh_as_user() {
local tmp rc=0
tmp=$(mktemp -d "${ssh_tmp_dir}/triple-c-connect.XXXXXXXX") \
|| { say "warning — no temporary directory; SSH files not merged"; return 0; }
if cp -R "$dir/ssh" "$tmp/ssh"; then
if [ -n "${triplec_run_as:-}" ] && [ "$(id -u)" = 0 ]; then
chown -R "$triplec_run_as:" "$tmp"
fi
_triplec_as_user bash -c '. "$1" && triplec_aws=1 && merge_host_ssh "$2"' _ "$lib" "$tmp/ssh" || rc=$?
[ "$rc" = 0 ] || say "warning — the SSH merge failed ($rc)"
else
say "warning — could not stage the SSH files; not merged"
fi
rm -rf "$tmp"
}
# Folders whose last recorded clone state is `failed`.
failed_folders() {
[ -f "$clone_status/repos" ] || return 0
awk -F'\t' '{ s[$1] = $2 } END { for (f in s) if (s[f] == "failed") print f }' "$clone_status/repos" | sort
}
# repos.json narrowed to the folders given as arguments.
repos_for() {
python3 -I -c '
import json, sys
want = set(sys.argv[1:])
try:
repos = json.load(sys.stdin)
except ValueError:
repos = []
print(json.dumps([r for r in repos if isinstance(r, dict) and r.get("folder") in want] if isinstance(repos, list) else []))
' "$@" < "$triplec_run_dir/repos.json"
}
# $1: `now` (the clone is done and `done` was just removed) or `after-done`
# (a clone that started without credentials is still running: wait for it).
start_retry() {
local mode="$1" out="$log_to"
[ -w "$out" ] || out=/dev/null
(
# Not the apply lock: this outlives the apply (up to retry_wait plus
# the clone), and a later connect must not wait for it.
exec 9>&-
trap '' HUP
if [ "$mode" = after-done ]; then
# Waits *without* the retry lock: a later connect that sees
# `done` first removes it and queues a `now` retry behind that
# lock, so a waiter holding it would never see `done` again.
# Any retry removes `no-credentials`; once it is gone someone
# else is cloning with credentials and this waiter has no job.
waited=0
while [ ! -e "$clone_status/done" ]; do
[ -e "$clone_status/no-credentials" ] || exit 0
[ "$waited" -lt "$retry_wait" ] || exit 0
sleep 2
waited=$((waited + 2))
done
exec 8>"$shm/clone-retry.lock" && flock 8
# Checked again under the lock: a `now` retry may have taken
# over (removed `done`) or already run (removed `no-credentials`).
[ -e "$clone_status/done" ] && [ -e "$clone_status/no-credentials" ] || exit 0
mapfile -t folders < <(failed_folders)
[ "${#folders[@]}" -gt 0 ] || exit 0
rm -f "$clone_status/done"
else
exec 8>"$shm/clone-retry.lock" && flock 8
mapfile -t folders < <(failed_folders)
fi
# From here the clone is running again; `done` however it ends.
trap ': > "$clone_status/done"' EXIT
rm -f "$clone_status/no-credentials"
[ "${#folders[@]}" -gt 0 ] || exit 0
echo "triple-c-apply-connect: cloning again with the project's credentials: ${folders[*]}"
json=$(repos_for "${folders[@]}")
# Authentication goes through ~/.git-credentials (written by the
# apply above), not this variable. GIT_TOKEN is exported only for
# whatever in the clone reads it directly (a user's credential
# helper or hook), and only into this clone, never the agent's env.
if [ -n "$connect_git_token" ]; then
export GIT_TOKEN="$connect_git_token"
fi
clone_repos "$json" "$workspace" "$clone_status"
) >>"$out" 2>&1 </dev/null &
}
# Only clones that failed, and only once whatever clone is running is done.
retry_failed_clones() {
[ -d "$clone_status" ] && [ -s "$triplec_run_dir/repos.json" ] || return 0
if [ -e "$clone_status/done" ]; then
[ -n "$(failed_folders)" ] || return 0
# Before returning, so a status read never sees the old failure as
# final: the clone is running again from now.
rm -f "$clone_status/done"
start_retry now
elif [ -e "$clone_status/no-credentials" ]; then
start_retry after-done
fi
# Otherwise the first clone is still waiting for this connect (it starts
# now, with the credentials) or already cloning with them.
}
# Puts this connect's CA files ($1) at $ca_dir, or removes $ca_dir when the
# connect brought none. The new files go to a temporary name first and are
# swapped in, so a failed move leaves the CA installed before still there.
replace_ca_dir() {
local src="$1" new="$ca_dir.new" old="$ca_dir.old"
if [ ! -d "$src" ]; then
rm -rf "$ca_dir"
return 0
fi
rm -rf "$new" "$old"
if ! mv -T "$src" "$new"; then
say "warning — could not move the CA files into place; keeping the ones from before"
return 0
fi
if [ -e "$ca_dir" ] && ! mv -T "$ca_dir" "$old"; then
say "warning — could not replace the CA files; keeping the ones from before"
rm -rf "$new"
return 0
fi
if ! mv -T "$new" "$ca_dir"; then
say "warning — could not move the CA files into place; keeping the ones from before"
[ ! -e "$old" ] || mv -T "$old" "$ca_dir"
rm -rf "$new"
return 0
fi
rm -rf "$old"
}
apply() {
local nonce="$1"
umask 077
case "$nonce" in
*[!0-9a-f]*|'') say "not a connect nonce: $nonce"; exit 2 ;;
esac
[ "${#nonce}" = 16 ] || { say "not a connect nonce: $nonce"; exit 2; }
dir="$stage/$nonce"
if [ ! -d "$dir" ] || [ -L "$dir" ] || [ -L "$stage" ]; then
say "$dir is missing; run --prepare and upload first"
exit 1
fi
take_lock
# Empty staging dirs: nothing of that kind is configured.
rmdir "$dir/ca" "$dir/ssh" 2>/dev/null
replace_ca_dir "$dir/ca"
CA_SRC="$ca_dir"
install_corporate_ca
if [ -d "$dir/ssh" ]; then
merge_ssh_as_user
fi
read_git_token
if [ -n "$connect_git_token" ]; then
printf '%s' "$connect_git_token" \
| _triplec_as_user bash -c '. "$1" && install_git_credentials "$(cat)"' _ "$lib" \
|| say "warning — could not write the git credential helper"
fi
if mkdir -p "$triplec_run_dir" && : > "$triplec_run_dir/connected"; then :; else
say "warning — could not record the connect in $triplec_run_dir"
fi
retry_failed_clones
rm -rf "$dir"
say "credentials applied"
}
case "${1:-}" in
--prepare) prepare ;;
''|-*) say "usage: triple-c-apply-connect --prepare | <nonce>"; exit 2 ;;
*) apply "$1" ;;
esac
+81 -4
View File
@@ -15,7 +15,8 @@
# * no `claude` process is running;
# * no scheduler job is running (`triple-c-scheduler status`, as claude);
# * the start's background repository clone has finished (entrypoint.sh,
# start_clone_repos: /run/triple-c-clone exists without `done`);
# start_clone_repos: /run/triple-c-clone exists without `done`), and so
# has the setup script (/run/triple-c-setup/status says `running`);
# * no work would be lost: `triple-c-wip-push --dry-run` (as claude) finds
# no dirty repository and no unpushed commit. With the WIP push on
# (TRIPLEC_WIP_PUSH=1) such work is pushed here, before the stop, and the
@@ -39,13 +40,26 @@
# forward the signal, but signalling the entrypoint directly does not depend on
# that and also works without an init.
#
# Lifetime cap (phase 6, Q8; TRIPLEC_MAX_LIFETIME_MINUTES, 0 or empty = none):
# once the run's start (the entrypoint's /run/triple-c/start-epoch) is that
# many minutes ago, whatever the activity, it pushes work in progress first
# when the WIP push is on (best effort, bounded by TRIPLEC_WIP_TIMEOUT) and
# then records "lifetime" and sends SIGTERM even if that push failed; the
# entrypoint exits 76. A `pushed`/`discard` marker (the app is stopping it
# already) is kept, with no push of its own. The cap is advisory: a workspace
# user has sudo and could stop this watcher.
#
# Either the idle stop or the lifetime cap must be configured; the watcher
# runs when at least one is.
#
# --check evaluates once and exits 0 (idle and nothing unsaved, or unsaved
# work the WIP push would take) or 1 (not idle); for tests and for looking at
# a live task. It never pushes.
#
# Configuration (environment; the entrypoint sets the first four, the rest
# exist for tests):
# TRIPLEC_IDLE_STOP_MINUTES N (required, > 0)
# TRIPLEC_IDLE_STOP_MINUTES N (> 0; empty: no idle stop)
# TRIPLEC_MAX_LIFETIME_MINUTES the lifetime cap (empty or 0: none)
# TRIPLEC_IDLE_SIGNAL_PID the entrypoint's PID
# TRIPLEC_AGENT_TOKEN_FILE default /dev/shm/triple-c/agent-token
# TRIPLEC_WIP_PUSH 1: push unsaved work before an idle stop
@@ -60,6 +74,8 @@
# TRIPLEC_WIP_BACKOFF_MAX longest wait between push retries, default 1800
# TRIPLEC_IDLE_PROC_NAME default claude
# TRIPLEC_CLONE_STATUS_DIR default /run/triple-c-clone
# TRIPLEC_SETUP_STATUS_DIR default /run/triple-c-setup
# TRIPLEC_START_EPOCH_FILE default /run/triple-c/start-epoch
set -u
@@ -77,6 +93,9 @@ wip_push="${TRIPLEC_WIP_PUSH:-0}"
wip_bin="${TRIPLEC_WIP_PUSH_BIN:-/usr/local/bin/triple-c-wip-push}"
wip_timeout="${TRIPLEC_WIP_TIMEOUT:-90}"
backoff_max="${TRIPLEC_WIP_BACKOFF_MAX:-1800}"
setup_dir="${TRIPLEC_SETUP_STATUS_DIR:-/run/triple-c-setup}"
lifetime="${TRIPLEC_MAX_LIFETIME_MINUTES:-}"
start_file="${TRIPLEC_START_EPOCH_FILE:-/run/triple-c/start-epoch}"
# >>> is_idle
# is_idle now last_activity_unix claude_running jobs_running minutes
@@ -131,6 +150,11 @@ clone_running() {
[ -d "$clone_dir" ] && [ ! -e "$clone_dir/done" ]
}
# So does the setup script (entrypoint.sh, aws_run_setup).
setup_running() {
[ "$(head -n 1 "$setup_dir/status" 2>/dev/null)" = running ]
}
# The folders with unsaved work (dirty, or commits no remote has), one per
# line; nothing when there is none. Fails when it cannot tell.
unsaved_work() {
@@ -146,6 +170,7 @@ quiet() {
if pgrep -x "$proc_name" >/dev/null 2>&1; then claude=1; else claude=0; fi
jobs=$(jobs_running)
clone_running && jobs=1
setup_running && jobs=1
is_idle "$(date +%s)" "${last:-}" "$claude" "$jobs" "$minutes"
}
@@ -166,22 +191,73 @@ check() {
}
case "$minutes" in
''|*[!0-9]*|0) echo "triple-c-idle-watch: TRIPLEC_IDLE_STOP_MINUTES must be a positive integer" >&2; exit 2 ;;
'') ;;
*[!0-9]*) echo "triple-c-idle-watch: TRIPLEC_IDLE_STOP_MINUTES must be a positive integer" >&2; exit 2 ;;
*) minutes=$((10#$minutes))
[ "$minutes" -gt 0 ] || { echo "triple-c-idle-watch: TRIPLEC_IDLE_STOP_MINUTES must be a positive integer" >&2; exit 2; } ;;
esac
case "$lifetime" in
'') ;;
*[!0-9]*) echo "triple-c-idle-watch: TRIPLEC_MAX_LIFETIME_MINUTES must be a whole number of minutes" >&2; exit 2 ;;
# Decimal even with leading zeros (`08` is 8); 0, 00, … mean no cap.
*) lifetime=$((10#$lifetime)); [ "$lifetime" -gt 0 ] || lifetime="" ;;
esac
if [ "${1:-}" = "--check" ]; then
[ -n "$minutes" ] || { echo "triple-c-idle-watch: --check needs TRIPLEC_IDLE_STOP_MINUTES" >&2; exit 2; }
check
exit $?
fi
if [ -z "$minutes" ] && [ -z "$lifetime" ]; then
echo "triple-c-idle-watch: neither TRIPLEC_IDLE_STOP_MINUTES nor TRIPLEC_MAX_LIFETIME_MINUTES is set — nothing to watch" >&2
exit 2
fi
case "$signal_pid" in
''|*[!0-9]*|0|1) echo "triple-c-idle-watch: TRIPLEC_IDLE_SIGNAL_PID must be the entrypoint's PID" >&2; exit 2 ;;
esac
# The run's start, for the lifetime cap. The entrypoint wrote it before
# starting this watcher; should it be unreadable the cap counts from now
# (later rather than never).
started_at=$(head -n 1 "$start_file" 2>/dev/null)
case "$started_at" in
''|*[!0-9]*)
[ -z "$lifetime" ] || echo "triple-c-idle-watch: no start time in $start_file — the lifetime cap counts from now" >&2
started_at=$(date +%s) ;;
esac
lifetime_reached() {
[ -n "$lifetime" ] && [ $(( $(date +%s) - started_at )) -ge $((lifetime * 60)) ]
}
# The cap is reached: push (best effort) unless the app has decided already,
# record "lifetime", signal the entrypoint and end.
lifetime_stop() {
echo "triple-c-idle-watch: the environment's maximum workspace lifetime (${lifetime} minutes) was reached — stopping the workspace"
mkdir -p "$(dirname "$reason_file")" 2>/dev/null
case "$(cat "$reason_file" 2>/dev/null)" in
pushed|discard)
echo "triple-c-idle-watch: Triple-C is already stopping it — keeping its decision" ;;
*)
if [ "$wip_push" = 1 ]; then
echo "triple-c-idle-watch: pushing work in progress before the stop..."
as_user timeout -k 5 "$wip_timeout" "$wip_bin" --reason lifetime \
|| echo "triple-c-idle-watch: the WIP push failed — stopping anyway (the lifetime cap is reached)"
fi
printf 'lifetime\n' > "$reason_file" ;;
esac
kill -TERM "$signal_pid"
exit 0
}
push_failures=0
next_push_at=0
while :; do
sleep "$interval"
lifetime_reached && lifetime_stop
[ -n "$minutes" ] || continue
if ! quiet; then
said=""
continue
@@ -220,8 +296,9 @@ while :; do
mkdir -p "$(dirname "$reason_file")" 2>/dev/null
# A pushed/discard marker means the app's stop guard has decided and
# its stop is under way: keep that decision rather than overwrite it.
# (Nor a lifetime stop's marker, should one be there.)
case "$(cat "$reason_file" 2>/dev/null)" in
pushed|discard) ;;
pushed|discard|lifetime) ;;
*) printf 'idle\n' > "$reason_file" ;;
esac
kill -TERM "$signal_pid"
+78 -9
View File
@@ -1,11 +1,27 @@
#!/bin/bash
# triple-c-wip-push [--dry-run] [--reason idle|stop|sigterm]
# triple-c-wip-push [--dry-run] [--reason idle|stop|sigterm|lifetime]
#
# Save unpushed work before an AWS workspace stops. For every git repository
# directly under /workspace whose working tree is dirty or which holds commits
# no remote has, push a snapshot of the *whole* working tree (tracked changes,
# staged or not, and untracked files .gitignore does not exclude) to
# refs/heads/triple-c/wip/<UTC yyyymmdd-HHMMSS> on the repo's remote.
# refs/heads/triple-c/wip/<run-id>-<folder> on the repo's remote.
#
# One branch per task run and folder (phase 6, Q9; ruling R4 — two folders
# can share a remote, so the folder is part of the name): <run-id> is the
# entrypoint's /run/triple-c/run-id, `<UTC yyyymmdd-HHMMSS>-<task id, first 8
# | local>`; <folder> is the folder name with everything outside
# [A-Za-z0-9._-] made `_`, cut to 64 characters, or — when the ref still is
# not valid (`git check-ref-format`: `..`, a `.lock` end, …) — the first 12
# hex digits of the folder name's SHA-256.
# Later pushes in the same run move that branch (a forced push — the branch is
# Triple-C's alone, and each WIP commit sits on the user's HEAD, not on the
# last WIP commit), and a push whose tree equals the one last pushed for that
# folder in this run is skipped: the tree is recorded after each successful
# push in /run/triple-c/wip-last/<folder> (made for `claude` by the
# entrypoint). Without a usable run id (Docker, or a run that recorded none)
# every push gets its own refs/heads/triple-c/wip/<UTC yyyymmdd-HHMMSS>-<folder>,
# never forced.
#
# It never touches the user's index, HEAD, branches or working tree: the
# snapshot is built in a temporary index (GIT_INDEX_FILE) seeded from a copy of
@@ -32,12 +48,15 @@
# any failed, 2 on bad usage.
#
# TRIPLEC_WORKSPACE_ROOT overrides /workspace, TRIPLEC_CLONE_STATUS_DIR
# /run/triple-c-clone (tests).
# /run/triple-c-clone, TRIPLEC_RUN_ID_FILE /run/triple-c/run-id and
# TRIPLEC_WIP_LAST_DIR /run/triple-c/wip-last (tests).
set -u
root="${TRIPLEC_WORKSPACE_ROOT:-/workspace}"
clone_dir="${TRIPLEC_CLONE_STATUS_DIR:-/run/triple-c-clone}"
run_id_file="${TRIPLEC_RUN_ID_FILE:-/run/triple-c/run-id}"
last_dir="${TRIPLEC_WIP_LAST_DIR:-/run/triple-c/wip-last}"
dry_run=0
reason=stop
@@ -46,10 +65,10 @@ while [ $# -gt 0 ]; do
--dry-run) dry_run=1; shift ;;
--reason)
case "${2:-}" in
idle|stop|sigterm) reason="$2"; shift 2 ;;
*) echo "triple-c-wip-push: --reason must be idle, stop or sigterm" >&2; exit 2 ;;
idle|stop|sigterm|lifetime) reason="$2"; shift 2 ;;
*) echo "triple-c-wip-push: --reason must be idle, stop, sigterm or lifetime" >&2; exit 2 ;;
esac ;;
*) echo "usage: triple-c-wip-push [--dry-run] [--reason idle|stop|sigterm]" >&2; exit 2 ;;
*) echo "usage: triple-c-wip-push [--dry-run] [--reason idle|stop|sigterm|lifetime]" >&2; exit 2 ;;
esac
done
@@ -60,6 +79,32 @@ shopt -s dotglob nullglob
stamp=$(date -u +%Y%m%d-%H%M%S)
failed=0
# This run's id, when the entrypoint recorded a well-formed one; the per-push
# stamp otherwise. Only our own per-run branches are ever force-pushed.
run_id=$(head -n 1 "$run_id_file" 2>/dev/null)
if [[ "$run_id" =~ ^[0-9]{8}-[0-9]{6}-[A-Za-z0-9]{1,32}$ ]]; then
ref_base="refs/heads/triple-c/wip/$run_id"
force=+
else
ref_base="refs/heads/triple-c/wip/$stamp"
force=
fi
# The WIP ref for folder $1: <base>-<folder>; <base>-<sanitised folder>-<hash>
# when the name had to change; or <base>-<hash> when no ref can carry it.
wip_ref() {
local safe r
safe=$(printf '%s' "$1" | LC_ALL=C tr -c 'A-Za-z0-9._-' '_' | cut -c 1-64)
r="$ref_base-$safe"
# Sanitising (or the cut) changed the name: add a hash of the raw name,
# so `a b` and `a_b` never share a branch.
[ "$safe" = "$1" ] || r="$r-$(printf '%s' "$1" | sha256sum | cut -c 1-8)"
if [ -z "$safe" ] || ! git check-ref-format "$r"; then
r="$ref_base-$(printf '%s' "$1" | sha256sum | cut -c 1-12)"
fi
printf '%s' "$r"
}
# The remote to push to: the branch's upstream remote, else origin, else the
# only remote. Empty when there is none.
pick_remote() {
@@ -83,6 +128,8 @@ user_ssh_command="${GIT_SSH_COMMAND:-}"
for dir in "$root"/*/; do
dir="${dir%/}"
[ -d "$dir" ] || continue
# clone_repos' scratch (entrypoint.sh): half-made clones, never the user's.
[ "$(basename "$dir")" = .triple-c-clone ] && continue
# A repository rooted exactly here. A symlinked folder resolves elsewhere
# and is skipped: it is not the workspace's own.
top=$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null) || continue
@@ -140,6 +187,7 @@ for dir in "$root"/*/; do
fi
commit="$head"
tree=""
if [ "$dirty" = true ]; then
tmp_index=$(mktemp) || exit 1
trap 'rm -f "$tmp_index"' EXIT
@@ -157,16 +205,29 @@ for dir in "$root"/*/; do
git config user.email >/dev/null 2>&1 || export GIT_AUTHOR_EMAIL=triple-c@localhost GIT_COMMITTER_EMAIL=triple-c@localhost
git add -A . >/dev/null 2>&1 || { echo "triple-c-wip-push: $folder: could not stage the working tree" >&2; exit 1; }
tree=$(git write-tree) || exit 1
unset GIT_INDEX_FILE
else
tree=$(git rev-parse -q --verify "$head^{tree}") || exit 1
fi
# Pushed already in this run, unchanged since: nothing new to save.
last_file="$last_dir/$folder"
if [ -n "$tree" ] && [ -f "$last_file" ] \
&& [ "$(head -n 1 "$last_file" 2>/dev/null)" = "$tree $remote" ]; then
echo "triple-c-wip-push: $folder: unchanged since the last push of this run — skipped"
exit 0
fi
ref=$(wip_ref "$folder")
if [ "$dirty" = true ]; then
msg="Triple-C WIP ($reason) of $branch at $stamp UTC"
if [ -n "$head" ]; then
commit=$(git commit-tree "$tree" -p "$head" -m "$msg") || exit 1
else
commit=$(git commit-tree "$tree" -m "$msg") || exit 1
fi
unset GIT_INDEX_FILE
fi
ref="refs/heads/triple-c/wip/$stamp"
# --no-verify skips every pre-push hook, Git LFS's upload among them.
# So when git-lfs is here and the snapshot has LFS files, upload their
# objects first — best effort: the push still goes, with a warning.
@@ -178,8 +239,16 @@ for dir in "$root"/*/; do
echo "triple-c-wip-push: $folder: warning — LFS upload failed, large files may be missing on the branch: $(printf '%s' "$lfs_err" | tail -n 1)" >&2
fi
fi
if err=$(git push --no-verify -q "$remote" "$commit:$ref" 2>&1); then
if err=$(git push --no-verify -q "$remote" "$force$commit:$ref" 2>&1); then
echo "triple-c-wip-push: $folder: pushed to $remote $ref"
# Remember what this run pushed (best effort: without it the next
# push just pushes again). Only where the entrypoint made the dir.
if [ -d "$last_dir" ] && [ -w "$last_dir" ]; then
tmp_last=$(mktemp "$last_dir/.tmp.XXXXXX" 2>/dev/null) \
&& printf '%s %s\n' "$tree" "$remote" > "$tmp_last" \
&& mv -f "$tmp_last" "$last_file" \
|| rm -f "${tmp_last:-}"
fi
else
echo "triple-c-wip-push: $folder: push to $remote failed: $(printf '%s' "$err" | tail -n 1)" >&2
exit 1
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/python3 -I
# triple-c-workspace-status
#
# An AWS workspace's start-up progress as one JSON object on one line, for the
# app's start command and Project Home (phase 6, Q4):
#
# {"clone": {"done": bool, "repos": [{"folder": str, "state": str}, ...]},
# "setup": {"state": "none"|"running"|"ok"|"failed",
# "code": int|null, "tail": [the setup log's last 20 lines]}}
#
# Read from what entrypoint.sh writes (root-owned, world-readable, so this
# works as `claude` and as root):
# /run/triple-c-clone `repos` ("<folder>\t<state>" lines; a folder's last
# line is its state: cloning, cloned, failed, exists)
# and `done`. No directory yet: the clone has not
# started (done: false).
# /run/triple-c-setup `status` (`running`, `ok`, `failed <code>`; absent:
# no setup script) and `log`.
# A missing or unreadable file is reported as its empty state, never an
# error; a status line it does not know is a failure without a code. Nothing
# else is read: no environment, no secrets — the tail is the setup script's
# own output and nothing more.
#
# TRIPLEC_CLONE_STATUS_DIR and TRIPLEC_SETUP_STATUS_DIR override the two
# directories (tests).
import json
import os
import sys
TAIL_LINES = 20
# Enough for 20 long lines; the log can be large and only its end matters.
TAIL_BYTES = 64 * 1024
MAX_LINE = 1000
clone_dir = os.environ.get("TRIPLEC_CLONE_STATUS_DIR") or "/run/triple-c-clone"
setup_dir = os.environ.get("TRIPLEC_SETUP_STATUS_DIR") or "/run/triple-c-setup"
def read_text(path, limit=None):
"""The file's text (its last `limit` bytes when given), or None."""
try:
with open(path, "rb") as f:
if limit is not None:
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(max(0, size - limit))
data = f.read() if limit is not None else f.read(1024 * 1024)
except (OSError, ValueError):
return None
return data.decode("utf-8", errors="replace")
def clone_status():
done = os.path.exists(os.path.join(clone_dir, "done"))
states = {}
for line in (read_text(os.path.join(clone_dir, "repos")) or "").splitlines():
folder, sep, state = line.partition("\t")
if sep and folder:
states[folder] = state # dicts keep first-seen order
return {"done": done, "repos": [{"folder": f, "state": s} for f, s in states.items()]}
def setup_status():
raw = read_text(os.path.join(setup_dir, "status"))
if raw is None:
state, code = "none", None
else:
word, _, rest = raw.strip().partition(" ")
if word == "running" and not rest:
state, code = "running", None
elif word == "ok" and not rest:
state, code = "ok", 0
else:
state = "failed"
code = int(rest) if word == "failed" and rest.strip().isdigit() else None
log = read_text(os.path.join(setup_dir, "log"), TAIL_BYTES) or ""
tail = [l[:MAX_LINE] for l in log.splitlines()[-TAIL_LINES:]]
return {"state": state, "code": code, "tail": tail}
json.dump({"clone": clone_status(), "setup": setup_status()}, sys.stdout, ensure_ascii=False,
separators=(",", ":"))
sys.stdout.write("\n")
@@ -0,0 +1,121 @@
# AWS Host Phase 6 Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Finish the Fargate workspace feature set: EFS storage, Copy-at-connect, setup scripts, a cost estimate, and the four phase 5 deferrals (port mappings, LLM gateway sidecar, the admin's maximum workspace lifetime, quieter WIP and clone handling).
**Architecture:** Everything builds on phase 5's shapes: one secret per project carries all project data (P1); the task definition gains a second EFS volume (EFS mode) and an optional `gateway` sidecar container; the entrypoint's `aws` mode gains a setup-script runner, a lifetime cap and a re-runnable "apply credentials" script for Copy-at-connect; the app follows clone and setup progress through the agent after connecting, forwards port mappings over the existing socat-over-attach tunnel, and prices a workspace from the AWS Pricing API.
**Tech Stack:** Rust (aws-sdk-ecs/-efs/-secretsmanager, + aws-sdk-pricing), OpenTofu (environment module), bash entrypoint and container scripts, React/TS.
**Spec:** `docs/superpowers/specs/2026-11-13-aws-ecs-host-design.md` (Storage, Secrets and uploads → Copy at connect, Snapshots → setup script, Multi-user accounts → Cost visibility, Lifecycle → idle auto-stop "the admin's maximum lifetime is a hard cap", Feature compatibility → port mappings and LLM gateway sidecar). Phase 5's rulings P1–P9 and R1–R11 (spec "Phase 5: rulings") still bind.
## Decisions this plan makes (rulings)
- **Q1. EFS mode uses a second access point mounted at `/workspace`.** Root `/triple-c/<owner_key>/<project>/workspace` would nest inside the home access point, so it gets a sibling root: `/triple-c-workspaces/<owner_key>/<project-component>`, POSIX 1000:1000, 0700, on the user's own file system. Hybrid's home access point and symlinks stay as they are; EFS = Hybrid + `/workspace` on EFS. Store-owned field `workspace_access_point_id`. Switching EFS → Hybrid/Ephemeral leaves the workspace access point (and its files) in place until "Remove AWS resources" removes it, said in the UI.
- **Q2. Copy-at-connect keeps settings, moves credentials.** With `secrets_delivery: CopyAtConnect` the project secret still carries non-credential project data (P1) and the agent token; it drops `TRIPLEC_FILES` and every credential env pair (`SECRET_ENV_KEYS` and all custom env values). After each connect the app uploads the files to `/tmp/.host-ssh` / `/tmp/.host-ca` and an env file to `/dev/shm/triple-c/connect.env` (root, 0600) through the agent, then runs a new root script `triple-c-apply-connect` (CA install, SSH merge, git credential helper — the entrypoint's own functions, moved into `/usr/local/lib/triple-c/entrypoint-lib.sh` so both call the same code) and re-runs the clone for repos that failed. Terminal sessions the app opens add the connect env to their `ExecSpec.env`. Scheduled jobs while the app is closed don't get credentials (spec says so; the UI says so). The spec's "persist to EFS" opt-in is **not** built.
- **Q3. Setup script: AWS only, run as `claude` after the clone, before the workspace counts as ready.** Stored in `AwsWorkspaceConfig.setup_script` (≤ 32 KiB), delivered as payload key `TRIPLEC_SETUP_SCRIPT` (P1). The entrypoint writes it to `/run/triple-c-setup/script`, runs `bash -e` as `claude` (who has sudo for apt) with a 30-minute timeout after the background clone finishes, logging to `/run/triple-c-setup/log` and writing `/run/triple-c-setup/status` (`running` | `ok` | `failed <code>`). The idle watcher treats a running setup like a running clone.
- **Q4. Start follows clone and setup.** After "Connecting…", the start command polls one new agent-side status read (`triple-c-workspace-status`, JSON of clone + setup state) every 2 s and emits each new state/log line as `container-progress` ("Cloning repos… (1/3)", "Running setup script…", last log line), until both are done or 35 minutes pass. A failed clone or setup does not fail the start: the project is Running and a warning toast carries the last lines. Project Home Overview shows the same status for AWS projects (still cloning / setup failed).
- **Q5. Cost estimate from the Pricing API, cached a day.** `pricing:GetProducts` (Resource `*`; the API has no resource-level permissions) goes into `workspace-user-tasks.json`. Prices come from the `AmazonECS` service code in `us-east-1`, filtered to the region and Fargate vCPU-hours / GB-hours / ephemeral storage GB-hours for x86 and ARM; cached on disk per region for 24 h. Spot isn't in the Pricing API: the UI shows the on-demand figure with "Spot is usually up to 70% cheaper". EFS storage isn't per-hour and isn't estimated. Shown in HostSection (as you configure) and Project Home Overview (while running). Denied or offline → "Estimate unavailable", never an error.
- **Q6. Port mappings reuse the auth bridge's tunnel.** `auth_bridge::tunnel` already forwards a host loopback port into the container over socat-over-attach, which works through the agent. Generalise `PortForward::bind` to take a host port and a container port. AWS mappings bind **loopback only** (Docker binds 0.0.0.0; the UI says so), TCP only (UDP refused for AWS projects), opened after connect and closed on stop/exit. No new `/v1/tunnel` client.
- **Q7. LLM gateway as a sidecar, opt-in per project.** `AwsWorkspaceConfig.gateway_sidecar: bool`. When on, the task definition adds a non-essential `gateway` container running the app's pinned LiteLLM image (`docker::gateway` registry ref, digest-pinned), with `memoryReservation` 512 MiB; its config YAML and keys arrive as payload keys `TRIPLEC_GATEWAY_CONFIG`, `TRIPLEC_GATEWAY_API_KEY`, `TRIPLEC_GATEWAY_MASTER_KEY` (rendered with the same `render_config` as Docker, keys via `os.environ/…` references) and the container's command writes the config to `/tmp` and starts LiteLLM on `127.0.0.1:4000`. The project must use the OpenAI-compatible backend; the app then points `ANTHROPIC_BASE_URL` at `http://127.0.0.1:4000` and `ANTHROPIC_AUTH_TOKEN` at the master key, regardless of the base URL typed for local use. The task's CPU/memory must leave room: the minimum for a sidecar project is 1 vCPU / 2 GiB (validated).
- **Q8. Lifetime cap is enforced inside the workspace (advisory), set per environment.** Environment module variable `max_workspace_hours` (0 = no cap, default 0) → record field `max_workspace_hours` (record schema stays 1; unknown-field tolerant parser already); `ENVIRONMENT_MODULE_VERSION` 1 → 2 so existing environments show "Update available". The app passes `TRIPLEC_MAX_LIFETIME_MINUTES`; the idle watcher stops the task when it's reached — WIP push first (best effort, 90 s), then stop even if the push failed — with exit status 76 (`StopKind::LifetimeCap`, toast "Stopped: the environment's maximum workspace lifetime"). The UI shows the cap in HostSection. **This is advisory**: a workspace user has sudo inside their own task and could disable it. Server-side enforcement (a scheduled Lambda that stops old tasks) is listed as a follow-up, not built. (Chosen by the user 2026-11-15.)
- **Q9. One WIP branch per task run, updated in place.** The branch becomes `triple-c/wip/<task-start-stamp>-<task-id-8>` (stamp written by the entrypoint at start into `/run/triple-c/run-id`; Docker/no-task fallback keeps today's per-push stamp); later pushes in the same run overwrite it (`+` refspec — the branch is ours alone), and a push whose tree equals the last one pushed for that repo in this run is skipped (tree hash kept in `/run/triple-c/wip-last/<folder>`).
## Global Constraints
- Everything in the phase 5 Global Constraints still holds (no `aws_config::defaults`; three tag keys only; names built in `aws/tofu_modules.rs`; no project data in task definitions — P1; container `workspace`; health check unchanged; the CPU/memory table incl. 256 → 512, 1024 or 2048 MiB; host/ boundary tests; policy + checks + README together, ≤ 6,144 characters each; three-part command registration; serde defaults with TS mirrors; live tests only through the scratchpad `awsx` wrapper, everything cleaned up, total spend cap $10).
- Docker behaviour unchanged unless a task says otherwise; every entrypoint addition gated on `TRIPLEC_HOST=aws`.
- New payload keys use the `TRIPLEC_` prefix (reserved since phase 5) and stay under the 64 KiB secret cap; the sizes add up (setup script ≤ 32 KiB + gateway config ≤ 8 KiB), so the cap error must name what is large.
- Root-run entrypoint code never follows a link the `claude` user controls (phase 5 hardening rule).
## Review Focus
1. A Copy-at-connect project started while the app is closed (scheduler) or before the first connect: private clones fail cleanly, the workspace still starts, and connecting later clones them — Task 6.
2. A setup script that hangs or needs input: bounded by its timeout, stdin is `/dev/null`, the start finishes with a warning, idle stop still works afterwards — Task 4.
3. A project switched between Hybrid and EFS with repos already present: no silent data loss, the UI says where the old files are — Task 3.
4. Two port mappings to the same host port, or a host port already in use on this machine: a clear per-mapping error; the other mappings still work — Task 7.
5. Pricing API slow/denied/offline: the config UI never blocks or errors — Task 9.
---
### Task 1: Model and payload keys
**Files:** `app/src-tauri/src/models/project.rs`, `app/src/lib/types.ts`, `app/src/lib/awsWorkspace.ts`, `app/src-tauri/src/commands/project_commands.rs` (store-owned fields), `storage/projects_store.rs`.
**Interfaces — Produces:** `StorageMode::Efs`; `SecretsDelivery { SecretsManager (default), CopyAtConnect }`; `AwsWorkspaceConfig.{secrets_delivery, setup_script: Option<String>, gateway_sidecar: bool, workspace_access_point_id: Option<String>}` (last one store-owned; `set_aws_runtime` gains it); `validate_aws_workspace` additions: setup script ≤ 32,768 bytes and no NUL; `gateway_sidecar` requires cpu ≥ 1024 and memory ≥ 2048; TS mirrors.
- [ ] Tests: old JSON loads (`secrets_delivery` = SecretsManager, `gateway_sidecar` false, `setup_script` None); `efs` round-trips; script size limit; sidecar minimum; `workspace_access_point_id` survives `update_project`.
- [ ] Commit `feat(aws): phase 6 project settings`.
### Task 2: Environment lifetime cap (module + record)
**Files:** `aws/terraform/environment/{variables.tf,main.tf}`, `aws/terraform/environment/.terraform.lock.hcl` (unchanged unless providers move), `app/src-tauri/src/aws/{tofu_modules.rs,environments.rs}` (+ tests), `app/src/components/settings/hosts/DeployEnvironmentModal.tsx`, `aws/README.md`.
**Interfaces — Produces:** variable `max_workspace_hours` (number, default 0, validation 0–168); record field `max_workspace_hours`; `EnvironmentInfo.max_workspace_hours: u32` (absent → 0); `environment_vars(env, region, &NetworkChoice, max_hours: u32)`; `ENVIRONMENT_MODULE_VERSION = 2` and `locals.template_version = 2`; deploy/update modal field "Maximum workspace lifetime (hours, 0 = none)".
- [ ] Tests: parser accepts a record without the field (0) and with it; rejects > 168; the HCL/const pinning tests updated (record field list, template version, variable); a v1 record now reads as UpdateAvailable.
- [ ] Commit `feat(aws): environments can cap workspace lifetime`.
### Task 3: EFS storage mode
**Files:** `aws/workspace.rs` (+ tests), `aws/tofu_modules.rs` (`workspace_access_point_root`), `aws/runtime.rs` (removal), `host/aws_host.rs` (wait for both access points), `commands/project_commands.rs` (`remove_aws_resources` and remove flow), `HostSection.tsx`, `ConfirmRemoveModal.tsx`, `awsWorkspace.ts` (`hasSavedFiles`).
**Interfaces — Produces:** `workspace_access_point_root(owner_key, project_id) -> String` = `/triple-c-workspaces/<owner_key>/<project-component>`; `WorkspacePlan.workspace_access_point_id: Option<String>`; second volume `workspace` mounted at `/workspace` when storage is Efs; `PreparedWorkspace.workspace_access_point_id`; removal with `discard_data` deletes both access points; the EFS segment enabled with the hint "Everything in /workspace survives a stop; git and builds are slower over NFS".
- [ ] Tests: Efs request has both volumes and mount points (home at `/mnt/triple-c-home`, workspace at `/workspace`), Hybrid unchanged (fingerprint of a Hybrid plan identical to before this task), the workspace access point is reused by root+owner+project tags, removal deletes both, switching Efs → Hybrid keeps `workspace_access_point_id` and the UI note.
- [ ] Commit `feat(aws): EFS storage mode`.
### Task 4: Container: setup script, lifetime cap, WIP per run, status script
**Files:** `container/entrypoint.sh`, `container/triple-c-idle-watch`, `container/triple-c-wip-push`, new `container/triple-c-workspace-status`, `container/Dockerfile` (COPY), `app/src-tauri/src/docker/aws_mode_tests.rs`, `app/src-tauri/src/aws/tasks.rs` (`LIFETIME_EXIT_CODE = 76`, `StopKind::LifetimeCap`).
**Interfaces — Produces (env contract):** `TRIPLEC_SETUP_SCRIPT` (Q3), `TRIPLEC_MAX_LIFETIME_MINUTES` (absent = none); `/run/triple-c/run-id` = `<UTC yyyymmdd-HHMMSS>-<task-id-8 or "local">`; `triple-c-workspace-status` prints one JSON object `{"clone":{"done":bool,"repos":[{"folder","state"}]},"setup":{"state":"none|running|ok|failed","code":int|null,"tail":[last 20 lines]}}`; WIP branch per Q9.
- [ ] Tests (bash, existing harness): setup runs after clone `done`, as claude, stdin `/dev/null`, timeout kills a hang and records `failed 124`; idle watcher not idle while setup `running`; lifetime reached → WIP push attempted, then TERM with exit 76 even if the push failed; two pushes in one run update the same branch and an unchanged tree is skipped; status JSON shape for none/running/ok/failed; Docker mode untouched (gating test).
- [ ] `docker build ./container` (amd64) succeeds.
- [ ] Commit `feat(container): setup script, lifetime cap and one WIP branch per run`.
### Task 5: App: payload, start follow, status UI
**Files:** `aws/workspace.rs` (payload keys), `host/aws_host.rs` (`follow_workspace_status`), `commands/project_commands.rs` (new command `aws_workspace_status(project_id)`), `lib.rs`, `capabilities/default.json`, `tauri-commands.ts`, `components/projects/home/OverviewTab.tsx` (AWS status card), `hooks/useProjectActions.ts` (warning toast).
**Interfaces — Consumes:** Task 1 fields, Task 2 `EnvironmentInfo.max_workspace_hours`, Task 4 status JSON. **Produces:** payload adds `TRIPLEC_SETUP_SCRIPT`, `TRIPLEC_MAX_LIFETIME_MINUTES`; `WorkspaceStatus` (Rust + TS mirror of the JSON); start emits progress per Q4 and returns `Ok` with a warning field when clone/setup failed (extend the start result or emit a `workspace-warning` event — pick one, document it in the report).
- [ ] Tests: payload contains/omits the keys; the size error names the setup script when it's the large part; follow loop (fake target returning a scripted status sequence, paused time) emits the expected progress strings, stops at done, times out at 35 min; Overview renders cloning/failed states.
- [ ] Commit `feat(aws): follow clone and setup when a workspace starts`.
### Task 6: Copy-at-connect
**Files:** `container/entrypoint.sh` (+ new `container/triple-c-apply-connect`, shared `container/entrypoint-lib.sh` installed at `/usr/local/lib/triple-c/`), `container/Dockerfile`, `aws/workspace.rs` (payload split), `host/aws_host.rs` (post-connect push), `commands/terminal_commands.rs` + `web_terminal/ws_handler.rs` (connect env for AWS sessions), `HostSection.tsx` (choice + warnings), tests in `docker/aws_mode_tests.rs`, `aws/workspace/tests.rs`, `host/aws_host/tests.rs`.
**Interfaces — Produces:** `connect_credentials(project, settings) -> ConnectBundle { files: Vec<(String, Vec<u8>)>, env: Vec<(String,String)> }` (same sources as phase 5's `workspace_files` + the credential env pairs); `push_connect_bundle(target, bundle)` (uploads as root, then runs `triple-c-apply-connect`); payload for CopyAtConnect omits `TRIPLEC_FILES` and every credential key; `triple-c-apply-connect` sources `connect.env` only for itself and the clone retry, never exports it into the agent's snapshot.
- [ ] Tests: CopyAtConnect payload has no `TRIPLEC_FILES`, no `SECRET_ENV_KEYS`, no custom env values, still has the agent token and settings; `triple-c-apply-connect` installs a CA and merges SSH from freshly uploaded dirs and retries only failed clones; terminal ExecSpec for an AWS CopyAtConnect project carries the env and a SecretsManager one doesn't; the moved entrypoint functions still pass their existing tests (Docker parity).
- [ ] Commit `feat(aws): copy credentials at connect instead of storing them`.
### Task 7: Port mappings on AWS
**Files:** `auth_bridge/tunnel.rs` (`PortForward::bind(container, host_port, container_port, family)`), new `host/port_forwards.rs` (per-project set, open after connect, close on stop/exit/registry close), `commands/project_commands.rs`, `models/project.rs` (AWS: TCP only), `components/projects/home/config/AccessSection.tsx` + `PortMappingsEditor.tsx` (enabled on AWS with "localhost only on this machine").
**Interfaces — Produces:** `port_forwards::open_for(project_id, &ContainerTarget, &[PortMapping]) -> Vec<PortForwardResult { mapping, error: Option<String> }>`, `port_forwards::close(project_id)`; results surfaced as a warning toast for the failed ones.
- [ ] Tests: two mappings, one host port busy → one error, one working forward (real loopback sockets + a fake target, as the auth bridge tests do); UDP mapping refused for an AWS project; Docker path and auth bridge behaviour unchanged (existing tests).
- [ ] Commit `feat(aws): forward port mappings to AWS workspaces`.
### Task 8: LLM gateway sidecar
**Files:** `docker/gateway.rs` (export `registry_image_ref`, `render_config` usable without Docker), `aws/workspace.rs` (sidecar container, payload keys, fingerprint), `docker/workspace_env.rs` or `host/aws_host.rs` (base URL/auth override when the sidecar is on), `ModelSection.tsx` / `HostSection.tsx` (toggle "Run Triple-C's LLM gateway in the workspace", requires OpenAI-compatible backend and gateway models configured in Settings).
**Interfaces — Produces:** container `gateway` (essential false, image = digest-pinned LiteLLM ref, command writes `$TRIPLEC_GATEWAY_CONFIG` to `/tmp/litellm.yaml` then `litellm --config /tmp/litellm.yaml --host 127.0.0.1 --port 4000`, `memoryReservation` 512, same awslogs group with stream prefix `…/gateway`); workspace env `ANTHROPIC_BASE_URL=http://127.0.0.1:4000`, `ANTHROPIC_AUTH_TOKEN=<master key>` when on.
- [ ] Tests: request with sidecar has two containers and the gateway gets only its three secret keys; workspace env overrides; validation refuses the toggle without gateway models / with a non-OpenAI-compatible backend; Hybrid fingerprint unchanged when the toggle is off.
- [ ] Commit `feat(aws): optional LLM gateway sidecar`.
### Task 9: Cost estimate
**Files:** `Cargo.toml` (+ `aws-sdk-pricing`, same features), new `aws/pricing.rs` (+ tests), `aws/policies/workspace-user-tasks.json` + `aws/policies.rs` checks + `aws/README.md`, new command `aws_cost_estimate(project_id | config)`, `HostSection.tsx`, `OverviewTab.tsx`.
**Interfaces — Produces:** `FargateRates { vcpu_hour: f64, gb_hour: f64, storage_gb_hour: f64 }` per (region, arch); `estimate(cfg: &AwsWorkspaceConfig, rates) -> HourlyEstimate { usd_per_hour, note_spot: bool }` (ephemeral billed above 20 GiB); disk cache `<data_dir>/triple-c/aws-pricing/<region>.json` with fetched-at, 24 h TTL; a `PricingApi` trait with fake for tests.
- [ ] Tests: estimate arithmetic for a few configs (pinned rates); cache hit/expiry; parse of a recorded `GetProducts` price-list JSON fixture (x86 + ARM rows); denied/timeout → `None`; policy checks for `pricing:GetProducts`.
- [ ] Commit `feat(aws): estimated hourly cost per workspace`.
### Task 10: Live verification
Extend `aws::live_tests::a_fargate_workspace_round_trips` (or add a second ignored test) and run it via `scripts/aws-e2e-workspace.sh`: an EFS project with a setup script (`sudo apt-get install -y jq` then a marker) and a lifetime cap set on the environment (verify the value reaches the task's environment; the cap's firing is covered by Task 4's unit tests, not waited for live); a port mapping reaching an HTTP server in the workspace; the gateway sidecar starting (health on 127.0.0.1:4000 from inside the workspace; no real model call); a Copy-at-connect start followed by a private-clone retry with a deploy key is out of scope (no credentials) — exercise the apply script with a CA file instead; one cost estimate fetched. Record results in `aws/README.md` and the spec; final inventory clean; spend recorded (< $2).
### Task 11: Docs
Spec: phase 6 done, rulings Q1–Q9 (and any execution rulings), Data model updated; `CLAUDE.md` (`aws/`, `host/`, container sections: second access point, Copy-at-connect script and its "never into the agent snapshot" rule, setup runner, lifetime exit code, port forwards on AWS, sidecar); `aws/README.md` (pricing permission, lifetime cap, sizes).
Loaded 100 of 102 files, more files were not shown because too many files have changed in this diff. Show more