@x-otto/compute-pool 0.0.0-stage → 0.4.0-beta.0
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/build-revision.json +4 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +166 -0
- package/package.json +24 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
//#region src/pool.d.ts
|
|
2
|
+
type ComputeTaskKind = 'derived' | 'fold' | 'view-restore';
|
|
3
|
+
interface ComputeInputBase {
|
|
4
|
+
root: string;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
walDirectory?: string;
|
|
7
|
+
segments: readonly {
|
|
8
|
+
name: string;
|
|
9
|
+
bytes: number;
|
|
10
|
+
}[];
|
|
11
|
+
nextSeq: number;
|
|
12
|
+
}
|
|
13
|
+
interface DerivedComputeInput extends ComputeInputBase {
|
|
14
|
+
kind: 'derived';
|
|
15
|
+
}
|
|
16
|
+
interface FoldComputeInput extends ComputeInputBase {
|
|
17
|
+
kind: 'fold';
|
|
18
|
+
}
|
|
19
|
+
interface ViewRestoreComputeInput extends ComputeInputBase {
|
|
20
|
+
kind: 'view-restore';
|
|
21
|
+
tailMessages?: number;
|
|
22
|
+
effort?: string;
|
|
23
|
+
}
|
|
24
|
+
type ComputeInput = DerivedComputeInput | FoldComputeInput | ViewRestoreComputeInput;
|
|
25
|
+
interface DerivedComputeOutput {
|
|
26
|
+
kind: 'derived';
|
|
27
|
+
events: number;
|
|
28
|
+
openJobs: number;
|
|
29
|
+
}
|
|
30
|
+
interface FoldComputeOutput {
|
|
31
|
+
kind: 'fold';
|
|
32
|
+
events: number;
|
|
33
|
+
fold: unknown;
|
|
34
|
+
}
|
|
35
|
+
interface ViewRestoreComputeOutput {
|
|
36
|
+
kind: 'view-restore';
|
|
37
|
+
nextSeq: number;
|
|
38
|
+
seqAt: readonly number[];
|
|
39
|
+
lastClearSeq: number | null;
|
|
40
|
+
fold?: unknown;
|
|
41
|
+
anchors?: unknown;
|
|
42
|
+
nextTurn?: number;
|
|
43
|
+
derivedSummary?: unknown;
|
|
44
|
+
titleProgress?: unknown;
|
|
45
|
+
tailEvents?: readonly unknown[];
|
|
46
|
+
events: unknown[];
|
|
47
|
+
messages: unknown[];
|
|
48
|
+
coveredHead: number | undefined;
|
|
49
|
+
historyWindow: unknown;
|
|
50
|
+
}
|
|
51
|
+
type ComputeOutput = DerivedComputeOutput | FoldComputeOutput | ViewRestoreComputeOutput;
|
|
52
|
+
interface ComputePoolOptions {
|
|
53
|
+
size?: number;
|
|
54
|
+
maxQueue?: number;
|
|
55
|
+
idleMs?: number;
|
|
56
|
+
timeoutMs?: number;
|
|
57
|
+
}
|
|
58
|
+
interface ComputePoolMetrics {
|
|
59
|
+
active: number;
|
|
60
|
+
queued: number;
|
|
61
|
+
completed: number;
|
|
62
|
+
failed: number;
|
|
63
|
+
rejected: number;
|
|
64
|
+
timedOut: number;
|
|
65
|
+
}
|
|
66
|
+
type ComputeFailureReason = 'queue-full' | 'queue-timeout' | 'worker-error' | 'cancelled' | 'closed';
|
|
67
|
+
declare class ComputePoolError extends Error {
|
|
68
|
+
readonly reason: ComputeFailureReason;
|
|
69
|
+
constructor(reason: ComputeFailureReason);
|
|
70
|
+
}
|
|
71
|
+
interface ComputeSubmitOptions {
|
|
72
|
+
key?: string;
|
|
73
|
+
signal?: AbortSignal;
|
|
74
|
+
}
|
|
75
|
+
interface ComputePool {
|
|
76
|
+
warm(): Promise<void>;
|
|
77
|
+
submit(input: ComputeInput, options?: ComputeSubmitOptions): Promise<ComputeOutput>;
|
|
78
|
+
metrics(): ComputePoolMetrics;
|
|
79
|
+
close(): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
declare function createComputePool(options?: ComputePoolOptions): ComputePool;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { type ComputeFailureReason, type ComputeInput, type ComputeInputBase, type ComputeOutput, type ComputePool, ComputePoolError, type ComputePoolMetrics, type ComputePoolOptions, type ComputeSubmitOptions, type ComputeTaskKind, type DerivedComputeInput, type DerivedComputeOutput, type FoldComputeInput, type FoldComputeOutput, type ViewRestoreComputeInput, type ViewRestoreComputeOutput, createComputePool };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { createTaskPool } from "@x-otto/host-tasks";
|
|
2
|
+
|
|
3
|
+
//#region src/pool.ts
|
|
4
|
+
var ComputePoolError = class extends Error {
|
|
5
|
+
constructor(reason) {
|
|
6
|
+
super(`compute pool: ${reason}`);
|
|
7
|
+
this.reason = reason;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
const DEFAULT_SIZE = 2;
|
|
11
|
+
const DEFAULT_MAX_QUEUE = 8;
|
|
12
|
+
const DEFAULT_TIMEOUT_MS = 3e4;
|
|
13
|
+
const MODULE_URLS = {
|
|
14
|
+
persistence: import.meta.resolve("@x-otto/persistence"),
|
|
15
|
+
session: import.meta.resolve("@x-otto/session")
|
|
16
|
+
};
|
|
17
|
+
const WORKER_PRELUDE = `
|
|
18
|
+
const [persistence, session] = await Promise.all([
|
|
19
|
+
import(input.moduleUrls.persistence),
|
|
20
|
+
import(input.moduleUrls.session),
|
|
21
|
+
])
|
|
22
|
+
const events = []
|
|
23
|
+
const walStore = persistence.createWalStore({ root: input.root })
|
|
24
|
+
for await (const event of persistence.iterWalEventsFromSnapshot(
|
|
25
|
+
walStore,
|
|
26
|
+
input.sessionId,
|
|
27
|
+
{ segments: input.segments, nextSeq: input.nextSeq },
|
|
28
|
+
input.walDirectory,
|
|
29
|
+
)) {
|
|
30
|
+
events.push(event)
|
|
31
|
+
}
|
|
32
|
+
`;
|
|
33
|
+
const DERIVED_WORKER_SOURCE = `${WORKER_PRELUDE}
|
|
34
|
+
const summary = session.recomputeDerivedSummary(events)
|
|
35
|
+
return { kind: 'derived', events: events.length, openJobs: summary.openJobs?.length ?? 0 }
|
|
36
|
+
`;
|
|
37
|
+
const VIEW_RESTORE_WORKER_SOURCE = `${WORKER_PRELUDE}
|
|
38
|
+
const fold = await session.foldWindowEvents(session.emptyWindowFold(), events)
|
|
39
|
+
let lastClearSeq = null
|
|
40
|
+
for (const event of events) {
|
|
41
|
+
if (event.type === 'clear') lastClearSeq = event.seq
|
|
42
|
+
}
|
|
43
|
+
const anchors = session.initAnchorProjection(events)
|
|
44
|
+
const notices = session.noticeAnchorsFromProjection(input.sessionId, anchors)
|
|
45
|
+
const turnEnds = session.turnEndAnchorsFromProjection(anchors)
|
|
46
|
+
const projected = session.projectHistory(
|
|
47
|
+
input.sessionId,
|
|
48
|
+
fold.surface,
|
|
49
|
+
notices,
|
|
50
|
+
fold.seqAt,
|
|
51
|
+
turnEnds,
|
|
52
|
+
fold.inputSourceAt,
|
|
53
|
+
fold.inputIdsAt,
|
|
54
|
+
input.effort,
|
|
55
|
+
)
|
|
56
|
+
const selection =
|
|
57
|
+
input.tailMessages === undefined
|
|
58
|
+
? undefined
|
|
59
|
+
: session.selectHistoryTail({
|
|
60
|
+
events: projected,
|
|
61
|
+
messages: fold.surface,
|
|
62
|
+
messageSeqs: fold.seqAt,
|
|
63
|
+
tailMessages: input.tailMessages,
|
|
64
|
+
})
|
|
65
|
+
const tail = selection?.events ?? projected
|
|
66
|
+
const messages =
|
|
67
|
+
selection === undefined
|
|
68
|
+
? fold.surface
|
|
69
|
+
: fold.surface.slice(Math.max(0, fold.surface.length - (input.tailMessages ?? fold.surface.length)))
|
|
70
|
+
return {
|
|
71
|
+
kind: 'view-restore',
|
|
72
|
+
nextSeq: events.length === 0 ? 0 : (events[events.length - 1].seq + 1),
|
|
73
|
+
seqAt: fold.seqAt,
|
|
74
|
+
lastClearSeq,
|
|
75
|
+
events: tail,
|
|
76
|
+
messages,
|
|
77
|
+
coveredHead: events.length === 0 ? undefined : events[events.length - 1].seq,
|
|
78
|
+
historyWindow: selection?.window,
|
|
79
|
+
fold,
|
|
80
|
+
anchors,
|
|
81
|
+
nextTurn: session.nextTurnFromEvents(events),
|
|
82
|
+
derivedSummary: session.recomputeDerivedSummary(fold.derivedEvents ?? []),
|
|
83
|
+
titleProgress: session.reduceTitleProgress(events, null),
|
|
84
|
+
tailEvents: events.slice(Math.max(0, events.length - 200)),
|
|
85
|
+
}
|
|
86
|
+
`;
|
|
87
|
+
const FOLD_WORKER_SOURCE = `${WORKER_PRELUDE}
|
|
88
|
+
const fold = await session.foldWindowEvents(session.emptyWindowFold(), events)
|
|
89
|
+
return { kind: 'fold', events: events.length, fold }
|
|
90
|
+
`;
|
|
91
|
+
function createComputePool(options = {}) {
|
|
92
|
+
const maxQueue = Math.max(0, options.maxQueue ?? DEFAULT_MAX_QUEUE);
|
|
93
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
94
|
+
const pool = createTaskPool({
|
|
95
|
+
size: Math.max(1, options.size ?? DEFAULT_SIZE),
|
|
96
|
+
...options.idleMs === void 0 ? {} : { idleMs: options.idleMs }
|
|
97
|
+
});
|
|
98
|
+
const counters = { rejected: 0 };
|
|
99
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
100
|
+
let closed = false;
|
|
101
|
+
const runTask = async (input, submitOptions) => {
|
|
102
|
+
if (closed) throw new ComputePoolError("closed");
|
|
103
|
+
if (pool.stats().queued >= maxQueue) {
|
|
104
|
+
counters.rejected += 1;
|
|
105
|
+
throw new ComputePoolError("queue-full");
|
|
106
|
+
}
|
|
107
|
+
const outcome = await pool.run({
|
|
108
|
+
name: `restore-compute:${input.kind}`,
|
|
109
|
+
workerSource: input.kind === "fold" ? FOLD_WORKER_SOURCE : input.kind === "view-restore" ? VIEW_RESTORE_WORKER_SOURCE : DERIVED_WORKER_SOURCE,
|
|
110
|
+
input: {
|
|
111
|
+
...input,
|
|
112
|
+
moduleUrls: MODULE_URLS
|
|
113
|
+
},
|
|
114
|
+
timeoutMs
|
|
115
|
+
}, submitOptions.signal === void 0 ? void 0 : { signal: submitOptions.signal });
|
|
116
|
+
if (outcome.ok) return outcome.output;
|
|
117
|
+
if (closed || outcome.kind === "rejected") throw new ComputePoolError("closed");
|
|
118
|
+
if (outcome.kind === "timeout") throw new ComputePoolError("queue-timeout");
|
|
119
|
+
if (outcome.kind === "cancelled") throw new ComputePoolError("cancelled");
|
|
120
|
+
throw new ComputePoolError("worker-error");
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
async warm() {
|
|
124
|
+
if (closed) return;
|
|
125
|
+
const size = Math.max(1, options.size ?? DEFAULT_SIZE);
|
|
126
|
+
const tasks = Array.from({ length: size }, () => pool.run({
|
|
127
|
+
name: "warm",
|
|
128
|
+
workerSource: "return { ok: true }",
|
|
129
|
+
input: {}
|
|
130
|
+
}).catch(() => void 0));
|
|
131
|
+
await Promise.all(tasks);
|
|
132
|
+
},
|
|
133
|
+
submit(input, submitOptions = {}) {
|
|
134
|
+
if (closed) return Promise.reject(new ComputePoolError("closed"));
|
|
135
|
+
const key = submitOptions.key;
|
|
136
|
+
const joined = key === void 0 ? void 0 : inFlight.get(key);
|
|
137
|
+
if (joined !== void 0) return joined;
|
|
138
|
+
const pending = runTask(input, submitOptions);
|
|
139
|
+
if (key === void 0) return pending;
|
|
140
|
+
inFlight.set(key, pending);
|
|
141
|
+
pending.finally(() => {
|
|
142
|
+
if (inFlight.get(key) === pending) inFlight.delete(key);
|
|
143
|
+
}).catch(() => void 0);
|
|
144
|
+
return pending;
|
|
145
|
+
},
|
|
146
|
+
metrics() {
|
|
147
|
+
const stats = pool.stats();
|
|
148
|
+
return {
|
|
149
|
+
active: stats.running,
|
|
150
|
+
queued: stats.queued,
|
|
151
|
+
completed: stats.completed,
|
|
152
|
+
failed: stats.failed,
|
|
153
|
+
rejected: counters.rejected,
|
|
154
|
+
timedOut: stats.timedOut
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
close() {
|
|
158
|
+
closed = true;
|
|
159
|
+
inFlight.clear();
|
|
160
|
+
return pool.close();
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
export { ComputePoolError, createComputePool };
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-otto/compute-pool",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0-beta.0",
|
|
4
|
+
"description": "恢复计算的线程池:有界队列、带代号的取消槽、故障回退与诊断(single-worker-perf M4)",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@x-otto/host-tasks": "^0.4.0-beta.0",
|
|
19
|
+
"@x-otto/persistence": "^0.4.0-beta.0",
|
|
20
|
+
"@x-otto/session": "^0.4.0-beta.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"tag": "beta"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/README.md
DELETED