@useavalon/caelence-agent 0.4.0 → 0.6.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/README.md +29 -4
- package/desktop/src/DeskComposer.tsx +10 -0
- package/desktop/src/SettingsPanel.tsx +18 -0
- package/desktop/src/api.ts +12 -1
- package/desktop/src/app.css +8 -3
- package/desktop/src/desk-types.ts +1 -0
- package/desktop/src/desktop-turn.ts +7 -0
- package/desktop/src/stream.ts +1 -0
- package/desktop/src/thought.test.ts +16 -0
- package/desktop/src/thought.ts +15 -4
- package/desktop/src/turn-verbs.ts +14 -0
- package/desktop/src/useDeskPicker.ts +17 -1
- package/package.json +7 -4
- package/src/cli/main.test.ts +12 -0
- package/src/cli/main.ts +45 -5
- package/src/cli/slash-dispatch.test.ts +43 -0
- package/src/cli/slash-dispatch.ts +34 -0
- package/src/cli/slash.test.ts +1 -0
- package/src/cli/slash.ts +2 -0
- package/src/cli/tui-progress.ts +15 -3
- package/src/cli/tui.tsx +15 -1
- package/src/config.ts +38 -2
- package/src/core/events.ts +12 -9
- package/src/core/mode.ts +5 -0
- package/src/core/reasoning.test.ts +37 -0
- package/src/core/reasoning.ts +32 -0
- package/src/core/run-turn.test.ts +112 -0
- package/src/core/run-turn.ts +13 -1
- package/src/core/strands.test.ts +106 -2
- package/src/core/strands.ts +46 -11
- package/src/core/usage.test.ts +80 -1
- package/src/core/usage.ts +35 -3
- package/src/desktop/bridge.test.ts +27 -0
- package/src/desktop/bridge.ts +17 -5
- package/src/index.ts +77 -1
- package/src/memory/brief.ts +62 -0
- package/src/memory/embeddings.ts +104 -0
- package/src/memory/extract.ts +9 -8
- package/src/memory/layered.test.ts +202 -0
- package/src/memory/layered.ts +125 -0
- package/src/memory/memory-store.test.ts +299 -0
- package/src/memory/memory-store.ts +295 -0
- package/src/memory/store.ts +70 -20
- package/src/observability/langfuse.ts +9 -2
- package/src/observability/types.ts +5 -0
- package/src/runtime.test.ts +27 -0
- package/src/runtime.ts +164 -34
- package/src/status/categories.ts +64 -0
- package/src/status/default-verbs.ts +190 -0
- package/src/status/status-verb-categories.json +6167 -0
- package/src/status/status-verbs.test.ts +209 -0
- package/src/status/status-verbs.ts +62 -0
- package/src/tools/background.test.ts +304 -0
- package/src/tools/background.ts +432 -0
- package/src/tools/memory.ts +94 -0
package/src/memory/store.ts
CHANGED
|
@@ -14,6 +14,8 @@ export interface MemoryFact {
|
|
|
14
14
|
scope: MemoryScope;
|
|
15
15
|
createdAt: string;
|
|
16
16
|
sessionId?: string;
|
|
17
|
+
/** Which layer a layered store read this from ("project", "workspace"). Never stored. */
|
|
18
|
+
layer?: string;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
interface FactsFile {
|
|
@@ -62,6 +64,15 @@ export function userMemoryPath(env: Record<string, string | undefined> = process
|
|
|
62
64
|
return resolve(harnessHome(env), "memory", "facts.json");
|
|
63
65
|
}
|
|
64
66
|
|
|
67
|
+
/** Where one scope's facts are stored. */
|
|
68
|
+
export function memoryPathForScope(
|
|
69
|
+
cwd: string,
|
|
70
|
+
scope: MemoryScope,
|
|
71
|
+
env: Record<string, string | undefined> = process.env,
|
|
72
|
+
): string {
|
|
73
|
+
return scope === "user" ? userMemoryPath(env) : projectMemoryPath(cwd);
|
|
74
|
+
}
|
|
75
|
+
|
|
65
76
|
export function projectMemoryPath(cwd: string): string {
|
|
66
77
|
return resolve(cwd, ".harness", "memory", "facts.json");
|
|
67
78
|
}
|
|
@@ -108,34 +119,55 @@ export function listFacts(
|
|
|
108
119
|
return [...readFactsFile(userMemoryPath(env)), ...readFactsFile(projectMemoryPath(cwd))];
|
|
109
120
|
}
|
|
110
121
|
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Turn raw text into facts worth storing: trimmed, deduplicated against `known` hashes, and
|
|
124
|
+
* without secrets or noise. `known` gains the hash of every fact returned.
|
|
125
|
+
*/
|
|
126
|
+
export function prepareFacts(
|
|
127
|
+
known: Set<string>,
|
|
113
128
|
incoming: Array<{ text: string; scope?: MemoryScope; sessionId?: string }>,
|
|
114
|
-
env: Record<string, string | undefined> = process.env,
|
|
115
129
|
): MemoryFact[] {
|
|
116
|
-
const
|
|
117
|
-
const added: MemoryFact[] = [];
|
|
130
|
+
const out: MemoryFact[] = [];
|
|
118
131
|
for (const item of incoming) {
|
|
119
132
|
const text = normalizeFactText(item.text);
|
|
120
133
|
if (text.length < 8 || text.length > 280 || isSecretLike(text)) continue;
|
|
121
134
|
const hash = factHash(text);
|
|
122
135
|
if (known.has(hash)) continue;
|
|
123
|
-
|
|
124
|
-
|
|
136
|
+
known.add(hash);
|
|
137
|
+
out.push({
|
|
125
138
|
id: newId("mem"),
|
|
126
139
|
hash,
|
|
127
140
|
text,
|
|
128
|
-
scope,
|
|
141
|
+
scope: item.scope === "user" ? "user" : "project",
|
|
129
142
|
createdAt: new Date().toISOString(),
|
|
130
143
|
...(item.sessionId ? { sessionId: item.sessionId } : {}),
|
|
131
|
-
};
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Append prepared facts to the file for their scope. */
|
|
150
|
+
export function insertFacts(
|
|
151
|
+
cwd: string,
|
|
152
|
+
facts: MemoryFact[],
|
|
153
|
+
env: Record<string, string | undefined> = process.env,
|
|
154
|
+
): void {
|
|
155
|
+
for (const scope of ["user", "project"] as const) {
|
|
156
|
+
const rows = facts.filter((fact) => fact.scope === scope);
|
|
157
|
+
if (rows.length === 0) continue;
|
|
132
158
|
const path = pathForScope(cwd, scope, env);
|
|
133
|
-
|
|
134
|
-
facts.push(fact);
|
|
135
|
-
writeFactsFile(path, facts);
|
|
136
|
-
known.add(hash);
|
|
137
|
-
added.push(fact);
|
|
159
|
+
writeFactsFile(path, [...readFactsFile(path), ...rows]);
|
|
138
160
|
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function addFacts(
|
|
164
|
+
cwd: string,
|
|
165
|
+
incoming: Array<{ text: string; scope?: MemoryScope; sessionId?: string }>,
|
|
166
|
+
env: Record<string, string | undefined> = process.env,
|
|
167
|
+
): MemoryFact[] {
|
|
168
|
+
const known = new Set(listFacts(cwd, env).map((fact) => fact.hash));
|
|
169
|
+
const added = prepareFacts(known, incoming);
|
|
170
|
+
insertFacts(cwd, added, env);
|
|
139
171
|
return added;
|
|
140
172
|
}
|
|
141
173
|
|
|
@@ -181,13 +213,19 @@ export function searchFacts(
|
|
|
181
213
|
topK = 10,
|
|
182
214
|
env: Record<string, string | undefined> = process.env,
|
|
183
215
|
): MemoryFact[] {
|
|
184
|
-
|
|
185
|
-
|
|
216
|
+
return rankFacts(listFacts(cwd, env), query, topK);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Facts that match the query's words, best first. Undefined when the query has no usable words,
|
|
221
|
+
* which callers treat as "show the most recent".
|
|
222
|
+
*/
|
|
223
|
+
export function keywordHits<T extends MemoryFact>(facts: T[], query: string): T[] | undefined {
|
|
186
224
|
const tokens = tokenizeMemoryQuery(query);
|
|
187
|
-
if (tokens.length === 0) return
|
|
225
|
+
if (tokens.length === 0) return undefined;
|
|
188
226
|
const needle = normalizeFactText(query).toLowerCase();
|
|
189
227
|
const leftover = leftoverNameBoosts(facts, query, tokens);
|
|
190
|
-
|
|
228
|
+
return facts
|
|
191
229
|
.map((fact, index) => ({
|
|
192
230
|
fact,
|
|
193
231
|
score: scoreFact(fact, index, facts.length, tokens, needle) + (leftover.get(fact.id) ?? 0),
|
|
@@ -195,6 +233,18 @@ export function searchFacts(
|
|
|
195
233
|
.filter((row) => row.score > 1)
|
|
196
234
|
.sort((a, b) => b.score - a.score)
|
|
197
235
|
.map((row) => row.fact);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Pad a hit list to `topK` with recent user facts, as keyword recall always has. */
|
|
239
|
+
export function completeHits<T extends MemoryFact>(hits: T[], all: T[], topK: number): T[] {
|
|
240
|
+
return fillMemoryHits(hits, all, topK);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Keyword recall over facts already in memory. */
|
|
244
|
+
export function rankFacts<T extends MemoryFact>(facts: T[], query: string, topK = 10): T[] {
|
|
245
|
+
if (facts.length === 0) return [];
|
|
246
|
+
const hits = keywordHits(facts, query);
|
|
247
|
+
if (!hits) return takeRecentFacts(facts, topK);
|
|
198
248
|
return fillMemoryHits(hits, facts, topK);
|
|
199
249
|
}
|
|
200
250
|
|
|
@@ -273,11 +323,11 @@ function scoreFact(
|
|
|
273
323
|
return score;
|
|
274
324
|
}
|
|
275
325
|
|
|
276
|
-
function takeRecentFacts(facts:
|
|
326
|
+
function takeRecentFacts<T extends MemoryFact>(facts: T[], topK: number): T[] {
|
|
277
327
|
return facts.slice(-topK).reverse();
|
|
278
328
|
}
|
|
279
329
|
|
|
280
|
-
function fillMemoryHits(hits:
|
|
330
|
+
function fillMemoryHits<T extends MemoryFact>(hits: T[], all: T[], topK: number): T[] {
|
|
281
331
|
const out = hits.slice(0, topK);
|
|
282
332
|
const seen = new Set(out.map((fact) => fact.id));
|
|
283
333
|
for (const fact of all.filter((item) => item.scope === "user").reverse()) {
|
|
@@ -85,6 +85,11 @@ function observationHandle(
|
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
function reportUnavailable(options: CreateObservabilityOptions, what: string, err: unknown): void {
|
|
89
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
90
|
+
safe(() => options.onUnavailable?.(`${what}: ${detail.slice(0, 300)}`));
|
|
91
|
+
}
|
|
92
|
+
|
|
88
93
|
export async function tryCreateLangfuseObservability(
|
|
89
94
|
options: CreateObservabilityOptions = {},
|
|
90
95
|
): Promise<Observability | undefined> {
|
|
@@ -112,7 +117,8 @@ export async function tryCreateLangfuseObservability(
|
|
|
112
117
|
LangfuseSpanProcessor = otelMod.LangfuseSpanProcessor;
|
|
113
118
|
NodeSDK = sdkMod.NodeSDK;
|
|
114
119
|
LangfuseClient = clientMod.LangfuseClient;
|
|
115
|
-
} catch {
|
|
120
|
+
} catch (err) {
|
|
121
|
+
reportUnavailable(options, "the Langfuse or OpenTelemetry packages did not load", err);
|
|
116
122
|
return undefined;
|
|
117
123
|
}
|
|
118
124
|
|
|
@@ -131,7 +137,8 @@ export async function tryCreateLangfuseObservability(
|
|
|
131
137
|
const sdk = new NodeSDK({ spanProcessors: [processor] });
|
|
132
138
|
sdk.start();
|
|
133
139
|
otel = sdk;
|
|
134
|
-
} catch {
|
|
140
|
+
} catch (err) {
|
|
141
|
+
reportUnavailable(options, "the OpenTelemetry SDK did not start", err);
|
|
135
142
|
return undefined;
|
|
136
143
|
}
|
|
137
144
|
|
|
@@ -92,6 +92,11 @@ export interface CreateObservabilityOptions {
|
|
|
92
92
|
agentVersion?: string;
|
|
93
93
|
environment?: string;
|
|
94
94
|
env?: Record<string, string | undefined>;
|
|
95
|
+
/**
|
|
96
|
+
* Called once when the Langfuse keys are set but tracing could not start, with the reason.
|
|
97
|
+
* Without it the failure is silent and the caller gets the no-op observer.
|
|
98
|
+
*/
|
|
99
|
+
onUnavailable?: (reason: string) => void;
|
|
95
100
|
}
|
|
96
101
|
|
|
97
102
|
export const FEEDBACK_CATEGORIES = [
|
package/src/runtime.test.ts
CHANGED
|
@@ -125,6 +125,33 @@ describe("createHarness modes", () => {
|
|
|
125
125
|
}
|
|
126
126
|
});
|
|
127
127
|
|
|
128
|
+
test("applies reasoning effort from config and setReasoningEffort", async () => {
|
|
129
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-effort-"));
|
|
130
|
+
const previous = process.env.HARNESS_REASONING_EFFORT;
|
|
131
|
+
delete process.env.HARNESS_REASONING_EFFORT;
|
|
132
|
+
try {
|
|
133
|
+
const harness = await createHarness({
|
|
134
|
+
cwd,
|
|
135
|
+
config: { ...DEFAULT_CONFIG, reasoningEffort: "high" },
|
|
136
|
+
provider: fakeProvider,
|
|
137
|
+
constructAgent: () => ({
|
|
138
|
+
async run() {
|
|
139
|
+
throw new Error("should not run");
|
|
140
|
+
},
|
|
141
|
+
}),
|
|
142
|
+
observability: noopObservability,
|
|
143
|
+
});
|
|
144
|
+
expect(harness.reasoningEffort).toBe("high");
|
|
145
|
+
harness.setReasoningEffort("medium");
|
|
146
|
+
expect(harness.reasoningEffort).toBe("medium");
|
|
147
|
+
harness.close();
|
|
148
|
+
} finally {
|
|
149
|
+
if (previous === undefined) delete process.env.HARNESS_REASONING_EFFORT;
|
|
150
|
+
else process.env.HARNESS_REASONING_EFFORT = previous;
|
|
151
|
+
await rm(cwd, { recursive: true, force: true });
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
128
155
|
test("accumulates usage on the runtime", async () => {
|
|
129
156
|
const cwd = await mkdtemp(join(tmpdir(), "harness-spend-"));
|
|
130
157
|
try {
|
package/src/runtime.ts
CHANGED
|
@@ -33,6 +33,11 @@ import {
|
|
|
33
33
|
parseAgentMode,
|
|
34
34
|
toolsAllowedInMode,
|
|
35
35
|
} from "./core/mode.ts";
|
|
36
|
+
import {
|
|
37
|
+
parseReasoningEffort,
|
|
38
|
+
type ReasoningEffort,
|
|
39
|
+
resolveReasoningEffort,
|
|
40
|
+
} from "./core/reasoning.ts";
|
|
36
41
|
import {
|
|
37
42
|
buildSystemPrompt,
|
|
38
43
|
defaultSummarizer,
|
|
@@ -63,15 +68,17 @@ import { type ChatFn, createOpenRouterChat } from "./evals/runner.ts";
|
|
|
63
68
|
import { buildRemoteMcpTools } from "./integrations/remote-mcp.ts";
|
|
64
69
|
import { linkedMcpSources } from "./integrations/store.ts";
|
|
65
70
|
import { listUserMcpServers, userMcpToolPrefix } from "./mcp/user-servers.ts";
|
|
71
|
+
import { createOpenAICompatEmbedder } from "./memory/embeddings.ts";
|
|
66
72
|
import { extractMemories, turnMemorySource } from "./memory/extract.ts";
|
|
73
|
+
import { createFileMemoryStore, type MemoryStore } from "./memory/memory-store.ts";
|
|
67
74
|
import { isMemoryEnabled } from "./memory/prefs.ts";
|
|
68
75
|
import { formatRetrievedMemory } from "./memory/prompt.ts";
|
|
69
|
-
import { searchFacts } from "./memory/store.ts";
|
|
70
76
|
import { createObservability } from "./observability/create.ts";
|
|
71
77
|
import { noopObservability } from "./observability/noop.ts";
|
|
72
78
|
import { harnessPromptRecords, syncPrompts } from "./observability/prompts.ts";
|
|
73
79
|
import type { Observability } from "./observability/types.ts";
|
|
74
80
|
import { resolveAgentVersion } from "./observability/version.ts";
|
|
81
|
+
import { PRODUCT_NAME } from "./product.ts";
|
|
75
82
|
import { authorSkillMarkdown } from "./skills/author-skill.ts";
|
|
76
83
|
import { createHostSkill, parseSkillNewArgs } from "./skills/create-skill.ts";
|
|
77
84
|
import {
|
|
@@ -96,8 +103,18 @@ import {
|
|
|
96
103
|
skillNameFromCatalogId,
|
|
97
104
|
} from "./skills/registry.ts";
|
|
98
105
|
import { userSkillsDir } from "./skills/user-dir.ts";
|
|
106
|
+
import { resolveStatusVerbPool } from "./status/categories.ts";
|
|
107
|
+
import {
|
|
108
|
+
BACKGROUND_AGENTS_PROMPT,
|
|
109
|
+
type BackgroundAgents,
|
|
110
|
+
createBackgroundAgents,
|
|
111
|
+
EXPLORE_INSTRUCTIONS,
|
|
112
|
+
readOnlyTools,
|
|
113
|
+
type SubagentKind,
|
|
114
|
+
} from "./tools/background.ts";
|
|
99
115
|
import { createGitTools } from "./tools/git.ts";
|
|
100
116
|
import { createLocalTools } from "./tools/local.ts";
|
|
117
|
+
import { createMemoryTools } from "./tools/memory.ts";
|
|
101
118
|
import { createReadSkillTool } from "./tools/read-skill.ts";
|
|
102
119
|
import { createTaskTool } from "./tools/subagent.ts";
|
|
103
120
|
import { createMemoryTodoStore, createTodoTool } from "./tools/todo.ts";
|
|
@@ -147,6 +164,7 @@ export interface HarnessRuntime {
|
|
|
147
164
|
systemPrompt: string;
|
|
148
165
|
modelId: string;
|
|
149
166
|
mode: AgentMode;
|
|
167
|
+
reasoningEffort: ReasoningEffort;
|
|
150
168
|
activeSessionId?: string;
|
|
151
169
|
/** True when the browser tools are on for the current chat, by `@browser`, a turn option, or config. */
|
|
152
170
|
readonly browserActive: boolean;
|
|
@@ -160,6 +178,7 @@ export interface HarnessRuntime {
|
|
|
160
178
|
): Promise<Session | undefined>;
|
|
161
179
|
setModel(modelId: string): void;
|
|
162
180
|
setMode(mode: AgentMode): void;
|
|
181
|
+
setReasoningEffort(effort: ReasoningEffort): void;
|
|
163
182
|
setSession(id: string | undefined): void;
|
|
164
183
|
allowExecAlways(): void;
|
|
165
184
|
allowRiskyAlways(toolName: string): void;
|
|
@@ -190,7 +209,13 @@ export interface CreateHarnessOptions {
|
|
|
190
209
|
approvalPolicy?: ExecApprovalPolicy;
|
|
191
210
|
extraTools?: McpToolDefinition[];
|
|
192
211
|
observability?: Observability;
|
|
212
|
+
/**
|
|
213
|
+
* Where cross-session memory is kept. A host passes its own store (a database scoped to one
|
|
214
|
+
* user or project). Defaults to the facts files under `cwd` and the harness home.
|
|
215
|
+
*/
|
|
216
|
+
memory?: MemoryStore;
|
|
193
217
|
mode?: AgentMode;
|
|
218
|
+
reasoningEffort?: ReasoningEffort;
|
|
194
219
|
/** Load one-click MCP connections from ~/.harness. Default on unless a test provider is passed. */
|
|
195
220
|
loadIntegrations?: boolean;
|
|
196
221
|
/** Replaces the stdio MCP loader for the browser server. Tests only. */
|
|
@@ -236,6 +261,10 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
236
261
|
let skills = loadActiveSkills();
|
|
237
262
|
const layers = loadInstructionLayers({ cwd, projectFile: config.instructionsFile });
|
|
238
263
|
let mode = parseAgentMode(options.mode ?? config.mode);
|
|
264
|
+
let reasoningEffort = resolveReasoningEffort({
|
|
265
|
+
option: options.reasoningEffort,
|
|
266
|
+
config: config.reasoningEffort,
|
|
267
|
+
});
|
|
239
268
|
const hostAppend = options.systemAppend?.trim();
|
|
240
269
|
const composePrompt = (nextMode: AgentMode) => {
|
|
241
270
|
const base = buildSystemPrompt({
|
|
@@ -383,6 +412,7 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
383
412
|
apiKey,
|
|
384
413
|
model: id,
|
|
385
414
|
baseUrl: or.baseUrl,
|
|
415
|
+
reasoningEffort,
|
|
386
416
|
sessionId: () => activeSessionId,
|
|
387
417
|
...(title ? { title } : {}),
|
|
388
418
|
});
|
|
@@ -392,6 +422,32 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
392
422
|
let activeEmit: AgentEventEmitter = () => undefined;
|
|
393
423
|
let activeApproval: ApprovalGate | undefined;
|
|
394
424
|
const subagentsEnabled = config.subagents?.enabled !== false;
|
|
425
|
+
const statusVerbPool = resolveStatusVerbPool(config.statusVerbs).pool;
|
|
426
|
+
// Semantic recall sends fact text to the embeddings provider, so it is off until asked for.
|
|
427
|
+
const embeddingModel =
|
|
428
|
+
process.env.HARNESS_MEMORY_EMBEDDINGS?.trim() ||
|
|
429
|
+
(config.memory?.embeddings?.enabled ? config.memory.embeddings.model?.trim() || "default" : "");
|
|
430
|
+
const memoryStore: MemoryStore =
|
|
431
|
+
options.memory ??
|
|
432
|
+
createFileMemoryStore({
|
|
433
|
+
cwd,
|
|
434
|
+
...(embeddingModel && embeddingModel !== "0" && embeddingModel !== "off"
|
|
435
|
+
? {
|
|
436
|
+
embedder: createOpenAICompatEmbedder({
|
|
437
|
+
apiKey: () => apiKey,
|
|
438
|
+
baseUrl: or.baseUrl,
|
|
439
|
+
...(embeddingModel === "default" || embeddingModel === "1"
|
|
440
|
+
? {}
|
|
441
|
+
: { model: embeddingModel }),
|
|
442
|
+
}),
|
|
443
|
+
}
|
|
444
|
+
: {}),
|
|
445
|
+
});
|
|
446
|
+
/** A host that brings its own store has asked for memory; otherwise the user's setting decides. */
|
|
447
|
+
const memoryEnabled = (): boolean =>
|
|
448
|
+
options.memory
|
|
449
|
+
? config.memory?.enabled !== false
|
|
450
|
+
: isMemoryEnabled({ configEnabled: config.memory?.enabled });
|
|
395
451
|
|
|
396
452
|
const baseTools = (): McpToolDefinition[] =>
|
|
397
453
|
filterToolsForMode(mode, [
|
|
@@ -405,12 +461,26 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
405
461
|
createReadSkillTool(skills),
|
|
406
462
|
]);
|
|
407
463
|
|
|
408
|
-
|
|
464
|
+
/** The model a subagent runs on. Explore agents can use a smaller, faster one than the chat. */
|
|
465
|
+
const subagentModel = (kind: SubagentKind): string => {
|
|
466
|
+
const general = process.env.HARNESS_SUBAGENT_MODEL?.trim() || config.subagents?.model?.trim();
|
|
467
|
+
if (kind === "explore") {
|
|
468
|
+
return (
|
|
469
|
+
process.env.HARNESS_EXPLORE_MODEL?.trim() ||
|
|
470
|
+
config.subagents?.exploreModel?.trim() ||
|
|
471
|
+
general ||
|
|
472
|
+
modelId
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
return general || modelId;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
const nestedSystem = (extraSystem: string, kind: SubagentKind = "general"): string =>
|
|
409
479
|
[
|
|
410
480
|
buildSystemPrompt({
|
|
411
481
|
name: `${config.name} subagent`,
|
|
412
482
|
cwd,
|
|
413
|
-
projectInstructions: SUBAGENT_INSTRUCTIONS,
|
|
483
|
+
projectInstructions: kind === "explore" ? EXPLORE_INSTRUCTIONS : SUBAGENT_INSTRUCTIONS,
|
|
414
484
|
skillCatalog: skillCatalogPrompt(skills),
|
|
415
485
|
mode,
|
|
416
486
|
}),
|
|
@@ -437,6 +507,9 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
437
507
|
get mode() {
|
|
438
508
|
return mode;
|
|
439
509
|
},
|
|
510
|
+
get reasoningEffort() {
|
|
511
|
+
return reasoningEffort;
|
|
512
|
+
},
|
|
440
513
|
spend: undefined,
|
|
441
514
|
get activeSessionId() {
|
|
442
515
|
return activeSessionId;
|
|
@@ -468,6 +541,10 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
468
541
|
mode = parseAgentMode(next);
|
|
469
542
|
systemPrompt = composePrompt(mode);
|
|
470
543
|
},
|
|
544
|
+
setReasoningEffort(next) {
|
|
545
|
+
reasoningEffort = parseReasoningEffort(next);
|
|
546
|
+
if (!options.provider) provider = makeProvider(modelId);
|
|
547
|
+
},
|
|
471
548
|
setSession(id) {
|
|
472
549
|
if (id !== activeSessionId) resetBrowserChat();
|
|
473
550
|
activeSessionId = id;
|
|
@@ -606,42 +683,90 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
606
683
|
activeEmit = emit;
|
|
607
684
|
activeApproval = approval;
|
|
608
685
|
await resolveBrowser(userText, turnOptions?.browser === true, turnOptions?.trustedHosts);
|
|
609
|
-
const
|
|
686
|
+
const memoryOn = memoryEnabled();
|
|
687
|
+
let tools = baseTools();
|
|
688
|
+
if (memoryOn) {
|
|
689
|
+
tools.push(
|
|
690
|
+
...filterToolsForMode(
|
|
691
|
+
mode,
|
|
692
|
+
createMemoryTools(memoryStore, { sessionId: () => activeSessionId }),
|
|
693
|
+
),
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
/** One nested turn for a subagent. Explore agents get the read-only tools and their own model. */
|
|
697
|
+
const runNested = async (input: {
|
|
698
|
+
prompt: string;
|
|
699
|
+
kind: SubagentKind;
|
|
700
|
+
extraSystem: string;
|
|
701
|
+
emit: AgentEventEmitter;
|
|
702
|
+
signal?: AbortSignal;
|
|
703
|
+
}): Promise<void> => {
|
|
704
|
+
const nestedModel = subagentModel(input.kind);
|
|
705
|
+
const nestedDeps: RunTurnDeps = {
|
|
706
|
+
cwd,
|
|
707
|
+
name: `${config.name} subagent`,
|
|
708
|
+
systemPrompt: nestedSystem(input.extraSystem, input.kind),
|
|
709
|
+
tools: input.kind === "explore" ? readOnlyTools(baseTools()) : baseTools(),
|
|
710
|
+
approval: activeApproval ?? approval,
|
|
711
|
+
store: createMemorySessionStore(),
|
|
712
|
+
provider:
|
|
713
|
+
options.provider || nestedModel === modelId
|
|
714
|
+
? provider
|
|
715
|
+
: makeProvider(nestedModel, `${PRODUCT_NAME} subagent`),
|
|
716
|
+
constructAgent,
|
|
717
|
+
observability,
|
|
718
|
+
traceName: input.kind === "explore" ? "explore_subagent" : "subagent",
|
|
719
|
+
requestType: "chat",
|
|
720
|
+
agentVersion,
|
|
721
|
+
hooks,
|
|
722
|
+
mode,
|
|
723
|
+
};
|
|
724
|
+
await runTurn(nestedDeps, { message: input.prompt }, input.emit, {
|
|
725
|
+
signal: input.signal,
|
|
726
|
+
});
|
|
727
|
+
};
|
|
610
728
|
if (subagentsEnabled && toolsAllowedInMode(mode, "task")) {
|
|
611
729
|
tools.push(
|
|
612
730
|
createTaskTool({
|
|
613
731
|
skills,
|
|
614
732
|
parentEmit: () => activeEmit,
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
approval: activeApproval ?? approval,
|
|
622
|
-
store: createMemorySessionStore(),
|
|
623
|
-
provider,
|
|
624
|
-
constructAgent,
|
|
625
|
-
resolveProvider: options.provider ? undefined : (id) => makeProvider(id),
|
|
626
|
-
observability,
|
|
627
|
-
traceName: "subagent",
|
|
628
|
-
requestType: "chat",
|
|
629
|
-
agentVersion,
|
|
630
|
-
hooks,
|
|
631
|
-
mode,
|
|
632
|
-
};
|
|
633
|
-
await runTurn(nestedDeps, { message: prompt, model: modelId }, nestedEmit, {
|
|
733
|
+
run: ({ prompt, extraSystem, emit: nestedEmit }) =>
|
|
734
|
+
runNested({
|
|
735
|
+
prompt,
|
|
736
|
+
kind: "general",
|
|
737
|
+
extraSystem,
|
|
738
|
+
emit: nestedEmit,
|
|
634
739
|
signal: turnOptions?.signal,
|
|
635
|
-
})
|
|
636
|
-
},
|
|
740
|
+
}),
|
|
637
741
|
}),
|
|
638
742
|
);
|
|
639
743
|
}
|
|
640
|
-
|
|
744
|
+
// Explore agents only read, so they are offered in ask and plan mode too.
|
|
745
|
+
let background: BackgroundAgents | undefined;
|
|
746
|
+
if (subagentsEnabled && config.subagents?.background !== false) {
|
|
747
|
+
background = createBackgroundAgents({
|
|
748
|
+
skills,
|
|
749
|
+
parentEmit: () => activeEmit,
|
|
750
|
+
kinds: mode === "agent" ? ["explore", "general"] : ["explore"],
|
|
751
|
+
maxConcurrent: config.subagents?.maxConcurrent,
|
|
752
|
+
maxAgents: config.subagents?.maxPerTurn,
|
|
753
|
+
signal: turnOptions?.signal,
|
|
754
|
+
run: ({ prompt, kind, extraSystem, emit: nestedEmit, signal }) =>
|
|
755
|
+
runNested({ prompt, kind, extraSystem, emit: nestedEmit, signal }),
|
|
756
|
+
});
|
|
757
|
+
tools = [
|
|
758
|
+
...tools.map((tool) => background?.withNotices(tool) ?? tool),
|
|
759
|
+
...background.tools,
|
|
760
|
+
];
|
|
761
|
+
}
|
|
641
762
|
const deps: RunTurnDeps = {
|
|
642
763
|
cwd,
|
|
643
764
|
name: config.name,
|
|
644
|
-
systemPrompt: [
|
|
765
|
+
systemPrompt: [
|
|
766
|
+
await systemPromptWithMemory(systemPrompt, memoryStore, userText, memoryOn),
|
|
767
|
+
background ? BACKGROUND_AGENTS_PROMPT : "",
|
|
768
|
+
browserNote,
|
|
769
|
+
]
|
|
645
770
|
.filter(Boolean)
|
|
646
771
|
.join("\n\n"),
|
|
647
772
|
tools,
|
|
@@ -660,6 +785,9 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
660
785
|
decisions,
|
|
661
786
|
shortcut: options.shortcut,
|
|
662
787
|
intercept: options.intercept,
|
|
788
|
+
...(statusVerbPool.length > 0
|
|
789
|
+
? { statusVerbs: { pool: statusVerbPool, perTurn: config.statusVerbs?.perTurn } }
|
|
790
|
+
: {}),
|
|
663
791
|
};
|
|
664
792
|
const capture: AgentEventEmitter = (event) => {
|
|
665
793
|
if (event.kind === "usage") {
|
|
@@ -690,10 +818,10 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
690
818
|
},
|
|
691
819
|
capture,
|
|
692
820
|
{ signal: turnOptions?.signal },
|
|
693
|
-
);
|
|
821
|
+
).finally(() => background?.close());
|
|
694
822
|
// Assign the variable, not the setter: a new chat's first turn must keep the browser on.
|
|
695
823
|
if (session) activeSessionId = session.id;
|
|
696
|
-
scheduleMemoryExtract(memoryOn, cwd, session, provider, decisions);
|
|
824
|
+
scheduleMemoryExtract(memoryOn, cwd, memoryStore, session, provider, decisions);
|
|
697
825
|
return session;
|
|
698
826
|
},
|
|
699
827
|
close() {
|
|
@@ -708,14 +836,15 @@ export async function createHarness(options: CreateHarnessOptions): Promise<Harn
|
|
|
708
836
|
return runtime;
|
|
709
837
|
}
|
|
710
838
|
|
|
711
|
-
function systemPromptWithMemory(
|
|
839
|
+
async function systemPromptWithMemory(
|
|
712
840
|
base: string,
|
|
713
|
-
|
|
841
|
+
store: MemoryStore,
|
|
714
842
|
message: string,
|
|
715
843
|
enabled: boolean,
|
|
716
|
-
): string {
|
|
844
|
+
): Promise<string> {
|
|
717
845
|
if (!enabled) return base;
|
|
718
|
-
const
|
|
846
|
+
const facts = await store.search(message).catch(() => []);
|
|
847
|
+
const block = formatRetrievedMemory(facts);
|
|
719
848
|
if (!block) return base;
|
|
720
849
|
return `${base}\n\n## Memory\n${block}`;
|
|
721
850
|
}
|
|
@@ -723,6 +852,7 @@ function systemPromptWithMemory(
|
|
|
723
852
|
function scheduleMemoryExtract(
|
|
724
853
|
enabled: boolean,
|
|
725
854
|
cwd: string,
|
|
855
|
+
store: MemoryStore,
|
|
726
856
|
session: Session | undefined,
|
|
727
857
|
provider: MainModelProvider,
|
|
728
858
|
decisions?: ResolvedDecisions,
|
|
@@ -733,6 +863,6 @@ function scheduleMemoryExtract(
|
|
|
733
863
|
const source = turnMemorySource(session);
|
|
734
864
|
if (!source.trim() || !(await turnHasDurableFact(decisions, source))) return;
|
|
735
865
|
}
|
|
736
|
-
await extractMemories({ cwd, session, provider });
|
|
866
|
+
await extractMemories({ cwd, session, provider, store });
|
|
737
867
|
})().catch(() => undefined);
|
|
738
868
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { DEFAULT_STATUS_VERBS, type StatusVerbsConfig } from "./status-verbs.ts";
|
|
4
|
+
|
|
5
|
+
let cached: Record<string, string[]> | undefined;
|
|
6
|
+
|
|
7
|
+
/** The themed verb lists, by name. Read from disk on first use. */
|
|
8
|
+
export function statusVerbCategories(): Record<string, string[]> {
|
|
9
|
+
if (cached) return cached;
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(
|
|
12
|
+
readFileSync(join(import.meta.dir, "status-verb-categories.json"), "utf8"),
|
|
13
|
+
) as Record<string, unknown>;
|
|
14
|
+
const out: Record<string, string[]> = {};
|
|
15
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
16
|
+
if (!Array.isArray(value)) continue;
|
|
17
|
+
out[name] = value.filter((item): item is string => typeof item === "string");
|
|
18
|
+
}
|
|
19
|
+
cached = out;
|
|
20
|
+
} catch {
|
|
21
|
+
cached = {};
|
|
22
|
+
}
|
|
23
|
+
return cached;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function statusVerbCategoryNames(): string[] {
|
|
27
|
+
return Object.keys(statusVerbCategories());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The verbs a config asks for: the built-in list, the named categories and any custom ones,
|
|
32
|
+
* without duplicates. Empty when verbs are turned off. `unknown` lists category names that
|
|
33
|
+
* matched nothing, so a host can tell the user about a typo.
|
|
34
|
+
*/
|
|
35
|
+
export function resolveStatusVerbPool(config: StatusVerbsConfig | undefined): {
|
|
36
|
+
pool: string[];
|
|
37
|
+
unknown: string[];
|
|
38
|
+
} {
|
|
39
|
+
if (config?.enabled === false) return { pool: [], unknown: [] };
|
|
40
|
+
const pool = new Set<string>();
|
|
41
|
+
const unknown: string[] = [];
|
|
42
|
+
if (config?.defaults !== false) for (const verb of DEFAULT_STATUS_VERBS) pool.add(verb);
|
|
43
|
+
if (config?.categories) {
|
|
44
|
+
const all = statusVerbCategories();
|
|
45
|
+
const byLower = new Map(Object.keys(all).map((name) => [name.toLowerCase(), name]));
|
|
46
|
+
const names =
|
|
47
|
+
config.categories === "all"
|
|
48
|
+
? Object.keys(all)
|
|
49
|
+
: config.categories.map((raw) => {
|
|
50
|
+
const name = byLower.get(raw.trim().toLowerCase());
|
|
51
|
+
if (!name) unknown.push(raw);
|
|
52
|
+
return name;
|
|
53
|
+
});
|
|
54
|
+
for (const name of names) {
|
|
55
|
+
if (!name) continue;
|
|
56
|
+
for (const verb of all[name] ?? []) pool.add(verb);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (const verb of config?.custom ?? []) {
|
|
60
|
+
const text = verb.trim();
|
|
61
|
+
if (text) pool.add(text);
|
|
62
|
+
}
|
|
63
|
+
return { pool: [...pool], unknown };
|
|
64
|
+
}
|