strands-darwin 0.0.33 → 0.0.35
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 +5 -1
- package/README.zh-CN.md +5 -1
- package/dist/patches/@strands-agents+sdk+1.16.0.patch +29 -15
- package/dist/patches/ink+7.1.1.patch +58 -0
- package/dist/src/agent/background-delegation.d.ts +3 -3
- package/dist/src/agent/background-delegation.js +2 -2
- package/dist/src/agent/background-delegation.js.map +1 -1
- package/dist/src/agent/cache-miss.d.ts +143 -0
- package/dist/src/agent/cache-miss.js +182 -0
- package/dist/src/agent/cache-miss.js.map +1 -0
- package/dist/src/agent/compact.d.ts +28 -0
- package/dist/src/agent/compact.js +34 -0
- package/dist/src/agent/compact.js.map +1 -1
- package/dist/src/agent/context-breakdown.d.ts +110 -0
- package/dist/src/agent/context-breakdown.js +117 -0
- package/dist/src/agent/context-breakdown.js.map +1 -0
- package/dist/src/agent/instructions.d.ts +23 -4
- package/dist/src/agent/instructions.js +67 -34
- package/dist/src/agent/instructions.js.map +1 -1
- package/dist/src/agent/permission-rules.d.ts +81 -2
- package/dist/src/agent/permission-rules.js +226 -6
- package/dist/src/agent/permission-rules.js.map +1 -1
- package/dist/src/agent/permission.d.ts +116 -2
- package/dist/src/agent/permission.js +201 -26
- package/dist/src/agent/permission.js.map +1 -1
- package/dist/src/agent/prompt-cache.d.ts +12 -3
- package/dist/src/agent/prompt-cache.js +12 -3
- package/dist/src/agent/prompt-cache.js.map +1 -1
- package/dist/src/agent/refusal.d.ts +44 -7
- package/dist/src/agent/refusal.js +54 -8
- package/dist/src/agent/refusal.js.map +1 -1
- package/dist/src/agent/runtime.d.ts +112 -7
- package/dist/src/agent/runtime.js +240 -19
- package/dist/src/agent/runtime.js.map +1 -1
- package/dist/src/agent/stream-resumption.d.ts +5 -2
- package/dist/src/agent/stream-resumption.js +5 -2
- package/dist/src/agent/stream-resumption.js.map +1 -1
- package/dist/src/agent/system-prompt.d.ts +1 -1
- package/dist/src/agent/system-prompt.js +4 -1
- package/dist/src/agent/system-prompt.js.map +1 -1
- package/dist/src/agent/thinking.js +4 -2
- package/dist/src/agent/thinking.js.map +1 -1
- package/dist/src/agents/child-recipe.d.ts +9 -1
- package/dist/src/agents/child-recipe.js +2 -0
- package/dist/src/agents/child-recipe.js.map +1 -1
- package/dist/src/agents/dispatch-registry.d.ts +18 -0
- package/dist/src/agents/dispatch-registry.js +2 -0
- package/dist/src/agents/dispatch-registry.js.map +1 -1
- package/dist/src/agents/retained-children.d.ts +101 -0
- package/dist/src/agents/retained-children.js +101 -0
- package/dist/src/agents/retained-children.js.map +1 -0
- package/dist/src/agents/subagent-tool.d.ts +50 -1
- package/dist/src/agents/subagent-tool.js +151 -12
- package/dist/src/agents/subagent-tool.js.map +1 -1
- package/dist/src/agents/workflow-tool.js +2 -2
- package/dist/src/agents/workflow-tool.js.map +1 -1
- package/dist/src/cli-doctor.js +10 -6
- package/dist/src/cli-doctor.js.map +1 -1
- package/dist/src/cli-main.js +9 -3
- package/dist/src/cli-main.js.map +1 -1
- package/dist/src/commands/custom-commands.d.ts +1 -1
- package/dist/src/commands/custom-commands.js +12 -5
- package/dist/src/commands/custom-commands.js.map +1 -1
- package/dist/src/commands/init-command.d.ts +47 -0
- package/dist/src/commands/init-command.js +110 -0
- package/dist/src/commands/init-command.js.map +1 -0
- package/dist/src/config.d.ts +78 -8
- package/dist/src/config.js +167 -20
- package/dist/src/config.js.map +1 -1
- package/dist/src/dev-repl.js +17 -2
- package/dist/src/dev-repl.js.map +1 -1
- package/dist/src/headless-protocol.d.ts +1 -1
- package/dist/src/headless-protocol.js +6 -5
- package/dist/src/headless-protocol.js.map +1 -1
- package/dist/src/headless-runner.js +12 -3
- package/dist/src/headless-runner.js.map +1 -1
- package/dist/src/headless.d.ts +11 -0
- package/dist/src/headless.js +33 -7
- package/dist/src/headless.js.map +1 -1
- package/dist/src/hooks/tool-hooks.js +10 -6
- package/dist/src/hooks/tool-hooks.js.map +1 -1
- package/dist/src/pricing/model-prices.d.ts +3 -1
- package/dist/src/pricing/model-prices.js +7 -1
- package/dist/src/pricing/model-prices.js.map +1 -1
- package/dist/src/skills/builtin/self-evolution-research/SKILL.md +14 -1
- package/dist/src/skills/prompt.d.ts +17 -0
- package/dist/src/skills/prompt.js +20 -0
- package/dist/src/skills/prompt.js.map +1 -1
- package/dist/src/tools/background-bash.d.ts +15 -3
- package/dist/src/tools/background-bash.js +18 -8
- package/dist/src/tools/background-bash.js.map +1 -1
- package/dist/src/tools/file-editor-serial.d.ts +14 -0
- package/dist/src/tools/file-editor-serial.js +15 -0
- package/dist/src/tools/file-editor-serial.js.map +1 -1
- package/dist/src/tools/shell-env.d.ts +77 -0
- package/dist/src/tools/shell-env.js +139 -0
- package/dist/src/tools/shell-env.js.map +1 -0
- package/dist/src/trajectory/record.d.ts +169 -2
- package/dist/src/trajectory/record.js +135 -0
- package/dist/src/trajectory/record.js.map +1 -1
- package/dist/src/trajectory/replay.d.ts +34 -1
- package/dist/src/trajectory/replay.js +157 -4
- package/dist/src/trajectory/replay.js.map +1 -1
- package/dist/src/trajectory/resume-recap.js +10 -3
- package/dist/src/trajectory/resume-recap.js.map +1 -1
- package/dist/src/trajectory/spend.d.ts +14 -1
- package/dist/src/trajectory/spend.js +40 -6
- package/dist/src/trajectory/spend.js.map +1 -1
- package/dist/src/trajectory/writer.d.ts +89 -4
- package/dist/src/trajectory/writer.js +191 -17
- package/dist/src/trajectory/writer.js.map +1 -1
- package/dist/src/tui/App.d.ts +12 -5
- package/dist/src/tui/App.js +328 -67
- package/dist/src/tui/App.js.map +1 -1
- package/dist/src/tui/InputBox.d.ts +1 -1
- package/dist/src/tui/InputBox.js +1 -1
- package/dist/src/tui/context-format.d.ts +16 -0
- package/dist/src/tui/context-format.js +41 -0
- package/dist/src/tui/context-format.js.map +1 -1
- package/dist/src/tui/instructions-format.d.ts +14 -0
- package/dist/src/tui/instructions-format.js +24 -0
- package/dist/src/tui/instructions-format.js.map +1 -0
- package/dist/src/tui/prompt-queue.js +4 -2
- package/dist/src/tui/prompt-queue.js.map +1 -1
- package/dist/src/tui/status-format.d.ts +50 -16
- package/dist/src/tui/status-format.js +92 -8
- package/dist/src/tui/status-format.js.map +1 -1
- package/dist/src/tui/subagent-format.js +13 -1
- package/dist/src/tui/subagent-format.js.map +1 -1
- package/dist/src/tui/tangent.d.ts +122 -0
- package/dist/src/tui/tangent.js +153 -0
- package/dist/src/tui/tangent.js.map +1 -0
- package/dist/src/tui/terminal-notify.d.ts +40 -0
- package/dist/src/tui/terminal-notify.js +136 -0
- package/dist/src/tui/terminal-notify.js.map +1 -0
- package/dist/src/tui/terminal-title.d.ts +94 -0
- package/dist/src/tui/terminal-title.js +121 -0
- package/dist/src/tui/terminal-title.js.map +1 -0
- package/dist/src/tui/turn-state.d.ts +12 -1
- package/dist/src/tui/turn-state.js +11 -0
- package/dist/src/tui/turn-state.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why the prompt cache missed (SER-074): a derivation over facts darwin already
|
|
3
|
+
* holds, never a new measurement.
|
|
4
|
+
*
|
|
5
|
+
* `/usage` and `/status` have always shown cache read/write counts and a hit ratio,
|
|
6
|
+
* but a number alone does not say why a call re-read the conversation uncached.
|
|
7
|
+
* Every input needed to name a likely cause is already in the runtime: the per-call
|
|
8
|
+
* provider counters `observeCallStats` folds (`afterModelCallEvent`
|
|
9
|
+
* `stopData.message.metadata.usage`), the cache-invalidating events the session
|
|
10
|
+
* itself performed (`/model`, `/effort`, `/compact`), the configured TTL
|
|
11
|
+
* (`promptCacheTtl`: `1h` default, `5m`), the wall clock, and whether the session
|
|
12
|
+
* was resumed. This module is the pure arithmetic over those facts; the runtime
|
|
13
|
+
* feeds it through {@link CacheMissTracker} with the same synchronous,
|
|
14
|
+
* non-throwing, latch-on-failure discipline the call stats observe under.
|
|
15
|
+
*
|
|
16
|
+
* Advisory only, on the context-pressure row's terms: the verdict is printed on
|
|
17
|
+
* existing reports (`/usage`, `/status`) and as one transcript notice before a
|
|
18
|
+
* `/model` or `/effort` switch on a warm cache. Nothing is compacted, no second
|
|
19
|
+
* threshold exists, no frame row, tick or channel is added, nothing is persisted or
|
|
20
|
+
* recorded in the trajectory.
|
|
21
|
+
*
|
|
22
|
+
* Invalidators, and one deliberate non-invalidator. A model switch, an effective
|
|
23
|
+
* effort change and a compaction each change the request prefix (a different model
|
|
24
|
+
* keys a different cache; `output_config.effort` is part of the request; compaction
|
|
25
|
+
* rewrites the messages), and idling past the TTL lets the entries expire. `/rewind`
|
|
26
|
+
* is not marked: it restores an earlier prefix of the same conversation, whose cache
|
|
27
|
+
* entries are still valid if they have not expired — the same rule Claude Code's
|
|
28
|
+
* prompt-caching documentation states. File edits, permission-mode changes and skill
|
|
29
|
+
* loads change no cached section either.
|
|
30
|
+
*
|
|
31
|
+
* Claude-only by the gate `prompt-cache.ts` explains: OpenAI caches automatically
|
|
32
|
+
* at the provider with no darwin-placed cache points, so its counters describe a
|
|
33
|
+
* cache darwin did not configure — the tracker stays silent unless the live plan
|
|
34
|
+
* has Darwin-managed cache points.
|
|
35
|
+
*/
|
|
36
|
+
import { DEFAULT_PROMPT_CACHE_TTL } from './prompt-cache.js';
|
|
37
|
+
/**
|
|
38
|
+
* A call reading less than this share of its own request total from the cache is a
|
|
39
|
+
* miss. A warm call on a long conversation reads nearly all of it (the last user
|
|
40
|
+
* message is the only uncached part); one fifth leaves room for a large tool result
|
|
41
|
+
* or user message without calling an ordinary turn a miss.
|
|
42
|
+
*/
|
|
43
|
+
export const CACHE_MISS_READ_FRACTION = 0.2;
|
|
44
|
+
/** The TTL in milliseconds; an unset TTL is darwin's {@link DEFAULT_PROMPT_CACHE_TTL}. */
|
|
45
|
+
export function promptCacheTtlMs(ttl) {
|
|
46
|
+
return (ttl ?? DEFAULT_PROMPT_CACHE_TTL) === '1h' ? 3_600_000 : 300_000;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* True when a call read less than {@link CACHE_MISS_READ_FRACTION} of its request
|
|
50
|
+
* from the cache. Unreported counters and an empty request are not misses: they are
|
|
51
|
+
* unknown, and unknown is never a verdict.
|
|
52
|
+
*/
|
|
53
|
+
export function isCacheMiss(call) {
|
|
54
|
+
if (call.cacheRead === undefined || call.requestInput === undefined)
|
|
55
|
+
return false;
|
|
56
|
+
if (!Number.isFinite(call.cacheRead) || !Number.isFinite(call.requestInput) || call.requestInput <= 0)
|
|
57
|
+
return false;
|
|
58
|
+
return call.cacheRead < call.requestInput * CACHE_MISS_READ_FRACTION;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The likely cause of the current call's cache miss, or `undefined` for no verdict.
|
|
62
|
+
*
|
|
63
|
+
* No verdict when the current call's counters are unreported, when it is not a miss,
|
|
64
|
+
* when it is a fresh session's first call (expected cold: nothing has been written
|
|
65
|
+
* yet), or when the previous call was itself cold — a miss is only a miss relative
|
|
66
|
+
* to a cache that was demonstrably warm. When several causes apply, the one that
|
|
67
|
+
* certainly invalidated the prefix wins over the one that merely could have:
|
|
68
|
+
* `model switched` > `effort changed` > `compacted` > `idle past cache TTL` >
|
|
69
|
+
* `first request of a resumed session` > `unknown`.
|
|
70
|
+
*/
|
|
71
|
+
export function cacheMissCause(input) {
|
|
72
|
+
const { previous, current, events, ttlMs, firstCallAfterResume } = input;
|
|
73
|
+
if (!isCacheMiss(current))
|
|
74
|
+
return undefined;
|
|
75
|
+
if (previous === undefined) {
|
|
76
|
+
if (!firstCallAfterResume)
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
else if (previous.cacheRead === undefined || !(previous.cacheRead > 0)) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (events.has('model'))
|
|
83
|
+
return 'model switched';
|
|
84
|
+
if (events.has('effort'))
|
|
85
|
+
return 'effort changed';
|
|
86
|
+
if (events.has('compact'))
|
|
87
|
+
return 'compacted';
|
|
88
|
+
if (previous !== undefined && current.at - previous.at > ttlMs)
|
|
89
|
+
return 'idle past cache TTL';
|
|
90
|
+
if (firstCallAfterResume)
|
|
91
|
+
return 'first request of a resumed session';
|
|
92
|
+
return 'unknown';
|
|
93
|
+
}
|
|
94
|
+
/** The cause as `/usage` and `/status` print it; the TTL cause names the configured TTL. */
|
|
95
|
+
export function describeCacheMissCause(cause, ttl) {
|
|
96
|
+
return cause === 'idle past cache TTL' ? `${cause} (${ttl ?? DEFAULT_PROMPT_CACHE_TTL})` : cause;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The one bounded notice `/model <target>` and `/effort <level>` print before a
|
|
100
|
+
* switch on a warm cache — `undefined` when the cache is cold or nothing is known,
|
|
101
|
+
* so a cold switch prints nothing new. States the cost and lets the switch proceed;
|
|
102
|
+
* deliberately not a confirmation dialog.
|
|
103
|
+
*/
|
|
104
|
+
export function warmCacheSwitchNotice(kind, warmth) {
|
|
105
|
+
if (warmth === undefined || !warmth.warm)
|
|
106
|
+
return undefined;
|
|
107
|
+
return (`cache is warm (${formatWarmthAge(warmth.ageMs)} ago, ${warmth.lastCacheRead.toLocaleString('en-US')} tokens read last call): ` +
|
|
108
|
+
`switching ${kind} re-reads the conversation uncached`);
|
|
109
|
+
}
|
|
110
|
+
/** `12s`, `3m 12s`, `1h 2m` — the `/tasks` duration vocabulary, without a TUI import. */
|
|
111
|
+
function formatWarmthAge(milliseconds) {
|
|
112
|
+
const seconds = Math.floor(Math.max(0, milliseconds) / 1000);
|
|
113
|
+
if (seconds < 60)
|
|
114
|
+
return `${seconds}s`;
|
|
115
|
+
const minutes = Math.floor(seconds / 60);
|
|
116
|
+
if (minutes < 60)
|
|
117
|
+
return `${minutes}m ${seconds % 60}s`;
|
|
118
|
+
const hours = Math.floor(minutes / 60);
|
|
119
|
+
return `${hours}h ${minutes % 60}m`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The per-runtime state behind {@link CacheMissReport} and {@link CacheWarmth}.
|
|
123
|
+
*
|
|
124
|
+
* Pure over what it is handed: the runtime passes each completed call's facts and
|
|
125
|
+
* the clock, marks the events it performed, and reads the two projections. Nothing
|
|
126
|
+
* here throws on ordinary data; the runtime wraps the calls in the observer latch
|
|
127
|
+
* anyway, so a surprise degrades to absence rather than to a dead turn. `/clear`'s
|
|
128
|
+
* successor runtime builds a new tracker, which is how the state stays session-scoped.
|
|
129
|
+
*/
|
|
130
|
+
export class CacheMissTracker {
|
|
131
|
+
ttlMs;
|
|
132
|
+
previous = undefined;
|
|
133
|
+
events = new Set();
|
|
134
|
+
lastMiss = undefined;
|
|
135
|
+
misses = 0;
|
|
136
|
+
firstCallAfterResume;
|
|
137
|
+
/**
|
|
138
|
+
* @param ttlMs the live TTL, read per call because `promptCacheTtl` is a model
|
|
139
|
+
* field and `/model` can change it mid-session.
|
|
140
|
+
*/
|
|
141
|
+
constructor(ttlMs, resumed) {
|
|
142
|
+
this.ttlMs = ttlMs;
|
|
143
|
+
this.firstCallAfterResume = resumed;
|
|
144
|
+
}
|
|
145
|
+
/** Records that the session performed one invalidating event since the last call. */
|
|
146
|
+
mark(event) {
|
|
147
|
+
this.events.add(event);
|
|
148
|
+
}
|
|
149
|
+
/** Folds one completed call in; returns the verdict for that call, if any. */
|
|
150
|
+
observe(call) {
|
|
151
|
+
const cause = cacheMissCause({
|
|
152
|
+
previous: this.previous,
|
|
153
|
+
current: call,
|
|
154
|
+
events: this.events,
|
|
155
|
+
ttlMs: this.ttlMs(),
|
|
156
|
+
firstCallAfterResume: this.firstCallAfterResume,
|
|
157
|
+
});
|
|
158
|
+
if (cause !== undefined && call.cacheRead !== undefined && call.requestInput !== undefined) {
|
|
159
|
+
this.misses += 1;
|
|
160
|
+
this.lastMiss = { cause, at: call.at, cacheRead: call.cacheRead, requestInput: call.requestInput };
|
|
161
|
+
}
|
|
162
|
+
// Every completed call closes the "since last call" window and settles the
|
|
163
|
+
// resume question, verdict or not: the events have had their effect on this
|
|
164
|
+
// request whether or not the counters let us see it.
|
|
165
|
+
this.previous = call;
|
|
166
|
+
this.events.clear();
|
|
167
|
+
this.firstCallAfterResume = false;
|
|
168
|
+
return cause;
|
|
169
|
+
}
|
|
170
|
+
report() {
|
|
171
|
+
return { lastMiss: this.lastMiss, misses: this.misses };
|
|
172
|
+
}
|
|
173
|
+
/** `undefined` until a call has completed. */
|
|
174
|
+
warmth(now) {
|
|
175
|
+
if (this.previous === undefined)
|
|
176
|
+
return undefined;
|
|
177
|
+
const lastCacheRead = this.previous.cacheRead ?? 0;
|
|
178
|
+
const ageMs = Math.max(0, now - this.previous.at);
|
|
179
|
+
return { warm: lastCacheRead > 0 && ageMs < this.ttlMs(), ageMs, lastCacheRead };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=cache-miss.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-miss.js","sourceRoot":"","sources":["../../../src/agent/cache-miss.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,EAAE,wBAAwB,EAAuB,MAAM,mBAAmB,CAAC;AAclF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAuB5C,0FAA0F;AAC1F,MAAM,UAAU,gBAAgB,CAAC,GAA+B;IAC9D,OAAO,CAAC,GAAG,IAAI,wBAAwB,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;AAC1E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,IAAoB;IAC9C,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,IAAI,IAAI,CAAC,YAAY,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IAClF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,Y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C,SAAS,IAAI,CAAC,CAAC;QACnD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QAClD,OAAO,EAAE,IAAI,EAAE,aAAa,GAAG,CAAC,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC;IACnF,CAAC;CACF"}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { Agent, Message, Model, SummarizingConversationManagerConfig } from '@strands-agents/sdk';
|
|
9
9
|
import { SummarizingConversationManager } from '@strands-agents/sdk';
|
|
10
|
+
import type { ContextCompactedEntry } from '../trajectory/record.js';
|
|
10
11
|
/**
|
|
11
12
|
* `/compact` collapses every reducible old message in one command, leaving one
|
|
12
13
|
* summary plus the recent window. 0.8 is the SDK's maximum ratio and minimizes
|
|
@@ -85,6 +86,33 @@ export interface CompactConversationOptions {
|
|
|
85
86
|
* Both rules apply to focused and unfocused managers alike.
|
|
86
87
|
*/
|
|
87
88
|
export declare function compactConversation({ agent, model, manager, preserveRecentMessages, persist, }: CompactConversationOptions): Promise<CompactResult>;
|
|
89
|
+
/**
|
|
90
|
+
* What a driver needs from the runtime to run one `/compact` and record it: the
|
|
91
|
+
* structural slice of `AgentRuntime` the TUI and headless drivers already hold.
|
|
92
|
+
* Typed structurally so `spike/verify-compact.ts` can prove the recording rules with
|
|
93
|
+
* a scripted host and a real recorder, without an `Agent`.
|
|
94
|
+
*/
|
|
95
|
+
export interface CompactionHost {
|
|
96
|
+
contextEstimate(): Promise<{
|
|
97
|
+
estimatedTokens: number;
|
|
98
|
+
}>;
|
|
99
|
+
compact(focus?: string): Promise<CompactResult>;
|
|
100
|
+
recordContextCompacted(entry: ContextCompactedEntry): void;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Runs one `/compact` through the host and appends the SRF-027 `contextCompacted`
|
|
104
|
+
* trajectory record when — and only when — the result says `compacted: true`.
|
|
105
|
+
*
|
|
106
|
+
* The one place that composes the record, shared by both drivers so the TUI and
|
|
107
|
+
* `--compact-before` cannot describe the same compaction two ways. The pre-compaction
|
|
108
|
+
* estimate is the host's own `contextEstimate()` read **before** the compaction (the
|
|
109
|
+
* anchor it rests on drops with the rewritten history), taken best-effort: a failed or
|
|
110
|
+
* non-positive read is absence, never 0, and never a reason not to compact. A
|
|
111
|
+
* no-shrink pass records nothing; a failure records nothing and rethrows untouched.
|
|
112
|
+
* `focused` is whether a non-empty focus was given — the focus text stays with the
|
|
113
|
+
* summarizer request and is not handed to the recorder.
|
|
114
|
+
*/
|
|
115
|
+
export declare function compactAndRecord(host: CompactionHost, focus?: string): Promise<CompactResult>;
|
|
88
116
|
/**
|
|
89
117
|
* Repairs user-role messages that carry `reasoningBlock` content in place.
|
|
90
118
|
*
|
|
@@ -151,6 +151,40 @@ export async function compactConversation({ agent, model, manager, preserveRecen
|
|
|
151
151
|
throw error;
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Runs one `/compact` through the host and appends the SRF-027 `contextCompacted`
|
|
156
|
+
* trajectory record when — and only when — the result says `compacted: true`.
|
|
157
|
+
*
|
|
158
|
+
* The one place that composes the record, shared by both drivers so the TUI and
|
|
159
|
+
* `--compact-before` cannot describe the same compaction two ways. The pre-compaction
|
|
160
|
+
* estimate is the host's own `contextEstimate()` read **before** the compaction (the
|
|
161
|
+
* anchor it rests on drops with the rewritten history), taken best-effort: a failed or
|
|
162
|
+
* non-positive read is absence, never 0, and never a reason not to compact. A
|
|
163
|
+
* no-shrink pass records nothing; a failure records nothing and rethrows untouched.
|
|
164
|
+
* `focused` is whether a non-empty focus was given — the focus text stays with the
|
|
165
|
+
* summarizer request and is not handed to the recorder.
|
|
166
|
+
*/
|
|
167
|
+
export async function compactAndRecord(host, focus) {
|
|
168
|
+
let estimatedTokensBefore;
|
|
169
|
+
try {
|
|
170
|
+
const estimate = await host.contextEstimate();
|
|
171
|
+
const tokens = estimate.estimatedTokens;
|
|
172
|
+
estimatedTokensBefore = Number.isFinite(tokens) && tokens > 0 ? Math.round(tokens) : undefined;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
estimatedTokensBefore = undefined;
|
|
176
|
+
}
|
|
177
|
+
const result = await host.compact(focus);
|
|
178
|
+
if (result.compacted) {
|
|
179
|
+
host.recordContextCompacted({
|
|
180
|
+
messagesBefore: result.messagesBefore,
|
|
181
|
+
messagesAfter: result.messagesAfter,
|
|
182
|
+
...(estimatedTokensBefore === undefined ? {} : { estimatedTokensBefore }),
|
|
183
|
+
focused: normalizeCompactFocus(focus) !== undefined,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
154
188
|
/**
|
|
155
189
|
* Repairs user-role messages that carry `reasoningBlock` content in place.
|
|
156
190
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compact.js","sourceRoot":"","sources":["../../../src/agent/compact.ts"],"names":[],"mappings":"AAQA,kFAAkF;AAClF,6EAA6E;AAC7E,6EAA6E;AAC7E,+EAA+E;AAC/E,qBAAqB;AACrB,OAAO,EAAE,4BAA4B,EAAE,8BAA8B,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"compact.js","sourceRoot":"","sources":["../../../src/agent/compact.ts"],"names":[],"mappings":"AAQA,kFAAkF;AAClF,6EAA6E;AAC7E,6EAA6E;AAC7E,+EAA+E;AAC/E,qBAAqB;AACrB,OAAO,EAAE,4BAA4B,EAAE,8BAA8B,EAAE,MAAM,qBAAqB,CAAC;AAInG;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AACzC,sFAAsF;AACtF,MAAM,CAAC,MAAM,6BAA6B,GAAG,GAAG,CAAC;AACjD;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,+DA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E,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC;QAC3D,QAAQ,EAAE,CAAC;IACb,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,uBAAuB,CAAC,KAAY,EAAE,KAAY;IAChE,OAAO,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,QAAQ,EAAE;QACvC,GAAG,CAAC,KAAK,CAAC,YAAY,KAAK,SAAS,IAAI,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC;QAC7E,SAAS,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC;KACpD,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/context` breakdown: what the next request is made of, component by component
|
|
3
|
+
* (SER-077).
|
|
4
|
+
*
|
|
5
|
+
* The total `/context` prints is one number — measured base plus a counted tail, or
|
|
6
|
+
* one whole-request count — and says nothing about *what* is large. This module
|
|
7
|
+
* counts the parts separately: the system prompt by section as Darwin composes it
|
|
8
|
+
* (base prompt, `<project-instructions>`, the official `<available_skills>`
|
|
9
|
+
* catalogue, `<working-context>`), the tool specs grouped by origin (darwin's own
|
|
10
|
+
* built-ins, then each MCP server by its configured name), and the conversation by
|
|
11
|
+
* role. Every component goes through the same `model.countTokens` the total already
|
|
12
|
+
* uses, so the parts are stated in the total's own units — but they are an
|
|
13
|
+
* *estimate over the current request shape*, never the anchor measurement, and the
|
|
14
|
+
* formatter labels them so. The measured total stays the authoritative line.
|
|
15
|
+
*
|
|
16
|
+
* Two rules from the rest of the runtime hold here. It is computed on demand only:
|
|
17
|
+
* `/context` asks for it, nothing else does — not the post-turn pressure advisory,
|
|
18
|
+
* not `/status` — because a provider-native `countTokens` may reach the provider's
|
|
19
|
+
* counting API and this makes several calls. And a component whose count fails is
|
|
20
|
+
* absent (`tokens: undefined`, rendered `not reported`), never 0: the `usageBuckets`
|
|
21
|
+
* honesty rule. Everything here is a pure function over injected inputs; the
|
|
22
|
+
* runtime supplies the live prompt, tools, servers, messages and the counter.
|
|
23
|
+
*/
|
|
24
|
+
import type { CountTokensOptions, Message, SystemPrompt, ToolSpec } from '@strands-agents/sdk';
|
|
25
|
+
import type { McpServerStatus } from '../mcp/registry.js';
|
|
26
|
+
/** One counted component of the request. */
|
|
27
|
+
export interface ContextComponent {
|
|
28
|
+
/** Bounded label, rendered as written. */
|
|
29
|
+
label: string;
|
|
30
|
+
/** Estimated tokens; absent when the count failed — `not reported`, never 0. */
|
|
31
|
+
tokens: number | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* A stated absence that is not a failed count — e.g. the skills catalogue before
|
|
34
|
+
* the plugin's first injection. Rendered instead of a count when present.
|
|
35
|
+
*/
|
|
36
|
+
absent?: string;
|
|
37
|
+
}
|
|
38
|
+
/** The request, component by component. Row order is the rendering order. */
|
|
39
|
+
export interface ContextBreakdown {
|
|
40
|
+
/** System prompt sections in composition order. */
|
|
41
|
+
systemPrompt: ContextComponent[];
|
|
42
|
+
/** Darwin's own tools plus plugin tools — everything no MCP server registered. */
|
|
43
|
+
builtinTools: ContextComponent;
|
|
44
|
+
/** One row per configured MCP server, in configuration order; the formatter bounds them. */
|
|
45
|
+
mcpServers: ContextComponent[];
|
|
46
|
+
/** Conversation messages by role, whole messages only. */
|
|
47
|
+
conversation: ContextComponent[];
|
|
48
|
+
}
|
|
49
|
+
/** `model.countTokens`, injected so the runtime and an offline suite share one path. */
|
|
50
|
+
export type ComponentCounter = (messages: Message[], options: CountTokensOptions) => Promise<number>;
|
|
51
|
+
export interface BreakdownInputs {
|
|
52
|
+
/** The live `agent.systemPrompt`. */
|
|
53
|
+
systemPrompt: SystemPrompt | undefined;
|
|
54
|
+
/** The section strings the composition seam produced (`composeSystemPrompt`'s inputs). */
|
|
55
|
+
composed: {
|
|
56
|
+
base: string;
|
|
57
|
+
instructions: string | undefined;
|
|
58
|
+
};
|
|
59
|
+
/** The live `agent.tools`. */
|
|
60
|
+
tools: readonly {
|
|
61
|
+
name: string;
|
|
62
|
+
toolSpec: ToolSpec;
|
|
63
|
+
}[];
|
|
64
|
+
/** `mcpServerStatuses(...)` — names and registered tool names, never `listTools()`. */
|
|
65
|
+
servers: readonly McpServerStatus[];
|
|
66
|
+
/** The live `agent.messages`. */
|
|
67
|
+
messages: readonly Message[];
|
|
68
|
+
}
|
|
69
|
+
export declare const BASE_PROMPT_LABEL = "system prompt \u00B7 base";
|
|
70
|
+
/** The `<project-instructions>` block — AGENTS.md or its CLAUDE.md fallback; the fragment's own `source` names which. */
|
|
71
|
+
export declare const PROJECT_INSTRUCTIONS_LABEL = "system prompt \u00B7 project instructions";
|
|
72
|
+
/** A restored prompt whose base block no longer equals the current composition. */
|
|
73
|
+
export declare const RESTORED_PROMPT_LABEL = "system prompt \u00B7 base + project instructions (restored)";
|
|
74
|
+
/** A prompt array in a shape Darwin does not own: counted whole rather than guessed at. */
|
|
75
|
+
export declare const WHOLE_PROMPT_LABEL = "system prompt";
|
|
76
|
+
export declare const SKILLS_CATALOGUE_LABEL = "system prompt \u00B7 skills catalogue";
|
|
77
|
+
export declare const WORKING_CONTEXT_LABEL = "system prompt \u00B7 working context";
|
|
78
|
+
export declare const CATALOGUE_NOT_INJECTED = "not yet injected (added before the first model call)";
|
|
79
|
+
export declare const BUILTIN_TOOLS_LABEL = "tools \u00B7 darwin built-ins";
|
|
80
|
+
export declare const UNATTRIBUTED_TOOLS_LABEL = "tools \u00B7 darwin built-ins and unattributed";
|
|
81
|
+
export declare const MCP_TOOLS_LABEL_PREFIX = "tools \u00B7 mcp ";
|
|
82
|
+
export declare const USER_MESSAGES_LABEL = "conversation \u00B7 user prompts and tool results";
|
|
83
|
+
export declare const ASSISTANT_MESSAGES_LABEL = "conversation \u00B7 assistant replies and tool calls";
|
|
84
|
+
/**
|
|
85
|
+
* Counts every component with `countTokens`, one call per component, sequentially
|
|
86
|
+
* so a provider-backed counter is never fanned out. A failed count is recorded as
|
|
87
|
+
* absent and the rest are still counted: one gap costs one row, never the report.
|
|
88
|
+
*/
|
|
89
|
+
export declare function measureContextBreakdown(inputs: BreakdownInputs, countTokens: ComponentCounter): Promise<ContextBreakdown>;
|
|
90
|
+
export interface ToolOriginGroups {
|
|
91
|
+
/** Names the built-in row honestly when a server's tool names could not be read. */
|
|
92
|
+
builtinLabel: string;
|
|
93
|
+
builtins: ToolSpec[];
|
|
94
|
+
/** `specs` is absent when the server's registered names are unavailable. */
|
|
95
|
+
servers: {
|
|
96
|
+
label: string;
|
|
97
|
+
specs: ToolSpec[] | undefined;
|
|
98
|
+
}[];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Attributes each live tool spec to the MCP server that registered it, by the
|
|
102
|
+
* names `mcpServerStatuses` already reads; whatever no server claims is darwin's
|
|
103
|
+
* own (built-ins, plugin tools, parent-only tools). A server whose names cannot be
|
|
104
|
+
* read gets a row with no specs — its tools then land in the built-in group, which
|
|
105
|
+
* says so in its label rather than passing them off as darwin's.
|
|
106
|
+
*/
|
|
107
|
+
export declare function groupToolsByOrigin(tools: readonly {
|
|
108
|
+
name: string;
|
|
109
|
+
toolSpec: ToolSpec;
|
|
110
|
+
}[], servers: readonly McpServerStatus[]): ToolOriginGroups;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { composeSystemPrompt } from './instructions.js';
|
|
2
|
+
import { knownPromptSections } from '../skills/prompt.js';
|
|
3
|
+
export const BASE_PROMPT_LABEL = 'system prompt · base';
|
|
4
|
+
/** The `<project-instructions>` block — AGENTS.md or its CLAUDE.md fallback; the fragment's own `source` names which. */
|
|
5
|
+
export const PROJECT_INSTRUCTIONS_LABEL = 'system prompt · project instructions';
|
|
6
|
+
/** A restored prompt whose base block no longer equals the current composition. */
|
|
7
|
+
export const RESTORED_PROMPT_LABEL = 'system prompt · base + project instructions (restored)';
|
|
8
|
+
/** A prompt array in a shape Darwin does not own: counted whole rather than guessed at. */
|
|
9
|
+
export const WHOLE_PROMPT_LABEL = 'system prompt';
|
|
10
|
+
export const SKILLS_CATALOGUE_LABEL = 'system prompt · skills catalogue';
|
|
11
|
+
export const WORKING_CONTEXT_LABEL = 'system prompt · working context';
|
|
12
|
+
export const CATALOGUE_NOT_INJECTED = 'not yet injected (added before the first model call)';
|
|
13
|
+
export const BUILTIN_TOOLS_LABEL = 'tools · darwin built-ins';
|
|
14
|
+
export const UNATTRIBUTED_TOOLS_LABEL = 'tools · darwin built-ins and unattributed';
|
|
15
|
+
export const MCP_TOOLS_LABEL_PREFIX = 'tools · mcp ';
|
|
16
|
+
export const USER_MESSAGES_LABEL = 'conversation · user prompts and tool results';
|
|
17
|
+
export const ASSISTANT_MESSAGES_LABEL = 'conversation · assistant replies and tool calls';
|
|
18
|
+
/**
|
|
19
|
+
* Counts every component with `countTokens`, one call per component, sequentially
|
|
20
|
+
* so a provider-backed counter is never fanned out. A failed count is recorded as
|
|
21
|
+
* absent and the rest are still counted: one gap costs one row, never the report.
|
|
22
|
+
*/
|
|
23
|
+
export async function measureContextBreakdown(inputs, countTokens) {
|
|
24
|
+
const count = async (label, messages, options) => {
|
|
25
|
+
try {
|
|
26
|
+
return { label, tokens: await countTokens(messages, options) };
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return { label, tokens: undefined };
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const systemPrompt = [];
|
|
33
|
+
const sections = knownPromptSections(inputs.systemPrompt);
|
|
34
|
+
if (sections === undefined) {
|
|
35
|
+
if (inputs.systemPrompt !== undefined) {
|
|
36
|
+
systemPrompt.push(await count(WHOLE_PROMPT_LABEL, [], { systemPrompt: inputs.systemPrompt }));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const composed = composeSystemPrompt(inputs.composed.base, inputs.composed.instructions === undefined
|
|
41
|
+
? undefined
|
|
42
|
+
: { fragment: inputs.composed.instructions });
|
|
43
|
+
if (sections.base === composed.trimEnd()) {
|
|
44
|
+
// The live block is exactly what this run composed, so the seam strings are
|
|
45
|
+
// its sections and can be counted apart.
|
|
46
|
+
systemPrompt.push(await count(BASE_PROMPT_LABEL, [], { systemPrompt: inputs.composed.base }));
|
|
47
|
+
if (inputs.composed.instructions !== undefined) {
|
|
48
|
+
systemPrompt.push(await count(PROJECT_INSTRUCTIONS_LABEL, [], { systemPrompt: inputs.composed.instructions }));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// A resumed session carries the snapshot's prompt, which may predate an
|
|
53
|
+
// AGENTS.md edit: the split is unknowable, so the block is counted whole.
|
|
54
|
+
systemPrompt.push(await count(RESTORED_PROMPT_LABEL, [], { systemPrompt: sections.base }));
|
|
55
|
+
}
|
|
56
|
+
systemPrompt.push(sections.catalogue === undefined
|
|
57
|
+
? { label: SKILLS_CATALOGUE_LABEL, tokens: undefined, absent: CATALOGUE_NOT_INJECTED }
|
|
58
|
+
: await count(SKILLS_CATALOGUE_LABEL, [], { systemPrompt: sections.catalogue }));
|
|
59
|
+
if (sections.workingContext !== undefined) {
|
|
60
|
+
systemPrompt.push(await count(WORKING_CONTEXT_LABEL, [], { systemPrompt: sections.workingContext }));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const groups = groupToolsByOrigin(inputs.tools, inputs.servers);
|
|
64
|
+
const builtinTools = await count(groups.builtinLabel, [], { toolSpecs: groups.builtins });
|
|
65
|
+
const mcpServers = [];
|
|
66
|
+
for (const server of groups.servers) {
|
|
67
|
+
mcpServers.push(server.specs === undefined
|
|
68
|
+
? { label: server.label, tokens: undefined }
|
|
69
|
+
: await count(server.label, [], { toolSpecs: server.specs }));
|
|
70
|
+
}
|
|
71
|
+
const byRole = (role) => inputs.messages.filter((message) => message.role === role);
|
|
72
|
+
const conversation = [
|
|
73
|
+
await count(USER_MESSAGES_LABEL, byRole('user'), {}),
|
|
74
|
+
await count(ASSISTANT_MESSAGES_LABEL, byRole('assistant'), {}),
|
|
75
|
+
];
|
|
76
|
+
return { systemPrompt, builtinTools, mcpServers, conversation };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Attributes each live tool spec to the MCP server that registered it, by the
|
|
80
|
+
* names `mcpServerStatuses` already reads; whatever no server claims is darwin's
|
|
81
|
+
* own (built-ins, plugin tools, parent-only tools). A server whose names cannot be
|
|
82
|
+
* read gets a row with no specs — its tools then land in the built-in group, which
|
|
83
|
+
* says so in its label rather than passing them off as darwin's.
|
|
84
|
+
*/
|
|
85
|
+
export function groupToolsByOrigin(tools, servers) {
|
|
86
|
+
const owner = new Map();
|
|
87
|
+
let unattributed = false;
|
|
88
|
+
for (const server of servers) {
|
|
89
|
+
if (server.toolNames === undefined) {
|
|
90
|
+
unattributed = true;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
for (const name of server.toolNames)
|
|
94
|
+
owner.set(name, server.name);
|
|
95
|
+
}
|
|
96
|
+
const perServer = new Map();
|
|
97
|
+
const builtins = [];
|
|
98
|
+
for (const tool of tools) {
|
|
99
|
+
const server = owner.get(tool.name);
|
|
100
|
+
if (server === undefined) {
|
|
101
|
+
builtins.push(tool.toolSpec);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const specs = perServer.get(server) ?? [];
|
|
105
|
+
specs.push(tool.toolSpec);
|
|
106
|
+
perServer.set(server, specs);
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
builtinLabel: unattributed ? UNATTRIBUTED_TOOLS_LABEL : BUILTIN_TOOLS_LABEL,
|
|
110
|
+
builtins,
|
|
111
|
+
servers: servers.map((server) => ({
|
|
112
|
+
label: `${MCP_TOOLS_LABEL_PREFIX}${server.name}`,
|
|
113
|
+
specs: server.toolNames === undefined ? undefined : perServer.get(server.name) ?? [],
|
|
114
|
+
})),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=context-breakdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-breakdown.js","sourceRoot":"","sources":["../../../src/agent/context-breakdown.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AA2C1D,MAAM,CAAC,MAAM,iBAAiB,GAAG,sBAAsB,CAAC;AACxD,yHAAyH;AACzH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sCAAsC,CAAC;AACjF,mFAAmF;AACnF,MAAM,CAAC,MAAM,qBAAqB,GAAG,wDAAwD,CAAC;AAC9F,2FAA2F;AAC3F,MAAM,CAAC,MAAM,kBAAkB,GAAG,eAAe,CAAC;AAClD,MAAM,CAAC,MAAM,sBAAsB,GAAG,kCAAkC,CAAC;AACzE,MAAM,CAAC,MAAM,qBAAqB,GAAG,iCAAiC,CAAC;AACvE,MAAM,CAAC,MAAM,sBAAsB,GAAG,sDAAsD,CAAC;AAC7F,MAAM,CAAC,MAAM,mBAAmB,GAAG,0BAA0B,CAAC;AAC9D,MAAM,CAAC,MAAM,wBAAwB,GAAG,2CAA2C,CAAC;AACpF,MAAM,CAAC,MAAM,sBAAsB,GAAG,cAAc,CAAC;AACrD,MAAM,CAAC,MAAM,mBAAmB,GAAG,8CAA8C,CAAC;AAClF,MAAM,CAAC,MAAM,wBAAwB,GAAG,iDAAiD,CAAC;AAE1F;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,MAAuB,EACvB,WAA6B;IAE7B,MAAM,KAAK,GAAG,KAAK,EAAE,KAAa,EAAE,QAAmB,EAAE,OAA2B,EAA6B,EAAE;QACjH,IAAI,CAAC;YACH,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC,QAAQ,EAAE,OA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AAQ,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC7B,SAAS;QACX,CAAC;QACD,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QAC1C,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC1B,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO;QACL,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,mBAAmB;QAC3E,QAAQ;QACR,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;YAChC,KAAK,EAAE,GAAG,sBAAsB,GAAG,MAAM,CAAC,IAAI,EAAE;YAChD,KAAK,EAAE,MAAM,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE;SACrF,CAAC,CAAC;KACJ,CAAC;AACJ,CAAC"}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
export declare const AGENTS_FILENAME = "AGENTS.md";
|
|
2
|
+
/** Read only when {@link AGENTS_FILENAME} does not exist. */
|
|
3
|
+
export declare const CLAUDE_FILENAME = "CLAUDE.md";
|
|
4
|
+
export type InstructionsFilename = typeof AGENTS_FILENAME | typeof CLAUDE_FILENAME;
|
|
5
|
+
/**
|
|
6
|
+
* One fixed line for the CLAUDE.md case only. Claude Code expands `@path` lines
|
|
7
|
+
* into the referenced files; darwin does not, so without this the model would
|
|
8
|
+
* take an import line for content it has already been given.
|
|
9
|
+
*/
|
|
10
|
+
export declare const CLAUDE_IMPORT_NOTICE: string;
|
|
2
11
|
/**
|
|
3
12
|
* Instructions past this size are truncated rather than sent whole.
|
|
4
13
|
*
|
|
@@ -8,6 +17,8 @@ export declare const AGENTS_FILENAME = "AGENTS.md";
|
|
|
8
17
|
*/
|
|
9
18
|
export declare const MAX_INSTRUCTIONS_BYTES: number;
|
|
10
19
|
export interface ProjectInstructions {
|
|
20
|
+
/** Which of the two names was actually loaded. */
|
|
21
|
+
filename: InstructionsFilename;
|
|
11
22
|
/** Absolute path of the file that was read. */
|
|
12
23
|
path: string;
|
|
13
24
|
/** Size on disk, in bytes, before any truncation. */
|
|
@@ -23,13 +34,16 @@ export interface ProjectInstructionsLoad {
|
|
|
23
34
|
/** What to inject, or undefined when there is nothing usable to inject. */
|
|
24
35
|
instructions: ProjectInstructions | undefined;
|
|
25
36
|
/**
|
|
26
|
-
* Why
|
|
27
|
-
*
|
|
37
|
+
* Why a file that is present could not be used. Undefined both when a file
|
|
38
|
+
* loaded and when there is no file at all — those two need no reporting.
|
|
28
39
|
*/
|
|
29
40
|
problem: string | undefined;
|
|
41
|
+
/** The file `problem` is about; undefined exactly when `problem` is. */
|
|
42
|
+
problemFile: InstructionsFilename | undefined;
|
|
30
43
|
}
|
|
31
44
|
/**
|
|
32
|
-
* Reads `<projectRoot>/AGENTS.md
|
|
45
|
+
* Reads `<projectRoot>/AGENTS.md`, or `<projectRoot>/CLAUDE.md` when the former
|
|
46
|
+
* does not exist.
|
|
33
47
|
*
|
|
34
48
|
* A broken file never blocks startup — the agent works without project
|
|
35
49
|
* instructions — but the two ways of ending up without them are not the same. No
|
|
@@ -38,6 +52,11 @@ export interface ProjectInstructionsLoad {
|
|
|
38
52
|
* user has rules they believe are in effect and nothing on screen contradicts
|
|
39
53
|
* that. Same isolation rule as a broken skill directory: skip it, say why.
|
|
40
54
|
*
|
|
55
|
+
* Only a truly missing `AGENTS.md` moves on to `CLAUDE.md`: an unreadable one is
|
|
56
|
+
* reported as such and never falls through, because the file the user wrote
|
|
57
|
+
* rules into is the one that must decide. Whichever file is found first is the
|
|
58
|
+
* only one opened.
|
|
59
|
+
*
|
|
41
60
|
* An empty or whitespace-only file is silently skipped: injecting the delimiters
|
|
42
61
|
* with nothing between them would only point the model at emptiness.
|
|
43
62
|
*/
|
|
@@ -51,4 +70,4 @@ export declare function loadProjectInstructions(projectRoot: string): Promise<Pr
|
|
|
51
70
|
* describe how to work in this repository; the skills list is a menu and reads
|
|
52
71
|
* better last, and the working context is not rules at all.
|
|
53
72
|
*/
|
|
54
|
-
export declare function composeSystemPrompt(basePrompt: string, instructions: ProjectInstructions | undefined): string;
|
|
73
|
+
export declare function composeSystemPrompt(basePrompt: string, instructions: Pick<ProjectInstructions, 'fragment'> | undefined): string;
|