dsh-harness-reflex 0.1.0 → 0.3.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 +20 -6
- package/client.js +3511 -185
- package/dist/attribution.d.ts +266 -0
- package/dist/attribution.js +238 -0
- package/dist/attribution.js.map +1 -0
- package/dist/bridge.d.ts +311 -13
- package/dist/bridge.js +714 -42
- package/dist/bridge.js.map +1 -1
- package/dist/client-bundle.js +4 -1
- package/dist/client-bundle.js.map +1 -1
- package/dist/command.js +10 -0
- package/dist/command.js.map +1 -1
- package/dist/config.d.ts +530 -22
- package/dist/config.js +808 -27
- package/dist/config.js.map +1 -1
- package/dist/delegation-dispatch.d.ts +132 -0
- package/dist/delegation-dispatch.js +186 -0
- package/dist/delegation-dispatch.js.map +1 -0
- package/dist/delegation-health.d.ts +131 -0
- package/dist/delegation-health.js +97 -0
- package/dist/delegation-health.js.map +1 -0
- package/dist/delegation.d.ts +148 -0
- package/dist/delegation.js +186 -0
- package/dist/delegation.js.map +1 -0
- package/dist/effort-delegation.d.ts +144 -0
- package/dist/effort-delegation.js +280 -0
- package/dist/effort-delegation.js.map +1 -0
- package/dist/effort.d.ts +141 -0
- package/dist/effort.js +251 -9
- package/dist/effort.js.map +1 -1
- package/dist/host-patch.d.ts +156 -0
- package/dist/host-patch.js +534 -0
- package/dist/host-patch.js.map +1 -0
- package/dist/index.d.ts +12 -3
- package/dist/index.js +99 -7
- package/dist/index.js.map +1 -1
- package/dist/interventions.d.ts +98 -373
- package/dist/interventions.js +442 -1702
- package/dist/interventions.js.map +1 -1
- package/dist/panel-api.d.ts +328 -4
- package/dist/panel-api.js +322 -15
- package/dist/panel-api.js.map +1 -1
- package/dist/panel-mount.d.ts +81 -22
- package/dist/panel-mount.js +228 -7
- package/dist/panel-mount.js.map +1 -1
- package/dist/routing.d.ts +23 -18
- package/dist/routing.js +471 -153
- package/dist/routing.js.map +1 -1
- package/dist/session-audit.d.ts +70 -0
- package/dist/session-audit.js +73 -0
- package/dist/session-audit.js.map +1 -1
- package/dist/systemone-probe.d.ts +114 -0
- package/dist/systemone-probe.js +383 -0
- package/dist/systemone-probe.js.map +1 -0
- package/dist/testing.d.ts +212 -7
- package/dist/testing.js +333 -27
- package/dist/testing.js.map +1 -1
- package/dist/tools.d.ts +109 -3
- package/dist/tools.js +103 -4
- package/dist/tools.js.map +1 -1
- package/dist/unconfigured-backend.d.ts +72 -0
- package/dist/unconfigured-backend.js +94 -0
- package/dist/unconfigured-backend.js.map +1 -0
- package/dist/vendor/harness-reflex/backend/factory.d.ts +31 -1
- package/dist/vendor/harness-reflex/backend/factory.js +18 -3
- package/dist/vendor/harness-reflex/backend/factory.js.map +1 -1
- package/dist/vendor/harness-reflex/backend/jev.d.ts +20 -0
- package/dist/vendor/harness-reflex/backend/jev.js +11 -4
- package/dist/vendor/harness-reflex/backend/jev.js.map +1 -1
- package/dist/vendor/harness-reflex/backend/simple-jev.d.ts +16 -0
- package/dist/vendor/harness-reflex/backend/simple-jev.js +17 -6
- package/dist/vendor/harness-reflex/backend/simple-jev.js.map +1 -1
- package/dist/vendor/harness-reflex/core/closing.d.ts +90 -0
- package/dist/vendor/harness-reflex/core/closing.js +111 -0
- package/dist/vendor/harness-reflex/core/closing.js.map +1 -0
- package/dist/vendor/harness-reflex/core/evidence.d.ts +119 -0
- package/dist/vendor/harness-reflex/core/evidence.js +209 -0
- package/dist/vendor/harness-reflex/core/evidence.js.map +1 -0
- package/dist/vendor/harness-reflex/core/judge.d.ts +221 -0
- package/dist/vendor/harness-reflex/core/judge.js +251 -0
- package/dist/vendor/harness-reflex/core/judge.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-closing.d.ts +237 -0
- package/dist/vendor/harness-reflex/core/orchestrate-closing.js +250 -0
- package/dist/vendor/harness-reflex/core/orchestrate-closing.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-correction.d.ts +209 -0
- package/dist/vendor/harness-reflex/core/orchestrate-correction.js +141 -0
- package/dist/vendor/harness-reflex/core/orchestrate-correction.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-host.d.ts +225 -0
- package/dist/vendor/harness-reflex/core/orchestrate-host.js +80 -0
- package/dist/vendor/harness-reflex/core/orchestrate-host.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-stagnation.d.ts +259 -0
- package/dist/vendor/harness-reflex/core/orchestrate-stagnation.js +270 -0
- package/dist/vendor/harness-reflex/core/orchestrate-stagnation.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate.d.ts +357 -0
- package/dist/vendor/harness-reflex/core/orchestrate.js +418 -0
- package/dist/vendor/harness-reflex/core/orchestrate.js.map +1 -0
- package/dist/vendor/harness-reflex/core/policy.d.ts +122 -0
- package/dist/vendor/harness-reflex/core/policy.js +102 -0
- package/dist/vendor/harness-reflex/core/policy.js.map +1 -0
- package/dist/vendor/harness-reflex/core/prune-plan.d.ts +141 -0
- package/dist/vendor/harness-reflex/core/prune-plan.js +390 -0
- package/dist/vendor/harness-reflex/core/prune-plan.js.map +1 -0
- package/dist/vendor/harness-reflex/core/text.d.ts +36 -0
- package/dist/vendor/harness-reflex/core/text.js +66 -0
- package/dist/vendor/harness-reflex/core/text.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/audit.d.ts +18 -0
- package/dist/vendor/harness-reflex/ports/audit.js +11 -0
- package/dist/vendor/harness-reflex/ports/audit.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/clock.d.ts +14 -0
- package/dist/vendor/harness-reflex/ports/clock.js +11 -0
- package/dist/vendor/harness-reflex/ports/clock.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/decision.d.ts +75 -0
- package/dist/vendor/harness-reflex/ports/decision.js +2 -0
- package/dist/vendor/harness-reflex/ports/decision.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/index.d.ts +14 -0
- package/dist/vendor/harness-reflex/ports/index.js +2 -0
- package/dist/vendor/harness-reflex/ports/index.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/log.d.ts +22 -0
- package/dist/vendor/harness-reflex/ports/log.js +12 -0
- package/dist/vendor/harness-reflex/ports/log.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/state.d.ts +46 -0
- package/dist/vendor/harness-reflex/ports/state.js +13 -0
- package/dist/vendor/harness-reflex/ports/state.js.map +1 -0
- package/package.json +25 -13
package/dist/routing.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isAgentLoopRequest, markAgentLoopRequest } from '@deepseek-ai/dsh-llm';
|
|
2
|
-
import { formatEffortStatusLine } from './effort.js';
|
|
2
|
+
import { formatEffortStatusLine, EFFORT_CONTEXT_TURNS } from './effort.js';
|
|
3
3
|
import { appendEffortRecord } from './session-audit.js';
|
|
4
4
|
import { resolveAdaptiveIntent } from './adaptive-intent.js';
|
|
5
5
|
import { alignReasoningEffort, estimateContextTokens } from './router.js';
|
|
@@ -221,8 +221,8 @@ export function installAdaptiveEffortSurface(_ctx, llm, policy) {
|
|
|
221
221
|
}
|
|
222
222
|
// Last gate before dispatch: the marker is an intent, never an effort any
|
|
223
223
|
// physical adapter can accept, so it is resolved here for the paths that
|
|
224
|
-
//
|
|
225
|
-
// `prepareCall` consumers). With the switch off there is no policy to
|
|
224
|
+
// never went through the `agent/pre-step` decision (hand-built one-shot
|
|
225
|
+
// calls, `prepareCall` consumers). With the switch off there is no policy to
|
|
226
226
|
// resolve it *to*, so it is deleted and the provider default applies.
|
|
227
227
|
if (!policy.enabled) {
|
|
228
228
|
const cleared = { ...config };
|
|
@@ -331,6 +331,350 @@ export function filterVirtualSwitchNotices(decision, virtualProvider, virtualMod
|
|
|
331
331
|
return decision;
|
|
332
332
|
return { ...decision, messages: kept };
|
|
333
333
|
}
|
|
334
|
+
/**
|
|
335
|
+
* How long one step's route/effort decision may take before the step is
|
|
336
|
+
* admitted with its selection untouched.
|
|
337
|
+
*
|
|
338
|
+
* The decision costs a System 1 round trip (routing) plus, adaptively, a second
|
|
339
|
+
* one (effort). Real backends answer in tens of milliseconds; this bound only
|
|
340
|
+
* exists so a hung or unreachable backend cannot stall a turn before its first
|
|
341
|
+
* token. Timing out is a *degradation*, not an error: the step runs on the
|
|
342
|
+
* adapter's own defaults, exactly like a deployment that never configured the
|
|
343
|
+
* layer, and the next step tries again.
|
|
344
|
+
*/
|
|
345
|
+
const STEP_DECISION_TIMEOUT_MS = 4000;
|
|
346
|
+
/**
|
|
347
|
+
* The budget one step's decision actually gets.
|
|
348
|
+
*
|
|
349
|
+
* `REFLEX_STEP_DECISION_TIMEOUT_MS` overrides the default so a test can prove
|
|
350
|
+
* the timeout path without waiting four seconds; an unset or unparseable value
|
|
351
|
+
* keeps {@link STEP_DECISION_TIMEOUT_MS}.
|
|
352
|
+
*
|
|
353
|
+
* @returns the budget in milliseconds.
|
|
354
|
+
*/
|
|
355
|
+
function stepDecisionBudget() {
|
|
356
|
+
const raw = Number(process.env.REFLEX_STEP_DECISION_TIMEOUT_MS);
|
|
357
|
+
return Number.isFinite(raw) && raw > 0 ? raw : STEP_DECISION_TIMEOUT_MS;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Run a decision but let nothing it does escape an enclosing `try`.
|
|
361
|
+
*
|
|
362
|
+
* Everything the decision needs from its payload is read by {@link planStep}
|
|
363
|
+
* before the first `await`, so the attempt can safely be abandoned: a decision
|
|
364
|
+
* that loses the timeout race keeps running detached, and a rejection it meets
|
|
365
|
+
* afterwards lands in a no-op handler instead of becoming an unhandled rejection
|
|
366
|
+
* that would take the Host down.
|
|
367
|
+
*
|
|
368
|
+
* @param run - the decision to start.
|
|
369
|
+
* @returns a promise that settles exactly once and never rejects.
|
|
370
|
+
*/
|
|
371
|
+
function startDecision(run) {
|
|
372
|
+
const attempt = Promise.resolve().then(run);
|
|
373
|
+
attempt.catch(NOOP);
|
|
374
|
+
return attempt;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Race one decision against a deadline.
|
|
378
|
+
*
|
|
379
|
+
* The timer is unref'd and cleared on every path, so neither a settled decision
|
|
380
|
+
* nor an abandoned one keeps the process alive or leaks a handle.
|
|
381
|
+
*
|
|
382
|
+
* @param attempt - the decision, already started.
|
|
383
|
+
* @param ms - budget in milliseconds.
|
|
384
|
+
* @returns the decision, or `undefined` when the budget ran out first.
|
|
385
|
+
*/
|
|
386
|
+
function withBudget(attempt, ms) {
|
|
387
|
+
return new Promise((resolve) => {
|
|
388
|
+
let settled = false;
|
|
389
|
+
const finish = (value) => {
|
|
390
|
+
if (settled)
|
|
391
|
+
return;
|
|
392
|
+
settled = true;
|
|
393
|
+
clearTimeout(timer);
|
|
394
|
+
resolve(value);
|
|
395
|
+
};
|
|
396
|
+
const timer = setTimeout(() => finish(undefined), ms);
|
|
397
|
+
// Node keeps the process alive for a pending timer; a decision that never
|
|
398
|
+
// answers must not turn into a hung host.
|
|
399
|
+
timer.unref?.();
|
|
400
|
+
attempt.then(finish, () => finish(undefined));
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Write one plan into the step's selection object.
|
|
405
|
+
*
|
|
406
|
+
* `payload.modelSelection` is `Readonly<ModelSelection>` in the contract, but
|
|
407
|
+
* that is a compile-time marker only: DSH 0.2.2 does not freeze the object at
|
|
408
|
+
* runtime, and mutating it here mutates `dsh-agent-loop`'s own local `selection`,
|
|
409
|
+
* which is what its `prepareRequest` guards compare against. That is the whole
|
|
410
|
+
* point of {@link installModelRouting}'s pre-step decision, and there is no
|
|
411
|
+
* other seam — `agent.model.select()` records a selection that only the *next*
|
|
412
|
+
* `current()` reads, and `current()` for this step has already been called.
|
|
413
|
+
* The write therefore goes through a mutable view of the same object, and a
|
|
414
|
+
* payload frozen by an embedder raises `TypeError`, which the caller catches.
|
|
415
|
+
*
|
|
416
|
+
* @param selection - the step's selection (the loop's `selection` object).
|
|
417
|
+
* @param plan - the decided route and effort.
|
|
418
|
+
*/
|
|
419
|
+
function applyPlan(selection, plan) {
|
|
420
|
+
const target = selection;
|
|
421
|
+
if (plan.provider !== undefined)
|
|
422
|
+
target.provider = plan.provider;
|
|
423
|
+
if (plan.model !== undefined)
|
|
424
|
+
target.model = plan.model;
|
|
425
|
+
if (!('reasoningEffort' in plan))
|
|
426
|
+
return;
|
|
427
|
+
if (plan.reasoningEffort === undefined)
|
|
428
|
+
delete target.reasoningEffort;
|
|
429
|
+
else
|
|
430
|
+
target.reasoningEffort = plan.reasoningEffort;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* The provider route the caller composed this agent with.
|
|
434
|
+
*
|
|
435
|
+
* `agent.options` is the durable intent: it is what the model selector was set
|
|
436
|
+
* to, and unlike the step's selection it is never overwritten by a physical
|
|
437
|
+
* route this layer chose for one step.
|
|
438
|
+
*
|
|
439
|
+
* @param agent - the stepping agent.
|
|
440
|
+
* @returns the composed provider id, or `undefined` when it carries none.
|
|
441
|
+
*/
|
|
442
|
+
function intendedProvider(agent) {
|
|
443
|
+
try {
|
|
444
|
+
const provider = agent?.options?.provider;
|
|
445
|
+
return typeof provider === 'string' && provider.length > 0 ? provider : undefined;
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* The virtual model the caller composed this agent with.
|
|
453
|
+
* @param agent - the stepping agent.
|
|
454
|
+
* @returns the composed model id, or `undefined` when it carries none.
|
|
455
|
+
*/
|
|
456
|
+
function intentModel(agent) {
|
|
457
|
+
try {
|
|
458
|
+
const model = agent?.options?.model;
|
|
459
|
+
return typeof model === 'string' && model.length > 0 ? model : undefined;
|
|
460
|
+
}
|
|
461
|
+
catch {
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Whether one agent is a delegated subagent.
|
|
467
|
+
*
|
|
468
|
+
* `effort-delegation.ts` treats a positive `delegationDepth` as authoritative and
|
|
469
|
+
* falls back to `origin: 'subagent'`; this check has to agree with it, or a
|
|
470
|
+
* child whose depth was recorded but whose origin was not would have its effort
|
|
471
|
+
* decided here *and* inherited there.
|
|
472
|
+
*
|
|
473
|
+
* @param agent - the stepping agent.
|
|
474
|
+
* @returns whether the agent was delegated.
|
|
475
|
+
*/
|
|
476
|
+
function isDelegatedAgent(agent) {
|
|
477
|
+
try {
|
|
478
|
+
const header = agent?.session?.header;
|
|
479
|
+
if (typeof header?.delegationDepth === 'number')
|
|
480
|
+
return header.delegationDepth > 0;
|
|
481
|
+
return header?.origin === 'subagent';
|
|
482
|
+
}
|
|
483
|
+
catch {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Decide one step's route and reasoning effort.
|
|
489
|
+
*
|
|
490
|
+
* This is the body the `agent/request` waterfall used to run (g-056). It
|
|
491
|
+
* resolves the caller's durable intent, routes a virtual request through System
|
|
492
|
+
* 1, then either adapts the effort (marker present) or aligns it onto the routed
|
|
493
|
+
* model's vocabulary (concrete level). The order is preserved exactly —
|
|
494
|
+
* intent → route → effort — because the effort is projected onto the model the
|
|
495
|
+
* route actually chose.
|
|
496
|
+
*
|
|
497
|
+
* Reuse is part of the contract, not an optimisation: `agent/pre-step` runs once
|
|
498
|
+
* per *step*, while a decision costs up to two System 1 round trips. A later step
|
|
499
|
+
* of the same turn reuses the turn's plan, which is also why a turn can no longer
|
|
500
|
+
* change its physical model mid-way.
|
|
501
|
+
*
|
|
502
|
+
* @param input - the step, its selection, and the installation's live seams.
|
|
503
|
+
* @returns the plan to write, or `undefined` when the step needs no change.
|
|
504
|
+
*/
|
|
505
|
+
async function planStep(input) {
|
|
506
|
+
const { ctx, agent, router, effort, routes, signal, selection, facts, probe } = input;
|
|
507
|
+
// Everything below this line is read synchronously, before the first `await`,
|
|
508
|
+
// so an abandoned (timed-out) attempt cannot observe a payload that moved on.
|
|
509
|
+
if (signal?.aborted)
|
|
510
|
+
return undefined;
|
|
511
|
+
// The selection is the step's starting configuration; `resolveAdaptiveIntent`
|
|
512
|
+
// may replace the virtual route and effort with the caller's durable intent.
|
|
513
|
+
const base = {
|
|
514
|
+
provider: selection.provider,
|
|
515
|
+
model: selection.model,
|
|
516
|
+
...(selection.reasoningEffort === undefined
|
|
517
|
+
? {}
|
|
518
|
+
: { reasoningEffort: selection.reasoningEffort })
|
|
519
|
+
};
|
|
520
|
+
const config = resolveAdaptiveIntent(ctx, agent, base, (provider) => router.owns(provider));
|
|
521
|
+
const remembered = recallFacts(facts, agent);
|
|
522
|
+
const requested = config.reasoningEffort;
|
|
523
|
+
const task = remembered?.task;
|
|
524
|
+
// With `effort.enabled` off this layer manages no effort at all: it still
|
|
525
|
+
// routes, but the only edit it is allowed to make to `reasoningEffort` is
|
|
526
|
+
// removing its own marker. That keeps a concrete level byte-identical on its
|
|
527
|
+
// way to the adapter, and it means no decision, no audit entry and no status
|
|
528
|
+
// can be produced for a deployment that switched the layer off.
|
|
529
|
+
const effortOff = effort.enabled === false;
|
|
530
|
+
// Reflex only automatically adapts reasoning effort when the effort was
|
|
531
|
+
// explicitly chosen as reflex/auto, or when an unpinned request names the
|
|
532
|
+
// virtual provider (which defaults to adaptive effort). If the user selected
|
|
533
|
+
// High, Low, Medium, Off, or Provider Default, reflex does not touch effort.
|
|
534
|
+
// A delegated agent's effort was already settled by `effort-delegation.ts` on
|
|
535
|
+
// its own selection, so this layer must not adapt it here either.
|
|
536
|
+
const delegated = isDelegatedAgent(agent);
|
|
537
|
+
const isAdaptive = !effortOff &&
|
|
538
|
+
!delegated &&
|
|
539
|
+
(isAdaptiveMarker(requested) ||
|
|
540
|
+
(requested === undefined && router.owns(config.provider) && isAdaptiveMarker(effort.setting)));
|
|
541
|
+
// Same-turn follow-up steps (step > 1) reuse the turn's decision: no second
|
|
542
|
+
// System 1 call, no second effort event, 0ms latency. The turn is the unit of
|
|
543
|
+
// decision — a later step carrying user text is still the same turn, so it
|
|
544
|
+
// does not break the reuse (g-063).
|
|
545
|
+
const isFollowupStep = (input.step ?? 1) > 1 &&
|
|
546
|
+
remembered?.decided !== undefined &&
|
|
547
|
+
remembered.decided.turn === input.turn &&
|
|
548
|
+
// A config-only save may replace the policy mid-turn; a decision taken
|
|
549
|
+
// under the previous policy is not this turn's answer any more.
|
|
550
|
+
remembered.decided.generation === effort.generation;
|
|
551
|
+
// Whether this step belongs on the virtual route. It cannot be read off the
|
|
552
|
+
// step's *selection*: this layer writes the physical target into that object,
|
|
553
|
+
// and the loop records it as the session's latest selection (the request header
|
|
554
|
+
// retires the pending choice), so every later step starts from a physical
|
|
555
|
+
// provider this router does not own. Asking the selection would make routing
|
|
556
|
+
// stop after the first step, which is why the question is asked of the
|
|
557
|
+
// caller's durable intent — the agent's composed options — and of the intent
|
|
558
|
+
// `resolveAdaptiveIntent` just recovered from the selection projection.
|
|
559
|
+
const intent = intendedProvider(agent);
|
|
560
|
+
const stepOwnsRoute = router.owns(config.provider);
|
|
561
|
+
const onVirtualRoute = routes && ((intent !== undefined && router.owns(intent)) || stepOwnsRoute);
|
|
562
|
+
// The route name to hand `routeModel`: the step's own when it still names the
|
|
563
|
+
// virtual provider, otherwise the router's current name (the intent path).
|
|
564
|
+
const virtualProviderForStep = stepOwnsRoute ? config.provider : router.virtualProvider;
|
|
565
|
+
const plan = {};
|
|
566
|
+
// Reuse only applies while the step is still *on the virtual route*: a caller
|
|
567
|
+
// that has switched the session onto a physical model (or pinned a concrete
|
|
568
|
+
// effort on one) must never have that decision replayed over its own choice.
|
|
569
|
+
const sameVirtualModel = remembered?.decided?.virtualModel === undefined || remembered.decided.virtualModel === config.model;
|
|
570
|
+
if (remembered?.decided && isFollowupStep && sameVirtualModel && onVirtualRoute) {
|
|
571
|
+
// The turn's route and effort, replayed exactly: re-routing a later step
|
|
572
|
+
// would move a warm session between physical models mid-turn.
|
|
573
|
+
const reuse = remembered.decided;
|
|
574
|
+
if (onVirtualRoute) {
|
|
575
|
+
plan.provider = reuse.provider;
|
|
576
|
+
plan.model = reuse.model;
|
|
577
|
+
}
|
|
578
|
+
if (reuse.effort !== undefined)
|
|
579
|
+
plan.reasoningEffort = reuse.effort;
|
|
580
|
+
return plan;
|
|
581
|
+
}
|
|
582
|
+
let routed = config;
|
|
583
|
+
let switched;
|
|
584
|
+
let heldForCache;
|
|
585
|
+
if (onVirtualRoute) {
|
|
586
|
+
// The virtual *model* to route is the one the caller's durable intent names,
|
|
587
|
+
// which `resolveAdaptiveIntent` has already folded into `config` (it
|
|
588
|
+
// prefers the session's pending selection over the step's own). Using
|
|
589
|
+
// `selection.model` here would instead route on whatever this layer wrote
|
|
590
|
+
// into the previous step, i.e. on a physical id it cannot route at all.
|
|
591
|
+
const virtualModel = config.model;
|
|
592
|
+
const decision = await router.routeModel({
|
|
593
|
+
provider: virtualProviderForStep,
|
|
594
|
+
model: virtualModel,
|
|
595
|
+
task,
|
|
596
|
+
...describeAgent(agent),
|
|
597
|
+
turn: input.turn,
|
|
598
|
+
step: input.step,
|
|
599
|
+
// Cache-affinity facts: the model this session already holds and how
|
|
600
|
+
// much context it is defending.
|
|
601
|
+
...(remembered?.currentModel ? { currentModel: remembered.currentModel } : {}),
|
|
602
|
+
...(remembered === undefined ? {} : { contextChars: remembered.contextChars }),
|
|
603
|
+
contextTokens: estimateContextTokens(remembered?.contextChars ?? 0)
|
|
604
|
+
});
|
|
605
|
+
routed = { ...config, provider: decision.target.provider, model: decision.target.model };
|
|
606
|
+
switched = decision.switched;
|
|
607
|
+
heldForCache = decision.reason === 'sticky-affinity';
|
|
608
|
+
plan.provider = decision.target.provider;
|
|
609
|
+
plan.model = decision.target.model;
|
|
610
|
+
rememberTarget(facts, agent, decision.target);
|
|
611
|
+
}
|
|
612
|
+
// A routed turn is recorded even when no effort was decided: its later steps
|
|
613
|
+
// must reuse the physical target instead of asking System 1 again, or a turn
|
|
614
|
+
// could migrate between models on every tool call.
|
|
615
|
+
const settle = (effortToWrite, changed) => {
|
|
616
|
+
if (plan.provider !== undefined) {
|
|
617
|
+
rememberDecision(facts, agent, input.turn, routed, { changed, ...(effortToWrite === undefined ? {} : { effort: effortToWrite }), outcome: 'passthrough' }, effort.generation, config.model);
|
|
618
|
+
}
|
|
619
|
+
if (changed)
|
|
620
|
+
plan.reasoningEffort = effortToWrite;
|
|
621
|
+
return Object.keys(plan).length === 0 ? undefined : plan;
|
|
622
|
+
};
|
|
623
|
+
if (!isAdaptive) {
|
|
624
|
+
if (effortOff) {
|
|
625
|
+
// Transparent pass-through: the selection keeps the caller's own level
|
|
626
|
+
// (never aligned, never snapped onto the routed model's vocabulary), and
|
|
627
|
+
// the plugin's marker is dropped instead of being translated — the
|
|
628
|
+
// adapter then applies the provider default, which is the only safe
|
|
629
|
+
// reading of "reflex" once the adaptive layer is switched off.
|
|
630
|
+
return settle(undefined, isAdaptiveMarker(requested));
|
|
631
|
+
}
|
|
632
|
+
// A delegated selection keeps exactly the effort it fixed: not aligned, not
|
|
633
|
+
// snapped. The selection layer has already made sure this value is a
|
|
634
|
+
// concrete level the adapter accepts.
|
|
635
|
+
if (delegated || requested === undefined)
|
|
636
|
+
return settle(requested, false);
|
|
637
|
+
// Concrete effort (high, low, off...): align if needed, never adapt! This is
|
|
638
|
+
// the *non-adaptive* branch, and it moves here for the same reason the
|
|
639
|
+
// adaptive one does — alignment is still a rewrite of the fixed value, which
|
|
640
|
+
// `agent/request` may no longer make.
|
|
641
|
+
const aligned = await alignReasoningEffort(requested, { provider: routed.provider, model: routed.model }, probe);
|
|
642
|
+
return settle(aligned, aligned !== requested);
|
|
643
|
+
}
|
|
644
|
+
const resolution = await effort.resolve({
|
|
645
|
+
requested,
|
|
646
|
+
target: { provider: routed.provider, model: routed.model },
|
|
647
|
+
probe,
|
|
648
|
+
task,
|
|
649
|
+
// Earlier turns of this agent: since g-063 every decision sees them, inside
|
|
650
|
+
// the effort layer's window, so a short prompt is graded against the work it
|
|
651
|
+
// continues instead of on its own few characters.
|
|
652
|
+
...(remembered?.turns?.length ? { history: remembered.turns.map((entry) => ({ text: entry.text })) } : {}),
|
|
653
|
+
// The level the last adaptive decision produced. It was recorded since g-056
|
|
654
|
+
// to recognise a header echo and nothing ever read it; g-063 makes it the
|
|
655
|
+
// decision input's prior, so "继续" after a hard turn cannot be graded cold.
|
|
656
|
+
...(remembered?.lastAdaptiveEffort ? { previousLevel: remembered.lastAdaptiveEffort } : {}),
|
|
657
|
+
agent: describeAgent(agent),
|
|
658
|
+
turn: input.turn,
|
|
659
|
+
step: input.step,
|
|
660
|
+
requestedProvider: config.provider,
|
|
661
|
+
requestedModel: config.model,
|
|
662
|
+
...(switched === undefined ? {} : { switched }),
|
|
663
|
+
...(heldForCache === undefined ? {} : { heldForCache })
|
|
664
|
+
});
|
|
665
|
+
// The decision is recorded where it is *made* now (g-056): the ledger, the
|
|
666
|
+
// session audit and the status feed all hear about it from here, not from the
|
|
667
|
+
// read-only request waterfall.
|
|
668
|
+
if (resolution.status)
|
|
669
|
+
reportEffort(ctx, agent, resolution.status);
|
|
670
|
+
rememberDecision(facts, agent, input.turn, routed, resolution, effort.generation, config.model);
|
|
671
|
+
// Only an effort that was asked for — or one this layer just decided — is
|
|
672
|
+
// written. A selection that never carried one keeps carrying none.
|
|
673
|
+
const decidedEffort = resolution.changed ? resolution.effort : requested;
|
|
674
|
+
if (decidedEffort !== requested)
|
|
675
|
+
plan.reasoningEffort = decidedEffort;
|
|
676
|
+
return Object.keys(plan).length === 0 ? undefined : plan;
|
|
677
|
+
}
|
|
334
678
|
/**
|
|
335
679
|
* Install the virtual route and its interception layers.
|
|
336
680
|
* @param ctx - the plugin's context (untagged listeners here observe every agent).
|
|
@@ -561,160 +905,91 @@ export function installModelRouting(ctx, options) {
|
|
|
561
905
|
}
|
|
562
906
|
catch { }
|
|
563
907
|
}));
|
|
908
|
+
/**
|
|
909
|
+
* Decide one step's route and reasoning effort, then write it into the step's
|
|
910
|
+
* selection.
|
|
911
|
+
*
|
|
912
|
+
* ## Why the write happens here (g-056)
|
|
913
|
+
*
|
|
914
|
+
* `dsh-agent-loop` reads `const selection = this.model.current()` before it
|
|
915
|
+
* dispatches `agent/pre-step`, keeps that very object as the step's fixed
|
|
916
|
+
* selection, and afterwards rejects a request that proposes anything else:
|
|
917
|
+
*
|
|
918
|
+
* agent/request proposed reasoning effort "high" after step preparation
|
|
919
|
+
* fixed "reflex"; use agent.model.select() before step preparation
|
|
920
|
+
*
|
|
921
|
+
* `payload.modelSelection` *is* that object — `preStep` hands the local
|
|
922
|
+
* `selection` straight into the waterfall payload, so mutating the payload
|
|
923
|
+
* mutates what the guards compare against. `AgentModel.current()` already
|
|
924
|
+
* returned a fresh mutable copy (`{ ...recorded }` / a new object from
|
|
925
|
+
* `toSelection`), which is why the object is writable at all.
|
|
926
|
+
*
|
|
927
|
+
* The `Readonly<ModelSelection>` in the contract is a compile-time marker with
|
|
928
|
+
* no runtime freeze attached, and DSH 0.2.2 offers no other write path before
|
|
929
|
+
* step preparation: `agent.model.select()` appends a `model/selection` event
|
|
930
|
+
* that only the *next* `current()` reads, and `current()` has already been
|
|
931
|
+
* called. So this is the only seam, and the cast below is its documentation.
|
|
932
|
+
*
|
|
933
|
+
* Every failure mode degrades to "selection unchanged": a throwing planner, a
|
|
934
|
+
* frozen payload (the write raises `TypeError`, caught here), or a decision
|
|
935
|
+
* that outran {@link STEP_DECISION_TIMEOUT_MS}. An unchanged `reflex` marker
|
|
936
|
+
* is then stripped by the virtual provider's own `resolveCallConfig`, so the
|
|
937
|
+
* request lands on the adapter default instead of an unsupported effort.
|
|
938
|
+
*
|
|
939
|
+
* `next()` is called *outside* the `try` on purpose: the rest of the waterfall
|
|
940
|
+
* decides whether the step is admitted (`enter`/`reject`) and with which
|
|
941
|
+
* messages, and swallowing its rejection would turn a rejected step into a
|
|
942
|
+
* silently admitted one.
|
|
943
|
+
*/
|
|
564
944
|
disposers.push(ctx.on('agent/pre-step', async (payload, next) => {
|
|
945
|
+
// Outside the try: a rejection here must reach the loop unchanged.
|
|
946
|
+
const decision = await next();
|
|
565
947
|
try {
|
|
566
948
|
// Cheap liveness: the host that commits config-only saves leaves the
|
|
567
949
|
// resolved config stale until someone asks. The check is a handful of
|
|
568
950
|
// identity comparisons and parses nothing unless the loader committed.
|
|
569
951
|
options.refreshConfig?.();
|
|
570
952
|
ensureVirtualSwitchFilter(payload.agent, router, agentFilterDisposers, agentFilters);
|
|
571
|
-
rememberStep(facts, payload.agent, captureTask(payload.messages, readTaskBudget()), measureMessages(payload.messages));
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
//
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
const rawConfig = await next();
|
|
580
|
-
if (!rawConfig || payload.signal?.aborted)
|
|
581
|
-
return rawConfig;
|
|
582
|
-
const config = resolveAdaptiveIntent(ctx, payload.agent, rawConfig, (provider) => router.owns(provider));
|
|
583
|
-
const remembered = recallFacts(facts, payload.agent);
|
|
584
|
-
const requested = config.reasoningEffort;
|
|
585
|
-
const task = remembered?.task;
|
|
586
|
-
// With `effort.enabled` off this layer manages no effort at all: it still
|
|
587
|
-
// routes, but the only edit it is allowed to make to `reasoningEffort` is
|
|
588
|
-
// removing its own marker. That keeps a concrete level byte-identical on
|
|
589
|
-
// its way to the adapter, and it means no decision, no audit entry and no
|
|
590
|
-
// status can be produced for a deployment that switched the layer off.
|
|
591
|
-
const effortOff = options.effort.enabled === false;
|
|
592
|
-
// Reflex only automatically adapts reasoning effort when the effort was
|
|
593
|
-
// explicitly chosen as reflex/auto, or when an unpinned request names the
|
|
594
|
-
// virtual provider (which defaults to adaptive effort). If the user selected
|
|
595
|
-
// High, Low, Medium, Off, or Provider Default, reflex does not touch effort.
|
|
596
|
-
const isAdaptive = !effortOff &&
|
|
597
|
-
(isAdaptiveMarker(requested) ||
|
|
598
|
-
(requested === undefined && router.owns(config.provider) && isAdaptiveMarker(options.effort.setting)));
|
|
599
|
-
// Same-turn follow-up steps (step > 1 without new user text) reuse the turn's
|
|
600
|
-
// decision: no second System 1 call, no second effort event, 0ms latency.
|
|
601
|
-
const isFollowupStep = (payload.step ?? 1) > 1 &&
|
|
602
|
-
remembered?.decided !== undefined &&
|
|
603
|
-
remembered.decided.turn === payload.turn &&
|
|
604
|
-
remembered.stepHasText !== true &&
|
|
605
|
-
// A config-only save may replace the policy mid-turn; a decision taken
|
|
606
|
-
// under the previous policy is not this turn's answer any more.
|
|
607
|
-
remembered.decided.generation === options.effort.generation;
|
|
608
|
-
if (isAdaptive && isFollowupStep && remembered?.decided) {
|
|
609
|
-
const reuse = remembered.decided;
|
|
610
|
-
const routedReuse = routes && router.owns(config.provider)
|
|
611
|
-
? { ...config, provider: reuse.provider, model: reuse.model }
|
|
612
|
-
: config;
|
|
613
|
-
const effort = reuse.effort;
|
|
614
|
-
if (effort === requested && routedReuse === config)
|
|
615
|
-
return config;
|
|
616
|
-
const rewritten = { ...routedReuse };
|
|
617
|
-
if (effort === undefined)
|
|
618
|
-
delete rewritten.reasoningEffort;
|
|
619
|
-
else
|
|
620
|
-
rewritten.reasoningEffort = effort;
|
|
621
|
-
return rewritten;
|
|
622
|
-
}
|
|
623
|
-
let routed = config;
|
|
624
|
-
let switched;
|
|
625
|
-
let heldForCache;
|
|
626
|
-
if (routes && router.owns(config.provider)) {
|
|
627
|
-
const decision = await router.routeModel({
|
|
628
|
-
provider: config.provider,
|
|
629
|
-
model: config.model,
|
|
630
|
-
task,
|
|
631
|
-
...describeAgent(payload.agent),
|
|
953
|
+
rememberStep(facts, payload.agent, captureTask(payload.messages, readTaskBudget()), measureMessages(payload.messages), payload.turn);
|
|
954
|
+
// The loop has already read `model.current()` for this step; reading our
|
|
955
|
+
// own attributes synchronously here (not through a composable) is what
|
|
956
|
+
// makes the attempt safely able to outlive its budget.
|
|
957
|
+
const selection = payload.modelSelection;
|
|
958
|
+
const attempt = startDecision(() => planStep({
|
|
959
|
+
ctx,
|
|
960
|
+
agent: payload.agent,
|
|
632
961
|
turn: payload.turn,
|
|
633
962
|
step: payload.step,
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
// Transparent pass-through: the request keeps the caller's own level
|
|
648
|
-
// (never aligned, never snapped onto the routed model's vocabulary),
|
|
649
|
-
// and the plugin's marker is deleted instead of being translated — the
|
|
650
|
-
// adapter then applies the provider default, which is the only safe
|
|
651
|
-
// reading of "reflex" once the adaptive layer is switched off.
|
|
652
|
-
const kept = isAdaptiveMarker(requested) ? undefined : requested;
|
|
653
|
-
if (routed === config && kept === requested)
|
|
654
|
-
return config;
|
|
655
|
-
const rewritten = { ...routed };
|
|
656
|
-
if (kept === undefined)
|
|
657
|
-
delete rewritten.reasoningEffort;
|
|
658
|
-
else
|
|
659
|
-
rewritten.reasoningEffort = kept;
|
|
660
|
-
return rewritten;
|
|
963
|
+
signal: payload.signal,
|
|
964
|
+
selection,
|
|
965
|
+
router,
|
|
966
|
+
effort: options.effort,
|
|
967
|
+
routes,
|
|
968
|
+
facts,
|
|
969
|
+
probe
|
|
970
|
+
}));
|
|
971
|
+
const budget = stepDecisionBudget();
|
|
972
|
+
const settled = await withBudget(attempt, budget);
|
|
973
|
+
if (settled === undefined) {
|
|
974
|
+
warn(`dsh-reflex: the step decision for "${agentKey(payload.agent) ?? 'agent'}" did not finish in ${budget}ms; the step keeps the selection it fixed`);
|
|
975
|
+
return decision;
|
|
661
976
|
}
|
|
662
|
-
//
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
if (aligned === requested)
|
|
666
|
-
return config;
|
|
667
|
-
const rewritten = { ...routed };
|
|
668
|
-
if (aligned !== undefined)
|
|
669
|
-
rewritten.reasoningEffort = aligned;
|
|
670
|
-
else
|
|
671
|
-
delete rewritten.reasoningEffort;
|
|
672
|
-
return rewritten;
|
|
673
|
-
}
|
|
674
|
-
if (routed !== config) {
|
|
675
|
-
const aligned = requested ? await alignReasoningEffort(requested, { provider: routed.provider, model: routed.model }, probe) : undefined;
|
|
676
|
-
const rewritten = { ...routed };
|
|
677
|
-
if (aligned !== undefined)
|
|
678
|
-
rewritten.reasoningEffort = aligned;
|
|
679
|
-
else
|
|
680
|
-
delete rewritten.reasoningEffort;
|
|
681
|
-
return rewritten;
|
|
682
|
-
}
|
|
683
|
-
return config;
|
|
977
|
+
// A read-only payload (frozen by an embedder, or a future Host that
|
|
978
|
+
// freezes it) rejects the writes below; that is the same safe outcome.
|
|
979
|
+
applyPlan(selection, settled);
|
|
684
980
|
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
task,
|
|
690
|
-
agent: describeAgent(payload.agent),
|
|
691
|
-
turn: payload.turn,
|
|
692
|
-
step: payload.step,
|
|
693
|
-
requestedProvider: config.provider,
|
|
694
|
-
requestedModel: config.model,
|
|
695
|
-
...(switched === undefined ? {} : { switched }),
|
|
696
|
-
...(heldForCache === undefined ? {} : { heldForCache })
|
|
697
|
-
});
|
|
698
|
-
if (resolution.status) {
|
|
699
|
-
reportEffort(ctx, payload.agent, resolution.status);
|
|
700
|
-
rememberDecision(facts, payload.agent, payload.turn, routed, resolution, options.effort.generation);
|
|
701
|
-
}
|
|
702
|
-
if (!resolution.changed && routed === config)
|
|
703
|
-
return config;
|
|
704
|
-
const rewritten = { ...routed };
|
|
705
|
-
// Only requests that carried an effort — or whose effort this layer just
|
|
706
|
-
// decided — get one back. A cleared effort deletes the key instead of
|
|
707
|
-
// leaving an `undefined` one behind: a request that never asked for an
|
|
708
|
-
// effort must look exactly as it did before the interception.
|
|
709
|
-
const effort = resolution.changed ? resolution.effort : requested;
|
|
710
|
-
if (config.reasoningEffort !== undefined || resolution.changed) {
|
|
711
|
-
if (effort === undefined)
|
|
712
|
-
delete rewritten.reasoningEffort;
|
|
713
|
-
else
|
|
714
|
-
rewritten.reasoningEffort = effort;
|
|
981
|
+
catch {
|
|
982
|
+
// Decision failure never vetoes a step: the selection keeps exactly what
|
|
983
|
+
// the loop fixed, and the virtual provider's `resolveCallConfig` strips
|
|
984
|
+
// the marker if one survives, so the request lands on the model default.
|
|
715
985
|
}
|
|
716
|
-
return
|
|
986
|
+
return decision;
|
|
717
987
|
}, { prepend: true }));
|
|
988
|
+
// Read-only by contract (see the module docstring): `await next()` and hand
|
|
989
|
+
// the configuration back exactly as produced. DSH 0.2.2 throws when this
|
|
990
|
+
// waterfall proposes a different provider, model, or reasoning effort than the
|
|
991
|
+
// step's selection, and a listener that can only pass through can never do so.
|
|
992
|
+
disposers.push(ctx.on('agent/request', async (_payload, next) => next(), { prepend: true }));
|
|
718
993
|
const dispose = () => {
|
|
719
994
|
if (disposed)
|
|
720
995
|
return;
|
|
@@ -785,19 +1060,21 @@ function captureTask(messages, budget) {
|
|
|
785
1060
|
* @param agent - the agent proposing the step.
|
|
786
1061
|
* @param text - the captured task text, when the step carried any.
|
|
787
1062
|
* @param chars - characters of step input observed.
|
|
1063
|
+
* @param turn - the turn the step belongs to.
|
|
788
1064
|
*/
|
|
789
|
-
function rememberStep(facts, agent, text, chars) {
|
|
1065
|
+
function rememberStep(facts, agent, text, chars, turn) {
|
|
790
1066
|
const key = agentKey(agent);
|
|
791
1067
|
if (key === undefined)
|
|
792
1068
|
return;
|
|
793
1069
|
const previous = facts.get(key);
|
|
1070
|
+
const turns = rememberTurn(previous?.turns ?? [], text, turn);
|
|
794
1071
|
const next = {
|
|
795
1072
|
...(text ?? previous?.task ? { task: text ?? previous?.task } : {}),
|
|
796
1073
|
...(previous?.currentModel ? { currentModel: previous.currentModel } : {}),
|
|
797
1074
|
contextChars: (previous?.contextChars ?? 0) + Math.max(0, Math.round(chars)),
|
|
798
|
-
stepHasText: text !== undefined,
|
|
799
1075
|
...(previous?.decided ? { decided: previous.decided } : {}),
|
|
800
|
-
...(previous?.lastAdaptiveEffort ? { lastAdaptiveEffort: previous.lastAdaptiveEffort } : {})
|
|
1076
|
+
...(previous?.lastAdaptiveEffort ? { lastAdaptiveEffort: previous.lastAdaptiveEffort } : {}),
|
|
1077
|
+
...(turns.length === 0 ? {} : { turns })
|
|
801
1078
|
};
|
|
802
1079
|
facts.delete(key);
|
|
803
1080
|
facts.set(key, next);
|
|
@@ -808,6 +1085,40 @@ function rememberStep(facts, agent, text, chars) {
|
|
|
808
1085
|
facts.delete(oldest.value);
|
|
809
1086
|
}
|
|
810
1087
|
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Append one observed turn text, one entry per turn, bounded by the effort
|
|
1090
|
+
* layer's window.
|
|
1091
|
+
*
|
|
1092
|
+
* A second step of the *same* turn replaces that turn's entry instead of adding
|
|
1093
|
+
* another: the window is defined in turns, so a step count must not be able to
|
|
1094
|
+
* push an earlier turn out of it. Consecutive replies of the same text are folded
|
|
1095
|
+
* too — "继续" said three times is one piece of context, not three that grow
|
|
1096
|
+
* with every turn.
|
|
1097
|
+
*
|
|
1098
|
+
* @param turns - the turns kept so far, oldest first.
|
|
1099
|
+
* @param text - the text this step carried; a step without text adds nothing.
|
|
1100
|
+
* @param turn - the turn the step belongs to; `undefined` is treated as a new turn.
|
|
1101
|
+
* @returns the bounded turn list, oldest first.
|
|
1102
|
+
*/
|
|
1103
|
+
function rememberTurn(turns, text, turn) {
|
|
1104
|
+
if (text === undefined)
|
|
1105
|
+
return [...turns];
|
|
1106
|
+
const last = turns.at(-1);
|
|
1107
|
+
// Without a turn number every step is its own turn: the pre-step hook is the
|
|
1108
|
+
// only place this is read, and guessing an increment would merge a multi-step
|
|
1109
|
+
// turn into one entry — which is harmless — while a wrong merge across real
|
|
1110
|
+
// turns would drop history. Separate entries is the safe reading.
|
|
1111
|
+
const entry = { turn: turn ?? (last === undefined ? 0 : last.turn + 1), text };
|
|
1112
|
+
const kept = turn !== undefined && last !== undefined && last.turn === turn ? [...turns.slice(0, -1), entry] : [...turns, entry];
|
|
1113
|
+
// The same text repeated consecutively is one context, not an ever-growing one.
|
|
1114
|
+
const deduped = [];
|
|
1115
|
+
for (const item of kept) {
|
|
1116
|
+
if (deduped.at(-1)?.text === item.text)
|
|
1117
|
+
continue;
|
|
1118
|
+
deduped.push(item);
|
|
1119
|
+
}
|
|
1120
|
+
return deduped.slice(-EFFORT_CONTEXT_TURNS);
|
|
1121
|
+
}
|
|
811
1122
|
/**
|
|
812
1123
|
* Remember one turn's decision so its later steps cost nothing.
|
|
813
1124
|
*
|
|
@@ -823,7 +1134,7 @@ function rememberStep(facts, agent, text, chars) {
|
|
|
823
1134
|
* @param resolution - the effort resolution, carrying the status when decided.
|
|
824
1135
|
* @param generation - effort policy version the decision was taken under.
|
|
825
1136
|
*/
|
|
826
|
-
function rememberDecision(facts, agent, turn, routed, resolution, generation) {
|
|
1137
|
+
function rememberDecision(facts, agent, turn, routed, resolution, generation, virtualModel) {
|
|
827
1138
|
const key = agentKey(agent);
|
|
828
1139
|
if (key === undefined)
|
|
829
1140
|
return;
|
|
@@ -832,21 +1143,28 @@ function rememberDecision(facts, agent, turn, routed, resolution, generation) {
|
|
|
832
1143
|
turn: turn ?? 0,
|
|
833
1144
|
provider: routed.provider,
|
|
834
1145
|
model: routed.model,
|
|
1146
|
+
...(virtualModel === undefined ? {} : { virtualModel }),
|
|
835
1147
|
...(resolution.effort === undefined ? {} : { effort: resolution.effort }),
|
|
836
1148
|
...(status === undefined ? {} : { status }),
|
|
837
1149
|
generation
|
|
838
1150
|
};
|
|
839
1151
|
const previous = facts.get(key);
|
|
1152
|
+
// g-063: a later step of the *same* turn replays this decision instead of
|
|
1153
|
+
// asking again, so the previous level the next decision is given must be the
|
|
1154
|
+
// level this turn settled on, not the one from before it. Otherwise a five-step
|
|
1155
|
+
// turn hands the turn after it a stale prior.
|
|
1156
|
+
const settledEffort = resolution.decision ? resolution.effort : (previous?.decided?.effort ?? previous?.lastAdaptiveEffort);
|
|
840
1157
|
facts.delete(key);
|
|
841
1158
|
facts.set(key, {
|
|
842
1159
|
...(previous?.task ? { task: previous.task } : {}),
|
|
843
1160
|
...(previous?.currentModel ? { currentModel: previous.currentModel } : {}),
|
|
844
1161
|
contextChars: previous?.contextChars ?? 0,
|
|
845
|
-
...(previous?.stepHasText === undefined ? {} : { stepHasText: previous.stepHasText }),
|
|
846
1162
|
decided: decision,
|
|
847
1163
|
// An echo is only recognisable when this layer, not the user, chose the
|
|
848
|
-
// effort — so the marker is recorded for adaptive decisions alone
|
|
849
|
-
|
|
1164
|
+
// effort — so the marker is recorded for adaptive decisions alone — and
|
|
1165
|
+
// since g-063 it is also the previous level the next decision is told about.
|
|
1166
|
+
...(settledEffort === undefined ? {} : { lastAdaptiveEffort: settledEffort }),
|
|
1167
|
+
...(previous?.turns?.length ? { turns: previous.turns } : {})
|
|
850
1168
|
});
|
|
851
1169
|
}
|
|
852
1170
|
/**
|
|
@@ -924,9 +1242,9 @@ function rememberTarget(facts, agent, target) {
|
|
|
924
1242
|
...(previous?.task ? { task: previous.task } : {}),
|
|
925
1243
|
currentModel: { provider: target.provider, model: target.model },
|
|
926
1244
|
contextChars: previous?.contextChars ?? 0,
|
|
927
|
-
...(previous?.stepHasText === undefined ? {} : { stepHasText: previous.stepHasText }),
|
|
928
1245
|
...(previous?.decided ? { decided: previous.decided } : {}),
|
|
929
|
-
...(previous?.lastAdaptiveEffort ? { lastAdaptiveEffort: previous.lastAdaptiveEffort } : {})
|
|
1246
|
+
...(previous?.lastAdaptiveEffort ? { lastAdaptiveEffort: previous.lastAdaptiveEffort } : {}),
|
|
1247
|
+
...(previous?.turns?.length ? { turns: previous.turns } : {})
|
|
930
1248
|
});
|
|
931
1249
|
}
|
|
932
1250
|
/**
|