Files
erp/.claude/skills/dolibarr-sandbox-checkpoint/scripts/checkpoint-relink-env.sh
T
arcodangeandClaude Opus 5 2e699e1fc6 feat(sandbox): scoped agents provisioned by the checkpoint cycle
Completes the role model on the sandbox side, and closes the third failure of
the 2026-07/08 sessions: a refresh wiped hand-granted rights and nothing
recorded them, so the sandbox silently lost capabilities nobody had written down.

- Provisioned `ai_agent_sandbox_read` (36 rights) and
  `ai_agent_sandbox_sandbox_write` (44 rights) from test/scopes.ts.
  Verified functionally on the live sandbox: the reader reads and gets
  403 on invoice creation; the writer creates a draft and gets 403 on DELETE.
- checkpoint-provision.sh now re-creates both scoped agents after every
  refresh, so their rights come from code rather than from someone's memory.
  Failure to provision a scope warns instead of aborting the whole checkpoint.
- checkpoint-relink-env.sh points the write skill at the scoped writer key,
  falling back to the legacy single-user key so an older checkout still works.
  The write skill now operates as ai_agent_sandbox_sandbox_write (id 6).

Smoke-tested end to end after the credential swap: the promote pipeline
rehearses on the sandbox under the scoped writer, and `apply` still refuses
without a recorded human gate.

The redundant `ai_agent_prod_prod_write` login is documented as deliberate:
renaming a provisioned production credential means creating a second privileged
user and repointing the promote flow — churn for cosmetics.

Left behind in the sandbox: draft invoice id=19, a scope probe. It cannot be
deleted (no scope grants DELETE, which is the point) and the next refresh
reclaims it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VRShc4QhLLU73FLHx9vskh
2026-08-09 19:41:37 +02:00

41 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# (Re)write the dolibarr-sandbox-write skill .env from the provisioned key file,
# then verify it authenticates. Run this after 'provision' (or any time the key
# changed). The key is instance-encrypted per Dolibarr instance, so a refresh
# invalidates it — re-provision first if this fails to authenticate.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="${ARCO_ROOT:-$(cd "${SCRIPT_DIR}/../../../.." && pwd)}"
SB_URL="${DOLIBARR_SANDBOX_URL:-https://erp-sandbox.arcodange.lab}"
# Prefer the SCOPED writer (test/scopes.ts, `sandbox-write`); fall back to the
# legacy single-user key so an older checkout keeps working. The scoped user is
# what a refresh re-provisions, which is why it wins.
KEY="${ROOT}/test/.ai_agent_sandbox_sandbox_write.key"
[[ -s "${KEY}" ]] || KEY="${ROOT}/test/.ai_agent_sandbox.key"
ENV="${ROOT}/.claude/skills/dolibarr-sandbox-write/.env"
DOLW="${ROOT}/.claude/skills/dolibarr-sandbox-write/scripts/dol-write.sh"
[[ -s "${KEY}" ]] || { echo "checkpoint-relink-env: no key at ${KEY}" >&2
echo " run 'arcodange sandbox checkpoint provision' first." >&2; exit 1; }
umask 077
{ echo "DOLIBARR_SANDBOX_URL=${SB_URL}"
printf 'DOLIBARR_SANDBOX_API_KEY=%s\n' "$(tr -d '\r\n' < "${KEY}")"; } > "${ENV}"
chmod 600 "${ENV}"
echo "✓ wrote ${ENV} (mode 600)"
printf 'verify: '
"${DOLW}" GET /users/info | python3 -c "import json,sys
d = json.load(sys.stdin)
if isinstance(d, dict) and d.get('login'):
print('OK — armed as %s (id %s)' % (d['login'], d.get('id'))); sys.exit(0)
err = d.get('error', {}) if isinstance(d, dict) else {}
code = str(err.get('code', ''))
if code == '401':
print('FAILED — 401 key rejected (stale/instance-encrypted) → re-run checkpoint provision')
elif code == '403':
print('FAILED — 403 key authenticates but right 251 (user lire) is missing → re-provision or grant 251 (WRITE_IDS, test/provisionSandbox.ts)')
else:
print('FAILED — %s' % (err.get('message', '?') if isinstance(d, dict) else str(d)))
sys.exit(1)"