Skip to content

feat(cli): print session id at the end of run#17229

Open
phall1 wants to merge 1 commit intoanomalyco:devfrom
phall1:ouptput-session-id-in-cli-runs
Open

feat(cli): print session id at the end of run#17229
phall1 wants to merge 1 commit intoanomalyco:devfrom
phall1:ouptput-session-id-in-cli-runs

Conversation

@phall1
Copy link

@phall1 phall1 commented Mar 12, 2026

Issue for this PR

[FEATURE]: output session_id after a cli run #17221
Closes #17221

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

opencode run already creates or resolves a session ID internally, but before this change it did not surface that ID at the end of the run in a reliable way for follow-up commands or shell scripts.

Updates packages/opencode/src/cli/cmd/run.ts so

  1. the run flow waits for the event loop to fully finish, then
  2. emits a final session.complete event and
  3. prints session: <id> at the end of the command. In the default CLI format, that final line is printed with UI.println, which goes to stderr, so it stays visible to the user without polluting stdout pipelines.

In --format json, the new session.complete event gives scripts a clean final event containing the sessionID.
This works because (1) the session ID is already known before the prompt/command is dispatched, and (2) awaiting the run loop guarantees the final output happens after all prior run events have been flushed.

How did you verify your code works?

  • Ran LSP diagnostics on packages/opencode/src/cli/cmd/run.ts and confirmed there were no errors in the modified file
  • Verified the committed diff only changes the run command behavior
  • Confirmed the default output path uses UI.println/stderr, while JSON mode emits structured events to stdout
  • Ran bun typecheck in packages/opencode, which currently reports unrelated pre-existing type errors in test and fixture files outside this change

Scripting patterns:

# Chain sessions (JSON mode — cleanest):
SID=$(opencode run "first task" --format json | jq -r 'select(.type=="session.complete") | .sessionID')
opencode run --session "$SID" "continue"

# Chain sessions (default mode — stderr parsing):

SID=$(opencode run "first task" 2>&1 >/dev/null | grep '^session:' | awk '{print $2}')
opencode run --session "$SID" "continue"

Screenshots / recordings

default

opencode ouptput-session-id-in-cli-runs default 
❯ bun run --cwd packages/opencode --conditions=browser src/index.ts run "say hi in one short sentence" --model opencode/big-pickle

Hello!

session: ses_31c86153bffewXqO5SoLPNqIPN

--json

opencode ouptput-session-id-in-cli-runs default  
❯ SID=$(bun run --cwd packages/opencode --conditions=browser src/index.ts run "say hi in one short sentence" --model opencode/big-pickle  --format json | jq -r 'select(.type=="session.complete") | .sessionID')
session: ses_31c818748ffe1zDv2xGY099qeY

opencode ouptput-session-id-in-cli-runs
❯ echo $SID
ses_31c818748ffe1zDv2xGY099qeY

stderr redirect:

bun run --cwd packages/opencode --conditions=browser src/index.ts run "say hi in one short sentence" --model opencode/big-pickle 2>&1 >/dev/null

session: ses_31c7cfea2ffecwIrub1jvV67qr

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 12, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

[FEATURE]: output session_id after a cli run

1 participant