typeclaw 0.39.7 → 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.
Files changed (72) hide show
  1. package/package.json +1 -1
  2. package/scripts/dump-system-prompt.ts +4 -50
  3. package/src/agent/auth-reloadable.ts +24 -0
  4. package/src/agent/auth.ts +12 -0
  5. package/src/agent/index.ts +18 -58
  6. package/src/agent/multimodal/look-at.ts +87 -0
  7. package/src/bundled-plugins/guard/policies/non-workspace-write.ts +0 -2
  8. package/src/bundled-plugins/guard/policy.ts +0 -5
  9. package/src/bundled-plugins/memory/README.md +26 -37
  10. package/src/bundled-plugins/memory/index.ts +32 -147
  11. package/src/bundled-plugins/memory/load-memory.ts +7 -121
  12. package/src/bundled-plugins/memory/memory-logger.ts +8 -2
  13. package/src/bundled-plugins/memory/search-command.ts +2 -11
  14. package/src/bundled-plugins/memory/vector/doctor.ts +1 -1
  15. package/src/bundled-plugins/memory/vector/embedder.ts +4 -5
  16. package/src/bundled-plugins/memory/vector/hybrid.ts +47 -5
  17. package/src/bundled-plugins/memory/vector/index-on-write.ts +25 -20
  18. package/src/bundled-plugins/memory/vector/reference-index-on-write.ts +28 -23
  19. package/src/bundled-plugins/memory/vector/relevance-gate.ts +11 -7
  20. package/src/bundled-plugins/memory/vector/script.ts +87 -0
  21. package/src/bundled-plugins/memory/vector/store.ts +2 -0
  22. package/src/bundled-plugins/planner/planner.ts +4 -2
  23. package/src/bundled-plugins/reviewer/reviewer.ts +9 -1
  24. package/src/channels/adapters/describe-error.ts +40 -0
  25. package/src/channels/adapters/discord.ts +9 -12
  26. package/src/channels/adapters/kakaotalk-classify.ts +31 -6
  27. package/src/channels/adapters/kakaotalk.ts +15 -18
  28. package/src/channels/adapters/line.ts +11 -14
  29. package/src/channels/adapters/slack-bot.ts +15 -18
  30. package/src/channels/adapters/slack.ts +45 -21
  31. package/src/channels/adapters/telegram-bot.ts +15 -18
  32. package/src/channels/adapters/webex-bot.ts +13 -14
  33. package/src/channels/adapters/webex.ts +14 -15
  34. package/src/cli/compose.ts +20 -0
  35. package/src/cli/docker-preflight.ts +42 -0
  36. package/src/cli/hostd.ts +12 -3
  37. package/src/cli/init.ts +24 -22
  38. package/src/cli/logs.ts +7 -0
  39. package/src/cli/provider.ts +8 -1
  40. package/src/cli/reload.ts +6 -0
  41. package/src/cli/restart.ts +7 -0
  42. package/src/cli/shell.ts +7 -0
  43. package/src/cli/start.ts +7 -0
  44. package/src/cli/status.ts +9 -0
  45. package/src/cli/stop.ts +7 -0
  46. package/src/cli/tui.ts +6 -0
  47. package/src/config/config.ts +31 -8
  48. package/src/container/docker-app.ts +253 -0
  49. package/src/container/index.ts +11 -0
  50. package/src/container/start.ts +85 -41
  51. package/src/hostd/current-host-daemon.ts +21 -0
  52. package/src/hostd/daemon.ts +75 -15
  53. package/src/hostd/kakao-renewal-manager.ts +35 -10
  54. package/src/hostd/supervisor.ts +18 -3
  55. package/src/hostd/tailscale.ts +41 -5
  56. package/src/hostd/webex-renewal-manager.ts +13 -4
  57. package/src/plugin/hooks.ts +3 -4
  58. package/src/plugin/index.ts +1 -0
  59. package/src/plugin/manager.ts +14 -0
  60. package/src/plugin/registry.ts +12 -0
  61. package/src/plugin/types.ts +5 -1
  62. package/src/run/channel-session-factory.ts +3 -5
  63. package/src/run/index.ts +63 -36
  64. package/src/server/command-runner.ts +2 -11
  65. package/src/skills/typeclaw-memory/SKILL.md +2 -2
  66. package/src/skills/typeclaw-plugins/SKILL.md +1 -1
  67. package/src/test-helpers/rm-temp-dir.ts +24 -0
  68. package/typeclaw.schema.json +1 -23
  69. package/src/bundled-plugins/guard/policies/memory-retrieval-cache-write.ts +0 -43
  70. package/src/bundled-plugins/memory/memory-retrieval.ts +0 -111
  71. package/src/bundled-plugins/memory/vector/cache-write.ts +0 -19
  72. package/src/bundled-plugins/memory/vector/config.ts +0 -28
@@ -1,43 +0,0 @@
1
- import path from 'node:path'
2
-
3
- import type { SessionOrigin } from '@/agent/session-origin'
4
- import type { SecuritySeverity } from '@/bundled-plugins/security/permissions'
5
-
6
- export const GUARD_MEMORY_RETRIEVAL_CACHE_WRITE = 'memoryRetrievalCacheWrite'
7
- export const GUARD_MEMORY_RETRIEVAL_CACHE_WRITE_SEVERITY: SecuritySeverity = 'low'
8
-
9
- const SESSION_ID_REGEX = /^[A-Za-z0-9._-]{1,128}$/
10
-
11
- export async function isMemoryRetrievalCacheWriteAllowed(options: {
12
- tool: string
13
- args: Record<string, unknown>
14
- agentDir: string
15
- origin?: SessionOrigin
16
- }): Promise<boolean> {
17
- const { tool, args, agentDir, origin } = options
18
- if (tool !== 'write') return false
19
- // Allow the memory-retrieval subagent (existing path) OR the in-process
20
- // vector retrieval writer (system origin with component='memory-retrieval').
21
- // The in-process path runs under the session origin, not a subagent, so we
22
- // check for the system component name as the trusted internal actor.
23
- const isMemoryRetrievalSubagent = origin?.kind === 'subagent' && origin.subagent === 'memory-retrieval'
24
- const isInProcessWriter = origin?.kind === 'system' && origin.component === 'memory-retrieval'
25
- if (!isMemoryRetrievalSubagent && !isInProcessWriter) return false
26
-
27
- const rawPath = args.path
28
- if (typeof rawPath !== 'string') return false
29
-
30
- const targetPath = path.resolve(agentDir, rawPath)
31
- const expectedDir = path.resolve(agentDir, 'memory', '.retrieval-cache')
32
- const relative = path.relative(expectedDir, targetPath)
33
- if (relative === '' || relative.startsWith('..') || path.isAbsolute(relative)) return false
34
-
35
- const parts = relative.split(path.sep).filter(Boolean)
36
- if (parts.length !== 1) return false
37
-
38
- const fileName = parts[0]!
39
- if (!fileName.endsWith('.md')) return false
40
-
41
- const sessionId = fileName.slice(0, -3)
42
- return SESSION_ID_REGEX.test(sessionId)
43
- }
@@ -1,111 +0,0 @@
1
- import { z } from 'zod'
2
-
3
- import { lsTool, readTool, type Subagent, writeTool } from '@/plugin'
4
-
5
- import { createMemorySearchTool } from './search-tool'
6
-
7
- export const memoryRetrievalPayloadSchema = z.object({
8
- parentSessionId: z.string().min(1),
9
- agentDir: z.string().min(1),
10
- recentPrompt: z.string(),
11
- cacheFilePath: z.string().min(1),
12
- origin: z.unknown().optional(),
13
- })
14
-
15
- export type MemoryRetrievalPayload = z.infer<typeof memoryRetrievalPayloadSchema>
16
-
17
- export function isMemoryRetrievalPayload(value: unknown): value is MemoryRetrievalPayload {
18
- return memoryRetrievalPayloadSchema.safeParse(value).success
19
- }
20
-
21
- export type MemoryRetrievalLogger = {
22
- info: (msg: string) => void
23
- warn: (msg: string) => void
24
- error: (msg: string) => void
25
- }
26
-
27
- export type CreateMemoryRetrievalSubagentOptions = {
28
- logger?: MemoryRetrievalLogger
29
- timeoutMs?: number
30
- }
31
-
32
- export const MEMORY_RETRIEVAL_SYSTEM_PROMPT = `You are the memory-retrieval subagent. Read the user's most recent prompt and decide what's relevant across topic shards in \`memory/topics/\` (consolidated long-term memory), references in \`memory/references/\` (verbatim artifacts), AND undreamed daily-stream events under \`memory/streams/\` (recent fragments not yet folded into shards). Use \`memory_search\` to query all three surfaces; use \`read\`/\`ls\` to pull full shard bodies when needed. Synthesize a focused ≤8 KB summary of the relevant memory. Save by \`write\`ing it to the exact path provided in your payload as \`cacheFilePath\`. Be ruthlessly concise. Do NOT write anywhere else. Do NOT delete files.
33
-
34
- Search discipline: issue ALL your \`memory_search\` queries in a SINGLE response as parallel tool calls (up to 3 at once), then wait for every result before deciding what to do next. Different angles in parallel, NEVER one search per turn — sequential searches waste a full LLM round-trip per query (~3s each) on file I/O that takes milliseconds. Pick queries that match the user's literal phrasing — not framing vocabulary, not metadata (session ids, dates), not words from your own system prompt. If the parallel batch turns up nothing relevant, write the empty-context note and stop.`
35
-
36
- export function memoryRetrievalExhaustedMessage(used: number, max: number): string {
37
- const usedKb = Math.round(used / 1024)
38
- const maxKb = Math.round(max / 1024)
39
- return [
40
- `[memory-retrieval budget exhausted: used ${usedKb}KB of ${maxKb}KB across memory_search and read]`,
41
- '',
42
- 'Stop searching. Stop reading. Every subsequent memory_search or read call will return this same notice.',
43
- 'Write the cache file at the provided cacheFilePath with whatever relevant memory you have already gathered.',
44
- 'If nothing was relevant, write a short empty-context note to the cache file and stop.',
45
- ].join('\n')
46
- }
47
-
48
- const consoleLogger: MemoryRetrievalLogger = {
49
- info: (m) => console.warn(m),
50
- warn: (m) => console.warn(m),
51
- error: (m) => console.error(m),
52
- }
53
-
54
- export function createMemoryRetrievalSubagent(
55
- options: CreateMemoryRetrievalSubagentOptions = {},
56
- ): Subagent<MemoryRetrievalPayload> {
57
- const logger = options.logger ?? consoleLogger
58
- return {
59
- systemPrompt: MEMORY_RETRIEVAL_SYSTEM_PROMPT,
60
- // Retrieval is "4 keyword searches + 1 write" — no reasoning required.
61
- // `fast` falls back to `default` (with a one-time warning) when the
62
- // operator hasn't configured it, so this is safe by construction.
63
- profile: 'fast',
64
- tools: [readTool, writeTool, lsTool],
65
- customTools: [createMemorySearchTool()],
66
- payloadSchema: memoryRetrievalPayloadSchema,
67
- inFlightKey: (payload) => payload.parentSessionId,
68
- ...(options.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
69
- // 256 KB read + memory_search budget. Sized for one retrieval pass:
70
- // ~16 KB of memory_search hits (3 queries × ~5 KB excerpts) plus a few
71
- // shard reads (~5 KB each). A smaller budget would systematically
72
- // exhaust on any agent with rich memory; a larger budget invites the
73
- // pre-fix failure mode where the LLM kept iterating searches until it
74
- // gave up. The exhausted-message tells the subagent to write the
75
- // cache file with what it has rather than retrying forever.
76
- toolResultBudget: {
77
- maxTotalBytes: 256 * 1024,
78
- toolNames: ['read', 'memory_search'],
79
- exhaustedMessage: memoryRetrievalExhaustedMessage,
80
- },
81
- handler: async (ctx, runSession) => {
82
- const start = Date.now()
83
- logger.info(`[memory-retrieval] ${ctx.payload.parentSessionId} start cache=${ctx.payload.cacheFilePath}`)
84
- try {
85
- await runSession({ userPrompt: buildInitialPrompt(ctx.payload) })
86
- logger.info(`[memory-retrieval] ${ctx.payload.parentSessionId} done elapsed_ms=${Date.now() - start}`)
87
- } catch (err) {
88
- const message = err instanceof Error ? err.message : String(err)
89
- logger.warn(
90
- `[memory-retrieval] ${ctx.payload.parentSessionId}: run threw: ${message} elapsed_ms=${Date.now() - start}`,
91
- )
92
- throw err
93
- }
94
- },
95
- }
96
- }
97
-
98
- function buildInitialPrompt(payload: MemoryRetrievalPayload): string {
99
- return [
100
- `Parent session: ${payload.parentSessionId}`,
101
- `Agent folder: ${payload.agentDir}`,
102
- `Recent user prompt: ${payload.recentPrompt}`,
103
- `Topic shard directory: memory/topics/`,
104
- `Daily-stream directory: memory/streams/`,
105
- `Cache output path: ${payload.cacheFilePath}`,
106
- '',
107
- 'Use `memory_search` to find relevant material across BOTH topic shards and undreamed stream events (results are discriminated by `source: "topic" | "stream"`). Read any shard whose body you need in full via `read`. Write one concise retrieval summary to the cache output path exactly as provided. Keep the file ≤8 KB. If nothing is relevant, write a short empty-context note to the cache output path. Do not write any other path.',
108
- ].join('\n')
109
- }
110
-
111
- export const memoryRetrievalSubagent: Subagent<MemoryRetrievalPayload> = createMemoryRetrievalSubagent()
@@ -1,19 +0,0 @@
1
- import { mkdir, writeFile, rename } from 'node:fs/promises'
2
- import { join } from 'node:path'
3
-
4
- /**
5
- * Atomically write retrieval cache to memory/.retrieval-cache/<sessionId>.md
6
- * using tmp+rename pattern for crash safety.
7
- */
8
- export async function writeRetrievalCache(agentDir: string, sessionId: string, content: string): Promise<void> {
9
- const cacheDir = join(agentDir, 'memory', '.retrieval-cache')
10
- const cachePath = join(cacheDir, `${sessionId}.md`)
11
- const tmpPath = `${cachePath}.tmp`
12
-
13
- // Create directory if it doesn't exist
14
- await mkdir(cacheDir, { recursive: true })
15
-
16
- // Write to tmp file, then atomically rename
17
- await writeFile(tmpPath, content, 'utf8')
18
- await rename(tmpPath, cachePath)
19
- }
@@ -1,28 +0,0 @@
1
- import { z } from 'zod'
2
-
3
- import { loadPluginConfigsSync } from '@/config'
4
-
5
- export const vectorConfigSchema = z
6
- .object({
7
- enabled: z.boolean().default(false),
8
- })
9
- .default({ enabled: false })
10
-
11
- export type VectorConfig = z.infer<typeof vectorConfigSchema>
12
-
13
- // Fails closed to `false`: a memory block we can't parse is treated as opted
14
- // out. Shared by the host-side download gate and the runtime's per-turn vs
15
- // system-prompt memory-injection decision, so both read the flag identically.
16
- export function vectorEnabledFromMemoryConfig(memory: unknown): boolean {
17
- if (typeof memory !== 'object' || memory === null) return false
18
- const parsed = vectorConfigSchema.safeParse((memory as Record<string, unknown>).vector)
19
- return parsed.success && parsed.data.enabled
20
- }
21
-
22
- export function agentUsesVector(cwd: string): boolean {
23
- try {
24
- return vectorEnabledFromMemoryConfig(loadPluginConfigsSync(cwd).memory)
25
- } catch {
26
- return false
27
- }
28
- }