tr-plugin-meta-harness 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/README.md CHANGED
@@ -1,3 +1,65 @@
1
- # Temporary Holding Version
1
+ # Command Fleet
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ All nine adapters share one plugin: Claude Code, Codex, Antigravity, Cursor,
4
+ DSH, Grok, Muse, Pi and Hermes. `runtime <agent> ...` exposes their native runtime
5
+ commands; DSH observations and Hermes gateway management live here as well.
6
+
7
+ `total-recall cli-agents setup` connects all nine native instruction surfaces.
8
+ The declared setup operation also runs during daemon-owned plugin refresh.
9
+ Every dispatched agent receives a freshly checked Total Recall context, complete
10
+ core help, installed plugin operations and composed commands. Setup failure or
11
+ an unready context stops dispatch.
12
+
13
+ `docs-refresh`, `docs-ensure`, `docs-status`, `docs-read` and `docs-search` manage
14
+ downloaded official documentation rather than a list of links. Content is hashed,
15
+ bounded and refreshed after 24 hours. Maintenance uses the existing Total Recall
16
+ daemon's plugin task, refreshing one stale adapter per run. Failed pages remain
17
+ visible as failures and cannot certify complete coverage; successful cached
18
+ content remains readable. `setup --node <recorded-node>` selects maintenance
19
+ placement without creating a second scheduler.
20
+
21
+ Standalone Total Recall owner for CLI-agent discovery and explicit mesh command execution. Install with `total-recall plugin install <path-or-private-git-url>`. Installation rebuilds the plugin inventory, CLI library and connected instruction surfaces. No model account is created or selected automatically.
22
+
23
+ ```sh
24
+ total-recall cli-agents catalog --json
25
+ total-recall cli-agents nodes --json
26
+ total-recall cli-agents inspect --node <recorded-node> --json
27
+ total-recall cli-agents run-command --stdin --json < request.json
28
+ ```
29
+
30
+ `request.json` selects one installed plugin operation, its literal arguments, a node and an agent:
31
+
32
+ ```json
33
+ {
34
+ "pluginId": "git-sentinel",
35
+ "subcommand": "status",
36
+ "args": [],
37
+ "execution": {
38
+ "agent": "codex",
39
+ "node": "local",
40
+ "permissions": { "read": true, "write": false, "send": false, "destructive": false }
41
+ }
42
+ }
43
+ ```
44
+
45
+ For remote nodes, supply `execution.cwd` as an explicit absolute workspace. `agent: "direct"` runs the actual installed Total Recall CLI without a model. Remote access comes from recorded mesh entities and verified SSH configuration; unavailable nodes or binaries produce errors. Secrets are never accepted in an agent prompt. Existing account metadata is inspected without printing credentials or substituting provider API keys.
46
+
47
+ The four permissions are strict boolean **command-effect admission controls**. They allow or deny the selected plugin command before dispatch. They are not a network policy or an OS sandbox for every action a model might propose. Supported adapter write modes are scoped through the vendor CLI; vendor approval and sandbox defaults stay enabled. The host's authenticated authorization still applies.
48
+
49
+ A completed model turn is distinct from a completed plugin operation. Structured results return `commandExecutionVerified: false` for agent turns; only direct CLI execution returns a command exit receipt. Muse preserves its actual headless JSONL log and documented process exit status rather than treating session-protocol notifications as headless output. Do not infer working accounts, provider delivery or successful mutations from discovery or fixture tests.
50
+
51
+ ## Primary contracts checked 2026-10-14
52
+
53
+ | Adapter | Contract |
54
+ | --- | --- |
55
+ | Claude Code | [Headless](https://code.claude.com/docs/en/headless) |
56
+ | Codex | [Non-interactive mode](https://developers.openai.com/codex/noninteractive) |
57
+ | Antigravity | [Headless CLI](https://antigravity.google/docs/cli/headless/) |
58
+ | Cursor | [CLI parameters](https://docs.cursor.com/en/cli/reference/parameters) |
59
+ | DSH | [Official CLI source](https://github.com/deepseek-ai/deepseek-harness/blob/master/apps/cli/README.md) |
60
+ | Grok | [Headless scripting](https://docs.x.ai/build/cli/headless-scripting) |
61
+ | Muse | [Headless and CI](https://dev.meta.ai/docs/muse-code/extending) |
62
+ | Pi | [Official CLI reference](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/docs/cli.md) |
63
+ | Hermes | [Official command reference](https://github.com/NousResearch/hermes-agent/blob/main/website/docs/reference/cli-commands.md) |
64
+
65
+ Cursor discovery uses `cursor-agent`; an ambiguous `agent` binary is not assumed to be Cursor. Missing installations are listed as unavailable. Unknown authentication requires explicit agent selection and remains unverified until a real run. Tests exercise literal stdin, bounded child-process cleanup, command admission, recorded SSH access, account checks and completed-result parsing. Run suites on the configured sanctioned test node.
@@ -0,0 +1,59 @@
1
+ # Meta Harness Capability Brick
2
+
3
+ A zero-dependency, host-free pure ES module providing universal agent harness discovery, two-stage complexity classification, cost-vs-frontier routing, gate cascade evaluation, and execution envelope construction.
4
+
5
+ ## Why This Brick Exists
6
+
7
+ Modern software engineering with autonomous AI harnesses suffers from two structural traps:
8
+ 1. **The Correction Penalty**: Sub-frontier models on complex architectural tasks burn more tokens retrying than frontier models take in a single shot.
9
+ 2. **Confirmation Bias**: An agent that writes flawed code often passes its own validation review unless evaluated by a separate model family.
10
+
11
+ This brick encapsulates the core deterministic intelligence needed to route, coordinate, and score agent harnesses without binding your application to any host runtime or secrets store.
12
+
13
+ ## Installation / Vendoring
14
+
15
+ When vendoring into another repository using Total Recall:
16
+ ```bash
17
+ total-recall plugin vendor meta-harness
18
+ ```
19
+
20
+ Or import the capability directly in any standard ES module environment:
21
+ ```javascript
22
+ import {
23
+ createMetaHarness,
24
+ ADAPTER_CATALOG,
25
+ classifyTaskComplexity,
26
+ selectOptimalHarness,
27
+ evaluateGateCascade,
28
+ computeEmpiricalScorecard
29
+ } from './capability/index.mjs';
30
+ ```
31
+
32
+ ## Core API
33
+
34
+ ### `classifyTaskComplexity(taskDescription, options)`
35
+ Analyzes instructions for architectural indicators, security keywords, blast radius, and length:
36
+ ```javascript
37
+ const complexity = classifyTaskComplexity("Refactor auth system to WebAuthn with test verification");
38
+ // Returns: { score: 95, tier: 'frontier', isFrontierRecommended: true, factors: [...] }
39
+ ```
40
+
41
+ ### `selectOptimalHarness(taskDescription, availableHarnesses, quotaStatus)`
42
+ Computes cost-vs-quality utility across available CLI runners:
43
+ ```javascript
44
+ const selection = selectOptimalHarness("Fix typo in README.md", ['agy', 'dsh', 'ollama']);
45
+ // Returns: { selected: 'ollama', complexity: { tier: 'local' }, candidates: [...] }
46
+ ```
47
+
48
+ ### `evaluateGateCascade(currentHarness, gateResults)`
49
+ FrugalGPT gate cascade: keeps cheap models on simple tasks, escalating only upon verified failure:
50
+ ```javascript
51
+ const cascade = evaluateGateCascade('ollama', { testsPassed: false, linterPassed: true });
52
+ // Returns: { escalationRequired: true, recommendedHarness: 'dsh', reason: '...' }
53
+ ```
54
+
55
+ ### `computeEmpiricalScorecard(historyRuns)`
56
+ Aggregates pass rates, failure frequencies, and average latencies across runs:
57
+ ```javascript
58
+ const scorecard = computeEmpiricalScorecard(runs);
59
+ ```
@@ -0,0 +1,534 @@
1
+ /**
2
+ * Meta Harness Capability Brick
3
+ * Zero-dependency, host-free pure ES module for agent routing,
4
+ * complexity classification, adapter discovery, gate cascades, and SSSS receipts.
5
+ * Works identically in any standalone app, edge device, workstation, or cloud environment.
6
+ */
7
+
8
+ export const ADAPTER_CATALOG = {
9
+ agy: {
10
+ id: 'agy',
11
+ name: 'Google Antigravity CLI',
12
+ bin: 'agy',
13
+ family: 'google',
14
+ tier: 'frontier',
15
+ billingModel: 'subscription',
16
+ speed: 'fast',
17
+ costScore: 6,
18
+ qualityScore: 9.2,
19
+ contextLimit: 1048576,
20
+ modalities: ['text', 'code', 'image', 'audio', 'video'],
21
+ features: ['subagents', 'mcp', 'headless', 'worktrees', 'live_api'],
22
+ flags: { prompt: '-p', nonInteractive: '--mode accept-edits', planMode: '--mode plan', output: '--output-format json' }
23
+ },
24
+ claude: {
25
+ id: 'claude',
26
+ name: 'Anthropic Claude Code',
27
+ bin: 'claude',
28
+ family: 'anthropic',
29
+ tier: 'frontier',
30
+ billingModel: 'subscription',
31
+ speed: 'balanced',
32
+ costScore: 8,
33
+ qualityScore: 9.5,
34
+ contextLimit: 200000,
35
+ modalities: ['text', 'code'],
36
+ features: ['subagents', 'mcp', 'headless', 'compact', 'plan_mode'],
37
+ flags: { prompt: '-p', nonInteractive: '--permission-mode acceptEdits', planMode: '--permission-mode plan', output: '--output-format json' }
38
+ },
39
+ codex: {
40
+ id: 'codex',
41
+ name: 'OpenAI Codex CLI',
42
+ bin: 'codex',
43
+ family: 'openai',
44
+ tier: 'frontier',
45
+ billingModel: 'subscription',
46
+ speed: 'balanced',
47
+ costScore: 8,
48
+ qualityScore: 9.3,
49
+ contextLimit: 128000,
50
+ modalities: ['text', 'code'],
51
+ features: ['mcp', 'headless', 'streaming'],
52
+ flags: { prompt: 'exec', nonInteractive: '--sandbox workspace-write', planMode: '--sandbox read-only', output: '--json' }
53
+ },
54
+ grok: {
55
+ id: 'grok',
56
+ name: 'xAI Grok CLI',
57
+ bin: 'grok',
58
+ family: 'xai',
59
+ tier: 'frontier',
60
+ billingModel: 'subscription',
61
+ speed: 'fast',
62
+ costScore: 6,
63
+ qualityScore: 8.8,
64
+ contextLimit: 131072,
65
+ modalities: ['text', 'code', 'image'],
66
+ features: ['headless', 'search_live'],
67
+ flags: { prompt: '-p', nonInteractive: '--yes', output: '--output-format json' }
68
+ },
69
+ ollama: {
70
+ id: 'ollama',
71
+ name: 'Ollama Local Runtime',
72
+ bin: 'ollama',
73
+ family: 'local-open-weights',
74
+ tier: 'local',
75
+ billingModel: 'local',
76
+ speed: 'ultra-fast',
77
+ costScore: 0,
78
+ qualityScore: 7.5,
79
+ contextLimit: 32768,
80
+ modalities: ['text', 'code'],
81
+ features: ['offline', 'zero_cost', 'pipe_stdin', 'gpu_accelerated'],
82
+ flags: { prompt: 'run', nonInteractive: '', output: '' }
83
+ },
84
+ hermes: {
85
+ id: 'hermes',
86
+ name: 'Hermes Agent Runtime',
87
+ bin: 'hermes',
88
+ family: 'nous-open-weights',
89
+ tier: 'local',
90
+ billingModel: 'local',
91
+ speed: 'fast',
92
+ costScore: 1,
93
+ qualityScore: 8.2,
94
+ contextLimit: 65536,
95
+ modalities: ['text', 'code'],
96
+ features: ['gateway', 'mesh_dispatch', 'tool_calling'],
97
+ flags: { prompt: 'chat', nonInteractive: '--oneshot', output: '--format stream-json' }
98
+ },
99
+ dsh: {
100
+ id: 'dsh',
101
+ name: 'DeepSeek Harness',
102
+ bin: 'dsh',
103
+ family: 'deepseek',
104
+ tier: 'budget',
105
+ billingModel: 'metered',
106
+ speed: 'balanced',
107
+ costScore: 2,
108
+ qualityScore: 8.9,
109
+ contextLimit: 65536,
110
+ modalities: ['text', 'code'],
111
+ features: ['cost_efficient', 'deep_reasoning', 'mesh_routing'],
112
+ flags: { prompt: '-', nonInteractive: '--profile headless', output: '--json' }
113
+ },
114
+ openclaw: {
115
+ id: 'openclaw',
116
+ name: 'OpenClaw Multi-Agent Gateway',
117
+ bin: 'openclaw',
118
+ family: 'multi-provider',
119
+ tier: 'specialized',
120
+ billingModel: 'metered',
121
+ speed: 'balanced',
122
+ costScore: 5,
123
+ qualityScore: 8.4,
124
+ contextLimit: 65536,
125
+ modalities: ['text', 'code'],
126
+ features: ['multi_channel', 'skill_orchestration'],
127
+ flags: { prompt: 'agent', nonInteractive: '--message', output: '--json' }
128
+ },
129
+ aider: {
130
+ id: 'aider',
131
+ name: 'Aider Pair Programmer',
132
+ bin: 'aider',
133
+ family: 'multi-provider',
134
+ tier: 'budget',
135
+ billingModel: 'metered',
136
+ speed: 'balanced',
137
+ costScore: 4,
138
+ qualityScore: 8.7,
139
+ contextLimit: 128000,
140
+ modalities: ['text', 'code'],
141
+ features: ['git_repo_map', 'lint_auto_fix'],
142
+ flags: { prompt: '--message', nonInteractive: '--yes', output: '' }
143
+ },
144
+ goose: {
145
+ id: 'goose',
146
+ name: 'Block Goose Developer Agent',
147
+ bin: 'goose',
148
+ family: 'multi-provider',
149
+ tier: 'specialized',
150
+ billingModel: 'metered',
151
+ speed: 'balanced',
152
+ costScore: 5,
153
+ qualityScore: 8.3,
154
+ contextLimit: 65536,
155
+ modalities: ['text', 'code'],
156
+ features: ['mcp', 'tool_calling'],
157
+ flags: { prompt: 'run', nonInteractive: '--text', output: '' }
158
+ },
159
+ openhands: {
160
+ id: 'openhands',
161
+ name: 'OpenHands Autonomous SWE',
162
+ bin: 'openhands',
163
+ family: 'multi-provider',
164
+ tier: 'frontier',
165
+ billingModel: 'metered',
166
+ speed: 'deliberate',
167
+ costScore: 7,
168
+ qualityScore: 9.0,
169
+ contextLimit: 128000,
170
+ modalities: ['text', 'code'],
171
+ features: ['sandboxed_docker', 'swe_bench_optimized'],
172
+ flags: { prompt: '-t', nonInteractive: '--headless', output: '--json' }
173
+ },
174
+ cline: {
175
+ id: 'cline',
176
+ name: 'Cline Autonomous CLI',
177
+ bin: 'cline',
178
+ family: 'multi-provider',
179
+ tier: 'specialized',
180
+ billingModel: 'metered',
181
+ speed: 'balanced',
182
+ costScore: 5,
183
+ qualityScore: 8.5,
184
+ contextLimit: 128000,
185
+ modalities: ['text', 'code'],
186
+ features: ['mcp', 'terminal_integration'],
187
+ flags: { prompt: 'task', nonInteractive: '-y', output: '--json' }
188
+ },
189
+ muse: {
190
+ id: 'muse',
191
+ name: 'Meta Muse Code',
192
+ bin: 'muse',
193
+ family: 'meta',
194
+ tier: 'specialized',
195
+ billingModel: 'subscription',
196
+ speed: 'fast',
197
+ costScore: 4,
198
+ qualityScore: 8.4,
199
+ contextLimit: 128000,
200
+ modalities: ['text', 'code'],
201
+ features: ['headless', 'jsonl_events'],
202
+ flags: { prompt: 'exec', nonInteractive: '--json', output: '--json' }
203
+ },
204
+ pi: {
205
+ id: 'pi',
206
+ name: 'Pi Coding Agent',
207
+ bin: 'pi',
208
+ family: 'multi-provider',
209
+ tier: 'budget',
210
+ billingModel: 'metered',
211
+ speed: 'fast',
212
+ costScore: 3,
213
+ qualityScore: 8.3,
214
+ contextLimit: 128000,
215
+ modalities: ['text', 'code'],
216
+ features: ['minimal_tools', 'headless_json'],
217
+ flags: { prompt: '--print', nonInteractive: '--mode json', output: '--mode json' }
218
+ }
219
+ };
220
+
221
+ /**
222
+ * Validate custom or dynamically loaded adapter manifest
223
+ */
224
+ export function validateAdapterManifest(adapter) {
225
+ if (!adapter || typeof adapter !== 'object') {
226
+ return { valid: false, errors: ['Adapter manifest must be an object'] };
227
+ }
228
+ const errors = [];
229
+ if (!adapter.name || typeof adapter.name !== 'string') errors.push('Missing string field: name');
230
+ const bin = adapter.bin || adapter.binary;
231
+ if (!bin || typeof bin !== 'string') errors.push('Missing string field: bin (or binary)');
232
+ if (typeof adapter.costScore !== 'number') errors.push('Missing numeric field: costScore');
233
+ if (typeof adapter.qualityScore !== 'number') errors.push('Missing numeric field: qualityScore');
234
+
235
+ return {
236
+ valid: errors.length === 0,
237
+ errors
238
+ };
239
+ }
240
+
241
+ /**
242
+ * Deterministic Complexity Classifier (Jev RouteLLM heuristic)
243
+ * Scores prompt on: multi-file keywords, architectural decisions,
244
+ * security boundaries, math/crypto, and instruction surface length.
245
+ */
246
+ export function classifyTaskComplexity(taskDescription, options = {}) {
247
+ const text = String(taskDescription || '').toLowerCase();
248
+ let score = 0;
249
+ const factors = [];
250
+
251
+ if (/refactor|architect|redesign|migrate|framework|unif/.test(text)) {
252
+ score += 35;
253
+ factors.push('architectural-scope');
254
+ }
255
+ if (/auth|crypto|fido2|passkey|secret|credential|token|sandbox|isolation/.test(text)) {
256
+ score += 30;
257
+ factors.push('security-sensitive');
258
+ }
259
+ if (/multi-file|across (all|every)|entire (repo|fleet)|worktree/.test(text)) {
260
+ score += 25;
261
+ factors.push('broad-blast-radius');
262
+ }
263
+ if (/benchmark|telemetry|concurrency|race condition|memory leak/.test(text)) {
264
+ score += 20;
265
+ factors.push('complex-debugging');
266
+ }
267
+ if (/typo|rename|docstring|comment|formatting|readme|lint fix/.test(text)) {
268
+ score -= 30;
269
+ factors.push('low-complexity-edit');
270
+ }
271
+
272
+ const words = text.trim() ? text.trim().split(/\s+/).length : 0;
273
+ if (words > 120) {
274
+ score += 20;
275
+ factors.push('long-instruction-surface');
276
+ } else if (words < 15) {
277
+ score -= 10;
278
+ factors.push('terse-instruction');
279
+ }
280
+
281
+ if (options.preferLocal) {
282
+ score -= 40;
283
+ factors.push('prefer-local-override');
284
+ }
285
+ if (options.preferFrontier) {
286
+ score += 50;
287
+ factors.push('prefer-frontier-override');
288
+ }
289
+
290
+ const bounded = Math.max(0, Math.min(100, 50 + score));
291
+ let tier = 'budget';
292
+ if (bounded >= 70) {
293
+ tier = 'frontier';
294
+ } else if (bounded <= 35) {
295
+ tier = 'local';
296
+ }
297
+
298
+ return {
299
+ score: bounded,
300
+ tier,
301
+ factors,
302
+ isFrontierRecommended: tier === 'frontier',
303
+ isLocalRecommended: tier === 'local'
304
+ };
305
+ }
306
+
307
+ /**
308
+ * Resolve normalized adapter specification from string ID or object
309
+ */
310
+ function resolveAdapterEntry(h, catalog = ADAPTER_CATALOG) {
311
+ if (typeof h === 'string') {
312
+ return catalog[h] || {
313
+ id: h,
314
+ bin: h,
315
+ name: h,
316
+ family: 'custom',
317
+ tier: 'budget',
318
+ qualityScore: 6.0,
319
+ costScore: 4.0
320
+ };
321
+ }
322
+ const id = h.id || h.bin || h.binary || 'custom';
323
+ const base = catalog[id] || {};
324
+ return {
325
+ ...base,
326
+ ...h,
327
+ id,
328
+ bin: h.bin || h.binary || base.bin || id,
329
+ name: h.name || base.name || id,
330
+ family: h.family || base.family || 'custom',
331
+ tier: h.tier || base.tier || 'budget',
332
+ qualityScore: typeof h.qualityScore === 'number' ? h.qualityScore : (base.qualityScore ?? 6.0),
333
+ costScore: typeof h.costScore === 'number' ? h.costScore : (base.costScore ?? 4.0)
334
+ };
335
+ }
336
+
337
+ /**
338
+ * Select optimal harness based on complexity score, actual installed availability,
339
+ * quota headroom, and optional empirical scorecard pass rates.
340
+ */
341
+ export function selectOptimalHarness(taskDescription, availableHarnesses = [], quotaStatus = {}, options = {}) {
342
+ const catalog = options.catalog || ADAPTER_CATALOG;
343
+ const empiricalScorecard = options.empiricalScorecard || {};
344
+ const complexity = classifyTaskComplexity(taskDescription, options);
345
+
346
+ const pool = availableHarnesses.length > 0
347
+ ? availableHarnesses.map(h => resolveAdapterEntry(h, catalog))
348
+ : Object.values(catalog);
349
+
350
+ // Filter out harnesses that are explicitly reported as exhausted or rate-limited (>95% measured usage)
351
+ // or local models when host memory is critically low
352
+ const viable = pool.filter(h => {
353
+ const q = quotaStatus[h.id] || quotaStatus[h.bin];
354
+ if (!q) return true;
355
+ if (q.status === 'exhausted' || q.status === 'unavailable' || q.status === 'low-ram') return false;
356
+ if (typeof q.usagePercent === 'number' && q.usagePercent >= 95) return false;
357
+ return true;
358
+ });
359
+
360
+ const candidates = viable.length > 0 ? viable : pool;
361
+ const isFrontier = complexity.tier === 'frontier';
362
+ const isLocal = complexity.tier === 'local';
363
+
364
+ const ranked = candidates.map(h => {
365
+ let rawUtility = 0;
366
+ if (isFrontier) {
367
+ rawUtility = (h.qualityScore * 2.0) - (h.costScore * 0.2);
368
+ if (h.tier === 'frontier') rawUtility += 5.0;
369
+ } else if (isLocal) {
370
+ rawUtility = (10 - h.costScore) * 2.0 + (h.tier === 'local' ? 5.0 : 0);
371
+ } else {
372
+ rawUtility = (h.qualityScore * 1.0) - (h.costScore * 0.8);
373
+ if (h.tier === 'budget') rawUtility += 3.0;
374
+ }
375
+
376
+ // Penalize harnesses that are installed but currently unauthenticated so ready harnesses rank first
377
+ const q = quotaStatus[h.id] || quotaStatus[h.bin];
378
+ if (q && q.status === 'needs_auth') {
379
+ rawUtility -= 6.0;
380
+ }
381
+
382
+ // Factor in empirical local pass rate if at least 2 runs recorded
383
+ const emp = empiricalScorecard[h.id] || empiricalScorecard[h.bin];
384
+ if (emp && emp.total >= 2 && typeof emp.passRate === 'number') {
385
+ const bonus = ((emp.passRate - 75) / 25) * 2.0; // -6.0 to +2.0 adjustment
386
+ rawUtility += bonus;
387
+ }
388
+
389
+ return {
390
+ harness: h.id || h.bin,
391
+ name: h.name,
392
+ family: h.family || 'custom',
393
+ tier: h.tier,
394
+ utility: parseFloat(rawUtility.toFixed(3)),
395
+ complexityMatched: h.tier === complexity.tier
396
+ };
397
+ }).sort((a, b) => b.utility - a.utility);
398
+
399
+ return {
400
+ selected: ranked[0]?.harness || pool[0]?.id || pool[0]?.bin || null,
401
+ complexity,
402
+ candidates: ranked
403
+ };
404
+ }
405
+
406
+ /**
407
+ * Pure deterministic envelope builder for preflight and execution context
408
+ */
409
+ export function buildBootstrapEnvelope(task, options = {}) {
410
+ const timestamp = options.timestamp || new Date().toISOString();
411
+ const normalizedTask = String(task || '').trim();
412
+ const harness = options.harness || 'unknown';
413
+ return {
414
+ id: options.id || `envelope-${harness}-${Buffer.from(normalizedTask.slice(0, 48)).toString('hex').slice(0, 12) || 'task'}`,
415
+ task: normalizedTask,
416
+ action: options.action || 'read,edit,test',
417
+ ready: options.ready !== false,
418
+ harness,
419
+ timestamp,
420
+ contextCapsule: {
421
+ requiredRules: options.requiredRules || [],
422
+ plugins: options.plugins || [],
423
+ budget: options.budget || 17000
424
+ },
425
+ verificationReceipt: {
426
+ preflightPassed: options.preflightPassed !== false,
427
+ ackTime: timestamp
428
+ }
429
+ };
430
+ }
431
+
432
+ /**
433
+ * Evaluate deterministic code quality gate cascade (FrugalGPT concept).
434
+ * Escalates to a higher tier (or cross-family frontier model) from the caller's
435
+ * actual available pool rather than assuming hardcoded binaries.
436
+ */
437
+ export function evaluateGateCascade(currentHarness, gateResults = {}, availableHarnesses = []) {
438
+ const { testsPassed = true, linterPassed = true, typecheckPassed = true } = gateResults;
439
+ const passedAll = testsPassed && linterPassed && typecheckPassed;
440
+
441
+ if (passedAll) {
442
+ return {
443
+ escalationRequired: false,
444
+ recommendedHarness: currentHarness,
445
+ reason: 'All deterministic gates passed.'
446
+ };
447
+ }
448
+
449
+ const pool = availableHarnesses.length > 0
450
+ ? availableHarnesses.map(h => resolveAdapterEntry(h))
451
+ : Object.values(ADAPTER_CATALOG);
452
+
453
+ const currentSpec = resolveAdapterEntry(currentHarness);
454
+ const currentTier = currentSpec.tier || 'budget';
455
+ const currentFamily = currentSpec.family || 'unknown';
456
+
457
+ const byUtility = (list) => [...list].sort((a, b) => (b.qualityScore - a.qualityScore) || (a.costScore - b.costScore));
458
+
459
+ let nextSpec = null;
460
+ let reason = 'Gate failures detected; escalating to higher-capability harness.';
461
+
462
+ if (currentTier === 'local') {
463
+ const budgetPool = byUtility(pool.filter(h => h.tier === 'budget' && h.id !== currentSpec.id));
464
+ const frontierPool = byUtility(pool.filter(h => h.tier === 'frontier' && h.id !== currentSpec.id));
465
+ nextSpec = budgetPool[0] || frontierPool[0] || byUtility(pool.filter(h => h.id !== currentSpec.id))[0];
466
+ reason = `Local tier (${currentSpec.id}) failed quality gates; escalating to ${nextSpec?.tier || 'next'} tier (${nextSpec?.id || currentSpec.id}).`;
467
+ } else if (currentTier === 'budget' || currentTier === 'specialized') {
468
+ const frontierPool = byUtility(pool.filter(h => h.tier === 'frontier' && h.id !== currentSpec.id));
469
+ nextSpec = frontierPool[0] || byUtility(pool.filter(h => h.id !== currentSpec.id))[0];
470
+ reason = `${currentTier} tier (${currentSpec.id}) failed quality gates; escalating to frontier harness (${nextSpec?.id || currentSpec.id}).`;
471
+ } else {
472
+ // Already frontier: pick a frontier harness from a DIFFERENT model family to break confirmation bias
473
+ const crossFamilyFrontier = byUtility(pool.filter(h => h.tier === 'frontier' && h.id !== currentSpec.id && h.family !== currentFamily));
474
+ const anyOtherFrontier = byUtility(pool.filter(h => h.tier === 'frontier' && h.id !== currentSpec.id));
475
+ nextSpec = crossFamilyFrontier[0] || anyOtherFrontier[0] || byUtility(pool.filter(h => h.id !== currentSpec.id))[0];
476
+ reason = `Primary frontier harness (${currentSpec.id}) failed gates; cross-auditing with distinct model family (${nextSpec?.id || currentSpec.id}).`;
477
+ }
478
+
479
+ return {
480
+ escalationRequired: true,
481
+ previousHarness: currentSpec.id,
482
+ recommendedHarness: nextSpec?.id || currentSpec.id,
483
+ reason,
484
+ failures: {
485
+ tests: !testsPassed,
486
+ linter: !linterPassed,
487
+ typecheck: !typecheckPassed
488
+ }
489
+ };
490
+ }
491
+
492
+ /**
493
+ * Compute empirical scorecard from ledger runs
494
+ */
495
+ export function computeEmpiricalScorecard(historyRuns = []) {
496
+ const summary = {};
497
+ for (const run of historyRuns) {
498
+ const h = run.harness || 'unknown';
499
+ if (!summary[h]) {
500
+ summary[h] = { total: 0, passed: 0, failed: 0, totalDurationMs: 0, passRate: 0, avgDurationMs: 0 };
501
+ }
502
+ summary[h].total += 1;
503
+ if (run.success) {
504
+ summary[h].passed += 1;
505
+ } else {
506
+ summary[h].failed += 1;
507
+ }
508
+ summary[h].totalDurationMs += (run.durationMs || 0);
509
+ }
510
+
511
+ for (const h of Object.keys(summary)) {
512
+ const item = summary[h];
513
+ item.passRate = item.total > 0 ? parseFloat(((item.passed / item.total) * 100).toFixed(1)) : 0;
514
+ item.avgDurationMs = item.total > 0 ? Math.round(item.totalDurationMs / item.total) : 0;
515
+ }
516
+
517
+ return summary;
518
+ }
519
+
520
+ /**
521
+ * Meta Harness Brick Factory
522
+ */
523
+ export function createMetaHarness(customAdapters = {}) {
524
+ const mergedAdapters = { ...ADAPTER_CATALOG, ...customAdapters };
525
+ return {
526
+ catalog: mergedAdapters,
527
+ validateAdapter: (adapter) => validateAdapterManifest(adapter),
528
+ classify: (task, opts) => classifyTaskComplexity(task, opts),
529
+ select: (task, available, quotas, opts) => selectOptimalHarness(task, available, quotas, { ...opts, catalog: mergedAdapters }),
530
+ buildEnvelope: (task, opts) => buildBootstrapEnvelope(task, opts),
531
+ evaluateCascade: (harness, gates, available) => evaluateGateCascade(harness, gates, available),
532
+ scorecard: (runs) => computeEmpiricalScorecard(runs)
533
+ };
534
+ }