Files
fusion/packages/engine/src/executor/worktree-remove-own.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

89 lines
3.1 KiB
TypeScript

/**
* FNXC:CodeOrganization 2026-08-03-14:50:
* removeOwnWorktreeWithReconcile peeled from TaskExecutor (U4 Slice B).
*/
import type { Settings } from "@fusion/core";
import {
activeSessionRegistry,
executingTaskLock,
reconcileSelfOwnedActiveSessionForRemoval,
} from "../agents/active-session-registry.js";
import {
RemovalReason,
removeWorktree,
} from "../worktree/worktree-pool.js";
import { ActiveSessionWorktreeRemovalError } from "../worktree/worktree-backend.js";
import { executorLog } from "../logger.js";
export type RemoveOwnWorktreeDeps = {
rootDir: string;
store: {
logEntry: (taskId: string, action: string, outcome?: string) => Promise<unknown>;
};
reconcileSelfOwnedBeforeRemove: (worktreePath: string, taskId: string) => Promise<void>;
hasActiveWorktreeBinding: (taskId: string, path: string) => boolean;
};
export async function removeOwnWorktreeWithReconcile(
deps: RemoveOwnWorktreeDeps,
input: {
worktreePath: string;
settings: Settings;
taskId: string;
reason: RemovalReason;
audit?: Parameters<typeof removeWorktree>[0]["audit"];
},
): Promise<void> {
await deps.reconcileSelfOwnedBeforeRemove(input.worktreePath, input.taskId);
const removeArgs = {
worktreePath: input.worktreePath,
rootDir: deps.rootDir,
settings: input.settings,
taskId: input.taskId,
reason: input.reason,
audit: input.audit,
expectedOwnerTaskId: input.taskId,
liveOwnerProbe: (path: string, ownerTaskId: string) => deps.hasActiveWorktreeBinding(ownerTaskId, path),
// FN-5256: route the worktree-backend defensive reconcile through the
// hardened gates (process-active + min-idle window).
processActiveProbe: (probeTaskId: string) => executingTaskLock.has(probeTaskId),
} as const;
try {
await removeWorktree(removeArgs);
} catch (error: unknown) {
if (
error instanceof ActiveSessionWorktreeRemovalError
&& error.details.taskId === input.taskId
&& !deps.hasActiveWorktreeBinding(input.taskId, input.worktreePath)
) {
// FN-5256: route the post-throw reconcile through the hardened path so
// process-active and too-recent signals also gate this leg.
const outcome = reconcileSelfOwnedActiveSessionForRemoval(
activeSessionRegistry,
input.worktreePath,
input.taskId,
(path, ownerTaskId) => deps.hasActiveWorktreeBinding(ownerTaskId, path),
{
processActiveProbe: (probeTaskId) => executingTaskLock.has(probeTaskId),
},
);
if (outcome.action === "reconciled") {
await deps.store.logEntry(
input.taskId,
"Reconciled stale self-owned active-session registration (post-throw)",
input.worktreePath,
);
await removeWorktree(removeArgs);
return;
}
if (outcome.action === "process-active-refuses" || outcome.action === "too-recent-refuses") {
executorLog.warn(
`[FN-5256] post-throw reconcile refused for ${input.taskId} at ${input.worktreePath}: action=${outcome.action}`,
);
// Refused — surface the original error so the caller can decide.
}
}
throw error;
}
}