tasks: add detached runtime plugin registration contract#68915
Conversation
6e5b5d8 to
888f9f4
Compare
Greptile SummaryThis PR adds the explicit plugin registration contract for detached task runtimes: a new state module ( Confidence Score: 5/5Safe to merge; all remaining findings are P2 style/design suggestions with no blocking correctness issues. The implementation is well-structured with clean separation between state, dispatch, and routing layers. Loader rollback/restore coverage is thorough across all four identified paths. Both open observations are P2 and do not affect correctness under the stated design invariants. src/plugins/registry.ts (same-plugin re-registration), src/tasks/task-executor.ts (early-return in cancelDetachedTaskRunById) Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/plugins/registry.ts
Line: 1176-1188
Comment:
**Same-plugin silent re-registration**
The guard `existing.pluginId !== record.id` allows the same plugin to call `registerDetachedTaskRuntime` multiple times, silently replacing the prior runtime without any diagnostic. If a plugin's activation path calls this twice (e.g., due to a misconfigured re-entrant load), the second registration overwrites the first with no warning — the same protection offered to *other* plugins is absent for the registering plugin itself. Consider emitting a warning-level diagnostic or making the second call a no-op when the plugin id already matches.
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/tasks/task-executor.ts
Line: 711-724
Comment:
**Registered runtime bypassed for unknown tasks**
When `getTaskById` returns `undefined` the function immediately falls back to `cancelTaskById`, never consulting the registered runtime. If a task were ever created through the registered runtime before it appeared in the core registry (or after it was removed from it), the registered runtime's cancel logic would be skipped entirely. The fallback is correct by current design invariants, but the early-return bypasses the runtime contract the rest of the function enforces.
```typescript
// current
if (!task) {
return cancelTaskById(params);
}
// would be more consistent with the contract:
if (!task) {
const registeredRuntime = getRegisteredDetachedTaskLifecycleRuntime();
if (registeredRuntime) {
const result = await registeredRuntime.cancelDetachedTaskRunById(params);
if (result.found) return result;
}
return cancelTaskById(params);
}
```
This is a design-level call — fine to leave as-is if the invariant "task in registry ⟺ runtime owns it" holds firmly, but worth a comment in the code.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "tasks: harden detached runtime ownership" | Re-trigger Greptile |
| registerDetachedTaskRuntime: (runtime) => { | ||
| const existing = getDetachedTaskLifecycleRuntimeRegistration(); | ||
| if (existing && existing.pluginId !== record.id) { | ||
| pushDiagnostic({ | ||
| level: "error", | ||
| pluginId: record.id, | ||
| source: record.source, | ||
| message: `detached task runtime already registered by ${existing.pluginId}`, | ||
| }); | ||
| return; | ||
| } | ||
| registerDetachedTaskLifecycleRuntime(record.id, runtime); | ||
| }, |
There was a problem hiding this comment.
Same-plugin silent re-registration
The guard existing.pluginId !== record.id allows the same plugin to call registerDetachedTaskRuntime multiple times, silently replacing the prior runtime without any diagnostic. If a plugin's activation path calls this twice (e.g., due to a misconfigured re-entrant load), the second registration overwrites the first with no warning — the same protection offered to other plugins is absent for the registering plugin itself. Consider emitting a warning-level diagnostic or making the second call a no-op when the plugin id already matches.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/plugins/registry.ts
Line: 1176-1188
Comment:
**Same-plugin silent re-registration**
The guard `existing.pluginId !== record.id` allows the same plugin to call `registerDetachedTaskRuntime` multiple times, silently replacing the prior runtime without any diagnostic. If a plugin's activation path calls this twice (e.g., due to a misconfigured re-entrant load), the second registration overwrites the first with no warning — the same protection offered to *other* plugins is absent for the registering plugin itself. Consider emitting a warning-level diagnostic or making the second call a no-op when the plugin id already matches.
How can I resolve this? If you propose a fix, please make it concise.| export async function cancelDetachedTaskRunById(params: { cfg: OpenClawConfig; taskId: string }) { | ||
| const task = getTaskById(params.taskId); | ||
| if (!task) { | ||
| return cancelTaskById(params); | ||
| } | ||
| const registeredRuntime = getRegisteredDetachedTaskLifecycleRuntime(); | ||
| if (registeredRuntime) { | ||
| const cancelled = await registeredRuntime.cancelDetachedTaskRunById(params); | ||
| if (cancelled.found) { | ||
| return cancelled; | ||
| } | ||
| } | ||
| return cancelTaskById(params); | ||
| } |
There was a problem hiding this comment.
Registered runtime bypassed for unknown tasks
When getTaskById returns undefined the function immediately falls back to cancelTaskById, never consulting the registered runtime. If a task were ever created through the registered runtime before it appeared in the core registry (or after it was removed from it), the registered runtime's cancel logic would be skipped entirely. The fallback is correct by current design invariants, but the early-return bypasses the runtime contract the rest of the function enforces.
// current
if (!task) {
return cancelTaskById(params);
}
// would be more consistent with the contract:
if (!task) {
const registeredRuntime = getRegisteredDetachedTaskLifecycleRuntime();
if (registeredRuntime) {
const result = await registeredRuntime.cancelDetachedTaskRunById(params);
if (result.found) return result;
}
return cancelTaskById(params);
}This is a design-level call — fine to leave as-is if the invariant "task in registry ⟺ runtime owns it" holds firmly, but worth a comment in the code.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/tasks/task-executor.ts
Line: 711-724
Comment:
**Registered runtime bypassed for unknown tasks**
When `getTaskById` returns `undefined` the function immediately falls back to `cancelTaskById`, never consulting the registered runtime. If a task were ever created through the registered runtime before it appeared in the core registry (or after it was removed from it), the registered runtime's cancel logic would be skipped entirely. The fallback is correct by current design invariants, but the early-return bypasses the runtime contract the rest of the function enforces.
```typescript
// current
if (!task) {
return cancelTaskById(params);
}
// would be more consistent with the contract:
if (!task) {
const registeredRuntime = getRegisteredDetachedTaskLifecycleRuntime();
if (registeredRuntime) {
const result = await registeredRuntime.cancelDetachedTaskRunById(params);
if (result.found) return result;
}
return cancelTaskById(params);
}
```
This is a design-level call — fine to leave as-is if the invariant "task in registry ⟺ runtime owns it" holds firmly, but worth a comment in the code.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 888f9f44a0
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const cancelled = await registeredRuntime.cancelDetachedTaskRunById(params); | ||
| if (cancelled.found) { |
There was a problem hiding this comment.
Handle runtime cancel exceptions before returning
When a registered detached runtime throws inside cancelDetachedTaskRunById (for example due to a transient RPC failure), this function currently propagates the exception instead of returning the structured cancel result used by the rest of the task APIs. That means callers like task/flow cancellation can fail hard and skip both the legacy fallback and normal reason reporting, leaving cancellation in a partially applied state. Wrap the runtime call in a try/catch and return a non-throwing result (or fallback) to preserve existing cancellation semantics.
Useful? React with 👍 / 👎.
🔒 Aisle Security AnalysisWe found 2 potential security issue(s) in this PR:
1. 🟠 Detached task lifecycle runtime can be overwritten via internal state registration (plugin isolation bypass)
DescriptionThe new global detached task runtime state allows unguarded overwrites.
Impact (if third-party plugins can access these internal modules at runtime): a malicious or compromised plugin could hijack detached task lifecycle operations (create/start/complete/fail/cancel), enabling task cancellation or manipulation across plugins. Vulnerable code: export function registerDetachedTaskLifecycleRuntime(pluginId: string, runtime: DetachedTaskLifecycleRuntime): void {
detachedTaskLifecycleRuntimeRegistration = { pluginId, runtime };
}RecommendationEnforce exclusivity at the state layer as the single source of truth, so callers cannot bypass checks. Example: export function registerDetachedTaskLifecycleRuntime(
pluginId: string,
runtime: DetachedTaskLifecycleRuntime,
): void {
const existing = detachedTaskLifecycleRuntimeRegistration;
if (existing && existing.pluginId !== pluginId) {
throw new Error(`detached task runtime already registered by ${existing.pluginId}`);
}
detachedTaskLifecycleRuntimeRegistration = { pluginId, runtime };
}Additionally:
2. 🟡 Untrusted detached-task runtime can return arbitrary TaskRecord in cancel response (data leakage)
Description
Because
Vulnerable flow:
Vulnerable code: const cancelled = await registeredRuntime.cancelDetachedTaskRunById(params);
if (cancelled.found) {
return cancelled;
}RecommendationDo not trust Recommended mitigations (apply one or more):
const cancelled = await registeredRuntime.cancelDetachedTaskRunById(params);
if (cancelled.found) {
const task = getTaskById(params.taskId);
return {
...cancelled,
task: task ?? undefined,
};
}
Analyzed PR: #68915 at commit Last updated on: 2026-04-19T11:21:37Z |
3c942b2 to
3bcd35f
Compare
3bcd35f to
68717e7
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 68717e7317
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (registeredRuntime) { | ||
| const cancelled = await registeredRuntime.cancelDetachedTaskRunById(params); | ||
| if (cancelled.found) { |
There was a problem hiding this comment.
Handle runtime cancel exceptions in detached cancel path
cancelDetachedTaskRunById directly awaits registeredRuntime.cancelDetachedTaskRunById(...) without a guard, so any runtime-side exception (for example transient RPC/network failures) will throw out of this API instead of returning the structured cancel result that callers expect. Since this function now fronts flow and task cancellation paths, one thrown runtime error can abort cancellation mid-operation and skip both legacy fallback and reason reporting.
Useful? React with 👍 / 👎.
Summary
PR1added the detached-task lifecycle seam, but there was still no real core-owned way for an external executor to register itself against that seam.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
None.
Diagram (if applicable)
Security Impact (required)
Yes/No) NoYes/No) NoYes/No) NoYes/No) NoYes/No) NoYes, explain risk + mitigation:Human Verification (required)
What I personally verified:
What I did not verify:
Authoritative validation on
mb-server, rerun after rebasing onto currentmain:OPENCLAW_TEST_PROFILE=serial OPENCLAW_TEST_SERIAL_GATEWAY=1 pnpm test -- src/tasks/task-executor.test.ts src/plugins/loader.test.ts src/plugins/runtime/runtime-tasks.test.ts src/tasks/detached-task-runtime.test.tspnpm tsgo:corepnpm tsgo:core:testKnown unrelated
mb-serverbaseline noise, not used as the authoritative gate for this narrow PR:pnpm tsgo:allstill fails in existing extension lanes on currentmainpnpm buildstill hits the knownruntime-postbuildbundled dependency staging issue onmb-serverRisks and Mitigations
Compatibility / Migration
Yes/No) YesYes/No) NoYes/No) NoReview Conversations