@@ -62,6 +62,17 @@ const decodeClaudeSettings = Schema.decodeUnknownEffect(ClaudeSettings);
6262const JSONL_EXTENSION = ".jsonl" ;
6363const MAX_LABEL_LENGTH = 80 ;
6464const MAX_TRANSCRIPT_MESSAGES = 200 ;
65+ // Every Claude Code session id observed on disk is a UUID (the filename
66+ // stem of `<sessionId>.jsonl`). `sessionId` is caller-controlled input that
67+ // gets interpolated into a filesystem path (`getTranscript`) — validating it
68+ // against this shape before doing anything with it closes off path
69+ // traversal (`../../other-project/secret`) rather than merely resolving to
70+ // a possibly-out-of-bounds path and hoping the caller was honest.
71+ const CLAUDE_SESSION_ID_PATTERN = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } $ / i;
72+
73+ function isValidClaudeSessionId ( sessionId : string ) : boolean {
74+ return CLAUDE_SESSION_ID_PATTERN . test ( sessionId ) ;
75+ }
6576
6677function encodeCwdForClaudeProjectDir ( cwd : string ) : string {
6778 return cwd . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "-" ) ;
@@ -220,10 +231,13 @@ function parseTranscriptMessages(
220231 return messages ;
221232}
222233
223- export interface ClaudeSessionHistoryBindResumeSessionInput {
234+ export interface ClaudeSessionHistoryBindSessionLaunchOptionsInput {
224235 readonly threadId : ThreadId ;
225236 readonly providerInstanceId : ProviderInstanceId ;
226- readonly resumeExternalSessionId : string ;
237+ /** Resume a picked on-disk session's model context via `--resume <uuid>`. */
238+ readonly resumeExternalSessionId ?: string ;
239+ /** Start the session with Claude Code's Remote Control (`--remote-control`) enabled. */
240+ readonly remoteControl ?: boolean ;
227241}
228242
229243export class ClaudeSessionHistory extends Context . Service <
@@ -242,12 +256,14 @@ export class ClaudeSessionHistory extends Context.Service<
242256 input : ServerGetClaudeResumableSessionTranscriptInput ,
243257 ) => Effect . Effect < ServerGetClaudeResumableSessionTranscriptResult > ;
244258 /**
245- * Binds a picked on-disk Claude session to a thread's provider session
246- * directory entry, so the next turn resumes it via `--resume <uuid>`.
247- * Fails if the target provider instance isn't a Claude instance.
259+ * Binds thread-creation-time Claude launch choices (resume a picked
260+ * on-disk session, and/or start with Remote Control enabled) onto a
261+ * thread's provider session directory entry, so they take effect on the
262+ * next (first) turn. No-op if neither option is set. Fails if the
263+ * target provider instance isn't a Claude instance.
248264 */
249- readonly bindResumeSession : (
250- input : ClaudeSessionHistoryBindResumeSessionInput ,
265+ readonly bindSessionLaunchOptions : (
266+ input : ClaudeSessionHistoryBindSessionLaunchOptionsInput ,
251267 ) => Effect . Effect < void , ProviderValidationError | ProviderSessionDirectoryWriteError > ;
252268 }
253269> ( ) ( "t3/provider/Layers/ClaudeSessionHistory" ) { }
@@ -266,20 +282,27 @@ export const make = Effect.gen(function* () {
266282 // rather than failing the whole listing — best-effort, matching `list`'s
267283 // other file-not-found handling.
268284 const resolveConfigDirPathForInstance = Effect . fn ( "ClaudeSessionHistory.resolveConfigDirPath" ) (
269- function * ( providerInstanceId : ProviderInstanceId | undefined ) {
285+ function * ( input : {
286+ readonly providerInstanceId : ProviderInstanceId | undefined ;
287+ readonly workspaceRoot : string ;
288+ } ) {
270289 const homePath = yield * Effect . gen ( function * ( ) {
271- if ( providerInstanceId === undefined ) return "" ;
290+ if ( input . providerInstanceId === undefined ) return "" ;
272291 const settings = yield * serverSettings . getSettings ;
273292 const configMap = deriveProviderInstanceConfigMap ( settings ) ;
274- const entry = configMap [ providerInstanceId ] ;
293+ const entry = configMap [ input . providerInstanceId ] ;
275294 const decoded = yield * decodeClaudeSettings ( entry ?. config ?? { } ) ;
276295 return decoded . homePath ;
277296 } ) . pipe ( Effect . orElseSucceed ( ( ) => "" ) ) ;
278297 // `resolveClaudeConfigDirPath` yields its own `Path.Path` requirement;
279298 // satisfy it from the already-resolved `path` in scope so this stays
280299 // free of a `Path.Path` requirement of its own (see the `list`/`make`
281300 // split above — required for `Layer.provideMerge` to fully resolve it).
282- return yield * resolveClaudeConfigDirPath ( { homePath } , process . env ) . pipe (
301+ // `workspaceRoot` is passed as `cwd`: when `homePath` is unset and a
302+ // *relative* `CLAUDE_CONFIG_DIR` is set in the environment, it must be
303+ // resolved the same way the spawned `claude` subprocess itself
304+ // resolves it — against the workspace cwd, not the server's own cwd.
305+ return yield * resolveClaudeConfigDirPath ( { homePath } , process . env , input . workspaceRoot ) . pipe (
283306 Effect . provideService ( Path . Path , path ) ,
284307 ) ;
285308 } ,
@@ -290,7 +313,10 @@ export const make = Effect.gen(function* () {
290313 readonly workspaceRoot : string ;
291314 readonly providerInstanceId ?: ProviderInstanceId | undefined ;
292315 } ) {
293- const configDirPath = yield * resolveConfigDirPathForInstance ( input . providerInstanceId ) ;
316+ const configDirPath = yield * resolveConfigDirPathForInstance ( {
317+ providerInstanceId : input . providerInstanceId ,
318+ workspaceRoot : input . workspaceRoot ,
319+ } ) ;
294320 return path . join (
295321 configDirPath ,
296322 "projects" ,
@@ -350,6 +376,16 @@ export const make = Effect.gen(function* () {
350376 const getTranscript : ClaudeSessionHistory [ "Service" ] [ "getTranscript" ] = Effect . fn (
351377 "ClaudeSessionHistory.getTranscript" ,
352378 ) ( function * ( input ) {
379+ // Best-effort/display-only endpoint (see class doc), so an invalid
380+ // sessionId — most importantly one crafted to escape `projectDirPath`
381+ // via `../` traversal segments — degrades to "no history to show"
382+ // rather than a hard error, matching this file's other not-found
383+ // handling. The validation itself is still load-bearing: it's what
384+ // stops `path.join` below from ever being handed a value that resolves
385+ // outside `projectDirPath`.
386+ if ( ! isValidClaudeSessionId ( input . sessionId ) ) {
387+ return { messages : [ ] } ;
388+ }
353389 const projectDirPath = yield * resolveProjectDirPath ( input ) ;
354390 const filePath = path . join ( projectDirPath , `${ input . sessionId } ${ JSONL_EXTENSION } ` ) ;
355391 const raw = yield * fileSystem . readFileString ( filePath ) . pipe ( Effect . orElseSucceed ( ( ) => "" ) ) ;
@@ -362,31 +398,47 @@ export const make = Effect.gen(function* () {
362398 return { messages : trimmedMessages } ;
363399 } ) ;
364400
365- const bindResumeSession : ClaudeSessionHistory [ "Service" ] [ "bindResumeSession" ] = Effect . fn (
366- "ClaudeSessionHistory.bindResumeSession" ,
367- ) ( function * ( input ) {
368- const instance = yield * providerInstanceRegistry . getInstance ( input . providerInstanceId ) ;
369- if ( ! instance ) {
370- return yield * new ProviderValidationError ( {
371- operation : "ClaudeSessionHistory.bindResumeSession" ,
372- issue : `Provider instance '${ input . providerInstanceId } ' is not configured.` ,
373- } ) ;
374- }
375- if ( instance . driverKind !== CLAUDE_PROVIDER ) {
376- return yield * new ProviderValidationError ( {
377- operation : "ClaudeSessionHistory.bindResumeSession" ,
378- issue : `resumeExternalSessionId is only supported for Claude Code provider instances; '${ input . providerInstanceId } ' is a '${ instance . driverKind } ' instance.` ,
401+ const bindSessionLaunchOptions : ClaudeSessionHistory [ "Service" ] [ "bindSessionLaunchOptions" ] =
402+ Effect . fn ( "ClaudeSessionHistory.bindSessionLaunchOptions" ) ( function * ( input ) {
403+ if ( input . resumeExternalSessionId === undefined && ! input . remoteControl ) {
404+ return ;
405+ }
406+ if (
407+ input . resumeExternalSessionId !== undefined &&
408+ ! isValidClaudeSessionId ( input . resumeExternalSessionId )
409+ ) {
410+ return yield * new ProviderValidationError ( {
411+ operation : "ClaudeSessionHistory.bindSessionLaunchOptions" ,
412+ issue : `resumeExternalSessionId must be a Claude session UUID; received '${ input . resumeExternalSessionId } '.` ,
413+ } ) ;
414+ }
415+ const instance = yield * providerInstanceRegistry . getInstance ( input . providerInstanceId ) ;
416+ if ( ! instance ) {
417+ return yield * new ProviderValidationError ( {
418+ operation : "ClaudeSessionHistory.bindSessionLaunchOptions" ,
419+ issue : `Provider instance '${ input . providerInstanceId } ' is not configured.` ,
420+ } ) ;
421+ }
422+ if ( instance . driverKind !== CLAUDE_PROVIDER ) {
423+ return yield * new ProviderValidationError ( {
424+ operation : "ClaudeSessionHistory.bindSessionLaunchOptions" ,
425+ issue : `Resume/Remote Control are only supported for Claude Code provider instances; '${ input . providerInstanceId } ' is a '${ instance . driverKind } ' instance.` ,
426+ } ) ;
427+ }
428+ yield * providerSessionDirectory . upsert ( {
429+ threadId : input . threadId ,
430+ provider : instance . driverKind ,
431+ providerInstanceId : input . providerInstanceId ,
432+ resumeCursor : {
433+ ...( input . resumeExternalSessionId !== undefined
434+ ? { resume : input . resumeExternalSessionId }
435+ : { } ) ,
436+ ...( input . remoteControl ? { remoteControl : true } : { } ) ,
437+ } ,
379438 } ) ;
380- }
381- yield * providerSessionDirectory . upsert ( {
382- threadId : input . threadId ,
383- provider : instance . driverKind ,
384- providerInstanceId : input . providerInstanceId ,
385- resumeCursor : { resume : input . resumeExternalSessionId } ,
386439 } ) ;
387- } ) ;
388440
389- return ClaudeSessionHistory . of ( { list, getTranscript, bindResumeSession } ) ;
441+ return ClaudeSessionHistory . of ( { list, getTranscript, bindSessionLaunchOptions } ) ;
390442} ) ;
391443
392444export const layer = Layer . effect ( ClaudeSessionHistory , make ) ;
0 commit comments