pi-web-ui 0.114.2 → 0.115.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/CHANGELOG.md +55 -1
- package/bin/pi-web-ui.mjs +69 -7
- package/dist/server/agent-service-daemon.js +440 -32
- package/dist/server/agent-service.js +878 -383
- package/dist/server/canvas/canvas-dispatch.js +13 -1
- package/dist/server/client-state.js +25 -0
- package/dist/server/communicate-tool.js +2 -0
- package/dist/server/context-usage.js +105 -0
- package/dist/server/conversation-communication.js +14 -0
- package/dist/server/conversation-identity.js +27 -0
- package/dist/server/device-center/approval-summary.js +31 -0
- package/dist/server/device-center/bot-catalog.js +21 -7
- package/dist/server/device-center/bot-execution.js +72 -9
- package/dist/server/device-center/bot-presets.js +46 -0
- package/dist/server/device-center/collab-service.js +328 -10
- package/dist/server/device-center/collab-tool.js +738 -12
- package/dist/server/device-center/connector.js +359 -23
- package/dist/server/device-center/desktop-page.js +22 -16
- package/dist/server/device-center/device-integration.js +595 -0
- package/dist/server/device-center/execution-guides.js +32 -0
- package/dist/server/device-center/execution-instructions.js +19 -0
- package/dist/server/device-center/execution-settings.js +100 -0
- package/dist/server/device-center/execution-system-prompt.js +38 -0
- package/dist/server/device-center/execution-workspace.js +20 -0
- package/dist/server/device-center/executor.js +389 -40
- package/dist/server/device-center/file-sync-paths.js +171 -0
- package/dist/server/device-center/file-sync.js +180 -91
- package/dist/server/device-center/group-context-delta.js +37 -0
- package/dist/server/device-center/group-context-message.js +208 -0
- package/dist/server/device-center/group-context.js +109 -0
- package/dist/server/device-center/group-events.js +136 -0
- package/dist/server/device-center/group-plan-guard.js +138 -0
- package/dist/server/device-center/hub-conversations.js +178 -0
- package/dist/server/device-center/hub-deliveries.js +277 -0
- package/dist/server/device-center/hub-integration.js +451 -0
- package/dist/server/device-center/hub.js +1544 -80
- package/dist/server/device-center/human-interaction.js +232 -0
- package/dist/server/device-center/integration-errors.js +25 -0
- package/dist/server/device-center/local-hub.js +18 -0
- package/dist/server/device-center/local.js +356 -40
- package/dist/server/device-center/main.js +11 -1
- package/dist/server/device-center/path-portal.js +11 -3
- package/dist/server/device-center/relay.js +364 -35
- package/dist/server/device-center/report-metadata.js +66 -0
- package/dist/server/device-center/report-upload.js +63 -0
- package/dist/server/device-center/runtime.js +549 -41
- package/dist/server/device-center/store.js +66 -3
- package/dist/server/device-center/task-context-budget.js +161 -0
- package/dist/server/device-center/task-journal.js +385 -0
- package/dist/server/device-center/task-plan.js +33 -0
- package/dist/server/device-center/task-progress.js +141 -0
- package/dist/server/device-center/task-recovery.js +452 -0
- package/dist/server/device-center/task-report-outbox.js +216 -0
- package/dist/server/device-center/task-report.js +384 -0
- package/dist/server/device-center/task-scheduler.js +400 -0
- package/dist/server/device-center/task-state.js +333 -0
- package/dist/server/device-center/transfer-stream.js +1006 -0
- package/dist/server/device-center/transfers.js +815 -8
- package/dist/server/dispatch-domain-handlers.js +21 -3
- package/dist/server/dsh/dsh-agent-service.js +6 -0
- package/dist/server/goal-service.js +10 -5
- package/dist/server/index.js +89 -13
- package/dist/server/message-deletion.js +40 -0
- package/dist/server/message-window.js +9 -0
- package/dist/server/patch-executable-lookup.js +396 -0
- package/dist/server/payload-limits.js +2 -0
- package/dist/server/plugin-llm.js +100 -9
- package/dist/server/plugins.js +1 -0
- package/dist/server/preset-fields.js +24 -0
- package/dist/server/preset-share.js +2 -0
- package/dist/server/prompt-base-store.js +85 -0
- package/dist/server/prompt-composer.js +5 -2
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/protocol.js +2 -2
- package/dist/server/remote-ssh-service.js +22 -9
- package/dist/server/resolve-global-sdk.js +2 -0
- package/dist/server/session-search.js +1 -1
- package/dist/server/settings-service.js +90 -39
- package/dist/server/skill-content-filler.js +29 -8
- package/dist/server/skill-tool.js +12 -3
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagents.js +50 -36
- package/dist/server/tool-host-factories.js +1 -0
- package/dist/server/universal/universal-session.js +5 -0
- package/dist/server/webui-context.js +25 -5
- package/docs/README.md +94 -0
- package/docs/device-center.md +100 -12
- package/docs/dsh-engine.md +10 -10
- package/extensions/webui.ts +43 -1
- package/package.json +1 -1
- package/themes/aetheris.css +5 -0
- package/themes/ayu-light.css +6 -0
- package/themes/bases/flow-base.css +68 -68
- package/themes/catppuccin-latte.css +6 -0
- package/themes/catppuccin.css +6 -0
- package/themes/claude-code-dark.css +5 -0
- package/themes/codex.css +6 -0
- package/themes/cyberpunk.css +6 -0
- package/themes/dark-teal.css +6 -1
- package/themes/dazzle.css +6 -0
- package/themes/everforest-light.css +6 -0
- package/themes/flow-light.css +8 -1
- package/themes/flow.css +6 -1
- package/themes/geist.css +6 -0
- package/themes/gruvbox-light.css +6 -0
- package/themes/kanagawa-lotus.css +6 -0
- package/themes/md-preview.css +6 -0
- package/themes/mist.css +6 -0
- package/themes/nord.css +6 -0
- package/themes/one-dark.css +6 -0
- package/themes/paper.css +6 -0
- package/themes/rose-pine-dawn.css +6 -0
- package/themes/sakura.css +6 -0
- package/themes/solarized-light.css +6 -0
- package/themes/tokyo-night.css +6 -0
- package/themes/translucent.css +6 -0
- package/themes/transparent.css +6 -0
- package/themes/vs-code-dark.css +5 -0
- package/themes/wallpaper-flow.css +5 -1
- package/themes/wallpaper-obsidian.css +6 -0
- package/themes/white.css +6 -0
- package/themes/zhujian-dark.css +6 -1
- package/themes/zhujian.css +6 -1
- package/themes/zhupi-dark.css +6 -1
- package/themes/zhupi.css +6 -1
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/BgTasksModal-BYzPgmoq.js +1 -0
- package/web/dist/assets/BgTasksModal-ni0VTmAQ.js +1 -0
- package/web/dist/assets/{CanvasStudio-B3W6b_rA.js → CanvasStudio-BCUeEf25.js} +3 -3
- package/web/dist/assets/{CanvasStudio-Csv1Sj1-.js → CanvasStudio-DNmDNBod.js} +3 -3
- package/web/dist/assets/DeviceCenter-BcBhRgL6.js +3 -0
- package/web/dist/assets/DeviceCenter-CmL5FZmt.js +3 -0
- package/web/dist/assets/DeviceGroups-CLdpdnI7.js +1 -0
- package/web/dist/assets/DeviceGroups-CgBaDPrD.js +1 -0
- package/web/dist/assets/Dropdown-CGXD0DfT.js +1 -0
- package/web/dist/assets/FilePreview-B8iqnBjy.js +2 -0
- package/web/dist/assets/FilePreview-BqviZKCN.js +2 -0
- package/web/dist/assets/GlobalSearchModal-mevF-ySi.js +1 -0
- package/web/dist/assets/GlobalSearchModal-qL99FN_F.js +1 -0
- package/web/dist/assets/GroupConversation-B88Yy-lt.js +3 -0
- package/web/dist/assets/GroupConversation-BwP5NVOE.js +3 -0
- package/web/dist/assets/HintTip-LHwkS2jV.js +1 -0
- package/web/dist/assets/ModelConfigModal-Do3Fe3jH.js +2 -0
- package/web/dist/assets/PiSetupModal-DE5-gEtI.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-6UMARucA.js +1 -0
- package/web/dist/assets/SCMPanel-Hi6QqC1Q.js +2 -0
- package/web/dist/assets/SCMPanel-ezY_ZtCu.js +2 -0
- package/web/dist/assets/SettingsModal-6HgeEsJ8.js +20 -0
- package/web/dist/assets/SettingsModal-CF-7MwqS.js +20 -0
- package/web/dist/assets/TerminalPanel-CTp_BZ4S.js +3 -0
- package/web/dist/assets/TerminalPanel-CkYxC-Gh.js +3 -0
- package/web/dist/assets/TuiOverlayModal-1cRLP8IF.js +1 -0
- package/web/dist/assets/TuiOverlayModal-2xlptqWX.js +1 -0
- package/web/dist/assets/app-globals-DZNa9v8u.js +325 -0
- package/web/dist/assets/canvas-client-Bye9nSFO.js +1 -0
- package/web/dist/assets/{desktop-YguLbrX1.js → desktop-kjBoE3Nu.js} +1 -1
- package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
- package/web/dist/assets/index-CsFs_HMb.css +1 -0
- package/web/dist/assets/index-DHeeHS_l.js +85 -0
- package/web/dist/assets/index-DTZch8uz.js +85 -0
- package/web/dist/assets/plugin-host-C8mNgghx.js +1 -0
- package/web/dist/assets/remote-ssh-client-CeP3CLUW.js +1 -0
- package/web/dist/assets/theme-BXGpyTMh.js +1 -0
- package/web/dist/index.html +10 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/BgTasksModal-BIlQ89vr.js +0 -1
- package/web/dist/assets/BgTasksModal-Ck2sjlPM.js +0 -1
- package/web/dist/assets/DeviceCenter-BQdDcO8A.js +0 -3
- package/web/dist/assets/DeviceCenter-CHRKL0Vy.js +0 -3
- package/web/dist/assets/DeviceGroups-D2rogxEN.js +0 -1
- package/web/dist/assets/Dropdown-ZLS04O8p.js +0 -1
- package/web/dist/assets/FilePreview-B9xoU6ON.js +0 -2
- package/web/dist/assets/FilePreview-DUQpVZkH.js +0 -2
- package/web/dist/assets/GlobalSearchModal-BXEGvUZq.js +0 -1
- package/web/dist/assets/GlobalSearchModal-CnVPmfOI.js +0 -1
- package/web/dist/assets/GroupConversation-CyIpQzDd.js +0 -1
- package/web/dist/assets/GroupConversation-DgYV3uYS.js +0 -1
- package/web/dist/assets/ModelConfigModal-DDZccDYt.js +0 -2
- package/web/dist/assets/PiSetupModal-DHbqfx8T.js +0 -1
- package/web/dist/assets/ProviderOAuthControls-C-MK36Dk.js +0 -1
- package/web/dist/assets/SCMPanel-imYI2Ecw.js +0 -2
- package/web/dist/assets/SCMPanel-sVZcV7dR.js +0 -2
- package/web/dist/assets/SettingsModal-Xw1Yoi3_.js +0 -20
- package/web/dist/assets/SettingsModal-mb4e34-0.js +0 -20
- package/web/dist/assets/TerminalPanel-CLkJJvxg.js +0 -3
- package/web/dist/assets/TerminalPanel-DpcIellm.js +0 -3
- package/web/dist/assets/TuiOverlayModal-4-fFAZe7.js +0 -1
- package/web/dist/assets/TuiOverlayModal-DCu01lks.js +0 -1
- package/web/dist/assets/app-globals-DcyO0UPi.js +0 -1
- package/web/dist/assets/fi-DaSO0Q-m.js +0 -325
- package/web/dist/assets/index-Cdh3sibd.js +0 -85
- package/web/dist/assets/index-DDZMv6LO.css +0 -1
- package/web/dist/assets/index-DWw_SxRq.js +0 -85
- package/web/dist/assets/plugin-host-BRBg6zmQ.js +0 -1
- package/web/dist/assets/theme-DDl-Oy1b.js +0 -1
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { executionRoot, executionRootsOverlap } from "./execution-workspace.js";
|
|
2
|
+
import { DeviceTaskError } from "./task-error.js";
|
|
3
|
+
import { DEFAULT_EXECUTION_SETTINGS, validateExecutionSettings, } from "./execution-settings.js";
|
|
4
|
+
export class DeviceTaskScheduler {
|
|
5
|
+
static MAX_ROOM_HISTORY = 256;
|
|
6
|
+
settings;
|
|
7
|
+
starvationThresholdMs;
|
|
8
|
+
maxSkipsBeforeStarvation;
|
|
9
|
+
queue = [];
|
|
10
|
+
active = new Set();
|
|
11
|
+
roomDispatchHistory = new Map();
|
|
12
|
+
constructor(options) {
|
|
13
|
+
const validated = validateExecutionSettings(options?.initialSettings);
|
|
14
|
+
this.settings = validated.valid ? { ...validated.value } : { ...DEFAULT_EXECUTION_SETTINGS };
|
|
15
|
+
this.starvationThresholdMs = options?.starvationThresholdMs ?? 8000;
|
|
16
|
+
this.maxSkipsBeforeStarvation = options?.maxSkipsBeforeStarvation ?? 3;
|
|
17
|
+
}
|
|
18
|
+
getSettings() {
|
|
19
|
+
return { ...this.settings };
|
|
20
|
+
}
|
|
21
|
+
updateSettings(next) {
|
|
22
|
+
const validation = validateExecutionSettings(next);
|
|
23
|
+
if (!validation.valid) {
|
|
24
|
+
throw new Error(validation.error);
|
|
25
|
+
}
|
|
26
|
+
this.settings = { ...validation.value };
|
|
27
|
+
this.drain();
|
|
28
|
+
return { ...this.settings };
|
|
29
|
+
}
|
|
30
|
+
getActiveCount() {
|
|
31
|
+
return this.active.size;
|
|
32
|
+
}
|
|
33
|
+
getWaitingCount() {
|
|
34
|
+
return this.queue.length;
|
|
35
|
+
}
|
|
36
|
+
getBlockedCount() {
|
|
37
|
+
return [...this.active].filter((a) => !!a.blocked).length;
|
|
38
|
+
}
|
|
39
|
+
isWorkspaceBlocked(botId, root) {
|
|
40
|
+
const targetRoot = executionRoot(root);
|
|
41
|
+
for (const owner of this.active) {
|
|
42
|
+
if (owner.blocked && this.checkConflict({ botId, root: targetRoot }, owner)) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
checkConflict(a, b) {
|
|
49
|
+
return a.botId === b.botId || executionRootsOverlap(a.root, b.root);
|
|
50
|
+
}
|
|
51
|
+
recordRoomDispatch(roomId) {
|
|
52
|
+
let history = this.roomDispatchHistory.get(roomId);
|
|
53
|
+
if (!history) {
|
|
54
|
+
// Bounded LRU eviction
|
|
55
|
+
if (this.roomDispatchHistory.size >= DeviceTaskScheduler.MAX_ROOM_HISTORY) {
|
|
56
|
+
let oldestKey;
|
|
57
|
+
let oldestTime = Infinity;
|
|
58
|
+
for (const [k, v] of this.roomDispatchHistory) {
|
|
59
|
+
if (v.lastDispatchedAt < oldestTime) {
|
|
60
|
+
oldestTime = v.lastDispatchedAt;
|
|
61
|
+
oldestKey = k;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (oldestKey)
|
|
65
|
+
this.roomDispatchHistory.delete(oldestKey);
|
|
66
|
+
}
|
|
67
|
+
history = { lastDispatchedAt: 0, count: 0 };
|
|
68
|
+
}
|
|
69
|
+
history.lastDispatchedAt = Date.now();
|
|
70
|
+
history.count++;
|
|
71
|
+
this.roomDispatchHistory.set(roomId, history);
|
|
72
|
+
}
|
|
73
|
+
clearRoomHistory(roomId) {
|
|
74
|
+
this.roomDispatchHistory.delete(roomId);
|
|
75
|
+
}
|
|
76
|
+
enqueue(task, callbacks, signal) {
|
|
77
|
+
const canonicalRoot = executionRoot(task.root);
|
|
78
|
+
const candidate = { botId: task.botId, root: canonicalRoot };
|
|
79
|
+
// Check if active items have unconfirmed stop blocks
|
|
80
|
+
const blockedBlocker = [...this.active].find((owner) => owner.blocked && this.checkConflict(candidate, owner));
|
|
81
|
+
if (blockedBlocker) {
|
|
82
|
+
throw new DeviceTaskError("Task stop unconfirmed; workspace blocked until service restart");
|
|
83
|
+
}
|
|
84
|
+
// If queue is empty, slots available, and no conflict with active tasks,
|
|
85
|
+
// task can start immediately even if maxWaitingTasks is 0.
|
|
86
|
+
const canStartImmediately = this.queue.length === 0 &&
|
|
87
|
+
this.active.size < this.settings.maxActiveTasks &&
|
|
88
|
+
![...this.active].some((owner) => this.checkConflict(candidate, owner));
|
|
89
|
+
if (canStartImmediately) {
|
|
90
|
+
try {
|
|
91
|
+
callbacks.start();
|
|
92
|
+
this.recordRoomDispatch(task.roomId);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
callbacks.reject(error);
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
// Needs to queue, check queue capacity
|
|
100
|
+
if (this.queue.length >= this.settings.maxWaitingTasks) {
|
|
101
|
+
throw new DeviceTaskError("Local task queue full");
|
|
102
|
+
}
|
|
103
|
+
const item = {
|
|
104
|
+
taskId: task.id,
|
|
105
|
+
attemptId: task.attemptId,
|
|
106
|
+
roomId: task.roomId,
|
|
107
|
+
botId: task.botId,
|
|
108
|
+
botRevision: task.botRevision,
|
|
109
|
+
root: canonicalRoot,
|
|
110
|
+
queuedAt: Date.now(),
|
|
111
|
+
skipCount: 0,
|
|
112
|
+
signal,
|
|
113
|
+
start: callbacks.start,
|
|
114
|
+
reject: callbacks.reject,
|
|
115
|
+
cancel: callbacks.cancel,
|
|
116
|
+
};
|
|
117
|
+
this.queue.push(item);
|
|
118
|
+
this.drain();
|
|
119
|
+
return item;
|
|
120
|
+
}
|
|
121
|
+
removeWaiting(item) {
|
|
122
|
+
const index = this.queue.indexOf(item);
|
|
123
|
+
if (index >= 0) {
|
|
124
|
+
this.queue.splice(index, 1);
|
|
125
|
+
this.drain();
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
cancelTask(taskId, attemptId) {
|
|
131
|
+
// 1. Check waiting queue
|
|
132
|
+
const waitingIndex = this.queue.findIndex((q) => q.taskId === taskId && (!attemptId || q.attemptId === attemptId));
|
|
133
|
+
if (waitingIndex >= 0) {
|
|
134
|
+
const [item] = this.queue.splice(waitingIndex, 1);
|
|
135
|
+
item.cancel();
|
|
136
|
+
this.drain();
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
// 2. Check active running tasks
|
|
140
|
+
const activeItem = [...this.active].find((a) => a.taskId === taskId && (!attemptId || a.attemptId === attemptId));
|
|
141
|
+
if (activeItem && activeItem.controller && !activeItem.controller.signal.aborted) {
|
|
142
|
+
activeItem.controller.abort(new DeviceTaskError("Task cancelled by user"));
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
registerActive(task, controller) {
|
|
148
|
+
const canonicalRoot = executionRoot(task.root);
|
|
149
|
+
// Guard against duplicate task ID
|
|
150
|
+
const duplicate = [...this.active].find((item) => item.taskId === task.id);
|
|
151
|
+
if (duplicate) {
|
|
152
|
+
throw new DeviceTaskError("Task is already running");
|
|
153
|
+
}
|
|
154
|
+
const candidate = { botId: task.botId, root: canonicalRoot };
|
|
155
|
+
// Guard against capacity breach
|
|
156
|
+
if (this.active.size >= this.settings.maxActiveTasks) {
|
|
157
|
+
throw new DeviceTaskError("Active task capacity exceeded");
|
|
158
|
+
}
|
|
159
|
+
// Guard against active conflict
|
|
160
|
+
const conflict = [...this.active].find((item) => this.checkConflict(candidate, item));
|
|
161
|
+
if (conflict) {
|
|
162
|
+
throw new DeviceTaskError(conflict.blocked
|
|
163
|
+
? "Task stop unconfirmed; workspace blocked until service restart"
|
|
164
|
+
: "Active task workspace or bot conflict");
|
|
165
|
+
}
|
|
166
|
+
const item = {
|
|
167
|
+
taskId: task.id,
|
|
168
|
+
attemptId: task.attemptId,
|
|
169
|
+
roomId: task.roomId,
|
|
170
|
+
botId: task.botId,
|
|
171
|
+
botRevision: task.botRevision,
|
|
172
|
+
root: canonicalRoot,
|
|
173
|
+
startedAt: Date.now(),
|
|
174
|
+
controller,
|
|
175
|
+
};
|
|
176
|
+
this.active.add(item);
|
|
177
|
+
return item;
|
|
178
|
+
}
|
|
179
|
+
markBlocked(taskId, attemptId, error) {
|
|
180
|
+
for (const item of this.active) {
|
|
181
|
+
if (item.taskId === taskId && item.attemptId === attemptId) {
|
|
182
|
+
item.blocked = error;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
releaseActive(item) {
|
|
187
|
+
this.active.delete(item);
|
|
188
|
+
this.drain();
|
|
189
|
+
}
|
|
190
|
+
drain() {
|
|
191
|
+
if (this.queue.length === 0)
|
|
192
|
+
return;
|
|
193
|
+
const now = Date.now();
|
|
194
|
+
// Reject tasks blocked by unconfirmed stops
|
|
195
|
+
const toReject = [];
|
|
196
|
+
for (let i = this.queue.length - 1; i >= 0; i--) {
|
|
197
|
+
const entry = this.queue[i];
|
|
198
|
+
const blocker = [...this.active].find((owner) => owner.blocked && this.checkConflict(entry, owner));
|
|
199
|
+
if (blocker) {
|
|
200
|
+
this.queue.splice(i, 1);
|
|
201
|
+
toReject.push({
|
|
202
|
+
entry,
|
|
203
|
+
error: new DeviceTaskError("Task stop unconfirmed; workspace blocked until service restart"),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const { entry, error } of toReject) {
|
|
208
|
+
entry.reject(error);
|
|
209
|
+
}
|
|
210
|
+
if (this.queue.length === 0)
|
|
211
|
+
return;
|
|
212
|
+
let availableSlots = this.settings.maxActiveTasks - this.active.size;
|
|
213
|
+
if (availableSlots <= 0)
|
|
214
|
+
return;
|
|
215
|
+
// Identify schedulable candidates
|
|
216
|
+
// A candidate can physically start if:
|
|
217
|
+
// 1. No active task conflicts with it.
|
|
218
|
+
// 2. No earlier queued task conflicts with it (strict FIFO for the same bot/workspace).
|
|
219
|
+
const schedulable = [];
|
|
220
|
+
for (let i = 0; i < this.queue.length; i++) {
|
|
221
|
+
const entry = this.queue[i];
|
|
222
|
+
const hasActiveConflict = [...this.active].some((owner) => this.checkConflict(entry, owner));
|
|
223
|
+
if (hasActiveConflict)
|
|
224
|
+
continue;
|
|
225
|
+
const hasPriorQueueConflict = this.queue.slice(0, i).some((prior) => this.checkConflict(entry, prior));
|
|
226
|
+
if (hasPriorQueueConflict)
|
|
227
|
+
continue;
|
|
228
|
+
const isStarved = now - entry.queuedAt >= this.starvationThresholdMs || entry.skipCount >= this.maxSkipsBeforeStarvation;
|
|
229
|
+
schedulable.push({ entry, isStarved });
|
|
230
|
+
}
|
|
231
|
+
if (schedulable.length === 0)
|
|
232
|
+
return;
|
|
233
|
+
// Sort schedulable candidates:
|
|
234
|
+
// 1. Starved tasks come first (anti-starvation guarantee, oldest starved first).
|
|
235
|
+
// 2. For non-starved tasks: Room fair share (fewer active tasks in room first, then room last dispatched).
|
|
236
|
+
// 3. FIFO within same priority.
|
|
237
|
+
schedulable.sort((a, b) => {
|
|
238
|
+
if (a.isStarved && !b.isStarved)
|
|
239
|
+
return -1;
|
|
240
|
+
if (!a.isStarved && b.isStarved)
|
|
241
|
+
return 1;
|
|
242
|
+
if (a.isStarved && b.isStarved) {
|
|
243
|
+
return a.entry.queuedAt - b.entry.queuedAt;
|
|
244
|
+
}
|
|
245
|
+
// Room active task count
|
|
246
|
+
const activeA = [...this.active].filter((item) => item.roomId === a.entry.roomId).length;
|
|
247
|
+
const activeB = [...this.active].filter((item) => item.roomId === b.entry.roomId).length;
|
|
248
|
+
if (activeA !== activeB)
|
|
249
|
+
return activeA - activeB;
|
|
250
|
+
// Room dispatch history
|
|
251
|
+
const histA = this.roomDispatchHistory.get(a.entry.roomId);
|
|
252
|
+
const histB = this.roomDispatchHistory.get(b.entry.roomId);
|
|
253
|
+
const countA = histA?.count ?? 0;
|
|
254
|
+
const countB = histB?.count ?? 0;
|
|
255
|
+
if (countA !== countB)
|
|
256
|
+
return countA - countB;
|
|
257
|
+
const lastA = histA?.lastDispatchedAt ?? 0;
|
|
258
|
+
const lastB = histB?.lastDispatchedAt ?? 0;
|
|
259
|
+
if (lastA !== lastB)
|
|
260
|
+
return lastA - lastB;
|
|
261
|
+
return a.entry.queuedAt - b.entry.queuedAt;
|
|
262
|
+
});
|
|
263
|
+
// Select up to availableSlots
|
|
264
|
+
const toDispatch = [];
|
|
265
|
+
for (const cand of schedulable) {
|
|
266
|
+
if (availableSlots <= 0)
|
|
267
|
+
break;
|
|
268
|
+
// Ensure candidates chosen in this batch do not conflict with each other
|
|
269
|
+
const conflictsWithSelected = toDispatch.some((selected) => this.checkConflict(cand.entry, selected));
|
|
270
|
+
if (conflictsWithSelected)
|
|
271
|
+
continue;
|
|
272
|
+
toDispatch.push(cand.entry);
|
|
273
|
+
availableSlots--;
|
|
274
|
+
}
|
|
275
|
+
// Update skip counts for schedulable candidates that were passed over
|
|
276
|
+
for (const cand of schedulable) {
|
|
277
|
+
if (!toDispatch.includes(cand.entry)) {
|
|
278
|
+
cand.entry.skipCount++;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// Dispatch selected entries with try/catch error isolation
|
|
282
|
+
for (const entry of toDispatch) {
|
|
283
|
+
const index = this.queue.indexOf(entry);
|
|
284
|
+
if (index >= 0) {
|
|
285
|
+
this.queue.splice(index, 1);
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
entry.start();
|
|
289
|
+
this.recordRoomDispatch(entry.roomId);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
entry.reject(error);
|
|
293
|
+
this.drain();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
analyzeBlockReason(entry, queueIndex) {
|
|
298
|
+
// Check workspace blocked by unconfirmed stop
|
|
299
|
+
const blockedBlocker = [...this.active].find((owner) => owner.blocked && this.checkConflict(entry, owner));
|
|
300
|
+
if (blockedBlocker) {
|
|
301
|
+
return {
|
|
302
|
+
reason: "workspace_blocked",
|
|
303
|
+
detail: "Workspace is locked due to an unconfirmed task stop",
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
// Check bot busy in active
|
|
307
|
+
const botActive = [...this.active].find((owner) => owner.botId === entry.botId);
|
|
308
|
+
if (botActive) {
|
|
309
|
+
return {
|
|
310
|
+
reason: "bot_busy",
|
|
311
|
+
detail: "Target bot is currently running another task",
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
// Check workspace conflict in active
|
|
315
|
+
const workspaceActive = [...this.active].find((owner) => executionRootsOverlap(entry.root, owner.root));
|
|
316
|
+
if (workspaceActive) {
|
|
317
|
+
return {
|
|
318
|
+
reason: "workspace_conflict",
|
|
319
|
+
detail: "Workspace is currently in use by an active task",
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
// Check prior queue item conflicts (FIFO constraint)
|
|
323
|
+
const priorBotQueue = this.queue.slice(0, queueIndex).find((prior) => prior.botId === entry.botId);
|
|
324
|
+
if (priorBotQueue) {
|
|
325
|
+
return {
|
|
326
|
+
reason: "bot_busy",
|
|
327
|
+
detail: "Target bot has an earlier queued task waiting",
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
const priorWorkspaceQueue = this.queue
|
|
331
|
+
.slice(0, queueIndex)
|
|
332
|
+
.find((prior) => executionRootsOverlap(entry.root, prior.root));
|
|
333
|
+
if (priorWorkspaceQueue) {
|
|
334
|
+
return {
|
|
335
|
+
reason: "workspace_conflict",
|
|
336
|
+
detail: "Workspace has an earlier queued task waiting",
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
// Check concurrency limit
|
|
340
|
+
if (this.active.size >= this.settings.maxActiveTasks) {
|
|
341
|
+
return {
|
|
342
|
+
reason: "max_concurrency_reached",
|
|
343
|
+
detail: "Device concurrent task limit reached",
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
// Otherwise waiting for room fairness round
|
|
347
|
+
return {
|
|
348
|
+
reason: "room_fairness_yield",
|
|
349
|
+
detail: "Waiting for fair scheduling turn across rooms",
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
getSnapshot() {
|
|
353
|
+
const running = [];
|
|
354
|
+
const blocked = [];
|
|
355
|
+
for (const item of this.active) {
|
|
356
|
+
if (item.blocked) {
|
|
357
|
+
const reason = item.blocked && item.blocked.stopUnconfirmed ? "unconfirmed_stop" : "workspace_blocked";
|
|
358
|
+
blocked.push({
|
|
359
|
+
taskId: item.taskId,
|
|
360
|
+
attemptId: item.attemptId,
|
|
361
|
+
roomId: item.roomId,
|
|
362
|
+
botId: item.botId,
|
|
363
|
+
reason,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
running.push({
|
|
368
|
+
taskId: item.taskId,
|
|
369
|
+
attemptId: item.attemptId,
|
|
370
|
+
roomId: item.roomId,
|
|
371
|
+
botId: item.botId,
|
|
372
|
+
startedAt: item.startedAt,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const waiting = this.queue.map((item, index) => {
|
|
377
|
+
const { reason, detail } = this.analyzeBlockReason(item, index);
|
|
378
|
+
return {
|
|
379
|
+
taskId: item.taskId,
|
|
380
|
+
attemptId: item.attemptId,
|
|
381
|
+
roomId: item.roomId,
|
|
382
|
+
botId: item.botId,
|
|
383
|
+
queuedAt: item.queuedAt,
|
|
384
|
+
queuePosition: index + 1,
|
|
385
|
+
blockReason: reason,
|
|
386
|
+
blockReasonDetail: detail,
|
|
387
|
+
cancelable: true,
|
|
388
|
+
};
|
|
389
|
+
});
|
|
390
|
+
return {
|
|
391
|
+
settings: this.getSettings(),
|
|
392
|
+
activeCount: this.active.size,
|
|
393
|
+
waitingCount: this.queue.length,
|
|
394
|
+
blockedCount: blocked.length,
|
|
395
|
+
running,
|
|
396
|
+
waiting,
|
|
397
|
+
blocked,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
}
|