Skip to content

Commit 3806a40

Browse files
committed
add --fork flag to duplicate sessions before continuing
- Add --fork CLI flag for session forking with --continue or --session - Forked sessions get automatic (fork #N) title suffix via existing Session.fork() - Validate that --fork requires --continue or --session - Applies to both attach mode and bootstrap mode Fixes anomalyco#11137
1 parent 7c0067d commit 3806a40

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

  • packages/opencode/src/cli/cmd

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

Lines changed: 31 additions & 4 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", {
54+
describe: "fork the session before continuing (requires --continue or --session)",
55+
type: "boolean",
56+
})
5357
.option("share", {
5458
type: "boolean",
5559
describe: "share the session",
@@ -133,6 +137,11 @@ export const RunCommand = cmd({
133137
process.exit(1)
134138
}
135139

140+
if (args.fork && !args.continue && !args.session) {
141+
UI.error("--fork requires --continue or --session")
142+
process.exit(1)
143+
}
144+
136145
const execute = async (sdk: OpencodeClient, sessionID: string) => {
137146
const printEvent = (color: string, type: string, title: string) => {
138147
UI.println(
@@ -279,11 +288,20 @@ export const RunCommand = cmd({
279288
const sdk = createOpencodeClient({ baseUrl: args.attach })
280289

281290
const sessionID = await (async () => {
291+
let id: string | undefined
282292
if (args.continue) {
283293
const result = await sdk.session.list()
284-
return result.data?.find((s) => !s.parentID)?.id
294+
id = result.data?.find((s) => !s.parentID)?.id
295+
} else if (args.session) {
296+
id = args.session
285297
}
286-
if (args.session) return args.session
298+
299+
if (id && args.fork) {
300+
const forked = await sdk.session.fork({ sessionID: id })
301+
return forked.data?.id
302+
}
303+
304+
if (id) return id
287305

288306
const title =
289307
args.title !== undefined
@@ -354,11 +372,20 @@ export const RunCommand = cmd({
354372
}
355373

356374
const sessionID = await (async () => {
375+
let id: string | undefined
357376
if (args.continue) {
358377
const result = await sdk.session.list()
359-
return result.data?.find((s) => !s.parentID)?.id
378+
id = result.data?.find((s) => !s.parentID)?.id
379+
} else if (args.session) {
380+
id = args.session
360381
}
361-
if (args.session) return args.session
382+
383+
if (id && args.fork) {
384+
const forked = await sdk.session.fork({ sessionID: id })
385+
return forked.data?.id
386+
}
387+
388+
if (id) return id
362389

363390
const title =
364391
args.title !== undefined

0 commit comments

Comments
 (0)