@adep/runtime 0.3.14 → 0.3.16

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.
@@ -3,7 +3,7 @@ import type { CapabilityBundle } from '@adep/types';
3
3
  import type { ProjectDriver } from '../../db/project-driver';
4
4
  export interface CreateDbCapabilityOptions {
5
5
  /**
6
- * 写终端(insert / insertMany / update / delete)执行前的项目级配额钩子(BILL-005,§2.7.3)。
6
+ * 写终端(insert / insertMany / update / delete / upsert)执行前的项目级配额钩子(BILL-005,§2.7.3)。
7
7
  * 由 provider 绑定 projectId + dataDir 注入(方言无关 usageStats 量当前用量,超限抛 429 拒写)。
8
8
  * 缺省不注入 → 不做写前配额门(fail-open 过渡)。
9
9
  */
@@ -11,6 +11,13 @@ export interface CreateDbCapabilityOptions {
11
11
  }
12
12
  /** 链式能力的装配规格:FN worker 只按方法名清单构造客户端,不感知 db 细节。 */
13
13
  export declare const CLOUD_DB_SPEC: ChainSpec;
14
+ /**
15
+ * 写终端:这些终值方法在事务期间必须走事务句柄(否则 `DB_UNSAFE_OP`),也是配额钩子
16
+ * `beforeWrite` 的判定集合。必须与 `CLOUD_DB_SPEC.terminalMethods` 里的写方法一一对应——
17
+ * DB-010 新增 `upsert` 时只登记了规格清单、漏了这里(提交说明声称已补但 diff 没改),
18
+ * 于是 upsert 绕过事务外写拒绝与 BILL-005 配额门。防漂移门 spec-drift.test.ts 用例 ④ 钉住它。
19
+ */
20
+ export declare const WRITE_TERMINALS: ReadonlySet<string>;
14
21
  /**
15
22
  * 按驱动产出一组 `cloud.db` 能力。驱动已绑定项目库(连接层隔离,DB-001);
16
23
  * `projectId` 在装配期绑定,函数侧不可感知。内部无共享可变状态污染跨项目调用。
@@ -82,12 +82,35 @@ export declare function isDynamicImportRejection(error: unknown): boolean;
82
82
  */
83
83
  export declare function containsDynamicImport(source: string): boolean;
84
84
  /** 构造一个文件模块:返回其 exports(含入口的 default 处理器)。
85
- * 导出:CF-036 状态对象加载器(state-objects/loader.ts)复用同一精简 require 沙箱。 */
86
- export declare function loadModule(path: string, files: Readonly<Record<string, string>>, sandboxGlobals: Record<string, unknown>, cache: Map<string, Record<string, unknown>>, resolveBare?: BareSpecifierResolver, bareCache?: Map<string, unknown>, strictPaths?: boolean): Record<string, unknown>;
85
+ * 导出:CF-036 状态对象加载器(state-objects/loader.ts)复用同一精简 require 沙箱。
86
+ * hints(W2,可选):按路径下发的预编译产物——命中跳过转译/变换与完整 parse,
87
+ * cachedData 被拒静默回退全量编译;context 与模块实例仍每调用新建(隔离红线不变)。 */
88
+ export declare function loadModule(path: string, files: Readonly<Record<string, string>>, sandboxGlobals: Record<string, unknown>, cache: Map<string, Record<string, unknown>>, resolveBare?: BareSpecifierResolver, bareCache?: Map<string, unknown>, strictPaths?: boolean, hints?: Readonly<Record<string, {
89
+ js: string;
90
+ cachedData?: Uint8Array;
91
+ }>>): Record<string, unknown>;
87
92
  export interface LoadFunctionResult {
88
93
  /** 入口的 default 处理器(假设为 async (ctx) => result)。 */
89
94
  handler: (ctx: unknown) => unknown;
90
95
  }
96
+ /**
97
+ * 单文件预编译产物(W2 执行热路径缓存):transpile + 导出变换 + 沙箱 wrapper 包装 +
98
+ * `vm.Script` 预编译(`produceCachedData`)。产物(js 字符串 + cachedData Buffer)可经
99
+ * 结构化克隆跨线程下发,worker 侧用 `new vm.Script(js, { cachedData })` 跳过完整 parse。
100
+ * cachedData 与 Node/V8 版本相关 ⇒ 消费侧必须检查 `cachedDataRejected` 并回退全量编译。
101
+ */
102
+ export interface CompiledModuleCode {
103
+ /** 含 wrapper 前缀的完整脚本源码(与沙箱编译形态逐字节一致)。 */
104
+ js: string;
105
+ /** V8 code cache;宿主环境不支持时为 null(消费侧按无缓存处理)。 */
106
+ cachedData: Buffer | null;
107
+ }
108
+ export declare function compileModuleCode(source: string, filename: string): CompiledModuleCode;
91
109
  /** 装配入口函数:多文件 + 精简 require + 白名单全局;resolveBare 提供裸模块解析(FN-009,可选)。
92
- * strictPaths(CORE-154 `--parity`)= true 时 `../` 越根引用报错(缺省 false 行为不变)。 */
93
- export declare function loadFunctionModule(files: Readonly<Record<string, string>>, entry: string, sandboxGlobals: Record<string, unknown>, resolveBare?: BareSpecifierResolver, strictPaths?: boolean): LoadFunctionResult;
110
+ * strictPaths(CORE-154 `--parity`)= true 时 `../` 越根引用报错(缺省 false 行为不变)。
111
+ * hints(W2,可选):按文件路径下发的预编译产物;命中即跳过转译/变换与完整 parse,
112
+ * `cachedDataRejected` 时静默回退全量编译——语义逐字节不变(见 CompileHint 契约注释)。 */
113
+ export declare function loadFunctionModule(files: Readonly<Record<string, string>>, entry: string, sandboxGlobals: Record<string, unknown>, resolveBare?: BareSpecifierResolver, strictPaths?: boolean, hints?: Readonly<Record<string, {
114
+ js: string;
115
+ cachedData?: Uint8Array;
116
+ }>>): LoadFunctionResult;
package/dist/index.js CHANGED
@@ -311,7 +311,9 @@ var WorkerFunctionExecutor = class {
311
311
  ...input.env === void 0 ? {} : { env: input.env },
312
312
  ...deps === void 0 ? {} : { deps },
313
313
  ...this.parity ? { parity: true } : {},
314
- ...effectiveFetchConfig === void 0 ? {} : { fetch: effectiveFetchConfig }
314
+ ...effectiveFetchConfig === void 0 ? {} : { fetch: effectiveFetchConfig },
315
+ // W2:预编译提示随 workerData 下发(结构化克隆可跨线程;缺省不传 = 全量编译路径)。
316
+ ...input.compileHints === void 0 ? {} : { compileHints: input.compileHints }
315
317
  },
316
318
  resourceLimits: { maxOldGenerationSizeMb: memoryLimitMb }
317
319
  });
@@ -1149,7 +1151,13 @@ var CLOUD_DB_SPEC = {
1149
1151
  directMethods: ["query", "writeQuery", "execute", "changes"],
1150
1152
  transactionMethod: "transaction"
1151
1153
  };
1152
- var WRITE_TERMINALS = /* @__PURE__ */ new Set(["insert", "insertMany", "update", "delete"]);
1154
+ var WRITE_TERMINALS = /* @__PURE__ */ new Set([
1155
+ "insert",
1156
+ "insertMany",
1157
+ "update",
1158
+ "delete",
1159
+ "upsert"
1160
+ ]);
1153
1161
  function errorWithCode(error) {
1154
1162
  if (error instanceof Error) {
1155
1163
  const sqlite = /no such table: (\S+)/.exec(error.message);
@@ -3291,13 +3299,32 @@ var StateObjectRuntime = class {
3291
3299
  }
3292
3300
  /**
3293
3301
  * alarm 轮询:把到期对象唤醒并调 onAlarm(CF-037)。
3302
+ * - 到期集合 = 后端落库到期记录 ∪ **驻留实例的内存 alarmAt(驻留时以内存值为准)**:
3303
+ * `setAlarm`/`clearAlarm` 只写内存、`alarmAt` 随 `flush()` 落库——若 pump 只读后端,
3304
+ * 「setAlarm 后没 flush」会静默丢调度(rift-brawl 实锤:先 flush 再 setAlarm,闹钟永不触发),
3305
+ * 反向「clearAlarm 后没 flush」会因 DB 残留到期值误触发。重启续接仍以落库值为准
3306
+ * (实例不驻留时后端值是唯一事实;要重启不丢 alarm,方法内仍须 flush——见 features/state-objects.md)。
3294
3307
  * - onAlarm 后统一持久化(flush):onAlarm 内改的 state 与 setAlarm 重调度 / clearAlarm
3295
3308
  * 的新 alarmAt 都落库;
3296
3309
  * - 单对象 onAlarm 抛错**不阻塞**同 tick 其余对象:失败对象不落库、alarmAt 保留
3297
3310
  * → 下轮 dueAlarms 仍命中重试;错误折叠进 failed(不泄漏内部栈)。
3298
3311
  */
3299
3312
  async pump(now) {
3300
- const due = await this.backend.dueAlarms(now);
3313
+ const dueMap = /* @__PURE__ */ new Map();
3314
+ for (const d of await this.backend.dueAlarms(now)) {
3315
+ dueMap.set(this.key(d.type, d.id), d);
3316
+ }
3317
+ for (const [k, inst] of this.instances) {
3318
+ const typeName = inst.constructor.typeName;
3319
+ if (typeName === void 0) continue;
3320
+ const raw = inst;
3321
+ if (raw.alarmAt === null || raw.alarmAt > now) {
3322
+ dueMap.delete(k);
3323
+ continue;
3324
+ }
3325
+ dueMap.set(k, { type: typeName, id: inst.id, alarmAt: raw.alarmAt });
3326
+ }
3327
+ const due = [...dueMap.values()].toSorted((a, b) => a.alarmAt - b.alarmAt);
3301
3328
  const fired = [];
3302
3329
  const failed = [];
3303
3330
  for (const d of due) {
@@ -101,6 +101,11 @@ export declare class StateObjectRuntime {
101
101
  flush<State extends Record<string, unknown>>(inst: StateObject<State>): Promise<void>;
102
102
  /**
103
103
  * alarm 轮询:把到期对象唤醒并调 onAlarm(CF-037)。
104
+ * - 到期集合 = 后端落库到期记录 ∪ **驻留实例的内存 alarmAt(驻留时以内存值为准)**:
105
+ * `setAlarm`/`clearAlarm` 只写内存、`alarmAt` 随 `flush()` 落库——若 pump 只读后端,
106
+ * 「setAlarm 后没 flush」会静默丢调度(rift-brawl 实锤:先 flush 再 setAlarm,闹钟永不触发),
107
+ * 反向「clearAlarm 后没 flush」会因 DB 残留到期值误触发。重启续接仍以落库值为准
108
+ * (实例不驻留时后端值是唯一事实;要重启不丢 alarm,方法内仍须 flush——见 features/state-objects.md)。
104
109
  * - onAlarm 后统一持久化(flush):onAlarm 内改的 state 与 setAlarm 重调度 / clearAlarm
105
110
  * 的新 alarmAt 都落库;
106
111
  * - 单对象 onAlarm 抛错**不阻塞**同 tick 其余对象:失败对象不落库、alarmAt 保留
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "homepage": "https://adep.top",
9
9
  "bugs": "https://gitcode.com/yunqiang_wu/ai-yunke/issues",
10
- "version": "0.3.14",
10
+ "version": "0.3.16",
11
11
  "private": false,
12
12
  "type": "module",
13
13
  "sideEffects": false,
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@adep/types": "0.4.4"
35
+ "@adep/types": "0.4.5"
36
36
  },
37
37
  "scripts": {
38
38
  "test": "vitest run",