@lota-sdk/core 0.1.14 → 0.1.16

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 (174) hide show
  1. package/infrastructure/schema/00_identity.surql +0 -2
  2. package/infrastructure/schema/01_memory.surql +1 -1
  3. package/infrastructure/schema/02_execution_plan.surql +62 -1
  4. package/infrastructure/schema/03_learned_skill.surql +1 -1
  5. package/infrastructure/schema/06_playbook.surql +25 -0
  6. package/infrastructure/schema/07_institutional_memory.surql +13 -0
  7. package/infrastructure/schema/08_quality_metrics.surql +17 -0
  8. package/package.json +9 -8
  9. package/src/ai/definitions.ts +80 -2
  10. package/src/ai/embedding-cache.ts +7 -6
  11. package/src/ai/index.ts +0 -1
  12. package/src/bifrost/bifrost.ts +14 -14
  13. package/src/config/agent-defaults.ts +32 -22
  14. package/src/config/agent-types.ts +11 -0
  15. package/src/config/constants.ts +2 -14
  16. package/src/config/debug-logger.ts +5 -1
  17. package/src/config/index.ts +3 -0
  18. package/src/config/logger.ts +7 -9
  19. package/src/config/model-constants.ts +16 -34
  20. package/src/config/search.ts +1 -15
  21. package/src/create-runtime.ts +453 -0
  22. package/src/db/cursor-pagination.ts +3 -6
  23. package/src/db/index.ts +2 -0
  24. package/src/db/memory-store.rows.ts +7 -7
  25. package/src/db/memory-store.ts +24 -24
  26. package/src/db/memory.ts +18 -16
  27. package/src/db/schema-fingerprint.ts +1 -0
  28. package/src/db/service.ts +193 -122
  29. package/src/db/startup.ts +9 -13
  30. package/src/db/surreal-mutation.ts +43 -0
  31. package/src/db/tables.ts +7 -0
  32. package/src/db/workstream-message-row.ts +15 -0
  33. package/src/embeddings/provider.ts +1 -1
  34. package/src/index.ts +1 -1
  35. package/src/queues/context-compaction.queue.ts +17 -52
  36. package/src/queues/delayed-node-promotion.queue.ts +41 -0
  37. package/src/queues/document-processor.queue.ts +7 -7
  38. package/src/queues/index.ts +3 -0
  39. package/src/queues/memory-consolidation.queue.ts +18 -54
  40. package/src/queues/plan-scheduler.queue.ts +97 -0
  41. package/src/queues/post-chat-memory.queue.ts +15 -60
  42. package/src/queues/queue-factory.ts +100 -0
  43. package/src/queues/recent-activity-title-refinement.queue.ts +15 -54
  44. package/src/queues/regular-chat-memory-digest.queue.ts +16 -55
  45. package/src/queues/skill-extraction.queue.ts +15 -50
  46. package/src/queues/workstream-title-generation.queue.ts +15 -51
  47. package/src/redis/connection.ts +12 -3
  48. package/src/redis/index.ts +2 -1
  49. package/src/redis/org-memory-lock.ts +1 -1
  50. package/src/redis/redis-lease-lock.ts +41 -8
  51. package/src/redis/stream-context.ts +11 -0
  52. package/src/runtime/agent-runtime-policy.ts +106 -21
  53. package/src/runtime/agent-stream-helpers.ts +2 -1
  54. package/src/runtime/approval-continuation.ts +12 -6
  55. package/src/runtime/context-compaction-constants.ts +1 -1
  56. package/src/runtime/context-compaction-runtime.ts +7 -5
  57. package/src/runtime/context-compaction.ts +40 -97
  58. package/src/runtime/execution-plan.ts +23 -19
  59. package/src/runtime/graph-designer.ts +15 -0
  60. package/src/runtime/helper-model.ts +10 -196
  61. package/src/runtime/index.ts +14 -1
  62. package/src/runtime/llm-content.ts +1 -1
  63. package/src/runtime/memory-block.ts +11 -12
  64. package/src/runtime/memory-pipeline.ts +26 -10
  65. package/src/runtime/plugin-resolution.ts +35 -0
  66. package/src/runtime/plugin-types.ts +73 -1
  67. package/src/runtime/retrieval-adapters.ts +1 -1
  68. package/src/runtime/runtime-config.ts +25 -12
  69. package/src/runtime/runtime-extensions.ts +91 -15
  70. package/src/runtime/runtime-worker-registry.ts +6 -0
  71. package/src/runtime/team-consultation-orchestrator.ts +45 -28
  72. package/src/runtime/team-consultation-prompts.ts +11 -2
  73. package/src/runtime/title-helpers.ts +11 -4
  74. package/src/runtime/workstream-chat-helpers.ts +6 -7
  75. package/src/runtime/workstream-routing-policy.ts +0 -30
  76. package/src/runtime/workstream-state.ts +17 -7
  77. package/src/services/adaptive-playbook.service.ts +152 -0
  78. package/src/services/agent-executor.service.ts +293 -0
  79. package/src/services/artifact-provenance.service.ts +172 -0
  80. package/src/services/attachment.service.ts +7 -12
  81. package/src/services/context-compaction.service.ts +75 -58
  82. package/src/services/context-enrichment.service.ts +33 -0
  83. package/src/services/coordination-registry.service.ts +117 -0
  84. package/src/services/document-chunk.service.ts +38 -33
  85. package/src/services/domain-agent-executor.service.ts +71 -0
  86. package/src/services/execution-plan.service.ts +271 -50
  87. package/src/services/feedback-loop.service.ts +96 -0
  88. package/src/services/global-orchestrator.service.ts +148 -0
  89. package/src/services/index.ts +26 -0
  90. package/src/services/institutional-memory.service.ts +145 -0
  91. package/src/services/learned-skill.service.ts +30 -15
  92. package/src/services/memory-assessment.service.ts +3 -2
  93. package/src/services/{memory.utils.ts → memory-utils.ts} +4 -13
  94. package/src/services/memory.service.ts +55 -69
  95. package/src/services/monitoring-window.service.ts +86 -0
  96. package/src/services/mutating-approval.service.ts +1 -1
  97. package/src/services/node-workspace.service.ts +155 -0
  98. package/src/services/notification.service.ts +39 -0
  99. package/src/services/organization-member.service.ts +12 -5
  100. package/src/services/organization.service.ts +5 -5
  101. package/src/services/ownership-dispatcher.service.ts +403 -0
  102. package/src/services/plan-approval.service.ts +1 -1
  103. package/src/services/plan-artifact.service.ts +1 -0
  104. package/src/services/plan-builder.service.ts +1 -0
  105. package/src/services/plan-checkpoint.service.ts +30 -2
  106. package/src/services/plan-compiler.service.ts +5 -0
  107. package/src/services/plan-coordination.service.ts +152 -0
  108. package/src/services/plan-cycle.service.ts +284 -0
  109. package/src/services/plan-deadline.service.ts +287 -0
  110. package/src/services/plan-executor.service.ts +386 -58
  111. package/src/services/plan-helpers.ts +15 -0
  112. package/src/services/plan-run.service.ts +41 -7
  113. package/src/services/plan-scheduler.service.ts +240 -0
  114. package/src/services/plan-template.service.ts +117 -0
  115. package/src/services/plan-validator.service.ts +87 -20
  116. package/src/services/plan-workspace.service.ts +83 -0
  117. package/src/services/playbook-registry.service.ts +67 -0
  118. package/src/services/plugin-executor.service.ts +103 -0
  119. package/src/services/quality-metrics.service.ts +132 -0
  120. package/src/services/recent-activity-title.service.ts +3 -10
  121. package/src/services/recent-activity.service.ts +33 -43
  122. package/src/services/skill-resolver.service.ts +19 -0
  123. package/src/services/system-executor.service.ts +105 -0
  124. package/src/services/workstream-message.service.ts +29 -41
  125. package/src/services/workstream-plan-registry.service.ts +22 -0
  126. package/src/services/workstream-title.service.ts +3 -9
  127. package/src/services/{workstream-turn-preparation.ts → workstream-turn-preparation.service.ts} +428 -373
  128. package/src/services/workstream-turn.ts +2 -2
  129. package/src/services/workstream.service.ts +55 -65
  130. package/src/services/workstream.types.ts +10 -19
  131. package/src/services/write-intent-validator.service.ts +81 -0
  132. package/src/storage/attachment-parser.ts +1 -1
  133. package/src/storage/attachment-storage.service.ts +4 -4
  134. package/src/storage/{attachments.utils.ts → attachment-utils.ts} +2 -5
  135. package/src/storage/generated-document-storage.service.ts +3 -2
  136. package/src/storage/index.ts +2 -2
  137. package/src/system-agents/{context-compacter.agent.ts → context-compaction.agent.ts} +4 -4
  138. package/src/system-agents/delegated-agent-factory.ts +5 -2
  139. package/src/system-agents/index.ts +8 -0
  140. package/src/system-agents/memory-reranker.agent.ts +1 -1
  141. package/src/system-agents/memory.agent.ts +1 -1
  142. package/src/system-agents/recent-activity-title-refiner.agent.ts +1 -1
  143. package/src/tools/execution-plan.tool.ts +17 -19
  144. package/src/tools/fetch-webpage.tool.ts +20 -18
  145. package/src/tools/index.ts +2 -3
  146. package/src/tools/read-file-parts.tool.ts +1 -1
  147. package/src/tools/search-web.tool.ts +18 -15
  148. package/src/tools/{search-tools.ts → search.tool.ts} +1 -1
  149. package/src/tools/team-think.tool.ts +14 -8
  150. package/src/tools/{tool-contract.ts → tool-contracts.ts} +9 -2
  151. package/src/utils/async.ts +3 -2
  152. package/src/utils/date-time.ts +4 -32
  153. package/src/utils/env.ts +8 -0
  154. package/src/utils/errors.ts +47 -0
  155. package/src/utils/hono-error-handler.ts +1 -2
  156. package/src/utils/index.ts +19 -2
  157. package/src/utils/string.ts +128 -1
  158. package/src/workers/bootstrap.ts +2 -2
  159. package/src/workers/index.ts +1 -0
  160. package/src/workers/memory-consolidation.worker.ts +12 -12
  161. package/src/workers/regular-chat-memory-digest.helpers.ts +2 -7
  162. package/src/workers/regular-chat-memory-digest.runner.ts +11 -105
  163. package/src/workers/skill-extraction.runner.ts +8 -102
  164. package/src/workers/utils/file-section-chunker.ts +6 -3
  165. package/src/workers/utils/repomix-file-sections.ts +2 -2
  166. package/src/workers/utils/sandbox-error.ts +11 -2
  167. package/src/workers/utils/workstream-message-query.ts +97 -0
  168. package/src/workers/worker-utils.ts +6 -2
  169. package/src/runtime/retrieval-pipeline.ts +0 -3
  170. package/src/runtime.ts +0 -387
  171. package/src/tools/log-hello-world.tool.ts +0 -17
  172. package/src/utils/error.ts +0 -10
  173. /package/src/services/{context-compaction-runtime.ts → context-compaction-runtime.singleton.ts} +0 -0
  174. /package/src/storage/{attachments.types.ts → attachment-types.ts} +0 -0
@@ -1,4 +1,6 @@
1
- import { parseRowMetadata, toTimestamp, withCreatedAtMetadata } from '@lota-sdk/shared'
1
+ import { createHash } from 'node:crypto'
2
+
3
+ import { parseRowMetadata, recordIdSchema, requireTimestamp, withCreatedAtMetadata } from '@lota-sdk/shared'
2
4
  import type { ChatMessage } from '@lota-sdk/shared'
3
5
  import { RecordId, surql } from 'surrealdb'
4
6
  import { z } from 'zod'
@@ -10,40 +12,33 @@ import { recordIdToString } from '../db/record-id'
10
12
  import type { RecordIdRef } from '../db/record-id'
11
13
  import { databaseService } from '../db/service'
12
14
  import { TABLES } from '../db/tables'
15
+ import { WorkstreamMessageRowSchema } from '../db/workstream-message-row'
16
+ import type { WorkstreamMessageRow } from '../db/workstream-message-row'
13
17
 
14
- const WorkstreamMessageRowSchema = z.object({
15
- id: z.unknown(),
16
- workstreamId: z.unknown(),
17
- messageId: z.string(),
18
- role: z.enum(['system', 'user', 'assistant']),
19
- parts: z.array(z.record(z.string(), z.unknown())).optional(),
20
- metadata: z.record(z.string(), z.unknown()).nullish(),
21
- createdAt: z.union([z.date(), z.string(), z.number()]),
22
- updatedAt: z.union([z.date(), z.string(), z.number()]).optional(),
23
- })
24
-
25
- type WorkstreamMessageRow = z.infer<typeof WorkstreamMessageRowSchema>
26
-
27
- const WorkstreamMessageExistingRowSchema = z.object({
28
- id: z.unknown(),
29
- createdAt: z.union([z.date(), z.string(), z.number()]),
30
- })
18
+ const WorkstreamMessageExistingRowSchema = z.object({ id: recordIdSchema, createdAt: z.coerce.date() })
31
19
 
32
20
  function toMessageId(value: string | RecordIdRef): string {
33
21
  return recordIdToString(value, TABLES.WORKSTREAM_MESSAGE)
34
22
  }
35
23
 
24
+ /**
25
+ * Builds a collision-free row id by hashing the workstream + message id pair.
26
+ * Previous implementation replaced non-alphanumeric chars with '_', which was
27
+ * lossy (e.g. "msg:foo" and "msg_foo" mapped to the same row id).
28
+ * Now uses a 32-char SHA-256 hex prefix -- short enough for ergonomic ids,
29
+ * long enough (128 bits) to make collisions negligible.
30
+ */
36
31
  function toWorkstreamMessageRowId(workstreamId: RecordIdRef, messageId: string): RecordId {
37
- const workstreamPart = recordIdToString(workstreamId, TABLES.WORKSTREAM).replace(/[^a-zA-Z0-9_-]/g, '_')
38
- const messagePart = messageId.replace(/[^a-zA-Z0-9_-]/g, '_')
39
- return new RecordId(TABLES.WORKSTREAM_MESSAGE, `${workstreamPart}__${messagePart}`)
32
+ const workstreamStr = recordIdToString(workstreamId, TABLES.WORKSTREAM)
33
+ const digest = createHash('sha256').update(`${workstreamStr}\0${messageId}`).digest('hex').slice(0, 32)
34
+ return new RecordId(TABLES.WORKSTREAM_MESSAGE, digest)
40
35
  }
41
36
 
42
37
  function toChatMessage(row: WorkstreamMessageRow): ChatMessage {
43
- const rowCreatedAt = toTimestamp(row.createdAt)
38
+ const rowCreatedAt = requireTimestamp(row.createdAt)
44
39
  const metadata = withCreatedAtMetadata(parseRowMetadata(row.metadata), rowCreatedAt)
45
40
 
46
- return { id: row.messageId, role: row.role, parts: (row.parts ?? []) as ChatMessage['parts'], metadata }
41
+ return { id: row.messageId, role: row.role, parts: row.parts as ChatMessage['parts'], metadata }
47
42
  }
48
43
 
49
44
  const workstreamPaginationConfig: CursorPaginationConfig = {
@@ -74,16 +69,16 @@ class WorkstreamMessageService {
74
69
  async upsertMessages(params: { workstreamId: RecordIdRef; messages: ChatMessage[] }): Promise<void> {
75
70
  const workstreamId = params.workstreamId
76
71
 
77
- for (const message of params.messages) {
72
+ const upsertPromises = params.messages.map(async (message) => {
78
73
  const messageId = message.id.trim()
79
- if (!messageId) continue
74
+ if (!messageId) return
80
75
 
81
76
  const role = message.role
82
77
  const parts = Array.isArray(message.parts)
83
78
  ? message.parts.map((part) => structuredClone(part) as Record<string, unknown>)
84
79
  : []
85
80
  if (parts.length === 0) {
86
- if (role === 'assistant') continue
81
+ if (role === 'assistant') return
87
82
  throw new Error(`Refusing to persist workstream message "${messageId}" with empty parts`)
88
83
  }
89
84
  const rowId = toWorkstreamMessageRowId(workstreamId, messageId)
@@ -93,7 +88,7 @@ class WorkstreamMessageService {
93
88
  WorkstreamMessageExistingRowSchema,
94
89
  )
95
90
  const persistedCreatedAt =
96
- existingRow === null ? toTimestamp(message.metadata?.createdAt) : toTimestamp(existingRow.createdAt)
91
+ existingRow === null ? requireTimestamp(message.metadata?.createdAt) : requireTimestamp(existingRow.createdAt)
97
92
  const metadata = withCreatedAtMetadata({ ...message.metadata, createdAt: persistedCreatedAt })
98
93
 
99
94
  await databaseService.upsert(
@@ -105,12 +100,13 @@ class WorkstreamMessageService {
105
100
  role,
106
101
  parts,
107
102
  metadata,
108
- createdAt: existingRow ? new Date(toTimestamp(existingRow.createdAt)) : new Date(persistedCreatedAt),
103
+ createdAt: existingRow ? existingRow.createdAt : new Date(persistedCreatedAt),
109
104
  },
110
105
  WorkstreamMessageRowSchema,
111
106
  { mutation: 'content' },
112
107
  )
113
- }
108
+ })
109
+ await Promise.all(upsertPromises)
114
110
  }
115
111
 
116
112
  async listMessages(workstreamId: RecordIdRef): Promise<ChatMessage[]> {
@@ -138,7 +134,7 @@ class WorkstreamMessageService {
138
134
  async listMessagesAfterCursor(workstreamId: RecordIdRef, afterMessageId?: string): Promise<ChatMessage[]> {
139
135
  const cursorMessageId = afterMessageId?.trim()
140
136
  if (!cursorMessageId) {
141
- return await this.listMessages(workstreamId)
137
+ return this.listMessages(workstreamId)
142
138
  }
143
139
 
144
140
  const cursorRow = await databaseService.findOne(
@@ -151,7 +147,7 @@ class WorkstreamMessageService {
151
147
  throw new Error(`Workstream cursor message not found: ${cursorMessageId}`)
152
148
  }
153
149
 
154
- const cursorCreatedAt = new Date(toTimestamp(cursorRow.createdAt))
150
+ const cursorCreatedAt = cursorRow.createdAt
155
151
  const cursorId = toWorkstreamMessageRowId(workstreamId, cursorMessageId)
156
152
  const rows = await databaseService.query<unknown>(surql`
157
153
  SELECT * FROM workstreamMessage
@@ -195,7 +191,7 @@ class WorkstreamMessageService {
195
191
  .map((message) => ({
196
192
  id: message.id,
197
193
  role: message.role as 'user' | 'assistant',
198
- createdAt: new Date(toTimestamp(message.metadata?.createdAt)).toISOString(),
194
+ createdAt: new Date(requireTimestamp(message.metadata?.createdAt)).toISOString(),
199
195
  content: message.parts
200
196
  .flatMap((part) => (part.type === 'text' && typeof part.text === 'string' ? [part.text] : []))
201
197
  .join('\n')
@@ -231,7 +227,7 @@ class WorkstreamMessageService {
231
227
  id: toMessageId(params.messageId),
232
228
  role: 'assistant',
233
229
  parts: params.parts,
234
- metadata: withCreatedAtMetadata(params.metadata),
230
+ metadata: withCreatedAtMetadata(params.metadata, Date.now()),
235
231
  }
236
232
 
237
233
  await this.upsertMessages({ workstreamId: params.workstreamId, messages: [message] })
@@ -269,14 +265,6 @@ class WorkstreamMessageService {
269
265
  ],
270
266
  })
271
267
  }
272
-
273
- async listAllMessages(workstreamId: RecordIdRef): Promise<ChatMessage[]> {
274
- return await this.listMessages(workstreamId)
275
- }
276
-
277
- async addAttachments(): Promise<void> {
278
- // Attachments are no longer persisted via workstreamMessage service in AI SDK mode.
279
- }
280
268
  }
281
269
 
282
270
  export const workstreamMessageService = new WorkstreamMessageService()
@@ -0,0 +1,22 @@
1
+ import type { PlanRunRecord, SerializableExecutionPlan } from '@lota-sdk/shared'
2
+
3
+ import type { RecordIdInput } from '../db/record-id'
4
+ import { planRunService } from './plan-run.service'
5
+
6
+ class WorkstreamPlanRegistryService {
7
+ async listActiveRuns(workstreamId: RecordIdInput): Promise<PlanRunRecord[]> {
8
+ return planRunService.getActiveRunRecords(workstreamId)
9
+ }
10
+
11
+ async countActiveRuns(workstreamId: RecordIdInput): Promise<number> {
12
+ const runs = await this.listActiveRuns(workstreamId)
13
+ return runs.length
14
+ }
15
+
16
+ async listActivePlans(workstreamId: RecordIdInput): Promise<SerializableExecutionPlan[]> {
17
+ const runs = await this.listActiveRuns(workstreamId)
18
+ return Promise.all(runs.map((run) => planRunService.toSerializablePlan(run)))
19
+ }
20
+ }
21
+
22
+ export const workstreamPlanRegistryService = new WorkstreamPlanRegistryService()
@@ -3,20 +3,14 @@ import { WORKSTREAM } from '@lota-sdk/shared'
3
3
  import { chatLogger } from '../config/logger'
4
4
  import type { RecordIdRef } from '../db/record-id'
5
5
  import { createHelperModelRuntime } from '../runtime/helper-model'
6
- import { deriveTitle, limitTitleWords } from '../runtime/title-helpers'
6
+ import { deriveTitle, limitTitleWords, normalizeTitle } from '../runtime/title-helpers'
7
7
  import {
8
8
  createWorkstreamTitleGeneratorAgent,
9
9
  WORKSTREAM_TITLE_GENERATOR_PROMPT,
10
10
  } from '../system-agents/title-generator.agent'
11
- import { compactWhitespace } from '../utils/string'
12
11
  import { workstreamService } from './workstream.service'
13
12
 
14
- function normalizeTitle(value: string): string {
15
- const normalized = compactWhitespace(value)
16
- .replace(/^["'`]+|["'`]+$/g, '')
17
- .replace(/[.!?,;:]+$/g, '')
18
- return normalized.length <= 80 ? normalized : normalized.slice(0, 80).trim()
19
- }
13
+ const WORKSTREAM_TITLE_TIMEOUT_MS = 5_000
20
14
 
21
15
  class WorkstreamTitleService {
22
16
  helperRuntime = createHelperModelRuntime()
@@ -29,7 +23,7 @@ class WorkstreamTitleService {
29
23
  tag: 'workstream-title',
30
24
  createAgent: createWorkstreamTitleGeneratorAgent,
31
25
  defaultSystemPrompt: WORKSTREAM_TITLE_GENERATOR_PROMPT,
32
- timeoutMs: 30_000,
26
+ timeoutMs: WORKSTREAM_TITLE_TIMEOUT_MS,
33
27
  messages: [{ role: 'user', content: sourceText }],
34
28
  }),
35
29
  )