typeclaw 0.40.0 → 0.41.0
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/package.json +1 -1
- package/src/agent/multimodal/look-at.ts +87 -0
- package/src/bundled-plugins/memory/index.ts +10 -11
- package/src/bundled-plugins/memory/vector/index-on-write.ts +25 -20
- package/src/bundled-plugins/memory/vector/reference-index-on-write.ts +28 -23
- package/src/bundled-plugins/memory/vector/store.ts +2 -0
- package/src/bundled-plugins/planner/planner.ts +4 -2
- package/src/bundled-plugins/reviewer/reviewer.ts +9 -1
- package/src/channels/adapters/kakaotalk-classify.ts +31 -6
- package/src/channels/adapters/kakaotalk.ts +1 -1
- package/src/hostd/daemon.ts +48 -12
- package/src/hostd/kakao-renewal-manager.ts +35 -10
- package/src/hostd/tailscale.ts +41 -5
- package/src/hostd/webex-renewal-manager.ts +13 -4
- package/src/plugin/index.ts +1 -0
- package/src/plugin/manager.ts +14 -0
- package/src/plugin/registry.ts +12 -0
- package/src/plugin/types.ts +4 -0
- package/src/run/index.ts +46 -20
- package/src/test-helpers/rm-temp-dir.ts +24 -0
package/package.json
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
|
|
1
3
|
import { Type } from '@mariozechner/pi-ai'
|
|
2
4
|
import type { ImageContent } from '@mariozechner/pi-ai'
|
|
3
5
|
import { defineTool } from '@mariozechner/pi-coding-agent'
|
|
@@ -5,6 +7,9 @@ import { defineTool } from '@mariozechner/pi-coding-agent'
|
|
|
5
7
|
import { createSessionWithDispose, type SessionOrigin } from '@/agent'
|
|
6
8
|
import type { ChannelRouter } from '@/channels/router'
|
|
7
9
|
import type { AdapterId } from '@/channels/schema'
|
|
10
|
+
import { getConfig, resolveModel, resolveProfile } from '@/config'
|
|
11
|
+
import { providerForModelRef } from '@/config/providers'
|
|
12
|
+
import { SecretsBackend } from '@/secrets'
|
|
8
13
|
|
|
9
14
|
import { buildMultimodalLookerSystemPrompt, resolveImage, type ImageInput } from './looker'
|
|
10
15
|
|
|
@@ -139,6 +144,15 @@ function toImageContent(data: string, mimeType: string): ImageContent {
|
|
|
139
144
|
}
|
|
140
145
|
|
|
141
146
|
async function runLookAtImages(imageContents: ImageContent[], prompt: string | undefined) {
|
|
147
|
+
// Exception for text-only GLM Coding Plan models: the subagent below would
|
|
148
|
+
// resolve to the same image-blind model and silently fail. GLM-4.6V vision is
|
|
149
|
+
// reachable on the coding-plan endpoint with the same key (POST
|
|
150
|
+
// /api/coding/paas/v4 → 200, drawn from the plan's vision quota). Only this
|
|
151
|
+
// setup diverts; every other one keeps the subagent path.
|
|
152
|
+
if (shouldUseGlmCodingPlanVision()) {
|
|
153
|
+
return await analyzeWithGlmCodingPlanVision(imageContents, prompt)
|
|
154
|
+
}
|
|
155
|
+
|
|
142
156
|
const systemPrompt = buildMultimodalLookerSystemPrompt(prompt)
|
|
143
157
|
const userText =
|
|
144
158
|
prompt !== undefined && prompt.trim() !== '' ? prompt.trim() : 'Please describe the attached image(s).'
|
|
@@ -188,6 +202,79 @@ async function runLookAtImages(imageContents: ImageContent[], prompt: string | u
|
|
|
188
202
|
}
|
|
189
203
|
}
|
|
190
204
|
|
|
205
|
+
const GLM_VISION_PROVIDER = 'zai-coding'
|
|
206
|
+
const GLM_VISION_MODEL = 'glm-4.6v'
|
|
207
|
+
const GLM_VISION_ENDPOINT = 'https://api.z.ai/api/coding/paas/v4/chat/completions'
|
|
208
|
+
const GLM_VISION_MAX_TOKENS = 32768
|
|
209
|
+
|
|
210
|
+
function shouldUseGlmCodingPlanVision(): boolean {
|
|
211
|
+
const ref = resolveProfile(getConfig().models, 'vision').ref
|
|
212
|
+
return providerForModelRef(ref) === GLM_VISION_PROVIDER && !resolveModel(ref).input.includes('image')
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function analyzeWithGlmCodingPlanVision(imageContents: ImageContent[], prompt: string | undefined) {
|
|
216
|
+
const details = { count: imageContents.length, prompt }
|
|
217
|
+
const apiKey = new SecretsBackend(join(process.cwd(), 'secrets.json')).tryReadProviderApiKeySync(GLM_VISION_PROVIDER)
|
|
218
|
+
if (apiKey === null) {
|
|
219
|
+
return errorResult('GLM Coding Plan vision unavailable: no zai-coding API key', details)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
let response: Response
|
|
223
|
+
try {
|
|
224
|
+
response = await fetch(GLM_VISION_ENDPOINT, {
|
|
225
|
+
method: 'POST',
|
|
226
|
+
headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
|
|
227
|
+
body: JSON.stringify({
|
|
228
|
+
model: GLM_VISION_MODEL,
|
|
229
|
+
max_tokens: GLM_VISION_MAX_TOKENS,
|
|
230
|
+
messages: buildGlmVisionMessages(imageContents, prompt),
|
|
231
|
+
}),
|
|
232
|
+
})
|
|
233
|
+
} catch (error) {
|
|
234
|
+
return errorResult(`GLM vision request failed: ${error instanceof Error ? error.message : String(error)}`, details)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (!response.ok) {
|
|
238
|
+
return errorResult(`GLM vision request failed: HTTP ${response.status}`, details)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const text = extractGlmVisionText(await response.json().catch(() => null))
|
|
242
|
+
if (text === null) {
|
|
243
|
+
return errorResult('GLM vision returned no text response', details)
|
|
244
|
+
}
|
|
245
|
+
return successResult(text, details)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Mirror the subagent path's systemPromptOverride so the direct route keeps
|
|
249
|
+
// the same visible-only / faithful-transcription / no-preamble behavior.
|
|
250
|
+
export function buildGlmVisionMessages(imageContents: ImageContent[], prompt: string | undefined) {
|
|
251
|
+
const userText =
|
|
252
|
+
prompt !== undefined && prompt.trim() !== '' ? prompt.trim() : 'Please describe the attached image(s).'
|
|
253
|
+
return [
|
|
254
|
+
{ role: 'system' as const, content: buildMultimodalLookerSystemPrompt(prompt) },
|
|
255
|
+
{
|
|
256
|
+
role: 'user' as const,
|
|
257
|
+
content: [
|
|
258
|
+
...imageContents.map((image) => ({
|
|
259
|
+
type: 'image_url' as const,
|
|
260
|
+
image_url: { url: `data:${image.mimeType};base64,${image.data}` },
|
|
261
|
+
})),
|
|
262
|
+
{ type: 'text' as const, text: userText },
|
|
263
|
+
],
|
|
264
|
+
},
|
|
265
|
+
]
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function extractGlmVisionText(body: unknown): string | null {
|
|
269
|
+
if (typeof body !== 'object' || body === null) return null
|
|
270
|
+
const choices = (body as { choices?: unknown }).choices
|
|
271
|
+
if (!Array.isArray(choices) || choices.length === 0) return null
|
|
272
|
+
const message = (choices[0] as { message?: unknown }).message
|
|
273
|
+
const text = (message as { content?: unknown } | undefined)?.content
|
|
274
|
+
if (typeof text !== 'string' || text.trim() === '') return null
|
|
275
|
+
return text.trim()
|
|
276
|
+
}
|
|
277
|
+
|
|
191
278
|
function toImageInput(p: ImageParam): ImageInput {
|
|
192
279
|
const hasUrl = 'url' in p && p.url !== undefined && p.url !== ''
|
|
193
280
|
const hasPath = 'path' in p && p.path !== undefined && p.path !== ''
|
|
@@ -28,7 +28,7 @@ import { embed } from './vector/embedder'
|
|
|
28
28
|
import { hybridSearch, type EmbedFn } from './vector/hybrid'
|
|
29
29
|
import { makeAppendHook } from './vector/index-on-write'
|
|
30
30
|
import { makeReferenceStoredHook } from './vector/reference-index-on-write'
|
|
31
|
-
import { VectorStore } from './vector/store'
|
|
31
|
+
import { type OpenVectorStore, VectorStore } from './vector/store'
|
|
32
32
|
|
|
33
33
|
const DEFAULT_IDLE_MS = 60_000
|
|
34
34
|
const DEFAULT_BUFFER_BYTES = 500_000
|
|
@@ -144,13 +144,13 @@ const VECTOR_TURN_TOP_K = 10
|
|
|
144
144
|
export type MemoryPluginDeps = {
|
|
145
145
|
hybridSearch: typeof hybridSearch
|
|
146
146
|
queryEmbedFn: EmbedFn
|
|
147
|
-
openAppendVectorStore: (agentDir: string) =>
|
|
147
|
+
openAppendVectorStore: (agentDir: string) => OpenVectorStore
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
const defaultDeps: MemoryPluginDeps = {
|
|
151
151
|
hybridSearch,
|
|
152
152
|
queryEmbedFn: embed,
|
|
153
|
-
openAppendVectorStore: (agentDir) => VectorStore.open(join(agentDir, 'memory', '.vectors', 'index.db')),
|
|
153
|
+
openAppendVectorStore: (agentDir) => () => VectorStore.open(join(agentDir, 'memory', '.vectors', 'index.db')),
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
// TypeClaw-owned infrastructure subagents (memory-logger, dreaming, backup) run
|
|
@@ -402,19 +402,18 @@ export function createMemoryPlugin(deps: MemoryPluginDeps = defaultDeps) {
|
|
|
402
402
|
error: (m: string) => ctx.logger.error(m),
|
|
403
403
|
}
|
|
404
404
|
|
|
405
|
-
//
|
|
406
|
-
//
|
|
407
|
-
//
|
|
408
|
-
//
|
|
409
|
-
|
|
410
|
-
const appendVectorStore = deps.openAppendVectorStore(ctx.agentDir)
|
|
405
|
+
// Opener (not an open handle) for append-time indexing. Each on-write hook
|
|
406
|
+
// opens its own short-lived store and closes it when the batch settles, so
|
|
407
|
+
// no bun:sqlite handle outlives a hook call to pin the agent dir (Bun
|
|
408
|
+
// #25964). Boot-only paths that never append never open the DB at all.
|
|
409
|
+
const openAppendVectorStore = deps.openAppendVectorStore(ctx.agentDir)
|
|
411
410
|
|
|
412
411
|
return {
|
|
413
412
|
subagents: {
|
|
414
413
|
'memory-logger': createMemoryLoggerSubagent({
|
|
415
414
|
logger: subagentLogger,
|
|
416
|
-
onFragmentsAppended: makeAppendHook(
|
|
417
|
-
onReferenceStored: makeReferenceStoredHook(
|
|
415
|
+
onFragmentsAppended: makeAppendHook(openAppendVectorStore),
|
|
416
|
+
onReferenceStored: makeReferenceStoredHook(openAppendVectorStore),
|
|
418
417
|
}),
|
|
419
418
|
dreaming: createDreamingSubagent({
|
|
420
419
|
logger: subagentLogger,
|
|
@@ -3,32 +3,37 @@ import type { FragmentEvent } from '../stream-events'
|
|
|
3
3
|
import type { FragmentsAppendedContext } from '../stream-io'
|
|
4
4
|
import { embed, EMBEDDING_MODEL_ID } from './embedder'
|
|
5
5
|
import type { EmbedFn } from './hybrid'
|
|
6
|
-
import type {
|
|
6
|
+
import type { OpenVectorStore } from './store'
|
|
7
7
|
|
|
8
8
|
export function makeAppendHook(
|
|
9
|
-
|
|
9
|
+
openStore: OpenVectorStore,
|
|
10
10
|
embedFn: EmbedFn = embed,
|
|
11
11
|
): (fragments: FragmentEvent[], context: FragmentsAppendedContext) => Promise<void> {
|
|
12
12
|
return async (fragments, context) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
const store = openStore()
|
|
14
|
+
try {
|
|
15
|
+
for (const fragment of fragments) {
|
|
16
|
+
const key = `${context.date ?? fragment.ts.slice(0, 10)}#${fragment.id}`
|
|
17
|
+
const id = `stream:${key}`
|
|
18
|
+
const contentHash = fragmentContentHash(fragment)
|
|
19
|
+
const existing = store.getByIds([id])[0]
|
|
20
|
+
if (existing?.contentHash === contentHash && existing.model === EMBEDDING_MODEL_ID) continue
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
const text = `${fragment.topic}\n${fragment.body}`
|
|
23
|
+
const [embedding] = await embedFn([text], 'passage')
|
|
24
|
+
if (embedding === undefined) continue
|
|
25
|
+
store.upsert({
|
|
26
|
+
id,
|
|
27
|
+
source: 'stream',
|
|
28
|
+
key,
|
|
29
|
+
model: EMBEDDING_MODEL_ID,
|
|
30
|
+
dims: embedding.length,
|
|
31
|
+
embedding,
|
|
32
|
+
contentHash,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
} finally {
|
|
36
|
+
store.close()
|
|
32
37
|
}
|
|
33
38
|
}
|
|
34
39
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { embed, EMBEDDING_MODEL_ID } from './embedder'
|
|
2
2
|
import type { EmbedFn } from './hybrid'
|
|
3
3
|
import { referencePassagesForOne } from './passages'
|
|
4
|
-
import type {
|
|
4
|
+
import type { OpenVectorStore } from './store'
|
|
5
5
|
|
|
6
6
|
export type ReferenceStoredContext = { slug: string; body: string; demoted?: boolean }
|
|
7
7
|
|
|
@@ -17,34 +17,39 @@ export type ReferenceStoredContext = { slug: string; body: string; demoted?: boo
|
|
|
17
17
|
// exists. We compute the wanted id set, upsert changed chunks, and delete any
|
|
18
18
|
// existing row for this slug that is not wanted.
|
|
19
19
|
export function makeReferenceStoredHook(
|
|
20
|
-
|
|
20
|
+
openStore: OpenVectorStore,
|
|
21
21
|
embedFn: EmbedFn = embed,
|
|
22
22
|
): (context: ReferenceStoredContext) => Promise<void> {
|
|
23
23
|
return async ({ slug, body, demoted }) => {
|
|
24
|
-
const
|
|
25
|
-
|
|
24
|
+
const store = openStore()
|
|
25
|
+
try {
|
|
26
|
+
const passages = referencePassagesForOne(slug, body, demoted)
|
|
27
|
+
const wantedIds = new Set(passages.map((passage) => passage.id))
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
const prefix = `reference:${slug}#`
|
|
30
|
+
const staleIds = store
|
|
31
|
+
.getAllMeta()
|
|
32
|
+
.flatMap((row) => (row.id.startsWith(prefix) && !wantedIds.has(row.id) ? [row.id] : []))
|
|
33
|
+
if (staleIds.length > 0) store.deleteMany(staleIds)
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
for (const passage of passages) {
|
|
36
|
+
const existing = store.getByIds([passage.id])[0]
|
|
37
|
+
if (existing?.contentHash === passage.contentHash && existing.model === EMBEDDING_MODEL_ID) continue
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
const [embedding] = await embedFn([passage.text], 'passage')
|
|
40
|
+
if (embedding === undefined) continue
|
|
41
|
+
store.upsert({
|
|
42
|
+
id: passage.id,
|
|
43
|
+
source: 'reference',
|
|
44
|
+
key: slug,
|
|
45
|
+
model: EMBEDDING_MODEL_ID,
|
|
46
|
+
dims: embedding.length,
|
|
47
|
+
embedding,
|
|
48
|
+
contentHash: passage.contentHash,
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
} finally {
|
|
52
|
+
store.close()
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
}
|
|
@@ -38,8 +38,10 @@ export const PLANNER_SKILLS: readonly LoadableSkill[] = [PROJECT_PLAN_SKILL, GEN
|
|
|
38
38
|
// request fails loudly. Sized for a thorough `deep`-model plan (research lookups
|
|
39
39
|
// + a careful decomposition), well above the typical sub-minute plan. This is
|
|
40
40
|
// liveness for the parent, not hard cancellation. See src/agent/subagents.ts
|
|
41
|
-
// `timeoutMs`.
|
|
42
|
-
|
|
41
|
+
// `timeoutMs`. 30m matches the reviewer and researcher — every deep-profile
|
|
42
|
+
// subagent shares the same nested-fan-out budget so a real pass is not killed
|
|
43
|
+
// mid-synthesis.
|
|
44
|
+
export const PLANNER_SPAWN_TIMEOUT_MS = 1_800_000
|
|
43
45
|
|
|
44
46
|
// The default write zone for the plan file when the caller does not specify
|
|
45
47
|
// `outputPath`. MUST be a universally-writable location, because a subagent
|
|
@@ -51,7 +51,15 @@ export const REVIEWER_SKILLS: readonly LoadableSkill[] = [
|
|
|
51
51
|
// liveness for the parent, not hard cancellation: pi's `session.prompt` takes no
|
|
52
52
|
// AbortSignal, so the LLM stream may run until the OS reaps it. See
|
|
53
53
|
// src/agent/subagents.ts `timeoutMs`.
|
|
54
|
-
|
|
54
|
+
//
|
|
55
|
+
// 30m, not the prior 10m: the `deep` profile trades speed for quality, and a
|
|
56
|
+
// real review of a large PR (many changed files + nested scout fan-out + a few
|
|
57
|
+
// web lookups + careful synthesis) routinely exceeds 10m. A 10m ceiling killed
|
|
58
|
+
// both the first and retry reviews of a 27-file PR mid-analysis, leaving the PR
|
|
59
|
+
// with no posted verdict — the exact "stranded review" failure the guards exist
|
|
60
|
+
// to prevent. Matches RESEARCHER_SPAWN_TIMEOUT_MS, the other deep-profile
|
|
61
|
+
// subagent that hit the same nested-fan-out wall.
|
|
62
|
+
export const REVIEWER_SPAWN_TIMEOUT_MS = 1_800_000
|
|
55
63
|
|
|
56
64
|
// The reviewer's read-only contract is enforced in depth: this system prompt
|
|
57
65
|
// states it, the global bash guards (`secret-exfil-bash`, `git-exfil`) catch
|
|
@@ -109,11 +109,10 @@ function parseReplyContext(event: KakaoTalkPushMessageEvent, selfUserId: string)
|
|
|
109
109
|
if (event.message_type !== KAKAO_MESSAGE_TYPE.REPLY) return null
|
|
110
110
|
if (event.attachment === null) return null
|
|
111
111
|
|
|
112
|
-
const logId =
|
|
113
|
-
const
|
|
114
|
-
if (logId === null ||
|
|
112
|
+
const logId = scalarIdField(event.attachment, 'src_logId')
|
|
113
|
+
const sourceAuthorId = decimalIdField(event.attachment, 'src_userId')
|
|
114
|
+
if (logId === null || sourceAuthorId === null) return null
|
|
115
115
|
|
|
116
|
-
const sourceAuthorId = String(sourceUserId)
|
|
117
116
|
const quotedText = stringField(event.attachment, 'src_message')
|
|
118
117
|
return {
|
|
119
118
|
logId,
|
|
@@ -150,7 +149,33 @@ function stringField(record: Record<string, unknown>, key: string): string | nul
|
|
|
150
149
|
return typeof value === 'string' && value.length > 0 ? value : null
|
|
151
150
|
}
|
|
152
151
|
|
|
153
|
-
|
|
152
|
+
// LOCO reply attachments are unvalidated `JSON.parse` output: `src_userId` arrives
|
|
153
|
+
// as a string in production despite the SDK annotating it `number`, so a number-only
|
|
154
|
+
// reader dropped every reply-to-bot. `src_userId` gates the bot-vs-other identity
|
|
155
|
+
// check, so require digits and keep it as an opaque string — never `Number()` a
|
|
156
|
+
// 64-bit id (lossy past 2^53).
|
|
157
|
+
function decimalIdField(record: Record<string, unknown>, key: string): string | null {
|
|
154
158
|
const value = record[key]
|
|
155
|
-
|
|
159
|
+
if (typeof value === 'string') {
|
|
160
|
+
const trimmed = value.trim()
|
|
161
|
+
return /^\d+$/.test(trimmed) ? trimmed : null
|
|
162
|
+
}
|
|
163
|
+
if (typeof value === 'number') {
|
|
164
|
+
return Number.isInteger(value) && value >= 0 ? String(value) : null
|
|
165
|
+
}
|
|
166
|
+
return null
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// `src_logId` is an opaque message reference we only echo back, never compare for
|
|
170
|
+
// identity, so accept any non-empty scalar (string or integer) without digit-gating.
|
|
171
|
+
function scalarIdField(record: Record<string, unknown>, key: string): string | null {
|
|
172
|
+
const value = record[key]
|
|
173
|
+
if (typeof value === 'string') {
|
|
174
|
+
const trimmed = value.trim()
|
|
175
|
+
return trimmed.length > 0 ? trimmed : null
|
|
176
|
+
}
|
|
177
|
+
if (typeof value === 'number') {
|
|
178
|
+
return Number.isInteger(value) && value >= 0 ? String(value) : null
|
|
179
|
+
}
|
|
180
|
+
return null
|
|
156
181
|
}
|
|
@@ -480,7 +480,7 @@ export function createKakaotalkAdapter(options: KakaotalkAdapterOptions): Kakaot
|
|
|
480
480
|
authorName: resolvedName ?? verdict.payload.authorId,
|
|
481
481
|
}
|
|
482
482
|
logger.info(
|
|
483
|
-
`[kakaotalk] routed log_id=${event.log_id} ${inboundTag} mention=${enriched.isBotMention} dm=${enriched.isDm}`,
|
|
483
|
+
`[kakaotalk] routed log_id=${event.log_id} ${inboundTag} mention=${enriched.isBotMention} reply=${enriched.replyToBotMessageId !== null} dm=${enriched.isDm}`,
|
|
484
484
|
)
|
|
485
485
|
await options.router.route(enriched)
|
|
486
486
|
} catch (err) {
|
package/src/hostd/daemon.ts
CHANGED
|
@@ -41,6 +41,10 @@ export type DaemonOptions = {
|
|
|
41
41
|
onLog?: (event: DaemonLogEvent | SupervisorLogEvent) => void
|
|
42
42
|
gcIntervalMs?: number
|
|
43
43
|
gcMissesToDeregister?: number
|
|
44
|
+
// Per-step bound on teardown calls (portbroker/renewal stop) run inside the
|
|
45
|
+
// per-container serial chain. Defaults to CLEANUP_STEP_TIMEOUT_MS; tests
|
|
46
|
+
// override it to a few ms to assert the chain never wedges on a hung step.
|
|
47
|
+
cleanupStepTimeoutMs?: number
|
|
44
48
|
socket?: string
|
|
45
49
|
// When provided, the daemon honors `restart` RPCs by invoking this with the
|
|
46
50
|
// (containerName, cwd) it captured at register time. Omit to disable the
|
|
@@ -130,6 +134,14 @@ const DEFAULT_GC_MISSES_TO_DEREGISTER = 3
|
|
|
130
134
|
const MAX_REQUEST_BUFFER_BYTES = 64 * 1024
|
|
131
135
|
const MAX_HTTP_REQUEST_BYTES = 64 * 1024
|
|
132
136
|
|
|
137
|
+
// Upper bound on any single teardown step (portbroker/renewal stop) run inside
|
|
138
|
+
// the per-container runSerially chain. A teardown dependency must never own a
|
|
139
|
+
// container's serial queue forever — that wedges every later register/deregister
|
|
140
|
+
// for that container (the renewal-restart wedge). Larger than tailscale's 30s
|
|
141
|
+
// per-call budget so a slow-but-progressing serve-off isn't cut off, yet finite
|
|
142
|
+
// so a genuine hang releases the queue instead of poisoning it.
|
|
143
|
+
const CLEANUP_STEP_TIMEOUT_MS = 60_000
|
|
144
|
+
|
|
133
145
|
// Preferred port for the HTTP control surface. Adjacent to CONTAINER_PORT
|
|
134
146
|
// (8973) for mnemonics. Stability matters: containers cache the URL in
|
|
135
147
|
// TYPECLAW_HOSTD_URL at `docker run` time, so a respawn that picks a fresh
|
|
@@ -223,6 +235,21 @@ function stringifyError(error: unknown): string {
|
|
|
223
235
|
return error instanceof Error ? error.message : String(error)
|
|
224
236
|
}
|
|
225
237
|
|
|
238
|
+
// Races a teardown step against a wall clock so a hung dependency can't hold the
|
|
239
|
+
// per-container serial chain open forever. Resolves (never rejects) on timeout
|
|
240
|
+
// so the surrounding runSerially op always settles and releases the queue.
|
|
241
|
+
async function withCleanupTimeout(work: Promise<unknown>, timeoutMs: number): Promise<void> {
|
|
242
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
243
|
+
const timeout = new Promise<void>((resolve) => {
|
|
244
|
+
timer = setTimeout(resolve, timeoutMs)
|
|
245
|
+
})
|
|
246
|
+
try {
|
|
247
|
+
await Promise.race([work.catch(() => {}), timeout])
|
|
248
|
+
} finally {
|
|
249
|
+
if (timer) clearTimeout(timer)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
226
253
|
function errorCode(error: Error): unknown {
|
|
227
254
|
const direct = error as Error & { code?: unknown; cause?: unknown }
|
|
228
255
|
if (direct.code !== undefined) return direct.code
|
|
@@ -282,6 +309,7 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
282
309
|
const exec = opts.exec ?? defaultDockerExec
|
|
283
310
|
const gcIntervalMs = opts.gcIntervalMs ?? DEFAULT_GC_INTERVAL_MS
|
|
284
311
|
const gcMissesToDeregister = opts.gcMissesToDeregister ?? DEFAULT_GC_MISSES_TO_DEREGISTER
|
|
312
|
+
const cleanupStepTimeoutMs = opts.cleanupStepTimeoutMs ?? CLEANUP_STEP_TIMEOUT_MS
|
|
285
313
|
const version = opts.version ?? UNVERSIONED_SENTINEL
|
|
286
314
|
const cwds = new Map<string, string>()
|
|
287
315
|
const restartTokens = new Map<string, string>()
|
|
@@ -362,9 +390,10 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
362
390
|
cwds.delete(containerName)
|
|
363
391
|
restartTokens.delete(containerName)
|
|
364
392
|
gcMisses.delete(containerName)
|
|
365
|
-
if (opts.portbroker)
|
|
366
|
-
|
|
367
|
-
if (opts.
|
|
393
|
+
if (opts.portbroker)
|
|
394
|
+
await withCleanupTimeout(opts.portbroker.stop(containerName, 'fatal-auth'), cleanupStepTimeoutMs)
|
|
395
|
+
if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(containerName), cleanupStepTimeoutMs)
|
|
396
|
+
if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(containerName), cleanupStepTimeoutMs)
|
|
368
397
|
await removeRegistrationFile(containerName)
|
|
369
398
|
log({ kind: 'registration-skipped', containerName, reason: `fatal broker auth: ${reason}` })
|
|
370
399
|
})
|
|
@@ -430,9 +459,10 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
430
459
|
restartTokens.delete(req.containerName)
|
|
431
460
|
brokerTokens.delete(req.containerName)
|
|
432
461
|
gcMisses.delete(req.containerName)
|
|
433
|
-
if (opts.portbroker)
|
|
434
|
-
|
|
435
|
-
if (opts.
|
|
462
|
+
if (opts.portbroker)
|
|
463
|
+
await withCleanupTimeout(opts.portbroker.stop(req.containerName, 'deregistered'), cleanupStepTimeoutMs)
|
|
464
|
+
if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(req.containerName), cleanupStepTimeoutMs)
|
|
465
|
+
if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(req.containerName), cleanupStepTimeoutMs)
|
|
436
466
|
await removeRegistrationFile(req.containerName)
|
|
437
467
|
if (hadCwd) log({ kind: 'deregister', containerName: req.containerName, reason: 'requested' })
|
|
438
468
|
return { ok: true }
|
|
@@ -759,9 +789,9 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
759
789
|
void runSerially(name, async () => {
|
|
760
790
|
const hadCwd = cwds.delete(name)
|
|
761
791
|
restartTokens.delete(name)
|
|
762
|
-
if (opts.portbroker) await opts.portbroker.stop(name, 'deregistered')
|
|
763
|
-
if (opts.kakaoRenewal) await opts.kakaoRenewal.stop(name)
|
|
764
|
-
if (opts.webexRenewal) await opts.webexRenewal.stop(name)
|
|
792
|
+
if (opts.portbroker) await withCleanupTimeout(opts.portbroker.stop(name, 'deregistered'), cleanupStepTimeoutMs)
|
|
793
|
+
if (opts.kakaoRenewal) await withCleanupTimeout(opts.kakaoRenewal.stop(name), cleanupStepTimeoutMs)
|
|
794
|
+
if (opts.webexRenewal) await withCleanupTimeout(opts.webexRenewal.stop(name), cleanupStepTimeoutMs)
|
|
765
795
|
await removeRegistrationFile(name)
|
|
766
796
|
if (hadCwd) log({ kind: 'deregister', containerName: name, reason: 'gone' })
|
|
767
797
|
return { ok: true }
|
|
@@ -787,17 +817,23 @@ export async function startDaemon(opts: DaemonOptions = {}): Promise<Daemon> {
|
|
|
787
817
|
// after the teardown loop below already ran, leaking it. Let restore
|
|
788
818
|
// settle (it never rejects past the detached catch) before tearing down.
|
|
789
819
|
await (restoreComplete ?? Promise.resolve()).catch(() => {})
|
|
820
|
+
// Bound shutdown teardown too: a wedged broker/renewal stop must not
|
|
821
|
+
// block daemon.stop() forever, which would also stall the version-drift
|
|
822
|
+
// respawn flow that waits for this to return before unlinking the socket.
|
|
790
823
|
if (opts.portbroker) {
|
|
791
824
|
const names = Array.from(cwds.keys())
|
|
792
|
-
await
|
|
825
|
+
await withCleanupTimeout(
|
|
826
|
+
Promise.allSettled(names.map((n) => opts.portbroker!.stop(n, 'broker-stopped'))),
|
|
827
|
+
cleanupStepTimeoutMs,
|
|
828
|
+
)
|
|
793
829
|
}
|
|
794
830
|
if (opts.kakaoRenewal) {
|
|
795
831
|
const names = Array.from(cwds.keys())
|
|
796
|
-
await Promise.allSettled(names.map((n) => opts.kakaoRenewal!.stop(n)))
|
|
832
|
+
await withCleanupTimeout(Promise.allSettled(names.map((n) => opts.kakaoRenewal!.stop(n))), cleanupStepTimeoutMs)
|
|
797
833
|
}
|
|
798
834
|
if (opts.webexRenewal) {
|
|
799
835
|
const names = Array.from(cwds.keys())
|
|
800
|
-
await Promise.allSettled(names.map((n) => opts.webexRenewal!.stop(n)))
|
|
836
|
+
await withCleanupTimeout(Promise.allSettled(names.map((n) => opts.webexRenewal!.stop(n))), cleanupStepTimeoutMs)
|
|
801
837
|
}
|
|
802
838
|
cwds.clear()
|
|
803
839
|
restartTokens.clear()
|
|
@@ -104,7 +104,14 @@ export function createKakaoRenewalManager(opts: KakaoRenewalManagerOptions = {})
|
|
|
104
104
|
accountId: result.account_id,
|
|
105
105
|
previousUpdatedAt: result.previousUpdatedAt,
|
|
106
106
|
})
|
|
107
|
-
|
|
107
|
+
// A tick that started before stop()/drain() (deregister, shutdown) or
|
|
108
|
+
// before a re-register with a different cwd can finish the slow login
|
|
109
|
+
// here. Restarting a container that is no longer the current
|
|
110
|
+
// registration would resurrect a just-deregistered agent or fight a
|
|
111
|
+
// shutdown, so skip onRenewalOk unless this container+cwd is still the
|
|
112
|
+
// live registration. Matches webex-renewal-manager's guard.
|
|
113
|
+
const current = latestInput.get(input.containerName)
|
|
114
|
+
if (opts.onRenewalOk && current?.cwd === input.cwd) {
|
|
108
115
|
log({
|
|
109
116
|
kind: 'kakao-renewal-restart-scheduled',
|
|
110
117
|
containerName: input.containerName,
|
|
@@ -187,9 +194,21 @@ export function createKakaoRenewalManager(opts: KakaoRenewalManagerOptions = {})
|
|
|
187
194
|
|
|
188
195
|
return {
|
|
189
196
|
start(input: KakaoRenewalStartInput): void {
|
|
190
|
-
|
|
197
|
+
// Tear down any prior registration for this container BEFORE the
|
|
198
|
+
// shouldRenew gate. The daemon calls start() on every registration, so a
|
|
199
|
+
// container that had Kakao can re-register to a non-Kakao cwd; if we
|
|
200
|
+
// returned early without clearing, the old timer would keep ticking and a
|
|
201
|
+
// post-login restart guard would still match the stale cwd. Clearing
|
|
202
|
+
// latestInput also makes an in-flight tick's onRenewalOk re-check fail.
|
|
191
203
|
const existing = timers.get(input.containerName)
|
|
192
|
-
if (existing)
|
|
204
|
+
if (existing) {
|
|
205
|
+
existing.stop()
|
|
206
|
+
timers.delete(input.containerName)
|
|
207
|
+
}
|
|
208
|
+
latestInput.delete(input.containerName)
|
|
209
|
+
|
|
210
|
+
if (opts.shouldRenew && !opts.shouldRenew(input)) return
|
|
211
|
+
|
|
193
212
|
latestInput.set(input.containerName, input)
|
|
194
213
|
const handle = schedule(() => {
|
|
195
214
|
void scheduleTick(input.containerName)
|
|
@@ -198,26 +217,32 @@ export function createKakaoRenewalManager(opts: KakaoRenewalManagerOptions = {})
|
|
|
198
217
|
void scheduleTick(input.containerName)
|
|
199
218
|
},
|
|
200
219
|
|
|
201
|
-
|
|
220
|
+
// Must NOT await the in-flight tick: a successful tick runs onRenewalOk →
|
|
221
|
+
// hostdRestart → container stop() → deregister RPC → handleDeregister →
|
|
222
|
+
// kakaoRenewal.stop(), so awaiting inFlight here waits on the very tick that
|
|
223
|
+
// is parked waiting for this restart — a self-deadlock that wedges the
|
|
224
|
+
// daemon's per-container serial chain forever. Clearing latestInput (not the
|
|
225
|
+
// await) is what fails the tick's onRenewalOk guard; drain() still awaits.
|
|
226
|
+
stop(containerName: string): Promise<void> {
|
|
202
227
|
const handle = timers.get(containerName)
|
|
203
228
|
if (handle) {
|
|
204
229
|
timers.delete(containerName)
|
|
205
230
|
handle.stop()
|
|
206
231
|
}
|
|
207
232
|
latestInput.delete(containerName)
|
|
208
|
-
|
|
209
|
-
// "stop returned → no work outstanding". Daemon shutdown depends on
|
|
210
|
-
// this; without it, a mid-tick `attemptLogin` HTTPS call is abandoned.
|
|
211
|
-
const promise = inFlight.get(containerName)
|
|
212
|
-
if (promise) await promise.catch(() => {})
|
|
233
|
+
return Promise.resolve()
|
|
213
234
|
},
|
|
214
235
|
|
|
215
236
|
async drain(): Promise<void> {
|
|
216
237
|
for (const [, handle] of timers) handle.stop()
|
|
217
238
|
timers.clear()
|
|
218
|
-
latestInput
|
|
239
|
+
// Clear latestInput AFTER awaiting in-flight: the onRenewalOk guard reads
|
|
240
|
+
// latestInput, so clearing first would suppress the restart of a tick that
|
|
241
|
+
// is mid-login when drain() runs. drain() is shutdown — it waits for that
|
|
242
|
+
// work to finish rather than dropping it.
|
|
219
243
|
const promises = Array.from(inFlight.values())
|
|
220
244
|
await Promise.allSettled(promises)
|
|
245
|
+
latestInput.clear()
|
|
221
246
|
},
|
|
222
247
|
}
|
|
223
248
|
}
|
package/src/hostd/tailscale.ts
CHANGED
|
@@ -36,6 +36,17 @@ type TailscaleStatus = {
|
|
|
36
36
|
const MACOS_APP_CLI = '/Applications/Tailscale.app/Contents/MacOS/Tailscale'
|
|
37
37
|
const WINDOWS_CLI = 'C:\\Program Files\\Tailscale\\tailscale.exe'
|
|
38
38
|
|
|
39
|
+
// Per-CLI-call wall clock. The tailscaled "serve config" is a global resource
|
|
40
|
+
// guarded by an etag; concurrent agent restarts racing `serve`/`serve off`
|
|
41
|
+
// surface "Another client is changing the serve config" and the losing CLI can
|
|
42
|
+
// block on the lock indefinitely. An unbounded `await proc.exited` then poisons
|
|
43
|
+
// stopAll() → portbroker.stop() → the daemon's per-container serial chain (the
|
|
44
|
+
// renewal-restart wedge). 30s is far longer than a healthy serve call yet bounds
|
|
45
|
+
// the hang. Kept below the daemon teardown budget so a timed-out call surfaces
|
|
46
|
+
// as a normal tailscale-serve-failed before the teardown wrapper trips.
|
|
47
|
+
const TAILSCALE_CLI_TIMEOUT_MS = 30_000
|
|
48
|
+
const TAILSCALE_TIMEOUT_EXIT_CODE = 124
|
|
49
|
+
|
|
39
50
|
export function createTailscaleServeManager(opts: TailscaleServeManagerOptions): TailscaleServeManager {
|
|
40
51
|
const exec = opts.exec ?? defaultTailscaleExec
|
|
41
52
|
const log = opts.onLog ?? (() => {})
|
|
@@ -153,7 +164,11 @@ export const defaultTailscaleExec: TailscaleExec = async (args) => {
|
|
|
153
164
|
return { exitCode: 127, stdout: '', stderr: lastError }
|
|
154
165
|
}
|
|
155
166
|
|
|
156
|
-
async function runTailscale(
|
|
167
|
+
export async function runTailscale(
|
|
168
|
+
bin: string,
|
|
169
|
+
args: string[],
|
|
170
|
+
timeoutMs: number = TAILSCALE_CLI_TIMEOUT_MS,
|
|
171
|
+
): Promise<TailscaleExecResult> {
|
|
157
172
|
const bun = getBun()
|
|
158
173
|
if (!bun) return { exitCode: 127, stdout: '', stderr: 'bun runtime not available' }
|
|
159
174
|
try {
|
|
@@ -163,10 +178,31 @@ async function runTailscale(bin: string, args: string[]): Promise<TailscaleExecR
|
|
|
163
178
|
stderr: 'pipe',
|
|
164
179
|
env: bin === MACOS_APP_CLI ? { ...process.env, TAILSCALE_BE_CLI: '1' } : process.env,
|
|
165
180
|
})
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
181
|
+
// Drain both pipes concurrently with the exit wait. Reading them only after
|
|
182
|
+
// `proc.exited` can deadlock: a child that fills its stdout/stderr pipe
|
|
183
|
+
// buffer blocks on write while we block on exit, so neither side advances.
|
|
184
|
+
const stdoutPromise = new Response(proc.stdout).text().catch(() => '')
|
|
185
|
+
const stderrPromise = new Response(proc.stderr).text().catch(() => '')
|
|
186
|
+
|
|
187
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
188
|
+
const timedOut = new Promise<'timeout'>((resolve) => {
|
|
189
|
+
timer = setTimeout(() => resolve('timeout'), timeoutMs)
|
|
190
|
+
})
|
|
191
|
+
const outcome = await Promise.race([proc.exited.then(() => 'exited' as const), timedOut])
|
|
192
|
+
if (timer) clearTimeout(timer)
|
|
193
|
+
|
|
194
|
+
if (outcome === 'timeout') {
|
|
195
|
+
try {
|
|
196
|
+
proc.kill()
|
|
197
|
+
} catch {}
|
|
198
|
+
return {
|
|
199
|
+
exitCode: TAILSCALE_TIMEOUT_EXIT_CODE,
|
|
200
|
+
stdout: '',
|
|
201
|
+
stderr: `tailscale ${args.join(' ')} timed out after ${timeoutMs}ms`,
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { exitCode: await proc.exited, stdout: await stdoutPromise, stderr: await stderrPromise }
|
|
170
206
|
} catch (error) {
|
|
171
207
|
const code = typeof error === 'object' && error !== null && 'code' in error ? String(error.code) : ''
|
|
172
208
|
const exitCode = code === 'ENOENT' ? 127 : 1
|
|
@@ -194,23 +194,32 @@ export function createWebexRenewalManager(opts: WebexRenewalManagerOptions = {})
|
|
|
194
194
|
void scheduleTick(input.containerName)
|
|
195
195
|
},
|
|
196
196
|
|
|
197
|
-
|
|
197
|
+
// Must NOT await the in-flight tick: a successful tick runs onRenewalOk →
|
|
198
|
+
// hostdRestart → container stop() → deregister RPC → handleDeregister →
|
|
199
|
+
// webexRenewal.stop(), so awaiting inFlight here waits on the very tick that
|
|
200
|
+
// is parked waiting for this restart — a self-deadlock that wedges the
|
|
201
|
+
// daemon's per-container serial chain forever. Clearing latestInput (not the
|
|
202
|
+
// await) is what fails the tick's onRenewalOk guard; drain() still awaits.
|
|
203
|
+
stop(containerName: string): Promise<void> {
|
|
198
204
|
const handle = timers.get(containerName)
|
|
199
205
|
if (handle) {
|
|
200
206
|
timers.delete(containerName)
|
|
201
207
|
handle.stop()
|
|
202
208
|
}
|
|
203
209
|
latestInput.delete(containerName)
|
|
204
|
-
|
|
205
|
-
if (promise) await promise.catch(() => {})
|
|
210
|
+
return Promise.resolve()
|
|
206
211
|
},
|
|
207
212
|
|
|
208
213
|
async drain(): Promise<void> {
|
|
209
214
|
for (const [, handle] of timers) handle.stop()
|
|
210
215
|
timers.clear()
|
|
211
|
-
latestInput
|
|
216
|
+
// Clear latestInput AFTER awaiting in-flight: the onRenewalOk guard reads
|
|
217
|
+
// latestInput, so clearing first would suppress the restart of a tick that
|
|
218
|
+
// is mid-login when drain() runs. drain() is shutdown — it waits for that
|
|
219
|
+
// work to finish rather than dropping it.
|
|
212
220
|
const promises = Array.from(inFlight.values())
|
|
213
221
|
await Promise.allSettled(promises)
|
|
222
|
+
latestInput.clear()
|
|
214
223
|
},
|
|
215
224
|
}
|
|
216
225
|
}
|
package/src/plugin/index.ts
CHANGED
package/src/plugin/manager.ts
CHANGED
|
@@ -51,6 +51,7 @@ export type LoadPluginsResult = {
|
|
|
51
51
|
failedPlugins: FailedPlugin[]
|
|
52
52
|
markBooted: () => void
|
|
53
53
|
setSpawnSubagent: (fn: SpawnSubagentFn) => void
|
|
54
|
+
disposePlugins: () => Promise<void>
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
export async function loadPlugins(opts: LoadPluginsOptions): Promise<LoadPluginsResult> {
|
|
@@ -181,9 +182,22 @@ export async function loadPlugins(opts: LoadPluginsOptions): Promise<LoadPlugins
|
|
|
181
182
|
setSpawnSubagent: (fn) => {
|
|
182
183
|
spawnSubagentImpl = fn
|
|
183
184
|
},
|
|
185
|
+
disposePlugins: () => disposePlugins(registry),
|
|
184
186
|
}
|
|
185
187
|
}
|
|
186
188
|
|
|
189
|
+
async function disposePlugins(registry: PluginRegistry): Promise<void> {
|
|
190
|
+
await Promise.all(
|
|
191
|
+
registry.disposers.map(async (disposer) => {
|
|
192
|
+
try {
|
|
193
|
+
await disposer.dispose()
|
|
194
|
+
} catch (err) {
|
|
195
|
+
disposer.logger.warn(`onDispose failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
196
|
+
}
|
|
197
|
+
}),
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
187
201
|
// Tags WHICH sub-phase failed (for the failedPlugins report) while preserving
|
|
188
202
|
// the ORIGINAL error so the caller can keep PluginSecurityError fatal even when
|
|
189
203
|
// it surfaces deep inside registration.
|
package/src/plugin/registry.ts
CHANGED
|
@@ -34,6 +34,11 @@ export type RegisteredCommand = {
|
|
|
34
34
|
command: PluginCommand
|
|
35
35
|
logger: PluginLogger
|
|
36
36
|
}
|
|
37
|
+
export type RegisteredPluginDisposer = {
|
|
38
|
+
pluginName: string
|
|
39
|
+
logger: PluginLogger
|
|
40
|
+
dispose: () => void | Promise<void>
|
|
41
|
+
}
|
|
37
42
|
|
|
38
43
|
export type PluginRegistry = {
|
|
39
44
|
tools: RegisteredTool[]
|
|
@@ -43,6 +48,7 @@ export type PluginRegistry = {
|
|
|
43
48
|
skillsDirs: RegisteredSkillDir[]
|
|
44
49
|
doctorChecks: RegisteredDoctorCheck[]
|
|
45
50
|
commands: RegisteredCommand[]
|
|
51
|
+
disposers: RegisteredPluginDisposer[]
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
export type RegisterContributionsOptions = {
|
|
@@ -159,6 +165,10 @@ export function registerContributions(opts: RegisterContributionsOptions): void
|
|
|
159
165
|
registry.commands.push({ pluginName, commandName, command, logger })
|
|
160
166
|
}
|
|
161
167
|
}
|
|
168
|
+
|
|
169
|
+
if (ex.onDispose) {
|
|
170
|
+
registry.disposers.push({ pluginName, logger, dispose: ex.onDispose })
|
|
171
|
+
}
|
|
162
172
|
}
|
|
163
173
|
|
|
164
174
|
export function discardRegistrationsBy(pluginName: string, registry: PluginRegistry, hooks: HookBus): void {
|
|
@@ -169,6 +179,7 @@ export function discardRegistrationsBy(pluginName: string, registry: PluginRegis
|
|
|
169
179
|
registry.skillsDirs = registry.skillsDirs.filter((d) => d.pluginName !== pluginName)
|
|
170
180
|
registry.doctorChecks = registry.doctorChecks.filter((d) => d.pluginName !== pluginName)
|
|
171
181
|
registry.commands = registry.commands.filter((c) => c.pluginName !== pluginName)
|
|
182
|
+
registry.disposers = registry.disposers.filter((d) => d.pluginName !== pluginName)
|
|
172
183
|
hooks.unregisterAll(pluginName)
|
|
173
184
|
}
|
|
174
185
|
|
|
@@ -181,6 +192,7 @@ export function emptyRegistry(): PluginRegistry {
|
|
|
181
192
|
skillsDirs: [],
|
|
182
193
|
doctorChecks: [],
|
|
183
194
|
commands: [],
|
|
195
|
+
disposers: [],
|
|
184
196
|
}
|
|
185
197
|
}
|
|
186
198
|
|
package/src/plugin/types.ts
CHANGED
|
@@ -294,6 +294,10 @@ export type PluginExports = {
|
|
|
294
294
|
skillsDirs?: string[]
|
|
295
295
|
hooks?: Hooks
|
|
296
296
|
doctorChecks?: Record<string, PluginDoctorCheck>
|
|
297
|
+
// Released once when the agent stops, for plugin-lifetime resources a GC can't
|
|
298
|
+
// reclaim deterministically (open DB handles, timers). Disposers are isolated:
|
|
299
|
+
// one throwing never blocks the others.
|
|
300
|
+
onDispose?: () => void | Promise<void>
|
|
297
301
|
}
|
|
298
302
|
|
|
299
303
|
// `typeclaw doctor` plugin extension surface. Each check is read-only by
|
package/src/run/index.ts
CHANGED
|
@@ -125,24 +125,32 @@ export type StartAgentResult = {
|
|
|
125
125
|
stop: () => void | Promise<void>
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
// process-wide listeners
|
|
131
|
-
//
|
|
132
|
-
//
|
|
133
|
-
// strand
|
|
134
|
-
//
|
|
128
|
+
// Owns boot-failure cleanup for everything installed before `startAgentRuntime`
|
|
129
|
+
// returns a `StartAgentResult`. `installCodexFetchObserver`/`installFatalGuard`
|
|
130
|
+
// attach process-wide listeners, and `loadPlugins` opens plugin-lifetime
|
|
131
|
+
// resources (the memory plugin's sqlite handle); if any boot step throws before
|
|
132
|
+
// the result exists, the caller never receives `stop()`, so each of those would
|
|
133
|
+
// strand. The runtime registers each cleanup via `registerBootCleanup` as boot
|
|
134
|
+
// progresses; on a throw we run them all (newest first, best-effort) and
|
|
135
135
|
// rethrow, on success ownership transfers to the returned `stop()`. `return
|
|
136
136
|
// await` is load-bearing — without it an async boot rejection would escape this
|
|
137
|
-
// try.
|
|
137
|
+
// try. Cleanups are idempotent, so the success path's `stop()` never double-fires.
|
|
138
138
|
export async function startAgent(options: StartAgentOptions): Promise<StartAgentResult> {
|
|
139
|
-
const
|
|
139
|
+
const bootCleanups: Array<() => void | Promise<void>> = []
|
|
140
140
|
try {
|
|
141
|
-
return await startAgentRuntime(options, (
|
|
142
|
-
|
|
141
|
+
return await startAgentRuntime(options, (cleanup) => {
|
|
142
|
+
bootCleanups.push(cleanup)
|
|
143
143
|
})
|
|
144
144
|
} catch (err) {
|
|
145
|
-
|
|
145
|
+
for (const cleanup of bootCleanups.reverse()) {
|
|
146
|
+
try {
|
|
147
|
+
await cleanup()
|
|
148
|
+
} catch (cleanupErr) {
|
|
149
|
+
console.warn(
|
|
150
|
+
`[run] boot-failure cleanup error: ${cleanupErr instanceof Error ? cleanupErr.message : cleanupErr}`,
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
146
154
|
throw err
|
|
147
155
|
}
|
|
148
156
|
}
|
|
@@ -161,7 +169,7 @@ async function startAgentRuntime(
|
|
|
161
169
|
createChannelManager: createChannelManagerFor = createChannelManager,
|
|
162
170
|
createTunnelManager: createTunnelManagerFor = createTunnelManager,
|
|
163
171
|
}: StartAgentOptions,
|
|
164
|
-
|
|
172
|
+
registerBootCleanup: (cleanup: () => void | Promise<void>) => void,
|
|
165
173
|
): Promise<StartAgentResult> {
|
|
166
174
|
const reloadRegistry = new ReloadRegistry()
|
|
167
175
|
|
|
@@ -216,7 +224,7 @@ async function startAgentRuntime(
|
|
|
216
224
|
if (sigtermHandler !== null) process.removeListener('SIGTERM', sigtermHandler)
|
|
217
225
|
}
|
|
218
226
|
let sigtermHandler: (() => void) | null = null
|
|
219
|
-
|
|
227
|
+
registerBootCleanup(disposeProcessGlobals)
|
|
220
228
|
|
|
221
229
|
const { config: cwdConfig, pluginConfigs: pluginConfigsByName } = loadConfigBundleSync(cwd)
|
|
222
230
|
const githubTokenBridge = createGithubTokenBridge()
|
|
@@ -261,6 +269,9 @@ async function startAgentRuntime(
|
|
|
261
269
|
if (mcpManager !== null) await mcpManager.closeAll()
|
|
262
270
|
throw err
|
|
263
271
|
}
|
|
272
|
+
// Plugins are loaded (sqlite handles open); from here any boot failure must
|
|
273
|
+
// release them — the caller gets no stop() on the failure path.
|
|
274
|
+
registerBootCleanup(() => pluginsLoaded.disposePlugins())
|
|
264
275
|
|
|
265
276
|
reloadRegistry.register(
|
|
266
277
|
createConfigReloadable({
|
|
@@ -555,6 +566,7 @@ async function startAgentRuntime(
|
|
|
555
566
|
return name
|
|
556
567
|
},
|
|
557
568
|
})
|
|
569
|
+
registerBootCleanup(() => subagentConsumer.stop())
|
|
558
570
|
subagentConsumer.start()
|
|
559
571
|
|
|
560
572
|
// Populated by startScheduler's factory (onCountStore). The consumer
|
|
@@ -693,11 +705,13 @@ async function startAgentRuntime(
|
|
|
693
705
|
// delivers only to live subscribers (no replay), so a fire published before
|
|
694
706
|
// the subscription exists would be lost. Subscribing to an empty stream is
|
|
695
707
|
// harmless when there are no jobs.
|
|
708
|
+
registerBootCleanup(() => cronConsumer.stop())
|
|
696
709
|
cronConsumer.start()
|
|
697
710
|
const scheduler = await startScheduler({
|
|
698
711
|
cwd,
|
|
699
712
|
loadCron,
|
|
700
713
|
createSchedulerFor: factory,
|
|
714
|
+
registerBootCleanup,
|
|
701
715
|
stream,
|
|
702
716
|
hasInternalJobs: internalJobs().length > 0,
|
|
703
717
|
getSubagents: () => pluginRuntime.get().subagents,
|
|
@@ -713,6 +727,7 @@ async function startAgentRuntime(
|
|
|
713
727
|
}
|
|
714
728
|
|
|
715
729
|
const tunnelBridge: TunnelBridge = createTunnelBridge({ stream, channelManager })
|
|
730
|
+
registerBootCleanup(() => tunnelBridge.stop())
|
|
716
731
|
|
|
717
732
|
// Bridge `subagent.completed` broadcasts into the channel router so a
|
|
718
733
|
// backgrounded subagent finishing wakes up its parent channel session
|
|
@@ -723,6 +738,7 @@ async function startAgentRuntime(
|
|
|
723
738
|
stream,
|
|
724
739
|
router: channelManager.router,
|
|
725
740
|
})
|
|
741
|
+
registerBootCleanup(() => subagentCompletionBridge.stop())
|
|
726
742
|
|
|
727
743
|
// Fan a landed formal review verdict out to the sibling sessions reviewing the
|
|
728
744
|
// same PR so they stand down from a redundant verdict (the per-thread fan-out
|
|
@@ -733,12 +749,14 @@ async function startAgentRuntime(
|
|
|
733
749
|
stream,
|
|
734
750
|
router: channelManager.router,
|
|
735
751
|
})
|
|
752
|
+
registerBootCleanup(() => prVerdictActivityBridge.stop())
|
|
736
753
|
setReviewObserver((review) => {
|
|
737
754
|
stream.publish({
|
|
738
755
|
target: { kind: 'broadcast' },
|
|
739
756
|
payload: { kind: 'pr.verdict-activity', ...review },
|
|
740
757
|
})
|
|
741
758
|
})
|
|
759
|
+
registerBootCleanup(() => setReviewObserver(null))
|
|
742
760
|
|
|
743
761
|
// Registered before channels so its cache clear lands before any channel
|
|
744
762
|
// session teardown observes it. secrets.json provider credentials are not
|
|
@@ -773,6 +791,7 @@ async function startAgentRuntime(
|
|
|
773
791
|
console.warn(`[run] channel restart-resume reserve failed: ${err instanceof Error ? err.message : err}`)
|
|
774
792
|
}
|
|
775
793
|
|
|
794
|
+
registerBootCleanup(() => channelManager.stop())
|
|
776
795
|
await channelManager.start()
|
|
777
796
|
|
|
778
797
|
if (restartReservation !== null) {
|
|
@@ -903,7 +922,7 @@ async function startAgentRuntime(
|
|
|
903
922
|
...mcpManagerOpt,
|
|
904
923
|
})
|
|
905
924
|
|
|
906
|
-
const
|
|
925
|
+
const serverFactory = createServer({
|
|
907
926
|
port,
|
|
908
927
|
reloadAll: () => reloadRegistry.reloadAll(),
|
|
909
928
|
reloadRegistry,
|
|
@@ -924,23 +943,26 @@ async function startAgentRuntime(
|
|
|
924
943
|
...runtimeVersionOpt,
|
|
925
944
|
...tuiTokenOpt,
|
|
926
945
|
...containerBrokerOpt,
|
|
927
|
-
})
|
|
946
|
+
})
|
|
947
|
+
let server: BunServer | null = null
|
|
948
|
+
registerBootCleanup(() => server?.stop(true))
|
|
949
|
+
server = serverFactory.start()
|
|
928
950
|
|
|
929
951
|
// Tunnel manager starts AFTER the WS server is up so a slow/hanging
|
|
930
952
|
// provider (PR 2's cloudflared first-URL wait) cannot block TUI, reload,
|
|
931
953
|
// or channel adapter availability. External providers resolve URLs
|
|
932
954
|
// synchronously; future managed providers will resolve asynchronously
|
|
933
955
|
// and broadcast URL events when ready.
|
|
956
|
+
registerBootCleanup(() => tunnelManager.stop())
|
|
934
957
|
await tunnelManager.start()
|
|
935
958
|
|
|
936
959
|
let stopped = false
|
|
937
960
|
const stop = async () => {
|
|
938
961
|
if (stopped) return
|
|
939
962
|
stopped = true
|
|
940
|
-
// The
|
|
941
|
-
//
|
|
942
|
-
// the
|
|
943
|
-
// listener would survive into the next `startAgent` and double-fire.
|
|
963
|
+
// The final disposers run in `finally` so an earlier async teardown
|
|
964
|
+
// rejection cannot strand process-global listeners or plugin-owned handles
|
|
965
|
+
// into the next `startAgent`.
|
|
944
966
|
try {
|
|
945
967
|
scheduler?.stop()
|
|
946
968
|
cronConsumer.stop()
|
|
@@ -955,6 +977,7 @@ async function startAgentRuntime(
|
|
|
955
977
|
await channelManager.stop()
|
|
956
978
|
await mcpManager?.closeAll()
|
|
957
979
|
} finally {
|
|
980
|
+
await pluginsLoaded.disposePlugins()
|
|
958
981
|
disposeProcessGlobals()
|
|
959
982
|
}
|
|
960
983
|
}
|
|
@@ -1079,6 +1102,7 @@ async function startScheduler({
|
|
|
1079
1102
|
cwd,
|
|
1080
1103
|
loadCron,
|
|
1081
1104
|
createSchedulerFor,
|
|
1105
|
+
registerBootCleanup,
|
|
1082
1106
|
stream,
|
|
1083
1107
|
hasInternalJobs,
|
|
1084
1108
|
getSubagents,
|
|
@@ -1087,6 +1111,7 @@ async function startScheduler({
|
|
|
1087
1111
|
cwd: string
|
|
1088
1112
|
loadCron: LoadCronFn
|
|
1089
1113
|
createSchedulerFor: SchedulerFactory
|
|
1114
|
+
registerBootCleanup: (cleanup: () => void | Promise<void>) => void
|
|
1090
1115
|
stream: Stream
|
|
1091
1116
|
hasInternalJobs: boolean
|
|
1092
1117
|
getSubagents?: () => SubagentRegistry
|
|
@@ -1111,6 +1136,7 @@ async function startScheduler({
|
|
|
1111
1136
|
stream.publish({ target: { kind: 'cron', jobId: job.id }, payload: job })
|
|
1112
1137
|
}
|
|
1113
1138
|
const scheduler = await createSchedulerFor({ cwd, file, onFire, onCountStore })
|
|
1139
|
+
registerBootCleanup(() => scheduler.stop())
|
|
1114
1140
|
scheduler.start()
|
|
1115
1141
|
return scheduler
|
|
1116
1142
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { rm } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
// `Bun.gc(true)` is load-bearing, not suppression: bun:sqlite holds the DB file
|
|
4
|
+
// handle open past `db.close()` until the Statement wrappers are finalized (Bun
|
|
5
|
+
// #25964), and on Windows that open handle pins the dir so `rm` throws EBUSY.
|
|
6
|
+
// The forced GC runs those finalizers. Bun ignores `fs.rm`'s maxRetries, so the
|
|
7
|
+
// loop covers the brief window a sibling worker / just-exited subprocess needs.
|
|
8
|
+
const RETRIES = 10
|
|
9
|
+
const RETRY_DELAY_MS = 50
|
|
10
|
+
const RETRYABLE = new Set(['EBUSY', 'EPERM', 'ENOTEMPTY'])
|
|
11
|
+
|
|
12
|
+
export async function rmTempDir(dir: string): Promise<void> {
|
|
13
|
+
for (let attempt = 0; attempt <= RETRIES; attempt++) {
|
|
14
|
+
Bun.gc(true)
|
|
15
|
+
try {
|
|
16
|
+
await rm(dir, { recursive: true, force: true })
|
|
17
|
+
return
|
|
18
|
+
} catch (err) {
|
|
19
|
+
const code = (err as NodeJS.ErrnoException).code
|
|
20
|
+
if (attempt === RETRIES || code === undefined || !RETRYABLE.has(code)) throw err
|
|
21
|
+
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS))
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|