@linnlabs/linnkit 0.38.0 → 0.39.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +33 -1
  2. package/dist/cli.cjs +111 -79
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +111 -79
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager.cjs +23 -17
  7. package/dist/context-manager.cjs.map +1 -1
  8. package/dist/context-manager.js +23 -17
  9. package/dist/context-manager.js.map +1 -1
  10. package/dist/contracts/contextPolicy.d.cts +2 -1
  11. package/dist/contracts/contextPolicy.d.ts +2 -1
  12. package/dist/contracts/token-accounting.d.cts +8 -8
  13. package/dist/contracts/token-accounting.d.ts +8 -8
  14. package/dist/contracts.cjs.map +1 -1
  15. package/dist/contracts.js.map +1 -1
  16. package/dist/index.cjs +157 -80
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.js +157 -80
  19. package/dist/index.js.map +1 -1
  20. package/dist/quickstart.cjs +111 -79
  21. package/dist/quickstart.cjs.map +1 -1
  22. package/dist/quickstart.js +111 -79
  23. package/dist/quickstart.js.map +1 -1
  24. package/dist/runtime-kernel/events.cjs.map +1 -1
  25. package/dist/runtime-kernel/events.js.map +1 -1
  26. package/dist/runtime-kernel/llm/definitions/llmCallInvocationContext.d.cts +4 -1
  27. package/dist/runtime-kernel/llm/definitions/llmCallInvocationContext.d.ts +4 -1
  28. package/dist/runtime-kernel.cjs +157 -80
  29. package/dist/runtime-kernel.cjs.map +1 -1
  30. package/dist/runtime-kernel.js +157 -80
  31. package/dist/runtime-kernel.js.map +1 -1
  32. package/dist/shared/prompt-budget.d.cts +4 -2
  33. package/dist/shared/prompt-budget.d.ts +4 -2
  34. package/dist/testkit.cjs +157 -80
  35. package/dist/testkit.cjs.map +1 -1
  36. package/dist/testkit.js +157 -80
  37. package/dist/testkit.js.map +1 -1
  38. package/docs/integration/agent-registration-guide.md +1 -1
  39. package/docs/integration/context-engineering.md +12 -8
  40. package/docs/integration/token-management.md +6 -1
  41. package/docs/integration/tools.md +4 -0
  42. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -12,7 +12,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
 
13
13
  ---
14
14
 
15
- ## [Unreleased]
15
+ ## [0.39.0] - 2026-10-27
16
+
17
+ ### Changed
18
+
19
+ - Separate model output capacity from response reservation. With a prepared route
20
+ and no explicit response reservation, build against the effective window while
21
+ leaving at least one generation token; after measuring the complete Prompt,
22
+ bound each request by the remaining window and the model/policy output limit.
23
+ This intentionally changes the default input budget and compaction watermarks;
24
+ explicit `reservedForResponse` and standalone fallback reservations are preserved.
25
+
26
+ ### Fixed
27
+
28
+ - Accept routes whose maximum output equals their context window. Resolve the
29
+ effective budget before constructing Context Manager configuration, so inherited
30
+ response defaults cannot reject a smaller explicit window. Apply the same budget
31
+ rules to internal compaction, retries, and fallback route admission; successful
32
+ usage snapshots report the actual requested output limit.
33
+
34
+ ### Integration
35
+
36
+ - This release is based on `0.38.1` and includes only the output-budget fix.
37
+ Existing explicit response reservations and public message types are preserved.
38
+ Hosts should expect default input budgets and compaction thresholds to use the
39
+ effective window rather than subtract the model's entire output capacity.
40
+
41
+ ## [0.38.1] - 2026-10-21
42
+
43
+ ### Fixed
44
+
45
+ - Unknown top-level arguments rejected by the default `BaseTool` validator now list
46
+ allowed parameter names and uniquely close spelling suggestions without changing
47
+ arguments, echoing values, or starting tool execution.
16
48
 
17
49
  ## [0.38.0] - 2026-10-14
18
50
 
package/dist/cli.cjs CHANGED
@@ -4914,6 +4914,88 @@ function evaluatePrimaryPromptCapacity(snapshot) {
4914
4914
  };
4915
4915
  }
4916
4916
 
4917
+ // src/shared/prompt-budget.ts
4918
+ var DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS = 256e3;
4919
+ var DEFAULT_PROMPT_MAX_OUTPUT_TOKENS = 16384;
4920
+ function resolveEffectivePromptBudget(input) {
4921
+ assertOptionalPositiveSafeInteger(input.policyMaxTokens, "policyMaxTokens");
4922
+ assertOptionalPositiveSafeInteger(
4923
+ input.policyReservedForResponse,
4924
+ "policyReservedForResponse"
4925
+ );
4926
+ assertOptionalPositiveSafeInteger(
4927
+ input.modelContextWindowTokens,
4928
+ "modelContextWindowTokens"
4929
+ );
4930
+ assertOptionalPositiveSafeInteger(input.modelMaxOutputTokens, "modelMaxOutputTokens");
4931
+ assertOptionalPositiveSafeInteger(
4932
+ input.fallbackContextWindowTokens,
4933
+ "fallbackContextWindowTokens"
4934
+ );
4935
+ assertOptionalPositiveSafeInteger(
4936
+ input.fallbackMaxOutputTokens,
4937
+ "fallbackMaxOutputTokens"
4938
+ );
4939
+ assertNonNegativeSafeInteger(input.toolDefinitionTokens, "toolDefinitionTokens");
4940
+ const modelContextWindowTokens = input.modelContextWindowTokens ?? input.fallbackContextWindowTokens ?? DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS;
4941
+ const modelMaxOutputTokens = input.modelMaxOutputTokens ?? input.fallbackMaxOutputTokens ?? DEFAULT_PROMPT_MAX_OUTPUT_TOKENS;
4942
+ const effectiveWindowTokens = input.policyMaxTokens === void 0 ? modelContextWindowTokens : Math.min(input.policyMaxTokens, modelContextWindowTokens);
4943
+ const outputLimitTokens = input.policyReservedForResponse === void 0 ? modelMaxOutputTokens : Math.min(input.policyReservedForResponse, modelMaxOutputTokens);
4944
+ const responseReservationTokens = input.policyReservedForResponse !== void 0 || input.modelContextWindowTokens === void 0 ? outputLimitTokens : 1;
4945
+ const inputBudgetTokens = effectiveWindowTokens - responseReservationTokens;
4946
+ if (inputBudgetTokens < 1) {
4947
+ throw new Error(
4948
+ "\u6709\u6548\u6A21\u578B\u7A97\u53E3\u5FC5\u987B\u5927\u4E8E\u8F93\u51FA\u9884\u7559\uFF0C\u624D\u80FD\u5F62\u6210\u6B63\u6570 Prompt \u8F93\u5165\u9884\u7B97\u3002"
4949
+ );
4950
+ }
4951
+ const messageBudgetTokens = inputBudgetTokens - input.toolDefinitionTokens;
4952
+ if (messageBudgetTokens < 1) {
4953
+ throw new Error(
4954
+ "Tool definitions \u5DF2\u5360\u6EE1 Prompt \u8F93\u5165\u9884\u7B97\uFF0C\u65E0\u6CD5\u4E3A messages \u4FDD\u7559 token\u3002"
4955
+ );
4956
+ }
4957
+ return {
4958
+ effectiveWindowTokens,
4959
+ outputLimitTokens,
4960
+ inputBudgetTokens,
4961
+ toolDefinitionTokens: input.toolDefinitionTokens,
4962
+ messageBudgetTokens
4963
+ };
4964
+ }
4965
+ function assertOptionalPositiveSafeInteger(value, field) {
4966
+ if (value !== void 0) {
4967
+ assertPositiveSafeInteger(value, field);
4968
+ }
4969
+ }
4970
+ function assertPositiveSafeInteger(value, field) {
4971
+ if (!Number.isSafeInteger(value) || value < 1) {
4972
+ throw new Error(`${field} \u5FC5\u987B\u662F\u6B63\u5B89\u5168\u6574\u6570\u3002`);
4973
+ }
4974
+ }
4975
+ function assertNonNegativeSafeInteger(value, field) {
4976
+ if (!Number.isSafeInteger(value) || value < 0) {
4977
+ throw new Error(`${field} \u5FC5\u987B\u662F\u975E\u8D1F\u5B89\u5168\u6574\u6570\u3002`);
4978
+ }
4979
+ }
4980
+
4981
+ // src/runtime-kernel/graph-engine/functions/resolveFallbackPromptBudget.ts
4982
+ function resolveFallbackPromptBudget(primaryBudget, requiredOutputLimitTokens, route) {
4983
+ if (!route) throw new Error("Fallback \u6A21\u578B\u7F3A\u5C11\u6B63\u5F0F inference route\u3002");
4984
+ const budget = resolveEffectivePromptBudget({
4985
+ policyMaxTokens: primaryBudget.effectiveWindowTokens,
4986
+ policyReservedForResponse: requiredOutputLimitTokens,
4987
+ modelContextWindowTokens: route.context_window_tokens,
4988
+ modelMaxOutputTokens: route.max_output_tokens,
4989
+ toolDefinitionTokens: primaryBudget.toolDefinitionTokens
4990
+ });
4991
+ const inputBudgetTokens = Math.min(primaryBudget.inputBudgetTokens, budget.inputBudgetTokens);
4992
+ return {
4993
+ ...budget,
4994
+ inputBudgetTokens,
4995
+ messageBudgetTokens: inputBudgetTokens - budget.toolDefinitionTokens
4996
+ };
4997
+ }
4998
+
4917
4999
  // src/runtime-kernel/graph-engine/tick-pipeline/stages/executeLlmStage.ts
4918
5000
  function createExecuteLlmStage(dependencies) {
4919
5001
  return defineTickStage({
@@ -4992,7 +5074,11 @@ function createExecuteLlmStage(dependencies) {
4992
5074
  servedModelId: activeModelId,
4993
5075
  messages,
4994
5076
  llmOptions: ctx.llmOptions,
4995
- promptBudget,
5077
+ promptBudget: activeModelId === ctx.modelId ? promptBudget : resolveFallbackPromptBudget(
5078
+ promptBudget,
5079
+ ctx.promptUsageCandidate?.output_limit_tokens ?? promptBudget.outputLimitTokens,
5080
+ dependencies.modelCatalog.getModelById(activeModelId)?.inference_route
5081
+ ),
4996
5082
  measurementPolicy: promptUsageMeasurementPolicy,
4997
5083
  imageInputTokens: ctx.imageInputAdmissionEvidence?.attachments.reduce(
4998
5084
  (total, attachment) => total + attachment.estimatedTokens,
@@ -5022,7 +5108,7 @@ function createExecuteLlmStage(dependencies) {
5022
5108
  return evaluateFallbackPromptCapacity({
5023
5109
  contextWindowTokens: route?.context_window_tokens,
5024
5110
  maxOutputTokens: route?.max_output_tokens,
5025
- requiredOutputLimitTokens: promptBudget.outputLimitTokens,
5111
+ requiredOutputLimitTokens: ctx.promptUsageCandidate?.output_limit_tokens ?? promptBudget.outputLimitTokens,
5026
5112
  estimatedPromptTokens: weights.systemPromptTokens + weights.conversationTokens + weights.toolDefinitionTokens
5027
5113
  });
5028
5114
  }
@@ -5310,7 +5396,7 @@ async function executeContextCompaction(dependencies, input) {
5310
5396
  response = await dependencies.llmCaller.call(
5311
5397
  input.modelId,
5312
5398
  messages,
5313
- compactionOptions,
5399
+ { ...compactionOptions, max_tokens: compactionPromptUsage.output_limit_tokens },
5314
5400
  input.signal,
5315
5401
  { imageInputAdmissionEvidence: input.imageInputAdmissionEvidence }
5316
5402
  );
@@ -5373,7 +5459,7 @@ async function executeContextCompaction(dependencies, input) {
5373
5459
  conversationId: input.conversationId,
5374
5460
  turnId: input.turnId,
5375
5461
  timestamp: Date.now(),
5376
- maxOutputTokens
5462
+ maxOutputTokens: compactionPromptUsage.output_limit_tokens
5377
5463
  });
5378
5464
  } catch (error) {
5379
5465
  if (error instanceof Error && error.name === "AbortError") throw error;
@@ -5558,14 +5644,11 @@ function buildCompactionLlmOptions(input) {
5558
5644
  };
5559
5645
  }
5560
5646
  function buildCompactionPromptBudget(mainBudget, maxOutputTokens) {
5561
- const inputBudgetTokens = mainBudget.effectiveWindowTokens - maxOutputTokens;
5562
- return {
5563
- effectiveWindowTokens: mainBudget.effectiveWindowTokens,
5564
- outputLimitTokens: maxOutputTokens,
5565
- inputBudgetTokens,
5566
- toolDefinitionTokens: mainBudget.toolDefinitionTokens,
5567
- messageBudgetTokens: inputBudgetTokens - mainBudget.toolDefinitionTokens
5568
- };
5647
+ return resolveEffectivePromptBudget({
5648
+ modelContextWindowTokens: mainBudget.effectiveWindowTokens,
5649
+ modelMaxOutputTokens: maxOutputTokens,
5650
+ toolDefinitionTokens: mainBudget.toolDefinitionTokens
5651
+ });
5569
5652
  }
5570
5653
 
5571
5654
  // src/runtime-kernel/graph-engine/tick-pipeline/stages/compactContextStage.ts
@@ -6142,6 +6225,10 @@ function createPromptUsageMeasurer(dependencies) {
6142
6225
  });
6143
6226
  const usedTokens = remoteResult?.inputTokens ?? calibratedLocalTotal;
6144
6227
  const components = normalizePromptUsageComponents(usedTokens, weights);
6228
+ const outputLimitTokens = usedTokens <= input.promptBudget.inputBudgetTokens ? Math.min(
6229
+ input.promptBudget.outputLimitTokens,
6230
+ input.promptBudget.effectiveWindowTokens - usedTokens
6231
+ ) : input.promptBudget.outputLimitTokens;
6145
6232
  return ContextUsageSnapshot.parse({
6146
6233
  basis: "last_completed_llm_prompt",
6147
6234
  budget_model_id: input.budgetModelId,
@@ -6151,7 +6238,7 @@ function createPromptUsageMeasurer(dependencies) {
6151
6238
  component_attribution: "normalized_local_estimate",
6152
6239
  input_budget_tokens: input.promptBudget.inputBudgetTokens,
6153
6240
  remaining_tokens: input.promptBudget.inputBudgetTokens - usedTokens,
6154
- output_limit_tokens: input.promptBudget.outputLimitTokens,
6241
+ output_limit_tokens: outputLimitTokens,
6155
6242
  source: remoteResult?.source ?? "local-estimate",
6156
6243
  confidence: remoteResult?.confidence ?? "estimate",
6157
6244
  measured_at: now()
@@ -9579,69 +9666,6 @@ function createHostToolCallBootstrap(input) {
9579
9666
  }
9580
9667
  };
9581
9668
  }
9582
-
9583
- // src/shared/prompt-budget.ts
9584
- var DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS = 256e3;
9585
- var DEFAULT_PROMPT_MAX_OUTPUT_TOKENS = 16384;
9586
- function resolveEffectivePromptBudget(input) {
9587
- assertOptionalPositiveSafeInteger(input.policyMaxTokens, "policyMaxTokens");
9588
- assertOptionalPositiveSafeInteger(
9589
- input.policyReservedForResponse,
9590
- "policyReservedForResponse"
9591
- );
9592
- assertOptionalPositiveSafeInteger(
9593
- input.modelContextWindowTokens,
9594
- "modelContextWindowTokens"
9595
- );
9596
- assertOptionalPositiveSafeInteger(input.modelMaxOutputTokens, "modelMaxOutputTokens");
9597
- assertOptionalPositiveSafeInteger(
9598
- input.fallbackContextWindowTokens,
9599
- "fallbackContextWindowTokens"
9600
- );
9601
- assertOptionalPositiveSafeInteger(
9602
- input.fallbackMaxOutputTokens,
9603
- "fallbackMaxOutputTokens"
9604
- );
9605
- assertNonNegativeSafeInteger(input.toolDefinitionTokens, "toolDefinitionTokens");
9606
- const modelContextWindowTokens = input.modelContextWindowTokens ?? input.fallbackContextWindowTokens ?? DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS;
9607
- const modelMaxOutputTokens = input.modelMaxOutputTokens ?? input.fallbackMaxOutputTokens ?? DEFAULT_PROMPT_MAX_OUTPUT_TOKENS;
9608
- const effectiveWindowTokens = input.policyMaxTokens === void 0 ? modelContextWindowTokens : Math.min(input.policyMaxTokens, modelContextWindowTokens);
9609
- const outputLimitTokens = input.policyReservedForResponse === void 0 ? modelMaxOutputTokens : Math.min(input.policyReservedForResponse, modelMaxOutputTokens);
9610
- const inputBudgetTokens = effectiveWindowTokens - outputLimitTokens;
9611
- if (inputBudgetTokens < 1) {
9612
- throw new Error(
9613
- "\u6709\u6548\u6A21\u578B\u7A97\u53E3\u5FC5\u987B\u5927\u4E8E\u8F93\u51FA\u9884\u7559\uFF0C\u624D\u80FD\u5F62\u6210\u6B63\u6570 Prompt \u8F93\u5165\u9884\u7B97\u3002"
9614
- );
9615
- }
9616
- const messageBudgetTokens = inputBudgetTokens - input.toolDefinitionTokens;
9617
- if (messageBudgetTokens < 1) {
9618
- throw new Error(
9619
- "Tool definitions \u5DF2\u5360\u6EE1 Prompt \u8F93\u5165\u9884\u7B97\uFF0C\u65E0\u6CD5\u4E3A messages \u4FDD\u7559 token\u3002"
9620
- );
9621
- }
9622
- return {
9623
- effectiveWindowTokens,
9624
- outputLimitTokens,
9625
- inputBudgetTokens,
9626
- toolDefinitionTokens: input.toolDefinitionTokens,
9627
- messageBudgetTokens
9628
- };
9629
- }
9630
- function assertOptionalPositiveSafeInteger(value, field) {
9631
- if (value !== void 0) {
9632
- assertPositiveSafeInteger(value, field);
9633
- }
9634
- }
9635
- function assertPositiveSafeInteger(value, field) {
9636
- if (!Number.isSafeInteger(value) || value < 1) {
9637
- throw new Error(`${field} \u5FC5\u987B\u662F\u6B63\u5B89\u5168\u6574\u6570\u3002`);
9638
- }
9639
- }
9640
- function assertNonNegativeSafeInteger(value, field) {
9641
- if (!Number.isSafeInteger(value) || value < 0) {
9642
- throw new Error(`${field} \u5FC5\u987B\u662F\u975E\u8D1F\u5B89\u5168\u6574\u6570\u3002`);
9643
- }
9644
- }
9645
9669
  var counter = zod.z.number().int().nonnegative();
9646
9670
  var executor = zod.z.object({
9647
9671
  stepCount: counter,
@@ -11213,7 +11237,15 @@ async function callWithRetryFallback(params) {
11213
11237
  eventHandler,
11214
11238
  requirement
11215
11239
  });
11216
- const attemptContextUsage = invocationContext?.measurePromptUsage ? await invocationContext.measurePromptUsage(activeModelId, messages) : void 0;
11240
+ const measuredContextUsage = invocationContext?.measurePromptUsage ? await invocationContext.measurePromptUsage(activeModelId, messages) : void 0;
11241
+ const attemptContextUsage = measuredContextUsage ? {
11242
+ ...measuredContextUsage,
11243
+ output_limit_tokens: Math.min(
11244
+ measuredContextUsage.output_limit_tokens,
11245
+ options.max_tokens ?? measuredContextUsage.output_limit_tokens
11246
+ )
11247
+ } : void 0;
11248
+ const attemptOptions = attemptContextUsage ? { ...options, max_tokens: attemptContextUsage.output_limit_tokens } : options;
11217
11249
  const isRetry = attempt > 0;
11218
11250
  let pendingErrorEvent = null;
11219
11251
  let streamedErrorClassification;
@@ -11230,7 +11262,7 @@ async function callWithRetryFallback(params) {
11230
11262
  inferencePort: deps.inferencePort,
11231
11263
  modelId: activeModelId,
11232
11264
  messages: resolvedMessages,
11233
- options,
11265
+ options: attemptOptions,
11234
11266
  eventHandler: attemptTracker.handle,
11235
11267
  onErrorClassification(classification) {
11236
11268
  streamedErrorClassification = classification;
@@ -11242,7 +11274,7 @@ async function callWithRetryFallback(params) {
11242
11274
  deps.inferencePort,
11243
11275
  activeModelId,
11244
11276
  resolvedMessages,
11245
- options,
11277
+ attemptOptions,
11246
11278
  signal,
11247
11279
  traceId
11248
11280
  );