Skip to content

Commit 7588374

Browse files
committed
feat(cli): add --fork-session flag for session forking
Add CLI flag to fork sessions when continuing, similar to Claude Code's behavior. The flag must be used with --continue or --session. Changes: - Add --fork-session option to run and TUI commands - Fork session at last message when flag is used - Use existing fork title convention (fork #1, fork #2, etc.) Usage: opencode --continue --fork-session opencode --session <id> --fork-session opencode run "hello" --continue --fork-session Closes anomalyco#11137
1 parent 7c0067d commit 7588374

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export const RunCommand = cmd({
5050
describe: "session id to continue",
5151
type: "string",
5252
})
53+
.option("fork-session", {
54+
type: "boolean",
55+
describe: "fork the session when continuing (use with --continue or --session)",
56+
})
5357
.option("share", {
5458
type: "boolean",
5559
describe: "share the session",
@@ -279,11 +283,16 @@ export const RunCommand = cmd({
279283
const sdk = createOpencodeClient({ baseUrl: args.attach })
280284

281285
const sessionID = await (async () => {
282-
if (args.continue) {
283-
const result = await sdk.session.list()
284-
return result.data?.find((s) => !s.parentID)?.id
286+
if (args.continue || args.session) {
287+
const sourceSessionID = args.continue
288+
? (await sdk.session.list()).data?.find((s) => !s.parentID)?.id
289+
: args.session
290+
if (args.forkSession && sourceSessionID) {
291+
const forkResult = await sdk.session.fork({ sessionID: sourceSessionID })
292+
return forkResult.data?.id
293+
}
294+
return sourceSessionID
285295
}
286-
if (args.session) return args.session
287296

288297
const title =
289298
args.title !== undefined
@@ -354,11 +363,16 @@ export const RunCommand = cmd({
354363
}
355364

356365
const sessionID = await (async () => {
357-
if (args.continue) {
358-
const result = await sdk.session.list()
359-
return result.data?.find((s) => !s.parentID)?.id
366+
if (args.continue || args.session) {
367+
const sourceSessionID = args.continue
368+
? (await sdk.session.list()).data?.find((s) => !s.parentID)?.id
369+
: args.session
370+
if (args.forkSession && sourceSessionID) {
371+
const forkResult = await sdk.session.fork({ sessionID: sourceSessionID })
372+
return forkResult.data?.id
373+
}
374+
return sourceSessionID
360375
}
361-
if (args.session) return args.session
362376

363377
const title =
364378
args.title !== undefined

‎packages/opencode/src/cli/cmd/tui/app.tsx‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,25 @@ function App() {
259259
let continued = false
260260
createEffect(() => {
261261
// When using -c, session list is loaded in blocking phase, so we can navigate at "partial"
262-
if (continued || sync.status === "loading" || !args.continue) return
263-
const match = sync.data.session
264-
.toSorted((a, b) => b.time.updated - a.time.updated)
265-
.find((x) => x.parentID === undefined)?.id
262+
if (continued || sync.status === "loading" || (!args.continue && !args.sessionID)) return
263+
264+
const match = args.sessionID
265+
? args.sessionID
266+
: sync.data.session
267+
.toSorted((a, b) => b.time.updated - a.time.updated)
268+
.find((x) => x.parentID === undefined)?.id
269+
266270
if (match) {
267271
continued = true
268-
route.navigate({ type: "session", sessionID: match })
272+
if (args.forkSession) {
273+
sdk.client.session.fork({ sessionID: match }).then((forkResult) => {
274+
if (forkResult.data) {
275+
route.navigate({ type: "session", sessionID: forkResult.data.id })
276+
}
277+
})
278+
} else {
279+
route.navigate({ type: "session", sessionID: match })
280+
}
269281
}
270282
})
271283

‎packages/opencode/src/cli/cmd/tui/context/args.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Args {
66
prompt?: string
77
continue?: boolean
88
sessionID?: string
9+
forkSession?: boolean
910
}
1011

1112
export const { use: useArgs, provider: ArgsProvider } = createSimpleContext({

‎packages/opencode/src/cli/cmd/tui/thread.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export const TuiThreadCommand = cmd({
6464
type: "string",
6565
describe: "session id to continue",
6666
})
67+
.option("fork-session", {
68+
type: "boolean",
69+
describe: "fork the session when continuing (use with --continue or --session)",
70+
})
6771
.option("prompt", {
6872
type: "string",
6973
describe: "prompt to use",
@@ -147,6 +151,7 @@ export const TuiThreadCommand = cmd({
147151
args: {
148152
continue: args.continue,
149153
sessionID: args.session,
154+
forkSession: args.forkSession,
150155
agent: args.agent,
151156
model: args.model,
152157
prompt,

0 commit comments

Comments
 (0)