@t2000/engine 0.42.0 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -625,6 +625,18 @@ interface Tool<TInput = unknown, TOutput = unknown> {
625
625
  maxResultSizeChars?: number;
626
626
  /** Custom truncation strategy. Falls back to generic slice + hint when omitted. */
627
627
  summarizeOnTruncate?: (result: string, maxChars: number) => string;
628
+ /**
629
+ * [v1.5.1] Whether `microcompact` may dedupe this tool's results across
630
+ * multiple calls with identical input. Default `true` — most tools are
631
+ * effectively pure within a session (price lookups, protocol info,
632
+ * yield pools). Set to `false` for tools whose result depends on
633
+ * mutable on-chain state and therefore changes after writes
634
+ * (`balance_check`, `savings_info`, `health_check`,
635
+ * `transaction_history`). Non-cacheable tools are excluded from the
636
+ * `seen` map entirely, so neither this call nor any later call with
637
+ * the same input gets replaced with a "[Same result …]" back-reference.
638
+ */
639
+ cacheable?: boolean;
628
640
  }
629
641
  type ThinkingEffort = 'low' | 'medium' | 'high' | 'max';
630
642
  type ThinkingConfig = {
@@ -945,6 +957,11 @@ interface BuildToolOptions<TInput, TOutput> {
945
957
  preflight?: (input: TInput) => PreflightResult;
946
958
  maxResultSizeChars?: number;
947
959
  summarizeOnTruncate?: (result: string, maxChars: number) => string;
960
+ /**
961
+ * [v1.5.1] See `Tool.cacheable`. Default `true`. Set `false` for
962
+ * tools whose results depend on mutable on-chain state.
963
+ */
964
+ cacheable?: boolean;
948
965
  }
949
966
  declare function buildTool<TInput, TOutput>(opts: BuildToolOptions<TInput, TOutput>): Tool<TInput, TOutput>;
950
967
  declare function toolsToDefinitions(tools: Tool[]): {
@@ -1180,11 +1197,23 @@ interface MicrocompactResult extends Array<Message> {
1180
1197
  * the full prior result with a compact back-reference. Runs before any
1181
1198
  * LLM-based compaction and costs nothing.
1182
1199
  *
1200
+ * [v1.5.1] Tools may opt out of dedupe by setting `cacheable: false` on
1201
+ * their `Tool` definition. Non-cacheable tools (e.g. `balance_check`,
1202
+ * `savings_info`, `health_check`, `transaction_history`) are excluded
1203
+ * from the `seen` map entirely, so neither the current call nor any
1204
+ * later call with identical inputs gets replaced — necessary because
1205
+ * their results depend on mutable on-chain state that writes invalidate.
1206
+ *
1183
1207
  * Returns a new array — does not mutate the input. The returned array
1184
1208
  * carries a `dedupedToolUseIds` property listing every tool-use ID whose
1185
1209
  * tool_result block was replaced with a back-reference this pass.
1210
+ *
1211
+ * @param messages — conversation ledger to compact.
1212
+ * @param tools — optional tool registry consulted to resolve the
1213
+ * per-tool `cacheable` flag. Omit to dedupe every
1214
+ * tool (legacy behavior — back-compat).
1186
1215
  */
1187
- declare function microcompact(messages: readonly Message[]): MicrocompactResult;
1216
+ declare function microcompact(messages: readonly Message[], tools?: readonly Tool[]): MicrocompactResult;
1188
1217
 
1189
1218
  /**
1190
1219
  * EarlyToolDispatcher — dispatches read-only tools mid-stream.
package/dist/index.js CHANGED
@@ -23,7 +23,8 @@ function buildTool(opts) {
23
23
  flags: opts.flags ?? {},
24
24
  preflight: opts.preflight,
25
25
  maxResultSizeChars: opts.maxResultSizeChars,
26
- summarizeOnTruncate: opts.summarizeOnTruncate
26
+ summarizeOnTruncate: opts.summarizeOnTruncate,
27
+ cacheable: opts.cacheable
27
28
  };
28
29
  }
29
30
  function toolsToDefinitions(tools) {
@@ -557,6 +558,10 @@ var balanceCheckTool = buildTool({
557
558
  inputSchema: z.object({}),
558
559
  jsonSchema: { type: "object", properties: {}, required: [] },
559
560
  isReadOnly: true,
561
+ // [v1.5.1] Wallet contents change after every send/swap/save/etc.
562
+ // Microcompact must NEVER dedupe these calls — each one reflects a
563
+ // different on-chain state.
564
+ cacheable: false,
560
565
  async call(_input, context) {
561
566
  if (hasNaviMcp(context)) {
562
567
  const address = getWalletAddress(context);
@@ -850,6 +855,9 @@ var savingsInfoTool = buildTool({
850
855
  inputSchema: z.object({}),
851
856
  jsonSchema: { type: "object", properties: {}, required: [] },
852
857
  isReadOnly: true,
858
+ // [v1.5.1] NAVI deposits change on save_deposit / withdraw / claim.
859
+ // Each call reflects a fresh on-chain snapshot — never dedupe.
860
+ cacheable: false,
853
861
  async call(_input, context) {
854
862
  if (context.positionFetcher && context.walletAddress) {
855
863
  const sp = await context.positionFetcher(context.walletAddress);
@@ -910,6 +918,9 @@ var healthCheckTool = buildTool({
910
918
  inputSchema: z.object({}),
911
919
  jsonSchema: { type: "object", properties: {}, required: [] },
912
920
  isReadOnly: true,
921
+ // [v1.5.1] Health factor changes on every borrow / repay / collateral
922
+ // movement and even passively as oracle prices update. Never dedupe.
923
+ cacheable: false,
913
924
  async call(_input, context) {
914
925
  if (context.positionFetcher && context.walletAddress) {
915
926
  const sp = await context.positionFetcher(context.walletAddress);
@@ -1151,6 +1162,10 @@ var transactionHistoryTool = buildTool({
1151
1162
  },
1152
1163
  isReadOnly: true,
1153
1164
  maxResultSizeChars: 8e3,
1165
+ // [v1.5.1] New transactions land continuously. Even with an explicit
1166
+ // `date` filter the dedupe is wrong post-write because the just-
1167
+ // executed write may now be in history. Never dedupe.
1168
+ cacheable: false,
1154
1169
  async call(input, context) {
1155
1170
  const limit = input.limit ?? 10;
1156
1171
  const action = input.action;
@@ -3724,15 +3739,23 @@ function extractConversationText(messages) {
3724
3739
  }
3725
3740
 
3726
3741
  // src/compact/microcompact.ts
3727
- function microcompact(messages) {
3742
+ function microcompact(messages, tools) {
3728
3743
  const seen = /* @__PURE__ */ new Map();
3729
3744
  let toolUseIndex = 0;
3730
3745
  const toolUseInputs = /* @__PURE__ */ new Map();
3746
+ const cacheableByName = /* @__PURE__ */ new Map();
3747
+ if (tools) {
3748
+ for (const t of tools) {
3749
+ cacheableByName.set(t.name, t.cacheable ?? true);
3750
+ }
3751
+ }
3731
3752
  const dedupedToolUseIds = /* @__PURE__ */ new Set();
3753
+ const toolNameById = /* @__PURE__ */ new Map();
3732
3754
  for (const msg of messages) {
3733
3755
  for (const block of msg.content) {
3734
3756
  if (block.type === "tool_use") {
3735
3757
  toolUseInputs.set(block.id, `${block.name}:${stableStringify(block.input)}`);
3758
+ toolNameById.set(block.id, block.name);
3736
3759
  }
3737
3760
  }
3738
3761
  }
@@ -3744,6 +3767,11 @@ function microcompact(messages) {
3744
3767
  if (block.type !== "tool_result") return block;
3745
3768
  const key = toolUseInputs.get(block.toolUseId);
3746
3769
  if (!key) return block;
3770
+ const toolName = toolNameById.get(block.toolUseId);
3771
+ if (toolName && cacheableByName.get(toolName) === false) {
3772
+ toolUseIndex++;
3773
+ return block;
3774
+ }
3747
3775
  toolUseIndex++;
3748
3776
  const prior = seen.get(key);
3749
3777
  if (prior && !block.isError) {
@@ -4442,7 +4470,7 @@ var QueryEngine = class {
4442
4470
  };
4443
4471
  const dispatcher = new EarlyToolDispatcher(this.tools, context);
4444
4472
  try {
4445
- const microcompacted = microcompact(this.messages);
4473
+ const microcompacted = microcompact(this.messages, this.tools);
4446
4474
  this.messages = microcompacted;
4447
4475
  for (const dedupedId of microcompacted.dedupedToolUseIds) {
4448
4476
  yield {