Skip to content

Refactor: delegate command execution to fusion-executor (ExecutorDriver interface) #176

Closed

Description

@dahai80

Context

fusion-executor (L4 generic-tool layer) is the monorepo's controlled execution sandbox + macOS OS-level control hub. It replaces what fusion-code currently does inline — subprocess spawning, command validation, timeout, stdio capture, file IO, git rollback — with a hardened Rust+PyO3 core (security guard, PTY/stdio sandbox, diagnostics slicer, auto-rollback, native file tools, GUI control).

Audit audit/fusion-ar-audit.md P2-7 designates fusion-executor as the home for the code-sandbox capability currently embedded per-vertical (e.g. fusion-science PythonExecutor/JupyterKernel/RExecutor). ARCH-3 of the fusion-executor architecture audit (audit/fusion-executor-audit-result-0827.md) found zero real consumers — the executor exists but nothing in the monorepo delegates to it yet.

Proposal

Strip fusion-code's own subprocess/file-IO into an ExecutorDriver interface that delegates to fusion_executor.FusionSandboxExecutor. All command validation, timeout, stdio capture, and diagnostics parsing move to the executor; fusion-code keeps the agent loop, model prompting, and self-healing control.

Stable Python API (already shipped)

from fusion_executor import FusionSandboxExecutor, ExecutionResult, RollbackPolicy

ex = FusionSandboxExecutor()
result: ExecutionResult = ex.run(
    "pytest tests/",
    cwd=repo,
    task_id="task-42",
    timeout_sec=60.0,
    auto_rollback=RollbackPolicy(max_consecutive_failures=3, file_damage_check=True),
)

ExecutionResult on exit_code != 0 carries a pre-sliced diagnostics (error_type, file_path, line_number, code_snippet, raw_trace) — feed it straight into the next model prompt; do not re-parse raw stdout.

Other relevant methods: run_streaming() (live stdio chunks), file_edit()/multi_edit()/write_file()/apply_patch()/replace_function() (Claude-SDK FileEdit parity, atomic + flock), glob()/grep_with_opts() (ripgrep parity), shell_start()/shell_output()/kill_shell() (background tasks), snapshot_create()/rollback() (caller-driven), serve() UDS JSON-RPC server with subscribe() server-push (telemetry/stdio/screenshot), gui_action() (macOS Accessibility control).

Contract points fusion-code must own

  1. Diagnostics parsing — read result.diagnostics to build the next model turn.
  2. Circuit breaker — the executor is per-task stateless by design and does not count consecutive failures. The caller owns that count. RollbackPolicy.max_consecutive_failures is the threshold field the caller reads (the executor accepts it on the wire but never reads it — reserved by design). Hitting the threshold stops the self-healing loop (fail visibly) instead of looping forever.

Integration skeleton

A runnable one-way reference skeleton lives in fusion-executor (does not import fusion-code):

  • fusion-executor/examples/08_integrate_fusion_code.py — self-healing loop: parse diagnostics, own the consecutive-failure count as a circuit breaker, optional auto_rollback.

Why file an issue, not a PR

This is a cross-project refactor touching fusion-code's architecture (agent loop vs execution delegation). Filing the issue for fusion-code maintainers to scope; a PR would follow once the ExecutorDriver boundary is agreed. Per the monorepo rule, upstream changes go issue -> PR -> code.

References

  • fusion-executor PRD: architecture/fusion-executor-prd.md (data schema §4, diagnostics algorithm §4.2, fusion-code refactor plan)
  • fusion-executor audit (ARCH-3): audit/fusion-executor-audit-result-0827.md
  • monorepo audit: audit/fusion-ar-audit.md P2-7

Activity

  1. dahai80 commented on Aug 30, 2026

    @dahai80
    OwnerAuthor

    Landed in 5296004 (direct-push main).

    What

    Extends the existing executor subprocess delegation (previously bash-only) to cover file-IO disk writes. FileWriteTool / FileEditTool / MultiEditTool now delegate the final byte-write step to the fusion-executor subprocess via the existing UDS NDJSON-RPC seam (executor.write_file), instead of writing in-process.

    Boundary (key design decision)

    File tools interleave heavy client-side coordination around the disk write — staleness check, quote normalization (findActualString + preserveQuoteStyle), client-side patch generation (getPatchForEdit), LSP didChange/didSave, VSCode diff notify, readFileState.set, git-diff. The executor's own file_edit/multi_edit/apply_patch do their own independent string matching (no quote-norm, no staleness, no LSP) — delegating "the edit" would diverge semantics and lose staleness/LSP/readFileState.

    Correct boundary: delegate ONLY the final writeTextContent step, AFTER fusion-code completes all client-side coordination. A new helper callWriteViaExecutor(...) is invoked in place of writeTextContent; it fail-opens to in-process writeTextContent on any divergence. The executor's file_edit/multi_edit/apply_patch/replace_function are deliberately not used — only write_file (raw byte write).

    Fail-open mitigations (never block a user's edit)

    callWriteViaExecutor returns null (→ in-process write) when:

    • executor disabled (env gate) — byte-identical to today
    • encoding !== utf8 (executor is UTF-8 only)
    • path is a symlink (executor atomic_write would replace the link; in-process preserves it)
    • content > 64MB (executor cap)
    • any transport error or ok:false EditResult

    CRLF is normalized client-side before sending (executor writes raw bytes). Everything after the write (LSP, VSCode, readFileState, git-diff) runs unchanged on both paths — it operates on the path, not the write mechanism.

    Default-off, byte-identical-off

    FUSION_CODE_EXECUTOR_ENABLED unset → callWriteViaExecutor returns null at the env gate → writeTextContent runs in-process, identical to today. Runtime env gate (like the bash seam), not a feature() build flag — unconditional require() keeps executor code in the bundle, gated at runtime.

    Wire

    executor.write_file params {path, content, cwd?} → EditResult {ok, path?, error?, matches?} (source of truth: fe-tools/src/lib.rs:54).

    Files

    • src/services/executor/types.ts — EditResult wire type.
    • src/services/executor/ExecutorClient.ts — writeFile method (reuses existing sendRequest).
    • src/services/executor/ExecutorInstance.ts — writeFile wrapper (health-gated, no retry).
    • src/services/executor/executorDriver.ts — isFileWriteRouteable + callWriteViaExecutor + ExecutorClientLike extension.
    • src/tools/FileWriteTool/FileWriteTool.ts / FileEditTool/FileEditTool.ts / MultiEditTool/MultiEditTool.ts — per-call swap site at writeTextContent.
    • src/__tests__/executor/writeDelegation.test.ts — 12 unit tests via the _setExecutorClientForTesting injection seam.

    Gates

    typecheck 0 / 973 tests pass (+12 new) / biome clean on changed files (pre-existing format deviations on FileEdit/FileWrite unchanged) / build:dev 3829 modules (matches HEAD, executor runtime-gated off).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions