Repository navigation
fix(apps): create AppStorage's kv dir on first write, not on the loop - #24460
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (2 items) — 2 justifiedIntent: stop lifecycle events for an app with the storage permission from blocking the gateway event loop on a
[FIRST-PRINCIPLES-REVIEWED] 5bb1f1b Description read[DESCRIPTION-READ] 059fd708a878beac73e10e2906cbb7be4bdd3def5d6ef8a71b3e437b6d776464 That sha256 names the PR description this verdict read. Recompute it with |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 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: |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks. The lazy create matches [DESIGN-REVIEWED] 5bb1f1b Description read[DESCRIPTION-READ] d331b9129bf0a8b8a1b29fa2f0ace9f0c3f2074d1692bc30305e6a64d8208efb That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
8a98d0f to
2182830
Compare
2182830 to
fc4eda4
Compare
fc4eda4 to
8a96907
Compare
8a96907 to
6807f2e
Compare
AppStorage.__init__ ran self._kv_dir.mkdir() directly. The constructor is invoked by the sync build_app_context factory, which the hook-context builders call on the gateway event loop, so every lifecycle event for a storage-enabled app ran a blocking mkdir on the loop. Move the create to the write path (set), the pattern the sibling JobStore already uses: JobStore.write mkdirs, and its reads tolerate a missing dir. AppStorage's reads already do too (get/delete gate on is_file, list_keys returns [] when the dir is absent). The create uses the identical Path.mkdir call, so the dir keeps the same mode; atomic_write finds it present and leaves it untouched, so the permission model is unchanged.
6807f2e to
5bb1f1b
Compare
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: fix (3 files). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, security checklist all-NO, no reviewer-lane findings, readiness signal fresh for this head SHA, declared category matches the diff. Category: AppStorage creates its kv directory on the first write instead of in the constructor, with tests.
Problem / Motivation
Goal: Stop every app lifecycle event for a storage-enabled app from blocking the gateway event loop on a filesystem
mkdir.AppStorage.__init__(src/kiro_crew/apps/app_storage.py) created the app's key-value directory with a directself._kv_dir.mkdir(parents=True, exist_ok=True). The constructor is invoked by the synchronousbuild_app_contextfactory (src/kiro_crew/apps/context.py), which the hook-context builders —LifecycleDispatcher._build_contextandhooks_integration._build_app_context_from_info— call from coroutines on the gateway event loop. So every lifecycle event (enable, gateway boot, disable, gateway shutdown) for an app granted thestoragepermission ran a blockingmkdiron the loop.Why this change
The reporter (IngridMorstrad) needs the
AppStoragekv-dir create off the event loop: it is the last constructor-timemkdiron thebuild_app_contextpath, the direct sibling of thedata-dir create already fixed in #24359. This is a correctness fix for a shared-latency bug — it does not change any default, UI, output, or the storage API; a storage-enabled app behaves exactly as before, the directory is just created on the first write instead of at construction. No user who did not ask for it sees any behaviour change. I matched the lazy-create-on-first-write pattern the siblingJobStorealready uses rather than adding anasyncio.to_threadhop, because it is the smaller change and keepsbuild_app_contextsynchronous.Why it matters
While that
mkdirwaits on a slow or contended data disk, every other session and request served by the gateway waits with it. It is the direct sibling of thedata-dir create fixed in #24359; that PR moved thedatadir off the loop but deliberately left theAppStoragekv dir, because fixing it needs a decision about where the store's directory contract lives (its constructor is sync and has many sync callers, including tests).Not a goal
build_app_contextinto an async factory; the constructor stays sync.What changed (motivation → approach → change)
mkdirruns on the event loop on every lifecycle event for a storage-enabled app.AppStorage.__init__, and the constructor runs on the loop via the sync factory.asyncio.to_threadhop in the async callers. I matched (a), because the nearest sibling store already uses it:JobSDK'sJobStore(also built inbuild_app_contextfrom the app'sdata_dir, also permission-gated) does notmkdirin its constructor — it creates the dir insideJobStore.write, and its reads tolerate a missing dir. This keepsbuild_app_contextsync and touches onlyAppStorage, the smallest change.mkdirfromAppStorage.__init__and made the identicalPath.mkdir(parents=True, exist_ok=True)call at the top ofset(the only write path). The read paths already treat a missing directory as empty:get/deletegate onpath.is_file()andlist_keysreturns[]whenself._kv_diris not a directory.Permissions are unchanged because the create is the exact same plain
Path.mkdircall the constructor used, and it runs beforeatomic_writetouches the tree.atomic_writewould otherwise create a missing parent owner-only (0700) inside the data home, but it finds the directory already present and leaves an existing directory untouched, so the kv dir keeps the mode it has today.Backwards compatibility
Compatible: nothing that works today stops working. The public
AppStorageAPI (get/set/delete/list_keys) is unchanged; only the moment of directory creation moves from construct-time to first-write. Callers that construct the store and then read see an empty store (as before); callers that write see the directory created on that write.Removes nothing: no path, record, key or function is removed, renamed or tightened — the only observable change is that
<app>/data/kvdoes not exist until the firstsetinstead of at construction, which every read path already handles as empty.Tests
test/test_app_storage_kv_dir_off_loop.py(new):test_build_app_context_does_not_create_the_kv_dir— building the context via the real on-loop factory records nokvmkdirand leaves the dir absent (red onorigin/main, where the constructor mkdirs it on the loop thread).test_first_set_creates_the_kv_dir— the firstsetcreates the dir and the value round-trips.test_first_set_creates_the_kv_dir_with_the_prior_permissions— the lazily-created dir's mode equals a plainPath.mkdir's mode in the same tree (pins the permission model).test_reads_tolerate_a_missing_kv_dir—get/list_keys/deletebehave as empty before the dir exists.test/test_app_context_cov80.pyupdated: the staleassert (tmp_path / "kv").is_dir()right afterbuild_app_contextencoded the old on-construct behaviour; it now asserts the dir is absent after the build and present after the firstset.Proof: PROVEN the new off-loop/first-write tests fail with the production hunks reverted (head 8a98d0f)
Manual verification
N/A — unit coverage sufficient. The reproduction (a
Path.mkdirthread probe over the realbuild_app_contextpath) is captured by the off-loop test.Related Issues
Closes #24358
Sibling sweep
Swept:
grep -rn "mkdir" src/kiro_crew/apps/*.pyfor stores thatmkdirin a constructor reachable from the syncbuild_app_contextfactory.Sibling:
src/kiro_crew/apps/job_sdk.py:JobStore— not affected — it already creates its dir lazily inJobStore.write, which is the pattern this PR adopts.Sibling:
src/kiro_crew/apps/hooks_integration.py/src/kiro_crew/apps/lifecycle.py— not affected — the siblingdata-dir create was already moved off the loop in #24359.Pattern harvest
Not generalizable: this is a one-off — the specific store (
AppStorage) was the last constructor-timemkdiron thebuild_app_contextpath; the sibling stores already create lazily or were fixed in #24359.Checklist