@kingsoftcloud/agentkit-harness-core 0.0.9 → 0.0.10
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 +1 -0
- package/dist/configuration.mjs +8 -2
- package/dist/context-planning.mjs +25 -4
- package/dist/contracts.d.mts +7 -1
- package/dist/machine.mjs +8 -1
- package/dist/model-surface.d.mts +2 -1
- package/dist/model-surface.mjs +2 -1
- package/dist/transitions.mjs +2 -1
- package/package.json +1 -1
- package/src/configuration.mts +9 -2
- package/src/context-planning.mts +23 -4
- package/src/contracts.mts +7 -1
- package/src/machine.mts +7 -1
- package/src/model-surface.mts +4 -2
- package/src/transitions.mts +2 -1
package/dist/configuration.d.mts
CHANGED
|
@@ -24,6 +24,7 @@ export interface ConfigurationInput {
|
|
|
24
24
|
inputPolicy?: RunConfiguration['inputPolicy'];
|
|
25
25
|
sourceReadPolicy?: RunConfiguration['sourceReadPolicy'];
|
|
26
26
|
outputRecoveryPolicy?: RunConfiguration['outputRecoveryPolicy'];
|
|
27
|
+
providerRecoveryPolicy?: RunConfiguration['providerRecoveryPolicy'];
|
|
27
28
|
toolErrorRecoveryPolicy?: RunConfiguration['toolErrorRecoveryPolicy'];
|
|
28
29
|
toolDiscoveryPolicy?: RunConfiguration['toolDiscoveryPolicy'];
|
|
29
30
|
todoPolicy?: RunConfiguration['todoPolicy'];
|
package/dist/configuration.mjs
CHANGED
|
@@ -48,6 +48,7 @@ export function configure(input) {
|
|
|
48
48
|
...(input.inputPolicy === undefined ? {} : { inputPolicy: input.inputPolicy }),
|
|
49
49
|
...(input.sourceReadPolicy === undefined ? {} : { sourceReadPolicy: input.sourceReadPolicy }),
|
|
50
50
|
...(input.outputRecoveryPolicy === undefined ? {} : { outputRecoveryPolicy: input.outputRecoveryPolicy }),
|
|
51
|
+
...(input.providerRecoveryPolicy === undefined ? {} : { providerRecoveryPolicy: input.providerRecoveryPolicy }),
|
|
51
52
|
...(input.toolErrorRecoveryPolicy === undefined ? {} : { toolErrorRecoveryPolicy: input.toolErrorRecoveryPolicy }),
|
|
52
53
|
...(input.toolDiscoveryPolicy === undefined ? {} : { toolDiscoveryPolicy: input.toolDiscoveryPolicy }),
|
|
53
54
|
...(input.todoPolicy === undefined ? {} : { todoPolicy: input.todoPolicy }),
|
|
@@ -102,6 +103,7 @@ export function configure(input) {
|
|
|
102
103
|
invariant(config.guidancePolicy == null || config.guidancePolicy === 'boundary-guide/v1', 'invalid_guidance_policy');
|
|
103
104
|
invariant(config.inputPolicy === undefined || config.inputPolicy === 'before-first-model/v1', 'invalid_input_policy');
|
|
104
105
|
invariant(config.outputRecoveryPolicy == null || ['task-or-delivery/v1', 'response-boundary/v2'].includes(config.outputRecoveryPolicy), 'invalid_output_recovery_policy');
|
|
106
|
+
invariant(config.providerRecoveryPolicy == null || config.providerRecoveryPolicy === 'provider-recovery/v2', 'invalid_provider_recovery_policy');
|
|
105
107
|
invariant(config.toolExecutionPolicy === undefined || config.toolExecutionPolicy === 'owner-execution/v1', 'invalid_tool_execution_policy');
|
|
106
108
|
invariant(config.toolErrorRecoveryPolicy == null || config.toolErrorRecoveryPolicy === 'diagnose-before-repeat/v1', 'invalid_tool_error_recovery_policy');
|
|
107
109
|
invariant(config.toolErrorRecoveryModelCalls == null || Number.isSafeInteger(config.toolErrorRecoveryModelCalls)
|
|
@@ -169,7 +171,7 @@ export function configure(input) {
|
|
|
169
171
|
}
|
|
170
172
|
invariant(profile.maxOutputTokens > 0 && profile.maxOutputTokens < profile.contextWindow
|
|
171
173
|
&& profile.requestTimeoutMs > 0 && profile.requestTimeoutMs < limits.effectTimeoutMs
|
|
172
|
-
&& profile.maxProviderRetries <=
|
|
174
|
+
&& profile.maxProviderRetries <= 10 && profile.retryInitialDelayMs <= profile.retryMaxDelayMs
|
|
173
175
|
&& Number.isFinite(profile.retryBackoffMultiplier) && profile.retryBackoffMultiplier >= 1, 'invalid_model_profile');
|
|
174
176
|
if (profile.inlineReasoningPolicy !== undefined) {
|
|
175
177
|
const policy = profile.inlineReasoningPolicy;
|
|
@@ -183,7 +185,7 @@ export function configure(input) {
|
|
|
183
185
|
const policy = profile.providerCompatibilityPolicy;
|
|
184
186
|
invariant(policy !== null && typeof policy === 'object' && !Array.isArray(policy)
|
|
185
187
|
&& Object.keys(policy).every(key => ['contract', 'rule'].includes(key))
|
|
186
|
-
&& ['provider-compat/v1', 'provider-compat/v2'].includes(policy.contract)
|
|
188
|
+
&& ['provider-compat/v1', 'provider-compat/v2', 'provider-compat/v3'].includes(policy.contract)
|
|
187
189
|
&& ['auto', 'permissive', 'openai', 'strict-openai', 'cerebras', 'groq', 'deepseek',
|
|
188
190
|
'zai', 'kspmas-gateway', 'moonshot', 'mimo'].includes(policy.rule), 'invalid_provider_compatibility_policy');
|
|
189
191
|
}
|
|
@@ -301,6 +303,10 @@ export function configure(input) {
|
|
|
301
303
|
|| config.backgroundNotificationPolicy === 'completed-before-model/v1', 'invalid_background_notification_policy');
|
|
302
304
|
if (config.contextPolicy) {
|
|
303
305
|
const policy = config.contextPolicy;
|
|
306
|
+
invariant(policy.summaryOutputCeiling === undefined || policy.summaryRecoveryPolicy === 'adaptive-budget/v1'
|
|
307
|
+
&& Number.isSafeInteger(policy.summaryOutputCeiling) && policy.summaryOutputCeiling > 0
|
|
308
|
+
&& policy.summaryOutputCeiling <= profile.maxOutputTokens, 'invalid_summary_output_ceiling');
|
|
309
|
+
invariant(policy.summaryRecoveryPolicy === undefined || policy.summaryRecoveryPolicy === 'adaptive-budget/v1', 'invalid_summary_recovery_policy');
|
|
304
310
|
invariant(policy.summaryFormatPolicy === undefined || policy.summaryFormatPolicy === 'task-continuity/v1', 'invalid_summary_format_policy');
|
|
305
311
|
invariant(typeof policy.tokenizerId === 'string' && policy.tokenizerId.trim() && typeof policy.tokenizerDigest === 'string' && policy.tokenizerDigest.trim()
|
|
306
312
|
&& typeof policy.enabled === 'boolean' && Number.isFinite(policy.triggerRatio) && Number.isFinite(policy.targetRatio)
|
|
@@ -249,7 +249,8 @@ function codePointStop(text, stop) {
|
|
|
249
249
|
/** Exact, contiguous source slices. Quotes are data, never executable tool frames. */
|
|
250
250
|
async function quoteChunk(snapshot, compaction, config, counters, digest, policies) {
|
|
251
251
|
const maxOutputTokens = ['deadline-delivery/v1', 'deadline-delivery/v2'].includes(config.wallDeliveryPolicy ?? '')
|
|
252
|
-
? config.profile.maxOutputTokens : Math.min(config.contextPolicy.summaryMaxTokens, config.profile.maxOutputTokens);
|
|
252
|
+
? config.profile.maxOutputTokens : Math.min(compaction.summaryOutputTokens ?? config.contextPolicy.summaryMaxTokens, config.profile.maxOutputTokens);
|
|
253
|
+
invariant(Number.isSafeInteger(maxOutputTokens) && maxOutputTokens > 0 && maxOutputTokens < config.profile.contextWindow, 'context_summary_budget_invalid');
|
|
253
254
|
const prefix = snapshot.messages.slice(0, compaction.prefixCount), refs = refsOf(prefix, digest);
|
|
254
255
|
const projected = summaryProjection(prefix, compaction, config, digest);
|
|
255
256
|
const mediaFor = (stop) => {
|
|
@@ -591,10 +592,11 @@ export function reduceContextModel(snapshot, result, config, usage, now, random)
|
|
|
591
592
|
invariant(compaction?.chunk, 'context_summary_quote_missing');
|
|
592
593
|
const base = { pendingIntent: null, contextState: state, preparedContext: null, usage,
|
|
593
594
|
phase: 'compacting', ...(result.usageReported === false ? { diagnostics: { ...snapshot.diagnostics, providerUsageIncomplete: true } } : {}) };
|
|
594
|
-
|
|
595
|
+
const adaptive = config.contextPolicy?.summaryRecoveryPolicy === 'adaptive-budget/v1';
|
|
596
|
+
if (!adaptive && result.kind === 'output_limit' && result.text.trim() && result.textCommitVersion !== 'provider-finished-text/v1') {
|
|
595
597
|
return { ...base, phase: 'finalizing', outcome: 'blocked', reason: 'context_summary_provider_commit_missing' };
|
|
596
598
|
}
|
|
597
|
-
if (
|
|
599
|
+
if ((result.kind === 'final_candidate' || !adaptive && result.kind === 'output_limit') && result.text.trim() && !result.toolCalls.length) {
|
|
598
600
|
compaction.proposal = result.text;
|
|
599
601
|
return base;
|
|
600
602
|
}
|
|
@@ -602,6 +604,8 @@ export function reduceContextModel(snapshot, result, config, usage, now, random)
|
|
|
602
604
|
return { ...base, phase: 'finalizing',
|
|
603
605
|
outcome: result.kind === 'policy_abort' ? 'blocked' : 'unverified', reason: `context_summary_${result.kind}` };
|
|
604
606
|
const provider = result.kind === 'provider_retryable_error';
|
|
607
|
+
if (!provider && config.providerRecoveryPolicy === 'provider-recovery/v2')
|
|
608
|
+
compaction.providerErrors = 0;
|
|
605
609
|
// Old persisted compactions had one combined counter. Seed their first
|
|
606
610
|
// provider retry conservatively; new compactions keep the two limits separate.
|
|
607
611
|
const attempts = provider ? (compaction.providerErrors ?? compaction.errors) + 1 : compaction.errors + 1;
|
|
@@ -610,9 +614,26 @@ export function reduceContextModel(snapshot, result, config, usage, now, random)
|
|
|
610
614
|
else
|
|
611
615
|
compaction.errors = attempts;
|
|
612
616
|
const limit = provider ? config.profile.maxProviderRetries : config.contextPolicy.maxSummaryRetries;
|
|
613
|
-
if (attempts > limit)
|
|
617
|
+
if (attempts > limit) {
|
|
618
|
+
if (adaptive || provider && config.providerRecoveryPolicy === 'provider-recovery/v2') {
|
|
619
|
+
compaction.errors = 0;
|
|
620
|
+
compaction.providerErrors = 0;
|
|
621
|
+
return { ...base, phase: 'paused', resumePhase: 'compacting', outcome: null,
|
|
622
|
+
reason: provider ? 'provider_unavailable' : 'context_summary_unavailable', retryNotBefore: 0 };
|
|
623
|
+
}
|
|
614
624
|
return { ...base, phase: 'finalizing', outcome: 'unverified', reason: 'context_summary_retry_exhausted' };
|
|
625
|
+
}
|
|
626
|
+
if (adaptive && result.kind === 'output_limit') {
|
|
627
|
+
const previous = compaction.chunk.maxOutputTokens;
|
|
628
|
+
const ceiling = Math.min(config.contextPolicy.summaryOutputCeiling ?? config.profile.maxOutputTokens, config.profile.maxOutputTokens, Math.floor(config.profile.contextWindow / 2));
|
|
629
|
+
if (previous < ceiling)
|
|
630
|
+
compaction.summaryOutputTokens = Math.min(ceiling, previous * 2);
|
|
631
|
+
else
|
|
632
|
+
compaction.quoteLimit = Math.max(1, Math.floor((compaction.chunk.stop - compaction.chunk.start) / 2));
|
|
633
|
+
compaction.chunk = null;
|
|
634
|
+
}
|
|
615
635
|
if (result.kind === 'context_overflow') {
|
|
636
|
+
invariant(compaction.chunk, 'context_summary_quote_missing');
|
|
616
637
|
const length = compaction.chunk.stop - compaction.chunk.start;
|
|
617
638
|
if (length <= 2)
|
|
618
639
|
return { ...base, phase: 'finalizing', outcome: 'blocked', reason: 'context_summary_quote_overflow' };
|
package/dist/contracts.d.mts
CHANGED
|
@@ -182,7 +182,7 @@ export interface ModelProfile {
|
|
|
182
182
|
rule: 'auto' | 'permissive' | 'openai' | 'strict-openai' | 'cerebras' | 'groq' | 'deepseek' | 'zai' | 'kspmas-gateway' | 'moonshot' | 'mimo';
|
|
183
183
|
};
|
|
184
184
|
providerCompatibilityPolicy?: {
|
|
185
|
-
contract: 'provider-compat/v1' | 'provider-compat/v2';
|
|
185
|
+
contract: 'provider-compat/v1' | 'provider-compat/v2' | 'provider-compat/v3';
|
|
186
186
|
rule: NonNullable<ModelProfile['inlineReasoningPolicy']>['rule'];
|
|
187
187
|
};
|
|
188
188
|
streaming?: boolean;
|
|
@@ -330,6 +330,9 @@ export interface HookState {
|
|
|
330
330
|
}
|
|
331
331
|
export interface ContextPlanningPolicy {
|
|
332
332
|
summaryFormatPolicy?: 'task-continuity/v1';
|
|
333
|
+
/** Adapt summary quotes after output exhaustion; preserve history on failure. */
|
|
334
|
+
summaryRecoveryPolicy?: 'adaptive-budget/v1';
|
|
335
|
+
summaryOutputCeiling?: number;
|
|
333
336
|
tokenizerId: string;
|
|
334
337
|
tokenizerDigest: string;
|
|
335
338
|
enabled: boolean;
|
|
@@ -410,6 +413,7 @@ export interface ContextCompaction {
|
|
|
410
413
|
/** Provider retry attempts are separate from invalid-summary/proposal attempts.
|
|
411
414
|
* Absence on restored legacy runs conservatively inherits `errors`. */
|
|
412
415
|
providerErrors?: number;
|
|
416
|
+
summaryOutputTokens?: number;
|
|
413
417
|
hookSubject: ObjectValue;
|
|
414
418
|
hooksComplete: boolean;
|
|
415
419
|
workspaceRevision?: string | null;
|
|
@@ -533,6 +537,8 @@ export interface RunConfiguration {
|
|
|
533
537
|
modelRecoveryPolicy: 'manual' | 'pure-inference/v1';
|
|
534
538
|
guidancePolicy?: 'boundary-guide/v1' | null;
|
|
535
539
|
outputRecoveryPolicy?: 'task-or-delivery/v1' | 'response-boundary/v2' | null;
|
|
540
|
+
/** v2 counts consecutive provider failures and pauses (resumable) instead of failing when they run out. */
|
|
541
|
+
providerRecoveryPolicy?: 'provider-recovery/v2' | null;
|
|
536
542
|
toolErrorRecoveryPolicy?: 'diagnose-before-repeat/v1' | null;
|
|
537
543
|
toolErrorRecoveryModelCalls?: number | null;
|
|
538
544
|
deliveryReviewPolicy?: DeliveryReviewPolicy | null;
|
package/dist/machine.mjs
CHANGED
|
@@ -329,6 +329,9 @@ export function reduceModel(snapshot, input, config, now, random, digest = canon
|
|
|
329
329
|
const replay = boundReplay(result, config);
|
|
330
330
|
const usage = addUsage(snapshot.usage, { ...result.usage, modelCalls: 0, toolCalls: 0 });
|
|
331
331
|
const base = { pendingIntent: null, preparedContext: null, usage,
|
|
332
|
+
// provider-recovery/v2: any provider response ends the consecutive-failure streak.
|
|
333
|
+
...(config.providerRecoveryPolicy === 'provider-recovery/v2' && result.kind !== 'provider_retryable_error'
|
|
334
|
+
&& snapshot.providerErrors ? { providerErrors: 0 } : {}),
|
|
332
335
|
...(snapshot.pendingGuidanceIds?.length && effectiveGuidanceResponse(result) ? { pendingGuidanceIds: [] } : {}),
|
|
333
336
|
...(result.usageReported === false || result.failureDiagnostic || snapshot.diagnostics.lastModelFailure
|
|
334
337
|
? { diagnostics: { ...snapshot.diagnostics,
|
|
@@ -471,9 +474,13 @@ export function reduceModel(snapshot, input, config, now, random, digest = canon
|
|
|
471
474
|
const attempts = (provider ? snapshot.providerErrors : snapshot.modelErrors) + 1;
|
|
472
475
|
const exhausted = attempts > (provider ? config.profile.maxProviderRetries : config.profile.maxRetries);
|
|
473
476
|
const delay = retryDelay(result, attempts, config, random);
|
|
477
|
+
// An unavailable provider is not a task failure: pause with a fresh budget so the Host can resume later.
|
|
478
|
+
if (provider && exhausted && config.providerRecoveryPolicy === 'provider-recovery/v2')
|
|
479
|
+
return { ...base, phase: 'paused',
|
|
480
|
+
resumePhase: 'preparing_context', outcome: null, reason: 'provider_unavailable', providerErrors: 0, retryNotBefore: 0 };
|
|
474
481
|
return { ...base, phase: exhausted ? 'finalizing' : 'preparing_context', outcome: exhausted ? 'failed' : null,
|
|
475
482
|
reason: exhausted ? `${result.kind}_retry_exhausted` : null,
|
|
476
|
-
providerErrors: provider ? attempts : snapshot.providerErrors,
|
|
483
|
+
providerErrors: provider ? attempts : base.providerErrors ?? snapshot.providerErrors,
|
|
477
484
|
modelErrors: provider ? snapshot.modelErrors : attempts, retryNotBefore: exhausted ? 0 : now + delay,
|
|
478
485
|
messages: provider ? snapshot.messages : [...snapshot.messages, { role: 'user', kind: responseContinuationPending(snapshot, config)
|
|
479
486
|
? 'response_continuation_feedback' : 'model_feedback',
|
package/dist/model-surface.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ArtifactRef, Message, RunConfiguration, VerificationReport } from './contracts.mjs';
|
|
2
|
+
export declare const NATIVE_LOCAL_RESOURCE_PROFILES: readonly ["bounded/v1", "local-development/v1", "local-development/v2"];
|
|
2
3
|
/** Facts are supplied by the execution owner, never inferred from prompt prose. */
|
|
3
4
|
export interface WorkspaceBrowserCapability {
|
|
4
5
|
contract: 'agentkit.workspace-browser-capability/v1';
|
|
@@ -23,7 +24,7 @@ export interface ModelSurface {
|
|
|
23
24
|
environmentNames?: string[];
|
|
24
25
|
commandResources?: {
|
|
25
26
|
contract: 'agentkit.local-resource-profile/v1';
|
|
26
|
-
profile:
|
|
27
|
+
profile: typeof NATIVE_LOCAL_RESOURCE_PROFILES[number];
|
|
27
28
|
requestedLimits: {
|
|
28
29
|
cpuSeconds: number;
|
|
29
30
|
addressSpaceBytes: number;
|
package/dist/model-surface.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { canonical, copy, invariant } from './values.mjs';
|
|
2
|
+
export const NATIVE_LOCAL_RESOURCE_PROFILES = ['bounded/v1', 'local-development/v1', 'local-development/v2'];
|
|
2
3
|
const bytes = (value) => new TextEncoder().encode(canonical(value)).length;
|
|
3
4
|
export function validateModelSurface(surface) {
|
|
4
5
|
if (surface === undefined)
|
|
@@ -35,7 +36,7 @@ export function validateModelSurface(surface) {
|
|
|
35
36
|
invariant(env.commandBackend === 'local_process' && resources
|
|
36
37
|
&& Object.keys(resources).sort().join(',') === 'contract,profile,requestedLimits'
|
|
37
38
|
&& resources.contract === 'agentkit.local-resource-profile/v1'
|
|
38
|
-
&&
|
|
39
|
+
&& NATIVE_LOCAL_RESOURCE_PROFILES.includes(resources.profile)
|
|
39
40
|
&& resources.requestedLimits && Object.keys(resources.requestedLimits).sort().join(',') === [...keys].sort().join(',')
|
|
40
41
|
&& Object.values(resources.requestedLimits).every(value => Number.isSafeInteger(value) && value > 0), 'invalid_model_command_resources');
|
|
41
42
|
}
|
package/dist/transitions.mjs
CHANGED
|
@@ -148,7 +148,8 @@ export function reduceCommand(snapshot, command, pending = null, bindings = {})
|
|
|
148
148
|
if (snapshot.phase !== 'paused')
|
|
149
149
|
reason = 'not_paused';
|
|
150
150
|
else
|
|
151
|
-
changes = { phase: snapshot.resumePhase ?? 'preparing_context', resumePhase: null
|
|
151
|
+
changes = { phase: snapshot.resumePhase ?? 'preparing_context', resumePhase: null,
|
|
152
|
+
...(['provider_unavailable', 'context_summary_unavailable'].includes(snapshot.reason ?? '') ? { reason: null } : {}) };
|
|
152
153
|
break;
|
|
153
154
|
case 'submit_interaction': {
|
|
154
155
|
const pending = snapshot.pendingInteraction;
|
package/package.json
CHANGED
package/src/configuration.mts
CHANGED
|
@@ -36,6 +36,7 @@ export interface ConfigurationInput {
|
|
|
36
36
|
inputPolicy?: RunConfiguration['inputPolicy'];
|
|
37
37
|
sourceReadPolicy?: RunConfiguration['sourceReadPolicy'];
|
|
38
38
|
outputRecoveryPolicy?: RunConfiguration['outputRecoveryPolicy'];
|
|
39
|
+
providerRecoveryPolicy?: RunConfiguration['providerRecoveryPolicy'];
|
|
39
40
|
toolErrorRecoveryPolicy?: RunConfiguration['toolErrorRecoveryPolicy'];
|
|
40
41
|
toolDiscoveryPolicy?: RunConfiguration['toolDiscoveryPolicy'];
|
|
41
42
|
todoPolicy?: RunConfiguration['todoPolicy'];
|
|
@@ -95,6 +96,7 @@ export function configure(input: ConfigurationInput): RunConfiguration {
|
|
|
95
96
|
...(input.inputPolicy === undefined ? {} : { inputPolicy: input.inputPolicy }),
|
|
96
97
|
...(input.sourceReadPolicy === undefined ? {} : { sourceReadPolicy: input.sourceReadPolicy }),
|
|
97
98
|
...(input.outputRecoveryPolicy === undefined ? {} : { outputRecoveryPolicy: input.outputRecoveryPolicy }),
|
|
99
|
+
...(input.providerRecoveryPolicy === undefined ? {} : { providerRecoveryPolicy: input.providerRecoveryPolicy }),
|
|
98
100
|
...(input.toolErrorRecoveryPolicy === undefined ? {} : { toolErrorRecoveryPolicy: input.toolErrorRecoveryPolicy }),
|
|
99
101
|
...(input.toolDiscoveryPolicy === undefined ? {} : { toolDiscoveryPolicy: input.toolDiscoveryPolicy }),
|
|
100
102
|
...(input.todoPolicy === undefined ? {} : { todoPolicy: input.todoPolicy }),
|
|
@@ -150,6 +152,7 @@ export function configure(input: ConfigurationInput): RunConfiguration {
|
|
|
150
152
|
invariant(config.guidancePolicy == null || config.guidancePolicy === 'boundary-guide/v1', 'invalid_guidance_policy');
|
|
151
153
|
invariant(config.inputPolicy === undefined || config.inputPolicy === 'before-first-model/v1', 'invalid_input_policy');
|
|
152
154
|
invariant(config.outputRecoveryPolicy == null || ['task-or-delivery/v1', 'response-boundary/v2'].includes(config.outputRecoveryPolicy), 'invalid_output_recovery_policy');
|
|
155
|
+
invariant(config.providerRecoveryPolicy == null || config.providerRecoveryPolicy === 'provider-recovery/v2', 'invalid_provider_recovery_policy');
|
|
153
156
|
invariant(config.toolExecutionPolicy === undefined || config.toolExecutionPolicy === 'owner-execution/v1', 'invalid_tool_execution_policy');
|
|
154
157
|
invariant(config.toolErrorRecoveryPolicy == null || config.toolErrorRecoveryPolicy === 'diagnose-before-repeat/v1', 'invalid_tool_error_recovery_policy');
|
|
155
158
|
invariant(config.toolErrorRecoveryModelCalls == null || Number.isSafeInteger(config.toolErrorRecoveryModelCalls)
|
|
@@ -219,7 +222,7 @@ export function configure(input: ConfigurationInput): RunConfiguration {
|
|
|
219
222
|
}
|
|
220
223
|
invariant(profile.maxOutputTokens > 0 && profile.maxOutputTokens < profile.contextWindow
|
|
221
224
|
&& profile.requestTimeoutMs > 0 && profile.requestTimeoutMs < limits.effectTimeoutMs
|
|
222
|
-
&& profile.maxProviderRetries <=
|
|
225
|
+
&& profile.maxProviderRetries <= 10 && profile.retryInitialDelayMs <= profile.retryMaxDelayMs
|
|
223
226
|
&& Number.isFinite(profile.retryBackoffMultiplier) && profile.retryBackoffMultiplier >= 1, 'invalid_model_profile');
|
|
224
227
|
if (profile.inlineReasoningPolicy !== undefined) {
|
|
225
228
|
const policy = profile.inlineReasoningPolicy;
|
|
@@ -233,7 +236,7 @@ export function configure(input: ConfigurationInput): RunConfiguration {
|
|
|
233
236
|
const policy = profile.providerCompatibilityPolicy;
|
|
234
237
|
invariant(policy !== null && typeof policy === 'object' && !Array.isArray(policy)
|
|
235
238
|
&& Object.keys(policy).every(key => ['contract', 'rule'].includes(key))
|
|
236
|
-
&& ['provider-compat/v1', 'provider-compat/v2'].includes(policy.contract)
|
|
239
|
+
&& ['provider-compat/v1', 'provider-compat/v2', 'provider-compat/v3'].includes(policy.contract)
|
|
237
240
|
&& ['auto', 'permissive', 'openai', 'strict-openai', 'cerebras', 'groq', 'deepseek',
|
|
238
241
|
'zai', 'kspmas-gateway', 'moonshot', 'mimo'].includes(policy.rule), 'invalid_provider_compatibility_policy');
|
|
239
242
|
}
|
|
@@ -354,6 +357,10 @@ export function configure(input: ConfigurationInput): RunConfiguration {
|
|
|
354
357
|
|| config.backgroundNotificationPolicy === 'completed-before-model/v1', 'invalid_background_notification_policy');
|
|
355
358
|
if (config.contextPolicy) {
|
|
356
359
|
const policy = config.contextPolicy;
|
|
360
|
+
invariant(policy.summaryOutputCeiling === undefined || policy.summaryRecoveryPolicy === 'adaptive-budget/v1'
|
|
361
|
+
&& Number.isSafeInteger(policy.summaryOutputCeiling) && policy.summaryOutputCeiling > 0
|
|
362
|
+
&& policy.summaryOutputCeiling <= profile.maxOutputTokens, 'invalid_summary_output_ceiling');
|
|
363
|
+
invariant(policy.summaryRecoveryPolicy === undefined || policy.summaryRecoveryPolicy === 'adaptive-budget/v1', 'invalid_summary_recovery_policy');
|
|
357
364
|
invariant(policy.summaryFormatPolicy === undefined || policy.summaryFormatPolicy === 'task-continuity/v1',
|
|
358
365
|
'invalid_summary_format_policy');
|
|
359
366
|
invariant(typeof policy.tokenizerId === 'string' && policy.tokenizerId.trim() && typeof policy.tokenizerDigest === 'string' && policy.tokenizerDigest.trim()
|
package/src/context-planning.mts
CHANGED
|
@@ -230,7 +230,8 @@ function codePointStop(text: string, stop: number): number {
|
|
|
230
230
|
async function quoteChunk(snapshot: RunSnapshot, compaction: ContextCompaction, config: RunConfiguration,
|
|
231
231
|
counters: ContextCounters, digest: (value: unknown) => string, policies: NativePolicyRegistry): Promise<void> {
|
|
232
232
|
const maxOutputTokens = ['deadline-delivery/v1', 'deadline-delivery/v2'].includes(config.wallDeliveryPolicy ?? '')
|
|
233
|
-
? config.profile.maxOutputTokens : Math.min(config.contextPolicy!.summaryMaxTokens, config.profile.maxOutputTokens);
|
|
233
|
+
? config.profile.maxOutputTokens : Math.min(compaction.summaryOutputTokens ?? config.contextPolicy!.summaryMaxTokens, config.profile.maxOutputTokens);
|
|
234
|
+
invariant(Number.isSafeInteger(maxOutputTokens) && maxOutputTokens > 0 && maxOutputTokens < config.profile.contextWindow, 'context_summary_budget_invalid');
|
|
234
235
|
const prefix = snapshot.messages.slice(0, compaction.prefixCount), refs = refsOf(prefix, digest);
|
|
235
236
|
const projected = summaryProjection(prefix, compaction, config, digest);
|
|
236
237
|
const mediaFor = (stop: number): NonNullable<Message['media']> => {
|
|
@@ -525,23 +526,41 @@ export function reduceContextModel(snapshot: RunSnapshot, result: ModelResult, c
|
|
|
525
526
|
invariant(compaction?.chunk, 'context_summary_quote_missing');
|
|
526
527
|
const base: Partial<RunSnapshot> = { pendingIntent: null, contextState: state, preparedContext: null, usage,
|
|
527
528
|
phase: 'compacting', ...(result.usageReported === false ? { diagnostics: { ...snapshot.diagnostics, providerUsageIncomplete: true } } : {}) };
|
|
528
|
-
|
|
529
|
+
const adaptive = config.contextPolicy?.summaryRecoveryPolicy === 'adaptive-budget/v1';
|
|
530
|
+
if (!adaptive && result.kind === 'output_limit' && result.text.trim() && result.textCommitVersion !== 'provider-finished-text/v1') {
|
|
529
531
|
return { ...base, phase: 'finalizing', outcome: 'blocked', reason: 'context_summary_provider_commit_missing' };
|
|
530
532
|
}
|
|
531
|
-
if (
|
|
533
|
+
if ((result.kind === 'final_candidate' || !adaptive && result.kind === 'output_limit') && result.text.trim() && !result.toolCalls.length) {
|
|
532
534
|
compaction.proposal = result.text; return base;
|
|
533
535
|
}
|
|
534
536
|
if (['policy_abort', 'user_abort', 'provider_terminal_error'].includes(result.kind)) return { ...base, phase: 'finalizing',
|
|
535
537
|
outcome: result.kind === 'policy_abort' ? 'blocked' : 'unverified', reason: `context_summary_${result.kind}` };
|
|
536
538
|
const provider = result.kind === 'provider_retryable_error';
|
|
539
|
+
if (!provider && config.providerRecoveryPolicy === 'provider-recovery/v2') compaction.providerErrors = 0;
|
|
537
540
|
// Old persisted compactions had one combined counter. Seed their first
|
|
538
541
|
// provider retry conservatively; new compactions keep the two limits separate.
|
|
539
542
|
const attempts = provider ? (compaction.providerErrors ?? compaction.errors) + 1 : compaction.errors + 1;
|
|
540
543
|
if (provider) compaction.providerErrors = attempts;
|
|
541
544
|
else compaction.errors = attempts;
|
|
542
545
|
const limit = provider ? config.profile.maxProviderRetries : config.contextPolicy!.maxSummaryRetries;
|
|
543
|
-
if (attempts > limit)
|
|
546
|
+
if (attempts > limit) {
|
|
547
|
+
if (adaptive || provider && config.providerRecoveryPolicy === 'provider-recovery/v2') {
|
|
548
|
+
compaction.errors = 0; compaction.providerErrors = 0;
|
|
549
|
+
return { ...base, phase: 'paused', resumePhase: 'compacting', outcome: null,
|
|
550
|
+
reason: provider ? 'provider_unavailable' : 'context_summary_unavailable', retryNotBefore: 0 };
|
|
551
|
+
}
|
|
552
|
+
return { ...base, phase: 'finalizing', outcome: 'unverified', reason: 'context_summary_retry_exhausted' };
|
|
553
|
+
}
|
|
554
|
+
if (adaptive && result.kind === 'output_limit') {
|
|
555
|
+
const previous = compaction.chunk.maxOutputTokens;
|
|
556
|
+
const ceiling = Math.min(config.contextPolicy!.summaryOutputCeiling ?? config.profile.maxOutputTokens,
|
|
557
|
+
config.profile.maxOutputTokens, Math.floor(config.profile.contextWindow / 2));
|
|
558
|
+
if (previous < ceiling) compaction.summaryOutputTokens = Math.min(ceiling, previous * 2);
|
|
559
|
+
else compaction.quoteLimit = Math.max(1, Math.floor((compaction.chunk.stop - compaction.chunk.start) / 2));
|
|
560
|
+
compaction.chunk = null;
|
|
561
|
+
}
|
|
544
562
|
if (result.kind === 'context_overflow') {
|
|
563
|
+
invariant(compaction.chunk, 'context_summary_quote_missing');
|
|
545
564
|
const length = compaction.chunk.stop - compaction.chunk.start;
|
|
546
565
|
if (length <= 2) return { ...base, phase: 'finalizing', outcome: 'blocked', reason: 'context_summary_quote_overflow' };
|
|
547
566
|
compaction.quoteLimit = Math.max(1, Math.floor(length / 2)); compaction.chunk = null;
|
package/src/contracts.mts
CHANGED
|
@@ -105,7 +105,7 @@ export interface ModelProfile {
|
|
|
105
105
|
/** Absent on older Runs; projection policy is frozen at admission. */
|
|
106
106
|
inlineReasoningPolicy?: { contract: 'profiled-content/v1' | 'profiled-content/v2'; rule: 'auto' | 'permissive' | 'openai'
|
|
107
107
|
| 'strict-openai' | 'cerebras' | 'groq' | 'deepseek' | 'zai' | 'kspmas-gateway' | 'moonshot' | 'mimo' };
|
|
108
|
-
providerCompatibilityPolicy?: { contract: 'provider-compat/v1' | 'provider-compat/v2'; rule: NonNullable<ModelProfile['inlineReasoningPolicy']>['rule'] };
|
|
108
|
+
providerCompatibilityPolicy?: { contract: 'provider-compat/v1' | 'provider-compat/v2' | 'provider-compat/v3'; rule: NonNullable<ModelProfile['inlineReasoningPolicy']>['rule'] };
|
|
109
109
|
streaming?: boolean; outputTokenParameter?: 'max_tokens' | 'max_completion_tokens';
|
|
110
110
|
streamTimeoutPolicy?: 'idle/v1'; providerThinkingDisplay?: boolean;
|
|
111
111
|
/** Absent on historical Runs, whose OpenAI usage reports required equality. */
|
|
@@ -181,6 +181,9 @@ export interface HookState {
|
|
|
181
181
|
}
|
|
182
182
|
export interface ContextPlanningPolicy {
|
|
183
183
|
summaryFormatPolicy?: 'task-continuity/v1';
|
|
184
|
+
/** Adapt summary quotes after output exhaustion; preserve history on failure. */
|
|
185
|
+
summaryRecoveryPolicy?: 'adaptive-budget/v1';
|
|
186
|
+
summaryOutputCeiling?: number;
|
|
184
187
|
tokenizerId: string; tokenizerDigest: string; enabled: boolean;
|
|
185
188
|
/** Estimates bound complete requests; exact counters keep the strict summary ceiling. */
|
|
186
189
|
summaryBudgetMode?: 'strict' | 'context_bounded_estimate';
|
|
@@ -225,6 +228,7 @@ export interface ContextCompaction {
|
|
|
225
228
|
/** Provider retry attempts are separate from invalid-summary/proposal attempts.
|
|
226
229
|
* Absence on restored legacy runs conservatively inherits `errors`. */
|
|
227
230
|
providerErrors?: number;
|
|
231
|
+
summaryOutputTokens?: number;
|
|
228
232
|
hookSubject: ObjectValue; hooksComplete: boolean;
|
|
229
233
|
workspaceRevision?: string | null;
|
|
230
234
|
/** References into the immutable prefix; cleared groups must belong to removedRefs. */
|
|
@@ -302,6 +306,8 @@ export interface RunConfiguration {
|
|
|
302
306
|
modelRecoveryPolicy: 'manual' | 'pure-inference/v1';
|
|
303
307
|
guidancePolicy?: 'boundary-guide/v1' | null;
|
|
304
308
|
outputRecoveryPolicy?: 'task-or-delivery/v1' | 'response-boundary/v2' | null;
|
|
309
|
+
/** v2 counts consecutive provider failures and pauses (resumable) instead of failing when they run out. */
|
|
310
|
+
providerRecoveryPolicy?: 'provider-recovery/v2' | null;
|
|
305
311
|
toolErrorRecoveryPolicy?: 'diagnose-before-repeat/v1' | null;
|
|
306
312
|
toolErrorRecoveryModelCalls?: number | null;
|
|
307
313
|
deliveryReviewPolicy?: DeliveryReviewPolicy | null;
|
package/src/machine.mts
CHANGED
|
@@ -311,6 +311,9 @@ export function reduceModel(snapshot: RunSnapshot, input: ModelResult, config: R
|
|
|
311
311
|
const replay = boundReplay(result, config);
|
|
312
312
|
const usage = addUsage(snapshot.usage, { ...result.usage, modelCalls: 0, toolCalls: 0 });
|
|
313
313
|
const base: Partial<RunSnapshot> = { pendingIntent: null, preparedContext: null, usage,
|
|
314
|
+
// provider-recovery/v2: any provider response ends the consecutive-failure streak.
|
|
315
|
+
...(config.providerRecoveryPolicy === 'provider-recovery/v2' && result.kind !== 'provider_retryable_error'
|
|
316
|
+
&& snapshot.providerErrors ? { providerErrors: 0 } : {}),
|
|
314
317
|
...(snapshot.pendingGuidanceIds?.length && effectiveGuidanceResponse(result) ? { pendingGuidanceIds: [] } : {}),
|
|
315
318
|
...(result.usageReported === false || result.failureDiagnostic || snapshot.diagnostics.lastModelFailure
|
|
316
319
|
? { diagnostics: { ...snapshot.diagnostics,
|
|
@@ -440,9 +443,12 @@ export function reduceModel(snapshot: RunSnapshot, input: ModelResult, config: R
|
|
|
440
443
|
const attempts = (provider ? snapshot.providerErrors : snapshot.modelErrors) + 1;
|
|
441
444
|
const exhausted = attempts > (provider ? config.profile.maxProviderRetries : config.profile.maxRetries);
|
|
442
445
|
const delay = retryDelay(result, attempts, config, random);
|
|
446
|
+
// An unavailable provider is not a task failure: pause with a fresh budget so the Host can resume later.
|
|
447
|
+
if (provider && exhausted && config.providerRecoveryPolicy === 'provider-recovery/v2') return { ...base, phase: 'paused',
|
|
448
|
+
resumePhase: 'preparing_context', outcome: null, reason: 'provider_unavailable', providerErrors: 0, retryNotBefore: 0 };
|
|
443
449
|
return { ...base, phase: exhausted ? 'finalizing' : 'preparing_context', outcome: exhausted ? 'failed' : null,
|
|
444
450
|
reason: exhausted ? `${result.kind}_retry_exhausted` : null,
|
|
445
|
-
providerErrors: provider ? attempts : snapshot.providerErrors,
|
|
451
|
+
providerErrors: provider ? attempts : base.providerErrors ?? snapshot.providerErrors,
|
|
446
452
|
modelErrors: provider ? snapshot.modelErrors : attempts, retryNotBefore: exhausted ? 0 : now + delay,
|
|
447
453
|
messages: provider ? snapshot.messages : [...snapshot.messages, { role: 'user', kind: responseContinuationPending(snapshot, config)
|
|
448
454
|
? 'response_continuation_feedback' : 'model_feedback',
|
package/src/model-surface.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { ArtifactRef, Message, RunConfiguration, VerificationReport } from './contracts.mjs';
|
|
2
2
|
import { canonical, copy, invariant } from './values.mjs';
|
|
3
3
|
|
|
4
|
+
export const NATIVE_LOCAL_RESOURCE_PROFILES = ['bounded/v1', 'local-development/v1', 'local-development/v2'] as const;
|
|
5
|
+
|
|
4
6
|
/** Facts are supplied by the execution owner, never inferred from prompt prose. */
|
|
5
7
|
export interface WorkspaceBrowserCapability {
|
|
6
8
|
contract: 'agentkit.workspace-browser-capability/v1'; toolName: 'browser_inspect_workspace';
|
|
@@ -16,7 +18,7 @@ export interface ModelSurface {
|
|
|
16
18
|
commandBackend: 'local_process' | 'e2b' | 'none';
|
|
17
19
|
commandCwd?: string; environmentNames?: string[];
|
|
18
20
|
commandResources?: {
|
|
19
|
-
contract: 'agentkit.local-resource-profile/v1'; profile:
|
|
21
|
+
contract: 'agentkit.local-resource-profile/v1'; profile: typeof NATIVE_LOCAL_RESOURCE_PROFILES[number];
|
|
20
22
|
requestedLimits: { cpuSeconds: number; addressSpaceBytes: number; maxProcesses: number;
|
|
21
23
|
maxOpenFiles: number; maxFileBytes: number; wallSeconds: number };
|
|
22
24
|
};
|
|
@@ -58,7 +60,7 @@ export function validateModelSurface(surface: ModelSurface | undefined): void {
|
|
|
58
60
|
invariant(env.commandBackend === 'local_process' && resources
|
|
59
61
|
&& Object.keys(resources).sort().join(',') === 'contract,profile,requestedLimits'
|
|
60
62
|
&& resources.contract === 'agentkit.local-resource-profile/v1'
|
|
61
|
-
&&
|
|
63
|
+
&& NATIVE_LOCAL_RESOURCE_PROFILES.includes(resources.profile)
|
|
62
64
|
&& resources.requestedLimits && Object.keys(resources.requestedLimits).sort().join(',') === [...keys].sort().join(',')
|
|
63
65
|
&& Object.values(resources.requestedLimits).every(value => Number.isSafeInteger(value) && value > 0),
|
|
64
66
|
'invalid_model_command_resources');
|
package/src/transitions.mts
CHANGED
|
@@ -143,7 +143,8 @@ export function reduceCommand(snapshot: RunSnapshot, command: RunCommand, pendin
|
|
|
143
143
|
break;
|
|
144
144
|
case 'resume':
|
|
145
145
|
if (snapshot.phase !== 'paused') reason = 'not_paused';
|
|
146
|
-
else changes = { phase: snapshot.resumePhase ?? 'preparing_context', resumePhase: null
|
|
146
|
+
else changes = { phase: snapshot.resumePhase ?? 'preparing_context', resumePhase: null,
|
|
147
|
+
...(['provider_unavailable', 'context_summary_unavailable'].includes(snapshot.reason ?? '') ? { reason: null } : {}) };
|
|
147
148
|
break;
|
|
148
149
|
case 'submit_interaction': {
|
|
149
150
|
const pending = snapshot.pendingInteraction;
|