opencode-swarm 7.163.1 → 7.164.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/README.md +1 -1
- package/dist/cli/{coder-settlement-f1gtvcy8.js → coder-settlement-w4qk90kh.js} +12 -13
- package/dist/cli/{config-doctor-0fnqzqz1.js → config-doctor-r81bp1nd.js} +3 -3
- package/dist/cli/{core-w81fk5sc.js → core-xfw4s9b9.js} +2 -2
- package/dist/cli/{curation-policy-j12j2gvc.js → curation-policy-2h37sd6p.js} +6 -7
- package/dist/cli/{curator-as495r18.js → curator-7zy6bdmw.js} +36 -39
- package/dist/cli/{curator-drift-g6w4ga1q.js → curator-drift-ykrdf08r.js} +105 -38
- package/dist/cli/curator-llm-factory-7gjvm4ds.js +66 -0
- package/dist/cli/{evidence-summary-service-51dxfcn5.js → evidence-summary-service-519ny6b1.js} +17 -18
- package/dist/cli/{gate-evidence-nb1e8hz0.js → gate-evidence-a7st3vbc.js} +8 -9
- package/dist/cli/guardrail-explain-ewcm37em.js +67 -0
- package/dist/cli/{guardrail-log-2kwntcqw.js → guardrail-log-kp51by1v.js} +8 -9
- package/dist/cli/{guardrail-reset-mrxrc56j.js → guardrail-reset-reyfwtwn.js} +36 -39
- package/dist/cli/hive-promoter-zf2vf5sw.js +81 -0
- package/dist/cli/{index-4p0wyz5p.js → index-1af3rb5n.js} +9 -9
- package/dist/cli/{index-2zgjcmmc.js → index-1b1h770t.js} +3 -3
- package/dist/cli/{index-0tervb1m.js → index-2ee2g27m.js} +7795 -2724
- package/dist/cli/{index-x2f1f011.js → index-2s40e3b0.js} +4 -3
- package/dist/cli/{index-9xd073d4.js → index-2yd732fd.js} +7 -1
- package/dist/cli/{index-9crtmnqg.js → index-36nt17m8.js} +1 -1
- package/dist/cli/{index-d3azve4r.js → index-5084szce.js} +122 -300
- package/dist/cli/{index-d9gdwh34.js → index-5bza8nxz.js} +2 -2
- package/dist/cli/{index-xyhmt813.js → index-6097tx05.js} +1 -1
- package/dist/cli/{index-5056zmpt.js → index-644t46hr.js} +1 -1
- package/dist/cli/{index-2v06a1yh.js → index-6ay5zp1v.js} +5 -5
- package/dist/cli/{index-n2mhyect.js → index-6kd3jnaf.js} +1 -1
- package/dist/cli/{index-n67vx5zw.js → index-6t35714n.js} +2 -2
- package/dist/cli/{index-tnpf9wwv.js → index-862c0xhc.js} +1 -1
- package/dist/cli/{index-2n1ym7jb.js → index-b98r3pyv.js} +5 -5
- package/dist/cli/{index-8ada7ag7.js → index-dhzsvmx6.js} +2 -2
- package/dist/cli/{index-n985274d.js → index-e630pvvw.js} +4 -4
- package/dist/cli/{index-3b762978.js → index-f9nfknjg.js} +182 -37
- package/dist/cli/{index-m1fvbah1.js → index-fwfhbs27.js} +2 -2
- package/dist/cli/{index-t34daffb.js → index-h2f21caa.js} +2 -2
- package/dist/cli/{index-9ss2m4rs.js → index-hcn69kp0.js} +410 -1
- package/dist/cli/{index-g3znh2yd.js → index-hzaw10zh.js} +7 -7
- package/dist/cli/{index-cqgpvx2d.js → index-j0ajeary.js} +2 -2
- package/dist/cli/{index-f923njwh.js → index-jczbtx2x.js} +7 -7
- package/dist/cli/{index-hm239n1p.js → index-k8j7m0e2.js} +1 -1
- package/dist/cli/{index-zjme8atd.js → index-n1wcq816.js} +5 -5
- package/dist/cli/{index-66073kej.js → index-pmmfhzsp.js} +3 -3
- package/dist/cli/{index-fckfgptr.js → index-qbr05tbw.js} +2 -2
- package/dist/cli/{index-r56f3f81.js → index-r5jn0ah5.js} +2 -2
- package/dist/cli/{index-dtn0pne5.js → index-v56mhyhs.js} +1 -1
- package/dist/cli/{index-etvq73mp.js → index-vc3zmtxb.js} +38 -41
- package/dist/cli/{index-kvra3nv1.js → index-vdgw2k4c.js} +3 -3
- package/dist/cli/{index-0103w5pf.js → index-vkckf2ch.js} +2 -2
- package/dist/cli/{index-swgy9xvk.js → index-vrxx0ea4.js} +4 -4
- package/dist/cli/{index-08y8pccg.js → index-wnf1ej2q.js} +4 -4
- package/dist/cli/{index-qynqyfgj.js → index-wv4b01ex.js} +32 -34
- package/dist/cli/{index-7n9jznv0.js → index-x2qmpkth.js} +5 -5
- package/dist/cli/index-y2f5s2cf.js +403 -0
- package/dist/cli/{index-ma52g2pq.js → index-yc415pzf.js} +655 -16
- package/dist/cli/{index-n249fbw7.js → index-zaab1wz9.js} +5 -0
- package/dist/cli/index.js +36 -39
- package/dist/cli/{knowledge-escalator-fq7dm2g4.js → knowledge-escalator-wwkbsgvw.js} +14 -15
- package/dist/cli/{knowledge-events-3zanyfkr.js → knowledge-events-vmmgwfmc.js} +12 -13
- package/dist/cli/{knowledge-link-vmdj5e5d.js → knowledge-link-bvra01zv.js} +5 -6
- package/dist/cli/{knowledge-store-vhehnw52.js → knowledge-store-6wbagbqm.js} +6 -7
- package/dist/cli/{knowledge-validator-0tt0pe6b.js → knowledge-validator-q1z4fmeg.js} +8 -9
- package/dist/cli/{model-preflight-c3mc8njg.js → model-preflight-1ecjbg73.js} +1 -1
- package/dist/cli/{pending-delegations-b92pecbj.js → pending-delegations-cq5g4rr4.js} +6 -8
- package/dist/cli/{pr-subscriptions-2mkdt0ak.js → pr-subscriptions-m0z5wxkb.js} +6 -7
- package/dist/cli/pr-workflow-gate-0zvp0qs9.js +221 -0
- package/dist/cli/{runner-3gw2njnx.js → runner-s55g5233.js} +6 -7
- package/dist/cli/{scan-cursor-1c8a3b0m.js → scan-cursor-z6zepy3r.js} +7 -8
- package/dist/cli/{schema-c554j5c8.js → schema-skf0ckh4.js} +2 -2
- package/dist/cli/{scope-persistence-vtbcszvv.js → scope-persistence-f6davq7x.js} +10 -11
- package/dist/cli/{skill-generator-0es21fck.js → skill-generator-wqbazppk.js} +16 -17
- package/dist/cli/{sqlite-loader-c92413n7.js → sqlite-loader-8zefc142.js} +1 -1
- package/dist/cli/{telemetry-x4zggfd7.js → telemetry-q51s6tyt.js} +1 -1
- package/dist/cli/{worktree-collision-ownership-043dg8gg.js → worktree-collision-ownership-c0030qbm.js} +7 -9
- package/dist/cli/{worktree-isolation-hk42yxw7.js → worktree-isolation-09qq8066.js} +36 -39
- package/dist/db/canonical-project.d.ts +31 -0
- package/dist/db/db-errors.d.ts +24 -0
- package/dist/db/driver-parity.d.ts +31 -0
- package/dist/db/durability.d.ts +40 -0
- package/dist/db/group-commit-writer.d.ts +117 -0
- package/dist/db/health.d.ts +35 -0
- package/dist/db/insight-candidate-store.d.ts +61 -0
- package/dist/db/legacy-import.d.ts +77 -0
- package/dist/db/phase-report-store.d.ts +45 -0
- package/dist/db/project-db.d.ts +35 -8
- package/dist/db/qa-gate-profile.d.ts +2 -0
- package/dist/hooks/curator-drift.d.ts +9 -5
- package/dist/hooks/curator-types.d.ts +1 -0
- package/dist/hooks/delegation-gate.d.ts +1 -0
- package/dist/hooks/knowledge-curator.d.ts +5 -3
- package/dist/hooks/micro-reflector.d.ts +12 -7
- package/dist/hooks/pr-workflow-gate.d.ts +14 -8
- package/dist/hooks/pr-workflow-response-gate.d.ts +9 -12
- package/dist/index.js +627 -574
- package/dist/memory/schema.d.ts +4 -4
- package/dist/pr-review/authorization.d.ts +18 -12
- package/dist/prm/index.d.ts +3 -1
- package/dist/tools/authorize-pr-review-reentry.d.ts +12 -8
- package/package.json +3 -2
- package/dist/cli/authorization-vstrh7ra.js +0 -23
- package/dist/cli/curator-llm-factory-v8vwz20c.js +0 -69
- package/dist/cli/guardrail-explain-z2qb6v67.js +0 -70
- package/dist/cli/hive-promoter-c62x2ncb.js +0 -84
- package/dist/cli/index-c8s9a3zh.js +0 -73
- package/dist/cli/index-ed5ykebp.js +0 -151
- package/dist/cli/index-mmngnxev.js +0 -883
- package/dist/cli/index-w7ekntej.js +0 -643
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
DbWriteError,
|
|
4
|
+
applySynchronousForClass,
|
|
5
|
+
batchDurabilityClass,
|
|
6
|
+
canonicalProjectKey,
|
|
7
|
+
classifyDbWriteError,
|
|
8
|
+
getProjectDb
|
|
9
|
+
} from "./index-hcn69kp0.js";
|
|
10
|
+
import {
|
|
11
|
+
init_logger,
|
|
12
|
+
log,
|
|
13
|
+
warn
|
|
14
|
+
} from "./index-zgwm4ryv.js";
|
|
15
|
+
|
|
16
|
+
// src/background/event-bus.ts
|
|
17
|
+
class AutomationEventBus {
|
|
18
|
+
listeners = new Map;
|
|
19
|
+
eventHistory = [];
|
|
20
|
+
maxHistorySize;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.maxHistorySize = options?.maxHistorySize ?? 100;
|
|
23
|
+
}
|
|
24
|
+
subscribe(type, listener) {
|
|
25
|
+
if (!this.listeners.has(type)) {
|
|
26
|
+
this.listeners.set(type, new Set);
|
|
27
|
+
}
|
|
28
|
+
this.listeners.get(type).add(listener);
|
|
29
|
+
return () => {
|
|
30
|
+
this.listeners.get(type)?.delete(listener);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
async publish(type, payload, source) {
|
|
34
|
+
const event = {
|
|
35
|
+
type,
|
|
36
|
+
timestamp: Date.now(),
|
|
37
|
+
payload,
|
|
38
|
+
source
|
|
39
|
+
};
|
|
40
|
+
this.eventHistory.push(event);
|
|
41
|
+
if (this.eventHistory.length > this.maxHistorySize) {
|
|
42
|
+
this.eventHistory.shift();
|
|
43
|
+
}
|
|
44
|
+
log(`[EventBus] ${type}`, {
|
|
45
|
+
source,
|
|
46
|
+
payload: typeof payload === "object" ? "..." : payload
|
|
47
|
+
});
|
|
48
|
+
const listeners = this.listeners.get(type);
|
|
49
|
+
if (listeners) {
|
|
50
|
+
await Promise.all(Array.from(listeners).map(async (listener) => {
|
|
51
|
+
try {
|
|
52
|
+
await listener(event);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
log(`[EventBus] Listener error for ${type}`, { error });
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
getHistory(types) {
|
|
60
|
+
if (!types || types.length === 0) {
|
|
61
|
+
return [...this.eventHistory];
|
|
62
|
+
}
|
|
63
|
+
return this.eventHistory.filter((e) => types.includes(e.type));
|
|
64
|
+
}
|
|
65
|
+
clearHistory() {
|
|
66
|
+
this.eventHistory = [];
|
|
67
|
+
}
|
|
68
|
+
getListenerCount(type) {
|
|
69
|
+
return this.listeners.get(type)?.size ?? 0;
|
|
70
|
+
}
|
|
71
|
+
hasListeners(type) {
|
|
72
|
+
return this.getListenerCount(type) > 0;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
var globalEventBus = null;
|
|
76
|
+
function getGlobalEventBus() {
|
|
77
|
+
if (!globalEventBus) {
|
|
78
|
+
globalEventBus = new AutomationEventBus;
|
|
79
|
+
}
|
|
80
|
+
return globalEventBus;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/db/group-commit-writer.ts
|
|
84
|
+
init_logger();
|
|
85
|
+
function isClosedHandleError(err) {
|
|
86
|
+
const msg = (err instanceof Error ? err.message : String(err)).toLowerCase();
|
|
87
|
+
return msg.includes("database has closed") || msg.includes("database is closed") || msg.includes("cannot use a closed database") || msg.includes("database is not open");
|
|
88
|
+
}
|
|
89
|
+
var MAX_QUEUED_OPS = 1024;
|
|
90
|
+
var FLUSH_THRESHOLD_OPS = 64;
|
|
91
|
+
var DEGRADED_COOLDOWN_MS = 60000;
|
|
92
|
+
var _writers = new Map;
|
|
93
|
+
var _internals = {
|
|
94
|
+
getProjectDb,
|
|
95
|
+
isClosedHandleError
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
class GroupCommitWriter {
|
|
99
|
+
db;
|
|
100
|
+
queue = [];
|
|
101
|
+
flushing = false;
|
|
102
|
+
degraded = null;
|
|
103
|
+
lastAdvisoryAt = 0;
|
|
104
|
+
closed = false;
|
|
105
|
+
registryKey = null;
|
|
106
|
+
constructor(db) {
|
|
107
|
+
this.db = db;
|
|
108
|
+
}
|
|
109
|
+
get queuedOpCount() {
|
|
110
|
+
return this.queue.length;
|
|
111
|
+
}
|
|
112
|
+
bindRegistryKey(key) {
|
|
113
|
+
this.registryKey = key;
|
|
114
|
+
}
|
|
115
|
+
enqueue(op) {
|
|
116
|
+
if (this.closed) {
|
|
117
|
+
throw new DbWriteError("unknown", "group-commit writer is closed");
|
|
118
|
+
}
|
|
119
|
+
if (this.queue.length >= MAX_QUEUED_OPS) {
|
|
120
|
+
this.flushSync();
|
|
121
|
+
}
|
|
122
|
+
this.queue.push(op);
|
|
123
|
+
if (this.queue.length >= FLUSH_THRESHOLD_OPS) {
|
|
124
|
+
this.flushSync();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
flushSync() {
|
|
128
|
+
if (this.closed || this.flushing || this.queue.length === 0)
|
|
129
|
+
return;
|
|
130
|
+
this.flushing = true;
|
|
131
|
+
const batch = this.queue;
|
|
132
|
+
this.queue = [];
|
|
133
|
+
try {
|
|
134
|
+
try {
|
|
135
|
+
this.applyBatch(batch);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
if (!isClosedHandleError(err) || this.registryKey === null)
|
|
138
|
+
throw err;
|
|
139
|
+
this.db = _internals.getProjectDb(this.registryKey);
|
|
140
|
+
this.applyBatch(batch);
|
|
141
|
+
}
|
|
142
|
+
} catch (err) {
|
|
143
|
+
const category = classifyDbWriteError(err);
|
|
144
|
+
if (isClosedHandleError(err)) {
|
|
145
|
+
this.close();
|
|
146
|
+
if (this.registryKey !== null)
|
|
147
|
+
_writers.delete(this.registryKey);
|
|
148
|
+
}
|
|
149
|
+
if (category === "busy" || category === "disk_full" || category === "read_only") {
|
|
150
|
+
this.queue = [...batch, ...this.queue];
|
|
151
|
+
this.noteDegraded(category);
|
|
152
|
+
} else {
|
|
153
|
+
this.noteDegraded(category);
|
|
154
|
+
}
|
|
155
|
+
throw new DbWriteError(category, `swarm.db group-commit flush failed (${category}): ${err instanceof Error ? err.message : String(err)}`);
|
|
156
|
+
} finally {
|
|
157
|
+
this.flushing = false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async flush() {
|
|
161
|
+
this.flushSync();
|
|
162
|
+
}
|
|
163
|
+
close() {
|
|
164
|
+
this.closed = true;
|
|
165
|
+
this.queue = [];
|
|
166
|
+
}
|
|
167
|
+
applyBatch(batch) {
|
|
168
|
+
const cls = batchDurabilityClass(batch.map((op) => op.durability));
|
|
169
|
+
applySynchronousForClass(this.db, cls);
|
|
170
|
+
try {
|
|
171
|
+
this.db.run("BEGIN IMMEDIATE");
|
|
172
|
+
} catch (err) {
|
|
173
|
+
applySynchronousForClass(this.db, "normal");
|
|
174
|
+
throw err;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
for (const op of batch) {
|
|
178
|
+
op.run(this.db);
|
|
179
|
+
}
|
|
180
|
+
this.db.run("COMMIT");
|
|
181
|
+
} catch (err) {
|
|
182
|
+
try {
|
|
183
|
+
this.db.run("ROLLBACK");
|
|
184
|
+
} catch {}
|
|
185
|
+
throw err;
|
|
186
|
+
} finally {
|
|
187
|
+
applySynchronousForClass(this.db, "normal");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
noteDegraded(category) {
|
|
191
|
+
const now = Date.now();
|
|
192
|
+
this.degraded = { category, until: now + DEGRADED_COOLDOWN_MS };
|
|
193
|
+
if (now - this.lastAdvisoryAt < DEGRADED_COOLDOWN_MS)
|
|
194
|
+
return;
|
|
195
|
+
this.lastAdvisoryAt = now;
|
|
196
|
+
warn(`[swarm.db] durable-state writer degraded (category=${category}); queued writes retry on the next flush`);
|
|
197
|
+
}
|
|
198
|
+
get degradation() {
|
|
199
|
+
if (!this.degraded)
|
|
200
|
+
return null;
|
|
201
|
+
if (Date.now() > this.degraded.until)
|
|
202
|
+
return null;
|
|
203
|
+
return this.degraded;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function getGroupCommitWriter(directory) {
|
|
207
|
+
const key = canonicalProjectKey(directory);
|
|
208
|
+
let writer = _writers.get(key);
|
|
209
|
+
if (!writer) {
|
|
210
|
+
writer = new GroupCommitWriter(getProjectDb(directory));
|
|
211
|
+
writer.bindRegistryKey(key);
|
|
212
|
+
_writers.set(key, writer);
|
|
213
|
+
}
|
|
214
|
+
return writer;
|
|
215
|
+
}
|
|
216
|
+
function closeGroupCommitWriter(directory) {
|
|
217
|
+
const key = canonicalProjectKey(directory);
|
|
218
|
+
const writer = _writers.get(key);
|
|
219
|
+
if (writer) {
|
|
220
|
+
try {
|
|
221
|
+
writer.flushSync();
|
|
222
|
+
} catch {}
|
|
223
|
+
writer.close();
|
|
224
|
+
_writers.delete(key);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/db/legacy-import.ts
|
|
229
|
+
init_logger();
|
|
230
|
+
import {
|
|
231
|
+
existsSync,
|
|
232
|
+
readdirSync,
|
|
233
|
+
readFileSync,
|
|
234
|
+
renameSync,
|
|
235
|
+
statSync
|
|
236
|
+
} from "fs";
|
|
237
|
+
import { join } from "path";
|
|
238
|
+
var RENAME_RETRIES = 20;
|
|
239
|
+
var RENAME_RETRY_DELAY_MS = 5;
|
|
240
|
+
var warnedStaleLegacy = new Set;
|
|
241
|
+
var MAX_LEGACY_IMPORT_BYTES = 32 * 1024 * 1024;
|
|
242
|
+
function sleepSync(ms) {
|
|
243
|
+
const shared = new SharedArrayBuffer(4);
|
|
244
|
+
Atomics.wait(new Int32Array(shared), 0, 0, ms);
|
|
245
|
+
}
|
|
246
|
+
function renameWithRetry(from, to) {
|
|
247
|
+
let lastErr;
|
|
248
|
+
for (let attempt = 0;attempt < RENAME_RETRIES; attempt++) {
|
|
249
|
+
try {
|
|
250
|
+
_internals2.renameSync(from, to);
|
|
251
|
+
return;
|
|
252
|
+
} catch (err) {
|
|
253
|
+
lastErr = err;
|
|
254
|
+
const code = err?.code;
|
|
255
|
+
if (code !== "EPERM" && code !== "EACCES" && code !== "EBUSY")
|
|
256
|
+
throw err;
|
|
257
|
+
sleepSync(RENAME_RETRY_DELAY_MS);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
throw lastErr;
|
|
261
|
+
}
|
|
262
|
+
function warnStaleLegacyOnce(key, fileName) {
|
|
263
|
+
const guard = `${key}::${fileName}`;
|
|
264
|
+
if (warnedStaleLegacy.has(guard))
|
|
265
|
+
return;
|
|
266
|
+
warnedStaleLegacy.add(guard);
|
|
267
|
+
warn(`[swarm.db] legacy artifact ${fileName} is present but the target table is non-empty; leaving the file untouched (it is inert \u2014 readers use swarm.db)`);
|
|
268
|
+
}
|
|
269
|
+
var _internals2 = {
|
|
270
|
+
renameSync,
|
|
271
|
+
warnStaleLegacyOnce,
|
|
272
|
+
maxLegacyImportBytes: () => MAX_LEGACY_IMPORT_BYTES
|
|
273
|
+
};
|
|
274
|
+
function importLegacyJsonl(directory, opts) {
|
|
275
|
+
const root = canonicalProjectKey(directory);
|
|
276
|
+
const filePath = join(root, ".swarm", opts.fileName);
|
|
277
|
+
if (!existsSync(filePath)) {
|
|
278
|
+
return { imported: 0, skipped: 0, archived: false };
|
|
279
|
+
}
|
|
280
|
+
const db = getProjectDb(directory);
|
|
281
|
+
let content;
|
|
282
|
+
try {
|
|
283
|
+
if (statSync(filePath).size > _internals2.maxLegacyImportBytes()) {
|
|
284
|
+
warn(`[swarm.db] legacy artifact ${opts.fileName} exceeds the import size cap; leaving it inert (not imported, not renamed)`);
|
|
285
|
+
return { imported: 0, skipped: 0, archived: false };
|
|
286
|
+
}
|
|
287
|
+
content = readFileSync(filePath, "utf-8");
|
|
288
|
+
} catch {
|
|
289
|
+
return { imported: 0, skipped: 0, archived: false };
|
|
290
|
+
}
|
|
291
|
+
const payloads = [];
|
|
292
|
+
let skipped = 0;
|
|
293
|
+
for (const line of content.split(`
|
|
294
|
+
`)) {
|
|
295
|
+
const trimmed = line.trim();
|
|
296
|
+
if (!trimmed)
|
|
297
|
+
continue;
|
|
298
|
+
const payload = opts.parseLine(trimmed);
|
|
299
|
+
if (payload === null) {
|
|
300
|
+
skipped++;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
payloads.push(payload);
|
|
304
|
+
}
|
|
305
|
+
let imported = 0;
|
|
306
|
+
db.run("BEGIN IMMEDIATE");
|
|
307
|
+
try {
|
|
308
|
+
if (opts.streamCount(db) === 0) {
|
|
309
|
+
for (const payload of payloads) {
|
|
310
|
+
imported += 1;
|
|
311
|
+
opts.insertRow(db, imported, payload);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
db.run("COMMIT");
|
|
315
|
+
} catch (err) {
|
|
316
|
+
try {
|
|
317
|
+
db.run("ROLLBACK");
|
|
318
|
+
} catch {}
|
|
319
|
+
throw err;
|
|
320
|
+
}
|
|
321
|
+
if (imported > 0) {
|
|
322
|
+
try {
|
|
323
|
+
renameWithRetry(filePath, `${filePath}.imported`);
|
|
324
|
+
return { imported, skipped, archived: true };
|
|
325
|
+
} catch {
|
|
326
|
+
return { imported, skipped, archived: false };
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (payloads.length > 0) {
|
|
330
|
+
_internals2.warnStaleLegacyOnce(root, opts.fileName);
|
|
331
|
+
}
|
|
332
|
+
return { imported: 0, skipped, archived: false };
|
|
333
|
+
}
|
|
334
|
+
function importLegacyJsonFiles(directory, opts) {
|
|
335
|
+
const root = canonicalProjectKey(directory);
|
|
336
|
+
const swarmDir = join(root, ".swarm");
|
|
337
|
+
let entries;
|
|
338
|
+
try {
|
|
339
|
+
entries = readdirSync(swarmDir);
|
|
340
|
+
} catch {
|
|
341
|
+
return { imported: 0, skipped: 0, archived: false };
|
|
342
|
+
}
|
|
343
|
+
const suffix = ".json";
|
|
344
|
+
const phaseRe = new RegExp(`^${opts.filePrefix}(\\d+)\\${suffix}$`);
|
|
345
|
+
const files = entries.filter((name) => name.startsWith(opts.filePrefix) && name.endsWith(suffix) && !name.includes(".imported"));
|
|
346
|
+
if (files.length === 0) {
|
|
347
|
+
return { imported: 0, skipped: 0, archived: false };
|
|
348
|
+
}
|
|
349
|
+
const parsed = [];
|
|
350
|
+
let skipped = 0;
|
|
351
|
+
for (const fileName of files) {
|
|
352
|
+
const match = phaseRe.exec(fileName);
|
|
353
|
+
if (!match) {
|
|
354
|
+
skipped++;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
try {
|
|
358
|
+
const abs = join(swarmDir, fileName);
|
|
359
|
+
if (statSync(abs).size > _internals2.maxLegacyImportBytes()) {
|
|
360
|
+
skipped++;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
const raw = readFileSync(abs, "utf-8");
|
|
364
|
+
JSON.parse(raw);
|
|
365
|
+
parsed.push({ phase: Number(match[1]), payload: raw, fileName });
|
|
366
|
+
} catch {
|
|
367
|
+
skipped++;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const db = getProjectDb(directory);
|
|
371
|
+
let imported = 0;
|
|
372
|
+
const archivedNames = [];
|
|
373
|
+
db.run("BEGIN IMMEDIATE");
|
|
374
|
+
try {
|
|
375
|
+
if (opts.kindCount(db) === 0 && parsed.length > 0) {
|
|
376
|
+
for (const item of parsed) {
|
|
377
|
+
imported += 1;
|
|
378
|
+
opts.upsertRow(db, item.phase, item.payload);
|
|
379
|
+
archivedNames.push(item.fileName);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
db.run("COMMIT");
|
|
383
|
+
} catch (err) {
|
|
384
|
+
try {
|
|
385
|
+
db.run("ROLLBACK");
|
|
386
|
+
} catch {}
|
|
387
|
+
throw err;
|
|
388
|
+
}
|
|
389
|
+
if (imported > 0) {
|
|
390
|
+
for (const fileName of archivedNames) {
|
|
391
|
+
try {
|
|
392
|
+
renameWithRetry(join(swarmDir, fileName), join(swarmDir, `${fileName}.imported`));
|
|
393
|
+
} catch {}
|
|
394
|
+
}
|
|
395
|
+
return { imported, skipped, archived: archivedNames.length > 0 };
|
|
396
|
+
}
|
|
397
|
+
if (parsed.length > 0) {
|
|
398
|
+
_internals2.warnStaleLegacyOnce(root, `${opts.filePrefix}*`);
|
|
399
|
+
}
|
|
400
|
+
return { imported: 0, skipped, archived: false };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export { getGroupCommitWriter, closeGroupCommitWriter, importLegacyJsonl, importLegacyJsonFiles, getGlobalEventBus };
|