Skip to content

fix(native): keep redirected output when an authored command writes nothing to a stream (HACK-2744) - #1283

Merged
roodboi merged 7 commits into
nextfrom
claude-lead/authored-exec-stdout-file
Nov 27, 2026
Merged

roodboi merged 7 commits into
nextfrom
claude-lead/authored-exec-stdout-file

Conversation

@roodboi

@roodboi roodboi commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes HACK-2744.

Problem

In an authored project (hack.project.json), redirecting hack exec or hack run output to a regular file could empty that file. Earlier content was lost, and so was the command's own output:

  • hack exec worker -- echo hello >> out.txt 2>&1, run on a file with an existing line, exits 0 and leaves the file empty.
  • hack exec worker -- true >> out.txt empties the file.
  • hack exec worker -- echo hello 2>> err.txt > /dev/null empties err.txt.

Through a pipe or a pty, the output came through as expected. Compose projects were not affected.

Root cause

src/lib/native-authored-command.ts (runAuthoredCommand) writes the command's captured streams with Bun.write(Bun.stdout, bytes) and then Bun.write(Bun.stderr, bytes). In Bun (1.4.4, the pinned version), Bun.write with an empty payload truncates a regular-file destination to 0 bytes, even when the file was opened for append. A two-line script reproduces it outside Hack. So any stream with no output erased the file it was redirected to:

  • A command with no stderr erased a >> log 2>&1 file: its earlier lines, and the stdout written just before.
  • An empty stdout erased >> out.txt.
  • An empty stderr erased 2>> err.txt.

Compose exec writes with process.stdout.write, which is why it was not affected. Authored one-off hack run goes through the same function, so it had the same bug. The cause is Bun's behaviour; HACK-2747 tracks it separately.

Behaviour

  • writeNativeCommandStream writes nothing when a stream is empty. A non-empty stream is written as before, at the file's current position, so >, >> and 2>&1 behave as they do for any other command.
  • This covers authored hack exec and authored hack run.
  • docs/guides/native-candidate.md gains one sentence: redirected output is written like any other command's output, and a stream with no output leaves its file untouched.

Evidence

Local (macOS arm64, Bun 1.4.4):

  • tests/native-authored-exec-output-file.test.ts: a worker process's real stdout and stderr point at files, with append, truncate and shared (2>&1) redirects. Each case runs once through the stream writer (on every platform) and once through the authored hack exec dispatch (macOS). With the fix, all 12 cases pass. Without it, 8 fail with an emptied file.
  • Changed and neighbouring suites: native-authored-exec-output-file, native-authored-command, native-authored-project-exec, native-authored-project-oneoff, 203 passed.
  • Root bun run typecheck and bun run check, docs:prepare and docs:check, privacy:check after staging, gitleaks.
  • After merging next at cd2e21f: typecheck, check, the same tests, privacy and gitleaks again.

Real VM (Apple Silicon), fresh isolated homes, an authored busybox worker, at 10018b5f (this fix before the merge of next). The control is next at cc8db3a9:

Row This fix next (control)
1. exec … -- echo hello > out.txt, stderr on a terminal PASS: out.txt holds hello (6 bytes) passes too
2. exec … -- echo hello >> out.txt 2>&1, file has an existing line PASS: existing line, then hello FAIL: file emptied
3. exec … -- echo hello 2>> err.txt > /dev/null, nothing on stderr, file has an existing line PASS: existing line kept FAIL: emptied
4. exec … -- true >> out.txt, file has an existing line PASS: existing line kept FAIL: emptied
6. authored hack run worker sh -c 'echo hello' >> out.txt 2>&1, file has an existing line PASS: both lines kept FAIL: emptied
7. smoke PASS

Row 1 passes on both heads: when stdout is a file and stderr is a terminal, the file receives its output. Every failing row on next sends an empty write to a regular file. There is no second cause.

Not run: the full suite, Linux (this change is TypeScript only; the stream-writer cases run on every platform in CI).

🤖 Generated with Claude Code


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

roodboi and others added 5 commits November 26, 2026 15:02
…HACK-2739)

An authored project's `hack exec` sent its request straight to the owner,
whose only lease wait was 15 s inside a fixed 30 s budget on its only loop.
During another project's 30 s stop it expired and the CLI reported the
command as unconfirmed, although nothing ran.

The requesting process (`graph native exec`) now waits up to
COMMAND_REQUEST_LEASE_WAIT (35 s) for a free lease before it sends
anything, through the shared lease-wait loop, so it prints the same single
progress line and, on expiry, the raw provider_busy naming the holder. The
wait is a bare lock attempt, released at once, so the uncontended path adds
one flock. The owner keeps its 15 s wait; if another holder took the lease
in between, it answers graph_authored_exec_lease_busy before any effect.
The CLI reports both as "nothing ran" with the holder and a retry step,
releases a background command's admission slot, and keeps "unconfirmed"
for every other failure. The frontend bound is 85 s.

ExecStopWait.tla gains an owner-side bound and a no-wait control for the
authored path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…2739)

Review round 1 on #1278:
- The owner's pre-effect refusal now reports its wire code in the CLI
  detail: native_graph_foreground caused by graph_authored_exec_lease_busy,
  not a synthesized provider_busy.
- await_free_command_lease's comment says what the code does: it does not
  read the pool record, so while a runtime down or cold start holds the lock
  it waits up to 35 s and may refuse provider_busy (honest and bounded)
  instead of reaching the owner's runtime_not_running.
- ExecStopWait.tla and COVERAGE.md say that PeerTake is enabled only between
  the two waits, so a second holder during the requester's own wait is not
  modelled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…run (HACK-2744)

An authored exec or one-off run wrote its captured streams with
Bun.write(Bun.stdout, bytes) and Bun.write(Bun.stderr, bytes). Bun.write
with an empty payload truncates a regular-file destination to zero bytes,
even one opened for append. So a command with no stderr erased a
`>> log 2>&1` file (its earlier content and the command's stdout), and an
empty stream erased its own redirect file (`>> out`, `2>> err`). The
Compose exec writes with process.stdout.write and was not affected.

An empty stream now writes nothing. A regression test points a worker's
real stdout and stderr at files, with append, truncate and shared
redirects, through the stream writer and through the authored exec
dispatch; without the fix six of its cases fail with an emptied file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@roodboi

roodboi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@vercel

vercel Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hack-docs Ready Ready Preview Nov 27, 2026 12:42am UTC

Request Review

@linear-code

linear-code Bot commented Nov 26, 2026

Copy link
Copy Markdown

HACK-2744

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T23:49:57.744890Z e57f065 Manual request
🔒 Security Review ✅ Completed 2026-11-26T23:51:54.113352Z e57f065 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: a7b9940206

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: a7b9940206

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

… (HACK-2744)

The time-bounded stale-env test expected the authored exec to write its
empty stderr, which is the write that truncated a redirected file. With the
fix it writes only the command's stdout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@roodboi

roodboi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: e57f065b73

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: e57f065b73

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Resolved to the tree tested in batch run m3:z495 (lead train sync).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@roodboi
roodboi merged commit 9241fa5 into next Nov 27, 2026
6 of 7 checks passed
@roodboi
roodboi deleted the claude-lead/authored-exec-stdout-file branch November 27, 2026 00:42
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