qiyu-cli 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +467 -2
- package/dist/cli.js +248 -0
- package/dist/commands/api.js +770 -0
- package/dist/commands/auth.js +349 -0
- package/dist/commands/credentials-source.js +70 -0
- package/dist/commands/db.js +503 -0
- package/dist/commands/local-fallback-wiring.js +67 -0
- package/dist/commands/local-fallback.js +34 -0
- package/dist/commands/query.js +564 -0
- package/dist/commands/skill-specs.js +64 -0
- package/dist/commands/skill.js +908 -0
- package/dist/commands/spec-types.js +145 -0
- package/dist/commands/stats.js +299 -0
- package/dist/commands/store-flags.js +56 -0
- package/dist/commands/store.js +258 -0
- package/dist/commands/sync-run.js +479 -0
- package/dist/commands/sync.js +757 -0
- package/dist/commands/tree.js +27 -0
- package/dist/core/config.js +259 -0
- package/dist/core/credentials.js +71 -0
- package/dist/core/edition.js +28 -0
- package/dist/core/envelope.js +201 -0
- package/dist/core/errors.js +356 -0
- package/dist/core/http.js +348 -0
- package/dist/core/ratelimit.js +126 -0
- package/dist/core/registry/index.js +212 -0
- package/dist/core/registry/params/ai.knowledge.addKnowledge.json +88 -0
- package/dist/core/registry/params/ai.knowledge.deleteKnowledges.json +46 -0
- package/dist/core/registry/params/ai.knowledge.editChannelAnswer.json +60 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledge.json +95 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledgeNoSimilars.json +102 -0
- package/dist/core/registry/params/ai.knowledge.getCategoryTree.json +39 -0
- package/dist/core/registry/params/ai.knowledge.getDetail.json +46 -0
- package/dist/core/registry/params/ai.knowledge.search.json +67 -0
- package/dist/core/registry/params/ai.statistic.transferHuman.json +60 -0
- package/dist/core/registry/params/category.list.json +39 -0
- package/dist/core/registry/params/chat.queue.group.query.json +39 -0
- package/dist/core/registry/params/crm.modifyOrder.json +48 -0
- package/dist/core/registry/params/crm.modifyUser.json +48 -0
- package/dist/core/registry/params/crm.syncCrmInfo.json +46 -0
- package/dist/core/registry/params/crm.tagChange.json +74 -0
- package/dist/core/registry/params/data.overview.ipcc.json +32 -0
- package/dist/core/registry/params/data.overview.session.json +53 -0
- package/dist/core/registry/params/evaluation.setting.get.json +39 -0
- package/dist/core/registry/params/event.applyStaff.json +116 -0
- package/dist/core/registry/params/event.evaluate.json +74 -0
- package/dist/core/registry/params/event.inviteEvaluate.json +81 -0
- package/dist/core/registry/params/event.qcResult.json +166 -0
- package/dist/core/registry/params/event.queryQueueStatus.json +39 -0
- package/dist/core/registry/params/event.queueEnter.json +40 -0
- package/dist/core/registry/params/event.queueLeave.json +46 -0
- package/dist/core/registry/params/event.quitQueue.json +39 -0
- package/dist/core/registry/params/event.sessionChange.json +95 -0
- package/dist/core/registry/params/event.sessionData.json +452 -0
- package/dist/core/registry/params/event.sessionEnd.json +117 -0
- package/dist/core/registry/params/event.sessionStart.json +109 -0
- package/dist/core/registry/params/event.updateUInfo.json +46 -0
- package/dist/core/registry/params/export.session.check.json +7 -0
- package/dist/core/registry/params/export.session.json +7 -0
- package/dist/core/registry/params/export.session.one.json +7 -0
- package/dist/core/registry/params/export.session.one.message.json +7 -0
- package/dist/core/registry/params/get_call_order.json +50 -0
- package/dist/core/registry/params/get_call_user_info.json +36 -0
- package/dist/core/registry/params/get_order.json +50 -0
- package/dist/core/registry/params/get_token.json +22 -0
- package/dist/core/registry/params/get_user_info.json +36 -0
- package/dist/core/registry/params/icoutcall.callIntercept.list.json +32 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.fuzzySearch.json +53 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.getDialogFlowList.json +32 -0
- package/dist/core/registry/params/icoutcall.get.customer.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobdetail.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobproperties.json +39 -0
- package/dist/core/registry/params/icoutcall.history.seat.json +67 -0
- package/dist/core/registry/params/icoutcall.history.team.json +67 -0
- package/dist/core/registry/params/icoutcall.job.create.json +53 -0
- package/dist/core/registry/params/icoutcall.job.importcustomer.json +46 -0
- package/dist/core/registry/params/icoutcall.job.pause.json +39 -0
- package/dist/core/registry/params/icoutcall.job.readdcustomer.json +109 -0
- package/dist/core/registry/params/icoutcall.job.start.json +39 -0
- package/dist/core/registry/params/icoutcall.job.terminate.json +39 -0
- package/dist/core/registry/params/icoutcall.phone.getLineList.json +32 -0
- package/dist/core/registry/params/ipcc.calltask.add.auto.json +117 -0
- package/dist/core/registry/params/ipcc.calltask.add.byCustom.json +96 -0
- package/dist/core/registry/params/ipcc.calltask.add.byRelation.json +82 -0
- package/dist/core/registry/params/ipcc.calltask.add.toGroup.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.add.toStaff.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.get.json +47 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.add.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.delete.json +40 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.get.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.update.json +40 -0
- package/dist/core/registry/params/ipcc.customfield.template.get.json +47 -0
- package/dist/core/registry/params/ipcc.login.json +47 -0
- package/dist/core/registry/params/kefu.add.json +130 -0
- package/dist/core/registry/params/kefu.delete.json +39 -0
- package/dist/core/registry/params/kefu.update.json +137 -0
- package/dist/core/registry/params/kefu.v2.query.json +39 -0
- package/dist/core/registry/params/kefuGroup.add.json +39 -0
- package/dist/core/registry/params/kefuGroup.delete.json +39 -0
- package/dist/core/registry/params/kefuGroup.update.json +46 -0
- package/dist/core/registry/params/kefuGroup.updateKefus.json +46 -0
- package/dist/core/registry/params/merchant.account.login.json +53 -0
- package/dist/core/registry/params/merchant.changePassword.json +46 -0
- package/dist/core/registry/params/merchant.create.json +81 -0
- package/dist/core/registry/params/merchant.disable.json +39 -0
- package/dist/core/registry/params/merchant.enable.json +39 -0
- package/dist/core/registry/params/merchant.list.json +46 -0
- package/dist/core/registry/params/merchant.query.json +46 -0
- package/dist/core/registry/params/merchant.update.json +74 -0
- package/dist/core/registry/params/message.send.image.json +53 -0
- package/dist/core/registry/params/message.send.json +53 -0
- package/dist/core/registry/params/message.send.voice.json +53 -0
- package/dist/core/registry/params/message.sendFile.json +39 -0
- package/dist/core/registry/params/message.uploadFile.json +39 -0
- package/dist/core/registry/params/online.session.category.list.json +39 -0
- package/dist/core/registry/params/online.session.customfield.template.json +39 -0
- package/dist/core/registry/params/online.session.customfield.update.json +74 -0
- package/dist/core/registry/params/order.upload.json +40 -0
- package/dist/core/registry/params/push.msg.image.json +74 -0
- package/dist/core/registry/params/push.msg.richText.json +74 -0
- package/dist/core/registry/params/push.msg.text.json +74 -0
- package/dist/core/registry/params/receive.message.json +89 -0
- package/dist/core/registry/params/robot.data.getQuestionsByCategoryName.json +67 -0
- package/dist/core/registry/params/robot.data.knowledge.json +46 -0
- package/dist/core/registry/params/robot.data.kpoint.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardByEffectiveQuestion.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardQuestionList.json +46 -0
- package/dist/core/registry/params/robot.evaluation.comment.json +67 -0
- package/dist/core/registry/params/robot.evaluation.setting.json +46 -0
- package/dist/core/registry/params/robot.session.assist.json +74 -0
- package/dist/core/registry/params/robot.session.chat.json +74 -0
- package/dist/core/registry/params/robot.session.close.json +53 -0
- package/dist/core/registry/params/robot.session.commonQuestions.json +74 -0
- package/dist/core/registry/params/robot.session.create.json +81 -0
- package/dist/core/registry/params/robot.session.evaluation.json +67 -0
- package/dist/core/registry/params/robot.session.msgAssociate.json +46 -0
- package/dist/core/registry/params/robot.session.msgEvaluation.json +53 -0
- package/dist/core/registry/params/robot.session.msgPredict.json +74 -0
- package/dist/core/registry/params/robot.session.transfer.json +36 -0
- package/dist/core/registry/params/robot.session.uploadFile.json +32 -0
- package/dist/core/registry/params/robot.statistic.summary.json +53 -0
- package/dist/core/registry/params/robot.transfer.config.json +15 -0
- package/dist/core/registry/params/session.user.profile.json +39 -0
- package/dist/core/registry/params/settings.wxTemplates.json +46 -0
- package/dist/core/registry/params/smstask.create.json +53 -0
- package/dist/core/registry/params/smstask.status.json +39 -0
- package/dist/core/registry/params/staff.group.list.new.json +39 -0
- package/dist/core/registry/params/staff.group.members.new.json +46 -0
- package/dist/core/registry/params/staff.list.new.json +46 -0
- package/dist/core/registry/params/staff.login.json +47 -0
- package/dist/core/registry/params/statistic.overview.json +61 -0
- package/dist/core/registry/params/statistic.performance.json +82 -0
- package/dist/core/registry/params/statistic.salaryDetail.json +54 -0
- package/dist/core/registry/params/statistic.satisfaction.report.json +68 -0
- package/dist/core/registry/params/statistic.staffAttendance.json +75 -0
- package/dist/core/registry/params/statistic.staffquality.json +75 -0
- package/dist/core/registry/params/statistic.staffworklod.json +68 -0
- package/dist/core/registry/params/tag.all.json +32 -0
- package/dist/core/registry/params/tag.batchUpdate.json +32 -0
- package/dist/core/registry/params/tag.update.json +60 -0
- package/dist/core/registry/params/ticket.apply.json +46 -0
- package/dist/core/registry/params/ticket.batch.modify.json +53 -0
- package/dist/core/registry/params/ticket.create.json +151 -0
- package/dist/core/registry/params/ticket.filter.count.json +46 -0
- package/dist/core/registry/params/ticket.filter.list.json +46 -0
- package/dist/core/registry/params/ticket.finish.json +60 -0
- package/dist/core/registry/params/ticket.followers.update.json +60 -0
- package/dist/core/registry/params/ticket.list.json +74 -0
- package/dist/core/registry/params/ticket.log.json +39 -0
- package/dist/core/registry/params/ticket.modify.json +74 -0
- package/dist/core/registry/params/ticket.new.detail.json +39 -0
- package/dist/core/registry/params/ticket.reminder.json +60 -0
- package/dist/core/registry/params/ticket.reopen.json +67 -0
- package/dist/core/registry/params/ticket.reply.json +60 -0
- package/dist/core/registry/params/ticket.search.json +116 -0
- package/dist/core/registry/params/ticket.template.fields.json +39 -0
- package/dist/core/registry/params/ticket.template.list.json +39 -0
- package/dist/core/registry/params/ticket.transfer.json +74 -0
- package/dist/core/registry/params/track.event.set.json +32 -0
- package/dist/core/registry/params/wecom.robot.switch.json +43 -0
- package/dist/core/registry/params.js +84 -0
- package/dist/core/registry/specs.json +2172 -0
- package/dist/core/registry/validate.json +11 -0
- package/dist/core/registry/validation-overrides.js +98 -0
- package/dist/core/sanitize.js +24 -0
- package/dist/core/sign.js +21 -0
- package/dist/core/time.js +194 -0
- package/dist/core/validate.js +140 -0
- package/dist/output.js +263 -0
- package/dist/postinstall.js +178 -0
- package/dist/query/errors.js +395 -0
- package/dist/query/mask.js +368 -0
- package/dist/query/metric-catalog.js +85 -0
- package/dist/query/sandbox.js +544 -0
- package/dist/query/table-query.js +297 -0
- package/dist/query/time-shape.js +44 -0
- package/dist/schedule/install.js +377 -0
- package/dist/skill/check.js +429 -0
- package/dist/skill/detect.js +371 -0
- package/dist/skill/install.js +1202 -0
- package/dist/skill/paths.js +343 -0
- package/dist/skill/qiyu/SKILL.md +92 -0
- package/dist/skill/qiyu/errors.md +55 -0
- package/dist/skill/qiyu/forbidden.md +52 -0
- package/dist/skill/qiyu/metrics.md +65 -0
- package/dist/skill/qiyu/protocols.md +55 -0
- package/dist/skill/qiyu/setup.md +50 -0
- package/dist/skill/refs.js +379 -0
- package/dist/store/config.js +137 -0
- package/dist/store/duckdb.js +321 -0
- package/dist/store/index.js +18 -0
- package/dist/store/retry.js +36 -0
- package/dist/store/schema.js +419 -0
- package/dist/store/types.js +4 -0
- package/dist/sync/day.js +8 -0
- package/dist/sync/engine.js +818 -0
- package/dist/sync/rate-gate.js +95 -0
- package/dist/sync/registry.js +22 -0
- package/dist/sync/state.js +114 -0
- package/dist/sync/tasks/dim.js +186 -0
- package/dist/sync/tasks/knowledge.js +252 -0
- package/dist/sync/tasks/pace.js +66 -0
- package/dist/sync/tasks/pipeline.js +321 -0
- package/dist/sync/tasks/reports.js +448 -0
- package/dist/sync/tasks/sessions.js +188 -0
- package/dist/sync/tasks/tickets.js +219 -0
- package/dist/sync/types.js +1 -0
- package/dist/sync/windowing.js +56 -0
- package/package.json +52 -4
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// src/store/duckdb.ts
|
|
2
|
+
// ⚠️ 并发契约(G 组 G-7):本实现持**单连接**,无内部串行化队列 —— 调用方必须
|
|
3
|
+
// 串行 await 所有 Store 方法(C1 的 store init/status 天然满足;D 的同步引擎
|
|
4
|
+
// 复用时同一 Store 实例上的 upsert/transaction 不得并发调用,否则 BEGIN/COMMIT
|
|
5
|
+
// 交错)。并发读写不同实例(rw 写者 + 多个 ro 读者)是受支持的用法(spec §5.2)。
|
|
6
|
+
// ⚠️ 重试归属(O 组 O-2/G-6):写者路径**无内置重试**——D 的重试调度须在 D 层做。
|
|
7
|
+
// ⚠️ SQL 红线基线(O 组 O-3):C1 交付时本文件 + commands/store.ts 的 SQL 字面
|
|
8
|
+
// 调用点共 8 处,全部为代码内常量(红线:外部派生字符串不得进 query());
|
|
9
|
+
// D/E 扩展时须保持并**更新此计数**。D-T1 追加 delete 原语后为 9 处
|
|
10
|
+
//(duckdb.ts 7:INSERT×2 / DELETE×1 / SELECT×4;commands/store.ts 2:SELECT×2)。
|
|
11
|
+
// D-T3 追加:src/sync/state.ts 常量 SELECT 3 处(水位/缺口读取),不在上述 9 处内。
|
|
12
|
+
// D-T11 追加:src/sync/tasks/knowledge.ts 常量 SELECT 1 处
|
|
13
|
+
//(knowledge.categories 的驱动序 SELECT DISTINCT robot_id),不在上述 9 处内。
|
|
14
|
+
import { MIGRATIONS, tableByName } from './schema.js';
|
|
15
|
+
import { retryWithBackoff, isLockError, isModuleNotFound } from './retry.js';
|
|
16
|
+
// `isModuleNotFound` 的定义已下沉到 retry.ts(架构审查 M-4):spec §5.1 白名单允许
|
|
17
|
+
// query/ import `store/retry` 但不允许 `store/duckdb`。这里保留 re-export,既有
|
|
18
|
+
// 导入点(tests/store/errors.test.ts)不破。
|
|
19
|
+
export { isModuleNotFound };
|
|
20
|
+
const defaultImporter = () => import('@duckdb/node-api');
|
|
21
|
+
/** 依赖缺失 → store_not_initialized(按缺失 specifier 前缀二分 hint,spec §5.1)。 */
|
|
22
|
+
function depError(e) {
|
|
23
|
+
const mn = isModuleNotFound(e);
|
|
24
|
+
const err = new Error(`无法加载 DuckDB 依赖:${e instanceof Error ? e.message : String(e)}`);
|
|
25
|
+
err.kind = 'store_not_initialized';
|
|
26
|
+
err.hint = mn && mn.missing.startsWith('@duckdb/node-bindings')
|
|
27
|
+
? '当前平台无 DuckDB 预编译包(无 node-gyp 回退)。请在受支持的平台上安装 @duckdb/node-api@1.5.6-r.1'
|
|
28
|
+
: '未安装 DuckDB 依赖。请在 qiyu-cli 的安装环境中运行:npm install @duckdb/node-api@1.5.6-r.1';
|
|
29
|
+
return err;
|
|
30
|
+
}
|
|
31
|
+
/** usage 错误(退出码 2):带 kind 供命令层经 thrownToExit 归 usageFail 双通道。 */
|
|
32
|
+
function usage(msg) {
|
|
33
|
+
const e = new Error(msg);
|
|
34
|
+
e.kind = 'usage';
|
|
35
|
+
return e;
|
|
36
|
+
}
|
|
37
|
+
export async function createDuckDbStore(cfg, mode, deps = {}) {
|
|
38
|
+
let mod;
|
|
39
|
+
try {
|
|
40
|
+
mod = (await (deps.importer ?? defaultImporter)());
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw depError(e);
|
|
44
|
+
}
|
|
45
|
+
const open = async () => {
|
|
46
|
+
// ⚠️ 只读模式必须真的以 READ_ONLY 打开 —— 这是 ro→ro 并发可用的前提(spec §5.2)。
|
|
47
|
+
// ✅ 第四轮实测(1.5.6-r.1 安装包):配置项名就是 `access_mode: 'READ_ONLY'`,
|
|
48
|
+
// 且 ro 打开后 INSERT 被 DuckDB 拒绝(read-only mode)——形态确认无误。
|
|
49
|
+
const inst = mode === 'ro'
|
|
50
|
+
? await mod.DuckDBInstance.create(cfg.path, { access_mode: 'READ_ONLY' })
|
|
51
|
+
: await mod.DuckDBInstance.create(cfg.path);
|
|
52
|
+
try {
|
|
53
|
+
const conn = await inst.connect();
|
|
54
|
+
return { conn, inst };
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
// ⚠️ R 组 R9:create 成功但 connect 失败 → instance 泄漏(锁可能随进程滞留),
|
|
58
|
+
// ro 退避路径会累积 —— 失败路径自清理。
|
|
59
|
+
try {
|
|
60
|
+
inst.closeSync?.();
|
|
61
|
+
}
|
|
62
|
+
catch { /* 清理错误不掩盖原异常 */ }
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
let conn;
|
|
67
|
+
let inst;
|
|
68
|
+
if (mode === 'ro') {
|
|
69
|
+
// 读者:锁冲突时退避重试(spec §5.2);**超限后必须包装 store_locked**(v3.1 修——
|
|
70
|
+
// v1 计划直接重抛原始错误,经 thrownToExit 会变成退出码 2 的 usage,违反验收 35)。
|
|
71
|
+
try {
|
|
72
|
+
const r = await retryWithBackoff({
|
|
73
|
+
tryOpen: open, isRetryable: isLockError, maxMs: cfg.readOnlyRetryMs,
|
|
74
|
+
sleep: deps.sleep, now: deps.now,
|
|
75
|
+
});
|
|
76
|
+
conn = r.conn;
|
|
77
|
+
inst = r.inst;
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (isLockError(e)) {
|
|
81
|
+
const err = new Error(`数据库被写进程占用(等待 ${cfg.readOnlyRetryMs}ms 后超时):${cfg.path}`);
|
|
82
|
+
err.kind = 'store_locked';
|
|
83
|
+
err.retryable = true;
|
|
84
|
+
err.hint = '请稍后重试;等待上限可经配置文件 readOnlyRetryMs(默认 30000 毫秒)调整,或用 --db 指向其他数据库文件';
|
|
85
|
+
throw err;
|
|
86
|
+
}
|
|
87
|
+
throw e;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
// 写者不重试(spec §5.2):撞锁立即抛。
|
|
92
|
+
try {
|
|
93
|
+
const r = await open();
|
|
94
|
+
conn = r.conn;
|
|
95
|
+
inst = r.inst;
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
if (isLockError(e)) {
|
|
99
|
+
const err = new Error(`数据库被写进程占用:${cfg.path}`);
|
|
100
|
+
err.kind = 'store_locked';
|
|
101
|
+
err.retryable = true;
|
|
102
|
+
err.hint = '另一个进程持有数据库写锁。请等该进程退出后重试';
|
|
103
|
+
throw err;
|
|
104
|
+
}
|
|
105
|
+
throw e;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// ⚠️ 真实 API(第四轮实测):runAndReadAll(sql, params) 返回带 getRowObjectsJson()
|
|
109
|
+
// 与 columnNames() 的读取器;run() 返回的 Result 没有取行方法。
|
|
110
|
+
const c = conn;
|
|
111
|
+
let closed = false;
|
|
112
|
+
let inTx = false;
|
|
113
|
+
// ⚠️ 所有语句执行收敛到这一个方法 —— 测试 spy 的落点(spec §5.3)。
|
|
114
|
+
// ⚠️ **挂载与调用契约(v3.1 定死,消除二选一)**:
|
|
115
|
+
// ① store 的方法体**双轨调用**:数据语句(SELECT/INSERT 等)必须经 `store.execSql(...)`
|
|
116
|
+
// (属性查找发生在调用时,vi.spyOn 替换后即命中 spy);事务控制语句
|
|
117
|
+
// BEGIN/COMMIT/ROLLBACK 闭包直调 execSql(否则 spy 数到事务壳、断言
|
|
118
|
+
// 「1000 行 1 条语句」必红——T5 实测裁定)——闭包直调数据语句会绕过 spy,
|
|
119
|
+
// 「1000 行批量 1 条语句」断言形同虚设(v1 计划实测缺陷,D/E/F 三组独立发现);
|
|
120
|
+
// ② `Object.defineProperty` 必须 `writable: true, configurable: true` —— 默认
|
|
121
|
+
// configurable:false 会让 vi.spyOn 重定义属性时直接抛 TypeError;
|
|
122
|
+
// ③ 挂为**不可枚举**(不污染 JSON 输出与接口形状)。
|
|
123
|
+
const execSql = async (sql, params = []) => {
|
|
124
|
+
if (closed)
|
|
125
|
+
throw usage('数据库连接已关闭');
|
|
126
|
+
// 统一走 runAndReadAll:其结果带 getRowObjectsJson/columnNames(run() 的结果没有)
|
|
127
|
+
return c.runAndReadAll(sql, params);
|
|
128
|
+
};
|
|
129
|
+
const assertTable = (name) => {
|
|
130
|
+
const t = tableByName(name);
|
|
131
|
+
if (!t)
|
|
132
|
+
throw usage(`未知表:${name}`);
|
|
133
|
+
return t;
|
|
134
|
+
};
|
|
135
|
+
const assertRows = (t, rows) => {
|
|
136
|
+
if (rows.length === 0)
|
|
137
|
+
return;
|
|
138
|
+
const cols = t.columns.map((x) => x.name);
|
|
139
|
+
for (let i = 0; i < rows.length; i++) {
|
|
140
|
+
const keys = Object.keys(rows[i]);
|
|
141
|
+
for (const k of keys)
|
|
142
|
+
if (!cols.includes(k))
|
|
143
|
+
throw usage(`未知列:${k}(表 ${t.name})`);
|
|
144
|
+
for (const cd of t.columns) {
|
|
145
|
+
if (!(cd.name in rows[i]))
|
|
146
|
+
throw usage(`表 ${t.name} 缺少列:${cd.name}`);
|
|
147
|
+
if (rows[i][cd.name] === null && !cd.nullable)
|
|
148
|
+
throw usage(`列 ${cd.name} 不允许 null(表 ${t.name})`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const doUpsert = async (t, rows) => {
|
|
153
|
+
if (rows.length === 0)
|
|
154
|
+
return 0;
|
|
155
|
+
const cols = t.columns.map((x) => x.name);
|
|
156
|
+
const quoted = cols.map((x) => `"${x}"`).join(', ');
|
|
157
|
+
const nonKey = cols.filter((x) => !t.primaryKey.includes(x));
|
|
158
|
+
const setClause = nonKey.map((x) => `"${x}" = excluded."${x}"`).join(', ');
|
|
159
|
+
const pk = t.primaryKey.map((x) => `"${x}"`).join(', ');
|
|
160
|
+
let written = 0;
|
|
161
|
+
// ⚠️ 切批规则(M2 修,与 spec §5.3 对齐):**batchSize 是首选切批尺寸**,
|
|
162
|
+
// 65000 参数硬顶无条件兜底 —— 实际行数 = min(batchSize, floor(65000/cols))。
|
|
163
|
+
// (Q 组 Q-高-1 的实测仍成立:1M 参数单语句 43s,硬顶必须恒生效、batchSize
|
|
164
|
+
// 配大也不能越过;但 batchSize 原义是「多值 INSERT 每批行数」(types.ts/§5.3),
|
|
165
|
+
// 小于硬顶时必须生效,此前实现把它完全忽略是契约偏离。)
|
|
166
|
+
// 65000 是保守值:实测该版本上限 ≥70000,勿据此放大(Q 组 Q-低-1)。
|
|
167
|
+
const hardCap = Math.max(1, Math.floor(65000 / cols.length));
|
|
168
|
+
const rowsPerStmt = Math.max(1, Math.min(cfg.batchSize, hardCap));
|
|
169
|
+
for (let i = 0; i < rows.length; i += rowsPerStmt) {
|
|
170
|
+
const batch = rows.slice(i, i + rowsPerStmt);
|
|
171
|
+
const placeholders = batch.map(() => `(${cols.map(() => '?').join(', ')})`).join(', ');
|
|
172
|
+
const params = batch.flatMap((r) => cols.map((k) => r[k] ?? null));
|
|
173
|
+
await store.execSql(`INSERT INTO "${t.name}" (${quoted}) VALUES ${placeholders} ON CONFLICT (${pk}) DO UPDATE SET ${setClause}`, params);
|
|
174
|
+
written += batch.length;
|
|
175
|
+
}
|
|
176
|
+
return written;
|
|
177
|
+
};
|
|
178
|
+
const del = async (tname, where, params = []) => {
|
|
179
|
+
const t = assertTable(tname);
|
|
180
|
+
const before = await store.count(tname);
|
|
181
|
+
await store.execSql(`DELETE FROM "${t.name}" WHERE ${where}`, params);
|
|
182
|
+
const after = await store.count(tname);
|
|
183
|
+
return Math.max(0, before - after);
|
|
184
|
+
};
|
|
185
|
+
// ⚠️ 回滚保护(G 组 G-3):若回滚本身失败(如连接已被并发 close),次生错误
|
|
186
|
+
// 不得掩盖原始失败原因 —— 吞掉回滚错误,保留 catch 里的原错误继续抛。
|
|
187
|
+
// (ROLLBACK 与 BEGIN/COMMIT 同为事务控制语句,闭包直调——见 upsert 处注释。)
|
|
188
|
+
const safeRollback = async () => {
|
|
189
|
+
try {
|
|
190
|
+
await execSql('ROLLBACK');
|
|
191
|
+
}
|
|
192
|
+
catch { /* 保留原错误 */ }
|
|
193
|
+
};
|
|
194
|
+
// ⚠️ store 自引用:方法体经 store.execSql 调用(见下方契约)。断言目标**必须含
|
|
195
|
+
// execSql**(第四轮 H 组实测:`as unknown as Store` 下方法体内所有 store.execSql
|
|
196
|
+
// 报 TS2339——纯编译期属性缺失,非 TDZ);方法参数显式标注(自引用打断上下文
|
|
197
|
+
// 推导,不标会隐式 any)。
|
|
198
|
+
const store = {
|
|
199
|
+
async init() {
|
|
200
|
+
if (mode === 'ro')
|
|
201
|
+
throw usage('只读模式下不能初始化(需 rw 模式)');
|
|
202
|
+
// 他人库防线(spec §6.1):已有表但无 schema_version → 拒绝。
|
|
203
|
+
const existing = await store.execSql(`SELECT table_name FROM information_schema.tables WHERE table_schema = 'main'`);
|
|
204
|
+
const names = existing.getRowObjectsJson()
|
|
205
|
+
.map((r) => String(r.table_name));
|
|
206
|
+
if (names.length > 0 && !names.includes('schema_version')) {
|
|
207
|
+
// ⚠️ 他人库 → store_not_initialized、退出码 1(spec §8.2 成因 6)——不是 usage。
|
|
208
|
+
const err = new Error(`目标数据库不是本 CLI 创建的(含表 ${names.join(', ')}):${cfg.path}`);
|
|
209
|
+
err.kind = 'store_not_initialized';
|
|
210
|
+
err.hint = '目标数据库不是本 CLI 创建的,请换 --db 路径';
|
|
211
|
+
throw err;
|
|
212
|
+
}
|
|
213
|
+
const cur = names.includes('schema_version')
|
|
214
|
+
? await store.execSql('SELECT COALESCE(MAX(version), 0) AS v FROM schema_version')
|
|
215
|
+
: null;
|
|
216
|
+
const v = cur === null ? 0 : Number(cur.getRowObjectsJson()[0].v);
|
|
217
|
+
for (const m of MIGRATIONS.filter((x) => x.version > v).sort((a, b) => a.version - b.version)) {
|
|
218
|
+
await execSql('BEGIN');
|
|
219
|
+
try {
|
|
220
|
+
await m.up((sql) => store.execSql(sql).then(() => undefined));
|
|
221
|
+
await store.execSql('INSERT INTO schema_version (version) VALUES (?)', [m.version]);
|
|
222
|
+
await execSql('COMMIT');
|
|
223
|
+
}
|
|
224
|
+
catch (e) {
|
|
225
|
+
// 迁移失败:回滚且**不推进**版本 —— 下次 init 重放该迁移(验收 19/20)。
|
|
226
|
+
await safeRollback();
|
|
227
|
+
throw e;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
async tables() {
|
|
232
|
+
const r = await store.execSql(`SELECT table_name FROM information_schema.tables WHERE table_schema = 'main'`);
|
|
233
|
+
return r.getRowObjectsJson()
|
|
234
|
+
.map((x) => String(x.table_name)).sort();
|
|
235
|
+
},
|
|
236
|
+
async count(table) {
|
|
237
|
+
assertTable(table);
|
|
238
|
+
const r = await store.execSql(`SELECT COUNT(*) AS n FROM "${table}"`);
|
|
239
|
+
const v = r.getRowObjectsJson()[0].n;
|
|
240
|
+
return Number(v); // ⚠️ BIGINT → string,必须收敛(spec §5.4,已实测确认)
|
|
241
|
+
},
|
|
242
|
+
async upsert(table, rows) {
|
|
243
|
+
const t = assertTable(table);
|
|
244
|
+
assertRows(t, rows);
|
|
245
|
+
if (inTx)
|
|
246
|
+
return doUpsert(t, rows);
|
|
247
|
+
// ⚠️ BEGIN/COMMIT 闭包直调 execSql:测试 spy 只断言**数据语句**条数(语句数 == 批次
|
|
248
|
+
// 数),事务控制语句不走 store.execSql 属性查找(与测试为权威的裁定一致)。
|
|
249
|
+
await execSql('BEGIN');
|
|
250
|
+
try {
|
|
251
|
+
const n = await doUpsert(t, rows);
|
|
252
|
+
await execSql('COMMIT');
|
|
253
|
+
return n;
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
await safeRollback();
|
|
257
|
+
throw e;
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
delete: del,
|
|
261
|
+
async transaction(fn) {
|
|
262
|
+
if (inTx)
|
|
263
|
+
throw usage('不得嵌套事务');
|
|
264
|
+
await execSql('BEGIN');
|
|
265
|
+
inTx = true;
|
|
266
|
+
try {
|
|
267
|
+
// ⚠️ 真实投影而非 cast:WriteTx 类型上不含 query/init/close(spec §4.2.6 防线分层),
|
|
268
|
+
// 运行时对象也不得携带 —— 直接传 store 的话 `Object.keys(tx)` 可枚举出 query。
|
|
269
|
+
const tx = {
|
|
270
|
+
tables: () => store.tables(),
|
|
271
|
+
count: (t) => store.count(t),
|
|
272
|
+
upsert: (t, rows) => store.upsert(t, rows),
|
|
273
|
+
delete: (t, w, p) => store.delete(t, w, p),
|
|
274
|
+
transaction: (f) => store.transaction(f),
|
|
275
|
+
};
|
|
276
|
+
const r = await fn(tx);
|
|
277
|
+
await execSql('COMMIT');
|
|
278
|
+
return r;
|
|
279
|
+
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
await safeRollback();
|
|
282
|
+
throw e;
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
inTx = false;
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
async query(sql, params) {
|
|
289
|
+
const r = await store.execSql(sql, params ?? []);
|
|
290
|
+
const rows = r.getRowObjectsJson();
|
|
291
|
+
// 空结果集也要给真实列名(不能留空数组)——columnNames() 是读取器上的方法,
|
|
292
|
+
// 空集也返回真实列名(第四轮实测确认)。
|
|
293
|
+
const cols = rows.length > 0
|
|
294
|
+
? Object.keys(rows[0])
|
|
295
|
+
: r.columnNames();
|
|
296
|
+
return { columns: cols, rows };
|
|
297
|
+
},
|
|
298
|
+
async close() {
|
|
299
|
+
if (closed)
|
|
300
|
+
return;
|
|
301
|
+
closed = true;
|
|
302
|
+
// ⚠️ 真实 API 是 **closeSync()**(同步方法)——`close?.()` 会静默跳过、连接泄漏
|
|
303
|
+
//(第四轮实测:instance 原型只有 connect/closeSync)。包 try 防次生错误掩盖原结果。
|
|
304
|
+
// ⚠️ N1 实测:**connection 与 instance 都要 closeSync** —— 只关 instance 不关 conn,
|
|
305
|
+
// 锁随进程存活、永不超时释放;先关 conn 再关 inst。
|
|
306
|
+
try {
|
|
307
|
+
conn.closeSync?.();
|
|
308
|
+
}
|
|
309
|
+
catch { /* 清理期错误不掩盖 */ }
|
|
310
|
+
try {
|
|
311
|
+
inst.closeSync?.();
|
|
312
|
+
}
|
|
313
|
+
catch { /* 清理期错误不掩盖 */ }
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
// ⚠️ 挂载契约见上方 execSql 注释:不可枚举 + writable + configurable(缺 configurable
|
|
317
|
+
// 会让 vi.spyOn 重定义时抛 TypeError);数据语句一律 store.execSql(...)(属性调用时
|
|
318
|
+
// 查找),事务控制语句闭包直调(双轨,见上方契约①)。
|
|
319
|
+
Object.defineProperty(store, 'execSql', { value: execSql, enumerable: false, writable: true, configurable: true });
|
|
320
|
+
return store;
|
|
321
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/store/index.ts
|
|
2
|
+
import { createDuckDbStore } from './duckdb.js';
|
|
3
|
+
/**
|
|
4
|
+
* 后端注册表(spec §4.2.4)—— 惰性映射,使 openStore 不必静态 import duckdb.js,
|
|
5
|
+
* 这是 optionalDependency 惰性加载的落点。C2 在此加 'postgres'|'mysql'|'sqlite'。
|
|
6
|
+
*/
|
|
7
|
+
export const BACKENDS = {
|
|
8
|
+
duckdb: (cfg, mode) => createDuckDbStore(cfg, mode),
|
|
9
|
+
};
|
|
10
|
+
export async function openStore(cfg, mode) {
|
|
11
|
+
const make = BACKENDS[cfg.backend];
|
|
12
|
+
if (!make) {
|
|
13
|
+
const e = new Error(`未知后端:${cfg.backend}。目前仅支持 duckdb`);
|
|
14
|
+
e.kind = 'usage';
|
|
15
|
+
throw e;
|
|
16
|
+
}
|
|
17
|
+
return make(cfg, mode);
|
|
18
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/store/retry.ts
|
|
2
|
+
// ⚠️ 曲线是**固定间隔**(实测 2.6s/13 次成功 ≈ 200ms 间隔,spec §5.2)——
|
|
3
|
+
// 不是指数:指数曲线在剩余预算不足时会大幅越过 maxMs。
|
|
4
|
+
/** 锁冲突的识别规则(spec §5.2 定死):message 含 `Conflicting lock`。 */
|
|
5
|
+
export function isLockError(e) {
|
|
6
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
7
|
+
return msg.includes('Conflicting lock');
|
|
8
|
+
}
|
|
9
|
+
/** 识别 ERR_MODULE_NOT_FOUND 并取出缺失的 specifier(spec §5.1 的二分规则)。 */
|
|
10
|
+
export function isModuleNotFound(e) {
|
|
11
|
+
const err = e;
|
|
12
|
+
if (err?.code !== 'ERR_MODULE_NOT_FOUND')
|
|
13
|
+
return null;
|
|
14
|
+
const m = /Cannot find (?:package|module) '([^']+)'/.exec(err.message ?? '');
|
|
15
|
+
return { missing: m?.[1] ?? '' };
|
|
16
|
+
}
|
|
17
|
+
const defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
18
|
+
/** 有界重试:可重试错误按固定间隔重试至 maxMs,超限抛最后一个错误。 */
|
|
19
|
+
export async function retryWithBackoff(opts) {
|
|
20
|
+
const intervalMs = opts.intervalMs ?? 200;
|
|
21
|
+
const sleep = opts.sleep ?? defaultSleep;
|
|
22
|
+
const now = opts.now ?? (() => Date.now());
|
|
23
|
+
const start = now();
|
|
24
|
+
for (;;) {
|
|
25
|
+
try {
|
|
26
|
+
return await opts.tryOpen();
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
if (!opts.isRetryable(e))
|
|
30
|
+
throw e;
|
|
31
|
+
if (now() - start + intervalMs > opts.maxMs)
|
|
32
|
+
throw e;
|
|
33
|
+
await sleep(intervalMs);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|