Skip to content

fix(apps): keep the app-config writes off the event loop - #4550

Closed
leonlaiyc wants to merge 2 commits into
kirodotdev:mainfrom
leonlaiyc:fix/apps-config-writes-off-loop
Closed

leonlaiyc wants to merge 2 commits into
kirodotdev:mainfrom
leonlaiyc:fix/apps-config-writes-off-loop

Conversation

@leonlaiyc

@leonlaiyc leonlaiyc commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Saving the app-registry list no longer blocks the gateway's event loop or silently reverts another settings save, and every trust grant it commits is audited.

PUT /api/apps/registries performed its config.json read-modify-write synchronously on the asyncio event loop and without holding both config-writer lock generations. A concurrent writer could therefore lose unrelated configuration.

The trust audit also had two opposite failure modes:

  • auditing before persistence could claim a trust grant after a failed write;
  • auditing after run_config_write in the async handler could be skipped on cancellation even though the shielded worker completed the write.

Current main already contains the earlier enable/disable offload work. This PR is intentionally limited to the remaining registry PUT path.

Why it matters

config.json holds every user setting, not only the registry list. A registry save that interleaves with any other dashboard settings save can silently revert that other save, and nothing reports it. The blocking read and write also run on the gateway's event loop, so a slow disk stalls every chat, channel and API request for that time. A registry host is a trust grant: its apps install with gateway privileges, and it joins the loosened clone trust set. A grant that lands without its registries.host_trust_granted record leaves incident response unable to establish when a host entered the trust set.

Not a goal

  • Any other config.json writer. This PR moves only the registry PUT.
  • Any change to registry validation, trust tiers, or the PUT request/response shape.

What changed (motivation → approach → change)

Still live on main (b43b751). handle_registries reads config.json with read_config_text and publishes it with a bare atomic_write, both inline on the loop and outside update_config_locked. That spelling is not one TestEveryConfigWriterIsLocked scans for. It audits each grant before the write, so a failed write still reports the grant.

Approach. config.json has two writer generations: update_config_locked (cross-process sidecar flock) and the legacy dashboard writers that serialize on the in-process _get_config_lock only. Holding one of them still lets the other family commit a stale snapshot over this write. dashboard.chat_utils.run_config_write is the existing bridge that holds both and runs the blocking work in a worker. It also shields and drains that worker across cancellation, then re-raises and discards the return value, so the grant audit has to live in the worker that committed the write rather than in the caller.

Change. In src/kiro_crew/apps/routes.py:

  • The whole registry read-modify-write moves into _write_registries_config, an update_config_locked mutation. Newly trusted hosts are computed inside the lock, against the prior on-disk registries. Main's or [] repair idiom for an explicit "registries": null is kept.
  • handle_registries dispatches it through run_config_write. A malformed or non-object config.json (ConfigReadError without an OSError cause) keeps main's 500 "malformed" answer, and an unreadable one keeps "cannot read config". A failed write now answers a structured 500 with registries_config_write_failed instead of escaping as an unhandled exception.
  • Each registries.host_trust_granted event is emitted in that worker right after the write commits. A failed write therefore emits no grant, and a cancelled caller can no longer leave persisted trust with no audit record. The request-level registries.update event stays with the request.
  • The run_config_write import stays call-time. No module in apps/ imports kiro_crew.dashboard at module scope, and hoisting it would load the dashboard tree into every importer of apps.routes.
  • The module-level atomic_write and read_config_text imports are now unused here and are removed.

This rebase (onto b43b751, 3240 commits). It resolved two textual conflicts. In routes.py, main had added the non-object check: update_config_locked → read_config_for_update raises ConfigReadError for exactly that case, so the PR side was taken. In the test file, main's _mark_recovered("inst", inst_double, 0) signature is kept, with this PR's event-based wait. Two newer main tests needed adapting to the moved write:

  • test_apps_install_owner_gate.py's side-effect sentinel wrapped routes.atomic_write. It now wraps routes.update_config_locked, which is where the registry PUT commits.
  • This PR's three registry tests call handle_registries with a bare request, and main's handle_registries has since gained require_owner_dashboard_request. They now admit the request through that gate, which they do not test; test_apps_install_owner_gate.py covers it.

The shared apps/routes.py changes in #6854 and #5488 are in separate semantic regions. #6206 is a broader App Store refresh that touches the same file.

Backwards compatibility

Compatible: the PUT accepts the same body and returns the same success shape (ok, registries, newlyTrustedHosts). The malformed and unreadable config answers are unchanged. The only new answer is a structured 500 for a failed write, which previously surfaced as an unhandled exception.

Reader: apps/routes.py module attribute atomic_write -- app -- removed as unused here. Its one reader was the test_apps_install_owner_gate.py sentinel, moved to update_config_locked (test_owner_reaches_the_side_effect[registries_put]). spec_builder/tests/test_routes.py patches atomic_write on spec_builder's own routes module, not this one.
Reader: apps/routes.py module attribute read_config_text -- app -- removed as unused here, with no reader on origin/main.

Tests

test/test_apps_instances_loop_offload.py, 23 passed, covering:

  • the registry PUT handing the locked mutation to a worker thread, and the loop staying responsive meanwhile;
  • the PUT holding and releasing the loop-side _get_config_lock;
  • the locked mutation preserving unrelated config and de-duplicating grants;
  • a failed write producing neither persistence nor a grant audit;
  • cancellation at a threading.Event boundary still producing the committed config and exactly one grant audit;
  • AST ratchets against a bare asyncio.to_thread regression;
  • the existing _mark_recovered cancellation test, driven by events and an event-loop sentinel instead of sleep(0.05).

test/test_apps_install_owner_gate.py (owner gate on all five install routes, including registries_put) passes with the moved sentinel. The two files together: 40 passed.

With the related suites (test_config_rmw_preserves_settings.py, which holds the TestEveryConfigWriterIsLocked ratchet, plus test_apps_routes_coverage.py, test_registries_api.py, test_file_menu_items.py and test_token_auth.py), the total is 710 passed, 11 skipped, 0 failed. On the changed files, mypy on apps/routes.py, flake8, isort --check-only, black --check and the comment-history gate are all clean.

Manual verification

N/A — unit coverage sufficient: the change is a backend config-write path, and the tests drive the real handle_registries through run_config_write and update_config_locked against a real config.json, including the cancellation window.

Related Issues

No issue filed; this is the registry-PUT path left after the enable/disable offload work.

Pattern harvest

Rule candidate: semgrep
Pattern: a config.json write spelled as a bare atomic_write(config_path, ...) bypasses update_config_locked and is invisible to the write_config_atomically-matching ratchet.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

🤖 Generated with Claude Code

@leonlaiyc
leonlaiyc requested a review from a team August 19, 2026 14:32
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Aug 19, 2026
@leonlaiyc
leonlaiyc force-pushed the fix/apps-config-writes-off-loop branch from da5ba08 to 81ec689 Compare August 19, 2026 15:15
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Aug 19, 2026
leonlaiyc added a commit to leonlaiyc/KiroCrew that referenced this pull request Aug 23, 2026
Every config.json update in dashboard/handlers/memory.py reads the file,
changes one key and writes it all back on the event loop. The read, the
JSON parse, and write_config_atomically -- a tmp-file write plus a rename,
which can fsync -- all block, stalling every other session while they run.
Three sites do it: api_memory_settings, _write_embed_model_config, and
_set_migrated, which runs on EVERY boot while migrated is false and so
lands the stall exactly when the gateway is bringing sessions up. This is
the class the repo has been closing site by site (kirodotdev#4118, kirodotdev#3803, kirodotdev#4550), and
kirodotdev#4946's review named this module.

The whole transaction crosses over, never just the read. Offloading the
read alone would leave the write on the loop and insert a suspension point
between the read and the write-back while the file is unguarded on disk: an
external editor, a CLI command or another process landing in that gap would
be silently overwritten by a write derived from state nobody re-checked.
That gap is zero today because the sequence is synchronous, and it stays
zero because the worker performs the whole thing without yielding. The
existing per-config lock is held across the hop, so two coroutines still
cannot interleave.

Two of the three sites also hand-rolled a reader this module already
imports. read_config_for_update is the documented companion to
write_config_atomically with 27 call sites, and api_memory_settings uses it
200 lines above; _set_migrated and _write_embed_model_config instead caught
Exception around json.loads. The helper additionally refuses a non-object
top level, where the hand-rolled version accepted a list and then raised
AttributeError from setdefault -- a crash where a fail-closed refusal was
intended.

ConfigReadError is not swallowed by the helper: what to tell the user
differs per site, and each keeps exactly the behaviour it had -- skip and
retry next boot, raise ValueError, or answer 500 config_unreadable.

api_memory_settings now validates its body before the transaction. None of
that reads the config, and a 400 previously took the lock and abandoned it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
leonlaiyc added a commit to leonlaiyc/KiroCrew that referenced this pull request Aug 23, 2026
_write_env_updates stats and reads the whole .env, re-parses it line by
line, then creates a 0600 temp file, chmods it, writes and renames. All
synchronous file I/O, and six async channel config-save handlers call it.

Three already reached it through asyncio.to_thread -- telegram, teams,
wecom -- and three called it inline on the gateway loop: slack, discord and
webex, stalling every other session for the duration of a token save.

So this is not a missing convention but an existing one applied to half the
call sites. messaging.py already uses asyncio.to_thread 29 times, and with
three siblings doing it correctly nothing in the file said which half was
right, or stopped the next channel from copying the wrong one. It is the
class the repo has been closing site by site (kirodotdev#4118, kirodotdev#3803, kirodotdev#4550), and
kirodotdev#4946's review named this module.

The WHOLE call is offloaded, never a part of it: the read-modify-write is
one transaction, and a suspension point between the read and the rename
would let a concurrent writer's keys be dropped by a write derived from
lines nobody re-read. Keeping _write_env_updates one synchronous function
on one worker preserves that without depending on the caller, which is now
said on the function itself so the next channel inherits the reason and not
just the shape.

The regression pins all six channels rather than the three that moved,
since the defect was the split and not any one site.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Aug 23, 2026
leonlaiyc added a commit to leonlaiyc/KiroCrew that referenced this pull request Aug 24, 2026
Every config.json update in dashboard/handlers/memory.py reads the file,
changes one key and writes it all back on the event loop. The read, the
JSON parse, and write_config_atomically -- a tmp-file write plus a rename,
which can fsync -- all block, stalling every other session while they run.
Three sites do it: api_memory_settings, _write_embed_model_config, and
_set_migrated, which runs on EVERY boot while migrated is false and so
lands the stall exactly when the gateway is bringing sessions up. This is
the class the repo has been closing site by site (kirodotdev#4118, kirodotdev#3803, kirodotdev#4550), and

The whole transaction crosses over, never just the read. Offloading the
read alone would leave the write on the loop and insert a suspension point
between the read and the write-back while the file is unguarded on disk: an
external editor, a CLI command or another process landing in that gap would
be silently overwritten by a write derived from state nobody re-checked.
That gap is zero today because the sequence is synchronous, and it stays
zero because the worker performs the whole thing without yielding. The
existing per-config lock is held across the hop, so two coroutines still
cannot interleave.

Two of the three sites also hand-rolled a reader this module already
imports. read_config_for_update is the documented companion to
write_config_atomically with 27 call sites, and api_memory_settings uses it
200 lines above; _set_migrated and _write_embed_model_config instead caught
Exception around json.loads. The helper additionally refuses a non-object
top level, where the hand-rolled version accepted a list and then raised
AttributeError from setdefault -- a crash where a fail-closed refusal was
intended.

ConfigReadError is not swallowed by the helper: what to tell the user
differs per site, and each keeps exactly the behaviour it had -- skip and
retry next boot, raise ValueError, or answer 500 config_unreadable.

api_memory_settings now validates its body before the transaction. None of
that reads the config, and a 400 previously took the lock and abandoned it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@bolichen97
bolichen97 enabled auto-merge (squash) August 24, 2026 06:58
leonlaiyc added a commit to leonlaiyc/KiroCrew that referenced this pull request Aug 24, 2026
Every config.json update in dashboard/handlers/memory.py reads the file,
changes one key and writes it all back on the event loop. The read, the
JSON parse, and write_config_atomically -- a tmp-file write plus a rename,
which can fsync -- all block, stalling every other session while they run.
Three sites do it: api_memory_settings, _write_embed_model_config, and
_set_migrated, which runs on EVERY boot while migrated is false and so
lands the stall exactly when the gateway is bringing sessions up. This is
the class the repo has been closing site by site (kirodotdev#4118, kirodotdev#3803, kirodotdev#4550), and

The whole transaction crosses over, never just the read. Offloading the
read alone would leave the write on the loop and insert a suspension point
between the read and the write-back while the file is unguarded on disk: an
external editor, a CLI command or another process landing in that gap would
be silently overwritten by a write derived from state nobody re-checked.
That gap is zero today because the sequence is synchronous, and it stays
zero because the worker performs the whole thing without yielding. The
existing per-config lock is held across the hop, so two coroutines still
cannot interleave.

Two of the three sites also hand-rolled a reader this module already
imports. read_config_for_update is the documented companion to
write_config_atomically with 27 call sites, and api_memory_settings uses it
200 lines above; _set_migrated and _write_embed_model_config instead caught
Exception around json.loads. The helper additionally refuses a non-object
top level, where the hand-rolled version accepted a list and then raised
AttributeError from setdefault -- a crash where a fail-closed refusal was
intended.

ConfigReadError is not swallowed by the helper: what to tell the user
differs per site, and each keeps exactly the behaviour it had -- skip and
retry next boot, raise ValueError, or answer 500 config_unreadable.

api_memory_settings now validates its body before the transaction. None of
that reads the config, and a 400 previously took the lock and abandoned it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
leonlaiyc added a commit to leonlaiyc/KiroCrew that referenced this pull request Aug 24, 2026
Every config.json update in dashboard/handlers/memory.py reads the file,
changes one key and writes it all back on the event loop. The read, the
JSON parse, and write_config_atomically -- a tmp-file write plus a rename,
which can fsync -- all block, stalling every other session while they run.
Three sites do it: api_memory_settings, _write_embed_model_config, and
_set_migrated, which runs on EVERY boot while migrated is false and so
lands the stall exactly when the gateway is bringing sessions up. This is
the class the repo has been closing site by site (kirodotdev#4118, kirodotdev#3803, kirodotdev#4550), and

The whole transaction crosses over, never just the read. Offloading the
read alone would leave the write on the loop and insert a suspension point
between the read and the write-back while the file is unguarded on disk: an
external editor, a CLI command or another process landing in that gap would
be silently overwritten by a write derived from state nobody re-checked.
That gap is zero today because the sequence is synchronous, and it stays
zero because the worker performs the whole thing without yielding. The
existing per-config lock is held across the hop, so two coroutines still
cannot interleave.

Two of the three sites also hand-rolled a reader this module already
imports. read_config_for_update is the documented companion to
write_config_atomically with 27 call sites, and api_memory_settings uses it
200 lines above; _set_migrated and _write_embed_model_config instead caught
Exception around json.loads. The helper additionally refuses a non-object
top level, where the hand-rolled version accepted a list and then raised
AttributeError from setdefault -- a crash where a fail-closed refusal was
intended.

ConfigReadError is not swallowed by the helper: what to tell the user
differs per site, and each keeps exactly the behaviour it had -- skip and
retry next boot, raise ValueError, or answer 500 config_unreadable.

api_memory_settings now validates its body before the transaction. None of
that reads the config, and a 400 previously took the lock and abandoned it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
leonlaiyc added a commit to leonlaiyc/KiroCrew that referenced this pull request Aug 25, 2026
Every config.json update in dashboard/handlers/memory.py reads the file,
changes one key and writes it all back on the event loop. The read, the
JSON parse, and write_config_atomically -- a tmp-file write plus a rename,
which can fsync -- all block, stalling every other session while they run.
Three sites do it: api_memory_settings, _write_embed_model_config, and
_set_migrated, which runs on EVERY boot while migrated is false and so
lands the stall exactly when the gateway is bringing sessions up. This is
the class the repo has been closing site by site (kirodotdev#4118, kirodotdev#3803, kirodotdev#4550), and

The whole transaction crosses over, never just the read. Offloading the
read alone would leave the write on the loop and insert a suspension point
between the read and the write-back while the file is unguarded on disk: an
external editor, a CLI command or another process landing in that gap would
be silently overwritten by a write derived from state nobody re-checked.
That gap is zero today because the sequence is synchronous, and it stays
zero because the worker performs the whole thing without yielding. The
existing per-config lock is held across the hop, so two coroutines still
cannot interleave.

Two of the three sites also hand-rolled a reader this module already
imports. read_config_for_update is the documented companion to
write_config_atomically with 27 call sites, and api_memory_settings uses it
200 lines above; _set_migrated and _write_embed_model_config instead caught
Exception around json.loads. The helper additionally refuses a non-object
top level, where the hand-rolled version accepted a list and then raised
AttributeError from setdefault -- a crash where a fail-closed refusal was
intended.

ConfigReadError is not swallowed by the helper: what to tell the user
differs per site, and each keeps exactly the behaviour it had -- skip and
retry next boot, raise ValueError, or answer 500 config_unreadable.

api_memory_settings now validates its body before the transaction. None of
that reads the config, and a 400 previously took the lock and abandoned it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
iamwhatever pushed a commit that referenced this pull request Aug 25, 2026
…5059)

Every config.json update in dashboard/handlers/memory.py reads the file,
changes one key and writes it all back on the event loop. The read, the
JSON parse, and write_config_atomically -- a tmp-file write plus a rename,
which can fsync -- all block, stalling every other session while they run.
Three sites do it: api_memory_settings, _write_embed_model_config, and
_set_migrated, which runs on EVERY boot while migrated is false and so
lands the stall exactly when the gateway is bringing sessions up. This is
the class the repo has been closing site by site (#4118, #3803, #4550), and

The whole transaction crosses over, never just the read. Offloading the
read alone would leave the write on the loop and insert a suspension point
between the read and the write-back while the file is unguarded on disk: an
external editor, a CLI command or another process landing in that gap would
be silently overwritten by a write derived from state nobody re-checked.
That gap is zero today because the sequence is synchronous, and it stays
zero because the worker performs the whole thing without yielding. The
existing per-config lock is held across the hop, so two coroutines still
cannot interleave.

Two of the three sites also hand-rolled a reader this module already
imports. read_config_for_update is the documented companion to
write_config_atomically with 27 call sites, and api_memory_settings uses it
200 lines above; _set_migrated and _write_embed_model_config instead caught
Exception around json.loads. The helper additionally refuses a non-object
top level, where the hand-rolled version accepted a list and then raised
AttributeError from setdefault -- a crash where a fail-closed refusal was
intended.

ConfigReadError is not swallowed by the helper: what to tell the user
differs per site, and each keeps exactly the behaviour it had -- skip and
retry next boot, raise ValueError, or answer 500 config_unreadable.

api_memory_settings now validates its body before the transaction. None of
that reads the config, and a 400 previously took the lock and abandoned it.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@dwu96 dwu96 added the needs-pr-triage PR scanner: awaiting automated triage label Aug 26, 2026
@iamwhatever iamwhatever added drive-to-green PR claimed by drive-to-green pipeline and removed needs-pr-triage PR scanner: awaiting automated triage labels Aug 26, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

🤖 Kiro Crew [operator: iamwhatever]: This PR has been inactive for 7+ days with failing CI. I've assessed the blockers and they appear resolvable — I'll push fixes directly to this branch as a co-author.

Assessment: Two mechanical blockers, neither in the PR's own logic. (1) The branch is CONFLICTING against main — both touched files moved after this head (src/kiro_crew/apps/routes.py last via #6007 app-trust-to-repository binding; test/test_apps_instances_loop_offload.py via #5676/#5220), so the fix is a rebase taking main's structure and layering this PR's asyncio.to_thread offload + replace_with_retry adoption + extended AST-ratchet banned set on top. (2) The only red check, E2E (stub ACP backend, offline), concluded cancelled on 2026-08-19 with no output — a superseded run, not a real failure; it re-runs on the rebased head. No review threads or reviewer concerns exist on this PR.

If you'd prefer I don't touch this PR, add the pr-no-autofix label.

@bolichen97
bolichen97 force-pushed the fix/apps-config-writes-off-loop branch from 81ec689 to bf0ae7c Compare August 30, 2026 01:51
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Aug 30, 2026
@dwu96

dwu96 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

3 similar comments
@iamwhatever

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

4 similar comments
@dwu96

dwu96 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@dwu96

dwu96 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@bolichen97

Copy link
Copy Markdown
Contributor

Open PR relationship audit

This is a consolidated, point-in-time code-level audit note. It compares complete merge-base diffs and current/merged code; it does not treat a shared topic as duplication or partial coverage as completion.

Relationship findings

  • PR #7167 is OVERLAPPING relative to this PR. The goals differ or the implementations can complement each other; this is not a duplicate claim. Recommended action for PR #7167: REBASE. Independent call site; landing either one does nothing for the other. Files: src/kiro_crew/apps/routes.py.
  • This PR is PARTIALLY_COVERED with PR #6984. Coverage is explicitly incomplete; this finding is not a completion or closure claim. Recommended action for PR #4550: KEEP. PR #6984 landed the sibling half in the same file; the registry PUT remains the uncovered half and this PR was already narrowed to exactly it, so the two are complementary rather than redundant. Files: src/kiro_crew/apps/routes.py.

No PR, Issue, label, branch, or review state was changed by the relationship-note portion of this audit.

@dwu96

dwu96 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@bolichen97
bolichen97 force-pushed the fix/apps-config-writes-off-loop branch from 7885b24 to 0eb7742 Compare September 8, 2026 09:34
@bolichen97

Copy link
Copy Markdown
Contributor

Rebased onto main 6fbb06bc by a maintainer as part of the 2026-09-08 open-PR audit.

Clean rebase — no conflicts. The only delta from your old head is one stray ) line that main's current shape already carried, so the diff is 525/-46 instead of 526/-47. Behaviour is unchanged.

Gates run locally on the rebased head: black --check on the two changed files (test/test_apps_instances_loop_offload.py is already in .github/black-baseline.txt and was equally unformatted on your old head, so nothing new), isort --check-only clean, flake8 clean, and pytest test/test_apps_instances_loop_offload.py → 23 passed.

Please review the rebased head. A maintainer push makes the maintainer the last pusher, so under this repo's last-push rule a second approver is needed. Reply here if anything looks wrong.

@dwu96

dwu96 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

1 similar comment
@dwu96

dwu96 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR's description is missing some required sections from our PR template. Workflow runs won't be auto-approved until the description is updated.

Missing sections:

  • ## Why it matters

Please update your PR description to include these sections, then push or re-save the description. The workflows will be approved on the next cycle.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 15, 2026
leonlaiyc and others added 2 commits October 5, 2026 00:38
Current main already offloads the enable and disable writers from the
original change. Complete the remaining registry PUT path by moving its
entire read-modify-write off the loop and using the shared locked config
updater. Compute newly trusted hosts from the state protected by that
lock, preserve unrelated config, deduplicate grants, and emit trust
events only after the write commits.

config.json has two writer generations that do not exclude each other:
update_config_locked takes the sidecar advisory flock, while the legacy
dashboard writers (agents endpoint, updates.py, security.py,
messaging.py, mcp.py, core.py STT) serialize on the loop-side
_get_config_lock alone. Dispatch the registry write through
dashboard.chat_utils.run_config_write, the one entry point that holds
both, so a concurrent legacy PUT cannot commit a stale snapshot over it.
The blocking work still runs in a worker, so the loop never stalls.

Add deterministic execution-level and AST coverage for worker-thread
dispatch, loop-side lock ownership across the write, locked mutation,
preserved data, grant deduplication, and failed-write audit behavior.
run_config_write shields its worker and drains it across cancellation, then
re-raises CancelledError and discards the return value. A gateway shutdown or
a client disconnect landing while the registry PUT's write is in flight
therefore committed the trust grant and never reached the caller-side audit:
persisted trust with no registries.host_trust_granted record, which is exactly
the reconstruction gap that event exists to close.

Emit the per-host grant inside _write_registries_config, immediately after
update_config_locked returns. Nothing can interrupt a thread between those two
statements, so the event and the commit cannot disagree; the emission is still
strictly after the write, so a failed write announces nothing; and SEL adapts
to an off-loop caller and swallows filesystem errors on a non-critical event,
so this cannot turn a committed write into a 500. The handler keeps the
registries.update API-outcome event, which correctly belongs to the request.

Three tests: a cancellation delivered while the worker is parked inside the
locked write (red-before: config committed, zero grants); exactly-once
auditing per newly trusted host; and no grant when the locked write raises.

Also corrects the run_config_write import comment, which implied the
top-level-imports circular-import exemption. There is no cycle on that edge --
every import order was checked. The real reasons are layering and load cost,
both now stated and checkable: no module in apps/ imports dashboard at module
scope, and hoisting pulls 117 extra modules into every importer of apps.routes
(479 -> 596). The import stays lazy; the justification is now accurate.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@leonlaiyc
leonlaiyc force-pushed the fix/apps-config-writes-off-loop branch from 0eb7742 to 6024990 Compare October 4, 2026 16:45
@leonlaiyc
leonlaiyc requested a review from a team as a code owner October 4, 2026 16:45
@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 4, 2026
@leonlaiyc

Copy link
Copy Markdown
Contributor Author

CI note on head 60249906a:

No push for these. A re-run after #16801 lands should clear shard 7.

@iamwhatever

Copy link
Copy Markdown
Contributor

Thank you, @leonlaiyc, for this fix — moving the registry config write off the event loop, holding both config-writer locks, and binding the per-host trust audit to the committed write so a cancellation can't persist a grant without a record.

Because the base had moved on considerably since this branch was cut, I've reopened the change as a fresh main-repo PR rebased on current main, crediting you as co-author: #18103. It carries your exact change (ported onto main's current handle_registries shape) and is green — all checks pass and the review lanes are satisfied, now awaiting a maintainer's approval. Closing this one in favour of #18103. Thanks again for the contribution!

@iamwhatever iamwhatever closed this Oct 8, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 8, 2026
@mbriones98 mbriones98 removed the drive-to-green PR claimed by drive-to-green pipeline label Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants