11import type {
22 OrchestrationCommand ,
33 OrchestrationProject ,
4- OrchestrationReadModel ,
54 OrchestrationThread ,
65 ProjectId ,
76 ThreadId ,
87} from "@t3tools/contracts" ;
98import { normalizeProjectPathForComparison } from "@t3tools/shared/path" ;
109import * as Effect from "effect/Effect" ;
10+ import * as HashMap from "effect/HashMap" ;
1111
12+ import {
13+ findProjectById ,
14+ findThreadById ,
15+ isThreadDeleted ,
16+ listThreadsByProjectId ,
17+ type CommandReadModel ,
18+ } from "./commandReadModel.ts" ;
1219import { OrchestrationCommandInvariantError } from "./Errors.ts" ;
1320
21+ export { findProjectById , findThreadById , listThreadsByProjectId } ;
22+
1423function invariantError ( commandType : string , detail : string ) : OrchestrationCommandInvariantError {
1524 return new OrchestrationCommandInvariantError ( {
1625 commandType,
1726 detail,
1827 } ) ;
1928}
2029
21- export function findThreadById (
22- readModel : OrchestrationReadModel ,
23- threadId : ThreadId ,
24- ) : OrchestrationThread | undefined {
25- return readModel . threads . find ( ( thread ) => thread . id === threadId ) ;
26- }
27-
28- export function findProjectById (
29- readModel : OrchestrationReadModel ,
30- projectId : ProjectId ,
31- ) : OrchestrationProject | undefined {
32- return readModel . projects . find ( ( project ) => project . id === projectId ) ;
33- }
34-
35- export function listThreadsByProjectId (
36- readModel : OrchestrationReadModel ,
37- projectId : ProjectId ,
38- ) : ReadonlyArray < OrchestrationThread > {
39- return readModel . threads . filter ( ( thread ) => thread . projectId === projectId ) ;
40- }
41-
4230export function requireProject ( input : {
43- readonly readModel : OrchestrationReadModel ;
31+ readonly readModel : CommandReadModel ;
4432 readonly command : OrchestrationCommand ;
4533 readonly projectId : ProjectId ;
4634} ) : Effect . Effect < OrchestrationProject , OrchestrationCommandInvariantError > {
@@ -57,7 +45,7 @@ export function requireProject(input: {
5745}
5846
5947export function requireProjectAbsent ( input : {
60- readonly readModel : OrchestrationReadModel ;
48+ readonly readModel : CommandReadModel ;
6149 readonly command : OrchestrationCommand ;
6250 readonly projectId : ProjectId ;
6351} ) : Effect . Effect < void , OrchestrationCommandInvariantError > {
@@ -73,18 +61,23 @@ export function requireProjectAbsent(input: {
7361}
7462
7563export function requireActiveProjectWorkspaceRootAbsent ( input : {
76- readonly readModel : OrchestrationReadModel ;
64+ readonly readModel : CommandReadModel ;
7765 readonly command : OrchestrationCommand ;
7866 readonly workspaceRoot : string ;
7967 readonly exceptProjectId ?: ProjectId ;
8068} ) : Effect . Effect < void , OrchestrationCommandInvariantError > {
8169 const normalizedWorkspaceRoot = normalizeProjectPathForComparison ( input . workspaceRoot ) ;
82- const existingProject = input . readModel . projects . find (
83- ( project ) =>
70+ let existingProject : OrchestrationProject | undefined ;
71+ for ( const project of HashMap . values ( input . readModel . projects ) ) {
72+ if (
8473 project . deletedAt === null &&
8574 normalizeProjectPathForComparison ( project . workspaceRoot ) === normalizedWorkspaceRoot &&
86- project . id !== input . exceptProjectId ,
87- ) ;
75+ project . id !== input . exceptProjectId
76+ ) {
77+ existingProject = project ;
78+ break ;
79+ }
80+ }
8881 if ( existingProject === undefined ) {
8982 return Effect . void ;
9083 }
@@ -97,7 +90,7 @@ export function requireActiveProjectWorkspaceRootAbsent(input: {
9790}
9891
9992export function requireThread ( input : {
100- readonly readModel : OrchestrationReadModel ;
93+ readonly readModel : CommandReadModel ;
10194 readonly command : OrchestrationCommand ;
10295 readonly threadId : ThreadId ;
10396} ) : Effect . Effect < OrchestrationThread , OrchestrationCommandInvariantError > {
@@ -114,7 +107,7 @@ export function requireThread(input: {
114107}
115108
116109export function requireThreadArchived ( input : {
117- readonly readModel : OrchestrationReadModel ;
110+ readonly readModel : CommandReadModel ;
118111 readonly command : OrchestrationCommand ;
119112 readonly threadId : ThreadId ;
120113} ) : Effect . Effect < OrchestrationThread , OrchestrationCommandInvariantError > {
@@ -133,7 +126,7 @@ export function requireThreadArchived(input: {
133126}
134127
135128export function requireThreadNotArchived ( input : {
136- readonly readModel : OrchestrationReadModel ;
129+ readonly readModel : CommandReadModel ;
137130 readonly command : OrchestrationCommand ;
138131 readonly threadId : ThreadId ;
139132} ) : Effect . Effect < OrchestrationThread , OrchestrationCommandInvariantError > {
@@ -152,11 +145,16 @@ export function requireThreadNotArchived(input: {
152145}
153146
154147export function requireThreadAbsent ( input : {
155- readonly readModel : OrchestrationReadModel ;
148+ readonly readModel : CommandReadModel ;
156149 readonly command : OrchestrationCommand ;
157150 readonly threadId : ThreadId ;
158151} ) : Effect . Effect < void , OrchestrationCommandInvariantError > {
159- if ( ! findThreadById ( input . readModel , input . threadId ) ) {
152+ // A deleted thread is evicted from `threads` but its id is retained in
153+ // `deletedThreadIds`, so reject re-using a live OR previously-deleted id.
154+ if (
155+ ! findThreadById ( input . readModel , input . threadId ) &&
156+ ! isThreadDeleted ( input . readModel , input . threadId )
157+ ) {
160158 return Effect . void ;
161159 }
162160 return Effect . fail (
0 commit comments