Skip to content

fix(headless): prevent isolated Edit from decoding executor stdout noise into files #1111

Closed

Description

@Astro-Han

Summary

The isolated Headless Edit tool can silently append non-UTF-8 bytes to edited files when the executor adds shell job-control output to stdout.

This was reproduced in the semantic-compact A/B runs behind #1021, but it is independent of semantic compaction. It affects the isolated file-edit transport itself.

Observed behavior

An isolated Edit reads the target using:

base64 < "$target"

The executor may append a shell notification such as:

[1]+ Done env MAKA_HARBOR_COMMAND_SCOPE=...

The current reader removes whitespace and passes the entire stdout to Node's permissive Base64 decoder:

const stdout = await execFileCommand(...);
return Buffer.from(stdout.replace(/\s+/g, ''), 'base64');

Characters from the job-control notification that belong to the Base64 alphabet are decoded as additional file bytes. In the observed failures, both edited files gained the same 25-byte non-UTF-8 suffix.

Re-encoding the suffix produced:

1+DoneenvMAD8cBEE5H8I4wwA0P9II48

The 1+DoneenvMA prefix directly identifies the contaminating job-control notification.

Reproduction evidence

Two independent sanitize-git-repo cells edited UTF-8 files with the isolated Edit tool and later failed verification with:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position 1951

Affected files included:

  • /app/dclm/ray_processing/ray_cluster.yaml
  • /app/dclm/ray_processing/process.py

One later cell detected and removed the 25-byte suffix during the agent run, but the verifier still observed the same suffix. The initial corruption mechanism is proven; the later reappearance needs focused lifecycle instrumentation.

The passing comparison arms avoided the failure only because they subsequently rewrote the complete files using truncating Python writes. That masked the deterministic Edit corruption.

Root cause

readIsolatedFileBytes treats the executor's complete stdout as an unframed Base64 payload and relies on permissive decoding.

The transport has no boundary, length, checksum, or strict alphabet validation, so unrelated stdout can become file content without producing an error.

Current seam:

  • packages/headless/src/tools.ts
  • readIsolatedFileBytes
  • EDIT_READ_BYTES_SCRIPT

Expected behavior

  • Isolated Edit must reproduce the target bytes exactly before applying a replacement.
  • Any stdout outside the framed payload must be rejected rather than decoded.
  • A successful edit must never introduce bytes unrelated to old_string and new_string.
  • Executor diagnostics and job-control notifications must not share an unframed data channel with file contents.

Suggested fix

Use a framed transport with strict validation, for example:

  1. emit an explicit payload marker and encoded byte length;
  2. extract only the framed Base64 payload;
  3. reject characters outside the canonical Base64 alphabet;
  4. decode and verify the expected byte length or SHA-256;
  5. optionally verify the post-write bytes before reporting success.

Do not solve this by merely stripping the currently observed [1]+ Done text; other executor noise would remain capable of corrupting files.

Regression coverage

Add an isolated Edit test whose executor returns:

<valid base64 payload>
[1]+ Done env MAKA_HARBOR_COMMAND_SCOPE=...

Verify that:

  • the edit fails closed, or correctly extracts only a framed payload;
  • the resulting file contains no trailing bytes;
  • shortening and lengthening replacements both preserve all unrelated bytes;
  • non-UTF-8 exact edits remain supported without lossy decoding;
  • concurrent edits retain the existing per-file serialization guarantees.

Also add an integration-level check through the real Harbor executor boundary so the test does not rely only on a clean mock stdout.

Impact

This can convert a correct model edit into a verifier failure, contaminate benchmark effectiveness results, and make the failure look like model behavior. It should be treated as harness/tooling corruption rather than a semantic-compaction regression.

Activity

  1. Nyvo-io commented on Jul 16, 2026

    @Nyvo-io
    Contributor

    I’d like to take this issue.

    I checked the current seam and confirmed that this should be fixed as a transport-integrity problem, not by stripping the observed job-control string. My proposed scope is:

    • replace the unframed base64 < target stdout contract with a versioned frame that binds the payload length and SHA-256;
    • parse exactly one frame and reject missing, duplicate, malformed, non-canonical, truncated, or digest-mismatched payloads, including any ambiguous stdout contamination;
    • preserve byte-exact, non-UTF-8 edits and the existing per-file serialization behavior;
    • add shortening/lengthening, executor-noise, corruption, non-UTF-8, and concurrent-edit regression tests;
    • add coverage through the Harbor executor boundary so the contract is not validated only against a clean mock.

    The read/validation path will fail closed before applying a replacement, and diagnostics will remain separate from file bytes. I’ll also review the write/result boundary for an exact post-write assertion if it can be added without changing the broader executor protocol.

    I found no open PR referencing #1111 or an equivalent in-flight fix. Please assign it to me if this scope matches the intended fix.

  2. added
    bug
    on Jul 16, 2026
  3. Astro-Han commented on Jul 16, 2026

    @Astro-Han
    ContributorAuthor

    Scope looks right, assigning to you.

    One thing I didn't flag in the issue: this blocks the semantic-compact A/B runs behind #1021, and I need to run those today. I'll unblock myself with a throwaway local patch (fail-closed frame extraction only, no PR) and drop it once your fix lands, so there's no time pressure on your side.

    If you do end up with a fail-closed version in the next few hours, ping me and I'll review right away and use that for today's runs instead.

  4. Nyvo-io commented on Jul 16, 2026

    @Nyvo-io
    Contributor

    @Astro-Han The fail-closed implementation is ready for review in #1147.

    The current CI run is fully green: typecheck, test, and e2e all pass. The fix uses an exact versioned frame with byte length and SHA-256 validation, rejects any ambiguous stdout contamination before writing, and verifies the stored bytes with an exact post-write readback. It also includes Harbor executor-boundary and non-UTF-8 regression coverage.

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions