|
| 1 | +package dev.arcp.examples.cancellation; |
| 2 | + |
| 3 | +import dev.arcp.client.ARCPClient; |
| 4 | +import dev.arcp.envelope.Envelope; |
| 5 | +import dev.arcp.error.ARCPException; |
| 6 | +import dev.arcp.error.ErrorCode; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +/** Two scenarios over the §10.4 / §10.5 control surface. */ |
| 10 | +public final class Main { |
| 11 | + |
| 12 | + private static final int CANCEL_DEADLINE_MS = 5_000; |
| 13 | + |
| 14 | + private Main() { |
| 15 | + } |
| 16 | + |
| 17 | + static String startLongJob(ARCPClient client) { |
| 18 | + // Envelope accepted = client.request(client.envelope("tool.invoke", |
| 19 | + // payload=Map.of( |
| 20 | + // "tool", "demo.long_running", |
| 21 | + // "arguments", Map.of("work_seconds", 600))), 10s); |
| 22 | + // return (String) accepted.payload().get("job_id"); |
| 23 | + throw new UnsupportedOperationException("startLongJob on " + client); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Cooperative cancel. Runtime drives target to a clean checkpoint inside |
| 28 | + * {@code deadlineMs} before terminating; escalates to {@code ABORTED} on |
| 29 | + * timeout (RFC §10.4). |
| 30 | + */ |
| 31 | + static Envelope cancelJob(ARCPClient client, String jobId, String reason, int deadlineMs) { |
| 32 | + // Envelope reply = client.request(client.envelope("cancel", payload=Map.of( |
| 33 | + // "target", "job", "target_id", jobId, "reason", reason, |
| 34 | + // "deadline_ms", deadlineMs)), deadlineMs / 1000 + 5); |
| 35 | + // if ("cancel.refused".equals(reply.type())) |
| 36 | + // throw new ARCPException(FAILED_PRECONDITION, ...); |
| 37 | + // return reply; |
| 38 | + throw new UnsupportedOperationException( |
| 39 | + "cancel job=" + jobId + " reason=" + reason + " deadline=" + deadlineMs); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Distinct from cancel: pauses the job ({@code blocked}), runtime emits |
| 44 | + * {@code human.input.request}. Job is NOT terminated (RFC §10.5). |
| 45 | + */ |
| 46 | + static void interruptJob(ARCPClient client, String jobId, String prompt) { |
| 47 | + // client.send(client.envelope("interrupt", payload=Map.of( |
| 48 | + // "target", "job", "target_id", jobId, "prompt", prompt))); |
| 49 | + if (Map.of(jobId, prompt).isEmpty()) { |
| 50 | + throw new UnsupportedOperationException("interrupt elided"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + static Envelope awaitTerminal(ARCPClient client, String jobId) { |
| 55 | + // for (Envelope env : client.events()) { |
| 56 | + // if (!jobId.equals(env.jobId())) continue; |
| 57 | + // if (TERMINAL.contains(env.type())) return env; |
| 58 | + // } |
| 59 | + throw new UnsupportedOperationException("awaitTerminal " + jobId); |
| 60 | + } |
| 61 | + |
| 62 | + static void scenarioCancel() { |
| 63 | + ARCPClient client = null; // transport, identity, auth elided |
| 64 | + // client.open(); |
| 65 | + try { |
| 66 | + String jobId = startLongJob(client); |
| 67 | + Thread.sleep(2_000); // let the job actually start |
| 68 | + Envelope ack = cancelJob(client, jobId, "user_aborted", CANCEL_DEADLINE_MS); |
| 69 | + System.out.println("cancel ack: " + ack.type()); |
| 70 | + Envelope terminal = awaitTerminal(client, jobId); |
| 71 | + System.out.println("terminal: " + terminal.type()); |
| 72 | + } catch (InterruptedException ie) { |
| 73 | + Thread.currentThread().interrupt(); |
| 74 | + } finally { |
| 75 | + // client.close(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static void scenarioInterrupt() { |
| 80 | + ARCPClient client = null; |
| 81 | + // client.open(); |
| 82 | + try { |
| 83 | + String jobId = startLongJob(client); |
| 84 | + Thread.sleep(2_000); |
| 85 | + interruptJob(client, jobId, "Pause and ask before touching production tables."); |
| 86 | + // Runtime now emits human.input.request; answer via examples/human_input. |
| 87 | + // for (Envelope env : client.events()) { |
| 88 | + // if ("human.input.request".equals(env.type()) && jobId.equals(env.jobId())) { |
| 89 | + // System.out.println("awaiting human: " + env.payload().get("prompt")); |
| 90 | + // return; |
| 91 | + // } |
| 92 | + // } |
| 93 | + } catch (InterruptedException ie) { |
| 94 | + Thread.currentThread().interrupt(); |
| 95 | + } finally { |
| 96 | + // client.close(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static void main(String[] args) { |
| 101 | + String which = args.length > 0 ? args[0] : "cancel"; |
| 102 | + switch (which) { |
| 103 | + case "cancel" -> scenarioCancel(); |
| 104 | + case "interrupt" -> scenarioInterrupt(); |
| 105 | + default -> throw new ARCPException(ErrorCode.INVALID_ARGUMENT, "unknown scenario: " + which); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments