qiyu-cli 0.2.1 → 0.2.3
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/README.md +433 -72
- package/dist/cli.js +48 -7
- package/dist/commands/api-notes.js +70 -0
- package/dist/commands/api.js +169 -49
- package/dist/commands/auth.js +151 -29
- package/dist/commands/credentials-source.js +30 -5
- package/dist/commands/db-notes.js +75 -0
- package/dist/commands/db.js +165 -127
- package/dist/commands/query-notes.js +58 -0
- package/dist/commands/query.js +449 -61
- package/dist/commands/skill.js +445 -51
- package/dist/commands/spec-types.js +101 -1
- package/dist/commands/stats.js +160 -33
- package/dist/commands/store-flags.js +18 -0
- package/dist/commands/store-notes.js +38 -0
- package/dist/commands/store-precheck.js +321 -0
- package/dist/commands/store.js +165 -77
- package/dist/commands/sync-notes.js +374 -0
- package/dist/commands/sync-run.js +180 -47
- package/dist/commands/sync.js +232 -127
- package/dist/core/config.js +280 -24
- package/dist/core/credentials.js +38 -4
- package/dist/core/edition.js +1 -1
- package/dist/core/envelope.js +73 -9
- package/dist/core/errors.js +73 -16
- package/dist/core/http.js +175 -54
- package/dist/core/ratelimit.js +5 -1
- package/dist/core/registry/index.js +36 -2
- package/dist/core/registry/params/export.session.check.json +17 -2
- package/dist/core/registry/params/export.session.json +24 -2
- package/dist/core/registry/params/export.session.one.json +10 -2
- package/dist/core/registry/params/export.session.one.message.json +17 -2
- package/dist/core/registry/specs.json +65 -43
- package/dist/core/shell.js +30 -0
- package/dist/core/time.js +1 -1
- package/dist/core/validate.js +21 -2
- package/dist/output.js +215 -29
- package/dist/postinstall.js +14 -5
- package/dist/query/errors.js +145 -45
- package/dist/query/mask.js +1277 -25
- package/dist/query/metric-catalog.js +11 -0
- package/dist/query/native-error.js +189 -0
- package/dist/query/sandbox.js +148 -25
- package/dist/query/table-query.js +36 -5
- package/dist/schedule/install.js +136 -13
- package/dist/skill/check.js +11 -6
- package/dist/skill/detect.js +89 -29
- package/dist/skill/install.js +233 -43
- package/dist/skill/paths.js +54 -0
- package/dist/skill/qiyu/SKILL.md +29 -5
- package/dist/skill/qiyu/errors.md +5 -4
- package/dist/skill/qiyu/protocols.md +57 -5
- package/dist/skill/qiyu/setup.md +6 -2
- package/dist/store/config.js +92 -16
- package/dist/store/duckdb.js +1551 -64
- package/dist/store/object-kinds.js +32 -0
- package/dist/store/retry.js +190 -7
- package/dist/store/schema.js +32 -4
- package/dist/sync/engine.js +439 -77
- package/dist/sync/registry.js +6 -0
- package/dist/sync/state.js +20 -0
- package/dist/sync/tasks/dim.js +23 -14
- package/dist/sync/tasks/knowledge.js +50 -8
- package/dist/sync/tasks/pace.js +27 -0
- package/dist/sync/tasks/pipeline.js +29 -9
- package/dist/sync/tasks/reports.js +176 -49
- package/dist/sync/tasks/sessions.js +88 -21
- package/dist/sync/tasks/tickets.js +59 -8
- package/dist/sync/types.js +60 -1
- package/dist/sync/windowing.js +0 -22
- package/dist/sync/write-contract.js +79 -0
- package/package.json +2 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/store/object-kinds.ts —— 库内对象类型(基表 / view …)的**唯一实现点**
|
|
2
|
+
// (2026-11-14,Codex PR#76)。
|
|
3
|
+
//
|
|
4
|
+
// 为什么在 store/ 而不是 commands/:`store/duckdb.ts` 的 `init()` 自身也必须判断
|
|
5
|
+
// 「这个对象是不是基表」(他人库指纹不认同名 view),而 `store/` 不得 import
|
|
6
|
+
// `commands/`。SQL 字符串在 `commands/store-precheck.ts` 的 `OBJECT_KINDS_SQL`
|
|
7
|
+
// (那里按命令层用法导出),本模块只放**判据**——两边共用这一份。
|
|
8
|
+
export function objectKinds(rows) {
|
|
9
|
+
return rows.map((r) => ({ name: String(r.table_name), type: String(r.table_type ?? '') }));
|
|
10
|
+
}
|
|
11
|
+
/** 是否为基表(类型缺失一律按「不是」处理 —— 宁可多拦,不可漏拦)。 */
|
|
12
|
+
export function isBaseTable(o) {
|
|
13
|
+
return o.type.toUpperCase() === 'BASE TABLE';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 是否为同一个对象名(DuckDB 标识符大小写不敏感)。
|
|
17
|
+
*
|
|
18
|
+
* ⚠️ 实测(Codex PR#76):`CREATE VIEW "TICKETS"` 之后 `SELECT COUNT(*) FROM "tickets"`
|
|
19
|
+
* 执行的就是这条 view —— 元数据里的名字是 `TICKETS`,本 CLI 的声明名全是小写,
|
|
20
|
+
* 精确字符串比较会漏掉它并继续执行 view 本体。所有「声明名 ↔ 库内对象」的比对都走这里。
|
|
21
|
+
*
|
|
22
|
+
* ⚠️ **只折叠 ASCII A–Z**(CodeRabbit PR#76 第二轮):DuckDB 的标识符大小写不敏感只针对
|
|
23
|
+
* ASCII 字母 —— 实测引号建的 `"ticKets"`(U+212A KELVIN SIGN)与 `"tickets"` 是两个对象,
|
|
24
|
+
* 而 JS 的 `toLowerCase()` 会把它折叠成 `tickets`(`"ticKets".toLowerCase() === "tickets"`),
|
|
25
|
+
* 于是两条同名字符串上本存在一个名字含 `K` 的基表、一个含 `K` 的 view 时,会把基表也当成
|
|
26
|
+
* 非基表跳过。用 `replace(/[A-Z]/g, …)` 替代 `toLowerCase()` / `localeCompare`(后者同样做
|
|
27
|
+
* Unicode 折叠与区域设置相关的排序)。
|
|
28
|
+
*/
|
|
29
|
+
export function sameObjectName(a, b) {
|
|
30
|
+
return foldAscii(a) === foldAscii(b);
|
|
31
|
+
}
|
|
32
|
+
const foldAscii = (s) => s.replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32));
|
package/dist/store/retry.js
CHANGED
|
@@ -1,36 +1,219 @@
|
|
|
1
1
|
// src/store/retry.ts
|
|
2
2
|
// ⚠️ 曲线是**固定间隔**(实测 2.6s/13 次成功 ≈ 200ms 间隔,spec §5.2)——
|
|
3
3
|
// 不是指数:指数曲线在剩余预算不足时会大幅越过 maxMs。
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* 锁冲突的识别:**模板**匹配,不是子串搜索。
|
|
6
|
+
*
|
|
7
|
+
* 真实报文(跨进程真持锁,@duckdb/node-api 1.5.6-r.1)。**两种平台形态**(都逐字实测):
|
|
8
|
+
* · macOS:`IO Error: Could not set lock on file "<路径>": Conflicting lock is held in
|
|
9
|
+
* /Users/x/.nvm/…/bin/node (PID 83991) by user x. See also https://…`
|
|
10
|
+
* · Linux:`… Conflicting lock is held in /opt/hostedtoolcache/node/24.22.1/x64/bin/node
|
|
11
|
+
* (PID 4894). See also https://…` —— **没有 ` by user ` 那段**。
|
|
12
|
+
* 首版判据锚了三段(含 ` by user `),那是**只在本机探针过**的产物:CI 立刻在
|
|
13
|
+
* Node 20.12/22/24 上全红(同一个用例在我这里是绿的)。教训与「采样源按平台核对」同族 ——
|
|
14
|
+
* 探针跑在哪个平台,判据就只对那个平台成立。现只锚两段跨平台实测都有的骨架。
|
|
15
|
+
*
|
|
16
|
+
* ⚠️⚠️ **不能**只搜 `Conflicting lock` 子串(2026-11-01 实测,形态「报文里嵌了用户给的
|
|
17
|
+
* 路径」):DuckDB 的**其它**报文同样把库路径原文嵌进去 ——
|
|
18
|
+
* `IO Error: The file "/tmp/Conflicting lock/q.db" exists, but it is not a valid
|
|
19
|
+
* DuckDB database file!`(路径里恰好含这两个词)。子串判于是把**非 DuckDB 文件**
|
|
20
|
+
* 判成撞锁:`db tables` / `store status` 会**白白退避 30 秒**再报「数据库被写进程占用」,
|
|
21
|
+
* `retryable: true` 还把 Agent 引向「稍后重试」—— 而真相是那个路径根本不是数据库文件,
|
|
22
|
+
* 重试一万次也一样。实测:`db tables --db '/tmp/qreview/Conflicting lock/q.db'` 耗时
|
|
23
|
+
* 30.1s、`kind: store_locked`;同一份内容放在不含该词的路径上 0.4s 就给出正确的
|
|
24
|
+
* `store_not_initialized`。
|
|
25
|
+
*
|
|
26
|
+
* 判据锚 DuckDB 自己的**模板骨架**:句首 `IO Error: Could not set lock on file "<路径>": `
|
|
27
|
+
* 与紧随的 `Conflicting lock is held in `。两段都不在路径里,第二种报文命中不了
|
|
28
|
+
*(`not a valid DuckDB database file` 那句句首是 `The file `)。
|
|
29
|
+
* ⚠️ 边界如实记录:路径由用户给出且会被原文嵌入,理论上可以构造一个路径让这句看起来
|
|
30
|
+
* 也像别的报文;但那要求整条报文**以** `IO Error: Could not set lock on file "` 开头,
|
|
31
|
+
* 而只有锁报文会这样开头 —— 放宽的代价(上面那 30 秒假锁 + 假 retryable)则是实测发生的。
|
|
32
|
+
*
|
|
33
|
+
* 与 spec §5.2 原文「message 含 `Conflicting lock`」的偏离是**有意**的:那句话写于
|
|
34
|
+
* 判据只有一处消费点、且假设报文只可能是锁报文的时候。
|
|
35
|
+
*/
|
|
5
36
|
export function isLockError(e) {
|
|
6
37
|
const msg = e instanceof Error ? e.message : String(e);
|
|
7
|
-
return
|
|
38
|
+
return /^IO Error: Could not set lock on file "[\s\S]*": Conflicting lock is held in /i.test(msg);
|
|
8
39
|
}
|
|
9
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* 识别「模块找不到」并取出缺失的 specifier(spec §5.1 的二分规则)。
|
|
42
|
+
*
|
|
43
|
+
* ⚠️ **两种错误码都要认**(2026-10-31 实测修):此前只认 `ERR_MODULE_NOT_FOUND`,
|
|
44
|
+
* 而那只可能在 **ESM 解析**阶段出现 —— 能命中它的 specifier 恒是 `@duckdb/node-api`
|
|
45
|
+
* 本身(整个包不装时)。**平台 binding 缺失**是另一条路径:`@duckdb/node-bindings`
|
|
46
|
+
* 用的是 **CJS `require`**(`duckdb.js` 里 `require('@duckdb/node-bindings-<平台>/duckdb.node')`),
|
|
47
|
+
* 抛出的 `e.code` 是 **`MODULE_NOT_FOUND`**(无 `ERR_` 前缀)。实测(删掉平台包后真跑):
|
|
48
|
+
* · `code = "MODULE_NOT_FOUND"`
|
|
49
|
+
* · `msg = "Cannot find module '@duckdb/node-bindings-darwin-x64/duckdb.node'"`
|
|
50
|
+
* 只认 ESM 码时这条一路返回 null → `depError` 的二分退化成单一分支,**「当前平台无预编译包」
|
|
51
|
+
* 那句 hint 在生产上从未被执行过**,Agent 拿到的是「未安装 DuckDB 依赖,请 npm install
|
|
52
|
+
* @duckdb/node-api」—— 而 `@duckdb/node-api` **本来就装着**(缺的是它的平台子包),
|
|
53
|
+
* 照 hint 重装修不好。这正是该分支存在的唯一理由(optionalDependency 在不支持的平台漏装)。
|
|
54
|
+
*
|
|
55
|
+
* 报文两种形态都覆盖:ESM 是 `Cannot find package '…'`、CJS 是 `Cannot find module '…'`
|
|
56
|
+
* (注意 binding 那条的 specifier 带 `/duckdb.node` 后缀,`startsWith('@duckdb/node-bindings')`
|
|
57
|
+
* 仍然成立)。
|
|
58
|
+
*/
|
|
10
59
|
export function isModuleNotFound(e) {
|
|
11
60
|
const err = e;
|
|
12
|
-
if (err?.code !== 'ERR_MODULE_NOT_FOUND')
|
|
61
|
+
if (err?.code !== 'ERR_MODULE_NOT_FOUND' && err?.code !== 'MODULE_NOT_FOUND')
|
|
13
62
|
return null;
|
|
14
63
|
const m = /Cannot find (?:package|module) '([^']+)'/.exec(err.message ?? '');
|
|
15
64
|
return { missing: m?.[1] ?? '' };
|
|
16
65
|
}
|
|
66
|
+
/** 「不是 DuckDB 数据库文件」的 hint(单一文案,store/query 共用)。 */
|
|
67
|
+
export const NOT_DUCKDB_HINT = '该路径已存在且不是 DuckDB 数据库,请换 --db 路径';
|
|
68
|
+
/**
|
|
69
|
+
* 「权限不足」的 hint(单一文案,store/query 共用)。
|
|
70
|
+
* ⚠️ 保持**读写中性**:`status`/`query` 是只读命令,说「写权限」会把只读者引向 `store init`。
|
|
71
|
+
*/
|
|
72
|
+
export const PERMISSION_HINT = '请检查路径与所在目录的读写权限(磁盘只读也会导致无法打开)';
|
|
73
|
+
/**
|
|
74
|
+
* 「文件已损坏」的 hint(单一文案,store/query/sync 共用)。
|
|
75
|
+
*
|
|
76
|
+
* ⚠️ 2026-11-05 审查修:此前「截断 / 页校验和不对」两类报文既不匹配权限、也不匹配
|
|
77
|
+
* 「非 DuckDB 文件」,落进各命令自己的兜底 —— `store status` 说「数据库执行期错误,
|
|
78
|
+
* 请用 store status 检查」(拿自己查自己),`query` 说「请确认文件存在且由本 CLI 创建
|
|
79
|
+
* (store init)」—— 而对已损坏的文件原地跑 `store init` 打开同样失败,是条死路。
|
|
80
|
+
* hint 必须先给备份(文件里是用户已同步的数据),再给可执行的恢复路径(新路径重建)。
|
|
81
|
+
*/
|
|
82
|
+
export const CORRUPT_HINT = '该文件已损坏(写入中断、磁盘写满、复制不完整都会造成),原地跑 store init 修不好,查询也会失败。请先备份该文件留档,再用 --db 指向一个新路径跑 store init 重建 —— 本地库只是缓存,之后带同一个 --db <新路径> 跑 qiyu sync run 重新拉取(不带 --db 的 sync run 仍会读到这个损坏的文件)';
|
|
83
|
+
const OPEN_FAILURE_HINTS = {
|
|
84
|
+
not_duckdb: NOT_DUCKDB_HINT,
|
|
85
|
+
permission: PERMISSION_HINT,
|
|
86
|
+
corrupt: CORRUPT_HINT,
|
|
87
|
+
};
|
|
88
|
+
/** 成因 → hint 的唯一映射点(`Record` 保证新增成因时编译期就漏不掉)。 */
|
|
89
|
+
export function openFailureHint(f) {
|
|
90
|
+
return OPEN_FAILURE_HINTS[f.cause];
|
|
91
|
+
}
|
|
92
|
+
export function classifyOpenFailure(e) {
|
|
93
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
94
|
+
if (/is not a valid DuckDB database file!?$/.test(msg))
|
|
95
|
+
return { cause: 'not_duckdb' };
|
|
96
|
+
if (/:\s*(Permission denied|Operation not permitted|Read-only file system)\s*$/.test(msg)) {
|
|
97
|
+
return { cause: 'permission' };
|
|
98
|
+
}
|
|
99
|
+
if (/^IO Error: Could not read enough bytes from file "/.test(msg))
|
|
100
|
+
return { cause: 'corrupt' };
|
|
101
|
+
if (/^IO Error: Corrupt database file:/.test(msg))
|
|
102
|
+
return { cause: 'corrupt' };
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
17
105
|
const defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
106
|
+
/** 单次尝试超时:`isRetryable` 之外由 `retryWithBackoff` 自行按可重试处理。 */
|
|
107
|
+
class AttemptTimeout extends Error {
|
|
108
|
+
constructor(ms) { super(`打开超过 ${ms}ms 未返回`); }
|
|
109
|
+
}
|
|
110
|
+
function withAttemptTimeout(p, ms) {
|
|
111
|
+
if (ms === undefined)
|
|
112
|
+
return p;
|
|
113
|
+
let timer;
|
|
114
|
+
// ⚠️ **不要 unref**(CodeRabbit PR#74 第二轮 major,实测确认):
|
|
115
|
+
// 计时器在 `finally` 里就被 `clearTimeout`,活不过这次尝试,unref 省不下任何东西;
|
|
116
|
+
// 而一旦尝试本身不持有任何活跃 handle(`new Promise(() => {})`、某些原生卡住形态),
|
|
117
|
+
// unref 的计时器会让 Node **直接退出**(实测:探针脚本 0.1s 退出、超时回调从未执行)——
|
|
118
|
+
// CLI 于是静默 exit 0、一个字都不吐,比「一直挂着」更难诊断。
|
|
119
|
+
// ⚠️ 残余边界(如实记录):被放弃的原生调用若仍持有活跃句柄,进程可能在打出
|
|
120
|
+
// `store_locked` 之后**仍不立刻退出** —— CLI 刻意不用 `process.exit`(会截断 stdout,
|
|
121
|
+
// 见 `src/cli.ts` 入口注释),所以这里换到的是「结论一定给出」,不是「进程一定立刻结束」。
|
|
122
|
+
// 为什么不用「拿到结论后宽限几秒再硬退出」补这一刀(CodeRabbit PR#74 第七轮提过):
|
|
123
|
+
// 那需要在入口调 `process.exit`,与 `tests/cli.test.ts` 的架构不变量直接冲突,而那条
|
|
124
|
+
// 不变量来自**实测事故**(管道输出 392846 字节只写出 131072,退出码仍 0、JSON 断在中间)。
|
|
125
|
+
// 「大输出被静默截断」比「错误已报出但进程多活一会儿」更贵:前者让 Agent 拿到坏 JSON
|
|
126
|
+
// 且以为成功,后者只是慢。所以这里保留残余边界,不引入退出兜底。
|
|
127
|
+
const timeout = new Promise((_, reject) => {
|
|
128
|
+
timer = setTimeout(() => reject(new AttemptTimeout(ms)), ms);
|
|
129
|
+
});
|
|
130
|
+
return Promise.race([p, timeout]).finally(() => { if (timer !== undefined)
|
|
131
|
+
clearTimeout(timer); });
|
|
132
|
+
}
|
|
18
133
|
/** 有界重试:可重试错误按固定间隔重试至 maxMs,超限抛最后一个错误。 */
|
|
19
134
|
export async function retryWithBackoff(opts) {
|
|
135
|
+
// 被放弃、还没交回清理的尝试(超时时刻 → 那次尝试的 promise)。清理时机见 `settle`。
|
|
136
|
+
const pendingAbandoned = [];
|
|
137
|
+
return settle(runLoop(opts, pendingAbandoned), pendingAbandoned, opts.onAbandoned);
|
|
138
|
+
}
|
|
139
|
+
async function runLoop(opts, pendingAbandoned) {
|
|
20
140
|
const intervalMs = opts.intervalMs ?? 200;
|
|
21
141
|
const sleep = opts.sleep ?? defaultSleep;
|
|
22
142
|
const now = opts.now ?? (() => Date.now());
|
|
23
143
|
const start = now();
|
|
144
|
+
const onAbandoned = opts.onAbandoned;
|
|
145
|
+
// 上一次失败的「预算耗尽时该抛什么」。⚠️ 循环顶部还要再查一次(Codex PR#74 第三轮 P2):
|
|
146
|
+
// `sleep` 可能比 `intervalMs` 长得多(进程被挂起、注入的 sleeper 把时钟推过头),
|
|
147
|
+
// 这时预算其实已经用光,但旧的判据只在**失败之后**比 —— 会再发起一次新的 `tryOpen`,
|
|
148
|
+
// 等于在 `readOnlyRetryMs` 到期后还去打开数据库(真实副作用:抢锁、起原生线程)。
|
|
149
|
+
let exhausted;
|
|
24
150
|
for (;;) {
|
|
151
|
+
if (exhausted !== undefined && now() - start >= opts.maxMs)
|
|
152
|
+
throw exhausted.err;
|
|
153
|
+
let attempt;
|
|
25
154
|
try {
|
|
26
|
-
|
|
155
|
+
attempt = opts.tryOpen();
|
|
27
156
|
}
|
|
28
157
|
catch (e) {
|
|
29
|
-
|
|
158
|
+
// 同步抛出也按普通失败处理(此前会直接冒泡、绕过预算判断)
|
|
159
|
+
attempt = Promise.reject(e);
|
|
160
|
+
}
|
|
161
|
+
// 这个 promise 之前超时过一次、现在又被当作新尝试(`tryOpen` 复用)→ 仍算放弃,
|
|
162
|
+
// 记下最早的超时时刻并把它从待清理表里取出来,等这一次的结果落定后再判。
|
|
163
|
+
const againAt = pendingAbandoned.find((x) => x.p === attempt)?.at;
|
|
164
|
+
if (againAt !== undefined) {
|
|
165
|
+
pendingAbandoned.splice(pendingAbandoned.findIndex((x) => x.p === attempt), 1);
|
|
166
|
+
}
|
|
167
|
+
// 单次尝试的上限还要被**剩余总预算**夹住(CodeRabbit PR#74):否则 30s 预算里
|
|
168
|
+
// 前面撞锁花掉 29s 后,下一次尝试仍能再跑满 30s —— 实际等待近 60s。
|
|
169
|
+
// `Math.max(1, …)` 只是兜住「剩余预算已 ≤0」的病态输入(1ms 的一次尝试仍是有界的);
|
|
170
|
+
// 「预算已耗尽却还发起新尝试」由循环顶部的 `exhausted` 判据挡住,不靠这个下限。
|
|
171
|
+
const remainingMs = opts.maxMs - (now() - start);
|
|
172
|
+
const attemptCap = opts.attemptMs === undefined ? undefined
|
|
173
|
+
: Math.max(1, Math.min(opts.attemptMs, remainingMs));
|
|
174
|
+
try {
|
|
175
|
+
return await withAttemptTimeout(attempt, attemptCap);
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
const timedOut = e instanceof AttemptTimeout;
|
|
179
|
+
if (timedOut && onAbandoned !== undefined) {
|
|
180
|
+
// 只**登记**,此刻不注册清理(见 `settle` 的注释):它可能被下一次尝试复用并成为结果。
|
|
181
|
+
pendingAbandoned.push({ at: againAt ?? now(), p: attempt });
|
|
182
|
+
}
|
|
183
|
+
if (!timedOut && !opts.isRetryable(e))
|
|
30
184
|
throw e;
|
|
185
|
+
// 超时且没有更具体的成因时,给调用方一个可识别的锁语义(与既有 store_locked 包装同源)
|
|
186
|
+
// 用自有字段而不是标准的 `cause`(那应该是 Error,写成字符串会误导诊断)
|
|
187
|
+
const budgetSpent = timedOut
|
|
188
|
+
// 报文用**实际生效的**那一档上限(`attemptCap` 可能被剩余预算夹小),
|
|
189
|
+
// 不是配置里的 `attemptMs` —— 否则日志说「等了 30000ms」而实际只等了 400ms。
|
|
190
|
+
? Object.assign(new Error(`打开超时(本次尝试 ${attemptCap ?? opts.attemptMs}ms 内未返回,等待 ${opts.maxMs}ms 后放弃)`), { timedOut: true })
|
|
191
|
+
: e;
|
|
192
|
+
exhausted = { err: budgetSpent };
|
|
31
193
|
if (now() - start + intervalMs > opts.maxMs)
|
|
32
|
-
throw
|
|
194
|
+
throw budgetSpent;
|
|
33
195
|
await sleep(intervalMs);
|
|
34
196
|
}
|
|
35
197
|
}
|
|
36
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* 重试循环的收尾:把**没被返回**的放弃尝试交回清理。
|
|
201
|
+
*
|
|
202
|
+
* 为什么不直接写在循环体的 `finally` 里:`return` 的 `finally` 会在值交给调用方之前跑 ——
|
|
203
|
+
* 只要把「本次返回的那个 promise」也一并清理,调用方拿到的就是关掉的连接。这里改成
|
|
204
|
+
* 「循环只负责登记放弃者,收尾统一处理」,并用 `pending` 里已经剔除「已复用的那个」来保证
|
|
205
|
+
* 不会误关;三条出口(成功返回 / 不可重试抛错 / 预算耗尽抛错)由同一个 `finally` 覆盖。
|
|
206
|
+
*/
|
|
207
|
+
async function settle(loop, pending, onAbandoned) {
|
|
208
|
+
try {
|
|
209
|
+
return await loop;
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
if (onAbandoned !== undefined) {
|
|
213
|
+
for (const x of pending) {
|
|
214
|
+
const done = x.p.then((v) => onAbandoned(v), () => { });
|
|
215
|
+
done.catch(() => { });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
package/dist/store/schema.js
CHANGED
|
@@ -36,11 +36,13 @@ export const TABLES = [
|
|
|
36
36
|
{
|
|
37
37
|
name: 'sync_gaps',
|
|
38
38
|
columns: [col('dataset', 'VARCHAR', false), col('range_start', 'VARCHAR', false), col('range_end', 'VARCHAR'),
|
|
39
|
-
|
|
39
|
+
// reason 进主键(v10):同一窗可能同时缺行(row_missing_key)与缺 fixture
|
|
40
|
+
// (export_fixture_unreported)—— 两类提示互不覆盖,一窗一行会互相覆盖。
|
|
41
|
+
col('reason', 'VARCHAR', false), col('recorded_at', 'VARCHAR'),
|
|
40
42
|
// D-T3:缺口诊断详情(recordGap 的 detail 落点)。
|
|
41
43
|
col('detail', 'VARCHAR')],
|
|
42
|
-
primaryKey: ['dataset', 'range_start'],
|
|
43
|
-
grain: '
|
|
44
|
+
primaryKey: ['dataset', 'range_start', 'reason'],
|
|
45
|
+
grain: '每个「缺口区间 × 原因」一行(同一窗可能有多条互不覆盖的提示)',
|
|
44
46
|
},
|
|
45
47
|
{
|
|
46
48
|
name: 'tags',
|
|
@@ -319,7 +321,12 @@ export const TABLES = [
|
|
|
319
321
|
},
|
|
320
322
|
];
|
|
321
323
|
export function tableByName(name) {
|
|
322
|
-
|
|
324
|
+
// 名字匹配按 DuckDB 的标识符规则:仅折叠 ASCII A–Z,不做 Unicode 折叠(Codex PR#76;
|
|
325
|
+
// 判据与 `store/object-kinds.ts` 的 `sameObjectName` 同一规则 —— schema.ts 在本模块内
|
|
326
|
+
// 不能再依赖 duckdb 层,故就地实现;`tests/store/object-kinds.test.ts` 双向钉住两者一致)。
|
|
327
|
+
const fold = (x) => x.replace(/[A-Z]/g, (c) => c.toLowerCase());
|
|
328
|
+
const key = fold(name);
|
|
329
|
+
return TABLES.find((t) => fold(t.name) === key);
|
|
323
330
|
}
|
|
324
331
|
/** v1 基线批次(C1 交付的四张);v3/v5 起的数据表批次不进 v1 建表循环。 */
|
|
325
332
|
export const V1_TABLES = ['schema_version', 'sync_state', 'sync_gaps', 'tags'];
|
|
@@ -413,6 +420,27 @@ export const MIGRATIONS = [
|
|
|
413
420
|
await exec('ALTER TABLE "sync_state" ADD COLUMN IF NOT EXISTS "resolved_range_end" VARCHAR');
|
|
414
421
|
},
|
|
415
422
|
},
|
|
423
|
+
// v10(2026-11-05,CodeRabbit PR #67):sync_gaps 主键补 reason —— 同一窗可能同时
|
|
424
|
+
// 缺行(row_missing_key)与缺 fixture(export_fixture_unreported),两类提示互不
|
|
425
|
+
// 覆盖;旧主键 (dataset, range_start) 让后写的一条覆盖前一条,消费方按 reason 分支
|
|
426
|
+
// 时永远漏一类。DuckDB 不能 ALTER PRIMARY KEY → 重建表。
|
|
427
|
+
// ⚠️ 幂等性:新库的 v1 已按新主键建表,本迁移照常跑(copy → drop → rename 对同构
|
|
428
|
+
// 表是恒等变换)。reason 为 NULL 的历史行 COALESCE 成 'unknown'(缺 reason 的缺口行
|
|
429
|
+
// 对消费方本来就没有可分支的信息,且主键列不能为 NULL)。
|
|
430
|
+
{
|
|
431
|
+
version: 10,
|
|
432
|
+
up: async (exec) => {
|
|
433
|
+
await exec(`CREATE TABLE "sync_gaps_v10" (
|
|
434
|
+
"dataset" VARCHAR NOT NULL, "range_start" VARCHAR NOT NULL, "range_end" VARCHAR,
|
|
435
|
+
"reason" VARCHAR NOT NULL, "recorded_at" VARCHAR, "detail" VARCHAR,
|
|
436
|
+
PRIMARY KEY ("dataset", "range_start", "reason"))`);
|
|
437
|
+
await exec(`INSERT INTO "sync_gaps_v10" ("dataset", "range_start", "range_end", "reason", "recorded_at", "detail")
|
|
438
|
+
SELECT "dataset", "range_start", "range_end", COALESCE("reason", 'unknown'), "recorded_at", "detail"
|
|
439
|
+
FROM "sync_gaps"`);
|
|
440
|
+
await exec('DROP TABLE "sync_gaps"');
|
|
441
|
+
await exec('ALTER TABLE "sync_gaps_v10" RENAME TO "sync_gaps"');
|
|
442
|
+
},
|
|
443
|
+
},
|
|
416
444
|
];
|
|
417
445
|
// ⚠️ E 交付后仍无命令层调用方(`db describe` 展示的是 `TABLES` 的列定义,不是迁移版本号);
|
|
418
446
|
// 现由 `tests/store/schema.test.ts` 与迁移一致性断言消费。保留导出(L2)。
|