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
package/dist/sync/registry.js
CHANGED
|
@@ -3,6 +3,7 @@ import { REPORT_TASKS } from './tasks/reports.js';
|
|
|
3
3
|
import { TICKET_TASKS } from './tasks/tickets.js';
|
|
4
4
|
import { SESSIONS_TASKS } from './tasks/sessions.js';
|
|
5
5
|
import { KNOWLEDGE_TASKS } from './tasks/knowledge.js';
|
|
6
|
+
import { assertWriteContracts, assertGateContracts } from './write-contract.js';
|
|
6
7
|
/** 全量同步任务注册表:dim 6 + reports 9 + tickets 2 + sessions 1 + knowledge 3 = 21。 */
|
|
7
8
|
export const SYNC_TASKS = [
|
|
8
9
|
...DIM_TASKS,
|
|
@@ -11,6 +12,11 @@ export const SYNC_TASKS = [
|
|
|
11
12
|
...SESSIONS_TASKS,
|
|
12
13
|
...KNOWLEDGE_TASKS,
|
|
13
14
|
];
|
|
15
|
+
// 写策略声明前提的加载期校验(`write-contract.ts`):声明错了就在此炸,
|
|
16
|
+
// 而不是在某个真实租户的边界日里静默丢/留数据。
|
|
17
|
+
assertWriteContracts(SYNC_TASKS);
|
|
18
|
+
// 闸门键:声明了就必须非空且含主端点(Sourcery PR#58;空列表会静默回退成单键)。
|
|
19
|
+
assertGateContracts(SYNC_TASKS);
|
|
14
20
|
/** 按 id 取任务(sync run <id> 的分派入口)。 */
|
|
15
21
|
export function taskById(id) {
|
|
16
22
|
return SYNC_TASKS.find((t) => t.id === id);
|
package/dist/sync/state.js
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sync_state.range_status` 的取值(闭集;`null` = 从未运行过)。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ 运行时数组 + `typeof ARR[number]` 而不是裸联合类型:这是**输出数据里的机器值**
|
|
5
|
+
* (`sync status` 的 `range_status`),Agent 要按它分支。值域必须能在运行时导出,
|
|
6
|
+
* `meta.field_notes` 的图例与测试才能双向核对「枚举 ⊆ 图例、图例 ⊆ 枚举」。
|
|
7
|
+
*
|
|
8
|
+
* ⚠️⚠️ **`complete` 的真实语义不是「全窗口成功」**(设计稿 §6.1 的原话),而是
|
|
9
|
+
* 「水位推进过」——引擎在**每个成功窗**的同一事务里调 `advanceWatermark`,它无条件写
|
|
10
|
+
* `complete`。2026-10-27 实测(3 窗、中窗失败):run 报 `status: partial`、
|
|
11
|
+
* `windows: {succeeded: 2, failed: 1}`,而 `range_status` 仍是 `complete`。
|
|
12
|
+
* 所以 `complete` **不能**用来判定「这段数据完整」;缺口要看 `gaps` / `sync_gaps`。
|
|
13
|
+
*
|
|
14
|
+
* 反过来 `pending` 也**不代表「一行都没写」**:首窗失败时 `contiguousEnd` 为 null,
|
|
15
|
+
* 后续成功窗的 `commitWindow` 拿到的 `watermarkRange` 是 null → 不调 `advanceWatermark`,
|
|
16
|
+
* 数据照常落库而状态停在 `markPending` 写的 `pending`。2026-10-27 实测:3 窗中首窗失败、
|
|
17
|
+
* 后两窗成功 → `range_status: pending`、`watermark: null`,但表里**有 2 行数据**。
|
|
18
|
+
* 所以 `pending` 只说明「水位没动过」,不说明「库里没数据」。
|
|
19
|
+
*/
|
|
20
|
+
export const RANGE_STATUSES = ['pending', 'complete'];
|
|
1
21
|
/** 读某 dataset 的水位状态行;不存在时全空。 */
|
|
2
22
|
export async function readWatermark(store, dataset) {
|
|
3
23
|
// 常量 SQL 调用点 1/3
|
package/dist/sync/tasks/dim.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// src/sync/tasks/dim.ts
|
|
2
|
-
// D-T6:维度表快照任务(6
|
|
3
|
-
//
|
|
2
|
+
// D-T6:维度表快照任务(6 任务)。⚠️ `tags` 表在 V1 就建了,但**本仓库没有任何同步任务
|
|
3
|
+
// 声明它**(`tag.all` 只是可调接口,不是同步数据集 id)——`sync run` 不会填充它,
|
|
4
|
+
// `sync tasks` 的输出里也没有它;早前这里写「归属 T12 汇总时一并声明」,与实况不符。
|
|
5
|
+
// 全部 kind=snapshot:接口全量无分页(设计稿 §6.2
|
|
4
6
|
// 实测),幂等来自「全删重插」。fetch 内部经 http.call + 信封归一化——sync 不绕过信封层。
|
|
5
7
|
// 落库行按 TABLES 列集显式投影(缺列补 null)——响应多出的键不进库,缺的键不炸 upsert。
|
|
6
8
|
//
|
|
@@ -26,16 +28,17 @@ const isEmptyKey = (v) => v === null || v === undefined || v === '';
|
|
|
26
28
|
* `keyCols`(C 低-7):落库主键列 —— 值为空串的行**显式失败**,绝不因 `''` 写出一行
|
|
27
29
|
* 能被反复覆盖的同键行(空串是合法的 VARCHAR 值,upsert 主键语义会把它们当成同一个键)。
|
|
28
30
|
* 只有 cols 里出现的键列才在这里判定(如 template_id 由行外补齐,由调用方自行守卫)。
|
|
31
|
+
*
|
|
32
|
+
* ⚠️ 这里**没有** per-row 变换回调(审查 #5,2026-11-03):早前本函数带 `perRow`,
|
|
33
|
+
* 但它返回 null 时静默 `continue` 丢行,而**所有**调用点都传 `undefined`——一支永不执行、
|
|
34
|
+
* 一旦执行就静默丢行的死代码。删除它,丢行只保留「显式抛错(keyCols)」一种形态。
|
|
29
35
|
*/
|
|
30
|
-
function project(rows, cols, nowIso,
|
|
36
|
+
function project(rows, cols, nowIso, keyCols = []) {
|
|
31
37
|
const out = [];
|
|
32
38
|
for (const raw of rows) {
|
|
33
|
-
const mid = perRow ? perRow(raw) : raw;
|
|
34
|
-
if (mid === null)
|
|
35
|
-
continue;
|
|
36
39
|
const row = {};
|
|
37
40
|
for (const c of cols) {
|
|
38
|
-
const v =
|
|
41
|
+
const v = raw[c];
|
|
39
42
|
if (isEmptyKey(v) && keyCols.includes(c)) {
|
|
40
43
|
throw new Error(`响应出现空的主键列 ${c}(值为 ${JSON.stringify(v)})——` +
|
|
41
44
|
'空串/缺键会写出一行能被反复覆盖的同键行,拒绝落库');
|
|
@@ -49,9 +52,9 @@ function project(rows, cols, nowIso, perRow, keyCols = []) {
|
|
|
49
52
|
}
|
|
50
53
|
/** 维度快照任务的通用 fetch:callPaced 调接口 → 行数组 → 投影直写(写侧引擎全删重插)。
|
|
51
54
|
* ⚠️ 走 callPaced(B 高-1):本地令牌桶拒绝被吸收为等待重试,远端限频写持久化冷却行。 */
|
|
52
|
-
async function fetchAll(endpointId, body, cols, ctx, nowIso,
|
|
55
|
+
async function fetchAll(endpointId, body, cols, ctx, nowIso, keyCols = []) {
|
|
53
56
|
const res = await callPaced(endpointId, body, ctx);
|
|
54
|
-
const rows = project(rowsOf(res, endpointId), cols, nowIso,
|
|
57
|
+
const rows = project(rowsOf(res, endpointId), cols, nowIso, keyCols);
|
|
55
58
|
return { rows, apiCount: rows.length };
|
|
56
59
|
}
|
|
57
60
|
// 列集常量(与 schema.ts TABLES 对齐;漂移由 schema drift 对拍测试拦)
|
|
@@ -71,7 +74,7 @@ export const DIM_TASKS = [
|
|
|
71
74
|
write: { kind: 'upsertRows' },
|
|
72
75
|
// role 文档标必填但实测 status/role 都省略时返回全部 330 行(§6.2)——不传,取全量。
|
|
73
76
|
// groupInfo 列表 → JSON 字符串列(project 统一 JSON.stringify,非字符串值均适用)。
|
|
74
|
-
fetch: (w, ctx) => fetchAll('staff.list.new', {}, STAFF_COLS, ctx, nowIsoOf(ctx),
|
|
77
|
+
fetch: (w, ctx) => fetchAll('staff.list.new', {}, STAFF_COLS, ctx, nowIsoOf(ctx), ['id']),
|
|
75
78
|
},
|
|
76
79
|
{
|
|
77
80
|
id: 'dim.staff_groups', table: 'staff_groups', kind: 'snapshot',
|
|
@@ -80,7 +83,7 @@ export const DIM_TASKS = [
|
|
|
80
83
|
endpoint: 'staff.group.list.new',
|
|
81
84
|
write: { kind: 'upsertRows' },
|
|
82
85
|
// staff:true 取 staffIdList(成员关系单独落 staff_group_members,本表不存)。
|
|
83
|
-
fetch: (w, ctx) => fetchAll('staff.group.list.new', { staff: true }, STAFF_GROUP_COLS, ctx, nowIsoOf(ctx),
|
|
86
|
+
fetch: (w, ctx) => fetchAll('staff.group.list.new', { staff: true }, STAFF_GROUP_COLS, ctx, nowIsoOf(ctx), ['id']),
|
|
84
87
|
},
|
|
85
88
|
{
|
|
86
89
|
id: 'dim.staff_group_members', table: 'staff_group_members', kind: 'snapshot',
|
|
@@ -121,6 +124,8 @@ export const DIM_TASKS = [
|
|
|
121
124
|
out.push({ group_id: gid, staff_id: String(sid), synchronized_at: nowIso });
|
|
122
125
|
}
|
|
123
126
|
}
|
|
127
|
+
// ⚠️ `reason` 必须 `as const`:宽松下推成 `string` 后 `FetchResult.gapHint` 的类型守卫
|
|
128
|
+
// 会因此报错,而这里唯一可能的取值就是该枚举成员。
|
|
124
129
|
const gapHint = (malformed.length > 0 || skippedMembers > 0)
|
|
125
130
|
? {
|
|
126
131
|
reason: 'staff_id_list_malformed',
|
|
@@ -139,7 +144,7 @@ export const DIM_TASKS = [
|
|
|
139
144
|
endpoint: 'category.list',
|
|
140
145
|
write: { kind: 'upsertRows' },
|
|
141
146
|
// 文档标 type 必填(2=工单分类);响应列名是 parentId,落库保持原名。
|
|
142
|
-
fetch: (w, ctx) => fetchAll('category.list', { type: 2 }, CATEGORY_COLS, ctx, nowIsoOf(ctx),
|
|
147
|
+
fetch: (w, ctx) => fetchAll('category.list', { type: 2 }, CATEGORY_COLS, ctx, nowIsoOf(ctx), ['id']),
|
|
143
148
|
},
|
|
144
149
|
{
|
|
145
150
|
id: 'dim.ticket_templates', table: 'ticket_templates', kind: 'snapshot',
|
|
@@ -148,13 +153,17 @@ export const DIM_TASKS = [
|
|
|
148
153
|
endpoint: 'ticket.template.list',
|
|
149
154
|
write: { kind: 'upsertRows' },
|
|
150
155
|
// 文档标 status 必填(-1=全部状态);响应 status 全为 1、不携带信息 → 不建列。
|
|
151
|
-
fetch: (w, ctx) => fetchAll('ticket.template.list', { status: -1 }, TEMPLATE_COLS, ctx, nowIsoOf(ctx),
|
|
156
|
+
fetch: (w, ctx) => fetchAll('ticket.template.list', { status: -1 }, TEMPLATE_COLS, ctx, nowIsoOf(ctx), ['id']),
|
|
152
157
|
},
|
|
153
158
|
{
|
|
154
159
|
id: 'dim.ticket_template_fields', table: 'ticket_template_fields', kind: 'snapshot',
|
|
155
160
|
watermark: { field: 'synchronized_at', initial: null },
|
|
156
161
|
primaryKey: ['template_id', 'fieldId'],
|
|
157
162
|
endpoint: 'ticket.template.fields',
|
|
163
|
+
// ⚠️ 闸门必须覆盖 fetch 实际调用的**每个** endpoint(2026-11-02 实测修):模板列表的
|
|
164
|
+
// `ticket.template.list` 被限流时 penalize 写的是它自己的冷却行,只声明
|
|
165
|
+
// `ticket.template.fields` 会让引擎读不到那把冷却、下一窗照旧超发。
|
|
166
|
+
gateEndpoints: ['ticket.template.list', 'ticket.template.fields'],
|
|
158
167
|
write: { kind: 'upsertRows' },
|
|
159
168
|
// 每模板一次调用(接口按 templateId 取字段);行补 template_id 列(主键 template_id+fieldId,fieldId 是响应键)。
|
|
160
169
|
// 闸门键取 ticket.template.fields(该任务的主要调用路径);templates 列表的
|
|
@@ -172,7 +181,7 @@ export const DIM_TASKS = [
|
|
|
172
181
|
}
|
|
173
182
|
const fres = await callPaced('ticket.template.fields', { templateId: t.id }, ctx);
|
|
174
183
|
// 主键列 fieldId 的空值在 project 内显式失败(C 低-7)
|
|
175
|
-
const rows = project(rowsOf(fres, 'ticket.template.fields'), FIELD_COLS, nowIso,
|
|
184
|
+
const rows = project(rowsOf(fres, 'ticket.template.fields'), FIELD_COLS, nowIso, ['fieldId']);
|
|
176
185
|
for (const row of rows)
|
|
177
186
|
out.push({ ...row, template_id: String(t.id) });
|
|
178
187
|
}
|
|
@@ -134,26 +134,55 @@ function projectRows(rows, keys, numericCols, nowIso) {
|
|
|
134
134
|
}
|
|
135
135
|
return { rows: out, dropped };
|
|
136
136
|
}
|
|
137
|
-
/**
|
|
137
|
+
/**
|
|
138
|
+
* 空串主键拒绝(C 低-7,与 dim/reports/tickets/sessions 同一判据):`''` 是合法的
|
|
139
|
+
* VARCHAR 值,落库后会被 upsert 主键语义当成同一个键反复覆盖。
|
|
140
|
+
*/
|
|
141
|
+
const isEmptyKey = (v) => v === null || v === undefined || v === '';
|
|
142
|
+
/**
|
|
143
|
+
* 类目树拍平(children 在叶子上为 null,实测形态——空数组/null 都没有子节点)。
|
|
144
|
+
*
|
|
145
|
+
* 返回**被丢弃的节点数**(审查 #5,2026-11-03):缺 id 的脏节点此前静默 `continue`
|
|
146
|
+
* ——它的**整棵子树**也随之中断(不再递归),而 `apiCount` 取拍平后的行数让配对核对
|
|
147
|
+
* 恒等,快照全删重插下这些类目无声消失。计数把「丢了多少」留给调用方落 gap。
|
|
148
|
+
* 子树一并计入(它们同样没落库),所以丢的是一个子树时数字会大于 1。
|
|
149
|
+
*/
|
|
138
150
|
function flattenTree(robotId, nodes, level, parentId, out, nowIso) {
|
|
139
151
|
if (!Array.isArray(nodes))
|
|
140
|
-
return;
|
|
152
|
+
return 0;
|
|
153
|
+
let dropped = 0;
|
|
141
154
|
for (const n of nodes) {
|
|
142
155
|
const cid = n.id;
|
|
143
|
-
|
|
144
|
-
|
|
156
|
+
// 空串 id 与缺键同等对待(CodeRabbit #61):`''` 是合法的 VARCHAR 值,落库后会被
|
|
157
|
+
// 当成同一个主键反复覆盖 —— 与其余任务「空主键行绝不落库」的红线同口径。
|
|
158
|
+
if (isEmptyKey(cid)) {
|
|
159
|
+
dropped += 1 + countNodes(n.children);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
145
162
|
const sId = String(cid);
|
|
146
163
|
out.push({ robot_id: robotId, category_id: sId, name: typeof n.name === 'string' ? n.name : null,
|
|
147
164
|
parent_id: parentId, level, payload: JSON.stringify(n), synchronized_at: nowIso });
|
|
148
|
-
flattenTree(robotId, n.children, level + 1, sId, out, nowIso);
|
|
165
|
+
dropped += flattenTree(robotId, n.children, level + 1, sId, out, nowIso);
|
|
166
|
+
}
|
|
167
|
+
return dropped;
|
|
168
|
+
}
|
|
169
|
+
/** 子树节点总数(缺 id 节点被丢弃时,它的后代同样没落库,规模要如实计入)。 */
|
|
170
|
+
function countNodes(nodes) {
|
|
171
|
+
if (!Array.isArray(nodes))
|
|
172
|
+
return 0;
|
|
173
|
+
let n = 0;
|
|
174
|
+
for (const node of nodes) {
|
|
175
|
+
n += 1 + countNodes(node?.children);
|
|
149
176
|
}
|
|
177
|
+
return n;
|
|
150
178
|
}
|
|
151
179
|
/**
|
|
152
180
|
* 类目树拍平的共享入口(T13 接线):从根(level=1)拍平一棵 getCategoryTree 响应。
|
|
153
181
|
* 编排层的 robotId 注入路径消费同一实现——复制投影会让行形态在两处漂移。
|
|
182
|
+
* 返回被丢弃的节点数(缺 id 的脏节点及其子树),调用方据此决定是否记 gap。
|
|
154
183
|
*/
|
|
155
184
|
export function flattenCategoryTree(robotId, payload, out, nowIso) {
|
|
156
|
-
flattenTree(robotId, payload, 1, null, out, nowIso);
|
|
185
|
+
return flattenTree(robotId, payload, 1, null, out, nowIso);
|
|
157
186
|
}
|
|
158
187
|
/**
|
|
159
188
|
* knowledge_items / knowledge_points 共用的游标快照 fetch。numericCols 是 INTEGER 列集(数字直通)。
|
|
@@ -235,14 +264,27 @@ export const KNOWLEDGE_TASKS = [
|
|
|
235
264
|
'请先同步 knowledge.items 任务');
|
|
236
265
|
}
|
|
237
266
|
const out = [];
|
|
267
|
+
let dropped = 0;
|
|
238
268
|
for (const rid of robotIds) {
|
|
239
269
|
const res = await callPaced('ai.knowledge.getCategoryTree', { robotId: Number(rid) }, ctx);
|
|
240
270
|
// 形状 A:data 直接是树数组;空树机器人整个丢 data 键(not_found)→ 0 行合法
|
|
241
271
|
if (res.payload === undefined || res.payload === null)
|
|
242
272
|
continue;
|
|
243
|
-
flattenTree(rid, res.payload, 1, null, out, nowIso);
|
|
273
|
+
dropped += flattenTree(rid, res.payload, 1, null, out, nowIso);
|
|
244
274
|
}
|
|
245
|
-
return {
|
|
275
|
+
return {
|
|
276
|
+
rows: out, apiCount: out.length,
|
|
277
|
+
...(dropped > 0
|
|
278
|
+
? {
|
|
279
|
+
gapHint: {
|
|
280
|
+
reason: 'knowledge_row_malformed',
|
|
281
|
+
detail: `knowledge.categories: ${dropped} 个类目节点被丢弃(自身或祖先缺 id,`
|
|
282
|
+
+ '连同其子树;快照全删重插下这些类目已消失)——接口 ai.knowledge.getCategoryTree 的'
|
|
283
|
+
+ '响应形态漂移,请核对',
|
|
284
|
+
},
|
|
285
|
+
}
|
|
286
|
+
: {}),
|
|
287
|
+
};
|
|
246
288
|
},
|
|
247
289
|
},
|
|
248
290
|
];
|
package/dist/sync/tasks/pace.js
CHANGED
|
@@ -41,6 +41,33 @@ export function nowIsoOf(ctx) {
|
|
|
41
41
|
* `gate.acquire(task.endpoint)` 必须落在同一把键上,冷却才拦得住下一次调用。
|
|
42
42
|
*/
|
|
43
43
|
export async function callPaced(endpointId, body, ctx, attempt = 0) {
|
|
44
|
+
// ⚠️ 真正要调用时才查这个端点的冷却(Sourcery PR#58 复核,2026-11-15 实测):引擎在
|
|
45
|
+
// fetch 前只预检主端点(按全部声明键预检会误伤「本次不调用」的端点,见 `acquireGated`)。
|
|
46
|
+
// 这里 15 处任务调用全经本函数 —— 是「会不会调用」的唯一准确答案。
|
|
47
|
+
// ⚠️ 只在**首次尝试**查(`attempt === 0`):重试路径上的等待已经为这次的 retryAfter
|
|
48
|
+
// 付过时间,再查会撞上「我们自己的 penalize 刚写下的冷却行」把自己锁死(实测:
|
|
49
|
+
// 14009 → penalize 写冷却 → 紧随的重试被自己的冷却拦住 → 整窗 partial)。
|
|
50
|
+
if (ctx.gate !== undefined && attempt === 0) {
|
|
51
|
+
for (let waits = 0;; waits++) {
|
|
52
|
+
let cooled = null;
|
|
53
|
+
try {
|
|
54
|
+
await ctx.gate.acquire(endpointId);
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
cooled = e;
|
|
58
|
+
if (cooled.kind !== 'rate_limit_local')
|
|
59
|
+
throw e;
|
|
60
|
+
}
|
|
61
|
+
if (cooled === null)
|
|
62
|
+
break;
|
|
63
|
+
// 有界等待(复用引擎的预算口径):到点重查,预算耗尽才上抛 —— 上抛后由引擎记
|
|
64
|
+
// `rate_limit_local` gap(不是 fetch_failed),本窗不落任何数据。
|
|
65
|
+
if (waits >= MAX_PACE_ATTEMPTS)
|
|
66
|
+
throw cooled;
|
|
67
|
+
const seconds = Math.ceil((cooled.retryAfter ?? 0) / 1000);
|
|
68
|
+
await sleep(clampGateWait((seconds + 1) * 1000) || GATE_WAIT_MIN_MS);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
44
71
|
try {
|
|
45
72
|
return await call(byId(endpointId), body, { credentials: ctx.credentials });
|
|
46
73
|
}
|
|
@@ -15,17 +15,19 @@
|
|
|
15
15
|
// 同参数重复创建返回不同 key 相同数据)——本函数只返回行,去重由 upsert(session_id) 幂等吸收。
|
|
16
16
|
import { callPaced } from './pace.js';
|
|
17
17
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
18
|
-
/** 形状
|
|
18
|
+
/** 形状 M 的载荷(create 的任务 key / check 的下载链接都走 message 原文)。 */
|
|
19
19
|
function messageOf(res, endpointId) {
|
|
20
|
-
// 形状
|
|
20
|
+
// 形状 M(envelope:'M',2026-11-04 修正):信封层直接把 message 原文放进 payload。
|
|
21
21
|
// 实测 create/check 的载荷就是 message 字符串本身(key/链接),不信任何 data 形态。
|
|
22
|
+
// ⚠️ 'OK' 是占位符(形态漂移的典型形态)——两个来源都要挡掉,否则会拿 'OK'
|
|
23
|
+
// 当下载链接去下载。
|
|
22
24
|
const p = res.payload;
|
|
23
|
-
if (typeof p === 'string' && p !== '')
|
|
25
|
+
if (typeof p === 'string' && p !== '' && p !== 'OK')
|
|
24
26
|
return p;
|
|
25
27
|
if (typeof res.messageText === 'string' && res.messageText !== '' && res.messageText !== 'OK') {
|
|
26
28
|
return res.messageText;
|
|
27
29
|
}
|
|
28
|
-
throw new Error(`接口 ${endpointId} 未返回可用的 message
|
|
30
|
+
throw new Error(`接口 ${endpointId} 未返回可用的 message 载荷(响应形态漂移?—— 空串或 'OK' 占位)`);
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
31
33
|
* 解压密码 zip(ZipCrypto,密码 = appKey 前 12 字符,设计稿 §7.5 实测)并取出 session.txt。
|
|
@@ -95,8 +97,16 @@ function findEntry(zip) {
|
|
|
95
97
|
};
|
|
96
98
|
}
|
|
97
99
|
/**
|
|
98
|
-
*
|
|
99
|
-
* → CD 起始偏移/条目数 → 逐条 CD 记录(46 字节定长 + 名 + 扩展 +
|
|
100
|
+
* 中央目录里取条目:路径 EOCD(尾部 22 字节定长 + 可选注释,签名 0x06054b50)
|
|
101
|
+
* → CD 起始偏移/条目数 → 逐条 CD 记录(46 字节定长 + 名 + 扩展 + 注释)匹配**基名**。
|
|
102
|
+
*
|
|
103
|
+
* ⚠️ 匹配的是**基名**(`name` 与末段相等),不是整段相等(2026-11-08 真实导出实测):
|
|
104
|
+
* 线上 zip 的条目名带导出 id 目录前缀(`5411798/session.txt`、`5411798/message.txt`),
|
|
105
|
+
* 精确匹配 `session.txt` 会**永远找不到** —— 每个窗口都抛「没有 session.txt 条目」、
|
|
106
|
+
* 整窗 fetch_failed。前缀是不可预知的导出 id,只能按基名匹配。
|
|
107
|
+
* 基名匹配必须**恰好一条**:同基名多条目(`a/session.txt` + `b/session.txt`)→ 显式抛错,
|
|
108
|
+
* 不猜(形态漂移,宁可失败也不在两条里任选一条把别人的数据落库)。
|
|
109
|
+
*
|
|
100
110
|
* 返回 CD 里的 crc / csize / 本地头偏移(偏移 42)。找不到/结构不符 → 显式抛错:
|
|
101
111
|
* 宁可失败,也绝不在「无校验字节可对」的状态下把错密码的乱码当数据吐出去。
|
|
102
112
|
* 修复批 4 #5:本函数从「bit3 的兜底补查」升格为**唯一定位来源**(原逐条目本地头
|
|
@@ -119,6 +129,7 @@ function centralDirectoryEntry(zip, name) {
|
|
|
119
129
|
const count = u16(eocd + 10);
|
|
120
130
|
let o = u32(eocd + 16);
|
|
121
131
|
const decoder = new TextDecoder();
|
|
132
|
+
let found;
|
|
122
133
|
for (let i = 0; i < count; i++) {
|
|
123
134
|
if (o + 46 > zip.length || u32(o) !== 0x02014b50) {
|
|
124
135
|
throw new Error('zip central directory 结构不符——无法校验解压密码,拒绝落库');
|
|
@@ -127,12 +138,21 @@ function centralDirectoryEntry(zip, name) {
|
|
|
127
138
|
const elen = u16(o + 30);
|
|
128
139
|
const clen = u16(o + 32);
|
|
129
140
|
const entryName = decoder.decode(zip.subarray(o + 46, o + 46 + nlen));
|
|
130
|
-
|
|
131
|
-
|
|
141
|
+
// 基名匹配:`dir/session.txt` 与 `session.txt` 都算(前缀是导出 id,不可预知)。
|
|
142
|
+
const base = entryName.split('/').pop() ?? entryName;
|
|
143
|
+
if (base === name) {
|
|
144
|
+
if (found !== undefined) {
|
|
145
|
+
throw new Error(`zip 内有多个 ${name} 条目(${found.entryName}、${entryName})`
|
|
146
|
+
+ '——形态漂移,无法确定用哪一个,拒绝落库');
|
|
147
|
+
}
|
|
148
|
+
found = { crc: u32(o + 16), csize: u32(o + 20), localOffset: u32(o + 42), entryName };
|
|
132
149
|
}
|
|
133
150
|
o = o + 46 + nlen + elen + clen;
|
|
134
151
|
}
|
|
135
|
-
|
|
152
|
+
if (found === undefined) {
|
|
153
|
+
throw new Error(`zip central directory 内没有 ${name} 条目——无法校验解压密码,拒绝落库`);
|
|
154
|
+
}
|
|
155
|
+
return { crc: found.crc, csize: found.csize, localOffset: found.localOffset };
|
|
136
156
|
}
|
|
137
157
|
/**
|
|
138
158
|
* ZipCrypto 解密:密钥由密码初始化,逐字节流式更新;校验字节匹配则剥掉 12 字节头。
|