|
| 1 | +// Two scenarios over the §10.4 / §10.5 control surface. |
| 2 | +using ARCP.Client; |
| 3 | +using ARCP.Errors; |
| 4 | +using ARCP.Ids; |
| 5 | +using ARCP.Messages.Control; |
| 6 | +using ARCP.Messages.Execution; |
| 7 | +using static ARCP.Samples.Cancellation.ClientStubs; |
| 8 | +using Env = ARCP.Envelope.Envelope; |
| 9 | + |
| 10 | +const int CancelDeadlineMs = 5_000; |
| 11 | + |
| 12 | +string which = args.Length > 0 ? args[0] : "cancel"; |
| 13 | +if (which == "cancel") |
| 14 | +{ |
| 15 | + await ScenarioCancelAsync(); |
| 16 | +} |
| 17 | +else if (which == "interrupt") |
| 18 | +{ |
| 19 | + await ScenarioInterruptAsync(); |
| 20 | +} |
| 21 | +else |
| 22 | +{ |
| 23 | + throw new InvalidOperationException($"unknown scenario: {which}"); |
| 24 | +} |
| 25 | + |
| 26 | +static async Task<JobId> StartLongJobAsync(ARCPClient client) |
| 27 | +{ |
| 28 | + Env accepted = await Request( |
| 29 | + client, |
| 30 | + Envelope(client, "tool.invoke", new ToolInvoke( |
| 31 | + Tool: "demo.long_running", |
| 32 | + Arguments: System.Text.Json.JsonSerializer.SerializeToElement(new { work_seconds = 600 }))), |
| 33 | + timeout: TimeSpan.FromSeconds(10)); |
| 34 | + return ((JobAccepted)accepted.Payload).JobId; |
| 35 | +} |
| 36 | + |
| 37 | +static async Task<Env> CancelJobAsync(ARCPClient client, JobId jobId, string reason, int deadlineMs) |
| 38 | +{ |
| 39 | + // Cooperative cancel. Runtime drives target to a clean checkpoint |
| 40 | + // inside `deadline_ms` before terminating; escalates to ABORTED on |
| 41 | + // timeout (RFC §10.4). |
| 42 | + Env reply = await Request( |
| 43 | + client, |
| 44 | + Envelope(client, "cancel", new Cancel( |
| 45 | + Target: CancelTarget.Job, |
| 46 | + TargetId: jobId.Value, |
| 47 | + Reason: reason, |
| 48 | + DeadlineMs: deadlineMs)), |
| 49 | + timeout: TimeSpan.FromMilliseconds(deadlineMs + 5_000)); |
| 50 | + if (reply.Type == "cancel.refused") |
| 51 | + { |
| 52 | + CancelRefused refused = (CancelRefused)reply.Payload; |
| 53 | + throw new ARCPException(ErrorCode.FailedPrecondition, $"cancel refused: {refused.Reason}"); |
| 54 | + } |
| 55 | + return reply; |
| 56 | +} |
| 57 | + |
| 58 | +static Task InterruptJobAsync(ARCPClient client, JobId jobId, string prompt) => |
| 59 | + // Distinct from cancel: pauses the job (`blocked`), runtime emits |
| 60 | + // `human.input.request`. Job is NOT terminated (RFC §10.5). |
| 61 | + Send(client, Envelope(client, "interrupt", new Interrupt( |
| 62 | + Target: CancelTarget.Job, |
| 63 | + TargetId: jobId.Value, |
| 64 | + Prompt: prompt))); |
| 65 | + |
| 66 | +static async Task<Env> AwaitTerminalAsync(ARCPClient client, JobId jobId) |
| 67 | +{ |
| 68 | + await foreach (Env env in Events(client)) |
| 69 | + { |
| 70 | + if (!Equals(env.JobId, jobId)) continue; |
| 71 | + if (env.Type is "job.completed" or "job.failed" or "job.cancelled") return env; |
| 72 | + } |
| 73 | + throw new InvalidOperationException("event stream closed before terminal"); |
| 74 | +} |
| 75 | + |
| 76 | +static async Task ScenarioCancelAsync() |
| 77 | +{ |
| 78 | + ARCPClient client = null!; // transport, identity, auth elided |
| 79 | + await Open(client); |
| 80 | + try |
| 81 | + { |
| 82 | + JobId jobId = await StartLongJobAsync(client); |
| 83 | + await Task.Delay(TimeSpan.FromSeconds(2)); // let the job actually start |
| 84 | + Env ack = await CancelJobAsync(client, jobId, reason: "user_aborted", deadlineMs: CancelDeadlineMs); |
| 85 | + Console.WriteLine($"cancel ack: {ack.Type}"); |
| 86 | + Env terminal = await AwaitTerminalAsync(client, jobId); |
| 87 | + Console.WriteLine($"terminal: {terminal.Type}"); |
| 88 | + } |
| 89 | + finally |
| 90 | + { |
| 91 | + await client.CloseAsync(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +static async Task ScenarioInterruptAsync() |
| 96 | +{ |
| 97 | + ARCPClient client = null!; |
| 98 | + await Open(client); |
| 99 | + try |
| 100 | + { |
| 101 | + JobId jobId = await StartLongJobAsync(client); |
| 102 | + await Task.Delay(TimeSpan.FromSeconds(2)); |
| 103 | + await InterruptJobAsync(client, jobId, prompt: "Pause and ask before touching production tables."); |
| 104 | + // Runtime now emits human.input.request; answer via samples/HumanInput. |
| 105 | + await foreach (Env env in Events(client)) |
| 106 | + { |
| 107 | + if (env.Type == "human.input.request" && Equals(env.JobId, jobId)) |
| 108 | + { |
| 109 | + ARCP.Messages.Human.HumanInputRequest req = (ARCP.Messages.Human.HumanInputRequest)env.Payload; |
| 110 | + Console.WriteLine($"awaiting human: \"{req.Prompt}\""); |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + finally |
| 116 | + { |
| 117 | + await client.CloseAsync(); |
| 118 | + } |
| 119 | +} |
0 commit comments