diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index 06563f901db..4e68f594b0b 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -586,6 +586,7 @@ export type ExperimentalSessionWaitOperation = ( export type ExperimentalSessionSpawnInput = { readonly sessionID: Session.ID + readonly id?: Session.ID | undefined readonly notify?: boolean | undefined readonly agent?: Agent.ID | undefined readonly model?: Model.Ref | undefined diff --git a/packages/client/src/effect/generated/client.ts b/packages/client/src/effect/generated/client.ts index 7aff07a0c87..f663d39f1a1 100644 --- a/packages/client/src/effect/generated/client.ts +++ b/packages/client/src/effect/generated/client.ts @@ -851,6 +851,7 @@ const EndpointExperimentalSessionSpawn = (raw: RawClient["server.session"]) => ( raw["session.spawn"]({ params: { sessionID: input["sessionID"] }, payload: { + id: input["id"], notify: input["notify"], agent: input["agent"], model: input["model"], diff --git a/packages/client/src/promise/generated/client.ts b/packages/client/src/promise/generated/client.ts index 3114dba1f65..30dc9c9b153 100644 --- a/packages/client/src/promise/generated/client.ts +++ b/packages/client/src/promise/generated/client.ts @@ -1127,6 +1127,7 @@ export function make(options: ClientOptions) { method: "POST", path: `/api/experimental/session/${encodeURIComponent(input.sessionID)}/spawn`, body: { + id: input["id"], notify: input["notify"], agent: input["agent"], model: input["model"], diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 29352c0aca0..d40b3c6fbbd 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -3875,6 +3875,7 @@ export type ExperimentalSessionWaitOutput = SessionWaitResponse["data"] export type ExperimentalSessionSpawnInput = { readonly sessionID: string + readonly id?: string | null | undefined readonly notify?: boolean | null | undefined readonly agent?: string | null | undefined readonly model?: { readonly id: string; readonly providerID: string; readonly variant?: string } | null | undefined diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 7d743594ea1..f9386e14f09 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -2761,8 +2761,15 @@ ], "security": [], "responses": { - "204": { - "description": "" + "200": { + "description": "SessionWaitResponse", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionWaitResponse" + } + } + } }, "400": { "description": "InvalidRequestError", @@ -2794,6 +2801,16 @@ } } }, + "500": { + "description": "UnknownError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnknownErrorEncoded" + } + } + } + }, "503": { "description": "ServiceUnavailableError", "content": { @@ -2805,10 +2822,172 @@ } } }, - "description": "Wait for a session agent loop to become idle.", + "description": "Wait for a session agent loop to become idle and report its outcome and final text.", "summary": "Wait for session" } }, + "/api/experimental/session/{sessionID}/spawn": { + "post": { + "tags": ["session"], + "operationId": "experimental.session.spawn", + "parameters": [ + { + "name": "sessionID", + "in": "path", + "schema": { + "$ref": "#/components/schemas/Session.ID" + }, + "required": true + } + ], + "security": [], + "responses": { + "200": { + "description": "SessionSpawnResponse", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SessionSpawnResponse" + } + } + } + }, + "400": { + "description": "InvalidRequestError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/InvalidRequestErrorEncoded" + } + } + } + }, + "401": { + "description": "UnauthorizedError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnauthorizedErrorEncoded" + } + } + } + }, + "404": { + "description": "SessionNotFoundError | LocationNotFoundError", + "content": { + "application/json": { + "schema": { + "anyOf": [ + { + "$ref": "#/components/schemas/SessionNotFoundErrorEncoded" + }, + { + "$ref": "#/components/schemas/LocationNotFoundErrorEncoded" + } + ] + } + } + } + }, + "409": { + "description": "ConflictError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ConflictErrorEncoded" + } + } + } + }, + "500": { + "description": "UnknownError", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UnknownErrorEncoded" + } + } + } + } + }, + "description": "Experimental: may change without compatibility guarantees. Create a child session under this session and optionally admit its first prompt. Pass an optional `id` to make the call retry-safe; reusing an id owned by another session fails with 409.", + "summary": "Spawn subagent session", + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "anyOf": [ + { + "$ref": "#/components/schemas/Session.ID" + }, + { + "type": "null" + } + ], + "description": "Client-chosen child id. A retry with the same id under the same parent returns the recorded child without admitting its prompt again. An id recorded under another parent, on a root session, or equal to the parent fails with 409." + }, + "notify": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "null" + } + ] + }, + "agent": { + "anyOf": [ + { + "$ref": "#/components/schemas/Agent.ID" + }, + { + "type": "null" + } + ] + }, + "model": { + "anyOf": [ + { + "$ref": "#/components/schemas/Model.Ref" + }, + { + "type": "null" + } + ] + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "prompt": { + "anyOf": [ + { + "$ref": "#/components/schemas/PromptInput" + }, + { + "type": "null" + } + ] + } + }, + "additionalProperties": false + } + } + }, + "required": true + } + } + }, "/api/session/{sessionID}/revert/stage": { "post": { "tags": ["session"], @@ -21735,6 +21914,34 @@ "required": ["id", "name"], "additionalProperties": false }, + "PromptInput": { + "type": "object", + "properties": { + "text": { + "type": "string" + }, + "files": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PromptInput.FileAttachment" + } + }, + "agents": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Prompt.AgentAttachment" + } + }, + "skills": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PromptInput.SkillAttachment" + } + } + }, + "required": ["text"], + "additionalProperties": false + }, "PromptInput.FileAttachment": { "type": "object", "properties": { @@ -24476,6 +24683,23 @@ "required": ["_tag", "sessionID", "message"], "additionalProperties": false }, + "SessionSpawnResponse": { + "type": "object", + "properties": { + "data": { + "type": "object", + "properties": { + "id": { + "$ref": "#/components/schemas/Session.ID" + } + }, + "required": ["id"], + "additionalProperties": false + } + }, + "required": ["data"], + "additionalProperties": false + }, "SessionStats.Activity": { "type": "object", "properties": { @@ -24832,6 +25056,27 @@ "required": ["info", "messages"], "additionalProperties": false }, + "SessionWaitResponse": { + "type": "object", + "properties": { + "data": { + "type": "object", + "properties": { + "outcome": { + "type": "string", + "enum": ["succeeded", "failed", "interrupted"] + }, + "text": { + "type": "string" + } + }, + "required": ["text"], + "additionalProperties": false + } + }, + "required": ["data"], + "additionalProperties": false + }, "SessionsCursor": { "type": "string", "description": "Opaque pagination cursor returned as cursor.previous or cursor.next in the previous response." diff --git a/packages/protocol/src/groups/session.ts b/packages/protocol/src/groups/session.ts index 4d96ddadca9..b0008da9b1a 100644 --- a/packages/protocol/src/groups/session.ts +++ b/packages/protocol/src/groups/session.ts @@ -475,6 +475,10 @@ export const makeSessionGroup = }).pipe(Effect.scoped), ) +it.live("spawns a child at a caller-chosen id and replays a retry without duplicating it", () => + Effect.gen(function* () { + const handler = yield* ServerFetch.make({ + app: { version: "test" }, + database: { path: ":memory:" }, + fs: { filewatcher: false }, + models: { fetch: false }, + }) + const parent = Schema.decodeUnknownSync(SessionResponse)( + yield* Effect.promise(async () => { + const response = await handler( + new Request("http://opencode.local/api/session", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ title: "Parent" }), + }), + ) + expect(response.status).toBe(200) + return response.json() + }), + ).data + const body = JSON.stringify({ id: Session.ID.create(), title: "Child", notify: false }) + const spawn = () => + Effect.promise(async () => { + const response = await handler( + new Request(`http://opencode.local/api/experimental/session/${parent.id}/spawn`, { + method: "POST", + headers: { "content-type": "application/json" }, + body, + }), + ) + expect(response.status).toBe(200) + return Schema.decodeUnknownSync(Schema.Struct({ data: Schema.Struct({ id: Session.ID }) }))( + await response.json(), + ).data + }) + const spawned = yield* spawn() + const replayed = yield* spawn() + const children = Schema.decodeUnknownSync(SessionsResponse)( + yield* Effect.promise(async () => { + const response = await handler(new Request(`http://opencode.local/api/session?parentID=${parent.id}`)) + expect(response.status).toBe(200) + return response.json() + }), + ) + + expect(spawned.id).toBe(JSON.parse(body).id) + expect(replayed.id).toBe(spawned.id) + expect(children.data.map((session) => session.id)).toEqual([spawned.id]) + }).pipe(Effect.scoped), +) + +it.live("returns conflict when the spawn id is not a child of this parent", () => + Effect.gen(function* () { + const handler = yield* ServerFetch.make({ + app: { version: "test" }, + database: { path: ":memory:" }, + fs: { filewatcher: false }, + models: { fetch: false }, + }) + const parent = Schema.decodeUnknownSync(SessionResponse)( + yield* Effect.promise(async () => { + const response = await handler( + new Request("http://opencode.local/api/session", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ title: "Parent" }), + }), + ) + expect(response.status).toBe(200) + return response.json() + }), + ).data + const response = yield* Effect.promise(() => + handler( + new Request(`http://opencode.local/api/experimental/session/${parent.id}/spawn`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ id: parent.id, title: "Child", notify: false }), + }), + ), + ) + expect(response.status).toBe(409) + expect(yield* Effect.promise(() => response.json())).toMatchObject({ + _tag: "ConflictError", + resource: parent.id, + }) + }).pipe(Effect.scoped), +) + it.live("returns not found when spawning under a missing session", () => Effect.gen(function* () { const handler = yield* ServerFetch.make({