[PR #13004] feat(session): allow setting custom session ID when creating a session #14469

Open
opened 2026-02-16 18:19:15 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/13004

State: open
Merged: No


Summary

Adds support for clients to provide a custom session ID when creating a session via POST /session.

  • Custom ID: POST /session {"id": "ses_<valid>"} creates a session with that exact ID
  • Default behavior: POST /session {} still auto-generates an ID (unchanged)
  • Duplicate detection: Attempting to create a session with an ID that already exists returns 409 Conflict
  • Validation: ID must match ^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$ — invalid formats return 400

Changes

Source (4 files, +32 lines)

  • packages/opencode/src/session/index.ts — Add id field with strict regex to Session.create() schema, duplicate check via Session.get(), new DuplicateIDError (NamedError)
  • packages/opencode/src/server/server.ts — Map DuplicateIDError to HTTP 409
  • packages/opencode/src/server/error.ts — Add 409 to OpenAPI ERRORS map (lazy getter to avoid circular import at init time)
  • packages/opencode/src/server/routes/session.ts — Document 409 in session.create route spec

Tests (1 file, +77 lines)

  • packages/opencode/test/session/session.test.ts — 6 new tests: custom ID accepted, default behavior preserved, duplicate returns error, invalid prefix/format/charset rejected

Generated (3 files)

  • SDK types + OpenAPI spec regenerated via ./script/generate.ts

Testing

  • All 9 session tests pass (bun test test/session/session.test.ts)
  • Typecheck clean (bun run typecheck)
  • Manually tested via curl against local server: 200/409/400 responses all correct

Closes #12916

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13004 **State:** open **Merged:** No --- ## Summary Adds support for clients to provide a custom session ID when creating a session via `POST /session`. - **Custom ID**: `POST /session {"id": "ses_<valid>"}` creates a session with that exact ID - **Default behavior**: `POST /session {}` still auto-generates an ID (unchanged) - **Duplicate detection**: Attempting to create a session with an ID that already exists returns **409 Conflict** - **Validation**: ID must match `^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$` — invalid formats return 400 ## Changes ### Source (4 files, +32 lines) - **`packages/opencode/src/session/index.ts`** — Add `id` field with strict regex to `Session.create()` schema, duplicate check via `Session.get()`, new `DuplicateIDError` (NamedError) - **`packages/opencode/src/server/server.ts`** — Map `DuplicateIDError` to HTTP 409 - **`packages/opencode/src/server/error.ts`** — Add 409 to OpenAPI ERRORS map (lazy getter to avoid circular import at init time) - **`packages/opencode/src/server/routes/session.ts`** — Document 409 in session.create route spec ### Tests (1 file, +77 lines) - **`packages/opencode/test/session/session.test.ts`** — 6 new tests: custom ID accepted, default behavior preserved, duplicate returns error, invalid prefix/format/charset rejected ### Generated (3 files) - SDK types + OpenAPI spec regenerated via `./script/generate.ts` ## Testing - All 9 session tests pass (`bun test test/session/session.test.ts`) - Typecheck clean (`bun run typecheck`) - Manually tested via `curl` against local server: 200/409/400 responses all correct Closes #12916
yindo added the pull-request label 2026-02-16 18:19:15 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14469