1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
// module-predictor.js — Predictive resource allocation engine
// v2 (2026-10-23): accuracy is measured ONLY over predictions that were paired with a
// real outcome. v1 counted every observed run in the denominator while incrementing the
// numerator only when an old `predict` record happened to match, so the dashboard read 0%.
// Commands: predict, predictor-status, predictor-config
class PredictorModule {
constructor() {
this.commands = {};
this.terminal = null;
this._dataPoints = []; // observed runs — the training set for _findSimilar
this._pending = []; // predictions waiting for an outcome (bounded, TTL'd)
this._predictions = 0;
this._accuracy = { correct: 0, total: 0 }; // total = paired comparisons only
this._errorSum = 0; // SUM of raw |predicted-observed|/observed (mean = /paired)
this._turnsErr = 0; // sum of |predicted turns - observed turns|
this._CONFIDENCE_THRESHOLD = 0.5;
this._MIN_DATA_POINTS = 5;
this._PAIR_TTL_MS = 15 * 60 * 1000;
this._SCHEMA = 2;
this._loaded = false;
}
getHelpInfo() {
return {
'predict': 'predict <task description> — Predict task duration, tool turns, and confidence',
'predictor-status': 'predictor-status — Paired accuracy, pending predictions, data points',
'predictor-config': 'predictor-config <key> <value> — Tune: confidence-threshold, min-data-points, pair-ttl',
};
}
registerCommands() {
this.commands['predict'] = (args) => this.predict(args.join(' '));
this.commands['predictor-status'] = () => this.showStatus();
this.commands['predictor-config'] = (args) => this.setConfig(args);
}
init(terminal) {
this.terminal = terminal;
this._loadFromVFS();
this._storeMode = 'cache';
// read-through after boot: a stale cache revalidates against the canonical file
setTimeout(() => { this._pullFromServer().catch(function () {}); }, 1500);
this._loaded = true;
this.terminal.print('📊 Predictor loaded — analyzing task patterns', 'info');
}
// ─── Prediction Engine ───
async predict(task) {
if (!task) return this.terminal.print('Usage: predict <task description>', 'error');
const features = this._extractFeatures(task);
const similar = this._findSimilar(features);
const prediction = this._computePrediction(similar, features);
// Register it as pending so the next matching run can be scored against it.
const pending = this.notePrediction(task, 'cli');
pending.similarCount = similar.length;
this.terminal.print('📈 Task Prediction', 'header');
this.terminal.print(` Task: ${task.substring(0, 50)}${task.length > 50 ? '...' : ''}`, 'info');
if (prediction.confidence >= this._CONFIDENCE_THRESHOLD) {
this.terminal.print(` ⏱ Estimated duration: ${prediction.duration}s`, 'info');
this.terminal.print(` 🔧 Estimated tool turns: ${prediction.toolTurns}`, 'info');
this.terminal.print(` 📊 Confidence: ${(prediction.confidence * 100).toFixed(0)}% (based on ${similar.length} similar tasks)`, 'success');
} else if (similar.length > 0) {
this.terminal.print(` ⏱ Estimated duration: ${prediction.duration}s (low confidence)`, 'warning');
this.terminal.print(` 🔧 Tool turns: ~${prediction.toolTurns}`, 'warning');
this.terminal.print(` 📊 Confidence: ${(prediction.confidence * 100).toFixed(0)}% — only ${similar.length} similar data point(s)`, 'warning');
this.terminal.print(' Tip: More task data will improve predictions.', 'info');
} else {
this.terminal.print(' ❓ No historical data to base prediction on.', 'warning');
this.terminal.print(' Tip: Run `orchestrate` with various tasks to build a baseline.', 'info');
this.terminal.print(' Default estimate: ~30s duration, ~5 tool turns', 'info');
}
if (prediction.confidence >= this._CONFIDENCE_THRESHOLD && prediction.duration > 30) {
this.terminal.print(` 💡 Recommendation: Set TTL to ${Math.ceil(prediction.duration * 1.5)}s for predictable completion`, 'info');
}
this.terminal.print(` 🎯 Tracked as ${pending.id} — the next matching run scores it`, 'info');
return prediction;
}
_extractFeatures(task) {
const lower = String(task || '').toLowerCase();
return {
length: String(task || '').length,
wordCount: String(task || '').split(/\s+/).length,
hasSearch: /\b(search|find|look)\b/.test(lower),
hasWrite: /\b(write|create|save|edit)\b/.test(lower),
hasDelete: /\b(delete|remove|rm)\b/.test(lower),
hasInstall: /\b(install|wpm|package)\b/.test(lower),
hasRead: /\b(read|cat|view|list)\b/.test(lower),
hasWeb: /\b(fetch|url|http|web)\b/.test(lower),
hasOrchestrate: /\b(orchestrat|delegate|agent)\b/.test(lower),
hasModule: /\b(module|edit_module|view_module)\b/.test(lower),
hasSync: /\b(sync|recall|remember|previous session|state sync)\b/.test(lower),
complexity: Math.min(String(task || '').length / 50, 10), // 0-10 scale
keywords: [...new Set(lower.split(/\s+/).filter(w => w.length > 3))].slice(0, 10)
};
}
// Same scoring _findSimilar uses, exposed so pairing can reuse it (v2).
_similarity(a, b) {
if (!a || !b) return 0;
const keys = ['hasSearch', 'hasWrite', 'hasDelete', 'hasInstall', 'hasRead',
'hasWeb', 'hasOrchestrate', 'hasModule', 'hasSync'];
const matches = keys.filter(k => a[k] === b[k]).length / keys.length;
const kw = a.keywords || [];
const overlap = kw.filter(k => (b.keywords || []).includes(k)).length;
const kScore = overlap / Math.max(kw.length, 1);
return matches * 0.6 + kScore * 0.4;
}
_findSimilar(features) {
return this._dataPoints.filter(dp => {
if (!dp.observedDuration) return false; // outcomes only, never predictions
if (dp.usable === false) return false; // aborted runs are not evidence
if (!(dp.observedDuration >= 1.5)) return false; // belt and braces: never evidence
if (dp.features) return this._similarity(features, dp.features) > 0.3;
return false;
});
}
_computePrediction(similar, features) {
if (similar.length === 0) {
let baseDuration = 15;
let baseTurns = 3;
if (features.hasSearch || features.hasWeb) { baseDuration = 25; baseTurns = 5; }
if (features.hasWrite) { baseDuration = 20; baseTurns = 4; }
if (features.hasOrchestrate) { baseDuration = 40; baseTurns = 8; }
if (features.hasSync) { baseDuration = 30; baseTurns = 6; }
if (features.complexity > 5) baseDuration *= 1.5;
return {
duration: baseDuration,
toolTurns: baseTurns,
confidence: 0.1,
similarCount: 0,
floored: false
};
}
const avgDuration = similar.reduce((s, dp) => s + (dp.observedDuration || 30), 0) / similar.length;
const avgTurns = similar.reduce((s, dp) => s + (dp.observedTurns || 3), 0) / similar.length;
// v2: the documented knob now actually scales confidence (v1 hardcoded 20).
const minPts = Math.max(1, this._MIN_DATA_POINTS || 5);
const confidence = Math.min(similar.length / minPts, 1) * 0.8 + 0.1;
const complexityMultiplier = 1 + (features.complexity - 3) * 0.1;
const adjustedDuration = avgDuration * Math.max(complexityMultiplier, 0.5);
const floored = adjustedDuration < 1; // no prediction is "0 seconds"
const duration = Math.max(1, Math.round(adjustedDuration));
return {
duration,
toolTurns: Math.max(1, Math.round(avgTurns)),
confidence: Math.min(confidence, 0.95),
similarCount: similar.length,
floored
};
}
// ─── Prediction ↔ Outcome pairing (v2) ───
// Task text never leaves the module: only a stable hash is stored/serialised.
_hashTask(t) {
const s = String(t || '').trim().toLowerCase().replace(/\s+/g, ' ').slice(0, 200);
let h = 0x811c9dc5;
for (let i = 0; i < s.length; i++) {
h ^= s.charCodeAt(i);
h = (h * 0x01000193) >>> 0;
}
return h.toString(16);
}
// Register a prediction to be scored later. Returns the pending record (with .id).
notePrediction(task, source) {
const features = this._extractFeatures(task || '');
const p = this._computePrediction(this._findSimilar(features), features);
const rec = {
id: 'p' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
taskHash: this._hashTask(task),
features,
predictedDuration: p.duration,
predictedTurns: p.toolTurns,
confidence: p.confidence,
floored: p.floored === true,
source: source || 'loop',
ts: Date.now()
};
this._pending.push(rec);
if (this._pending.length > 50) this._pending = this._pending.slice(-50);
this._predictions++;
this._debounceSave();
return rec;
}
_takePending(features, opts) {
const now = Date.now();
this._pending = this._pending.filter(p => p && p.ts && (now - p.ts) <= this._PAIR_TTL_MS);
const taskHash = opts.task ? this._hashTask(opts.task) : null;
let idx = -1;
if (opts.predictionId) idx = this._pending.findIndex(p => p.id === opts.predictionId);
if (idx < 0 && taskHash) {
for (let i = this._pending.length - 1; i >= 0; i--) {
if (this._pending[i].taskHash === taskHash) { idx = i; break; }
}
}
if (idx < 0) {
// Fallback only for a manual `predict` (source 'cli'): a tool/loop prediction must be
// matched by id or exact task, never by shape alone (nine equal booleans score 0.6).
let best = -1, bestScore = 0;
for (let i = 0; i < this._pending.length; i++) {
const cand = this._pending[i];
if (cand.source !== 'cli') continue;
const shared = (features.keywords || []).filter(k => (cand.features?.keywords || []).includes(k)).length;
if (shared === 0) continue;
const s = this._similarity(features, cand.features);
if (s > bestScore) { bestScore = s; best = i; }
}
if (bestScore >= 0.5) idx = best;
}
return idx >= 0 ? this._pending.splice(idx, 1)[0] : null;
}
// ─── Learning from Results ───
recordResult(features, duration, toolTurns, success, opts) {
const o = opts || {};
const pred = this._takePending(features, o);
const observed = Number(duration) || 0;
const rec = {
features,
observedDuration: observed,
observedTurns: toolTurns,
success: !!success,
paired: !!pred,
timestamp: new Date().toISOString()
};
rec.usable = observed >= 1.5; // aborted/instant runs say nothing about duration
if (pred) {
const absErr = Math.abs(pred.predictedDuration - observed);
rec.predictedDuration = pred.predictedDuration;
rec.predictedTurns = pred.predictedTurns;
// RAW error — the renderer clamps, the store does not (a 475x miss must stay 475x)
rec.errorPct = observed > 0 ? absErr / observed : null;
rec.turnsError = Math.abs((pred.predictedTurns || 0) - (toolTurns || 0));
rec.scored = rec.usable && rec.errorPct !== null && pred.floored !== true;
if (rec.scored) {
this._accuracy.total++;
this._errorSum += rec.errorPct;
this._turnsErr += rec.turnsError;
// 30% relative, with a 3 s floor so rounding on short tasks does not flip the verdict
if (absErr <= Math.max(0.30 * observed, 3)) this._accuracy.correct++;
}
}
this._dataPoints.push(rec);
if (this._dataPoints.length > 500) this._dataPoints = this._dataPoints.slice(-500);
if (pred) this._saveNow(); else this._debounceSave();
return rec;
}
// ─── Commands ───
showStatus() {
const actualExecutions = this._dataPoints.filter(dp => dp.observedDuration && dp.usable !== false).length;
const paired = this._accuracy.total;
const unpaired = this._dataPoints.filter(dp => dp.paired === false).length;
const accPct = paired > 0 ? ((this._accuracy.correct / paired) * 100).toFixed(0) + '%' : 'n/a';
const meanErr = paired > 0 ? this._errorSum / paired : 0;
const mape = paired > 0
? ` · mean error ${(Math.min(meanErr, 5) * 100).toFixed(0)}%${meanErr > 5 ? '+' : ''}` +
` · turns ±${(this._turnsErr / paired).toFixed(1)}`
: '';
const unscored = this._dataPoints.filter(dp => dp.scored === false).length;
this.terminal.print('📊 Predictor Status', 'header');
this.terminal.print(` Total data points: ${this._dataPoints.length}`, 'info');
this.terminal.print(` Actual executions: ${actualExecutions}`, 'info');
this.terminal.print(` Predictions made: ${this._predictions}`, 'info');
this.terminal.print(` Accuracy: ${accPct} (${this._accuracy.correct}/${paired} paired)${mape}`, 'info');
this.terminal.print(` Awaiting outcome: ${this._pending.length}`, 'info');
this.terminal.print(` Unpaired runs: ${unpaired}`, 'info');
this.terminal.print(` Unscored pairs: ${unscored} (aborted run or floored prediction)`, 'info');
this.terminal.print(` Store: ${this._storeMode || 'cache'} — server file canonical, browser copy is a ${Math.round(this._STORE_TTL_MS / 60000)} min read-through cache`, 'info');
this.terminal.print(` Confidence threshold: ${this._CONFIDENCE_THRESHOLD}`, 'info');
this.terminal.print(` Min data points: ${this._MIN_DATA_POINTS}`, 'info');
this.terminal.print(` Pair TTL: ${Math.round(this._PAIR_TTL_MS / 60000)}m`, 'info');
}
setConfig(args) {
if (args.length < 2) return this.terminal.print('Usage: predictor-config <key> <value>', 'error');
const key = args[0];
const val = args[1];
const configMap = {
'confidence-threshold': () => {
const n = parseFloat(val);
if (isNaN(n) || n < 0 || n > 1) return 'Must be 0.0-1.0';
this._CONFIDENCE_THRESHOLD = n;
return `confidence-threshold set to ${n}`;
},
'min-data-points': () => {
const n = parseInt(val);
if (isNaN(n) || n < 1) return 'Must be >= 1';
this._MIN_DATA_POINTS = n;
return `min-data-points set to ${n}`;
},
'pair-ttl': () => {
const n = parseInt(val);
if (isNaN(n) || n < 60) return 'Must be >= 60 (seconds)';
this._PAIR_TTL_MS = n * 1000;
return `pair-ttl set to ${n}s`;
},
};
if (!configMap[key]) return this.terminal.print(`Unknown: ${key}. Valid: ${Object.keys(configMap).join(', ')}`, 'error');
const msg = configMap[key]();
this.terminal.print(`✅ ${msg}`, 'success');
}
// ─── Persistence ───
// v4.6.68 (decision 2026-10-23): the SERVER file is canonical, the browser copy is a
// TTL'd read-through cache written through on save. Identity is the user's own WebDAV
// space — no new endpoint, no new credential, nothing unauthenticated added.
_STORE_TTL_MS = 300000;
_wdConfig() {
try {
const raw = localStorage.getItem('webx11_webdav_config');
if (!raw) return null;
const c = JSON.parse(raw);
if (!c || !c.url || String(c.url).indexOf('/spaces/') === -1) return null;
return {
base: String(c.url).replace(/\/+$/, ''),
user: c.user || c.username || '',
pass: c.pass || c.password || ''
};
} catch (e) { return null; }
}
_storeUrl(cfg) { return cfg.base + '/_data/predictor.json'; }
// The VFS write fails silently when the parent directory is missing (writeFile does not
// create parents), so the cache is written through one guarded path that proves itself.
_writeCache(obj) {
const fs = this.terminal.fs;
if (!fs.readDir('/system/metrics')) fs.mkdirRecursive('/system/metrics');
fs.fsWriteJSON('/system/metrics/predictor.json', obj);
if (!fs.readFile('/system/metrics/predictor.json')) {
this._storeMode = 'cache-only (cache write failed)';
if (!this._writeWarned) {
this._writeWarned = true;
this.terminal.print('📊 Predictor: could not write its cache file — /system/metrics is not writable in this VFS', 'error');
}
return false;
}
return true;
}
_storeHeaders(cfg) {
const h = { 'Content-Type': 'application/json' };
if (cfg.user) h.Authorization = 'Basic ' + btoa(cfg.user + ':' + cfg.pass);
return h;
}
_loadFromVFS() {
try {
const fs = this.terminal.fs;
if (!fs) return;
const data = fs.fsReadJSON('/system/metrics/predictor.json');
if (!data) return;
// Legacy (pre-v2) points carry no `usable`. Backfill it from the measurement — an
// aborted run must not enter similarity as evidence (measured: a 0.079 s point
// dragged avgDuration to ~0 and floored every prediction at 1 s).
this._dataPoints = (data.dataPoints || [])
.filter(dp => dp && dp.observedDuration)
.map(dp => {
if (typeof dp.usable !== 'boolean') dp.usable = dp.observedDuration >= 1.5;
return dp;
})
.filter(dp => dp.usable !== false);
this._predictions = data.predictions || 0;
if (data.v === this._SCHEMA) {
const acc = data.accuracy || {};
this._accuracy = { correct: acc.correct || 0, total: acc.total || 0 };
this._errorSum = (typeof data.errorSum === 'number') ? data.errorSum : (data.mape || 0);
this._turnsErr = data.turnsErr || 0;
const cfg = data.config || {};
if (typeof cfg.confidenceThreshold === 'number') this._CONFIDENCE_THRESHOLD = cfg.confidenceThreshold;
if (typeof cfg.minDataPoints === 'number') this._MIN_DATA_POINTS = cfg.minDataPoints;
if (typeof cfg.pairTtlMs === 'number') this._PAIR_TTL_MS = cfg.pairTtlMs;
const now = Date.now();
this._pending = (data.pending || []).filter(p => p && p.ts && (now - p.ts) <= this._PAIR_TTL_MS);
} else {
// v1 counted every observation in `total` but only some in `correct` → 0%.
this._accuracy = { correct: 0, total: 0 };
this._errorSum = 0;
this._pending = [];
}
} catch (e) {
console.warn('[Predictor] Load error (fresh start):', e.message);
}
}
_saveNow() {
try {
const fs = this.terminal.fs;
if (!fs) return;
if (!fs.readDir('/system/metrics')) fs.mkdirRecursive('/system/metrics');
fs.fsWriteJSON('/system/metrics/predictor.json', {
v: this._SCHEMA,
cachedAt: Date.now(),
storeMode: 'cache',
dataPoints: this._dataPoints.slice(-500),
predictions: this._predictions,
accuracy: this._accuracy,
errorSum: this._errorSum,
turnsErr: this._turnsErr,
config: {
confidenceThreshold: this._CONFIDENCE_THRESHOLD,
minDataPoints: this._MIN_DATA_POINTS,
pairTtlMs: this._PAIR_TTL_MS
},
pending: this._pending.slice(-50)
});
this._debouncePush();
} catch (e) {
console.error('[Predictor] Save error:', e.message);
}
}
_debouncePush() {
clearTimeout(this._pushTimer);
this._pushTimer = setTimeout(() => this._pushToServer(), 5000);
}
async _pushToServer() {
const cfg = this._wdConfig();
if (!cfg) { this._storeMode = 'cache-only (no WebDAV config)'; return false; }
try {
const body = this.terminal.fs.readFile('/system/metrics/predictor.json') || '{}';
const put = () => fetch(this._storeUrl(cfg), { method: 'PUT', headers: this._storeHeaders(cfg), body: body });
let resp = await put();
// first push into a fresh space: the collection has to exist before the file can
if (resp.status === 409 || resp.status === 404) {
try { await fetch(cfg.base + '/_data', { method: 'MKCOL', headers: this._storeHeaders(cfg) }); } catch (e) {}
resp = await put();
}
if (resp.ok) { this._storeMode = 'server'; this._serverStamp = Date.now(); return true; }
this._storeMode = 'cache-only (PUT ' + resp.status + ')';
return false;
} catch (e) {
this._storeMode = 'cache-only (offline)';
return false;
}
}
// Read-through: the cache wins while fresh; the server wins when the two differ.
async _pullFromServer(force) {
const cfg = this._wdConfig();
if (!cfg) { this._storeMode = 'cache-only (no WebDAV config)'; return false; }
if (!force && this._cacheStamp && (Date.now() - this._cacheStamp) < this._STORE_TTL_MS) return false;
try {
const resp = await fetch(this._storeUrl(cfg), { headers: this._storeHeaders(cfg) });
if (resp.status === 404) { this._storeMode = 'server (empty)'; return false; }
if (!resp.ok) { this._storeMode = 'cache-only (GET ' + resp.status + ')'; return false; }
const remote = await resp.json();
if (!remote || typeof remote !== 'object') return false;
const localPts = (this._dataPoints || []).length;
const localPaired = (this._accuracy || {}).total || 0;
const remotePts = (remote.dataPoints || []).length;
const remotePaired = (remote.accuracy || {}).total || 0;
const diverge = remotePts !== localPts || remotePaired !== localPaired;
remote.cachedAt = Date.now();
remote.storeMode = 'server';
if (!this._writeCache(remote)) return false;
this._cacheStamp = Date.now();
this._serverStamp = Date.now();
this._loadFromVFS();
this._storeMode = 'server';
if (diverge && !this._divergenceNoted) {
this._divergenceNoted = true;
this.terminal.print(`📊 Predictor store: server copy differs from the local cache (server ${remotePts} pts / ${remotePaired} paired, cache ${localPts} / ${localPaired}) — server wins`, 'warning');
}
return true;
} catch (e) {
this._storeMode = 'cache-only (offline)';
return false;
}
}
_debounceSave() {
clearTimeout(this._saveTimer);
this._saveTimer = setTimeout(() => this._saveNow(), 3000);
}
}
// Required for module system
if (typeof window !== 'undefined') {
window.PredictorModule = PredictorModule;
}
|