Skip to content

refactor(tasks): extract delivery policy - #57475

Merged
vincentkoc merged 3 commits into
mainfrom
pr2/task-delivery-policy
Mar 30, 2026
Merged

vincentkoc merged 3 commits into
mainfrom
pr2/task-delivery-policy

Conversation

@vincentkoc

Copy link
Copy Markdown
Member

Summary

  • Problem: task delivery and wake decisions still live inline inside src/tasks/task-registry.ts, mixing persistence/orchestration code with product policy.
  • Why it matters: later producer migrations need a stable place for detached-run delivery policy without burying behavior in the registry implementation.
  • What changed: extracted terminal/state-change delivery policy and message formatting into src/tasks/task-executor-policy.ts, added focused policy tests, and kept registry behavior wired through the new module.
  • What did NOT change (scope boundary): no producer migration, no executor behavior change, no cron target split, and no delivery backend change.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

Root Cause / Regression History (if applicable)

  • Root cause: N/A
  • Missing detection / guardrail: N/A
  • Prior context (git blame, prior PR, issue, or refactor if known): N/A
  • Why this regressed now: N/A
  • If unknown, what was ruled out: N/A

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/tasks/task-executor-policy.test.ts
  • Scenario the test should lock in: terminal/state-change formatting and detached-run delivery rules stay explicit and behavior-preserving while the registry delegates to the policy module.
  • Why this is the smallest reliable guardrail: the new module is pure policy, so focused unit tests catch drift without forcing broader producer coverage into this checkpoint.
  • Existing test that already covers this (if any): src/tasks/task-registry.test.ts
  • If no new test is added, why not: N/A

User-visible / Behavior Changes

None.

Diagram (if applicable)

Before:
registry -> inline delivery/wake policy -> message/send behavior

After:
registry -> task-executor-policy -> message/send behavior

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (No)
  • New/changed network calls? (No)
  • Command/tool execution surface changed? (No)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation:

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: Node 22 / pnpm
  • Model/provider: N/A
  • Integration/channel (if any): detached task delivery / state-change policy
  • Relevant config (redacted): default local task sqlite state

Steps

  1. Evaluate delivery policy for terminal and progress events.
  2. Verify the registry still delegates through the extracted module without behavior drift.
  3. Build the repo to confirm the extraction did not break the lazy task runtime path.

Expected

  • Policy lives in a dedicated module with unchanged runtime behavior.

Actual

  • Registry behavior remains the same, but the policy surface is now explicit and separately testable.

Evidence

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

  • Verified scenarios:
    • pnpm test -- src/tasks/task-executor-policy.test.ts src/tasks/task-registry.test.ts
    • pnpm build
  • Edge cases checked:
    • blocked terminal messaging
    • progress message formatting
    • terminal/state-change delivery gating
    • ACP duplicate terminal suppression decision stays explicit
  • What you did not verify:
    • producer migrations; those are follow-up stacked PRs

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)
  • If yes, exact upgrade steps:

Risks and Mitigations

  • Risk: policy extraction could accidentally drift message text or suppression rules.
    • Mitigation: the new pure-policy tests lock the formatting and gating decisions directly, and src/tasks/task-registry.test.ts still exercises the integrated flow.

AI-assisted: yes. Fully tested on the touched surface.

@vincentkoc vincentkoc self-assigned this Mar 30, 2026
@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Mar 30, 2026
Base automatically changed from pr1/task-executor to main March 30, 2026 04:35
@vincentkoc
vincentkoc marked this pull request as ready for review March 30, 2026 04:35
@greptile-apps

greptile-apps Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR extracts terminal/state-change delivery policy and message formatting from task-registry.ts into two new modules — a pure-policy module (task-executor-policy.ts) and a thin semantic façade (task-executor.ts) — and adds focused unit tests for each. The runtime behavior is unchanged; only the code organization moves.

Key changes:

  • task-executor-policy.ts: policy helpers (isTerminalTaskStatus, formatTaskTerminalMessage, formatTaskBlockedFollowupMessage, formatTaskStateChangeMessage, shouldAutoDeliverTaskTerminalUpdate, shouldAutoDeliverTaskStateChange, shouldSuppressDuplicateTerminalDelivery) extracted as pure, dependency-free functions.
  • task-executor.ts: semantic wrapper over task-registry.ts primitives providing a stable surface for producer callers.
  • acp-spawn-parent-stream.ts: updated to import recordTaskRunProgressByRunId from task-executor.ts instead of recordTaskProgressByRunId from task-registry.ts.
  • The shouldSuppressDuplicateTerminalDelivery refactor correctly moves the pickPreferredRunIdTask lookup into the registry call site and passes only preferredTaskId to the pure policy function, preserving exact runtime semantics including whitespace-only runId edge cases.
  • One P2 test-coverage gap: the two "don't suppress" scenarios for shouldSuppressDuplicateTerminalDelivery are untested.

Confidence Score: 5/5

Safe to merge — pure refactor with no behavior changes and comprehensive test coverage of the extracted policy surface.

All inline policy functions are extracted with verified behavior parity, including edge cases. The only finding is a P2 test-coverage gap for two 'don't suppress' branches in shouldSuppressDuplicateTerminalDelivery, which does not block correctness.

src/tasks/task-executor-policy.test.ts — minor test gap in shouldSuppressDuplicateTerminalDelivery coverage.

Important Files Changed

Filename Overview
src/tasks/task-executor-policy.ts New pure-policy module extracted from task-registry.ts. All policy functions are behavior-equivalent to their inline predecessors.
src/tasks/task-executor.ts New thin semantic façade over task-registry.ts primitives. Functions delegate 1-to-1 with renamed, more descriptive identifiers.
src/tasks/task-registry.ts Removed inline policy helpers and replaced call sites with imports from task-executor-policy.ts. Behavior-preserving.
src/agents/acp-spawn-parent-stream.ts Import updated from recordTaskProgressByRunId (task-registry) to recordTaskRunProgressByRunId (task-executor). All three call-sites are unaffected.
src/tasks/task-executor-policy.test.ts Good focused tests for the new pure-policy module. Missing two 'don't suppress' branches in shouldSuppressDuplicateTerminalDelivery coverage.
src/tasks/task-executor.test.ts New integration-style tests for the executor façade covering queued→running→completed and running→failed→delivery-status flows.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/tasks/task-executor-policy.test.ts
Line: 100-108

Comment:
**Missing "don't suppress" test cases for `shouldSuppressDuplicateTerminalDelivery`**

The test only covers the suppression-active path (`preferredTaskId !== task.taskId → true`). The two "don't suppress" branches aren't tested:

1. When `preferredTaskId === task.taskId` (current task _is_ the preferred one → should return `false`)
2. When `preferredTaskId` is `undefined` (no competing task found → should return `false`)

These fall naturally out of the new signature change (the registry now pre-computes `preferred?.taskId` before calling the policy). Adding explicit assertions here would guard against future regressions in those paths:

```
// Current task is preferred → do not suppress
expect(
  shouldSuppressDuplicateTerminalDelivery({
    task: createTask({ runtime: "acp", runId: "run-dup" }),
    preferredTaskId: "task-1", // same as default taskId
  }),
).toBe(false);

// No competing task found → do not suppress
expect(
  shouldSuppressDuplicateTerminalDelivery({
    task: createTask({ runtime: "acp", runId: "run-dup" }),
    preferredTaskId: undefined,
  }),
).toBe(false);
```

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

Reviews (1): Last reviewed commit: "Merge branch 'main' into pr2/task-delive..." | Re-trigger Greptile

Comment on lines +100 to +108
expect(
shouldSuppressDuplicateTerminalDelivery({
task: createTask({
runtime: "acp",
runId: "run-duplicate",
}),
preferredTaskId: "task-2",
}),
).toBe(true);

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.

P2 Missing "don't suppress" test cases for shouldSuppressDuplicateTerminalDelivery

The test only covers the suppression-active path (preferredTaskId !== task.taskId → true). The two "don't suppress" branches aren't tested:

  1. When preferredTaskId === task.taskId (current task is the preferred one → should return false)
  2. When preferredTaskId is undefined (no competing task found → should return false)

These fall naturally out of the new signature change (the registry now pre-computes preferred?.taskId before calling the policy). Adding explicit assertions here would guard against future regressions in those paths:

// Current task is preferred → do not suppress
expect(
  shouldSuppressDuplicateTerminalDelivery({
    task: createTask({ runtime: "acp", runId: "run-dup" }),
    preferredTaskId: "task-1", // same as default taskId
  }),
).toBe(false);

// No competing task found → do not suppress
expect(
  shouldSuppressDuplicateTerminalDelivery({
    task: createTask({ runtime: "acp", runId: "run-dup" }),
    preferredTaskId: undefined,
  }),
).toBe(false);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/tasks/task-executor-policy.test.ts
Line: 100-108

Comment:
**Missing "don't suppress" test cases for `shouldSuppressDuplicateTerminalDelivery`**

The test only covers the suppression-active path (`preferredTaskId !== task.taskId → true`). The two "don't suppress" branches aren't tested:

1. When `preferredTaskId === task.taskId` (current task _is_ the preferred one → should return `false`)
2. When `preferredTaskId` is `undefined` (no competing task found → should return `false`)

These fall naturally out of the new signature change (the registry now pre-computes `preferred?.taskId` before calling the policy). Adding explicit assertions here would guard against future regressions in those paths:

```
// Current task is preferred → do not suppress
expect(
  shouldSuppressDuplicateTerminalDelivery({
    task: createTask({ runtime: "acp", runId: "run-dup" }),
    preferredTaskId: "task-1", // same as default taskId
  }),
).toBe(false);

// No competing task found → do not suppress
expect(
  shouldSuppressDuplicateTerminalDelivery({
    task: createTask({ runtime: "acp", runId: "run-dup" }),
    preferredTaskId: undefined,
  }),
).toBe(false);
```

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

@vincentkoc
vincentkoc merged commit 817ac55 into main Mar 30, 2026
32 of 36 checks passed
@vincentkoc
vincentkoc deleted the pr2/task-delivery-policy branch March 30, 2026 04:45
Alix-007 pushed a commit to Alix-007/openclaw that referenced this pull request Mar 30, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
alexjiang1 pushed a commit to alexjiang1/openclaw that referenced this pull request Mar 31, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
pgondhi987 pushed a commit to pgondhi987/openclaw that referenced this pull request Mar 31, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
Tardisyuan pushed a commit to Tardisyuan/openclaw that referenced this pull request Apr 30, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
* refactor(tasks): add executor facade

* refactor(tasks): extract delivery policy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant