@linnlabs/linnkit 0.10.0 → 0.21.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 (91) hide show
  1. package/CHANGELOG.md +156 -1
  2. package/dist/{agent-invocation-BHcNfrBV.d.cts → agent-invocation-CAlm9qOw.d.cts} +1 -1
  3. package/dist/{agent-invocation-BznDaXDs.d.ts → agent-invocation-DKoEDLkl.d.ts} +1 -1
  4. package/dist/{ai-engine.types-vZRnQcJa.d.ts → ai-engine.types-CeLoM624.d.cts} +9 -1
  5. package/dist/{ai-engine.types-BpeU_XQG.d.cts → ai-engine.types-LqmnfR2W.d.ts} +9 -1
  6. package/dist/{audit-CtcfART1.d.ts → audit-BuWHoMEr.d.ts} +4 -2
  7. package/dist/{audit-LeOrm2hX.d.cts → audit-DnzNQzwq.d.cts} +4 -2
  8. package/dist/cli.cjs +565 -194
  9. package/dist/cli.cjs.map +1 -1
  10. package/dist/cli.js +565 -194
  11. package/dist/cli.js.map +1 -1
  12. package/dist/context-manager.cjs +590 -26
  13. package/dist/context-manager.cjs.map +1 -1
  14. package/dist/context-manager.d.cts +84 -7
  15. package/dist/context-manager.d.ts +84 -7
  16. package/dist/context-manager.js +590 -26
  17. package/dist/context-manager.js.map +1 -1
  18. package/dist/{context-trace-BHKDS-eq.d.cts → context-trace-DrtvKac4.d.cts} +36 -3
  19. package/dist/{context-trace-CHbqHmyE.d.ts → context-trace-WV1HAZeB.d.ts} +36 -3
  20. package/dist/contracts.cjs +222 -1
  21. package/dist/contracts.cjs.map +1 -1
  22. package/dist/contracts.d.cts +5 -3
  23. package/dist/contracts.d.ts +5 -3
  24. package/dist/contracts.js +202 -2
  25. package/dist/contracts.js.map +1 -1
  26. package/dist/{defaultGraphExecutor-C2E59v_R.d.cts → defaultGraphExecutor-L1ri3oij.d.cts} +35 -7
  27. package/dist/{defaultGraphExecutor-B29_qTHy.d.ts → defaultGraphExecutor-QzzdudLQ.d.ts} +35 -7
  28. package/dist/{index-BAaUP9yU.d.cts → index-BeQO0rx2.d.cts} +328 -263
  29. package/dist/{index-Cvr23YCl.d.cts → index-CDofGr_4.d.ts} +24 -6
  30. package/dist/{index-CKQzzZ5Y.d.cts → index-Crl5E4eF.d.cts} +26 -3
  31. package/dist/{index-C0DAjsdX.d.ts → index-DWGKvssS.d.ts} +2 -2
  32. package/dist/{index-D0mKxTR5.d.ts → index-X5lYU1Lj.d.ts} +26 -3
  33. package/dist/{index-DDzuSb0n.d.ts → index-uCNCpJBg.d.cts} +24 -6
  34. package/dist/{index-BaVpVNi2.d.ts → index-wOZD-p2-.d.ts} +328 -263
  35. package/dist/{index-BnYCS8Zg.d.cts → index-wXVAHZ0D.d.cts} +2 -2
  36. package/dist/index.cjs +893 -312
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +17 -14
  39. package/dist/index.d.ts +17 -14
  40. package/dist/index.js +893 -312
  41. package/dist/index.js.map +1 -1
  42. package/dist/{messages-XthmnHZ3.d.cts → messages-Br5SiLD3.d.cts} +706 -1
  43. package/dist/{messages-XthmnHZ3.d.ts → messages-Br5SiLD3.d.ts} +706 -1
  44. package/dist/{ports-s-tSp3sB.d.cts → ports-Dugizzje.d.cts} +4 -0
  45. package/dist/{ports-DpPTFhSd.d.ts → ports-fhp84x_C.d.ts} +4 -0
  46. package/dist/ports.d.cts +8 -5
  47. package/dist/ports.d.ts +8 -5
  48. package/dist/quickstart.cjs +564 -193
  49. package/dist/quickstart.cjs.map +1 -1
  50. package/dist/quickstart.d.cts +9 -8
  51. package/dist/quickstart.d.ts +9 -8
  52. package/dist/quickstart.js +564 -193
  53. package/dist/quickstart.js.map +1 -1
  54. package/dist/{runAgent-C6F-399C.d.ts → runAgent-BmMSju8I.d.ts} +4 -4
  55. package/dist/{runAgent-ilEj66Ik.d.cts → runAgent-CKgcRSRH.d.cts} +4 -4
  56. package/dist/{runHandle-BNOqS-Bl.d.cts → runHandle-DH4VmQx6.d.cts} +4 -2
  57. package/dist/{runHandle-BdLXOFqF.d.ts → runHandle-o3p06Nva.d.ts} +4 -2
  58. package/dist/runtime-kernel/events.cjs +201 -1
  59. package/dist/runtime-kernel/events.cjs.map +1 -1
  60. package/dist/runtime-kernel/events.d.cts +3 -3
  61. package/dist/runtime-kernel/events.d.ts +3 -3
  62. package/dist/runtime-kernel/events.js +201 -1
  63. package/dist/runtime-kernel/events.js.map +1 -1
  64. package/dist/runtime-kernel.cjs +824 -264
  65. package/dist/runtime-kernel.cjs.map +1 -1
  66. package/dist/runtime-kernel.d.cts +14 -12
  67. package/dist/runtime-kernel.d.ts +14 -12
  68. package/dist/runtime-kernel.js +816 -265
  69. package/dist/runtime-kernel.js.map +1 -1
  70. package/dist/testkit.cjs +847 -264
  71. package/dist/testkit.cjs.map +1 -1
  72. package/dist/testkit.d.cts +14 -12
  73. package/dist/testkit.d.ts +14 -12
  74. package/dist/testkit.js +847 -264
  75. package/dist/testkit.js.map +1 -1
  76. package/dist/{agentSpec-Du4Iye0q.d.cts → token-accounting-CwpSPg4y.d.cts} +1265 -1
  77. package/dist/{agentSpec-Du4Iye0q.d.ts → token-accounting-CwpSPg4y.d.ts} +1265 -1
  78. package/dist/token-calibration-Cxv6dDYF.d.cts +327 -0
  79. package/dist/token-calibration-Cxv6dDYF.d.ts +327 -0
  80. package/dist/token-counter-CFfP8PQb.d.cts +26 -0
  81. package/dist/token-counter-SFyyTeaG.d.ts +26 -0
  82. package/dist/token-usage-CqwT7-5K.d.cts +133 -0
  83. package/dist/token-usage-CqwT7-5K.d.ts +133 -0
  84. package/dist/{tokenizer-DFL4I7-I.d.ts → tokenizer-BFJNCHFX.d.ts} +1 -1
  85. package/dist/{tokenizer-DH_JXv-H.d.cts → tokenizer-DoGsBE3A.d.cts} +1 -1
  86. package/docs/README.md +1 -1
  87. package/docs/integration/README.md +2 -2
  88. package/docs/integration/agent-registration-guide.md +2 -0
  89. package/docs/integration/context-engineering.md +53 -1
  90. package/docs/integration/context-fences.md +12 -0
  91. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -14,13 +14,168 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
14
 
15
15
  ## [Unreleased]
16
16
 
17
+ ## [0.21.0] - 2026-06-20
18
+
19
+ ### Added
20
+
21
+ - `GraphExecutorContextBuildOutput` can now carry context token components and context component ledger entries.
22
+ - `context_build` telemetry events now accept `tokenComponents` and `tokenLedgerEntry`.
23
+ - The build context stage creates a `context-component` ledger entry from kept context components and emits it with context build telemetry.
24
+
25
+ ### Compatibility
26
+
27
+ - Minor bump because runtime-kernel context builder and telemetry contracts expose new optional context component accounting fields.
28
+
29
+ ## [0.20.0] - 2026-06-20
30
+
31
+ ### Added
32
+
33
+ - Context trace now records build-time `ContextTokenComponent[]` for final message states when token breakdown tracing is enabled.
34
+ - Added context token component derivation for system, user, assistant, tool, fence, context injection, and history summary messages.
35
+ - Tool components with execution-time observation truncation metadata now expose build-time `originalTokensEstimate` and `droppedTokensEstimate`.
36
+
37
+ ### Compatibility
38
+
39
+ - Minor bump because `@linnlabs/linnkit/context-manager` context traces expose new token component breakdown data.
40
+
41
+ ## [0.19.0] - 2026-06-20
42
+
43
+ ### Added
44
+
45
+ - `ContextTokenComponent` can now carry execution-time tool observation truncation estimates: `truncatedAtExecution`, `originalTokensEstimate`, and `droppedTokensEstimate`.
46
+
47
+ ### Compatibility
48
+
49
+ - Minor bump because `@linnlabs/linnkit/contracts` exposes new optional context component accounting fields.
50
+
51
+ ## [0.18.0] - 2026-06-20
52
+
53
+ ### Added
54
+
55
+ - Added `ObservationTruncationMeta` in `@linnlabs/linnkit/contracts` for execution-time tool observation truncation character metrics.
56
+ - `ObservationPreviewResult` can now return optional `originalChars`, `previewChars`, `originalLines`, and `previewLines` when a host truncates and stores a tool observation.
57
+ - `ToolNode` writes execution-time observation truncation metrics to `tool_output.metadata.observationTruncation`; token estimation remains deferred to context build.
58
+
59
+ ### Compatibility
60
+
61
+ - Minor bump because `@linnlabs/linnkit/contracts` and runtime-kernel tool ports expose new optional observation truncation metadata.
62
+
63
+ ## [0.17.0] - 2026-06-19
64
+
65
+ ### Added
66
+
67
+ - Added `ContextBuildTokenEstimate` from `@linnlabs/linnkit/contracts` to expose build-time local and calibrated context token estimates.
68
+ - `GraphExecutorContextBuildOutput` can now carry `tokenEstimate`, and runtime-kernel emits a `context_build` telemetry event when it is present.
69
+ - Exported pure `addUsageTotals` and `addLedgerAggregate` helpers from `@linnlabs/linnkit/runtime-kernel` for host-side collectors.
70
+
71
+ ### Changed
72
+
73
+ - `contextPolicy.tokenEstimation.calibration` now supports `minCoefficient`; the default lower bound is `1` to avoid under-budgeting from noisy samples.
74
+
75
+ ### Compatibility
76
+
77
+ - Minor bump because `@linnlabs/linnkit/contracts`, `@linnlabs/linnkit/runtime-kernel`, and telemetry event types expose new public APIs.
78
+
79
+ ## [0.16.0] - 2026-06-18
80
+
81
+ ### Added
82
+
83
+ - Added the optional `TokenCounterPort` from `@linnlabs/linnkit/ports` for route-aware preflight token counts.
84
+ - `contextPolicy.tokenEstimation.remoteCount` can now opt into remote preflight counting during context build.
85
+ - `AgentMessageOrchestrator` accepts a host-supplied `tokenCounter` and `resolveTokenRoute` hook without changing default behavior.
86
+ - `ContextTrace` records whether remote count was enabled, attempted, applied, and which route was used.
87
+
88
+ ### Changed
89
+
90
+ - Remote count is only attempted when the policy is enabled, a counter is injected, and the current `TokenRoute` explicitly declares `supportsRemoteTokenCount`.
91
+ - `mergeContextPolicy` now merges nested token estimation calibration and remote count policies field-by-field.
92
+
93
+ ### Compatibility
94
+
95
+ - Minor bump because `@linnlabs/linnkit/ports` and `@linnlabs/linnkit/context-manager` expose new public token counting APIs.
96
+
97
+ ## [0.15.0] - 2026-06-18
98
+
99
+ ### Added
100
+
101
+ - Added opt-in token usage calibration contracts from `@linnlabs/linnkit/contracts` for route-scoped actual-usage samples and auditable calibration traces.
102
+ - `AgentMessageOrchestrator` can now receive route-scoped calibration samples for context budgeting, while default behavior remains unchanged.
103
+ - `ContextTrace` now records token calibration status, coefficient, sample count, and sample ledger entry IDs when trace is enabled.
104
+
105
+ ### Changed
106
+
107
+ - Context token estimates only apply calibration when `contextPolicy.tokenEstimation.calibration.enabled` is true and enough same-route actual samples are available.
108
+
109
+ ### Compatibility
110
+
111
+ - Minor bump because `@linnlabs/linnkit/contracts` and `@linnlabs/linnkit/context-manager` expose new public token calibration APIs.
112
+
113
+ ## [0.14.0] - 2026-06-18
114
+
115
+ ### Changed
116
+
117
+ - `RunCost` now uses `tokenUsage` as the canonical run-level token aggregate and no longer exposes the ambiguous single-call `canonicalUsage` field.
118
+ - Legacy `RunCost.tokensInput` and `RunCost.tokensOutput` remain for compatibility and should be read as projections from the run-level token aggregate.
119
+
120
+ ### Compatibility
121
+
122
+ - Minor bump because `@linnlabs/linnkit/runtime-kernel` changes the public `RunCost` shape by removing the ambiguous `canonicalUsage` field.
123
+
124
+ ## [0.13.0] - 2026-06-18
125
+
126
+ ### Added
127
+
128
+ - Added `TokenPricing`, `CostBreakdown`, and `TokenCostComponent` contracts for host-supplied effective per-million token prices.
129
+ - Added pure `computeCost(usage, pricing)` from `@linnlabs/linnkit/runtime-kernel` for input/output/reasoning/cache cost breakdowns.
130
+
131
+ ### Changed
132
+
133
+ - `computeCost` reports `status: 'unknown'` when a required price is missing instead of treating missing prices as zero.
134
+
135
+ ### Compatibility
136
+
137
+ - Minor bump because `@linnlabs/linnkit/contracts` and `@linnlabs/linnkit/runtime-kernel` expose new public cost accounting APIs.
138
+
139
+ ## [0.12.0] - 2026-06-18
140
+
141
+ ### Added
142
+
143
+ - Added token ledger contracts from `@linnlabs/linnkit/contracts`: `TokenLedgerEntry`, `LlmUsageTokenLedgerEntry`, `ContextComponentTokenLedgerEntry`, `ContextTokenComponent`, `TokenUsageTotals`, `TokenLedgerAggregate`, and `RunTokenUsageAggregate`.
144
+ - Added `tokenAccounting` helpers from `@linnlabs/linnkit/runtime-kernel` for pure canonical usage aggregation, ledger entry creation, and parent/child run token aggregation without double counting.
145
+ - `llm_call` telemetry and `RunCost` can now carry optional token ledger references/aggregates while preserving the existing prompt/completion fields.
146
+
147
+ ### Changed
148
+
149
+ - `ContextTrace` now has optional token ledger sidecars for component breakdowns without depending on runtime-kernel internals.
150
+
151
+ ### Compatibility
152
+
153
+ - Minor bump because `@linnlabs/linnkit/contracts` and `@linnlabs/linnkit/runtime-kernel` expose new public token accounting APIs.
154
+
155
+ ## [0.11.0] - 2026-06-18
156
+
157
+ ### Added
158
+
159
+ - Added token usage contracts from `@linnlabs/linnkit/contracts`: `CanonicalLlmUsage`, `TokenRoute`, `TokenRouteCapabilities`, `TokenCountSource`, and `TokenCountConfidence`.
160
+ - `llm_call` telemetry now carries canonical usage alongside the legacy prompt/completion token shape when provider usage can be normalized.
161
+ - `AgentAiEngine` responses can now return `canonicalUsage`, and `@linnlabs/linnkit/ports` exposes the optional `UsageNormalizer` type for hosts that centralize usage mapping.
162
+
163
+ ### Changed
164
+
165
+ - Local LLM telemetry estimates now use the injected `TokenizerPort` instead of calling `TokenCalculator` directly.
166
+ - Legacy `NormalizedLlmUsage` no longer fabricates prompt/completion tokens from a total-only `usage.tokens` payload; unknown input/output stays unknown and falls back to local estimates.
167
+ - OpenAI-compatible default usage normalization now splits cached input tokens out of `prompt_tokens` and preserves `completion_tokens_details.reasoning_tokens` when reported.
168
+
169
+ ### Compatibility
170
+
171
+ - Minor bump because `@linnlabs/linnkit/contracts` exposes new public runtime schemas and the `AgentAiEngine` port accepts canonical usage metadata.
172
+
17
173
  ## [0.10.0] - 2026-06-15
18
174
 
19
175
  ### Changed
20
176
 
21
177
  - `GraphExecutor` / `Checkpointer` now name engine-state snapshot keys as `checkpointKey`, and graph telemetry reads the runtime `conversationId` from graph local state instead of treating the checkpoint key as a host conversation.
22
178
  - Synchronous child runs can now receive an explicit host `conversationId` while still using an internal checkpoint key for GraphExecutor state isolation. This keeps RuntimeEvent / Audit / Telemetry scope aligned with the child run registered by the host and prevents EventStore-backed audit writes from mixing a child `runId` with an internal conversation key.
23
- - `copyToolContextRuntimeCapability` is now exported from `@linnlabs/linnkit/runtime-kernel` so child-run tool contexts can preserve the parent tool runtime binding without manually copying reserved fields.
24
179
  - Detached runs now execute against the `AgentSpec`, request, and metadata snapshots captured during `spawnDetached()` registration, so later caller-side object mutations cannot change the background run context.
25
180
 
26
181
  ### Compatibility
@@ -1,4 +1,4 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.cjs';
1
+ import { A as AiMessage } from './messages-Br5SiLD3.cjs';
2
2
 
3
3
  /**
4
4
  * Agent runtime 最小调用协议。
@@ -1,4 +1,4 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.js';
1
+ import { A as AiMessage } from './messages-Br5SiLD3.js';
2
2
 
3
3
  /**
4
4
  * Agent runtime 最小调用协议。
@@ -1,4 +1,5 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.js';
1
+ import { A as AiMessage } from './messages-Br5SiLD3.cjs';
2
+ import { C as CanonicalLlmUsage } from './token-usage-CqwT7-5K.cjs';
2
3
 
3
4
  /**
4
5
  * @file packages/linnkit/src/ports/ai-engine.types.ts
@@ -103,6 +104,13 @@ interface LlmResponseContent {
103
104
  * - 不同 provider 字段可能不同(例如 mock 返回 usage.tokens),上层应做归一化处理。
104
105
  */
105
106
  usage?: unknown;
107
+ /**
108
+ * 已归一化的 token 用量。
109
+ *
110
+ * host adapter 如果已经理解 provider usage 字段,应直接回传这个结构;
111
+ * linnkit 会优先使用它,避免从 raw usage 里猜 provider family。
112
+ */
113
+ canonicalUsage?: CanonicalLlmUsage;
106
114
  }
107
115
  interface LlmCallOptions {
108
116
  tools?: unknown[];
@@ -1,4 +1,5 @@
1
- import { A as AiMessage } from './messages-XthmnHZ3.cjs';
1
+ import { A as AiMessage } from './messages-Br5SiLD3.js';
2
+ import { C as CanonicalLlmUsage } from './token-usage-CqwT7-5K.js';
2
3
 
3
4
  /**
4
5
  * @file packages/linnkit/src/ports/ai-engine.types.ts
@@ -103,6 +104,13 @@ interface LlmResponseContent {
103
104
  * - 不同 provider 字段可能不同(例如 mock 返回 usage.tokens),上层应做归一化处理。
104
105
  */
105
106
  usage?: unknown;
107
+ /**
108
+ * 已归一化的 token 用量。
109
+ *
110
+ * host adapter 如果已经理解 provider usage 字段,应直接回传这个结构;
111
+ * linnkit 会优先使用它,避免从 raw usage 里猜 provider family。
112
+ */
113
+ canonicalUsage?: CanonicalLlmUsage;
106
114
  }
107
115
  interface LlmCallOptions {
108
116
  tools?: unknown[];
@@ -1,10 +1,12 @@
1
- import { a as LlmRequestMessage, L as LlmCallOptions, d as ToolCallChunk, P as ProviderReasoningDetails } from './ai-engine.types-vZRnQcJa.js';
1
+ import { C as CanonicalLlmUsage } from './token-usage-CqwT7-5K.js';
2
+ import { a as LlmRequestMessage, L as LlmCallOptions, d as ToolCallChunk, P as ProviderReasoningDetails } from './ai-engine.types-LqmnfR2W.js';
2
3
  import { e as AuditEnvelope } from './audit-BaRUGaqv.js';
3
4
 
4
5
  type AgentAiEngineStreamContent = string | {
5
6
  content?: string;
6
7
  tool_calls?: ToolCallChunk[];
7
8
  reasoning_details?: ProviderReasoningDetails;
9
+ canonicalUsage?: CanonicalLlmUsage;
8
10
  };
9
11
  interface AgentAiEngine {
10
12
  chatCompletion(modelId: string, messages: LlmRequestMessage[], options?: LlmCallOptions & {
@@ -15,7 +17,7 @@ interface AgentAiEngine {
15
17
  stream_options?: {
16
18
  include_usage?: boolean;
17
19
  };
18
- }, onContent?: (content: AgentAiEngineStreamContent) => void, onError?: (error: Error) => void, onFinish?: (reason: string) => void, onThought?: (thought: string) => void, onUsage?: (usage: unknown) => void): Promise<void>;
20
+ }, onContent?: (content: AgentAiEngineStreamContent) => void, onError?: (error: Error) => void, onFinish?: (reason: string) => void, onThought?: (thought: string) => void, onUsage?: (usage: unknown) => void, onCanonicalUsage?: (usage: CanonicalLlmUsage) => void): Promise<void>;
19
21
  }
20
22
 
21
23
  /**
@@ -1,10 +1,12 @@
1
- import { a as LlmRequestMessage, L as LlmCallOptions, d as ToolCallChunk, P as ProviderReasoningDetails } from './ai-engine.types-BpeU_XQG.cjs';
1
+ import { C as CanonicalLlmUsage } from './token-usage-CqwT7-5K.cjs';
2
+ import { a as LlmRequestMessage, L as LlmCallOptions, d as ToolCallChunk, P as ProviderReasoningDetails } from './ai-engine.types-CeLoM624.cjs';
2
3
  import { e as AuditEnvelope } from './audit-BaRUGaqv.cjs';
3
4
 
4
5
  type AgentAiEngineStreamContent = string | {
5
6
  content?: string;
6
7
  tool_calls?: ToolCallChunk[];
7
8
  reasoning_details?: ProviderReasoningDetails;
9
+ canonicalUsage?: CanonicalLlmUsage;
8
10
  };
9
11
  interface AgentAiEngine {
10
12
  chatCompletion(modelId: string, messages: LlmRequestMessage[], options?: LlmCallOptions & {
@@ -15,7 +17,7 @@ interface AgentAiEngine {
15
17
  stream_options?: {
16
18
  include_usage?: boolean;
17
19
  };
18
- }, onContent?: (content: AgentAiEngineStreamContent) => void, onError?: (error: Error) => void, onFinish?: (reason: string) => void, onThought?: (thought: string) => void, onUsage?: (usage: unknown) => void): Promise<void>;
20
+ }, onContent?: (content: AgentAiEngineStreamContent) => void, onError?: (error: Error) => void, onFinish?: (reason: string) => void, onThought?: (thought: string) => void, onUsage?: (usage: unknown) => void, onCanonicalUsage?: (usage: CanonicalLlmUsage) => void): Promise<void>;
19
21
  }
20
22
 
21
23
  /**