Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion harness-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ alongside `harness-node` over the iii bus.
| Folder | Bus surface | Role |
|---|---|---|
| `src/harness/` | `harness::status`, `ui::subscribe`/`unsubscribe`, `harness::fs::read_inline`, `policy::check_permissions` | Meta-worker; loads `iii-permissions.yaml`; spins up `ui::*` fanout pumps. |
| `src/approval-gate/` | `approval::resolve`, `policy::approval_gate` (subscriber) | Consults policy + pause-and-wait approval flow. |
| `src/approval-gate/` | `approval::resolve` | Routes operator decisions to per-call `turn::approval_resume` fns (registered by turn-orchestrator). |
| `src/turn-orchestrator/` | `run::start`, `run::start_and_wait`, `agent::call`, `turn::step` | Durable FSM driving each agent turn; chokepoint dispatcher. |
| `src/session/` | `session-tree::*` (11 fns), `session-inbox::*` (3 fns) | Branching session storage + per-session inbox queues. |
| `src/llm-budget/` | `budget::*` (14 fns) | Workspace + agent LLM spend caps. |
Expand Down
5 changes: 0 additions & 5 deletions harness-node/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ permissions_path: "./iii-permissions.yaml"
sync_default_timeout_ms: 120000
system_default_skills:
- "iii://iii-directory/index"
policy_function_id: "policy::check_permissions"

# approval-gate
approval_gate:
approval_state_scope: "approvals"

# session worker
session:
Expand Down
56 changes: 22 additions & 34 deletions harness-node/docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ workers.
|---|---|---|---|
| harness | [src/harness/](harness-node/src/harness/) | Meta-worker; loads `iii-permissions.yaml`, exposes `harness::call` (WS ingestion bridge — see [Telemetry & trace correlation](#telemetry--trace-correlation)) / `harness::status` / `policy::check_permissions` / `ui::*`, spins up `agent::events` fan-out. | [workers/harness.md](harness-node/docs/workers/harness.md) |
| turn-orchestrator | [src/turn-orchestrator/](harness-node/src/turn-orchestrator/) | Durable FSM driving each agent turn; chokepoint dispatcher for `agent::call`. | [workers/turn-orchestrator.md](harness-node/docs/workers/turn-orchestrator.md) |
| approval-gate | [src/approval-gate/](harness-node/src/approval-gate/) | Owns approval state. Registers `approval::resolve` and a state-trigger adapter on the `approvals` scope that wakes `turn::step` on every decision write. | [workers/approval-gate.md](harness-node/docs/workers/approval-gate.md) |
| approval-gate | [src/approval-gate/](harness-node/src/approval-gate/) | Registers `approval::resolve` and shared approval wire schemas; routes decisions to per-call `turn::approval_resume` fns owned by the turn-orchestrator. | [workers/approval-gate.md](harness-node/docs/workers/approval-gate.md) |
| session | [src/session/](harness-node/src/session/) | Branching session storage (`session-tree::*`) plus per-session inbox queues (`session-inbox::*`). | [workers/session.md](harness-node/docs/workers/session.md) |
| llm-budget | [src/llm-budget/](harness-node/src/llm-budget/) | Workspace + agent LLM spend caps with alerts, forecast, period rollover. | [workers/llm-budget.md](harness-node/docs/workers/llm-budget.md) |
| hook-fanout | [src/hook-fanout/](harness-node/src/hook-fanout/) | Generic publish-and-collect primitive over a stream topic. | [workers/hook-fanout.md](harness-node/docs/workers/hook-fanout.md) |
Expand Down Expand Up @@ -68,9 +68,9 @@ flowchart LR
turnOrch -- "state::* persistence" --> state

client -- "approval::resolve" --> approval
approval -- "state::set approvals/<sid>/<cid>" --> state
state -- "state trigger (scope=approvals)" --> approval
approval -- "iii.trigger turn::step" --> turnOrch
approval -- "trigger turn::approval_resume::<sid>/<cid>" --> turnOrch
turnOrch -- "state::set approvals/<sid>/<cid>" --> state
turnOrch -- "iii.trigger turn::step" --> turnOrch

provAnth -- "auth::get_token" --> auth
provOAI -- "auth::get_token" --> auth
Expand All @@ -89,9 +89,9 @@ flowchart LR
defines an 11-state durable FSM. Every transition is driven by the
`turn::step` durable subscriber, which is woken by a publish to the
`turn::step_requested` topic — either by the orchestrator itself
(re-publish at the end of a step), by the approval-gate's
`approvals` state trigger (when a human decision lands), or by the
orchestrator's own `abort_signal` state trigger.
(re-publish at the end of a step), by a per-call
`turn::approval_resume` handler (when a human decision or abort lands), or by
the orchestrator's own `abort_signal` state trigger.

```mermaid
stateDiagram-v2
Expand All @@ -117,9 +117,10 @@ stateDiagram-v2

The orchestrator consults `policy::check_permissions` directly inside
`consultBefore` — `allow`, `deny`, or `pending`. There is no hook fanout on
the before path; resume is driven by a `state` trigger on the `approvals`
scope. The orchestrator parks the turn in `function_awaiting_approval` until
that trigger fires and re-triggers `turn::step`.
the before path. The orchestrator parks the turn in `function_awaiting_approval`,
registers a `turn::approval_resume` function per pending call, and waits until
`approval::resolve` (or abort) triggers that function, which persists the
decision and invokes `turn::step`.

```mermaid
sequenceDiagram
Expand All @@ -138,9 +139,9 @@ sequenceDiagram
Harness-->>Turn: needs_approval → park in function_awaiting_approval
Note over Turn,Bus: Orchestrator stops re-publishing turn::step_requested.<br/>The TurnStateRecord.awaiting_approval list pins the open calls.
User->>Gate: approval::resolve(decision, reason)
Gate->>Bus: state::set approvals/<sid>/<cid> = {decision, reason}
Note over Gate,Turn: state trigger on scope=approvals<br/>(approval::is_decision_write → approval::on_decision_written)<br/>directly triggers turn::step
Bus-->>Turn: turn::step wakes → function_awaiting_approval reads<br/>approvals/<sid>/<cid> for each pending entry
Gate->>Turn: trigger turn::approval_resume::<sid>/<cid>
Turn->>Bus: state::set approvals/<sid>/<cid> = {decision, reason}
Turn->>Turn: turn::step → function_awaiting_approval reads<br/>approvals/<sid>/<cid> for each pending entry
Turn->>Turn: fold decisions into prepared snapshot,<br/>transition back to function_execute
end
```
Expand All @@ -150,9 +151,8 @@ Fail-closed: policy unreachable (transport error or 5 s timeout) →

Abort: `router::abort` writes `session/<sid>/abort_signal = true` (waking
the orchestrator through its own `agent`-scope state trigger) and, if the
turn is paused on approvals, also writes one
`{decision: 'aborted', reason: 'session_aborted'}` record per pending
call so the approvals state trigger releases the parked step.
turn is paused on approvals, triggers each registered
`turn::approval_resume` function with `{decision: 'aborted'}`.

## Kernel deny list

Expand All @@ -162,24 +162,12 @@ it at boot and watches for changes via `chokidar`. Rules are scanned
top-to-bottom; first match wins. Kernel rules below are shipped by default
and protect the gate / state surface.

| Rule id | Function | Action | Why |
|---|---|---|---|
| `kernel/no-self-approve` | `approval::resolve` | deny | An agent must not flip its own pending approval. |
| `kernel/no-self-policy` | `policy::check_permissions` | deny | Don't let the model trace or precompute its own gate decision. |
| `kernel/no-self-hook` | `hook-fanout::publish_collect` | deny | The hook primitive is operator-only. |
| `kernel/no-state-set` | `state::set` | deny | Use a worker function; never raw state writes. |
| `kernel/no-state-update` | `state::update` | deny | Same. |
| `kernel/no-state-delete` | `state::delete` | deny | Same. |
| `kernel/no-stream-set` | `stream::set` | deny | Bypasses the agent loop. |
| `kernel/no-durable-publish` | `iii::durable::publish` | deny | Bypasses the durable hook plane. |
| `kernel/no-auth-set` | `auth::set_token` | deny | Token plumbing is operator-only. |
| `kernel/no-auth-delete` | `auth::delete_token` | deny | Same. |
| `kernel/no-oauth-anthropic-login` | `oauth::anthropic::login` | deny | Same. |
| `kernel/no-oauth-openai-login` | `oauth::openai-codex::login` | deny | Same. |
| `kernel/no-self-run-start` | `run::start` | deny | No re-entrant runs. |
| `kernel/no-self-run-start-and-wait` | `run::start_and_wait` | deny | Same. |
| `kernel/no-router-stream-assistant` | `router::stream_assistant` | deny | Routing internal. |
| `kernel/no-router-abort` | `router::abort` | deny | Routing internal. |
Deny shorthands (`!function_id` in the YAML): `approval::resolve`,
`policy::check_permissions`, `hook-fanout::publish_collect`, `state::set`,
`state::update`, `state::delete`, `stream::set`, `iii::durable::publish`,
`auth::set_token`, `auth::delete_token`, `oauth::anthropic::login`,
`oauth::openai-codex::login`, `run::start`, `run::start_and_wait`,
`router::stream_assistant`, `router::abort`.

Bare-string allow rules: `harness::status`, `state::get`, `state::list`,
`models::list`, `models::get`, `models::supports`, `auth::get_token`,
Expand Down
125 changes: 52 additions & 73 deletions harness-node/docs/workers/approval-gate.md
Original file line number Diff line number Diff line change
@@ -1,118 +1,97 @@
# approval-gate

Owns approval state. Registers `approval::resolve` and a state-trigger adapter
on the `approvals` scope that wakes `turn::step` on every decision write.
Registers `approval::resolve` and shared wire schemas for the approval path.
Per-call resume functions (`turn::approval_resume::<session>/<call>`) live in
the turn-orchestrator.

## Purpose

The approval gate is the runtime resolution surface for
[iii-permissions.yaml](iii-permissions.yaml). It does **not** intercept
function calls on the bus — the turn-orchestrator consults
`policy::check_permissions` directly inside `consultBefore`. The gate's job
is to accept human decisions and release parked turns.
The approval gate is the bus entry point for human decisions on parked tool
calls. It does **not** intercept function calls on the bus — the
turn-orchestrator consults `policy::check_permissions` directly inside
`consultBefore`. The gate's job is to accept operator input from the console
and route it to the correct per-call resume function.

| Policy outcome (in orchestrator) | Orchestrator effect |
|---|---|
| `allow` | dispatch proceeds immediately |
| `deny` | dispatch short-circuits with a `DenialEnvelope` |
| `needs_approval` | orchestrator parks the call in `function_awaiting_approval` and stops re-publishing `turn::step_requested` until a decision lands |
| `needs_approval` | orchestrator parks the call in `function_awaiting_approval` and registers a resume fn |

Resume happens when an operator calls `approval::resolve`. The resolve
handler writes the decision to `approvals/<sid>/<call_id>`; the state
trigger (`approval::on_decision_written`, gated by
`approval::is_decision_write`) then directly triggers `turn::step` for
the affected session — the same primitive the orchestrator uses in its own
`on-record-written` pattern. The gate itself owns no in-process pending map.
## Resolution flow

## Topology
1. While parked, the orchestrator calls `registerApprovalResume` for each
pending call (see [approval-resume.ts](harness-node/src/turn-orchestrator/approval-resume.ts)).
2. The console calls `approval::resolve` with `{ session_id, function_call_id, decision, reason? }`.
3. `approval::resolve` triggers `turn::approval_resume::<sid>/<cid>` with the decision payload.
4. The resume handler writes `approvals/<sid>/<cid>` (if not already set), invokes `turn::step`, and unregisters the resume fn.
5. `handleAwaitingApproval` reads all decisions, folds them into the prepared snapshot, and returns to `function_execute`.

approval-gate owns two pieces of state surface:

1. The `approval::resolve` function — bus entry point the UI calls to write a
decision into `approvals/<session_id>/<function_call_id>`.
2. A state trigger on `scope: approvals` (condition: `new_value.decision` is a
string) whose handler directly triggers `turn::step` for the affected
session — the same primitive the orchestrator uses for `on-record-written`.

The orchestrator consults policy directly (`policy::check_permissions`) inside
`consultBefore`; approval-gate is no longer a hook subscriber and there is no
`agent::before_function_call` topic.
Abort uses the same resume path: `performAbortSideEffects` triggers each
registered resume fn with `{ decision: 'aborted', reason: 'session_aborted' }`
instead of calling `approval::resolve`.

## Registered functions

- `approval::resolve` — Write the final approval decision (`allow` or `deny`) for a pending call. The state write is itself the wake-up event.
- `approval::is_decision_write` — Condition function bound to the approvals state trigger; returns `true` only for `state:created`/`state:updated` events whose `new_value.decision` is a string.
- `approval::on_decision_written` — State trigger adapter: extracts `session_id` from the `<sid>/<cid>` key and directly triggers `turn::step` for that session.
- `approval::resolve` — Validates the payload and triggers the per-call resume function. Returns `{ ok: true }` or `{ ok: false, error: 'invalid_payload' | 'resume_failed' }`.

## Triggers
Per-call resume functions are registered by the turn-orchestrator, not this worker:

- **State trigger** on `scope: approvals` gated by
`condition_function_id: approval::is_decision_write` →
`approval::on_decision_written`. This is what wakes the orchestrator
after a human resolves an approval.
- `turn::approval_resume::<session_id>/<function_call_id>` — Persists the decision to scope `approvals` and wakes `turn::step`.

## State keys

All keys live under the iii state scope configured by
`approval_gate.approval_state_scope` (default `approvals`). From
[src/approval-gate/types.ts](harness-node/src/approval-gate/types.ts) and
[src/approval-gate/pending.ts](harness-node/src/approval-gate/pending.ts):
All decision records use scope `approvals` (constant `STATE_SCOPE` in
[src/approval-gate/schemas.ts](harness-node/src/approval-gate/schemas.ts)):

| Key shape | Value | Purpose |
|---|---|---|
| `<session_id>/<function_call_id>` | `{ decision: 'allow' \| 'deny' \| 'aborted', reason: string \| null }` | One record per **resolved** approval. The record only exists once a decision (or abort) lands; pending is implicit in the absence of a record. The orchestrator's `function_awaiting_approval` state reads this key. |
| `<session_id>/<function_call_id>` | `{ decision: 'allow' \| 'deny' \| 'aborted', reason: string \| null }` | Written by the resume handler when an operator resolves or abort fires. `handleAwaitingApproval` reads these keys while the turn is in `function_awaiting_approval`. |

Pending calls are tracked on the turn record (`awaiting_approval[]`), not as
separate rows under `approvals` until a decision lands.

## Denial envelopes

`'aborted'` decisions are written by
[src/turn-orchestrator/abort.ts](harness-node/src/turn-orchestrator/abort.ts)
when `router::abort` fires while the turn is paused on approvals.
[src/approval-gate/denial.ts](harness-node/src/approval-gate/denial.ts) builds
`DenialEnvelope` values for policy denies (`permissionsDenyEnvelope` via
`consultBefore` in [hook.ts](harness-node/src/turn-orchestrator/hook.ts)).
[src/approval-gate/redact.ts](harness-node/src/approval-gate/redact.ts)
sanitizes tool args for `args_excerpt` on those envelopes.

## Pending-approval signalling

The frontend does not consume signal events for approvals. Instead, the
orchestrator emits a `turn_state_changed` event on every `turn_state`
write (see `harness-node/docs/workers/turn-orchestrator.md`). The
console reads `awaiting_approval` from the new record to render
approve/deny buttons, and uses `function_execution_end` (which already
carries the blocked result for denied calls via the orchestrator's
`handleExecute` blocked-result branch) to close the card. On page
reload the console fires a one-shot `turn::get_state { session_id }`
(orchestrator function — clients do not read iii state directly) to
recover any modals that were pending when the page loaded.
The frontend does not consume a dedicated approval signal event. The
orchestrator emits `turn_state_changed` on every `turn_state` write; the
console derives pending approvals from `awaiting_approval` on the mirrored
record. On reload it uses `turn::get_state` (not direct iii state reads).

## Configuration

From the `approval_gate` section of
[config.yaml](harness-node/config.yaml):
There is no `approval_gate` section in [config.yaml](harness-node/config.yaml).
Scope `approvals` is fixed in code (`STATE_SCOPE`).

- `approval_state_scope` (default `approvals`) — iii state scope for
decision records. The state trigger that wakes the orchestrator is
registered against this same scope.

The policy function id is owned by the orchestrator's config slice, not
approval-gate's — set it at top level as `policy_function_id` (default
`policy::check_permissions`). See `harness-node/config.yaml` and
Policy consultation is a direct `iii.trigger` to `policy::check_permissions`
from turn-orchestrator `consultBefore`. See
[workers/turn-orchestrator.md](workers/turn-orchestrator.md).

There is no `default_timeout_ms`: the turn record sits in
`function_awaiting_approval` until a decision is written, so the only relevant
timeout is the durable bus's own backstop.

## Dependencies

From
[src/approval-gate/iii.worker.yaml](harness-node/src/approval-gate/iii.worker.yaml):
no explicit dependency block (the gate reads/writes iii state).
no explicit dependency block.

## Source layout

| File | Purpose |
|---|---|
| [src/approval-gate/main.ts](harness-node/src/approval-gate/main.ts) | Binary entry point (`iii-approval-gate`). |
| [src/approval-gate/register.ts](harness-node/src/approval-gate/register.ts) | Registers `approval::resolve` and the decision-written trigger pair. |
| [src/approval-gate/config.ts](harness-node/src/approval-gate/config.ts) | Loads the `approval_gate` config section. |
| [src/approval-gate/types.ts](harness-node/src/approval-gate/types.ts) | Wire types and constants: `DenialEnvelope`, `MatchedConstraint`, `WireDecision`, `DeniedBy`, `FN_RESOLVE`, `STATE_SCOPE`, `pendingKey`. |
| [src/approval-gate/policy-consult.ts](harness-node/src/approval-gate/policy-consult.ts) | Calls `policy::check_permissions` and decodes the decision (imported by the orchestrator's `hook.ts`). |
| [src/approval-gate/pending.ts](harness-node/src/approval-gate/pending.ts) | `handleResolve` writes `{decision, reason}` to `approvals/<sid>/<cid>`. |
| [src/approval-gate/on-decision-written.ts](harness-node/src/approval-gate/on-decision-written.ts) | State trigger adapter — `approval::is_decision_write` (condition) + `approval::on_decision_written` (handler) — directly triggers `turn::step` when a decision lands. |
| [src/approval-gate/denial.ts](harness-node/src/approval-gate/denial.ts) | Builds `DenialEnvelope` instances (permissions, user, gate_unavailable). |
| [src/approval-gate/resolve.ts](harness-node/src/approval-gate/resolve.ts) | Registers `approval::resolve`; triggers per-call resume fns. |
| [src/approval-gate/schemas.ts](harness-node/src/approval-gate/schemas.ts) | `STATE_SCOPE`, wire schemas, `parsePolicyReply`, `pendingKey`, `approvalResumeFnId`, `ResolvePayloadSchema`. |
| [src/approval-gate/denial.ts](harness-node/src/approval-gate/denial.ts) | `permissionsDenyEnvelope` and related helpers. |
| [src/approval-gate/redact.ts](harness-node/src/approval-gate/redact.ts) | `redact` / `clip` for safe `args_excerpt` on denials. |
| [src/approval-gate/iii.worker.yaml](harness-node/src/approval-gate/iii.worker.yaml) | Worker manifest. |

Related orchestrator code:
[approval-resume.ts](harness-node/src/turn-orchestrator/approval-resume.ts),
[hook.ts](harness-node/src/turn-orchestrator/hook.ts).
Loading
Loading