nolo-cli 0.77.0-alpha.1 → 0.77.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 (137) hide show
  1. package/{_executeModel-MONX3AUM.js → _executeModel-G6BRN2BO.js} +43 -43
  2. package/{action-D22U6CTH.js → action-7GHFPDSQ.js} +10 -10
  3. package/{addContentAction-XAPUTGL4.js → addContentAction-ZNVB2NOV.js} +8 -8
  4. package/{addDialogAgentAction-M7VLLBZM.js → addDialogAgentAction-CIDFWATM.js} +7 -7
  5. package/{agentDeleteCommand-2TYFURBN.js → agentDeleteCommand-C5JTDBIX.js} +28 -26
  6. package/{agentEmailCommands-HXIX4KJE.js → agentEmailCommands-AYSYNUSH.js} +56 -54
  7. package/{agentListCommands-IFUOWXP3.js → agentListCommands-CVFL3P3D.js} +30 -28
  8. package/{agentMachineCommands-NEJEQV5S.js → agentMachineCommands-2EPX7IYC.js} +28 -26
  9. package/{agentProviderCommands-65ZC2WB4.js → agentProviderCommands-EHR3PV2V.js} +87 -0
  10. package/{agentPullCommand-N7L2CT7L.js → agentPullCommand-U73W5IBM.js} +1 -1
  11. package/{agentRecordCommands-USSLZKED.js → agentRecordCommands-DPZVHOOA.js} +54 -52
  12. package/{agentRunCommand-WZZSCEEP.js → agentRunCommand-BE53VNNN.js} +54 -52
  13. package/{agentRuntimeLocal-PUZINLWJ.js → agentRuntimeLocal-BPN5IOFT.js} +27 -25
  14. package/{agentSlice-BLCUNM5V.js → agentSlice-PYJFXGZL.js} +9 -9
  15. package/{appDeleteCommands-U4NTEJYQ.js → appDeleteCommands-TREMNXCA.js} +28 -26
  16. package/{appDeployCommands-FXF45Q7F.js → appDeployCommands-7FYCMERT.js} +28 -26
  17. package/{appDeployStatusCommands-KONVRM53.js → appDeployStatusCommands-7LAYH7IX.js} +28 -26
  18. package/{appGetCommands-3VOGIZ7T.js → appGetCommands-BONTQ2XH.js} +28 -26
  19. package/{appListCommands-A6ROBW7P.js → appListCommands-BYIELWUL.js} +28 -26
  20. package/{authCommand-EVGFLAHO.js → authCommand-J5C7GS33.js} +92 -17
  21. package/{authCommands-ILTBI6JV.js → authCommands-VOTT6XWX.js} +3 -4
  22. package/builtinSkillRegistry-YITVDLWG.js +33 -0
  23. package/{chatQueueReduxAdapter-ROFM5SW7.js → chatQueueReduxAdapter-PIOXZBGH.js} +23 -23
  24. package/{chunk-EXMHJN7C.js → chunk-2F4VYGFW.js} +2 -2
  25. package/{chunk-C3FZR5EC.js → chunk-2F7RJC6L.js} +1 -1
  26. package/{chunk-3QPYFHAH.js → chunk-2XEVK4BT.js} +1 -1
  27. package/{chunk-HQUORJTM.js → chunk-3BMYXEUW.js} +1 -1
  28. package/{chunk-AFIWC5NH.js → chunk-3DZIWHJ6.js} +1 -1
  29. package/{chunk-L2Y24GRM.js → chunk-4PAFEM6V.js} +3 -3
  30. package/{chunk-ERD7UXVL.js → chunk-5DAMTON3.js} +1 -1
  31. package/{chunk-YFADVSZE.js → chunk-5EJZGJS2.js} +6 -6
  32. package/chunk-5ZUL4CUO.js +10 -0
  33. package/{chunk-KIS76A2P.js → chunk-6HCLDVEI.js} +38 -4
  34. package/{chunk-FPTQQSS6.js → chunk-6LWO3QQS.js} +25 -4
  35. package/{chunk-BYEJ7K2Z.js → chunk-7S4YWN7P.js} +1 -1
  36. package/{chunk-2SFNAHL6.js → chunk-7TEU3WX2.js} +41 -25
  37. package/{chunk-SREASRV5.js → chunk-ABQYVJBJ.js} +1 -1
  38. package/{chunk-N7DNNV6J.js → chunk-ACFF7U3F.js} +340 -76
  39. package/{chunk-AFIKMMGC.js → chunk-AGX5TE5W.js} +1 -1
  40. package/{chunk-AJHEFRGJ.js → chunk-ASGDO77C.js} +122 -1
  41. package/{chunk-ZDONXM5U.js → chunk-D4TJCP3V.js} +7 -7
  42. package/{chunk-PYXL2CJL.js → chunk-DNQPPRHY.js} +1 -1
  43. package/{chunk-QH6NK22I.js → chunk-DQD4PYQY.js} +60 -788
  44. package/{chunk-PI4DWFYV.js → chunk-FSISQCCB.js} +1132 -31
  45. package/{chunk-3SPWIDHH.js → chunk-FWGPT6VI.js} +3 -3
  46. package/{chunk-SZLVDREM.js → chunk-GIG262ZG.js} +3 -3
  47. package/{chunk-PZM3EENV.js → chunk-GXWEF4IZ.js} +1 -1
  48. package/chunk-HJKGQPYF.js +266 -0
  49. package/{chunk-QA6YKM3H.js → chunk-HLRYI5XO.js} +31 -3
  50. package/{chunk-3JQ2NRYU.js → chunk-HVURBGF3.js} +4 -4
  51. package/{chunk-UPQF3C7O.js → chunk-IEILL33V.js} +135 -56
  52. package/{chunk-UZ6XXCVR.js → chunk-IZAWJCG7.js} +15 -2
  53. package/{chunk-XWNRAIUL.js → chunk-JITSBA52.js} +6 -16
  54. package/{chunk-6XIE5ZGI.js → chunk-JPGCXHNM.js} +1 -1
  55. package/{chunk-BGIWMGSP.js → chunk-JRFQPOPS.js} +10 -172
  56. package/chunk-JT7FRJP5.js +1625 -0
  57. package/{chunk-CMH2SYHX.js → chunk-K7M3MWCE.js} +2 -2
  58. package/{chunk-E36HZARC.js → chunk-KT6LENVP.js} +3 -3
  59. package/{chunk-FSFQ3A7S.js → chunk-LGSXBNWH.js} +9 -9
  60. package/{chunk-RXX4GDIH.js → chunk-LS2ZANRJ.js} +1 -1
  61. package/{chunk-4EJ5QYB5.js → chunk-LSV2BOJL.js} +1 -1
  62. package/{chunk-EVBFYPDC.js → chunk-MC4XLIJ7.js} +6 -2
  63. package/{chunk-PGSKWZCB.js → chunk-MJHQ3NRV.js} +1 -1
  64. package/{chunk-G3O3PD6Q.js → chunk-MRT4KARW.js} +14 -14
  65. package/{chunk-K7AG465Q.js → chunk-ND2J3NRB.js} +4 -2
  66. package/{chunk-Q6FF2MCK.js → chunk-OAJRZ35U.js} +1 -1
  67. package/{chunk-5J7W2VWW.js → chunk-OHUU52T3.js} +6 -265
  68. package/{chunk-V3ZKNK5D.js → chunk-OKCTLCKS.js} +9 -9
  69. package/{chunk-2R5EVB2O.js → chunk-PGO7O65Q.js} +5 -5
  70. package/{chunk-ZZCJUXUI.js → chunk-PHDKTASK.js} +2 -2
  71. package/{chunk-AETZ7AD6.js → chunk-Q554LLSI.js} +1 -1
  72. package/chunk-Q5FJJ6XI.js +103 -0
  73. package/{chunk-RILY7TKG.js → chunk-RHB6OCZS.js} +9 -9
  74. package/chunk-RJ3TDODF.js +665 -0
  75. package/{chunk-QC5LGHYP.js → chunk-TIJK4PBP.js} +10 -5
  76. package/{chunk-CGRJWXTE.js → chunk-UDK3ZSMN.js} +19 -15
  77. package/{chunk-E6RGHPDR.js → chunk-UINEAQ2R.js} +3 -5
  78. package/{chunk-DWPNOYMB.js → chunk-UQ45IHLM.js} +1 -1
  79. package/{chunk-ZY4K4CZR.js → chunk-UQYEVBMM.js} +684 -96
  80. package/{chunk-BJCWNHVU.js → chunk-US463UAD.js} +2 -2
  81. package/{chunk-ZLPA7K55.js → chunk-UUVQJZ75.js} +28 -9
  82. package/{chunk-DSRXPQWA.js → chunk-VEJ4BOQV.js} +43 -43
  83. package/{chunk-NHRQCXIQ.js → chunk-VMFTFOWF.js} +13 -3
  84. package/{chunk-UJ63UXGE.js → chunk-VRLI2WNW.js} +1073 -752
  85. package/{chunk-NG4UT4K4.js → chunk-WS22FPWT.js} +1 -1
  86. package/{chunk-MYU3MJCF.js → chunk-XCSSFPTB.js} +175 -7
  87. package/{chunk-MQ25G6VQ.js → chunk-XIYOAH3S.js} +3 -3
  88. package/{chunk-UDQWGU7F.js → chunk-YZHP76TR.js} +1 -1
  89. package/{chunk-GBPZKFYK.js → chunk-Z33ARZCP.js} +4 -4
  90. package/{contentThunks-3BFZJFHI.js → contentThunks-RYZTYVUV.js} +24 -24
  91. package/{createAgentAutomationAction-YCWUKLMK.js → createAgentAutomationAction-GUBB2LWY.js} +24 -24
  92. package/{createDialogAction-VULS2PKA.js → createDialogAction-577E6FB2.js} +27 -27
  93. package/{createPageAction-MZZLQPFE.js → createPageAction-4CNYIRJ5.js} +25 -25
  94. package/{dbSlice-GKGGFNOB.js → dbSlice-4CUQE35F.js} +7 -7
  95. package/{deleteFile-LWSR674W.js → deleteFile-YG6QMJLU.js} +3 -3
  96. package/{dialogCommands-G223U5WU.js → dialogCommands-U4RQIRKK.js} +29 -27
  97. package/{dialogSlice-NQO5ZETL.js → dialogSlice-YXMORK5T.js} +23 -23
  98. package/{docCreateCommands-2P47IJDK.js → docCreateCommands-AXXATGCL.js} +29 -27
  99. package/{docDeleteCommands-N3EGDOIC.js → docDeleteCommands-CRPIFYFA.js} +30 -28
  100. package/{docReadCommands-6BFV2HI7.js → docReadCommands-4IMZXFDR.js} +29 -27
  101. package/{docStore-AEJONTM4.js → docStore-3U5FBN2O.js} +8 -8
  102. package/{docUpdateCommands-ZTMPXHC5.js → docUpdateCommands-RRPRZXCE.js} +29 -27
  103. package/{fetchMessages-DRABNDPZ.js → fetchMessages-EIU7UFFK.js} +3 -4
  104. package/{getPricing-HRMSOF3E.js → getPricing-F2C65RIX.js} +4 -4
  105. package/{handleSendMessageAction-WIUO466V.js → handleSendMessageAction-BQIRKHC6.js} +23 -23
  106. package/index.js +61 -53
  107. package/{localDialogRead-PUQKFI73.js → localDialogRead-BTFJ4V2I.js} +2 -2
  108. package/{localRuntimeDb-P37ZTX55.js → localRuntimeDb-SYQGUF4U.js} +1 -1
  109. package/{machineCommands-FUA7HBND.js → machineCommands-RNBADNTE.js} +54 -52
  110. package/{machineWsRunDispatch-URIG5N2V.js → machineWsRunDispatch-7J6WNF57.js} +52 -50
  111. package/{memberThunks-KATHH3F4.js → memberThunks-N35XAOC6.js} +8 -8
  112. package/{models-GS72DVJS.js → models-WW7VFNXB.js} +2 -2
  113. package/package.json +1 -1
  114. package/providerPresetApply-SAJAK7C3.js +214 -0
  115. package/providerRegistry-COD57OVY.js +31 -0
  116. package/{read-LC6HVYBN.js → read-2PV4PN2E.js} +4 -3
  117. package/{readDocTool-N3ERJHHF.js → readDocTool-FGK5XGAW.js} +1 -1
  118. package/{readlineWorkspace-HVQLD7T3.js → readlineWorkspace-ZO2W3N4U.js} +135 -75
  119. package/{removeDialogAgentAction-F5GPTALR.js → removeDialogAgentAction-O2POGBOE.js} +10 -10
  120. package/{runAgentClientLoop-QHL2HNAI.js → runAgentClientLoop-KJZ7DK6D.js} +42 -42
  121. package/{runtimeDoctorCommands-CROTMULY.js → runtimeDoctorCommands-X6WDBEQX.js} +2 -2
  122. package/{setDialogExtraReferencesAction-AY4ZCF3A.js → setDialogExtraReferencesAction-KPSVVY3C.js} +8 -8
  123. package/{setPrimaryDialogAgentAction-HPMG7V3R.js → setPrimaryDialogAgentAction-RMJETONI.js} +10 -10
  124. package/{settingSlice-OEJA4NQO.js → settingSlice-I6MTTNDX.js} +9 -9
  125. package/{spaceCommands-MRP5U3C3.js → spaceCommands-VZRYKVWZ.js} +27 -25
  126. package/{streamAgentChatTurn-CE2H23VD.js → streamAgentChatTurn-L3SO4N2R.js} +54 -53
  127. package/{switchDialogAgentAction-HMDZTZ4I.js → switchDialogAgentAction-OQSGWVQQ.js} +7 -7
  128. package/{tools-F25QG6XS.js → tools-GRV74JPO.js} +38 -36
  129. package/tuiSummaryLlmCaller-ATDKMGI5.js +127 -0
  130. package/{updateDialogTitleAction-Z333XUN4.js → updateDialogTitleAction-KRUWX66L.js} +44 -27
  131. package/chunk-3IT2PFHL.js +0 -611
  132. package/chunk-I42VC5KR.js +0 -15
  133. package/codingSkills-E6KSDNSJ.js +0 -48
  134. package/tuiSummaryLlmCaller-3Z24VUWM.js +0 -116
  135. package/{chunk-3RAG2NAD.js → chunk-UKWGR5SS.js} +3 -3
  136. package/{db-VQSAOWAO.js → db-WK5MDZVN.js} +1 -1
  137. package/{localRuntimeAuthority-R5UBODT3.js → localRuntimeAuthority-6IR6BBXE.js} +3 -3
@@ -27,7 +27,7 @@ import {
27
27
  resolveLocalUserId,
28
28
  t,
29
29
  toolLabel
30
- } from "./chunk-UPQF3C7O.js";
30
+ } from "./chunk-IEILL33V.js";
31
31
  import {
32
32
  createRunActivityTracker,
33
33
  popSingleQueueMessage,
@@ -43,11 +43,11 @@ import {
43
43
  } from "./chunk-DWUCB2A3.js";
44
44
  import {
45
45
  getReadableCliDb
46
- } from "./chunk-RXX4GDIH.js";
46
+ } from "./chunk-LS2ZANRJ.js";
47
47
  import {
48
48
  readDbRecord,
49
49
  resolveAgentRecordFromHybridStore
50
- } from "./chunk-QA6YKM3H.js";
50
+ } from "./chunk-HLRYI5XO.js";
51
51
  import {
52
52
  isLocalCliAgentKey
53
53
  } from "./chunk-SPAQ4PVV.js";
@@ -63,7 +63,7 @@ import {
63
63
  } from "./chunk-LTRXGFMS.js";
64
64
  import {
65
65
  formatAgentListCard
66
- } from "./chunk-QH6NK22I.js";
66
+ } from "./chunk-DQD4PYQY.js";
67
67
  import {
68
68
  isTransientFetchError
69
69
  } from "./chunk-5XTOKQLW.js";
@@ -84,11 +84,11 @@ import {
84
84
  chooseMemoryOwners,
85
85
  loadMemoryCandidatesFromDb,
86
86
  resolveAgentMemoryPolicy
87
- } from "./chunk-BGIWMGSP.js";
87
+ } from "./chunk-JRFQPOPS.js";
88
88
  import {
89
89
  API_REPORTED_COST_MULTIPLIER,
90
90
  parseUiAskChoiceContent
91
- } from "./chunk-N7DNNV6J.js";
91
+ } from "./chunk-ACFF7U3F.js";
92
92
  import {
93
93
  isOAuthApiKeyRef
94
94
  } from "./chunk-YYLHWIW6.js";
@@ -99,50 +99,50 @@ import {
99
99
  } from "./chunk-TRV2MIDW.js";
100
100
  import {
101
101
  getDefaultCliLocalRuntimeDb
102
- } from "./chunk-C3FZR5EC.js";
102
+ } from "./chunk-2F7RJC6L.js";
103
103
  import {
104
104
  buildSkillDiscoveryContextLayer
105
105
  } from "./chunk-E4MARS3Q.js";
106
106
  import {
107
107
  parseExternalSkillMarkdown,
108
108
  parseSkillDocProtocol
109
- } from "./chunk-AETZ7AD6.js";
109
+ } from "./chunk-Q554LLSI.js";
110
110
  import {
111
111
  LOCAL_AGENT_CONFIG_MISSING_CODE,
112
112
  LOCAL_TURN_ABORTED_CODE,
113
113
  readActionGate
114
- } from "./chunk-ZY4K4CZR.js";
114
+ } from "./chunk-UQYEVBMM.js";
115
115
  import {
116
116
  resolveAgentRuntimeConfigFromRecord
117
- } from "./chunk-KIS76A2P.js";
117
+ } from "./chunk-6HCLDVEI.js";
118
118
  import {
119
119
  createThinkParserState,
120
120
  flushThinkParser,
121
121
  processThinkChunk
122
122
  } from "./chunk-N7J63Q6C.js";
123
- import {
124
- isAntigravityOAuthAgent
125
- } from "./chunk-PGSKWZCB.js";
126
- import {
127
- resolveClientVersion
128
- } from "./chunk-CMH2SYHX.js";
129
123
  import {
130
124
  isQuotaExhaustedError,
131
125
  isQuotaLimited403Body,
132
126
  mergeAvailabilityDeadline,
133
127
  resolveCooldownGate
134
128
  } from "./chunk-CJCJF5XB.js";
129
+ import {
130
+ isAntigravityOAuthAgent
131
+ } from "./chunk-MJHQ3NRV.js";
132
+ import {
133
+ resolveClientVersion
134
+ } from "./chunk-K7M3MWCE.js";
135
135
  import {
136
136
  DEFAULT_CONTEXT_WINDOW,
137
137
  getModelContextWindow,
138
138
  normalizeUsage
139
- } from "./chunk-DSRXPQWA.js";
139
+ } from "./chunk-VEJ4BOQV.js";
140
140
  import {
141
141
  clipCompactText
142
- } from "./chunk-E36HZARC.js";
142
+ } from "./chunk-KT6LENVP.js";
143
143
  import {
144
144
  findModelConfig
145
- } from "./chunk-UJ63UXGE.js";
145
+ } from "./chunk-VRLI2WNW.js";
146
146
  import {
147
147
  compactWhitespace
148
148
  } from "./chunk-DWLEOOGR.js";
@@ -167,13 +167,14 @@ import {
167
167
  CLIENT_VERSION_TOO_OLD_CODE,
168
168
  NOLO_CLIENT_VERSION_HEADER,
169
169
  buildClientVersionUpgradeMessage
170
- } from "./chunk-AJHEFRGJ.js";
170
+ } from "./chunk-ASGDO77C.js";
171
171
  import {
172
172
  parsePositiveFiniteNumberOrFallback
173
173
  } from "./chunk-Y5GCAMGP.js";
174
174
  import {
175
- createMemoryKey
176
- } from "./chunk-NHRQCXIQ.js";
175
+ createMemoryKey,
176
+ ulid
177
+ } from "./chunk-VMFTFOWF.js";
177
178
  import {
178
179
  asTrimmedLowercaseString
179
180
  } from "./chunk-QSJ3HFEF.js";
@@ -181,10 +182,13 @@ import {
181
182
  BUILTIN_NOLO_AGENT_KEY,
182
183
  BUILTIN_NOLO_AGENT_NAME,
183
184
  builtinAgentCatalogEntryById
184
- } from "./chunk-5J7W2VWW.js";
185
+ } from "./chunk-HJKGQPYF.js";
185
186
  import {
186
187
  parsePublicAgentId
187
188
  } from "./chunk-BJIRLHJR.js";
189
+ import {
190
+ isLevelNotFoundError
191
+ } from "./chunk-OW765XBU.js";
188
192
  import {
189
193
  asTrimmedString
190
194
  } from "./chunk-3EUW3WEO.js";
@@ -1496,8 +1500,8 @@ function userSurfaceBackgroundSequence(env = process.env, brightness = resolveTu
1496
1500
  return "";
1497
1501
  }
1498
1502
  const palette = THEME_PALETTES[activeThemeName] ?? THEME_PALETTES.trail;
1499
- const surfaceHex = brightness === "light" ? palette.light.chrome.hex : palette.dark.accent.hex;
1500
- const weight = brightness === "dark" ? 0.3 : 0.08;
1503
+ const weight = brightness === "dark" ? 0.3 : 0.14;
1504
+ const surfaceHex = (brightness === "light" ? palette.light : palette.dark).accent.hex;
1501
1505
  return hexToBgSgr(blendHex(surfaceHex, resolveTerminalBaseHex(brightness), weight));
1502
1506
  }
1503
1507
  function themeText(text, token, colorEnabled = resolveCliColorEnabled(), env = process.env) {
@@ -10411,9 +10415,9 @@ function findCollapsedPasteSpanAt(buffer, pos, opts = {}) {
10411
10415
  }
10412
10416
 
10413
10417
  // packages/cli/client/agentRun.ts
10414
- import { ulid } from "ulid";
10418
+ import { ulid as ulid2 } from "ulid";
10415
10419
  async function loadRunLocalAgentTurn() {
10416
- const { runLocalAgentTurn } = await import("./agentRuntimeLocal-PUZINLWJ.js");
10420
+ const { runLocalAgentTurn } = await import("./agentRuntimeLocal-BPN5IOFT.js");
10417
10421
  return runLocalAgentTurn;
10418
10422
  }
10419
10423
  var stripDebugNoise = (s) => s.replace(/\s*raw="[\s\S]*"/g, "").replace(/\s*raw=\S+/g, "").replace(/\s*headers=\[[^\]]*\]/g, "").replace(/\s*headers=\S+/g, "").trim();
@@ -11078,8 +11082,8 @@ async function runLocalAgentTurnForCli(options, settings) {
11078
11082
  ...options.parentWakeOnTerminal ? { parentWakeOnTerminal: true } : {},
11079
11083
  ...options.parentDialogId ? { parentThreadId: options.parentDialogId } : {}
11080
11084
  } : void 0;
11081
- const currentDialogId = options.continueDialogId ?? ulid();
11082
- const turnId = `turn-${ulid()}`;
11085
+ const currentDialogId = options.continueDialogId ?? ulid2();
11086
+ const turnId = `turn-${ulid2()}`;
11083
11087
  const runtimeContextWithOwnership = {
11084
11088
  ...runtimeContext ?? {},
11085
11089
  dialogId: currentDialogId,
@@ -12073,6 +12077,1074 @@ var rankMemoryCandidates = (items, query, context) => {
12073
12077
  });
12074
12078
  };
12075
12079
 
12080
+ // packages/ai/memory/vnext/store.ts
12081
+ var enc = (value) => encodeURIComponent(value).replace(/-/g, "%2D");
12082
+ var rangeOf = (prefix) => ({ gte: prefix, lte: `${prefix}\uFFFF` });
12083
+ var entityKey = (ownerId, id) => `mem2-e-${enc(ownerId)}-${enc(id)}`;
12084
+ var entityOwnerPrefix = (ownerId) => `mem2-e-${enc(ownerId)}-`;
12085
+ var stateKey = (ownerId, id) => `mem2-s-${enc(ownerId)}-${enc(id)}`;
12086
+ var stateEntityIndexKey = (ownerId, entityId, stateId) => `mem2-si-${enc(ownerId)}-${enc(entityId)}-${enc(stateId)}`;
12087
+ var stateEntityIndexPrefix = (ownerId, entityId) => `mem2-si-${enc(ownerId)}-${enc(entityId)}-`;
12088
+ var evidenceKey = (ownerId, id) => `mem2-v-${enc(ownerId)}-${enc(id)}`;
12089
+ var relationKey = (ownerId, id) => `mem2-r-${enc(ownerId)}-${enc(id)}`;
12090
+ var relationFromIndexKey = (ownerId, from, relationId) => `mem2-rf-${enc(ownerId)}-${enc(from)}-${enc(relationId)}`;
12091
+ var relationToIndexKey = (ownerId, to, relationId) => `mem2-rt-${enc(ownerId)}-${enc(to)}-${enc(relationId)}`;
12092
+ var readIndexed = async (db, prefix) => {
12093
+ const refs = [];
12094
+ for await (const [, value] of db.iterator({ ...rangeOf(prefix), reverse: false })) {
12095
+ if (typeof value?.key === "string") refs.push(value.key);
12096
+ }
12097
+ const rows = await Promise.all(refs.map((key) => db.get(key).catch(() => null)));
12098
+ return rows.filter((row) => !!row);
12099
+ };
12100
+ var getMemoryEntityVNext = async (db, ownerId, entityId) => db.get(entityKey(ownerId, entityId)).catch(() => null);
12101
+ var listMemoryEntitiesVNext = async (db, ownerId) => {
12102
+ const rows = [];
12103
+ for await (const [, value] of db.iterator({
12104
+ ...rangeOf(entityOwnerPrefix(ownerId)),
12105
+ reverse: false
12106
+ })) {
12107
+ if (value) rows.push(value);
12108
+ }
12109
+ return rows;
12110
+ };
12111
+ var getMemoryStateVNext = async (db, ownerId, stateId) => db.get(stateKey(ownerId, stateId)).catch(() => null);
12112
+ var listMemoryStatesForEntityVNext = (db, ownerId, entityId) => readIndexed(db, stateEntityIndexPrefix(ownerId, entityId));
12113
+ var putMemoryEvidenceVNext = async (db, evidence) => {
12114
+ await db.put(evidenceKey(evidence.ownerId, evidence.id), evidence);
12115
+ };
12116
+ var getMemoryEvidenceVNext = async (db, ownerId, evidenceId) => db.get(evidenceKey(ownerId, evidenceId)).catch(() => null);
12117
+ var getMemoryEvidenceVNextStrict = async (db, ownerId, evidenceId) => {
12118
+ try {
12119
+ return await db.get(evidenceKey(ownerId, evidenceId));
12120
+ } catch (error) {
12121
+ if (isLevelNotFoundError(error)) return null;
12122
+ throw error;
12123
+ }
12124
+ };
12125
+ var _appendEntityBatch = (batch, entity) => {
12126
+ batch.put(entityKey(entity.ownerId, entity.id), entity);
12127
+ };
12128
+ var _appendEvidenceBatch = (batch, evidence) => {
12129
+ batch.put(evidenceKey(evidence.ownerId, evidence.id), evidence);
12130
+ };
12131
+ var _appendStateBatch = (batch, state2) => {
12132
+ const key = stateKey(state2.ownerId, state2.id);
12133
+ batch.put(key, state2);
12134
+ batch.put(stateEntityIndexKey(state2.ownerId, state2.entityId, state2.id), { key });
12135
+ };
12136
+ var _appendRetiredStateBatch = (batch, state2) => {
12137
+ batch.put(stateKey(state2.ownerId, state2.id), state2);
12138
+ };
12139
+ var _appendRelationBatch = (batch, relation) => {
12140
+ const key = relationKey(relation.ownerId, relation.id);
12141
+ batch.put(key, relation);
12142
+ batch.put(relationFromIndexKey(relation.ownerId, relation.from, relation.id), { key });
12143
+ batch.put(relationToIndexKey(relation.ownerId, relation.to, relation.id), { key });
12144
+ };
12145
+
12146
+ // packages/ai/memory/vnext/recall.ts
12147
+ var MAX_RECALL_REFS = 6;
12148
+ var stripFence = (raw) => {
12149
+ const trimmed = raw.trim();
12150
+ const match = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);
12151
+ return match ? match[1].trim() : trimmed;
12152
+ };
12153
+ var parseSelection = (raw) => {
12154
+ let parsed;
12155
+ try {
12156
+ parsed = JSON.parse(stripFence(raw));
12157
+ } catch {
12158
+ throw new Error("memory recall selector output is not valid JSON");
12159
+ }
12160
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
12161
+ throw new Error("memory recall selector output must be an object");
12162
+ }
12163
+ const obj = parsed;
12164
+ if (!Array.isArray(obj.refs) || !obj.refs.every((item) => typeof item === "string")) {
12165
+ throw new Error("memory recall selector refs must be string[]");
12166
+ }
12167
+ if (typeof obj.reason !== "string") {
12168
+ throw new Error("memory recall selector reason must be string");
12169
+ }
12170
+ return {
12171
+ refs: [...new Set(obj.refs)].slice(0, MAX_RECALL_REFS),
12172
+ reason: obj.reason
12173
+ };
12174
+ };
12175
+ var buildCatalog = async (db, ownerId) => {
12176
+ const entities = await listMemoryEntitiesVNext(db, ownerId);
12177
+ const states = [];
12178
+ for (const entity of entities) {
12179
+ states.push(...await listMemoryStatesForEntityVNext(db, ownerId, entity.id));
12180
+ }
12181
+ return { entities, states };
12182
+ };
12183
+ var catalogText = (entities, states) => JSON.stringify(
12184
+ {
12185
+ entities: entities.map((entity) => ({
12186
+ id: entity.id,
12187
+ type: entity.type,
12188
+ name: entity.name,
12189
+ ...entity.subtype ? { subtype: entity.subtype } : {},
12190
+ ...entity.aliases?.length ? { aliases: entity.aliases } : {}
12191
+ })),
12192
+ states: states.map((state2) => ({
12193
+ id: state2.id,
12194
+ entityId: state2.entityId,
12195
+ facet: state2.facet,
12196
+ text: state2.text,
12197
+ // Retired (superseded) States stay listed as recoverable history; the
12198
+ // boolean — not the retiredAt timestamp — enters the prompt so no
12199
+ // timestamps reach LLM text.
12200
+ ...state2.retiredAt ? { retired: true } : {}
12201
+ }))
12202
+ },
12203
+ null,
12204
+ 2
12205
+ );
12206
+ var createMemoryVNextRecall = (input) => async (query) => {
12207
+ const startedAt = performance.now();
12208
+ const { entities, states } = await buildCatalog(input.db, input.ownerId);
12209
+ if (entities.length === 0 && states.length === 0) {
12210
+ return {
12211
+ query,
12212
+ refs: [],
12213
+ entities: [],
12214
+ states: [],
12215
+ reason: "memory catalog is empty",
12216
+ trace: {
12217
+ candidateEntities: 0,
12218
+ candidateStates: 0,
12219
+ selectedRefs: 0,
12220
+ elapsedMs: Math.round((performance.now() - startedAt) * 10) / 10
12221
+ }
12222
+ };
12223
+ }
12224
+ const result = await input.provider.complete(
12225
+ [
12226
+ {
12227
+ role: "system",
12228
+ content: [
12229
+ "Select the smallest useful set of long-lived memory entrances for the user query.",
12230
+ 'Return ONLY JSON: {"refs":["exact-id"],"reason":"short explanation"}.',
12231
+ `Select at most ${MAX_RECALL_REFS} exact ids from the supplied catalog.`,
12232
+ "Prefer State ids when current durable understanding answers the query.",
12233
+ "Select an Entity id when it is a useful navigation anchor and no single State is enough.",
12234
+ "Do not select unrelated memories merely because they are personal or interesting.",
12235
+ "Historical-detail questions may select the relevant current State/Entity as an entrance; deeper evidence is handled by inspect, not here."
12236
+ ].join("\n")
12237
+ },
12238
+ {
12239
+ role: "user",
12240
+ content: `Query:
12241
+ ${query}
12242
+
12243
+ Memory catalog:
12244
+ ${catalogText(entities, states)}`
12245
+ }
12246
+ ],
12247
+ input.timeoutMs ? { timeoutMs: input.timeoutMs } : void 0
12248
+ );
12249
+ const selected = parseSelection(result.content);
12250
+ const entityById = new Map(entities.map((entity) => [entity.id, entity]));
12251
+ const stateById = new Map(states.map((state2) => [state2.id, state2]));
12252
+ const validRefs = selected.refs.filter((ref) => entityById.has(ref) || stateById.has(ref));
12253
+ return {
12254
+ query,
12255
+ refs: validRefs,
12256
+ entities: validRefs.flatMap((ref) => {
12257
+ const entity = entityById.get(ref);
12258
+ return entity ? [entity] : [];
12259
+ }),
12260
+ states: validRefs.flatMap((ref) => {
12261
+ const state2 = stateById.get(ref);
12262
+ return state2 ? [state2] : [];
12263
+ }),
12264
+ reason: selected.reason,
12265
+ trace: {
12266
+ candidateEntities: entities.length,
12267
+ candidateStates: states.length,
12268
+ selectedRefs: validRefs.length,
12269
+ elapsedMs: Math.round((performance.now() - startedAt) * 10) / 10
12270
+ }
12271
+ };
12272
+ };
12273
+
12274
+ // packages/ai/memory/vnext/legacyMigration.ts
12275
+ import { createHash } from "crypto";
12276
+ var LEGACY_MEMORY_SOURCE_REF_PREFIX = "legacy-memory";
12277
+ var LEGACY_MEMORY_EVIDENCE_ID_PREFIX = "mem-legacy-";
12278
+ var resolveLegacyMemoryPrincipal = (item) => ({
12279
+ ownerType: item.ownerType,
12280
+ ownerId: item.ownerId,
12281
+ principalId: `${item.ownerType}:${encodeURIComponent(item.ownerId)}`
12282
+ });
12283
+ var buildLegacyMemorySourceRef = (item) => {
12284
+ const { principalId } = resolveLegacyMemoryPrincipal(item);
12285
+ return `${LEGACY_MEMORY_SOURCE_REF_PREFIX}:${principalId}:${encodeURIComponent(item.id)}`;
12286
+ };
12287
+ var legacyEvidenceSeed = (principalId, itemId) => JSON.stringify([principalId, itemId]);
12288
+ var buildLegacyEvidenceId = (item) => {
12289
+ const { principalId } = resolveLegacyMemoryPrincipal(item);
12290
+ const digest = createHash("sha256").update(legacyEvidenceSeed(principalId, item.id)).digest("hex");
12291
+ return `${LEGACY_MEMORY_EVIDENCE_ID_PREFIX}${digest.slice(0, 32)}`;
12292
+ };
12293
+ var clampLegacyConfidence = (value) => {
12294
+ if (typeof value !== "number" || !Number.isFinite(value)) return 0;
12295
+ if (value < 0) return 0;
12296
+ if (value > 1) return 1;
12297
+ return value;
12298
+ };
12299
+ var buildLegacyMemoryEvidence = (item) => {
12300
+ const { principalId } = resolveLegacyMemoryPrincipal(item);
12301
+ return {
12302
+ id: buildLegacyEvidenceId(item),
12303
+ ownerId: principalId,
12304
+ text: item.content,
12305
+ sourceRef: buildLegacyMemorySourceRef(item),
12306
+ confidence: clampLegacyConfidence(item.confidence),
12307
+ createdAt: item.createdAt
12308
+ };
12309
+ };
12310
+
12311
+ // packages/ai/memory/vnext/shadowRead.ts
12312
+ var SHADOW_TIMEOUT_MS = 2e4;
12313
+ var ERROR_MESSAGE_MAX = 120;
12314
+ var normalizeTextKey = (text) => text.trim().replace(/\s+/g, " ").toLowerCase();
12315
+ var legacyItemKeys = (item) => {
12316
+ const keys = [normalizeTextKey(item.content ?? "")];
12317
+ return keys.filter((key) => key.length > 0);
12318
+ };
12319
+ var vnextResultKeys = (result) => {
12320
+ const keys = [];
12321
+ for (const entity of result.entities) {
12322
+ if (entity.name) keys.push(normalizeTextKey(entity.name));
12323
+ }
12324
+ for (const state2 of result.states) {
12325
+ if (state2.text) keys.push(normalizeTextKey(state2.text));
12326
+ }
12327
+ return keys.filter((key) => key.length > 0);
12328
+ };
12329
+ var vnextContextChars = (result) => result.entities.reduce(
12330
+ (sum, entity) => sum + (entity.name?.length ?? 0),
12331
+ result.states.reduce((sum, state2) => sum + (state2.text?.length ?? 0), 0)
12332
+ );
12333
+ var shortError = (error) => {
12334
+ const message = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
12335
+ return message.length > ERROR_MESSAGE_MAX ? `${message.slice(0, ERROR_MESSAGE_MAX)}\u2026` : message;
12336
+ };
12337
+ var withTimeout = async (promise, timeoutMs, label) => {
12338
+ let timer;
12339
+ const timeout = new Promise((_, reject) => {
12340
+ timer = setTimeout(() => reject(new Error(label)), timeoutMs);
12341
+ });
12342
+ try {
12343
+ return await Promise.race([promise, timeout]);
12344
+ } finally {
12345
+ if (timer) clearTimeout(timer);
12346
+ }
12347
+ };
12348
+ var runMemoryVNextShadowRead = async (input) => {
12349
+ const now = input.ctx.now ?? (() => performance.now());
12350
+ const emit = input.emit ?? ((observation2) => console.info("[memory] vnext shadow read", {
12351
+ event: "memory_vnext_shadow_read",
12352
+ ...observation2
12353
+ }));
12354
+ const legacyKeys = new Set(
12355
+ input.legacyItems.flatMap((item) => legacyItemKeys(item))
12356
+ );
12357
+ const vnextStartedAt = now();
12358
+ let vnextHitCount = 0;
12359
+ let vnextChars = 0;
12360
+ const vnextKeys = /* @__PURE__ */ new Set();
12361
+ let vnextError;
12362
+ try {
12363
+ const shadowTimeoutMs = input.ctx.shadowTimeoutMs ?? SHADOW_TIMEOUT_MS;
12364
+ const perOwner = async (owner) => {
12365
+ const principal = resolveLegacyMemoryPrincipal({
12366
+ ownerType: owner.ownerType,
12367
+ ownerId: owner.ownerId
12368
+ });
12369
+ const recall = createMemoryVNextRecall({
12370
+ db: input.ctx.db,
12371
+ ownerId: principal.principalId,
12372
+ provider: input.ctx.provider,
12373
+ timeoutMs: input.ctx.timeoutMs
12374
+ });
12375
+ return recall(input.ctx.query);
12376
+ };
12377
+ const results = await withTimeout(
12378
+ Promise.all(input.ctx.owners.map((owner) => perOwner(owner))),
12379
+ shadowTimeoutMs,
12380
+ `memory vnext shadow read timed out after ${shadowTimeoutMs}ms`
12381
+ );
12382
+ for (const result of results) {
12383
+ vnextHitCount += result.refs.length;
12384
+ vnextChars += vnextContextChars(result);
12385
+ for (const key of vnextResultKeys(result)) vnextKeys.add(key);
12386
+ }
12387
+ } catch (error) {
12388
+ vnextError = shortError(error);
12389
+ }
12390
+ const vnextLatencyMs = Math.round((now() - vnextStartedAt) * 10) / 10;
12391
+ let overlapCount = null;
12392
+ let legacyOnlyCount = null;
12393
+ let vnextOnlyCount = null;
12394
+ if (!vnextError) {
12395
+ let overlap = 0;
12396
+ for (const key of vnextKeys) if (legacyKeys.has(key)) overlap += 1;
12397
+ overlapCount = overlap;
12398
+ legacyOnlyCount = Math.max(0, legacyKeys.size - overlap);
12399
+ vnextOnlyCount = Math.max(0, vnextKeys.size - overlap);
12400
+ }
12401
+ const observation = {
12402
+ legacyHitCount: input.legacyItems.length,
12403
+ vnextHitCount,
12404
+ overlapCount,
12405
+ legacyOnlyCount,
12406
+ vnextOnlyCount,
12407
+ legacyLatencyMs: input.legacyLatencyMs,
12408
+ vnextLatencyMs,
12409
+ legacyContextChars: input.legacyContextChars,
12410
+ vnextContextChars: vnextChars,
12411
+ ...vnextError ? { vnextError } : {}
12412
+ };
12413
+ emit(observation);
12414
+ return observation;
12415
+ };
12416
+ var isMemoryVNextShadowReadEnabled = (env = typeof process !== "undefined" ? process.env : {}) => env.NOLO_MEMORY_VNEXT_SHADOW_READ === "1" || env.NOLO_MEMORY_VNEXT_SHADOW_READ === "true";
12417
+
12418
+ // packages/ai/memory/vnext/applyMutation.ts
12419
+ async function validateMutation(db, ownerId, mutation) {
12420
+ const pending = {
12421
+ evidenceIds: /* @__PURE__ */ new Set(),
12422
+ entityIds: /* @__PURE__ */ new Set(),
12423
+ stateIds: /* @__PURE__ */ new Set(),
12424
+ relationIds: /* @__PURE__ */ new Set()
12425
+ };
12426
+ for (const ev of mutation.evidence ?? []) {
12427
+ if (!ev.id || typeof ev.id !== "string") {
12428
+ throw new Error("evidence id must be a non-empty string");
12429
+ }
12430
+ if (pending.evidenceIds.has(ev.id)) {
12431
+ throw new Error(`duplicate evidence id in mutation: ${ev.id}`);
12432
+ }
12433
+ pending.evidenceIds.add(ev.id);
12434
+ }
12435
+ for (const ent of mutation.entities ?? []) {
12436
+ if (!ent.id || typeof ent.id !== "string") {
12437
+ throw new Error("entity id must be a non-empty string");
12438
+ }
12439
+ if (pending.entityIds.has(ent.id)) {
12440
+ throw new Error(`duplicate entity id in mutation: ${ent.id}`);
12441
+ }
12442
+ pending.entityIds.add(ent.id);
12443
+ }
12444
+ for (const st of mutation.states ?? []) {
12445
+ if (!st.id || typeof st.id !== "string") {
12446
+ throw new Error("state id must be a non-empty string");
12447
+ }
12448
+ if (pending.stateIds.has(st.id)) {
12449
+ throw new Error(`duplicate state id in mutation: ${st.id}`);
12450
+ }
12451
+ pending.stateIds.add(st.id);
12452
+ }
12453
+ for (const rel of mutation.relations ?? []) {
12454
+ if (!rel.id || typeof rel.id !== "string") {
12455
+ throw new Error("relation id must be a non-empty string");
12456
+ }
12457
+ if (pending.relationIds.has(rel.id)) {
12458
+ throw new Error(`duplicate relation id in mutation: ${rel.id}`);
12459
+ }
12460
+ pending.relationIds.add(rel.id);
12461
+ }
12462
+ const supersededIds = /* @__PURE__ */ new Set();
12463
+ for (const sid of mutation.supersededStateIds ?? []) {
12464
+ if (supersededIds.has(sid)) {
12465
+ throw new Error(`duplicate supersededStateId in mutation: ${sid}`);
12466
+ }
12467
+ supersededIds.add(sid);
12468
+ }
12469
+ for (const ev of mutation.evidence ?? []) {
12470
+ if (ev.ownerId !== ownerId) {
12471
+ throw new Error(
12472
+ `evidence ${ev.id} has ownerId ${ev.ownerId}, expected ${ownerId}`
12473
+ );
12474
+ }
12475
+ }
12476
+ for (const ent of mutation.entities ?? []) {
12477
+ if (ent.ownerId !== ownerId) {
12478
+ throw new Error(
12479
+ `entity ${ent.id} has ownerId ${ent.ownerId}, expected ${ownerId}`
12480
+ );
12481
+ }
12482
+ }
12483
+ for (const st of mutation.states ?? []) {
12484
+ if (st.ownerId !== ownerId) {
12485
+ throw new Error(
12486
+ `state ${st.id} has ownerId ${st.ownerId}, expected ${ownerId}`
12487
+ );
12488
+ }
12489
+ }
12490
+ for (const rel of mutation.relations ?? []) {
12491
+ if (rel.ownerId !== ownerId) {
12492
+ throw new Error(
12493
+ `relation ${rel.id} has ownerId ${rel.ownerId}, expected ${ownerId}`
12494
+ );
12495
+ }
12496
+ }
12497
+ for (const st of mutation.states ?? []) {
12498
+ if (supersededIds.has(st.id)) {
12499
+ throw new Error(
12500
+ `state ${st.id} appears in both states and supersededStateIds; a record cannot be created and retired in the same mutation`
12501
+ );
12502
+ }
12503
+ }
12504
+ const facetSlots = /* @__PURE__ */ new Set();
12505
+ for (const st of mutation.states ?? []) {
12506
+ const slot = `${st.entityId} ${st.facet}`;
12507
+ if (facetSlots.has(slot)) {
12508
+ throw new Error(
12509
+ `mutation writes parallel current states for entity ${st.entityId} facet ${st.facet}`
12510
+ );
12511
+ }
12512
+ facetSlots.add(slot);
12513
+ }
12514
+ for (const ev of mutation.evidence ?? []) {
12515
+ const existing = await getMemoryEvidenceVNext(db, ownerId, ev.id);
12516
+ if (existing) {
12517
+ throw new Error(
12518
+ `evidence ${ev.id} already exists (owner ${ownerId}); use a unique id`
12519
+ );
12520
+ }
12521
+ }
12522
+ for (const ent of mutation.entities ?? []) {
12523
+ const existing = await getMemoryEntityVNext(db, ownerId, ent.id);
12524
+ if (existing) {
12525
+ throw new Error(
12526
+ `entity ${ent.id} already exists (owner ${ownerId}); use a unique id`
12527
+ );
12528
+ }
12529
+ }
12530
+ for (const st of mutation.states ?? []) {
12531
+ if (!pending.entityIds.has(st.entityId)) {
12532
+ const entity = await getMemoryEntityVNext(db, ownerId, st.entityId);
12533
+ if (!entity) {
12534
+ throw new Error(
12535
+ `memory state ${st.id} references missing entity ${st.entityId} (owner ${ownerId})`
12536
+ );
12537
+ }
12538
+ }
12539
+ for (const evidenceId of st.evidenceIds) {
12540
+ if (!pending.evidenceIds.has(evidenceId)) {
12541
+ const ev = await getMemoryEvidenceVNext(db, ownerId, evidenceId);
12542
+ if (!ev) {
12543
+ throw new Error(
12544
+ `memory state ${st.id} references missing evidence ${evidenceId} (owner ${ownerId})`
12545
+ );
12546
+ }
12547
+ }
12548
+ }
12549
+ const existing = await getMemoryStateVNext(db, ownerId, st.id);
12550
+ if (existing) {
12551
+ if (existing.retiredAt) {
12552
+ throw new Error(
12553
+ `cannot update retired memory state ${st.id} (owner ${ownerId})`
12554
+ );
12555
+ }
12556
+ if (existing.entityId !== st.entityId || existing.facet !== st.facet) {
12557
+ throw new Error(
12558
+ `state ${st.id} already exists with different entity/facet (${existing.entityId}/${existing.facet} vs ${st.entityId}/${st.facet}); use supersede instead`
12559
+ );
12560
+ }
12561
+ }
12562
+ if (!existing) {
12563
+ const existingStates = await listMemoryStatesForEntityVNext(db, ownerId, st.entityId);
12564
+ for (const es of existingStates) {
12565
+ if (es.facet === st.facet && !es.retiredAt) {
12566
+ if (!supersededIds.has(es.id)) {
12567
+ throw new Error(
12568
+ `entity ${st.entityId} facet ${st.facet} already has current state ${es.id}; supersede it or use a different facet`
12569
+ );
12570
+ }
12571
+ }
12572
+ }
12573
+ }
12574
+ }
12575
+ for (const sid of mutation.supersededStateIds ?? []) {
12576
+ const existing = await getMemoryStateVNext(db, ownerId, sid);
12577
+ if (!existing) {
12578
+ throw new Error(
12579
+ `cannot supersede missing memory state: ${sid} (owner ${ownerId})`
12580
+ );
12581
+ }
12582
+ if (existing.retiredAt) {
12583
+ throw new Error(
12584
+ `cannot supersede already retired memory state: ${sid} (owner ${ownerId})`
12585
+ );
12586
+ }
12587
+ const replacement = (mutation.states ?? []).find(
12588
+ (st) => st.entityId === existing.entityId && st.facet === existing.facet
12589
+ );
12590
+ if (!replacement) {
12591
+ throw new Error(
12592
+ `superseded state ${sid} (entity ${existing.entityId} facet ${existing.facet}) has no replacement in this mutation`
12593
+ );
12594
+ }
12595
+ }
12596
+ for (const rel of mutation.relations ?? []) {
12597
+ for (const [side, ref] of [
12598
+ ["from", rel.from],
12599
+ ["to", rel.to]
12600
+ ]) {
12601
+ if (pending.entityIds.has(ref) || pending.stateIds.has(ref) || pending.evidenceIds.has(ref)) {
12602
+ continue;
12603
+ }
12604
+ const resolved = await getMemoryEntityVNext(db, ownerId, ref) ?? await getMemoryStateVNext(db, ownerId, ref) ?? await getMemoryEvidenceVNext(db, ownerId, ref);
12605
+ if (!resolved) {
12606
+ throw new Error(
12607
+ `memory relation ${rel.id} references missing ${side} ${ref} (owner ${ownerId})`
12608
+ );
12609
+ }
12610
+ }
12611
+ }
12612
+ }
12613
+ var applyMemoryInterpreterMutation = async (db, ownerId, mutation) => {
12614
+ await validateMutation(db, ownerId, mutation);
12615
+ const batch = db.batch();
12616
+ for (const ev of mutation.evidence ?? []) {
12617
+ _appendEvidenceBatch(batch, ev);
12618
+ }
12619
+ for (const ent of mutation.entities ?? []) {
12620
+ _appendEntityBatch(batch, ent);
12621
+ }
12622
+ for (const st of mutation.states ?? []) {
12623
+ _appendStateBatch(batch, st);
12624
+ }
12625
+ for (const sid of mutation.supersededStateIds ?? []) {
12626
+ const existing = await getMemoryStateVNext(db, ownerId, sid);
12627
+ if (!existing) {
12628
+ throw new Error(
12629
+ `cannot supersede missing memory state: ${sid} (owner ${ownerId})`
12630
+ );
12631
+ }
12632
+ const replacement = (mutation.states ?? []).find(
12633
+ (st) => st.entityId === existing.entityId && st.facet === existing.facet
12634
+ );
12635
+ const retired = {
12636
+ ...existing,
12637
+ retiredAt: replacement?.updatedAt ?? (/* @__PURE__ */ new Date()).toISOString()
12638
+ };
12639
+ _appendRetiredStateBatch(batch, retired);
12640
+ }
12641
+ for (const rel of mutation.relations ?? []) {
12642
+ _appendRelationBatch(batch, rel);
12643
+ }
12644
+ await batch.write();
12645
+ };
12646
+
12647
+ // packages/ai/memory/vnext/legacyMigrationApply.ts
12648
+ var isSameLegacyMemoryEvidence = (left, right) => left.id === right.id && left.ownerId === right.ownerId && left.text === right.text && left.sourceRef === right.sourceRef && left.confidence === right.confidence && left.createdAt === right.createdAt;
12649
+ var applyLegacyMemoryEvidence = async (db, item) => {
12650
+ const incoming = buildLegacyMemoryEvidence(item);
12651
+ const existing = await getMemoryEvidenceVNextStrict(db, incoming.ownerId, incoming.id);
12652
+ if (existing) {
12653
+ return isSameLegacyMemoryEvidence(existing, incoming) ? { status: "already_exists", evidence: existing } : { status: "conflict", existing, incoming };
12654
+ }
12655
+ await putMemoryEvidenceVNext(db, incoming);
12656
+ return { status: "created", evidence: incoming };
12657
+ };
12658
+
12659
+ // packages/ai/memory/vnext/types.ts
12660
+ var MEMORY_ENTITY_TYPES = [
12661
+ "person",
12662
+ "organization",
12663
+ "project",
12664
+ "place",
12665
+ "object",
12666
+ "activity",
12667
+ "concept"
12668
+ ];
12669
+ var MEMORY_RELATION_TYPES = [
12670
+ "related_to",
12671
+ "part_of",
12672
+ "uses",
12673
+ "involves",
12674
+ "supersedes"
12675
+ ];
12676
+
12677
+ // packages/ai/memory/vnext/interpreterWireProtocol.ts
12678
+ var isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
12679
+ var isString = (value) => typeof value === "string" && value.trim().length > 0;
12680
+ var isStringArray = (value) => Array.isArray(value) && value.every(isString);
12681
+ var optionalArray = (value, validate2) => value === void 0 || Array.isArray(value) && value.every(validate2);
12682
+ var ACTIONS = /* @__PURE__ */ new Set([
12683
+ "no_op",
12684
+ "create",
12685
+ "update",
12686
+ "specialize",
12687
+ "supersede",
12688
+ "ambiguous"
12689
+ ]);
12690
+ var validateEvidence = (value) => {
12691
+ if (!isObject(value)) return false;
12692
+ return isString(value.key) && isString(value.text) && typeof value.confidence === "number" && Number.isFinite(value.confidence) && value.confidence >= 0 && value.confidence <= 1 && (value.sourceRef === void 0 || isString(value.sourceRef));
12693
+ };
12694
+ var validateEntity = (value) => {
12695
+ if (!isObject(value)) return false;
12696
+ return isString(value.key) && MEMORY_ENTITY_TYPES.includes(value.type) && isString(value.name) && (value.subtype === void 0 || isString(value.subtype)) && (value.aliases === void 0 || isStringArray(value.aliases));
12697
+ };
12698
+ var validateState = (value) => {
12699
+ if (!isObject(value)) return false;
12700
+ return (value.stateId === void 0 || isString(value.stateId)) && (value.supersedes === void 0 || isString(value.supersedes)) && isString(value.entityRef) && isString(value.facet) && "value" in value && isString(value.text) && isStringArray(value.evidenceRefs);
12701
+ };
12702
+ var validateRelation = (value) => {
12703
+ if (!isObject(value)) return false;
12704
+ return isString(value.fromRef) && MEMORY_RELATION_TYPES.includes(value.type) && isString(value.toRef);
12705
+ };
12706
+ var stripJsonFence = (raw) => {
12707
+ const trimmed = raw.trim();
12708
+ const match = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);
12709
+ return match ? match[1].trim() : trimmed;
12710
+ };
12711
+ var parseMemoryInterpreterWireMutation = (raw) => {
12712
+ let parsed;
12713
+ try {
12714
+ parsed = JSON.parse(stripJsonFence(raw));
12715
+ } catch {
12716
+ return { ok: false, error: "interpreter output is not valid JSON" };
12717
+ }
12718
+ if (!isObject(parsed)) return { ok: false, error: "interpreter output must be an object" };
12719
+ if (!isString(parsed.action) || !ACTIONS.has(parsed.action)) {
12720
+ return { ok: false, error: "invalid reconciliation action" };
12721
+ }
12722
+ if (!isString(parsed.reason)) return { ok: false, error: "reason is required" };
12723
+ if (!optionalArray(parsed.evidence, validateEvidence)) return { ok: false, error: "invalid evidence array" };
12724
+ if (!optionalArray(parsed.entities, validateEntity)) return { ok: false, error: "invalid entities array" };
12725
+ if (!optionalArray(parsed.states, validateState)) return { ok: false, error: "invalid states array" };
12726
+ if (!optionalArray(parsed.relations, validateRelation)) return { ok: false, error: "invalid relations array" };
12727
+ return { ok: true, value: parsed };
12728
+ };
12729
+ var buildMemoryInterpreterWireContract = () => `
12730
+ Return ONLY one JSON object:
12731
+ {
12732
+ "action": "no_op|create|update|specialize|supersede|ambiguous",
12733
+ "reason": "short explanation",
12734
+ "evidence": [{ "key": "e1", "text": "...", "confidence": 0.9, "sourceRef": "optional" }],
12735
+ "entities": [{ "key": "n1", "type": "person|organization|project|place|object|activity|concept", "name": "...", "subtype": "optional", "aliases": [] }],
12736
+ "states": [{ "stateId": "optional-existing-state-id", "supersedes": "optional-existing-state-id-this-statement-replaces", "entityRef": "existing-entity-id-or-new-entity-key", "facet": "...", "value": {}, "text": "...", "evidenceRefs": ["e1"] }],
12737
+ "relations": [{ "fromRef": "existing-id-or-local-key", "type": "related_to|part_of|uses|involves|supersedes", "toRef": "existing-id-or-local-key" }]
12738
+ }
12739
+
12740
+ Do not generate persistent ids, owner ids, timestamps or storage keys. Runtime owns identity and time.
12741
+ Use short local keys only to connect records created in the SAME response. Every local key must be unique across evidence/entities.
12742
+ State evidenceRefs may reference Evidence emitted in this response. Evidence ids marked referenceable in the supplied memory context may be referenced directly. Do not reference any other prior Evidence id.
12743
+ When updating an existing State, reuse its exact stateId, exact entityRef and exact facet. Runtime preserves that State's original entity/facet/createdAt and appends new Evidence provenance.
12744
+ When creating a new State, omit stateId.
12745
+ For supersede: emit the replacing statement as a NEW State (omit stateId) and set supersedes to the exact id of the replaced current State. Runtime retires the replaced State \u2014 it stays recoverable as history but is no longer current, and the new State becomes the only current one for that facet. Never update or supersede an already retired State.
12746
+ supersedes and stateId are mutually exclusive on one State.
12747
+ Do not emit an existing Entity again merely to update its State; reference its exact id in entityRef.
12748
+ Relations may reference only known persisted Entity/State/Evidence ids or local Entity/Evidence keys emitted in the same response.
12749
+ Do not invent new Entity or Relation types. Keep expressive detail in subtype/facet/value/text.
12750
+ `.trim();
12751
+ var assertUniqueLocalKeys = (wire) => {
12752
+ const seen = /* @__PURE__ */ new Set();
12753
+ for (const item of [...wire.evidence ?? [], ...wire.entities ?? []]) {
12754
+ if (seen.has(item.key)) throw new Error(`duplicate local key: ${item.key}`);
12755
+ seen.add(item.key);
12756
+ }
12757
+ };
12758
+ var materializeMemoryInterpreterMutation = (input) => {
12759
+ assertUniqueLocalKeys(input.wire);
12760
+ const now = input.now ?? (/* @__PURE__ */ new Date()).toISOString();
12761
+ const nextId = input.nextId ?? ulid;
12762
+ const existingStates = new Map((input.existingStates ?? []).map((state2) => [state2.id, state2]));
12763
+ const existingStateIds = new Set(existingStates.keys());
12764
+ const existingEvidenceIds = new Set(
12765
+ [...existingStates.values()].flatMap((state2) => state2.evidenceIds)
12766
+ );
12767
+ const existingEntityIds = new Set(input.existingEntityIds ?? []);
12768
+ const entityRefs = /* @__PURE__ */ new Map();
12769
+ const evidenceRefs = /* @__PURE__ */ new Map();
12770
+ const evidence = (input.wire.evidence ?? []).map((item) => {
12771
+ const id = nextId();
12772
+ evidenceRefs.set(item.key, id);
12773
+ return {
12774
+ id,
12775
+ ownerId: input.ownerId,
12776
+ text: item.text,
12777
+ ...item.sourceRef ? { sourceRef: item.sourceRef } : {},
12778
+ confidence: item.confidence,
12779
+ createdAt: now
12780
+ };
12781
+ });
12782
+ const entities = (input.wire.entities ?? []).map((item) => {
12783
+ const id = nextId();
12784
+ entityRefs.set(item.key, id);
12785
+ return {
12786
+ id,
12787
+ ownerId: input.ownerId,
12788
+ type: item.type,
12789
+ name: item.name,
12790
+ ...item.subtype ? { subtype: item.subtype } : {},
12791
+ ...item.aliases ? { aliases: item.aliases } : {},
12792
+ createdAt: now,
12793
+ updatedAt: now
12794
+ };
12795
+ });
12796
+ const resolveEntityRef = (ref) => {
12797
+ const created = entityRefs.get(ref);
12798
+ if (created) return created;
12799
+ if (existingEntityIds.has(ref)) return ref;
12800
+ throw new Error(`unknown entity ref: ${ref}`);
12801
+ };
12802
+ const supersededStateIds = [];
12803
+ const referenceableEvidenceIds = new Set(input.referenceableEvidenceIds ?? []);
12804
+ const resolveEvidenceRef = (ref) => {
12805
+ const created = evidenceRefs.get(ref);
12806
+ if (created) return created;
12807
+ if (referenceableEvidenceIds.has(ref)) return ref;
12808
+ throw new Error(`unknown evidence ref: ${ref}`);
12809
+ };
12810
+ const states = (input.wire.states ?? []).map((item) => {
12811
+ const prior = item.stateId ? existingStates.get(item.stateId) : void 0;
12812
+ if (item.stateId && !prior) {
12813
+ throw new Error(`unknown existing state: ${item.stateId}`);
12814
+ }
12815
+ if (prior?.retiredAt) {
12816
+ throw new Error(`retired state cannot be updated: ${item.stateId}`);
12817
+ }
12818
+ if (prior && item.entityRef !== prior.entityId) {
12819
+ throw new Error(`existing state entityRef changed: ${item.stateId}`);
12820
+ }
12821
+ if (prior && item.facet !== prior.facet) {
12822
+ throw new Error(`existing state facet changed: ${item.stateId}`);
12823
+ }
12824
+ let supersededStateId;
12825
+ if (item.supersedes !== void 0) {
12826
+ if (input.wire.action !== "supersede") {
12827
+ throw new Error(
12828
+ `supersedes is only valid for the supersede action, got ${input.wire.action}`
12829
+ );
12830
+ }
12831
+ if (item.stateId !== void 0) {
12832
+ throw new Error("supersedes and stateId are mutually exclusive on one state");
12833
+ }
12834
+ const superseded = existingStates.get(item.supersedes);
12835
+ if (!superseded) {
12836
+ throw new Error(`unknown superseded state id: ${item.supersedes}`);
12837
+ }
12838
+ if (superseded.retiredAt) {
12839
+ throw new Error(`retired state cannot be superseded: ${item.supersedes}`);
12840
+ }
12841
+ if (supersededStateIds.includes(item.supersedes)) {
12842
+ throw new Error(`duplicate superseded state id: ${item.supersedes}`);
12843
+ }
12844
+ supersededStateId = item.supersedes;
12845
+ }
12846
+ const newEvidenceIds = item.evidenceRefs.map(resolveEvidenceRef);
12847
+ const id = prior?.id ?? nextId();
12848
+ if (supersededStateId !== void 0) supersededStateIds.push(supersededStateId);
12849
+ return {
12850
+ id,
12851
+ ownerId: input.ownerId,
12852
+ entityId: prior?.entityId ?? resolveEntityRef(item.entityRef),
12853
+ facet: prior?.facet ?? item.facet,
12854
+ value: item.value,
12855
+ text: item.text,
12856
+ evidenceIds: [.../* @__PURE__ */ new Set([...prior?.evidenceIds ?? [], ...newEvidenceIds])],
12857
+ createdAt: prior?.createdAt ?? now,
12858
+ updatedAt: now
12859
+ };
12860
+ });
12861
+ const resolveRelationRef = (ref) => {
12862
+ const createdEntity = entityRefs.get(ref);
12863
+ if (createdEntity) return createdEntity;
12864
+ const createdEvidence = evidenceRefs.get(ref);
12865
+ if (createdEvidence) return createdEvidence;
12866
+ if (existingEntityIds.has(ref) || existingStateIds.has(ref) || existingEvidenceIds.has(ref)) {
12867
+ return ref;
12868
+ }
12869
+ throw new Error(`unknown relation ref: ${ref}`);
12870
+ };
12871
+ const relations = (input.wire.relations ?? []).map((item) => ({
12872
+ id: nextId(),
12873
+ ownerId: input.ownerId,
12874
+ from: resolveRelationRef(item.fromRef),
12875
+ type: item.type,
12876
+ to: resolveRelationRef(item.toRef),
12877
+ createdAt: now
12878
+ }));
12879
+ return {
12880
+ action: input.wire.action,
12881
+ reason: input.wire.reason,
12882
+ ...evidence.length ? { evidence } : {},
12883
+ ...entities.length ? { entities } : {},
12884
+ ...states.length ? { states } : {},
12885
+ ...supersededStateIds.length ? { supersededStateIds } : {},
12886
+ ...relations.length ? { relations } : {}
12887
+ };
12888
+ };
12889
+
12890
+ // packages/ai/memory/vnext/interpreterPrompt.ts
12891
+ var MEMORY_VNEXT_ENTITY_CREATION_CONTRACT = `
12892
+ Entity creation is NOT named-entity recognition.
12893
+
12894
+ Create a new long-lived Entity only when the subject has future continuity value.
12895
+ A mention alone is insufficient.
12896
+
12897
+ Prefer leaving information as Evidence until at least one strong continuity signal exists:
12898
+ 1. The subject recurs across independent dialogs, or the user explicitly describes it as enduring.
12899
+ 2. The user connects it to their own preferences, relationships, goals, identity, worldview, ongoing work, or repeated activity.
12900
+ 3. Future conversations are likely to refer back to it under different wording, so a stable anchor materially improves cross-dialog continuity.
12901
+ 4. Creating the anchor is necessary to update or relate existing long-lived State.
12902
+
12903
+ Do NOT create an Entity merely because the text contains a named person, product, place, historical event, book, philosopher, food, device, or concept.
12904
+ When uncertain, keep Evidence and defer Entity creation.
12905
+
12906
+ Concept-specific rule:
12907
+ - concept is for abstract subjects that become part of the user's personal meaning world, such as freedom, absurdism, privacy, modernity, beauty, loneliness, safety, or maintainability.
12908
+ - Do not create a Concept Entity for every topic discussed or external fact queried.
12909
+ - Promote a concept only when it recurs, carries a durable user stance/interest/tension, connects multiple memories, or is likely to become a future recall/navigation anchor.
12910
+ - A one-off question such as "when was Kant born?" should normally create no long-lived Concept or Person Entity. Repeated discussion about Kant, freedom, and the user's own values may justify them.
12911
+ `.trim();
12912
+ var MEMORY_VNEXT_THIRD_PARTY_RELEVANCE_CONTRACT = `
12913
+ Third-party relevance test:
12914
+ - A fact about another person is durable only when it concerns the user's own world. Observation-only third-party facts \u2014 someone's appearance, news, habits, or the doings of a person the user merely encounters \u2014 stay Evidence, no matter how often they recur.
12915
+ - Closeness of that person, and repeated mentions across independent dialogs, do not by themselves make such an observation durable: never invent a user-world consequence to justify promotion (for example "the user keeps watching this person").
12916
+ - Matters that are part of the user's own shared life \u2014 household or family arrangements the user takes part in, a family member's health, or what the user relies on someone for \u2014 do belong to the user's world: state the user-world side (caregiving, changed time, changed plans, a sustained constraint) instead of a chronicle of the other person's doings.
12917
+ `.trim();
12918
+ var MEMORY_VNEXT_RECONCILIATION_CONTRACT = `
12919
+ When durable understanding changes, choose the smallest correct reconciliation:
12920
+ - no_op: no durable change.
12921
+ - create: genuinely new durable state/entity.
12922
+ - update: advance an existing state without changing its scope.
12923
+ - specialize: add a scoped exception/qualification; do not replace the broader state.
12924
+ - supersede: a newer statement explicitly replaces old current state. Emit the replacement as a NEW State and name the replaced current State in its supersedes field; runtime retires the old one (kept as recoverable history, no longer current).
12925
+ - ambiguous: evidence conflicts with current state, but replacement vs exception cannot be determined reliably.
12926
+
12927
+ Examples:
12928
+ - "\u56DE\u7B54\u7B80\u5355" then "\u5B89\u5168\u548C bug \u6392\u67E5\u8BE6\u7EC6" => specialize, not supersede.
12929
+ - "\u6211\u6709 Ollama Pro" then "\u6211\u5DF2\u7ECF\u4E0D\u7528 Ollama Pro \u4E86" => supersede.
12930
+ - Repeatedly choosing thin noodles without explicitly rejecting thick noodles => update or ambiguous, not automatic supersession.
12931
+
12932
+ Preserve ambiguity. Do not ask the user merely to clean memory. A later task may ask only if the unresolved state materially changes the answer.
12933
+ `.trim();
12934
+ var buildMemoryVNextInterpreterPrompt = () => [
12935
+ "You maintain long-lived adaptive relationship memory backed by evidence.",
12936
+ "External dialogs/files/web facts remain authoritative outside memory.",
12937
+ "Prefer a small stable set of Entities and updateable States over an ever-growing bag of facts.",
12938
+ MEMORY_VNEXT_ENTITY_CREATION_CONTRACT,
12939
+ MEMORY_VNEXT_THIRD_PARTY_RELEVANCE_CONTRACT,
12940
+ MEMORY_VNEXT_RECONCILIATION_CONTRACT,
12941
+ buildMemoryInterpreterWireContract()
12942
+ ].join("\n\n");
12943
+
12944
+ // packages/ai/memory/vnext/legacyMigrationShadow.ts
12945
+ var LEGACY_MIGRATION_SHADOW_INSTRUCTION = `
12946
+ This Evidence came from a legacy memory record.
12947
+ Legacy kind and subject metadata are advisory hints only.
12948
+ Re-evaluate it under the current vNext contract.
12949
+ Do not mechanically map:
12950
+ - semantic -> State
12951
+ - procedural -> procedure State
12952
+ - subjectType=agent -> agent Entity
12953
+ - visibility -> scope
12954
+ Prefer Evidence-only when durable semantic value is unclear.
12955
+ `.trim();
12956
+ var serializeExisting = (existing) => JSON.stringify({
12957
+ entityIds: existing?.entityIds ?? [],
12958
+ states: (existing?.states ?? []).map((state2) => ({
12959
+ stateId: state2.id,
12960
+ entityId: state2.entityId,
12961
+ facet: state2.facet,
12962
+ text: state2.text
12963
+ }))
12964
+ });
12965
+ var buildLegacyMigrationInterpreterInput = (input) => [
12966
+ LEGACY_MIGRATION_SHADOW_INSTRUCTION,
12967
+ `The legacy record's Evidence is marked referenceable with id "${input.evidence.id}" and sourceRef "${input.evidence.sourceRef}". Evidence ids marked referenceable in the supplied memory context may be referenced directly. When a State cites it, reference that exact id in evidenceRefs. Do not emit another Evidence for this legacy record.`,
12968
+ `Imported Evidence (text is the authoritative legacy content, referenceable directly):
12969
+ ${JSON.stringify(
12970
+ {
12971
+ ...input.evidence,
12972
+ referenceable: true
12973
+ },
12974
+ null,
12975
+ 2
12976
+ )}`,
12977
+ `Legacy metadata (advisory hints only, NOT vNext semantics):
12978
+ ${JSON.stringify(
12979
+ {
12980
+ kind: input.item.kind,
12981
+ visibility: input.item.visibility,
12982
+ subjectType: input.item.subjectType,
12983
+ subjectId: input.item.subjectId,
12984
+ facet: input.item.facet,
12985
+ importance: input.item.importance,
12986
+ resident: input.item.resident
12987
+ },
12988
+ null,
12989
+ 2
12990
+ )}`,
12991
+ `Existing vNext context for this principal (caller-supplied shadow snapshot):
12992
+ ${serializeExisting(
12993
+ input.existing
12994
+ )}`
12995
+ ].join("\n\n");
12996
+ var mergeImportedEvidence = (mutation, imported) => {
12997
+ const emitted = mutation.evidence ?? [];
12998
+ const duplicatedIds = new Set(
12999
+ emitted.filter(
13000
+ (item) => item.sourceRef === imported.sourceRef || // A re-emitted copy that drops sourceRef is still the same legacy record:
13001
+ // text equality is the only identity left on the wire to catch it.
13002
+ item.sourceRef === void 0 && item.text === imported.text
13003
+ ).map((item) => item.id)
13004
+ );
13005
+ const rewrite = (id) => duplicatedIds.has(id) ? imported.id : id;
13006
+ return {
13007
+ ...mutation,
13008
+ evidence: [imported, ...emitted.filter((item) => !duplicatedIds.has(item.id))],
13009
+ ...mutation.states ? {
13010
+ states: mutation.states.map((state2) => ({
13011
+ ...state2,
13012
+ evidenceIds: [...new Set(state2.evidenceIds.map(rewrite))]
13013
+ }))
13014
+ } : {},
13015
+ ...mutation.relations ? {
13016
+ relations: mutation.relations.map((relation) => ({
13017
+ ...relation,
13018
+ from: rewrite(relation.from),
13019
+ to: rewrite(relation.to)
13020
+ }))
13021
+ } : {}
13022
+ };
13023
+ };
13024
+ var runLegacyMemoryMigrationShadow = async (input) => {
13025
+ const principal = resolveLegacyMemoryPrincipal(input.item);
13026
+ const evidence = buildLegacyMemoryEvidence(input.item);
13027
+ const completion = await input.provider.complete(
13028
+ [
13029
+ { role: "system", content: buildMemoryVNextInterpreterPrompt() },
13030
+ {
13031
+ role: "user",
13032
+ content: buildLegacyMigrationInterpreterInput({
13033
+ item: input.item,
13034
+ evidence,
13035
+ ...input.existing ? { existing: input.existing } : {}
13036
+ })
13037
+ }
13038
+ ],
13039
+ input.timeoutMs === void 0 ? void 0 : { timeoutMs: input.timeoutMs }
13040
+ );
13041
+ const parsed = parseMemoryInterpreterWireMutation(completion.content);
13042
+ if (!parsed.ok) {
13043
+ throw new Error(`legacy migration shadow: unparsable interpreter response (${parsed.error})`);
13044
+ }
13045
+ const candidate = materializeMemoryInterpreterMutation({
13046
+ // The principal identity (user:/space:/system:<id>) is the migration owner
13047
+ // space, so a space migration can never land in a user space.
13048
+ ownerId: evidence.ownerId,
13049
+ wire: parsed.value,
13050
+ ...input.existing?.states ? { existingStates: [...input.existing.states] } : {},
13051
+ ...input.existing?.entityIds ? { existingEntityIds: [...input.existing.entityIds] } : {},
13052
+ referenceableEvidenceIds: [evidence.id],
13053
+ ...input.now === void 0 ? {} : { now: input.now },
13054
+ ...input.nextId ? { nextId: input.nextId } : {}
13055
+ });
13056
+ const mutation = mergeImportedEvidence(candidate, evidence);
13057
+ return {
13058
+ principal,
13059
+ evidence,
13060
+ wire: parsed.value,
13061
+ mutation,
13062
+ action: mutation.action,
13063
+ reason: mutation.reason
13064
+ };
13065
+ };
13066
+
13067
+ // packages/ai/memory/vnext/lazyPromotion.ts
13068
+ var ERROR_MESSAGE_MAX2 = 120;
13069
+ var inFlight = /* @__PURE__ */ new Set();
13070
+ var shortError2 = (error) => {
13071
+ const message = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
13072
+ return message.length > ERROR_MESSAGE_MAX2 ? `${message.slice(0, ERROR_MESSAGE_MAX2)}\u2026` : message;
13073
+ };
13074
+ var loadExistingContext = async (db, ownerId) => {
13075
+ const entities = await listMemoryEntitiesVNext(db, ownerId);
13076
+ const states = (await Promise.all(
13077
+ entities.map((entity) => listMemoryStatesForEntityVNext(db, ownerId, entity.id))
13078
+ )).flat();
13079
+ return {
13080
+ entityIds: entities.map((entity) => entity.id),
13081
+ states
13082
+ };
13083
+ };
13084
+ var withoutImportedEvidence = (mutation, importedEvidenceId) => ({
13085
+ ...mutation,
13086
+ evidence: (mutation.evidence ?? []).filter((evidence) => evidence.id !== importedEvidenceId)
13087
+ });
13088
+ var hasWrites = (mutation) => (mutation.evidence?.length ?? 0) > 0 || (mutation.entities?.length ?? 0) > 0 || (mutation.states?.length ?? 0) > 0 || (mutation.supersededStateIds?.length ?? 0) > 0 || (mutation.relations?.length ?? 0) > 0;
13089
+ var runMemoryVNextLazyPromotion = async (input) => {
13090
+ const clock = input.clock ?? (() => performance.now());
13091
+ const startedAt = clock();
13092
+ const emit = input.emit ?? ((observation) => console.info("[memory] vnext lazy promotion", {
13093
+ event: "memory_vnext_lazy_promotion",
13094
+ ...observation
13095
+ }));
13096
+ const finish = (partial) => {
13097
+ const observation = {
13098
+ ...partial,
13099
+ latencyMs: Math.round((clock() - startedAt) * 10) / 10
13100
+ };
13101
+ try {
13102
+ emit(observation);
13103
+ } catch {
13104
+ }
13105
+ return observation;
13106
+ };
13107
+ const deterministicEvidence = buildLegacyMemoryEvidence(input.item);
13108
+ const flightKey = `${deterministicEvidence.ownerId}\0${deterministicEvidence.id}`;
13109
+ if (inFlight.has(flightKey)) {
13110
+ return finish({ status: "in_flight" });
13111
+ }
13112
+ inFlight.add(flightKey);
13113
+ try {
13114
+ const evidenceResult = await applyLegacyMemoryEvidence(input.db, input.item);
13115
+ if (evidenceResult.status === "conflict") {
13116
+ return finish({ status: "conflict" });
13117
+ }
13118
+ const evidence = evidenceResult.evidence;
13119
+ const existing = await loadExistingContext(input.db, evidence.ownerId);
13120
+ const alreadyCovered = (existing.states ?? []).some(
13121
+ (state2) => !state2.retiredAt && state2.evidenceIds.includes(evidence.id)
13122
+ );
13123
+ if (alreadyCovered) {
13124
+ return finish({ status: "already_covered" });
13125
+ }
13126
+ const interpreted = await runLegacyMemoryMigrationShadow({
13127
+ item: input.item,
13128
+ provider: input.provider,
13129
+ existing,
13130
+ ...input.timeoutMs === void 0 ? {} : { timeoutMs: input.timeoutMs },
13131
+ ...input.now === void 0 ? {} : { now: input.now },
13132
+ ...input.nextId ? { nextId: input.nextId } : {}
13133
+ });
13134
+ const semanticMutation = withoutImportedEvidence(interpreted.mutation, evidence.id);
13135
+ if (!hasWrites(semanticMutation)) {
13136
+ return finish({ status: "no_op", action: semanticMutation.action });
13137
+ }
13138
+ await applyMemoryInterpreterMutation(input.db, evidence.ownerId, semanticMutation);
13139
+ return finish({ status: "promoted", action: semanticMutation.action });
13140
+ } catch (error) {
13141
+ return finish({ status: "error", error: shortError2(error) });
13142
+ } finally {
13143
+ inFlight.delete(flightKey);
13144
+ }
13145
+ };
13146
+ var isMemoryVNextLazyPromotionEnabled = (env = typeof process !== "undefined" ? process.env : {}) => env.NOLO_MEMORY_VNEXT_LAZY_PROMOTION === "1" || env.NOLO_MEMORY_VNEXT_LAZY_PROMOTION === "true";
13147
+
12076
13148
  // packages/ai/memory/runtime.ts
12077
13149
  var COLD_STORAGE_CONFIDENCE = 0.3;
12078
13150
  var MEMORY_OVERLAY_TOKEN_BUDGET = DEFAULT_MEMORY_OVERLAY_TOKEN_BUDGET;
@@ -12196,6 +13268,7 @@ var resolveMemoryRuntime = async (input) => {
12196
13268
  return { selectedItems: [], promptBlock: null };
12197
13269
  }
12198
13270
  const policy = resolveAgentMemoryPolicy({ agentKey: input.agentKey });
13271
+ const legacyStartedAt = performance.now();
12199
13272
  const candidates = await loadMemoryCandidatesFromDb(input.db, {
12200
13273
  owners,
12201
13274
  subjects: buildMemorySubjectsForAgent({
@@ -12222,13 +13295,40 @@ var resolveMemoryRuntime = async (input) => {
12222
13295
  input.userInput
12223
13296
  );
12224
13297
  const selected = [...residentItems, ...ranked];
13298
+ const legacyLatencyMs = Math.round((performance.now() - legacyStartedAt) * 10) / 10;
13299
+ const promptBlock = selected.length === 0 ? null : buildMemoryOverlay(selected, { maxTokens: MEMORY_OVERLAY_TOKEN_BUDGET });
13300
+ const shadowEnabled = input.vNextShadowProvider != null && isMemoryVNextShadowReadEnabled();
13301
+ if (shadowEnabled) {
13302
+ await runMemoryVNextShadowRead({
13303
+ ctx: {
13304
+ db: input.db,
13305
+ owners,
13306
+ query: input.userInput,
13307
+ provider: input.vNextShadowProvider
13308
+ },
13309
+ legacyItems: selected,
13310
+ legacyLatencyMs,
13311
+ legacyContextChars: promptBlock?.length ?? 0
13312
+ });
13313
+ }
13314
+ const promotionCandidate = selected[0];
13315
+ const lazyPromotionEnabled = input.vNextShadowProvider != null && isMemoryVNextLazyPromotionEnabled();
13316
+ if (lazyPromotionEnabled && promotionCandidate) {
13317
+ void runMemoryVNextLazyPromotion({
13318
+ db: input.db,
13319
+ item: promotionCandidate,
13320
+ provider: input.vNextShadowProvider,
13321
+ ...input.lazyPromotionEmit ? { emit: input.lazyPromotionEmit } : {}
13322
+ }).catch(() => {
13323
+ });
13324
+ }
12225
13325
  if (selected.length === 0) {
12226
13326
  return { selectedItems: [], promptBlock: null };
12227
13327
  }
12228
13328
  await touchMemoryItemsInDb(input.db, selected);
12229
13329
  return {
12230
13330
  selectedItems: selected,
12231
- promptBlock: buildMemoryOverlay(selected, { maxTokens: MEMORY_OVERLAY_TOKEN_BUDGET })
13331
+ promptBlock
12232
13332
  };
12233
13333
  };
12234
13334
 
@@ -12272,7 +13372,8 @@ var queryLocalMemory = async (env, agentKey, userInput, spaceId) => {
12272
13372
  userId: machineId,
12273
13373
  agentKey,
12274
13374
  userInput,
12275
- ...spaceId ? { spaceId } : {}
13375
+ ...spaceId ? { spaceId } : {},
13376
+ ...isMemoryVNextShadowReadEnabled(env) ? { vNextShadowProvider: void 0 } : {}
12276
13377
  });
12277
13378
  return resolution.promptBlock;
12278
13379
  };