@@ -16,6 +16,7 @@ import {
1616} from "@t3tools/contracts" ;
1717import * as DateTime from "effect/DateTime" ;
1818import * as Effect from "effect/Effect" ;
19+ import * as Layer from "effect/Layer" ;
1920
2021import {
2122 makeSubagentChildThread ,
@@ -26,7 +27,9 @@ import {
2627 endRunlessRootTurns ,
2728} from "@t3tools/provider-core/server/subagentProjection" ;
2829
29- import { emptyProjection } from "./ProjectionStore.ts" ;
30+ import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
31+ import * as ProjectionStore from "./ProjectionStore.ts" ;
32+ import { resolveAutoSettlementAt } from "./ThreadSettlementService.ts" ;
3033
3134const parentThreadId = ThreadId . make ( "thread:subagent-snoozed-parent" ) ;
3235const childThreadId = ThreadId . make ( "thread:subagent-awake-child" ) ;
@@ -117,8 +120,62 @@ it("keeps a subagent child awake when its parent thread is snoozed", () => {
117120 } ) ;
118121} ) ;
119122
123+ it . effect . each ( [
124+ [ "sql" , ProjectionStore . layer . pipe ( Layer . provideMerge ( SqlitePersistence . layerMemory ) ) ] ,
125+ ] as const ) (
126+ "%s: a pinned parent's subagent child is born unpinned and settles once idle" ,
127+ ( [ , testLayer ] ) =>
128+ Effect . gen ( function * ( ) {
129+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
130+ const parentThread = { ...makeParentThread ( ) , pinnedAt : parentCreatedAt , pinOrderKey : "a0" } ;
131+ const childThread = makeSubagentChildThread ( {
132+ parentThread,
133+ childThreadId,
134+ parentNodeId : NodeId . make ( "node:subagent-parent" ) ,
135+ activeProviderThreadId : null ,
136+ providerInstanceId : childProviderInstanceId ,
137+ modelSelection : childModelSelection ,
138+ title : "Child" ,
139+ now : childCreatedAt ,
140+ createdBy : "agent" ,
141+ creationSource : "provider" ,
142+ } ) ;
143+
144+ assert . equal ( parentThread . pinnedAt , parentCreatedAt ) ;
145+ assert . equal ( parentThread . pinOrderKey , "a0" ) ;
146+ assert . isNull ( childThread . pinnedAt ) ;
147+ assert . isNull ( childThread . pinOrderKey ) ;
148+
149+ for ( const thread of [ parentThread , childThread ] ) {
150+ yield * store . apply ( {
151+ id : EventId . make ( `event:${ thread . id } ` ) ,
152+ type : "thread.created" ,
153+ threadId : thread . id ,
154+ occurredAt : thread . createdAt ,
155+ payload : thread ,
156+ } ) ;
157+ }
158+ const savedParent = yield * store . getThread ( parentThreadId ) ;
159+ assert . deepEqual ( savedParent . pinnedAt , parentCreatedAt ) ;
160+ assert . equal ( savedParent . pinOrderKey , "a0" ) ;
161+ const candidates = yield * store . getSettlementCandidates ( ) ;
162+ assert . deepEqual (
163+ candidates . map ( ( candidate ) => candidate . id ) ,
164+ [ childThreadId ] ,
165+ ) ;
166+ const settledAt = resolveAutoSettlementAt ( {
167+ thread : candidates [ 0 ] ! ,
168+ pullRequest : null ,
169+ nowMs : DateTime . toEpochMillis ( DateTime . add ( childCreatedAt , { days : 4 } ) ) ,
170+ autoSettleAfterDays : 3 ,
171+ autoSettleOnMerge : false ,
172+ } ) ;
173+ assert . equal ( settledAt && DateTime . formatIso ( settledAt ) , DateTime . formatIso ( childCreatedAt ) ) ;
174+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
175+ ) ;
176+
120177function taskFixture ( ) {
121- const projection = emptyProjection ( {
178+ const projection = ProjectionStore . emptyProjection ( {
122179 type : "thread.created" ,
123180 id : EventId . make ( "task-fixture" ) ,
124181 threadId : parentThreadId ,
0 commit comments