@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
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { resolveOpenRouter } from "../config.ts";
|
|
2
2
|
import { errorMessage } from "../core/errors.ts";
|
|
3
3
|
import { AGENT_MODES, tryParseAgentMode } from "../core/mode.ts";
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_REASONING_EFFORT,
|
|
6
|
+
REASONING_EFFORT_USAGE,
|
|
7
|
+
REASONING_EFFORTS,
|
|
8
|
+
tryParseReasoningEffort,
|
|
9
|
+
} from "../core/reasoning.ts";
|
|
4
10
|
import type { Session } from "../core/session.ts";
|
|
5
11
|
import { maskSecret, writeOpenRouterKey } from "../desktop/secrets.ts";
|
|
6
12
|
import { listEvalSuites, runNamedEval } from "../evals/runner.ts";
|
|
@@ -37,6 +43,7 @@ import { formatSkillList, parseSlashLine, resolveSlashSubmit, SLASH_HELP } from
|
|
|
37
43
|
export type SlashPickerKind =
|
|
38
44
|
| "model"
|
|
39
45
|
| "mode"
|
|
46
|
+
| "effort"
|
|
40
47
|
| "resume"
|
|
41
48
|
| "image"
|
|
42
49
|
| "video"
|
|
@@ -58,6 +65,7 @@ export type SlashOutcome =
|
|
|
58
65
|
const PICKER_KINDS = new Set<SlashPickerKind>([
|
|
59
66
|
"model",
|
|
60
67
|
"mode",
|
|
68
|
+
"effort",
|
|
61
69
|
"resume",
|
|
62
70
|
"image",
|
|
63
71
|
"video",
|
|
@@ -292,6 +300,25 @@ async function handleMode(harness: HarnessRuntime, arg: string): Promise<SlashOu
|
|
|
292
300
|
};
|
|
293
301
|
}
|
|
294
302
|
|
|
303
|
+
async function handleEffort(harness: HarnessRuntime, arg: string): Promise<SlashOutcome> {
|
|
304
|
+
if (arg) {
|
|
305
|
+
const next = tryParseReasoningEffort(arg);
|
|
306
|
+
if (!next) return { kind: "text", text: REASONING_EFFORT_USAGE };
|
|
307
|
+
harness.setReasoningEffort(next);
|
|
308
|
+
return { kind: "applied" };
|
|
309
|
+
}
|
|
310
|
+
return {
|
|
311
|
+
kind: "picker",
|
|
312
|
+
title: "Reasoning effort",
|
|
313
|
+
picker: "effort",
|
|
314
|
+
items: REASONING_EFFORTS.map((item) => ({
|
|
315
|
+
id: item,
|
|
316
|
+
label: item,
|
|
317
|
+
...(item === DEFAULT_REASONING_EFFORT ? { hint: "default" } : {}),
|
|
318
|
+
})),
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
295
322
|
async function handleMediaPicker(
|
|
296
323
|
harness: HarnessRuntime,
|
|
297
324
|
arg: string,
|
|
@@ -437,6 +464,7 @@ const SLASH_HANDLERS: Record<string, SlashHandler> = {
|
|
|
437
464
|
},
|
|
438
465
|
model: handleModel,
|
|
439
466
|
mode: handleMode,
|
|
467
|
+
effort: handleEffort,
|
|
440
468
|
compact: async (harness) => {
|
|
441
469
|
const ok = await harness.compact();
|
|
442
470
|
return { kind: "text", text: ok ? "Compacted earlier turns." : "Nothing to compact." };
|
|
@@ -504,6 +532,12 @@ export async function applyPickerChoice(
|
|
|
504
532
|
harness.setMode(next);
|
|
505
533
|
return { kind: "applied" };
|
|
506
534
|
}
|
|
535
|
+
if (picker === "effort") {
|
|
536
|
+
const next = tryParseReasoningEffort(id);
|
|
537
|
+
if (!next) return { kind: "text", text: REASONING_EFFORT_USAGE };
|
|
538
|
+
harness.setReasoningEffort(next);
|
|
539
|
+
return { kind: "applied" };
|
|
540
|
+
}
|
|
507
541
|
const writePref = MEDIA_PICKER_PREFS[picker];
|
|
508
542
|
if (writePref) {
|
|
509
543
|
writePref(id);
|
package/src/cli/slash.test.ts
CHANGED
|
@@ -121,6 +121,7 @@ describe("resolveSlashSubmit", () => {
|
|
|
121
121
|
test("opens pickers for empty model, mode, and media commands", () => {
|
|
122
122
|
expect(resolveSlashSubmit("/image")).toEqual({ action: "send", line: "/image" });
|
|
123
123
|
expect(resolveSlashSubmit("/mode")).toEqual({ action: "send", line: "/mode" });
|
|
124
|
+
expect(resolveSlashSubmit("/effort")).toEqual({ action: "send", line: "/effort" });
|
|
124
125
|
expect(resolveSlashSubmit("/integrations")).toEqual({ action: "send", line: "/integrations" });
|
|
125
126
|
expect(resolveSlashSubmit("/skill find")).toEqual({ action: "send", line: "/skill find" });
|
|
126
127
|
expect(resolveSlashSubmit("/settings")).toEqual({ action: "send", line: "/settings" });
|
package/src/cli/slash.ts
CHANGED
|
@@ -19,6 +19,7 @@ export const SLASH_COMMANDS: SlashCommand[] = [
|
|
|
19
19
|
{ name: "resume", hint: "pick a session by title" },
|
|
20
20
|
{ name: "model", hint: "pick a model" },
|
|
21
21
|
{ name: "mode", hint: "ask, plan, or agent" },
|
|
22
|
+
{ name: "effort", hint: "reasoning effort" },
|
|
22
23
|
{ name: "compact", hint: "summarize earlier turns" },
|
|
23
24
|
{ name: "memory", hint: "remember across chats", arg: true },
|
|
24
25
|
{ name: "image", hint: "generate an image", arg: true, slot: "[prompt]" },
|
|
@@ -58,6 +59,7 @@ export function formatSlashCommand(command: SlashCommand): string {
|
|
|
58
59
|
export const PICKS_WHEN_EMPTY = new Set([
|
|
59
60
|
"model",
|
|
60
61
|
"mode",
|
|
62
|
+
"effort",
|
|
61
63
|
"resume",
|
|
62
64
|
"image",
|
|
63
65
|
"video",
|
package/src/cli/tui-progress.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { statusVerbAt } from "../status/status-verbs.ts";
|
|
1
2
|
import { clipLabel } from "./tui-layout.ts";
|
|
2
3
|
import type { TurnToolLine } from "./tui-stream.ts";
|
|
3
4
|
|
|
@@ -5,7 +6,15 @@ export const WARMUP_PHRASES = ["Thinking", "Looking this over", "Working it out"
|
|
|
5
6
|
|
|
6
7
|
const TICK_FRAMES = ["", ".", "..", "..."] as const;
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
/** Progress ticks a verb stays up before the next one. A tick is 450 ms, so about 3 seconds. */
|
|
10
|
+
const TICKS_PER_VERB = 7;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* What to show while the agent is thinking. With the turn's verbs it walks through them; without
|
|
14
|
+
* (verbs turned off, or the event has not arrived) it is one of the plain phrases.
|
|
15
|
+
*/
|
|
16
|
+
export function warmupHeadline(seed: number, verbs: readonly string[] = [], tick = 0): string {
|
|
17
|
+
if (verbs.length > 0) return statusVerbAt(verbs, tick, TICKS_PER_VERB);
|
|
9
18
|
return WARMUP_PHRASES[Math.abs(seed) % WARMUP_PHRASES.length] ?? "Thinking";
|
|
10
19
|
}
|
|
11
20
|
|
|
@@ -37,6 +46,8 @@ export function turnProgressLabel(input: {
|
|
|
37
46
|
assistantStarted: boolean;
|
|
38
47
|
tick: number;
|
|
39
48
|
warmupSeed: number;
|
|
49
|
+
/** The turn's verbs from its `status` event. */
|
|
50
|
+
verbs?: readonly string[];
|
|
40
51
|
}): string {
|
|
41
52
|
if (input.assistantStarted) {
|
|
42
53
|
const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
|
|
@@ -47,9 +58,10 @@ export function turnProgressLabel(input: {
|
|
|
47
58
|
const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
|
|
48
59
|
return `${toolActivityLabel(running)}${frame}`;
|
|
49
60
|
}
|
|
61
|
+
const headline = warmupHeadline(input.warmupSeed, input.verbs, input.tick);
|
|
50
62
|
if (input.tools.length > 0) {
|
|
51
|
-
return `${
|
|
63
|
+
return `${headline}…`;
|
|
52
64
|
}
|
|
53
65
|
const frame = TICK_FRAMES[input.tick % TICK_FRAMES.length] ?? "";
|
|
54
|
-
return `${
|
|
66
|
+
return `${headline}${frame}`;
|
|
55
67
|
}
|
package/src/cli/tui.tsx
CHANGED
|
@@ -95,6 +95,7 @@ function applyEvent(lines: StreamLine[], event: AgentEvent): StreamLine[] {
|
|
|
95
95
|
case "usage":
|
|
96
96
|
case "completion":
|
|
97
97
|
case "approval_request":
|
|
98
|
+
case "status":
|
|
98
99
|
return lines;
|
|
99
100
|
}
|
|
100
101
|
}
|
|
@@ -169,6 +170,7 @@ function Header(
|
|
|
169
170
|
title: string;
|
|
170
171
|
status: UiStatus;
|
|
171
172
|
mode: string;
|
|
173
|
+
effort: string;
|
|
172
174
|
spend?: string;
|
|
173
175
|
}>,
|
|
174
176
|
): React.ReactElement {
|
|
@@ -183,7 +185,7 @@ function Header(
|
|
|
183
185
|
<Text bold> {PRODUCT_NAME}</Text>
|
|
184
186
|
<Text color={props.theme.muted}>
|
|
185
187
|
{" "}
|
|
186
|
-
{props.title} · {props.mode} · {label}
|
|
188
|
+
{props.title} · {props.mode} · {props.effort} · {label}
|
|
187
189
|
{target && target.toLowerCase() !== label.toLowerCase() ? ` · ${target}` : ""}
|
|
188
190
|
{props.spend ? ` · ${props.spend}` : ""}
|
|
189
191
|
</Text>
|
|
@@ -463,6 +465,7 @@ function ApprovalCard(props: Readonly<{ theme: Theme; req: ApprovalRequest }>):
|
|
|
463
465
|
function pickerSelectedValue(picker: SlashPickerKind, harness: HarnessRuntime): string | undefined {
|
|
464
466
|
if (picker === "model") return harness.modelId;
|
|
465
467
|
if (picker === "mode") return harness.mode;
|
|
468
|
+
if (picker === "effort") return harness.reasoningEffort;
|
|
466
469
|
const prefs = readMediaPrefs();
|
|
467
470
|
if (picker === "image") return prefs.imageModel;
|
|
468
471
|
if (picker === "video") return prefs.videoModel;
|
|
@@ -656,11 +659,13 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
656
659
|
const [lines, setLines] = useState<StreamLine[]>([]);
|
|
657
660
|
const [turnTools, setTurnTools] = useState<TurnToolLine[]>([]);
|
|
658
661
|
const [progressTick, setProgressTick] = useState(0);
|
|
662
|
+
const [turnVerbs, setTurnVerbs] = useState<string[]>([]);
|
|
659
663
|
const [value, setValue] = useState("");
|
|
660
664
|
const [status, setStatus] = useState<UiStatus>("idle");
|
|
661
665
|
const [title, setTitle] = useState("New chat");
|
|
662
666
|
const [model, setModel] = useState(harness.modelId);
|
|
663
667
|
const [mode, setMode] = useState(harness.mode);
|
|
668
|
+
const [effort, setEffort] = useState(harness.reasoningEffort);
|
|
664
669
|
const [mediaLine, setMediaLine] = useState(() => formatMediaComposerLine(readMediaPrefs()));
|
|
665
670
|
const [spend, setSpend] = useState<TokenUsage | undefined>(harness.spend);
|
|
666
671
|
const [slashIndex, setSlashIndex] = useState(0);
|
|
@@ -770,6 +775,7 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
770
775
|
}
|
|
771
776
|
setModel(harness.modelId);
|
|
772
777
|
setMode(harness.mode);
|
|
778
|
+
setEffort(harness.reasoningEffort);
|
|
773
779
|
refreshMedia();
|
|
774
780
|
const nextDraft = draftAfterPicker(state.kind);
|
|
775
781
|
if (nextDraft) setValue(nextDraft);
|
|
@@ -813,6 +819,7 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
813
819
|
if (outcome.kind === "applied" || outcome.kind === "hold") {
|
|
814
820
|
setModel(harness.modelId);
|
|
815
821
|
setMode(harness.mode);
|
|
822
|
+
setEffort(harness.reasoningEffort);
|
|
816
823
|
refreshMedia();
|
|
817
824
|
return;
|
|
818
825
|
}
|
|
@@ -824,6 +831,7 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
824
831
|
pushSystem(outcome.text);
|
|
825
832
|
setModel(harness.modelId);
|
|
826
833
|
setMode(harness.mode);
|
|
834
|
+
setEffort(harness.reasoningEffort);
|
|
827
835
|
return;
|
|
828
836
|
}
|
|
829
837
|
if (outcome.kind === "clear") {
|
|
@@ -892,6 +900,10 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
892
900
|
trimmed,
|
|
893
901
|
(event) => {
|
|
894
902
|
if (event.kind === "session_meta") setTitle(event.title);
|
|
903
|
+
if (event.kind === "status") {
|
|
904
|
+
setTurnVerbs(event.verbs);
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
895
907
|
if (event.kind === "usage") setSpend(harness.spend);
|
|
896
908
|
if (event.kind === "completion") {
|
|
897
909
|
setTurnTools((tools) => {
|
|
@@ -960,6 +972,7 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
960
972
|
assistantStarted,
|
|
961
973
|
tick: progressTick,
|
|
962
974
|
warmupSeed,
|
|
975
|
+
verbs: turnVerbs,
|
|
963
976
|
});
|
|
964
977
|
const showProgress = status === "running";
|
|
965
978
|
const chrome = tuiChromeRows({
|
|
@@ -981,6 +994,7 @@ function App(props: Readonly<{ harness: HarnessRuntime; theme: Theme }>): React.
|
|
|
981
994
|
title={title}
|
|
982
995
|
status={status}
|
|
983
996
|
mode={mode}
|
|
997
|
+
effort={effort}
|
|
984
998
|
spend={spend ? formatUsage(spend) : undefined}
|
|
985
999
|
/>
|
|
986
1000
|
{picker ? (
|
package/src/config.ts
CHANGED
|
@@ -6,7 +6,9 @@ import type { Theme, ThemeName } from "./cli/theme.ts";
|
|
|
6
6
|
import type { ExecApprovalPolicy } from "./core/approval.ts";
|
|
7
7
|
import type { HookConfig } from "./core/hooks.ts";
|
|
8
8
|
import type { AgentMode } from "./core/mode.ts";
|
|
9
|
+
import type { ReasoningEffort } from "./core/reasoning.ts";
|
|
9
10
|
import type { HarnessDecisionsConfig } from "./decisions/config.ts";
|
|
11
|
+
import type { StatusVerbsConfig } from "./status/status-verbs.ts";
|
|
10
12
|
|
|
11
13
|
export interface ExtraMcpServerConfig {
|
|
12
14
|
command: string;
|
|
@@ -17,6 +19,11 @@ export interface ExtraMcpServerConfig {
|
|
|
17
19
|
export interface HarnessConfig {
|
|
18
20
|
name: string;
|
|
19
21
|
model: string;
|
|
22
|
+
/**
|
|
23
|
+
* OpenRouter reasoning effort for the chat model (`none`, `minimal`, `low`, `medium`, `high`,
|
|
24
|
+
* `xhigh`). Defaults to `low`. `HARNESS_REASONING_EFFORT` overrides it.
|
|
25
|
+
*/
|
|
26
|
+
reasoningEffort?: ReasoningEffort;
|
|
20
27
|
instructionsFile: string;
|
|
21
28
|
skillsDir: string;
|
|
22
29
|
evalsDir: string;
|
|
@@ -24,10 +31,38 @@ export interface HarnessConfig {
|
|
|
24
31
|
tools: { exec: { approval: ExecApprovalPolicy } };
|
|
25
32
|
mcp: ExtraMcpServerConfig[];
|
|
26
33
|
openrouterBaseUrl?: string;
|
|
27
|
-
subagents?: {
|
|
34
|
+
subagents?: {
|
|
35
|
+
enabled?: boolean;
|
|
36
|
+
/** Background agents (spawn_agent / await_agents). On unless this is false. */
|
|
37
|
+
background?: boolean;
|
|
38
|
+
/** Model for subagents. Defaults to the chat model. HARNESS_SUBAGENT_MODEL overrides it. */
|
|
39
|
+
model?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Model for read-only explore agents. A small fast one makes search fan-out quick and cheap.
|
|
42
|
+
* Defaults to `model`. HARNESS_EXPLORE_MODEL overrides it.
|
|
43
|
+
*/
|
|
44
|
+
exploreModel?: string;
|
|
45
|
+
/** Background agents running at once (default 4). */
|
|
46
|
+
maxConcurrent?: number;
|
|
47
|
+
/** Background agents one turn may start (default 12). */
|
|
48
|
+
maxPerTurn?: number;
|
|
49
|
+
};
|
|
28
50
|
observability?: { enabled?: boolean };
|
|
29
51
|
/** Cross-session memory. Off unless this is true or ~/.harness/memory.json enables it. */
|
|
30
|
-
memory?: {
|
|
52
|
+
memory?: {
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Semantic recall. Fact text is sent to the embeddings endpoint of the OpenRouter base URL,
|
|
56
|
+
* so this is off unless enabled here or HARNESS_MEMORY_EMBEDDINGS names a model.
|
|
57
|
+
*/
|
|
58
|
+
embeddings?: { enabled?: boolean; model?: string };
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* The verbs shown while the agent is thinking ("Pondering", "Brewing"). The built-in list is
|
|
62
|
+
* on by default; add themed `categories` or your own `custom` ones, or set `enabled: false`
|
|
63
|
+
* for a plain "Thinking".
|
|
64
|
+
*/
|
|
65
|
+
statusVerbs?: StatusVerbsConfig;
|
|
31
66
|
/** Default agent. ask = read only. plan = read + todos. */
|
|
32
67
|
mode?: AgentMode;
|
|
33
68
|
hooks?: HookConfig[];
|
|
@@ -51,6 +86,7 @@ export const OPENROUTER_DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
|
|
|
51
86
|
export const DEFAULT_CONFIG: HarnessConfig = {
|
|
52
87
|
name: "caelence",
|
|
53
88
|
model: DEFAULT_MODEL,
|
|
89
|
+
reasoningEffort: "low",
|
|
54
90
|
instructionsFile: "AGENTS.md",
|
|
55
91
|
skillsDir: "skills",
|
|
56
92
|
evalsDir: "evals",
|
package/src/core/events.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* Agent event kinds. `error` and `completion` are the two terminal kinds.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import type { TokenUsage } from "./usage.ts";
|
|
6
|
+
|
|
5
7
|
export type AgentEvent =
|
|
6
8
|
| { kind: "text_delta"; text: string }
|
|
7
9
|
| { kind: "reasoning_delta"; text: string }
|
|
@@ -23,6 +25,11 @@ export type AgentEvent =
|
|
|
23
25
|
reason?: string;
|
|
24
26
|
}
|
|
25
27
|
| { kind: "session_meta"; sessionId: string; title: string }
|
|
28
|
+
/**
|
|
29
|
+
* Sent once at the start of a turn: the verbs a UI rotates through while the agent is thinking
|
|
30
|
+
* ("Pondering", "Brewing"). A UI that ignores it shows its own plain label.
|
|
31
|
+
*/
|
|
32
|
+
| { kind: "status"; verbs: string[] }
|
|
26
33
|
| {
|
|
27
34
|
kind: "usage";
|
|
28
35
|
promptTokens: number;
|
|
@@ -80,13 +87,9 @@ export interface MainModelProvider {
|
|
|
80
87
|
request: ProviderTurnRequest,
|
|
81
88
|
options?: { signal?: AbortSignal },
|
|
82
89
|
): AsyncIterable<ProviderStreamEvent>;
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
totalTokens: number;
|
|
89
|
-
costUsd?: number;
|
|
90
|
-
}
|
|
91
|
-
| undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Billed usage for the turn's model calls, if the provider reported it.
|
|
92
|
+
* May return a promise so streamed OpenRouter usage can finish arriving.
|
|
93
|
+
*/
|
|
94
|
+
takeUsage?(): TokenUsage | undefined | Promise<TokenUsage | undefined>;
|
|
92
95
|
}
|
package/src/core/mode.ts
CHANGED
|
@@ -11,6 +11,11 @@ const READ_TOOLS = new Set([
|
|
|
11
11
|
"git_status",
|
|
12
12
|
"git_diff",
|
|
13
13
|
"git_log",
|
|
14
|
+
"memory_search",
|
|
15
|
+
// Background agents are limited to read-only explorers outside agent mode.
|
|
16
|
+
"spawn_agent",
|
|
17
|
+
"await_agents",
|
|
18
|
+
"cancel_agent",
|
|
14
19
|
]);
|
|
15
20
|
|
|
16
21
|
const PLAN_TOOLS = new Set([...READ_TOOLS, "todo_write"]);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_REASONING_EFFORT,
|
|
4
|
+
parseReasoningEffort,
|
|
5
|
+
REASONING_EFFORTS,
|
|
6
|
+
resolveReasoningEffort,
|
|
7
|
+
tryParseReasoningEffort,
|
|
8
|
+
} from "./reasoning.ts";
|
|
9
|
+
|
|
10
|
+
describe("reasoning effort", () => {
|
|
11
|
+
test("accepts OpenRouter effort values", () => {
|
|
12
|
+
expect(REASONING_EFFORTS).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"]);
|
|
13
|
+
expect(tryParseReasoningEffort("HIGH")).toBe("high");
|
|
14
|
+
expect(tryParseReasoningEffort(" xhigh ")).toBe("xhigh");
|
|
15
|
+
expect(tryParseReasoningEffort("max")).toBeUndefined();
|
|
16
|
+
expect(parseReasoningEffort(undefined)).toBe(DEFAULT_REASONING_EFFORT);
|
|
17
|
+
expect(parseReasoningEffort("nope")).toBe("low");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("prefers option, then env, then config", () => {
|
|
21
|
+
expect(
|
|
22
|
+
resolveReasoningEffort({
|
|
23
|
+
option: "medium",
|
|
24
|
+
config: "high",
|
|
25
|
+
env: { HARNESS_REASONING_EFFORT: "xhigh" },
|
|
26
|
+
}),
|
|
27
|
+
).toBe("medium");
|
|
28
|
+
expect(
|
|
29
|
+
resolveReasoningEffort({
|
|
30
|
+
config: "high",
|
|
31
|
+
env: { HARNESS_REASONING_EFFORT: "xhigh" },
|
|
32
|
+
}),
|
|
33
|
+
).toBe("xhigh");
|
|
34
|
+
expect(resolveReasoningEffort({ config: "minimal", env: {} })).toBe("minimal");
|
|
35
|
+
expect(resolveReasoningEffort({ env: { HARNESS_REASONING_EFFORT: "nope" } })).toBe("low");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** OpenRouter `reasoning.effort` values. */
|
|
2
|
+
export const REASONING_EFFORTS = ["none", "minimal", "low", "medium", "high", "xhigh"] as const;
|
|
3
|
+
export type ReasoningEffort = (typeof REASONING_EFFORTS)[number];
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_REASONING_EFFORT: ReasoningEffort = "low";
|
|
6
|
+
|
|
7
|
+
export const REASONING_EFFORT_USAGE = "Effort is none, minimal, low, medium, high, or xhigh.";
|
|
8
|
+
|
|
9
|
+
export function tryParseReasoningEffort(raw: string | undefined): ReasoningEffort | undefined {
|
|
10
|
+
const value = raw?.trim().toLowerCase();
|
|
11
|
+
return (REASONING_EFFORTS as readonly string[]).includes(value ?? "")
|
|
12
|
+
? (value as ReasoningEffort)
|
|
13
|
+
: undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function parseReasoningEffort(raw: string | undefined): ReasoningEffort {
|
|
17
|
+
return tryParseReasoningEffort(raw) ?? DEFAULT_REASONING_EFFORT;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resolveReasoningEffort(input: {
|
|
21
|
+
option?: string;
|
|
22
|
+
config?: string;
|
|
23
|
+
env?: Record<string, string | undefined>;
|
|
24
|
+
}): ReasoningEffort {
|
|
25
|
+
const env = input.env ?? process.env;
|
|
26
|
+
return (
|
|
27
|
+
tryParseReasoningEffort(input.option) ??
|
|
28
|
+
tryParseReasoningEffort(env.HARNESS_REASONING_EFFORT) ??
|
|
29
|
+
tryParseReasoningEffort(input.config) ??
|
|
30
|
+
DEFAULT_REASONING_EFFORT
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -143,6 +143,118 @@ describe("runTurn", () => {
|
|
|
143
143
|
}
|
|
144
144
|
});
|
|
145
145
|
|
|
146
|
+
test("waits for async takeUsage so streamed cost is not dropped", async () => {
|
|
147
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-usage-async-"));
|
|
148
|
+
try {
|
|
149
|
+
const provider: MainModelProvider = {
|
|
150
|
+
kind: "fake",
|
|
151
|
+
modelId: "fake",
|
|
152
|
+
async *stream() {
|
|
153
|
+
yield { type: "done" };
|
|
154
|
+
},
|
|
155
|
+
async takeUsage() {
|
|
156
|
+
await Promise.resolve();
|
|
157
|
+
return {
|
|
158
|
+
promptTokens: 80_000,
|
|
159
|
+
completionTokens: 4_000,
|
|
160
|
+
totalTokens: 84_000,
|
|
161
|
+
costUsd: 5.4,
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
const events: AgentEvent[] = [];
|
|
166
|
+
await runTurn(
|
|
167
|
+
{
|
|
168
|
+
cwd,
|
|
169
|
+
name: "test",
|
|
170
|
+
systemPrompt: "test",
|
|
171
|
+
tools: [],
|
|
172
|
+
approval: createExecApprovalGate({ policy: "deny" }),
|
|
173
|
+
store: createFileSessionStore(cwd),
|
|
174
|
+
provider,
|
|
175
|
+
constructAgent: () => ({
|
|
176
|
+
async run(_message, emit) {
|
|
177
|
+
emit({ kind: "text_delta", text: "ok" });
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
181
|
+
{ message: "hi" },
|
|
182
|
+
(event) => events.push(event),
|
|
183
|
+
);
|
|
184
|
+
const usage = events.find((event) => event.kind === "usage");
|
|
185
|
+
expect(usage).toMatchObject({ kind: "usage", costUsd: 5.4, promptTokens: 80_000 });
|
|
186
|
+
} finally {
|
|
187
|
+
await rm(cwd, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("does not keep billed usage from a failed turn for the next turn", async () => {
|
|
192
|
+
const cwd = await mkdtemp(join(tmpdir(), "harness-usage-fail-"));
|
|
193
|
+
try {
|
|
194
|
+
let stored:
|
|
195
|
+
| { promptTokens: number; completionTokens: number; totalTokens: number; costUsd: number }
|
|
196
|
+
| undefined = {
|
|
197
|
+
promptTokens: 30,
|
|
198
|
+
completionTokens: 5,
|
|
199
|
+
totalTokens: 35,
|
|
200
|
+
costUsd: 0.03,
|
|
201
|
+
};
|
|
202
|
+
const provider: MainModelProvider = {
|
|
203
|
+
kind: "fake",
|
|
204
|
+
modelId: "fake",
|
|
205
|
+
async *stream() {
|
|
206
|
+
yield { type: "done" };
|
|
207
|
+
},
|
|
208
|
+
takeUsage() {
|
|
209
|
+
const usage = stored;
|
|
210
|
+
stored = undefined;
|
|
211
|
+
return usage;
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
const deps = {
|
|
215
|
+
cwd,
|
|
216
|
+
name: "test",
|
|
217
|
+
systemPrompt: "test",
|
|
218
|
+
tools: [],
|
|
219
|
+
approval: createExecApprovalGate({ policy: "deny" }),
|
|
220
|
+
store: createFileSessionStore(cwd),
|
|
221
|
+
provider,
|
|
222
|
+
};
|
|
223
|
+
const first: AgentEvent[] = [];
|
|
224
|
+
await runTurn(
|
|
225
|
+
{
|
|
226
|
+
...deps,
|
|
227
|
+
constructAgent: () => ({
|
|
228
|
+
async run() {
|
|
229
|
+
throw new Error("model failed");
|
|
230
|
+
},
|
|
231
|
+
}),
|
|
232
|
+
},
|
|
233
|
+
{ message: "hi" },
|
|
234
|
+
(event) => first.push(event),
|
|
235
|
+
);
|
|
236
|
+
expect(first.some((event) => event.kind === "usage")).toBe(false);
|
|
237
|
+
expect(first.some((event) => event.kind === "error")).toBe(true);
|
|
238
|
+
|
|
239
|
+
const second: AgentEvent[] = [];
|
|
240
|
+
await runTurn(
|
|
241
|
+
{
|
|
242
|
+
...deps,
|
|
243
|
+
constructAgent: () => ({
|
|
244
|
+
async run(_message, emit) {
|
|
245
|
+
emit({ kind: "text_delta", text: "ok" });
|
|
246
|
+
},
|
|
247
|
+
}),
|
|
248
|
+
},
|
|
249
|
+
{ message: "again" },
|
|
250
|
+
(event) => second.push(event),
|
|
251
|
+
);
|
|
252
|
+
expect(second.some((event) => event.kind === "usage")).toBe(false);
|
|
253
|
+
} finally {
|
|
254
|
+
await rm(cwd, { recursive: true, force: true });
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
146
258
|
test("emits todos after todo_write and honors a denying pre_tool hook", async () => {
|
|
147
259
|
const cwd = await mkdtemp(join(tmpdir(), "harness-todos-"));
|
|
148
260
|
try {
|
package/src/core/run-turn.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { ShortcutPlanner } from "../decisions/shortcuts.ts";
|
|
|
3
3
|
import { noopObservability } from "../observability/noop.ts";
|
|
4
4
|
import { sanitize } from "../observability/sanitize.ts";
|
|
5
5
|
import type { Observability } from "../observability/types.ts";
|
|
6
|
+
import { pickStatusVerbs } from "../status/status-verbs.ts";
|
|
6
7
|
import type { ApprovalGate } from "./approval.ts";
|
|
7
8
|
import { isFileWriteTool, isRiskyToolCall } from "./approval.ts";
|
|
8
9
|
import { errorMessage, isAbortError } from "./errors.ts";
|
|
@@ -104,6 +105,11 @@ export interface RunTurnDeps {
|
|
|
104
105
|
shortcut?: ShortcutPlanner;
|
|
105
106
|
/** Host handler that can answer a whole turn without the chat model. */
|
|
106
107
|
intercept?: TurnInterceptor;
|
|
108
|
+
/**
|
|
109
|
+
* Verbs to pick from for this turn's `status` event. Leave it out for a nested turn, or to
|
|
110
|
+
* send no verbs at all.
|
|
111
|
+
*/
|
|
112
|
+
statusVerbs?: { pool: readonly string[]; perTurn?: number };
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
export async function runTurn(
|
|
@@ -139,6 +145,11 @@ export async function runTurn(
|
|
|
139
145
|
session = undefined;
|
|
140
146
|
}
|
|
141
147
|
|
|
148
|
+
if (deps.statusVerbs && deps.statusVerbs.pool.length > 0) {
|
|
149
|
+
const verbs = pickStatusVerbs(deps.statusVerbs.pool, deps.statusVerbs.perTurn);
|
|
150
|
+
if (verbs.length > 0) emit({ kind: "status", verbs });
|
|
151
|
+
}
|
|
152
|
+
|
|
142
153
|
const obs = deps.observability ?? noopObservability;
|
|
143
154
|
const startedAt = Date.now();
|
|
144
155
|
return obs.startTurn(
|
|
@@ -325,7 +336,7 @@ export async function runTurn(
|
|
|
325
336
|
async (gen) => {
|
|
326
337
|
gen.update({ model: provider.modelId, input: { message: req.message } });
|
|
327
338
|
await agent.run(req.message, capture, { signal: options.signal });
|
|
328
|
-
const usage = provider.takeUsage?.();
|
|
339
|
+
const usage = await Promise.resolve(provider.takeUsage?.());
|
|
329
340
|
gen.update({
|
|
330
341
|
output: assistantText,
|
|
331
342
|
...(usage
|
|
@@ -364,6 +375,7 @@ export async function runTurn(
|
|
|
364
375
|
});
|
|
365
376
|
obs.score({ name: "generation_duration", value: Date.now() - startedAt });
|
|
366
377
|
} catch (err) {
|
|
378
|
+
await Promise.resolve(provider.takeUsage?.());
|
|
367
379
|
closeOpenTools("Turn stopped before this tool finished.");
|
|
368
380
|
await persistChain;
|
|
369
381
|
if (session) session = await finalize(deps, session, assistantText, thoughtText);
|