From 1b8c36e2664663da509340c90314a98df2b0f3df Mon Sep 17 00:00:00 2001 From: devhost agent Date: Wed, 2 Dec 2026 13:12:25 +0000 Subject: [PATCH 1/2] fix(copilot): make the state folder usable without a per-repo step (copilot 0.5.0, copilot-home-writable) A first copilot run failed with "failed to register ownership for session ... Permission denied" where the Feature's volume is not mounted (Envbuilder ignores Feature mounts and hooks), because COPILOT_HOME named a missing, root-owned path. copilot-home, run by copilot-setup and a new /etc/profile.d snippet, makes the folder exist, belong to the remote user and be mode 700: the volume when it is mounted (~/.copilot linked to it), else ~/.copilot with the Feature path linked to it when root or sudo is there, else COPILOT_HOME=~/.copilot in login shells plus a one-line hint. install.sh pre-creates the folder for the build's remote user; a root-run copilot-setup leaves nothing root-owned. New unit suite (home_test.sh) and scenario home_as_user (uid 1000). Co-Authored-By: Claude Opus 5.5 Signed-off-by: devhost agent --- Taskfile.yml | 3 +- src/copilot/CHANGELOG.md | 30 ++++ src/copilot/NOTES.md | 42 ++++- src/copilot/README.md | 91 +++++++--- src/copilot/copilot-home.sh | 229 ++++++++++++++++++++++++++ src/copilot/copilot-setup.sh | 49 ++++-- src/copilot/devcontainer-feature.json | 4 +- src/copilot/install.sh | 42 ++++- src/copilot/taskfile.yml | 2 +- test/copilot/home_as_user.sh | 59 +++++++ test/copilot/scenarios.json | 7 + test/copilot/unit/home_test.sh | 134 +++++++++++++++ 12 files changed, 643 insertions(+), 49 deletions(-) create mode 100644 src/copilot/copilot-home.sh create mode 100644 test/copilot/home_as_user.sh create mode 100644 test/copilot/unit/home_test.sh diff --git a/Taskfile.yml b/Taskfile.yml index 433129e1..04b597a3 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -191,9 +191,10 @@ tasks: - bash test/claude-code/unit/skills_test.sh test:copilot-unit: - desc: 'Docker-free tests of the copilot Feature''s agent-adapter-copilot (src/copilot) with a fake `copilot` and no login: new and resume (--name and --resume by session name, AGENT_SESSION_NAME or the handle, a tab falling back), run-once (the prompt from stdin as -p with --silent --no-ask-user --no-auto-update, args passed through, --allow-all-tools only when asked, an empty or over-long prompt refused, --session-id a stable UUID only with AGENT_ADAPTER_COPILOT_CONTINUE=1, copilot''s stdout and exit code kept), mcp-attach (remove then add through copilot mcp, no token or --env, idempotent, nothing with no facilitator on PATH, a missing copilot or other target refused, a private COPILOT_HOME made), every other verb exit 3, and supports agreeing with the verbs; AGENT_MODEL (0.2.0) as --model on new and resume in place of a --model or --model= in the args, said, a value that is not one word left out, run-once unchanged; skills (0.3.0, ADR-0077) in a temp COPILOT_HOME: bad use without COMPONENT_REGISTRY_SKILLS (nothing created), one agent''s view personal and marked owned, two views split into the shared personal folder and each agent''s own .github/skills, an unowned skill never touched, a project skill clash said, --add-dir its own folder last and once on new, resume and run-once and none without a folder, the start line, skills-show, shared, off and forget. The contract tests run the real adapter too (test:agents-contract).' + desc: 'Docker-free tests of the copilot Feature''s agent-adapter-copilot (src/copilot) with a fake `copilot` and no login: new and resume (--name and --resume by session name, AGENT_SESSION_NAME or the handle, a tab falling back), run-once (the prompt from stdin as -p with --silent --no-ask-user --no-auto-update, args passed through, --allow-all-tools only when asked, an empty or over-long prompt refused, --session-id a stable UUID only with AGENT_ADAPTER_COPILOT_CONTINUE=1, copilot''s stdout and exit code kept), mcp-attach (remove then add through copilot mcp, no token or --env, idempotent, nothing with no facilitator on PATH, a missing copilot or other target refused, a private COPILOT_HOME made), every other verb exit 3, and supports agreeing with the verbs; AGENT_MODEL (0.2.0) as --model on new and resume in place of a --model or --model= in the args, said, a value that is not one word left out, run-once unchanged; skills (0.3.0, ADR-0077) in a temp COPILOT_HOME: bad use without COMPONENT_REGISTRY_SKILLS (nothing created), one agent''s view personal and marked owned, two views split into the shared personal folder and each agent''s own .github/skills, an unowned skill never touched, a project skill clash said, --add-dir its own folder last and once on new, resume and run-once and none without a folder, the start line, skills-show, shared, off and forget. The contract tests run the real adapter too (test:agents-contract). And copilot-home (home_test.sh) over temp folders with a fake sudo: a mounted state folder kept at mode 700 with ~/.copilot linked to it (a real ~/.copilot copied in without overwriting and kept aside), no mount with root (the Feature path replaced by a link to ~/.copilot, what the image folder held kept, run twice), no mount without root (~/.copilot as the path, the hint only without --quiet), a repo''s own COPILOT_HOME, an unusable mount (one actionable line), never fatal.' cmds: - bash test/copilot/unit/adapter_test.sh + - bash test/copilot/unit/home_test.sh test:phi-check-unit: desc: 'Docker-free tests of the phi-check Feature (src/phi-check): every rule with a positive and a near-miss case (never-issued SSN ranges, a lower-case MBI, an ICD code list, an NPI, a date, a name or a diagnosis word alone, an anchor outside the three-line window), no output line in any format holding the matched value, a binary or missing path failing closed, exit 2 on bad usage, the exceptions file skipping only the listed path and reporting a malformed line, --no-exceptions, standard input with --stdin-filename, the importable module; then a mutation check that three deliberately broken patterns each fail the suite; part of lint. Needs python3.' diff --git a/src/copilot/CHANGELOG.md b/src/copilot/CHANGELOG.md index c471d009..8fd9d0f3 100644 --- a/src/copilot/CHANGELOG.md +++ b/src/copilot/CHANGELOG.md @@ -6,6 +6,36 @@ of the Feature and whether they must act (an option renamed, a task removed, a r The version in `devcontainer-feature.json` is bumped in the same change that adds its entry here; `task lint` fails when the entry is missing. +## [0.5.0] - 2026-12-02 + +### Fixed + +- A first `copilot` run no longer fails with "Failed to initialize session: ... failed to register + ownership for session ...: Permission denied (os error 13)" where the image was built without the + Feature's volume (Envbuilder ignores Feature mounts and runs no Feature hook), so `COPILOT_HOME` + named a folder that did not exist or was root's. Nothing to do in a consuming repo: drop any + hand-written `mkdir ~/.copilot` step once you are on 0.5.0. + +### Added + +- `copilot-home`, used by `copilot-setup` (post-create, `task copilot:setup`) and by the new + login-shell snippet `/etc/profile.d/copilot-home.sh`: the state folder exists, belongs to the + remote user and is mode 700. The Feature's volume when it is mounted (with `~/.copilot` linked to + it; a real `~/.copilot` is copied in without overwriting and kept as + `~/.copilot.before-copilot-feature`); else `~/.copilot`, with `/var/lib/devcontainer-features/copilot` + turned into a link to it when root or passwordless sudo is there, so `COPILOT_HOME` holds for every + process. Without root, login shells export `COPILOT_HOME=~/.copilot` and `copilot-setup` says in one + line how to make it hold everywhere (`remoteEnv`, or mount a volume). A folder that stays unusable + is said in one line with the command that fixes it. +- `install.sh` creates the state folder empty, owned by the build's remote user and mode 700, so a + new named volume starts with that owner and mode. + +### Changed + +- `copilot-setup` run as root (a builder that runs hooks as root) works for the remote user recorded + at build, leaves nothing root-owned, and runs the rest (the version, `attach`) as that user. The state + folder is now mode 700 (was: `go-w` on the top level). + ## [0.4.1] - 2026-11-30 This change was first numbered 0.4.0; ghcr already serves a 0.4.0 published by `nabil-becker/the-stockroom`, so it is 0.4.1 (ADR 0089). diff --git a/src/copilot/NOTES.md b/src/copilot/NOTES.md index fe113224..2af8e71b 100644 --- a/src/copilot/NOTES.md +++ b/src/copilot/NOTES.md @@ -13,7 +13,7 @@ | Data | Where it lives | Storage tier | | --- | --- | --- | -| Login (when made with `copilot login`), settings, session history, the facilitator MCP entry | named volume at `COPILOT_HOME` (`/var/lib/devcontainer-features/copilot`); a repo may bind-mount a host folder there | 4 working set | +| Login (when made with `copilot login`), settings, session history, the facilitator MCP entry | named volume at `COPILOT_HOME` (`/var/lib/devcontainer-features/copilot`); a repo may bind-mount a host folder there. Where no mount is applied (Envbuilder), `~/.copilot`, with the Feature path linked to it (0.5.0) | 4 working set | | Registry skills (0.3.0, with the component-registry Feature 2.1.0): shared ones in `$COPILOT_HOME/skills`, each agent's own in `$COPILOT_HOME/registry-agents/./`, each copy marked `.component-registry` | the same volume, owner-only | 4 working set | | Agent list (with `participants: true`) | `.devcontainer/participants.yml` in the repository | 1 git | | Leases and start markers (with `participants: true`) | `/var/lib/devcontainer-features/agents`, not a volume; a lease must not survive a rebuild | 4 working set | @@ -36,8 +36,9 @@ nothing. What it adds: - **`participants`**, the participant component, exactly as `claude-code` installs it, into one shared place. - **`attach`**, the facilitator's MCP entry, registered through Copilot's own command. -- **`COPILOT_HOME` as a named volume**, so a login survives a rebuild, made usable and private by a - post-create hook (`copilot-setup`). +- **`COPILOT_HOME` as a named volume**, so a login survives a rebuild, made usable and private by + `copilot-home` (0.5.0), which the post-create hook (`copilot-setup`) and a login-shell snippet run; + where the volume is not mounted (Envbuilder) the state goes to `~/.copilot` (below). - The Feature Taskfile (`copilot:version`, `login`, `config`, `setup`, `attach`). ## The CLI comes from the official Feature (`dependsOn`) @@ -256,10 +257,37 @@ harness's choice, not this Feature's; for the facilitator's pump the Operator's from OpenBao into the environment of `facilitator serve`. `COPILOT_HOME` must not be writable by group or other: Copilot refuses to write to one that is ("The -storage directory is writable by group or other users"; found while building this Feature). Docker -volumes arrive owned by root and mode 755, so `copilot-setup` hands the folder to the remote user with -`dcf-persist` and runs `chmod go-w` on its top level. A bind mount a repo puts on the same target is -treated the same. +storage directory is writable by group or other users"; found while building this Feature). And it must +exist and be the user's: otherwise the first session fails with "Failed to initialize session: ... +failed to register ownership for session ...: Permission denied (os error 13)" (seen in a Coder +workspace built by Envbuilder; it shows with no login, which the `home_as_user` scenario uses). + +## The state folder (`copilot-home`, 0.5.0) + +`copilot-home` makes the folder exist, belong to the remote user and be mode 700. It runs from +`copilot-setup` (post-create and `task copilot:setup`) and from `/etc/profile.d/copilot-home.sh` in +every login shell, so a builder that runs no Feature hook still gets a working first terminal. It is +idempotent, never fatal, and prints no token. Where the state goes, in order: + +| Case | `COPILOT_HOME` | What `copilot-home` does | +| --- | --- | --- | +| A repo sets `COPILOT_HOME` to another path | that path | creates it, the user's, mode 700 | +| The Feature path is mounted (the devcontainer CLI: the named volume, or a repo's bind mount) | `/var/lib/devcontainer-features/copilot` | hands it to the user (`chown -R` with root or sudo), mode 700; links `~/.copilot` to it (a real `~/.copilot` is copied in without overwriting and kept as `~/.copilot.before-copilot-feature`) | +| Not mounted (Envbuilder ignores Feature mounts), root or passwordless sudo available | `/var/lib/devcontainer-features/copilot`, now a link to `~/.copilot` | makes `~/.copilot` (mode 700), copies in what the image folder held, replaces the image folder with the link, so every process that reads `containerEnv` agrees | +| Not mounted, no root | `~/.copilot` in login shells | makes `~/.copilot`; `copilot-setup` says in one line to set `"COPILOT_HOME": "/.copilot"` in the repo's `remoteEnv` (or mount a volume) so processes that are not login shells agree | +| Mounted but owned by someone else, and no root to fix it | `~/.copilot` in login shells | one line on stderr with the `sudo chown` that fixes it | + +The user is the caller; run as root, it is the remote user `install.sh` recorded from `_REMOTE_USER` +(else `_CONTAINER_USER`) in `/usr/local/share/copilot-feature/options.env`, else the account with uid +1000. A root-run `copilot-setup` prepares the folder for that user and then runs the rest as them +(`runuser`, else `su`), so nothing is left root-owned. `install.sh` also creates the folder empty, the +user's and mode 700, when the user exists at build: a new named volume starts with that owner and mode. + +Under Envbuilder the state lives in `~/.copilot`, so it survives a restart only where the home folder +is persistent (a Coder workspace's home volume). To keep it on the Feature path instead, the consuming +template mounts a volume at `/var/lib/devcontainer-features/copilot` (ADR 0095 has the same advice for +open-model-agent). The login-shell snippet covers `bash -l` and shells that read `/etc/profile`; a zsh +that does not is covered by the link where root was available, else by `remoteEnv`. ## What works without a login, and what needs one diff --git a/src/copilot/README.md b/src/copilot/README.md index 174b0e27..6b80f2c4 100644 --- a/src/copilot/README.md +++ b/src/copilot/README.md @@ -1,7 +1,7 @@ # GitHub Copilot CLI (copilot) (copilot) -GitHub Copilot CLI (https://docs.github.com/copilot/concepts/agents/about-copilot-cli) as a member of this collection's ecosystem, and the second vendor for the participant component's contracts: brings the CLI in through dependsOn on the official ghcr.io/devcontainers/features/copilot-cli (which installs it, pinned to Copilot CLI 1.0.114; this Feature installs no binary of its own) and adds agent-adapter-copilot (new, resume, run-once for the facilitator's pump, mcp-attach, and from 0.3.0 skills and skills-show: the component registry's rendered skills into $COPILOT_HOME/skills and, for what one agent alone resolves, a folder its sessions read through --add-dir; exit 3 for the verbs Copilot has no local route for), the participants option (the participant component, one shared copy with claude-code), the attach option (attach: facilitator registers the facilitator's MCP entry through `copilot mcp add`, with no token in it), COPILOT_HOME as a named volume so a copilot login survives rebuilds, and a post-create hook (copilot-setup) that makes it usable and private. Never logs in and never stores a token. Plus the Feature Taskfile. +GitHub Copilot CLI (https://docs.github.com/copilot/concepts/agents/about-copilot-cli) as a member of this collection's ecosystem, and the second vendor for the participant component's contracts: brings the CLI in through dependsOn on the official ghcr.io/devcontainers/features/copilot-cli (which installs it, pinned to Copilot CLI 1.0.114; this Feature installs no binary of its own) and adds agent-adapter-copilot (new, resume, run-once for the facilitator's pump, mcp-attach, and from 0.3.0 skills and skills-show: the component registry's rendered skills into $COPILOT_HOME/skills and, for what one agent alone resolves, a folder its sessions read through --add-dir; exit 3 for the verbs Copilot has no local route for), the participants option (the participant component, one shared copy with claude-code), the attach option (attach: facilitator registers the facilitator's MCP entry through `copilot mcp add`, with no token in it), COPILOT_HOME as a named volume so a copilot login survives rebuilds, and copilot-home, run by a post-create hook (copilot-setup) and by a login-shell snippet, which makes Copilot's state folder exist, belong to the remote user and be mode 700: the volume when it is mounted, else ~/.copilot with COPILOT_HOME's path linked to it (Envbuilder ignores Feature mounts and hooks), so a first copilot run never fails with 'failed to register ownership for session ... Permission denied'. Never logs in and never stores a token. Plus the Feature Taskfile. ## Example Usage @@ -34,7 +34,7 @@ GitHub Copilot CLI (https://docs.github.com/copilot/concepts/agents/about-copilo | Data | Where it lives | Storage tier | | --- | --- | --- | -| Login (when made with `copilot login`), settings, session history, the facilitator MCP entry | named volume at `COPILOT_HOME` (`/var/lib/devcontainer-features/copilot`); a repo may bind-mount a host folder there | 4 working set | +| Login (when made with `copilot login`), settings, session history, the facilitator MCP entry | named volume at `COPILOT_HOME` (`/var/lib/devcontainer-features/copilot`); a repo may bind-mount a host folder there. Where no mount is applied (Envbuilder), `~/.copilot`, with the Feature path linked to it (0.5.0) | 4 working set | | Registry skills (0.3.0, with the component-registry Feature 2.1.0): shared ones in `$COPILOT_HOME/skills`, each agent's own in `$COPILOT_HOME/registry-agents/./`, each copy marked `.component-registry` | the same volume, owner-only | 4 working set | | Agent list (with `participants: true`) | `.devcontainer/participants.yml` in the repository | 1 git | | Leases and start markers (with `participants: true`) | `/var/lib/devcontainer-features/agents`, not a volume; a lease must not survive a rebuild | 4 working set | @@ -57,8 +57,9 @@ nothing. What it adds: - **`participants`**, the participant component, exactly as `claude-code` installs it, into one shared place. - **`attach`**, the facilitator's MCP entry, registered through Copilot's own command. -- **`COPILOT_HOME` as a named volume**, so a login survives a rebuild, made usable and private by a - post-create hook (`copilot-setup`). +- **`COPILOT_HOME` as a named volume**, so a login survives a rebuild, made usable and private by + `copilot-home` (0.5.0), which the post-create hook (`copilot-setup`) and a login-shell snippet run; + where the volume is not mounted (Envbuilder) the state goes to `~/.copilot` (below). - The Feature Taskfile (`copilot:version`, `login`, `config`, `setup`, `attach`). ## The CLI comes from the official Feature (`dependsOn`) @@ -277,10 +278,37 @@ harness's choice, not this Feature's; for the facilitator's pump the Operator's from OpenBao into the environment of `facilitator serve`. `COPILOT_HOME` must not be writable by group or other: Copilot refuses to write to one that is ("The -storage directory is writable by group or other users"; found while building this Feature). Docker -volumes arrive owned by root and mode 755, so `copilot-setup` hands the folder to the remote user with -`dcf-persist` and runs `chmod go-w` on its top level. A bind mount a repo puts on the same target is -treated the same. +storage directory is writable by group or other users"; found while building this Feature). And it must +exist and be the user's: otherwise the first session fails with "Failed to initialize session: ... +failed to register ownership for session ...: Permission denied (os error 13)" (seen in a Coder +workspace built by Envbuilder; it shows with no login, which the `home_as_user` scenario uses). + +## The state folder (`copilot-home`, 0.5.0) + +`copilot-home` makes the folder exist, belong to the remote user and be mode 700. It runs from +`copilot-setup` (post-create and `task copilot:setup`) and from `/etc/profile.d/copilot-home.sh` in +every login shell, so a builder that runs no Feature hook still gets a working first terminal. It is +idempotent, never fatal, and prints no token. Where the state goes, in order: + +| Case | `COPILOT_HOME` | What `copilot-home` does | +| --- | --- | --- | +| A repo sets `COPILOT_HOME` to another path | that path | creates it, the user's, mode 700 | +| The Feature path is mounted (the devcontainer CLI: the named volume, or a repo's bind mount) | `/var/lib/devcontainer-features/copilot` | hands it to the user (`chown -R` with root or sudo), mode 700; links `~/.copilot` to it (a real `~/.copilot` is copied in without overwriting and kept as `~/.copilot.before-copilot-feature`) | +| Not mounted (Envbuilder ignores Feature mounts), root or passwordless sudo available | `/var/lib/devcontainer-features/copilot`, now a link to `~/.copilot` | makes `~/.copilot` (mode 700), copies in what the image folder held, replaces the image folder with the link, so every process that reads `containerEnv` agrees | +| Not mounted, no root | `~/.copilot` in login shells | makes `~/.copilot`; `copilot-setup` says in one line to set `"COPILOT_HOME": "/.copilot"` in the repo's `remoteEnv` (or mount a volume) so processes that are not login shells agree | +| Mounted but owned by someone else, and no root to fix it | `~/.copilot` in login shells | one line on stderr with the `sudo chown` that fixes it | + +The user is the caller; run as root, it is the remote user `install.sh` recorded from `_REMOTE_USER` +(else `_CONTAINER_USER`) in `/usr/local/share/copilot-feature/options.env`, else the account with uid +1000. A root-run `copilot-setup` prepares the folder for that user and then runs the rest as them +(`runuser`, else `su`), so nothing is left root-owned. `install.sh` also creates the folder empty, the +user's and mode 700, when the user exists at build: a new named volume starts with that owner and mode. + +Under Envbuilder the state lives in `~/.copilot`, so it survives a restart only where the home folder +is persistent (a Coder workspace's home volume). To keep it on the Feature path instead, the consuming +template mounts a volume at `/var/lib/devcontainer-features/copilot` (ADR 0095 has the same advice for +open-model-agent). The login-shell snippet covers `bash -l` and shells that read `/etc/profile`; a zsh +that does not is covered by the link where root was available, else by `remoteEnv`. ## What works without a login, and what needs one @@ -344,6 +372,36 @@ the line "manage the environment, not the CLI" (NFR-13); `trust` answers exit 3 ## Changes +### [0.5.0] - 2026-12-02 + +#### Fixed + +- A first `copilot` run no longer fails with "Failed to initialize session: ... failed to register + ownership for session ...: Permission denied (os error 13)" where the image was built without the + Feature's volume (Envbuilder ignores Feature mounts and runs no Feature hook), so `COPILOT_HOME` + named a folder that did not exist or was root's. Nothing to do in a consuming repo: drop any + hand-written `mkdir ~/.copilot` step once you are on 0.5.0. + +#### Added + +- `copilot-home`, used by `copilot-setup` (post-create, `task copilot:setup`) and by the new + login-shell snippet `/etc/profile.d/copilot-home.sh`: the state folder exists, belongs to the + remote user and is mode 700. The Feature's volume when it is mounted (with `~/.copilot` linked to + it; a real `~/.copilot` is copied in without overwriting and kept as + `~/.copilot.before-copilot-feature`); else `~/.copilot`, with `/var/lib/devcontainer-features/copilot` + turned into a link to it when root or passwordless sudo is there, so `COPILOT_HOME` holds for every + process. Without root, login shells export `COPILOT_HOME=~/.copilot` and `copilot-setup` says in one + line how to make it hold everywhere (`remoteEnv`, or mount a volume). A folder that stays unusable + is said in one line with the command that fixes it. +- `install.sh` creates the state folder empty, owned by the build's remote user and mode 700, so a + new named volume starts with that owner and mode. + +#### Changed + +- `copilot-setup` run as root (a builder that runs hooks as root) works for the remote user recorded + at build, leaves nothing root-owned, and runs the rest (the version, `attach`) as that user. The state + folder is now mode 700 (was: `go-w` on the top level). + ### [0.4.1] - 2026-11-30 This change was first numbered 0.4.0; ghcr already serves a 0.4.0 published by `nabil-becker/the-stockroom`, so it is 0.4.1 (ADR 0089). @@ -364,23 +422,6 @@ This change was first numbered 0.4.0; ghcr already serves a 0.4.0 published by ` `skills-shared-helper`). No change in behaviour: the same files, modes, folders and lines, checked byte for byte before and after in `split`, `shared`, `off` and forget. Nothing to do. -### [0.3.0] - 2026-11-24 - -#### Added - -- **Registry skills** (ADR-0077 slice 2.1, backlog `templating-slice-2`): `agent-adapter-copilot` - gains the `skills` verb (and the read-only `skills-show`). With the component-registry Feature 2.1.0 - and a layer list, each Copilot agent gets its registry skills, rendered from the `copilot` templates: - the ones every agent shares in `$COPILOT_HOME/skills` (Copilot's personal skills folder), the ones - only its own path resolves in a folder that `new`, `resume` and `run-once` pass with `--add-dir`. - A project or personal skill of the same name wins, and that is said. - -#### Changed - -- This is a change for a Copilot agent in a container with a layer list: it now gets skills it did - not get before. The default is `split`, as for Claude Code. Action, only to keep the old - behaviour: `registry-skills: off` on the agent's entry, or `COMPONENT_REGISTRY_SKILLS_MODE=off`. - The full history is in [CHANGELOG.md](https://github.com/nabil-becker/devcontainer-features/blob/main/src/copilot/CHANGELOG.md). --- diff --git a/src/copilot/copilot-home.sh b/src/copilot/copilot-home.sh new file mode 100644 index 00000000..d1dba3d8 --- /dev/null +++ b/src/copilot/copilot-home.sh @@ -0,0 +1,229 @@ +#!/usr/bin/env bash +# copilot-home: resolve Copilot's state directory for the remote user and make it usable: it exists, +# belongs to that user and is mode 700 (Copilot refuses a home writable by group or other, and fails +# its first session with "failed to register ownership for session ... Permission denied" when it +# cannot write there). Run by copilot-setup (post-create, `task copilot:setup`) and by the login-shell +# snippet /etc/profile.d/copilot-home.sh, so a builder that runs no Feature hook (Envbuilder) still +# gets a working copilot in the first terminal. Idempotent, never fatal, prints no secret. +# +# copilot-home [--quiet] [--path] [--self] [--user ] +# +# --path print only the resolved COPILOT_HOME on stdout (for `export COPILOT_HOME=...`) +# --quiet no report lines (an actionable problem is still said on stderr) +# --self act only for the caller: as root, do nothing unless the Feature's user is root +# --user act for this user (root only); default: the caller, or, for root, the remote user +# recorded at build (_REMOTE_USER, else _CONTAINER_USER), else the account with uid 1000 +# +# Where the state lives, in order (F is the Feature path, /var/lib/devcontainer-features/copilot, +# which containerEnv names as COPILOT_HOME): +# 1. COPILOT_HOME set to another path (a repo's own choice): that path. +# 2. F is a symbolic link (an earlier run redirected it): F, and the folder it points to. +# 3. F is a mount (the Feature's named volume, or a bind mount a repo put there) and usable: F, +# with ~/.copilot linked to it (a real ~/.copilot is copied in without overwriting, then kept +# beside it as ~/.copilot.before-copilot-feature). +# 4. Otherwise (Envbuilder ignores Feature mounts, so F is a folder of the image or missing): +# ~/.copilot. With root or passwordless sudo, F becomes a link to it, so every process that +# reads containerEnv agrees; without, login shells export COPILOT_HOME=~/.copilot (the +# profile.d snippet) and a one-line hint says how to make it hold for everything else. +# A mount that stays unusable (owned by someone else, no root to fix it) is said in one line with +# the command that fixes it, and login shells fall back to ~/.copilot meanwhile. +# +# Test seams: COPILOT_FEATURE_HOME replaces F; COPILOT_FEATURE_MOUNTED=yes|no replaces the mount +# check; COPILOT_FEATURE_OPTIONS replaces the options file install.sh writes. +set -u + +F="${COPILOT_FEATURE_HOME:-/var/lib/devcontainer-features/copilot}" +options="${COPILOT_FEATURE_OPTIONS:-/usr/local/share/copilot-feature/options.env}" + +quiet=0 want_path=0 self=0 user="" +while [ $# -gt 0 ]; do + case "$1" in + --quiet) quiet=1 ;; + --path) want_path=1 ;; + --self) self=1 ;; + --user) user="${2:-}"; shift ;; + *) echo "usage: copilot-home [--quiet] [--path] [--self] [--user ]" >&2; exit 0 ;; + esac + shift +done + +say() { [ "$quiet" = 1 ] || [ "$want_path" = 1 ] || echo "copilot: $*"; } +warn() { echo "copilot: $*" >&2; } +note() { [ "$quiet" = 1 ] || warn "$@"; } # a degraded but working state: said by copilot-setup, not by every shell +done_with() { # [report]: the resolved path, printed for --path + [ "$want_path" = 1 ] && printf '%s\n' "$1" + exit 0 +} + +me_uid="$(id -u)" +as_root() { # a command as root: directly, or through passwordless sudo + if [ "$me_uid" -eq 0 ]; then "$@" + elif command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then sudo -n "$@" + else return 1 + fi +} +can_root() { [ "$me_uid" -eq 0 ] || { command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; }; } + +# ------------------------------------------------------------ the user ---- +built_user="" +if [ -r "$options" ]; then + built_user="$(sed -n 's/^REMOTE_USER=//p' "$options" | head -n 1)" + built_user="${built_user#\'}"; built_user="${built_user%\'}" +fi +if [ "$me_uid" -ne 0 ]; then + user="$(id -un)" # a non-root caller acts for itself only +elif [ -z "$user" ]; then + user="$built_user" + [ -n "$user" ] || user="$(getent passwd 1000 2>/dev/null | cut -d: -f1)" + [ -n "$user" ] || user=root + if [ "$self" = 1 ] && [ "$user" != root ]; then exit 0; fi # root's own shell: not the Feature's user +fi +entry="$(getent passwd "$user" 2>/dev/null || true)" +if [ -z "$entry" ]; then + warn "user '$user' does not exist here; COPILOT_HOME left as it is" + done_with "${COPILOT_HOME:-$F}" +fi +uid="$(printf '%s' "$entry" | cut -d: -f3)" +gid="$(printf '%s' "$entry" | cut -d: -f4)" +home="$(printf '%s' "$entry" | cut -d: -f6)" +[ "$me_uid" -eq "$uid" ] && home="${HOME:-$home}" +H="$home/.copilot" + +# ------------------------------------------------------------- helpers ---- +is_mounted() { # : a mount point (volume or bind), not a folder of the image + case "${COPILOT_FEATURE_MOUNTED:-}" in yes) return 0 ;; no) return 1 ;; esac + [ -d "$1" ] && [ ! -L "$1" ] || return 1 + if command -v mountpoint >/dev/null 2>&1 && mountpoint -q "$1" 2>/dev/null; then return 0; fi + if command -v findmnt >/dev/null 2>&1 && [ "$(findmnt -no TARGET --target "$1" 2>/dev/null | head -n 1)" = "$1" ]; then return 0; fi + [ "$(stat -c %d "$1" 2>/dev/null)" != "$(stat -c %d "$(dirname "$1")" 2>/dev/null)" ] +} + +usable() { # : the user can keep state in it (checked as the user, or by owner and mode for root) + [ -d "$1" ] || return 1 + if [ "$me_uid" -eq "$uid" ]; then [ -w "$1" ] && [ -x "$1" ] + else [ "$(stat -L -c %u "$1" 2>/dev/null)" = "$uid" ] && [ $((0$(stat -L -c %a "$1") & 0700)) -eq $((0700)) ] + fi +} + +prepare() { # : exists, the user's, mode 700; never creates a missing home folder + local d="$1" parent + parent="$(dirname "$d")" + if [ ! -d "$d" ]; then + if [ "$parent" = "$home" ] && [ ! -d "$home" ]; then return 1; fi + if [ "$me_uid" -eq "$uid" ]; then mkdir -p "$d" 2>/dev/null || as_root mkdir -p "$d" 2>/dev/null || return 1 + else as_root mkdir -p "$d" 2>/dev/null || return 1 + fi + fi + # Only the top level is checked, so a large folder is not walked on every start; -R because the + # contents of a volume or bind mount come with the same foreign owner. + if [ "$(stat -L -c %u "$d" 2>/dev/null)" != "$uid" ]; then + as_root chown -R "$uid:$gid" "$d" 2>/dev/null || true + fi + if [ "$(stat -L -c %a "$d" 2>/dev/null)" != 700 ]; then + chmod 700 "$d" 2>/dev/null || as_root chmod 700 "$d" 2>/dev/null || true + fi + usable "$d" +} + +as_user_link() { # : a symbolic link owned by the user + ln -s "$1" "$2" 2>/dev/null || as_root ln -s "$1" "$2" 2>/dev/null || return 1 + [ "$me_uid" -eq "$uid" ] || as_root chown -h "$uid:$gid" "$2" 2>/dev/null || true +} + +copy_in() { # : what Copilot already wrote, without overwriting anything, then the user's + [ -d "$1" ] && [ -n "$(ls -A "$1" 2>/dev/null)" ] || return 0 + cp -a -n "$1/." "$2/" 2>/dev/null || as_root cp -a -n "$1/." "$2/" 2>/dev/null || return 1 + # cp -a gives the target the source folder's own mode and owner: put back the user's and 700. + chmod 700 "$2" 2>/dev/null || as_root chmod 700 "$2" 2>/dev/null || true + [ "$me_uid" -eq "$uid" ] && [ -z "$(find "$2" ! -user "$uid" -print -quit 2>/dev/null)" ] && return 0 + as_root chown -R "$uid:$gid" "$2" 2>/dev/null || true +} + +link_home() { # ~/.copilot -> F, so tools that look there find the same state + if [ -L "$H" ]; then + [ "$(readlink -f "$H")" = "$(readlink -f "$F")" ] || say "$H links elsewhere ($(readlink "$H")); left as it is, COPILOT_HOME wins" + return 0 + fi + if [ -d "$H" ]; then + if [ -n "$(ls -A "$H" 2>/dev/null)" ]; then + local aside="$H.before-copilot-feature" + [ -e "$aside" ] && { say "$H is a folder and $aside exists; left as it is, COPILOT_HOME wins"; return 0; } + copy_in "$H" "$F" || { say "could not copy $H into $F; left as it is"; return 0; } + mv "$H" "$aside" 2>/dev/null || as_root mv "$H" "$aside" 2>/dev/null || return 0 + say "copied $H into $F (nothing overwritten); the old folder is $aside" + else + rmdir "$H" 2>/dev/null || as_root rmdir "$H" 2>/dev/null || return 0 + fi + fi + [ -d "$home" ] || return 0 + as_user_link "$F" "$H" && say "$H links to $F" +} + +report() { # : the one line copilot-setup has always printed, plus the volume advice + local d="$1" src fstype backing + src="$(findmnt -no SOURCE --target "$d" 2>/dev/null || true)" + fstype="$(findmnt -no FSTYPE --target "$d" 2>/dev/null || true)" + case "$fstype" in overlay | "") backing=image ;; *) case "$src" in *"/volumes/"*) backing=volume ;; *) backing=bind ;; esac ;; esac + if usable "$d"; then say "$d ($backing, writable)"; else say "$d ($backing, NOT writable by $user)"; fi + case "$backing" in + volume) say "a named volume is deleted by a container-engine storage reset; for the host disk, bind-mount the same target in devcontainer.json (see the Feature README)." ;; + esac +} + +# ----------------------------------------------------------- resolution ---- +cur="${COPILOT_HOME:-}" + +# 1. A repo's own COPILOT_HOME (and the fallback a login shell exported earlier) is kept as given, +# unless it is the fallback and the Feature path has since become usable. +if [ -n "$cur" ] && [ "$cur" != "$F" ] && { [ "$cur" != "$H" ] || ! usable "$F"; }; then + if prepare "$cur"; then report "$cur" + else warn "$cur (COPILOT_HOME) is not usable by $user; run: sudo mkdir -p '$cur' && sudo chown -R $user '$cur' && chmod 700 '$cur'" + fi + done_with "$cur" +fi + +# 2. F already redirected to the user's home by an earlier run. +if [ -L "$F" ]; then + t="$(readlink -f "$F")" + if prepare "$t"; then + report "$t" + say "COPILOT_HOME ($F) links to $t" + done_with "$F" + fi + warn "$F links to $t, which is not usable by $user; run: sudo chown -R $user '$t' && chmod 700 '$t'" + done_with "$F" +fi + +# 3. The Feature's volume (or a repo's bind mount on the same target). +if is_mounted "$F"; then + if prepare "$F"; then + report "$F" + link_home + done_with "$F" + fi + owner="$(stat -c %U "$F" 2>/dev/null || echo '?')" + warn "$F (COPILOT_HOME, a mount) is owned by $owner and $user cannot fix it; run: sudo chown -R $user '$F' && sudo chmod 700 '$F' (or rebuild); login shells use $H meanwhile" + prepare "$H" || true + done_with "$H" +fi + +# 4. No mount: the image folder, or nothing (Envbuilder). The state goes to ~/.copilot. +if ! prepare "$H"; then + if usable "$F"; then report "$F"; say "$H could not be made; Copilot keeps its state in the image folder $F, lost at the next rebuild"; done_with "$F"; fi + warn "neither $F nor $H is usable by $user (is $home missing?); run: sudo mkdir -p '$F' && sudo chown $user '$F' && sudo chmod 700 '$F'" + done_with "$F" +fi +if can_root; then + # Only a folder of the image is removed (is_mounted said no), and only after its content is safe. + if [ -d "$F" ] && copy_in "$F" "$H"; then as_root rm -rf "$F" 2>/dev/null || true; fi + as_root mkdir -p "$(dirname "$F")" 2>/dev/null || true + if [ ! -e "$F" ] && as_root ln -s "$H" "$F" 2>/dev/null; then + report "$H" + say "COPILOT_HOME ($F) links to $H: $F is not a mount here (Envbuilder ignores Feature mounts), so the state is kept in the home folder" + done_with "$F" + fi +fi +copy_in "$F" "$H" || true +report "$H" +note "$F is not a mount and cannot be redirected without root; login shells use COPILOT_HOME=$H. For every other process, set \"COPILOT_HOME\": \"$H\" in the repo's remoteEnv, or mount a volume at $F" +done_with "$H" diff --git a/src/copilot/copilot-setup.sh b/src/copilot/copilot-setup.sh index 00a6c622..9dc1f3fa 100644 --- a/src/copilot/copilot-setup.sh +++ b/src/copilot/copilot-setup.sh @@ -3,11 +3,13 @@ # workspace folder after every create/rebuild; also behind `task copilot:setup`. Idempotent, # never fatal: a problem is reported, not allowed to abort the rest of post-create. # -# 1. dcf-persist makes COPILOT_HOME (the Feature's named volume, or a bind mount a repo put on the -# same target) exist and belong to the remote user, so a `copilot login` keeps across rebuilds. -# Copilot refuses a home that is writable by group or other ("The storage directory is writable -# by group or other users"), so the folder is made owner-only here (chmod go-w on the top level -# only; the CLI keeps its own files private). +# 1. copilot-home resolves Copilot's state directory and makes it exist, belong to the remote user +# and be mode 700: the Feature's named volume (or a bind mount a repo put on the same target) when +# it is mounted, else ~/.copilot, with COPILOT_HOME's path linked to it (see copilot-home). Copilot +# refuses a home that is writable by group or other, and its first session fails with "failed to +# register ownership for session" when the folder is missing or not the user's. Run as root (a +# builder that runs hooks as root), it works for the remote user recorded at build, never leaves +# a root-owned folder, and runs the rest as that user. # 2. With attach=facilitator, registers the facilitator's MCP entry through Copilot's own command # (agent-adapter-copilot mcp-attach). That needs no login. # 3. Reports the copilot version and where its login comes from, never the token. The Feature never @@ -22,7 +24,37 @@ set -u # shellcheck disable=SC1091 # written by install.sh at image build [ -f /usr/local/share/copilot-feature/options.env ] && source /usr/local/share/copilot-feature/options.env attach="${COPILOT_SETUP_ATTACH-${ATTACH:-}}" -dir="${COPILOT_HOME:-$HOME/.copilot}" + +home_cmd() { # copilot-home, or a plain fallback when an older image lacks it + if command -v copilot-home >/dev/null 2>&1; then copilot-home "$@" + else dcf-persist --label copilot "${COPILOT_HOME:-$HOME/.copilot}" >/dev/null; chmod 700 "${COPILOT_HOME:-$HOME/.copilot}" 2>/dev/null; [ "${1:-}" = --path ] && echo "${COPILOT_HOME:-$HOME/.copilot}" + fi +} + +# As root: prepare the folder for the remote user, then go on as that user, so Copilot's own files +# (the MCP entry, its config) are theirs. COPILOT_SETUP_AS_USER stops a second hand-over. +if [ "$(id -u)" -eq 0 ] && [ -z "${COPILOT_SETUP_AS_USER:-}" ]; then + # shellcheck disable=SC2153 # REMOTE_USER comes from options.env + ruser="${REMOTE_USER:-}" + [ -n "$ruser" ] || ruser="$(getent passwd 1000 2>/dev/null | cut -d: -f1)" + if [ -n "$ruser" ] && [ "$ruser" != root ] && getent passwd "$ruser" >/dev/null 2>&1; then + home_cmd --quiet --user "$ruser" + if command -v runuser >/dev/null 2>&1; then + exec runuser -u "$ruser" -- env COPILOT_SETUP_AS_USER=1 "$0" "$@" + elif command -v su >/dev/null 2>&1; then + # shellcheck disable=SC2016 # expanded by the inner shell + exec su -s /bin/sh "$ruser" -c 'COPILOT_SETUP_AS_USER=1 exec "$0" "$@"' "$0" "$@" + fi + echo "copilot: the state folder is ready for $ruser; run copilot-setup as $ruser for the rest (no runuser or su here)" >&2 + exit 0 + fi +fi + +# The report first (it says what it moved or linked), then the path for the rest of this run. +[ "${1:-}" = attach ] || home_cmd +dir="$(home_cmd --path --quiet 2>/dev/null)" +[ -n "$dir" ] || dir="${COPILOT_HOME:-$HOME/.copilot}" +export COPILOT_HOME="$dir" attach_now() { command -v agent-adapter-copilot >/dev/null 2>&1 || { echo "copilot: agent-adapter-copilot is missing; rebuild" >&2; return 0; } @@ -30,15 +62,10 @@ attach_now() { } if [ "${1:-}" = attach ]; then - dcf-persist --label copilot "$dir" >/dev/null - chmod go-w "$dir" 2>/dev/null || true attach_now exit 0 fi -dcf-persist --label copilot "$dir" -chmod go-w "$dir" 2>/dev/null || true - if ! command -v copilot >/dev/null 2>&1; then echo "copilot: not found on PATH; the copilot Feature gets it through its dependsOn on ghcr.io/devcontainers/features/copilot-cli:1 - rebuild the container and check the build log for that Feature" >&2 exit 0 diff --git a/src/copilot/devcontainer-feature.json b/src/copilot/devcontainer-feature.json index df3a3dbd..8c1a565e 100644 --- a/src/copilot/devcontainer-feature.json +++ b/src/copilot/devcontainer-feature.json @@ -1,8 +1,8 @@ { "id": "copilot", - "version": "0.4.1", + "version": "0.5.0", "name": "GitHub Copilot CLI (copilot)", - "description": "GitHub Copilot CLI (https://docs.github.com/copilot/concepts/agents/about-copilot-cli) as a member of this collection's ecosystem, and the second vendor for the participant component's contracts: brings the CLI in through dependsOn on the official ghcr.io/devcontainers/features/copilot-cli (which installs it, pinned to Copilot CLI 1.0.114; this Feature installs no binary of its own) and adds agent-adapter-copilot (new, resume, run-once for the facilitator's pump, mcp-attach, and from 0.3.0 skills and skills-show: the component registry's rendered skills into $COPILOT_HOME/skills and, for what one agent alone resolves, a folder its sessions read through --add-dir; exit 3 for the verbs Copilot has no local route for), the participants option (the participant component, one shared copy with claude-code), the attach option (attach: facilitator registers the facilitator's MCP entry through `copilot mcp add`, with no token in it), COPILOT_HOME as a named volume so a copilot login survives rebuilds, and a post-create hook (copilot-setup) that makes it usable and private. Never logs in and never stores a token. Plus the Feature Taskfile.", + "description": "GitHub Copilot CLI (https://docs.github.com/copilot/concepts/agents/about-copilot-cli) as a member of this collection's ecosystem, and the second vendor for the participant component's contracts: brings the CLI in through dependsOn on the official ghcr.io/devcontainers/features/copilot-cli (which installs it, pinned to Copilot CLI 1.0.114; this Feature installs no binary of its own) and adds agent-adapter-copilot (new, resume, run-once for the facilitator's pump, mcp-attach, and from 0.3.0 skills and skills-show: the component registry's rendered skills into $COPILOT_HOME/skills and, for what one agent alone resolves, a folder its sessions read through --add-dir; exit 3 for the verbs Copilot has no local route for), the participants option (the participant component, one shared copy with claude-code), the attach option (attach: facilitator registers the facilitator's MCP entry through `copilot mcp add`, with no token in it), COPILOT_HOME as a named volume so a copilot login survives rebuilds, and copilot-home, run by a post-create hook (copilot-setup) and by a login-shell snippet, which makes Copilot's state folder exist, belong to the remote user and be mode 700: the volume when it is mounted, else ~/.copilot with COPILOT_HOME's path linked to it (Envbuilder ignores Feature mounts and hooks), so a first copilot run never fails with 'failed to register ownership for session ... Permission denied'. Never logs in and never stores a token. Plus the Feature Taskfile.", "documentationURL": "https://github.com/nabil-becker/devcontainer-features/tree/main/src/copilot", "licenseURL": "https://github.com/nabil-becker/devcontainer-features/blob/main/LICENSE", "options": { diff --git a/src/copilot/install.sh b/src/copilot/install.sh index 8b868d7e..791bb3cd 100644 --- a/src/copilot/install.sh +++ b/src/copilot/install.sh @@ -12,8 +12,10 @@ # A missing copilot (a tool that ignores dependsOn) is a loud warning rather than a failure; # copilot-setup reports the absence again at post-create. # -# Never writes into COPILOT_HOME: the volume is not mounted at image build; copilot-setup makes it -# usable at post-create. +# Writes nothing into COPILOT_HOME: the volume is not mounted at image build. It only creates the +# empty folder for the remote user (mode 700), so a new named volume copies that owner and mode up, +# and installs copilot-home, which copilot-setup (post-create) and a login-shell snippet use to make +# the folder usable at run time, also under Envbuilder, which ignores Feature mounts and hooks. set -euo pipefail PARTICIPANTS="${PARTICIPANTS:-false}" @@ -61,6 +63,40 @@ else echo "copilot: WARNING - copilot not found; this Feature's dependsOn on ghcr.io/devcontainers/features/copilot-cli:1 should have installed it (check the build log for that Feature). The adapter, copilot-setup, dcf-persist and the Taskfile are installed anyway." >&2 fi +# ------------------------------------------------------- the state folder ---- +# The remote user as the build tool names it (_REMOTE_USER, else _CONTAINER_USER), when that account +# exists at this point; nothing is assumed otherwise (copilot-home resolves it again at run time). +remote_user="" +for u in "${_REMOTE_USER:-}" "${_CONTAINER_USER:-}"; do + if [ -n "$u" ] && getent passwd "$u" >/dev/null 2>&1; then remote_user="$u"; break; fi +done +state_dir=/var/lib/devcontainer-features/copilot +if [ -n "$remote_user" ] && [ ! -L "$state_dir" ]; then + # Empty and owned by the user: a new named volume mounted here starts with this owner and mode, + # and under Envbuilder (no mount) a first copilot run can write here before any hook has run. + mkdir -p "$state_dir" + chown "$(id -u "$remote_user"):$(id -g "$remote_user")" "$state_dir" + chmod 0700 "$state_dir" + echo "copilot: $state_dir made for $remote_user (mode 700)" +else + echo "copilot: no remote user known at build; copilot-home makes the state folder at the first start or login shell" +fi + +# Login shells: COPILOT_HOME resolved by copilot-home (the Feature path, or ~/.copilot where that +# cannot be used), so the first terminal works even where no Feature hook ran (Envbuilder). +cat >/etc/profile.d/copilot-home.sh <<'P' +# Installed by the copilot Feature: make Copilot's state folder usable for this user (see copilot-home). +if command -v copilot-home >/dev/null 2>&1; then + case "$-" in + *i*) _copilot_home="$(copilot-home --path --quiet --self)" ;; + *) _copilot_home="$(copilot-home --path --quiet --self 2>/dev/null)" ;; + esac + if [ -n "$_copilot_home" ]; then export COPILOT_HOME="$_copilot_home"; fi + unset _copilot_home +fi +P +chmod 0644 /etc/profile.d/copilot-home.sh + # ------------------------------------------------------- ecosystem pieces ---- # dcf-persist makes the state directory usable (verbatim copy of src/common/dcf-persist.sh); # copilot-setup is this Feature's postCreateCommand (see devcontainer-feature.json) and reads the @@ -72,9 +108,11 @@ mkdir -p "$share" echo "# Written by the copilot Feature's install.sh from its options; sourced by copilot-setup." printf 'ATTACH=%q\n' "$ATTACH" printf 'PARTICIPANTS=%q\n' "$PARTICIPANTS" + printf 'REMOTE_USER=%q\n' "$remote_user" } > "$share/options.env" chmod 0644 "$share/options.env" install -m 0755 "$feature_dir/copilot-setup.sh" /usr/local/bin/copilot-setup +install -m 0755 "$feature_dir/copilot-home.sh" /usr/local/bin/copilot-home install -m 0755 "$feature_dir/agent-adapter-copilot.sh" /usr/local/bin/agent-adapter-copilot install -m 0755 "$feature_dir/dcf-persist.sh" /usr/local/bin/dcf-persist diff --git a/src/copilot/taskfile.yml b/src/copilot/taskfile.yml index 01eaa3b8..05a17966 100644 --- a/src/copilot/taskfile.yml +++ b/src/copilot/taskfile.yml @@ -26,7 +26,7 @@ tasks: setup: dir: '{{.USER_WORKING_DIR}}' - desc: 'Re-run the post-create setup: create COPILOT_HOME (by default the Feature''s named volume), hand it to the current user and keep it private to them, and register the facilitator entry when attach is facilitator. Idempotent.' + desc: 'Re-run the post-create setup: make Copilot''s state folder (the Feature''s named volume when it is mounted, else ~/.copilot with COPILOT_HOME''s path linked to it) exist, belong to the remote user and be mode 700, and register the facilitator entry when attach is facilitator. Run as root, it works for the remote user and leaves nothing root-owned. Idempotent.' cmds: - copilot-setup diff --git a/test/copilot/home_as_user.sh b/test/copilot/home_as_user.sh new file mode 100644 index 00000000..30f1f2b8 --- /dev/null +++ b/test/copilot/home_as_user.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# Scenario home_as_user: the copilot Feature with default options, the remote user vscode (uid 1000). +# Copilot's state folder must exist, belong to that user, be mode 700 and work for a first copilot +# run, which otherwise fails with "Failed to initialize session: ... Permission denied (os error 13)". +# That error needs no login to show: a usable folder gets past it to "No authentication information +# found", so each case runs `copilot -p` with no token and checks which of the two it says. +# The devcontainer CLI always applies the Feature's mount; Envbuilder ignores it and runs no Feature +# hook, which is played here with COPILOT_FEATURE_HOME on a folder of the image (the Feature path as +# Envbuilder leaves it: root's, or missing) with real sudo, and as a user with no sudo in a login shell. +# shellcheck disable=SC2016,SC2088 # the checks are expanded by the inner bash on purpose +set -e +# shellcheck disable=SC1091 # provided by the devcontainer CLI at test time +source dev-container-features-test-lib + +F=/var/lib/devcontainer-features/copilot +first_run() { # [env...]: what a first copilot run says, with no token; never its token + (cd /tmp && env -u COPILOT_GITHUB_TOKEN -u GH_TOKEN -u GITHUB_TOKEN "$@" timeout 60 copilot -p hi --no-auto-update &1 | head -n 3) +} +export -f first_run + +check "the tests run as the non-root user, uid 1000" bash -c '[ "$(id -u)" = 1000 ] && [ "$(id -un)" = vscode ]' +check "COPILOT_HOME is the Feature path" bash -c "[ \"\$COPILOT_HOME\" = $F ]" +check "the state folder exists, is vscode's and mode 700" bash -c "[ \"\$(stat -c '%U %a' $F)\" = 'vscode 700' ]" +check "the state folder is writable by vscode" bash -c "t=$F/.probe.\$\$ && touch \"\$t\" && rm -f \"\$t\"" +check "copilot --version runs as vscode" bash -c "copilot --version | grep -Eq 'Copilot CLI [0-9]+\.'" +check "~/.copilot links to the state folder" bash -c "[ \"\$(readlink ~/.copilot)\" = $F ]" +check "a first copilot run gets past the state folder (no Permission denied)" bash -c "o=\$(first_run); printf '%s' \"\$o\" | grep -q 'No authentication information' && ! printf '%s' \"\$o\" | grep -qi 'permission denied'" +check "the old failure shows on an unusable folder (the check can fail)" bash -c "first_run COPILOT_HOME=/var/lib/devcontainer-features/not-there | grep -q 'Permission denied'" +check "the build recorded the remote user" grep -qx 'REMOTE_USER=vscode' /usr/local/share/copilot-feature/options.env +check "copilot-home on PATH, root-owned" bash -c "[ \"\$(stat -c %U \"\$(command -v copilot-home)\")\" = root ]" +check "the login-shell snippet is installed" test -r /etc/profile.d/copilot-home.sh +check "a login shell keeps COPILOT_HOME on the Feature path" bash -c "[ \"\$(bash -lc 'echo \$COPILOT_HOME')\" = $F ]" + +# Run as root (a builder that runs hooks as root): nothing left root's, the rest done as vscode. +check "copilot-setup as root reports the folder" bash -c "sudo copilot-setup | grep -Eq '^copilot: $F \((volume|bind|image), writable\)\$'" +check "copilot-setup as root leaves nothing root-owned in the folder" bash -c "[ -z \"\$(sudo find $F -user root -print -quit)\" ] && [ \"\$(stat -c '%U %a' $F)\" = 'vscode 700' ]" +check "copilot-setup as root keeps ~/.copilot vscode's" bash -c "[ \"\$(stat -c %U ~/.copilot)\" = vscode ]" +check "copilot-setup is idempotent" bash -c "copilot-setup >/dev/null && copilot-setup | grep -Eq '^copilot: $F \((volume|bind|image), writable\)\$'" +check "a folder handed to root is handed back" bash -c "sudo chown root:root $F && sudo chmod 755 $F && copilot-setup >/dev/null && [ \"\$(stat -c '%U %a' $F)\" = 'vscode 700' ]" + +# Envbuilder, played: the Feature path is a root-owned folder of the image, not a mount. +check "no mount, with sudo: the Feature path becomes a link to ~/.copilot" bash -c ' + E=/var/lib/devcontainer-features/envb-sudo; sudo mkdir -p "$E"; sudo chmod 755 "$E" + export HOME; HOME="$(mktemp -d)" + COPILOT_FEATURE_HOME="$E" COPILOT_HOME="$E" copilot-home | grep -q "not a mount here" || exit 1 + [ "$(readlink "$E")" = "$HOME/.copilot" ] || exit 1 + [ "$(stat -L -c "%U %a" "$E")" = "vscode 700" ] || exit 1 + first_run COPILOT_HOME="$E" | grep -q "No authentication information"' +check "no mount, no sudo: a login shell uses ~/.copilot and copilot runs" bash -c ' + sudo useradd -m -s /bin/bash nosudo 2>/dev/null || true + E=/var/lib/devcontainer-features/envb-nosudo + o=$(sudo runuser -u nosudo -- env COPILOT_HOME="$E" COPILOT_FEATURE_HOME="$E" bash -lc "cd; echo CH=\$COPILOT_HOME; stat -c \"%U %a\" ~/.copilot; timeout 60 copilot -p hi --no-auto-update &1 | head -n 1") + printf "%s\n" "$o" | grep -qx "CH=/home/nosudo/.copilot" && printf "%s\n" "$o" | grep -qx "nosudo 700" && printf "%s" "$o" | grep -q "No authentication information"' +check "no mount, no sudo: copilot-setup prints the one-line remoteEnv hint" bash -c ' + E=/var/lib/devcontainer-features/envb-nosudo2 + sudo runuser -u nosudo -- env HOME=/home/nosudo COPILOT_HOME="$E" COPILOT_FEATURE_HOME="$E" copilot-setup 2>&1 | grep -q "remoteEnv"' +check "no token value is ever printed" bash -c "! COPILOT_GITHUB_TOKEN=ghp_secretvalue copilot-setup 2>&1 | grep -q secretvalue" + +reportResults diff --git a/test/copilot/scenarios.json b/test/copilot/scenarios.json index dbf53acd..2accd8e7 100644 --- a/test/copilot/scenarios.json +++ b/test/copilot/scenarios.json @@ -59,5 +59,12 @@ } }, "remoteUser": "vscode" + }, + "home_as_user": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "copilot": {} + }, + "remoteUser": "vscode" } } diff --git a/test/copilot/unit/home_test.sh b/test/copilot/unit/home_test.sh new file mode 100644 index 00000000..697a0e7d --- /dev/null +++ b/test/copilot/unit/home_test.sh @@ -0,0 +1,134 @@ +#!/usr/bin/env bash +# Docker-free tests of the copilot Feature's copilot-home (src/copilot/copilot-home.sh), run as the +# calling user over temp folders, with a fake sudo that either runs the command or refuses +# (FAKE_SUDO=deny), and the mount check replaced by COPILOT_FEATURE_MOUNTED: +# a mounted Feature folder: kept, mode 700, ~/.copilot linked to it; a real ~/.copilot copied in +# without overwriting and kept aside; an existing link elsewhere left alone +# no mount, root available (Envbuilder with sudo): ~/.copilot made 700, what the image folder held +# copied there, the Feature path replaced by a link to it, the same on a second run +# no mount, no root: ~/.copilot as the path, the hint on stderr only without --quiet +# a COPILOT_HOME of the repo's own: made and kept; the fallback given up once the Feature path works +# a foreign-owned mount (simulated: not writable and no root): one actionable line, ~/.copilot meanwhile +# no token or other secret in any output +# Part of `task lint` (`task test:copilot-unit`). HOME_SH points the test at another copy. +set -u + +here="$(cd "$(dirname "$0")" && pwd)" +root="$(cd "$here/../../.." && pwd)" +home_sh="${HOME_SH:-$root/src/copilot/copilot-home.sh}" + +T="$(mktemp -d)" +trap 'chmod -R u+rwx "$T" 2>/dev/null; rm -rf "$T"' EXIT +T="$(cd "$T" && pwd -P)" + +pass=0 fail=0 +check() { if [ "$2" = "$3" ]; then pass=$((pass + 1)); echo "ok - $1"; else fail=$((fail + 1)); echo "FAIL - $1 (expected '$2', got '$3')"; fi; } +contains() { case "$2" in *"$3"*) pass=$((pass + 1)); echo "ok - $1" ;; *) fail=$((fail + 1)); echo "FAIL - $1 (no '$3' in '$2')" ;; esac; } +lacks() { case "$2" in *"$3"*) fail=$((fail + 1)); echo "FAIL - $1 ('$3' found in '$2')" ;; *) pass=$((pass + 1)); echo "ok - $1" ;; esac; } + +mkdir -p "$T/bin" +cp "$home_sh" "$T/bin/copilot-home"; chmod +x "$T/bin/copilot-home" +cat > "$T/bin/sudo" <<'FAKE' +#!/usr/bin/env bash +[ "${FAKE_SUDO:-allow}" = deny ] && exit 1 +[ "${1:-}" = -n ] && shift +exec "$@" +FAKE +chmod +x "$T/bin/sudo" +export PATH="$T/bin:$PATH" COPILOT_FEATURE_OPTIONS="$T/none.env" GH_TOKEN=ghp_secretvalue + +fresh() { # a new case: a home and a parent for the Feature folder + C="$T/$1"; mkdir -p "$C/home" "$C/var" + export HOME="$C/home" COPILOT_FEATURE_HOME="$C/var/copilot" COPILOT_HOME="$C/var/copilot" + F="$C/var/copilot" +} +run() { copilot-home "$@" 2>"$C/err"; } +mode() { stat -c %a "$1"; } + +# ---- a mounted Feature folder ---- +fresh mounted; mkdir -p "$F"; chmod 755 "$F" +out="$(COPILOT_FEATURE_MOUNTED=yes run)" +check "mounted: the folder is mode 700" 700 "$(mode "$F")" +contains "mounted: reported writable" "$out" "copilot: $F (" +contains "mounted: reported writable (state)" "$out" ", writable)" +check "mounted: ~/.copilot links to it" "$F" "$(readlink "$HOME/.copilot")" +check "mounted: --path prints the Feature path" "$F" "$(COPILOT_FEATURE_MOUNTED=yes run --path)" +out2="$(COPILOT_FEATURE_MOUNTED=yes run)" +lacks "mounted: a second run links nothing again" "$out2" "links to $F" +lacks "mounted: no token in the output" "$out$out2" secretvalue + +fresh mounted-home; mkdir -p "$F" "$HOME/.copilot"; echo old > "$HOME/.copilot/config.json"; echo keep > "$HOME/.copilot/session"; echo vol > "$F/session" +out="$(COPILOT_FEATURE_MOUNTED=yes run)" +check "a real ~/.copilot: its file copied in" old "$(cat "$F/config.json")" +check "a real ~/.copilot: nothing in the volume overwritten" vol "$(cat "$F/session")" +check "a real ~/.copilot: kept aside" keep "$(cat "$HOME/.copilot.before-copilot-feature/session")" +check "a real ~/.copilot: replaced by the link" "$F" "$(readlink "$HOME/.copilot")" +contains "a real ~/.copilot: said" "$out" "copied $HOME/.copilot into $F" + +fresh mounted-other; mkdir -p "$F" "$C/elsewhere"; ln -s "$C/elsewhere" "$HOME/.copilot" +out="$(COPILOT_FEATURE_MOUNTED=yes run)" +check "a ~/.copilot link elsewhere is left alone" "$C/elsewhere" "$(readlink "$HOME/.copilot")" +contains "a ~/.copilot link elsewhere is said" "$out" "links elsewhere" + +# ---- no mount, root available (Envbuilder with sudo) ---- +fresh redirect; mkdir -p "$F"; echo image > "$F/mcp-config.json" +out="$(COPILOT_FEATURE_MOUNTED=no run)" +check "no mount + root: the Feature path is a link" "$HOME/.copilot" "$(readlink "$F")" +check "no mount + root: ~/.copilot is a real folder, mode 700" "700" "$(test -d "$HOME/.copilot" && ! test -L "$HOME/.copilot" && mode "$HOME/.copilot")" +check "no mount + root: what the image folder held is kept" image "$(cat "$HOME/.copilot/mcp-config.json")" +contains "no mount + root: said" "$out" "links to $HOME/.copilot" +check "no mount + root: --path is the Feature path (containerEnv agrees)" "$F" "$(COPILOT_FEATURE_MOUNTED=no run --path)" +out="$(COPILOT_FEATURE_MOUNTED=no run)" +check "no mount + root: a second run keeps the link" "$HOME/.copilot" "$(readlink "$F")" +check "no mount + root: no nested link inside ~/.copilot" "" "$(find "$HOME/.copilot" -type l)" + +fresh redirect-missing; rmdir "$C/var" +COPILOT_FEATURE_MOUNTED=no run >/dev/null +check "no mount + root, nothing at the Feature path: the link and its parent made" "$HOME/.copilot" "$(readlink "$F")" + +# ---- no mount, no root (Envbuilder without sudo) ---- +fresh noroot +p="$(FAKE_SUDO=deny COPILOT_FEATURE_MOUNTED=no run --path --quiet)" +check "no mount, no root: --path is ~/.copilot" "$HOME/.copilot" "$p" +check "no mount, no root: ~/.copilot mode 700" 700 "$(mode "$HOME/.copilot")" +check "no mount, no root: --quiet says nothing" "" "$(cat "$C/err")" +check "no mount, no root: the Feature path not created" no "$(test -e "$F" && echo yes || echo no)" +FAKE_SUDO=deny COPILOT_FEATURE_MOUNTED=no run >/dev/null +contains "no mount, no root: the hint without --quiet names remoteEnv" "$(cat "$C/err")" '"COPILOT_HOME": "'"$HOME/.copilot"'"' + +fresh noroot-image; mkdir -p "$F"; echo image > "$F/config.json" +FAKE_SUDO=deny COPILOT_FEATURE_MOUNTED=no run --quiet >/dev/null +check "no mount, no root: a user-owned image folder's content copied to ~/.copilot" image "$(cat "$HOME/.copilot/config.json")" + +# ---- COPILOT_HOME of the repo's own, and the shell fallback ---- +fresh override; export COPILOT_HOME="$C/mine/cfg" +out="$(run)"; p="$(run --path)" +check "override: --path is the repo's path" "$C/mine/cfg" "$p" +check "override: made mode 700" 700 "$(mode "$C/mine/cfg")" +contains "override: reported" "$out" "copilot: $C/mine/cfg (" +check "override: ~/.copilot not touched" no "$(test -e "$HOME/.copilot" && echo yes || echo no)" + +fresh fallback-gone; mkdir -p "$F" "$HOME/.copilot"; export COPILOT_HOME="$HOME/.copilot" +check "the shell fallback is given up once the Feature mount works" "$F" "$(COPILOT_FEATURE_MOUNTED=yes run --path)" + +# ---- a mount the user cannot use (no root to fix it) ---- +fresh foreign; mkdir -p "$F"; chmod 500 "$F" +# The owner could chmod its own folder; a foreign owner is simulated by a chmod that fails. +cat > "$T/bin/chmod" <<'FAKE' +#!/usr/bin/env bash +for a in "$@"; do [ "$a" = "$COPILOT_FEATURE_HOME" ] && exit 1; done +exec /bin/chmod "$@" +FAKE +chmod +x "$T/bin/chmod" +p="$(FAKE_SUDO=deny COPILOT_FEATURE_MOUNTED=yes run --path --quiet)" +rm -f "$T/bin/chmod" +check "unusable mount: login shells use ~/.copilot" "$HOME/.copilot" "$p" +contains "unusable mount: one actionable line, even with --quiet" "$(cat "$C/err")" "sudo chown -R $(id -un) '$F'" +check "unusable mount: exactly one line" 1 "$(wc -l < "$C/err")" + +# ---- never fatal ---- +fresh usage +copilot-home --bogus >/dev/null 2>&1; check "a wrong call exits 0" 0 "$?" + +echo "copilot-home: $pass passed, $fail failed" +[ "$fail" -eq 0 ] From 131396a514d44e2476b1a63db1113daff02625e6 Mon Sep 17 00:00:00 2001 From: devhost agent Date: Wed, 2 Dec 2026 13:20:30 +0000 Subject: [PATCH 2/2] test(copilot): check a non-root remote user, not uid 1000 (copilot-home-writable) The devcontainer CLI remaps vscode to the runner's uid (1001 on GitHub), so the fixed uid failed the home_as_user scenario in CI and stopped its other checks. Co-Authored-By: Claude Opus 5.5 Signed-off-by: devhost agent --- test/copilot/home_as_user.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/copilot/home_as_user.sh b/test/copilot/home_as_user.sh index 30f1f2b8..b378b6b8 100644 --- a/test/copilot/home_as_user.sh +++ b/test/copilot/home_as_user.sh @@ -1,5 +1,6 @@ #!/bin/bash -# Scenario home_as_user: the copilot Feature with default options, the remote user vscode (uid 1000). +# Scenario home_as_user: the copilot Feature with default options, the remote user vscode (uid 1000 in +# the image, remapped to the host uid by the devcontainer CLI). # Copilot's state folder must exist, belong to that user, be mode 700 and work for a first copilot # run, which otherwise fails with "Failed to initialize session: ... Permission denied (os error 13)". # That error needs no login to show: a usable folder gets past it to "No authentication information @@ -18,7 +19,9 @@ first_run() { # [env...]: what a first copilot run says, with no token; never it } export -f first_run -check "the tests run as the non-root user, uid 1000" bash -c '[ "$(id -u)" = 1000 ] && [ "$(id -un)" = vscode ]' +# vscode is uid 1000 in the image; the devcontainer CLI remaps it to the host's uid (1001 on GitHub's +# runners), so the check is "not root", not a fixed number. +check "the tests run as the non-root remote user" bash -c '[ "$(id -u)" != 0 ] && [ "$(id -un)" = vscode ]' check "COPILOT_HOME is the Feature path" bash -c "[ \"\$COPILOT_HOME\" = $F ]" check "the state folder exists, is vscode's and mode 700" bash -c "[ \"\$(stat -c '%U %a' $F)\" = 'vscode 700' ]" check "the state folder is writable by vscode" bash -c "t=$F/.probe.\$\$ && touch \"\$t\" && rm -f \"\$t\""