typeclaw 0.39.6 → 0.40.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/scripts/dump-system-prompt.ts +4 -50
- package/src/agent/auth-reloadable.ts +24 -0
- package/src/agent/auth.ts +12 -0
- package/src/agent/index.ts +18 -58
- package/src/agent/todo/continuation-policy.ts +8 -0
- package/src/agent/todo/continuation-state.ts +41 -7
- package/src/agent/todo/continuation-wiring.ts +26 -0
- package/src/bundled-plugins/guard/policies/non-workspace-write.ts +0 -2
- package/src/bundled-plugins/guard/policy.ts +0 -5
- package/src/bundled-plugins/memory/README.md +26 -37
- package/src/bundled-plugins/memory/index.ts +32 -144
- package/src/bundled-plugins/memory/load-memory.ts +7 -121
- package/src/bundled-plugins/memory/memory-logger.ts +12 -6
- package/src/bundled-plugins/memory/search-command.ts +124 -0
- package/src/bundled-plugins/memory/vector/doctor.ts +1 -1
- package/src/bundled-plugins/memory/vector/embedder.ts +4 -5
- package/src/bundled-plugins/memory/vector/hybrid.ts +47 -5
- package/src/bundled-plugins/memory/vector/relevance-gate.ts +11 -7
- package/src/bundled-plugins/memory/vector/script.ts +87 -0
- package/src/channels/adapters/describe-error.ts +40 -0
- package/src/channels/adapters/discord.ts +9 -12
- package/src/channels/adapters/kakaotalk.ts +14 -17
- package/src/channels/adapters/line.ts +11 -14
- package/src/channels/adapters/slack-bot.ts +15 -18
- package/src/channels/adapters/slack.ts +45 -21
- package/src/channels/adapters/telegram-bot.ts +15 -18
- package/src/channels/adapters/webex-bot.ts +13 -14
- package/src/channels/adapters/webex.ts +14 -15
- package/src/channels/router.ts +60 -1
- package/src/cli/command-meta.ts +7 -16
- package/src/cli/compose.ts +21 -1
- package/src/cli/cron.ts +1 -1
- package/src/cli/docker-preflight.ts +42 -0
- package/src/cli/doctor.ts +1 -1
- package/src/cli/dreams.ts +1 -1
- package/src/cli/hostd.ts +12 -3
- package/src/cli/index.ts +1 -1
- package/src/cli/init.ts +24 -22
- package/src/cli/inspect.ts +1 -1
- package/src/cli/logs.ts +7 -0
- package/src/cli/memory.ts +85 -0
- package/src/cli/model.ts +1 -1
- package/src/cli/provider.ts +8 -1
- package/src/cli/reload.ts +6 -0
- package/src/cli/restart.ts +7 -0
- package/src/cli/shell.ts +7 -0
- package/src/cli/start.ts +7 -0
- package/src/cli/status.ts +9 -0
- package/src/cli/stop.ts +7 -0
- package/src/cli/tui.ts +6 -0
- package/src/config/config.ts +31 -8
- package/src/container/docker-app.ts +253 -0
- package/src/container/index.ts +11 -0
- package/src/container/start.ts +85 -41
- package/src/hostd/current-host-daemon.ts +21 -0
- package/src/hostd/daemon.ts +27 -3
- package/src/hostd/supervisor.ts +18 -3
- package/src/plugin/hooks.ts +3 -4
- package/src/plugin/types.ts +1 -1
- package/src/role-claim/client.ts +6 -1
- package/src/run/channel-session-factory.ts +3 -5
- package/src/run/index.ts +50 -17
- package/src/server/command-runner.ts +2 -11
- package/src/server/index.ts +8 -0
- package/src/skills/typeclaw-memory/SKILL.md +2 -2
- package/src/skills/typeclaw-plugins/SKILL.md +1 -1
- package/typeclaw.schema.json +1 -23
- package/src/bundled-plugins/guard/policies/memory-retrieval-cache-write.ts +0 -43
- package/src/bundled-plugins/memory/memory-retrieval.ts +0 -111
- package/src/bundled-plugins/memory/vector/cache-write.ts +0 -19
- package/src/bundled-plugins/memory/vector/config.ts +0 -28
package/src/plugin/types.ts
CHANGED
|
@@ -183,7 +183,7 @@ export type SessionTurnStartEvent = {
|
|
|
183
183
|
userPrompt: string
|
|
184
184
|
origin?: SessionOrigin
|
|
185
185
|
// Mutable ref: plugin writes retrieval results here; server/router reads after hook returns.
|
|
186
|
-
//
|
|
186
|
+
// Populated when a turn driver supplies a retrieval context bag.
|
|
187
187
|
retrievalContext?: { results: string }
|
|
188
188
|
}
|
|
189
189
|
|
package/src/role-claim/client.ts
CHANGED
|
@@ -13,6 +13,10 @@ export type ClaimSessionOptions = {
|
|
|
13
13
|
role: string
|
|
14
14
|
channel?: string
|
|
15
15
|
ttlMs?: number
|
|
16
|
+
// Defaults to connectTimeoutMs. Split out so a loaded host can't let the
|
|
17
|
+
// open (transport) phase exhaust the budget meant for the connected phase
|
|
18
|
+
// and surface the wrong timeout error. See waitForOpen/waitForConnected.
|
|
19
|
+
openTimeoutMs?: number
|
|
16
20
|
connectTimeoutMs?: number
|
|
17
21
|
onStarted?: (payload: ClaimStartedPayload) => void
|
|
18
22
|
}
|
|
@@ -28,11 +32,12 @@ const DEFAULT_CONNECT_TIMEOUT_MS = 30_000
|
|
|
28
32
|
export async function runClaimSession(opts: ClaimSessionOptions): Promise<ClaimSessionResult> {
|
|
29
33
|
const ttlMs = opts.ttlMs ?? DEFAULT_TTL_MS
|
|
30
34
|
const connectTimeoutMs = opts.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS
|
|
35
|
+
const openTimeoutMs = opts.openTimeoutMs ?? connectTimeoutMs
|
|
31
36
|
const code = generateClaimCode()
|
|
32
37
|
|
|
33
38
|
const ws = new WebSocket(opts.url)
|
|
34
39
|
const displayUrl = redactUrl(opts.url)
|
|
35
|
-
await waitForOpen(ws, displayUrl,
|
|
40
|
+
await waitForOpen(ws, displayUrl, openTimeoutMs)
|
|
36
41
|
await waitForConnected(ws, displayUrl, connectTimeoutMs)
|
|
37
42
|
|
|
38
43
|
try {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
|
|
1
3
|
import { SessionManager } from '@mariozechner/pi-coding-agent'
|
|
2
4
|
|
|
3
5
|
import { createSession as defaultCreateSession } from '@/agent'
|
|
@@ -71,9 +73,6 @@ export type BuildChannelSessionFactoryDeps = {
|
|
|
71
73
|
subagentRegistry?: SubagentRegistry
|
|
72
74
|
getCreateSessionForSubagent?: () => CreateSessionForSubagent
|
|
73
75
|
liveSessionRegistry?: LiveSessionRegistry
|
|
74
|
-
// Forwarded to createSession: when true the `# Memory` section is omitted
|
|
75
|
-
// from the system prompt (vector agents inject memory per-turn instead).
|
|
76
|
-
suppressSystemMemory?: boolean
|
|
77
76
|
}
|
|
78
77
|
|
|
79
78
|
// Tight basename validation so a tampered or corrupt channels/sessions.json
|
|
@@ -141,7 +140,6 @@ export function buildChannelSessionFactory(deps: BuildChannelSessionFactoryDeps)
|
|
|
141
140
|
...(deps.getCreateSessionForSubagent !== undefined
|
|
142
141
|
? { createSessionForSubagent: deps.getCreateSessionForSubagent() }
|
|
143
142
|
: {}),
|
|
144
|
-
...(deps.suppressSystemMemory !== undefined ? { suppressSystemMemory: deps.suppressSystemMemory } : {}),
|
|
145
143
|
})
|
|
146
144
|
|
|
147
145
|
const sessionId = sessionManager.getSessionId()
|
|
@@ -192,7 +190,7 @@ function tryReopenOrCreate(
|
|
|
192
190
|
)
|
|
193
191
|
return SessionManager.create(cwd, sessionDir)
|
|
194
192
|
}
|
|
195
|
-
const path =
|
|
193
|
+
const path = join(sessionDir, existingSessionFile)
|
|
196
194
|
if (capOptions !== null) {
|
|
197
195
|
try {
|
|
198
196
|
const stats = capJsonlFileInPlace(path, capOptions)
|
package/src/run/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SessionManager } from '@mariozechner/pi-coding-agent'
|
|
2
2
|
|
|
3
3
|
import { createSession, createSessionWithDispose } from '@/agent'
|
|
4
|
+
import { createProviderAuthReloadable } from '@/agent/auth-reloadable'
|
|
4
5
|
import { LiveSessionRegistry } from '@/agent/live-sessions'
|
|
5
6
|
import { LiveSubagentRegistry } from '@/agent/live-subagents'
|
|
6
7
|
import { requestContainerRestart } from '@/agent/restart'
|
|
@@ -19,8 +20,7 @@ import {
|
|
|
19
20
|
type SubagentRegistry,
|
|
20
21
|
type SubagentShared,
|
|
21
22
|
} from '@/agent/subagents'
|
|
22
|
-
import { clearTodosForOrigin } from '@/agent/todo/continuation-wiring'
|
|
23
|
-
import { vectorEnabledFromMemoryConfig } from '@/bundled-plugins/memory/vector/config'
|
|
23
|
+
import { clearTodosForOrigin, markRestartAbortPendingForOrigin } from '@/agent/todo/continuation-wiring'
|
|
24
24
|
import { embed, warmEmbedder } from '@/bundled-plugins/memory/vector/embedder'
|
|
25
25
|
import { buildStartupVectorIndex } from '@/bundled-plugins/memory/vector/startup'
|
|
26
26
|
import { resolveCapOptionsFromConfig } from '@/bundled-plugins/tool-result-cap'
|
|
@@ -211,14 +211,14 @@ async function startAgentRuntime(
|
|
|
211
211
|
processGlobalsDisposed = true
|
|
212
212
|
uninstallCodexFetchObserver()
|
|
213
213
|
fatalGuard.dispose()
|
|
214
|
+
// onSigterm is defined later in this scope but only ever invoked after init;
|
|
215
|
+
// removing it here keeps a restarted startAgent() from stacking listeners.
|
|
216
|
+
if (sigtermHandler !== null) process.removeListener('SIGTERM', sigtermHandler)
|
|
214
217
|
}
|
|
218
|
+
let sigtermHandler: (() => void) | null = null
|
|
215
219
|
onProcessGlobalsInstalled(disposeProcessGlobals)
|
|
216
220
|
|
|
217
221
|
const { config: cwdConfig, pluginConfigs: pluginConfigsByName } = loadConfigBundleSync(cwd)
|
|
218
|
-
// Vector agents omit the system-prompt `# Memory` section and inject memory
|
|
219
|
-
// per-turn instead. Derived once here: `memory.vector.enabled` is
|
|
220
|
-
// restart-required, so a single boot read is coherent for the process.
|
|
221
|
-
const suppressSystemMemory = vectorEnabledFromMemoryConfig(pluginConfigsByName.memory)
|
|
222
222
|
const githubTokenBridge = createGithubTokenBridge()
|
|
223
223
|
const mcpManager =
|
|
224
224
|
cwdConfig.mcpServers.length > 0 ? createMcpManager(cwdConfig.mcpServers, { env: process.env }) : null
|
|
@@ -252,7 +252,7 @@ async function startAgentRuntime(
|
|
|
252
252
|
hasGithubAppTokenResolver: githubTokenBridge.hasAppTokenResolver,
|
|
253
253
|
...(cwdConfig.roles !== undefined ? { roles: cwdConfig.roles } : {}),
|
|
254
254
|
})
|
|
255
|
-
const vectorStartupPromise =
|
|
255
|
+
const vectorStartupPromise = runVectorStartup(cwd)
|
|
256
256
|
let pluginsLoaded: LoadPluginsResult
|
|
257
257
|
try {
|
|
258
258
|
const [loaded] = await Promise.all([pluginsLoadedPromise, vectorStartupPromise])
|
|
@@ -364,7 +364,6 @@ async function startAgentRuntime(
|
|
|
364
364
|
stream,
|
|
365
365
|
reloadRegistry,
|
|
366
366
|
pluginRuntime,
|
|
367
|
-
suppressSystemMemory,
|
|
368
367
|
getChannelRouter: () => channelManager.router,
|
|
369
368
|
rehydrateCapOptions: resolveCapOptionsFromConfig(pluginConfigsByName['tool-result-cap']),
|
|
370
369
|
permissions: pluginsLoaded.permissions,
|
|
@@ -608,7 +607,6 @@ async function startAgentRuntime(
|
|
|
608
607
|
containerName: containerNameOpt.containerName,
|
|
609
608
|
sessionFactory,
|
|
610
609
|
channelRouter: channelManager.router,
|
|
611
|
-
suppressSystemMemory,
|
|
612
610
|
...mcpManagerOpt,
|
|
613
611
|
}),
|
|
614
612
|
subagent: (subName: string, payload?: unknown) =>
|
|
@@ -649,7 +647,6 @@ async function startAgentRuntime(
|
|
|
649
647
|
channelRouter: channelManager.router,
|
|
650
648
|
origin: cronOrigin,
|
|
651
649
|
permissions: pluginsLoaded.permissions,
|
|
652
|
-
suppressSystemMemory,
|
|
653
650
|
...(refOverride !== undefined ? { refOverride } : {}),
|
|
654
651
|
...(snap.hasAnyPluginContent
|
|
655
652
|
? {
|
|
@@ -743,6 +740,17 @@ async function startAgentRuntime(
|
|
|
743
740
|
})
|
|
744
741
|
})
|
|
745
742
|
|
|
743
|
+
// Registered before channels so its cache clear lands before any channel
|
|
744
|
+
// session teardown observes it. secrets.json provider credentials are not
|
|
745
|
+
// part of the typeclaw.json config diff, so a rotated key takes effect on
|
|
746
|
+
// `typeclaw reload` only via this dedicated scope. Live sessions captured
|
|
747
|
+
// their AuthStorage at creation, so teardown recreates them with fresh auth.
|
|
748
|
+
reloadRegistry.register(
|
|
749
|
+
createProviderAuthReloadable({
|
|
750
|
+
onProviderAuthChanged: () => channelManager.router.tearDownAllLive(),
|
|
751
|
+
}),
|
|
752
|
+
)
|
|
753
|
+
|
|
746
754
|
reloadRegistry.register(createChannelsReloadable({ manager: channelManager }))
|
|
747
755
|
|
|
748
756
|
// Two-phase channel restart-resume around adapter startup, to close the race
|
|
@@ -782,12 +790,10 @@ async function startAgentRuntime(
|
|
|
782
790
|
// In-flight coalescing for direct ctx.spawnSubagent calls mirrors the
|
|
783
791
|
// SubagentConsumer's stream-path gate (subagents.ts:441). Two queued
|
|
784
792
|
// `new-session` messages for the same (name, inFlightKey) drop the second
|
|
785
|
-
// on the consumer side; without the same gate here, two consecutive
|
|
786
|
-
//
|
|
787
|
-
//
|
|
788
|
-
//
|
|
789
|
-
// spawn in the hook used to mask this; now that the hook is fire-and-forget,
|
|
790
|
-
// the race is exposed and the gate is mandatory.
|
|
793
|
+
// on the consumer side; without the same gate here, two consecutive direct
|
|
794
|
+
// plugin spawns with the same in-flight key could race. Awaiting the spawn in
|
|
795
|
+
// the hook used to mask this; detached hook paths expose the race and make the
|
|
796
|
+
// gate mandatory.
|
|
791
797
|
//
|
|
792
798
|
// Same key shape as the consumer: `${name}:${inFlightKey(payload)}` when the
|
|
793
799
|
// subagent declares one, else just `${name}`. Collisions resolve cleanly
|
|
@@ -894,7 +900,6 @@ async function startAgentRuntime(
|
|
|
894
900
|
outbound,
|
|
895
901
|
sessionFactory,
|
|
896
902
|
channelRouter: channelManager.router,
|
|
897
|
-
suppressSystemMemory,
|
|
898
903
|
...mcpManagerOpt,
|
|
899
904
|
})
|
|
900
905
|
|
|
@@ -954,6 +959,34 @@ async function startAgentRuntime(
|
|
|
954
959
|
}
|
|
955
960
|
}
|
|
956
961
|
|
|
962
|
+
// Graceful shutdown on host-initiated restart (`typeclaw restart` → docker
|
|
963
|
+
// stop → SIGTERM, ~10s before SIGKILL). Mark every live session's todo scope
|
|
964
|
+
// so the turn this restart aborts does not arm the durable user-abort block —
|
|
965
|
+
// each scope's incomplete todos then auto-continue after the new container
|
|
966
|
+
// boots. Then run best-effort teardown. A hard deadline force-exits well
|
|
967
|
+
// inside Docker's grace window so a hung turn can't get the process SIGKILL'd
|
|
968
|
+
// mid-flush. Only meaningful inside a container; outside Docker SIGTERM is an
|
|
969
|
+
// ordinary stop with nothing to resume.
|
|
970
|
+
const SHUTDOWN_DEADLINE_MS = 8_000
|
|
971
|
+
let shuttingDown = false
|
|
972
|
+
const onSigterm = (): void => {
|
|
973
|
+
if (shuttingDown) return
|
|
974
|
+
shuttingDown = true
|
|
975
|
+
const forceExit = setTimeout(() => process.exit(0), SHUTDOWN_DEADLINE_MS)
|
|
976
|
+
forceExit.unref()
|
|
977
|
+
void (async () => {
|
|
978
|
+
if (containerName !== undefined) {
|
|
979
|
+
await markRestartAbortPendingForOrigin(cwd, { kind: 'tui', sessionId: 'tui' }).catch(() => undefined)
|
|
980
|
+
await channelManager.router.markRestartAbortForAllLive().catch(() => undefined)
|
|
981
|
+
}
|
|
982
|
+
await stop().catch(() => undefined)
|
|
983
|
+
clearTimeout(forceExit)
|
|
984
|
+
process.exit(0)
|
|
985
|
+
})()
|
|
986
|
+
}
|
|
987
|
+
sigtermHandler = onSigterm
|
|
988
|
+
process.on('SIGTERM', onSigterm)
|
|
989
|
+
|
|
957
990
|
if (!attachTui) {
|
|
958
991
|
return {
|
|
959
992
|
server,
|
|
@@ -58,10 +58,6 @@ export type CommandRunnerOptions = {
|
|
|
58
58
|
// wire for the handler/command path.
|
|
59
59
|
channelRouter: ChannelRouter | undefined
|
|
60
60
|
mcpManager?: McpManager
|
|
61
|
-
// When true, prompt sessions spawned here omit the system-prompt `# Memory`
|
|
62
|
-
// section (vector agents inject memory per-turn). Forwarded to createSession
|
|
63
|
-
// so command/handler sessions stay coherent with the rest of the runtime.
|
|
64
|
-
suppressSystemMemory?: boolean
|
|
65
61
|
}
|
|
66
62
|
|
|
67
63
|
type CommandHandle = {
|
|
@@ -201,7 +197,6 @@ export function createCommandRunner(opts: CommandRunnerOptions): CommandRunner {
|
|
|
201
197
|
signal: abortController.signal,
|
|
202
198
|
sessionFactory: opts.sessionFactory,
|
|
203
199
|
channelRouter: opts.channelRouter,
|
|
204
|
-
...(opts.suppressSystemMemory !== undefined ? { suppressSystemMemory: opts.suppressSystemMemory } : {}),
|
|
205
200
|
...(opts.mcpManager !== undefined ? { mcpManager: opts.mcpManager } : {}),
|
|
206
201
|
}),
|
|
207
202
|
subagent: (subName, payload) =>
|
|
@@ -388,7 +383,6 @@ export async function runPromptForCommand(args: {
|
|
|
388
383
|
// so the spawned session exposes `channel_send`.
|
|
389
384
|
channelRouter?: ChannelRouter
|
|
390
385
|
mcpManager?: McpManager
|
|
391
|
-
suppressSystemMemory?: boolean
|
|
392
386
|
// Test seam for the agent-session boundary. Production passes the real
|
|
393
387
|
// `createSessionWithDispose`; tests inject a fake to verify wiring
|
|
394
388
|
// (specifically: the sessionManager handed off must be persisted, not
|
|
@@ -418,14 +412,11 @@ export async function runPromptForCommand(args: {
|
|
|
418
412
|
...(args.mcpManager !== undefined ? { mcpManager: args.mcpManager } : {}),
|
|
419
413
|
...(args.runtimeVersion !== undefined ? { runtimeVersion: args.runtimeVersion } : {}),
|
|
420
414
|
...(args.containerName !== undefined ? { containerName: args.containerName } : {}),
|
|
421
|
-
...(args.suppressSystemMemory !== undefined ? { suppressSystemMemory: args.suppressSystemMemory } : {}),
|
|
422
415
|
})
|
|
423
416
|
const detachAbort = bindSignalToSession(args.signal, session)
|
|
424
417
|
// Mirror the other turn drivers (TUI/channel/cron/subagent): fire
|
|
425
|
-
// session.turn.start with a retrievalContext so
|
|
426
|
-
//
|
|
427
|
-
// injected per-turn into the user prompt here too. Without this, command and
|
|
428
|
-
// handler prompt sessions would have no memory at all under vector mode.
|
|
418
|
+
// session.turn.start with a retrievalContext so vector memory is injected
|
|
419
|
+
// per-turn into command and handler prompts too.
|
|
429
420
|
const turnEvent = { sessionId, agentDir: args.agentDir, origin: args.origin }
|
|
430
421
|
const retrievalContext = { results: '' }
|
|
431
422
|
try {
|
package/src/server/index.ts
CHANGED
|
@@ -25,6 +25,7 @@ import type { CreateSessionForSubagent } from '@/agent/subagents'
|
|
|
25
25
|
import { TODO_CONTINUATION_SOURCE } from '@/agent/todo/continuation'
|
|
26
26
|
import {
|
|
27
27
|
armRestartKickForOrigin,
|
|
28
|
+
clearAbortSuppressionForOrigin,
|
|
28
29
|
extractTurnUsage,
|
|
29
30
|
recordTurnOutcome,
|
|
30
31
|
recordTurnStart,
|
|
@@ -620,6 +621,13 @@ export function createServer({
|
|
|
620
621
|
await armRestartKickForOrigin(agentDir, origin).catch((err) =>
|
|
621
622
|
logger.error(`[server] ${sessionFileId}: arm restart-kick suppression failed: ${describeErr(err)}`),
|
|
622
623
|
)
|
|
624
|
+
// The restart may have aborted an in-flight turn, arming the
|
|
625
|
+
// durable user-abort block. This resume IS the restart, so
|
|
626
|
+
// clear it — otherwise the resumed session never auto-continues
|
|
627
|
+
// its incomplete todos. Gated to this restart-handoff path.
|
|
628
|
+
await clearAbortSuppressionForOrigin(agentDir, origin).catch((err) =>
|
|
629
|
+
logger.error(`[server] ${sessionFileId}: clear abort suppression failed: ${describeErr(err)}`),
|
|
630
|
+
)
|
|
623
631
|
}
|
|
624
632
|
stream.publish({
|
|
625
633
|
target: { kind: 'session', sessionId: sessionFileId },
|
|
@@ -9,9 +9,9 @@ The agent's long-term memory is sharded across files in `memory/topics/<slug>.md
|
|
|
9
9
|
|
|
10
10
|
## Reading
|
|
11
11
|
|
|
12
|
-
The `# Memory`
|
|
12
|
+
The `# Memory` block is injected per turn into the user prompt; the system prompt never contains long-term memory; vector memory is always on. Undreamed daily-stream events are **not** dumped wholesale into prompts — use `memory_search` when you need them. When speaking in a channel, shard bodies are dropped and only headings/slugs show; call `memory_search` to fetch the bodies you need. The same `memory_search` covers both surfaces (topic shards and undreamed stream events), so one tool call reaches everything.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Under budget you get shard bodies de-duplicated across turns; over budget you get the top-K shards/fragments/references most relevant to the current message (hybrid vector + keyword search). This keeps the system-prompt cache prefix stable across a session and lets retrieval track the current topic instead of a stale session-start snapshot. `memory_search` remains available on demand.
|
|
15
15
|
|
|
16
16
|
## Writing
|
|
17
17
|
|
|
@@ -380,7 +380,7 @@ hooks: {
|
|
|
380
380
|
|
|
381
381
|
The `prompt` field on `SessionPromptEvent` is the system prompt as it's being composed by `createResourceLoader` (`basePrompt + IDENTITY.md + SOUL.md`), NOT the user's most recent message. Reading it as if it were the user's prompt — and feeding it to a retrieval system, classifier, or LLM — will keyword-mine TypeClaw's framing prose (`TypeClaw`, `subagent`, `AGENTS.md`) on every session.
|
|
382
382
|
|
|
383
|
-
If you want the **user's actual prompt** (their message text), subscribe to `session.turn.start` and read `event.userPrompt`. The bundled memory plugin's
|
|
383
|
+
If you want the **user's actual prompt** (their message text), subscribe to `session.turn.start` and read `event.userPrompt`. The bundled memory plugin's per-turn retrieval uses this hook so it embeds the user's text instead of the assembling system prompt; see `src/bundled-plugins/memory/index.ts`'s `session.turn.start` handler.
|
|
384
384
|
|
|
385
385
|
#### CRITICAL: `session.prompt` and provider prompt caching
|
|
386
386
|
|
package/typeclaw.schema.json
CHANGED
|
@@ -2528,11 +2528,7 @@
|
|
|
2528
2528
|
"bufferBytes": 500000,
|
|
2529
2529
|
"injectionBudgetBytes": 16384,
|
|
2530
2530
|
"minIdleDeltaLines": 3,
|
|
2531
|
-
"spawnTimeoutMs": 50000
|
|
2532
|
-
"retrievalSpawnTimeoutMs": 30000,
|
|
2533
|
-
"vector": {
|
|
2534
|
-
"enabled": false
|
|
2535
|
-
}
|
|
2531
|
+
"spawnTimeoutMs": 50000
|
|
2536
2532
|
},
|
|
2537
2533
|
"type": "object",
|
|
2538
2534
|
"properties": {
|
|
@@ -2566,12 +2562,6 @@
|
|
|
2566
2562
|
"minimum": 1,
|
|
2567
2563
|
"maximum": 9007199254740991
|
|
2568
2564
|
},
|
|
2569
|
-
"retrievalSpawnTimeoutMs": {
|
|
2570
|
-
"default": 30000,
|
|
2571
|
-
"type": "integer",
|
|
2572
|
-
"minimum": 1,
|
|
2573
|
-
"maximum": 9007199254740991
|
|
2574
|
-
},
|
|
2575
2565
|
"dreaming": {
|
|
2576
2566
|
"type": "object",
|
|
2577
2567
|
"properties": {
|
|
@@ -2580,18 +2570,6 @@
|
|
|
2580
2570
|
"minLength": 1
|
|
2581
2571
|
}
|
|
2582
2572
|
}
|
|
2583
|
-
},
|
|
2584
|
-
"vector": {
|
|
2585
|
-
"default": {
|
|
2586
|
-
"enabled": false
|
|
2587
|
-
},
|
|
2588
|
-
"type": "object",
|
|
2589
|
-
"properties": {
|
|
2590
|
-
"enabled": {
|
|
2591
|
-
"default": false,
|
|
2592
|
-
"type": "boolean"
|
|
2593
|
-
}
|
|
2594
|
-
}
|
|
2595
2573
|
}
|
|
2596
2574
|
}
|
|
2597
2575
|
},
|
|
@@ -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
|
-
}
|