nx-kv 0.41.0 → 0.41.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/CHANGELOG.md +29 -6
- package/README.md +1 -0
- package/package.json +2 -2
- package/src/core/attachmentdb/docs.js +104 -0
- package/src/core/attachmentdb/ids.js +96 -0
- package/src/core/attachmentdb/query.js +189 -0
- package/src/core/attachmentdb/relations.js +153 -0
- package/src/core/attachmentdb/resources.js +225 -0
- package/src/core/attachmentdb.js +42 -715
- package/src/core/db/import-json/data.js +140 -0
- package/src/core/db/import-json/extras.js +69 -0
- package/src/core/db/import-json/links.js +147 -0
- package/src/core/db/import-json/refs.js +183 -0
- package/src/core/db/import-json/repos.js +180 -0
- package/src/core/db/import-json/scan.js +35 -0
- package/src/core/db/import-json/tasks.js +317 -0
- package/src/core/db/import-json.js +31 -1061
- package/src/modules/docs/attach.js +181 -0
- package/src/modules/docs/index.js +20 -448
- package/src/modules/docs/knowledge.js +7 -2
- package/src/modules/docs/render.js +84 -0
- package/src/modules/docs/service.js +200 -0
- package/src/modules/localtask/action-attach.js +88 -0
- package/src/modules/localtask/action-flags.js +6 -0
- package/src/modules/localtask/action-lifecycle.js +190 -0
- package/src/modules/localtask/action-maintenance.js +130 -0
- package/src/modules/localtask/action-move.js +58 -0
- package/src/modules/localtask/action-query.js +46 -0
- package/src/modules/localtask/action-remove.js +116 -0
- package/src/modules/localtask/action-render.js +200 -0
- package/src/modules/localtask/action-submit.js +186 -0
- package/src/modules/localtask/action-topics.js +46 -0
- package/src/modules/localtask/action-waiting.js +95 -0
- package/src/modules/localtask/index.js +29 -1104
- package/src/modules/repos/boards/core.js +255 -0
- package/src/modules/repos/boards/docs.js +53 -0
- package/src/modules/repos/boards/members.js +228 -0
- package/src/modules/repos/boards/transfer.js +159 -0
- package/src/modules/repos/boards.js +25 -661
- package/src/modules/repos/cli/board-actions.js +177 -0
- package/src/modules/repos/cli/ref-actions.js +157 -0
- package/src/modules/repos/cli/remote-actions.js +141 -0
- package/src/modules/repos/cli/render.js +168 -0
- package/src/modules/repos/cli/repo-actions.js +199 -0
- package/src/modules/repos/cli/transfer-actions.js +105 -0
- package/src/modules/repos/index.js +18 -920
- package/src/modules/repos/service/bind-topics.js +97 -0
- package/src/modules/repos/service/context.js +189 -0
- package/src/modules/repos/service/crud.js +345 -0
- package/src/modules/repos/service/identity.js +188 -0
- package/src/modules/repos/service/store.js +274 -0
- package/src/modules/repos/service.js +41 -1061
- package/src/modules/repos/transfer/diff.js +126 -0
- package/src/modules/repos/transfer/export.js +223 -0
- package/src/modules/repos/transfer/import.js +377 -0
- package/src/modules/repos/transfer/layout.js +135 -0
- package/src/modules/repos/transfer/render.js +72 -0
- package/src/modules/repos/transfer.js +11 -922
- package/src/modules/tasktool/attach-resource.js +293 -0
- package/src/modules/tasktool/attach-template.js +266 -0
- package/src/modules/tasktool/attach.js +70 -558
- package/src/modules/tasktool/delivery-template.js +2 -1
- package/src/modules/tasktool/doc-inline.js +84 -0
- package/src/modules/tasktool/groupctx.js +3 -1
- package/src/modules/tasktool/plugins/timegoal-core-store.js +151 -0
- package/src/modules/tasktool/plugins/timegoal-cwd-store.js +441 -0
- package/src/modules/tasktool/plugins/timegoal-store.js +16 -842
- package/src/modules/tasktool/plugins/timegoal-task-store.js +289 -0
- package/src/modules/tasktool/wait.js +3 -1
- package/src/modules/todo/attachments.js +149 -0
- package/src/modules/todo/locate.js +82 -0
- package/src/modules/todo/service.js +42 -382
- package/src/modules/todo/store.js +158 -0
- package/src/shared/prompt-blocks.js +13 -4
- package/src/web/frontend/components/md/find-bar.jsx +38 -0
- package/src/web/frontend/components/md/live-block.jsx +129 -0
- package/src/web/frontend/components/md/md-panes-helpers.js +92 -0
- package/src/web/frontend/components/md/md-panes-keys.js +193 -0
- package/src/web/frontend/components/md/md-panes-root.jsx +158 -0
- package/src/web/frontend/components/md/md-panes.jsx +124 -1342
- package/src/web/frontend/components/md/slash-menu.jsx +83 -0
- package/src/web/frontend/components/md/status-bar.jsx +46 -0
- package/src/web/frontend/components/md/use-live-preview.js +279 -0
- package/src/web/frontend/components/md/use-slash-menu.js +80 -0
- package/src/web/frontend/components/md/use-source-editor.js +297 -0
- package/src/web/frontend/modules/md/md-blocks.js +248 -0
- package/src/web/frontend/modules/md/md-parse.js +23 -507
- package/src/web/frontend/modules/md/md-render.js +258 -0
- package/src/web/frontend/modules/md/style.css +7 -1605
- package/src/web/frontend/modules/md/styles/chrome.css +252 -0
- package/src/web/frontend/modules/md/styles/docs.css +185 -0
- package/src/web/frontend/modules/md/styles/editor.css +308 -0
- package/src/web/frontend/modules/md/styles/panels.css +372 -0
- package/src/web/frontend/modules/md/styles/shell.css +363 -0
- package/src/web/frontend/modules/md/styles/templates.css +125 -0
- package/src/web/frontend/page/dag/edges.js +55 -0
- package/src/web/frontend/page/dag/head-bar.jsx +46 -0
- package/src/web/frontend/page/dag/index.jsx +57 -420
- package/src/web/frontend/page/dag/layout.js +83 -0
- package/src/web/frontend/page/dag/model.js +47 -0
- package/src/web/frontend/page/dag/node-card.jsx +123 -0
- package/src/web/frontend/page/dag/use-interactions.js +135 -0
- package/src/web/frontend/page/dir/task-ack.js +38 -0
- package/src/web/frontend/page/dir/task-actions.js +379 -0
- package/src/web/frontend/page/dir/task-beside-header.jsx +18 -0
- package/src/web/frontend/page/dir/task-board-section.jsx +38 -0
- package/src/web/frontend/page/dir/task-chain.js +172 -0
- package/src/web/frontend/page/dir/task-derived-ctx.js +46 -0
- package/src/web/frontend/page/dir/task-dock-panel.jsx +227 -0
- package/src/web/frontend/page/dir/task-dock.js +79 -0
- package/src/web/frontend/page/dir/task-feedback.js +207 -0
- package/src/web/frontend/page/dir/task-goal-section.jsx +50 -0
- package/src/web/frontend/page/dir/task-goal.js +36 -0
- package/src/web/frontend/page/dir/task-history-view.jsx +44 -0
- package/src/web/frontend/page/dir/task-not-found.jsx +23 -0
- package/src/web/frontend/page/dir/task-preview-modal.jsx +82 -0
- package/src/web/frontend/page/dir/task-sel-menu.js +54 -0
- package/src/web/frontend/page/dir/task-status-bar.jsx +61 -0
- package/src/web/frontend/page/dir/task-utils.js +7 -0
- package/src/web/frontend/page/dir/task.jsx +117 -1532
- package/src/web/frontend/page/repos/bind-modals.jsx +118 -0
- package/src/web/frontend/page/repos/board-admin.jsx +226 -0
- package/src/web/frontend/page/repos/board-refs.jsx +97 -0
- package/src/web/frontend/page/repos/doc-search-modal.jsx +241 -0
- package/src/web/frontend/page/repos/filters-bar.jsx +44 -0
- package/src/web/frontend/page/repos/index.jsx +248 -1834
- package/src/web/frontend/page/repos/pending-section.jsx +163 -0
- package/src/web/frontend/page/repos/ref-panel.jsx +406 -0
- package/src/web/frontend/page/repos/remote-modal.jsx +112 -0
- package/src/web/frontend/page/repos/repo-form-modal.jsx +219 -0
- package/src/web/frontend/page/repos/repo-table.jsx +216 -0
- package/src/web/frontend/page/settings/prompt-template-params.js +214 -0
- package/src/web/frontend/page/settings/prompt-templates-section.jsx +1 -213
- package/src/web/frontend/style-v2.css +6 -1232
- package/src/web/frontend/style.css +10 -2434
- package/src/web/frontend/styles/base.css +139 -0
- package/src/web/frontend/styles/board.css +322 -0
- package/src/web/frontend/styles/buttons-cards.css +244 -0
- package/src/web/frontend/styles/modals.css +458 -0
- package/src/web/frontend/styles/panels.css +366 -0
- package/src/web/frontend/styles/repos-admin.css +207 -0
- package/src/web/frontend/styles/repos.css +330 -0
- package/src/web/frontend/styles/settings.css +244 -0
- package/src/web/frontend/styles/submit-bar.css +124 -0
- package/src/web/frontend/styles-v2/dag.css +285 -0
- package/src/web/frontend/styles-v2/dir.css +210 -0
- package/src/web/frontend/styles-v2/shell.css +251 -0
- package/src/web/frontend/styles-v2/sidebar.css +199 -0
- package/src/web/frontend/styles-v2/views.css +287 -0
- package/src/web/frontend/workspace/layout.js +399 -0
- package/src/web/frontend/workspace/open.js +246 -0
- package/src/web/frontend/workspace/panels.js +417 -0
- package/src/web/frontend/workspace.js +8 -1044
- package/src/web/public/assets/Introduction-Dnh0L_ss.js +1 -0
- package/src/web/public/assets/app-context-D7HAx67n.js +1 -0
- package/src/web/public/assets/bar-CgCAhY5x.js +1 -0
- package/src/web/public/assets/{board-view-t8CKtq17.js → board-view-CZKZsyk-.js} +1 -1
- package/src/web/public/assets/{button-M9TUn-AR.js → button-Cmo-6aoW.js} +1 -1
- package/src/web/public/assets/cloud-DMeqfWK9.js +3 -0
- package/src/web/public/assets/composer-launch-Bf4rXhLs.js +1 -0
- package/src/web/public/assets/composer-panel-C0qsxE7e.js +1 -0
- package/src/web/public/assets/dag-BBtSqd-X.js +1 -0
- package/src/web/public/assets/dir-UUWImKeX.js +5 -0
- package/src/web/public/assets/frontmatter-QvrjUNzF.js +2 -0
- package/src/web/public/assets/group-DFMGHQ-H.js +1 -0
- package/src/web/public/assets/index-CgEWvYQk.js +2 -0
- package/src/web/public/assets/md-history-CKumOs4q.js +39 -0
- package/src/web/public/assets/md-parse-awNLHAzO.js +13 -0
- package/src/web/public/assets/{member-picker--32VkTmv.js → member-picker-BAgkj36N.js} +1 -1
- package/src/web/public/assets/model-DzdWfp14.js +2 -0
- package/src/web/public/assets/{overview-B788GcJE.js → overview-BrcPfsJO.js} +1 -1
- package/src/web/public/assets/progress-DHDlz9O9.js +3 -0
- package/src/web/public/assets/refs-CqvRGv8w.js +1 -0
- package/src/web/public/assets/repos-DGrGWDnX.js +14 -0
- package/src/web/public/assets/route-crumb-yyPGKKKq.js +1 -0
- package/src/web/public/assets/settings-9zNucAPD.js +6 -0
- package/src/web/public/assets/{status-tag-BPbfa4zt.js → status-tag-bDdrFgpZ.js} +1 -1
- package/src/web/public/assets/task-CG4fzV6s.js +11 -0
- package/src/web/public/assets/{task-status-BP6y8MVu.js → task-status-BnymQMTw.js} +1 -1
- package/src/web/public/assets/templates-DdHWIxUE.js +1 -0
- package/src/web/public/assets/tool-bar-D8q3kOOl.js +1 -0
- package/src/web/public/assets/upload-BIMbGcCB.js +1 -0
- package/src/web/public/assets/workspace-C_5X2gIr.js +28 -0
- package/src/web/public/index.html +8 -4
- package/src/web/public/assets/Introduction-DKqRmABL.js +0 -1
- package/src/web/public/assets/bar-B8QJ4EnF.js +0 -1
- package/src/web/public/assets/cloud-C2O_1vT5.js +0 -3
- package/src/web/public/assets/composer-launch-BgMj0T3Y.js +0 -1
- package/src/web/public/assets/composer-panel-C5XUApWJ.js +0 -1
- package/src/web/public/assets/dag-CWAWk_gt.js +0 -1
- package/src/web/public/assets/dir-Cu5B5z6J.js +0 -5
- package/src/web/public/assets/group-YA-FgKS3.js +0 -1
- package/src/web/public/assets/index-DxX-ZzRP.js +0 -30
- package/src/web/public/assets/md-history-4sXoj4RH.js +0 -28
- package/src/web/public/assets/md-parse-C2bDc3IZ.js +0 -25
- package/src/web/public/assets/progress-CfzFVSDu.js +0 -3
- package/src/web/public/assets/refs-qtxw66hp.js +0 -1
- package/src/web/public/assets/repos-CpVuGaP2.js +0 -14
- package/src/web/public/assets/route-crumb-B8oEN8BB.js +0 -1
- package/src/web/public/assets/settings-BmWNBqcv.js +0 -6
- package/src/web/public/assets/task-DsFCNaOJ.js +0 -11
- package/src/web/public/assets/templates-4Xaj2m9L.js +0 -1
- package/src/web/public/assets/tool-bar-CfBWzhpV.js +0 -1
- package/src/web/public/assets/upload-L1g-pfvZ.js +0 -1
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// repos / boards / members 阶段:legacy repos.json → repos 表 + repo_group_members。
|
|
2
|
+
//
|
|
3
|
+
// 审计裁定:不做远程融合、不做"按路径猜 key",key 原样入驻;
|
|
4
|
+
// legacy 的 type(ref / dev)不是新 schema 的二分(repo/group),存 ext 取证。
|
|
5
|
+
import { groupKeyOf } from '../../keys.js';
|
|
6
|
+
import { anyTimeMs } from '../time.js';
|
|
7
|
+
import { emptyReport, jsonText, stringOrNull, SENTINEL_REPO_KEY, SENTINEL_REPO_NAME } from './data.js';
|
|
8
|
+
|
|
9
|
+
/** 哨兵仓库必须**永远在场**:topics.repo_key 是 NOT NULL 外键,没有它连一条队列都建不出来。
|
|
10
|
+
* 幂等(已存在跳过)。bootstrap 在建表后无条件调用 —— 与「有没有 legacy JSON」无关。 */
|
|
11
|
+
export function ensureSentinelRepo(db, atMs, report = emptyReport()) {
|
|
12
|
+
const exists = db.get('SELECT key FROM repos WHERE key = :k', { k: SENTINEL_REPO_KEY });
|
|
13
|
+
if (exists) return;
|
|
14
|
+
db.run(
|
|
15
|
+
`INSERT INTO repos (key, type, name, description, path, remote, sort_order, ext, state, version,
|
|
16
|
+
deleted_at, created_at, updated_at)
|
|
17
|
+
VALUES (:key, 'repo', :name, :desc, '', '', -1, :ext, '', 0, NULL, :at, :at)`,
|
|
18
|
+
{
|
|
19
|
+
key: SENTINEL_REPO_KEY,
|
|
20
|
+
name: SENTINEL_REPO_NAME,
|
|
21
|
+
desc: '迁移时承载"对不上真实仓库"的队列与全局池;不是用户登记的仓库(面板默认隐藏)',
|
|
22
|
+
ext: jsonText({ ext_sentinel: 1 }),
|
|
23
|
+
at: atMs,
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
report.imported.repos += 1;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function importRepos(db, reposData, atMs, report) {
|
|
30
|
+
const repos = Array.isArray(reposData && reposData.repos) ? reposData.repos : [];
|
|
31
|
+
const boards = Array.isArray(reposData && reposData.boards) ? reposData.boards : [];
|
|
32
|
+
|
|
33
|
+
ensureSentinelRepo(db, atMs, report);
|
|
34
|
+
|
|
35
|
+
const seen = new Set([SENTINEL_REPO_KEY]);
|
|
36
|
+
const legacyIdToKey = new Map(); // 旧 r_xxx → 新 key
|
|
37
|
+
const boardLegacyIdToKey = new Map(); // 旧 b_xxx → 组 key
|
|
38
|
+
|
|
39
|
+
repos.forEach((r, index) => {
|
|
40
|
+
const key = String((r && r.key) || '').trim();
|
|
41
|
+
if (!key) {
|
|
42
|
+
report.warnings.push({ what: 'repo', why: 'key 为空(repos.key 是身份,不能伪造)', legacy: r && r.id });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (seen.has(key)) {
|
|
46
|
+
report.warnings.push({ what: 'repo', why: 'key 重复', key });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
seen.add(key);
|
|
50
|
+
if (r.id) legacyIdToKey.set(String(r.id), key);
|
|
51
|
+
|
|
52
|
+
const ext = {};
|
|
53
|
+
if (r.id) ext.ext_legacy_id = String(r.id);
|
|
54
|
+
if (r.type) ext.ext_legacy_type = String(r.type);
|
|
55
|
+
if (r.notes) ext.ext_notes = String(r.notes);
|
|
56
|
+
if (r.icon) ext.ext_icon = String(r.icon);
|
|
57
|
+
if (Array.isArray(r.remotes) && r.remotes.length) ext.ext_remotes = r.remotes.map(String);
|
|
58
|
+
if (r.keyCustom) ext.ext_key_custom = 1;
|
|
59
|
+
if (r.keyUpdatedAt) ext.ext_key_updated_at = stringOrNull(r.keyUpdatedAt);
|
|
60
|
+
// `linked` 里 kind='board' 的是 repo_group_members 的反向查询(下面的入组边负责),
|
|
61
|
+
// kind='ref' 的是"参考仓库"关系 —— 新 schema 没有这张表,存 ext 取证 + 报告。
|
|
62
|
+
const refLinks = Array.isArray(r.linked) ? r.linked.filter((x) => x && x.kind === 'ref').map((x) => String(x.id)) : [];
|
|
63
|
+
if (refLinks.length) {
|
|
64
|
+
ext.ext_linked_refs = refLinks;
|
|
65
|
+
report.discarded.push({ what: 'repo.linked(kind=ref)', key, count: refLinks.length, note: '新 schema 无仓库↔仓库关系表,存 ext 取证' });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const createdAt = anyTimeMs(r.addedAt, atMs);
|
|
69
|
+
const updatedAt = anyTimeMs(r.updatedAt, createdAt);
|
|
70
|
+
db.run(
|
|
71
|
+
`INSERT INTO repos (key, type, name, description, path, remote, sort_order, ext, state, version,
|
|
72
|
+
deleted_at, created_at, updated_at)
|
|
73
|
+
VALUES (:key, 'repo', :name, :desc, :path, :remote, :sort, :ext, :state, :version,
|
|
74
|
+
:deleted, :created, :updated)`,
|
|
75
|
+
{
|
|
76
|
+
key,
|
|
77
|
+
name: String(r.name || key),
|
|
78
|
+
desc: String(r.desc || ''),
|
|
79
|
+
path: String(r.path || ''),
|
|
80
|
+
remote: String(r.remote || (Array.isArray(r.remotes) ? r.remotes[0] : '') || ''),
|
|
81
|
+
sort: index,
|
|
82
|
+
ext: jsonText(ext),
|
|
83
|
+
state: String(r.state || ''),
|
|
84
|
+
version: Number(r.version) || 0,
|
|
85
|
+
deleted: r.deletedAt ? anyTimeMs(r.deletedAt, null) : null,
|
|
86
|
+
created: createdAt,
|
|
87
|
+
updated: updatedAt,
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
report.imported.repos += 1;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// ── boards → repos(type='group') ──
|
|
94
|
+
// key = `group/<slug(初始 name)>`:**冻结**(name 之后可改,key 不变)。
|
|
95
|
+
// 冻结的理由与 repos.key 同理:key 是身份,跟着显示名漂会让外部引用全断。
|
|
96
|
+
const groupKeys = [];
|
|
97
|
+
boards.forEach((b, index) => {
|
|
98
|
+
const name = String((b && b.name) || '').trim();
|
|
99
|
+
if (!name) {
|
|
100
|
+
report.warnings.push({ what: 'board', why: 'name 为空,无法派生组 key', legacy: b && b.id });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
let key = groupKeyOf(name);
|
|
104
|
+
if (seen.has(key)) {
|
|
105
|
+
// slug 冲突(同名组 / 与仓库 key 撞车)→ 加序号后缀 + 报告,绝不覆盖
|
|
106
|
+
let n = 2;
|
|
107
|
+
while (seen.has(`${key}-${n}`)) n += 1;
|
|
108
|
+
report.warnings.push({ what: 'board', why: `组 key 冲突,改用后缀 -${n}`, from: key, to: `${key}-${n}` });
|
|
109
|
+
key = `${key}-${n}`;
|
|
110
|
+
}
|
|
111
|
+
seen.add(key);
|
|
112
|
+
groupKeys.push(key);
|
|
113
|
+
if (b.id) boardLegacyIdToKey.set(String(b.id), key);
|
|
114
|
+
|
|
115
|
+
const ext = {};
|
|
116
|
+
if (b.id) ext.ext_legacy_id = String(b.id);
|
|
117
|
+
const createdAt = anyTimeMs(b.createdAt, atMs);
|
|
118
|
+
db.run(
|
|
119
|
+
`INSERT INTO repos (key, type, name, description, path, remote, sort_order, ext, state, version,
|
|
120
|
+
deleted_at, created_at, updated_at)
|
|
121
|
+
VALUES (:key, 'group', :name, :desc, '', '', :sort, :ext, '', 0, NULL, :created, :updated)`,
|
|
122
|
+
{
|
|
123
|
+
key,
|
|
124
|
+
name,
|
|
125
|
+
desc: String(b.desc || ''),
|
|
126
|
+
sort: index,
|
|
127
|
+
ext: jsonText(ext),
|
|
128
|
+
created: createdAt,
|
|
129
|
+
updated: anyTimeMs(b.updatedAt, createdAt),
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
report.imported.repos += 1;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// ── members(组 ← 仓库)──
|
|
136
|
+
boards.forEach((b, bi) => {
|
|
137
|
+
const groupKey = groupKeys[bi];
|
|
138
|
+
if (!groupKey) return;
|
|
139
|
+
const notesIn = b.repoNotes && typeof b.repoNotes === 'object' ? b.repoNotes : {};
|
|
140
|
+
let keys = Array.isArray(b.repoKeys) ? b.repoKeys : [];
|
|
141
|
+
let notes = notesIn;
|
|
142
|
+
// 老结构(迁移前的存量数据):成员存随机 id(repoIds / repoNotes 按 id 键)。
|
|
143
|
+
// repos 已全部入库(legacyIdToKey 齐),一次性换算成 key;换算不到的丢弃 + 报告
|
|
144
|
+
//(与 JSON 时代 keys.js migrateRelations 同一口径,只是搬进了导入事务)。
|
|
145
|
+
const oldIds = Array.isArray(b.repoIds) ? b.repoIds : null;
|
|
146
|
+
if (!keys.length && oldIds && oldIds.length) {
|
|
147
|
+
keys = [];
|
|
148
|
+
for (const id of oldIds) {
|
|
149
|
+
const k = legacyIdToKey.get(String(id));
|
|
150
|
+
if (k) keys.push(k);
|
|
151
|
+
else report.warnings.push({ what: 'member', why: 'repoIds 里的 id 换算不到 key(登记缺失),已丢弃', group: groupKey, id: String(id) });
|
|
152
|
+
}
|
|
153
|
+
notes = {};
|
|
154
|
+
for (const [k, v] of Object.entries(notesIn)) notes[legacyIdToKey.get(String(k)) || k] = v;
|
|
155
|
+
report.warnings.push({ what: 'member', why: 'repoIds(旧结构)已换算为 repoKeys', group: groupKey, count: keys.length });
|
|
156
|
+
}
|
|
157
|
+
keys.forEach((repoKeyRaw, position) => {
|
|
158
|
+
const repoKey = String(repoKeyRaw || '').trim();
|
|
159
|
+
if (!repoKey) return;
|
|
160
|
+
if (!db.get('SELECT key FROM repos WHERE key = :k', { k: repoKey })) {
|
|
161
|
+
// 脏数据(组里挂了一个不存在的仓库)→ 记报告丢弃,不整体回滚
|
|
162
|
+
report.warnings.push({ what: 'member', why: '成员仓库不存在,已丢弃', group: groupKey, repo: repoKey });
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (repoKey === groupKey) {
|
|
166
|
+
report.warnings.push({ what: 'member', why: '组把自己当成员,已丢弃', group: groupKey });
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
db.run(
|
|
170
|
+
`INSERT INTO repo_group_members (group_key, repo_key, position, note, created_at)
|
|
171
|
+
VALUES (:g, :r, :pos, :note, :at)
|
|
172
|
+
ON CONFLICT(group_key, repo_key) DO NOTHING`,
|
|
173
|
+
{ g: groupKey, r: repoKey, pos: position, note: String(notes[repoKey] || ''), at: atMs }
|
|
174
|
+
);
|
|
175
|
+
report.imported.members += 1;
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return { legacyIdToKey, boardLegacyIdToKey, groupKeys, boardNames: boards.map((b) => String((b && b.name) || '')) };
|
|
180
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// legacy 文件探测:只负责「把 ~/.nx-kv 下的 legacy JSON 读进来」,不做任何写库。
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { LEGACY_FILES } from './data.js';
|
|
5
|
+
|
|
6
|
+
function readJson(path) {
|
|
7
|
+
try {
|
|
8
|
+
const raw = readFileSync(path, 'utf8');
|
|
9
|
+
return { ok: true, data: JSON.parse(raw), path };
|
|
10
|
+
} catch (err) {
|
|
11
|
+
if (err && err.code === 'ENOENT') return { ok: false, missing: true, path };
|
|
12
|
+
return { ok: false, error: String((err && err.message) || err), path };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 探测 legacy 数据。
|
|
18
|
+
*
|
|
19
|
+
* `localtasks.json` 缺失时回落到 `sessiontasks.json`(改名前的旧文件),
|
|
20
|
+
* 与运行期 `legacyStorePath()` 同口径 —— 只认「主文件不在」这一种情况。
|
|
21
|
+
*
|
|
22
|
+
* @returns {{files: object, any: boolean}}
|
|
23
|
+
*/
|
|
24
|
+
export function scanLegacy(appDir) {
|
|
25
|
+
const files = {};
|
|
26
|
+
for (const [kind, name] of Object.entries(LEGACY_FILES)) {
|
|
27
|
+
files[kind] = readJson(join(appDir, name));
|
|
28
|
+
}
|
|
29
|
+
if (!files.localtasks.ok && files.localtasks.missing && files.sessiontasks.ok) {
|
|
30
|
+
files.localtasks = files.sessiontasks;
|
|
31
|
+
files.localtasksFromLegacyName = true;
|
|
32
|
+
}
|
|
33
|
+
const any = ['localtasks', 'repos', 'timegoals', 'templates', 'config'].some((k) => files[k] && files[k].ok);
|
|
34
|
+
return { files, any };
|
|
35
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// topics / tasks 阶段:legacy localtasks.json → topics + topic_presence + tasks(含内嵌 timegoal → goals)。
|
|
2
|
+
import { topicKeyOf, GLOBAL_TOPIC_KEY } from '../../topic-key.js';
|
|
3
|
+
import { anyTimeMs } from '../time.js';
|
|
4
|
+
import {
|
|
5
|
+
jsonText,
|
|
6
|
+
legacyDurationSec,
|
|
7
|
+
mapLegacyStatus,
|
|
8
|
+
numberOrNull,
|
|
9
|
+
stringOrNull,
|
|
10
|
+
MAX_BODY,
|
|
11
|
+
SENTINEL_REPO_KEY,
|
|
12
|
+
} from './data.js';
|
|
13
|
+
|
|
14
|
+
export function importTopicsAndTasks(db, localtasksData, atMs, report) {
|
|
15
|
+
const topics = localtasksData && localtasksData.topics && typeof localtasksData.topics === 'object'
|
|
16
|
+
? localtasksData.topics
|
|
17
|
+
: {};
|
|
18
|
+
|
|
19
|
+
/** legacy topic key → { id, legacyKey } */
|
|
20
|
+
const byLegacyKey = new Map();
|
|
21
|
+
/** 新 key → topic 行(碰撞时合并到同一行) */
|
|
22
|
+
const byNewKey = new Map();
|
|
23
|
+
|
|
24
|
+
for (const [legacyKey, t] of Object.entries(topics)) {
|
|
25
|
+
const topic = t && typeof t === 'object' ? t : {};
|
|
26
|
+
// cwd:优先 .cwd,其次 cwds[0](schema 一条队列一个 cwd,其余丢弃 + 报告)
|
|
27
|
+
let cwd = String(topic.cwd || '').trim();
|
|
28
|
+
if (!cwd && Array.isArray(topic.cwds) && topic.cwds.length) cwd = String(topic.cwds[0] || '').trim();
|
|
29
|
+
if (Array.isArray(topic.cwds) && topic.cwds.length > 1) {
|
|
30
|
+
report.discarded.push({
|
|
31
|
+
what: 'topic.cwds[1..]',
|
|
32
|
+
key: legacyKey,
|
|
33
|
+
count: topic.cwds.length - 1,
|
|
34
|
+
note: 'schema 一条队列一个 cwd',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const hasTasks = Array.isArray(topic.tasks) && topic.tasks.length > 0;
|
|
38
|
+
if (!cwd && legacyKey !== GLOBAL_TOPIC_KEY && hasTasks) {
|
|
39
|
+
// 异常行(v1/v2 迁移来的、没 cwd 的旧等待点):挂哨兵 + cwd='' + 报告
|
|
40
|
+
report.warnings.push({
|
|
41
|
+
what: 'topic',
|
|
42
|
+
why: 'cwd 为空且不是全局池(旧 slot/session 迁移残留),cwd 落空串',
|
|
43
|
+
legacyKey,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const key = topicKeyOf(cwd, '');
|
|
47
|
+
let row = byNewKey.get(key);
|
|
48
|
+
if (row) {
|
|
49
|
+
// 两条 legacy 队列算出同一个 key = 同一个 (cwd, group)。按新口径它们本就是
|
|
50
|
+
// 一条队列 → 合并(旧口径下同名目录共用一条队列正是被修掉的 bug)。
|
|
51
|
+
report.warnings.push({ what: 'topic', why: '与另一条 legacy 队列同 (cwd, group),已合并', legacyKey, key });
|
|
52
|
+
} else {
|
|
53
|
+
const ext = { ext_legacy_key: legacyKey };
|
|
54
|
+
if (topic.summary) ext.ext_last_summary = String(topic.summary);
|
|
55
|
+
if (topic.manual) ext.ext_manual = 1;
|
|
56
|
+
if (legacyKey === GLOBAL_TOPIC_KEY) ext.ext_global = 1;
|
|
57
|
+
|
|
58
|
+
const createdAt = anyTimeMs(topic.createdAt, atMs);
|
|
59
|
+
const res = db.run(
|
|
60
|
+
`INSERT INTO topics (key, repo_key, cwd, group_key, name, next_seq, ext, state, version,
|
|
61
|
+
deleted_at, created_at, updated_at)
|
|
62
|
+
VALUES (:key, :repo, :cwd, NULL, :name, 1, :ext, :state, :version, NULL, :created, :updated)`,
|
|
63
|
+
{
|
|
64
|
+
key,
|
|
65
|
+
repo: SENTINEL_REPO_KEY,
|
|
66
|
+
cwd,
|
|
67
|
+
name: String(topic.name || ''),
|
|
68
|
+
ext: jsonText(ext),
|
|
69
|
+
state: String(topic.state || ''),
|
|
70
|
+
version: Number(topic.workflowRevision) || 0,
|
|
71
|
+
created: createdAt,
|
|
72
|
+
updated: anyTimeMs(topic.updatedAt, createdAt),
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
row = { id: res.lastInsertRowid, taken: new Set(), maxSeq: 0 };
|
|
76
|
+
byNewKey.set(key, row);
|
|
77
|
+
report.imported.topics += 1;
|
|
78
|
+
}
|
|
79
|
+
byLegacyKey.set(legacyKey, { id: row.id, legacyKey });
|
|
80
|
+
|
|
81
|
+
// ── 等待态 → topic_presence(瞬态数据独立成表,见 schema 的 P0-4 说明)──
|
|
82
|
+
const waiting = topic.waiting === true || topic.waiting === 1;
|
|
83
|
+
const untilMs = anyTimeMs(
|
|
84
|
+
topic.waitingUntilMs !== undefined ? topic.waitingUntilMs : topic.waitingUntil,
|
|
85
|
+
NaN
|
|
86
|
+
);
|
|
87
|
+
const cancelRequested = topic.cancelRequested === true || topic.cancelRequested === 1;
|
|
88
|
+
if (waiting || cancelRequested || !Number.isNaN(untilMs)) {
|
|
89
|
+
db.run(
|
|
90
|
+
`INSERT INTO topic_presence (topic_id, waiting, since_ms, until_ms, cancel_requested, updated_at)
|
|
91
|
+
VALUES (:id, :waiting, :since, :until, :cancel, :at)
|
|
92
|
+
ON CONFLICT(topic_id) DO UPDATE SET waiting = excluded.waiting, since_ms = excluded.since_ms,
|
|
93
|
+
until_ms = excluded.until_ms, cancel_requested = excluded.cancel_requested,
|
|
94
|
+
updated_at = excluded.updated_at`,
|
|
95
|
+
{
|
|
96
|
+
id: row.id,
|
|
97
|
+
waiting: waiting ? 1 : 0,
|
|
98
|
+
since: numberOrNull(anyTimeMs(topic.waitingSince, NaN)),
|
|
99
|
+
until: numberOrNull(untilMs),
|
|
100
|
+
cancel: cancelRequested ? 1 : 0,
|
|
101
|
+
at: anyTimeMs(topic.updatedAt, atMs),
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
report.imported.presence += 1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ── tasks ──
|
|
108
|
+
for (const task of Array.isArray(topic.tasks) ? topic.tasks : []) {
|
|
109
|
+
insertTask(db, { row, legacyKey, task, atMs, report });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 其余 legacy 字段:审计已裁定丢弃,如实记账
|
|
113
|
+
if (Array.isArray(topic.subs) && topic.subs.length) {
|
|
114
|
+
report.discarded.push({ what: 'topic.subs', key: legacyKey, count: topic.subs.length, note: '代码 0 引用' });
|
|
115
|
+
}
|
|
116
|
+
if (topic.submitSummary) {
|
|
117
|
+
report.discarded.push({ what: 'topic.submitSummary', key: legacyKey, count: 1, note: '队列是开发侧概念,不进库' });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// next_seq = max(seq)+1(只增不减;编号复用会让历史引用指向另一条任务)
|
|
122
|
+
for (const [, row] of byNewKey) {
|
|
123
|
+
db.run('UPDATE topics SET next_seq = :n WHERE id = :id', { n: (row.maxSeq || 0) + 1, id: row.id });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return { byLegacyKey, byNewKey };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function insertTask(db, { row, legacyKey, task, atMs, report }) {
|
|
130
|
+
const mapped = mapLegacyStatus(task);
|
|
131
|
+
report.distribution[mapped.status] = (report.distribution[mapped.status] || 0) + 1;
|
|
132
|
+
if (mapped.unknown) {
|
|
133
|
+
report.warnings.push({
|
|
134
|
+
what: 'task.status',
|
|
135
|
+
why: `未知状态 ${JSON.stringify(task && task.status)} → 当 pending`,
|
|
136
|
+
key: legacyKey,
|
|
137
|
+
id: task && task.id,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (mapped.archived) {
|
|
141
|
+
report.warnings.push({ what: 'task.status', why: 'archived(本机实测 0 条,按判据第 4 步处理)', key: legacyKey, id: task && task.id });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const seq = Math.trunc(Number(task && task.id) || 0);
|
|
145
|
+
if (!seq) {
|
|
146
|
+
report.warnings.push({ what: 'task', why: 'id 缺失/非法,无法保持编号', key: legacyKey, id: task && task.id });
|
|
147
|
+
// 编号是身份的一部分:丢一条 / 改一个号都会让「id N 当初指的是哪条」失效。
|
|
148
|
+
// 数据损坏必须整体拒绝(事务回滚、JSON 不动),不能悄悄补一条。
|
|
149
|
+
throw new Error(`legacy 数据损坏:队列 ${legacyKey} 里有任务缺 id(${JSON.stringify(task && task.id)})`);
|
|
150
|
+
}
|
|
151
|
+
if (row.taken.has(seq)) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`legacy 数据损坏:队列 ${legacyKey} 里 id ${seq} 重复(tasks.seq 必须与源 id 一一对应)`
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
row.taken.add(seq);
|
|
157
|
+
if (seq > row.maxSeq) row.maxSeq = seq;
|
|
158
|
+
|
|
159
|
+
const rawBody = String((task && task.text) ?? '');
|
|
160
|
+
let body = rawBody;
|
|
161
|
+
const ext = {};
|
|
162
|
+
if (body.length > MAX_BODY) {
|
|
163
|
+
body = body.slice(0, MAX_BODY);
|
|
164
|
+
ext.ext_body_truncated = 1;
|
|
165
|
+
report.warnings.push({ what: 'task.body', why: `正文超长已截断(${rawBody.length} > ${MAX_BODY})`, key: legacyKey, id: seq });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 审计裁定不建列 → 存 ext 或直接丢弃(都要记账,避免"静默没了")
|
|
169
|
+
if (task.pickupSec !== undefined) ext.ext_pickup_sec = Number(task.pickupSec) || 0;
|
|
170
|
+
if (task.trashIndex !== undefined) ext.ext_trash_index = Number(task.trashIndex) || 0;
|
|
171
|
+
if (task.ready !== undefined) ext.ext_ready = task.ready ? 1 : 0;
|
|
172
|
+
if (mapped.archived) ext.ext_archived = 1;
|
|
173
|
+
if (task.archivedAt) ext.ext_archived_at = stringOrNull(task.archivedAt);
|
|
174
|
+
if (task.archivedReason) ext.ext_archived_reason = String(task.archivedReason);
|
|
175
|
+
if (task.supersededBy !== undefined && task.supersededBy !== '') ext.ext_superseded_by = task.supersededBy;
|
|
176
|
+
// history[] 整体丢弃(旧"同一条任务内的编辑记录"这个概念随派生模型一起消失);
|
|
177
|
+
// 非空时存 ext 取证,本机实测 1 条且为 []。
|
|
178
|
+
if (Array.isArray(task.history) && task.history.length) {
|
|
179
|
+
ext.ext_legacy_history = task.history;
|
|
180
|
+
report.discarded.push({ what: 'task.history', key: legacyKey, id: seq, count: task.history.length });
|
|
181
|
+
}
|
|
182
|
+
if (task.remote !== undefined) report.discarded.push({ what: 'task.remote', key: legacyKey, id: seq, count: 1 });
|
|
183
|
+
if (task.claimedBy !== undefined || task.claimedFrom !== undefined || task.claimedByName !== undefined) {
|
|
184
|
+
report.discarded.push({ what: 'task.claimedBy/From/ByName', key: legacyKey, id: seq, count: 1 });
|
|
185
|
+
}
|
|
186
|
+
if (task.migratedTo !== undefined && task.migratedTo !== null && task.migratedTo !== '') {
|
|
187
|
+
// 迁出墓碑:丢了它,云端那条已搬走的任务会被当成「待领取」复活。
|
|
188
|
+
// 旧形状可能是字符串("elsewhere"),新视图只认对象 → 统一包成对象。
|
|
189
|
+
const m = task.migratedTo;
|
|
190
|
+
ext.ext_migrated_to =
|
|
191
|
+
m && typeof m === 'object'
|
|
192
|
+
? m
|
|
193
|
+
: { side: 'cloud', topic: String(m), targetId: 0, at: '' };
|
|
194
|
+
if (!m || typeof m !== 'object') {
|
|
195
|
+
report.warnings.push({ what: 'task.migratedTo', why: '旧字符串形状 → 已包装成对象保留', key: legacyKey, id: seq });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const createdAt = anyTimeMs(task.createdAt, atMs);
|
|
200
|
+
const updatedAt = anyTimeMs(task.updatedAt, createdAt);
|
|
201
|
+
let claimedAt = numberOrNull(anyTimeMs(task.consumedAt, NaN));
|
|
202
|
+
// 新词直通行可能缺 consumedAt(领取时刻没落 JSON)→ 用创建时刻兜底:
|
|
203
|
+
// 「有 claimed 词却没有领取时刻」比「补一个近似时刻」更糟(verify 的不变量会挂)。
|
|
204
|
+
if (mapped.status === 'claimed' && claimedAt === null) claimedAt = numberOrNull(createdAt);
|
|
205
|
+
|
|
206
|
+
let doneAt = null;
|
|
207
|
+
let feedback = '';
|
|
208
|
+
let feedbackAt = null;
|
|
209
|
+
if (mapped.status === 'done') {
|
|
210
|
+
// completedAt 在本机全为空(7 条是空串键)→ 回落到 updatedAt
|
|
211
|
+
doneAt = numberOrNull(anyTimeMs(task.completedAt, NaN));
|
|
212
|
+
if (doneAt === null) doneAt = updatedAt;
|
|
213
|
+
feedback = String(task.roundSummary ?? '');
|
|
214
|
+
feedbackAt = doneAt;
|
|
215
|
+
} else if (mapped.hasSummary) {
|
|
216
|
+
// 非 done 行却有非空 roundSummary:不能塞进 feedback(那是 done 的语义),
|
|
217
|
+
// 但也不能丢 → 存 ext 取证。
|
|
218
|
+
ext.ext_legacy_round_summary = String(task.roundSummary);
|
|
219
|
+
report.warnings.push({ what: 'task.roundSummary', why: `状态 ${mapped.status} 但有非空 roundSummary,已存 ext`, key: legacyKey, id: seq });
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const round = (Number(task.reopenCount) || 0) + 1;
|
|
223
|
+
if (task.reopenCount) report.discarded.push({ what: 'task.reopenCount', key: legacyKey, id: seq, count: 1, note: '→ round = reopenCount+1' });
|
|
224
|
+
|
|
225
|
+
db.run(
|
|
226
|
+
`INSERT INTO tasks (topic_id, seq, type, status, version, body, ext, feedback, feedback_at,
|
|
227
|
+
from_src, blocks_following, claimed_at, done_at, round, deleted_at,
|
|
228
|
+
created_at, updated_at)
|
|
229
|
+
VALUES (:topic, :seq, '', :status, 0, :body, :ext, :feedback, :feedbackAt,
|
|
230
|
+
:from, :blocks, :claimedAt, :doneAt, :round, :deletedAt, :createdAt, :updatedAt)`,
|
|
231
|
+
{
|
|
232
|
+
topic: row.id,
|
|
233
|
+
seq,
|
|
234
|
+
status: mapped.status,
|
|
235
|
+
body,
|
|
236
|
+
ext: jsonText(ext),
|
|
237
|
+
feedback,
|
|
238
|
+
feedbackAt,
|
|
239
|
+
from: String(task.from || ''),
|
|
240
|
+
blocks: task.blocksFollowing ? 1 : 0,
|
|
241
|
+
claimedAt,
|
|
242
|
+
doneAt,
|
|
243
|
+
round,
|
|
244
|
+
// 软删与状态正交:deletedAt 非空就写 deleted_at,状态照上面的判据走
|
|
245
|
+
deletedAt: numberOrNull(anyTimeMs(task.deletedAt, NaN)),
|
|
246
|
+
createdAt,
|
|
247
|
+
updatedAt,
|
|
248
|
+
}
|
|
249
|
+
);
|
|
250
|
+
report.imported.tasks += 1;
|
|
251
|
+
|
|
252
|
+
// 内嵌 timegoal → goals(本机 7 条键存在但值全为 null,实际 0 条)
|
|
253
|
+
if (task.timegoal && typeof task.timegoal === 'object') {
|
|
254
|
+
insertGoal(db, { topicId: row.id, seq, legacyKey, timegoal: task.timegoal, claimedAt, createdAt, report });
|
|
255
|
+
report.discarded.push({ what: 'task.timegoal', key: legacyKey, id: seq, count: 1, note: '已迁入 goals' });
|
|
256
|
+
} else if (task.timegoal === null) {
|
|
257
|
+
report.discarded.push({ what: 'task.timegoal', key: legacyKey, id: seq, count: 1, note: '值为 null,无目标可迁' });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function insertGoal(db, { topicId, seq, legacyKey, timegoal, claimedAt, createdAt, report }) {
|
|
262
|
+
const taskRow = db.get('SELECT id FROM tasks WHERE topic_id = :t AND seq = :s', { t: topicId, s: seq });
|
|
263
|
+
if (!taskRow) return;
|
|
264
|
+
let durationSec = legacyDurationSec(timegoal);
|
|
265
|
+
const startedAt = anyTimeMs(timegoal.startedAt, NaN);
|
|
266
|
+
const start = Number.isFinite(startedAt) ? startedAt : claimedAt || createdAt;
|
|
267
|
+
const deadline = anyTimeMs(timegoal.deadline, NaN);
|
|
268
|
+
// 只有「起止时刻」没有时长时,用差值补出时长(旧数据常见形状)
|
|
269
|
+
if (durationSec <= 0 && Number.isFinite(deadline) && deadline > start) {
|
|
270
|
+
durationSec = Math.trunc((deadline - start) / 1000);
|
|
271
|
+
}
|
|
272
|
+
const end = Number.isFinite(deadline) ? deadline : start + durationSec * 1000;
|
|
273
|
+
// CHECK (duration_sec > 0) / CHECK (deadline > started_at):不满足就不插,
|
|
274
|
+
// 记报告(脏数据不能让整个迁移回滚)
|
|
275
|
+
if (durationSec <= 0 || end <= start) {
|
|
276
|
+
report.warnings.push({ what: 'goal', why: '时长或截止时刻非法,已跳过', key: legacyKey, id: seq });
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
// 同一条任务最多一个未删目标:内嵌 timegoal 与 standalone timegoals.json 可能
|
|
280
|
+
// 指向同一任务,裸 INSERT 会撞 ux_goals_active → 整库回滚。冲突时保留先到的一条。
|
|
281
|
+
const res = db.run(
|
|
282
|
+
`INSERT INTO goals (task_id, duration_sec, started_at, deadline, prompt, state, version,
|
|
283
|
+
deleted_at, created_at, updated_at)
|
|
284
|
+
VALUES (:task, :dur, :start, :deadline, :prompt, '', 0, NULL, :at, :at)
|
|
285
|
+
ON CONFLICT(task_id) WHERE deleted_at IS NULL DO NOTHING`,
|
|
286
|
+
{
|
|
287
|
+
task: taskRow.id,
|
|
288
|
+
dur: durationSec,
|
|
289
|
+
start,
|
|
290
|
+
deadline: end,
|
|
291
|
+
prompt: String(timegoal.prompt || ''),
|
|
292
|
+
at: start,
|
|
293
|
+
}
|
|
294
|
+
);
|
|
295
|
+
const goalRow = db.get('SELECT id FROM goals WHERE task_id = :t AND deleted_at IS NULL', { t: taskRow.id });
|
|
296
|
+
if (!goalRow) return;
|
|
297
|
+
if (Number(res.changes) > 0) report.imported.goals += 1;
|
|
298
|
+
|
|
299
|
+
for (const log of Array.isArray(timegoal.log) ? timegoal.log : []) {
|
|
300
|
+
const at = anyTimeMs(log.at, NaN);
|
|
301
|
+
if (!Number.isFinite(at)) continue;
|
|
302
|
+
db.run(
|
|
303
|
+
`INSERT INTO goal_checks (goal_id, at, elapsed_sec, remaining_sec, overtime_sec, reached, summary)
|
|
304
|
+
VALUES (:g, :at, :elapsed, :remaining, :overtime, :reached, :summary)`,
|
|
305
|
+
{
|
|
306
|
+
g: goalRow.id,
|
|
307
|
+
at,
|
|
308
|
+
elapsed: Math.trunc(Number(log.elapsedSec) || 0),
|
|
309
|
+
remaining: Math.trunc(Number(log.remainingSec) || 0),
|
|
310
|
+
overtime: Math.trunc(Number(log.overtimeSec) || 0),
|
|
311
|
+
reached: log.reached ? 1 : 0,
|
|
312
|
+
summary: String(log.summary || ''),
|
|
313
|
+
}
|
|
314
|
+
);
|
|
315
|
+
report.imported.goalChecks += 1;
|
|
316
|
+
}
|
|
317
|
+
}
|