@lota-sdk/core 0.1.45 → 0.1.47
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.
- package/infrastructure/schema/00_workstream.surql +0 -1
- package/package.json +2 -2
- package/src/runtime/agent-runtime-policy.ts +0 -2
- package/src/runtime/context-compaction-runtime.ts +2 -3
- package/src/runtime/context-compaction.ts +48 -590
- package/src/runtime/execution-plan.ts +7 -1
- package/src/runtime/index.ts +0 -12
- package/src/runtime/post-turn-side-effects.ts +0 -3
- package/src/runtime/runtime-extensions.ts +0 -1
- package/src/runtime/workstream-chat-helpers.ts +1 -8
- package/src/services/context-compaction.service.ts +5 -21
- package/src/services/workstream-turn-preparation.service.ts +46 -53
- package/src/system-agents/context-compaction.agent.ts +4 -10
- package/src/runtime/workstream-state.ts +0 -274
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { SerializableExecutionPlan } from '@lota-sdk/shared'
|
|
2
2
|
|
|
3
|
+
type ExecutionPlanPromptSummary = Pick<SerializableExecutionPlan, 'runId' | 'title'>
|
|
4
|
+
|
|
3
5
|
const EXECUTION_PLAN_AGENT_PROTOCOL_PROMPT = `<execution-plan-protocol>
|
|
4
6
|
- Create execution plans for multi-step work. Review existing plans before creating new ones.
|
|
5
7
|
- The runtime executor owns lifecycle truth. Do not claim node completion until the executor confirms.
|
|
@@ -9,10 +11,14 @@ const EXECUTION_PLAN_AGENT_PROTOCOL_PROMPT = `<execution-plan-protocol>
|
|
|
9
11
|
- If contracts or criteria materially change, replace the plan.
|
|
10
12
|
</execution-plan-protocol>`
|
|
11
13
|
|
|
14
|
+
function toExecutionPlanPromptSummaries(plans: SerializableExecutionPlan[]): ExecutionPlanPromptSummary[] {
|
|
15
|
+
return plans.map(({ runId, title }) => ({ runId, title }))
|
|
16
|
+
}
|
|
17
|
+
|
|
12
18
|
function formatExecutionPlansForPrompt(plans: SerializableExecutionPlan[]): string | undefined {
|
|
13
19
|
if (plans.length === 0) return undefined
|
|
14
20
|
|
|
15
|
-
const payload = { activePlans: plans, planCount: plans.length }
|
|
21
|
+
const payload = { activePlans: toExecutionPlanPromptSummaries(plans), planCount: plans.length }
|
|
16
22
|
|
|
17
23
|
return ['<execution-plan-state>', JSON.stringify(payload, null, 2), '</execution-plan-state>'].join('\n')
|
|
18
24
|
}
|
package/src/runtime/index.ts
CHANGED
|
@@ -25,15 +25,3 @@ export * from './team-consultation-orchestrator'
|
|
|
25
25
|
export * from './team-consultation-prompts'
|
|
26
26
|
export * from './turn-lifecycle'
|
|
27
27
|
export * from './workstream-chat-helpers'
|
|
28
|
-
export {
|
|
29
|
-
WorkstreamStateSchema,
|
|
30
|
-
type WorkstreamState,
|
|
31
|
-
type WorkstreamStateDelta,
|
|
32
|
-
StructuredWorkstreamStateDeltaSchema,
|
|
33
|
-
type StructuredWorkstreamStateDelta,
|
|
34
|
-
createEmptyStructuredWorkstreamStateDelta,
|
|
35
|
-
parseStructuredWorkstreamStateDelta,
|
|
36
|
-
StructuredCompactionOutputSchema,
|
|
37
|
-
type CompactionOutput,
|
|
38
|
-
createEmptyWorkstreamState,
|
|
39
|
-
} from './workstream-state'
|
|
@@ -28,7 +28,6 @@ import { workstreamService } from '../services/workstream.service'
|
|
|
28
28
|
import type { NormalizedWorkstream, WorkstreamRecord } from '../services/workstream.types'
|
|
29
29
|
import { safeEnqueue } from '../utils/async'
|
|
30
30
|
import { toIsoDateTimeString } from '../utils/date-time'
|
|
31
|
-
import type { WorkstreamState } from './workstream-state'
|
|
32
31
|
|
|
33
32
|
function buildRecentActivityChatDeepLink(params: {
|
|
34
33
|
workstream: NormalizedWorkstream
|
|
@@ -77,7 +76,6 @@ interface PostTurnSideEffectsParams {
|
|
|
77
76
|
visibleWorkstreamAgentId: string | null | undefined
|
|
78
77
|
defaultLeadAgentId: string
|
|
79
78
|
latestWorkstreamRecord: WorkstreamRecord
|
|
80
|
-
latestPersistedState: WorkstreamState | null
|
|
81
79
|
isUserTurn: boolean
|
|
82
80
|
}
|
|
83
81
|
|
|
@@ -87,7 +85,6 @@ export async function runPostTurnSideEffects(params: PostTurnSideEffectsParams):
|
|
|
87
85
|
const agentMessages = buildAgentHistoryMessages(params.allAssistantMessages)
|
|
88
86
|
const historyMessagesForMemory = appendPersistedWorkstreamContextToHistoryMessages(toHistoryMessages(recentHistory), {
|
|
89
87
|
compactionSummary: params.latestWorkstreamRecord.compactionSummary,
|
|
90
|
-
persistedState: params.latestPersistedState,
|
|
91
88
|
})
|
|
92
89
|
|
|
93
90
|
const userMessageText = params.referenceUserMessage ? extractMessageText(params.referenceUserMessage).trim() : ''
|
|
@@ -88,7 +88,7 @@ export function buildAgentHistoryMessages(messages: ChatMessageLike[]): Array<{
|
|
|
88
88
|
|
|
89
89
|
export function appendPersistedWorkstreamContextToHistoryMessages(
|
|
90
90
|
historyMessages: WorkstreamHistoryMessage[],
|
|
91
|
-
params: { compactionSummary?: string | null
|
|
91
|
+
params: { compactionSummary?: string | null },
|
|
92
92
|
): WorkstreamHistoryMessage[] {
|
|
93
93
|
const nextHistoryMessages = [...historyMessages]
|
|
94
94
|
const compactionSummary = typeof params.compactionSummary === 'string' ? params.compactionSummary.trim() : ''
|
|
@@ -96,13 +96,6 @@ export function appendPersistedWorkstreamContextToHistoryMessages(
|
|
|
96
96
|
nextHistoryMessages.push({ role: 'agent', content: `Compacted chat summary:\n${compactionSummary}` })
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
if (params.persistedState !== null && params.persistedState !== undefined) {
|
|
100
|
-
nextHistoryMessages.push({
|
|
101
|
-
role: 'agent',
|
|
102
|
-
content: `Structured workstream state:\n${JSON.stringify(params.persistedState)}`,
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
|
|
106
99
|
return nextHistoryMessages
|
|
107
100
|
}
|
|
108
101
|
|
|
@@ -7,10 +7,8 @@ import { databaseService } from '../db/service'
|
|
|
7
7
|
import { TABLES } from '../db/tables'
|
|
8
8
|
import { getRedisConnection } from '../redis/connection-accessor'
|
|
9
9
|
import { withRedisLeaseLock } from '../redis/redis-lease-lock'
|
|
10
|
-
import { parseWorkstreamState, toStateFieldsUpdated } from '../runtime/context-compaction'
|
|
11
10
|
import { CONTEXT_WINDOW_TOKENS, WORKSTREAM_RAW_TAIL_MESSAGES } from '../runtime/context-compaction-constants'
|
|
12
|
-
import
|
|
13
|
-
import { compactMemoryBlockSummary, contextCompactionRuntime } from './context-compaction-runtime.singleton'
|
|
11
|
+
import { contextCompactionRuntime, compactMemoryBlockSummary } from './context-compaction-runtime.singleton'
|
|
14
12
|
import { workstreamMessageService } from './workstream-message.service'
|
|
15
13
|
import { WorkstreamSchema } from './workstream.types'
|
|
16
14
|
|
|
@@ -24,8 +22,6 @@ interface PersistedCompactionMetrics {
|
|
|
24
22
|
compactedMessageCount: number
|
|
25
23
|
remainingMessageCount: number
|
|
26
24
|
estimatedTokens: number
|
|
27
|
-
stateFieldsUpdated: string[]
|
|
28
|
-
conflictsDetected: number
|
|
29
25
|
}
|
|
30
26
|
|
|
31
27
|
class ContextCompactionService {
|
|
@@ -33,10 +29,6 @@ class ContextCompactionService {
|
|
|
33
29
|
return contextCompactionRuntime.createSummaryMessage(summaryText)
|
|
34
30
|
}
|
|
35
31
|
|
|
36
|
-
formatWorkstreamStateForPrompt(state: WorkstreamState | null | undefined) {
|
|
37
|
-
return contextCompactionRuntime.formatWorkstreamStateForPrompt(state)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
32
|
estimateThreshold(contextSize = CONTEXT_WINDOW_TOKENS): number {
|
|
41
33
|
return contextCompactionRuntime.estimateThreshold(contextSize)
|
|
42
34
|
}
|
|
@@ -48,7 +40,7 @@ class ContextCompactionService {
|
|
|
48
40
|
async compactWorkstreamHistory(params: {
|
|
49
41
|
workstreamId: RecordIdRef
|
|
50
42
|
contextSize?: number
|
|
51
|
-
}): Promise<{ compacted: boolean
|
|
43
|
+
}): Promise<{ compacted: boolean }> {
|
|
52
44
|
const entityId = recordIdToString(params.workstreamId, TABLES.WORKSTREAM)
|
|
53
45
|
|
|
54
46
|
return withRedisLeaseLock(
|
|
@@ -69,7 +61,6 @@ class ContextCompactionService {
|
|
|
69
61
|
throw new Error(`Workstream not found for compaction: ${entityId}`)
|
|
70
62
|
}
|
|
71
63
|
|
|
72
|
-
const currentState = parseWorkstreamState(workstream.state)
|
|
73
64
|
const liveMessages = await workstreamMessageService.listMessagesAfterCursor(
|
|
74
65
|
params.workstreamId,
|
|
75
66
|
typeof workstream.lastCompactedMessageId === 'string' ? workstream.lastCompactedMessageId : undefined,
|
|
@@ -80,11 +71,10 @@ class ContextCompactionService {
|
|
|
80
71
|
liveMessages,
|
|
81
72
|
tailMessageCount: WORKSTREAM_RAW_TAIL_MESSAGES,
|
|
82
73
|
contextSize: params.contextSize,
|
|
83
|
-
existingState: currentState,
|
|
84
74
|
})
|
|
85
75
|
|
|
86
76
|
if (!result.compacted || !result.lastCompactedMessageId) {
|
|
87
|
-
return { compacted: false
|
|
77
|
+
return { compacted: false }
|
|
88
78
|
}
|
|
89
79
|
|
|
90
80
|
if (result.compactedMessages.length > 0) {
|
|
@@ -97,11 +87,7 @@ class ContextCompactionService {
|
|
|
97
87
|
await databaseService.update(
|
|
98
88
|
TABLES.WORKSTREAM,
|
|
99
89
|
params.workstreamId,
|
|
100
|
-
{
|
|
101
|
-
compactionSummary: result.summaryText,
|
|
102
|
-
lastCompactedMessageId: result.lastCompactedMessageId,
|
|
103
|
-
state: result.state,
|
|
104
|
-
},
|
|
90
|
+
{ compactionSummary: result.summaryText, lastCompactedMessageId: result.lastCompactedMessageId },
|
|
105
91
|
WorkstreamSchema,
|
|
106
92
|
)
|
|
107
93
|
|
|
@@ -115,11 +101,9 @@ class ContextCompactionService {
|
|
|
115
101
|
compactedMessageCount: result.compactedMessageCount,
|
|
116
102
|
remainingMessageCount: result.remainingMessageCount,
|
|
117
103
|
estimatedTokens: result.estimatedTokens,
|
|
118
|
-
stateFieldsUpdated: toStateFieldsUpdated(result.stateDelta),
|
|
119
|
-
conflictsDetected: result.stateDelta.conflicts?.length ?? 0,
|
|
120
104
|
})
|
|
121
105
|
|
|
122
|
-
return { compacted: true
|
|
106
|
+
return { compacted: true }
|
|
123
107
|
},
|
|
124
108
|
)
|
|
125
109
|
}
|
|
@@ -34,7 +34,6 @@ import { hasApprovalRespondedParts } from '../runtime/approval-continuation'
|
|
|
34
34
|
import { buildModelInputMessagesWithUploadMetadata, buildReadableUploadMetadataText } from '../runtime/chat-attachments'
|
|
35
35
|
import { hasMessageContent } from '../runtime/chat-message'
|
|
36
36
|
import { waitForCompactionIfNeeded } from '../runtime/chat-run-orchestration'
|
|
37
|
-
import { parseWorkstreamState } from '../runtime/context-compaction'
|
|
38
37
|
import { CONTEXT_WINDOW_TOKENS } from '../runtime/context-compaction-constants'
|
|
39
38
|
import { createExecutionPlanInstructionSectionCache } from '../runtime/execution-plan'
|
|
40
39
|
import { mergeInstructionSections } from '../runtime/instruction-sections'
|
|
@@ -55,7 +54,6 @@ import {
|
|
|
55
54
|
buildPlanTurnSubmitToolDescription,
|
|
56
55
|
} from '../runtime/workstream-plan-turn'
|
|
57
56
|
import type { WorkstreamPlanTurnContext } from '../runtime/workstream-plan-turn'
|
|
58
|
-
import type { WorkstreamState } from '../runtime/workstream-state'
|
|
59
57
|
import { assembleWorkstreamTurnContext } from '../runtime/workstream-turn-context'
|
|
60
58
|
import { chatRunRegistry } from '../services/chat-run-registry.service'
|
|
61
59
|
import type { NormalizedWorkstream, WorkstreamRecord } from '../services/workstream.types'
|
|
@@ -88,15 +86,6 @@ function hasUIMessageStream(value: unknown): value is UIMessageStreamResult {
|
|
|
88
86
|
|
|
89
87
|
const PRESEEDED_MEMORY_LOOKUP_LIMIT = 3
|
|
90
88
|
|
|
91
|
-
function stripExecutionPlanFieldsFromWorkstreamState(
|
|
92
|
-
state: WorkstreamState | null | undefined,
|
|
93
|
-
hasExecutionPlan: boolean,
|
|
94
|
-
): WorkstreamState | null | undefined {
|
|
95
|
-
if (!state || !hasExecutionPlan) return state
|
|
96
|
-
|
|
97
|
-
return { ...state, currentPlan: null, tasks: [], artifacts: [] }
|
|
98
|
-
}
|
|
99
|
-
|
|
100
89
|
async function waitForWorkstreamCompactionIfNeeded(workstreamId: RecordIdRef): Promise<WorkstreamRecord> {
|
|
101
90
|
return waitForCompactionIfNeeded({
|
|
102
91
|
entityId: recordIdToString(workstreamId, TABLES.WORKSTREAM),
|
|
@@ -121,6 +110,26 @@ function optionalInstructionSection(value: unknown): string[] | undefined {
|
|
|
121
110
|
return section ? [section] : undefined
|
|
122
111
|
}
|
|
123
112
|
|
|
113
|
+
function writeMultiAgentEvent(
|
|
114
|
+
writer: UIMessageStreamWriter<ChatMessage> | undefined,
|
|
115
|
+
event: {
|
|
116
|
+
phase: 'routing' | 'waiting-for-agent' | 'agent-message-persisted' | 'complete'
|
|
117
|
+
agentId?: string
|
|
118
|
+
agentName?: string
|
|
119
|
+
messageId?: string
|
|
120
|
+
note?: string
|
|
121
|
+
},
|
|
122
|
+
): void {
|
|
123
|
+
if (!writer) return
|
|
124
|
+
|
|
125
|
+
writer.write({
|
|
126
|
+
type: 'data-multi-agent-event',
|
|
127
|
+
id: `multi-agent-${Bun.randomUUIDv7()}`,
|
|
128
|
+
data: event,
|
|
129
|
+
transient: true,
|
|
130
|
+
} as unknown as ChatStreamChunk)
|
|
131
|
+
}
|
|
132
|
+
|
|
124
133
|
function applyPlanTurnToolPolicy(tools: ToolSet, nodeSpec: PlanNodeSpecRecord): ToolSet {
|
|
125
134
|
const blockedToolNames = new Set([...OWNERSHIP_DISPATCH_BLOCKED_TOOL_NAMES, ...nodeSpec.toolPolicy.deny])
|
|
126
135
|
const allowList = nodeSpec.toolPolicy.allow.length > 0 ? new Set(nodeSpec.toolPolicy.allow) : null
|
|
@@ -219,7 +228,6 @@ interface StreamAgentResponseContext {
|
|
|
219
228
|
buildContextResult: Record<string, unknown> | null
|
|
220
229
|
getExecutionPlanInstructionSections: () => Promise<string[] | undefined>
|
|
221
230
|
getPreSeededMemoriesSection: (agentId: string) => Promise<string | undefined>
|
|
222
|
-
getWorkstreamStateSection: () => Promise<string | undefined>
|
|
223
231
|
getLearnedSkillsSection: (agentId: string, queryText?: string) => Promise<string | undefined>
|
|
224
232
|
promptContext: { systemWorkspaceDetails?: string }
|
|
225
233
|
retrievedKnowledgeSection: string | undefined
|
|
@@ -245,7 +253,6 @@ interface StreamAgentResponseParams {
|
|
|
245
253
|
stopWhen?: StopCondition<ToolSet> | Array<StopCondition<ToolSet>>
|
|
246
254
|
prepareStep?: PrepareStepFunction<ToolSet>
|
|
247
255
|
abortSignal?: AbortSignal
|
|
248
|
-
suppressFinish?: boolean
|
|
249
256
|
}
|
|
250
257
|
|
|
251
258
|
async function streamAgentResponse(
|
|
@@ -278,12 +285,11 @@ async function streamAgentResponse(
|
|
|
278
285
|
const resolvedAgentId = readOptionalString(agentResolution?.agentId) ?? streamParams.agentId
|
|
279
286
|
const latestUserMessage = [...streamParams.messages].reverse().find((message) => message.role === 'user')
|
|
280
287
|
const latestUserMessageText = latestUserMessage ? extractMessageText(latestUserMessage).trim() : undefined
|
|
281
|
-
const [preSeededMemoriesSection,
|
|
288
|
+
const [preSeededMemoriesSection, learnedSkillsSection] = await Promise.all([
|
|
282
289
|
ctx.getPreSeededMemoriesSection(resolvedAgentId),
|
|
283
|
-
ctx.getWorkstreamStateSection(),
|
|
284
290
|
ctx.getLearnedSkillsSection(resolvedAgentId, latestUserMessageText),
|
|
285
291
|
])
|
|
286
|
-
agentTimer.step('parallel-fetch(memories+
|
|
292
|
+
agentTimer.step('parallel-fetch(memories+skills)')
|
|
287
293
|
const toolNames = new Set(Object.keys(streamParams.tools))
|
|
288
294
|
const hasRetrievalTools = [
|
|
289
295
|
'memorySearch',
|
|
@@ -308,7 +314,6 @@ async function streamAgentResponse(
|
|
|
308
314
|
preSeededMemoriesSection,
|
|
309
315
|
retrievedKnowledgeSection: ctx.retrievedKnowledgeSection,
|
|
310
316
|
workstreamMemoryBlock: ctx.memoryBlock,
|
|
311
|
-
workstreamStateSection,
|
|
312
317
|
learnedSkillsSection,
|
|
313
318
|
userMessageText: latestUserMessageText,
|
|
314
319
|
ruleOptions: { includeMemr3Rule: hasRetrievalTools, includeDomainReasoningFallbackRule: hasDomainRoutingSkills },
|
|
@@ -395,15 +400,6 @@ async function streamAgentResponse(
|
|
|
395
400
|
firstChunkLogged = true
|
|
396
401
|
}
|
|
397
402
|
if (streamParams.writer) {
|
|
398
|
-
if (
|
|
399
|
-
streamParams.suppressFinish &&
|
|
400
|
-
typeof value === 'object' &&
|
|
401
|
-
value !== null &&
|
|
402
|
-
'type' in value &&
|
|
403
|
-
(value as { type: string }).type === 'finish'
|
|
404
|
-
) {
|
|
405
|
-
continue
|
|
406
|
-
}
|
|
407
403
|
streamParams.writer.write(value)
|
|
408
404
|
}
|
|
409
405
|
}
|
|
@@ -497,7 +493,6 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
497
493
|
timer.step('persist-approval-message')
|
|
498
494
|
}
|
|
499
495
|
|
|
500
|
-
const initialWorkstreamState = parseWorkstreamState(workstreamRecord.state)
|
|
501
496
|
const persistedCompactionCursor = toOptionalTrimmedString(workstreamRecord.lastCompactedMessageId) ?? undefined
|
|
502
497
|
const persistedLiveHistoryPromise = workstreamMessageService.listMessagesAfterCursor(
|
|
503
498
|
workstreamRef,
|
|
@@ -630,7 +625,6 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
630
625
|
} = assembledContext
|
|
631
626
|
|
|
632
627
|
let memoryBlock = workstreamService.formatMemoryBlockForPrompt(workstreamRecord)
|
|
633
|
-
let workstreamState = initialWorkstreamState
|
|
634
628
|
const executionPlanInstructionSectionCache = createExecutionPlanInstructionSectionCache({
|
|
635
629
|
disabled: false,
|
|
636
630
|
loadPlans: async () => {
|
|
@@ -638,18 +632,11 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
638
632
|
return Promise.all(runs.map((run) => planRunService.toSerializablePlan(run, { slim: true })))
|
|
639
633
|
},
|
|
640
634
|
})
|
|
641
|
-
const getExecutionPlans = async () => await executionPlanInstructionSectionCache.getPlans()
|
|
642
635
|
const getExecutionPlanInstructionSections = async (): Promise<string[] | undefined> =>
|
|
643
636
|
await executionPlanInstructionSectionCache.getSections()
|
|
644
637
|
const invalidateExecutionPlanInstructionSections = () => {
|
|
645
638
|
executionPlanInstructionSectionCache.invalidate()
|
|
646
639
|
}
|
|
647
|
-
const getWorkstreamStateSection = async (): Promise<string | undefined> => {
|
|
648
|
-
const executionPlans = await getExecutionPlans()
|
|
649
|
-
return contextCompactionRuntime.formatWorkstreamStateForPrompt(
|
|
650
|
-
stripExecutionPlanFieldsFromWorkstreamState(workstreamState, executionPlans.length > 0),
|
|
651
|
-
)
|
|
652
|
-
}
|
|
653
640
|
if (userMessage) {
|
|
654
641
|
const appliedHumanInput = await executionPlanService.applyHumanInputFromUserMessage({
|
|
655
642
|
workstreamId: workstreamRef,
|
|
@@ -809,7 +796,6 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
809
796
|
buildContextResult,
|
|
810
797
|
getExecutionPlanInstructionSections,
|
|
811
798
|
getPreSeededMemoriesSection,
|
|
812
|
-
getWorkstreamStateSection,
|
|
813
799
|
getLearnedSkillsSection,
|
|
814
800
|
promptContext,
|
|
815
801
|
retrievedKnowledgeSection,
|
|
@@ -828,7 +814,7 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
828
814
|
filterTools?: (tools: ToolSet) => ToolSet
|
|
829
815
|
includeExecutionPlanTools?: boolean
|
|
830
816
|
metadataPatch?: NonNullable<MessageMetadata>
|
|
831
|
-
|
|
817
|
+
headless?: boolean
|
|
832
818
|
}): Promise<ChatMessage> => {
|
|
833
819
|
const visibleTimer = lotaDebugLogger.timer(`visible:${runParams.agentId}`)
|
|
834
820
|
let runMemoryBlock = memoryBlock
|
|
@@ -863,8 +849,7 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
863
849
|
skills: runParams.skills,
|
|
864
850
|
additionalInstructionSections: runParams.additionalInstructionSections,
|
|
865
851
|
includeExecutionPlanTools,
|
|
866
|
-
writer,
|
|
867
|
-
suppressFinish: runParams.suppressFinish,
|
|
852
|
+
writer: runParams.headless ? undefined : writer,
|
|
868
853
|
})
|
|
869
854
|
|
|
870
855
|
visibleTimer.step('stream-agent-response')
|
|
@@ -914,6 +899,7 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
914
899
|
const wsMembers = (workstream as { members?: string[] }).members ?? []
|
|
915
900
|
const members = wsMembers.length > 0 ? wsMembers : [...agentRoster]
|
|
916
901
|
const fallbackAgentId = coreWorkstreamProfile?.config.agentId ?? defaultLeadAgentId
|
|
902
|
+
writeMultiAgentEvent(writer, { phase: 'routing', note: 'Routing this turn to the right agent.' })
|
|
917
903
|
|
|
918
904
|
const recentContext = currentMessages
|
|
919
905
|
.slice(-6)
|
|
@@ -929,7 +915,7 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
929
915
|
|
|
930
916
|
const runGroupAgent = async (
|
|
931
917
|
agentId: string,
|
|
932
|
-
options?: { routingContext?: string;
|
|
918
|
+
options?: { routingContext?: string; headless?: boolean },
|
|
933
919
|
) => {
|
|
934
920
|
const additionalSections = [...(coreInstructionSections ?? []), ...hookInstructionSections]
|
|
935
921
|
if (options?.routingContext) {
|
|
@@ -945,23 +931,23 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
945
931
|
mode: 'workstreamMode',
|
|
946
932
|
skills: coreWorkstreamProfile?.skills ? [...coreWorkstreamProfile.skills] : undefined,
|
|
947
933
|
additionalInstructionSections: additionalSections,
|
|
948
|
-
|
|
934
|
+
headless: options?.headless,
|
|
949
935
|
})
|
|
950
936
|
}
|
|
951
937
|
|
|
952
938
|
if (!triageResult) {
|
|
953
|
-
// No specialist match — fallback to owner (core) or chief (non-core), single
|
|
939
|
+
// No specialist match — fallback to owner (core) or chief (non-core), single visible turn.
|
|
954
940
|
await runGroupAgent(fallbackAgentId)
|
|
941
|
+
writeMultiAgentEvent(writer, { phase: 'complete' })
|
|
955
942
|
} else {
|
|
956
|
-
// Run first routed agent — suppress finish in case more agents follow
|
|
957
943
|
const respondedAgents: string[] = []
|
|
958
944
|
let lastResponse = await runGroupAgent(triageResult.agentId, {
|
|
959
945
|
routingContext: triageResult.routingContext,
|
|
960
|
-
suppressFinish: true,
|
|
961
946
|
})
|
|
962
947
|
respondedAgents.push(triageResult.agentId)
|
|
963
948
|
|
|
964
|
-
//
|
|
949
|
+
// Follow-up specialists run headless, persist their own messages,
|
|
950
|
+
// and are surfaced by transient events plus cache refresh.
|
|
965
951
|
while (respondedAgents.length < 3) {
|
|
966
952
|
const lastResponseText = extractMessageText(lastResponse).slice(0, 500)
|
|
967
953
|
const checkResult = await checkForNextAgent({
|
|
@@ -974,6 +960,13 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
974
960
|
|
|
975
961
|
if (checkResult.done || !checkResult.agentId) break
|
|
976
962
|
|
|
963
|
+
writeMultiAgentEvent(writer, {
|
|
964
|
+
phase: 'waiting-for-agent',
|
|
965
|
+
agentId: checkResult.agentId,
|
|
966
|
+
agentName: agentDisplayNames[checkResult.agentId] ?? checkResult.agentId,
|
|
967
|
+
note: checkResult.routingContext,
|
|
968
|
+
})
|
|
969
|
+
|
|
977
970
|
// Insert hidden bridge message between agent turns
|
|
978
971
|
const bridgeMessage: ChatMessage = {
|
|
979
972
|
id: Bun.randomUUIDv7(),
|
|
@@ -994,22 +987,24 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
994
987
|
|
|
995
988
|
lastResponse = await runGroupAgent(checkResult.agentId, {
|
|
996
989
|
routingContext: checkResult.routingContext,
|
|
997
|
-
|
|
990
|
+
headless: true,
|
|
998
991
|
})
|
|
999
992
|
respondedAgents.push(checkResult.agentId)
|
|
993
|
+
writeMultiAgentEvent(writer, {
|
|
994
|
+
phase: 'agent-message-persisted',
|
|
995
|
+
agentId: checkResult.agentId,
|
|
996
|
+
agentName: agentDisplayNames[checkResult.agentId] ?? checkResult.agentId,
|
|
997
|
+
messageId: lastResponse.id,
|
|
998
|
+
})
|
|
1000
999
|
}
|
|
1001
1000
|
|
|
1002
|
-
|
|
1003
|
-
if (writer) {
|
|
1004
|
-
writer.write({ type: 'finish', finishReason: 'stop' } as ChatStreamChunk)
|
|
1005
|
-
}
|
|
1001
|
+
writeMultiAgentEvent(writer, { phase: 'complete' })
|
|
1006
1002
|
}
|
|
1007
1003
|
}
|
|
1008
1004
|
}
|
|
1009
1005
|
} finally {
|
|
1010
1006
|
try {
|
|
1011
1007
|
const latestWorkstreamRecord = await workstreamService.getById(workstreamRef)
|
|
1012
|
-
const latestPersistedState = parseWorkstreamState(latestWorkstreamRecord.state)
|
|
1013
1008
|
|
|
1014
1009
|
await finalizeTurnRun({
|
|
1015
1010
|
serverRunId,
|
|
@@ -1063,7 +1058,6 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
1063
1058
|
visibleWorkstreamAgentId,
|
|
1064
1059
|
defaultLeadAgentId,
|
|
1065
1060
|
latestWorkstreamRecord,
|
|
1066
|
-
latestPersistedState,
|
|
1067
1061
|
isUserTurn: params.kind === 'userTurn',
|
|
1068
1062
|
})
|
|
1069
1063
|
}
|
|
@@ -1079,7 +1073,6 @@ export async function prepareWorkstreamRunCore(params: WorkstreamRunCoreParams):
|
|
|
1079
1073
|
referenceUserMessage,
|
|
1080
1074
|
assistantMessages: allAssistantMessages,
|
|
1081
1075
|
latestWorkstreamRecord,
|
|
1082
|
-
latestPersistedState,
|
|
1083
1076
|
context: buildContextResult,
|
|
1084
1077
|
})
|
|
1085
1078
|
}
|
|
@@ -10,30 +10,24 @@ import type { CreateHelperToolLoopAgentOptions } from '../runtime/agent-types'
|
|
|
10
10
|
import { resolveHelperAgentOptions } from './helper-agent-options'
|
|
11
11
|
|
|
12
12
|
const CONTEXT_COMPACTION_PROMPT = `<agent-instructions>
|
|
13
|
-
You are a **Context Compacter** that produces
|
|
14
|
-
1) a dense but shorter summary of prior context
|
|
15
|
-
2) a structured state delta for durable execution continuity
|
|
13
|
+
You are a **Context Compacter** that produces one dense replacement summary of prior context.
|
|
16
14
|
|
|
17
15
|
<task>
|
|
18
16
|
Compress historical context while preserving execution-critical details.
|
|
19
|
-
When provided with existing state, update only changed fields.
|
|
20
17
|
</task>
|
|
21
18
|
|
|
22
19
|
<rules>
|
|
23
20
|
- Preserve concrete decisions, constraints, assumptions, plans, open questions, and unresolved risks.
|
|
24
21
|
- Preserve action ownership, specialist consultations, and the latest working direction.
|
|
25
|
-
- Preserve provenance by attaching \`sourceMessageIds\` only from the provided message IDs.
|
|
26
22
|
- Keep identifiers, numbers, dates, and proper nouns when present.
|
|
27
23
|
- Remove filler and repetition.
|
|
28
24
|
- Do not invent details.
|
|
29
|
-
- If uncertain,
|
|
25
|
+
- If uncertain, omit the detail rather than guessing.
|
|
30
26
|
</rules>
|
|
31
27
|
|
|
32
28
|
<output-format>
|
|
33
|
-
The caller enforces a structured output schema
|
|
34
|
-
|
|
35
|
-
- summary: concise text summary
|
|
36
|
-
- stateDelta: include every field; use empty arrays for unchanged list fields, null for unchanged nullable fields, and \`currentPlan.action\` to signal \`unchanged\`, \`clear\`, or \`set\`
|
|
29
|
+
The caller enforces a structured output schema with exactly one field:
|
|
30
|
+
- summary: concise replacement summary
|
|
37
31
|
</output-format>
|
|
38
32
|
</agent-instructions>`
|
|
39
33
|
|