Skip to content

feat(intercept): target-gate schema, opt-in flag and approval requests (S3b 1.2-1.4) - #178

Merged
BaaRai merged 1 commit into
mainfrom
feat/target-gate-schema-request
Nov 10, 2026
Merged

BaaRai merged 1 commit into
mainfrom
feat/target-gate-schema-request

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Steps 1.2 to 1.4 of the S3b phase 1 plan (approval gate for asset targets): schema and store, the opt-in flag, and the request / wait / decide path. Follows #177 (step 1.1).

What this does not do

The gate is not wired into the tool hook yet. Extraction, resolution, exemptions and the decision in the hook are steps 1.5 to 1.7. After this PR target_gate can be set through the API, but nothing is extracted, resolved, asked or blocked, whatever the flag says. Do not present the flag as a control until 1.7 is merged. There is no UI (phase 4).

With the flag off (the default) behaviour is unchanged for every task and conversation: no code added here runs on a tool call.

Schema and store (1.2)

  • tasks.target_gate, conversations.target_gate, both BOOLEAN NOT NULL DEFAULT false.
  • intercept_pending.target_key (NULL = classic ask) and the partial unique index uq_intercept_pending_target: one open request per scope (task or conversation) and host:port.
  • target_pins, without foreign keys on task_id, conversation_id and intercept_id: a pin outlives its task, its conversation and its request row. uq_target_pins_active keeps one active pin per key.
  • target_gate_exemption_log (table only; its writer is step 1.6).
  • All DDL is idempotent (IF NOT EXISTS), no DO block, no CHECK constraint, no existing constraint changed. intercept_pending.conversation_id keeps ON DELETE CASCADE.
  • db/target_pins.go: ActiveTargetPins (the pin on the exact port first, then the pin on port 0; a pin on a specific port never covers a target whose port is not declared), ListTargetPins, RevokeTargetPin, FindOrCreateTargetRequest (INSERT ... ON CONFLICT on the partial expression index), ResolveTargetRequest (settle the row, supersede the old pin, write the new version: one transaction).
  • Task hard delete and archive-stub delete: the task's pins are revoked (task_deleted) and its open target requests settled as denied by the system, in the delete transaction. Pins and approval rows stay readable.
  • DeleteConversation and DeleteConversations are now one transaction each: delete, and revoke the pins of the conversations that were removed (conversation_deleted). The batch form revokes only the ids the delete returned. The request rows of a chat still go with it through the cascade.

Opt-in flag (1.3)

  • Task: target_gate on POST /api/tasks and on PATCH /api/tasks/{id}; returned in the task DTO. The in-memory flag is atomic and is read on every call through TargetScope.Enabled, so a switch applies to the next tool call.
  • Conversation: target_gate on POST /api/conversations and PATCH /api/conversations/{id}. A turn runs with the value read when it started.
  • A retest conversation takes the flag of the finding's task, never its pins. A task spawned by the orchestration agent starts with the gate off. Archive and restore keep the flag and the pins.
  • TargetScope now carries the Interceptor that owns the gate (needed by the redirect callback of step 1.9).

Request, wait, decide (1.4)

  • pendingManager keeps a list of waiters per request; resolve wakes all of them. HandleAsk changes only in the add / remove call shape.
  • Interceptor.RequestTargets: find-or-create per target, the approval card emitted once by the call that wrote the row, then wait on channel + poll (2 s) + context + wait bound.
    • A plain target request is host-level and durable. A call that stops waiting (bound elapsed, cancelled, claim lost) never settles it; it returns TARGET_APPROVAL_PENDING and a later approval still pins.
    • A request merged with the ask of a rule or of the judge is bound to its creating call, like a classic ask. When that call leaves, the system closes the row. A rule with timeout_action = allow is overridden on a merged row, because a timeout cannot pin; the override is recorded in the audit.
    • A call attached to a merged row that closes without an approval asks once more with a plain request, then blocks.
    • A waiter whose row no longer exists (conversation deleted) stops with TARGET_GATE_UNAVAILABLE.
    • Wait bound 0 (final settlement round): the request is created and the call returns at once.
  • Interceptor.Decide(id, allowed, approver, remoteAddr): on a target request, "allowed" pins the addresses the request displayed (old addresses plus displayed ones when the key is already pinned). This is the only writer of target_pins. A second decision still returns 409.
  • Approver and remote address are recorded for every human decision, classic asks included: in the row's audit (approver, approver_addr), and on the pin (decided_by, decided_from) for target requests. intercept_pending.decided_by keeps the actor class (human).
  • Global egress proxy: a request raised while global_proxy is set says that the approval is name-only, and its pin is stored with name_only_effective = true. A name-only pin stays name-only when the proxy is removed later; enforcing addresses again takes a revocation and a new approval. (The gate that reads the mark, and skips the address comparison for such a pin, is step 1.6.)
  • Routes: GET /api/tasks/{id}/target-pins, POST /api/tasks/{id}/target-pins/{pid}/revoke, and the same pair under /api/conversations/{id}. decision_source=target is accepted by the approval list filters. POST /api/intercept/pending/{id}/decide keeps its request and response shape.
  • intercept.ValidateExemptionHost: an exemption written as an IP literal may be private unicast (10.0.0.5), never loopback, link-local (169.254.0.0/16 with the cloud metadata address, fe80::/10), unspecified or multicast, nor the two metadata addresses outside link-local (fd00:ec2::254, 100.100.100.200). It has no caller yet: the exemption API is step 1.6.

Differences from the plan text

  • The pin column is name_only_effective, not name_only.
  • ActiveTargetPin is ActiveTargetPins (it returns the candidates). RevokeScopePins and SettleScopeTargetRequests are unexported helpers that take the delete transaction.
  • CreateConversation and ResolveIntercept keep their signatures; CreateConversationGated and ResolveInterceptBy carry the new arguments.
  • Pin versions count every pin a key ever had, revoked ones included, so a version number never names two pins.
  • Target rows, merged ones included, carry decision_source = target; a merged row is marked audit.merged_ask.
  • An approved plain target request gets execution_status = unknown: it is host-level and no single call owns its result. A merged row keeps the classic behaviour.

Tests

PostgreSQL tests in db, intercept (new TestMain, own database like the other packages) and server; unit tests for the waiter list, the exemption guard and the token subject.

  • Schema applied twice gives the same tables, columns, indexes and constraints; no CHECK and no foreign key on the new tables; the cascade is still there.
  • One open request per key under 24 concurrent callers on two instances.
  • Atomic decide (a failing pin insert leaves the row pending and the old pin active), versioning, one active pin per key, port precedence, idempotent revoke.
  • Task delete, archive-stub delete, archive restore, restore of rows without target_key.
  • Conversation delete revokes in the same transaction (a failing delete leaves the pin active); the batch delete revokes only the ids it removed.
  • Flag: default off, create, patch, live switch, retest copy, spawned task, conversation over HTTP.
  • Request path: shared request across two interceptors, waiter leaving, wait bound, merged row, timeout never pins, displayed addresses pinned, denial, requeue after a merged denial, deleted row, approver and address (target row and classic ask), name-only pin, claim lost while waiting.
  • Gate off: TestEveryAgentKindCarriesTargetScope (every session's scope reports the gate off by default), TestTargetGateSeamAllowsWithoutDBWrite, TestTaskTargetGateDefaultsOff.

Local run on a freshly created PostgreSQL 16 database: go build ./..., go vet ./..., go test -count=1 -p 1 ./... (14 packages ok, 820 top-level tests pass, 4 skipped as on main), go test -race -p 1 on the CI package list (13 packages ok, no race).

Follow-ups

  • Read route or export for revoked pins of a deleted task or conversation. They stay in target_pins but the pin routes are scoped to an existing task or conversation, so today they are readable in the database only. A read route or an export is required before they are presented as audit evidence.
  • Steps 1.5 to 1.7 wire the gate. Until then the flag enforces nothing (see the top of this description).
  • Step 1.6 has to call ValidateExemptionHost from the exemption API, on the normalised host, and apply the void rule with link-local, unspecified and ULA addresses counted as private, to seeded entries as well.
  • Step 1.7: check that the target-only guard records no passing call in its audit ring and that a block goes through block(). This PR does not touch the guard.
  • Phase 4 (UI): label and filter for decision_source = target, display of approver, the task and conversation toggle.

…s (S3b 1.2-1.4)

Storage, flag and request path of the approval gate for asset targets. The
gate itself is not wired into the tool hook yet (steps 1.5 to 1.7): with this
change nothing is extracted, resolved or blocked, whatever the flag says.

Schema and store (1.2)
- tasks.target_gate, conversations.target_gate (default false).
- intercept_pending.target_key and a partial unique index: one open request
  per scope (task or conversation) and host:port.
- target_pins (no foreign keys: a pin outlives its task, its conversation and
  its request row) and target_gate_exemption_log. All DDL is idempotent; no
  CHECK constraints; the cascade on intercept_pending.conversation_id stays.
- db/target_pins.go: pin lookup with port precedence, list, revoke,
  find-or-create of a request, and ResolveTargetRequest, which settles the
  row, supersedes the old pin and writes the new version in one transaction.
- Deleting a task or an archive stub revokes its pins and settles its open
  target requests in the delete transaction. DeleteConversation and
  DeleteConversations revoke the pins of the removed conversations in the
  same transaction as the delete.

Opt-in flag (1.3)
- Per task through the create and metadata-patch paths, per conversation
  through create and patch; exposed in the task DTO. A retest conversation
  takes the flag of the finding's task, never its pins. A spawned task starts
  with the gate off.
- The task flag is atomic and read on every call through TargetScope.Enabled.
  TargetScope now carries the Interceptor that owns the gate.

Request, wait, decide (1.4)
- pendingManager keeps a list of waiters per request; resolve wakes all.
- RequestTargets: find-or-create per target, one card per request, wait on
  channel, poll, context and wait bound. A plain request is never settled by
  a call that leaves. A request merged with an ask is bound to its creating
  call and a timeout never allows it.
- Decide records the approver (subject of the session token) and the remote
  address for every human decision, classic asks included. On a target
  request "allowed" pins the displayed addresses; that is the only writer of
  pins.
- A request raised while the global egress proxy is set says the approval is
  name-only and its pin is stored with name_only_effective.
- Routes: GET/POST .../target-pins and .../target-pins/{pid}/revoke under
  /api/tasks/{id} and /api/conversations/{id}; decision_source=target filter.
- ValidateExemptionHost: an exemption written as an IP literal may be private
  unicast, never loopback, link-local or metadata, unspecified or multicast.
@BaaRai
BaaRai merged commit 923c4a8 into main Nov 10, 2026
4 checks passed
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