@nowcrew/daemon 0.5.29 → 0.5.31

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,46 +1,58 @@
1
1
  export function createSharedSlotManager(limits) {
2
2
  const activeByHandle = new Map();
3
- const queuesByHandle = new Map();
4
- const promoteNext = (handle) => {
5
- const queue = queuesByHandle.get(handle) ?? [];
6
- while ((activeByHandle.get(handle) ?? 0) < limits.maxParallelPerAgent && queue.length > 0) {
7
- const next = queue.shift();
8
- if (next.released)
3
+ const queue = [];
4
+ let activeTotal = 0;
5
+ const queuedFor = (handle) => queue.filter((entry) => !entry.released && entry.handle === handle).length;
6
+ const promote = () => {
7
+ while (activeTotal < limits.maxParallelTotal) {
8
+ const index = queue.findIndex((entry) => !entry.released
9
+ && (activeByHandle.get(entry.handle) ?? 0) < limits.maxParallelPerAgent);
10
+ if (index < 0)
11
+ return;
12
+ const [next] = queue.splice(index, 1);
13
+ if (!next || next.released)
9
14
  continue;
10
15
  next.promoted = true;
11
- activeByHandle.set(handle, (activeByHandle.get(handle) ?? 0) + 1);
16
+ activeTotal += 1;
17
+ activeByHandle.set(next.handle, (activeByHandle.get(next.handle) ?? 0) + 1);
12
18
  next.resolve();
13
19
  }
14
- if (queue.length === 0)
15
- queuesByHandle.delete(handle);
16
20
  };
17
21
  return {
18
22
  reserve: (handle, kind) => {
19
- const active = activeByHandle.get(handle) ?? 0;
20
- const queued = queuesByHandle.get(handle)?.length ?? 0;
21
- const facts = { activeForAgent: active, queuedForAgent: queued };
22
- if (kind === "execution" && active >= limits.maxParallelPerAgent
23
- && queued >= limits.maxQueuedPerAgent) {
24
- return { facts, ready: Promise.resolve(), isQueued: () => false, release: () => { } };
23
+ const activeForAgent = activeByHandle.get(handle) ?? 0;
24
+ const queuedForAgent = queuedFor(handle);
25
+ const facts = {
26
+ activeForAgent,
27
+ queuedForAgent,
28
+ activeTotal,
29
+ queuedTotal: queue.length,
30
+ };
31
+ const canStartImmediately = activeTotal < limits.maxParallelTotal
32
+ && activeForAgent < limits.maxParallelPerAgent
33
+ && queue.length === 0;
34
+ if (kind === "execution" && !canStartImmediately && (queuedForAgent >= limits.maxQueuedPerAgent
35
+ || queue.length >= limits.maxQueuedTotal)) {
36
+ return {
37
+ accepted: false,
38
+ facts,
39
+ ready: Promise.resolve(),
40
+ isQueued: () => false,
41
+ release: () => { },
42
+ };
25
43
  }
26
44
  let resolveReady;
27
45
  const ready = new Promise((resolve) => { resolveReady = resolve; });
28
46
  const entry = {
29
- kind,
47
+ handle,
30
48
  released: false,
31
- promoted: active < limits.maxParallelPerAgent,
49
+ promoted: false,
32
50
  resolve: resolveReady,
33
51
  };
34
- if (entry.promoted) {
35
- activeByHandle.set(handle, active + 1);
36
- resolveReady();
37
- }
38
- else {
39
- const queue = queuesByHandle.get(handle) ?? [];
40
- queue.push(entry);
41
- queuesByHandle.set(handle, queue);
42
- }
52
+ queue.push(entry);
53
+ promote();
43
54
  return {
55
+ accepted: true,
44
56
  facts,
45
57
  state: entry.promoted ? "ready" : "queued",
46
58
  ready,
@@ -50,19 +62,22 @@ export function createSharedSlotManager(limits) {
50
62
  return;
51
63
  entry.released = true;
52
64
  if (entry.promoted) {
53
- activeByHandle.set(handle, Math.max(0, (activeByHandle.get(handle) ?? 1) - 1));
65
+ activeTotal = Math.max(0, activeTotal - 1);
66
+ const nextForAgent = Math.max(0, (activeByHandle.get(handle) ?? 1) - 1);
67
+ if (nextForAgent === 0)
68
+ activeByHandle.delete(handle);
69
+ else
70
+ activeByHandle.set(handle, nextForAgent);
54
71
  }
55
72
  else {
56
- const queue = queuesByHandle.get(handle);
57
- const index = queue?.indexOf(entry) ?? -1;
58
- if (queue !== undefined && index >= 0)
73
+ const index = queue.indexOf(entry);
74
+ if (index >= 0)
59
75
  queue.splice(index, 1);
60
- if (queue?.length === 0)
61
- queuesByHandle.delete(handle);
62
76
  }
63
- promoteNext(handle);
77
+ promote();
64
78
  },
65
79
  };
66
80
  },
81
+ snapshot: () => ({ activeTotal, queuedTotal: queue.length }),
67
82
  };
68
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nowcrew/daemon",
3
- "version": "0.5.29",
3
+ "version": "0.5.31",
4
4
  "type": "module",
5
5
  "description": "crew daemon — 运行在用户机器:拉起/管理 agent 进程,注入 crew CLI,归一化 runtime 事件",
6
6
  "license": "Apache-2.0",