- Replace provider session-id centric plumbing with thread-id based runtime handling across server orchestration and provider layers - Update persistence repositories/migrations and contracts for threadless runtime semantics - Refresh integration/unit tests and web session/store types to match the new runtime model
7.5 KiB
Provider Runtime Refactor Plan
Goal
Remove providerSessionId and providerThreadId from the core application model and make
provider continuity depend only on:
- canonical
threadId - provider kind
- runtime mode
- opaque
resumeCursor
Live provider session/process state becomes ephemeral and provider-private.
Desired Architecture
Durable state
Persist only the data required to reconstruct a provider conversation after process restart:
threadIdproviderruntimeModeresumeCursor(opaque)statusactiveTurnId/lastError/updatedAtif needed for UX- optional provider runtime payload only if it is required for resume or useful UX
Do not persist:
providerSessionIdproviderThreadId- live child-process/session handles
- adapter-private routing aliases
Ephemeral state
Each adapter keeps an in-memory map keyed by canonical threadId:
threadId -> live provider context
Examples:
- Codex child process context
- Cursor ACP live session
- Claude Code live subprocess context
These are disposable and rebuilt from persisted resumeCursor.
Observability
Provider-native identifiers should live in logs/runtime events only, not in the canonical DB model.
Examples:
- Codex thread id
- Cursor ACP thread/session id
- Claude conversation/thread id
High-Level Refactor Sequence
1. Contracts cleanup
Remove provider identity fields from shared contracts:
packages/contracts/src/provider.tspackages/contracts/src/orchestration.tspackages/contracts/src/providerRuntime.ts- related tests
Planned changes:
- remove
ProviderSessionIdfrom cross-layer service payloads where possible - remove
providerSessionId/providerThreadIdfrom thread session snapshot contracts - keep
resumeCursoropaque
2. Redefine thread runtime/session projection model
Replace thread session projection as a provider-identity carrier with a pure UX/runtime status view.
Possible projected shape:
statusproviderNameruntimeModeactiveTurnIdlastErrorupdatedAt
No provider-native ids in the projection.
Files likely affected:
apps/server/src/persistence/Services/ProjectionThreadSessions.tsapps/server/src/persistence/Layers/ProjectionThreadSessions.tsapps/server/src/orchestration/projector.tsapps/server/src/orchestration/Layers/ProjectionPipeline.tsapps/server/src/orchestration/Layers/ProjectionSnapshotQuery.tsapps/web/src/store.ts
3. Make provider service thread-keyed
Refactor provider service APIs to route by canonical threadId, not providerSessionId.
Methods to change:
startSessionsendTurninterruptTurnrespondToRequestrollbackConversationstopSession/ equivalentlistSessions
New mental model:
- orchestration calls provider service with
threadId - provider service resolves/creates live adapter context for that canonical thread
- adapter handles provider-native resume semantics internally
Files likely affected:
apps/server/src/provider/Services/ProviderService.tsapps/server/src/provider/Layers/ProviderService.tsapps/server/src/provider/Services/ProviderAdapter.ts- all adapter implementations/tests
4. Replace session directory with thread-keyed runtime directory
Current ProviderSessionDirectory is centered on session ids.
Replace with a thread-keyed directory/repository.
Persist per canonical thread:
threadIdproviderruntimeModeresumeCursorstatus- optional runtime payload
Likely replacement:
ProviderThreadRuntimeDirectoryor similar
Files likely affected:
apps/server/src/provider/Services/ProviderSessionDirectory.tsapps/server/src/provider/Layers/ProviderSessionDirectory.tsapps/server/src/persistence/Services/ProviderSessionRuntime.tsapps/server/src/persistence/Layers/ProviderSessionRuntime.ts
5. Database migration
Perform a clean-slate schema migration to remove provider ids from persistence.
Targets:
- remove
provider_session_id - remove
provider_thread_id - make canonical thread id the primary runtime key where appropriate
Likely tables impacted:
provider_session_runtimeprojection_thread_sessions
Because SQLite column removal is awkward, and app is still early. A destructive migration is preferred. Existing users will need to reset their db so migrations run from scratch.
Migration should also preserve:
resume_cursor_jsonruntime_moderuntime_payload_json- status metadata
6. Orchestration refactor
Remove provider-id assumptions from orchestration.
Affected areas:
ProviderCommandReactorProviderRuntimeIngestionCheckpointReactor- decider/projector event payloads
Rules:
- orchestration should only know canonical
threadId - restart/reconcile should use provider service APIs keyed by canonical thread
- no orchestration logic should depend on provider-native ids
7. Adapter refactor
Each adapter should:
- keep a
Map<ThreadId, LiveContext>(LiveContext is a provider-specific context object, typed independently for each adapter) - use persisted opaque
resumeCursorto recreate live state - emit provider-native ids only in runtime/log events if desired
Specific adapters:
CodexAdapterCursorAdapterClaudeCodeAdapter
8. Runtime events and logs
Keep provider-native ids in logs and runtime events only when useful for debugging.
Need to review:
packages/contracts/src/providerRuntime.tsapps/server/src/provider/Layers/*Adapter.ts- log sinks / canonical event logging
Goal:
- provider-native ids optional and observational
- not required by orchestration/persistence correctness
9. Web/client cleanup
Web should consume only canonical thread status.
Remove use of:
providerSessionIdproviderThreadId
Likely files:
apps/web/src/store.tsapps/web/src/types.ts- related tests
10. Test migration
Update and simplify tests across:
- provider service tests
- orchestration tests
- integration harnesses
- checkpoint/runtime ingestion tests
Tests should validate:
- thread continuity across restart using persisted opaque
resumeCursor - runtime mode switches after reopen
- no correctness dependency on provider-native ids
Suggested Execution Order
- Add/adjust plan-approved contracts
- Introduce new thread-keyed provider runtime persistence alongside old code temporarily in implementation
- Refactor provider service/adapters to use canonical
threadId - Switch orchestration to thread-keyed APIs
- Switch projections/web contracts
- Remove old provider-id fields and dead code
- Run migration and clean tests
Key Invariants After Refactor
- Canonical
threadIdis the only cross-layer routing key resumeCursoris the only persisted continuity primitive- provider-native ids are never required for correctness
- live provider session/process state is always reconstructible and disposable
- restart/reopen behavior is first-class, not a stale-session special case
Open Questions
- Should
thread.sessionremain in snapshots at all, or be folded into thread runtime fields? - Should provider runtime payload remain persisted if not required for resume?
- Do we want one provider runtime row per canonical thread, or a more generic runtime-state table keyed by thread?
Recommendation
Proceed with a full clean-slate migration rather than incremental compatibility shims. The current model is carrying too much provider-native identity into layers that should only reason about canonical threads and opaque resume state.