Skip to content

fix(workloads): tell a repeated delete from a wrong id - #674

Merged
cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/workload-delete-idempotent-602
Nov 2, 2026
Merged

cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/workload-delete-idempotent-602

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

The bug

DELETE /workloads/{name} collapsed two different situations into one answer:

  • a record the caller owns that is already deleted
  • a name nothing is known about

Both returned 404 workload_not_found. A cleanup script re-running a delete, or a user retrying one, could not tell "already handled" from "wrong id" — so the only safe reading of the error was the alarming one.

The fix

Server — an owned, already-deleted record now answers 404 workload_already_deleted.

  • Still 404, because there is still no live workload to delete. Only the code narrows, so nothing that keys on the status changes.
  • Reveals nothing new. GET already reports that same record as deleted for a day (MOCA_WORKLOAD_DELETED_TTL_SECONDS). Once the record expires, the delete reverts to workload_not_found.
  • A record in deleting — an unfinished delete or create rollback — is untouched and still retries.
  • createUpload, activateWorkload and resolveWorkloadRun keep their own deleted → workload_not_found guards, deliberately: those are real refusals.

CLI — mocactl workloads delete treats that one code as success and exits 0, as kubectl delete does, so a script can delete every id it knows without sorting "already done" from a real failure.

  • workload_not_found still exits 1 — there, the id may simply be a typo.
  • stderr says which happened: workload X was already deleted vs deleted workload X.
  • --json prints the same document either way, plus "alreadyDeleted": true for a reader that wants to tell them apart. The key is absent (not false) when the delete did the work, so existing consumers see no change.

Other commands that name a deleted workload get a new describeWorkloadError case, so the message says which of the two 404s came back.

Scoped out, deliberately

The issue also asks for a third, distinct permission denied error for a workload belonging to another user. Not done, on purpose: that indistinguishability is a documented privacy property (docs/context-service.md, "Names are per user", #358) — another user's workload must look exactly like a missing one. Such a record is not read at all and stays workload_not_found. A server test pins that property so it cannot be eroded later.

Verification

Gate Result
New tests 10 (5 server, 5 CLI)
Red check (fix stashed) 2 CLI + 3 server fail
test/workloads.test.ts 33 CLI, 81 server pass
Full knative-server suite 632 passed
Full mocactl suite +5 pre-existing Ink waitFor timeouts, identical set on clean main
tsc --noEmit 0 errors, both packages
Prettier (pinned 3.9.10) clean
check-doc-links.py 212 files, 0 broken
Mutation testing 9 mutants, all caught, no survivors

Mutants: server reports the old conflated code · deleting treated as already-deleted (losing the retry) · guard falls through instead of returning · CLI back to exit 1 · any error becomes exit 0 · both messages identical · id validated after the request · JSON marker never added · marker always present.

One of these earned its keep: an early sanitizes the id in the message test let a no-sanitize mutant survive, because WORKLOAD_ID rejects escapes and checkId exits 2 before the message is ever built. It was replaced with the guarantee that actually holds — a crafted id is rejected before any request is made.

New tests cover: distinguishability from a name that never existed · another user's deleted workload says nothing · reversion to workload_not_found once the deleted record expires · a deleting record still retries · a repeat makes zero Context Service calls and releases quota once · both --json shapes · workload_not_found still exits 1 and prints no JSON · crafted id rejected pre-request.

Docs

  • docs/context-service.md — route table gains the new code; a paragraph on what it does and does not reveal, and why another user's record is excluded.
  • docs/reference/cli-mocactl.md — exit-code behaviour and the --json shape.
  • packages/mocactl/README.md — (idempotent), prose, --json row.

Closes #602.

Assisted-By: Claude

`DELETE /workloads/{name}` collapsed two different situations into one
answer: a record the caller owns that is already `deleted`, and a name
nothing is known about, both returned `404 workload_not_found`. A cleanup
script re-running a delete, or a user retrying one, could not tell "already
handled" from "wrong id", so the only safe reading of the error was the
alarming one.

The server now answers an owned, already-deleted record with
`404 workload_already_deleted`. It stays 404 because there is still no live
workload to delete, so nothing that keys on the status changes; only the
code narrows. It reveals nothing new either: `GET` already reports that
record as `deleted` for a day (MOCA_WORKLOAD_DELETED_TTL_SECONDS), and once
that record expires the delete reverts to `workload_not_found`. A record in
`deleting` -- an unfinished delete or create rollback -- is untouched and
still retries.

`mocactl workloads delete` treats that one code as success and exits 0, as
`kubectl delete` does, so a script can delete every id it knows without
sorting "already done" from a real failure. `workload_not_found` still
exits 1, because there the id may simply be a typo. `--json` prints the
same document either way, plus `"alreadyDeleted": true` for a reader that
wants to tell them apart.

The issue also asks for a third, distinct `permission denied` error for a
workload belonging to another user. Not done, deliberately: that
indistinguishability is a documented privacy property (docs/context-service.md,
"Names are per user", rossoctl#358) -- another user's workload must look
exactly like a missing one. Such a record is not read at all and stays
`workload_not_found`; a server test pins that.

Tests: 10 new (5 server, 5 CLI) covering distinguishability, the deleted-record
TTL boundary, another user's record, the `deleting` retry, that a repeat calls
the Context Service zero times and releases quota once, both `--json` shapes,
and that a crafted id is rejected before any request. Verified with 9 mutants,
all caught.

Closes rossoctl#602.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik requested a review from pdettori as a code owner November 2, 2026 19:44
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 2, 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.

Summary

DELETE /workloads/{name} on the caller's own already-deleted record now answers 404 workload_already_deleted instead of 404 workload_not_found. deleting records still retry, and another user's record is never read, so the per-user privacy property ("Names are per user", #358) holds; a test pins it. mocactl workloads delete treats the new code as success (exit 0, alreadyDeleted: true in --json), while workload_not_found still exits 1. The server change is a minimal split of the existing guard: no Context Service call and no second quota release on the repeat path (tested). Docs are updated in context-service.md, cli-mocactl.md and the mocactl README. routes.ts needs no change because workload routes are internal and outside harness-openapi.yaml.

Areas reviewed: TypeScript (knative-server, mocactl), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, signed-off: yes; conventional title
CI status: all 17 checks passing (check, lint, k8s-kind-e2e, deploy-scripts, trivy, codeql, DCO, …)
Base: dd918ac; main is at 32c117e (#673 headless, no overlap with the changed files)
Prompt injection: none found in PR content

One suggestion inline; nothing blocking.

return "the harness's workload settings are incomplete — ask its operator";
case 'workload_not_found':
return id ? `no workload '${sanitizeRemote(id)}' (or it is not yours)` : 'no such workload';
// `delete` treats this as success and never reaches here (#602); any other request that

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: This comment says delete "never reaches here" and that "any other request that names a deleted workload" can get this code. Neither is accurate. The server sends workload_already_deleted only from DELETE (uploads, activate and runs still answer workload_not_found for a deleted record). And one delete caller does reach this case: cmdWorkloadCreate's rollback in fail() (line ~192). If a concurrent mocactl workloads delete finished first, that rollback would print could not delete workload X (workload 'X' was already deleted); run \mocactl workloads delete X`, which asks the user to redo something that is already done. Consider treating workload_already_deletedas success there as well (printdeleted workload X` / "was already deleted"), and rewording this comment to say the code comes only from DELETE.

@cwiklik
cwiklik merged commit b43859c into rossoctl:main Nov 2, 2026
17 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.

mocactl workloads delete: deleted-status workload returns "no workload or it is not yours" instead of a status-specific error

2 participants