Skip to content

feat(session): allow setting custom session ID when creating a session#13004

Open
sjawhar wants to merge 1 commit intoanomalyco:devfrom
sjawhar:feat/custom-session-id
Open

feat(session): allow setting custom session ID when creating a session#13004
sjawhar wants to merge 1 commit intoanomalyco:devfrom
sjawhar:feat/custom-session-id

Conversation

@sjawhar
Copy link

@sjawhar sjawhar commented Feb 10, 2026

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 #2159

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@sjawhar sjawhar force-pushed the feat/custom-session-id branch 9 times, most recently from a847ac4 to 9ad4fbb Compare February 17, 2026 00:46
@sjawhar
Copy link
Author

sjawhar commented Feb 17, 2026

@adamdotdevin this is ready for review: CI is passing and PR is mergeable. Could you review when you have a chance?

@sjawhar sjawhar force-pushed the feat/custom-session-id branch 4 times, most recently from 806f7db to c2b4476 Compare February 27, 2026 20:05
@sjawhar sjawhar force-pushed the feat/custom-session-id branch 2 times, most recently from 141a273 to e482f0a Compare March 5, 2026 00:03
Add optional `id` field to Session.create() with strict format validation
(ses_ + 12 lowercase hex + 14 Base62). Duplicate IDs return 409 Conflict.

- Add `id` field with regex validation to Session.create schema
- Add duplicate session ID check before creation
- Add DuplicateIDError (NamedError) with 409 status mapping
- Update OpenAPI spec and regenerate SDK with id field + 409 response
- Add 6 tests: custom ID, default behavior, duplicate, invalid prefix/format

Closes anomalyco#12916

resolve: custom session id conflicts
@sjawhar sjawhar force-pushed the feat/custom-session-id branch from e482f0a to f5aa54b Compare March 6, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create session with specified ID

1 participant