Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@pandemicsyn
Copy link
Contributor

@pandemicsyn pandemicsyn commented Jan 18, 2026

Context

I hit two linked issues in --session --auto flows:

  1. First CI would frequently exit immediately on a historical completion_result restored from a prior session, before the new prompt could run. So first invocation on a new session would work fine, but a follow up prompt the - prompt would never execute.
  2. The follow-up --auto prompt could hang because the extension emits a resume_task / resume_completed_task ask after restore, but the CLI was sending the prompt as a new task instead of answering that ask.

This update fixes both: prevent early CI exit on historical completion, and ensure the prompt answers the resume ask so the session continues instead of stalling.

Implementation

CI exit guard (historical completion):

  • Added a ciCompletionIgnoreBeforeTimestampAtom that represents “ignore completion_result at or before this timestamp.”
  • When resuming a session, we set this timestamp from the last known chat message and clear the “resumed” flag once the prompt starts.
  • useCIMode and the message handler now check this timestamp (plus the resume flag) to avoid exiting on historical completion_result.
  • effects.ts mirrors the same check to avoid setting ciCompletionDetectedAtom from restored messages.
  • Tests were updated to cover “skip historical completion” and “detect new completion after ignore timestamp.”

Resume ask handling (prevents hang):

  • Implemented explicit resume-ask detection via isResumeAskMessage / shouldWaitForResumeAsk.
  • In UI.tsx, when --auto is used after a session restore and a resume ask arrives, the prompt is sent using sendAskResponse (as a response to that ask) instead
    of creating a new task.
  • Commands are only executed when we are not answering a resume ask.
  • Attachments are handled consistently in both paths (resume response vs new task).
  • Added focused tests for the resume-ask helper to keep the logic well-defined.

Screenshots

Before:

❯ kilocode --auto "whats 2+2?" --json > /tmp/invocation1.json

kilocode-backend/cloud-agent florian/chore/fix-parse-tool-metadata-from-ask* ≡19s
❯ kilocode --auto "whats 4+4?" --json --session=0eeb751e-8d9f-4853-993f-3bfee207dbea > /tmp/invocation2.json

that second invocation doesn't do anything

{"timestamp":1,"source":"cli","id":"msg-1768591300815-2","type":"welcome","content":"","metadata":{"welcomeOptions":{"clearScreen":false,"showInstructions":false,"instructions":[]}}}
{"timestamp":1768591230871,"source":"extension","type":"say","say":"text","content":"whats 2+2?"}
{"timestamp":1768591230897,"source":"extension","type":"say","say":"api_req_started","metadata":{"apiProtocol":"openai","tokensIn":11292,"tokensOut":95,"cacheWrites":0,"cacheReads":0,"cost":0.07294625,"usageMissing":false,"inferenceProvider":"Anthropic"}}
{"timestamp":1768591242685,"source":"extension","type":"say","say":"text","partial":false,"content":"2 + 2 = **4**\n\nThis is a simple arithmetic question that doesn't require any tools or planning. The sum of 2 and 2 equals 4."}
{"timestamp":1768591243925,"source":"extension","type":"say","say":"completion_result","partial":false,"content":"2 + 2 = **4**"}
{"timestamp":1768591244327,"source":"extension","type":"ask","ask":"completion_result"}
{"timestamp":1768591300858,"source":"extension","type":"ask","ask":"resume_completed_task"}
{"sessionId":"0eeb751e-8d9f-4853-993f-3bfee207dbea","updatedAt":1768591302852,"timestamp":1768591303823,"event":"session_synced"} 

How to Test

Get in Touch

@changeset-bot
Copy link

changeset-bot bot commented Jan 18, 2026

🦋 Changeset detected

Latest commit: f660a18

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

This PR includes changesets to release 1 package
Name Type
@kilocode/cli 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

@pandemicsyn pandemicsyn added the CLI Kilo Code CLI label Jan 18, 2026
@pandemicsyn pandemicsyn marked this pull request as ready for review January 18, 2026 23:14
@kiloconnect
Copy link
Contributor

kiloconnect bot commented Jan 18, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Overview

This PR properly fixes a race condition in CLI session restoration where historical completion_result messages could trigger premature CI exit before the prompt could execute.

Key improvements:

  • Sets taskResumedViaContinueOrSessionAtom flag BEFORE async session restore/fork operations
  • Adds timestamp-based filtering to distinguish historical vs new completion messages
  • Properly waits for resume ask before executing prompts
  • Comprehensive test coverage for the new behavior
Files Reviewed (8 files)
  • .changeset/cozy-shirts-try.md - Changeset for CLI patch
  • cli/src/cli.ts - Sets session flag before restore/fork
  • cli/src/state/atoms/__tests__/effects-ci-completion.test.ts - New tests for CI completion detection
  • cli/src/state/hooks/__tests__/useCIMode.test.tsx - New tests for useCIMode hook
  • cli/src/state/hooks/useCIMode.ts - Adds historical message filtering
  • cli/src/ui/UI.tsx - Adds resume ask handling and flag management
  • cli/src/ui/utils/__tests__/resumePrompt.test.ts - New tests for resume prompt utilities
  • cli/src/ui/utils/resumePrompt.ts - New utility functions for resume ask detection

Copy link

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

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: c710df7d14

ℹ️ 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 on lines 193 to 197
const shouldAnswerResumeAsk = taskResumedViaSession && isResumeAskMessage(lastChatMessage)

// Determine if it's a command or regular message
if (isCommandInput(trimmedPrompt)) {
if (isCommandInput(trimmedPrompt) && !shouldAnswerResumeAsk) {
executeCommand(trimmedPrompt, onExit)

Choose a reason for hiding this comment

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

P2 Badge Keep CLI commands executable during resume asks

When --auto is used with --session/--continue, a prompt that starts with / is still a CLI command (e.g., /exit, /clear). This new guard skips executeCommand whenever a resume ask is pending, so the command text is instead sent as a messageResponse and treated as user input by the resumed task. That means command-driven automation in resumed sessions no longer works and can even feed /exit or /clear to the model. Consider handling commands before the resume-ask branch or explicitly erroring instead of sending the command string as a response.

Useful? React with 👍 / 👎.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@pandemicsyn are you not using /commands within the prompt? Could this not represent an issue then?

@pandemicsyn pandemicsyn merged commit 809251d into main Jan 19, 2026
12 checks passed
@pandemicsyn pandemicsyn deleted the florian/fix/session-restore-race branch January 19, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLI Kilo Code CLI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants