Skip to content

fix(pi): estender os seatbelts de comando ao modo notebook - #1

Merged
shishiv merged 1 commit into
mainfrom
fm/fm-notebook-guard-bridge
Sep 10, 2026
Merged

shishiv merged 1 commit into
mainfrom
fm/fm-notebook-guard-bridge

Conversation

@shishiv

@shishiv shishiv commented Sep 10, 2026 •

Copy link
Copy Markdown
Owner

Summary

Bridge Pi's optional Code/Notebook nested-shell preflight into the existing Firstmate command handler.
The actual evaluated command reaches the unchanged cd and watcher-arm classifiers; no JavaScript parsing, extra daemon, queue, runtime dependency or package installation is introduced.
Ordinary bash, the turn-end guard and watcher implementation remain unchanged.

Bounded protection, not a JavaScript/Deno sandbox: direct runtime subprocess APIs and cwd changes remain outside the nested-tool hook.
This boundary was reproduced, escalated and explicitly accepted for this bridge before implementation.

Behavior

  • Subscribe/request discovery supports either extension load order, replacement and shutdown.
  • exec_command.cmd and the package's legacy command fallback use the same checks and denial reasons as bash.
  • Reject malformed/empty/NUL-bearing nested execution payloads and cells without an active compatible broker.
  • Reject interactive stdin fragments that cannot be classified independently; polling and Ctrl-C remain usable.
  • No provider/model/prompt check controls protection.
  • No personal Pi settings or residual conversion configuration was changed.

Published contract, not guessed tool names

Downloaded with npm pack @howaboua/pi-codex-conversion --ignore-scripts into the isolated task copy.
Inspected version 3.0.31, tarball SHA-1 015717bc4b7cb50877e0fb46fb54ba8dd903aadd.
Paths are within that published tarball:

Contract Source
Outer exec({code: string}); preflight attached to its execution context src/tools/code-mode/public-tools.ts:39-41,65-93
Nested tools.exec_command({cmd,...}) src/adapter/code-mode.ts:119-125
Command schema and legacy preparation src/tools/exec/command-tool.ts:15-23,49-57
Versioned channels and evaluated input src/tools/code-mode/preflight-protocol.ts:3-14
Discovery and denial before invocation src/code-mode-preflight.ts:22-57, src/tools/code-mode/nested-tool-preflight.ts:23-93
Shared invocation boundary src/tools/code-mode/delegate-runtime.ts:277-288
All-provider execution-mode selection src/adapter/activation/runtime-plan.ts:154-187

Red before the fix, green afterwards

Before production edits, a runnable probe imported the actual Firstmate guard plus the published broker, delegate, notebook bootstrap, HTTP bridge and Deno Jupyter kernel.
Actual notebook cells called a harmless shell fixture; the watcher stand-in only wrote a sentinel, never armed a real watcher.

Command: mise exec node@26 -- node .no-mistakes/notebook-guard/red.mjs.
Initial output, exit 1:

watcher background: bash blocked=true; notebook result=ok; shell entered=true
persistent cd: bash blocked=true; notebook result=ok; shell entered=true
direct Deno command: result=ok; shell entered=true (outside nested-tool preflight)
AssertionError [ERR_ASSERTION]: Notebook commands must be denied before shell entry
2 !== 0

Afterwards, both extension orders returned:

watcher background: bash blocked=true; notebook result=error; shell entered=false
persistent cd: bash blocked=true; notebook result=error; shell entered=false
direct Deno command: result=ok; shell entered=true (outside nested-tool preflight)

The committed live regression goes further: real Pi TUI, published Notebook registration/runtime, the actual nested adapter and native shell executor, and the package-pinned Deno.
All four permutations passed: guard first/last × azure-anthropic/claude-opus-5 / azure-openai-responses/gpt-5.6 identities.
Only provider responses are local deterministic fixtures, not Azure calls.

Supervision visibility: observed, not assumed

Item Method and observed result Degradation / boundary
Busy/idle, including a long cell Real Pi lifecycle events drove the existing busy writer; state=busy during a five-second command, state=idle after settlement. One outer cell, not one Pi turn per nested command. The test uses the same event-to-writer contract as generated worker wiring, not an fm-spawn launch.
Turn-end notification Marker absent during the call; real turn_end touched it; the real watcher emitted and queued its signal. No fabricated intermediate completed turns.
Worker status Real nested exec_command appended working and terminal status; the real watcher queued worker.status. Direct runtime effects do not automatically produce status lines.
Panel readability Real tmux captures at 160 columns, details off and on, showed individual nested command summaries and output. Details on additionally showed cell source. Long commands truncate. The package can label blocked commands Ran; denial reasons and absent execution sentinels establish what actually happened.
Queue, lock, crash records A duplicate watcher refused to start. SIGKILL during another active cell preserved queued outcomes and did not fabricate idle. A replacement busy generation rejected the old callback; drain replayed retained events with the required acknowledgement. Full worker relaunch and notebook checkpoint recovery were not tested.
Resolved command visibility Dynamic cmd variables reached both existing classifiers; blocked sentinels remained absent and legitimate commands ran. A published completion observer saw four nested calls. Native Pi tool events saw only exec; direct Deno subprocess/cwd operations were absent from nested completion events.

Additional visibility gap: ordinary notebook updates did not refresh the existing native-harness .progress marker.
Busy/idle still works, but the long-turn age bound is not refreshed by nested activity.
The smallest follow-up is to feed the package's published completion hook into the existing generation-bound progress writer; no daemon or parallel queue is needed.
A long silent cell would still have no intermediate completion to observe.
That worker-observer change is proposed, not silently included here.

Can direct runtime execution also be closed locally?

Approach investigated Evidence Verdict and maintenance cost
Kernel permission flags jupyter-kernel.ts:252-260 uses fixed jupyter --kernel --conn arguments. Real deno jupyter --deny-run rejects the flag. shared-runtime.ts:16-20 exposes only heap size, agent directory and profile. No published permission configuration. Cached binary wrappers are rejected by size/hash validation in deno-binary.ts:38-51.
Bootstrap/prelude interception session-startup.ts:60-81 executes fixed bootstrap then restores state; no extension prelude callback is offered. Wrapping bootstrap or kernel APIs is an internal monkey patch requiring revalidation on updates. Synchronous cwd APIs cannot simply await the shell classifiers.
Runtime permission revocation In real Deno, revoke({name:"run"}) changed granted→prompt, blocked both Deno.Command and node:child_process; permission request then returned denied. Deno.chdir still worked. Possible partial restriction via per-cell source injection, not an official package initialization hook or complete bridge. It disables legitimate direct subprocess libraries, runs after restoration and does not cover cwd changes. Not included.
Other published hooks Preflight is before nested tools; completion observes their outcomes. Neither observes direct runtime system calls. The PR retains the explicitly bounded guarantee rather than claiming total visibility or sandboxing.

Checks and counts

Node 26.8.1, ABI 147; Pi 0.85.1; published package 3.0.31.
Initial low-level reproduction used system Deno 2.9.6; the committed real-Pi matrix used package-pinned Deno 2.9.5, verified against its published SHA-256.
All test-runner invocations used --jobs 1; live cases are serial.

Suite Before: reported ok checks After
arm/pretool 146 147
cd/pretool 13 13
turn-end 87 87
watcher extension 48 48
Pi strict typecheck skipped: tsc absent 1
native Windows invocation skipped: Linux skipped: Linux
test-runner regression, additional validation not run 35
new real Pi/Notebook matrix not present 4

No existing test was deleted or weakened.
The missing compiler was resolved only in the disposable task copy using unpacked TypeScript tooling; no personal installation/configuration was changed.

Final runner totals: 7 scripts, 0 failures, 1 Windows skip, plus 1 live script, 4 permutations, 0 failures/skips.
Also passed: bin/fm-lint.sh (ShellCheck 0.11.0, actionlint 1.7.12), explicit lint of the new shell test, Node syntax check, git diff --check, documentation audience check (97 surfaces / 379 links), and test inventory coverage (195 scripts).
The coverage check needed LC_ALL=C because its existing sort/comm sequence disagrees under the host's en_US.UTF-8; no runner sorting policy was changed.

Broad CI/full-suite execution is not claimed.
Windows, signed Pi, non-tmux backends, credentialed Azure transport, full package entry-point activation and production-home installation were not exercised.
The separate native Code Mode host was not launched; its common published preflight/delegate path was inspected, while the live matrix exercised Notebook Mode.

Repeatable commands and current evidence are recorded in docs/verification/runtime-backends.md, with the transport contract in docs/arm-pretool-check.md and a pointer from docs/cd-guard.md.

@shishiv
shishiv merged commit 7fcc62a into main Sep 10, 2026
shishiv added a commit that referenced this pull request Sep 28, 2026
… box

Merges shishiv/firstmate main (8709a31) into local main (5bab8f1) as a
true merge, so fork/main is an ancestor and publishing to the fork's main
is a fast-forward.

The fork's main was rewritten after the originally named commits:
7fcc62a (#1) and 6cba29f (#2) survive only on fork branches such as
main-antes-20260928. fork/main carries their re-ports, c0ad743 (#1) and
8709a31 (#2), on top of an older age of the Kiro work (26e0caa,
4ae0ca5, aec0779) plus 056b9a0 (.kiro/skills). This accounting is
against those real hashes.

Resolution rule: in every conflicted file whose fork/main content equals
local 28c5629 (the local-side twin of fork aec0779), the local side is
the newer age of the same change, so local wins. That covers
bin/fm-harness.sh, bin/fm-session-lock-lib.sh, bin/fm-primary-endpoint-lib.sh,
bin/fm-kiro-turnend-hook.sh, docs/supervision-protocols/kiro-cli.md,
docs/turnend-guard.md and operational-home-layout/SKILL.md. This keeps the
shared harness/lock ancestry walk (ce770af) and the every-turn ensure
doorbell (92ee7b2, 5bab8f1) intact.

Kiro twins (26e0caa = 43a3714, aec0779 = 28c5629, 4ae0ca5 = d4ea5ad):
- superseded by the local side, except the fork-only pieces below.
- kept from 26e0caa: bin/fm-control.sh passes "$HARNESS" to
  fm_backend_composer_state on exit, which the Kiro reference documents
  (kiro-cli placeholder read as empty only under the recorded identity);
  tests/fm-gotmp.test.sh links fm-kiro-lib.sh.
- discarded: the fork's extra `cp fm-path-lib.sh` in
  tests/fm-primary-endpoint.test.sh; local copies the whole bin tree, which
  already covers it.

056b9a0 (.kiro/skills): kept whole. .kiro/skills is a real tracked
symlink to ../.agents/skills (mode 120000); the kiro.md Skill row and
the fm-kiro-harness test that proves it resolves were re-applied onto
the local versions.

c0ad743 (#1, pi nested notebook shell guard): kept whole. That covers
the .pi turn-end guard preflight with its single session_shutdown release,
bin/fm-test-run.sh routing, docs/arm-pretool-check.md, docs/cd-guard.md,
docs/verification/runtime-backends.md, tests/fm-arm-pretool-check.test.sh
and the opt-in fm-pi-notebook-live e2e and fixture. None of it existed
locally.

8709a31 (#2, process liveness and watcher continuity): kept whole. That
covers bin/fm-branch-outcome-lib.sh and its callers (fm-branch-outcome,
fm-inactive-reconcile, fm-wake-drain), the Pi watch extension and the
turn-end guard's generation-owned restoration and marker rewrite, and the
docs and tests. The only textual conflict was its guardGeneration
declaration beside local's object-shaped LockOwnership; both are kept,
and the merged guard equals fork/main's. The parts #2 had already
dropped for upstream kunchenguid#4191 (Herdr process snapshot, shared process-kind
vocabulary) stay dropped.

Verification: bash -n on all 25 touched .sh files, and shellcheck -x
plus bin/fm-lint.sh are clean. These pass with no failures:
fm-kiro-harness, fm-primary-endpoint, fm-sessionstart-nudge,
fm-omp-harness, fm-afk-return, fm-composer-lib, fm-turnend-guard,
fm-watch-checkpoint, fm-arm-pretool-check, fm-backend-herdr,
fm-branch-supervision, fm-gotmp, fm-inactive-reconcile,
fm-pi-branch-extension, fm-pi-watch-extension, fm-home-summary-refresh
and fm-control. fm-pi-notebook-live-e2e skips because it is opt-in and
no fixture was supplied.
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