Skip to content

fix(cli): preserve buffered prompt output during signal shutdown#2170

Open
morluto wants to merge 2 commits into
MoonshotAI:mainfrom
morluto:codex/audit-headless-signal
Open

fix(cli): preserve buffered prompt output during signal shutdown#2170
morluto wants to merge 2 commits into
MoonshotAI:mainfrom
morluto:codex/audit-headless-signal

Conversation

@morluto

@morluto morluto commented Jul 25, 2026

Copy link
Copy Markdown

Related Issue

Resolve #1897

Problem

Headless prompt output can be truncated when a signal arrives after the final assistant response and session.resume_hint have been queued but before a slow stdout pipe has flushed.

The prompt runner removed its signal handlers at the start of asynchronous cleanup. A signal during permission restoration, telemetry shutdown, or harness disposal therefore used Node's default immediate exit. If the custom handler ran earlier, it also called process.exit() without first draining stdout.

final records queued
        |
handlers removed
        |
async cleanup
        |
     SIGTERM
        |
default exit ---> buffered records lost

This is especially damaging for stream-json: consumers can lose both the final assistant message and the resume hint even though the completed response is already persisted.

What changed

  • Keep prompt signal handlers installed through bounded cleanup and output flushing.
  • Flush real Node stdout/stderr Writables before signal-triggered exit.
  • Apply the lifecycle to both v1 and native v2 print mode.
  • Preserve cleanup failures independently from output draining.
  • Preserve compatibility with custom PromptRunIO writers that are not Node Writables.
  • Add regressions using a real backpressured Writable.

The existing cleanup timeout, stdio-drain timeout, and force-exit fallback remain unchanged. A permanently stalled consumer is still bounded, and SIGKILL remains outside the interceptable signal contract.

Validation:

  • 63 focused CLI tests passed
  • @moonshot-ai/kimi-code typecheck
  • Oxlint on changed TypeScript files
  • git diff --check
  • Changeset validation

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, and included a patch changeset.
  • This restores existing headless output behavior and needs no documentation update.

@changeset-bot

changeset-bot Bot commented Jul 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1fcd711

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fbc128b682

ℹ️ 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".

Comment thread apps/kimi-code/src/cli/run-prompt.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant