@cortexkit/magic-context 0.49.35 → 0.49.37
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/dist/index.js +234 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -27078,11 +27078,210 @@ function registerExitAbort(controller) {
|
|
|
27078
27078
|
listenerRegistered = true;
|
|
27079
27079
|
process.once("exit", abortAll);
|
|
27080
27080
|
}
|
|
27081
|
+
function unregisterExitAbort(controller) {
|
|
27082
|
+
controllers.delete(controller);
|
|
27083
|
+
}
|
|
27081
27084
|
var controllers, listenerRegistered = false;
|
|
27082
27085
|
var init_exit_abort_registry = __esm(() => {
|
|
27083
27086
|
controllers = new Set;
|
|
27084
27087
|
});
|
|
27085
27088
|
|
|
27089
|
+
// ../plugin/src/shared/reusable-worker.ts
|
|
27090
|
+
import { EventEmitter } from "node:events";
|
|
27091
|
+
import * as timers from "node:timers";
|
|
27092
|
+
import {
|
|
27093
|
+
MessageChannel,
|
|
27094
|
+
receiveMessageOnPort,
|
|
27095
|
+
Worker
|
|
27096
|
+
} from "node:worker_threads";
|
|
27097
|
+
function cloneJobValue(value, transferList) {
|
|
27098
|
+
if (sharedCloneWorks)
|
|
27099
|
+
return structuredClone(value, { transfer: transferList });
|
|
27100
|
+
if (!cloneChannel) {
|
|
27101
|
+
cloneChannel = inRootAsyncContext(() => new MessageChannel);
|
|
27102
|
+
cloneChannel.port1.unref();
|
|
27103
|
+
cloneChannel.port2.unref();
|
|
27104
|
+
}
|
|
27105
|
+
cloneChannel.port1.postMessage(value, transferList);
|
|
27106
|
+
const packet = receiveMessageOnPort(cloneChannel.port2);
|
|
27107
|
+
if (!packet)
|
|
27108
|
+
throw new Error("worker input clone did not arrive synchronously");
|
|
27109
|
+
return packet.message;
|
|
27110
|
+
}
|
|
27111
|
+
function reuseWorker(entry, options, idleMs = IDLE_MS) {
|
|
27112
|
+
const data = cloneJobValue(options.workerData);
|
|
27113
|
+
const slot = acquireSlot(entry, options, idleMs);
|
|
27114
|
+
const lease = new Lease(slot, data);
|
|
27115
|
+
enqueue(slot, lease);
|
|
27116
|
+
return lease;
|
|
27117
|
+
}
|
|
27118
|
+
function enqueue(slot, lease) {
|
|
27119
|
+
timers.clearTimeout(slot.idle);
|
|
27120
|
+
lease.slot = slot;
|
|
27121
|
+
slot.queue.push(lease);
|
|
27122
|
+
queueMicrotask(() => slot.advance());
|
|
27123
|
+
}
|
|
27124
|
+
function acquireSlot(entry, options, idleMs) {
|
|
27125
|
+
const data = options.workerData;
|
|
27126
|
+
const key = JSON.stringify([
|
|
27127
|
+
String(entry),
|
|
27128
|
+
data?.path ?? data?.dbPath,
|
|
27129
|
+
data?.harness,
|
|
27130
|
+
data?.job
|
|
27131
|
+
]);
|
|
27132
|
+
let slot = pools.get(key);
|
|
27133
|
+
if (!slot) {
|
|
27134
|
+
const worker = inRootAsyncContext(() => new Worker(entry, { ...options, workerData: { reusableWorker: true } }));
|
|
27135
|
+
const controller = new AbortController;
|
|
27136
|
+
slot = { worker, controller, queue: [], advance: () => {} };
|
|
27137
|
+
pools.set(key, slot);
|
|
27138
|
+
liveSlots.add(slot);
|
|
27139
|
+
const owned = slot;
|
|
27140
|
+
const advance = () => {
|
|
27141
|
+
if (controller.signal.aborted || owned.active)
|
|
27142
|
+
return;
|
|
27143
|
+
timers.clearTimeout(owned.idle);
|
|
27144
|
+
const lease = owned.queue.shift();
|
|
27145
|
+
owned.active = lease;
|
|
27146
|
+
if (!lease) {
|
|
27147
|
+
worker.unref();
|
|
27148
|
+
owned.idle = timers.setTimeout(() => {
|
|
27149
|
+
if (pools.get(key) === owned)
|
|
27150
|
+
pools.delete(key);
|
|
27151
|
+
worker.terminate();
|
|
27152
|
+
}, idleMs);
|
|
27153
|
+
owned.idle.unref();
|
|
27154
|
+
return;
|
|
27155
|
+
}
|
|
27156
|
+
lease.start();
|
|
27157
|
+
};
|
|
27158
|
+
owned.advance = advance;
|
|
27159
|
+
worker.on("message", (message) => {
|
|
27160
|
+
const lease = owned.active;
|
|
27161
|
+
if (!lease || message.jobId !== lease.id)
|
|
27162
|
+
return;
|
|
27163
|
+
if (!message.finished) {
|
|
27164
|
+
lease.emit("message", message.value);
|
|
27165
|
+
return;
|
|
27166
|
+
}
|
|
27167
|
+
lease.ended = true;
|
|
27168
|
+
owned.active = undefined;
|
|
27169
|
+
if (message.code) {
|
|
27170
|
+
if (pools.get(key) === owned)
|
|
27171
|
+
pools.delete(key);
|
|
27172
|
+
lease.emit("exit", message.code);
|
|
27173
|
+
worker.terminate();
|
|
27174
|
+
return;
|
|
27175
|
+
}
|
|
27176
|
+
lease.emit("exit", 0);
|
|
27177
|
+
advance();
|
|
27178
|
+
});
|
|
27179
|
+
worker.on("error", (error) => {
|
|
27180
|
+
if (pools.get(key) === owned)
|
|
27181
|
+
pools.delete(key);
|
|
27182
|
+
owned.active?.emit("error", error);
|
|
27183
|
+
});
|
|
27184
|
+
worker.on("exit", (code) => {
|
|
27185
|
+
liveSlots.delete(owned);
|
|
27186
|
+
timers.clearTimeout(owned.idle);
|
|
27187
|
+
if (pools.get(key) === owned)
|
|
27188
|
+
pools.delete(key);
|
|
27189
|
+
const active = owned.active;
|
|
27190
|
+
owned.active = undefined;
|
|
27191
|
+
if (active && !active.ended) {
|
|
27192
|
+
active.ended = true;
|
|
27193
|
+
active.emit("exit", code);
|
|
27194
|
+
}
|
|
27195
|
+
for (const lease of owned.queue.splice(0)) {
|
|
27196
|
+
if (controller.signal.aborted) {
|
|
27197
|
+
lease.ended = true;
|
|
27198
|
+
lease.emit("exit", code);
|
|
27199
|
+
} else {
|
|
27200
|
+
try {
|
|
27201
|
+
enqueue(acquireSlot(entry, options, idleMs), lease);
|
|
27202
|
+
} catch (error) {
|
|
27203
|
+
lease.ended = true;
|
|
27204
|
+
lease.emit("error", error);
|
|
27205
|
+
lease.emit("exit", 1);
|
|
27206
|
+
}
|
|
27207
|
+
}
|
|
27208
|
+
}
|
|
27209
|
+
});
|
|
27210
|
+
controller.signal.addEventListener("abort", () => {
|
|
27211
|
+
worker.terminate();
|
|
27212
|
+
}, { once: true });
|
|
27213
|
+
registerExitAbort(controller);
|
|
27214
|
+
worker.once("exit", () => unregisterExitAbort(controller));
|
|
27215
|
+
}
|
|
27216
|
+
return slot;
|
|
27217
|
+
}
|
|
27218
|
+
var pools, liveSlots, nextId = 0, IDLE_MS = 60000, sharedCloneWorks, cloneChannel, Lease;
|
|
27219
|
+
var init_reusable_worker = __esm(async () => {
|
|
27220
|
+
init_exit_abort_registry();
|
|
27221
|
+
await init_sqlite();
|
|
27222
|
+
pools = new Map;
|
|
27223
|
+
liveSlots = new Set;
|
|
27224
|
+
sharedCloneWorks = (() => {
|
|
27225
|
+
const source = new SharedArrayBuffer(1);
|
|
27226
|
+
const clone = structuredClone(source);
|
|
27227
|
+
new Uint8Array(source)[0] = 1;
|
|
27228
|
+
return new Uint8Array(clone)[0] === 1;
|
|
27229
|
+
})();
|
|
27230
|
+
Lease = class Lease extends EventEmitter {
|
|
27231
|
+
slot;
|
|
27232
|
+
data;
|
|
27233
|
+
id = ++nextId;
|
|
27234
|
+
ended = false;
|
|
27235
|
+
referenced = true;
|
|
27236
|
+
outbox = [];
|
|
27237
|
+
constructor(slot, data) {
|
|
27238
|
+
super();
|
|
27239
|
+
this.slot = slot;
|
|
27240
|
+
this.data = data;
|
|
27241
|
+
}
|
|
27242
|
+
postMessage(value, transferList) {
|
|
27243
|
+
if (this.ended)
|
|
27244
|
+
return;
|
|
27245
|
+
if (this.slot.active === this)
|
|
27246
|
+
this.slot.worker.postMessage({ jobId: this.id, value }, transferList);
|
|
27247
|
+
else
|
|
27248
|
+
this.outbox.push(cloneJobValue({ value, transferList }, transferList));
|
|
27249
|
+
}
|
|
27250
|
+
start() {
|
|
27251
|
+
if (this.referenced)
|
|
27252
|
+
this.slot.worker.ref();
|
|
27253
|
+
else
|
|
27254
|
+
this.slot.worker.unref();
|
|
27255
|
+
this.emit("online");
|
|
27256
|
+
this.slot.worker.postMessage({ jobId: this.id, start: this.data });
|
|
27257
|
+
for (const { value, transferList } of this.outbox.splice(0))
|
|
27258
|
+
this.postMessage(value, transferList);
|
|
27259
|
+
}
|
|
27260
|
+
ref() {
|
|
27261
|
+
this.referenced = true;
|
|
27262
|
+
if (this.slot.active === this)
|
|
27263
|
+
this.slot.worker.ref();
|
|
27264
|
+
return this;
|
|
27265
|
+
}
|
|
27266
|
+
unref() {
|
|
27267
|
+
this.referenced = false;
|
|
27268
|
+
if (this.slot.active === this)
|
|
27269
|
+
this.slot.worker.unref();
|
|
27270
|
+
return this;
|
|
27271
|
+
}
|
|
27272
|
+
async terminate() {
|
|
27273
|
+
if (this.ended)
|
|
27274
|
+
return 0;
|
|
27275
|
+
if (this.slot.active === this)
|
|
27276
|
+
return await this.slot.worker.terminate();
|
|
27277
|
+
this.ended = true;
|
|
27278
|
+
this.slot.queue.splice(this.slot.queue.indexOf(this), 1);
|
|
27279
|
+
this.emit("exit", 0);
|
|
27280
|
+
return 0;
|
|
27281
|
+
}
|
|
27282
|
+
};
|
|
27283
|
+
});
|
|
27284
|
+
|
|
27086
27285
|
// ../plugin/src/shared/worker-shutdown.ts
|
|
27087
27286
|
function createWorkerShutdown(worker, site, deadlineMs) {
|
|
27088
27287
|
let exited = false;
|
|
@@ -28177,7 +28376,7 @@ END`;
|
|
|
28177
28376
|
});
|
|
28178
28377
|
|
|
28179
28378
|
// ../plugin/src/hooks/magic-context/raw-ordinal-warmup.ts
|
|
28180
|
-
import { Worker } from "node:worker_threads";
|
|
28379
|
+
import { Worker as Worker2 } from "node:worker_threads";
|
|
28181
28380
|
function stopRawSessionOrdinalWarmups() {
|
|
28182
28381
|
for (const stop of [...activeWorkers.values()])
|
|
28183
28382
|
stop("stopped");
|
|
@@ -28209,6 +28408,7 @@ function runSessionWarmupWorker(input, options) {
|
|
|
28209
28408
|
clearTimeout(deadline);
|
|
28210
28409
|
options.signal?.removeEventListener("abort", abort);
|
|
28211
28410
|
if (worker) {
|
|
28411
|
+
worker.off("online", startDeadline);
|
|
28212
28412
|
activeWorkers.delete(worker);
|
|
28213
28413
|
const stopped = shutdown?.close() ?? worker.terminate();
|
|
28214
28414
|
worker.unref();
|
|
@@ -28226,6 +28426,10 @@ function runSessionWarmupWorker(input, options) {
|
|
|
28226
28426
|
deadline = setTimeout(() => finish({ failure, detail: `no reply within ${ms} ms` }), ms);
|
|
28227
28427
|
deadline.unref?.();
|
|
28228
28428
|
};
|
|
28429
|
+
const startDeadline = () => {
|
|
28430
|
+
if (!settled)
|
|
28431
|
+
arm("not-accepted", options.acceptMs ?? ACCEPT_TIMEOUT_MS);
|
|
28432
|
+
};
|
|
28229
28433
|
const abort = () => finish({ failure: "stopped", detail: "stopped" });
|
|
28230
28434
|
if (options.signal?.aborted) {
|
|
28231
28435
|
abort();
|
|
@@ -28233,8 +28437,14 @@ function runSessionWarmupWorker(input, options) {
|
|
|
28233
28437
|
}
|
|
28234
28438
|
options.signal?.addEventListener("abort", abort, { once: true });
|
|
28235
28439
|
try {
|
|
28236
|
-
worker = inRootAsyncContext(() => new
|
|
28440
|
+
worker = inRootAsyncContext(() => options.entry ? new Worker2(options.entry, {
|
|
28237
28441
|
workerData: { ...input, openCancellation: openCancellation.buffer }
|
|
28442
|
+
}) : reuseWorker(defaultEntry(), {
|
|
28443
|
+
workerData: {
|
|
28444
|
+
...input,
|
|
28445
|
+
job: rawWorkerJobKind(input),
|
|
28446
|
+
openCancellation: openCancellation.buffer
|
|
28447
|
+
}
|
|
28238
28448
|
}));
|
|
28239
28449
|
shutdown = createWorkerShutdown(worker, "raw-ordinal-warmup", WARMUP_CLOSE_DEADLINE_MS);
|
|
28240
28450
|
} catch (error) {
|
|
@@ -28243,7 +28453,10 @@ function runSessionWarmupWorker(input, options) {
|
|
|
28243
28453
|
}
|
|
28244
28454
|
worker.unref();
|
|
28245
28455
|
activeWorkers.set(worker, (failure) => finish({ failure, detail: "stopped" }));
|
|
28246
|
-
|
|
28456
|
+
if (options.entry)
|
|
28457
|
+
startDeadline();
|
|
28458
|
+
else
|
|
28459
|
+
worker.once("online", startDeadline);
|
|
28247
28460
|
worker.on("message", (reply) => {
|
|
28248
28461
|
if (reply.kind === "accepted")
|
|
28249
28462
|
arm("no-result", options.resultMs ?? RESULT_TIMEOUT_MS);
|
|
@@ -28273,6 +28486,19 @@ function runSessionWarmupWorker(input, options) {
|
|
|
28273
28486
|
worker.on("exit", (code) => finish({ failure: "exited", detail: `exit code ${code}` }));
|
|
28274
28487
|
});
|
|
28275
28488
|
}
|
|
28489
|
+
function rawWorkerJobKind(input) {
|
|
28490
|
+
if (input.indexMessage)
|
|
28491
|
+
return "index-message";
|
|
28492
|
+
if (input.ftsRowidBackfill)
|
|
28493
|
+
return "fts-rowid-backfill";
|
|
28494
|
+
if (input.searchArchive)
|
|
28495
|
+
return "search-archive";
|
|
28496
|
+
if (input.exactOrdinalIds)
|
|
28497
|
+
return "exact-ordinal-ids";
|
|
28498
|
+
if (input.tagRatio !== undefined)
|
|
28499
|
+
return input.tagState ? "tag-delta" : "tag-summary";
|
|
28500
|
+
return "ordinal-scan";
|
|
28501
|
+
}
|
|
28276
28502
|
function prewarmRawSessionOrdinalsForDb(db, sessionId, options = {}) {
|
|
28277
28503
|
if (isRawSessionSummaryWarm(db, sessionId))
|
|
28278
28504
|
return Promise.resolve("warm");
|
|
@@ -28335,6 +28561,7 @@ var init_raw_ordinal_warmup = __esm(async () => {
|
|
|
28335
28561
|
init_logger();
|
|
28336
28562
|
init_worker_shutdown();
|
|
28337
28563
|
await __promiseAll([
|
|
28564
|
+
init_reusable_worker(),
|
|
28338
28565
|
init_sqlite(),
|
|
28339
28566
|
init_read_session_raw(),
|
|
28340
28567
|
init_sqlite()
|
|
@@ -28934,7 +29161,10 @@ var init_off_thread_migration_clock = () => {};
|
|
|
28934
29161
|
var init_migration_worker_client = __esm(async () => {
|
|
28935
29162
|
init_logger();
|
|
28936
29163
|
init_off_thread_migration_clock();
|
|
28937
|
-
await
|
|
29164
|
+
await __promiseAll([
|
|
29165
|
+
init_reusable_worker(),
|
|
29166
|
+
init_sqlite()
|
|
29167
|
+
]);
|
|
28938
29168
|
});
|
|
28939
29169
|
|
|
28940
29170
|
// ../plugin/src/features/magic-context/memory/constants.ts
|