@kingsoftcloud/agentkit-harness-core 0.0.16 → 0.0.18
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/dist/configuration.d.mts +2 -0
- package/dist/configuration.mjs +20 -3
- package/dist/context-contributions.d.mts +2 -0
- package/dist/context-contributions.mjs +57 -2
- package/dist/context-planning.mjs +7 -4
- package/dist/contracts.d.mts +24 -2
- package/dist/engine-preset.d.mts +48 -0
- package/dist/engine-preset.mjs +75 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/kernel.d.mts +2 -1
- package/dist/kernel.mjs +50 -19
- package/dist/machine.d.mts +3 -1
- package/dist/machine.mjs +57 -7
- package/dist/model-stream.d.mts +1 -1
- package/dist/model-stream.mjs +15 -11
- package/dist/output-recovery.d.mts +2 -0
- package/dist/output-recovery.mjs +11 -1
- package/dist/recovery.mjs +2 -0
- package/dist/response-continuation.mjs +2 -1
- package/dist/transitions.mjs +2 -1
- package/package.json +1 -1
- package/src/configuration.mts +25 -3
- package/src/context-contributions.mts +53 -2
- package/src/context-planning.mts +7 -4
- package/src/contracts.mts +21 -2
- package/src/engine-preset.mts +110 -0
- package/src/index.mts +1 -0
- package/src/kernel.mts +52 -18
- package/src/machine.mts +55 -7
- package/src/model-stream.mts +11 -8
- package/src/output-recovery.mts +13 -1
- package/src/recovery.mts +1 -0
- package/src/response-continuation.mts +2 -1
- package/src/transitions.mts +2 -1
package/src/machine.mts
CHANGED
|
@@ -16,7 +16,7 @@ import { rememberCommand } from './background-runtime.mjs';
|
|
|
16
16
|
import { admittedToolBudget, outstandingChildCalls, reduceChildBudget } from './child-budget.mjs';
|
|
17
17
|
import { prepareChildLifecycle, unsettledChildren } from './child-runtime.mjs';
|
|
18
18
|
import { toolResourceUsage, toolUsageIncomplete } from './tool-resource-usage.mjs';
|
|
19
|
-
import { completeContinuation, outputRecoveryMode, restartContinuation } from './output-recovery.mjs';
|
|
19
|
+
import { completeContinuation, outputRecoveryMode, reasoningStall, reasoningStallRetryTokens, restartContinuation } from './output-recovery.mjs';
|
|
20
20
|
import { responseBoundaryRecovery, responseContinuationPending, responseScopedOutputRestarts } from './response-continuation.mjs';
|
|
21
21
|
import { advanceDiagnosticRecovery, advanceObservedProgress, advanceToolErrorStreak, guardedToolErrorAction,
|
|
22
22
|
refuseUndiagnosedToolAction, reserveDiagnosticTrial, toolErrorRecoveryBoundary, toolErrorRecoveryEnabled,
|
|
@@ -66,8 +66,18 @@ export function finalizationOnly(snapshot: RunSnapshot, config: RunConfiguration
|
|
|
66
66
|
const calls = modelRoom(snapshot, config);
|
|
67
67
|
return calls <= config.limits.finalizationReserveModelCalls;
|
|
68
68
|
}
|
|
69
|
+
export function initialPlanPending(snapshot: RunSnapshot, config: RunConfiguration): boolean {
|
|
70
|
+
return config.initialPlanPolicy === 'plan-act-observe/v1' && !snapshot.initialPlanCompleted;
|
|
71
|
+
}
|
|
72
|
+
export function initialPlanMessages(snapshot: RunSnapshot, config: RunConfiguration, messages: Message[]): Message[] {
|
|
73
|
+
if (!initialPlanPending(snapshot, config)) return messages;
|
|
74
|
+
const instruction = responseContinuationPending(snapshot, config)
|
|
75
|
+
? 'Continue the unfinished execution plan. Return only its missing text suffix.'
|
|
76
|
+
: 'First give a concise step-by-step execution plan for the current user request. Tools are unavailable in this step. The next step will execute the plan.';
|
|
77
|
+
return [{ role: 'system', content: instruction }, ...messages];
|
|
78
|
+
}
|
|
69
79
|
export function advertisedTools(snapshot: RunSnapshot, config: RunConfiguration, policies = new NativePolicyRegistry()) {
|
|
70
|
-
if (responseContinuationPending(snapshot, config)) return [];
|
|
80
|
+
if (initialPlanPending(snapshot, config) || responseContinuationPending(snapshot, config)) return [];
|
|
71
81
|
if (policies.forRun(config).review.reviewPending(snapshot)) return policies.forRun(config).review.deliveryReviewTools(snapshot, config);
|
|
72
82
|
const submission = finalSubmissionTool(config);
|
|
73
83
|
const tools = finalizationOnly(snapshot, config) || toolRoom(snapshot, config) <= 0 ? []
|
|
@@ -80,7 +90,7 @@ export function advertisedTools(snapshot: RunSnapshot, config: RunConfiguration,
|
|
|
80
90
|
export interface PlanCapabilities { context: boolean; workspace: boolean; verification: boolean; input?: boolean }
|
|
81
91
|
/** Stable declaration is distinct from the per-turn execution admission above. */
|
|
82
92
|
export function modelToolCatalog(snapshot: RunSnapshot, config: RunConfiguration, policies = new NativePolicyRegistry()) {
|
|
83
|
-
if (responseContinuationPending(snapshot, config)) return [];
|
|
93
|
+
if (initialPlanPending(snapshot, config) || responseContinuationPending(snapshot, config)) return [];
|
|
84
94
|
if ((config.profile.toolCatalogPolicy !== 'stable-task/v1' && config.profile.providerToolSearchPolicy !== 'anthropic-deferred/v1')
|
|
85
95
|
|| policies.forRun(config).review.reviewPending(snapshot)) return discoveryModelTools(snapshot, config, advertisedTools(snapshot, config, policies));
|
|
86
96
|
const submission = finalSubmissionTool(config);
|
|
@@ -216,6 +226,8 @@ export function plan(snapshot: RunSnapshot, config: RunConfiguration, capabiliti
|
|
|
216
226
|
if (candidate) return transition(snapshot, { phase: 'verifying', candidate, verification: null, preparedContext: null },
|
|
217
227
|
[{ kind: 'verification.checkpoint_started', payload: { workspaceRevision: snapshot.workspaceRevision } }]);
|
|
218
228
|
}
|
|
229
|
+
if (initialPlanPending(snapshot, config)) return prepareEffect(snapshot, 'model', 'requesting_model',
|
|
230
|
+
{ purpose: 'initial_plan', finalizationOnly: false });
|
|
219
231
|
return prepareEffect(snapshot, 'model', 'requesting_model', policies.forRun(config).review.reviewPending(snapshot)
|
|
220
232
|
? { purpose: 'delivery_review', finalizationOnly: false } : { finalizationOnly: finalizationOnly(snapshot, config) },
|
|
221
233
|
policies.forRun(config).review.reviewPending(snapshot) ? {} : policies.forRun(config).providerContext.captureAnthropicContext(snapshot, config, modelToolCatalog(snapshot, config, policies), digest));
|
|
@@ -243,11 +255,11 @@ export function plan(snapshot: RunSnapshot, config: RunConfiguration, capabiliti
|
|
|
243
255
|
[{ kind: 'tools.budget_exhausted', payload: {} }]);
|
|
244
256
|
}
|
|
245
257
|
let calls: ToolCall[] = [first];
|
|
246
|
-
if (definition.readOnly && definition.concurrentSafe && !definition.delegatedReadOnly && !guardedToolErrorAction(snapshot, first, digest)) {
|
|
258
|
+
if (definition.readOnly && definition.concurrentSafe && !definition.approvalPolicy && !definition.delegatedReadOnly && !guardedToolErrorAction(snapshot, first, digest)) {
|
|
247
259
|
calls = [];
|
|
248
260
|
for (const call of snapshot.pendingTools.slice(0, Math.min(config.limits.maxParallelTools, toolRoom(snapshot, config)))) {
|
|
249
261
|
const tool = config.tools.find(item => item.name === call.name);
|
|
250
|
-
if (!tool?.readOnly || !tool.concurrentSafe || tool.delegatedReadOnly || tool.observationRole !== 'none') break;
|
|
262
|
+
if (!tool?.readOnly || !tool.concurrentSafe || tool.approvalPolicy || tool.delegatedReadOnly || tool.observationRole !== 'none') break;
|
|
251
263
|
if (guardedToolErrorAction(snapshot, call, digest)) break;
|
|
252
264
|
calls.push(call);
|
|
253
265
|
}
|
|
@@ -338,12 +350,22 @@ export function reduceModel(snapshot: RunSnapshot, input: ModelResult, config: R
|
|
|
338
350
|
// errors, malformed calls and context-overflow retries keep this input debt.
|
|
339
351
|
if (snapshot.hookState && ['tool_calls', 'final_candidate', 'output_limit'].includes(result.kind)
|
|
340
352
|
&& (result.toolCalls.length > 0 || result.text.trim())) base.hookState = { ...currentHookState(snapshot), feedback: [] };
|
|
353
|
+
const stalled = config.outputRecoveryPolicy === 'response-boundary/v4' && reasoningStall(result);
|
|
354
|
+
// Keep the narrowed retry through transport errors and context preparation.
|
|
355
|
+
// A sealed cutoff with text or an unfinished tool call uses the ordinary budget again.
|
|
356
|
+
if (snapshot.reasoningStallRetry && (result.kind === 'tool_calls' || result.kind === 'final_candidate'
|
|
357
|
+
|| result.kind === 'output_limit' && !stalled)) base.reasoningStallRetry = false;
|
|
341
358
|
const message: Message = { role: 'assistant', content: result.text,
|
|
342
359
|
...(config.contextPolicy?.pruneIdleThresholdMinutes !== undefined && Number.isSafeInteger(now) && now >= 0
|
|
343
360
|
&& ['tool_calls', 'final_candidate', 'output_limit'].includes(result.kind) ? { completedAtMs: now } : {}),
|
|
344
361
|
...(replay ? { providerReplay: replay, ...(result.replayToolIds ? { replayToolIds: result.replayToolIds } : {}) } : {}) };
|
|
345
362
|
switch (result.kind) {
|
|
346
363
|
case 'tool_calls':
|
|
364
|
+
if (initialPlanPending(snapshot, config)) {
|
|
365
|
+
const errors = snapshot.modelErrors + 1, exhausted = errors > config.profile.maxRetries;
|
|
366
|
+
return { ...base, phase: exhausted ? 'finalizing' : 'preparing_context', pendingTools: [],
|
|
367
|
+
modelErrors: errors, outcome: exhausted ? 'blocked' : null, reason: exhausted ? 'initial_plan_invalid' : null };
|
|
368
|
+
}
|
|
347
369
|
if (responseContinuationPending(snapshot, config)) {
|
|
348
370
|
const errors = snapshot.modelErrors + 1, exhausted = errors > config.profile.maxRetries;
|
|
349
371
|
return { ...base, pendingTools: [], modelErrors: errors, phase: exhausted ? 'finalizing' : 'preparing_context',
|
|
@@ -398,6 +420,17 @@ export function reduceModel(snapshot: RunSnapshot, input: ModelResult, config: R
|
|
|
398
420
|
continuations: 0, modelErrors: 0,
|
|
399
421
|
...(responseScopedOutputRestarts(config) ? { outputRestarts: 0 } : {}) };
|
|
400
422
|
case 'final_candidate': {
|
|
423
|
+
if (initialPlanPending(snapshot, config)) {
|
|
424
|
+
const text = completeContinuation(snapshot.messages, result.text, digest).trim();
|
|
425
|
+
if (!text) {
|
|
426
|
+
const errors = snapshot.modelErrors + 1, exhausted = errors > config.profile.maxRetries;
|
|
427
|
+
return { ...base, phase: exhausted ? 'finalizing' : 'preparing_context', modelErrors: errors,
|
|
428
|
+
outcome: exhausted ? 'failed' : null, reason: exhausted ? 'initial_plan_empty' : null };
|
|
429
|
+
}
|
|
430
|
+
return { ...base, phase: 'preparing_context', initialPlanCompleted: true, modelErrors: 0,
|
|
431
|
+
continuations: 0, outputRestarts: 0,
|
|
432
|
+
messages: [...snapshot.messages, { role: 'assistant', kind: 'initial_plan', content: text }] };
|
|
433
|
+
}
|
|
401
434
|
if (config.contract.finalSubmissionSchema) {
|
|
402
435
|
const errors = snapshot.modelErrors + 1;
|
|
403
436
|
const exhausted = errors > config.profile.maxRetries || modelRoom(snapshot, config) <= 0;
|
|
@@ -414,6 +447,10 @@ export function reduceModel(snapshot: RunSnapshot, input: ModelResult, config: R
|
|
|
414
447
|
}
|
|
415
448
|
case 'output_limit': {
|
|
416
449
|
const resumable = config.outputRestartPolicy === 'per-response/v2';
|
|
450
|
+
const boundedRecovery = config.outputRecoveryPolicy === 'response-boundary/v4' || config.outputRecoveryPolicy === 'response-boundary/v5';
|
|
451
|
+
// Honor stricter caller limits. The experimental retry spends the same
|
|
452
|
+
// continuation allowance as the ordinary retry.
|
|
453
|
+
const continuationLimit = boundedRecovery ? Math.min(config.profile.maxContinuations, 2) : config.profile.maxContinuations;
|
|
417
454
|
const continuation = () => {
|
|
418
455
|
const mode = result.text ? outputRecoveryMode({ ...snapshot, ...base }, config,
|
|
419
456
|
advertisedTools({ ...snapshot, ...base }, config, policies).length > 0, result) : 'empty_output';
|
|
@@ -435,11 +472,22 @@ export function reduceModel(snapshot: RunSnapshot, input: ModelResult, config: R
|
|
|
435
472
|
? { phase: 'paused', resumePhase: 'preparing_context', outcome: null, reason: 'output_budget_exhausted',
|
|
436
473
|
messages: result.text ? continuation().messages : snapshot.messages }
|
|
437
474
|
: { phase: 'finalizing', outcome: 'unverified', reason: 'continuation_budget_exhausted' };
|
|
438
|
-
if (
|
|
475
|
+
if (stalled) {
|
|
476
|
+
if (snapshot.reasoningStallRetry || snapshot.continuations >= continuationLimit
|
|
477
|
+
|| reasoningStallRetryTokens(config.profile.maxOutputTokens) >= config.profile.maxOutputTokens)
|
|
478
|
+
return { ...base, ...exhausted(), messages: snapshot.messages };
|
|
479
|
+
const suffix = responseContinuationPending(snapshot, config);
|
|
480
|
+
return { ...base, phase: 'preparing_context', reasoningStallRetry: true, continuations: snapshot.continuations + 1,
|
|
481
|
+
messages: [...snapshot.messages, { role: 'user',
|
|
482
|
+
kind: suffix ? 'response_continuation_feedback' : 'output_limit_reasoning_observation',
|
|
483
|
+
content: suffix ? 'Continue with only the missing text suffix now.'
|
|
484
|
+
: 'The previous attempt spent its output budget on reasoning without taking action. Take the next useful tool action now, or answer directly if the task is complete.' }] };
|
|
485
|
+
}
|
|
486
|
+
if (snapshot.continuations >= continuationLimit) {
|
|
439
487
|
// Committed answer text stays a suffix contract even when the first
|
|
440
488
|
// response has no remaining continuation allowance. It is not a tool
|
|
441
489
|
// fragment that may be discarded for an output restart.
|
|
442
|
-
if (responseContinuationPending(snapshot, config) || resumable && continuation().mode === 'suffix')
|
|
490
|
+
if (boundedRecovery || responseContinuationPending(snapshot, config) || resumable && continuation().mode === 'suffix')
|
|
443
491
|
return { ...base, ...exhausted() };
|
|
444
492
|
if (snapshot.outputRestarts < config.limits.maxOutputRestarts) {
|
|
445
493
|
try { return { ...base, ...restartContinuation(snapshot, config, result.text, digest), phase: 'preparing_context',
|
package/src/model-stream.mts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { ModelStreamEvent, ModelStreamRecord } from './contracts.mjs';
|
|
2
2
|
import { canonical, copy } from './values.mjs';
|
|
3
3
|
|
|
4
|
-
/**
|
|
4
|
+
/** Publish live output and retain a bounded public prefix for history. Neither supplies execution authority. */
|
|
5
5
|
export function modelStreamCapture(maxBytes: number, now: () => number,
|
|
6
6
|
publish: (event: ModelStreamEvent, record: ModelStreamRecord) => void) {
|
|
7
7
|
const records: ModelStreamRecord[] = [];
|
|
8
|
-
let bytes = 2, truncated = false, discarded = 0, closed = false;
|
|
8
|
+
let bytes = 2, lastTime = 0, truncated = false, discarded = 0, closed = false;
|
|
9
9
|
return {
|
|
10
10
|
emit(value: ModelStreamEvent) {
|
|
11
|
-
if (closed
|
|
11
|
+
if (closed) return;
|
|
12
12
|
if (!value || !['text', 'thinking', 'tool_delta'].includes(value.kind) || typeof value.text !== 'string') {
|
|
13
13
|
discarded++; return;
|
|
14
14
|
}
|
|
@@ -24,11 +24,14 @@ export function modelStreamCapture(maxBytes: number, now: () => number,
|
|
|
24
24
|
...(typeof value.metadata?.name === 'string' ? { name: value.metadata.name } : {}) };
|
|
25
25
|
}
|
|
26
26
|
const clock = now();
|
|
27
|
-
|
|
28
|
-
const record = { time, event };
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
lastTime = Math.max(lastTime, Number.isSafeInteger(clock) && clock >= 0 ? clock : 0);
|
|
28
|
+
const record = { time: lastTime, event };
|
|
29
|
+
if (!truncated) {
|
|
30
|
+
const size = new TextEncoder().encode(canonical(record)).length + (records.length ? 1 : 0);
|
|
31
|
+
if (bytes + size > maxBytes) truncated = true;
|
|
32
|
+
else { bytes += size; records.push(record); }
|
|
33
|
+
}
|
|
34
|
+
if (maxBytes === 0) return;
|
|
32
35
|
// Display observers cannot veto or mutate the model result or receipt.
|
|
33
36
|
try { publish(copy(event), copy(record)); } catch { /* non-authoritative observer */ }
|
|
34
37
|
},
|
package/src/output-recovery.mts
CHANGED
|
@@ -5,12 +5,24 @@ import { contextMessageRef } from './context-planning.mjs';
|
|
|
5
5
|
|
|
6
6
|
type Digest = (value: unknown) => string;
|
|
7
7
|
|
|
8
|
+
export function reasoningStall(result: ModelResult): boolean {
|
|
9
|
+
// The adapter detects unfinished wire tool calls even when they aren't
|
|
10
|
+
// executable. Missing usage or a mixed response isn't proof of pure reasoning.
|
|
11
|
+
return result.kind === 'output_limit' && result.outputBoundary === 'no-visible-output/v1'
|
|
12
|
+
&& !result.text.trim() && !result.toolCalls.length && result.usageReported === true
|
|
13
|
+
&& result.usage.outputTokens > 0 && result.usage.reasoningTokens === result.usage.outputTokens;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function reasoningStallRetryTokens(maxOutputTokens: number): number {
|
|
17
|
+
return Math.max(1, Math.min(8192, Math.floor(maxOutputTokens / 2)));
|
|
18
|
+
}
|
|
19
|
+
|
|
8
20
|
/** Select from actual admission after the completed model claim, never the catalog. */
|
|
9
21
|
export function outputRecoveryMode(snapshot: RunSnapshot, config: RunConfiguration, toolsAvailable: boolean, result?: ModelResult): 'suffix' | 'task_resume' {
|
|
10
22
|
if (responseBoundaryRecovery(config) && config.contract.finalOutputFormat === 'text') {
|
|
11
23
|
// A provider length boundary identifies truncated text, not task completion.
|
|
12
24
|
// Preserve explicit sealed delivery, otherwise retain the admitted tools.
|
|
13
|
-
if (config.outputRecoveryPolicy
|
|
25
|
+
if (config.outputRecoveryPolicy !== 'response-boundary/v2')
|
|
14
26
|
return responseContinuationPending(snapshot, config) || !toolsAvailable ? 'suffix' : 'task_resume';
|
|
15
27
|
if (responseContinuationPending(snapshot, config)
|
|
16
28
|
|| result?.textCommitVersion === 'provider-finished-text/v1' && result.outputBoundary === 'assistant-text/v1') return 'suffix';
|
package/src/recovery.mts
CHANGED
|
@@ -12,6 +12,7 @@ export function safeReadRecoveryCalls(calls: ToolCall[], configuration: RunConfi
|
|
|
12
12
|
}
|
|
13
13
|
function replayPolicy(intent: EffectIntent, configuration: RunConfiguration): string | null {
|
|
14
14
|
if (intent.kind === 'workspace') return 'workspace-observation/v1';
|
|
15
|
+
if (intent.kind === 'verify' && configuration.hostVerification) return 'host-verification-read/v1';
|
|
15
16
|
if (intent.kind === 'model' && configuration.modelRecoveryPolicy === 'pure-inference/v1') return 'pure-inference/v1';
|
|
16
17
|
if (intent.kind === 'tool') {
|
|
17
18
|
const calls = intent.payload.calls as unknown as ToolCall[];
|
|
@@ -3,7 +3,8 @@ import type { Message, RunConfiguration, RunSnapshot } from './contracts.mjs';
|
|
|
3
3
|
/** The output recovery strategy is versioned separately from the sealed v2
|
|
4
4
|
* text-fragment protocol. A v3 task may still own an explicit suffix chain. */
|
|
5
5
|
export function responseBoundaryRecovery(config: Pick<RunConfiguration, 'outputRecoveryPolicy'>): boolean {
|
|
6
|
-
return config.outputRecoveryPolicy === 'response-boundary/v2' || config.outputRecoveryPolicy === 'response-boundary/v3'
|
|
6
|
+
return config.outputRecoveryPolicy === 'response-boundary/v2' || config.outputRecoveryPolicy === 'response-boundary/v3'
|
|
7
|
+
|| config.outputRecoveryPolicy === 'response-boundary/v4' || config.outputRecoveryPolicy === 'response-boundary/v5';
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
export function responseScopedOutputRestarts(config: Pick<RunConfiguration, 'outputRestartPolicy'>): boolean {
|
package/src/transitions.mts
CHANGED
|
@@ -146,7 +146,8 @@ export function reduceCommand(snapshot: RunSnapshot, command: RunCommand, pendin
|
|
|
146
146
|
if (snapshot.phase !== 'paused') reason = 'not_paused';
|
|
147
147
|
else changes = { phase: snapshot.resumePhase ?? 'preparing_context', resumePhase: null,
|
|
148
148
|
...(snapshot.reason === 'output_budget_exhausted' && bindings.configuration?.outputRestartPolicy === 'per-response/v2'
|
|
149
|
-
? { reason: null, continuations: 0, outputRestarts: 0
|
|
149
|
+
? { reason: null, continuations: 0, outputRestarts: 0,
|
|
150
|
+
...(snapshot.reasoningStallRetry ? { reasoningStallRetry: false } : {}) } : {}),
|
|
150
151
|
...(bindings.configuration?.approvalOutcomePolicy === APPROVAL_OUTCOME_POLICY
|
|
151
152
|
&& ['approval_rejected', 'approval_cancelled', 'approval_unavailable'].includes(snapshot.reason ?? '') ? { reason: null } : {}),
|
|
152
153
|
...(['provider_unavailable', 'context_summary_unavailable', 'context_source_unavailable', 'child_paused']
|