Fix blocking Pi extension hook dispatch

Merged
AU
4mo ago
+9936-365

Summary

  • replace the generated Pi extension's synchronous spawnSync hook path with an asynchronous, serialized spawn dispatcher so Pi's Node event loop remains responsive
  • bound terminal-feed work globally and per importance/session class while preserving same-session ordering, independent-session progress, lifecycle completion, and failure propagation
  • make Feed acceptance authoritative: validate the live workspace/surface owner, return the accepted canonical target atomically, and repair Pi's cached route when a surface moves
  • compose socket, relay-auth, route-resolution, and acknowledgment work under one monotonic operation deadline
  • keep accepted-event publication and UI callbacks outside the Feed commit lock and off the main actor where required, without allowing a timed-out request to mutate the Feed later
  • bound fallback transcript resolution globally across distinct sessions
  • reject blank explicit Pi targets and malformed compacted markers instead of silently routing or falling through

This may also address the similar freeze/queue symptom in #6507, but that report's exact scenario was not reproduced, so this PR does not close it.

Concrete before/after reproduction

Using Pi 0.73.0 with the xAI grok-3-fast streaming provider:

# origin/main generated extension
$ pi --print --no-session "Reply with exactly: HARNESS OK"
stdout: HARNESS OK
stderr: four cmux hook timeout warnings
elapsed: 28.75s

# hooks-disabled control
$ CMUX_PI_HOOKS_DISABLED=1 pi --print --no-session "Reply with exactly: HARNESS OK"
stdout: HARNESS OK
stderr: empty
elapsed: 9.87s

# PR generated extension
$ pi --print --no-session "Reply with exactly: HARNESS OK"
stdout: HARNESS OK
stderr: empty
exit: 0
elapsed: 4.63s

The prior Pi extension and settings were restored byte-for-byte after the run.

Red/green regression evidence

The behavioral Bun harness fails against the original synchronous dispatcher:

origin/main: FAIL: Pi hook dispatch was blocking or concurrent

The final branch passes:

PASS: Pi dispatch stays responsive, serialized, and fails stale surfaces once
PASS: generated Pi extension installs and emits cmux hooks

Earlier hosted red proofs captured ownership and total-deadline failures:

  • 30155091444, commit 6105ca937d: the batch-snapshot race delivered only 5 of 64 payloads.
  • 30155092309, commit b085ba9c05: a slowly drained request exceeded one operation deadline and blank explicit Pi targets were accepted.

The corrected final test-only proof run 30165033218, commit e05f5dd077, keeps the new tests while omitting the corresponding final source fixes. It records the intended failures, including:

positiveTimeoutAcceptanceCallbackCompletesBeforeAcknowledgedCallerReturns()
  callback/publication ordering and acknowledgment expectations failed

positiveTimeoutReturnsAuthoritativeEventWhenMainActorCallbackStalls()
  authoritative acknowledgment expectation failed

blockingAcceptanceCallbackCompletesBeforeResolvedCallerReturns()
  blocking publication ordering expectations failed

distinctSessionFallbackResolutionHasGlobalAdmissionBound()
  fallback scans for distinct sessions must have a global admission bound

The final red/green commit pairs are:

  1. f2ba6498da / 6b152c219a — deadline composition coverage/fix
  2. 8b8083725f / 24c8eecf05 — stalled callback coverage/lock-scope fix
  3. 8288228764 / 0cd0465711 — authoritative handoff coverage/fix
  4. baa6c92bc9 / e4064278ed — cross-session isolation coverage/fix
  5. 84c91727a1 / e1796976c8 — bounded fallback/publication coverage/fix
  6. c378b4fb93 — correct the final test semaphore's synchronization scope

The temporary red-proof branch was deleted after the hosted evidence was captured.

Exact-head verification

Final pushed HEAD: c378b4fb93a591eef13742435c0d82c2c7c984d2

  • origin/main is an ancestor of HEAD; merge-conflict gate is clean
  • tagged Debug build: pass (issue-8672-route)
  • exact tagged-binary Pi dispatcher harness: pass
  • exact tagged-binary compacted Feed suite: pass
  • exact tagged-binary socket operation deadline suite: pass with ambient cmux socket credentials/context scrubbed
  • strict determinism guard: 0 findings
  • pbxproj test-wiring guard: pass (593 files)
  • project normalization, workspace package grouping, and SwiftPM lockfile policy guards: pass
  • Swift parsing/Python syntax and git diff --check: pass
  • worktree is clean and matches the pushed branch

Exact-head CI run 30164877041:

  • all four app-host unit-test shards: pass (shard 4 passed on rerun after its first attempt failed before checkout because the runner could not resolve github.com)
  • workflow guards, Linux preflight, web/typecheck/database, React/webviews, remote daemon, sidecar, and agent-session resources: pass
  • tests-build-and-lag: the complete test build succeeds, then the repository-wide warning budget fails on four files untouched by this PR (AppDelegate+WindowDock.swift, ContentView.swift, MobileTerminalRenderGridAnchorRegistry.swift, and SidebarGroupHeaderRowView.swift)
  • swift-package-tests: package compilation reaches final link, then fails because the CI Ghostty static library lacks _ghostty_surface_render_grid_json_v2

The latter two failures are current-main/toolchain infrastructure signatures; they are recorded here rather than hidden or represented as green.

Review closeout

Canonical semantic autoreview on exact HEAD returned:

{
  "findings": [],
  "overall_correctness": "patch is correct",
  "overall_confidence": 0.90
}

Greptile's exact-head check succeeded; its substantive summary rates the patch 5/5 and safe to merge. GitHub reports zero unresolved review threads.

The canonical wrapper itself exits nonzero because its static cmux policy phase reports 12 matches. Those matches were individually triaged and intentionally retained:

  • five file-organization matches are private state/queue helpers or tightly namespaced result DTOs whose ownership is local to Feed ingress
  • the lock/semaphore/main-sync matches implement the synchronous socket-to-main-actor bridge: admission locks guard only short state transitions, never wait or execute event work; runtime waits have explicit deadlines; the main-queue hop enters the main-actor Feed store
  • semantic review plus the deadline, cancellation, publication-order, cross-session, and red/green harnesses exercise those boundaries

This is an explicit owner-documented exception, not a claim that the static wrapper exited 0. An actor rewrite or new micro-package is intentionally outside this bug fix.

Localization audit

The changed user-facing surfaces are CLI/socket and feed.push errors. Resources/Localizable.xcstrings parses successfully; all 12 added keys have translated English and Japanese values. Added localized references were cross-checked against the catalog, and the final review commits add no user-facing text.

Closes #8672