8000 feat(claude): render the work host's planwright overlay from its own work-account item by inkatze · Pull Request #230 · inkatze/dotfiles · GitHub
[go: up one dir, main page]

Skip to content
Merged
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ jobs:
timeout-minutes: 10
run: scripts/mise-global-config-test.sh

# Needs ansible-playbook, which this job installs, and jq; stubs op.
- name: Run the planwright overlay render suite
timeout-minutes: 10
run: scripts/claude-overlay-render-test.sh

# macOS ships no timeout; without gtimeout the suite skips its FIFO case,
# which bounds a play that would hang on a FIFO.
- name: Install GNU coreutils for gtimeout
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ row names a writer. Keep machine-specific values here, never in tracked files.
| `host` | `scripts/playbook.sh`, `roles/claude/files/skills/review-shared/backends.md` | This machine's inventory alias; empty or whitespace-only counts as absent |
| `ssh-host` | the `sshc` function in `roles/fish/files/fish/config.fish` | `kitten ssh` target hostname |
| `kitty-ssh.conf` | `roles/kitty/files/kitty/ssh.conf` (`globinclude`) | Host-specific kitty `ssh.conf` sections |
| `op-account`, `op-work-item` | `scripts/playbook.sh` | The 1Password account a playbook run's `op` calls use, exported as `OP_ACCOUNT` (set that yourself for a direct script run), where more than one is signed in; `op-work-item` names the account, vault and item of the second item a work-only reviewer renders from (0600) |
| `op-account`, `op-work-item` | `scripts/playbook.sh` | The 1Password account a playbook run's `op` calls use, exported as `OP_ACCOUNT` (set that yourself for a direct script run), where more than one is signed in; `op-work-item` names the account, vault and item of the second item a work-only reviewer renders from, and the account and vault a work host's own overlay item is read from (0600) |
| `op-service-account-token` | `scripts/op-token.sh`, for the 1Password syncs | Service-account token, a bearer credential (0600) |
| `git-work-email`, `git-work-orgs` | `roles/git/defaults/main.yml` | The work identity written to `~/.gitconfig.work`; `git-work-orgs` names the GitHub owners whose repositories also take it, rendered by `scripts/op-render.sh` from `roles/git/files/git-work-orgs.tpl` |
| `planwright-overlay.supplement.yml`, `planwright-overlay.rendered` | `scripts/op-render.sh` | Mission control's own writes to the planwright adopter overlay, carried over the 1Password item, and the snapshot of the last render that tells them apart (0600), both written by the renderer |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ map beside them are rendered from 1Password by the claude role; the repo
| `ssh-host` | the `sshc` fish function | Hostname `kitten ssh` connects to |
| `kitty-ssh.conf` | kitty's `ssh.conf`, via `globinclude` | Host-specific kitty `ssh.conf` sections (see that file for an example) |
| `op-account` | `scripts/playbook.sh` | The 1Password account (sign-in address or user id) every `op` call should use (the `op-work-item` read names its own), e.g. `my.1password.com`. Only needed where `op account list` shows more than one account — the normal state of a work Mac that carries a company tenant alongside a personal one. With two accounts and no selection, `op` fails every read with "multiple accounts found", which takes down the GitHub MCP sync and the Pushover credential read. An absent, empty or whitespace-only file exports nothing, so single-account hosts are unaffected; an already-exported `OP_ACCOUNT` wins. Any other file value that isn't an account identifier (one starting with a letter or digit and made only of letters, digits, `.`, `_`, `@` and `-`) makes `scripts/playbook.sh` refuse to run; an exported `OP_ACCOUNT` is not checked. |
| `op-work-item` | `scripts/playbook.sh`, for `scripts/op-render.sh` | `DOTFILES_OP_WORK_ACCOUNT=`, `DOTFILES_OP_WORK_VAULT=` and `DOTFILES_OP_WORK_ITEM=` lines naming a second 1Password item, read with `op --account`, that the review template's work-only reviewer renders from; absent, that reviewer is left out. It must be a regular file you own, mode 0600 or 0400, with no ACL, or the playbook refuses to run |
| `op-work-item` | `scripts/playbook.sh`, for `scripts/op-render.sh` | `DOTFILES_OP_WORK_ACCOUNT=`, `DOTFILES_OP_WORK_VAULT=` and `DOTFILES_OP_WORK_ITEM=` lines naming a second 1Password item, read with `op --account`, that the review template's work-only reviewer renders from; absent, that reviewer is left out. The same account and vault hold a work host's own planwright overlay item, which that host refuses to render without this file. It must be a regular file you own, mode 0600 or 0400, with no ACL, or the playbook refuses to run |
| `op-service-account-token` | `scripts/ssh-lan-config-sync.sh`, `scripts/claude-gemini-auth-sync.sh`, `scripts/op-render.sh`, `scripts/op-key-sync.sh`, all through `scripts/op-token.sh` | 1Password service-account token, mode 0600. The only secret here, and the only way these syncs can authenticate on a headless host, where there is no desktop app to approve them. Each role probes for `op` first and skips with a notice when it is missing; but on a host that *has* `op` and no token, each of them fails its run, because from `op`'s side that is indistinguishable from a locked vault. On a Mac with the desktop app unlocked, none of them needs this file. See `docs/machine-local-files.md` for how to mint one. |

`host` is the one that matters most: `scripts/playbook.sh` uses it to pick
Expand Down
10 changes: 10 additions & 0 deletions docs/machine-local-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ app rather than the service-account token, which cannot reach another
account; with neither the file nor the three variables exported, every such
reference resolves empty and the entry drops out. The file names an employer's account, so it stays untracked.

A host listed in `claude_planwright_overlay_work_hosts`
(`roles/claude/defaults/main.yml`) renders the overlay from its own item,
`dotfiles-planwright-overlay-<alias>`, which `op-render.sh --work-account`
reads from the account and vault `op-work-item` names (the item it names is
for the review template and plays no part here). Its program settings
therefore never sit in the shared item the other hosts render, and a listed
host with no `op-work-item` fails the play rather than render the shared
item's programs. Create that item, with the same `flight_pr_hosts` and
`mission_control` fields, before the host's first run after joining the list.

`cubic-api-key` is synced by `scripts/op-key-sync.sh` from the `credential`
field of the `dotfiles-cubic-api-key` item (category API Credential), through a
task in the same file and behind the same guards. It is a raw key, not a
Expand Down
12 changes: 12 additions & 0 deletions roles/claude/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ claude_planwright_repo: inkatze/planwright
# planwright's adopter overlay root, the same path conf.d/planwright.fish
# exports as PLANWRIGHT_ADOPTER_OVERLAY.
claude_planwright_overlay: "{{ ansible_facts.env.HOME }}/.claude/plugins/data/planwright-planwright/overlay"
# Listed hosts render the overlay from their own item,
# dotfiles-planwright-overlay-<alias>, read from the work account with
# op-render.sh --work-account, so their program settings stay out of the
# personal item and a host without that account refuses rather than rendering
# the personal item's programs. Every other host shares
# dotfiles-planwright-overlay.
claude_planwright_overlay_work_hosts:
- work
claude_planwright_overlay_from_work: "{{ inventory_hostname in claude_planwright_overlay_work_hosts }}"
claude_planwright_overlay_item: >-
{{ 'dotfiles-planwright-overlay-' ~ inventory_hostname
if claude_planwright_overlay_from_work else 'dotfiles-planwright-overlay' }}
claude_steps_catalog_src: "{{ ansible_facts.env.PWD }}/roles/claude/files/planwright/steps.yaml"
# The first line of the tracked catalog, and what makes a file at the overlay's
# catalogs/steps.yaml this role's to replace or remove.
Expand Down
16 changes: 9 additions & 7 deletions roles/claude/tasks/op-render.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---

# Machine-local files rendered from 1Password items in the service-account
# vault (and, where op-work-item exists, one item read with `op --account`
# outside it) by scripts/op-render.sh, which validates each before it lands, and
# vault (and, where op-work-item exists, items read with `op --account`
# outside it: its named item, and a work host's own overlay item) by
# scripts/op-render.sh, which validates each before it lands, and
# API keys synced by scripts/op-key-sync.sh. Behind
# the same `op` probe split as the Gemini key sync in main.yml: a host without
# op is skipped with a notice, a host with op that still fails is an error.
Expand Down Expand Up @@ -44,11 +45,12 @@

- name: Render the planwright adopter overlay config from 1Password
ansible.builtin.command:
argv:
- "{{ ansible_facts.env.PWD }}/scripts/op-render.sh"
- "{{ ansible_facts.env.PWD }}/roles/claude/files/planwright/planwright.yml.tpl"
- dotfiles-planwright-overlay
- "{{ claude_planwright_overlay }}/planwright.yml"
argv: >-
{{ [ansible_facts.env.PWD ~ '/scripts/op-render.sh']
+ (['--work-account'] if claude_planwright_overlay_from_work | bool else [])
+ [ansible_facts.env.PWD ~ '/roles/claude/files/planwright/planwright.yml.tpl',
claude_planwright_overlay_item,
claude_planwright_overlay ~ '/planwright.yml'] }}
register: claude_render_overlay_config
changed_when: "'CHANGED' in claude_render_overlay_config.stdout"
failed_when: false
Expand Down Expand Up @@ -79,7 +81,7 @@
# is where a failure stops the play, naming every one that failed.
- name: Fail on any 1Password render or key sync that failed
ansible.builtin.fail:
msg: >-

Check warning on line 84 in roles/claude/tasks/op-render.yml

View workflow job for this annotation

GitHub Actions / lint

jinja[spacing]

Jinja2 spacing could be improved: {% for r in claude_op_failed %}{{ r.stderr | default('', true) or r.msg | default('failed with no output') }} {{- '' if loop.last else ' | ' }}{% endfor %} -> {% for r in claude_op_failed %}{{ r.stderr | default('', true) or r.msg | default('failed with no output') }}{{- '' if loop.last else ' | ' }}{% endfor %}
{% for r in claude_op_failed %}{{ r.stderr | default('', true) or r.msg | default('failed with no output') }}
{{- '' if loop.last else ' | ' }}{% endfor %}
vars:
Expand Down
178 changes: 178 additions & 0 deletions scripts/claude-overlay-render-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/usr/bin/env bash
# Fixture suite for the planwright overlay render in
# roles/claude/tasks/op-render.yml: which item each inventory host renders its
# overlay from, and from which 1Password account. A work host reads its own
# item from the work account and never the personal one; every other host
# renders exactly what it did before the work host had an item of its own.
#
# Driven through ansible-playbook as each inventory alias, against scratch
# HOMEs and a fake `op`, from a scratch directory whose roles/ links into this
# repo. Its scripts/ holds stand-ins that pass the overlay render to the real
# scripts/op-render.sh and answer every other render and key sync with OK, so
# the cases see only the overlay.
set -uo pipefail
unset GIT_DIR GIT_INDEX_FILE GIT_WORK_TREE
unset DOTFILES_OP_WORK_ACCOUNT DOTFILES_OP_WORK_VAULT DOTFILES_OP_WORK_ITEM
unset OP_SERVICE_ACCOUNT_TOKEN DOTFILES_OP_TOKEN_FILE DOTFILES_OP_VAULT

here="$(cd -- "$(dirname "$0")" && pwd -P)"
repo="$(cd -- "$here/.." && pwd -P)"
work="$(cd -- "$(mktemp -d)" && pwd -P)" || { echo "FAIL[harness]: mktemp failed"; exit 1; }
# Remove the link into this repo before anything else: a recursive delete must
# never reach the checkout through it.
trap 'rm -f "$work/roles"; rm -rf "$work"' EXIT
trap 'exit 130' INT TERM HUP
fails=0

ok() { printf 'ok[%s]: %s\n' "$1" "$2"; }
fail() { printf 'FAIL[%s]: %s\n' "$1" "$2"; fails=$((fails + 1)); }

tpl="$repo/roles/claude/files/planwright/planwright.yml.tpl"
ln -s "$repo/roles" "$work/roles" || { echo "FAIL[harness]: cannot link roles"; exit 1; }
mkdir -p "$work/scripts" "$work/bin" "$work/items"

cat >"$work/scripts/op-render.sh" <<STUB
#!/usr/bin/env bash
printf '%s\n' "\$*" >>"\$RENDER_ARGV"
case " \$* " in
*"/planwright.yml.tpl "*) exec "$repo/scripts/op-render.sh" "\$@" ;;
esac
echo "OK: stand-in"
STUB
printf '#!/usr/bin/env bash\necho "OK: stand-in"\n' >"$work/scripts/op-key-sync.sh"

# The fake op serves the personal item from the service-account vault (no
# --account) and the work host's item from the work account and vault alone.
cat >"$work/bin/op" <<'FAKE'
#!/usr/bin/env bash
printf '%s\n' "$*" >>"$OP_ARGV"
[ "$1 $2" = "item get" ] || exit 2
case " $* " in
*" --account work.example.com "*)
[ "$3" = dotfiles-planwright-overlay-work ] && [[ " $* " == *" --vault Work Vault "* ]] \
&& exec cat "$OP_ITEMS/work.json" ;;
*" --account "*) ;;
*)
[ "$3" = dotfiles-planwright-overlay ] && [[ " $* " == *" --vault Dotfiles Service Account "* ]] \
&& exec cat "$OP_ITEMS/personal.json" ;;
esac
echo "[ERROR] \"$3\" isn't an item in this account and vault" >&2
exit 1
FAKE
chmod +x "$work/scripts/op-render.sh" "$work/scripts/op-key-sync.sh" "$work/bin/op"

# to_item <file> <label=value>...: an item in op's JSON shape.
to_item() {
local dest="$1"
shift
printf '%s\n' "$@" | jq -R -s '
split("\n") | map(select(length > 0) | capture("^(?<label>[^=]+)=(?<value>.*)$"))
| {id: "stub", title: "stub", fields: map({id: .label, type: "STRING", label, value})}' >"$dest"
}
to_item "$work/items/personal.json" 'flight_pr_hosts=[github.com/personal-org]' \
'mission_control=mission_control_programs: [personal-program]'
to_item "$work/items/work.json" 'flight_pr_hosts=[github.com/work-org]' \
'mission_control=mission_control_programs: [work-program]'

play="$work/play.yml"
cat >"$play" <<'YAML' || { echo "FAIL[harness]: cannot write the play"; exit 1; }
- name: Exercise the 1Password render tasks
hosts: all
connection: local
gather_facts: true
# Only the env subset op-render reads: the default set and `min` each cost
# about 72s a play on GitHub's macOS runners.
gather_subset: ['!all', '!min', 'env']
tasks:
- name: Run only the 1Password render tasks
ansible.builtin.include_role:
name: claude
tasks_from: op-render
YAML

work_source=(DOTFILES_OP_WORK_ACCOUNT=work.example.com "DOTFILES_OP_WORK_VAULT=Work Vault" DOTFILES_OP_WORK_ITEM=work-item)

# run_as <alias> [env assignments...]: a fresh HOME, the play run as that
# inventory host; 0 when it completed, 1 when a task failed.
run_as() {
local alias="$1"
shift
h="$(mktemp -d "$work/h.XXXXXX")" || { echo "FAIL[harness]: mktemp failed"; exit 1; }
ov="$h/.claude/plugins/data/planwright-planwright/overlay/planwright.yml"
: >"$work/op-argv"
: >"$work/render-argv"
# CI unset, since the tasks skip under it; become off, so sudo cannot reset
# HOME and aim the role at a real home; fact injection off, as the repo's
# ansible.cfg (unread from $work) has it.
# stdin is /dev/null because an open stdin pipe (a backgrounded shell's) hangs
# the play.
(cd "$work" && env -u CI -u ANSIBLE_CONFIG "$@" HOME="$h" PATH="$work/bin:$PATH" \
OP_ARGV="$work/op-argv" OP_ITEMS="$work/items" RENDER_ARGV="$work/render-argv" \
ANSIBLE_BECOME=false ANSIBLE_INJECT_FACT_VARS=false ANSIBLE_PIPELINING=true \
ANSIBLE_STDOUT_CALLBACK=ansible.builtin.default ANSIBLE_CALLBACK_RESULT_FORMAT=json \
ansible-playbook -i "$alias," "$play" -e '{"claude_op_probe": {"rc": 0}}' </dev/null >"$work/out" 2>&1)
grep -q 'PLAY RECAP' "$work/out" || { echo "FAIL[harness]: no recap"; sed 's/^/ /' "$work/out" | tail -20; exit 1; }
grep -q 'Render the planwright adopter overlay config' "$work/out" \
|| { echo "FAIL[harness]: the overlay render never ran"; sed 's/^/ /' "$work/out" | tail -20; exit 1; }
grep -qE 'failed=0 ' "$work/out"
}
overlay_argv() { grep -F '/planwright.yml.tpl ' "$work/render-argv"; }
show() { sed 's/^/ /' "$work/out" | tail -12; }

# What a host rendered before work hosts had their own item: the template and
# the shared item, through the real renderer directly.
ref_home="$(mktemp -d "$work/ref.XXXXXX")"
(env HOME="$ref_home" PATH="$work/bin:$PATH" OP_ARGV="$work/ref-argv" OP_ITEMS="$work/items" \
"$repo/scripts/op-render.sh" "$tpl" dotfiles-planwright-overlay "$ref_home/planwright.yml" >/dev/null) \
|| { echo "FAIL[harness]: the reference render failed"; exit 1; }

# 1. Every host but work renders the shared item exactly as before, through
# the same renderer call, and never reads the work account, even with the
# work source exported.
for alias in personal alt server; do
for exported in no yes; do
extra=()
[ "$exported" = no ] || extra=("${work_source[@]}")
label="$alias-unchanged$([ "$exported" = no ] || echo -with-work-source)"
if ! run_as "$alias" ${extra[@]+"${extra[@]}"}; then
fail "$label" "the play failed"; show; continue
fi
if [ "$(overlay_argv)" = "$work/roles/claude/files/planwright/planwright.yml.tpl dotfiles-planwright-overlay $ov" ] &&
cmp -s "$ref_home/planwright.yml" "$ov" && ! grep -q -- '--account' "$work/op-argv"; then
ok "$label" "shared item rendered byte-for-byte as before, no work-account read"
else
fail "$label" "argv=$(overlay_argv) op=$(cat "$work/op-argv")"
fi
done
done

# 2. The work host renders its own item from the work account, and nothing of
# the shared item's programs.
if run_as work "${work_source[@]}"; then
if [ "$(overlay_argv)" = "--work-account $work/roles/claude/files/planwright/planwright.yml.tpl dotfiles-planwright-overlay-work $ov" ] &&
grep -qx 'mission_control_programs: \[work-program\]' "$ov" &&
grep -qx 'flight_pr_hosts: \[github.com/work-org\]' "$ov" && ! grep -q personal "$ov" &&
! grep -v -- '--account' "$work/op-argv" | grep -q .; then
ok work-own-item "work item rendered from the work account, no shared programs"
else
fail work-own-item "argv=$(overlay_argv) op=$(cat "$work/op-argv") overlay=$(cat "$ov" 2>/dev/null)"
fi
else
fail work-own-item "the play failed"; show
fi

# 3. A work host without the work source fails the play rather than fall back
# to the shared item, and nothing is written.
if run_as work; then
fail work-no-fallback "the play succeeded"; show
elif [ ! -e "$ov" ] && ! [ -s "$work/op-argv" ] && grep -qF -- '--work-account needs DOTFILES_OP_WORK_ACCOUNT' "$work/out"; then
ok work-no-fallback "play failed naming the missing work source, no op read, no overlay written"
else
fail work-no-fallback "op=$(cat "$work/op-argv") overlay=$(cat "$ov" 2>/dev/null)"; show
fi

if [ "$fails" -gt 0 ]; then
printf 'claude-overlay-render-test: %d assertion(s) failed\n' "$fails"
exit 1
fi
printf 'claude-overlay-render-test: all assertions hold\n'
Loading
Loading
0