Skip to content

fix: harden workflow cleanup, replay leases, child provenance, and Web draft recovery - #925

Merged
tt-a1i merged 1 commit into
mainfrom
fix/runtime-and-evidence-boundaries
Nov 28, 2026
Merged

tt-a1i merged 1 commit into
mainfrom
fix/runtime-and-evidence-boundaries

Conversation

@tt-a1i

@tt-a1i tt-a1i commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Problem

Several runtime, replay, permission, and Web projection boundaries had narrow race or recovery gaps:

  1. Workflow worktree reclamation & late-session slot retention: Cancelling a workflow agent while session creation or shutdown was still pending could invoke reclaimWorktree before the child working directory was confirmed quiescent, and retaining an admission lease across late session creation could leak the shared child-execution slot once late disposal succeeded.
  2. Workflow replay TOCTOU & Git read tools: replayLease.canReplay was captured statically before await getResources(...), allowing cache hits after an overlapping unsafe call entered during resource loading. In addition, git_show/git_diff/git_log were treated as replayable without hashing Git index/ref state, and git_log (file parameter) was rejected by the path-only replay filesystem wrapper.
  3. Cross-directory child tool provenance: Child sessions verified sourceInfo on the parent side but forwarded tool names only, allowing a same-named tool from another trusted cwd to satisfy final binding by name alone.
  4. Plan Mode reminder ordering: Context reminder anchors relied on wall-clock timestamps rather than branch entry order and stable message identity, which could invert enable/disable order under equal timestamps, clock skew, or non-destructive context hook edits.
  5. Web compaction-held draft durability & evidence projection: Prompts held in Host memory during compaction cleared Composer drafts before Pi actually admitted them (risking silent loss on Host exit/disconnect), cancelling an active turn bypassed prompt_blocked publication, deleting a session left orphaned compaction draft backups in browser storage, settled/reopened subagent and workflow transcripts dropped nested parentToolCallId links, and tool-arguments performed unbounded JSON.stringify / stripped leading BOMs in paths while Web streamed redundant projectMessage calls when tracing was off.

Value

  • Preserves fail-closed worktree safety without stranding shared childExecutions capacity after late shutdown completes.
  • Prevents stale workflow replay hits across concurrent writes or staged Git changes, and restores git_log execution inside read-only workflow children.
  • Enforces end-to-end tool provenance across parent-to-child boundaries (OP-06).
  • Protects user-authored Web prompts and image attachments until Pi confirms admission, with explicit user-controlled draft recovery and clean storage reclamation on session deletion.
  • Retains nested tool call lineage across live, settled, and reopened child transcripts while bounding argument serialization overhead.

Approach

Workflow / Child / Plan boundaries
├── workflows/runner.ts + controller.ts + index.ts
│   ├── gate reclaimWorktree on confirmed child quiescence
│   └── hold shared admission slot until late session disposal settles, then release
├── workflows/replay-safety.ts
│   ├── evaluate canReplay() dynamically at replay.take() against activeUnsafe & epoch
│   └── execute git_show / git_diff / git_log live instead of caching partial Git state
├── shared/child-session.ts
│   └── carry verified parent tool provenance into bindChildSessionExtensions & replay keys
└── plan-mode/index.ts + persisted-state.ts
    └── anchor reminders by branch order & message identity (preserving redacted hooks)

Web & Evidence projections
├── web/runtime/pi-runtime.ts + web-host.ts + web-store.ts + Composer.tsx
│   ├── distinguish held-for-compaction, blocked, and Pi-admitted prompt states
│   ├── retain per-command draft backups until Pi admission; publish prompt_blocked on cancel
│   ├── clear session-owned backups on confirmed session deletion
│   └── reuse a single projectMessage pass on streaming updates
├── subagent-transcript.ts + subagent-history.ts + workflows/progress-projection.ts
│   └── preserve parentToolCallId, completion status, and missing-parent indicators on settle/reopen
└── shared/tool-arguments.ts
    └── bound JSON preview traversal to 4 KiB and decode UTF-8 with ignoreBOM: true

Validation

  • bun run check — passed (check:config-contract, check:docs-contract, check:discipline, check:web + build:web, format:check, lint, typecheck).
  • bun run test — passed (2,516 Node tests passed, 8 platform-conditional skipped; 1,677 Vitest UI tests passed).
  • Targeted fault-injection & regression suites verified failure before fix and pass after fix across workflow cancellation/slot release, replay lease ABA overlap, live git_log/git_diff staging changes, cross-cwd same-name tool provenance rejection, Plan clock-skew/hook-redaction ordering, compaction draft recovery/cleanup, nested tool lineage, and bounded BOM-safe argument previews.
  • Manual interactive TUI / browser smoke: unverified in this automated run (covered by unit, integration, and Rebuild checks; GitHub Actions Web E2E and cross-platform matrix run on this PR).

Impact

  • User-visible behavior: Compaction-held Web prompts keep recoverable backups until Pi admits them (and surface recovery controls if blocked or disconnected); settled/reopened child transcripts retain nested tool parent links; git_log works inside read-only workflow agents.
  • Model-visible context/tools: Resident 8-tool surface is unchanged; Plan Mode reminders preserve chronological branch order even under same-millisecond or clock-skewed timestamps.
  • Runtime/lifecycle: Workflow worktrees stay preserved when child shutdown is uncertain, and shared admission slots release cleanly once late child disposal completes.
  • Persisted config/data: None for ~/.pi/agent/my-pi-setup.json; workflow replay keys now incorporate verified tool provenance (older cache keys safely miss and re-execute once); browser draft storage cleans up entries for confirmed deleted sessions.
  • Compatibility or risk: Low risk, two-way door; compatible across supported Pi versions (>=0.99.1, tested against locked 1.2.0 and CI endpoint jobs).

@github-actions github-actions Bot added area:workflows Workflow engine, capability, skills, or tests area:subagents Subagent delegation, skills, or tests labels Nov 28, 2026
@tt-a1i
tt-a1i merged commit 97b158f into main Nov 28, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:subagents Subagent delegation, skills, or tests area:workflows Workflow engine, capability, skills, or tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant