@lloyal-labs/lloyal-agents 1.5.8 → 2.0.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 +81 -97
- package/dist/Agent.d.ts +60 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +87 -1
- package/dist/Agent.js.map +1 -1
- package/dist/AgentPolicy.d.ts +52 -16
- package/dist/AgentPolicy.d.ts.map +1 -1
- package/dist/AgentPolicy.js +114 -43
- package/dist/AgentPolicy.js.map +1 -1
- package/dist/agent-pool.d.ts +17 -5
- package/dist/agent-pool.d.ts.map +1 -1
- package/dist/agent-pool.js +707 -417
- package/dist/agent-pool.js.map +1 -1
- package/dist/combinators.d.ts +29 -0
- package/dist/combinators.d.ts.map +1 -0
- package/dist/combinators.js +37 -0
- package/dist/combinators.js.map +1 -0
- package/dist/context.d.ts +18 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +18 -1
- package/dist/context.js.map +1 -1
- package/dist/create-agent-pool.d.ts +96 -0
- package/dist/create-agent-pool.d.ts.map +1 -0
- package/dist/create-agent-pool.js +84 -0
- package/dist/create-agent-pool.js.map +1 -0
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -8
- package/dist/index.js.map +1 -1
- package/dist/orchestrators.d.ts +161 -0
- package/dist/orchestrators.d.ts.map +1 -0
- package/dist/orchestrators.js +173 -0
- package/dist/orchestrators.js.map +1 -0
- package/dist/replay.d.ts +96 -0
- package/dist/replay.d.ts.map +1 -0
- package/dist/replay.js +108 -0
- package/dist/replay.js.map +1 -0
- package/dist/shared-root.d.ts +56 -18
- package/dist/shared-root.d.ts.map +1 -1
- package/dist/shared-root.js +79 -52
- package/dist/shared-root.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/trace-types.d.ts +23 -2
- package/dist/trace-types.d.ts.map +1 -1
- package/dist/trace-writer.d.ts +4 -1
- package/dist/trace-writer.d.ts.map +1 -1
- package/dist/trace-writer.js +6 -2
- package/dist/trace-writer.js.map +1 -1
- package/dist/types.d.ts +42 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/use-agent.d.ts +92 -0
- package/dist/use-agent.d.ts.map +1 -0
- package/dist/use-agent.js +127 -0
- package/dist/use-agent.js.map +1 -0
- package/package.json +5 -5
- package/dist/generate.d.ts +0 -77
- package/dist/generate.d.ts.map +0 -1
- package/dist/generate.js +0 -166
- package/dist/generate.js.map +0 -1
- package/dist/run-agents.d.ts +0 -39
- package/dist/run-agents.d.ts.map +0 -1
- package/dist/run-agents.js +0 -46
- package/dist/run-agents.js.map +0 -1
- package/dist/spawn-agents.d.ts +0 -104
- package/dist/spawn-agents.d.ts.map +0 -1
- package/dist/spawn-agents.js +0 -255
- package/dist/spawn-agents.js.map +0 -1
package/dist/AgentPolicy.d.ts
CHANGED
|
@@ -16,13 +16,12 @@ export interface ToolGuard {
|
|
|
16
16
|
/** Error message sent back to the agent as a tool result */
|
|
17
17
|
message: string;
|
|
18
18
|
}
|
|
19
|
-
/** Default guards for deduplication and recursion discipline */
|
|
20
19
|
export declare const defaultToolGuards: ToolGuard[];
|
|
21
20
|
/**
|
|
22
21
|
* Why the agent entered idle status.
|
|
23
22
|
* @category Agents
|
|
24
23
|
*/
|
|
25
|
-
export type IdleReason = 'reported' | 'pressure_critical' | 'pressure_softcut' | 'pressure_settle_reject' | 'max_turns' | 'free_text_stop' | 'tool_error';
|
|
24
|
+
export type IdleReason = 'reported' | 'pressure_critical' | 'pressure_softcut' | 'pressure_settle_reject' | 'settle_stall_break' | 'max_turns' | 'free_text_stop' | 'tool_error';
|
|
26
25
|
/**
|
|
27
26
|
* Action returned by policy.onProduced — tells the pool what to do.
|
|
28
27
|
* @category Agents
|
|
@@ -91,12 +90,20 @@ export interface AgentPolicy {
|
|
|
91
90
|
toolCalls: ParsedToolCall[];
|
|
92
91
|
}, pressure: ContextPressure, config: PolicyConfig): ProduceAction;
|
|
93
92
|
/**
|
|
94
|
-
* SETTLE
|
|
93
|
+
* SETTLE stall-break: consulted when deferred tool results have no
|
|
94
|
+
* active siblings to free KV — the last-resort moment where a policy
|
|
95
|
+
* decides whether to nudge the agent (replacing the oversized result
|
|
96
|
+
* with a compact error payload) or drop it and let recovery extract.
|
|
95
97
|
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
98
|
+
* In normal operation, SETTLE defers oversized items across ticks.
|
|
99
|
+
* Siblings completing (parallel) or the spine growing (chain) restores
|
|
100
|
+
* headroom on subsequent ticks — this hook fires only when all agents
|
|
101
|
+
* are `awaiting_tool`/idle and deferral can't resolve on its own.
|
|
102
|
+
*
|
|
103
|
+
* Return `{type: 'nudge'}` to replace the oversized item with a compact
|
|
104
|
+
* error payload (carries the budget in its message). Return
|
|
105
|
+
* `{type: 'idle', reason: 'pressure_settle_reject'}` to drop the agent.
|
|
106
|
+
* If the hook is absent, the pool falls back to `settle_stall_break`.
|
|
100
107
|
*/
|
|
101
108
|
onSettleReject(agent: Agent, resultTokens: number, pressure: ContextPressure, config: PolicyConfig): SettleAction;
|
|
102
109
|
/**
|
|
@@ -146,7 +153,7 @@ export interface AgentPolicy {
|
|
|
146
153
|
*
|
|
147
154
|
* Optional — defaults to skip when absent.
|
|
148
155
|
*/
|
|
149
|
-
onRecovery?(agent: Agent): RecoveryAction;
|
|
156
|
+
onRecovery?(agent: Agent, pressure: ContextPressure): RecoveryAction;
|
|
150
157
|
}
|
|
151
158
|
/**
|
|
152
159
|
* Pool-level configuration passed to policy methods.
|
|
@@ -177,9 +184,23 @@ export interface DefaultAgentPolicyOpts {
|
|
|
177
184
|
guards?: ToolGuard[];
|
|
178
185
|
/** Append additional guards to the defaults. */
|
|
179
186
|
extraGuards?: ToolGuard[];
|
|
180
|
-
/**
|
|
181
|
-
*
|
|
182
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Explore/exploit thresholds — both axes checked independently.
|
|
189
|
+
* Either falling below its threshold flips the policy into exploit mode
|
|
190
|
+
* (rerank tool results against the original query via the entailment
|
|
191
|
+
* scorer). Explore mode preserves the agent's local-query ordering.
|
|
192
|
+
*/
|
|
193
|
+
shouldExplore?: {
|
|
194
|
+
/** Minimum fraction of KV capacity (0–1) that must remain free for
|
|
195
|
+
* explore mode. Checks `pressure.percentAvailable / 100`. Below this
|
|
196
|
+
* fraction, exploit mode kicks in. @default 0.4 */
|
|
197
|
+
context?: number;
|
|
198
|
+
/** Maximum fraction of the time soft limit (0–1) that can be consumed
|
|
199
|
+
* before exploit mode kicks in. When `elapsed / timeSoftLimit >= time`,
|
|
200
|
+
* `shouldExplore` returns false regardless of KV headroom. Only
|
|
201
|
+
* applies when `budget.time.softLimit` is set. @default 0.5 */
|
|
202
|
+
time?: number;
|
|
203
|
+
};
|
|
183
204
|
/** Scratchpad recovery for agents killed without reporting.
|
|
184
205
|
* Policy decides per-agent via {@link AgentPolicy.onRecovery}. */
|
|
185
206
|
recovery?: {
|
|
@@ -207,16 +228,31 @@ export interface DefaultAgentPolicyOpts {
|
|
|
207
228
|
hardLimit?: number;
|
|
208
229
|
};
|
|
209
230
|
};
|
|
231
|
+
/** Terminal tool name. When set, agents mid-generation of this tool are
|
|
232
|
+
* protected from shouldExit — the hard limit is deferred until the tool
|
|
233
|
+
* call completes naturally or KV pressure forces a kill. */
|
|
234
|
+
terminalTool?: string;
|
|
210
235
|
}
|
|
211
236
|
export declare class DefaultAgentPolicy implements AgentPolicy {
|
|
212
237
|
private _minToolCalls;
|
|
213
238
|
private _guards;
|
|
214
|
-
private
|
|
239
|
+
private _exploreContext;
|
|
240
|
+
private _exploreTime;
|
|
215
241
|
private _forceExploit;
|
|
216
242
|
private _recovery;
|
|
217
243
|
private _budget;
|
|
244
|
+
private _terminalTool;
|
|
218
245
|
private _startTime;
|
|
219
246
|
constructor(opts?: DefaultAgentPolicyOpts);
|
|
247
|
+
/**
|
|
248
|
+
* Elapsed wall time for *this agent* (since its first idle→active transition),
|
|
249
|
+
* falling back to the policy's own construction time when the agent hasn't
|
|
250
|
+
* started yet (defensive — shouldn't normally happen).
|
|
251
|
+
*
|
|
252
|
+
* Per-agent timing means orchestrators that spawn agents sequentially (e.g.
|
|
253
|
+
* `chain`) get the correct "how long has this task been running?" semantics
|
|
254
|
+
* without the time budget leaking across iterations.
|
|
255
|
+
*/
|
|
220
256
|
private _elapsed;
|
|
221
257
|
/** KV pressure thresholds for ContextPressure construction.
|
|
222
258
|
* Pool reads this once at setup. */
|
|
@@ -232,13 +268,14 @@ export declare class DefaultAgentPolicy implements AgentPolicy {
|
|
|
232
268
|
private _isOverBudget;
|
|
233
269
|
private _handleOverBudget;
|
|
234
270
|
private _checkGuards;
|
|
235
|
-
onSettleReject(agent: Agent, _resultTokens: number,
|
|
271
|
+
onSettleReject(agent: Agent, _resultTokens: number, pressure: ContextPressure, config: PolicyConfig): SettleAction;
|
|
236
272
|
/**
|
|
237
273
|
* UI-driven override. Harness calls this when the user wants agents
|
|
238
274
|
* to wrap up. Overrides pressure-based logic immediately.
|
|
239
275
|
*/
|
|
240
276
|
setExploitMode(force: boolean): void;
|
|
241
|
-
|
|
277
|
+
shouldExit(agent: Agent, pressure: ContextPressure): boolean;
|
|
278
|
+
shouldExplore(agent: Agent, pressure: ContextPressure): boolean;
|
|
242
279
|
/**
|
|
243
280
|
* Trailing stop: at most one agent nudged or killed per tick.
|
|
244
281
|
* The sacrificed agent's findings are extracted and its KV freed,
|
|
@@ -248,7 +285,6 @@ export declare class DefaultAgentPolicy implements AgentPolicy {
|
|
|
248
285
|
private _killedThisTick;
|
|
249
286
|
private _nudgedThisTick;
|
|
250
287
|
resetTick(): void;
|
|
251
|
-
|
|
252
|
-
onRecovery(agent: Agent): RecoveryAction;
|
|
288
|
+
onRecovery(agent: Agent, pressure: ContextPressure): RecoveryAction;
|
|
253
289
|
}
|
|
254
290
|
//# sourceMappingURL=AgentPolicy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgentPolicy.d.ts","sourceRoot":"","sources":["../src/AgentPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"AgentPolicy.d.ts","sourceRoot":"","sources":["../src/AgentPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAuBlD;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACxB,uCAAuC;IACvC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,wGAAwG;IACxG,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,EAAE,gBAAgB,EAAE,EAAE,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC;IACrG,4DAA4D;IAC5D,OAAO,EAAE,MAAM,CAAC;CACjB;AAOD,eAAO,MAAM,iBAAiB,EAAE,SAAS,EAsBxC,CAAC;AAIF;;;GAGG;AACH,MAAM,MAAM,UAAU,GAClB,UAAU,GACV,mBAAmB,GACnB,kBAAkB,GAClB,wBAAwB,GACxB,oBAAoB,GACpB,WAAW,GACX,gBAAgB,GAChB,YAAY,CAAC;AAEjB;;;GAGG;AACH,MAAM,MAAM,aAAa,GACrB;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,EAAE,EAAE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IACH,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAEpC,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,eAAe,GAAG,OAAO;IAiB5D,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,eAAe,GAAG,OAAO;IAY/D;;;;;OAKG;IACH,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,eAAe,CAAS;IAEhC,SAAS,IAAI,IAAI;IAKjB,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,eAAe,GAAG,cAAc;CAuBpE"}
|
package/dist/AgentPolicy.js
CHANGED
|
@@ -2,13 +2,38 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultAgentPolicy = exports.defaultToolGuards = void 0;
|
|
4
4
|
const agent_pool_1 = require("./agent-pool");
|
|
5
|
+
const prompt_1 = require("./prompt");
|
|
6
|
+
// Recovery-phase KV accounting constants. These size the hardLimit reserve
|
|
7
|
+
// allocation for recoverInline: the prefill cost of the recovery prompt +
|
|
8
|
+
// room for llama.cpp's batch workspace. Used to compute the budget
|
|
9
|
+
// communicated to the model in its recovery prompt.
|
|
10
|
+
const RECOVERY_PREFILL_OVERHEAD = 150;
|
|
11
|
+
const BATCH_BUFFER = 512;
|
|
12
|
+
/**
|
|
13
|
+
* Convert a token budget to a conservative word count for the model-facing
|
|
14
|
+
* prompt. Tokens are tokenizer-specific; words are universal and better
|
|
15
|
+
* reflected in training data. Applies a 0.7 words/token ratio (vs the
|
|
16
|
+
* typical ~0.75) to under-advertise the budget, rounds down to the nearest
|
|
17
|
+
* 10, and floors at 10 so the model always has a non-zero target.
|
|
18
|
+
*/
|
|
19
|
+
function tokenBudgetAsWords(budgetTokens) {
|
|
20
|
+
return Math.max(10, Math.floor(budgetTokens * 0.7 / 10) * 10);
|
|
21
|
+
}
|
|
5
22
|
/** Default guards for deduplication and recursion discipline */
|
|
23
|
+
function parseHistoryArgs(argsStr) {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(argsStr);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
6
31
|
exports.defaultToolGuards = [
|
|
7
32
|
{
|
|
8
33
|
tools: ['fetch_page'],
|
|
9
34
|
reject: (args, history) => {
|
|
10
35
|
const url = args.url;
|
|
11
|
-
return !!url && history.some(h => h.name === 'fetch_page' && h.args === url);
|
|
36
|
+
return !!url && history.some(h => h.name === 'fetch_page' && parseHistoryArgs(h.args).url === url);
|
|
12
37
|
},
|
|
13
38
|
message: 'This URL was already fetched. Try a different source.',
|
|
14
39
|
},
|
|
@@ -16,44 +41,50 @@ exports.defaultToolGuards = [
|
|
|
16
41
|
tools: ['web_search'],
|
|
17
42
|
reject: (args, history) => {
|
|
18
43
|
const query = args.query?.toLowerCase();
|
|
19
|
-
return !!query && history.some(h =>
|
|
44
|
+
return !!query && history.some(h => {
|
|
45
|
+
const prev = parseHistoryArgs(h.args).query?.toLowerCase();
|
|
46
|
+
return h.name === 'web_search' && prev === query;
|
|
47
|
+
});
|
|
20
48
|
},
|
|
21
49
|
message: 'This query was already searched. Refine your search or report findings.',
|
|
22
50
|
},
|
|
23
|
-
{
|
|
24
|
-
tools: ['web_research', 'research'],
|
|
25
|
-
reject: (_args, _lineage, agent) => {
|
|
26
|
-
// Agent-local history: each agent must do its own research before delegating,
|
|
27
|
-
// regardless of what ancestors did. Lineage history would let children bypass
|
|
28
|
-
// this by inheriting parent's search+fetch — producing blind relay chains.
|
|
29
|
-
const local = agent.toolHistory;
|
|
30
|
-
const hasSearch = local.some(h => h.name === 'web_search' || h.name === 'search');
|
|
31
|
-
const hasFetch = local.some(h => h.name === 'fetch_page' || h.name === 'read_file');
|
|
32
|
-
return !hasSearch || !hasFetch;
|
|
33
|
-
},
|
|
34
|
-
message: 'Read your search results with fetch_page before spawning sub-agents.',
|
|
35
|
-
},
|
|
36
51
|
];
|
|
37
52
|
class DefaultAgentPolicy {
|
|
38
53
|
_minToolCalls;
|
|
39
54
|
_guards;
|
|
40
|
-
|
|
55
|
+
_exploreContext;
|
|
56
|
+
_exploreTime;
|
|
41
57
|
_forceExploit = false;
|
|
42
58
|
_recovery;
|
|
43
59
|
_budget;
|
|
60
|
+
_terminalTool;
|
|
44
61
|
_startTime;
|
|
45
62
|
constructor(opts) {
|
|
46
63
|
this._minToolCalls = opts?.minToolCallsBeforeReport ?? 2;
|
|
47
|
-
this.
|
|
64
|
+
this._exploreContext = opts?.shouldExplore?.context ?? 0.4;
|
|
65
|
+
this._exploreTime = opts?.shouldExplore?.time ?? 0.5;
|
|
48
66
|
this._guards = opts?.guards ?? [
|
|
49
67
|
...exports.defaultToolGuards,
|
|
50
68
|
...(opts?.extraGuards ?? []),
|
|
51
69
|
];
|
|
52
70
|
this._recovery = opts?.recovery ?? null;
|
|
53
71
|
this._budget = opts?.budget ?? null;
|
|
72
|
+
this._terminalTool = opts?.terminalTool ?? null;
|
|
54
73
|
this._startTime = performance.now();
|
|
55
74
|
}
|
|
56
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Elapsed wall time for *this agent* (since its first idle→active transition),
|
|
77
|
+
* falling back to the policy's own construction time when the agent hasn't
|
|
78
|
+
* started yet (defensive — shouldn't normally happen).
|
|
79
|
+
*
|
|
80
|
+
* Per-agent timing means orchestrators that spawn agents sequentially (e.g.
|
|
81
|
+
* `chain`) get the correct "how long has this task been running?" semantics
|
|
82
|
+
* without the time budget leaking across iterations.
|
|
83
|
+
*/
|
|
84
|
+
_elapsed(agent) {
|
|
85
|
+
const started = agent?.startedAt ?? this._startTime;
|
|
86
|
+
return performance.now() - started;
|
|
87
|
+
}
|
|
57
88
|
/** KV pressure thresholds for ContextPressure construction.
|
|
58
89
|
* Pool reads this once at setup. */
|
|
59
90
|
get pressureThresholds() {
|
|
@@ -70,11 +101,19 @@ class DefaultAgentPolicy {
|
|
|
70
101
|
return this._handleNoToolCall(agent, parsed);
|
|
71
102
|
if (this._isTerminalTool(tc, config))
|
|
72
103
|
return this._handleTerminalTool(tc, agent, config, pressure);
|
|
73
|
-
|
|
74
|
-
|
|
104
|
+
// Guards before budget: when an agent is over budget AND emitting
|
|
105
|
+
// a tool call the guards already want to reject (duplicate query,
|
|
106
|
+
// duplicate fetch, delegation-before-research), the guard's specific
|
|
107
|
+
// message is more actionable than a generic "report now within N
|
|
108
|
+
// words" turn-limit nudge. Previously, `_isOverBudget` preempted the
|
|
109
|
+
// guard — stuck agents (same query repeated past maxTurns) saw only
|
|
110
|
+
// turn-limit nudges instead of the dedup message that named the
|
|
111
|
+
// actual problem (see trace-1776819196054 agent 65539).
|
|
75
112
|
const guardRejection = this._checkGuards(tc, agent);
|
|
76
113
|
if (guardRejection)
|
|
77
114
|
return guardRejection;
|
|
115
|
+
if (this._isOverBudget(agent, tc, pressure, config))
|
|
116
|
+
return this._handleOverBudget(agent, tc, pressure, config);
|
|
78
117
|
// Normal tool call
|
|
79
118
|
return { type: 'tool_call', tc };
|
|
80
119
|
}
|
|
@@ -104,7 +143,7 @@ class DefaultAgentPolicy {
|
|
|
104
143
|
}
|
|
105
144
|
_isUnderPressure(agent, pressure, config) {
|
|
106
145
|
const timeSoft = this._budget?.time?.softLimit;
|
|
107
|
-
const timeNudge = timeSoft != null && this._elapsed() >= timeSoft;
|
|
146
|
+
const timeNudge = timeSoft != null && this._elapsed(agent) >= timeSoft;
|
|
108
147
|
return agent.turns >= config.maxTurns || pressure.headroom < 0 || timeNudge;
|
|
109
148
|
}
|
|
110
149
|
_isOverBudget(agent, tc, pressure, config) {
|
|
@@ -113,15 +152,21 @@ class DefaultAgentPolicy {
|
|
|
113
152
|
}
|
|
114
153
|
_handleOverBudget(agent, tc, pressure, config) {
|
|
115
154
|
const timeSoft = this._budget?.time?.softLimit;
|
|
116
|
-
const timeNudge = timeSoft != null && this._elapsed() >= timeSoft;
|
|
155
|
+
const timeNudge = timeSoft != null && this._elapsed(agent) >= timeSoft;
|
|
117
156
|
if (config.terminalTool && agent.toolCallCount > 0 && !pressure.critical) {
|
|
118
157
|
if (!this._nudgedThisTick) {
|
|
119
158
|
this._nudgedThisTick = true;
|
|
159
|
+
// Budget the model can emit before `pressure.critical` kills it.
|
|
160
|
+
// Overshoot → kill → recoverInline extracts from the hardLimit reserve.
|
|
161
|
+
// Expressed in words (not tokens) because tokenizers vary across
|
|
162
|
+
// models but words are universal. Under-advertised + rounded down
|
|
163
|
+
// so the model has slack on the ceiling.
|
|
164
|
+
const words = tokenBudgetAsWords(pressure.remaining - pressure.hardLimit);
|
|
120
165
|
const msg = timeNudge
|
|
121
|
-
?
|
|
166
|
+
? `Time limit reached — report your findings now within ${words} words.`
|
|
122
167
|
: agent.turns >= config.maxTurns
|
|
123
|
-
?
|
|
124
|
-
:
|
|
168
|
+
? `Turn limit reached — report your findings now within ${words} words.`
|
|
169
|
+
: `KV memory pressure — report your findings now within ${words} words.`;
|
|
125
170
|
return { type: 'nudge', message: msg };
|
|
126
171
|
}
|
|
127
172
|
return { type: 'tool_call', tc };
|
|
@@ -144,10 +189,11 @@ class DefaultAgentPolicy {
|
|
|
144
189
|
}
|
|
145
190
|
return null;
|
|
146
191
|
}
|
|
147
|
-
onSettleReject(agent, _resultTokens,
|
|
192
|
+
onSettleReject(agent, _resultTokens, pressure, config) {
|
|
148
193
|
// Nudge if possible — stateless, no escalation tracking
|
|
149
194
|
if (config.terminalTool && agent.toolCallCount > 0) {
|
|
150
|
-
|
|
195
|
+
const words = tokenBudgetAsWords(pressure.remaining - pressure.hardLimit);
|
|
196
|
+
return { type: 'nudge', message: `Tool result too large for remaining KV. Report your findings now within ${words} words.` };
|
|
151
197
|
}
|
|
152
198
|
// No terminal tool: kill
|
|
153
199
|
return { type: 'idle', reason: 'pressure_settle_reject' };
|
|
@@ -157,10 +203,33 @@ class DefaultAgentPolicy {
|
|
|
157
203
|
* to wrap up. Overrides pressure-based logic immediately.
|
|
158
204
|
*/
|
|
159
205
|
setExploitMode(force) { this._forceExploit = force; }
|
|
160
|
-
|
|
206
|
+
shouldExit(agent, pressure) {
|
|
207
|
+
// Terminal-tool protection applies in the graceful zone only — once
|
|
208
|
+
// `pressure.critical` fires, the agent must yield so the pool can
|
|
209
|
+
// kill+recover before native OOM. Holding this protection through
|
|
210
|
+
// critical territory was the DOJ runaway cause (trace-1776782401659).
|
|
211
|
+
if (this._terminalTool && agent.currentTool === this._terminalTool && !pressure.critical)
|
|
212
|
+
return false;
|
|
213
|
+
if (!pressure.critical) {
|
|
214
|
+
const timeHard = this._budget?.time?.hardLimit;
|
|
215
|
+
if (timeHard != null && this._elapsed(agent) >= timeHard)
|
|
216
|
+
return true;
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if (this._killedThisTick)
|
|
220
|
+
return false;
|
|
221
|
+
this._killedThisTick = true;
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
shouldExplore(agent, pressure) {
|
|
161
225
|
if (this._forceExploit)
|
|
162
226
|
return false;
|
|
163
|
-
|
|
227
|
+
const contextOk = pressure.percentAvailable / 100 > this._exploreContext;
|
|
228
|
+
const timeSoftLimit = this._budget?.time?.softLimit;
|
|
229
|
+
const timeOk = timeSoftLimit == null
|
|
230
|
+
? true
|
|
231
|
+
: this._elapsed(agent) / timeSoftLimit < this._exploreTime;
|
|
232
|
+
return contextOk && timeOk;
|
|
164
233
|
}
|
|
165
234
|
/**
|
|
166
235
|
* Trailing stop: at most one agent nudged or killed per tick.
|
|
@@ -174,19 +243,7 @@ class DefaultAgentPolicy {
|
|
|
174
243
|
this._killedThisTick = false;
|
|
175
244
|
this._nudgedThisTick = false;
|
|
176
245
|
}
|
|
177
|
-
|
|
178
|
-
if (!pressure.critical) {
|
|
179
|
-
const timeHard = this._budget?.time?.hardLimit;
|
|
180
|
-
if (timeHard != null && this._elapsed() >= timeHard)
|
|
181
|
-
return true;
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
if (this._killedThisTick)
|
|
185
|
-
return false;
|
|
186
|
-
this._killedThisTick = true;
|
|
187
|
-
return true;
|
|
188
|
-
}
|
|
189
|
-
onRecovery(agent) {
|
|
246
|
+
onRecovery(agent, pressure) {
|
|
190
247
|
if (!this._recovery)
|
|
191
248
|
return { type: 'skip' };
|
|
192
249
|
const minTokens = this._recovery.minTokens ?? 100;
|
|
@@ -194,7 +251,21 @@ class DefaultAgentPolicy {
|
|
|
194
251
|
if (agent.tokenCount < minTokens || agent.toolCallCount < minToolCalls) {
|
|
195
252
|
return { type: 'skip' };
|
|
196
253
|
}
|
|
197
|
-
|
|
254
|
+
// Budget recovery's generation can consume: hardLimit reserve minus the
|
|
255
|
+
// prefill overhead and llama.cpp's batch workspace. Expressed as words
|
|
256
|
+
// (not tokens) and under-advertised so the model has slack — tokenizers
|
|
257
|
+
// vary across models but words are universal. Rendered into the prompt
|
|
258
|
+
// as `it.budget` so authors can reference it via `<%= it.budget %>`.
|
|
259
|
+
const budgetTokens = Math.max(50, pressure.remaining - RECOVERY_PREFILL_OVERHEAD - BATCH_BUFFER);
|
|
260
|
+
const budget = tokenBudgetAsWords(budgetTokens);
|
|
261
|
+
const tctx = { budget };
|
|
262
|
+
return {
|
|
263
|
+
type: 'extract',
|
|
264
|
+
prompt: {
|
|
265
|
+
system: (0, prompt_1.renderTemplate)(this._recovery.prompt.system, tctx),
|
|
266
|
+
user: (0, prompt_1.renderTemplate)(this._recovery.prompt.user, tctx),
|
|
267
|
+
},
|
|
268
|
+
};
|
|
198
269
|
}
|
|
199
270
|
}
|
|
200
271
|
exports.DefaultAgentPolicy = DefaultAgentPolicy;
|
package/dist/AgentPolicy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgentPolicy.js","sourceRoot":"","sources":["../src/AgentPolicy.ts"],"names":[],"mappings":";;;AACA,6CAA+C;
|
|
1
|
+
{"version":3,"file":"AgentPolicy.js","sourceRoot":"","sources":["../src/AgentPolicy.ts"],"names":[],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L,IAAI,EAAE,SAAS;YACf,MAAM,EAAE;gBACN,MAAM,EAAE,IAAA,uBAAc,EAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC;gBAC1D,IAAI,EAAE,IAAA,uBAAc,EAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC;aACvD;SACF,CAAC;IACJ,CAAC;CACF;AA9OD,gDA8OC"}
|
package/dist/agent-pool.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Operation } from 'effection';
|
|
1
|
+
import type { Operation, Subscription } from 'effection';
|
|
2
2
|
import { type SessionContext } from '@lloyal-labs/sdk';
|
|
3
|
-
import type { PressureThresholds, AgentPoolOptions, AgentPoolResult } from './types';
|
|
3
|
+
import type { PressureThresholds, AgentPoolOptions, AgentPoolResult, AgentEvent } from './types';
|
|
4
4
|
/**
|
|
5
5
|
* Immutable KV budget snapshot for one tick of the agent loop
|
|
6
6
|
*
|
|
@@ -44,8 +44,20 @@ import type { PressureThresholds, AgentPoolOptions, AgentPoolResult } from './ty
|
|
|
44
44
|
export declare class ContextPressure {
|
|
45
45
|
/** Default softLimit: 1024 tokens reserved for downstream work */
|
|
46
46
|
static readonly DEFAULT_SOFT_LIMIT = 1024;
|
|
47
|
-
/**
|
|
48
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Default hardLimit: 512 tokens — matches llama.cpp's default `n_batch`.
|
|
49
|
+
* The pool validates at startup that `hardLimit >= nBatch`; the default
|
|
50
|
+
* is sized to satisfy the invariant for the default llama.cpp context.
|
|
51
|
+
* Recovery fits within the `hardLimit` reserve.
|
|
52
|
+
*/
|
|
53
|
+
static readonly DEFAULT_HARD_LIMIT = 512;
|
|
54
|
+
/**
|
|
55
|
+
* Assumed `nBatch` when the native binding doesn't expose it.
|
|
56
|
+
* Pool startup validates `pressureThresholds.hardLimit >= this`.
|
|
57
|
+
* TODO: once `SessionContext.nBatch` is exposed (lloyal.node
|
|
58
|
+
* follow-up), read from ctx.nBatch instead.
|
|
59
|
+
*/
|
|
60
|
+
static readonly ASSUMED_N_BATCH = 512;
|
|
49
61
|
/** Total KV cache capacity (max positions). 0 when no context limit. */
|
|
50
62
|
readonly nCtx: number;
|
|
51
63
|
/** KV cells currently in use (monotonic within a pool run). */
|
|
@@ -125,5 +137,5 @@ export declare class ContextPressure {
|
|
|
125
137
|
*
|
|
126
138
|
* @category Agents
|
|
127
139
|
*/
|
|
128
|
-
export declare function useAgentPool(opts: AgentPoolOptions): Operation<AgentPoolResult
|
|
140
|
+
export declare function useAgentPool(opts: AgentPoolOptions): Operation<Subscription<AgentEvent, AgentPoolResult>>;
|
|
129
141
|
//# sourceMappingURL=agent-pool.d.ts.map
|
package/dist/agent-pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-pool.d.ts","sourceRoot":"","sources":["../src/agent-pool.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"agent-pool.d.ts","sourceRoot":"","sources":["../src/agent-pool.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzD,OAAO,EAA+G,KAAK,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAYpK,OAAO,KAAK,EACV,kBAAkB,EAElB,gBAAgB,EAChB,eAAe,EACf,UAAU,EAEX,MAAM,SAAS,CAAC;AAsBjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,eAAe;IAC1B,kEAAkE;IAClE,MAAM,CAAC,QAAQ,CAAC,kBAAkB,QAAQ;IAC1C;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,kBAAkB,OAAO;IACzC;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,eAAe,OAAO;IAEtC,wEAAwE;IACxE,QAAQ,C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