Files
fusion/packages/engine/src/executor/bootstrap-misbinding-recovery.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

87 lines
3.1 KiB
TypeScript

/**
* FNXC:CodeOrganization 2026-08-03-20:15:
* tryBootstrapMisbindingRecovery peeled from TaskExecutor (U4).
* Re-anchor branches that were bootstrapped onto wrong base with zero own commits.
*/
import type { Task, TaskStore } from "@fusion/core";
import {
BranchCrossContaminationError,
classifyBootstrapMisbinding,
reanchorBranchToBase,
} from "../execution/branch-conflicts.js";
import { classifyTaskWorktree } from "../worktree/worktree-pool.js";
import { formatError } from "../logger.js";
import type { EngineRunContext, RunAuditor } from "../util/run-audit.js";
import { resolveReboundColumnFor } from "./lifecycle-columns.js";
export type BootstrapMisbindingRecoveryDeps = {
rootDir: string;
store: TaskStore;
getRunContextFor: (taskId: string) => EngineRunContext | undefined;
markGraphExecuteSelfRequeued: (taskId: string) => void;
};
export async function tryBootstrapMisbindingRecovery(
deps: BootstrapMisbindingRecoveryDeps,
task: Task,
contamination: BranchCrossContaminationError,
audit: RunAuditor,
): Promise<boolean> {
const bootstrap = await classifyBootstrapMisbinding({
repoDir: deps.rootDir,
branchName: contamination.branchName,
baseSha: contamination.baseSha,
taskId: task.id,
foreignCommits: contamination.foreignCommits,
});
if (!bootstrap.isBootstrapMisbinding) {
return false;
}
const worktreePath = task.worktree;
const worktreeClassification = worktreePath
? await classifyTaskWorktree(deps.rootDir, worktreePath)
: { ok: false as const };
if (!worktreePath || !worktreeClassification.ok) {
await deps.store.logEntry(task.id, `[recovery] bootstrap misbinding detected but worktree unavailable for re-anchor: ${worktreePath ?? "none"}`, undefined, deps.getRunContextFor(task.id));
return false;
}
await deps.store.logEntry(task.id, `[recovery] bootstrap-time branch misbinding detected on ${contamination.branchName}: 0 own commits, re-anchoring to ${contamination.baseSha}`, undefined, deps.getRunContextFor(task.id));
try {
const reanchor = await reanchorBranchToBase({
repoDir: deps.rootDir,
worktreePath,
branchName: contamination.branchName,
baseSha: contamination.baseSha,
taskId: task.id,
});
await audit.git({
type: "branch:reanchor",
target: contamination.branchName,
metadata: {
taskId: task.id,
baseSha: contamination.baseSha,
previousTipSha: reanchor.previousTipSha,
newTipSha: reanchor.newTipSha,
trigger: "bootstrap-misbinding",
},
});
await deps.store.updateTask(task.id, {
recoveryRetryCount: null,
nextRecoveryAt: null,
error: null,
paused: false,
pausedReason: null,
});
deps.markGraphExecuteSelfRequeued(task.id);
await deps.store.moveTask(task.id, await resolveReboundColumnFor(deps.store, task.id), { preserveResumeState: false, preserveWorktree: true });
return true;
} catch (error) {
await deps.store.logEntry(task.id, `[recovery] bootstrap re-anchor failed; falling back to contamination safety path: ${formatError(error)}`, undefined, deps.getRunContextFor(task.id));
return false;
}
}