Skip to content

perf(acp): stop the KAS projection repeating work every new chat - #37097

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
perf/kas-projection-pure-reuse
Nov 14, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
perf/kas-projection-pure-reuse

Conversation

@bolichen97

@bolichen97 bolichen97 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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, before session/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 an fsync rewrite 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

  • No cache of the whole projection, of any grant or auto-approval decision, or of anything that reads config.
  • No change to what the projection sends: same agents, servers, permissions and env handling.
  • No change to the kiro-cli backend or to any other harness.

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:

  • Glob matches. batch_guard._read_glob_matches memoizes 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_approved and every decision built on the match are still made fresh.
  • Config scopes. KasHarness.session_extras reads 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.
  • Launch snapshots. kas_env_launch._write_snapshot leaves 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.
  • Sub-agent targets. Each target an agent's availableAgents names 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_agents over 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:

base this branch
In the gateway, 64 servers, median (range) 456 ms (446 to 706) 321 ms (313 to 344)
In the gateway, no servers 66 ms (64 to 100) 40 ms (38 to 70)
Alone, 64 servers 148 ms (147 to 151) 82 ms (77 to 84)
Alone, no servers 46 ms (45 to 51) 29 ms (28 to 33)

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_extras alone 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) and app-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):
    • a 64-server projection gives the same bytes with and without the reuse;
    • a second projection of the same launch does not rewrite its snapshot, and the file's content and 0600 mode are unchanged;
    • a snapshot that is different, missing or group-readable is written again, and a FIFO at its name is refused without waiting for a writer;
    • one session_extras reads the config scopes once;
    • a start with four sub-agent targets reads each target's spec in the name scan, each skill file and each installed app once, and projects the same bytes as a start without the reuse.
  • 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:
    • every cached answer equals the matcher's own;
    • an unreadable glob is not cached;
    • an oversized name is matched and not kept;
    • the memoized function names no module state.
  • 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.py and test/test_member_dispatch_mount.py: their stand-ins for build_kas_custom_agents take 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_matches does not exist there. The same holds for the sub-agent target test (manager_lookup._declared_agents) and test_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 2 and alone with -n 0 in 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

  • 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

@bolichen97
bolichen97 requested review from a team and KiroCrewCaptain as code owners November 14, 2026 05:11
@bolichen97
bolichen97 requested a review from CrysisDeu November 14, 2026 05:12
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 24318a2f34460a08919f7a4eb6776bb2f48d94ed touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 24318a2f34460a08919f7a4eb6776bb2f48d94ed — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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

  • Manual verification covers Linux only. The snapshot skip never runs on Windows, but per-start reuse and the glob memo run on every platform. Attach the promised macOS and Windows timings, or remove that promise from the description.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 24318a2f34460a08919f7a4eb6776bb2f48d94ed — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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

  • Item 4 — one consumer, generalized: one_build() has one caller, in_one_build, and that has one caller, kas.py:1432 (grepped one_build|in_one_build: 1 production site). Its branch "A nested block keeps using the outer block's answers" is never reached by a nested call.
    Subtraction: merge one_build into in_one_build and delete the nested-block branch.

What this change ships

Inventory (8 items) — 7 justified

Intent: make a new chat with many MCP servers start faster by not repeating identical work in the KAS projection. ADDITION (perf), #37041.

  1. Tool-name glob matches are remembered for the gateway's lifetime (4,096 pairs, strings up to 256 characters) — justified
  2. The MCP config scopes are read once per projection, not 2–3 times (scope_muted passed down) — justified
  3. A shared launch snapshot that already holds the exact bytes, as an owner-only file, is no longer rewritten with fsync (POSIX only) — justified
  4. New build_reuse module: a memo that lasts for one session start, with count and size budgets — one consumer, generalized
  5. Each skill file in scope is matched and read once per start across all sub-agent targets — justified
  6. Two targets copying the same skill under the same name write it once — justified
  7. The agent-spec name scan keeps each declared name per start, keyed on the file's identity — justified
  8. Installed-app manifest lookup is kept per start (_declared_agents split out) — justified

[FIRST-PRINCIPLES-REVIEWED] 24318a2

Description read

[DESCRIPTION-READ] 0155cbbd484d528868c73ff1ca12c7c89d885688f13681b84d4df80c68337a52

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 24318a2f34460a08919f7a4eb6776bb2f48d94ed — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 24318a2

Verdict parsed from the review's SHA-scoped output markers for commit 24318a2f34460a08919f7a4eb6776bb2f48d94ed.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 24318a2f34460a08919f7a4eb6776bb2f48d94ed false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 24318a2f34460a08919f7a4eb6776bb2f48d94ed and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 24318a2

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 24318a2f34460a08919f7a4eb6776bb2f48d94ed false-positive: <reason, 20 to 500 characters>

@bolichen97
bolichen97 enabled auto-merge (squash) November 14, 2026 05:20
@bolichen97
bolichen97 force-pushed the perf/kas-projection-pure-reuse branch from a245ffb to a4c143e Compare November 14, 2026 05:31
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 14, 2026
@KiroCrewCaptain

KiroCrewCaptain commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: 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.

  • The glob memo sits on a pure function that takes two strings. Inputs the matcher can't parse and strings over 256 characters bypass it. Grant decisions are still made fresh.
  • The MCP config scopes are now read once per projection. mcp_scope_muted_aliases takes no arguments and never raises. Every new keyword defaults to reading as before.
  • The snapshot-write skip runs under _SHARE_LOCK, the same lock discard_batch holds when it removes files. It opens without following links and without blocking, and checks that the file is regular, owned by this user, owner-only, the right size and byte-identical. Otherwise it writes as before; Windows always writes. Per-process HMAC names mean the boot sweep can't remove a file that was skipped.
  • build_reuse is scoped by a ContextVar that lives only inside the thread running _build. A read that raises keeps nothing. The spec name scan is keyed on file identity, and the skill copy dedup is keyed on target and content hash.
  • The app-agent lookup is equivalent to before, because failures are caught per app.
    Tests cover every chan

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 d984867fc0 · 911 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@bolichen97
bolichen97 disabled auto-merge November 14, 2026 06:15
@bolichen97

Copy link
Copy Markdown
Contributor Author

CI on a4c143e: the reds are not from this change.

  • test/test_search_docs.py::test_search_docs_ranks_the_page_the_query_is_about_first[...how do I run a task in the background] (Backend Tests 3.12 shard 6 and Windows shard 6) fails the same way on main at c28afea when run locally: assert 'task-runner' == 'subagents'. This PR changes no shipped doc and nothing search_docs reads.
  • test/test_denied_commands_security_cost.py::TestSelfModuleIndexIsLinear::test_the_scan_is_linear_not_quadratic (shard 7) missed its time bound by 1 ms (2k tokens took 0.501s). That shard is re-running.
  • Coverage Gate fails closed because the backend tests failed.

The first head's Cross-Platform Portability finding (os.getuid on an added line) is fixed: the owner check now uses platform_compat.effective_uid().

@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 Nov 14, 2026
@bolichen97
bolichen97 force-pushed the perf/kas-projection-pure-reuse branch from a4c143e to 30254af Compare November 14, 2026 08:36
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 14, 2026
@bolichen97
bolichen97 force-pushed the perf/kas-projection-pure-reuse branch from 30254af to dcc4fb9 Compare November 14, 2026 09:43
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 14, 2026
@bolichen97
bolichen97 force-pushed the perf/kas-projection-pure-reuse branch from dcc4fb9 to d984867 Compare November 14, 2026 10:54
@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 Nov 14, 2026
@bolichen97

Copy link
Copy Markdown
Contributor Author

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, apps/manager.py lists the new lookup helper, and the scope-reading tests isolate KIRO_HOME.

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
@bolichen97
bolichen97 force-pushed the perf/kas-projection-pure-reuse branch from d984867 to 24318a2 Compare November 14, 2026 12:04
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Nov 14, 2026

@buluoray buluoray left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
KiroCrewCaptain merged commit dd9d0e3 into main Nov 14, 2026
101 of 105 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the perf/kas-projection-pure-reuse branch November 14, 2026 14:01

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_writer stays green when nonblocking=True is dropped. It releases the blocked open itself after a 10 s join. Record blocked = probe.is_alive() before the release and assert not blocked.
  • The linked and foreign-owner branches of _holds_exactly survive their removal. Two more damage cases (a symlink at the snapshot name, and a patched effective_uid) would pin what the security spec row states.
  • The sub-agent target reuse (build_reuse.py and 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.

buluoray added a commit that referenced this pull request Nov 14, 2026
)

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: passed Eligible automated validation passed for the current revision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: the KAS projection repeats glob matches, MCP scope reads and snapshot writes on every new chat

3 participants