Skip to content

feat(llm): ctx.fs + ctx.tools + ctx.exec + ctx.sandbox capability seam (ar-plan PR #4) - #157

Merged
dahai80 merged 1 commit into
mainfrom
feat/ctx-fs-tools-seam
Aug 27, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/ctx-fs-tools-seam

Conversation

@dahai80

@dahai80 dahai80 commented Aug 27, 2026

Copy link
Copy Markdown
Owner

Summary

PR #4 of the 9-PR ar-plan (fusion-code-enhance-ar-plan-0827.md), milestone M2. Extends the Ctx envelope shipped in PR #3 (ctx.llm) with four provider-neutral capability seams — solves arch-ecosystem A3 (no DSH-style ctx.fs / ctx.tools / ctx.exec injection seam).

Seam File Wraps (existing primitive)
ctx.fs capabilities/fs.ts node:fs/promises read/write/glob + rg grep
ctx.tools capabilities/tools.ts getAllBaseTools + findToolByName + isToolDenied
ctx.exec? capabilities/exec.ts InProcessExecCapability (node:child_process spawn) + ExecutorExecCapability (drain callBashViaExecutor)
ctx.sandbox? capabilities/sandbox.ts NoneSandboxProvider no-op stub (Landlock = cross-repo defer)

Design — facade, not rewrite

Per the ar-plan constraint: "fs/tools seam 抽象易过度 → 包现有工具不重写(capability = facade). consumer 迁移渐进而非全量(本 PR 只定义 seam + 1-2 示范消费点,余渐进)". Each provider wraps ONE existing primitive — no new logic, no re-listing. If a provider would duplicate runShellCommand's 1000 lines, it's wrong.

  • ctx.fs uses node:fs/promises directly, NOT the FileReadTool object (that pulls ToolUseContext/permissions/UI graph — facade keeps the seam primitive-light).
  • ctx.tools wraps the 3 registry primitives; callTool() deferred (needs full ToolUseContext, would over-couple).
  • ctx.exec two swappable backends. Existing BashTool routing + runShellCommand orchestration stay UNTOUCHED — this seam is for future one-shot exec consumers (ctx-inspect, code-search helpers) that want a result without the BashTool orchestration layer.
  • ctx.sandbox stub only — macOS seatbelt stays in BashTool; Linux Landlock is a cross-repo fusion-executor dependency, deferred.

Byte-identical when off

  • fs + tools always present (pure facades) but invoked by 0 migrated prod consumers this PR — demo consumers live in the test suite only. No behavior change.
  • exec env-gated via FUSION_CODE_CTX_EXEC_ENABLED (default undefined → ctx.exec undefined).
  • sandbox not injected → ctx.sandbox undefined.

PR #3's ctx-optional pattern holds: existing 2 consumers (betas.ts, thinking.ts) still pass no-ctx → old path.

Tests

  • 4 new capability test files: fs.test.ts, tools.test.ts, exec.test.ts, sandbox.test.ts
  • 7 new createCtx seam tests in capability.test.ts (fs populated + round-trip, tools list/getTool, exec undefined-when-off / defined-when-on, sandbox undefined)
  • Tmp dirs cleaned after each test (Rule 5).

Gates

  • bun run typecheck — clean (1 pre-existing unrelated retrieveContext.test baseline, not introduced)
  • bun test src/__tests__/ — 589 pass / 0 fail (was 514+; +~40 new)
  • bun run lint — changed files clean (4601 pre-existing baseline unaffected; CI lints new files only)
  • bun run build:dev — pass

Scope-creep deferred (explicit)

  • Migrating prod consumers (QueryEngine, Read/Grep/Glob tools) to ctx.fs/ctx.tools → gradual, future PRs
  • BashTool routing via ctx.exec → NOT this PR
  • Landlock SandboxCapability impl → cross-repo fusion-executor issue
  • callTool() on ToolsCapability → deferred (needs full ToolUseContext)

Co-Authored-By: Claude Fable 5 [email protected]

…m (ar-plan PR #4, S1.b-e)

Extend the Ctx envelope (PR #3 shipped ctx.llm) with four provider-neutral
seams so consumers inject ctx.fs / ctx.tools / ctx.exec / ctx.sandbox instead
of importing concrete adapters. Solves arch-ecosystem A3 (no DSH-style
capability injection seam).

Each seam is a thin facade wrapping an EXISTING primitive, not a rewrite:
- fs.ts    LocalFsCapability — node:fs/promises read/write/glob + rg grep
- tools.ts BaseToolsCapability — getAllBaseTools + findToolByName + isToolDenied
- exec.ts  InProcessExecCapability (node:child_process spawn) +
           ExecutorExecCapability (drain callBashViaExecutor generator)
- sandbox.ts NoneSandboxProvider no-op stub (Landlock = cross-repo defer)

Default-off + byte-identical when off: fs+tools are pure facades invoked by
0 migrated prod consumers this PR (define seam + demo in tests only, gradual
migration is later PRs per ar-plan). exec is env-gated
(FUSION_CODE_CTX_EXEC_ENABLED, default undefined). sandbox not injected.
Existing BashTool routing + runShellCommand orchestration stay UNTOUCHED —
the exec seam is for future one-shot exec consumers, not a replacement.

Tests: 4 new capability test files (fs/tools/exec/sandbox) + 7 new createCtx
seam tests in capability.test.ts. typecheck clean, 589 tests pass, lint clean
on changed files, build:dev passes.

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit 8bb3ed8 into main Aug 27, 2026
1 check passed
@dahai80
dahai80 deleted the feat/ctx-fs-tools-seam branch August 27, 2026 07:27
dahai80 added a commit that referenced this pull request Aug 27, 2026
Biome formatter reformat (import reorder/organize, line-wrap). Zero logic
change. PR #157 landed these files pre-formatted; this commits the on-disk
formatted version.

Files: src/services/llm/{ctx,capabilities/{exec,fs,tools}}.ts,
src/__tests__/services/llm/{capability,capabilities/{fs,sandbox,tools}}.test.ts

Co-Authored-By: Claude Fable 5 <[email protected]>
dahai80 added a commit that referenced this pull request Aug 28, 2026
…173)

First real prod consumer for the ctx.llm capability seam (ar-plan PR
#155/#157 landed it byte-identical-off with 0 prod consumers — 555 LOC
dead scaffolding). Per audit 1.1.2 decision "wire 1 real consumer".

sideQuery (stateless, async, self-contained) now builds a Ctx and passes
it to modelSupportsStructuredOutputs when:
  - output_format is in play (structured-output beta decision), AND
  - provider is cloud (firstParty/foundry/bedrock/vertex/openai).

fusionMlx stays on the old provider-if path: its capability reads MLX
model keywords (qwen3/llama3/...) and would enable the beta for models
the old path blanket-denied — a deliberate capability-authoritative
divergence deferred until the MLX beta-header behavior is verified.
Cloud path is byte-identical (firstParty 6-clause list matches betas.ts;
foundry/3P both return false). Fail-open: ctx build error -> undefined
ctx -> old path (no new failure mode).

Lazy: ctx only built when output_format truthy AND provider non-MLX.
Globals getCwd()/getSessionId() (sync) supply cwd/sessionId without
prop-drilling (sideQuery has 10 callers, all classifiers, stateless).

Test: src/__tests__/utils/sideQueryCtxSeam.test.ts (7 tests, 38 expect):
  - firstParty 9 model ids with-ctx == no-ctx (byte-identical loop)
  - firstParty structured models return true
  - firstParty non-structured returns false
  - foundry capability-authoritative returns true
  - bedrock/vertex/openai byte-identical (both false)
  - fusionMlx gate: sideQuery passes ctx=undefined -> old path false
    (reference ctx would return true — divergence documented)
  - seam guard: mismatched ctx.llm.modelId falls back to old path

Also fix pre-existing typecheck error in viewedTaskSelector.test.ts
(fakes re-typed to narrowed InProcessTeammateTaskState/LocalAgentTaskState
— .toBe() overload rejected widened TaskState). Behavior unchanged,
10/10 still pass. Per repo rule: fix failing cases even if unrelated.

Gates: typecheck 0 / 721 tests / lint net-zero 4601 / build:dev 3762.

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant