Skip to content

Commit 3429999

Browse files
feat(tool): allow human-readable slugs as task_id
The task tool now accepts human-readable slugs (e.g. "explore-auth") as task_id, making it easy for LLMs to create and resume named task sessions without carrying opaque ses_ IDs across turns. If the slug hasn't been used in the current root session, a new session is created with a derived ID (ses_<hash>_<slug>). If it already exists, the session is resumed with full context. Closes anomalyco#32118
1 parent 73dbd8a commit 3429999

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎packages/opencode/src/session/session.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ export const CreateInput = Schema.optional(
255255
metadata: Schema.optional(Metadata),
256256
permission: Schema.optional(PermissionV1.Ruleset),
257257
workspaceID: Schema.optional(WorkspaceV2.ID),
258+
id: Schema.optional(SessionID),
258259
}),
259260
)
260261
export type CreateInput = Types.DeepMutable<Schema.Schema.Type<typeof CreateInput>>
@@ -469,7 +470,9 @@ export interface Interface {
469470
metadata?: typeof Metadata.Type
470471
permission?: PermissionV1.Ruleset
471472
workspaceID?: WorkspaceV2.ID
473+
id?: SessionID
472474
}) => Effect.Effect<Info>
475+
readonly root: (sessionID: SessionID) => Effect.Effect<SessionID, NotFound>
473476
readonly fork: (input: { sessionID: SessionID; messageID?: MessageID }) => Effect.Effect<Info, NotFound>
474477
readonly touch: (sessionID: SessionID) => Effect.Effect<void>
475478
readonly get: (id: SessionID) => Effect.Effect<Info, NotFound>
@@ -714,10 +717,12 @@ export const layer: Layer.Layer<
714717
metadata?: typeof Metadata.Type
715718
permission?: PermissionV1.Ruleset
716719
workspaceID?: WorkspaceV2.ID
720+
id?: SessionID
717721
}) {
718722
const ctx = yield* InstanceState.context
719723
const workspace = yield* InstanceState.workspaceID
720724
return yield* createNext({
725+
id: input?.id,
721726
parentID: input?.parentID,
722727
directory: ctx.directory,
723728
path: sessionPath(ctx.worktree, ctx.directory),
@@ -730,6 +735,15 @@ export const layer: Layer.Layer<
730735
})
731736
})
732737

738+
const root = Effect.fn("Session.root")(function* (sessionID: SessionID) {
739+
let current = sessionID
740+
while (true) {
741+
const s = yield* get(current)
742+
if (!s.parentID) return current
743+
current = s.parentID
744+
}
745+
})
746+
733747
const fork = Effect.fn("Session.fork")(function* (input: { sessionID: SessionID; messageID?: MessageID }) {
734748
const ctx = yield* InstanceState.context
735749
const original = yield* get(input.sessionID)
@@ -936,6 +950,7 @@ export const layer: Layer.Layer<
936950
list,
937951
listGlobal,
938952
create,
953+
root,
939954
fork,
940955
touch,
941956
get,

‎packages/opencode/src/tool/task.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ import { Effect, Exit, Schema, Scope } from "effect"
1414
import { EffectBridge } from "@/effect/bridge"
1515
import { RuntimeFlags } from "@/effect/runtime-flags"
1616
import { Database } from "@opencode-ai/core/database/database"
17+
import { createHash } from "crypto"
18+
19+
function isSlug(taskId: string): boolean {
20+
return !taskId.startsWith("ses_")
21+
}
22+
23+
function deriveSlugSessionID(slug: string, rootID: SessionID): SessionID {
24+
const hash = createHash("sha256").update(rootID).digest("hex").slice(0, 4)
25+
return SessionID.descending(`ses_${hash}_${slug}`)
26+
}
1727

1828
export interface TaskPromptOps {
1929
cancel(sessionID: SessionID): Effect.Effect<void>
@@ -46,7 +56,7 @@ const BaseParameterFields = {
4656
subagent_type: Schema.String.annotate({ description: "The type of specialized agent to use for this task" }),
4757
task_id: Schema.optional(Schema.String).annotate({
4858
description:
49-
"This should only be set if you mean to resume a previous task (you can pass a prior task_id and the task will continue the same subagent session as before instead of creating a fresh one)",
59+
'A human-readable slug (e.g. "explore-auth") to create or resume a named task session within this root session. If the slug has not been used yet, a new task is created with that identifier. If it already exists, the existing session is resumed. Also accepts full "ses_..." session IDs to resume a specific session directly.',
5060
}),
5161
command: Schema.optional(Schema.String).annotate({ description: "The command that triggered this task" }),
5262
}
@@ -118,8 +128,13 @@ export const TaskTool = Tool.define(
118128
return yield* Effect.fail(new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`))
119129
}
120130

131+
const slugTaskId = params.task_id && isSlug(params.task_id) ? params.task_id : undefined
132+
const derivedID = slugTaskId
133+
? deriveSlugSessionID(slugTaskId, yield* sessions.root(ctx.sessionID))
134+
: undefined
135+
121136
const session = params.task_id
122-
? yield* sessions.get(SessionID.make(params.task_id)).pipe(Effect.catchCause(() => Effect.succeed(undefined)))
137+
? yield* sessions.get(derivedID ?? SessionID.make(params.task_id)).pipe(Effect.catchCause(() => Effect.succeed(undefined)))
123138
: undefined
124139
const parent = yield* sessions.get(ctx.sessionID)
125140
const childPermission = deriveSubagentSessionPermission({
@@ -142,6 +157,7 @@ export const TaskTool = Tool.define(
142157
const nextSession =
143158
session ??
144159
(yield* sessions.create({
160+
id: derivedID,
145161
parentID: ctx.sessionID,
146162
title: params.description + ` (@${next.name} subagent)`,
147163
agent: next.name,

‎packages/opencode/src/tool/task.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ When NOT to use the Task tool:
1212
Usage notes:
1313
1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
1414
2. Once you have delegated work to an agent, do not duplicate that work yourself. Continue with non-overlapping tasks, or wait for the result. For background tasks, you will be notified automatically when the result is ready.
15-
3. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result. The output includes a task_id you can reuse later to continue the same subagent session.
15+
3. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result. The output includes a task_id you can reuse later to continue the same subagent session. You can also pass a human-readable slug (e.g. "explore-auth") as task_id to create or resume a named task within the current root session — if the slug has not been used yet, a new task is created; if it already exists, the existing session is resumed.
1616
4. Each agent invocation starts with a fresh context unless you provide task_id to resume the same subagent session (which continues with its previous messages and tool outputs). When starting fresh, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.
1717
5. The agent's outputs should generally be trusted
1818
6. Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, web fetches, etc.), since it is not aware of the user's intent. Tell it how to verify its work if possible (e.g., relevant test commands).

0 commit comments

Comments
 (0)