Repository navigation
fix(aws-control): gate Layer B in the sessions archive on the operator's permission - #12178
Conversation
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
CI triage for 446f89cAll four AI review lanes hit the same external cause in one window (04:26-04:34 UTC): the GitHub App installation's API rate limit. Each lane's log carries What that produced, per lane:
The remaining two reds are one root cause, and it is not a review finding:
PR Hygiene was the one red that was genuinely mine: the |
446f89c to
93d42b8
Compare
|
Disposition of the GPT 5.6 blocking finding on The finding was right. The permission read Fixed at the placement rather than by hardening the read, which is what the finding's own Fix line asks for. Head
Pinned so the regression cannot return quietly: one test offers all three Also in this head: a rebase onto current |
Design Review (Fable 5) — 🟡 CONCERNSDesign-level review of Design-Verdict: CONCERNS Existing installs' backups silently degrade: Layer B drops with no console control until a follow-up PR, while retention ages full-fidelity archives out of the bucket. Watch
Suggestions
[DESIGN-REVIEWED] cb039fc |
Opus 4.8 Review — ✅ no blocking findingsReviewed Review detailsBased on my analysis: Candidate 1 — verified real. On the skip path ( Candidate 2 — dropped. The refusal when No new groundable findings surfaced in Step 2. One advisory finding: the unchanged-tree skip path silently drops the archive's FINDING — src/kiro_crew/apps/builtins/aws_control/backend/backup.py:3869 — [OPUS-REVIEWED] 756fb50 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
First Principles Review (Fable 5) — ✅ human override accepted@chenmingwei23 overrode this lane for |
1f71ff5 to
15a811f
Compare
|
Disposition of the three lanes that failed on GPT 5.6 F1 (security-fenced, adjudicator FLAGGED as likely edge case): accepted and fixed, not overridden. The finding named a real window -- enabled at the read, revoked while the tar is written, bytes upload under a permission that no longer exists. The adjudicator was right that the remedy it proposed, rebuild-without-Layer-B, would reintroduce the torn state the read-once rule exists to prevent. Refusing does not: First Principles item 3 (zero consumers): taken as the subtraction the reviewer named. PR Hygiene (commit count): squashed. Three commits against a ceiling of two. Now one. First Principles blocker (the default flip) is not the author's to clear, and is left for a maintainer. The reviewer is right that the shape is proposed rather than decided: Evidence on this head: 498 tests pass across the four aws-control files; black, flake8, isort, mypy, comment-history, brand and docs-lint all exit 0; zero non-ASCII added lines; one commit. |
15a811f to
4220bee
Compare
buluoray
left a comment
There was a problem hiding this comment.
Requesting changes on the lock scope.
_state_lock() is not only the cross-process file lock — its body is with _run_lock, open_lock_file(...) (src/kiro_crew/backup/backup.py:299), so it also takes the process-wide _run_lock. This PR newly wraps the put_file upload inside that block; on main that call site holds no lock at all.
last_runs() (backup.py:3225) takes the same _run_lock, and the backup status polling in src/kiro_crew/dashboard/routes.py:2304 goes through it. So while any one account is uploading, the whole backup status surface blocks — across accounts — for as long as the upload takes, which for a large snapshot can exceed an hour. The module already warns against exactly this at backup.py:1452.
The consent re-read this PR adds is correct and worth keeping; it's the duration that's the problem. Please narrow the critical section so the network PUT happens outside _state_lock() (re-read the grant under the lock, release, then upload — or hold only the file lock, not _run_lock, for the upload window).
Nothing else blocking: the per-kind grant / redaction gate ordering checks out, and CI is green on the current head.
|
Accepting the lock-scope finding. It is correct, and the module had already made this Verified against the current head before replying:
One correction to the citations, offered so the finding is not dismissed on it: the paths The decisive argument is this module's own: the comment above Taking the second of the two suggested remedies, not the first. Re-reading the grant under One detail is load-bearing and is being settled before the change lands rather than The fix will arrive as a single push carrying the narrowed scope and its tests, rather than |
|
Thanks -- this was a real regression and the narrower of your two suggested What changedThe upload window now holds ONLY the state file's sidecar file lock, not Measured across the three heads, by walking the AST for every
Why dropping
|
|
/ai-review override first-principles 7703ba0: Default accepted as the product decision: the sessions archive withholds Layer B unless an operator opts in. rfc-s3-backup.md D4 already records session fidelity as a tier, so its draft status is a documentation lag, not an unmade decision. The opt-in path is reachable and the description states the changed default. Accepting the recorded-RFC gap. |
Human judgment recorded@chenmingwei23 marked the first-principles AI finding as false positive, not applicable, or explicitly accepted for
This decision applies only to this commit. A new push requires a new judgment. |
|
Branch frozen: the first-principles override recorded above is pinned to 7703ba0. Any further push voids it and the block returns. Do not push this branch. |
bolichen97
left a comment
There was a problem hiding this comment.
Approving at 7703ba07cf.
The lock-scope regression @buluoray requested changes on is fixed, and I verified it rather than taking the reply. Reading the diff at this head: _upload_lock() takes open_lock_file + file_lock and deliberately omits _run_lock; the withdrawal recheck and storage.put_file are both inside it; _state_lock() is unchanged and still takes both for the setter path. That is the narrower of the two remedies offered, and it is the correct one — re-reading the grant under the lock and releasing before the PUT would have reopened the revocation race an earlier round blocked this branch on. The ordering the gate owns is against the setter, and the setter goes through the same sidecar file lock exclusively, so dropping _run_lock costs the guarantee nothing while freeing the status read. Same shape _delete_under_the_retention_gate already composes in this file, for the same stated reason.
The permission placement holds. The store is backup.json under the app's data dir, which is in security._CREW_SECRET_LEAVES, so an agent shell cannot write the answer to its own gate — the point the description makes and the reason config.json was rejected. Sole writer is the owner-gated route through _guarded. Read once, before the archive opens, defaulting False on absent/non-boolean/unparseable.
The record describes the archive, not the permission. layer_b=layer_b_files > 0 is taken from the count actually added, so a granted run on a CLI-idle install records false rather than claiming a fidelity the object does not hold. That is the right direction and it is what a restore reads.
Two things I am flagging rather than blocking on:
- No console control ships here. An existing operator's sessions archive drops Layer B and the owner-gated endpoint is the only way to opt back in. The default moves in the safe direction and the run record states it, so this is recoverable — but the follow-up toggle should not sit long.
- Merge order. This, #12381 and #12190 all rewrite
backup.py, and both this branch and #12381 carry SHA-pinned first-principles overrides that a rebase voids. I am landing the two frozen branches first for that reason.
@buluoray your CHANGES_REQUESTED is still recorded against cf0e7cc7ca; please dismiss it if you agree the narrower remedy answers the finding.
|
Approved above at
Two things worth saying plainly before the push:
Happy to do the rebase and run the aws-control suites if you would rather not touch it — say so and I will, but I will not re-issue the override. |
|
/ai-review override gpt 078f740: Verified on this head that the only occurrence of record[layer_b] in the aws-control backend is the write at backup.py:1392, so no reader exists; the skip record still carries the baseline key, bytes, fingerprint, version and tree (backup.py:2742-2751) so restore fidelity is untouched; and the next real upload restates it at backup.py:3288. Recoverable, inspection-only gap. |
|
/ai-review override first-principles 078f740: Owner decision to ship the sessions-archive Layer B consent gate now and leave rfc-s3-backup.md draft. Item 1 is the RFC state rather than a defect; items 6 to 8 are scope objections to the mid-upload revocation gate that the human review required, and that gate takes only the sidecar state lock, never the run lock, so status reads are not serialized. |
Human judgment recorded@chenmingwei23 marked the gpt AI finding as false positive, not applicable, or explicitly accepted for
This decision applies only to this commit. A new push requires a new judgment. |
Human judgment recorded@chenmingwei23 marked the first-principles AI finding as false positive, not applicable, or explicitly accepted for
This decision applies only to this commit. A new push requires a new judgment. |
|
Re-verified at What checks out:
What does not hold — the
On
The smallest fix I can see inside lines this PR already touches is to take the file lock before |
|
Fixed at The finding reproduces
Where your remedy's premise does not holdYour fix paragraph says "No other site takes So swapping the order inside For the record, the file has 24 textual mentions of What the change does insteadOne acquisition order for the module, written in a comment above Nothing may hold Removing the outer The false claim, everywhere it was statedYou flagged the Tests
Mutation matrix, each mutation applied alone:
Each reds only its own test, with its own assertion text, so the two are independent and each half of the fix is load-bearing rather than one change covering for the other. Scoped runs, One stale rationale your change surfaces indirectly: The head moved, so the two SHA-pinned overrides recorded on |
|
Head The three cascading gatesThe Comment History Gate was the root cause, and it was entirely mine: my new comments narrated what the code changed instead of stating what it does, so the gate flagged 7 markers ("used to", "no longer") on 6 added lines. All 6 rewritten to present tense. The counterfactual is now stated as a conditional -- "a Await Fast Gate and Coverage Gate were downstream of it, not separate problems. Comment History is a member of the Fast Gate run, Await Fast Gate reported GPT 5.6: real, self-introduced, fixedThe finding is correct and I introduced it. Before this change the outer I had reasoned past this on the strength of the comment at that line ("this new locked write supersedes prior state, regardless of its clock or process"). That comment was true only because the outer hold made the two operations one critical section, which my change ended. The comment is rewritten.
First Principles items 6 and 7: the premise does not holdItem 6 says holding the sidecar lock across the PUT "changes nothing the operator can observe -- a revocation landing on either side of the recheck still finds the bytes shipped". There is a third position, and it is the one the lock exists for: a revocation landing AFTER the recheck and BEFORE Without the lock,
With the exclusive sidecar hold spanning the recheck and the PUT, step 2 cannot land between 1 and 3. The setter reaches the same lock file through This is not hypothetical. So the proposed subtraction -- delete The lock-order regime item 7 objects to is not an optional addition either. Once the hold exists, Item 1 is unchanged and is not mine to clearItem 1 -- the default flip without a non-draft RFC -- is the same premise blocker this PR has carried, and its two named clearances are an RFC merged non-draft on the base or a maintainer override. Neither is reachable from here. The keystone placement is not in question; only the default is. Nothing in this head changes that item either way. Verification on this headScoped runs, |
…permission
run_sessions_backup tarred the kiro-cli session directory wholesale, so Layer B
-- the byte-exact, unredacted model context window -- rode every sessions
backup. It rode because the raw files were in the directory being tarred, not
because any permission was consulted, while the file-export path required an
operator opt-in for the same payload.
The permission is stored per account as sessionsIncludeLayerB in the app's
backup.json, inside the apps/aws-control/data directory already registered in
security._CREW_SECRET_LEAVES -- the read+write keystone floor, beside the nightly
bit that authorizes unattended paid uploads. Deliberately NOT a config.json key:
config.json is writable by any auto-approved agent shell, so a permission
honoured from there is one a prompt-injected agent can grant itself, and an
unredacted archive already in a bucket cannot be recalled. An authorization whose
subject can write it is not an authorization.
The sole writer is the owner-gated POST /backup/{account}/layer-b, which opens
the state file directly rather than through the agent file gate. The read
defaults to withhold and fails closed on anything unparseable. It is taken once,
before the archive opens, so the tar and its run record cannot disagree; a
revocation landing in that window refuses the upload rather than rebuilding,
because nothing uploaded and nothing recorded leaves no record to disagree with.
sessions_layer_b_enabled takes no lock of its own, so rechecking alone left a
check-to-use window in which a revocation committing between the recheck and
put_file still shipped the bytes. The recheck, the live authorization and the PUT
therefore run inside one hold of the state file's sidecar lock, taken before
_authorize_upload, which makes a revocation land wholly before the upload or
wholly after it.
That hold takes ONLY the sidecar file lock, deliberately NOT _run_lock -- the
same shape _delete_under_the_retention_gate composes for the retention sweep, and
for the same reason. An earlier revision held _run_lock as well (via
_state_lock). But _run_lock also serializes last_runs, and the dashboard's
backup-status read goes through it, so holding it across a PUT allowed
_PUSH_TIMEOUT_SECS stalled every account's status surface for one account's
upload -- for a large snapshot, over an hour. The revocation guarantee does not
need _run_lock: the setter (set_sessions_layer_b -> _locked_state_update ->
_state_lock) takes this same sidecar file lock exclusively, so an exclusive hold
across the upload already orders a revocation wholly before or wholly after it,
across processes as well as threads.
Dropping _run_lock from the upload gate was necessary and NOT sufficient, because
the stall arrived through the contending WRITER rather than through the upload.
_state_lock took _run_lock BEFORE parking on the sidecar file lock, so a writer
meeting an in-flight upload held _run_lock for that upload's whole duration and
last_runs queued behind the writer, bounded only by _STATE_LOCK_TIMEOUT_SECS.
Both of the feature's own paths reach that: a mid-upload revocation, which must
contend on the file lock for the ordering above to mean anything, and any second
account's _record_run finishing. So this states ONE acquisition order for the
module and makes every path obey it:
_RETENTION_GATE -> state sidecar FILE lock -> _run_lock -> leaf locks
Nothing may hold _run_lock while waiting for the file lock. _state_lock takes the
file lock first, and _record_run and _record_skip no longer wrap it in _run_lock
at all. Swapping the order inside _state_lock alone would NOT have been enough
and would have been worse: _record_run and _record_skip both reach _state_lock
through _record_run_locked -> _locked_state_update, so one thread would hold
_run_lock waiting for the file lock while another held the file lock waiting for
_run_lock -- a cycle, not a stall.
Removing their outer hold is not a pure deletion either. It was the only thing
serializing the _run_sequence bump, and (process, sequence) is the identity the
compare-and-set inside mutate reads and the pair _run_is_newer orders by, so a
shared sequence would let a stale baseline pass a check it must fail.
_record_run_locked therefore takes _run_lock for that bump alone, which cannot
park, and keeps it outside mutate so the state-read and state-write failure paths
still hand a uniquely numbered record to _remember_unpersisted. The order is
written in a comment above _state_lock and _run_lock's own definition points at
it. The remaining cost is unchanged: the setter and the nightly loop wait for the
upload, bounded by _PUSH_TIMEOUT_SECS, and no status read waits.
Decoupling the bump from the write has a second consequence, and mutate now
guards it. Since _run_lock is released before the file lock is taken, two
same-kind runs in this process can reach the file lock in an order that differs
from their sequence order, and the unconditional `runs[kind] = record` would then
let the lower-sequenced writer persist last -- leaving the slot holding the older
key, tree and layer_b, so the status surface and the next baseline comparison
both read a superseded run. A manual run overlapping a nightly wake for one
account reaches it, because the nightly loop calls work directly and so does not
take the Job SDK's (kind, account) dedupe. The write is therefore skipped when
this process has already persisted a higher-sequenced run for the same kind.
That guard is the same-process half of _run_is_newer and deliberately not a call
to it: its other half compares wall time, and applying that here would let a peer
install with a lagging clock refuse a locked write that really is newer. A
foreign record still loses to this write exactly as an unguarded assignment would
have it, so clock and process comparisons stay confined to best-effort recovery.
The uploads and versions merge stays unconditional, because the object is in the
bucket whichever run persists and uploaded_versions is what retention reads
before it erases object versions.
The run record carries layer_b, taken from the number of kiro-cli files actually
added rather than from the permission: a granted run whose kiro-cli directory is
absent or empty adds none, the crew half alone carries the run past the
empty-archive guard, and a record is written once with nothing afterwards to
correct it.
Tests pin both directions, that no config.json key can grant it, that a grant
does not cross accounts, that the store stays inside the fenced directory, that a
revocation refuses while a grant does not, and that a granted run with no
kiro-cli files records layer_b false. The lock tests are mutation-verified in
BOTH directions, probed without blocking from another thread: the upload holds
the setter's sidecar file lock across the recheck and PUT, so reintroducing no
lock reddens the revocation-guarantee tests. The status-read test drives a real
second-account _record_run while the upload holds the file lock and times
last_runs from a third thread, reaching the park point deterministically by
spying on file_lock rather than sleeping -- restoring the old order plus the
outer _run_lock reddens it, which the earlier single-threaded version could not
observe and which is why it passed vacuously. A separate test holds _run_lock and
asserts _run_sequence cannot move while another thread holds it, so leaving the
bump outside that hold reddens too. A third produces the write-order inversion
rather than simulating it -- the low-sequence writer parks in file_lock until the
high-sequence writer has committed -- and asserts the newer run survives while
both uploads still reach the versions map, so removing the newer-sibling guard
reddens it. Each mutation reddens the test it is aimed at. The two that reintroduce the
coupling also deadlock the inversion test's harness, which parks its
low-sequence writer and waits for the high-sequence writer to commit: that
writer cannot proceed once a parked writer holds _run_lock again, so the second
red is a further symptom of the same cause rather than a separate defect. The
bump and guard mutations redden one test each. Together they
show the fix is conditional: the reader is freed, the writer is not.
That hold spans a PUT allowed an hour, so the sidecar lock is taken with a
ceiling derived from the work it covers rather than file_lock's 300s default,
which is sized for a sub-second read plus a rename and must be overridden by any
caller that can hold the lock longer. A short ceiling would refuse a contender
that is only waiting, and that refusal arrives as the OSError _record_run absorbs
by keeping a completed upload's record in memory alone, so a short-lived process
that exits before the next successful write loses it and leaves the nightly loop
due and re-uploading. frontend._STAGING_LOCK_TIMEOUT derives its ceiling the same
way for a lock spanning a frontend build. A test asserts the value reaches
file_lock, so neither dropping the argument nor sizing it below the push bound
survives. A second test drives the consequence rather than the value: with the
module default patched down, a holder keeps the sidecar lock past it and a
contender must still acquire once the holder releases. Without the explicit
ceiling that contender is refused with "a holder is stuck" while the holder is
only working, which is the failure being fixed.
Closes #11559
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsFINDING -- test/test_aws_control_backup.py:1256 -- False positive or not applicable? A repository writer can comment: |
|
/ai-review override first-principles 756fb50: Items 7-9 ask to delete the lock-ordering fix that buluoray's CHANGES_REQUESTED asks to keep, and the lane's own clearance test is unsatisfiable: no revocation interleaving separates the two lock orders -- one landing after the recheck is admitted under both, one during the in-lock STS call is rejected by the unlocked recheck and admitted by the locked one. Item 1 is the draft RFC, tracked outside this PR. Overriding the lane only; the human review stands. |
Human judgment recorded@chenmingwei23 marked the first-principles AI finding as false positive, not applicable, or explicitly accepted for
This decision applies only to this commit. A new push requires a new judgment. |
|
@buluoray your CHANGES_REQUESTED is recorded against Your finding was right, and this head is what it produced. The vacuous test is covered by name. One correction to the minimal fix you sketched: Head |
buluoray
left a comment
There was a problem hiding this comment.
Source-verified at 756fb50ba56c16be693ec0e6151aef74b8bd187b (read through the refs/pull/12178/merge tree). The blocker I raised at cf0e7cc7 is closed. Dismissing my earlier request for changes and approving.
What I asked for was that the network PUT stop happening under _run_lock, while the consent read stays inside a lock that orders against the setter. Both hold at this head:
_state_lock()still takes_run_lock, and now takes it AFTER the file lock (src/kiro_crew/apps/builtins/aws_control/backend/backup.py:355-362). That reversal matters on its own: the old order parked a contending writer on the file lock while holding_run_lock, which is what queuedlast_runs()behind an upload even though the upload gate held no_run_lockitself.- The Layer B upload gate takes the new
_upload_lock()(:3938), whose body isopen_lock_file+file_lockand nothing else (:411-415) — no_run_lockin the function at all; the other_run_lockmentions in that range are docstring prose. Sostorage.put_file(:3960) runs without_run_lock, and the_run_lock-only status read (last_runs) is no longer behind it. - No TOCTOU traded in:
_authorize_upload(:3942) and the withdrawal refusalif layer_b and not sessions_layer_b_enabled(account)(:3952) are both inside the same_upload_lock()block as the PUT, with the authorize call moved ahead of the recheck so the whole decide-then-upload span is one critical section. - Both locks use
_state_path().with_suffix(".lock")(:354,:411), the same sidecar, so exclusion against the setter (set_sessions_layer_b→_locked_state_update→_state_lock) is real rather than two locks over one document. - Relative to
mainthis diff ADDS a lock where there was none: onmainthe sessions Layer B upload ran_authorize_uploadthenput_filewith no state lock at all. The-lines are the acquisition-order refactor, whose body relocates intact.
The effect is pinned, not just the call: test_a_status_read_is_not_blocked_by_an_in_flight_upload (test/test_aws_control_backup.py:782) runs a real last_runs() from inside put_file; test_the_upload_holds_the_setter_lock_across_the_permission_read (:711) and test_a_status_read_is_not_blocked_by_a_writer_parked_behind_an_upload (:829) cover the other two halves. test_aws_control_backup.py + test_aws_control_nightly_sessions.py are 274 passed at this head.
The lock-order comment block above _state_lock enumerating every site that takes both locks is the right shape for this — it puts the one acquisition order where a future holder will look.
Not verified: I did not mutation-test the lock guards; the three concurrency tests reproduce the old-order hang directly and pass here. The Design lane's 🟡 is pinned to an older sha and is advisory either way.
bolichen97
left a comment
There was a problem hiding this comment.
Reviewed at 756fb50 with Windows compatibility as the primary lens. Both new lock helpers (_state_lock, _upload_lock) reuse platform_compat.open_lock_file + file_lock, which are Windows-correct (byte-0 msvcrt.locking, no truncation, no pre-primed lock byte); the route handler validates bool rather than coercing. run_sessions_backup refuses on Windows via _CAN_PIN_TRAVERSAL (POSIX-only openat), so the Layer B upload path itself never executes there - the gate, withdrawal recheck and lock plumbing that do run on Windows are the setter route and run records. buluoray's lock-scope finding is resolved: the upload holds the sidecar file lock only, deliberately without _run_lock, matching _delete_under_the_retention_gate; buluoray has since approved at this head. The grant lives in backup.json under the fenced state leaf, not agent-writable config.json. Local run on native Windows (worktree src): test_aws_control_backup/routes/app.py 520 passed, 30 skipped, 0 failed; the 19 Layer-B skips (incl. the four lock-scope ordering tests) are gated on _CAN_PIN_TRAVERSAL and are covered by the Linux/macOS shards - noting for the record that Windows CI cannot catch a regression in that fix.
Problem / Motivation
run_sessions_backuptarred the kiro-cli session directory wholesale, so Layer B -- the byte-exact, unredacted model context window -- rode every sessions backup. It rode because the raw files were in the directory being tarred, not because any permission was consulted.The file-export path requires an operator opt-in before the same payload leaves in a bundle. Of its conditions, the sessions archive already met one and met neither of the others:
The operator-identity half was already covered: both routes that can start a sessions backup are owner-gated (
apps/job_routes.pyrequiresis_owner_dashboard_request, and the app's own start route requires the drive). The per-invocation half has nowhere to live here --JobSDK.start_async(kind, dedupe_key)carries no parameters, and the nightly path has no request at all. The standing permission was the missing condition.Why it matters
Layer B is strictly more sensitive than the transcript it accompanies. The transcript is what was displayed, with display-time redaction applied; Layer B is what the model actually held, unredacted. It also cannot be redacted on the way out -- the thinking blocks inside it carry a provider signature over their own content, so rewriting one invalidates the conversation. That leaves exactly two options, byte-exact or absent, which is why a permission and not a filter is the right instrument.
Before this change an operator pressing the backup button could not tell, from any surface, that the archive carried unredacted model context. Nothing recorded it either, so a restore could not tell what it had.
What changed
Where the permission lives, and why not
config.json.config.jsonis writable by any auto-approved agent shell, so a permission honoured from there is one a prompt-injected agent can grant itself: edit the key, wait for the owner to run an ordinary sessions backup, and the unredacted context uploads with no consent. An object already in a bucket cannot be recalled, so there is no recovery path. An authorization whose subject can write it is not an authorization, and this repo already states the rule --CredentialPolicy.exempt_exact_hostssays such a value is "NEVER sourced fromconfig.json".So the answer lives in the app's own state document,
backup.json, inside theapps/aws-control/datadirectory that is already registered insecurity._CREW_SECRET_LEAVES. That is the read+write keystone floor: the agent's file tools refuse every path in there and the shell forms are refused too. It is the same placement, and the same reasoning, as thenightlybit stored beside it, which authorizes unattended paid uploads -- and ascomputer_use.json,aws_service_consent.jsonandfile_delivery_consent.jsonelsewhere in the tree.The sole writer is the new owner-gated
POST /api/apps/aws-control/backup/{account}/layer-b. Every route in this app goes through_guarded, which requiresis_owner_dashboard_request; there is no agent path to it. The handler opens the state file directly rather than through the agent file gate, which is what lets the operator's toggle work while the agent's writes stay refused.Per account, like
nightlyand unlike the export key, because the risk this permission prices is the destination: the archive lands in one account's bucket, so granting it for that bucket must not grant it for another the operator adds later.The read.
backup.sessions_layer_b_enabled(account)defaults toFalse. A non-boolean stored value reads asFalse(bool("false")isTruein Python, so the value is validated, never coerced), and an unreadable or unparseable state document reads asFalsetoo -- the same fail-closed posturenightly_enableddocuments for itself. The permission is read once, before the archive is opened: reading it twice would let a write landing mid-build produce a tar that carries Layer B alongside a record saying it does not, and the record is what a restore trusts.The record. The run record carries
layer_b, so which layers an archive holds is stated rather than inferred from an absent key, mirroring the export path'slayer_b_skipped. The value is taken from the number of kiro-cli files actually added, never from the permission: a granted run whose kiro-cli directory is absent or empty -- an ordinary state on a fresh or CLI-idle install -- adds none, the crew half alone carries the run past the empty-archive guard, and a record is written once with nothing afterwards to correct it, so reading the permission there would state a fidelity the object does not hold. The field is omitted for the snapshot kind, where the question does not arise. It is a record for a reader, not an input:restore_downloaddoes not consult it, and earlier revisions of this description and of the code comments claimed it did. Two archives with the same stamped name are otherwise identical, so without the field nobody can tell which one can resume a session at full fidelity.A withdrawal landing mid-build refuses the upload. Reading the permission once leaves a window: enabled at the read, withdrawn while the tar is written, and the bytes would upload under a permission the operator has withdrawn. The recheck raises instead, and nothing is uploaded and no run record is written -- so the read-once invariant is untouched, because there is no record left to disagree with anything. Rebuilding without Layer B would instead be the torn state that invariant exists to prevent. Only the withdrawn direction refuses; a grant arriving mid-build leaves an archive without Layer B, which the next run picks up.
The recheck is the last statement before
storage.put_file, after_authorize_upload. That call goes to the network, so a check placed in front of it leaves the whole round trip inside the window it exists to close. A test pins the ORDER rather than the behaviour, because both orders pass every other case. The refusal goes through_refuse_upload, the module's audited refusal helper, so a withdrawn permission lands in the SEL beside every other refused upload rather than being the one refusal path that leaves no record.Both directions of the decision are audited. The allow/withhold decision emits
aws_control.backup_layer_b_decisionat the point it is made, carrying the account and which way it went. Only refusals reached the SEL before, which left the ALLOW direction -- the one that ships the bytes -- with no event at all.calleris threaded through so an unattended nightly run is not recorded against the dashboard owner, and the audit is best-effort: a failed audit must never be what stops a backup.No console control in this PR, stated rather than implied. There is no
DrivePagerow for this permission, so an existing operator's sessions archive drops Layer B with no in-product way to see that or opt back in -- the owner-gated endpoint is the only way to grant it today. That is a deliberate scope line: a toggle row is four new i18n keys across thirteen catalogs plus the generated pseudolocale, the translator-context sidecar and the dead-key baseline, and translations this author cannot verify do not belong on the same head as a security placement fix. A follow-up PR adds the toggle and state indicator beside the nightly row onDrivePage, and the run record already states which way each archive went in the meantime. The status response deliberately does NOT carrysessionsIncludeLayerByet: a field with no consumer is not the smallest honest version, so it ships with the row that reads it.The request field is validated, never coerced, for the same reason the nightly toggle is -- and the cost of getting it wrong is higher here, because the wrong direction is unrecallable rather than merely billable.
Why the default is withhold, and the counter-argument. There is a real case for the opposite default, and a reviewer should see it.
security_posture.pystates a doctrine for exactly this payload on the tunnel path: "What bounds the exposure is the destination rather than the payload", which is why a tunnel send carries Layer B byte-exact with no opt-in.rfc-s3-backup.mdlikewise asks for a private, ownership-enforced, encrypted bucket of transcripts rather than a payload gate, andstorage.pyprovides that. Under that reading the previous behaviour was consistent and this permission should default on.The default is withhold anyway, for one reason: the destination is not provably a single operator's. This app supports several installs writing one drive and says so --
ORIGIN_UNVERIFIEDexists because "anyone who can write to the bucket can write to a name" -- so a co-writer can reach an archive here. Being wrong in the withhold direction costs a restore its full-fidelity resume until the operator flips one toggle, and the record states that it happened. Being wrong in the carry direction puts unredacted context somewhere it cannot be recalled from. Only one of those is recoverable.A maintainer who reads the destination as bounded can flip the default in one place.
The default is a recorded decision, D4, and this change implements it.
docs/request-for-change/rfc-s3-backup.md:155states it: "D4 -- session fidelity is a tier. Defaultsessions= crew transcripts + uploads + artifacts, which buys readable history. Opt-in (O2) adds<kiro home>/sessions/cli/<sid>.*for reachable sids only ... which buys real resume instead of a lossy prefix. Measured: 1.17 GB selected versus 30 GB unselected." That is this change's shape: the default carries the crew half, the kiro-cli tree rides on an opt-in, and the tiers come with a measurement. The RFC's own numbering separates its decisions from its open questions -- D-items are decided, O-items are open -- and the only O-item touching this area is O1, whose line says it "Blocks neither milestone". So the document beingstatus: draftdescribes its review state, not the status of each item inside it. What remains genuinely open is narrower and is a maintainer's: whether an acceptance of D4 must be recorded before merge. First Principles Review blocks this head on the wider claim that the decision exists nowhere, and its two named clearances -- an RFC merged non-draft on base, or an override -- are both outside the author's reach. The keystone placement is boundary-derived and is not in question; only the default is.One lock order, and every path obeys it. The recheck, the live authorization and the PUT run inside one hold of the state file's sidecar lock, taken through
_upload_lock, which holds the FILE half only and deliberately not_run_lock--_run_lockalso serializeslast_runs, which the dashboard's backup-status read goes through, so holding it across a PUT allowed_PUSH_TIMEOUT_SECSstalled every account's status surface for one account's upload. Dropping it from the gate was necessary and not sufficient: the stall arrived through the contending WRITER._state_locktook_run_lockbefore parking on the file lock, so a writer meeting an in-flight upload -- a mid-upload revocation, or any second account's_record_runfinishing -- held_run_lockfor that upload's whole duration andlast_runsqueued behind the writer. So this states one order, in a comment above_state_lockwith_run_lock's own definition pointing at it:Nothing may hold
_run_lockwhile waiting for the file lock._state_locktakes the file lock first, and_record_runand_record_skipno longer wrap it in_run_lockat all. Swapping the order inside_state_lockalone would have been worse than the stall: both of those reach_state_lockthrough_record_run_locked -> _locked_state_update, so one thread would hold_run_lockwaiting for the file lock while another held the file lock waiting for_run_lock-- a cycle. Of the file's 24 textual mentions of_run_lockonly 5 are acquisitions;uploaded_objectsandlast_runstake it alone and never reach the file lock. Removing the outer hold is also not a pure deletion: it was the only thing serialising the_run_sequencebump, and(process, sequence)is the identity the compare-and-set insidemutatereads, so_record_run_lockednow takes_run_lockfor that bump alone -- which cannot park -- and keeps it outsidemutateso the state-read and state-write failure paths still hand a uniquely numbered record to_remember_unpersisted.The spec.
docs/system-specs/modules/aws-control.mddescribes the per-accountbackup.jsonkey, its owner-gated writer, and why notconfig.json. The first commit's paragraph documented theconfig.jsonplacement the second commit removes as unsafe; leaving it would have instructed the next contributor to re-implement the self-grantable shape.Backwards compatibility
This changes what an existing sessions archive contains: until the operator grants the permission, new archives carry the transcript half only, and a restore from one replays a transcript rather than resuming through
session/load. Archives already in a bucket are untouched. The loss is stated on the run record rather than silent.No signature changes reach existing callers:
layer_bis keyword-only with a default on_record_run, and the new state key is additive -- the writer is a read-modify-write of the whole document under the existing sidecar lock, so thenightlybit beside it survives (pinned by a test).Tests
test/test_aws_control_backup.py::TestSessionsArchiveLayerBGate(12 cases) and three new cases intest/test_aws_control_routes.py::TestBackupEndpoints. 496 pass acrosstest_aws_control_backup.py,test_aws_control_backup_job.py,test_aws_control_routes.pyandtest_aws_control_app.py.Block path: the default withholds with no state file at all (the real reader, not a stub, so the default itself is what is pinned); a non-boolean
"true"withholds; an unparseable state document withholds and the backup still runs; anenabledfield that is not a boolean is refused by the route withinvalid_enabledand never reaches the writer. Each asserts the kiro-cli half is absent from the tar and that the record sayslayer_bis false.Allow path: with the permission recorded both halves ride, and the record says so. Both Layer B filenames are fixtured (
<sid>.jsonand<sid>.jsonl) so a narrower filter cannot pass by matching one.Also pinned: an agent-writable
config.jsoncannot grant the permission under any of the three spellings an earlier draft read, so moving the read back there reddens; the export key does not grant here; a grant for one account does not carry to another;STATE_DIR_LEAFis insecurity._CREW_SECRET_LEAVESand the app's data dir is that leaf, so moving the store out of the fenced directory reddens; the writer round-trips both directions and keepsnightlybeside it; the permission is read exactly once; a granted run whose kiro-cli directory holds nothing recordslayer_bfalse while the permission still reads true; a snapshot record carries nolayer_bkey; a write that fails returnsstate_persist_failedwithout reporting the setting as applied and without echoing the state file's absolute path.Red on base. The original class run against
origin/main(f31f25a) with the fix absent: 6 of 7 failed, three of them onassert ['cli/abc.json', 'cli/abc.jsonl', 'crew/t.jsonl'] == ['crew/t.jsonl'], which is also the measurement of the exposure -- no operator choice recorded, Layer B in the archive.Mutation-verified, each mutation applied by hand and both files restored byte-identical afterwards:
config.json -> dashboard.backup_include_layer_bagainisinstance(raw, bool) else False->bool(raw)Trueisinstance(raw, bool)->raw is not None+bool(raw)_run_lockback before the file lock in_state_lock_run_lockback in_record_run_run_sequencebump moved outside_run_lockThe last three are the lock-order half. Each reds only its own test, with its own assertion text, so the two lock tests are independent and each half of the fix is load-bearing rather than one change covering for the other. The earlier status-read test could not observe any of this: it had no concurrent writer, so it passed with the old order restored. It is kept for the solo-upload case and the new one carries the contention.
Manual verification
None beyond the tests: the upload path is mocked in every case, so no bucket is reached.
Related Issues
Closes #11559
Pattern harvest
Rule candidate: review-prompt -- when a change introduces a permission, a consent record, or any stored value that AUTHORIZES an irreversible action, ask where that value lives before asking whether the check is correct. A check that reads an agent-writable file is not a gate, however carefully it validates what it finds there, because the party the gate constrains can write the answer. The concrete test is whether the store sits behind the agent file floor and whether exactly one owner-gated writer opens it directly.
Rule candidate: review-prompt -- when a change adds or moves a bulk filesystem copy (a tar, zip, or rsync of a whole directory), ask whether any path inside that directory is governed elsewhere by an explicit permission, and require the bulk path to consult the same permission. The original defect here was not a wrong decision; it was a decision that no code made, because taring a directory reaches whatever is in it.
A payload that cannot be redacted has only two states, so the instrument is a permission rather than a filter -- and when a permission decides what an artifact contains, read it once and record the answer on the artifact, or the artifact and its record can disagree about the same bytes.
When two paths handle one payload differently, the question is which condition each path's destination justifies, not which path is wrong. Here the operator-identity condition already held on both, the per-invocation condition has no channel on an unattended job, and only the standing permission was genuinely missing.