@ricsam/r5dctl 0.0.59 → 0.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mjs/cli.mjs CHANGED
@@ -78,6 +78,19 @@ const K8S_HELP_TEXT = [
78
78
  "Show current and rolling CPU and memory usage for managed-vCluster workload containers.",
79
79
  ""
80
80
  ].join("\n");
81
+ const AGENT_HELP_TEXT = [
82
+ "Usage:",
83
+ ' r5dctl -p <project> agent start --worker <label> --source worktree:<branch> <research|debug|test> "<prompt>"',
84
+ " r5dctl agent status <session-id>",
85
+ ' r5dctl agent prompt --worker <label> <session-id> "<prompt>"',
86
+ " r5dctl agent merge --worker <label> <session-id>",
87
+ " r5dctl agent stop <session-id>",
88
+ ' r5dctl agent finish-merge --worker <label> <resolver-session-id> --summary "<summary>"',
89
+ "",
90
+ "Create, inspect, resume, stop, and merge independent branch agents.",
91
+ "The source is the named branch worktree, including its committed and uncommitted files.",
92
+ ""
93
+ ].join("\n");
81
94
  const SHARED_HELP_ENTRIES = [
82
95
  { section: "auth", usage: "auth status", description: "Show the current authenticated user and credential source." },
83
96
  { section: "auth", usage: "auth logout", description: "Revoke the current credential and clear saved auth." },
@@ -201,18 +214,34 @@ const SHARED_HELP_ENTRIES = [
201
214
  { section: "processes", usage: "ps stop <run-id>", description: "Gracefully stop an active process with SIGTERM." },
202
215
  {
203
216
  section: "agents",
204
- usage: '-p <project> start-agent <src-branch> <agent-type> "<prompt>"',
205
- description: "Start a detached branch agent."
217
+ usage: '-p <project> agent start --worker <label> --source worktree:<branch> <research|debug|test> "<prompt>"',
218
+ description: "Create a branch from an explicit worktree source and start an independent agent."
219
+ },
220
+ {
221
+ section: "agents",
222
+ usage: "agent status <session-id>",
223
+ description: "Show branch-agent execution, publication, and merge status."
224
+ },
225
+ {
226
+ section: "agents",
227
+ usage: 'agent prompt --worker <label> <session-id> "<prompt>"',
228
+ description: "Queue a prompt or resume an idle branch agent on the selected worker."
206
229
  },
207
- { section: "agents", usage: "agent-status <session-id>", description: "Show detached branch agent status." },
208
- { section: "agents", usage: 'send-prompt <session-id> "<prompt>"', description: "Queue or resume a detached branch agent." },
209
230
  {
210
- section: "merges",
211
- usage: "-p <project> merge-changes <target-branch> <sub-agent-branch>",
212
- description: "Squash merge an agent branch into a target branch."
231
+ section: "agents",
232
+ usage: "agent merge --worker <label> <session-id>",
233
+ description: "Merge a terminal branch agent into its recorded parent worktree."
234
+ },
235
+ {
236
+ section: "agents",
237
+ usage: "agent stop <session-id>",
238
+ description: "Stop a branch agent or merge resolver."
213
239
  },
214
- { section: "merges", usage: "-p <project> continue-merge <target-branch>", description: "Commit a resolved merge." },
215
- { section: "merges", usage: "-p <project> abort-merge <target-branch>", description: "Abort an in-progress merge." }
240
+ {
241
+ section: "agents",
242
+ usage: 'agent finish-merge --worker <label> <resolver-session-id> --summary "<summary>"',
243
+ description: "Publish and finalize a manually resolved merge."
244
+ }
216
245
  ];
217
246
  const HELP_SECTION_ORDER = [
218
247
  "auth",
@@ -224,8 +253,7 @@ const HELP_SECTION_ORDER = [
224
253
  "kubernetes",
225
254
  "processes",
226
255
  "shell",
227
- "agents",
228
- "merges"
256
+ "agents"
229
257
  ];
230
258
  const HELP_SECTION_TITLES = {
231
259
  auth: "Auth",
@@ -237,8 +265,7 @@ const HELP_SECTION_TITLES = {
237
265
  kubernetes: "Kubernetes",
238
266
  processes: "Processes",
239
267
  shell: "Shell",
240
- agents: "Agents",
241
- merges: "Merges"
268
+ agents: "Branch agents"
242
269
  };
243
270
  function getCliHelpText() {
244
271
  const entries = [...CLI_ONLY_HELP_ENTRIES, ...SHARED_HELP_ENTRIES];
@@ -337,7 +364,7 @@ function renderCliOnlyCommandHelp(entry) {
337
364
  function renderSharedCommandHelp(pathSegments) {
338
365
  const entry = SHARED_HELP_ENTRIES.map((candidate) => ({
339
366
  candidate,
340
- usageSegments: candidate.usage.split(" ").map((segment) => segment.replace(/^\[/, "").replace(/\]$/, "")).filter((segment) => !segment.startsWith("-") && !segment.startsWith("<") && !segment.startsWith("["))
367
+ usageSegments: candidate.usage.split(" ").map((segment) => segment.replace(/^\[/, "").replace(/\]$/, "")).filter((segment) => !segment.startsWith("-") && !segment.includes("<") && !segment.startsWith("["))
341
368
  })).filter(
342
369
  ({ usageSegments }) => usageSegments.length <= pathSegments.length && usageSegments.every((segment, index) => segment === pathSegments[index])
343
370
  ).sort((left, right) => right.usageSegments.length - left.usageSegments.length)[0]?.candidate;
@@ -385,6 +412,108 @@ function parseOptionalFlagValue(args, flag, shortFlag) {
385
412
  function hasBooleanFlag(args, flag) {
386
413
  return args.includes(flag);
387
414
  }
415
+ function parseAgentOperands(args, valueFlags, options = {}) {
416
+ const positionals = [];
417
+ const values = /* @__PURE__ */ new Map();
418
+ let parseOptions = true;
419
+ for (let index = 0; index < args.length; index += 1) {
420
+ const arg = args[index];
421
+ if (parseOptions && arg === "--") {
422
+ parseOptions = false;
423
+ continue;
424
+ }
425
+ const inlineFlag = parseOptions ? [...valueFlags].find((flag) => arg.startsWith(`${flag}=`)) : void 0;
426
+ if (inlineFlag) {
427
+ if (values.has(inlineFlag)) throw new Error(`${inlineFlag} may only be provided once`);
428
+ const value = arg.slice(inlineFlag.length + 1).trim();
429
+ if (!value) throw new Error(`Missing value for ${inlineFlag}`);
430
+ values.set(inlineFlag, value);
431
+ continue;
432
+ }
433
+ if (parseOptions && valueFlags.has(arg)) {
434
+ if (values.has(arg)) throw new Error(`${arg} may only be provided once`);
435
+ const value = args[index + 1];
436
+ if (!value || value === "--" || valueFlags.has(value)) throw new Error(`Missing value for ${arg}`);
437
+ values.set(arg, value.trim());
438
+ index += 1;
439
+ continue;
440
+ }
441
+ if (parseOptions && arg.startsWith("-") && positionals.length < (options.allowFlagLikeValuesAfter ?? Number.POSITIVE_INFINITY)) {
442
+ throw new Error(`Unknown agent flag: ${arg}`);
443
+ }
444
+ positionals.push(arg);
445
+ }
446
+ return { positionals, values };
447
+ }
448
+ function requireAgentWorker(values) {
449
+ return requireValue(values.get("--worker")?.trim(), "--worker <label> is required");
450
+ }
451
+ function requireAgentSource(values) {
452
+ const raw = requireValue(values.get("--source")?.trim(), "--source worktree:<branch> is required for `agent start`");
453
+ const separator = raw.indexOf(":");
454
+ const type = raw.slice(0, separator);
455
+ const branchName = raw.slice(separator + 1).trim();
456
+ if (type !== "worktree" || separator < 1 || !branchName) {
457
+ throw new Error("Invalid --source. Expected worktree:<branch>");
458
+ }
459
+ return { type, branchName };
460
+ }
461
+ function rejectAgentGlobalScopes(options, allowProject) {
462
+ if (!allowProject && options.project) throw new Error("--project/-p is only supported for `agent start`");
463
+ if (options.branch) throw new Error("--branch/-b is not supported for `agent`; pass --source explicitly");
464
+ if (options.session) throw new Error("--session/-s is not supported for `agent`; pass the session id explicitly");
465
+ }
466
+ function parseBranchAgentCommand(options, args) {
467
+ const subcommand = requireValue(args[0], "Missing agent command");
468
+ const commandArgs = args.slice(1);
469
+ if (subcommand === "start") {
470
+ rejectAgentGlobalScopes(options, true);
471
+ const project = requireValue(options.project, "--project/-p is required for `agent start`");
472
+ const parsed = parseAgentOperands(commandArgs, /* @__PURE__ */ new Set(["--worker", "--source"]), { allowFlagLikeValuesAfter: 1 });
473
+ const agentType = requireValue(parsed.positionals[0], "Agent type is required for `agent start`");
474
+ if (!AGENT_TYPES.has(agentType)) {
475
+ throw new Error("Invalid agent type. Expected one of: research, debug, test");
476
+ }
477
+ const prompt = parsed.positionals.slice(1).join(" ").trim();
478
+ if (!prompt) throw new Error("Agent prompt is required for `agent start`");
479
+ return {
480
+ kind: "start",
481
+ project,
482
+ worker: requireAgentWorker(parsed.values),
483
+ source: requireAgentSource(parsed.values),
484
+ agentType,
485
+ prompt
486
+ };
487
+ }
488
+ rejectAgentGlobalScopes(options, false);
489
+ if (subcommand === "status" || subcommand === "stop") {
490
+ const parsed = parseAgentOperands(commandArgs, /* @__PURE__ */ new Set());
491
+ const sessionId = requireValue(parsed.positionals[0], `Session id is required for \`agent ${subcommand}\``);
492
+ if (parsed.positionals.length > 1) throw new Error(`Unexpected agent ${subcommand} argument: ${parsed.positionals[1]}`);
493
+ return { kind: subcommand, sessionId };
494
+ }
495
+ if (subcommand === "prompt") {
496
+ const parsed = parseAgentOperands(commandArgs, /* @__PURE__ */ new Set(["--worker"]), { allowFlagLikeValuesAfter: 1 });
497
+ const sessionId = requireValue(parsed.positionals[0], "Session id is required for `agent prompt`");
498
+ const prompt = parsed.positionals.slice(1).join(" ").trim();
499
+ if (!prompt) throw new Error("Prompt is required for `agent prompt`");
500
+ return { kind: "prompt", worker: requireAgentWorker(parsed.values), sessionId, prompt };
501
+ }
502
+ if (subcommand === "merge") {
503
+ const parsed = parseAgentOperands(commandArgs, /* @__PURE__ */ new Set(["--worker"]));
504
+ const sessionId = requireValue(parsed.positionals[0], "Session id is required for `agent merge`");
505
+ if (parsed.positionals.length > 1) throw new Error(`Unexpected agent merge argument: ${parsed.positionals[1]}`);
506
+ return { kind: "merge", worker: requireAgentWorker(parsed.values), sessionId };
507
+ }
508
+ if (subcommand === "finish-merge") {
509
+ const parsed = parseAgentOperands(commandArgs, /* @__PURE__ */ new Set(["--worker", "--summary"]));
510
+ const resolverSessionId = requireValue(parsed.positionals[0], "Resolver session id is required for `agent finish-merge`");
511
+ if (parsed.positionals.length > 1) throw new Error(`Unexpected agent finish-merge argument: ${parsed.positionals[1]}`);
512
+ const summary = requireValue(parsed.values.get("--summary")?.trim(), "--summary <text> is required for `agent finish-merge`");
513
+ return { kind: "finish-merge", worker: requireAgentWorker(parsed.values), resolverSessionId, summary };
514
+ }
515
+ throw new Error(`Unknown agent command: ${subcommand}`);
516
+ }
388
517
  function assertAuthLoginArgs(args) {
389
518
  const valueFlags = /* @__PURE__ */ new Set(["--device-name", "--worker-label"]);
390
519
  const booleanFlags = /* @__PURE__ */ new Set(["--no-open", "--no-qr"]);
@@ -1160,7 +1289,7 @@ function renderWorkspaceStatus(status) {
1160
1289
  `Remediation: ${status.incident ? `${status.incident.kind} ${status.incident.status} (${status.incident.id}) on ${status.incident.originWorkerLabel}` : "none"}`,
1161
1290
  `Materialization: ${status.materialization ? `${status.materialization.state}; ${status.materialization.materializedHead ?? "(none)"} -> ${status.materialization.desiredHead ?? "(none)"}; lag ${status.materialization.lagMs}ms` : "not initialized"}`,
1162
1291
  ...status.workers.map(
1163
- (worker) => `Worker ${worker.workerLabel}: ${worker.ready ? "ready" : "bootstrapping"}; ${worker.checkpointState}; head ${worker.localHead ?? "(none)"}; desired ${worker.desiredCanonicalHead ?? "(none)"}; generations ${worker.publishedGeneration}/${worker.dirtyGeneration}; pending checkouts ${worker.pendingCheckouts.length}; CAS retries ${worker.casRetries}; fan-out lag ${worker.fanoutLagMs ?? "unknown"}ms`
1292
+ (worker) => `Worker ${worker.workerLabel}: ${worker.ready ? "ready" : "bootstrapping"}; publication ${worker.publicationState}; head ${worker.localHead ?? "(none)"}; desired ${worker.desiredCanonicalHead ?? "(none)"}; generations ${worker.publishedGeneration}/${worker.dirtyGeneration}; pending checkouts ${worker.pendingCheckouts.length}; CAS retries ${worker.casRetries}; fan-out lag ${worker.fanoutLagMs ?? "unknown"}ms`
1164
1293
  )
1165
1294
  ];
1166
1295
  return `${lines.join("\n")}
@@ -1530,43 +1659,164 @@ async function setProjectEnvs(client, projectRef, args) {
1530
1659
  }
1531
1660
  return result;
1532
1661
  }
1533
- function renderAgentStart(agent) {
1534
- return [
1535
- `Agent: ${agent.sessionId}`,
1536
- `Status: ${agent.status}`,
1537
- `Branch: ${agent.branchName}`,
1538
- `Base: ${agent.baseBranch} @ ${agent.baseCommit}`
1539
- ].join("\n") + "\n";
1540
- }
1541
- function renderAgentStatus(agent) {
1542
- const lines = [`Agent: ${agent.sessionId}`, `Status: ${agent.status}`, `Branch: ${agent.branchName}`, `Head: ${agent.headCommit}`];
1543
- if (agent.baseBranch && agent.baseCommit) {
1544
- lines.push(`Base: ${agent.baseBranch} @ ${agent.baseCommit}`);
1545
- }
1546
- if (agent.error) {
1547
- lines.push(`Error: ${agent.error}`);
1548
- }
1549
- if (agent.diffSummary) {
1550
- lines.push("", agent.diffSummary);
1662
+ function responseString(response, key) {
1663
+ const value = response[key];
1664
+ return typeof value === "string" && value.length > 0 ? value : void 0;
1665
+ }
1666
+ function responseSource(response) {
1667
+ const source = response.source;
1668
+ if (!source || source.type !== "worktree" || !source.branchName) return void 0;
1669
+ return `${source.type}:${source.branchName}`;
1670
+ }
1671
+ function formatStatusValue(value) {
1672
+ if (typeof value === "string" && value.length > 0) return value;
1673
+ if (typeof value === "object" && value !== null) {
1674
+ const status = value.status;
1675
+ if (typeof status === "string" && status.length > 0) return status;
1676
+ return JSON.stringify(value);
1677
+ }
1678
+ return value === void 0 || value === null ? void 0 : String(value);
1679
+ }
1680
+ function asResponseRecord(value) {
1681
+ return typeof value === "object" && value !== null ? value : void 0;
1682
+ }
1683
+ function appendMergeDetails(lines, response) {
1684
+ const merge = asResponseRecord(response.merge);
1685
+ if (!merge) return;
1686
+ const mergeStatus = formatStatusValue(merge.status);
1687
+ lines.push("", `Merge: ${mergeStatus ?? "pending"}`);
1688
+ for (const [label, key] of [
1689
+ ["Attempt", "attemptId"],
1690
+ ["Source", "sourceBranch"],
1691
+ ["Target", "targetBranch"],
1692
+ ["Commit", "commitHash"],
1693
+ ["Resolver agent", "resolverSessionId"],
1694
+ ["Resolver branch", "resolverBranch"],
1695
+ ["Resolver worktree", "folderPath"]
1696
+ ]) {
1697
+ const value = responseString(merge, key);
1698
+ if (value) lines.push(`${label}: ${value}`);
1699
+ }
1700
+ const conflictedFiles = Array.isArray(merge.conflictedFiles) ? merge.conflictedFiles.filter((file) => typeof file === "string") : [];
1701
+ if (conflictedFiles.length > 0) {
1702
+ lines.push("", "Conflicts:", ...conflictedFiles.map((file) => `- ${file}`));
1703
+ }
1704
+ const message = responseString(merge, "message");
1705
+ if (message) lines.push("", message);
1706
+ const failureReason = responseString(merge, "failureReason");
1707
+ if (failureReason) lines.push(`Failure: ${failureReason}`);
1708
+ const resolverSessionId = responseString(merge, "resolverSessionId");
1709
+ const worker = responseString(merge, "worker") ?? responseString(response, "worker");
1710
+ if (resolverSessionId && worker) {
1711
+ lines.push(
1712
+ "",
1713
+ "Resolver takeover:",
1714
+ ` r5dctl agent stop ${resolverSessionId}`,
1715
+ ` r5dctl agent finish-merge --worker ${worker} ${resolverSessionId} --summary "<summary>"`
1716
+ );
1551
1717
  }
1552
- if (agent.summary) {
1553
- lines.push("", agent.summary);
1718
+ }
1719
+ function renderBranchAgentStart(agent) {
1720
+ const lines = [
1721
+ `Agent: ${responseString(agent, "sessionId") ?? "(unknown)"}`,
1722
+ `Status: ${formatStatusValue(agent.status) ?? "queued"}`,
1723
+ `Branch: ${responseString(agent, "branchName") ?? "(provisioning)"}`,
1724
+ `Source: ${responseSource(agent) ?? "(unknown)"}`,
1725
+ `Workspace head: ${responseString(agent, "workspaceHead") ?? "(pending)"}`,
1726
+ `Baseline: ${responseString(agent, "baselineCommit") ?? "(pending)"}`,
1727
+ `Worker: ${responseString(agent, "worker") ?? "(unassigned)"}`
1728
+ ];
1729
+ const sessionUrl = responseString(agent, "sessionUrl");
1730
+ if (sessionUrl) lines.push(`Session: ${sessionUrl}`);
1731
+ const sessionId = responseString(agent, "sessionId");
1732
+ if (sessionId) lines.push("", `Next: r5dctl agent status ${sessionId}`);
1733
+ return `${lines.join("\n")}
1734
+ `;
1735
+ }
1736
+ function renderBranchAgentStatus(agent) {
1737
+ const status = formatStatusValue(agent.status) ?? "unknown";
1738
+ const lines = [`Agent: ${responseString(agent, "sessionId") ?? "(unknown)"}`, `Status: ${status}`];
1739
+ const runStatus = formatStatusValue(agent.runStatus);
1740
+ if (runStatus) lines.push(`Run: ${runStatus}`);
1741
+ const promptDisposition = responseString(agent, "promptDisposition");
1742
+ if (promptDisposition) lines.push(`Prompt: ${promptDisposition}`);
1743
+ const branch = responseString(agent, "branchName");
1744
+ if (branch) lines.push(`Branch: ${branch}`);
1745
+ const source = responseSource(agent);
1746
+ if (source) lines.push(`Source: ${source}`);
1747
+ for (const [label, key] of [
1748
+ ["Workspace head", "workspaceHead"],
1749
+ ["Baseline", "baselineCommit"],
1750
+ ["Head", "headCommit"],
1751
+ ["Worker", "worker"],
1752
+ ["Session", "sessionUrl"]
1753
+ ]) {
1754
+ const value = responseString(agent, key);
1755
+ if (value) lines.push(`${label}: ${value}`);
1756
+ }
1757
+ const error = responseString(agent, "error");
1758
+ if (error) lines.push(`Error: ${error}`);
1759
+ const headCommitError = responseString(agent, "headCommitError");
1760
+ if (headCommitError) lines.push(`Head error: ${headCommitError}`);
1761
+ const diffSummary = responseString(agent, "diffSummary");
1762
+ if (diffSummary) lines.push("", diffSummary);
1763
+ const summary = responseString(agent, "summary");
1764
+ if (summary) lines.push("", summary);
1765
+ appendMergeDetails(lines, agent);
1766
+ const sessionId = responseString(agent, "sessionId");
1767
+ if (sessionId && !asResponseRecord(agent.merge)) {
1768
+ if (["provisioning", "queued", "running"].includes(status)) {
1769
+ lines.push("", "Next:", ` r5dctl agent status ${sessionId}`, ` r5dctl agent stop ${sessionId}`);
1770
+ } else if (["completed", "failed", "stopped"].includes(status)) {
1771
+ const worker = responseString(agent, "worker") ?? "<worker>";
1772
+ lines.push("", "Next:", ` r5dctl agent merge --worker ${worker} ${sessionId}`);
1773
+ }
1554
1774
  }
1555
1775
  return `${lines.join("\n")}
1556
1776
  `;
1557
1777
  }
1558
- function renderMergeResult(result) {
1559
- if (result.status === "merged") {
1560
- return `Merged ${result.sourceBranch} into ${result.targetBranch}: ${result.commitHash}
1561
- ${result.message}
1778
+ function renderBranchAgentMerge(result) {
1779
+ if (asResponseRecord(result.merge)) return renderBranchAgentStatus(result);
1780
+ const wrapped = {
1781
+ sessionId: result.sessionId,
1782
+ status: result.status,
1783
+ worker: result.worker,
1784
+ merge: result
1785
+ };
1786
+ const lines = [];
1787
+ appendMergeDetails(lines, wrapped);
1788
+ return `${lines.slice(lines[0] === "" ? 1 : 0).join("\n")}
1562
1789
  `;
1790
+ }
1791
+ async function dispatchBranchAgentCommand(client, command) {
1792
+ if (command.kind === "start") {
1793
+ const data2 = await client.projects.agents.start(command.project, {
1794
+ source: command.source,
1795
+ agentType: command.agentType,
1796
+ prompt: command.prompt,
1797
+ worker: command.worker,
1798
+ requestId: randomUUID()
1799
+ });
1800
+ return { data: data2, human: renderBranchAgentStart(data2) };
1563
1801
  }
1564
- const files = result.conflictedFiles.length > 0 ? `
1565
- Conflicts:
1566
- ${result.conflictedFiles.map((file) => `- ${file}`).join("\n")}
1567
- ` : "\n";
1568
- return `Merge has conflicts from ${result.sourceBranch} into ${result.targetBranch}.${files}${result.message}
1569
- `;
1802
+ if (command.kind === "status") {
1803
+ const data2 = await client.agents.status(command.sessionId);
1804
+ return { data: data2, human: renderBranchAgentStatus(data2) };
1805
+ }
1806
+ if (command.kind === "prompt") {
1807
+ const data2 = await client.agents.sendPrompt(command.sessionId, { prompt: command.prompt, worker: command.worker });
1808
+ return { data: data2, human: renderBranchAgentStatus(data2) };
1809
+ }
1810
+ if (command.kind === "merge") {
1811
+ const data2 = await client.agents.merge(command.sessionId, { worker: command.worker });
1812
+ return { data: data2, human: renderBranchAgentMerge(data2) };
1813
+ }
1814
+ if (command.kind === "stop") {
1815
+ const data2 = await client.agents.stop(command.sessionId);
1816
+ return { data: data2, human: renderBranchAgentStatus(data2) };
1817
+ }
1818
+ const data = await client.agents.finishMerge(command.resolverSessionId, { worker: command.worker, summary: command.summary });
1819
+ return { data, human: renderBranchAgentMerge(data) };
1570
1820
  }
1571
1821
  function parseProjectUpdateArgs(args) {
1572
1822
  const input = {};
@@ -1866,65 +2116,6 @@ Sessions: ${result.sessions.length}
1866
2116
  write(session, renderSessionDescription(session));
1867
2117
  return;
1868
2118
  }
1869
- if (first === "start-agent") {
1870
- const projectRef = requireValue(args[1], "Missing project reference");
1871
- const sourceBranch = requireValue(args[2], "Missing source branch");
1872
- const agentType = requireValue(args[3], "Missing agent type");
1873
- if (!AGENT_TYPES.has(agentType)) {
1874
- throw new Error("Invalid agent type. Expected one of: research, debug, test");
1875
- }
1876
- const prompt = args.slice(4).join(" ").trim();
1877
- if (!prompt) {
1878
- throw new Error("Agent prompt is required");
1879
- }
1880
- const agent = await client.projects.agents.start(projectRef, {
1881
- sourceBranch,
1882
- agentType,
1883
- prompt
1884
- });
1885
- write(agent, renderAgentStart(agent));
1886
- return;
1887
- }
1888
- if (first === "agent-status") {
1889
- const agent = await client.agents.status(requireValue(args[1], "Missing session id"));
1890
- write(agent, renderAgentStatus(agent));
1891
- return;
1892
- }
1893
- if (first === "send-prompt") {
1894
- const sessionId = requireValue(args[1], "Missing session id");
1895
- const prompt = args.slice(2).join(" ").trim();
1896
- if (!prompt) {
1897
- throw new Error("Prompt is required");
1898
- }
1899
- const agent = await client.agents.sendPrompt(sessionId, { prompt });
1900
- write(agent, renderAgentStatus(agent));
1901
- return;
1902
- }
1903
- if (first === "merge-changes") {
1904
- const result = await client.projects.mergeChanges(requireValue(args[1], "Missing project reference"), {
1905
- targetBranch: requireValue(args[2], "Missing target branch"),
1906
- sourceBranch: requireValue(args[3], "Missing source branch")
1907
- });
1908
- write(result, renderMergeResult(result));
1909
- return;
1910
- }
1911
- if (first === "continue-merge") {
1912
- const result = await client.projects.continueMerge(requireValue(args[1], "Missing project reference"), {
1913
- targetBranch: requireValue(args[2], "Missing target branch")
1914
- });
1915
- write(result, `Merge committed: ${result.commitHash}
1916
- ${result.message}
1917
- `);
1918
- return;
1919
- }
1920
- if (first === "abort-merge") {
1921
- const result = await client.projects.abortMerge(requireValue(args[1], "Missing project reference"), {
1922
- targetBranch: requireValue(args[2], "Missing target branch")
1923
- });
1924
- write(result, `${result.message}
1925
- `);
1926
- return;
1927
- }
1928
2119
  if (first === "sessions" && second === "describe" || first === "describe" && second === "session") {
1929
2120
  const session = await client.sessions.describe(requireValue(args[2], "Missing session id"));
1930
2121
  write(session, renderSessionDescription(session));
@@ -2020,6 +2211,12 @@ function resolveCommandExecution(options, rest) {
2020
2211
  text: K8S_HELP_TEXT
2021
2212
  };
2022
2213
  }
2214
+ if (command === "agent" && commandArgs.length === 0) {
2215
+ return {
2216
+ kind: "cli-help",
2217
+ text: AGENT_HELP_TEXT
2218
+ };
2219
+ }
2023
2220
  if (trailingHelp) {
2024
2221
  const cliOnlyHelp = findCliOnlyCommandHelp(normalizedRest);
2025
2222
  if (cliOnlyHelp) {
@@ -2424,91 +2621,8 @@ function resolveCommandExecution(options, rest) {
2424
2621
  pluginArgs: ["prompt", sessionId, parsed.mode, parsed.model, parsed.message]
2425
2622
  };
2426
2623
  }
2427
- if (command === "start-agent") {
2428
- if (!options.project) {
2429
- throw new Error("--project/-p is required for `start-agent`");
2430
- }
2431
- const sourceBranch = commandArgs[0] ?? options.branch;
2432
- if (!sourceBranch) {
2433
- throw new Error("Source branch is required for `start-agent`");
2434
- }
2435
- const agentType = requireValue(commandArgs[1], "Agent type is required for `start-agent`");
2436
- if (!AGENT_TYPES.has(agentType)) {
2437
- throw new Error("Invalid agent type. Expected one of: research, debug, test");
2438
- }
2439
- const prompt = commandArgs.slice(2).join(" ").trim();
2440
- if (!prompt) {
2441
- throw new Error("Agent prompt is required");
2442
- }
2443
- return {
2444
- kind: "plugin",
2445
- pluginArgs: ["start-agent", options.project, sourceBranch, agentType, prompt]
2446
- };
2447
- }
2448
- if (command === "agent-status") {
2449
- const sessionId = commandArgs[0] ?? options.session;
2450
- if (!sessionId) {
2451
- throw new Error("Session id is required for `agent-status`");
2452
- }
2453
- return {
2454
- kind: "plugin",
2455
- pluginArgs: ["agent-status", sessionId]
2456
- };
2457
- }
2458
- if (command === "send-prompt") {
2459
- const sessionId = options.session ?? commandArgs[0];
2460
- if (!sessionId) {
2461
- throw new Error("Session id is required for `send-prompt`");
2462
- }
2463
- const promptArgs = options.session ? commandArgs : commandArgs.slice(1);
2464
- const prompt = promptArgs.join(" ").trim();
2465
- if (!prompt) {
2466
- throw new Error("Prompt is required for `send-prompt`");
2467
- }
2468
- return {
2469
- kind: "plugin",
2470
- pluginArgs: ["send-prompt", sessionId, prompt]
2471
- };
2472
- }
2473
- if (command === "merge-changes") {
2474
- if (!options.project) {
2475
- throw new Error("--project/-p is required for `merge-changes`");
2476
- }
2477
- const targetBranch = commandArgs[0] ?? options.branch;
2478
- if (!targetBranch) {
2479
- throw new Error("Target branch is required for `merge-changes`");
2480
- }
2481
- const sourceBranch = requireValue(commandArgs[1], "Sub-agent branch is required for `merge-changes`");
2482
- return {
2483
- kind: "plugin",
2484
- pluginArgs: ["merge-changes", options.project, targetBranch, sourceBranch]
2485
- };
2486
- }
2487
- if (command === "continue-merge") {
2488
- if (!options.project) {
2489
- throw new Error("--project/-p is required for `continue-merge`");
2490
- }
2491
- const targetBranch = commandArgs[0] ?? options.branch;
2492
- if (!targetBranch) {
2493
- throw new Error("Target branch is required for `continue-merge`");
2494
- }
2495
- return {
2496
- kind: "plugin",
2497
- pluginArgs: ["continue-merge", options.project, targetBranch]
2498
- };
2499
- }
2500
- if (command === "abort-merge") {
2501
- if (!options.project) {
2502
- throw new Error("--project/-p is required for `abort-merge`");
2503
- }
2504
- const targetBranch = commandArgs[0] ?? options.branch;
2505
- if (!targetBranch) {
2506
- throw new Error("Target branch is required for `abort-merge`");
2507
- }
2508
- return {
2509
- kind: "plugin",
2510
- pluginArgs: ["abort-merge", options.project, targetBranch]
2511
- };
2624
+ if (command === "agent") {
2625
+ return { kind: "agent", command: parseBranchAgentCommand(options, commandArgs) };
2512
2626
  }
2513
2627
  if (command === "answer-questions") {
2514
2628
  const sessionId = options.session;
@@ -2558,6 +2672,11 @@ async function runCommand(argv) {
2558
2672
  process.stdout.write(execution.text);
2559
2673
  return 0;
2560
2674
  }
2675
+ if (execution.kind === "agent") {
2676
+ const result = await dispatchBranchAgentCommand(client, execution.command);
2677
+ writeDataOutput(options.json, result.data, result.human);
2678
+ return 0;
2679
+ }
2561
2680
  if (execution.kind === "shell") {
2562
2681
  const project = await client.projects.describe(options.project);
2563
2682
  return await runR5dctlShell({
@@ -2606,6 +2725,7 @@ async function main(argv = process.argv.slice(2)) {
2606
2725
  export {
2607
2726
  advanceTransientDevicePollBackoff,
2608
2727
  collectK8sUsage,
2728
+ dispatchBranchAgentCommand,
2609
2729
  formatK8sCpu,
2610
2730
  formatK8sMemory,
2611
2731
  formatProcessAge,
@@ -2617,6 +2737,7 @@ export {
2617
2737
  handleAuthLogin,
2618
2738
  main,
2619
2739
  parseAnswerFlags,
2740
+ parseBranchAgentCommand,
2620
2741
  parseConversationRenderArgs,
2621
2742
  parseConversationWorkDetailArgs,
2622
2743
  parseEnvFlags,
@@ -2630,6 +2751,9 @@ export {
2630
2751
  parseSetEnvArgs,
2631
2752
  parseShellArgs,
2632
2753
  readDotenvFile,
2754
+ renderBranchAgentMerge,
2755
+ renderBranchAgentStart,
2756
+ renderBranchAgentStatus,
2633
2757
  renderConversationNodeResponse,
2634
2758
  renderConversationOverviewResponse,
2635
2759
  renderConversationResponse,
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/r5dctl",
3
- "version": "0.0.59",
3
+ "version": "0.0.60",
4
4
  "type": "module"
5
5
  }