@lota-sdk/core 0.4.13 → 0.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/package.json +4 -4
  2. package/src/ai/embedding-cache.ts +17 -11
  3. package/src/ai-gateway/ai-gateway.ts +164 -94
  4. package/src/ai-gateway/index.ts +4 -1
  5. package/src/config/agent-defaults.ts +2 -2
  6. package/src/config/agent-types.ts +1 -1
  7. package/src/config/constants.ts +1 -1
  8. package/src/create-runtime.ts +259 -200
  9. package/src/db/cursor-pagination.ts +2 -9
  10. package/src/db/memory-store.ts +194 -175
  11. package/src/db/memory.ts +125 -71
  12. package/src/db/schema-fingerprint.ts +5 -4
  13. package/src/db/service-normalization.ts +4 -3
  14. package/src/db/service.ts +3 -2
  15. package/src/db/startup.ts +15 -16
  16. package/src/effect/errors.ts +161 -21
  17. package/src/effect/index.ts +0 -1
  18. package/src/embeddings/provider.ts +15 -7
  19. package/src/queues/autonomous-job.queue.ts +10 -22
  20. package/src/queues/delayed-node-promotion.queue.ts +8 -14
  21. package/src/queues/document-processor.queue.ts +13 -4
  22. package/src/queues/memory-consolidation.queue.ts +26 -14
  23. package/src/queues/plan-agent-heartbeat.queue.ts +48 -31
  24. package/src/queues/plan-scheduler.queue.ts +37 -15
  25. package/src/queues/queue-factory.ts +59 -35
  26. package/src/queues/standalone-worker.ts +3 -2
  27. package/src/redis/connection.ts +10 -3
  28. package/src/redis/org-memory-lock.ts +1 -1
  29. package/src/redis/redis-lease-lock.ts +5 -5
  30. package/src/redis/stream-context.ts +1 -1
  31. package/src/runtime/chat-message.ts +64 -1
  32. package/src/runtime/chat-run-orchestration.ts +33 -20
  33. package/src/runtime/context-compaction/context-compaction-runtime.ts +14 -7
  34. package/src/runtime/context-compaction/context-compaction.ts +78 -66
  35. package/src/runtime/domain-layer.ts +19 -13
  36. package/src/runtime/execution-plan.ts +7 -3
  37. package/src/runtime/memory/memory-block.ts +3 -9
  38. package/src/runtime/memory/memory-scope.ts +3 -1
  39. package/src/runtime/plugin-resolution.ts +2 -1
  40. package/src/runtime/post-turn-side-effects.ts +6 -5
  41. package/src/runtime/retrieval-adapters.ts +8 -20
  42. package/src/runtime/runtime-config.ts +3 -9
  43. package/src/runtime/runtime-extensions.ts +2 -4
  44. package/src/runtime/runtime-lifecycle.ts +56 -16
  45. package/src/runtime/runtime-services.ts +180 -102
  46. package/src/runtime/runtime-worker-registry.ts +3 -1
  47. package/src/runtime/social-chat/social-chat-agent-runner.ts +1 -1
  48. package/src/runtime/social-chat/social-chat-history.ts +21 -18
  49. package/src/runtime/social-chat/social-chat.ts +356 -223
  50. package/src/runtime/specialist-runner.ts +3 -1
  51. package/src/runtime/team-consultation/team-consultation-orchestrator.ts +3 -2
  52. package/src/runtime/thread-turn-context.ts +142 -102
  53. package/src/runtime/turn-lifecycle.ts +15 -46
  54. package/src/services/agent-activity.service.ts +1 -1
  55. package/src/services/agent-executor.service.ts +107 -77
  56. package/src/services/autonomous-job.service.ts +354 -293
  57. package/src/services/background-work.service.ts +3 -3
  58. package/src/services/context-compaction.service.ts +7 -2
  59. package/src/services/document-chunk.service.ts +50 -32
  60. package/src/services/execution-plan/execution-plan-schedule.ts +5 -3
  61. package/src/services/execution-plan/execution-plan.service.ts +162 -179
  62. package/src/services/feedback-loop.service.ts +5 -4
  63. package/src/services/graph-full-routing.ts +37 -36
  64. package/src/services/institutional-memory.service.ts +28 -30
  65. package/src/services/learned-skill.service.ts +107 -72
  66. package/src/services/memory/memory-errors.ts +4 -23
  67. package/src/services/memory/memory-org-memory.ts +10 -5
  68. package/src/services/memory/memory-rerank.ts +18 -6
  69. package/src/services/memory/memory.service.ts +170 -111
  70. package/src/services/memory/rerank.service.ts +29 -20
  71. package/src/services/organization-member.service.ts +1 -1
  72. package/src/services/organization.service.ts +69 -75
  73. package/src/services/ownership-dispatcher.service.ts +40 -39
  74. package/src/services/plan/plan-agent-heartbeat.service.ts +22 -24
  75. package/src/services/plan/plan-agent-query.service.ts +39 -31
  76. package/src/services/plan/plan-completion-side-effects.ts +13 -17
  77. package/src/services/plan/plan-coordination.service.ts +2 -1
  78. package/src/services/plan/plan-cycle.service.ts +6 -5
  79. package/src/services/plan/plan-deadline.service.ts +57 -54
  80. package/src/services/plan/plan-event-delivery.service.ts +5 -4
  81. package/src/services/plan/plan-executor-graph.ts +18 -15
  82. package/src/services/plan/plan-executor.service.ts +235 -262
  83. package/src/services/plan/plan-run.service.ts +169 -93
  84. package/src/services/plan/plan-scheduler.service.ts +192 -202
  85. package/src/services/plan/plan-template.service.ts +1 -1
  86. package/src/services/plan/plan-transaction-events.ts +1 -1
  87. package/src/services/plan/plan-workspace.service.ts +23 -14
  88. package/src/services/plugin-executor.service.ts +5 -9
  89. package/src/services/queue-job.service.ts +117 -59
  90. package/src/services/recent-activity-title.service.ts +13 -12
  91. package/src/services/recent-activity.service.ts +6 -1
  92. package/src/services/social-chat-history.service.ts +29 -25
  93. package/src/services/system-executor.service.ts +5 -9
  94. package/src/services/thread/thread-active-run.ts +2 -2
  95. package/src/services/thread/thread-listing.ts +61 -57
  96. package/src/services/thread/thread-memory-block.ts +73 -48
  97. package/src/services/thread/thread-message.service.ts +76 -65
  98. package/src/services/thread/thread-record-store.ts +8 -8
  99. package/src/services/thread/thread-title.service.ts +10 -4
  100. package/src/services/thread/thread-turn-execution.ts +43 -45
  101. package/src/services/thread/thread-turn-preparation.service.ts +257 -135
  102. package/src/services/thread/thread-turn-streaming.ts +83 -92
  103. package/src/services/thread/thread-turn.ts +18 -16
  104. package/src/services/thread/thread.service.ts +135 -100
  105. package/src/services/user.service.ts +45 -48
  106. package/src/storage/attachment-parser.ts +6 -2
  107. package/src/storage/attachment-storage.service.ts +5 -6
  108. package/src/storage/generated-document-storage.service.ts +1 -1
  109. package/src/system-agents/context-compaction.agent.ts +10 -9
  110. package/src/system-agents/delegated-agent-factory.ts +30 -6
  111. package/src/system-agents/memory-reranker.agent.ts +10 -9
  112. package/src/system-agents/memory.agent.ts +10 -9
  113. package/src/system-agents/recent-activity-title-refiner.agent.ts +13 -15
  114. package/src/system-agents/regular-chat-memory-digest.agent.ts +13 -12
  115. package/src/system-agents/skill-extractor.agent.ts +13 -12
  116. package/src/system-agents/skill-manager.agent.ts +13 -12
  117. package/src/system-agents/thread-router.agent.ts +11 -7
  118. package/src/system-agents/title-generator.agent.ts +13 -12
  119. package/src/tools/fetch-webpage.tool.ts +13 -13
  120. package/src/tools/memory-block.tool.ts +3 -1
  121. package/src/tools/plan-approval.tool.ts +4 -2
  122. package/src/tools/read-file-parts.tool.ts +10 -4
  123. package/src/tools/remember-memory.tool.ts +3 -1
  124. package/src/tools/research-topic.tool.ts +9 -5
  125. package/src/tools/search-web.tool.ts +16 -16
  126. package/src/tools/search.tool.ts +20 -5
  127. package/src/tools/team-think.tool.ts +61 -38
  128. package/src/utils/async.ts +5 -5
  129. package/src/utils/errors.ts +19 -18
  130. package/src/utils/sse-keepalive.ts +28 -25
  131. package/src/workers/bootstrap.ts +75 -11
  132. package/src/workers/memory-consolidation.worker.ts +82 -91
  133. package/src/workers/organization-learning.worker.ts +14 -4
  134. package/src/workers/regular-chat-memory-digest.runner.ts +105 -67
  135. package/src/workers/skill-extraction.runner.ts +97 -61
  136. package/src/workers/utils/repo-structure-extractor.ts +13 -8
  137. package/src/workers/utils/thread-message-query.ts +24 -24
  138. package/src/workers/worker-utils.ts +23 -4
  139. package/src/effect/helpers.ts +0 -123
@@ -33,7 +33,7 @@ import type { RecordIdInput } from '../../db/record-id'
33
33
  import { ensureRecordId, recordIdToString } from '../../db/record-id'
34
34
  import type { SurrealDBService } from '../../db/service'
35
35
  import { TABLES } from '../../db/tables'
36
- import { makeEffectTryPromiseWithMessage } from '../../effect/helpers'
36
+ import { ERROR_TAGS } from '../../effect/errors'
37
37
  import { DatabaseServiceTag } from '../../effect/services'
38
38
  import { toIsoDateTimeString, toOptionalIsoDateTimeString } from '../../utils/date-time'
39
39
 
@@ -159,41 +159,37 @@ function serializeEvent(event: PlanEventRecord): SerializablePlanEvent {
159
159
  }
160
160
  }
161
161
 
162
- class PlanRunServiceError extends Schema.TaggedErrorClass<PlanRunServiceError>()('PlanRunServiceError', {
162
+ class PlanRunServiceError extends Schema.TaggedErrorClass<PlanRunServiceError>()(ERROR_TAGS.PlanRunServiceError, {
163
163
  message: Schema.String,
164
164
  cause: Schema.optional(Schema.Defect),
165
165
  }) {}
166
166
 
167
- class PlanRunNotFoundError extends Schema.TaggedErrorClass<PlanRunNotFoundError>()('PlanRunNotFoundError', {
167
+ class PlanRunNotFoundError extends Schema.TaggedErrorClass<PlanRunNotFoundError>()(ERROR_TAGS.PlanRunNotFoundError, {
168
168
  message: Schema.String,
169
169
  entity: Schema.Literals(['plan spec', 'plan run', 'plan node spec', 'plan node run']),
170
170
  id: Schema.String,
171
171
  }) {}
172
172
 
173
173
  class PlanRunSerializationError extends Schema.TaggedErrorClass<PlanRunSerializationError>()(
174
- 'PlanRunSerializationError',
174
+ '@lota-sdk/core/PlanRunSerializationError',
175
175
  { message: Schema.String },
176
176
  ) {}
177
177
 
178
- const effectTryPlanRunPromise = makeEffectTryPromiseWithMessage(
179
- (message, cause) => new PlanRunServiceError({ message, cause }),
180
- )
181
-
182
- function tryPlanRunPromise<A>(
183
- message: string,
184
- evaluate: () => PromiseLike<A> | Effect.Effect<A, unknown>,
185
- ): Effect.Effect<A, PlanRunServiceError> {
186
- return effectTryPlanRunPromise(evaluate, message)
178
+ function toPlanRunServiceError(message: string, cause: unknown): PlanRunServiceError {
179
+ return new PlanRunServiceError({ message, cause })
187
180
  }
188
181
 
189
182
  export function makePlanRunService(db: SurrealDBService) {
190
183
  const getPlanSpecByIdEffect = (planSpecId: RecordIdInput) =>
191
184
  Effect.gen(function* () {
192
185
  const planSpecRecordId = ensureRecordId(planSpecId, TABLES.PLAN_SPEC)
193
- const spec = yield* tryPlanRunPromise(
194
- `Failed to load plan spec ${recordIdToString(planSpecId, TABLES.PLAN_SPEC)}`,
195
- () => db.findOne(TABLES.PLAN_SPEC, { id: planSpecRecordId }, PlanSpecSchema),
196
- )
186
+ const spec = yield* db
187
+ .findOne(TABLES.PLAN_SPEC, { id: planSpecRecordId }, PlanSpecSchema)
188
+ .pipe(
189
+ Effect.mapError((cause) =>
190
+ toPlanRunServiceError(`Failed to load plan spec ${recordIdToString(planSpecId, TABLES.PLAN_SPEC)}`, cause),
191
+ ),
192
+ )
197
193
 
198
194
  if (!spec) {
199
195
  return yield* new PlanRunNotFoundError({
@@ -207,40 +203,61 @@ export function makePlanRunService(db: SurrealDBService) {
207
203
  })
208
204
 
209
205
  const listPlanSpecsByThreadEffect = (threadId: RecordIdInput) =>
210
- tryPlanRunPromise(`Failed to load plan specs for thread ${recordIdToString(threadId, TABLES.THREAD)}`, () =>
211
- db.findMany(TABLES.PLAN_SPEC, { threadId: ensureRecordId(threadId, TABLES.THREAD) }, PlanSpecSchema, {
206
+ db
207
+ .findMany(TABLES.PLAN_SPEC, { threadId: ensureRecordId(threadId, TABLES.THREAD) }, PlanSpecSchema, {
212
208
  orderBy: 'createdAt',
213
209
  orderDir: 'DESC',
214
- }),
215
- )
210
+ })
211
+ .pipe(
212
+ Effect.mapError((cause) =>
213
+ toPlanRunServiceError(
214
+ `Failed to load plan specs for thread ${recordIdToString(threadId, TABLES.THREAD)}`,
215
+ cause,
216
+ ),
217
+ ),
218
+ )
216
219
 
217
220
  const listRunsBySpecEffect = (planSpecId: RecordIdInput) =>
218
- tryPlanRunPromise(`Failed to load plan runs for spec ${recordIdToString(planSpecId, TABLES.PLAN_SPEC)}`, () =>
219
- db.findMany(TABLES.PLAN_RUN, { planSpecId: ensureRecordId(planSpecId, TABLES.PLAN_SPEC) }, PlanRunSchema, {
221
+ db
222
+ .findMany(TABLES.PLAN_RUN, { planSpecId: ensureRecordId(planSpecId, TABLES.PLAN_SPEC) }, PlanRunSchema, {
220
223
  orderBy: 'createdAt',
221
224
  orderDir: 'DESC',
222
- }),
223
- )
225
+ })
226
+ .pipe(
227
+ Effect.mapError((cause) =>
228
+ toPlanRunServiceError(
229
+ `Failed to load plan runs for spec ${recordIdToString(planSpecId, TABLES.PLAN_SPEC)}`,
230
+ cause,
231
+ ),
232
+ ),
233
+ )
224
234
 
225
235
  const listNodeSpecsEffect = (planSpecId: RecordIdInput) =>
226
- tryPlanRunPromise(`Failed to load plan node specs for spec ${recordIdToString(planSpecId, TABLES.PLAN_SPEC)}`, () =>
227
- db.findMany(
236
+ db
237
+ .findMany(
228
238
  TABLES.PLAN_NODE_SPEC,
229
239
  { planSpecId: ensureRecordId(planSpecId, TABLES.PLAN_SPEC) },
230
240
  PlanNodeSpecRecordSchema,
231
241
  { orderBy: 'position', orderDir: 'ASC' },
232
- ),
233
- )
242
+ )
243
+ .pipe(
244
+ Effect.mapError((cause) =>
245
+ toPlanRunServiceError(
246
+ `Failed to load plan node specs for spec ${recordIdToString(planSpecId, TABLES.PLAN_SPEC)}`,
247
+ cause,
248
+ ),
249
+ ),
250
+ )
234
251
 
235
252
  const getNodeSpecByNodeIdEffect = (planSpecId: RecordIdInput, nodeId: string) =>
236
253
  Effect.gen(function* () {
237
- const nodeSpec = yield* tryPlanRunPromise(`Failed to load plan node spec "${nodeId}"`, () =>
238
- db.findOne(
254
+ const nodeSpec = yield* db
255
+ .findOne(
239
256
  TABLES.PLAN_NODE_SPEC,
240
257
  { planSpecId: ensureRecordId(planSpecId, TABLES.PLAN_SPEC), nodeId },
241
258
  PlanNodeSpecRecordSchema,
242
- ),
243
- )
259
+ )
260
+ .pipe(Effect.mapError((cause) => toPlanRunServiceError(`Failed to load plan node spec "${nodeId}"`, cause)))
244
261
 
245
262
  if (!nodeSpec) {
246
263
  return yield* new PlanRunNotFoundError({
@@ -256,9 +273,13 @@ export function makePlanRunService(db: SurrealDBService) {
256
273
  const getRunByIdEffect = (runId: RecordIdInput) =>
257
274
  Effect.gen(function* () {
258
275
  const runRecordId = ensureRecordId(runId, TABLES.PLAN_RUN)
259
- const run = yield* tryPlanRunPromise(`Failed to load plan run ${recordIdToString(runId, TABLES.PLAN_RUN)}`, () =>
260
- db.findOne(TABLES.PLAN_RUN, { id: runRecordId }, PlanRunSchema),
261
- )
276
+ const run = yield* db
277
+ .findOne(TABLES.PLAN_RUN, { id: runRecordId }, PlanRunSchema)
278
+ .pipe(
279
+ Effect.mapError((cause) =>
280
+ toPlanRunServiceError(`Failed to load plan run ${recordIdToString(runId, TABLES.PLAN_RUN)}`, cause),
281
+ ),
282
+ )
262
283
 
263
284
  if (!run) {
264
285
  return yield* new PlanRunNotFoundError({
@@ -273,14 +294,19 @@ export function makePlanRunService(db: SurrealDBService) {
273
294
 
274
295
  const getActiveRunRecordsEffect = (threadId: RecordIdInput) =>
275
296
  Effect.gen(function* () {
276
- const runs = yield* tryPlanRunPromise(
277
- `Failed to load active plan runs for thread ${recordIdToString(threadId, TABLES.THREAD)}`,
278
- () =>
279
- db.findMany(TABLES.PLAN_RUN, { threadId: ensureRecordId(threadId, TABLES.THREAD) }, PlanRunSchema, {
280
- orderBy: 'updatedAt',
281
- orderDir: 'DESC',
282
- }),
283
- )
297
+ const runs = yield* db
298
+ .findMany(TABLES.PLAN_RUN, { threadId: ensureRecordId(threadId, TABLES.THREAD) }, PlanRunSchema, {
299
+ orderBy: 'updatedAt',
300
+ orderDir: 'DESC',
301
+ })
302
+ .pipe(
303
+ Effect.mapError((cause) =>
304
+ toPlanRunServiceError(
305
+ `Failed to load active plan runs for thread ${recordIdToString(threadId, TABLES.THREAD)}`,
306
+ cause,
307
+ ),
308
+ ),
309
+ )
284
310
  return runs.filter((run) => ACTIVE_RUN_STATUSES.has(run.status))
285
311
  })
286
312
 
@@ -305,26 +331,39 @@ export function makePlanRunService(db: SurrealDBService) {
305
331
  }
306
332
 
307
333
  const statuses = params.statuses ? new Set(params.statuses) : ACTIVE_RUN_STATUSES
308
- const runs = yield* tryPlanRunPromise(
309
- `Failed to load execution plans in context ${recordIdToString(params.organizationId, TABLES.ORGANIZATION)}`,
310
- () => db.findMany(TABLES.PLAN_RUN, filter, PlanRunSchema, { orderBy: 'updatedAt', orderDir: 'DESC' }),
311
- )
334
+ const runs = yield* db
335
+ .findMany(TABLES.PLAN_RUN, filter, PlanRunSchema, { orderBy: 'updatedAt', orderDir: 'DESC' })
336
+ .pipe(
337
+ Effect.mapError((cause) =>
338
+ toPlanRunServiceError(
339
+ `Failed to load execution plans in context ${recordIdToString(params.organizationId, TABLES.ORGANIZATION)}`,
340
+ cause,
341
+ ),
342
+ ),
343
+ )
312
344
  return runs.filter((run) => statuses.has(run.status))
313
345
  })
314
346
 
315
347
  const listNodeRunsEffect = (runId: RecordIdInput) =>
316
- tryPlanRunPromise(`Failed to load plan node runs for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`, () =>
317
- db.findMany(TABLES.PLAN_NODE_RUN, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanNodeRunSchema, {
348
+ db
349
+ .findMany(TABLES.PLAN_NODE_RUN, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanNodeRunSchema, {
318
350
  orderBy: 'nodeId',
319
351
  orderDir: 'ASC',
320
- }),
321
- )
352
+ })
353
+ .pipe(
354
+ Effect.mapError((cause) =>
355
+ toPlanRunServiceError(
356
+ `Failed to load plan node runs for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
357
+ cause,
358
+ ),
359
+ ),
360
+ )
322
361
 
323
362
  const getNodeRunByNodeIdEffect = (runId: RecordIdInput, nodeId: string) =>
324
363
  Effect.gen(function* () {
325
- const nodeRun = yield* tryPlanRunPromise(`Failed to load plan node run "${nodeId}"`, () =>
326
- db.findOne(TABLES.PLAN_NODE_RUN, { runId: ensureRecordId(runId, TABLES.PLAN_RUN), nodeId }, PlanNodeRunSchema),
327
- )
364
+ const nodeRun = yield* db
365
+ .findOne(TABLES.PLAN_NODE_RUN, { runId: ensureRecordId(runId, TABLES.PLAN_RUN), nodeId }, PlanNodeRunSchema)
366
+ .pipe(Effect.mapError((cause) => toPlanRunServiceError(`Failed to load plan node run "${nodeId}"`, cause)))
328
367
 
329
368
  if (!nodeRun) {
330
369
  return yield* new PlanRunNotFoundError({
@@ -338,20 +377,34 @@ export function makePlanRunService(db: SurrealDBService) {
338
377
  })
339
378
 
340
379
  const listArtifactsEffect = (runId: RecordIdInput) =>
341
- tryPlanRunPromise(`Failed to load plan artifacts for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`, () =>
342
- db.findMany(TABLES.PLAN_ARTIFACT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanArtifactSchema, {
380
+ db
381
+ .findMany(TABLES.PLAN_ARTIFACT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanArtifactSchema, {
343
382
  orderBy: 'createdAt',
344
383
  orderDir: 'ASC',
345
- }),
346
- )
384
+ })
385
+ .pipe(
386
+ Effect.mapError((cause) =>
387
+ toPlanRunServiceError(
388
+ `Failed to load plan artifacts for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
389
+ cause,
390
+ ),
391
+ ),
392
+ )
347
393
 
348
394
  const listAttemptsEffect = (runId: RecordIdInput) =>
349
- tryPlanRunPromise(`Failed to load plan attempts for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`, () =>
350
- db.findMany(TABLES.PLAN_NODE_ATTEMPT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanNodeAttemptSchema, {
395
+ db
396
+ .findMany(TABLES.PLAN_NODE_ATTEMPT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanNodeAttemptSchema, {
351
397
  orderBy: 'createdAt',
352
398
  orderDir: 'ASC',
353
- }),
354
- )
399
+ })
400
+ .pipe(
401
+ Effect.mapError((cause) =>
402
+ toPlanRunServiceError(
403
+ `Failed to load plan attempts for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
404
+ cause,
405
+ ),
406
+ ),
407
+ )
355
408
 
356
409
  const listValidationIssuesEffect = (params: {
357
410
  runId?: RecordIdInput
@@ -364,33 +417,45 @@ export function makePlanRunService(db: SurrealDBService) {
364
417
  if (params.planSpecId) filter.planSpecId = ensureRecordId(params.planSpecId, TABLES.PLAN_SPEC)
365
418
  if (params.attemptId) filter.attemptId = ensureRecordId(params.attemptId, TABLES.PLAN_NODE_ATTEMPT)
366
419
 
367
- return yield* tryPlanRunPromise('Failed to load plan validation issues', () =>
368
- db.findMany(TABLES.PLAN_VALIDATION_ISSUE, filter, PlanValidationIssueSchema, {
420
+ return yield* db
421
+ .findMany(TABLES.PLAN_VALIDATION_ISSUE, filter, PlanValidationIssueSchema, {
369
422
  orderBy: 'createdAt',
370
423
  orderDir: 'ASC',
371
- }),
372
- )
424
+ })
425
+ .pipe(Effect.mapError((cause) => toPlanRunServiceError('Failed to load plan validation issues', cause)))
373
426
  })
374
427
 
375
428
  const listApprovalsEffect = (runId: RecordIdInput) =>
376
- tryPlanRunPromise(`Failed to load plan approvals for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`, () =>
377
- db.findMany(TABLES.PLAN_APPROVAL, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanApprovalSchema, {
429
+ db
430
+ .findMany(TABLES.PLAN_APPROVAL, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanApprovalSchema, {
378
431
  orderBy: 'createdAt',
379
432
  orderDir: 'ASC',
380
- }),
381
- )
433
+ })
434
+ .pipe(
435
+ Effect.mapError((cause) =>
436
+ toPlanRunServiceError(
437
+ `Failed to load plan approvals for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
438
+ cause,
439
+ ),
440
+ ),
441
+ )
382
442
 
383
443
  const getLatestCheckpointEffect = (runId: RecordIdInput) =>
384
444
  Effect.gen(function* () {
385
- const checkpoints = yield* tryPlanRunPromise(
386
- `Failed to load latest checkpoint for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
387
- () =>
388
- db.findMany(TABLES.PLAN_CHECKPOINT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanCheckpointSchema, {
389
- orderBy: 'sequence',
390
- orderDir: 'DESC',
391
- limit: 1,
392
- }),
393
- )
445
+ const checkpoints = yield* db
446
+ .findMany(TABLES.PLAN_CHECKPOINT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanCheckpointSchema, {
447
+ orderBy: 'sequence',
448
+ orderDir: 'DESC',
449
+ limit: 1,
450
+ })
451
+ .pipe(
452
+ Effect.mapError((cause) =>
453
+ toPlanRunServiceError(
454
+ `Failed to load latest checkpoint for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
455
+ cause,
456
+ ),
457
+ ),
458
+ )
394
459
  return checkpoints.at(0) ?? null
395
460
  })
396
461
 
@@ -401,15 +466,20 @@ export function makePlanRunService(db: SurrealDBService) {
401
466
 
402
467
  const listEventsEffect = (runId: RecordIdInput, limit = 20) =>
403
468
  Effect.gen(function* () {
404
- const events = yield* tryPlanRunPromise(
405
- `Failed to load plan events for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
406
- () =>
407
- db.findMany(TABLES.PLAN_EVENT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanEventSchema, {
408
- orderBy: 'createdAt',
409
- orderDir: 'DESC',
410
- limit,
411
- }),
412
- )
469
+ const events = yield* db
470
+ .findMany(TABLES.PLAN_EVENT, { runId: ensureRecordId(runId, TABLES.PLAN_RUN) }, PlanEventSchema, {
471
+ orderBy: 'createdAt',
472
+ orderDir: 'DESC',
473
+ limit,
474
+ })
475
+ .pipe(
476
+ Effect.mapError((cause) =>
477
+ toPlanRunServiceError(
478
+ `Failed to load plan events for run ${recordIdToString(runId, TABLES.PLAN_RUN)}`,
479
+ cause,
480
+ ),
481
+ ),
482
+ )
413
483
  return [...events].reverse()
414
484
  })
415
485
 
@@ -424,10 +494,16 @@ export function makePlanRunService(db: SurrealDBService) {
424
494
  return []
425
495
  }
426
496
 
427
- const previousRun = yield* tryPlanRunPromise(
428
- `Failed to load lineage run ${recordIdToString(currentRunId, TABLES.PLAN_RUN)}`,
429
- () => db.findOne(TABLES.PLAN_RUN, { id: currentRunId }, PlanRunSchema),
430
- )
497
+ const previousRun = yield* db
498
+ .findOne(TABLES.PLAN_RUN, { id: currentRunId }, PlanRunSchema)
499
+ .pipe(
500
+ Effect.mapError((cause) =>
501
+ toPlanRunServiceError(
502
+ `Failed to load lineage run ${recordIdToString(currentRunId, TABLES.PLAN_RUN)}`,
503
+ cause,
504
+ ),
505
+ ),
506
+ )
431
507
  if (!previousRun) {
432
508
  return []
433
509
  }