Skip to content

fix(sandbox): backport the scope probe PATH and bus-path fixes to release/0.8.0 - #15638

Merged
bolichen97 merged 2 commits into
release/0.8.0from
backport/0.8.0-scope-probe-no-path-walk
Oct 1, 2026
Merged

bolichen97 merged 2 commits into
release/0.8.0from
backport/0.8.0-scope-probe-no-path-walk

Conversation

@bolichen97

@bolichen97 bolichen97 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Backport of #15636 to release/0.8.0 (cherry-pick -x, clean). release/0.8.0 already carries #15622 through #15624. Draft until #15636 merges on main.

Goal: The cgroup-scope probe added in #15622 never stalls the gateway's event loop on a hung PATH entry, and never drops the cgroup ceiling because a D-Bus bus address was percent-escaped.

#15622 merged with two blocking findings from its GPT 5.6 review still open, both in code that PR added:

  1. The probe now refreshes on a 60s TTL and on session changes, and each refresh ran shutil.which("systemd-run") on the event loop. That is a walk of every PATH entry, and one entry on a stalled network mount freezes the loop and the liveness heartbeat.
  2. _user_bus_socket_paths dialed DBUS_SESSION_BUS_ADDRESS unix:path= values literally. The D-Bus spec lets any byte be %XX-escaped, so an escaped address that names a bus outside XDG_RUNTIME_DIR probed as unreachable, and spawns ran without the fork-bomb/memory ceiling.

Why it matters

(1) is a gateway-wide freeze that ends in a watchdog kill and a crash loop on hosts with automounted or network PATH entries. (2) silently drops a security ceiling on hosts whose bus address is escaped.

Not a goal

What changed (motivation → approach → change)

  • _compute_cgroup_scope_probe resolves systemd-run through the new platform_compat.trusted_system_bin_quiet, a handful of stats over fixed system directories. This is the lookup cgroup_scope_argv already uses to pin the wrapper it prepends, without its miss diagnostic, which itself walks PATH with shutil.which; the review's second round found that walk. trusted_system_bin now delegates to it and keeps the diagnostic, so its behaviour is unchanged. The probe's answer also matches what the wrapper can actually use. Before, a systemd-run found only on PATH made the probe answer "available", and the wrapper then refused it.
  • _user_bus_socket_paths percent-decodes each unix:path= value (urllib.parse.unquote_to_bytes → os.fsdecode) before it is stat'ed and dialed. A value that decodes to an empty path or contains a NUL cannot name a socket and would make os.stat raise ValueError on the spawn path, so it is skipped.

Backwards compatibility

Compatible: on a host whose systemd-run is in a trusted directory and whose bus address is unescaped, the probe's answer is unchanged. A host with systemd-run only on PATH already got no ceiling from the wrapper. It now also gets a probe answer that says so, which also stops the bus locators from being forwarded to a spawn that is not wrapped.

Tests

test/test_cgroup_scope_probe_liveness.py:

  • test_a_percent_escaped_bus_address_is_decoded: the only live bus is reachable through an escaped unix:path=. Red without the decode.
  • test_a_probe_refresh_never_walks_path: shutil.which raises if called, and a TTL refresh must still answer (True, "ok"). Red with shutil.which restored.
  • test_a_probe_miss_never_walks_path: no trusted systemd-run and platform_compat.shutil.which raises; the probe must answer unavailable without walking PATH. Red with the diagnosing trusted_system_bin.
  • test_a_bus_address_naming_no_socket_never_breaks_a_spawn (%00, an embedded %00, and an empty path=): the probe and wrapper still work off the XDG_RUNTIME_DIR bus. Red without the NUL filter.

All 17 tests in the file pass. test_sandbox_argv, test_per_runtime_process_resources, test_sandbox_agent_slice_identity and test_session_scope_reap pass on release/0.8.0 (285 in test_sandbox_argv + test_session_scope_reap). test_platform_compat and every test that references trusted_system_bin pass (6296). The one failure, TestPeakRssIsThisProcesss, fails identically on unmodified main on this host. flake8, isort, mypy (src/kiro_crew, 0 issues), black, docs-lint and the comment-history gate are clean. docs/system-specs/common/platform-compat.md gains a row for the quiet lookup.

Manual verification

N/A — unit coverage sufficient: both changes are pure functions of the environment and the fixed system directories, and both tests exercise them through the real probe against a real listening socket.

Related Issues

Backport of #15636. Follow-up to #15622 / #15624 (its GPT 5.6 review findings F1 and F2). Refs #11473. Internal report: https://t.corp.amazon.com/187b5c6e-b5cb-4d1b-9c36-ccf8c598e4f4

Pattern harvest

Rule candidate: review-prompt
Pattern: a once-per-process lookup that becomes periodic (TTL or fingerprint refresh) inherits the event-loop budget of every call, so PATH walks and other unbounded filesystem lookups inside it must be swapped for fixed-directory resolution.

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

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of 67269be3962247fa0462017beaffad58a802b6e4 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Verify the cherry-picks match what finally merged on main in #15636 — the PR was drafted before its second-round review there concluded.

[DESIGN-REVIEWED] 67269be

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of 53fc823f540e851ad322c138cb49579cba5506c2 — 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/CONCERNS are advisory.

First-Principles-Verdict: PASS

Verify the cherry-pick is byte-identical to #15636 as merged on main — a diverged backport is the only risk this diff carries.

What this change ships

Inventory (6 items) — 6 justified

Intent: FIX — stop the cgroup-scope probe refresh from hanging the event loop on a PATH walk, and stop a percent-escaped D-Bus address from silently dropping the cgroup ceiling (backport of #15636).

  1. A probe refresh resolves systemd-run from fixed system directories, never PATH — justified
  2. A host with systemd-run only on PATH now gets probe answer "unavailable" (wrapper already refused it) — justified
  3. A percent-escaped unix:path= bus address now reaches the live bus, keeping the ceiling — justified
  4. An address decoding to empty or NUL is skipped instead of raising on the spawn path — justified
  5. New callable trusted_system_bin_quiet; trusted_system_bin delegates to it — justified
  6. New platform-compat.md spec row for the quiet lookup — justified

Counts run: trusted_system_bin_quiet has 2 non-test consumers (trusted_system_bin at platform_compat.py:2964, probe at sandbox.py:14471). Sibling check: the spawn-path trusted_system_bin("systemd-run") diagnostic walk (sandbox.py:14963) is gated by the probe at 14947, so the fix closes it too — 0 unfixed siblings. The decode's twin (pod/runtime.py:421) already decodes; the sandbox spelling is byte-correct and NUL-guarded, meaningfully different.

[FIRST-PRINCIPLES-REVIEWED] 53fc823

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of 53fc823f540e851ad322c138cb49579cba5506c2 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 53fc823

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 53fc823f540e851ad322c138cb49579cba5506c2: <one-sentence reason>

@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 Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review — ✅ no blocking findings

Reviewed 53fc823f540e851ad322c138cb49579cba5506c2 — this comment is updated in place on each push.

Review details

unquote_to_bytes makes _user_bus_socket_paths raise on a non-UTF-8 bus address, where it previously could not raise at all.

FINDING — src/kiro_crew/sandbox.py:14320 — decoded = urllib.parse.unquote_to_bytes(value) passes a str into _unquote_impl, which does a strict string.encode('utf-8'), so a DBUS_SESSION_BUS_ADDRESS holding any non-UTF-8 byte (decoded by CPython into a lone surrogate via surrogateescape) raises UnicodeEncodeError — a ValueError, caught by nothing on sandboxed_spawn_argv → cgroup_scope_bus_env/cgroup_scope_argv → _probe_cgroup_scope → _cgroup_scope_session_fingerprint → _user_bus_socket_paths — so every sandboxed spawn fails instead of degrading, where the pre-change line appended the raw value and only os.stat/connect_ex touched it (both surrogate-safe) → Fix: decode bytes, not text — urllib.parse.unquote_to_bytes(os.fsencode(value)).

[OPUS-REVIEWED] 53fc823

Verdict parsed from the review's SHA-scoped output markers for commit 53fc823f540e851ad322c138cb49579cba5506c2.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 53fc823f540e851ad322c138cb49579cba5506c2: <one-sentence reason>

@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 Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Fable 5) — ⏭️ skipped

Revision 53fc823f540e851ad322c138cb49579cba5506c2 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

Two defects the post-logout probe fix (#15622) introduced, from its
review:

- The probe now refreshes on a TTL, and every refresh resolved
  systemd-run with shutil.which on the event loop. A PATH entry on a
  stalled mount would hang the gateway's loop. The probe now uses
  platform_compat.trusted_system_bin, the same fixed-directory lookup
  cgroup_scope_argv already pins its wrapper with.
- DBUS_SESSION_BUS_ADDRESS unix:path= values may percent-escape any
  byte. Probing the escaped spelling literally misses a live bus that
  lives outside XDG_RUNTIME_DIR and drops the cgroup ceiling. The value
  is now decoded before it is dialed.

(cherry picked from commit e5a68df)
(cherry picked from commit 623f2b4)
Second-round review of this PR: trusted_system_bin's miss diagnostic
still calls shutil.which, so a host with no trusted systemd-run walked
PATH on the event loop at the first probe refresh. The probe now uses
platform_compat.trusted_system_bin_quiet, the same fixed-directory
lookup without the diagnostic, and reports the miss in its reason.

A D-Bus unix:path= value that decodes to an empty path or one holding
a NUL cannot name a socket and made os.stat raise ValueError on the
spawn path; such values are now skipped.

(cherry picked from commit 5843231)
@bolichen97
bolichen97 force-pushed the backport/0.8.0-scope-probe-no-path-walk branch from 67269be to 53fc823 Compare October 1, 2026 01:52
@bolichen97
bolichen97 marked this pull request as ready for review October 1, 2026 02:56
@bolichen97
bolichen97 requested a review from a team as a code owner October 1, 2026 02:56
@bolichen97
bolichen97 requested review from dodgeblaster and removed request for a team October 1, 2026 02:56
@bolichen97
bolichen97 merged commit c57489e into release/0.8.0 Oct 1, 2026
21 of 23 checks passed
@bolichen97
bolichen97 deleted the backport/0.8.0-scope-probe-no-path-walk branch October 1, 2026 02:56
@github-actions github-actions Bot removed the readiness: checking Automated validation is still running label Oct 1, 2026
bolichen97 added a commit that referenced this pull request Oct 1, 2026
… release/0.8.0 (#15619)

* fix(windows): backport the directory-junction hardening to release/0.8.0 (#15666)

Backport of #14327, #14962, #15310 to release/0.8.0, cherry-picked in main's merge order.

* fix(windows): anchor six directory-link fences to pinned descriptors (#14327)
  (cherry picked from commit 6b0a6d5)
* fix(config): refuse a link or junction at the workspace dir name (#14962)
  (cherry picked from commit 4bb50ef)
* fix(windows): make four is_symlink guards see a junction (#12379, #10492) (#15310)
  (cherry picked from commit 6d54165)

* style(test): black-format the failed-start reap test and drop its history wording on release/0.8.0 (#15665)

test/test_acp_runtime_failed_start_reap.py arrived with #15625 not
black-formatted (fails scripts/check_black_formatting.py on the release
tip), and the _gone docstring said "no longer runs", which
scripts/check_comment_history.py flags as history narration. Black 26.3.1
(--target-version py310) reflow, and the docstring now reads "has stopped
running", the wording the #15664 forward-port uses. Test file only.

* fix(adaptive): keep a same-window gate step from eating idle recovery (#15595)

Idle recovery (#15541) took its exec step only when nothing else had
changed on the sample. A spawn-gate increase earned on the same sample
set that shared flag, so the idle exec step due on it was skipped, and
the gate step also restarted the one-increase-per-window clock: the
exec cap then waited a further window for a step it had already been
due.

Judge the idle step on the exec track alone. Both tracks keep sharing
one increase-per-window clock across samples, as the earn rules do.

An increase's reason now names each track it moved: the exec note only
when the exec cap moved, and every gate step as "spawn gate target
X -> Y" (the policy's target; the daemon confirms or clamps it). A
gate-only step used to read "clean window earned +1", and a gate step
beside an exec step was not mentioned at all.

(cherry picked from commit f23a39f)

* fix(acp): treat only JSON whitespace as an empty mcp.json (#15673)

#15627 read an empty or whitespace-only MCP settings file as absent, using
bytes.strip() with no argument. That also strips \x0b and \x0c, which JSON
does not allow, so a file holding only those bytes read as "no restrictions"
instead of failing closed like any other malformed document. The emptiness
check now strips only space, tab, CR and LF (RFC 8259); anything else goes
to the parser and fails closed as before #15627.

Refs #15616

(cherry picked from commit 157fd51)

* fix(sandbox): backport the scope probe PATH and bus-path fixes to release/0.8.0 (#15638)

* fix(sandbox): keep the scope probe refresh off PATH, decode bus paths

Two defects the post-logout probe fix (#15622) introduced, from its
review:

- The probe now refreshes on a TTL, and every refresh resolved
  systemd-run with shutil.which on the event loop. A PATH entry on a
  stalled mount would hang the gateway's loop. The probe now uses
  platform_compat.trusted_system_bin, the same fixed-directory lookup
  cgroup_scope_argv already pins its wrapper with.
- DBUS_SESSION_BUS_ADDRESS unix:path= values may percent-escape any
  byte. Probing the escaped spelling literally misses a live bus that
  lives outside XDG_RUNTIME_DIR and drops the cgroup ceiling. The value
  is now decoded before it is dialed.

(cherry picked from commit e5a68df)
(cherry picked from commit 623f2b4)

* fix(sandbox): resolve the probe's systemd-run without any PATH walk

Second-round review of this PR: trusted_system_bin's miss diagnostic
still calls shutil.which, so a host with no trusted systemd-run walked
PATH on the event loop at the first probe refresh. The probe now uses
platform_compat.trusted_system_bin_quiet, the same fixed-directory
lookup without the diagnostic, and reports the miss in its reason.

A D-Bus unix:path= value that decodes to an empty path or one holding
a NUL cannot name a socket and made os.stat raise ValueError on the
spawn path; such values are now skipped.

(cherry picked from commit 5843231)

* fix(chat): restore the interrupted turn when Resume lands on a natively loaded session (#15643) (#15644)

* fix(chat): restore the interrupted turn when Resume lands on a natively loaded session (#15643)

kiro-cli appends a prompt to <sid>.jsonl only once the model has answered
it. When the process serving a turn dies mid-answer (a gateway restart, a
crash, a recycled runtime) that request is never written, so session/load
brings the conversation back without it. A natively resumed session gets
no Kiro Crew replay, so the Resume press on that turn sent a bare "finish
the user's most recent request" with the request missing: the model
answered the turn before it, or said there was nothing to continue.

Reproduced against kiro-cli 2.26.1: after a completed turn and a second
turn killed mid-stream, session/load restores only the first. The path is
any native resume: a dashboard chat with agent.tool_search off, a
dashboard chat linked to a channel, and the automatic connection-lost
recovery on a respawned runtime.

When the Resume message or the connection-lost recovery runs on a session
that this turn natively resumed, _run_chat now prepends the interrupted
turn from the slot's own transcript (the opener the turn-in-flight marker
restores, plus the partial answer), bracketed [INTERRUPTED TURN] so the
context scanner attributes it. A cold start that replays already carries
the turn and is unchanged. A session whose only turn was interrupted was
already covered: SessionMap.get prunes a sid whose log holds no turn.

Backport of #15643 to release/0.8.0. Conflict: context.py still carries
compress_thread_history here (removed on main by #14952); the new helper
is placed after it.

* fix(chat): open the interrupted-turn restore at the right row, and mint it after the scrub (#15643)

A cron, app or synthesis delivery opens its turn as an inject row. The
interrupted-turn walk only stopped at user, nudge and sub-agent rows, so
for such a turn it reached the already-answered request before it and
restored that as the user's most recent request. Openers now include the
inject kinds dashboard.state.TURN_OPENING_INJECT_KINDS names (the
dispatching kinds minus recovery and user_replay, which continue a turn).

The session-busy recovery (_BUSY_RECOVER_MSG) also resets the session and
session/loads the same native conversation, so it gets the restore too.

The frame tells the model the request inside it is the one to carry on
with, so its markers join _STRUCTURAL_MARKER_RES and the runner prepends
it after the egress scrub, like the task checklist; the builder scrubs
the restored payload itself.

The turn-in-flight marker now copies a sub-agent completion opener too
(_LOCAL_TURN_OPENER_ROLES / _LOCAL_TURN_PROMPT_ROLES): without it a crash
before the flush loses that row, and the restore would walk past it to
the previous, already-answered request.

* fix(mcp): repair Windows command casing at every resolver, not only the rewriter

On Windows `shutil.which` spells the extension it appends exactly as
PATHEXT spells it, so a bare `demo-mcp` resolves to `demo-mcp.EXE` while
the file on disk is `demo-mcp.exe`. A launcher that dispatches on its own
argv[0] basename case-sensitively (a tool manager's multiplexer shim)
refuses to run under that spelling and the MCP server never starts.

The rewriter already restored the on-disk basename, but the agent-config
resolver (`agent._resolve_command`) and the dashboard probe
(`mcp_discovery`) consumed raw `which` output, and the resolver's result is
PERSISTED as the spec's absolute `command`. An absolute command is accepted
verbatim on every later pass, so the uppercase spelling written once looked
operator-authored to the rewriter from then on and its repair never ran.

Move the helper to `kiro_crew.env.resolved_command_casing`, beside the
`mcp_search_path` the three resolvers already share, and apply it on both
return paths of `_resolve_command` and in the probe. Repairing at the
resolver also keeps the provenance record's `emitted` value repaired, so
`command_is_ours` still recognises the entry and re-derivation stays on.

Fixes #11627

(cherry picked from commit aa7c0bd, #15071;
 applied as its pre-#14919 revision 457210d plus the round-2 test
 and docstring delta, since release/0.8.0 predates the agent.py split)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant