Repository navigation
perf(acp): stop the KAS projection repeating work every new chat - #37097
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS No design-level concerns. All three reuses only apply where the inputs are identical, and none lasts longer than one session start. Every decision built on a reused read is still made fresh each time. Notes
[DESIGN-REVIEWED] 24318a2 Description read[DESCRIPTION-READ] 1a79dcbc8a2fc1a6cb9e6b206acabb224e78ee0eb6bd722712de097dc01bdc3a 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 |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks: each reuse targets work the profile measured as repeated with the same inputs, and the projection's bytes are unchanged. One note: the scope helper is built more generally than its one caller needs. Notes
What this change shipsInventory (8 items) — 7 justifiedIntent: make a new chat with many MCP servers start faster by not repeating identical work in the KAS projection. ADDITION (perf), #37041.
[FIRST-PRINCIPLES-REVIEWED] 24318a2 Description read[DESCRIPTION-READ] 0155cbbd484d528868c73ff1ca12c7c89d885688f13681b84d4df80c68337a52 That sha256 names the PR description this verdict read. Recompute it with |
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: |
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: |
a245ffb to
a4c143e
Compare
Code change approver recommendation: A person should decideWhy: the decider estimates a 12% chance that this change introduces a defect, within the 12% limit for approving on its own, but it changes 911 lines, more than the 400 it may approve on its own. This performance PR reuses work in the KAS projection in four ways, and each holds up against the code.
An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit |
|
CI on a4c143e: the reds are not from this change.
The first head's Cross-Platform Portability finding ( |
a4c143e to
30254af
Compare
30254af to
dcc4fb9
Compare
dcc4fb9 to
d984867
Compare
|
CI on d984867: every review lane passes. The remaining reds are not from this change.
Since the last note: sub-agent targets now share one session start's reads of agent specs, skill files and app manifests (bounded, nothing kept past the start), the snapshot check opens without blocking so a FIFO cannot stall it, |
With 64 MCP servers the KAS custom-agent projection still took 433 to 673 ms of a new chat's start on Linux, and 138 to 157 ms on its own. Three parts of it redo the same work every chat with the same inputs: - the resource-glob match behind the auto-approval and hook-coverage checks, about 4,100 calls, is now memoized (lru_cache of 4096 pairs on batch_guard._read_glob_matches; a string over 256 characters is matched uncached). It is a pure function of two strings; a test pins that it reads no module state. Every grant decision built on it is still made fresh. - the MCP config scopes are read once per projection in KasHarness.session_extras and handed to the worker-stub withdrawal, the agent block, the declared names and each sub-agent target, instead of two or more reads. - a shared launch snapshot that already holds exactly its bytes, as this user's owner-only regular file, is left in place instead of rewritten with fsync. Missing, different, linked or not owner-only files are written as before; Windows always writes. An agent that names several sub-agent targets also repeated, for each target, the agent-spec name scan, every skill file read and every installed app's manifest read. One session start now reads each once (build_reuse.one_build around the projection): the spec scan keyed on each file's identity, so a spec written meanwhile is read again; nothing is kept past the start. With 8 targets the targets took 9.1 s before and 0.74 s after. The projection's output is unchanged: tests project a 64-server spec and a several-target agent with and without the reuse and compare the bytes. Closes #37041
d984867 to
24318a2
Compare
buluoray
left a comment
There was a problem hiding this comment.
Approve on 24318a2. This PR makes a new KAS chat start faster by not repeating three kinds of work, and keeps every per-chat input fresh. The only thing kept from one chat to the next is a bounded memo of a pure glob-match function, and a shared launch file is skipped only when it already holds exactly the bytes that would be written. Everything else is reused only inside one session start, through a context variable that is set in the projection's worker thread and cleared when the start ends. The agent spec, tools, MCP servers, allow and deny rules, model, identity and folder are all read again for each new chat. Mutating each reuse guard turns a named test red, except the owner check on the shared launch file, which has no test. The two red test shards on this head are main's tracing tests, which #37171 fixed after this run was created; the PR needs a rebase for them, and the newer head is that rebase. Decisive lines: src/kiro_crew/build_reuse.py:48-80, src/kiro_crew/acp/harness/kas.py:1260-1264, src/kiro_crew/acp/harness/kas.py:1429-1434, src/kiro_crew/acp/kas_env_launch.py:252-299, src/kiro_crew/acp/batch_guard.py:1527-1550, src/kiro_crew/agent_parts/spec_lookup.py:48-74
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Approved. On head 24318a2 the review checked four things. The glob memo sits only on a pure two-string matcher, with bounded size and per-string length, and _auto_approved is still computed fresh on every call. The single MCP scope read per projection keeps the old ordering for the worker-stub withdrawal, and every new keyword defaults to the old self-read. The per-start reuse is scoped to one start inside the worker thread, keyed on file identity, and stores nothing when a computation raises. The new and changed tests also passed when run on this head.
Security: the snapshot skip does not widen anything. The check and the skipped write both run under _SHARE_LOCK. The file is opened without following a link and without blocking. Any doubt (not a regular file, a foreign uid, group or other bits, a size or byte mismatch, or Windows) falls back to the old fsync write. Snapshot names come from a per-process HMAC key, so a file left by an earlier process never matches. No grant decision changes.
Done well: TestOneStartReadsEachFileOnce runs the same start with and without reuse, asserts the projected agents are byte-identical, and counts each read.
Optional:
test_a_fifo_at_the_name_is_refused_without_waiting_for_a_writerstays green whennonblocking=Trueis dropped. It releases the blocked open itself after a 10 s join. Recordblocked = probe.is_alive()before the release and assertnot blocked.- The
linkedand foreign-owner branches of_holds_exactlysurvive their removal. Two more damage cases (a symlink at the snapshot name, and a patchedeffective_uid) would pin what the security spec row states. - The sub-agent target reuse (
build_reuse.pyand its callers) goes beyond #37041 and could have been its own PR. The description also still says "macOS and Windows runs to follow", and its Proof line names an older head.
) With 64 MCP servers the KAS custom-agent projection still took 433 to 673 ms of a new chat's start on Linux, and 138 to 157 ms on its own. Three parts of it redo the same work every chat with the same inputs: - the resource-glob match behind the auto-approval and hook-coverage checks, about 4,100 calls, is now memoized (lru_cache of 4096 pairs on batch_guard._read_glob_matches; a string over 256 characters is matched uncached). It is a pure function of two strings; a test pins that it reads no module state. Every grant decision built on it is still made fresh. - the MCP config scopes are read once per projection in KasHarness.session_extras and handed to the worker-stub withdrawal, the agent block, the declared names and each sub-agent target, instead of two or more reads. - a shared launch snapshot that already holds exactly its bytes, as this user's owner-only regular file, is left in place instead of rewritten with fsync. Missing, different, linked or not owner-only files are written as before; Windows always writes. An agent that names several sub-agent targets also repeated, for each target, the agent-spec name scan, every skill file read and every installed app's manifest read. One session start now reads each once (build_reuse.one_build around the projection): the spec scan keyed on each file's identity, so a spec written meanwhile is read again; nothing is kept past the start. With 8 targets the targets took 9.1 s before and 0.74 s after. The projection's output is unchanged: tests project a 64-server spec and a several-target agent with and without the reuse and compare the bytes. Closes #37041 Hand-resolved backport; original author: Bolin Chen. (cherry picked from commit dd9d0e3)
Problem / Motivation
Goal: A new chat with many MCP servers starts faster, because the KAS agent projection stops redoing the same work every chat while producing the same bytes.
With 64 MCP servers the KAS custom-agent projection (
AcpRuntime._kas_custom_agents, beforesession/new) was the largest Kiro Crew part of a new chat's start on Linux: 446 to 706 ms per chat in the gateway, 147 to 151 ms timed alone. A profile showed three pieces of work every chat repeats with identical inputs: about 4,100 resource-glob matches for the auto-approval and hook-coverage checks, two to three reads of every MCP config scope, and anfsyncrewrite of each stdio server's shared launch snapshot that already holds those exact bytes.Why it matters
Every new chat on the default KAS backend pays this before its first prompt, and it grows with the user's servers. People with many servers wait longest for the first answer.
Not a goal
What changed (motivation → approach → change)
The projection is the agent and MCP server list Kiro Crew builds for the KAS engine before each new chat. It repeated three pieces of work with the same inputs. Each is now reused only where reuse cannot change an answer:
batch_guard._read_glob_matchesmemoizes one readable glob against one resource (4,096 pairs): a pure function of the two strings, pinned by a test. A string over 256 characters is matched uncached._auto_approvedand every decision built on the match are still made fresh.KasHarness.session_extrasreads the MCP config scopes once and hands that reading to the worker-stub withdrawal, the agent block, the declared names and each sub-agent target. Callers that pass nothing still read for themselves.kas_env_launch._write_snapshotleaves a shared snapshot in place when it already holds exactly its bytes as this user's owner-only regular file, opened without following a link. Anything else is written as before; Windows always writes.availableAgentsnames re-read the same files: the agent-spec name scan, every skill file in scope and every installed app's manifest.build_reuse.one_build, around the projection, keeps the first answer for that one start only, within a budget (8,192 answers, 8M characters; past it a read is repeated). The spec scan keeps only each declared name, keyed on the file's identity, so a spec written meanwhile is read again. Every screened read still runs its screen.Linux, packaged installs of the base and of this branch, 64 real public MCP servers, default KAS backend. The "In the gateway" rows are the gateway's own wall time of
_kas_custom_agentsover 6 new chats each; it runs before the first prompt is sent, so the user waits through all of it. The "Alone" rows time the projection by itself, 5 repetitions each:The branch column was measured one revision before the final head. The later changes only bound the memo to 256-character strings (every tool name in the run is shorter) and hand the same scope reading to sub-agent targets, so they cannot make it slower.
The base and branch projections are byte-identical once the install path and the per-process snapshot version (an HMAC under a key each gateway draws at start) are normalized.
Sub-agent targets, Linux,
session_extrasalone for a real agent with 8 sub-agent targets, 183 skills and 42 installed apps, in a copy of that home: the 8 targets took 9.0 to 9.1 s before and 0.75 to 0.80 s after (3 runs each, the after runs beside a running test suite); the projected agents are byte-identical.Specs:
acp-client.md,security.md(the snapshot rule),memory-skills-hooks.md(skill copies) andapp-kit-platform.md(the app-owner lookup).Code:
src/kiro_crew/acp/batch_guard.py(memo);src/kiro_crew/acp/harness/kas.py,src/kiro_crew/acp/kas_agents.py,src/kiro_crew/acp/kas_settings_mcp.py,src/kiro_crew/acp/kas_subagent_targets.py(one reading);src/kiro_crew/acp/kas_env_launch.py(snapshot);src/kiro_crew/build_reuse.py,src/kiro_crew/agent_parts/spec_lookup.py,src/kiro_crew/agent.py,src/kiro_crew/acp/kas_skill_scope.py,src/kiro_crew/apps/manager_lookup.py(per-start reuse).Backwards compatibility
Compatible: every new parameter is keyword-only with a default that keeps today's behaviour, and the projection's output is unchanged.
Removes nothing: the memo and the shared scope reading sit in front of the same readers, and a snapshot is skipped only when the file already holds the exact bytes the write would produce.
Tests
test/test_kas_projection_reuse.py(new):0600mode are unchanged;session_extrasreads the config scopes once;test/test_build_reuse.py(new): nothing is kept outside a block or past it, a read that raises keeps nothing, an answer past the count or weight budget is handed back and not kept, and the spec scan keeps only the name and re-reads a rewritten spec.test/test_batch_guard_glob.py:test/test_kas_subagent_targets.py: the session and its sub-agent target answer from one scope reading, and a scope's mute still reaches the target.test/test_kas_agents_mcp_servers.pyandtest/test_member_dispatch_mount.py: their stand-ins forbuild_kas_custom_agentstake the new keyword.Proof: PROVEN test/test_kas_projection_reuse.py::TestSharedSnapshotWrite::test_a_second_projection_of_the_same_launch_does_not_rewrite_it, test/test_kas_projection_reuse.py::TestOneScopeReadPerProjection::test_a_projection_reads_the_config_scopes_once, test/test_kas_subagent_targets.py::test_the_session_and_its_targets_answer_from_one_scope_reading (head 30254af)
The glob tests fail on the base with an
AttributeError:_read_glob_matchesdoes not exist there. The same holds for the sub-agent target test (manager_lookup._declared_agents) andtest_build_reuse.py(kiro_crew.build_reuse).Determinism: no clock, sleep, port or ordering assumption in the new tests. They were run repeatedly with
-n 2and alone with-n 0in different file orders.Manual verification
Linux numbers in the table above: packaged installs, the shared benchmark lock held around each chat start, memory pressure (PSI
some avg10) 0.00 to 2.03 before each run. macOS and Windows runs to follow.Related Issues
Closes #37041
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)