nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Architecture: Negative-space mutation firewall

Open akramcodez opened this issue 1 month ago • 2 comments

Description

Define strict, mechanically enforced boundaries for what a task is allowed to mutate, rejecting edits that fall outside the authorized "envelope."

Use Case

When small models encounter a bug they don't understand, they sometimes attempt "fixes" by deleting tests, broadening types to any, or removing unrelated API validations. This degrades project health over time.

Proposed Solution

  1. At the start of a task, establish an envelope (e.g., "Allowed: implementation of validateToken. Preserve: exported signatures, dependencies").
  2. Before accepting a patch from the model, the harness compares the AST of the proposed change against the original.
  3. If the patch alters an exported signature or modifies an unrelated test, the harness rejects the edit locally with an error: "Action blocked: You are not authorized to change the exported signature."

Alternatives Considered

  • LLM-as-a-judge review (costs tokens, adds latency, unreliable on local models).

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

akramcodez avatar Oct 05 '26 09:10 akramcodez

I'd love to take this on! Here is a structured implementation:


Core Architecture & Strategy

The firewall will act as a deterministic pre-mutation gate before any file edit or write is committed to disk (integrating with source/tools/file-ops/ and source/services/lifecycle-hooks.ts).

1. Mutation Boundary Validator (source/firewall/mutation-firewall.ts)

  • Envelope Configuration:
    • blockTypeDegradation: Rejects patches that replace explicit types with any / unknown in existing signatures.
    • protectTestSuites: Prevents the deletion or commenting-out of existing test(...) / it(...) blocks in spec/test files unless explicitly targeted.
    • preserveExportSignatures: Uses TypeScript AST diffing (ts.createSourceFile) to verify that exported function/interface/type signatures remain structurally compatible before and after the edit.
    • protectedPaths: Guardrails critical files (e.g. package.json, lockfiles, CI configs) from incidental deletion or unexpected structural edits.

2. Tool Integration Gate

  • Hook the firewall directly into the file mutation pipeline (write_file, string_replace, diff_edit):
    • Prior to applying the diff/replacement, run the proposed content through evaluateMutationEnvelope(targetPath, oldContent, newContent, options).
    • If a rule is violated, abort the write and return a structured rejection message to the LLM (e.g., Action blocked by mutation firewall: You attempted to delete test cases in 'source/auth.spec.ts'. Modify implementation code instead.).
    • This allows the model to self-correct during the active conversation loop without corrupting the codebase.

3. Configuration & CLI Settings (source/types/config.ts)

  • Add a configurable firewall block in agents.config.json with sensible defaults (enabled for local/small model profiles, configurable via /tune or session settings).

4. Comprehensive Test Suite (source/firewall/mutation-firewall.spec.ts)

  • Unit tests verifying:
    • Detection and blocking of any type injections into typed functions.
    • Detection and rejection of deleted test cases in .spec.ts files.
    • Enforcement of exported symbol signature preservation.
    • Allowing legitimate modifications, refactors, and valid new tests.

rishu685 avatar Oct 07 '26 16:10 rishu685

Great plan @rishu685, just assigned you!

akramcodez avatar Oct 08 '26 05:10 akramcodez