@@ -20,11 +20,12 @@ import * as TestClock from "effect/testing/TestClock";
2020
2121import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts" ;
2222import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts" ;
23+ import * as EffectWorker from "./EffectWorker.ts" ;
2324import * as EventSink from "./EventSink.ts" ;
2425import * as Orchestrator from "./Orchestrator.ts" ;
2526import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts" ;
2627import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts" ;
27- import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
28+ import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
2829
2930const forkCases = (
3031 [
@@ -395,3 +396,217 @@ it.effect.each(forkCases)(
395396 assert . isNull ( target . providerThreads [ 0 ] ?. forkedFrom ) ;
396397 } ) . pipe ( Effect . provide ( layer ) ) ,
397398) ;
399+
400+ it . effect . each ( [
401+ { execution : "portable_context" , rejected : true } ,
402+ { execution : "native_fork" , rejected : false } ,
403+ ] as const ) (
404+ "a $execution fork's first /compact is rejected only when no history reaches it: $rejected" ,
405+ ( { execution, rejected } ) => {
406+ const opened : Array < ProviderInstanceId > = [ ] ;
407+ const adapter = ( driverName : "claudeAgent" | "codex" ) => {
408+ const driver = ProviderDriverKind . make ( driverName ) ;
409+ const instanceId = ProviderInstanceId . make ( driverName ) ;
410+ return {
411+ instanceId,
412+ driver,
413+ getCapabilities : ( ) =>
414+ Effect . succeed (
415+ driver === "codex" ? CodexProviderCapabilitiesV2 : ClaudeProviderCapabilitiesV2 ,
416+ ) ,
417+ planSelectionTransition : ( ) => Effect . succeed ( { type : "apply_on_next_turn" } ) ,
418+ openSession : ( input ) =>
419+ Effect . sync ( ( ) => opened . push ( instanceId ) ) . pipe (
420+ Effect . andThen (
421+ new ProviderAdapter . ProviderAdapterOpenSessionError ( {
422+ driver,
423+ providerSessionId : input . providerSessionId ,
424+ } ) ,
425+ ) ,
426+ ) ,
427+ } satisfies ProviderAdapter . ProviderAdapterV2 [ "Service" ] ;
428+ } ;
429+ const claude = adapter ( "claudeAgent" ) ;
430+ const target = execution === "portable_context" ? adapter ( "codex" ) : claude ;
431+ return Effect . gen ( function * ( ) {
432+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
433+ const eventSink = yield * EventSink . EventSinkV2 ;
434+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
435+ const now = yield * DateTime . now ;
436+ const sourceThreadId = ThreadId . make ( "fork-compact-source" ) ;
437+ const targetThreadId = ThreadId . make ( "fork-compact-target" ) ;
438+ const providerThreadId = ProviderThreadId . make ( "fork-compact-native-thread" ) ;
439+ const sourceRunId = RunId . make ( "fork-compact-source-run" ) ;
440+ const userMessageId = MessageId . make ( "fork-compact-source-message" ) ;
441+ const sourceSelection = { instanceId : claude . instanceId , model : "test-model" } ;
442+
443+ yield * orchestrator . dispatch ( {
444+ type : "thread.create" ,
445+ commandId : CommandId . make ( "fork-compact-create-source" ) ,
446+ threadId : sourceThreadId ,
447+ projectId : ProjectId . make ( "fork-compact-project" ) ,
448+ title : "Fork compact source" ,
449+ modelSelection : sourceSelection ,
450+ runtimeMode : "full-access" ,
451+ interactionMode : "default" ,
452+ branch : null ,
453+ worktreePath : null ,
454+ createdBy : "user" ,
455+ creationSource : "web" ,
456+ } ) ;
457+ const item = {
458+ threadId : sourceThreadId ,
459+ runId : sourceRunId ,
460+ nodeId : null ,
461+ providerThreadId,
462+ providerTurnId : null ,
463+ nativeItemRef : null ,
464+ parentItemId : null ,
465+ status : "completed" ,
466+ title : null ,
467+ startedAt : now ,
468+ completedAt : now ,
469+ updatedAt : now ,
470+ } as const ;
471+ yield * eventSink . write ( {
472+ events : [
473+ {
474+ id : EventId . make ( "fork-compact-provider-thread" ) ,
475+ type : "provider-thread.updated" ,
476+ threadId : sourceThreadId ,
477+ occurredAt : now ,
478+ payload : {
479+ id : providerThreadId ,
480+ driver : claude . driver ,
481+ providerInstanceId : claude . instanceId ,
482+ providerSessionId : null ,
483+ appThreadId : sourceThreadId ,
484+ ownerNodeId : null ,
485+ nativeThreadRef : {
486+ driver : claude . driver ,
487+ nativeId : "native-fork-compact-source" ,
488+ strength : "strong" ,
489+ } ,
490+ nativeConversationHeadRef : null ,
491+ status : "idle" ,
492+ firstRunOrdinal : 1 ,
493+ lastRunOrdinal : 1 ,
494+ handoffIds : [ ] ,
495+ forkedFrom : null ,
496+ createdAt : now ,
497+ updatedAt : now ,
498+ } ,
499+ } ,
500+ {
501+ id : EventId . make ( "fork-compact-source-run" ) ,
502+ type : "run.created" ,
503+ threadId : sourceThreadId ,
504+ runId : sourceRunId ,
505+ occurredAt : now ,
506+ payload : {
507+ id : sourceRunId ,
508+ threadId : sourceThreadId ,
509+ ordinal : 1 ,
510+ providerInstanceId : claude . instanceId ,
511+ modelSelection : sourceSelection ,
512+ providerThreadId,
513+ userMessageId,
514+ rootNodeId : null ,
515+ activeAttemptId : null ,
516+ status : "completed" ,
517+ queuePosition : null ,
518+ requestedAt : now ,
519+ startedAt : now ,
520+ completedAt : now ,
521+ checkpointId : null ,
522+ contextHandoffId : null ,
523+ } ,
524+ } ,
525+ // Each message alone exceeds the default 16k handoff budget, so a
526+ // portable handoff omits both whole items.
527+ {
528+ id : EventId . make ( "fork-compact-user-item" ) ,
529+ type : "turn-item.updated" ,
530+ threadId : sourceThreadId ,
531+ runId : sourceRunId ,
532+ occurredAt : now ,
533+ payload : {
534+ ...item ,
535+ id : TurnItemId . make ( "fork-compact-user-item" ) ,
536+ ordinal : 1 ,
537+ type : "user_message" ,
538+ createdBy : "user" ,
539+ creationSource : "web" ,
540+ inputIntent : "turn_start" ,
541+ messageId : userMessageId ,
542+ text : "u" . repeat ( 17_000 ) ,
543+ attachments : [ ] ,
544+ } ,
545+ } ,
546+ {
547+ id : EventId . make ( "fork-compact-assistant-item" ) ,
548+ type : "turn-item.updated" ,
549+ threadId : sourceThreadId ,
550+ runId : sourceRunId ,
551+ occurredAt : now ,
552+ payload : {
553+ ...item ,
554+ id : TurnItemId . make ( "fork-compact-assistant-item" ) ,
555+ ordinal : 2 ,
556+ type : "assistant_message" ,
557+ messageId : MessageId . make ( "fork-compact-assistant-message" ) ,
558+ text : "a" . repeat ( 17_000 ) ,
559+ streaming : false ,
560+ } ,
561+ } ,
562+ ] ,
563+ } ) ;
564+ yield * orchestrator . dispatch ( {
565+ type : "thread.fork" ,
566+ commandId : CommandId . make ( "fork-compact-fork" ) ,
567+ sourceThreadId,
568+ targetThreadId,
569+ sourcePoint : { type : "run" , runId : sourceRunId } ,
570+ createdBy : "user" ,
571+ creationSource : "web" ,
572+ } ) ;
573+ yield * orchestrator . dispatch ( {
574+ type : "message.dispatch" ,
575+ commandId : CommandId . make ( "fork-compact-compact" ) ,
576+ threadId : targetThreadId ,
577+ messageId : MessageId . make ( "fork-compact-compact" ) ,
578+ text : "/compact" ,
579+ attachments : [ ] ,
580+ modelSelection : { instanceId : target . instanceId , model : "test-model" } ,
581+ dispatchMode : { type : "start_immediately" } ,
582+ createdBy : "user" ,
583+ creationSource : "web" ,
584+ } ) ;
585+ yield * worker . drain ( ) ;
586+
587+ const projection = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
588+ if ( execution === "portable_context" ) {
589+ assert . equal ( projection . contextTransfers [ 0 ] ?. resolution ?. strategy , "portable_context" ) ;
590+ assert . deepEqual ( projection . contextHandoffs [ 0 ] ?. history ?. messages , [ ] ) ;
591+ } else {
592+ assert . isEmpty ( projection . contextHandoffs ) ;
593+ }
594+ const emptyCompaction = projection . turnItems . some (
595+ ( turnItem ) =>
596+ turnItem . type === "error" &&
597+ turnItem . failure . message === "Start a conversation before compacting this thread." ,
598+ ) ;
599+ assert . equal ( emptyCompaction , rejected ) ;
600+ assert . deepEqual ( opened , rejected ? [ ] : [ target . instanceId ] ) ;
601+ } ) . pipe (
602+ Effect . provide (
603+ ProviderReplayHarness . layerWithRegistry (
604+ { name : `fork-compact-${ execution } ` } ,
605+ ProviderAdapterRegistry . layerFromAdapters (
606+ target === claude ? [ claude ] : [ claude , target ] ,
607+ ) ,
608+ ) ,
609+ ) ,
610+ ) ;
611+ } ,
612+ ) ;
0 commit comments