Skip to content

feat(agent): delegated low-risk write tools, gated by the grant's capabilities - #5683

Queued
kojiwakayama wants to merge 5 commits into
mainfrom
fix/inbox-4992-delegated-write-tools
Queued

kojiwakayama wants to merge 5 commits into
mainfrom
fix/inbox-4992-delegated-write-tools

Conversation

@kojiwakayama

Copy link
Copy Markdown
Contributor

Refs veryfront/veryfront-issue-inbox#4992 (C4 of epic veryfront/veryfront-issue-inbox#4966). Runtime side of veryfront/veryfront-api#7959 (A7).

Problem

A7 maps the low-risk delegated writes (projects.create, schedules.activate, schedules.pause, input_requests.answer) and the schedules.list read, but a hosted run had no tool to call them. The delegated client only offered a raw request, and nothing read which capabilities the grant carries, so a run could not offer a tool only when its grant allows it.

Root cause

  • createHostedDelegatedUserTools built the seven phase 1 reads and nothing else.
  • The client had no way to report the grant's capabilities without handing out the grant.
  • There was no JSON write path that fixes the method and body bytes before the grant enters the request.

Fix

Client (delegated-user-credential.ts):

  • capabilities(): the actions of the grant's single veryfront_delegated_actions entry in authorization_details, decoded without verifying the signature (presence only; the API checks every call). It follows renewals. A grant that is not a JWT, or names another type, has none.
  • sendJson(path, { method, body, signal }): POST or PATCH only. The body is serialized once with a captured JSON.stringify, before any grant is in a request, and sent with Content-Type: application/json through request, so the snapshot-once rules apply and a 401 retry replays the same method and bytes.
  • JWT payload decoding is shared between expiry and capabilities (readGrantClaims).

Tools (delegated-user-tools.ts), each present only when the grant names its capability:

Tool Capability Call
user_schedules_list schedules.list GET /projects/{ref}/schedules
user_project_create projects.create POST /projects with account_id (UUID, required), name, optional slug, description
user_schedule_set_status schedules.activate and/or schedules.pause PATCH /projects/{ref}/schedules/{id} with exactly { status }. The schema offers only the statuses the grant carries
user_input_request_answer input_requests.answer POST /input-requests/{id}/responses with exactly { values }
  • Each write returns { changed, undo, <api body> }: project create says the person moves the project to trash in Studio; a schedule change names the opposite status (with this tool when the grant carries it, otherwise in Studio); an answer says it cannot be undone.
  • A refused write says that nothing changed and why. A 403 is matched on the API's detail (route_not_delegated, write flag off, capability not granted, body not allowed) to a fixed message; the API body is never echoed. A 5xx says the change may or may not have been applied.
  • Phase 1 reads are unchanged and still offered for every grant.

Docs: the delegated section of docs/architecture/native-request-processing.md.

Tests

Fail on main (missing exports and methods), pass here:

  • delegated-user-tools.test.ts, new capability tools block: no phase 2 tool without its capability (non-JWT grant, phase 1 grant, foreign type); each tool exactly when its capability is present; only granted schedule statuses accepted; exact bodies per guard (method, path, JSON, content type), with project keys a subset of the projects.create guard keys; schema refusals of what the guards refuse (missing or non-UUID account_id, legacy accountId, extra keys, cron, deleting, extra answer keys); the schedules read; changed/undo in results; grant, its segments and the run token absent from descriptions, input JSON schemas, results, errors and logs; 403 route_not_delegated and the other refusals give clear model-facing messages without echoing the body.
  • delegated-user-credential.test.ts, new block: sendJson serializes once before any request and replays the same bytes and method on the 401 retry even after the caller mutates its object; only POST/PATCH and JSON-serializable bodies; capabilities() decoding, filtering and renewal.

Ran: deno task test:file src/agent/hosted (1089 passed, 0 failed), deno fmt --check, deno lint src/agent/hosted, deno check src/agent/hosted/*.ts, lint:anti-slop, lint:check-awaits, lint:test-typecheck, lint:ban-test-only.

Not in this PR

  • user_agent_create with the approval wait (C5) and advertising delegated_user_approvals.
  • Agent policy and guidance for these tools (G2), Studio cards with undo (S3).
  • Gating the phase 1 reads on the grant's capabilities: every grant minted today carries them all, and those tools keep their current behavior.
  • Matching 403 reasons relies on the API's detail text because the problem body carries no reason code. A stable reason field in the API would make this exact; unknown details fall back to a generic "refused, nothing changed" message.

🤖 Generated with Claude Code

…abilities

Adds user_schedules_list, user_project_create, user_schedule_set_status and
user_input_request_answer over the delegated API client. Each exists only
when the grant names its capability in authorization_details (decoded
unverified, for presence only). Each write sends exactly the body the API
guard admits and returns what changed and how the person can undo it. A
refused change tells the model that nothing changed and why.

The client gains sendJson (POST or PATCH, serialized once before the grant
enters the request, replayed byte for byte on the 401 retry) and
capabilities().

Refs veryfront/veryfront-issue-inbox#4992

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T22:23:35.133151Z 4f25c5d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

📦 Client bundle boundary

Entrypoint Modules Source size Server leaks
src/index.client.ts 66 371 KiB ✅ 0
src/react/components/Head.tsx 5 74 KiB ✅ 0
src/react/router/index.tsx 5 74 KiB ✅ 0
src/react/context/index.tsx 5 74 KiB ✅ 0
src/react/fonts/index.ts 6 81 KiB ✅ 0
src/react/components/ui/index.ts 111 720 KiB ✅ 0
src/react/components/ui/adapter/contract.ts 1 18 KiB ✅ 0
src/react/components/ui/icons/index.ts 3 17 KiB ✅ 0
src/chat/index.ts 305 2055 KiB ✅ 0
src/chat/ag-ui.ts 61 464 KiB ✅ 0
src/chat/protocol.ts 1 11 KiB ✅ 0
src/chat/types.ts 46 248 KiB ✅ 0
src/chat/message-prep.ts 68 519 KiB ✅ 0
src/markdown/index.ts 6 30 KiB ✅ 0
src/mdx/index.ts 2 2 KiB ✅ 0
src/agent/identity-contracts.ts 1 3 KiB ✅ 0
src/schemas/index.ts 43 222 KiB ✅ 0
src/platform/env.ts 5 55 KiB ✅ 0
src/workflow/claude-code/react/index.ts 9 91 KiB ✅ 0
src/events/index.ts 37 631 KiB ✅ 0
src/events/ag-ui/public.ts 36 624 KiB ✅ 0
src/run-events/index.ts 54 323 KiB ✅ 0

A server module in a client graph aborts hydration in the browser. New leaks fail CI; known leaks are tracked in scripts/lint/client-bundle-baseline.json to burn down.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

Copy link
Copy Markdown
Contributor

Review score: 82/100: good

The capability-gated write tools have a careful security design. Most of what's left is about ambiguous outcomes when a write fails, not about whether it's correct.

Strengths

  • Credential handling is solid. sendJson serializes once with a captured JSON.stringify before the grant enters any request, limits methods to POST/PATCH, and sends everything through requestAsPerson. That means the 401 retry replays the same snapshot bytes. The request refactor only moves code (a closure lifted out of the frozen object); I found no behavior change.
  • Least privilege at the tool layer. Each tool exists only when its capability is in authorization_details. user_schedule_set_status builds its enum from the granted statuses and checks again in execute. Each body has exactly the keys its API guard admits ({ status }, { values }, the project keys). The API remains the authority, and the docs say so.
  • Results the model can act on. changed and undo in each write result, plus 403 refusals mapped to fixed messages without echoing the body, are good choices for an agent-facing surface. The 5xx text "may or may not have been applied: read it back" is the right instruction.
  • Tests match the risk. They cover absence without a capability (non-JWT grant, foreign type), exact method/path/body/content type, byte replay on 401 after the caller mutates its object, and the grant or run token not appearing in descriptions, schemas, results or logs.

Concerns (none blocking)

  • Transport failures skip the "may or may not" path. writeJson only classifies HTTP statuses. If api.sendJson rejects after the server already applied the change (connection reset, timeout, or an abort while the response is in flight), the model gets a raw error with no guidance. A retry of user_project_create can then create a duplicate project. Wrap the sendJson call and give non-abort transport errors the same "may or may not have been applied: read it back before you try again" message the 5xx path uses. Add one test for a rejecting transport.
  • Abort during a 403 is hidden. readRefusalDetail swallows every error, including an AbortError from readResponseTextPrefix. A cancelled call then shows up as the generic "refused, nothing changed" 403 message instead of a cancellation. Rethrow when abortSignal?.aborted is true.
  • Matching 403s on English detail prefixes is brittle. The PR already says this. Please track the follow-up for a stable reason code in the API, so a wording change there doesn't silently fall back to the generic message. A test that pins each prefix to the API's current strings (or a shared fixture) would catch drift sooner.
  • The tool set doesn't follow renewals. capabilities() follows renewals, but createCapabilityTools reads it once when the run starts, so a renewal that narrows the grant leaves stale tools in place. That's safe, because the API refuses them with a clear 403. The doc paragraph should still say the tool set is fixed when the run starts, so nobody assumes it updates.
  • Nit: user_input_request_answer.values is record(string, unknown) with no size or depth limit. Consider capping the number of keys, or the serialized size in sendJson, so a runaway model can't post a very large body as the person.

Not verified here: the review container has no Deno toolchain, so I relied on the test run reported in the PR description (deno task test:file src/agent/hosted, 1089 passed).


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ddd3b81e33

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.36275% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/agent/hosted/delegated-user-tools.ts 91.63% 13 Missing and 10 partials ⚠️

📢 Thoughts on this report? Let us know!

…revoked capabilities

- user_input_request_answer sends one Idempotency-Key per answer, which
  sendJson keeps across the 401 retry; the route requires it.
- Delegated tools read response status through the native getter the client
  captured at load, so a patched Response.prototype.status cannot flip a
  write's outcome.
- Answer values are limited to string, number, boolean or null, as the
  input contract allows.
- A capability tool checks the current grant before each call, so a renewed
  grant that drops a capability stops its tool.

Refs veryfront/veryfront-issue-inbox#4992

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: ebdad6c1d4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

… prototype

The semantic unit-boundary audit refuses a unit test that redefines
Response.prototype.status. The test now gives each stubbed response an own
status that lies, which the tools must ignore, and stays hermetic.

Refs veryfront/veryfront-issue-inbox#4992

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 12365f72ed

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…choice field

The API's InputResponseValueSchema admits a list of option values, and a
select or radio field with multiple: true accepts only a list. The tool's
schema refused lists, so such a request could not be answered.

Refs veryfront/veryfront-issue-inbox#4992

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9892cdde7a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…ore the answer

A transport failure after the request reached the API used to surface the
raw fetch error, so the model could retry user_project_create without
knowing the first project may already exist. Report the same read-back
warning used for ambiguous HTTP failures, without echoing the raw error.
A cancelled call keeps its own reason.

Refs veryfront/veryfront-issue-inbox#4992

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 4f25c5d680

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

@kojiwakayama
kojiwakayama added this pull request to the merge queue Dec 4, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

No deployments
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.

2 participants