Plan: a helper’s approved step goes back to the helper that asked

Closes: #840. Relates to: #662 (a dead helper’s steps), #369 (the one-time pass), #471 (forwarded steps). Branch: fix/helper-step-to-its-helper. Status: Done (2026-11-17) — a helper’s approved step is that helper’s alone, never the parent’s to run; released in 0.165.0

Erratum (2026-11-17): D2’s default cannot read getSubagentsService() when a decision is made. pi-subagents runs its factory again in every helper session and publishes a new, empty service over the parent’s, so the one on globalThis after a helper has started is a helper’s own (seen with a probe: the service captured at the parent’s session start listed both helpers, the one published later listed none). sessionHelperReach therefore takes hold of the service at this session’s start and keeps it. Its regression test is in helper-reach.test.ts.

Context

A helper’s permission ask is forwarded to its parent session and, in a mode that queues, waits in the parent’s /pending queue. Today, when the human approves it:

  • actOnStep (packages/pi-modes/judge-wiring.ts) tells the parent model "Run exactly that action again; it is allowed once". The parent did not ask for it and does not know why it exists. It either refuses to run it or runs another session’s action.

  • The pass is keyed by the action alone (pendingKey, takeApproved in packages/pi-modes/pending-queue.ts), with no owner. Whoever runs that action first takes it, parent or any helper.

  • A pass lapses when the parent’s run settles (onRunSettled), though the helper may still be working.

  • A helper’s steps are removed when the last running helper of its agent type ends (#662, withdrawHelperSteps), because the asker could not be told apart.

What makes a better rule possible, all read in the installed code:

  • A forwarded ask carries forwarding.requesterSessionId, the helper’s pi session id, and forwarding.requesterAgentName (@gotgenes/pi-permission-system, src/authority/forwarded-request-server.ts, buildForwardedAskDetails). The id is "unknown" when the helper’s context gave none (src/authority/forwarder-context.ts).

  • pi-subagents publishes one service per process on globalThis (@gotgenes/pi-subagents, src/service/service.ts): listAgents() returns records with id, status, outputFile and resumeRefusal; steer(id, message) resolves false when the helper is not running.

  • A record’s outputFile is its session file, which pi names <timestamp>_<sessionId>.jsonl. That joins a forwarded ask to its record. A resumed helper continues the same session, so the id holds across a resume.

The operator’s decision (2026-11-17): on approve, the helper that asked is steered to run it again and the pass is its alone; the parent is told only that it happened. If the helper has ended, its step stays listed while it can be resumed, and approving tells the parent to resume that helper, not to run the action itself.

Design

D1. A step and a pass have an owner

normalizeAsk copies forwarding.requesterSessionId onto the ask as requester when it is a non-empty string other than "unknown". AskDetails (packages/pi-modes/judge.ts) gains requester?: string.

pendingKey and the key string are unchanged: other code reads the tool and subject out of the key (stepSubject in packages/pi-modes/standing-approval.ts, the trace’s target). Instead PendingEntry and ApprovedPass gain owner?: string (the requester), and identity becomes the pair: enqueue/put carry owner into the entry and look an existing entry up by key and owner; find(ask) and takeApproved(ask) match key and ask.requester against owner (both absent, or equal). So the same command asked by the parent and by a helper, or by two helpers, is two steps, and a pass is found only by an ask from the same session. approve copies owner to the pass. authorize.ts calls find and takeApproved with the ask as now and is not changed.

A forwarded ask with no usable requester keeps today’s key and today’s rules throughout (D5).

D2. Reaching the helper

New packages/pi-modes/helper-reach.ts:

  • helperOf(requester, records) returns { id?: string; state } for the record whose outputFile ends _<requester>.jsonl:

    • running: status running or queued, or resumeRefusal is still-running or resume-queued (the transient ones: a run is in flight or waiting for a slot; a steer to it is delivered or buffered).

    • resumable: not running and resumeRefusal undefined.

    • gone: any other resumeRefusal (no-session, session-released, workspace-disposed), or no record matches (then with no id).

  • HelperReach: { stateOf(requester): Promise<{ id?: string; state }>; steer(id, text): Promise<boolean> }. The default loads getSubagentsService() by the same awaited import helper-worktree.ts uses (defaultService). No service: every helper is gone.

JudgeDeps gains helpers?: HelperReach, wired in packages/pi-modes/index.ts.

D3. Approve, deny, clear

actOnStep, for an entry with an owner, becomes asynchronous and awaits helpers.stateOf(owner):

Helper is Approve

running

The pass is made (owned). The helper is steered: "The human approved this step: «…». Run exactly that action again; it is allowed once." The parent is told the fact only when its run is in flight (as a steer): "The human approved a step of the helper <agent>: «…». The helper has been told and runs it; do not run it yourself." An idle parent is not woken for it. If the steer resolves false, the row below applies.

resumable

The pass is made (owned). The parent is told: "The human approved a step of the helper <agent>, which has ended: «…». Resume that helper (id <id>) and tell it to run exactly that action again. The approval is the helper’s alone: it does not let you run it."

gone

No pass. The step is removed. The human is notified: "The helper that asked has ended and cannot be resumed; nothing was approved." The parent is told nothing.

Deny: the step is settled as today. A running helper is steered with the declined text; the parent, when its run is in flight, is told the fact. For a resumable or gone helper the parent is told the fact only when its run is in flight.

/pending always (approveAlways, a separate function) saves its rule and approves the step as today; for an owned step its "run exactly that action again" text (alwaysApprovedText) is routed by the same table instead of going to the parent, and for a gone helper the rule is saved, no pass is made and no model is told. /pending clear steers each running owner that its step was cleared and counts only the parent’s own steps in the parent’s text.

The human’s notice line names the helper: "Approved (helper Explore): …".

D4. When an owned step or pass ends

PendingQueue.lapseApprovals() returns and removes only passes with no owner, so a parent’s run settling no longer takes a helper’s pass.

PendingQueue.owners() lists the distinct owners of entries and passes; PendingQueue.dropOwner(owner) removes that owner’s entries and passes and returns { entries, passes }.

A new sweepGoneHelpers(deps) in judge-wiring.ts (deps: state, modes, appendTrace, helpers) awaits each owner’s state and, for each that is gone, calls dropOwner. A removed entry is traced as withdrawHelperSteps traces one today, with "the helper that asked ended and can no longer be resumed". A removed pass is not traced as withdrawn (the human approved it): it is logged (pending.owned_pass_dropped) and, with a UI, notified as "Approval lapsed: «…» — its helper ended and can no longer be resumed."

It is called at three moments: a helper’s end (withdrawHelperSteps, which keeps its own by-agent removal for D5 and then starts the sweep, not awaited, a failure logged); the parent’s run settling (the agent_settled handler beside onRunSettled, whose signature stays; not awaited, a failure logged); and before /pending lists (awaited, in the runPending and openPending closures of registerJudge, which hold the full deps; pendingCommand itself is unchanged). A resumable helper’s step therefore stays listed until pi-subagents releases its session (ten minutes after its result is read, by default), and goes at the next of those moments after that.

PendingQueue.helperEnded(agent) stays for entries with no owner (D5).

D5. A forwarded ask with no requester id

Kept exactly as now: keyed by the action, removed by agent type at #662’s moment, the parent told as today. It is logged (pending.forwarded_unowned) so its frequency can be read. Nothing is guessed from the agent name.

D6. Whose step it is

The /pending pane (packages/pi-modes/pending-pane.ts) and the select rows (PendingQueue.listLines()) show "(helper <agent>)" after a forwarded step’s line. The bus event and the phone’s record are unchanged.

Scope

In: packages/pi-modes/ judge.ts, judge-wiring.ts, pending-queue.ts, pending-pane.ts, helper-reach.ts (new), index.ts, their tests (standing-approval.ts is read, not changed); docs/modules/ROOT/pages/modes.adoc; CHANGELOG.adoc.

Out: the phone’s view of the owner (a pivot-wire record change); dialogs in modes that do not queue; how a helper learns of a typed reply to its step; a change to pi-permission-system or pi-subagents.

Risks

Risk Mode Mitigation

The join by session file name finds no record though the helper runs (a session with no file, a changed file name)

fail-closed

The step reads as gone: no pass, the human is told nothing was approved. The contract test below pins the name against pi’s own SessionManager and pi-subagents' record; pending.owner_not_found is logged with the count of records.

A helper ends between the state read and the steer

fail-closed

steer resolving false falls to the resumable row; the pass stays owned and unusable by anyone else.

An owned pass outlives its helper

fail-closed

Only an ask from the owner’s session can take it; sweepGoneHelpers removes it once the helper is gone; the session’s end drops the queue.

The parent, told to resume, runs the action itself

fail-closed

Its ask has no owner, so the owned pass does not match and it is judged afresh. Pinned by the regression test.

An unowned forwarded ask (D5) still tells the parent to run it

silent

Unchanged behaviour, logged as pending.forwarded_unowned; the measured row before merge reports how many of the forwarded asks seen had no id.

A steer interrupts a helper mid-tool

silent

pi-subagents delivers a steer after the current tool; it is the delivery the parent’s own approvals already use.

Tests

Unit (packages/pi-modes/test/pending-queue.test.ts, helper-reach.test.ts):

  • The same command from the parent and from a helper is two steps; an owned pass is taken by its owner’s ask and by no other (the parent’s, another helper’s). Pins "the pass is its alone".

  • lapseApprovals leaves an owned pass; dropOwner removes that owner’s entries and passes and no other’s.

  • listLines() and the pane’s row carry "(helper <agent>)" for a forwarded step and nothing for the session’s own (D6).

  • helperOf: running, queued, still-running, resume-queued (running, not gone), resumable, each final resumeRefusal, no match, no outputFile.

Regression, the case that would have caught #840 (packages/pi-modes/test/pending-wiring.test.ts):

  • Approving a running helper’s step steers that helper with the run-again text, sends the parent no text telling it to run the action, and the parent’s same command afterwards is not allowed by the pass.

  • Approving an ended, resumable helper’s step tells the parent to resume the helper by id, and the step’s pass is taken when that session asks again.

  • Approving a gone helper’s step makes no pass and tells no model.

  • Deny and clear reach the running helper.

  • /pending always on a running helper’s step: the rule is saved (not refused: the key still yields its tool and subject), the helper is steered, the parent is sent no run-it text.

  • sweepGoneHelpers: a gone owner’s step is traced as withdrawn and its pass is logged and notified as lapsed, not traced as withdrawn; a helper waiting for a resume slot keeps both.

  • An idle parent is sent nothing for a running helper’s approval.

Concurrency: the helper ends between the state read and the steer (the steer resolves false): the parent is told to resume, the pass stands.

Contract (packages/pi-modes/test/helper-reach.test.ts): a session file made by pi’s SessionManager for a known session id is matched by helperOf; a forwarded ask built by pi-permission-system’s buildForwardedAskDetails shape is normalised to requester.

Unowned (D5): a forwarded ask with requester "unknown" behaves as before this change, pinned by the existing #662 tests kept unchanged.

Measured before merge, prediction on file first: in a terminal session on luchs and on panther, one helper asked to run a command the valve queues; approve it; rows: who was steered, whether the helper ran it, what the parent was sent, and the count of forwarded asks with and without a requester id in the diagnostics. Run by hand by the operator or the Windows seat, since a one-shot pi -p cannot serve a helper’s forwarded ask.

Delivery

  1. Plan committed in docs/modules/ROOT/pages/plans/helper-step-to-its-helper.adoc with its nav.adoc line.

  2. One branch: the code and tests, modes.adoc (the /pending section’s helper paragraph rewritten: owner, the three rows, when a step goes), CHANGELOG under Unreleased, Fixed.

  3. node tools/task.mjs validate, full consumer-sim, the measured rows; MR closing #840, squash off, the merge SHA on the issue.

Edit this page · latest