|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# email_vendor_leases — Claude tool-use loop with a lease that denies send_reply. |
| 4 | +# |
| 5 | +# A triage agent receives an "inbox check" task with a lease that grants |
| 6 | +# read-only tools but NOT send_reply. Claude reads each message, emits a |
| 7 | +# vendor-extension event per parsed message so dashboards can render |
| 8 | +# them specially, and eventually decides one needs a reply. When it |
| 9 | +# tries to call send_reply the lease check denies it; Claude observes |
| 10 | +# the PERMISSION_DENIED tool_result and degrades to drafting the reply |
| 11 | +# for human review. |
| 12 | +# |
| 13 | +# Highlights: §13.4 lease violation as a *recoverable* tool_result error |
| 14 | +# (not session-fatal), §15 / §8.2 x-vendor.* event-kind namespace, and |
| 15 | +# a realistic Claude tool-use loop that handles a deny without crashing. |
| 16 | + |
| 17 | +require 'anthropic' |
| 18 | +require_relative '../../samples/_harness' |
| 19 | + |
| 20 | +module EmailVendorLeasesRecipe |
| 21 | + TOOLS = [ |
| 22 | + { |
| 23 | + 'name' => 'inbox_list', |
| 24 | + 'description' => 'List recent unread messages.', |
| 25 | + 'input_schema' => { 'type' => 'object', 'properties' => {} } |
| 26 | + }, |
| 27 | + { |
| 28 | + 'name' => 'inbox_read', |
| 29 | + 'description' => 'Read one message by id.', |
| 30 | + 'input_schema' => { |
| 31 | + 'type' => 'object', |
| 32 | + 'properties' => { 'id' => { 'type' => 'string' } }, |
| 33 | + 'required' => ['id'] |
| 34 | + } |
| 35 | + }, |
| 36 | + { |
| 37 | + 'name' => 'send_reply', |
| 38 | + 'description' => 'Send a reply to a message.', |
| 39 | + 'input_schema' => { |
| 40 | + 'type' => 'object', |
| 41 | + 'properties' => { 'id' => { 'type' => 'string' }, 'body' => { 'type' => 'string' } }, |
| 42 | + 'required' => %w[id body] |
| 43 | + } |
| 44 | + } |
| 45 | + ].freeze |
| 46 | + |
| 47 | + # stand-in inbox so the recipe is self-contained — swap for IMAP/Gmail in real use |
| 48 | + INBOX = { |
| 49 | + 'm1' => { 'id' => 'm1', 'from' => 'ops@acme.dev', 'subject' => 'Status', |
| 50 | + 'body' => 'All quiet.', 'urgency' => 'low' }, |
| 51 | + 'm2' => { 'id' => 'm2', 'from' => 'ceo@acme.dev', 'subject' => 'Outage!', |
| 52 | + 'body' => 'Site is down — fix asap.', 'urgency' => 'high' } |
| 53 | + }.freeze |
| 54 | + |
| 55 | + def self.run_tool(name, args) |
| 56 | + case name |
| 57 | + when 'inbox_list' |
| 58 | + INBOX.values.map { |m| m.slice('id', 'subject', 'from') } |
| 59 | + when 'inbox_read' |
| 60 | + INBOX.fetch(args['id']) |
| 61 | + else |
| 62 | + raise "tool #{name} should have been denied before reaching run_tool" |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + HANDLER = lambda do |ctx| |
| 67 | + lease_manager = $arcp_runtime.lease_manager |
| 68 | + anthropic = Anthropic::Client.new |
| 69 | + |
| 70 | + messages = [{ |
| 71 | + role: 'user', |
| 72 | + content: 'Triage my inbox. Read each unread message and reply to anything urgent.' |
| 73 | + }] |
| 74 | + |
| 75 | + # tool-use loop: Claude proposes a tool call, we authorize against the |
| 76 | + # lease, run it (or surface a denial), feed the result back, repeat. |
| 77 | + loop do |
| 78 | + turn = anthropic.messages( |
| 79 | + parameters: { |
| 80 | + model: 'claude-sonnet-4-6', |
| 81 | + max_tokens: 1024, |
| 82 | + tools: TOOLS, |
| 83 | + messages: messages |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + if turn['stop_reason'] == 'end_turn' |
| 88 | + text = turn['content'].find { |b| b['type'] == 'text' }&.dig('text').to_s |
| 89 | + ctx.finish(result: { 'drafted_reply' => text, 'sent' => false }) |
| 90 | + return |
| 91 | + end |
| 92 | + |
| 93 | + # append the assistant turn so the next call has full context |
| 94 | + messages << { role: 'assistant', content: turn['content'] } |
| 95 | + tool_results = [] |
| 96 | + |
| 97 | + turn['content'].each do |block| |
| 98 | + next unless block['type'] == 'tool_use' |
| 99 | + |
| 100 | + ctx.tool_call(call_id: block['id'], tool: block['name'], args: block['input']) |
| 101 | + |
| 102 | + begin |
| 103 | + # the lease grants tool.call only for the read-only tools; the |
| 104 | + # send_reply pattern is absent so this raises PermissionDenied |
| 105 | + lease_manager.check!(ctx.job_id, capability: "tool.call:#{block['name']}") |
| 106 | + rescue Arcp::Errors::PermissionDenied => e |
| 107 | + # surface the denial on the ARCP stream as a recoverable error... |
| 108 | + ctx.tool_result(call_id: block['id'], error: e.to_payload) |
| 109 | + # ...and hand it to Claude as the tool result so the model can |
| 110 | + # recover gracefully — lease violations are not session-fatal |
| 111 | + tool_results << { |
| 112 | + type: 'tool_result', |
| 113 | + tool_use_id: block['id'], |
| 114 | + content: "denied: #{e.message}", |
| 115 | + is_error: true |
| 116 | + } |
| 117 | + next |
| 118 | + end |
| 119 | + |
| 120 | + result = run_tool(block['name'], block['input']) |
| 121 | + if block['name'] == 'inbox_read' |
| 122 | + # vendor-extension event — dashboards that recognise the |
| 123 | + # x-vendor.acme.* namespace render parsed metadata specially |
| 124 | + ctx.emit( |
| 125 | + kind: 'x-vendor.acme.email.parsed', |
| 126 | + body: { |
| 127 | + 'message_id' => result['id'], |
| 128 | + 'from' => result['from'], |
| 129 | + 'subject' => result['subject'], |
| 130 | + 'urgency' => result['urgency'] |
| 131 | + } |
| 132 | + ) |
| 133 | + end |
| 134 | + ctx.tool_result(call_id: block['id'], result: result) |
| 135 | + tool_results << { type: 'tool_result', tool_use_id: block['id'], content: result.to_json } |
| 136 | + end |
| 137 | + |
| 138 | + messages << { role: 'user', content: tool_results } |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + def self.runtime |
| 143 | + r = Harness.runtime(agents: { 'triage' => HANDLER }) |
| 144 | + $arcp_runtime = r |
| 145 | + r |
| 146 | + end |
| 147 | +end |
0 commit comments