Files
fusion/packages/engine/src/executor/task-add-dep-tool.ts
gsxdsm 1cf86baa1c refactor: package code organization wave 18 (executor pure peels) (#3317)
## Summary

Wave 18 continues the package code-organization program after wave 17
domain folders (U4 Slice A from
`docs/plans/2026-07-14-001-refactor-package-code-organization-plan.md`).

### What changed
Peel **pure, behavior-preserving** helpers out of
`packages/engine/src/executor.ts` into domain modules under
`packages/engine/src/executor/`, with **stable re-exports** from
`executor.ts` so deep imports and `vi.mock("../executor.js")` keep
working.

| New module | Symbols |
|------------|---------|
| `executor/task-done-refusal.ts` | `evaluateTaskDoneRefusal`,
`determineRevisionResetStart`, skip-bypass refusal helper |
| `executor/workflow-feedback-paths.ts` |
`extractReferencedPathsFromWorkflowFeedback`,
`isAlwaysAllowedScopeLeakPath`, `workflowPathMatchesDeclaredScope` |
| `executor/workflow-step-verdict.ts` |
`FUSION_WORKFLOW_STEP_CONVENTIONS_PREAMBLE`, `parseWorkflowStepVerdict`
/ `parseWorkflowStepOutput`, step outcome types |
| `executor/await-input-parse.ts` | `parseAwaitInputSentinel`,
`parseAwaitInputQuestionToolCall` |
| `executor/no-commit-eligibility.ts` | `getNoCommitEligibilityReason`
(+ prompt heuristics) |

`executor.ts` live LOC ~**22817 → ~22427** (first pure-peel batch; more
peels needed to approach the 2k cap).

### Shims
- `old path` `executor.ts` public exports → `new path` `executor/*.ts` →
delete-when consumer deep-imports are re-pointed (not this PR)

### Test plan
- [x] `@fusion/engine` typecheck
- [x] Oracle: task-done refusal, skip-bypass, workflow malformed
verdict, scope-leak allowlist, executor-step-session, executor-prompt
- [x] `vitest --project=engine-core` (merge-gate curated suite)
- [ ] CI merge gate

**Stack:** wave17 (merged) → **this PR**

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Improved recognition of workflow outcomes from structured and
conversational responses.
* Added support for extracting questions from await-input responses and
tool calls.
* Improved workflow feedback handling for referenced files and declared
scope patterns.
* Added clearer guidance for task execution, approvals, verification,
and available tools.

* **Bug Fixes**
* Prevented completion when required review approvals are missing or
revisions remain pending.
* Improved handling of workflows that legitimately require no code
changes.
  * Added clearer refusal messages and more reliable revision restarts.
  * Sanitized repository paths in Git remediation instructions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-09 15:46:09 -10:00

115 lines
4.1 KiB
TypeScript

/**
* FNXC:CodeOrganization 2026-08-03-18:00:
* createTaskAddDepTool peeled from TaskExecutor (U4). Mid-execution dependency
* declaration tool; confirm=true aborts the active session for re-planning.
*/
import { Type, type Static } from "@earendil-works/pi-ai";
import type { TaskStore } from "@fusion/core";
import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
import { executorLog } from "../logger.js";
const taskAddDepParams = Type.Object({
task_id: Type.String({ description: "The ID of the task to depend on (e.g. \"KB-001\")" }),
confirm: Type.Optional(Type.Boolean({ description: "Set to true to confirm adding the dependency. Required because adding a dep to an in-progress task will stop execution and discard current work." })),
});
export type TaskAddDepToolDeps = {
store: TaskStore;
depAborted: Set<string>;
getActiveSession: (taskId: string) => { session: { dispose: () => void } } | undefined;
getActiveStepExecutor: (taskId: string) => { terminateAllSessions: () => Promise<unknown> } | undefined;
};
export function createTaskAddDepTool(deps: TaskAddDepToolDeps, taskId: string): ToolDefinition {
const store = deps.store;
return {
name: "fn_task_add_dep",
label: "Add Dependency",
description:
"Declare a dependency on an existing task. Use when you discover " +
"mid-execution that another task must be completed first. " +
"Adding a dependency to an in-progress task will stop execution " +
"and discard current work, so confirm=true is required. " +
"Without confirm=true, a warning is returned first.",
parameters: taskAddDepParams,
execute: async (_id: string, params: Static<typeof taskAddDepParams>) => {
const targetId = params.task_id;
// Prevent self-dependency
if (targetId === taskId) {
return {
content: [{
type: "text" as const,
text: `Cannot add self-dependency: ${taskId} cannot depend on itself.`,
}],
details: {},
};
}
// Validate target task exists
try {
await store.getTask(targetId);
} catch {
return {
content: [{
type: "text" as const,
text: `Task ${targetId} not found. Cannot add dependency on a non-existent task.`,
}],
details: {},
};
}
// Read current task to get existing dependencies
const currentTask = await store.getTask(taskId);
const existing = currentTask.dependencies;
// Dedup check
if (existing.includes(targetId)) {
return {
content: [{
type: "text" as const,
text: `${targetId} is already a dependency of ${taskId}. No changes made.`,
}],
details: {},
};
}
// Confirmation gate — destructive action for in-progress tasks
if (!params.confirm) {
return {
content: [{
type: "text" as const,
text: `Warning: adding a dependency to an in-progress task will stop execution and discard current work. Call with confirm=true to proceed.`,
}],
details: {},
};
}
// Add the dependency
await store.updateTask(taskId, { dependencies: [...existing, targetId] });
await store.logEntry(taskId, `Added dependency on ${targetId} — stopping execution for re-planning`);
// Trigger abort flow (same pattern as pausedAborted)
deps.depAborted.add(taskId);
const activeSession = deps.getActiveSession(taskId);
activeSession?.session.dispose();
// Also terminate step sessions if active
const stepExecutor = deps.getActiveStepExecutor(taskId);
if (stepExecutor) {
stepExecutor.terminateAllSessions().catch(err =>
executorLog.warn(`Failed to terminate step sessions for dep-abort ${taskId}: ${err}`)
);
}
return {
content: [{
type: "text" as const,
text: `Added dependency on ${targetId}. Stopping execution — task will move to triage for re-planning.`,
}],
details: {},
};
},
};
}