@swarmclawai/swarmclaw 1.8.13 → 1.9.2
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/README.md +19 -0
- package/package.json +3 -3
- package/scripts/ensure-sandbox-browser-image.mjs +12 -2
- package/src/app/api/knowledge/hygiene/route.ts +19 -1
- package/src/app/api/portability/export/route.test.ts +17 -0
- package/src/app/api/portability/export/route.ts +11 -2
- package/src/app/api/tasks/task-workspace-route.test.ts +112 -0
- package/src/components/tasks/task-card.tsx +49 -1
- package/src/components/tasks/task-sheet.tsx +173 -1
- package/src/components/ui/info-chip.tsx +3 -2
- package/src/features/tasks/queries.ts +2 -1
- package/src/lib/server/agents/delegation-advisory.test.ts +1 -0
- package/src/lib/server/agents/delegation-advisory.ts +10 -0
- package/src/lib/server/chat-execution/iteration-event-handler.ts +24 -8
- package/src/lib/server/chat-execution/reasoning-tag-scrubber.test.ts +117 -0
- package/src/lib/server/chat-execution/reasoning-tag-scrubber.ts +219 -0
- package/src/lib/server/knowledge-sources.test.ts +45 -0
- package/src/lib/server/knowledge-sources.ts +33 -0
- package/src/lib/server/portability/export.ts +10 -0
- package/src/lib/server/session-tools/crud.ts +25 -2
- package/src/lib/server/session-tools/manage-tasks.test.ts +7 -2
- package/src/lib/server/tasks/task-execution-workspace.test.ts +117 -0
- package/src/lib/server/tasks/task-execution-workspace.ts +321 -0
- package/src/lib/server/tasks/task-route-service.ts +87 -9
- package/src/lib/server/tasks/task-service.test.ts +60 -2
- package/src/lib/server/tasks/task-service.ts +35 -0
- package/src/lib/tasks.ts +13 -5
- package/src/lib/validation/schemas.ts +19 -0
- package/src/types/misc.ts +1 -1
- package/src/types/task.ts +62 -0
|
@@ -124,6 +124,7 @@ describe('delegation-advisory', () => {
|
|
|
124
124
|
assert.equal(result.shouldDelegate, true)
|
|
125
125
|
assert.equal(result.style, 'managerial')
|
|
126
126
|
assert.equal(result.recommended?.agentId, 'builder')
|
|
127
|
+
assert.equal(result.recommended?.routeKey, 'coding:coding,debugging,implementation:builder')
|
|
127
128
|
assert.match(advisory.formatDelegationRationale(result.recommended), /coding/i)
|
|
128
129
|
})
|
|
129
130
|
|
|
@@ -20,6 +20,7 @@ export interface DelegationTaskProfile {
|
|
|
20
20
|
export interface DelegationCandidateFit {
|
|
21
21
|
agentId: string
|
|
22
22
|
agentName: string
|
|
23
|
+
routeKey: string
|
|
23
24
|
score: number
|
|
24
25
|
availability: 'idle' | 'working' | 'unknown'
|
|
25
26
|
matchedCapabilities: string[]
|
|
@@ -76,6 +77,14 @@ function matchedCapabilities(agentCapabilities: string[] | undefined, requiredCa
|
|
|
76
77
|
return requiredCapabilities.filter((entry) => agentSet.has(entry.toLowerCase()))
|
|
77
78
|
}
|
|
78
79
|
|
|
80
|
+
function buildRouteKey(agent: Agent, profile: DelegationTaskProfile): string {
|
|
81
|
+
const required = profile.requiredCapabilities
|
|
82
|
+
.map((entry) => entry.toLowerCase())
|
|
83
|
+
.sort()
|
|
84
|
+
.join(',')
|
|
85
|
+
return [profile.workType, required || 'general', agent.id].join(':')
|
|
86
|
+
}
|
|
87
|
+
|
|
79
88
|
function roleAdjustment(agent: Agent, profile: DelegationTaskProfile): number {
|
|
80
89
|
const role = agent.role === 'coordinator' ? 'coordinator' : 'worker'
|
|
81
90
|
if (profile.workType === 'operations') {
|
|
@@ -141,6 +150,7 @@ function buildCandidateFit(
|
|
|
141
150
|
return {
|
|
142
151
|
agentId: agent.id,
|
|
143
152
|
agentName: agent.name,
|
|
153
|
+
routeKey: buildRouteKey(agent, profile),
|
|
144
154
|
score,
|
|
145
155
|
availability,
|
|
146
156
|
matchedCapabilities: matched,
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
createReasoningContentMetadata,
|
|
31
31
|
extractReasoningContentDelta,
|
|
32
32
|
} from '@/lib/providers/deepseek-reasoning-chat-openai'
|
|
33
|
+
import { StreamingReasoningTagScrubber } from '@/lib/server/chat-execution/reasoning-tag-scrubber'
|
|
33
34
|
|
|
34
35
|
// ---------------------------------------------------------------------------
|
|
35
36
|
// LangGraph event kind constants
|
|
@@ -92,10 +93,24 @@ export async function processIterationEvents(opts: ProcessIterationEventsOpts):
|
|
|
92
93
|
let toolEndCount = 0
|
|
93
94
|
const iterationText = { value: '' }
|
|
94
95
|
const toolPerfEnds = new Map<string, (extra?: Record<string, unknown>) => number>()
|
|
96
|
+
const reasoningTagScrubber = new StreamingReasoningTagScrubber()
|
|
95
97
|
|
|
96
98
|
/** Interval for progress checkpoint nudges */
|
|
97
99
|
const PROGRESS_CHECK_INTERVAL = 10
|
|
98
100
|
|
|
101
|
+
const emitThinking = (text: string) => {
|
|
102
|
+
if (!text) return
|
|
103
|
+
state.accumulatedThinking += text
|
|
104
|
+
write(`data: ${JSON.stringify({ t: 'thinking', text })}\n\n`)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const appendScrubbedText = (text: string) => {
|
|
108
|
+
if (!text) return
|
|
109
|
+
const scrubbed = reasoningTagScrubber.feed(text)
|
|
110
|
+
if (scrubbed.reasoning) emitThinking(scrubbed.reasoning)
|
|
111
|
+
if (scrubbed.visible) state.appendText(scrubbed.visible, iterationText, write)
|
|
112
|
+
}
|
|
113
|
+
|
|
99
114
|
for await (const event of eventStream) {
|
|
100
115
|
const kind = event.event
|
|
101
116
|
|
|
@@ -104,27 +119,24 @@ export async function processIterationEvents(opts: ProcessIterationEventsOpts):
|
|
|
104
119
|
const chunk = event.data?.chunk
|
|
105
120
|
const reasoningDelta = extractReasoningContentDelta(chunk?.additional_kwargs as Record<string, unknown> | undefined)
|
|
106
121
|
if (reasoningDelta) {
|
|
107
|
-
|
|
108
|
-
write(`data: ${JSON.stringify({ t: 'thinking', text: reasoningDelta })}\n\n`)
|
|
122
|
+
emitThinking(reasoningDelta)
|
|
109
123
|
write(`data: ${JSON.stringify({ t: 'md', text: JSON.stringify(createReasoningContentMetadata(reasoningDelta)) })}\n\n`)
|
|
110
124
|
}
|
|
111
125
|
if (chunk?.content) {
|
|
112
126
|
if (Array.isArray(chunk.content)) {
|
|
113
127
|
for (const block of chunk.content) {
|
|
114
128
|
if (block.type === 'thinking' && block.thinking) {
|
|
115
|
-
|
|
116
|
-
write(`data: ${JSON.stringify({ t: 'thinking', text: block.thinking })}\n\n`)
|
|
129
|
+
emitThinking(block.thinking)
|
|
117
130
|
} else if (typeof block.text === 'string' && block.text.startsWith('[[thinking]]')) {
|
|
118
|
-
|
|
119
|
-
write(`data: ${JSON.stringify({ t: 'thinking', text: block.text.slice(12) })}\n\n`)
|
|
131
|
+
emitThinking(block.text.slice(12))
|
|
120
132
|
} else if (block.text) {
|
|
121
|
-
|
|
133
|
+
appendScrubbedText(block.text)
|
|
122
134
|
}
|
|
123
135
|
}
|
|
124
136
|
} else {
|
|
125
137
|
const text = typeof chunk.content === 'string' ? chunk.content : ''
|
|
126
138
|
if (text) {
|
|
127
|
-
|
|
139
|
+
appendScrubbedText(text)
|
|
128
140
|
}
|
|
129
141
|
}
|
|
130
142
|
}
|
|
@@ -351,6 +363,10 @@ export async function processIterationEvents(opts: ProcessIterationEventsOpts):
|
|
|
351
363
|
}
|
|
352
364
|
}
|
|
353
365
|
|
|
366
|
+
const finalScrubbed = reasoningTagScrubber.flush()
|
|
367
|
+
if (finalScrubbed.reasoning) emitThinking(finalScrubbed.reasoning)
|
|
368
|
+
if (finalScrubbed.visible) state.appendText(finalScrubbed.visible, iterationText, write)
|
|
369
|
+
|
|
354
370
|
return {
|
|
355
371
|
reachedExecutionBoundary,
|
|
356
372
|
executionFollowthroughReason,
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { test } from 'node:test'
|
|
3
|
+
import { StreamingReasoningTagScrubber } from './reasoning-tag-scrubber'
|
|
4
|
+
import { ChatTurnState } from './chat-turn-state'
|
|
5
|
+
import { processIterationEvents } from './iteration-event-handler'
|
|
6
|
+
|
|
7
|
+
function drive(deltas: string[]) {
|
|
8
|
+
const scrubber = new StreamingReasoningTagScrubber()
|
|
9
|
+
let visible = ''
|
|
10
|
+
let reasoning = ''
|
|
11
|
+
for (const delta of deltas) {
|
|
12
|
+
const result = scrubber.feed(delta)
|
|
13
|
+
visible += result.visible
|
|
14
|
+
reasoning += result.reasoning
|
|
15
|
+
}
|
|
16
|
+
const final = scrubber.flush()
|
|
17
|
+
visible += final.visible
|
|
18
|
+
reasoning += final.reasoning
|
|
19
|
+
return { visible, reasoning }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
test('strips closed reasoning pairs from visible text and captures reasoning', () => {
|
|
23
|
+
assert.deepEqual(
|
|
24
|
+
drive(['Hello <think>private note</think> world']),
|
|
25
|
+
{ visible: 'Hello world', reasoning: 'private note' },
|
|
26
|
+
)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('handles all supported tag variants case-insensitively', () => {
|
|
30
|
+
assert.deepEqual(
|
|
31
|
+
drive(['<THINKING>a</Thinking><reasoning>b</reasoning><thought>c</thought><REASONING_SCRATCHPAD>d</REASONING_SCRATCHPAD>done']),
|
|
32
|
+
{ visible: 'done', reasoning: 'abcd' },
|
|
33
|
+
)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('holds split opening tags across stream deltas', () => {
|
|
37
|
+
assert.deepEqual(
|
|
38
|
+
drive(['<', 'think>reasoning</think>', 'done']),
|
|
39
|
+
{ visible: 'done', reasoning: 'reasoning' },
|
|
40
|
+
)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('captures reasoning until a split close tag resolves', () => {
|
|
44
|
+
assert.deepEqual(
|
|
45
|
+
drive(['<think>first', ' second</th', 'ink>answer']),
|
|
46
|
+
{ visible: 'answer', reasoning: 'first second' },
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('does not treat a mid-line prose mention as an open reasoning block', () => {
|
|
51
|
+
const text = 'Use the <think> element for examples'
|
|
52
|
+
assert.deepEqual(drive([text]), { visible: text, reasoning: '' })
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('does strip a bounded mid-line pair because it is model reasoning markup', () => {
|
|
56
|
+
assert.deepEqual(
|
|
57
|
+
drive(['Use <think>hidden</think>this answer']),
|
|
58
|
+
{ visible: 'Use this answer', reasoning: 'hidden' },
|
|
59
|
+
)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('drops unterminated reasoning content at flush after capturing streamed content', () => {
|
|
63
|
+
assert.deepEqual(
|
|
64
|
+
drive(['Visible\n<think>private reasoning with no close']),
|
|
65
|
+
{ visible: 'Visible\n', reasoning: 'private reasoning with no close' },
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('reset clears an interrupted reasoning block and buffered partial tags', () => {
|
|
70
|
+
const scrubber = new StreamingReasoningTagScrubber()
|
|
71
|
+
assert.deepEqual(scrubber.feed('<think>hanging'), { visible: '', reasoning: 'hanging' })
|
|
72
|
+
scrubber.reset()
|
|
73
|
+
assert.deepEqual(scrubber.feed('fresh<'), { visible: 'fresh', reasoning: '' })
|
|
74
|
+
scrubber.reset()
|
|
75
|
+
assert.deepEqual(scrubber.feed('content'), { visible: 'content', reasoning: '' })
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('processIterationEvents routes split reasoning tags away from visible deltas', async () => {
|
|
79
|
+
async function* eventStream() {
|
|
80
|
+
yield { event: 'on_chat_model_stream', data: { chunk: { content: '<' } } }
|
|
81
|
+
yield { event: 'on_chat_model_stream', data: { chunk: { content: 'think>private' } } }
|
|
82
|
+
yield { event: 'on_chat_model_stream', data: { chunk: { content: ' reasoning</th' } } }
|
|
83
|
+
yield { event: 'on_chat_model_stream', data: { chunk: { content: 'ink>done' } } }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const state = new ChatTurnState()
|
|
87
|
+
const writes: string[] = []
|
|
88
|
+
const outcome = await processIterationEvents({
|
|
89
|
+
eventStream: eventStream(),
|
|
90
|
+
state,
|
|
91
|
+
timers: {
|
|
92
|
+
armIdleWatchdog: () => undefined,
|
|
93
|
+
clearIdleWatchdog: () => undefined,
|
|
94
|
+
clearRequiredToolKickoff: () => undefined,
|
|
95
|
+
} as never,
|
|
96
|
+
loopTracker: {} as never,
|
|
97
|
+
toolEventTracker: {} as never,
|
|
98
|
+
session: { id: 'sess_reasoning_tags', provider: 'openai', model: 'gpt-4o-mini' } as never,
|
|
99
|
+
message: 'answer briefly',
|
|
100
|
+
write: (data: string) => writes.push(data),
|
|
101
|
+
sessionExtensions: [],
|
|
102
|
+
boundedExternalExecutionTask: false,
|
|
103
|
+
toolToExtensionMap: {},
|
|
104
|
+
iterationController: new AbortController(),
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
assert.equal(outcome.iterationText, 'done')
|
|
108
|
+
assert.equal(state.fullText, 'done')
|
|
109
|
+
assert.equal(state.accumulatedThinking, 'private reasoning')
|
|
110
|
+
|
|
111
|
+
const rendered = writes.join('')
|
|
112
|
+
assert.match(rendered, /"t":"thinking"/)
|
|
113
|
+
assert.match(rendered, /"text":"private"/)
|
|
114
|
+
assert.match(rendered, /"text":" reasoning"/)
|
|
115
|
+
assert.doesNotMatch(rendered, /"t":"d"[^\n]*private/)
|
|
116
|
+
assert.doesNotMatch(rendered, /<think>|<\/think>/i)
|
|
117
|
+
})
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
export interface ReasoningTagScrubResult {
|
|
2
|
+
visible: string
|
|
3
|
+
reasoning: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const TAG_NAMES = [
|
|
7
|
+
'think',
|
|
8
|
+
'thinking',
|
|
9
|
+
'reasoning',
|
|
10
|
+
'thought',
|
|
11
|
+
'reasoning_scratchpad',
|
|
12
|
+
] as const
|
|
13
|
+
|
|
14
|
+
const OPEN_TAGS = TAG_NAMES.map((name) => `<${name}>`)
|
|
15
|
+
const CLOSE_TAGS = TAG_NAMES.map((name) => `</${name}>`)
|
|
16
|
+
const MAX_TAG_LENGTH = Math.max(...OPEN_TAGS.map((tag) => tag.length), ...CLOSE_TAGS.map((tag) => tag.length))
|
|
17
|
+
|
|
18
|
+
function isWhitespace(text: string): boolean {
|
|
19
|
+
for (let i = 0; i < text.length; i++) {
|
|
20
|
+
const code = text.charCodeAt(i)
|
|
21
|
+
if (code !== 9 && code !== 10 && code !== 11 && code !== 12 && code !== 13 && code !== 32) return false
|
|
22
|
+
}
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function matchTagAt(lowerText: string, index: number, tags: string[]): number {
|
|
27
|
+
for (const tag of tags) {
|
|
28
|
+
if (lowerText.startsWith(tag, index)) return tag.length
|
|
29
|
+
}
|
|
30
|
+
return 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function findFirstTag(text: string, tags: string[]): { index: number; length: number } {
|
|
34
|
+
const lowerText = text.toLowerCase()
|
|
35
|
+
let bestIndex = -1
|
|
36
|
+
let bestLength = 0
|
|
37
|
+
for (const tag of tags) {
|
|
38
|
+
const index = lowerText.indexOf(tag)
|
|
39
|
+
if (index !== -1 && (bestIndex === -1 || index < bestIndex)) {
|
|
40
|
+
bestIndex = index
|
|
41
|
+
bestLength = tag.length
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { index: bestIndex, length: bestLength }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function maxPartialSuffix(text: string, tags: string[]): number {
|
|
48
|
+
const lowerText = text.toLowerCase()
|
|
49
|
+
const maxLength = Math.min(MAX_TAG_LENGTH - 1, lowerText.length)
|
|
50
|
+
let best = 0
|
|
51
|
+
for (let length = 1; length <= maxLength; length++) {
|
|
52
|
+
const suffix = lowerText.slice(-length)
|
|
53
|
+
if (tags.some((tag) => tag.startsWith(suffix))) best = length
|
|
54
|
+
}
|
|
55
|
+
return best
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function stripOrphanCloseTags(text: string): string {
|
|
59
|
+
const lowerText = text.toLowerCase()
|
|
60
|
+
let output = ''
|
|
61
|
+
let index = 0
|
|
62
|
+
while (index < text.length) {
|
|
63
|
+
const closeLength = matchTagAt(lowerText, index, CLOSE_TAGS)
|
|
64
|
+
if (closeLength > 0) {
|
|
65
|
+
index += closeLength
|
|
66
|
+
while (index < text.length && isWhitespace(text[index])) index++
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
output += text[index]
|
|
70
|
+
index++
|
|
71
|
+
}
|
|
72
|
+
return output
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export class StreamingReasoningTagScrubber {
|
|
76
|
+
private inBlock = false
|
|
77
|
+
private buffer = ''
|
|
78
|
+
private lastVisibleEndedNewline = true
|
|
79
|
+
|
|
80
|
+
reset(): void {
|
|
81
|
+
this.inBlock = false
|
|
82
|
+
this.buffer = ''
|
|
83
|
+
this.lastVisibleEndedNewline = true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
feed(text: string): ReasoningTagScrubResult {
|
|
87
|
+
if (!text) return { visible: '', reasoning: '' }
|
|
88
|
+
let buffer = this.buffer + text
|
|
89
|
+
this.buffer = ''
|
|
90
|
+
let visible = ''
|
|
91
|
+
let reasoning = ''
|
|
92
|
+
|
|
93
|
+
while (buffer) {
|
|
94
|
+
if (this.inBlock) {
|
|
95
|
+
const close = findFirstTag(buffer, CLOSE_TAGS)
|
|
96
|
+
if (close.index === -1) {
|
|
97
|
+
const held = maxPartialSuffix(buffer, CLOSE_TAGS)
|
|
98
|
+
const captureEnd = held ? buffer.length - held : buffer.length
|
|
99
|
+
reasoning += buffer.slice(0, captureEnd)
|
|
100
|
+
this.buffer = held ? buffer.slice(-held) : ''
|
|
101
|
+
return { visible, reasoning }
|
|
102
|
+
}
|
|
103
|
+
reasoning += buffer.slice(0, close.index)
|
|
104
|
+
buffer = buffer.slice(close.index + close.length)
|
|
105
|
+
this.inBlock = false
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const pair = this.findEarliestClosedPair(buffer)
|
|
110
|
+
const open = this.findOpenAtBoundary(buffer)
|
|
111
|
+
|
|
112
|
+
if (pair && (open.index === -1 || pair.start <= open.index)) {
|
|
113
|
+
const preceding = stripOrphanCloseTags(buffer.slice(0, pair.start))
|
|
114
|
+
if (preceding) {
|
|
115
|
+
visible += preceding
|
|
116
|
+
this.lastVisibleEndedNewline = preceding.endsWith('\n')
|
|
117
|
+
}
|
|
118
|
+
reasoning += buffer.slice(pair.contentStart, pair.contentEnd)
|
|
119
|
+
buffer = buffer.slice(pair.end)
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (open.index !== -1) {
|
|
124
|
+
const preceding = stripOrphanCloseTags(buffer.slice(0, open.index))
|
|
125
|
+
if (preceding) {
|
|
126
|
+
visible += preceding
|
|
127
|
+
this.lastVisibleEndedNewline = preceding.endsWith('\n')
|
|
128
|
+
}
|
|
129
|
+
this.inBlock = true
|
|
130
|
+
buffer = buffer.slice(open.index + open.length)
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const held = Math.max(maxPartialSuffix(buffer, OPEN_TAGS), maxPartialSuffix(buffer, CLOSE_TAGS))
|
|
135
|
+
const emitText = held ? buffer.slice(0, -held) : buffer
|
|
136
|
+
this.buffer = held ? buffer.slice(-held) : ''
|
|
137
|
+
if (emitText) {
|
|
138
|
+
const cleaned = stripOrphanCloseTags(emitText)
|
|
139
|
+
if (cleaned) {
|
|
140
|
+
visible += cleaned
|
|
141
|
+
this.lastVisibleEndedNewline = cleaned.endsWith('\n')
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { visible, reasoning }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { visible, reasoning }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
flush(): ReasoningTagScrubResult {
|
|
151
|
+
if (this.inBlock) {
|
|
152
|
+
this.inBlock = false
|
|
153
|
+
this.buffer = ''
|
|
154
|
+
return { visible: '', reasoning: '' }
|
|
155
|
+
}
|
|
156
|
+
const tail = stripOrphanCloseTags(this.buffer)
|
|
157
|
+
this.buffer = ''
|
|
158
|
+
if (tail) this.lastVisibleEndedNewline = tail.endsWith('\n')
|
|
159
|
+
return { visible: tail, reasoning: '' }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private findEarliestClosedPair(text: string): {
|
|
163
|
+
start: number
|
|
164
|
+
contentStart: number
|
|
165
|
+
contentEnd: number
|
|
166
|
+
end: number
|
|
167
|
+
} | null {
|
|
168
|
+
const lowerText = text.toLowerCase()
|
|
169
|
+
let best: { start: number; contentStart: number; contentEnd: number; end: number } | null = null
|
|
170
|
+
for (const name of TAG_NAMES) {
|
|
171
|
+
const openTag = `<${name}>`
|
|
172
|
+
const closeTag = `</${name}>`
|
|
173
|
+
let searchFrom = 0
|
|
174
|
+
while (searchFrom < lowerText.length) {
|
|
175
|
+
const start = lowerText.indexOf(openTag, searchFrom)
|
|
176
|
+
if (start === -1) break
|
|
177
|
+
const contentStart = start + openTag.length
|
|
178
|
+
const contentEnd = lowerText.indexOf(closeTag, contentStart)
|
|
179
|
+
if (contentEnd !== -1) {
|
|
180
|
+
const candidate = {
|
|
181
|
+
start,
|
|
182
|
+
contentStart,
|
|
183
|
+
contentEnd,
|
|
184
|
+
end: contentEnd + closeTag.length,
|
|
185
|
+
}
|
|
186
|
+
if (!best || candidate.start < best.start) best = candidate
|
|
187
|
+
break
|
|
188
|
+
}
|
|
189
|
+
searchFrom = contentStart
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return best
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private findOpenAtBoundary(text: string): { index: number; length: number } {
|
|
196
|
+
const lowerText = text.toLowerCase()
|
|
197
|
+
let best = { index: -1, length: 0 }
|
|
198
|
+
for (const openTag of OPEN_TAGS) {
|
|
199
|
+
let searchFrom = 0
|
|
200
|
+
while (searchFrom < lowerText.length) {
|
|
201
|
+
const index = lowerText.indexOf(openTag, searchFrom)
|
|
202
|
+
if (index === -1) break
|
|
203
|
+
if (this.isBlockBoundary(text.slice(0, index))) {
|
|
204
|
+
if (best.index === -1 || index < best.index) best = { index, length: openTag.length }
|
|
205
|
+
break
|
|
206
|
+
}
|
|
207
|
+
searchFrom = index + openTag.length
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return best
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private isBlockBoundary(preceding: string): boolean {
|
|
214
|
+
if (!preceding) return this.lastVisibleEndedNewline
|
|
215
|
+
const lastNewline = Math.max(preceding.lastIndexOf('\n'), preceding.lastIndexOf('\r'))
|
|
216
|
+
if (lastNewline !== -1) return isWhitespace(preceding.slice(lastNewline + 1))
|
|
217
|
+
return this.lastVisibleEndedNewline && isWhitespace(preceding)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -259,3 +259,48 @@ test('runKnowledgeHygieneMaintenance keeps same-content sources separate when vi
|
|
|
259
259
|
assert.equal(output.agent1Hits, 2)
|
|
260
260
|
assert.equal(output.agent2Hits, 1)
|
|
261
261
|
})
|
|
262
|
+
|
|
263
|
+
test('pruneArchivedKnowledgeSources deletes old archived sources and records prune actions', () => {
|
|
264
|
+
const output = runWithTempDataDir<{
|
|
265
|
+
pruned: number
|
|
266
|
+
sourceStillExists: boolean
|
|
267
|
+
hitCount: number
|
|
268
|
+
actionKind: string | null
|
|
269
|
+
}>(`
|
|
270
|
+
const knowledgeMod = await import('./src/lib/server/knowledge-sources.ts')
|
|
271
|
+
const storageMod = await import('./src/lib/server/storage.ts')
|
|
272
|
+
const knowledge = knowledgeMod.default || knowledgeMod
|
|
273
|
+
const storage = storageMod.default || storageMod
|
|
274
|
+
|
|
275
|
+
const source = await knowledge.createKnowledgeSource({
|
|
276
|
+
kind: 'manual',
|
|
277
|
+
title: 'Old Archived Notes',
|
|
278
|
+
content: 'prune candidate payload',
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
await knowledge.archiveKnowledgeSource(source.source.id, { reason: 'obsolete' })
|
|
282
|
+
const oldTimestamp = Date.now() - 60 * 24 * 60 * 60 * 1000
|
|
283
|
+
storage.patchKnowledgeSource(source.source.id, (current) => current ? {
|
|
284
|
+
...current,
|
|
285
|
+
archivedAt: oldTimestamp,
|
|
286
|
+
maintenanceUpdatedAt: oldTimestamp,
|
|
287
|
+
updatedAt: oldTimestamp,
|
|
288
|
+
} : null)
|
|
289
|
+
|
|
290
|
+
const result = await knowledge.pruneArchivedKnowledgeSources({ olderThanDays: 30 })
|
|
291
|
+
const summary = await knowledge.getKnowledgeHygieneSummary()
|
|
292
|
+
const hits = await knowledge.searchKnowledgeHits({ query: 'candidate', includeArchived: true })
|
|
293
|
+
|
|
294
|
+
console.log(JSON.stringify({
|
|
295
|
+
pruned: result.pruned,
|
|
296
|
+
sourceStillExists: Boolean(storage.loadKnowledgeSource(source.source.id)),
|
|
297
|
+
hitCount: hits.length,
|
|
298
|
+
actionKind: summary.recentActions.find((action) => action.sourceId === source.source.id)?.kind || null,
|
|
299
|
+
}))
|
|
300
|
+
`, { prefix: 'swarmclaw-knowledge-prune-' })
|
|
301
|
+
|
|
302
|
+
assert.equal(output.pruned, 1)
|
|
303
|
+
assert.equal(output.sourceStillExists, false)
|
|
304
|
+
assert.equal(output.hitCount, 0)
|
|
305
|
+
assert.equal(output.actionKind, 'prune')
|
|
306
|
+
})
|
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
import { onNextIdleWindow } from '@/lib/server/runtime/idle-window'
|
|
32
32
|
|
|
33
33
|
const KNOWLEDGE_STALE_AFTER_MS = 1000 * 60 * 60 * 24 * 14
|
|
34
|
+
const DEFAULT_PRUNE_ARCHIVED_AFTER_DAYS = 30
|
|
34
35
|
const CHUNK_TARGET_CHARS = 2200
|
|
35
36
|
const CHUNK_OVERLAP_CHARS = 320
|
|
36
37
|
const MAX_KNOWLEDGE_SCAN = 10_000
|
|
@@ -1049,6 +1050,38 @@ export async function supersedeKnowledgeSource(
|
|
|
1049
1050
|
return getKnowledgeSourceDetail(updated.id)
|
|
1050
1051
|
}
|
|
1051
1052
|
|
|
1053
|
+
export async function pruneArchivedKnowledgeSources(input?: {
|
|
1054
|
+
olderThanDays?: number | null
|
|
1055
|
+
now?: number
|
|
1056
|
+
}): Promise<{ pruned: number; sourceIds: string[] }> {
|
|
1057
|
+
await ensureLegacyKnowledgeBackfill()
|
|
1058
|
+
const now = typeof input?.now === 'number' && Number.isFinite(input.now) ? input.now : Date.now()
|
|
1059
|
+
const olderThanDays = typeof input?.olderThanDays === 'number' && Number.isFinite(input.olderThanDays)
|
|
1060
|
+
? Math.max(1, Math.trunc(input.olderThanDays))
|
|
1061
|
+
: DEFAULT_PRUNE_ARCHIVED_AFTER_DAYS
|
|
1062
|
+
const cutoff = now - olderThanDays * 24 * 60 * 60 * 1000
|
|
1063
|
+
const sourceIds: string[] = []
|
|
1064
|
+
|
|
1065
|
+
for (const source of listStoredSources()) {
|
|
1066
|
+
if (!sourceIsArchived(source) && !sourceIsSuperseded(source)) continue
|
|
1067
|
+
const lifecycleAt = source.archivedAt || source.maintenanceUpdatedAt || source.updatedAt
|
|
1068
|
+
if (lifecycleAt > cutoff) continue
|
|
1069
|
+
const title = source.title
|
|
1070
|
+
const removed = await deleteKnowledgeSource(source.id)
|
|
1071
|
+
if (!removed) continue
|
|
1072
|
+
sourceIds.push(source.id)
|
|
1073
|
+
recordMaintenanceAction({
|
|
1074
|
+
kind: 'prune',
|
|
1075
|
+
sourceId: source.id,
|
|
1076
|
+
relatedSourceId: source.duplicateOfSourceId || source.supersededBySourceId || null,
|
|
1077
|
+
summary: `Pruned ${title}`,
|
|
1078
|
+
createdAt: now,
|
|
1079
|
+
})
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
return { pruned: sourceIds.length, sourceIds }
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1052
1085
|
function sameSourceOrigin(left: KnowledgeSource, right: KnowledgeSource): boolean {
|
|
1053
1086
|
if (left.id === right.id) return false
|
|
1054
1087
|
if (left.sourceUrl && right.sourceUrl) return left.sourceUrl === right.sourceUrl
|
|
@@ -34,6 +34,16 @@ export interface PortableManifest {
|
|
|
34
34
|
extensions?: PortableExtensionRef[]
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
export function buildPortableExportFilename(manifest: Pick<PortableManifest, 'exportedAt'> = { exportedAt: new Date().toISOString() }): string {
|
|
38
|
+
const safeStamp = manifest.exportedAt
|
|
39
|
+
.replaceAll(':', '')
|
|
40
|
+
.replaceAll('.', '')
|
|
41
|
+
.replaceAll('-', '')
|
|
42
|
+
.replace('T', '-')
|
|
43
|
+
.replace('Z', 'Z')
|
|
44
|
+
return `swarmclaw-export-${safeStamp}.json`
|
|
45
|
+
}
|
|
46
|
+
|
|
37
47
|
export type PortableAgent = Omit<Agent,
|
|
38
48
|
| 'id' | 'credentialId' | 'fallbackCredentialIds' | 'apiEndpoint'
|
|
39
49
|
| 'threadSessionId' | 'lastUsedAt' | 'totalCost' | 'trashedAt'
|
|
@@ -43,6 +43,7 @@ import {
|
|
|
43
43
|
buildDelegationTaskProfile,
|
|
44
44
|
formatDelegationRationale,
|
|
45
45
|
resolveDelegationAdvisory,
|
|
46
|
+
type DelegationWorkType,
|
|
46
47
|
} from '@/lib/server/agents/delegation-advisory'
|
|
47
48
|
import type { ToolBuildContext } from './context'
|
|
48
49
|
import { normalizeToolInputArgs } from './normalize-tool-args'
|
|
@@ -102,6 +103,17 @@ function buildTaskDelegationText(parsed: Record<string, unknown>): string {
|
|
|
102
103
|
return [title, description].filter(Boolean).join('\n\n').trim()
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
function normalizeDelegationWorkType(value: unknown): DelegationWorkType | null {
|
|
107
|
+
return value === 'coding'
|
|
108
|
+
|| value === 'research'
|
|
109
|
+
|| value === 'writing'
|
|
110
|
+
|| value === 'review'
|
|
111
|
+
|| value === 'operations'
|
|
112
|
+
|| value === 'general'
|
|
113
|
+
? value
|
|
114
|
+
: null
|
|
115
|
+
}
|
|
116
|
+
|
|
105
117
|
async function resolveManagedTaskDelegation(params: {
|
|
106
118
|
parsed: Record<string, unknown>
|
|
107
119
|
agents: ReturnType<typeof loadAgents>
|
|
@@ -122,9 +134,11 @@ async function resolveManagedTaskDelegation(params: {
|
|
|
122
134
|
return { assignedAgentId: params.assignedAgentId, advisory: null }
|
|
123
135
|
}
|
|
124
136
|
|
|
137
|
+
const decisionStartedAt = Date.now()
|
|
125
138
|
const explicitCapabilities = normalizeStringList(params.parsed.requiredCapabilities)
|
|
139
|
+
const explicitWorkType = normalizeDelegationWorkType(params.parsed.workType)
|
|
126
140
|
const classificationText = buildTaskDelegationText(params.parsed)
|
|
127
|
-
const classification = (!explicitCapabilities.length && classificationText && params.ctx?.sessionId)
|
|
141
|
+
const classification = (!explicitCapabilities.length && !explicitWorkType && classificationText && params.ctx?.sessionId)
|
|
128
142
|
? await classifyMessage({
|
|
129
143
|
sessionId: params.ctx.sessionId,
|
|
130
144
|
agentId: currentAgentId,
|
|
@@ -134,6 +148,7 @@ async function resolveManagedTaskDelegation(params: {
|
|
|
134
148
|
|
|
135
149
|
const profile = buildDelegationTaskProfile({
|
|
136
150
|
classification,
|
|
151
|
+
workType: explicitWorkType,
|
|
137
152
|
requiredCapabilities: explicitCapabilities,
|
|
138
153
|
})
|
|
139
154
|
if (!profile.substantial) {
|
|
@@ -167,10 +182,18 @@ async function resolveManagedTaskDelegation(params: {
|
|
|
167
182
|
advisory: {
|
|
168
183
|
recommendedAgentId: recommended.agentId,
|
|
169
184
|
recommendedAgentName: recommended.agentName,
|
|
185
|
+
routeKey: recommended.routeKey,
|
|
170
186
|
rationale: formatDelegationRationale(recommended),
|
|
171
187
|
workType: profile.workType,
|
|
172
188
|
requiredCapabilities: profile.requiredCapabilities,
|
|
173
189
|
autoAssigned,
|
|
190
|
+
routingMode: explicitCapabilities.length || explicitWorkType
|
|
191
|
+
? 'deterministic'
|
|
192
|
+
: classification
|
|
193
|
+
? 'classified'
|
|
194
|
+
: 'default',
|
|
195
|
+
decisionLatencyMs: Date.now() - decisionStartedAt,
|
|
196
|
+
score: Number(recommended.score.toFixed(3)),
|
|
174
197
|
},
|
|
175
198
|
}
|
|
176
199
|
}
|
|
@@ -465,7 +488,7 @@ export function buildCrudTools(bctx: ToolBuildContext): StructuredToolInterface[
|
|
|
465
488
|
}
|
|
466
489
|
description += '\n\nCreate/update calls accept either `data` as a JSON string or direct top-level fields like `title`, `description`, `status`, `agentId`, and `projectId`.'
|
|
467
490
|
if (canAssignOtherAgents) {
|
|
468
|
-
description += '\n\nWhen you omit an assignee, the runtime may auto-assign the task to a materially better-fit teammate based on `requiredCapabilities` or the classified work type. If you set an explicit assignee, it is respected in v1, but the response may include `delegationAdvisory` when another teammate is a better fit.'
|
|
491
|
+
description += '\n\nWhen you omit an assignee, the runtime may auto-assign the task to a materially better-fit teammate based on `requiredCapabilities`, explicit `workType`, or the classified work type. Use `workType:"coding"|"research"|"writing"|"review"|"operations"|"general"` for deterministic routing without a classifier call. If you set an explicit assignee, it is respected in v1, but the response may include `delegationAdvisory` when another teammate is a better fit.'
|
|
469
492
|
}
|
|
470
493
|
description += '\n\nFor follow-up work, set `continueFromTaskId` (or `followUpToTaskId`) to a prior task ID. The new task will inherit the predecessor\'s project/agent/session context, block on that task by default, and reuse its execution session when possible.'
|
|
471
494
|
if (ctx?.projectId) {
|
|
@@ -172,7 +172,7 @@ describe('manage_tasks tool', () => {
|
|
|
172
172
|
action: 'create',
|
|
173
173
|
title: 'Implement API integration',
|
|
174
174
|
description: 'Build the API client and fix the failing tests.',
|
|
175
|
-
|
|
175
|
+
workType: 'coding',
|
|
176
176
|
status: 'backlog',
|
|
177
177
|
})
|
|
178
178
|
|
|
@@ -185,7 +185,11 @@ describe('manage_tasks tool', () => {
|
|
|
185
185
|
assert.equal(output.stored.agentId, 'builder')
|
|
186
186
|
assert.equal(output.response.delegationAdvisory.autoAssigned, true)
|
|
187
187
|
assert.equal(output.response.delegationAdvisory.recommendedAgentId, 'builder')
|
|
188
|
-
assert.deepEqual(output.response.delegationAdvisory.requiredCapabilities, ['coding'])
|
|
188
|
+
assert.deepEqual(output.response.delegationAdvisory.requiredCapabilities, ['coding', 'implementation', 'debugging'])
|
|
189
|
+
assert.equal(output.response.delegationAdvisory.routingMode, 'deterministic')
|
|
190
|
+
assert.match(output.response.delegationAdvisory.routeKey, /^coding:coding,debugging,implementation:builder$/)
|
|
191
|
+
assert.equal(typeof output.response.delegationAdvisory.decisionLatencyMs, 'number')
|
|
192
|
+
assert.equal(output.stored.workflowStateId, 'in_progress')
|
|
189
193
|
})
|
|
190
194
|
|
|
191
195
|
it('keeps an explicit assignee but returns delegation advisory when another teammate is a better fit', () => {
|
|
@@ -262,5 +266,6 @@ describe('manage_tasks tool', () => {
|
|
|
262
266
|
assert.equal(output.stored.agentId, 'researcher')
|
|
263
267
|
assert.equal(output.response.delegationAdvisory.autoAssigned, false)
|
|
264
268
|
assert.equal(output.response.delegationAdvisory.recommendedAgentId, 'builder')
|
|
269
|
+
assert.equal(output.response.delegationAdvisory.routingMode, 'deterministic')
|
|
265
270
|
})
|
|
266
271
|
})
|