kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

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

Open morluto opened this issue 4 months ago • 1 comments

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

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

morluto avatar Jul 25 '26 02:07 morluto

🦋 Changeset detected

Latest commit: 1fcd71174605dea398ade5f6ce7e9e39d42af6d2

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

changeset-bot[bot] avatar Jul 25 '26 02:07 changeset-bot[bot]