Repository navigation
feat(mocactl): add workloads list (#552) - #675
Conversation
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Clean, well-scoped addition. The index writes sit inside the RESERVE and TRANSITION Lua scripts that already own the quota counter, so index membership and quota consumption change in one atomic step. deleting stays listed, deleted is dropped, and the create-rollback path (transition(reserved → deleted)) clears the entry too. The LIST script takes its cursor from the last member it considered, which matches the listByOwner fix in control-plane ownership.ts. The tests cover the cases that matter: the #601 generated id, a page of only ghost entries, cross-owner isolation, records without createdAt, and anonymous refusal. Nothing blocking. Inline: 1 suggestion (a clearer CLI message against a pre-#552 harness) and 3 nits (doc/comment accuracy, silent truncation).
Author: cwiklik (MEMBER — maintainer)
Areas reviewed: TypeScript (knative-server workloads route + Lua, mocactl CLI/API client), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (all 17 checks)
Assisted-By: Claude Code
| workloads = await client.list(opts.signal); | ||
| } catch (err) { | ||
| if (aborted(opts.signal)) return 130; | ||
| io.err(describeWorkloadError(err)); |
There was a problem hiding this comment.
suggestion: Against a harness from before #552, GET /workloads hits routeFor, which returns null for a GET with no id, and the route then answers 405 method_not_allowed. describeWorkloadError has no case for that code, so the user sees the harness refused: 405 method_not_allowed. That's confusing, because mocactl and the harness version separately. A method_not_allowed case here (or a check on err.status === 405 in cmdWorkloadList) could say something like "this harness has no workload listing (it predates #552), so upgrade it, or use mocactl workloads get ID". A test with a 405 fake would pin it down.
| * | ||
| * A member whose record is gone is skipped rather than returned as a hole. That window is real: the | ||
| * record key carries the deleted-record TTL while the index entry does not, so a process that dies | ||
| * between the SET and the ZREM leaves a member behind. The listing only reads -- it never writes -- |
There was a problem hiding this comment.
nit: The comment overstates how a ghost entry can appear. The SET … EX and the ZREM run in the same TRANSITION script, which Redis/Valkey executes atomically, so no process can die between them. The deleted record's TTL also starts at the same moment its entry is removed. A member can only outlive its record if something deletes the record key outside these scripts (a manual DEL, an operator cleanup, or the test's redis.del). Also, "until the owner's next delete of that name clears it" doesn't hold: once the record is gone, DELETE /workloads/{name} returns 404 without touching the index. Only a re-create (whose ZADD re-scores the member) followed by a delete removes it. Skipping the ghost is still the right behaviour; it's only the explanation that's off.
| rule the listing itself obeys. These workloads age out: each holds its quota unit until it is | ||
| deleted, and once deleted the quota is released and the name is reusable. To reach them, delete by | ||
| name, or let `MOCA_WORKLOAD_DELETED_TTL_SECONDS` expire the records of ones already deleted. An | ||
| operator with Redis access can list the pre-#552 records directly: |
There was a problem hiding this comment.
nit: redis-cli --scan --pattern 'sh:wl:*' lists every workload record, not only the pre-#552 ones the sentence promises. You can only tell those apart by the missing createdAt in the JSON value. Something like "lists all workload records; the pre-#552 ones are those whose value has no createdAt" would fix it. On a related point, the earlier GET /workloads paragraph's "anything counted against the quota can be found" is true only for records created after this change. Pointing it at this Upgrading note would keep the two consistent.
| if (typeof next !== 'number') return workloads; | ||
| cursor = next; | ||
| } | ||
| return workloads; |
There was a problem hiding this comment.
nit: When MAX_LIST_PAGES runs out, this returns a truncated listing with no signal, so the caller can't tell a short answer from a complete one. In practice this is unreachable (quota 5, page 50). But the JSDoc pitches the bound as the guard against a server whose cursor never advances, and in exactly that case a silent short answer is the worst outcome for a "what do I own?" command. A thrown invalid('workload list (cursor did not advance)'), or a check that next is strictly below the previous cursor, would surface it instead.
`mocactl workloads create` printed an id and there was no way to ask the harness for it again, so any crash between create and whatever persisted that id left an orphan holding a unit of the per-owner quota (5 by default) with no way to find or free it (rossoctl#601). Adds `GET /workloads` and `mocactl workloads list [--json]`, scoped to the authenticated caller and newest first. The route reads an owner-scored zset (`sh:wl-index:<ownerScope>`) rather than scanning the keyspace, which spec section 13 / rossoctl#361 forbids for caller-facing reads. `ZADD`/`ZREM` happen inside the same compare-and-set Lua scripts that own the quota counter, so index membership is exactly quota consumption -- which is what makes the listing able to surface the go through the already-declared `eval`. - `deleting` records are listed (they still hold their quota unit, and a stuck delete is precisely what needs showing); `deleted` ones are not. - The listing is read-only. It never settles a `provisioning` record, so N rows cost no provisioner calls and `sandboxes.ready` may lag one `get`. - Pages with `?limit` (default 50, cap 200) and `?cursor`. The cursor is the score of the last member *considered*, not of the last record found, so a page of expired records still advances the walk. - Another user's workload stays indistinguishable from a missing one (rossoctl#358). - The CLI follows the cursor to the end, bounded by MAX_LIST_PAGES. Existing records predate the index and will not appear; noted under Upgrading in docs/context-service.md. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
78ec37d to
1d1a149
Compare
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Re-checked after the rebase onto #674: the change is identical apart from the README/docs conflict resolutions, which correctly keep #674's (idempotent) / alreadyDeleted / workload_already_deleted text. LGTM.
One nit outside the diff: the rebase dropped a line from the commit message. The sentence now reads "…able to surface the go through the already-declared eval." The missing line is "#601 orphan instead of "most of your workloads". No store widening: both". Worth restoring if this is squash-merged with that message.
The four comments from the previous round (the 405 method_not_allowed message, the ghost-member comment at knative-server workloads.ts:233, the sh:wl:* docs scan, and the silent MAX_LIST_PAGES truncation) still apply at this head. All are non-blocking.
Closes #552.
Problem
mocactl workloads createprinted an id and there was no way to ask the harness for it again. Any crash betweencreateand whatever persisted that id left an orphan holding a unit of the per-owner quota (5 by default), with no way to find it or free it (#601).What this adds
GET /workloadsandmocactl workloads list [--json], scoped to the authenticated caller, newest first.Design
The route reads an owner-scored zset (
sh:wl-index:<ownerScope>) rather than scanning the keyspace, which spec §13 / #361 forbids for caller-facing reads — the same idiom ascontrol-plane/src/ownership.tslistByOwner.ZADD/ZREMhappen inside the same compare-and-set Lua scripts that already own the quota counter, so index membership is exactly quota consumption. That is what lets the listing surface the #601 orphan rather than "most of your workloads". NoWorkloadStorewidening and no newRedisResultStoremethods — both go through the already-declaredeval.Other decisions:
deletingrecords are listed — they still hold their quota unit, and a stuck delete is precisely what needs showing.deletedones are not (they hold nothing);GET /workloads/{name}still reports them for their TTL.provisioningrecord, so N rows cost no provisioner calls;sandboxes.readymay lag oneget.?limit(default 50, cap 200) and?cursor. The cursor is the score of the last member considered, not of the last record found, so a page of entirely expired records still advances the walk instead of stalling.MAX_LIST_PAGESso a server returning a fixed cursor cannot hang the command.Workload routes are
surface: 'internal'inroutes.ts, sodocs/api/harness-openapi.yamlis unchanged.Upgrading
Records created before this change predate the index and will not appear in the listing. Noted under Upgrading in
docs/context-service.md, with an administrative-only scan for finding them.Testing
workloads.test.ts+cli.test.ts100/100createdAt#foundfor#names(cursor stalls on a ghost page), a missingZREM(deleted workloads linger), and a shared index key (cross-owner leak)tsc --noEmitclean;prettier --checkclean;scripts/check-doc-links.py0 broken linksAssisted-By: Claude