Skip to content
This repository was archived by the owner on May 22, 2026. It is now read-only.

fix: session ID persistence with Cloudflare Workers workaround - #21

Merged
fankaidev merged 3 commits into
mainfrom
fix/session-id-persistence
Feb 19, 2026
Merged

fankaidev merged 3 commits into
mainfrom
fix/session-id-persistence

Conversation

@fankaidev

Copy link
Copy Markdown
Owner

Summary

  • Fixed session ID persistence issues caused by Cloudflare Workers bug where state.id.name is not available in Durable Object constructors
  • Implemented workaround by passing session ID via X-Session-Id header instead of relying on DO ID properties
  • Changed session ID format from UUID to ISO timestamp-based format: web-YYYYMMDDTHHmmssZ-{random} or slack-YYYYMMDDTHHmmssZ-{random}
  • Added fail-fast validation throughout the stack to catch errors early
  • Improved session list UI with full session IDs and first user message display

Technical Details

This PR implements a workaround for Cloudflare Workers issue #2240 where the name property on Durable Object IDs is not properly set even when using idFromName().

Key Changes:

  • Modified ChatSession.fetch() to extract session ID from X-Session-Id header
  • Updated all stub.fetch() calls in api.ts and slack.ts to include the header
  • Added validation checks after every idFromName() call with fail-fast error handling
  • Created /api/session/:id/init endpoint to persist sessions on creation
  • Changed session ID generation from crypto.randomUUID() to ISO timestamp format

Design Principles:

  • Added "Fail Fast, Fail Explicitly" section to README documenting the principle
  • No silent fallbacks - all unexpected conditions throw errors immediately
  • Validation checks kept in place, excessive logging removed

Test Plan

  • Verify web sessions persist to database on creation
  • Verify Slack thread sessions persist correctly
  • Test session switching between multiple sessions
  • Confirm session history loads correctly
  • Check that session IDs are displayed in full in the session list
  • Verify first user message appears in session list
  • Test fail-fast error handling when session ID mismatches occur
  • Confirm no console errors during normal operation

🤖 Generated with Claude Code

fankaidev and others added 2 commits February 19, 2026 21:12
This change fixes session ID persistence issues caused by Cloudflare Workers bug #2240
where state.id.name is not available in Durable Object constructors.

Changes:
- Pass session ID via X-Session-Id header instead of relying on DO ID properties
- Change session ID format from UUID to ISO timestamp-based: web/slack-YYYYMMDDTHHmmssZ-{random}
- Add fail-fast validation after idFromName() calls to catch errors early
- Add /api/session/:id/init endpoint to initialize sessions on creation
- Display full session IDs in session list (not truncated)
- Show first user message in session list for better context
- Add "Fail Fast, Fail Explicitly" design principles to README
- Clean up excessive logging while keeping validation checks

Technical details:
- Workaround for cloudflare/workerd#2240
- Modified ChatSession.fetch() to extract session ID from request header
- Added session ID mismatch validation throughout the stack
- All stub.fetch() calls now include X-Session-Id header

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Split initialization into two phases to improve /history endpoint performance:
- initialize(): Lightweight, loads only session and messages from D1
- initializeFilesystem(): Heavy, initializes file system, directories, and git mounts

Changes:
- /history requests now skip filesystem initialization (3s → ~100ms expected)
- /chat and bash commands lazy-load filesystem only when needed
- Add performance logging to identify bottlenecks
- Filesystem state cached in DO instance, initialized once per DO lifetime

Performance improvements:
- Session query timing
- Messages query timing with count
- Directory initialization timing
- Mount restoration timing
- Total timings for both phases

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Move /history endpoint from Durable Object to Worker API layer
- Query D1 database directly instead of routing through DO
- Remove unused /history endpoint from ChatSession DO
- Add Known Issues section to README documenting CF Workers bug #2240

This optimization reduces the /history endpoint latency from ~3s to <100ms
by avoiding unnecessary DO initialization and filesystem setup.

Related: #22

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@fankaidev
fankaidev merged commit 7569716 into main Feb 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant