doukui 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.en.md +42 -0
- package/README.md +147 -2
- package/bin/commands/build.js +178 -0
- package/bin/commands/config.js +88 -0
- package/bin/commands/help.js +83 -0
- package/bin/commands/init.js +194 -0
- package/bin/commands/install.js +202 -0
- package/bin/commands/interactive.js +352 -0
- package/bin/commands/list.js +108 -0
- package/bin/commands/loop-server.js +357 -0
- package/bin/commands/loop.js +1398 -0
- package/bin/commands/migrate.js +120 -0
- package/bin/commands/setup-global.js +81 -0
- package/bin/commands/status.js +140 -0
- package/bin/commands/team-cleanup.js +190 -0
- package/bin/commands/validate-config.js +35 -0
- package/bin/commands/validate.js +21 -0
- package/bin/commands/version.js +22 -0
- package/bin/context.js +143 -0
- package/bin/doukui.js +216 -0
- package/package.json +47 -4
- package/plugins/contracts/capabilities.js +268 -0
- package/plugins/contracts/checkresult-schema.json +75 -0
- package/plugins/contracts/config-schema.json +179 -0
- package/plugins/contracts/plugin-interface.js +262 -0
- package/plugins/contracts/plugin-schema.json +104 -0
- package/plugins/core/hook-session-start/index.js +281 -0
- package/plugins/core/hook-session-start/plugin.json +7 -0
- package/plugins/core/hook-skill-gate/index.js +661 -0
- package/plugins/core/hook-skill-gate/plugin.json +12 -0
- package/plugins/core/provider-loop-engine/index.js +1397 -0
- package/plugins/core/provider-loop-engine/plugin.json +15 -0
- package/plugins/core/provider-memory-store/index.js +510 -0
- package/plugins/core/provider-memory-store/plugin.json +9 -0
- package/plugins/core/provider-role-registry/index.js +376 -0
- package/plugins/core/provider-role-registry/plugin.json +9 -0
- package/plugins/core/provider-state-store/index.js +244 -0
- package/plugins/core/provider-state-store/plugin.json +9 -0
- package/plugins/core/provider-watchdog/assets/daemon-config.template.json +32 -0
- package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +676 -0
- package/plugins/core/provider-watchdog/assets/watchdog.js +754 -0
- package/plugins/core/provider-watchdog/index.js +151 -0
- package/plugins/core/provider-watchdog/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +214 -0
- package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +249 -0
- package/plugins/core/skill-agent-dispatcher/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/roles-reference.md +253 -0
- package/plugins/core/skill-agent-dispatcher/skill.md +1291 -0
- package/plugins/core/skill-agentic-loop/plugin.json +20 -0
- package/plugins/core/skill-agentic-loop/skill.md +572 -0
- package/plugins/core/skill-batch-task-creator/plugin.json +15 -0
- package/plugins/core/skill-batch-task-creator/skill.md +620 -0
- package/plugins/core/skill-checklist/plugin.json +10 -0
- package/plugins/core/skill-checklist/skill.md +171 -0
- package/plugins/core/skill-completion-report/plugin.json +10 -0
- package/plugins/core/skill-completion-report/skill.md +331 -0
- package/plugins/core/skill-doukui-plan/plugin.json +15 -0
- package/plugins/core/skill-doukui-plan/skill.md +233 -0
- package/plugins/core/skill-doukui-sync/plugin.json +10 -0
- package/plugins/core/skill-doukui-sync/skill.md +76 -0
- package/plugins/core/skill-experience-logger/plugin.json +10 -0
- package/plugins/core/skill-experience-logger/skill.md +235 -0
- package/plugins/core/skill-human-checkpoint/plugin.json +10 -0
- package/plugins/core/skill-human-checkpoint/skill.md +194 -0
- package/plugins/core/skill-progress-tracker/plugin.json +10 -0
- package/plugins/core/skill-progress-tracker/skill.md +228 -0
- package/plugins/core/skill-role-manager/plugin.json +10 -0
- package/plugins/core/skill-role-manager/skill.md +252 -0
- package/plugins/core/skill-split-work-package/plugin.json +15 -0
- package/plugins/core/skill-split-work-package/skill.md +441 -0
- package/plugins/core/skill-task-archive/plugin.json +10 -0
- package/plugins/core/skill-task-archive/skill.md +116 -0
- package/plugins/core/skill-task-creator/plugin.json +15 -0
- package/plugins/core/skill-task-creator/skill.md +886 -0
- package/plugins/core/skill-team-cleanup/plugin.json +10 -0
- package/plugins/core/skill-team-cleanup/skill.md +266 -0
- package/plugins/core/skill-watchdog-manager/plugin.json +10 -0
- package/plugins/core/skill-watchdog-manager/skill.md +143 -0
- package/plugins/core/skill-workflow-orchestrator/plugin.json +13 -0
- package/plugins/core/skill-workflow-orchestrator/skill.md +273 -0
- package/plugins/core/validator-doc-sync/index.js +248 -0
- package/plugins/core/validator-doc-sync/plugin.json +12 -0
- package/plugins/core/validator-work-package/index.js +300 -0
- package/plugins/core/validator-work-package/plugin.json +12 -0
- package/plugins/plugin-registry.json +186 -0
- package/plugins/runtime/agent-def-generator.js +150 -0
- package/plugins/runtime/audit-logger.js +311 -0
- package/plugins/runtime/build-cli.js +89 -0
- package/plugins/runtime/checkresult-validator.js +131 -0
- package/plugins/runtime/claude-md-injector.js +159 -0
- package/plugins/runtime/cleanup-utils.js +293 -0
- package/plugins/runtime/config-manager.js +317 -0
- package/plugins/runtime/config-validator.js +320 -0
- package/plugins/runtime/context-window.js +81 -0
- package/plugins/runtime/event-bus.js +207 -0
- package/plugins/runtime/executor-claude.js +828 -0
- package/plugins/runtime/executor-default.js +448 -0
- package/plugins/runtime/executor-local.js +178 -0
- package/plugins/runtime/executor-sdk.js +390 -0
- package/plugins/runtime/harness-build.js +1037 -0
- package/plugins/runtime/hook-dispatcher.js +315 -0
- package/plugins/runtime/logger.js +192 -0
- package/plugins/runtime/loop-actuator.js +292 -0
- package/plugins/runtime/loop-coordinator.js +347 -0
- package/plugins/runtime/loop-defaults.js +29 -0
- package/plugins/runtime/loop-dispatch-batch.js +247 -0
- package/plugins/runtime/loop-executor.js +120 -0
- package/plugins/runtime/loop-report.js +327 -0
- package/plugins/runtime/loop-server-core.js +750 -0
- package/plugins/runtime/loop-snapshot.js +581 -0
- package/plugins/runtime/loop-trace.js +242 -0
- package/plugins/runtime/loop-worktree.js +404 -0
- package/plugins/runtime/manifest-resolver.js +457 -0
- package/plugins/runtime/multi-window-coordinator.js +560 -0
- package/plugins/runtime/objective-verifier.js +136 -0
- package/plugins/runtime/plan-reader.js +763 -0
- package/plugins/runtime/plugin-loader.js +798 -0
- package/plugins/runtime/plugin-trust.js +182 -0
- package/plugins/runtime/plugin-validator.js +491 -0
- package/plugins/runtime/provider-failover.js +114 -0
- package/plugins/runtime/provider-resolver.js +342 -0
- package/plugins/runtime/reflection-evaluator.js +540 -0
- package/plugins/runtime/resolve-plugin-path.js +297 -0
- package/plugins/runtime/safe-path.js +119 -0
- package/plugins/runtime/sandbox-context.js +235 -0
- package/plugins/runtime/sandbox-manager.js +609 -0
- package/plugins/runtime/sandbox-worker.js +215 -0
- package/plugins/runtime/settings-merger.js +172 -0
- package/plugins/runtime/state-store.js +527 -0
- package/plugins/runtime/team-cleanup.js +497 -0
- package/plugins/runtime/text-tokenize.js +25 -0
- package/plugins/runtime/validator-pipeline.js +475 -0
- package/plugins/runtime/yaml-parser.js +692 -0
- package/templates/harness-config.yaml +388 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Window Coordinator — Aggregates per-window states into a global session view
|
|
3
|
+
*
|
|
4
|
+
* @module multi-window-coordinator
|
|
5
|
+
*
|
|
6
|
+
* This module provides:
|
|
7
|
+
* - Data structure factories for multi-window-session.json, window state.json, heartbeat.json
|
|
8
|
+
* - Aggregation logic: reads windows/{id}/state.json + heartbeat.json, produces session-level view
|
|
9
|
+
* - Stage completion detection and global progress calculation
|
|
10
|
+
*
|
|
11
|
+
* Design doc: docs/reports/multi-window-monitoring-design.html (Section 5)
|
|
12
|
+
* Work package: WP-172-1-impl-a
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
var fs = require('fs');
|
|
18
|
+
var path = require('path');
|
|
19
|
+
|
|
20
|
+
// ─────────────────────────────────────────────
|
|
21
|
+
// Section 1: Data Structure Factories
|
|
22
|
+
// ─────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a multi-window-session.json template.
|
|
26
|
+
*
|
|
27
|
+
* @param {object} opts
|
|
28
|
+
* @param {string} opts.session_id - Unique session identifier (e.g. "mws-20260606-143000")
|
|
29
|
+
* @param {number} opts.total_windows - Number of windows in this session
|
|
30
|
+
* @param {Array<object>} [opts.stages] - Stage definitions
|
|
31
|
+
* @returns {object} Session skeleton conforming to the multi-window-session.json schema
|
|
32
|
+
*/
|
|
33
|
+
function createSessionSchema(opts) {
|
|
34
|
+
var now = new Date().toISOString();
|
|
35
|
+
return {
|
|
36
|
+
session_id: opts.session_id,
|
|
37
|
+
created_at: now,
|
|
38
|
+
updated_at: now,
|
|
39
|
+
status: 'active', // active | paused | completed | failed
|
|
40
|
+
total_windows: opts.total_windows || 0,
|
|
41
|
+
total_tasks: 0,
|
|
42
|
+
completed_tasks: 0,
|
|
43
|
+
failed_tasks: 0,
|
|
44
|
+
stages: opts.stages || [],
|
|
45
|
+
windows: {},
|
|
46
|
+
global_concurrency: {
|
|
47
|
+
max_total: 8, // cross-window max concurrent teamees
|
|
48
|
+
current_active: 0
|
|
49
|
+
},
|
|
50
|
+
stage_transitions: []
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create a windows/{win-id}/state.json template.
|
|
56
|
+
* Backward-compatible with dispatcher-state.json; adds window_id, session_id,
|
|
57
|
+
* current_stage, assigned_wps.
|
|
58
|
+
*
|
|
59
|
+
* @param {object} opts
|
|
60
|
+
* @param {string} opts.window_id - Window identifier (e.g. "win-1")
|
|
61
|
+
* @param {string} opts.session_id - Parent session ID
|
|
62
|
+
* @param {string} opts.team_name - Team name for this window
|
|
63
|
+
* @param {number} [opts.total_tasks=0] - Total tasks assigned to this window
|
|
64
|
+
* @returns {object} Window state skeleton
|
|
65
|
+
*/
|
|
66
|
+
function createWindowStateSchema(opts) {
|
|
67
|
+
return {
|
|
68
|
+
window_id: opts.window_id,
|
|
69
|
+
session_id: opts.session_id,
|
|
70
|
+
team_name: opts.team_name || '',
|
|
71
|
+
teamee_map: {},
|
|
72
|
+
wp_assignments: {},
|
|
73
|
+
start_time: new Date().toISOString(),
|
|
74
|
+
loop_iteration: 0,
|
|
75
|
+
processed_action_ids: [],
|
|
76
|
+
total_tasks: opts.total_tasks || 0,
|
|
77
|
+
status: 'monitoring', // monitoring | completed
|
|
78
|
+
max_batch_size: 5,
|
|
79
|
+
current_batch: [],
|
|
80
|
+
pending_batches: [],
|
|
81
|
+
global_pause_flag: false,
|
|
82
|
+
current_stage: opts.current_stage || null,
|
|
83
|
+
assigned_wps: opts.assigned_wps || []
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Create a windows/{win-id}/heartbeat.json template.
|
|
89
|
+
* Extends existing heartbeat.json with window_id field.
|
|
90
|
+
*
|
|
91
|
+
* @param {object} opts
|
|
92
|
+
* @param {string} opts.window_id - Window identifier
|
|
93
|
+
* @param {string} opts.session_id - Parent session / team name
|
|
94
|
+
* @param {number} [opts.pid=0] - OS process ID
|
|
95
|
+
* @param {string} opts.team_name - Team name
|
|
96
|
+
* @returns {object} Heartbeat skeleton
|
|
97
|
+
*/
|
|
98
|
+
function createWindowHeartbeatSchema(opts) {
|
|
99
|
+
return {
|
|
100
|
+
window_id: opts.window_id,
|
|
101
|
+
session_id: opts.session_id || '',
|
|
102
|
+
pid: opts.pid || 0,
|
|
103
|
+
team_name: opts.team_name || '',
|
|
104
|
+
loop_iteration: 0,
|
|
105
|
+
total_tasks: 0,
|
|
106
|
+
completed_tasks: 0,
|
|
107
|
+
in_progress_tasks: 0,
|
|
108
|
+
pending_tasks: 0,
|
|
109
|
+
last_update: new Date().toISOString(),
|
|
110
|
+
status: 'monitoring' // monitoring | shutting_down | completed
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ─────────────────────────────────────────────
|
|
115
|
+
// Section 2: Aggregation Logic
|
|
116
|
+
// ─────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Read and parse a JSON file. Returns null on any error.
|
|
120
|
+
* @param {string} filePath
|
|
121
|
+
* @returns {object|null}
|
|
122
|
+
*/
|
|
123
|
+
function readJsonSafe(filePath) {
|
|
124
|
+
try {
|
|
125
|
+
var content = fs.readFileSync(filePath, 'utf-8');
|
|
126
|
+
return JSON.parse(content);
|
|
127
|
+
} catch (_e) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* List subdirectory names under a directory. Returns [] on error.
|
|
134
|
+
* @param {string} dirPath
|
|
135
|
+
* @returns {string[]}
|
|
136
|
+
*/
|
|
137
|
+
function listSubdirs(dirPath) {
|
|
138
|
+
try {
|
|
139
|
+
return fs.readdirSync(dirPath, { withFileTypes: true })
|
|
140
|
+
.filter(function (d) { return d.isDirectory(); })
|
|
141
|
+
.map(function (d) { return d.name; });
|
|
142
|
+
} catch (_e) {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Aggregate all window states into a multi-window-session object.
|
|
149
|
+
*
|
|
150
|
+
* Reads windows/{win-id}/state.json and windows/{win-id}/heartbeat.json
|
|
151
|
+
* for every subdirectory under `windowsDir`, then computes global totals.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} windowsDir - Absolute path to the windows/ directory
|
|
154
|
+
* @param {object} [existingSession] - Optional existing session to update (preserves stages, transitions)
|
|
155
|
+
* @returns {object} Aggregated multi-window-session.json content
|
|
156
|
+
*/
|
|
157
|
+
function aggregateWindowStates(windowsDir, existingSession) {
|
|
158
|
+
var windowDirs = listSubdirs(windowsDir);
|
|
159
|
+
var now = new Date().toISOString();
|
|
160
|
+
|
|
161
|
+
var session = existingSession || createSessionSchema({
|
|
162
|
+
session_id: 'mws-' + now.replace(/[:.]/g, '').slice(0, 15),
|
|
163
|
+
total_windows: windowDirs.length
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
session.updated_at = now;
|
|
167
|
+
session.total_windows = windowDirs.length;
|
|
168
|
+
|
|
169
|
+
var globalTotalTasks = 0;
|
|
170
|
+
var globalCompleted = 0;
|
|
171
|
+
var globalFailed = 0;
|
|
172
|
+
var globalActive = 0;
|
|
173
|
+
|
|
174
|
+
// Reset windows map to reflect current scan
|
|
175
|
+
session.windows = {};
|
|
176
|
+
|
|
177
|
+
for (var i = 0; i < windowDirs.length; i++) {
|
|
178
|
+
var winId = windowDirs[i];
|
|
179
|
+
var winDir = path.join(windowsDir, winId);
|
|
180
|
+
|
|
181
|
+
var state = readJsonSafe(path.join(winDir, 'state.json'));
|
|
182
|
+
var heartbeat = readJsonSafe(path.join(winDir, 'heartbeat.json'));
|
|
183
|
+
|
|
184
|
+
// Build aggregated window entry
|
|
185
|
+
var winEntry = aggregateSingleWindow(winId, state, heartbeat);
|
|
186
|
+
session.windows[winId] = winEntry;
|
|
187
|
+
|
|
188
|
+
// Accumulate global counters from heartbeat (source of truth for task counts)
|
|
189
|
+
if (heartbeat) {
|
|
190
|
+
globalTotalTasks += (heartbeat.total_tasks || 0);
|
|
191
|
+
globalCompleted += (heartbeat.completed_tasks || 0);
|
|
192
|
+
// Count failed from state if available, otherwise 0
|
|
193
|
+
globalFailed += (state && state.failed_tasks) || 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (winEntry.status === 'active') {
|
|
197
|
+
globalActive += (heartbeat && heartbeat.in_progress_tasks) || 0;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
session.total_tasks = globalTotalTasks;
|
|
202
|
+
session.completed_tasks = globalCompleted;
|
|
203
|
+
session.failed_tasks = globalFailed;
|
|
204
|
+
session.global_concurrency.current_active = globalActive;
|
|
205
|
+
|
|
206
|
+
// Determine overall session status
|
|
207
|
+
session.status = computeSessionStatus(session, windowDirs);
|
|
208
|
+
|
|
209
|
+
return session;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Aggregate a single window's state + heartbeat into a summary entry.
|
|
214
|
+
*
|
|
215
|
+
* @param {string} winId
|
|
216
|
+
* @param {object|null} state - Parsed state.json
|
|
217
|
+
* @param {object|null} heartbeat - Parsed heartbeat.json
|
|
218
|
+
* @returns {object} Window summary for session.windows[winId]
|
|
219
|
+
*/
|
|
220
|
+
function aggregateSingleWindow(winId, state, heartbeat) {
|
|
221
|
+
var windowStatus = 'disconnected';
|
|
222
|
+
var error = null;
|
|
223
|
+
|
|
224
|
+
if (heartbeat) {
|
|
225
|
+
var lastUpdateMs = new Date(heartbeat.last_update).getTime();
|
|
226
|
+
var heartbeatAge = Date.now() - lastUpdateMs;
|
|
227
|
+
var STALE_THRESHOLD_MS = 120000; // 2 minutes
|
|
228
|
+
|
|
229
|
+
// B11: a malformed/missing last_update yields NaN age. NaN > THRESHOLD is
|
|
230
|
+
// false, so without this guard the window would be misclassified as
|
|
231
|
+
// alive. Treat NaN age as disconnected (safest — we have no valid signal).
|
|
232
|
+
if (isNaN(heartbeatAge) || heartbeatAge > STALE_THRESHOLD_MS) {
|
|
233
|
+
windowStatus = 'disconnected';
|
|
234
|
+
} else if (heartbeat.status === 'completed') {
|
|
235
|
+
windowStatus = 'completed';
|
|
236
|
+
} else if (heartbeat.in_progress_tasks > 0) {
|
|
237
|
+
windowStatus = 'active';
|
|
238
|
+
} else if (heartbeat.pending_tasks > 0) {
|
|
239
|
+
windowStatus = 'idle';
|
|
240
|
+
} else {
|
|
241
|
+
windowStatus = 'active';
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
window_id: winId,
|
|
247
|
+
pid: heartbeat ? heartbeat.pid : 0,
|
|
248
|
+
status: windowStatus,
|
|
249
|
+
current_stage: state ? state.current_stage : null,
|
|
250
|
+
assigned_wps: state ? (state.assigned_wps || []) : [],
|
|
251
|
+
heartbeat: heartbeat ? {
|
|
252
|
+
last_update: heartbeat.last_update,
|
|
253
|
+
loop_iteration: heartbeat.loop_iteration,
|
|
254
|
+
completed: heartbeat.completed_tasks || 0,
|
|
255
|
+
in_progress: heartbeat.in_progress_tasks || 0,
|
|
256
|
+
pending: heartbeat.pending_tasks || 0
|
|
257
|
+
} : null,
|
|
258
|
+
error: error
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Compute overall session status from aggregated data.
|
|
264
|
+
*
|
|
265
|
+
* @param {object} session
|
|
266
|
+
* @param {string[]} windowDirs
|
|
267
|
+
* @returns {string} 'active' | 'completed' | 'failed' | 'paused'
|
|
268
|
+
*/
|
|
269
|
+
function computeSessionStatus(session, windowDirs) {
|
|
270
|
+
if (session.status === 'paused') {
|
|
271
|
+
return 'paused';
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
var allCompleted = true;
|
|
275
|
+
var anyFailed = false;
|
|
276
|
+
var anyActive = false;
|
|
277
|
+
|
|
278
|
+
var winIds = Object.keys(session.windows);
|
|
279
|
+
for (var i = 0; i < winIds.length; i++) {
|
|
280
|
+
var w = session.windows[winIds[i]];
|
|
281
|
+
if (w.status !== 'completed') {
|
|
282
|
+
allCompleted = false;
|
|
283
|
+
}
|
|
284
|
+
if (w.status === 'failed') {
|
|
285
|
+
anyFailed = true;
|
|
286
|
+
}
|
|
287
|
+
if (w.status === 'active') {
|
|
288
|
+
anyActive = true;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (anyFailed) return 'failed';
|
|
293
|
+
if (allCompleted && winIds.length > 0) return 'completed';
|
|
294
|
+
if (anyActive) return 'active';
|
|
295
|
+
return 'active';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ─────────────────────────────────────────────
|
|
299
|
+
// Section 3: Stage Management
|
|
300
|
+
// ─────────────────────────────────────────────
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Check whether a stage is complete: all windows in the stage have
|
|
304
|
+
* completed all their assigned work_packages.
|
|
305
|
+
*
|
|
306
|
+
* @param {object} session - Aggregated session
|
|
307
|
+
* @param {number} stageId
|
|
308
|
+
* @returns {boolean}
|
|
309
|
+
*/
|
|
310
|
+
function isStageComplete(session, stageId) {
|
|
311
|
+
var stage = null;
|
|
312
|
+
for (var i = 0; i < session.stages.length; i++) {
|
|
313
|
+
if (session.stages[i].stage_id === stageId) {
|
|
314
|
+
stage = session.stages[i];
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (!stage) return false;
|
|
319
|
+
|
|
320
|
+
for (var j = 0; j < stage.windows.length; j++) {
|
|
321
|
+
var winId = stage.windows[j];
|
|
322
|
+
var winState = session.windows[winId];
|
|
323
|
+
if (!winState) return false;
|
|
324
|
+
|
|
325
|
+
// Check that all WPs assigned to this window for this stage are reflected
|
|
326
|
+
// as completed in the heartbeat
|
|
327
|
+
if (winState.heartbeat && winState.heartbeat.pending > 0) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
if (winState.heartbeat && winState.heartbeat.in_progress > 0) {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Find the currently active stage.
|
|
340
|
+
*
|
|
341
|
+
* @param {object} session
|
|
342
|
+
* @returns {object|null} The active stage, or null
|
|
343
|
+
*/
|
|
344
|
+
function findActiveStage(session) {
|
|
345
|
+
for (var i = 0; i < session.stages.length; i++) {
|
|
346
|
+
if (session.stages[i].status === 'active') {
|
|
347
|
+
return session.stages[i];
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Get the next stage after the given one.
|
|
355
|
+
*
|
|
356
|
+
* @param {object} session
|
|
357
|
+
* @param {number} currentStageId
|
|
358
|
+
* @returns {object|null}
|
|
359
|
+
*/
|
|
360
|
+
function getNextStage(session, currentStageId) {
|
|
361
|
+
for (var i = 0; i < session.stages.length; i++) {
|
|
362
|
+
if (session.stages[i].stage_id === currentStageId && i + 1 < session.stages.length) {
|
|
363
|
+
return session.stages[i + 1];
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Advance the session to the next stage. Marks current stage completed,
|
|
371
|
+
* next stage active, and records the transition.
|
|
372
|
+
*
|
|
373
|
+
* @param {object} session - Mutated in place
|
|
374
|
+
* @returns {boolean} true if advanced, false if no next stage
|
|
375
|
+
*/
|
|
376
|
+
function advanceStage(session) {
|
|
377
|
+
var active = findActiveStage(session);
|
|
378
|
+
if (!active) return false;
|
|
379
|
+
|
|
380
|
+
if (!isStageComplete(session, active.stage_id)) return false;
|
|
381
|
+
|
|
382
|
+
var now = new Date().toISOString();
|
|
383
|
+
active.status = 'completed';
|
|
384
|
+
active.completed_at = now;
|
|
385
|
+
|
|
386
|
+
var next = getNextStage(session, active.stage_id);
|
|
387
|
+
if (next) {
|
|
388
|
+
next.status = 'active';
|
|
389
|
+
next.started_at = now;
|
|
390
|
+
session.stage_transitions.push({
|
|
391
|
+
from_stage: active.stage_id,
|
|
392
|
+
to_stage: next.stage_id,
|
|
393
|
+
trigger: 'auto',
|
|
394
|
+
condition: 'all_windows_completed',
|
|
395
|
+
transitioned_at: now
|
|
396
|
+
});
|
|
397
|
+
return true;
|
|
398
|
+
} else {
|
|
399
|
+
// All stages done
|
|
400
|
+
session.status = 'completed';
|
|
401
|
+
session.stage_transitions.push({
|
|
402
|
+
from_stage: active.stage_id,
|
|
403
|
+
to_stage: null,
|
|
404
|
+
trigger: 'auto',
|
|
405
|
+
condition: 'all_stages_completed',
|
|
406
|
+
transitioned_at: now
|
|
407
|
+
});
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// ─────────────────────────────────────────────
|
|
413
|
+
// Section 4: Stage Transition Protocol
|
|
414
|
+
// ─────────────────────────────────────────────
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Find the stage entry that includes the given window_id.
|
|
418
|
+
*
|
|
419
|
+
* @param {object} session - Multi-window session
|
|
420
|
+
* @param {string} windowId - Window identifier
|
|
421
|
+
* @returns {object|null} The stage entry, or null
|
|
422
|
+
*/
|
|
423
|
+
function findStageForWindow(session, windowId) {
|
|
424
|
+
for (var i = 0; i < session.stages.length; i++) {
|
|
425
|
+
var stage = session.stages[i];
|
|
426
|
+
if (stage.windows && stage.windows.indexOf(windowId) !== -1) {
|
|
427
|
+
return stage;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Write a stage signal to a window's state.json file.
|
|
435
|
+
* Updates the current_stage field so the window's agent-dispatcher
|
|
436
|
+
* can detect stage transitions during its Phase 0 check.
|
|
437
|
+
*
|
|
438
|
+
* @param {string} windowsDir - Absolute path to the windows/ directory
|
|
439
|
+
* @param {string} windowId - Target window identifier
|
|
440
|
+
* @param {number} stageId - The stage ID to signal
|
|
441
|
+
* @param {Array<string>} [workPackages] - Optional work packages for the new stage
|
|
442
|
+
* @returns {boolean} true if written successfully
|
|
443
|
+
*/
|
|
444
|
+
function writeStageSignal(windowsDir, windowId, stageId, workPackages) {
|
|
445
|
+
var statePath = path.join(windowsDir, windowId, 'state.json');
|
|
446
|
+
var state = readJsonSafe(statePath);
|
|
447
|
+
if (!state) return false;
|
|
448
|
+
|
|
449
|
+
state.current_stage = stageId;
|
|
450
|
+
if (workPackages) {
|
|
451
|
+
state.assigned_wps = workPackages;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
try {
|
|
455
|
+
fs.writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf-8');
|
|
456
|
+
return true;
|
|
457
|
+
} catch (_e) {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// ─────────────────────────────────────────────
|
|
463
|
+
// Section 5: current_batch Persistence Fix
|
|
464
|
+
// ─────────────────────────────────────────────
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Ensure current_batch is correctly preserved in a state write-back object.
|
|
468
|
+
*
|
|
469
|
+
* The bug (wp-069-3 diagnostic): Phase B.5/C.5/D.5 state write-backs could
|
|
470
|
+
* lose the current_batch value if the variable was stale or reset. This
|
|
471
|
+
* function validates and returns the correct current_batch value.
|
|
472
|
+
*
|
|
473
|
+
* @param {*} currentBatch - The current_batch value from memory
|
|
474
|
+
* @param {object} stateFromDisk - The state read from disk (Phase 0 recovery)
|
|
475
|
+
* @returns {Array} The authoritative current_batch
|
|
476
|
+
*/
|
|
477
|
+
function resolveCurrentBatch(currentBatch, stateFromDisk) {
|
|
478
|
+
// If in-memory batch is a non-empty array, it takes priority
|
|
479
|
+
if (Array.isArray(currentBatch) && currentBatch.length > 0) {
|
|
480
|
+
return currentBatch;
|
|
481
|
+
}
|
|
482
|
+
// Fall back to disk value
|
|
483
|
+
if (stateFromDisk && Array.isArray(stateFromDisk.current_batch) && stateFromDisk.current_batch.length > 0) {
|
|
484
|
+
return stateFromDisk.current_batch;
|
|
485
|
+
}
|
|
486
|
+
// Both empty — this is valid (no active batch)
|
|
487
|
+
return Array.isArray(currentBatch) ? currentBatch : [];
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Build a complete state write-back payload that guarantees current_batch
|
|
492
|
+
* and pending_batches are never lost.
|
|
493
|
+
*
|
|
494
|
+
* @param {object} params
|
|
495
|
+
* @param {string} params.team_name
|
|
496
|
+
* @param {object} params.teamee_map
|
|
497
|
+
* @param {object} params.wp_assignments
|
|
498
|
+
* @param {string|Date} params.start_time
|
|
499
|
+
* @param {number} params.loop_iteration
|
|
500
|
+
* @param {string[]} params.processed_action_ids
|
|
501
|
+
* @param {number} params.total_tasks
|
|
502
|
+
* @param {string} params.status
|
|
503
|
+
* @param {number} params.max_batch_size
|
|
504
|
+
* @param {Array} params.current_batch
|
|
505
|
+
* @param {Array} params.pending_batches
|
|
506
|
+
* @param {boolean} params.global_pause_flag
|
|
507
|
+
* @returns {object} Complete state object ready for JSON serialization
|
|
508
|
+
*/
|
|
509
|
+
function buildStatePayload(params) {
|
|
510
|
+
return {
|
|
511
|
+
team_name: params.team_name,
|
|
512
|
+
teamee_map: params.teamee_map || {},
|
|
513
|
+
wp_assignments: params.wp_assignments || {},
|
|
514
|
+
start_time: params.start_time instanceof Date
|
|
515
|
+
? params.start_time.toISOString()
|
|
516
|
+
: params.start_time,
|
|
517
|
+
loop_iteration: params.loop_iteration,
|
|
518
|
+
processed_action_ids: params.processed_action_ids || [],
|
|
519
|
+
total_tasks: params.total_tasks,
|
|
520
|
+
status: params.status || 'monitoring',
|
|
521
|
+
max_batch_size: params.max_batch_size,
|
|
522
|
+
current_batch: Array.isArray(params.current_batch) ? params.current_batch : [],
|
|
523
|
+
pending_batches: Array.isArray(params.pending_batches) ? params.pending_batches : [],
|
|
524
|
+
global_pause_flag: params.global_pause_flag || false
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// ─────────────────────────────────────────────
|
|
529
|
+
// Exports
|
|
530
|
+
// ─────────────────────────────────────────────
|
|
531
|
+
|
|
532
|
+
module.exports = {
|
|
533
|
+
// Data structure factories
|
|
534
|
+
createSessionSchema: createSessionSchema,
|
|
535
|
+
createWindowStateSchema: createWindowStateSchema,
|
|
536
|
+
createWindowHeartbeatSchema: createWindowHeartbeatSchema,
|
|
537
|
+
|
|
538
|
+
// Aggregation
|
|
539
|
+
aggregateWindowStates: aggregateWindowStates,
|
|
540
|
+
aggregateSingleWindow: aggregateSingleWindow,
|
|
541
|
+
computeSessionStatus: computeSessionStatus,
|
|
542
|
+
|
|
543
|
+
// Stage management
|
|
544
|
+
isStageComplete: isStageComplete,
|
|
545
|
+
findActiveStage: findActiveStage,
|
|
546
|
+
getNextStage: getNextStage,
|
|
547
|
+
advanceStage: advanceStage,
|
|
548
|
+
|
|
549
|
+
// Stage transition protocol
|
|
550
|
+
findStageForWindow: findStageForWindow,
|
|
551
|
+
writeStageSignal: writeStageSignal,
|
|
552
|
+
|
|
553
|
+
// current_batch fix
|
|
554
|
+
resolveCurrentBatch: resolveCurrentBatch,
|
|
555
|
+
buildStatePayload: buildStatePayload,
|
|
556
|
+
|
|
557
|
+
// Internal helpers (exposed for testing)
|
|
558
|
+
readJsonSafe: readJsonSafe,
|
|
559
|
+
listSubdirs: listSubdirs
|
|
560
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// 客观交叉验证(Phase D-2)
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// LLM 自报 checklist 与客观信号不一致时以客观为准。checklist item 可声明
|
|
7
|
+
// verify: { command, expectExit?, timeoutMs? }——本模块实跑 command,
|
|
8
|
+
// 退出码不符则把该项改判 fail(即使 LLM 自报 passed),并同步重算
|
|
9
|
+
// summary/passed 与 failedItems,保证下游 evaluator 消费一致结构。
|
|
10
|
+
//
|
|
11
|
+
// 设计约束:
|
|
12
|
+
// - 仅对显式声明 verify 的 item 执行(无 verify 的 item 不受影响,零回归)
|
|
13
|
+
// - 命令执行容错:exec 抛错(非零退出以 error 对象携带 code)→ 按 exitCode 归类
|
|
14
|
+
// - execFn 可注入(DI-over-mocking;默认 child_process.execFileSync)
|
|
15
|
+
// - cwd 默认 runCwd(worktree 感知,与 executor 的执行目录一致)
|
|
16
|
+
// - 超时默认 120s,防止 verify 命令挂死
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
var cp = require('child_process');
|
|
20
|
+
|
|
21
|
+
var DEFAULT_VERIFY_TIMEOUT_MS = 120000;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 实跑一个 verify 声明。
|
|
25
|
+
* @param {object} verify { command, expectExit?, timeoutMs? }
|
|
26
|
+
* @param {object} [opts] { cwd, execFn }
|
|
27
|
+
* @returns {{ ok: boolean, exitCode: number|null, output: string }}
|
|
28
|
+
*/
|
|
29
|
+
function runVerifyCommand(verify, opts) {
|
|
30
|
+
opts = opts || {};
|
|
31
|
+
var execFn = opts.execFn || function (cmd, runOpts) {
|
|
32
|
+
return cp.execSync(cmd, runOpts);
|
|
33
|
+
};
|
|
34
|
+
var expectExit = typeof verify.expectExit === 'number' ? verify.expectExit : 0;
|
|
35
|
+
var timeoutMs = typeof verify.timeoutMs === 'number' ? verify.timeoutMs : DEFAULT_VERIFY_TIMEOUT_MS;
|
|
36
|
+
|
|
37
|
+
var exitCode = null;
|
|
38
|
+
var output = '';
|
|
39
|
+
try {
|
|
40
|
+
output = String(execFn(verify.command, {
|
|
41
|
+
cwd: opts.cwd,
|
|
42
|
+
timeout: timeoutMs,
|
|
43
|
+
encoding: 'utf8',
|
|
44
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
45
|
+
}));
|
|
46
|
+
exitCode = 0; // execSync 无抛错 = 退出码 0
|
|
47
|
+
} catch (e) {
|
|
48
|
+
// execFileSync 非零退出抛错:e.status 是退出码;被信号杀 = e.signal
|
|
49
|
+
exitCode = (typeof e.status === 'number') ? e.status : null;
|
|
50
|
+
output = String((e.stdout || '') + (e.stderr || '')).slice(0, 2000);
|
|
51
|
+
}
|
|
52
|
+
return { ok: exitCode === expectExit, exitCode: exitCode, output: output };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 对 CheckResult 的 categories[].items[].verify 做客观验证并改判。
|
|
57
|
+
* @param {object} chk normalizeCheckResult 后(或 raw)的 CheckResult(原地修改并返回)
|
|
58
|
+
* @param {object} [opts] { cwd, execFn }
|
|
59
|
+
* @returns {{ chk: object, verified: number, overridden: number }}
|
|
60
|
+
*/
|
|
61
|
+
function applyObjectiveVerification(chk, opts) {
|
|
62
|
+
opts = opts || {};
|
|
63
|
+
var verified = 0;
|
|
64
|
+
var overridden = 0;
|
|
65
|
+
|
|
66
|
+
if (!chk || typeof chk !== 'object' || !Array.isArray(chk.categories)) {
|
|
67
|
+
return { chk: chk, verified: verified, overridden: overridden };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
var failedItems = Array.isArray(chk.failedItems) ? chk.failedItems : [];
|
|
71
|
+
var totalDelta = 0;
|
|
72
|
+
var passedDelta = 0;
|
|
73
|
+
|
|
74
|
+
for (var c = 0; c < chk.categories.length; c++) {
|
|
75
|
+
var cat = chk.categories[c];
|
|
76
|
+
if (!cat || !Array.isArray(cat.items)) continue;
|
|
77
|
+
for (var i = 0; i < cat.items.length; i++) {
|
|
78
|
+
var item = cat.items[i];
|
|
79
|
+
if (!item || !item.verify || typeof item.verify.command !== 'string') continue;
|
|
80
|
+
|
|
81
|
+
verified++;
|
|
82
|
+
var res = runVerifyCommand(item.verify, opts);
|
|
83
|
+
if (res.ok) {
|
|
84
|
+
// 客观通过:若 LLM 自报 fail 而客观 pass,不改判(自报 fail 保守,无谎报风险)
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 客观不符但 LLM 自报 passed → 谎报,硬改判
|
|
89
|
+
if (item.passed === true) {
|
|
90
|
+
item.passed = false;
|
|
91
|
+
item.reason = 'objective verify failed: ' + item.verify.command +
|
|
92
|
+
' → exit ' + res.exitCode + ' (expect ' +
|
|
93
|
+
(typeof item.verify.expectExit === 'number' ? item.verify.expectExit : 0) + ')' +
|
|
94
|
+
(res.output ? ' | ' + res.output.slice(0, 200) : '');
|
|
95
|
+
overridden++;
|
|
96
|
+
// 类别 passed 联动
|
|
97
|
+
cat.passed = false;
|
|
98
|
+
failedItems.push({
|
|
99
|
+
id: item.id || ('verify-' + verified),
|
|
100
|
+
category: cat.name || 'objective',
|
|
101
|
+
text: item.text || item.verify.command,
|
|
102
|
+
reason: item.reason,
|
|
103
|
+
});
|
|
104
|
+
totalDelta++;
|
|
105
|
+
// passedDelta 不加(改判 fail)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// summary 重算(基于改判后的 items;仅当存在被改判项时重算,避免扰动自报统计)
|
|
111
|
+
if (overridden > 0) {
|
|
112
|
+
var allItems = [];
|
|
113
|
+
for (var c2 = 0; c2 < chk.categories.length; c2++) {
|
|
114
|
+
if (chk.categories[c2] && Array.isArray(chk.categories[c2].items)) {
|
|
115
|
+
allItems = allItems.concat(chk.categories[c2].items);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
var passedCount = allItems.filter(function (it) { return it && it.passed === true; }).length;
|
|
119
|
+
chk.summary = {
|
|
120
|
+
total: allItems.length,
|
|
121
|
+
passed: passedCount,
|
|
122
|
+
failed: allItems.length - passedCount,
|
|
123
|
+
};
|
|
124
|
+
chk.passed = passedCount === allItems.length && allItems.length > 0;
|
|
125
|
+
chk.failedItems = failedItems;
|
|
126
|
+
chk._objective = { verified: verified, overridden: overridden };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { chk: chk, verified: verified, overridden: overridden };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = {
|
|
133
|
+
applyObjectiveVerification: applyObjectiveVerification,
|
|
134
|
+
runVerifyCommand: runVerifyCommand,
|
|
135
|
+
_DEFAULT_VERIFY_TIMEOUT_MS: DEFAULT_VERIFY_TIMEOUT_MS,
|
|
136
|
+
};
|