Skip to content

feat(cron): add acpTurn payload kind for one-shot ACP runs - #44214

Closed
shiwenwen wants to merge 1 commit into
openclaw:mainfrom
shiwenwen:feat/cron-acp-turn
Closed

shiwenwen wants to merge 1 commit into
openclaw:mainfrom
shiwenwen:feat/cron-acp-turn

Conversation

@shiwenwen

Copy link
Copy Markdown

Summary

  • Adds a new acpTurn cron payload kind for directly triggering one-shot ACP runs (Claude Code, Codex, Gemini CLI, etc.) without going through systemEvent or a generic agentTurn workaround.
  • acpTurn is restricted to sessionTarget: "isolated", uses the real ACP session manager control plane (initializeSession({ mode: "oneshot" }) + runTurn()), and enforces ACP policy / agent allowlist before execution.
  • Reuses existing cron delivery semantics (none / announce / webhook) unchanged.
  • Explicit error returned when ACP is disabled by policy or backend is unavailable.
  • UI: cron form extended with acpTurn payload selector, ACP agent ID field, i18n (en + zh-CN).

Known MVP limitations

  • payload.model not yet forwarded to ACP session init.
  • Token usage telemetry always empty for ACP runs (follow-up).
  • No streaming delivery during run (buffered completion only).
  • No thread-bound or persistent ACP cron sessions (intentional scope).

Test plan

  • pnpm test src/cron/cron-acp-turn.test.ts - 20/20 passing
  • pnpm test src/cron/ ui/src/ui/ - 567/567 passing
  • pnpm tsgo - typecheck clean
  • pnpm check - lint + format clean
  • Manual UX: cron form acpTurn selector, ACP agent field, save/edit roundtrip

Co-authored-by: shiwenwen [email protected]

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui gateway Gateway runtime agents Agent runtime and tooling size: L labels Mar 12, 2026
@shiwenwen shiwenwen closed this Mar 12, 2026

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

Copy link
Copy Markdown
Contributor

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: e0340eb7f8

ℹ️ 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".

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

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +572 to +574
if (form.payloadKind === "acpTurn") {
const acpPayload: {
kind: "acpTurn";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Badge Accept acpTurn in gateway cron payload validators

This path now builds and submits payload.kind="acpTurn", but the gateway validator still only accepts systemEvent/agentTurn in src/gateway/protocol/schema/cron.ts (CronPayloadSchema/CronPayloadPatchSchema). In the gateway context I inspected (src/gateway/server-methods/cron.ts), cron.add/cron.update validate before calling CronService, so any UI/tool request using acpTurn is rejected as invalid cron.* params and the new feature cannot be created/updated through the API.

Useful? React with 👍 / 👎.

Comment on lines +70 to +72
const resolvedAcpAgentId = (payload.acpAgentId?.trim() || "").toLowerCase() || undefined;
if (resolvedAcpAgentId) {
const agentPolicyError = resolveAcpAgentPolicyError(params.cfg, resolvedAcpAgentId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Badge Enforce ACP allowlist for implicit acpTurn agent

The allowlist check only runs when payload.acpAgentId is explicitly set, but this function later falls back to agent: resolvedAcpAgentId ?? "claude" during session init. That means jobs omitting acpAgentId skip acp.allowedAgents enforcement and can execute an unintended harness (or fail with a confusing init error) instead of honoring policy/default-agent constraints.

Useful? React with 👍 / 👎.

@greptile-apps

greptile-apps Bot commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a first-class acpTurn payload kind for cron jobs, enabling one-shot ACP harness runs (Claude Code, Codex, Gemini CLI, etc.) directly from the cron scheduler — without piggybacking on agentTurn or systemEvent. The implementation is well-scoped, follows existing patterns, and ships with a comprehensive test suite (20 new focused tests). The integration spans types, normalization, validation, execution routing, gateway wiring, UI form, and i18n.

Key changes:

  • src/cron/isolated-agent/run-acp.ts (new): Core ACP execution path. Policy check → initializeSession(mode:"oneshot") → runTurn → delivery dispatch.
  • src/cron/service/timer.ts: executeJobCore routes acpTurn to the new runAcpJob dep before falling through to the agentTurn path; explicit error when runAcpJob is not configured.
  • src/cron/service/jobs.ts: assertSupportedJobSpec checks acpTurn requires isolated first (best error message); mergeCronPayload and buildPayloadFromPatch handle the new payload type.
  • src/cron/normalize.ts: Case-insensitive normalization and auto-defaulting of sessionTarget / delivery for acpTurn.
  • UI + i18n: CronFormState.payloadAcpAgentId added, form rendering updated, en + zh-CN translations added.

Issues found:

  • Potential resource leak (run-acp.ts lines 188–195): when runTurn throws and turnError is set, the function returns without calling acpManager.closeSession. The abort path (lines 146–152) correctly calls closeSession; the error path does not. For oneshot mode sessions this may be harmless if the manager auto-closes, but the asymmetry is suspicious and could leak ACP backend processes under repeated failures.
  • Misleading comment (run-acp.ts line 114): "so a bad target fails early" — resolvedDelivery.ok is never checked before ACP execution, so the delivery failure is only discovered inside dispatchCronDelivery after the turn completes. The behavior is consistent with run.ts, but the comment sets a false expectation.
  • Unused lane parameter in RunCronAcpTurnParams: accepted and passed by the gateway but never read or forwarded inside runCronAcpTurn.

Confidence Score: 4/5

  • This PR is safe to merge with one issue worth addressing before production: the missing closeSession call on runTurn error in run-acp.ts.
  • The implementation is well-structured and follows existing cron patterns. The test suite is comprehensive (20 new focused tests covering validation, normalization, routing, and error cases). The main concern is a potential ACP backend process leak when runTurn throws, because the session is not explicitly closed on that code path — unlike the abort path which does call closeSession. In oneshot mode this may be auto-handled, but the inconsistency is concerning enough to reduce confidence below 5. All other code (types, normalization, validation, routing, gateway wiring, UI, i18n) looks correct and complete.
  • src/cron/isolated-agent/run-acp.ts — review the turnError return path for missing closeSession call
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/cron/isolated-agent/run-acp.ts
Line: 114-121

Comment:
**Misleading "fail early" comment**

The comment says "Resolve delivery target before execution so a bad target fails early", but `resolvedDelivery.ok` is never checked here. The ACP session is initialized and `runTurn` is called even when the delivery target resolution failed — the `ok: false` case is only caught inside `dispatchCronDelivery` well after the turn completes.

This is consistent with how `run.ts` behaves (the check also happens in `dispatchCronDelivery` there), but the comment creates a false expectation of an early-exit guard. Consider either:
- Removing the "fail early" qualifier so the comment simply describes intent, or
- Adding an explicit check: `if (!resolvedDelivery.ok) { return { status: "error", error: ..., errorKind: "delivery-target" }; }` before `getAcpSessionManager()` to match the comment's promise.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/cron/isolated-agent/run-acp.ts
Line: 17-26

Comment:
**`lane` parameter accepted but never used**

`RunCronAcpTurnParams` declares `lane?: string` and `server-cron.ts` passes `lane: "cron"`, but `runCronAcpTurn` never reads or forwards it. For `runCronIsolatedAgentTurn` the `lane` is used for sub-agent concurrency tracking and nested agent lane resolution — functionality that doesn't apply to ACP runs.

If ACP runs are intentionally exempt from lane tracking, a brief comment explaining why would prevent future confusion. Otherwise, if the lane is meant to be forwarded to `initializeSession` for backend routing, it's currently silently dropped.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/cron/isolated-agent/run-acp.ts
Line: 188-195

Comment:
**ACP session not closed on `runTurn` error**

When `acpManager.runTurn` throws (setting `turnError`), the function returns an error without calling `acpManager.closeSession`. In contrast, the abort-check path above explicitly calls `closeSession` with a best-effort catch before returning.

For a `mode: "oneshot"` session the manager *may* auto-close after a failed turn, but if `runTurn` throws before the backend triggers its own cleanup, the session process may linger. This asymmetry compared to the abort path looks unintentional and could leak ACP backend processes across repeated cron job failures.

Consider adding a best-effort `closeSession` call here to mirror the abort path, passing `reason: "cron-turn-error"`. This is a low-cost defensive guard.

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: e0340eb

Comment on lines +114 to +121
// Resolve delivery target before execution so a bad target fails early.
const deliveryPlan = resolveCronDeliveryPlan(params.job);
const resolvedDelivery = await resolveDeliveryTarget(params.cfg, agentId, {
channel: deliveryPlan.channel ?? "last",
to: deliveryPlan.to,
accountId: deliveryPlan.accountId,
sessionKey: params.job.sessionKey,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Misleading "fail early" comment

The comment says "Resolve delivery target before execution so a bad target fails early", but resolvedDelivery.ok is never checked here. The ACP session is initialized and runTurn is called even when the delivery target resolution failed — the ok: false case is only caught inside dispatchCronDelivery well after the turn completes.

This is consistent with how run.ts behaves (the check also happens in dispatchCronDelivery there), but the comment creates a false expectation of an early-exit guard. Consider either:

  • Removing the "fail early" qualifier so the comment simply describes intent, or
  • Adding an explicit check: if (!resolvedDelivery.ok) { return { status: "error", error: ..., errorKind: "delivery-target" }; } before getAcpSessionManager() to match the comment's promise.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cron/isolated-agent/run-acp.ts
Line: 114-121

Comment:
**Misleading "fail early" comment**

The comment says "Resolve delivery target before execution so a bad target fails early", but `resolvedDelivery.ok` is never checked here. The ACP session is initialized and `runTurn` is called even when the delivery target resolution failed — the `ok: false` case is only caught inside `dispatchCronDelivery` well after the turn completes.

This is consistent with how `run.ts` behaves (the check also happens in `dispatchCronDelivery` there), but the comment creates a false expectation of an early-exit guard. Consider either:
- Removing the "fail early" qualifier so the comment simply describes intent, or
- Adding an explicit check: `if (!resolvedDelivery.ok) { return { status: "error", error: ..., errorKind: "delivery-target" }; }` before `getAcpSessionManager()` to match the comment's promise.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +17 to +26
export type RunCronAcpTurnParams = {
cfg: OpenClawConfig;
deps: CliDeps;
job: CronJob; // payload.kind must be "acpTurn"
message: string;
abortSignal?: AbortSignal;
agentId?: string;
sessionKey: string;
lane?: string;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lane parameter accepted but never used

RunCronAcpTurnParams declares lane?: string and server-cron.ts passes lane: "cron", but runCronAcpTurn never reads or forwards it. For runCronIsolatedAgentTurn the lane is used for sub-agent concurrency tracking and nested agent lane resolution — functionality that doesn't apply to ACP runs.

If ACP runs are intentionally exempt from lane tracking, a brief comment explaining why would prevent future confusion. Otherwise, if the lane is meant to be forwarded to initializeSession for backend routing, it's currently silently dropped.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cron/isolated-agent/run-acp.ts
Line: 17-26

Comment:
**`lane` parameter accepted but never used**

`RunCronAcpTurnParams` declares `lane?: string` and `server-cron.ts` passes `lane: "cron"`, but `runCronAcpTurn` never reads or forwards it. For `runCronIsolatedAgentTurn` the `lane` is used for sub-agent concurrency tracking and nested agent lane resolution — functionality that doesn't apply to ACP runs.

If ACP runs are intentionally exempt from lane tracking, a brief comment explaining why would prevent future confusion. Otherwise, if the lane is meant to be forwarded to `initializeSession` for backend routing, it's currently silently dropped.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +188 to +195
if (turnError) {
return {
status: "error",
error: `ACP turn failed (${turnError.code}): ${turnError.message}`,
summary: outputText,
sessionKey: agentSessionKey,
};
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACP session not closed on runTurn error

When acpManager.runTurn throws (setting turnError), the function returns an error without calling acpManager.closeSession. In contrast, the abort-check path above explicitly calls closeSession with a best-effort catch before returning.

For a mode: "oneshot" session the manager may auto-close after a failed turn, but if runTurn throws before the backend triggers its own cleanup, the session process may linger. This asymmetry compared to the abort path looks unintentional and could leak ACP backend processes across repeated cron job failures.

Consider adding a best-effort closeSession call here to mirror the abort path, passing reason: "cron-turn-error". This is a low-cost defensive guard.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cron/isolated-agent/run-acp.ts
Line: 188-195

Comment:
**ACP session not closed on `runTurn` error**

When `acpManager.runTurn` throws (setting `turnError`), the function returns an error without calling `acpManager.closeSession`. In contrast, the abort-check path above explicitly calls `closeSession` with a best-effort catch before returning.

For a `mode: "oneshot"` session the manager *may* auto-close after a failed turn, but if `runTurn` throws before the backend triggers its own cleanup, the session process may linger. This asymmetry compared to the abort path looks unintentional and could leak ACP backend processes across repeated cron job failures.

Consider adding a best-effort `closeSession` call here to mirror the abort path, passing `reason: "cron-turn-error"`. This is a low-cost defensive guard.

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling app: web-ui App: web-ui gateway Gateway runtime size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant