Skip to content

Graduate the six FS tool bodies into engine/adapter/tools so engine importers don't reimplement them #269

Closed

Description

@jerm-dro

Problem

The engine/ module ships the tool.Tool / tool.Catalog / tool.Workspace interfaces and engine/tool.NewCatalog(), but no tool bodies. The real Read/Edit/Write/Grep/Glob/Bash implementations live in the root module's internal/adapter/tools, which is not importable.

So an external consumer that embeds engine/agent gets an empty catalog and must reimplement the entire model-facing tool kit from scratch to have a working coding loop. That means re-deriving subtle, security-sensitive invariants we already got right and test hard — most notably Edit's read-before-edit / exact-match / uniqueness-unless-replace_all contract. Two copies of that code drifting apart is exactly the failure mode worth avoiding.

This is squarely in the atrium-convergence theme (make the engine core consumable as a library): engine/adapter/* already advertises itself as "offline test doubles and sane defaults for any engine consumer" (mockllm, memfs, memstore, …). The tool bodies are the same role and belong in the same place.

Scope: the tool bodies ONLY

This issue is deliberately narrow. It graduates the six tool bodies and nothing else, because that is what actually delivers the reuse win (the invariant-heavy code) at the lowest risk.

In scope

  • Move the six tool bodies (read.go, edit.go, write.go, grep.go, glob.go, bash.go) + tools.go (the All() / Register() / NewBashTool(runner) constructors) into engine/adapter/tools. These are already engine-clean: their entire non-stdlib import set is engine/session, engine/tool, and five small helpers from internal/adapter/toolkit (MaxOutputBytes, TruncationMarker, ParseArgs, Schema, Truncate).
  • Carry only those five helpers with the tools — inline them into the tools package (or a package-private file), NOT a public toolkit move. They depend only on engine/session + stdlib (ParseArgs is a one-line passthrough to the already-exported session.ParseArgs). This avoids touching the ~20 unrelated internal/ sites that use the full toolkit grab-bag.
  • depguard + DAG entries — one list-mode: strict block in .golangci.yml (copy the memfs pattern) + one entry in engine/arch/layering_test.go. Bash needs os/exec allowed in its block (or a separate block for Bash); everything else stays os-free by going through the injected tool.Workspace.
  • Keep the graduated package behind the same engine/COMPATIBILITY.md "excluded, may change without notice" contract as the other reference adapters.

Out of scope (deliberately)

  • The concrete osfs Workspace / CommandRunner. The tool bodies are Workspace-agnostic — they take an injected tool.Workspace and (for Bash) a tool.CommandRunner. The hardened osfs implementation (~900 lines) pulls in os, os/exec, the gitenv env handling, and the secret-scrub cone, which collides with the os-deny convention every engine/adapter/* block enforces and raises real design questions (does a stripped Workspace ship? does secret-scrub move into the engine module?). None of that is needed to deliver the reuse win. A consumer supplies its own Workspace (validated against the already-exported fsconformance.Run) and its own CommandRunner. If a batteries-included reference Workspace is wanted later, that is a separate follow-up with its own design discussion.
  • The full internal/adapter/toolkit migration. It is a shared utility used well beyond tools (mcp, skills, memory, agents); moving the whole thing ripples through ~20 files for no benefit to this issue. Take only the five tool-facing helpers.
  • The LLM provider adapters (internal/adapter/anthropic, internal/adapter/openai) — heavy SDK dep closures that should not be forced onto every engine importer. Implementing the two-method port.LLMProvider is the consumer's job.

What a consumer still owns after this lands

A consumer imports the six tools and never reimplements the correctness-critical bodies. It still supplies (as it should — these are where the sandboxing/credential decisions live):

  • a tool.Workspace scoped to its own root (ordinary file I/O behind a folder boundary; validate against fsconformance.Run),
  • a tool.CommandRunner for Bash (a thin os/exec wrapper),
  • a port.LLMProvider.

Opt-in / opt-out contract (unchanged)

The catalog is caller-assembled, so the graduated kit is purely additive — never mandatory or all-or-nothing:

  • engine/tool.NewCatalog() returns an empty catalog; the caller registers tools explicitly.
  • Take all of tools.All() (+ NewBashTool(runner)), take a subset (e.g. read-only, omit Edit/Write/Bash), swap one for a custom implementation (lookup is by name — the caller's tool wins by being the only one registered), or ignore the kit entirely.

The graduation PR + package doc should state this contract explicitly.

Payoff

Engine importers get the invariant-heavy, security-sensitive tool bodies (Edit's contract chief among them) by importing them, instead of re-deriving and testing a second copy that must stay in lockstep with ours — while keeping full control over the Workspace, shell runner, and model credentials.

Activity

  1. added
    enhancement
    atrium-convergence
    on Jul 16, 2026
  2. jerm-dro commented on Jul 16, 2026

    @jerm-dro
    ContributorAuthor

    Will callers still be able to inject their own tools / disable these?

    Yes — nothing about this changes the composition model, because the catalog is caller-assembled, not framework-owned:

    • engine/tool.NewCatalog() returns an empty catalog.
    • catalog.Register(t tool.Tool) (and MustRegister) adds any tool the caller wants — including the caller's own implementations.
    • tools.All() just returns a []tool.Tool slice; registering it is the caller's choice, tool by tool. There is no implicit/global auto-registration.

    So after graduating the reference kit, a consumer can:

    • Take all of it — register tools.All() (+ NewBashTool(runner)) for a batteries-included coding loop.
    • Take a subset — register only Read/Grep/Glob and omit Edit/Write/Bash (e.g. a read-only agent), or drop Bash by simply not constructing it.
    • Swap one out — register their own Edit implementation and skip ours; the engine looks tools up by name via Catalog.Lookup, so the caller's tool wins by being the only one registered under that name.
    • Ignore the kit entirely — register a fully hand-rolled set, exactly as today.

    The reference kit is purely additive: an importable default the caller opts into per-tool, never a mandatory or all-or-nothing bundle. This should be stated explicitly in the graduation PR (and the package doc) so the opt-in/opt-out contract is unambiguous.

  3. changed the title [-]Graduate the reference tool kit (toolkit + FS tool bodies) into engine/adapter/tools so engine importers don't reimplement them[/-] [+]Graduate the six FS tool bodies into engine/adapter/tools so engine importers don't reimplement them[/+] on Jul 16, 2026
  4. jerm-dro commented on Jul 16, 2026

    @jerm-dro
    ContributorAuthor

    Implementation findings (from a spike; no code landed)

    I explored the move end-to-end against the tree before handing off to planning. Key findings that shape the actual PR — several of which the issue body did not anticipate:

    1. The tools package can't move wholesale — it must be SPLIT

    internal/adapter/tools holds more than the six FS bodies: it also has webfetch.go, fetchmcpresource.go, and websearch.go. websearch.go imports engine/agent (for agent.FenceUntrusted), and FetchMcpResource is MCP-coupled — so the package as a whole cannot live under engine/adapter/. Only the six FS bodies (Read/Edit/Write/Grep/Glob/Bash) graduate; the three web/MCP tools stay in the root module.

    Recommended target package name: engine/adapter/fstools (the tools name stays with the root package that keeps the web tools).

    2. The shared helpers + caps consts are entangled across the split

    tools.go defines package-private parseArgs / truncateBytes / schema wrappers and the maxReadLines/maxGrepMatches/maxGlobResults consts. These are used by BOTH the moving FS bodies AND the staying web tools. The move must:

    • Inline the ~5 helpers into fstools (the engine module cannot import internal/adapter/toolkit). ParseArgs is a one-line passthrough to the already-exported session.ParseArgs; Truncate/Schema/utf8RuneStart + MaxOutputBytes/TruncationMarker are ~30 lines total.
    • Keep the root tools package's own copies for the web tools (they still use toolkit).
    • MaxOutputBytes must stay in lockstep with toolkit.MaxOutputBytes (25 KiB) — note it, since a mixed session applies one cap. engine/session/content.go already carries a mirror-comment about this constant; add fstools to that note.

    3. bash.go references toolkit DIRECTLY (not only via wrappers)

    bashErrorMessage uses toolkit.MaxOutputBytes / toolkit.Truncate / toolkit.TruncationMarker inline. These become the local fstools equivalents. (No other FS body touches toolkit outside the three wrappers.) None of the five non-Bash bodies import os; Bash imports neither os nor os/exec (it runs through the injected tool.CommandRunner) — so the depguard os-deny that every engine/adapter/* block carries can stay ON for the whole fstools package. That removes the one anticipated depguard complication.

    4. All() / NoFS() / Register() bundle FS + web tools — callers depend on the bundling

    Today tools.All() returns {Read,Edit,Write,Grep,Glob,WebFetch,FetchMcpResource} and NoFS() returns {WebFetch,FetchMcpResource}. ~13 non-test + ~8 test callers use tools.All(), tools.NoFS(), tools.NewBashTool(...), and the concrete types (tools.EditTool{}, etc.) across internal/app (build.go, agentdefs.go, catalog.go), cmd/mecademo, cmd/mecatui/ui, internal/adapter/{forker,server,acp}, and tests.

    5. Recommended shape: alias shim (least churn, ~0 callers rewritten)

    To keep the PR small and additive:

    • engine/adapter/fstools gets the six bodies + inlined helpers + an fstools.All() (the FIVE FS tools, no web) + Register() + NewBashTool.
    • internal/adapter/tools keeps the web tools and adds a thin alias file re-exporting the FS types (type EditTool = fstools.EditTool, var NewBashTool = fstools.NewBashTool, const BashToolName = fstools.BashToolName, etc.). Its All()/NoFS() reassemble from fstools.All() + the local web tools, preserving today's exact bundles.
    • Result: every existing caller compiles unchanged; blast radius ≈ new package + one alias file + one .golangci.yml block.

    6. Enforcement gates — verified

    • api-compat gate (engine/api/*.txt): tracks only the 7 core packages, NOT adapters. Graduating tools does not touch it — no task api:update needed.
    • DAG layering test (engine/arch/layering_test.go): treats all of engine/adapter/* as one forbidden-prefix branch; a new engine/adapter/fstools needs no new entry (auto-covered like memfs).
    • depguard: the ONE required config change — add an engine-adapter-fstools list-mode: strict block allowing $gostd + engine/session + engine/tool (copy the memfs block; keep the os deny).
    • BashToolName is referenced by internal/adapter/server capability probing — the alias must preserve it.

    Suggested PR steps for the planning agent

    1. git mv the six bodies → engine/adapter/fstools; rewrite package tools → package fstools.
    2. Add engine/adapter/fstools/fstools.go with inlined helpers + caps consts + All()/Register().
    3. Strip toolkit from bash.go; point at local helpers.
    4. Move the FS-tool test files (edit/read/abspath/argkeys/etc — but NOT websearch_*/fetchmcpresource_test) to fstools.
    5. Add the alias file in internal/adapter/tools; re-slim its All()/NoFS()/tools.go to web-only + FS aliases.
    6. Add the depguard block. Gate: task lint && task test (BOTH modules incl. test:engine-standalone) + go run ./cmd/mecademo.
  5. self-assigned this
    on Jul 17, 2026
  6. JAORMX commented on Aug 1, 2026

    @JAORMX
    Contributor

    Shipped in PR #277 (merged 2026-07-17) and now released: the fstools graduation is included in the engine module release engine/v0.7.0 (tag pushed today; go get github.com/stacklok/mecatl/engine@engine/v0.7.0). Closing.

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

Metadata

Metadata

Assignees

Labels

atrium-convergenceenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions