@hyperwindmill/caretaker-cli 0.23.0 → 0.25.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/dist/acp/index.d.ts +3 -0
- package/dist/acp/index.d.ts.map +1 -0
- package/dist/acp/index.js +2 -0
- package/dist/acp/install.d.ts +12 -0
- package/dist/acp/install.d.ts.map +1 -0
- package/dist/acp/install.js +110 -0
- package/dist/acp/registry.d.ts +10 -0
- package/dist/acp/registry.d.ts.map +1 -0
- package/dist/acp/registry.js +116 -0
- package/dist/cli/web/mcp_bridge.d.ts +7 -1
- package/dist/cli/web/mcp_bridge.d.ts.map +1 -1
- package/dist/cli/web/mcp_bridge.js +10 -6
- package/dist/cli/web/scheduler/memory_extract.d.ts +55 -0
- package/dist/cli/web/scheduler/memory_extract.d.ts.map +1 -0
- package/dist/cli/web/scheduler/memory_extract.js +112 -0
- package/dist/cli/web/scheduler/memory_sweep.d.ts +68 -0
- package/dist/cli/web/scheduler/memory_sweep.d.ts.map +1 -0
- package/dist/cli/web/scheduler/memory_sweep.js +330 -0
- package/dist/cli/web/scheduler/task_review.d.ts +3 -0
- package/dist/cli/web/scheduler/task_review.d.ts.map +1 -1
- package/dist/cli/web/scheduler/task_review.js +26 -0
- package/dist/cli/web/scheduler/task_roles.d.ts +1 -1
- package/dist/cli/web/scheduler/task_roles.d.ts.map +1 -1
- package/dist/cli/web/scheduler/task_roles.js +6 -6
- package/dist/cli/web/scheduler/task_strategy.d.ts.map +1 -1
- package/dist/cli/web/scheduler/task_strategy.js +27 -2
- package/dist/cli/web/scheduler.d.ts.map +1 -1
- package/dist/cli/web/scheduler.js +5 -0
- package/dist/cli/web/server.d.ts +1 -1
- package/dist/cli/web/server.d.ts.map +1 -1
- package/dist/cli/web/server.js +33 -6
- package/dist/harness/acp_policy.d.ts +41 -0
- package/dist/harness/acp_policy.d.ts.map +1 -0
- package/dist/harness/acp_policy.js +59 -0
- package/dist/harness/acp_pool.d.ts +33 -0
- package/dist/harness/acp_pool.d.ts.map +1 -0
- package/dist/harness/acp_pool.js +148 -0
- package/dist/harness/acp_runner.d.ts +4 -0
- package/dist/harness/acp_runner.d.ts.map +1 -0
- package/dist/harness/acp_runner.js +347 -0
- package/dist/harness/claude_code_runner.d.ts +2 -0
- package/dist/harness/claude_code_runner.d.ts.map +1 -1
- package/dist/harness/claude_code_runner.js +6 -1
- package/dist/harness/confirm_gate.d.ts +12 -0
- package/dist/harness/confirm_gate.d.ts.map +1 -0
- package/dist/harness/confirm_gate.js +33 -0
- package/dist/harness/index.d.ts +1 -0
- package/dist/harness/index.d.ts.map +1 -1
- package/dist/harness/index.js +1 -0
- package/dist/harness/loop.d.ts +12 -0
- package/dist/harness/loop.d.ts.map +1 -1
- package/dist/harness/loop.js +16 -0
- package/dist/harness/memory_recall.d.ts +28 -0
- package/dist/harness/memory_recall.d.ts.map +1 -0
- package/dist/harness/memory_recall.js +74 -0
- package/dist/harness/title.js +1 -1
- package/dist/harness/tools/builtin/index.d.ts +2 -1
- package/dist/harness/tools/builtin/index.d.ts.map +1 -1
- package/dist/harness/tools/builtin/index.js +4 -1
- package/dist/harness/tools/builtin/memory_tools.d.ts +3 -0
- package/dist/harness/tools/builtin/memory_tools.d.ts.map +1 -0
- package/dist/harness/tools/builtin/memory_tools.js +60 -0
- package/dist/harness/tools/resolve.d.ts.map +1 -1
- package/dist/harness/tools/resolve.js +15 -0
- package/dist/harness/tools/types.d.ts +6 -0
- package/dist/harness/tools/types.d.ts.map +1 -1
- package/dist/lib/project_resolve.d.ts +5 -0
- package/dist/lib/project_resolve.d.ts.map +1 -0
- package/dist/lib/project_resolve.js +23 -0
- package/dist/mcp/builtin_server.d.ts +2 -0
- package/dist/mcp/builtin_server.d.ts.map +1 -1
- package/dist/mcp/builtin_server.js +9 -7
- package/dist/mcp/run_command_tool.d.ts +13 -0
- package/dist/mcp/run_command_tool.d.ts.map +1 -0
- package/dist/mcp/run_command_tool.js +51 -0
- package/dist/session/store.d.ts +1 -0
- package/dist/session/store.d.ts.map +1 -1
- package/dist/session/store.js +6 -0
- package/dist/session/types.d.ts +3 -0
- package/dist/session/types.d.ts.map +1 -1
- package/dist/store/db.d.ts +66 -0
- package/dist/store/db.d.ts.map +1 -1
- package/dist/store/db.js +68 -0
- package/dist/tui/agents.d.ts.map +1 -1
- package/dist/tui/agents.js +62 -25
- package/dist/tui/chat.d.ts.map +1 -1
- package/dist/tui/chat.js +7 -8
- package/dist/tui/providers.d.ts.map +1 -1
- package/dist/tui/providers.js +116 -17
- package/dist/webview/standalone.css +18 -0
- package/dist/webview/standalone.css.map +2 -2
- package/dist/webview/standalone.js +792 -440
- package/dist/webview/standalone.js.map +4 -4
- package/package.json +8 -3
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// ACP runner: implements the same run() contract as loop.ts by driving an
|
|
2
|
+
// Agent Client Protocol agent (claude-agent-acp, codex-acp, agy_acp_server, …)
|
|
3
|
+
// over JSON-RPC/stdio. The agent owns the loop and its tools; caretaker owns
|
|
4
|
+
// the permission policy (acp_policy.ts), display persistence (cb.onMessage),
|
|
5
|
+
// and session continuity (acpSessionId + session/load when supported).
|
|
6
|
+
//
|
|
7
|
+
// System prompt is fabricated client-side (ACP has no system-prompt field):
|
|
8
|
+
// the stable context block (agent systemPrompt + context files minus the
|
|
9
|
+
// runner's self-loaded ones — the mirror of claude-code's
|
|
10
|
+
// --append-system-prompt) rides the FIRST turn of each ACP session; volatile
|
|
11
|
+
// parts (<memories>, voice block) ride every turn. Under ACP the prompt enters
|
|
12
|
+
// history and accumulates, hence the split.
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { loadContextFiles, formatContextBlock, resolveFileReferences } from './context_files.js';
|
|
15
|
+
import { buildMemoriesBlock } from './memory_recall.js';
|
|
16
|
+
import { VOICE_CONVERSATION_PRELUDE } from './prelude.js';
|
|
17
|
+
import { foldHistory } from './claude_code_runner.js';
|
|
18
|
+
import { decidePermission, buildPermissionResponse } from './acp_policy.js';
|
|
19
|
+
import { acquireAcpAgent, releaseAcpAgent } from './acp_pool.js';
|
|
20
|
+
import { readSession, updateAcpSessionId, assistantMessage, toolMessage } from '../session/store.js';
|
|
21
|
+
import { loadMcpServers } from '../store/json.js';
|
|
22
|
+
import { resolvedServerRuntime } from '../mcp/client.js';
|
|
23
|
+
const CANCEL_GRACE_MS = 5_000;
|
|
24
|
+
/** Agent's configured MCP servers + per-run extras (task bridge) as ACP shapes. */
|
|
25
|
+
async function resolveAcpMcpServers(serverIds, extra) {
|
|
26
|
+
const out = [];
|
|
27
|
+
if (serverIds.length > 0) {
|
|
28
|
+
const file = await loadMcpServers();
|
|
29
|
+
for (const id of serverIds) {
|
|
30
|
+
const cfg = file.servers.find((s) => s.id === id);
|
|
31
|
+
if (!cfg)
|
|
32
|
+
continue;
|
|
33
|
+
const r = await resolvedServerRuntime(cfg).catch(() => null);
|
|
34
|
+
if (!r) {
|
|
35
|
+
console.warn(`[acp] skipping MCP server "${id}" (disabled or no usable credentials)`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (r.type === 'stdio') {
|
|
39
|
+
out.push({
|
|
40
|
+
name: id,
|
|
41
|
+
command: r.command,
|
|
42
|
+
args: r.args ?? [],
|
|
43
|
+
env: Object.entries(r.env ?? {}).map(([name, value]) => ({ name, value })),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
out.push({
|
|
48
|
+
type: 'http',
|
|
49
|
+
name: id,
|
|
50
|
+
url: r.url,
|
|
51
|
+
headers: Object.entries(r.headers ?? {}).map(([name, value]) => ({ name, value })),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const [name, def] of Object.entries(extra ?? {})) {
|
|
57
|
+
out.push({
|
|
58
|
+
type: 'http',
|
|
59
|
+
name,
|
|
60
|
+
url: def.url,
|
|
61
|
+
headers: Object.entries(def.headers ?? {}).map(([n, v]) => ({ name: n, value: v })),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/** Session-cumulative → per-turn usage (clamped; unknown baseline = full total). */
|
|
67
|
+
function diffUsage(prev, cur) {
|
|
68
|
+
const d = (a, b) => Math.max(0, a - (b ?? 0));
|
|
69
|
+
const usage = {
|
|
70
|
+
input: d(cur.inputTokens, prev?.inputTokens),
|
|
71
|
+
output: d(cur.outputTokens, prev?.outputTokens),
|
|
72
|
+
};
|
|
73
|
+
if (cur.cachedReadTokens != null)
|
|
74
|
+
usage.cacheRead = d(cur.cachedReadTokens, prev?.cachedReadTokens ?? undefined);
|
|
75
|
+
if (cur.cachedWriteTokens != null)
|
|
76
|
+
usage.cacheWrite = d(cur.cachedWriteTokens, prev?.cachedWriteTokens ?? undefined);
|
|
77
|
+
return usage;
|
|
78
|
+
}
|
|
79
|
+
/** claude-agent-acp harness notices (hook output, warnings) are demoted to
|
|
80
|
+
* the thinking channel: visible but collapsed, out of the persisted reply
|
|
81
|
+
* text and out of TTS. Two signals, in order:
|
|
82
|
+
* 1. `_meta.claudeCode.kind === 'informational'` on the chunk — the marker
|
|
83
|
+
* https://github.com/agentclientprotocol/claude-agent-acp/issues/1042
|
|
84
|
+
* landed (PR #1055, 2026-09-23). Covers every level, including 'info'
|
|
85
|
+
* chunks that carry no visible prefix.
|
|
86
|
+
* 2. Fallback for pre-#1055 adapters (pinned npx versions): the folded
|
|
87
|
+
* `**Notice:** …` / `**Warning:** …` / `**Error:** …` text prefix. A
|
|
88
|
+
* notice arrives as ONE complete chunk, so only a whole-chunk prefix
|
|
89
|
+
* match counts — model deltas that merely contain a bold label
|
|
90
|
+
* mid-stream stay untouched. Drop this arm when pre-fix adapters are no
|
|
91
|
+
* longer a concern. */
|
|
92
|
+
const ADAPTER_NOTICE_RE = /^\*\*(Notice|Warning|Error):\*\* /;
|
|
93
|
+
export function isAdapterNotice(text, meta) {
|
|
94
|
+
const kind = meta?.claudeCode?.kind;
|
|
95
|
+
if (kind === 'informational')
|
|
96
|
+
return true;
|
|
97
|
+
return ADAPTER_NOTICE_RE.test(text);
|
|
98
|
+
}
|
|
99
|
+
function extractToolResultText(content) {
|
|
100
|
+
if (!Array.isArray(content))
|
|
101
|
+
return '';
|
|
102
|
+
return content
|
|
103
|
+
.map((c) => c?.type === 'content' && c.content?.type === 'text' ? String(c.content.text) : '')
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
.join('\n');
|
|
106
|
+
}
|
|
107
|
+
export async function runAcp(opts, cb = {}) {
|
|
108
|
+
const { agent, provider } = opts;
|
|
109
|
+
const extras = opts.acp ?? {};
|
|
110
|
+
const workingDir = opts.workingDir ?? process.cwd();
|
|
111
|
+
const safeEmit = async (fn) => {
|
|
112
|
+
try {
|
|
113
|
+
await fn?.();
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
console.warn('[acp] callback error:', err);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
// 1. Stable context block (mirror of claude-code's --append-system-prompt,
|
|
120
|
+
// minus files this runner self-loads).
|
|
121
|
+
const selfLoaded = new Set(provider.selfLoadedContextFiles ?? []);
|
|
122
|
+
const sys = await resolveFileReferences(agent.systemPrompt ?? '', workingDir);
|
|
123
|
+
const ctxEntries = (await loadContextFiles(workingDir)).filter((e) => !selfLoaded.has(path.basename(e.path)));
|
|
124
|
+
const stableBlock = [sys, ctxEntries.length ? formatContextBlock(ctxEntries) : '']
|
|
125
|
+
.filter(Boolean)
|
|
126
|
+
.join('\n\n');
|
|
127
|
+
// 2. Volatile per-turn block.
|
|
128
|
+
const memoriesBlock = opts.skipMemoryRecall
|
|
129
|
+
? ''
|
|
130
|
+
: await buildMemoriesBlock(opts.prompt, workingDir, opts.memoryProjectId);
|
|
131
|
+
const volatileBlock = [memoriesBlock, opts.voiceConversation ? VOICE_CONVERSATION_PRELUDE : '']
|
|
132
|
+
.filter(Boolean)
|
|
133
|
+
.join('\n\n');
|
|
134
|
+
const mcpServers = await resolveAcpMcpServers(agent.mcpServers ?? [], extras.extraMcpServers);
|
|
135
|
+
// 3. Acquire child (pooled per caretaker session; ephemeral otherwise).
|
|
136
|
+
const poolKey = opts.sessionId ? `${agent.id}:${opts.sessionId}` : null;
|
|
137
|
+
let handle;
|
|
138
|
+
try {
|
|
139
|
+
handle = await acquireAcpAgent(provider, poolKey);
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
throw new Error(`acp runner failed to start the agent for provider "${provider.name}": ${err?.message ?? err} ` +
|
|
143
|
+
`(is it installed and authenticated? Log in with the agent's own CLI first.)`);
|
|
144
|
+
}
|
|
145
|
+
// 4. Turn accumulation state.
|
|
146
|
+
const parts = [];
|
|
147
|
+
let text = '';
|
|
148
|
+
let toolCalls = 0;
|
|
149
|
+
const pushText = (t) => {
|
|
150
|
+
text += t;
|
|
151
|
+
const last = parts[parts.length - 1];
|
|
152
|
+
if (last && last.type === 'text')
|
|
153
|
+
last.text += t;
|
|
154
|
+
else
|
|
155
|
+
parts.push({ type: 'text', text: t });
|
|
156
|
+
};
|
|
157
|
+
const onUpdate = async (n) => {
|
|
158
|
+
const u = n.update;
|
|
159
|
+
switch (u.sessionUpdate) {
|
|
160
|
+
case 'agent_message_chunk':
|
|
161
|
+
if (u.content.type === 'text') {
|
|
162
|
+
if (isAdapterNotice(u.content.text, u._meta)) {
|
|
163
|
+
// Adapter harness notice (see isAdapterNotice / issue #1042):
|
|
164
|
+
// demote to thinking instead of polluting the reply.
|
|
165
|
+
await safeEmit(() => cb.onThinking?.(u.content.text));
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
pushText(u.content.text);
|
|
169
|
+
await safeEmit(() => cb.onChunk?.(u.content.text));
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
case 'agent_thought_chunk':
|
|
173
|
+
if (u.content.type === 'text')
|
|
174
|
+
await safeEmit(() => cb.onThinking?.(u.content.text));
|
|
175
|
+
break;
|
|
176
|
+
case 'tool_call': {
|
|
177
|
+
toolCalls += 1;
|
|
178
|
+
const name = u.name ?? u.title;
|
|
179
|
+
parts.push({ type: 'tool_use', id: u.toolCallId, name, args: u.rawInput ?? {} });
|
|
180
|
+
await safeEmit(() => cb.onToolCall?.(u.toolCallId, name, u.rawInput ?? {}));
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case 'tool_call_update': {
|
|
184
|
+
if (u.status === 'completed' || u.status === 'failed') {
|
|
185
|
+
const resultText = extractToolResultText(u.content);
|
|
186
|
+
await safeEmit(() => cb.onToolResult?.(u.toolCallId, resultText));
|
|
187
|
+
await safeEmit(() => cb.onMessage?.(toolMessage(u.toolCallId, resultText)));
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
default:
|
|
192
|
+
break; // plan / usage_update / mode updates: not rendered in v1
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const onPermission = async (req) => {
|
|
196
|
+
const decision = decidePermission(req, extras);
|
|
197
|
+
if (decision === 'allow')
|
|
198
|
+
return buildPermissionResponse(req.options, 'once');
|
|
199
|
+
if (decision === 'deny')
|
|
200
|
+
return buildPermissionResponse(req.options, 'deny');
|
|
201
|
+
// 'ask' → the ordinary confirm gate; no gate wired means allow (the caller
|
|
202
|
+
// decides which surfaces gate, same contract as the native loop).
|
|
203
|
+
if (!cb.confirmTool)
|
|
204
|
+
return buildPermissionResponse(req.options, 'once');
|
|
205
|
+
const name = req.toolCall.name ?? req.toolCall.title ?? 'tool';
|
|
206
|
+
const answer = await cb.confirmTool(req.toolCall.toolCallId, String(name), req.toolCall.rawInput ?? {});
|
|
207
|
+
return buildPermissionResponse(req.options, answer);
|
|
208
|
+
};
|
|
209
|
+
let stopReason = 'error';
|
|
210
|
+
let usage = { input: 0, output: 0 };
|
|
211
|
+
let aborted = false;
|
|
212
|
+
let graceTimer;
|
|
213
|
+
try {
|
|
214
|
+
// 5. Resolve the ACP session: pooled live id → persisted id via
|
|
215
|
+
// session/load (capability-gated) → session/new (+ folded history).
|
|
216
|
+
let acpSessionId = handle.acpSessionId;
|
|
217
|
+
let isNewSession = false;
|
|
218
|
+
if (!acpSessionId) {
|
|
219
|
+
let persisted;
|
|
220
|
+
let modes;
|
|
221
|
+
if (opts.sessionId) {
|
|
222
|
+
try {
|
|
223
|
+
persisted = (await readSession(agent.id, opts.sessionId)).meta.acpSessionId;
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
/* new session */
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (persisted && handle.init.agentCapabilities?.loadSession) {
|
|
230
|
+
// Load BEFORE arming the binding: the replayed history streams as
|
|
231
|
+
// session/update notifications we deliberately drop (our own store
|
|
232
|
+
// already has the conversation).
|
|
233
|
+
try {
|
|
234
|
+
const loadRes = await handle.conn.agent.request('session/load', {
|
|
235
|
+
sessionId: persisted,
|
|
236
|
+
cwd: workingDir,
|
|
237
|
+
mcpServers,
|
|
238
|
+
});
|
|
239
|
+
modes = loadRes.modes;
|
|
240
|
+
acpSessionId = persisted;
|
|
241
|
+
}
|
|
242
|
+
catch (err) {
|
|
243
|
+
console.warn(`[acp] session/load "${persisted}" failed; starting fresh:`, err);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (!acpSessionId) {
|
|
247
|
+
const res = await handle.conn.agent.request('session/new', { cwd: workingDir, mcpServers });
|
|
248
|
+
acpSessionId = res.sessionId;
|
|
249
|
+
modes = res.modes;
|
|
250
|
+
isNewSession = true;
|
|
251
|
+
}
|
|
252
|
+
handle.acpSessionId = acpSessionId;
|
|
253
|
+
// Agent-level session mode (AgentConfig.acpMode): pin the agent to one
|
|
254
|
+
// of its OWN permission modes so it stops asking at the source. Only for
|
|
255
|
+
// interactive-policy runs — task runs' policy (unattended/planner/
|
|
256
|
+
// deny-all) stays authoritative. Unknown/unadvertised modes warn and
|
|
257
|
+
// continue: a wrong mode id must never brick the chat.
|
|
258
|
+
const wantMode = agent.acpMode?.trim();
|
|
259
|
+
if (wantMode && (extras.mode ?? 'interactive') === 'interactive') {
|
|
260
|
+
const available = modes?.availableModes.some((m) => m.id === wantMode);
|
|
261
|
+
if (!available) {
|
|
262
|
+
console.warn(`[acp] agent "${agent.name}": acpMode "${wantMode}" is not among the agent's advertised modes — ignored`);
|
|
263
|
+
}
|
|
264
|
+
else if (modes.currentModeId !== wantMode) {
|
|
265
|
+
try {
|
|
266
|
+
await handle.conn.agent.request('session/set_mode', {
|
|
267
|
+
sessionId: acpSessionId,
|
|
268
|
+
modeId: wantMode,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
catch (err) {
|
|
272
|
+
console.warn(`[acp] session/set_mode "${wantMode}" failed:`, err);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (opts.sessionId && acpSessionId !== persisted) {
|
|
277
|
+
try {
|
|
278
|
+
await updateAcpSessionId({ agentId: agent.id, id: opts.sessionId }, acpSessionId);
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
console.warn('[acp] failed to persist session id:', err);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// 6. Prompt blocks: stable context on the session's first turn; volatile
|
|
286
|
+
// every turn; history folded only into a brand-new session.
|
|
287
|
+
const blocks = [];
|
|
288
|
+
if (isNewSession && stableBlock) {
|
|
289
|
+
blocks.push({ type: 'text', text: `<caretaker-context>\n${stableBlock}\n</caretaker-context>` });
|
|
290
|
+
}
|
|
291
|
+
if (volatileBlock)
|
|
292
|
+
blocks.push({ type: 'text', text: volatileBlock });
|
|
293
|
+
blocks.push({
|
|
294
|
+
type: 'text',
|
|
295
|
+
text: isNewSession ? foldHistory(opts.history, opts.prompt) : opts.prompt,
|
|
296
|
+
});
|
|
297
|
+
// 7. Arm the binding, wire abort, send the prompt.
|
|
298
|
+
handle.binding.current = { acpSessionId, onUpdate, onPermission };
|
|
299
|
+
const onAbort = () => {
|
|
300
|
+
aborted = true;
|
|
301
|
+
void handle.conn.agent.notify('session/cancel', { sessionId: acpSessionId }).catch(() => { });
|
|
302
|
+
graceTimer = setTimeout(() => handle.kill(), CANCEL_GRACE_MS);
|
|
303
|
+
};
|
|
304
|
+
opts.signal?.addEventListener('abort', onAbort, { once: true });
|
|
305
|
+
try {
|
|
306
|
+
if (opts.signal?.aborted) {
|
|
307
|
+
// addEventListener never fires for a signal aborted BEFORE registration
|
|
308
|
+
// (e.g. a Pause or wall-clock budget landing during session setup) —
|
|
309
|
+
// without this check the prompt would run un-cancellable, and a wedged
|
|
310
|
+
// agent would hang the turn forever.
|
|
311
|
+
aborted = true;
|
|
312
|
+
return { text, toolCalls, usage, stop: 'aborted' };
|
|
313
|
+
}
|
|
314
|
+
const res = await handle.conn.agent.request('session/prompt', {
|
|
315
|
+
sessionId: acpSessionId,
|
|
316
|
+
prompt: blocks,
|
|
317
|
+
});
|
|
318
|
+
stopReason = res.stopReason;
|
|
319
|
+
if (res.usage) {
|
|
320
|
+
usage = diffUsage(handle.lastUsage, res.usage);
|
|
321
|
+
handle.lastUsage = res.usage;
|
|
322
|
+
await safeEmit(() => cb.onUsage?.(usage));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
finally {
|
|
326
|
+
opts.signal?.removeEventListener('abort', onAbort);
|
|
327
|
+
if (graceTimer)
|
|
328
|
+
clearTimeout(graceTimer);
|
|
329
|
+
handle.binding.current = null;
|
|
330
|
+
}
|
|
331
|
+
if (parts.length > 0)
|
|
332
|
+
await safeEmit(() => cb.onMessage?.(assistantMessage(parts, usage)));
|
|
333
|
+
if (aborted || stopReason === 'cancelled')
|
|
334
|
+
return { text, toolCalls, usage, stop: 'aborted' };
|
|
335
|
+
if (stopReason === 'max_turn_requests')
|
|
336
|
+
return { text, toolCalls, usage, stop: 'max_turns' };
|
|
337
|
+
return { text, toolCalls, usage, stop: 'done' };
|
|
338
|
+
}
|
|
339
|
+
catch (err) {
|
|
340
|
+
if (aborted)
|
|
341
|
+
return { text, toolCalls, usage, stop: 'aborted' };
|
|
342
|
+
throw new Error(`acp runner: ${err?.message ?? String(err)}`);
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
releaseAcpAgent(poolKey, handle);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type SpawnOptions } from 'node:child_process';
|
|
2
2
|
import type { RunOptions, RunCallbacks, RunResult } from './loop.js';
|
|
3
|
+
import type { MessageRecord } from '../session/types.js';
|
|
3
4
|
export type ClaudeCodeRunExtras = {
|
|
4
5
|
permissionMode?: string;
|
|
5
6
|
allowedTools?: string[];
|
|
@@ -50,6 +51,7 @@ export declare function claudeCodeTaskExtras(p: {
|
|
|
50
51
|
token: string;
|
|
51
52
|
};
|
|
52
53
|
}): ClaudeCodeRunExtras;
|
|
54
|
+
export declare function foldHistory(history: MessageRecord[] | undefined, prompt: string): string;
|
|
53
55
|
export declare function runClaudeCode(opts: RunOptions, cb?: RunCallbacks): Promise<RunResult>;
|
|
54
56
|
export {};
|
|
55
57
|
//# sourceMappingURL=claude_code_runner.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude_code_runner.d.ts","sourceRoot":"","sources":["../../src/harness/claude_code_runner.ts"],"names":[],"mappings":"AAMA,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAM3E,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"claude_code_runner.d.ts","sourceRoot":"","sources":["../../src/harness/claude_code_runner.ts"],"names":[],"mappings":"AAMA,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAM3E,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAYrE,OAAO,KAAK,EAAiB,aAAa,EAAE,MAAM,qBAAqB,CAAC;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C,YAAY,GAAE,MAA4D,GACzE,MAAM,GAAG,IAAI,CAQf;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;kFAC8E;IAC9E,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,OAAO,CAAC;CACzB;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,eAAe,GAAG,MAAM,EAAE,CAe5D;AAED,uEAAuE;AACvE,wBAAgB,oBAAoB,CAAC,CAAC,EAAE;IACtC,QAAQ,EAAE,OAAO,CAAC;IAClB,GAAG,EAAE,OAAO,CAAC;IACb,MAAM,CAAC,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CACzC,GAAG,mBAAmB,CAwBtB;AA8BD,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIxF;AAoKD,wBAAsB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,GAAE,YAAiB,GAAG,OAAO,CAAC,SAAS,CAAC,CA2I/F"}
|
|
@@ -11,6 +11,7 @@ import os from 'node:os';
|
|
|
11
11
|
import path from 'node:path';
|
|
12
12
|
import { parseClaudeStreamLine } from './claude_code_stream.js';
|
|
13
13
|
import { loadContextFiles, formatContextBlock, resolveFileReferences } from './context_files.js';
|
|
14
|
+
import { buildMemoriesBlock } from './memory_recall.js';
|
|
14
15
|
import { VOICE_CONVERSATION_PRELUDE } from './prelude.js';
|
|
15
16
|
import { readSession, updateClaudeSessionId, assistantMessage, toolMessage, } from '../session/store.js';
|
|
16
17
|
import { loadMcpServers } from '../store/json.js';
|
|
@@ -112,7 +113,7 @@ async function buildMcpConfigFile(serverIds, extra) {
|
|
|
112
113
|
await writeFile(configPath, JSON.stringify({ mcpServers: servers }), { mode: 0o600 });
|
|
113
114
|
return { configPath, cleanup: () => rm(dir, { recursive: true, force: true }) };
|
|
114
115
|
}
|
|
115
|
-
function foldHistory(history, prompt) {
|
|
116
|
+
export function foldHistory(history, prompt) {
|
|
116
117
|
if (!history?.length)
|
|
117
118
|
return prompt;
|
|
118
119
|
const lines = history.filter((m) => m.role !== 'tool').map((m) => `[${m.role}] ${m.content}`);
|
|
@@ -271,9 +272,13 @@ export async function runClaudeCode(opts, cb = {}) {
|
|
|
271
272
|
// ~/.caretaker/AGENTS.md it does not — verified on CLI 2.1.207).
|
|
272
273
|
const sys = await resolveFileReferences(agent.systemPrompt ?? '', workingDir);
|
|
273
274
|
const ctxEntries = (await loadContextFiles(workingDir)).filter((e) => path.basename(e.path) !== 'CLAUDE.md');
|
|
275
|
+
const memoriesBlock = opts.skipMemoryRecall
|
|
276
|
+
? ''
|
|
277
|
+
: await buildMemoriesBlock(opts.prompt, workingDir, opts.memoryProjectId);
|
|
274
278
|
const appendSystemPrompt = [
|
|
275
279
|
sys,
|
|
276
280
|
ctxEntries.length ? formatContextBlock(ctxEntries) : '',
|
|
281
|
+
memoriesBlock,
|
|
277
282
|
opts.voiceConversation ? VOICE_CONVERSATION_PRELUDE : '',
|
|
278
283
|
]
|
|
279
284
|
.filter(Boolean)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ConfirmDecision } from './tools/index.js';
|
|
2
|
+
export declare class ConfirmGateState {
|
|
3
|
+
private readonly isAcp;
|
|
4
|
+
/** Native mode: names that still prompt ("always" removes). */
|
|
5
|
+
private readonly confirmSet;
|
|
6
|
+
/** ACP mode: names the user already granted "always" (adds). */
|
|
7
|
+
private readonly acpAllowed;
|
|
8
|
+
constructor(providerType: string | undefined, confirmTools: string[] | undefined);
|
|
9
|
+
needsAsk(name: string): boolean;
|
|
10
|
+
remember(name: string, decision: ConfirmDecision): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=confirm_gate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"confirm_gate.d.ts","sourceRoot":"","sources":["../../src/harness/confirm_gate.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAExD,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAU;IAChC,+DAA+D;IAC/D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAc;IACzC,gEAAgE;IAChE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAqB;gBAEpC,YAAY,EAAE,MAAM,GAAG,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,SAAS;IAKhF,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI/B,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,eAAe,GAAG,IAAI;CAKxD"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Shared confirm-gate state for interactive chat surfaces (web, VSCode, TUI).
|
|
2
|
+
//
|
|
3
|
+
// Native and claude-code agents gate by the user's [!] picker selection
|
|
4
|
+
// (agent.confirmTools): only listed names prompt, and "always" un-lists the
|
|
5
|
+
// name for the rest of the controller's lifetime.
|
|
6
|
+
//
|
|
7
|
+
// ACP agents invert the rule: agent.confirmTools is always empty (the picker
|
|
8
|
+
// does not apply to external runners), and a session/request_permission means
|
|
9
|
+
// the agent's OWN permission model already decided this call was worth asking
|
|
10
|
+
// about — so every request surfaces a card, and "always" is remembered
|
|
11
|
+
// per tool name in-memory.
|
|
12
|
+
export class ConfirmGateState {
|
|
13
|
+
isAcp;
|
|
14
|
+
/** Native mode: names that still prompt ("always" removes). */
|
|
15
|
+
confirmSet;
|
|
16
|
+
/** ACP mode: names the user already granted "always" (adds). */
|
|
17
|
+
acpAllowed = new Set();
|
|
18
|
+
constructor(providerType, confirmTools) {
|
|
19
|
+
this.isAcp = providerType === 'acp';
|
|
20
|
+
this.confirmSet = new Set(confirmTools ?? []);
|
|
21
|
+
}
|
|
22
|
+
needsAsk(name) {
|
|
23
|
+
return this.isAcp ? !this.acpAllowed.has(name) : this.confirmSet.has(name);
|
|
24
|
+
}
|
|
25
|
+
remember(name, decision) {
|
|
26
|
+
if (decision !== 'always')
|
|
27
|
+
return;
|
|
28
|
+
if (this.isAcp)
|
|
29
|
+
this.acpAllowed.add(name);
|
|
30
|
+
else
|
|
31
|
+
this.confirmSet.delete(name);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/harness/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { run } from './loop.js';
|
|
2
|
+
export { ConfirmGateState } from './confirm_gate.js';
|
|
2
3
|
export type { RunOptions, RunCallbacks, RunResult, ConfirmDecision } from './loop.js';
|
|
3
4
|
export { resolveAgentTools, ToolRegistry, registerBuiltins, toOpenAiTool } from './tools/index.js';
|
|
4
5
|
export type { Tool, ToolContext, ToolResult, ConfirmGate, OpenAiFunctionTool, } from './tools/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/harness/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChC,YAAY,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEtF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACnG,YAAY,EACV,IAAI,EACJ,WAAW,EACX,UAAU,EACV,WAAW,EACX,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/harness/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,YAAY,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEtF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACnG,YAAY,EACV,IAAI,EACJ,WAAW,EACX,UAAU,EACV,WAAW,EACX,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/harness/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// keep importing their specific files; only this barrel is part of the
|
|
4
4
|
// stable surface exposed via `caretaker-cli/harness`.
|
|
5
5
|
export { run } from './loop.js';
|
|
6
|
+
export { ConfirmGateState } from './confirm_gate.js';
|
|
6
7
|
export { resolveAgentTools, ToolRegistry, registerBuiltins, toOpenAiTool } from './tools/index.js';
|
|
7
8
|
export { tools } from './tools/instance.js';
|
|
8
9
|
export { fetchOpenAiStyleModels } from './models.js';
|
package/dist/harness/loop.d.ts
CHANGED
|
@@ -57,11 +57,23 @@ export interface RunOptions {
|
|
|
57
57
|
/** claude-code runner extras (ignored by the native loop): permission
|
|
58
58
|
* mode override, tool allow/deny rules, extra per-run MCP servers. */
|
|
59
59
|
claudeCode?: import('./claude_code_runner.js').ClaudeCodeRunExtras;
|
|
60
|
+
/** acp runner extras (ignored by the native loop): permission policy mode,
|
|
61
|
+
* extra per-run MCP servers, docker confinement. */
|
|
62
|
+
acp?: import('./acp_policy.js').AcpRunExtras;
|
|
60
63
|
/** Native-loop only: run `bash` commands inside this docker container. */
|
|
61
64
|
dockerContainer?: string;
|
|
62
65
|
/** The turn came from voice conversation mode; append the spoken-reply
|
|
63
66
|
* block to the system prompt so the agent writes speakable prose. */
|
|
64
67
|
voiceConversation?: boolean;
|
|
68
|
+
/** Skip the per-turn <memories> recall block. Set by the memory sweep's
|
|
69
|
+
* own summarize runs: injecting recalled memories into the call that
|
|
70
|
+
* extracts memories would pollute its carefully-shaped prompt. */
|
|
71
|
+
skipMemoryRecall?: boolean;
|
|
72
|
+
/** Explicit project scope for the recall block. Set by autonomous task
|
|
73
|
+
* runs, whose workingDir is a worktree (or managed repo dir) that never
|
|
74
|
+
* prefix-matches the project's workingDir — the task record already
|
|
75
|
+
* knows the project. Host-side, never the model's choice. */
|
|
76
|
+
memoryProjectId?: string;
|
|
65
77
|
}
|
|
66
78
|
export interface RunResult {
|
|
67
79
|
/** Concatenated assistant text across all turns. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../../src/harness/loop.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE/D,OAAO,KAAK,EAAiB,aAAa,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC9F,OAAO,EAAgC,KAAK,cAAc,EAAoB,MAAM,eAAe,CAAC;AAEpG,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,eAAe,IAAI,mBAAmB,EAC3C,KAAK,IAAI,EAGV,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file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CAAC,CAqWrF"}
|
package/dist/harness/loop.js
CHANGED
|
@@ -17,6 +17,7 @@ import { buildRequestBody, readStream } from './provider.js';
|
|
|
17
17
|
import { mapMessagesToChat } from './history.js';
|
|
18
18
|
import { toOpenAiTool, } from './tools/index.js';
|
|
19
19
|
import { withHarnessPrelude, VOICE_CONVERSATION_PRELUDE } from './prelude.js';
|
|
20
|
+
import { buildMemoriesBlock } from './memory_recall.js';
|
|
20
21
|
import { formatRuntimeInfoBlock } from './runtime_info.js';
|
|
21
22
|
import { loadContextFiles, formatContextBlock, resolveFileReferences } from './context_files.js';
|
|
22
23
|
let fetchImpl = globalThis.fetch.bind(globalThis);
|
|
@@ -34,6 +35,12 @@ export async function run(opts, cb = {}) {
|
|
|
34
35
|
const { runClaudeCode } = await import('./claude_code_runner.js');
|
|
35
36
|
return runClaudeCode(opts, cb);
|
|
36
37
|
}
|
|
38
|
+
if (opts.provider.type === 'acp') {
|
|
39
|
+
// Same single-dispatch pattern: ACP providers get the whole loop replaced
|
|
40
|
+
// by an external ACP agent (see acp_runner.ts).
|
|
41
|
+
const { runAcp } = await import('./acp_runner.js');
|
|
42
|
+
return runAcp(opts, cb);
|
|
43
|
+
}
|
|
37
44
|
const { agent, provider, tools, prompt } = opts;
|
|
38
45
|
const baseUrl = provider.endpoint.replace(/\/+$/, '');
|
|
39
46
|
const endpoint = `${baseUrl}/v1/chat/completions`;
|
|
@@ -78,6 +85,15 @@ export async function run(opts, cb = {}) {
|
|
|
78
85
|
workingDir: toolCtx.workingDir,
|
|
79
86
|
});
|
|
80
87
|
effectiveSystemPrompt = `${effectiveSystemPrompt}\n\n${runtimeBlock}`.trim();
|
|
88
|
+
// Memory recall (read path): per-turn <memories> block — titles matched
|
|
89
|
+
// host-side against the user prompt, zero model calls. '' when memory is
|
|
90
|
+
// unconfigured or nothing matches.
|
|
91
|
+
if (!opts.skipMemoryRecall) {
|
|
92
|
+
const memoriesBlock = await buildMemoriesBlock(prompt, toolCtx.workingDir, opts.memoryProjectId);
|
|
93
|
+
if (memoriesBlock) {
|
|
94
|
+
effectiveSystemPrompt = `${effectiveSystemPrompt}\n\n${memoriesBlock}`.trim();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
81
97
|
// Voice-conversation block is appended last (strongest recency) and only
|
|
82
98
|
// on voice turns — the same session mixes typed and spoken turns.
|
|
83
99
|
if (opts.voiceConversation) {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Memory } from '../store/db.js';
|
|
2
|
+
export declare const RECALL_TOP_K = 5;
|
|
3
|
+
export declare const MIN_KEYWORD_LENGTH = 3;
|
|
4
|
+
/** Inverted lexical match — no query tokenization: keywords are split into
|
|
5
|
+
* their component words (deduped per memory), and each word contained in
|
|
6
|
+
* the lowercased prompt counts as a match. A multi-word keyword therefore
|
|
7
|
+
* fires on any of its words ("reaper linux" ← "reaper") and scores higher
|
|
8
|
+
* when the full phrase is present (every word matches) — graded for free,
|
|
9
|
+
* no phrase-special-casing. Generic components over-firing is mitigated by
|
|
10
|
+
* the scoring plus the top-K cut.
|
|
11
|
+
* score = matchedWords × importanceWeight × (1 + log2(1 + recallCount)):
|
|
12
|
+
* acquired strength weighs in — much-recalled memories surface more easily.
|
|
13
|
+
* Ties: lastRecalledAt desc, then createdAt desc. Top-K, score > 0 only. */
|
|
14
|
+
export declare function matchMemories(prompt: string, memories: Memory[]): Memory[];
|
|
15
|
+
/** The prelude block. Titles only, never bodies: the explicit memory_read
|
|
16
|
+
* IS the recall event that feeds acquired strength — a system that always
|
|
17
|
+
* injects bodies never learns what mattered. '' when no matches. */
|
|
18
|
+
export declare function formatMemoriesBlock(matches: Memory[]): string;
|
|
19
|
+
/** One-stop per-turn recall: gate on MemoryConfig (present = read path on,
|
|
20
|
+
* same gate as the sweep), resolve the project from the run's workingDir,
|
|
21
|
+
* match global + project memories, format. Never throws — recall must
|
|
22
|
+
* never break a chat turn.
|
|
23
|
+
* `explicitProjectId` overrides dir resolution: task cycles run in a
|
|
24
|
+
* worktree (or managed repo dir) that never prefix-matches the project's
|
|
25
|
+
* workingDir, but the caller knows the project from the task record —
|
|
26
|
+
* still host-side, never the model's choice. */
|
|
27
|
+
export declare function buildMemoriesBlock(prompt: string, workingDir: string, explicitProjectId?: string): Promise<string>;
|
|
28
|
+
//# sourceMappingURL=memory_recall.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory_recall.d.ts","sourceRoot":"","sources":["../../src/harness/memory_recall.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAK7C,eAAO,MAAM,YAAY,IAAI,CAAC;AAC9B,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAOpC;;;;;;;;;6EAS6E;AAC7E,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAqB1E;AAED;;qEAEqE;AACrE,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAS7D;AAED;;;;;;;iDAOiD;AACjD,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,MAAM,CAAC,CAajB"}
|