Skip to content

fix(sbx): confirm step 7's cleanup instead of believing it - #669

Merged
cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:fix/653-smoke-step7-cleanup
Nov 2, 2026
Merged

cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:fix/653-smoke-step7-cleanup

Conversation

@cwiklik

@cwiklik cwiklik commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

What

The last unchecked item under #653's "Test gaps that hide real checks (do first)": step 7's
(egress credentials) deferred cleanup follow-ups in deploy/sbx/smoke.sh.

Three things the step-7 cleanup reported as done but had not done. Each is the case the cleanup
exists to catch, so a leak was reported as cleaned up.

1. drop_branch believed GitHub's ambiguous "gone" codes

It accepted 204 | 404 | 422 as success. But GitHub sends Reference does not exist as both
422 and 404 (cli/cli#11187), 422 is also its answer for the default branch and for a spammed
endpoint, and a PAT that can no longer reach the repository gets 404 for every ref. So a
revoked PAT left the branch and the smoke said it had deleted it.

Tightening the accepted-code list cannot fix this — the codes are genuinely ambiguous. Anything
but a 204 is now confirmed: the ref reads as absent and the repository still answers with
this PAT (the second probe is what separates "gone" from "I can't see it").

2. The trap deleted credential names no add had ever stored

cred_add records a name before the add. That is deliberate: an add whose answer was lost did
store the credential server-side, so it must still be cleaned up. But it also records names no add
ever stored — a refused add, a connection that never opened. Deleting those printed a cleanup
failure for a credential that never existed, and the run exited on that instead of on the real
error.

The record-first contract stays. The trap now deletes only the recorded names that
credentials list still shows (new stored_creds).

3. The credential names were shared between runs

The sandboxes and the branch were already namespaced per run; the credentials were not
(smoke-gh, smoke-gh-mcp). Credential names are a shared namespace per user, so two runs as the
same user — a retry over a still-running one, two CI jobs — stored one credential and each deleted
the other's mid-check. Both names now carry $RUN and fit mocactl's 40-character
CREDENTIAL_NAME.

Tests

Six new assertions in deploy/sbx/tests/smoke.test.sh, each verified to fail without its fix
(smoke.sh reverted to HEAD, tests kept: 15 failures, covering all six plus the existing
name-dependent ones).

New fake knobs: MOCK_BRANCH_DELETE_CODE, MOCK_BRANCH_GONE, MOCK_REPO_GONE,
MOCK_CRED_ADD_LOST, MOCK_SBX_DELBRANCH_FAIL, and credentials list --json in the mock
mocactl.

MOCK_SBX_DELBRANCH_FAIL is worth calling out: drop_branch is only reachable when the
in-sandbox branch delete fails. On a clean step 7 the sandbox deletes its own branch and clears
BRANCH, so the trap has nothing to do — without that knob the branch tests set their codes and
never reached the code under test.

The existing fakes keyed on the literal name cred-smoke-gh; have(), the fake gh and the fake
git push now match the preset suffix, so they test the production logic rather than a hardcoded
name.

Verification

Check Result
bash deploy/sbx/tests/smoke.test.sh exit 0, 26 passing
Each new assertion fails without its fix confirmed (15 failures on reverted smoke.sh)
shellcheck -x -S warning (CI severity) on both files clean
make test-deploy-bash32 PASS
make test-deploy every sbx suite passes

make test-deploy does fail on deploy/microvm/tests/build-snapshot.test.sh. That is
pre-existing and unrelated — it exits 2 identically with these changes stashed, and it is two
SC2016 infos in deploy/microvm/build-snapshot.sh, which this PR does not touch.

Docs

deploy/sbx/RUNBOOK.md §step-7 and smoke.sh's own header both named the old fixed credentials;
updated to the per-run form.

Part of #653.

Assisted-By: Claude Code

deploy/sbx/smoke.sh's step-7 (egress credentials) cleanup reported success
for three things it had not actually done. Each is the one case the cleanup
exists to catch, so a leak was reported as cleaned up.

drop_branch accepted 404 and 422 as "the branch is gone". GitHub sends
"Reference does not exist" as *both* (cli/cli#11187), 422 is also its answer
for the default branch and for a spammed endpoint, and a PAT that can no
longer reach the repository gets 404 for every ref. Now anything but a 204
is confirmed: the ref reads as absent AND the repository still answers with
this PAT.

The trap deleted every recorded credential name. cred_add records a name
*before* the add -- deliberately, so an add whose answer was lost is still
cleaned up -- but that also records names no add ever stored. Deleting those
printed a cleanup failure for a credential that never existed and masked the
real error. The record-first contract stays; the trap now deletes only the
recorded names `credentials list` still shows (stored_creds).

The credential names were fixed (smoke-gh, smoke-gh-mcp) while the sandboxes
and the branch were already namespaced per run. Two runs as the same user --
a retry over a still-running one, two CI jobs -- shared one credential and
each deleted the other's mid-check. Both names now carry $RUN and fit
mocactl's 40-character CREDENTIAL_NAME.

Six new assertions in deploy/sbx/tests/smoke.test.sh, each verified to fail
without the corresponding fix. The fakes gained MOCK_BRANCH_DELETE_CODE,
MOCK_BRANCH_GONE, MOCK_REPO_GONE, MOCK_CRED_ADD_LOST and
MOCK_SBX_DELBRANCH_FAIL (drop_branch is only reachable when the in-sandbox
delete fails), and `credentials list --json`.

Part of rossoctl#653 (the last unchecked item under "Test gaps that hide real
checks").

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik requested a review from pdettori as a code owner October 30, 2026 15:13
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Oct 30, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch confirmation and per-run credential names look right. Names fit CREDENTIAL_NAME (at most 31 characters), the credentials --json shape matches cmdCredentials, the branch probe fails safe, and each fix has a test.

One must-fix: the new stored_creds fails open. If listing fails at cleanup, a stored PAT credential is left with no message and rc 0, which is the "leak reported as cleaned up" case this PR sets out to remove. One suggestion on the ref-probe endpoint.

Comment thread deploy/sbx/smoke.sh Outdated
sys.exit(0)
for c in doc.get("credentials") or []:
if c.get("name"):
print(c["name"])' 2>>"$LOGS/cleanup.log")" || listed=''

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must-fix: stored_creds fails open. If as a credentials --json fails at cleanup time (connection refused, session expired, a proxy's HTML error page), the pipeline's non-zero exit sets listed=''. The except ValueError: sys.exit(0) path does the same. Then no recorded name matches, left_cred never runs, and the trap prints nothing and keeps rc at 0. A credential that was stored (and holds the PAT) is left on the service, and the run gives no sign of it. That's the same class as the 404/422 bug this PR fixes. Before this PR, the trap at least tried the delete and printed could not delete credential ….

Proposed fix: tell "listed, and not there" apart from "could not list". When the listing fails or isn't JSON, fall back to deleting every name in $CRED_NAMES, as before. A spurious "could not delete" is the safe side here. Or print # cleanup: could not list credentials of user A; check <names> by hand and set rc=1. A mock knob such as MOCK_CRED_LIST_FAIL=1, combined with MOCK_CRED_ADD_LOST=1, would pin this case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ef39395.

stored_creds now tells "listed, and not there" from "could not list". The listing's failure (and the non-JSON path, now sys.exit(1) rather than 0) returns 1 after printing every recorded name, so the caller falls back to trying all of $CRED_NAMES, as before this PR. The trap also prints # cleanup: could not list user A's credentials; trying every recorded name (<names>).

One deliberate difference from your second option: the listing failure alone does not force rc=1. If the fallback's deletes all succeed, nothing leaked, so rc 0 is the honest answer; rc goes to 1 as soon as any of those deletes fails. The run still says it could not list either way. Happy to make the listing failure itself fatal if you'd rather have the harder signal.

Added the mock knob you suggested (MOCK_CRED_LIST_FAIL=1) and two assertions in deploy/sbx/tests/smoke.test.sh:

  • MOCK_CRED_ADD_LOST=1 MOCK_CRED_LIST_FAIL=1 — exactly your case: the credential is on the service, the listing is broken. Asserts exit 1, the could-not-list message, the credential is still deleted, and none is left live.
  • MOCK_FAIL=add MOCK_CRED_LIST_FAIL=1 — the safe side: with the listing broken the trap cannot rule out a name no add ever stored, so it tries the delete and reports the failure. Reached only when listing failed, so the "no spurious delete failure for a refused add" assertion still holds.

I verified this by reverting only the stored_creds change (keeping the ref-path change) and re-running: FAIL: a failed listing was not reported.

Comment thread deploy/sbx/smoke.sh Outdated
local code ref
code="$(pat_curl -X DELETE -o /dev/null -w '%{http_code}' "https://api.github.com/repos/$CRED_REPO/git/refs/heads/$BRANCH" 2>>"$LOGS/cleanup.log")" || code=''
if [[ "$code" != 204 ]]; then
ref="$(api_code "https://api.github.com/repos/$CRED_REPO/git/refs/heads/$BRANCH")"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: GET /repos/{o}/{r}/git/refs/heads/<branch> is the legacy plural form. When no ref matches exactly, it can return 200 with an array of refs that start with that name. Exact-match "get a reference" is the singular GET /repos/{o}/{r}/git/ref/heads/<branch>, which returns 404 when the ref is absent. With sbx-smoke-<ts> names a prefix collision is unlikely, and the result fails safe (a false "could not delete"). Still, the singular path makes the probe mean exactly "this ref". The mock's */git/refs/heads/* pattern would need the same change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken, in ef39395. The probe is now the singular exact-match get:

ref="$(api_code "https://api.github.com/repos/$CRED_REPO/git/ref/heads/$BRANCH")"

The DELETE stays on the documented plural git/refs/heads/<branch>. The mock's pattern moved to */git/ref/heads/* to match, and the DELETE branch there is reached before the case on the URL, so it is unaffected.

Agreed the prefix collision is unlikely with sbx-smoke-<ts> names and that it failed safe — but the probe is the thing that decides whether to trust a 404, so it should mean exactly that ref rather than "a ref starting with it".

Verified the direction: with the plural path restored the suite fails at FAIL: a 404 whose ref is confirmed absent was not accepted as deleted.

Review of rossoctl#669: stored_creds failed open. If `credentials --json` itself
failed at cleanup time (a refused connection, an expired session, a
proxy's error page), pipefail set listed='' and the except-ValueError
path did the same, so no recorded name matched, the trap printed
nothing and kept rc 0 -- a stored PAT credential left on the service
with no sign of it. The same leak-reported-as-cleaned-up class the PR
sets out to remove.

stored_creds now tells "listed, and not there" from "could not list":
the listing's failure (or its not being JSON) exits 1, and the caller
falls back to trying every recorded name, as before this PR. A spurious
"could not delete" is the safe side, and the run says it could not list.

The ref probe also moved to the singular git/ref/heads/<branch>, the
exact-match get; the plural git/refs form can answer 200 with refs that
merely start with the name. The DELETE stays on the documented plural
form. The mock curl's pattern follows.

Two new assertions in deploy/sbx/tests/smoke.test.sh
(MOCK_CRED_LIST_FAIL, combined with MOCK_CRED_ADD_LOST and with
MOCK_FAIL=add), each verified to fail without the fix.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik

cwiklik commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@ingpaolodettori-dev ready for another look — both items are addressed in ef39395, all 15 checks green.

Must-fix: stored_creds failed open. It now exits 1 after printing every recorded name, so a failed listing falls back to attempting a delete for each name rather than silently reporting nothing left behind. The caller says so on stderr and rc goes to 1 as soon as any of those deletes fails.

One deliberate difference from a strict reading of the request, so it's visible rather than buried: a failed listing does not by itself force rc=1. If the fallback's deletes all succeed, nothing leaked, and claiming otherwise would be its own false signal. rc becomes 1 the moment a delete fails. If you'd rather have the harder signal — any unlistable store is a failure regardless of what the fallback achieves — say so and I'll make it fatal.

Suggestion: ref-probe endpoint. The probe moved to the singular git/ref/heads/<branch>, which matches exactly and 404s when the branch is absent; the plural form prefix-matches and can 200 on a branch that doesn't exist. The DELETE stays on the plural path, which is where it belongs.

Tests. Added a MOCK_CRED_LIST_FAIL=1 knob and two assertions: a lost-credential run where the listing also fails (the fallback must still delete it), and a failed-add run where the listing fails (the failure must be reported). Both were proven to bite by reverting each fix in isolation and watching the specific assertion fail.

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up at ef39395: both 85aac6a items are fixed, and I verified them against the code.

  • stored_creds no longer fails open. With pipefail on in the trap, a failed or non-JSON credentials --json returns 1 after echoing every recorded name, so the trap reports that it could not list and still tries every delete. Any failure sets rc=1. Leaving rc at 0 when every fallback delete succeeds is fine with me. The two new MOCK_CRED_LIST_FAIL tests pin both sides.
  • The ref probe now uses the exact-match singular git/ref/heads/<branch>, and the mock was updated to match.

CI is green. Approving.

Assisted-By: Claude Code

@cwiklik
cwiklik merged commit d01a7f4 into rossoctl:main Nov 2, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants