@fa-software/semi2-dsh 0.0.16 → 0.0.17-preview.20260929054409

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.
@@ -1,79 +0,0 @@
1
- // @fa-software/semi2-dsh/service/host-writes —— 磁盘写入的唯一闸口:串行队列 + 宿主就绪门 + 两段独立预算。
2
- // 只剩「盘上自铸内容的认领清理」走这里;状态段与工作区映射都是内存覆盖行,不落盘、不过这道门。
3
- // dsh 0.1.7 的 settings.update 是 Profile 配置编辑,内部 `ctx.loader.await()` 要等整棵 loader 空闲:
4
- // 排队(含等宿主就绪)到点只让调用方降级、写入留在队列继续等,写盘到点才按 writeTimeoutMs 降级。
5
- import { ConfigurationError } from "../hub/errors.js";
6
- /** 排队(含等宿主就绪)的调用方上限缺省值(ms)。 */
7
- const QUEUE_WAIT_MS = 15_000;
8
- /** 单次写盘的上限缺省值(ms)。 */
9
- const WRITE_TIMEOUT_MS = 3_000;
10
- /** 到点标记:与「底层真的返回了 undefined」区分开。 */
11
- const EXPIRED = Symbol('expired');
12
- export function createHostWrites(warn, options = {}) {
13
- const queueWaitMs = options.queueWaitMs ?? QUEUE_WAIT_MS;
14
- const writeTimeoutMs = options.writeTimeoutMs ?? WRITE_TIMEOUT_MS;
15
- /** 队列尾:只按底层任务真正落定前进,调用方是否已降级与它无关。 */
16
- let tail = Promise.resolve();
17
- /** 排队告警去重(按段):成功落定一次即复位。 */
18
- const reported = new Set();
19
- return {
20
- run(ns, task) {
21
- const started = Promise.withResolvers();
22
- const execution = tail.then(async () => {
23
- await waitHostReady(options.ready);
24
- started.resolve();
25
- await task();
26
- });
27
- // 队列尾按底层任务真正落定前进:被放弃的那笔仍占着 profile 的 writer 锁,下一笔不能抢跑。
28
- tail = execution.then(() => undefined, () => undefined);
29
- // 结果处理先接住拒绝:调用方降级后任务仍会跑完,迟到失败不该变成未处理拒绝。
30
- const outcome = execution.then(() => null, (error) => (error instanceof Error ? error : new Error(String(error))));
31
- return (async () => {
32
- if ((await within(started.promise, queueWaitMs)) === EXPIRED) {
33
- if (!reported.has(ns)) {
34
- reported.add(ns);
35
- warn(`${ns} 写入排队中(宿主仍在启动,等满 ${queueWaitMs}ms),本轮先降级;宿主就绪后照常落定`);
36
- }
37
- throw new ConfigurationError('CONFIG_APPLY_FAILED');
38
- }
39
- const result = await within(outcome, writeTimeoutMs);
40
- if (result === EXPIRED) {
41
- warn(`${ns} 设置写入超时(${writeTimeoutMs}ms),本端继续,状态可能落后`);
42
- throw new ConfigurationError('CONFIG_APPLY_FAILED');
43
- }
44
- if (result !== null)
45
- throw result;
46
- reported.delete(ns);
47
- })();
48
- },
49
- };
50
- }
51
- /** 宿主就绪门:等 profile 的 loader 条目树空闲——设置写入内部等的就是它;没有该端(宿主直跑 / 假宿主)立即放行。 */
52
- async function waitHostReady(ready) {
53
- if (ready === undefined)
54
- return;
55
- try {
56
- await ready();
57
- }
58
- catch {
59
- // 就绪门只负责等:树自身异常不该拦下写入,交给写盘预算与调用方的降级。
60
- }
61
- }
62
- /** 到点返回 {@link EXPIRED},否则原样给出结果;定时器总在返回前清掉。 */
63
- async function within(promise, ms) {
64
- let timer;
65
- try {
66
- return await Promise.race([
67
- promise,
68
- new Promise((resolve) => {
69
- timer = globalThis.setTimeout(() => {
70
- resolve(EXPIRED);
71
- }, ms);
72
- }),
73
- ]);
74
- }
75
- finally {
76
- if (timer !== undefined)
77
- globalThis.clearTimeout(timer);
78
- }
79
- }
@@ -1,16 +0,0 @@
1
- export interface HostWritesOptions {
2
- /** 宿主就绪门(调用方从 `ctx.get('loader', false)` 取;缺席即立刻放行)。 */
3
- ready?: (() => Promise<unknown>) | undefined;
4
- /** 调用方在队列里等宿主就绪的上限(ms,缺省 {@link QUEUE_WAIT_MS})。 */
5
- queueWaitMs?: number | undefined;
6
- /** 调用方等真正写盘的上限(ms,缺省 {@link WRITE_TIMEOUT_MS})。 */
7
- writeTimeoutMs?: number | undefined;
8
- }
9
- export interface HostWrites {
10
- /**
11
- * 排进本进程唯一的写入队列:前一笔**真正落定**后才轮到自己,轮到时先等宿主(loader 条目树)空闲,再执行 task。
12
- * 调用方在任一预算到期时按 `CONFIG_APPLY_FAILED` 降级;task 不被取消——排队超时的这笔仍会照常落定。
13
- */
14
- run(ns: string, task: () => Promise<unknown>): Promise<void>;
15
- }
16
- export declare function createHostWrites(warn: (message: string) => void, options?: HostWritesOptions): HostWrites;