harness-everything 0.21.0 → 0.22.1

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.
Files changed (64) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +18 -0
  4. package/README.md +30 -0
  5. package/benchmarks/results/system-one/cua-s1-forms-resident-async-cpu-2026-09-23/README.md +87 -0
  6. package/benchmarks/results/system-one/cua-s1-forms-resident-async-cpu-2026-09-23/paired-latency.json +107 -0
  7. package/benchmarks/results/system-one/cua-s1-forms-resident-async-cpu-2026-09-23/report.json +856 -0
  8. package/benchmarks/results/system-one/cua-s1-forms-resident-cpu-2026-09-23/README.md +74 -0
  9. package/benchmarks/results/system-one/cua-s1-forms-resident-cpu-2026-09-23/report.json +856 -0
  10. package/benchmarks/results/system-one/pre-merge-review-2026-09-23/README.md +92 -0
  11. package/benchmarks/results/system-one/pre-merge-review-2026-09-23/haiku-spec-conformance.md +241 -0
  12. package/benchmarks/results/system-one/pre-merge-review-2026-09-23/luna-adversarial.md +212 -0
  13. package/benchmarks/results/system-one/pre-merge-review-2026-09-23/skill-top1-probe.js +83 -0
  14. package/benchmarks/results/system-one/pre-merge-review-2026-09-23/skill-top1-probe.json +7700 -0
  15. package/ci/fixtures/system-one-resident-stub.py +28 -0
  16. package/ci/fixtures/system-one-worker-probe.js +15 -0
  17. package/ci/mechanism-packaging-bytecode.test.js +61 -0
  18. package/ci/mechanism-system-one-install.test.js +11 -0
  19. package/ci/mechanism-system-one-resident.test.js +296 -0
  20. package/ci/mechanism-system-one-router.test.js +25 -0
  21. package/ci/system-one-adapter-test.py +166 -0
  22. package/docs/platform-capabilities.md +8 -0
  23. package/docs/system-one-routing.md +91 -3
  24. package/evals/pilot-router-nonsoftware/graders/no-tier-language.md +6 -0
  25. package/evals/pilot-router-nonsoftware/graders/skill-not-fired.md +9 -0
  26. package/evals/pilot-router-nonsoftware/prompt.md +8 -0
  27. package/evals/pilot-router-tier3/graders/skill-fired.md +6 -0
  28. package/evals/pilot-router-tier3/prompt.md +8 -0
  29. package/evals/pilot-tdd-bugfix/graders/skill-fired.md +6 -0
  30. package/evals/pilot-tdd-bugfix/prompt.md +8 -0
  31. package/evals/pilot-tdd-trivia/graders/no-red-phase-claim.md +7 -0
  32. package/evals/pilot-tdd-trivia/graders/skill-not-fired.md +9 -0
  33. package/evals/pilot-tdd-trivia/prompt.md +8 -0
  34. package/evals/pilot-verify-discussion/graders/no-verification-claim.md +7 -0
  35. package/evals/pilot-verify-discussion/graders/skill-not-fired.md +9 -0
  36. package/evals/pilot-verify-discussion/prompt.md +8 -0
  37. package/evals/pilot-verify-preship/graders/skill-fired.md +6 -0
  38. package/evals/pilot-verify-preship/prompt.md +8 -0
  39. package/harness-everything/SKILL.md +1 -1
  40. package/harness-everything/scripts/system-one/cua_adapter.py +265 -6
  41. package/harness-everything/scripts/system-one/install.js +17 -8
  42. package/harness-everything/scripts/system-one/provider.js +18 -3
  43. package/harness-everything/scripts/system-one/resident.js +200 -0
  44. package/harness-everything/scripts/system-one/router.js +18 -2
  45. package/harness-everything/scripts/tier-router.js +13 -4
  46. package/opencode-plugin/plugin.json +1 -1
  47. package/package.json +1 -1
  48. package/plugins/harness-everything/.codex-plugin/plugin.json +1 -1
  49. package/plugins/harness-everything/plugin.json +1 -1
  50. package/plugins/harness-everything/scripts/lib/packaging-noise.js +6 -0
  51. package/plugins/harness-everything/scripts/lib/skills.js +2 -0
  52. package/plugins/harness-everything/skills/harness-everything/SKILL.md +1 -1
  53. package/plugins/harness-everything/skills/harness-everything/scripts/system-one/cua_adapter.py +265 -6
  54. package/plugins/harness-everything/skills/harness-everything/scripts/system-one/install.js +17 -8
  55. package/plugins/harness-everything/skills/harness-everything/scripts/system-one/provider.js +18 -3
  56. package/plugins/harness-everything/skills/harness-everything/scripts/system-one/resident.js +200 -0
  57. package/plugins/harness-everything/skills/harness-everything/scripts/system-one/router.js +18 -2
  58. package/plugins/harness-everything/skills/harness-everything/scripts/tier-router.js +13 -4
  59. package/sbom.cdx.json +7 -7
  60. package/scripts/build-openai-submission.js +2 -0
  61. package/scripts/evaluate-system-one.js +18 -9
  62. package/scripts/lib/packaging-noise.js +6 -0
  63. package/scripts/lib/skills.js +2 -0
  64. package/scripts/sync-openai-plugin.js +2 -1
@@ -1,9 +1,16 @@
1
1
  """Optional CPU CUA-S1 bridge. Stdout is protocol-only; never execute UI actions."""
2
2
  import hashlib
3
+ import hmac
3
4
  import json
5
+ import os
4
6
  from pathlib import Path
5
7
  import re
8
+ import secrets
9
+ import socket
10
+ import subprocess
6
11
  import sys
12
+ import threading
13
+ import time
7
14
 
8
15
 
9
16
  def verify_artifacts(manifest):
@@ -25,18 +32,23 @@ def check_limits(request, config):
25
32
  raise ValueError('input-too-long')
26
33
 
27
34
 
28
- def infer(request, manifest):
29
- weights = verify_artifacts(manifest)
35
+ def load_scorer(weights, manifest):
36
+ """Load the verified checkpoint once; return (score(request) -> probabilities, config)."""
30
37
  import torch
31
38
  from cua_s1.model import ChoiceExample, load_checkpoint
32
39
 
33
40
  torch.set_num_threads(1)
34
41
  torch.use_deterministic_algorithms(True)
35
42
  model, collator, config = load_checkpoint(weights, 'cpu')
36
- check_limits(request, config)
37
- example = ChoiceExample(context=request['context'], options=tuple(o['text'] for o in request['options']), label=0)
38
- with torch.inference_mode():
39
- probabilities = model(collator([example])).softmax(-1)[0].tolist()
43
+
44
+ def score(request):
45
+ example = ChoiceExample(context=request['context'], options=tuple(o['text'] for o in request['options']), label=0)
46
+ with torch.inference_mode():
47
+ return model(collator([example])).softmax(-1)[0].tolist()
48
+ return score, config
49
+
50
+
51
+ def build_response(request, manifest, probabilities):
40
52
  return {
41
53
  'schemaVersion': 1, 'requestHash': request['requestHash'], 'catalogHash': request['catalogHash'],
42
54
  'model': {'id': manifest['modelId'], 'revision': manifest['revision'], 'domain': manifest['domain']},
@@ -44,6 +56,251 @@ def infer(request, manifest):
44
56
  }
45
57
 
46
58
 
59
+ def infer(request, manifest):
60
+ weights = verify_artifacts(manifest)
61
+ score, config = load_scorer(weights, manifest)
62
+ check_limits(request, config)
63
+ return build_response(request, manifest, score(request))
64
+
65
+
66
+ MAX_REQUEST_BYTES = 2 * 1024 * 1024
67
+ MAX_OPTIONS = 256
68
+ MAX_CLIENTS = 64
69
+ READ_DEADLINE_S = 1.0 # never longer than the Node client's 1000 ms call deadline
70
+ STARTUP_REASONS = ('artifact-hash', 'checkpoint-format')
71
+ ID_PATTERN = re.compile(r'[a-z0-9._-]{1,80}')
72
+ HASH_PATTERN = re.compile(r'[0-9a-f]{64}')
73
+ _USER_SID = []
74
+
75
+
76
+ def _artifact_stamp(weights):
77
+ return [(p.stat().st_size, p.stat().st_mtime_ns) for p in (weights, weights.with_suffix('.json'))]
78
+
79
+
80
+ def _restrict_to_current_user(path):
81
+ """Windows: replace the whole DACL with one protected full-control ACE for the current user.
82
+
83
+ Editing ACEs is not enough: some hosts create files with explicit (non-inherited) SYSTEM,
84
+ Administrators or OWNER RIGHTS entries that survive `icacls /inheritance:r /grant:r`.
85
+ """
86
+ if os.name != 'nt':
87
+ return
88
+ import ctypes
89
+ from ctypes import wintypes
90
+ if not _USER_SID:
91
+ tools = Path(os.environ.get('SystemRoot', 'C:/Windows')) / 'System32'
92
+ out = subprocess.run([str(tools / 'whoami.exe'), '/user', '/fo', 'csv', '/nh'],
93
+ capture_output=True, text=True, timeout=10, check=True).stdout
94
+ sid = out.strip().split(',')[-1].strip().strip('"')
95
+ if not re.fullmatch(r'S-1-[0-9-]+', sid):
96
+ raise OSError('user-sid')
97
+ _USER_SID.append(sid)
98
+ advapi32 = ctypes.WinDLL('advapi32', use_last_error=True)
99
+ kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
100
+ descriptor = ctypes.c_void_p()
101
+ if not advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW(
102
+ f'D:P(A;;FA;;;{_USER_SID[0]})', 1, ctypes.byref(descriptor), None):
103
+ raise OSError('dacl-build')
104
+ try:
105
+ present, defaulted, dacl = wintypes.BOOL(), wintypes.BOOL(), ctypes.c_void_p()
106
+ if not advapi32.GetSecurityDescriptorDacl(descriptor, ctypes.byref(present), ctypes.byref(dacl), ctypes.byref(defaulted)):
107
+ raise OSError('dacl-read')
108
+ SE_FILE_OBJECT, DACL_INFO, PROTECTED_DACL_INFO = 1, 0x4, 0x80000000
109
+ advapi32.SetNamedSecurityInfoW.argtypes = [wintypes.LPWSTR, ctypes.c_int, wintypes.DWORD,
110
+ ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
111
+ if advapi32.SetNamedSecurityInfoW(str(path), SE_FILE_OBJECT, DACL_INFO | PROTECTED_DACL_INFO, None, None, dacl, None) != 0:
112
+ raise OSError('dacl-set')
113
+ finally:
114
+ kernel32.LocalFree(descriptor)
115
+
116
+
117
+ def _write_private_json(target, value):
118
+ """Atomic replace of an owner-only file (0600; explicit owner-only DACL on Windows) before the token is written."""
119
+ tmp = target.with_name(f'.{target.name}.{os.getpid()}.{secrets.token_hex(4)}.tmp')
120
+ os.close(os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600))
121
+ _restrict_to_current_user(tmp)
122
+ with open(tmp, 'w', encoding='utf-8') as handle:
123
+ json.dump(value, handle)
124
+ os.replace(tmp, target)
125
+
126
+
127
+ def _read_request_line(conn):
128
+ buffer = bytearray()
129
+ while b'\n' not in buffer and len(buffer) <= MAX_REQUEST_BYTES:
130
+ chunk = conn.recv(65536)
131
+ if not chunk:
132
+ break
133
+ buffer.extend(chunk)
134
+ if len(buffer.split(b'\n', 1)[0]) > MAX_REQUEST_BYTES:
135
+ # Drain to EOF (bounded by size and the read deadline) so the reply is not lost to a reset.
136
+ drained = 0
137
+ try:
138
+ while drained < 8 * MAX_REQUEST_BYTES and conn.recv(65536):
139
+ drained += 65536
140
+ except OSError:
141
+ pass
142
+ raise ValueError('request-limit')
143
+ return bytes(buffer).split(b'\n', 1)[0]
144
+
145
+
146
+ def _valid_score_request(request):
147
+ """Phase 0 request structure. Hashes are fingerprints, not authentication, so they are shape-checked only."""
148
+ if (not isinstance(request, dict)
149
+ or set(request) != {'schemaVersion', 'task', 'context', 'options', 'catalogHash', 'requestHash'}
150
+ or request['schemaVersion'] != 1 or not isinstance(request['task'], str) or not ID_PATTERN.fullmatch(request['task'])
151
+ or not isinstance(request['context'], str)
152
+ or not all(isinstance(request[k], str) and HASH_PATTERN.fullmatch(request[k]) for k in ('catalogHash', 'requestHash'))):
153
+ return False
154
+ options = request['options']
155
+ if not isinstance(options, list) or not 2 <= len(options) <= MAX_OPTIONS:
156
+ return False
157
+ if any(not isinstance(o, dict) or set(o) != {'id', 'text'} or not isinstance(o['id'], str)
158
+ or not ID_PATTERN.fullmatch(o['id']) or not isinstance(o['text'], str) for o in options):
159
+ return False
160
+ return len({o['id'] for o in options}) == len(options)
161
+
162
+
163
+ def _handle(conn, token, manifest, weights, stamp, score, config):
164
+ """Return (reply, stop). Errors carry bounded reason codes only: no prompt, path, or traceback."""
165
+ invalid = {'ok': False, 'reason': 'invalid-request'}
166
+ try:
167
+ line = _read_request_line(conn)
168
+ except ValueError:
169
+ return {'ok': False, 'reason': 'request-limit'}, False
170
+ except OSError:
171
+ return invalid, False
172
+ try:
173
+ message = json.loads(line)
174
+ except ValueError:
175
+ return invalid, False
176
+ if not isinstance(message, dict):
177
+ return invalid, False
178
+ supplied = message.get('token')
179
+ if not isinstance(supplied, str) or not hmac.compare_digest(supplied.encode('utf-8'), token.encode('utf-8')):
180
+ return {'ok': False, 'reason': 'unauthorized'}, False
181
+ op = message.get('op')
182
+ if op == 'ping':
183
+ return {'ok': True, 'pid': os.getpid(), 'model': {'id': manifest['modelId'], 'revision': manifest['revision'], 'domain': manifest['domain']}}, False
184
+ if op == 'shutdown':
185
+ return {'ok': True}, True
186
+ if op != 'score' or not _valid_score_request(message.get('request')):
187
+ return invalid, False
188
+ try:
189
+ changed = _artifact_stamp(weights) != stamp
190
+ except OSError:
191
+ changed = True
192
+ if changed:
193
+ return {'ok': False, 'reason': 'artifact-changed'}, True
194
+ request = message['request']
195
+ try:
196
+ check_limits(request, config)
197
+ except ValueError:
198
+ return {'ok': False, 'reason': 'input-too-long'}, False
199
+ try:
200
+ return {'ok': True, 'response': build_response(request, manifest, score(request))}, False
201
+ except Exception:
202
+ return {'ok': False, 'reason': 'score-failed'}, False
203
+
204
+
205
+ def serve(manifest, state_path, lock_path, scorer_factory=None, idle_timeout_s=None):
206
+ """Resident loopback scorer: verify, load once, publish state, serve until idle/shutdown/artifact change."""
207
+ state_path, lock_path = Path(state_path), Path(lock_path)
208
+ try:
209
+ weights = verify_artifacts(manifest)
210
+ stamp = _artifact_stamp(weights)
211
+ score, config = (scorer_factory or load_scorer)(weights, manifest)
212
+ except Exception as exc:
213
+ reason = str(exc) if str(exc) in STARTUP_REASONS else 'load-failed'
214
+ try:
215
+ _write_private_json(lock_path, {'failed': reason, 'at': time.time()})
216
+ except OSError:
217
+ pass
218
+ return 1
219
+ idle = idle_timeout_s if idle_timeout_s is not None else manifest.get('idleTimeoutMs', 1800000) / 1000
220
+ token = secrets.token_hex(32)
221
+ pid = os.getpid()
222
+ stop = threading.Event()
223
+ score_lock = threading.Lock() # one model, one inference at a time; connections are handled concurrently
224
+ slots = threading.BoundedSemaphore(MAX_CLIENTS)
225
+
226
+ def guarded_score(request):
227
+ with score_lock:
228
+ return score(request)
229
+
230
+ def send(conn, reply):
231
+ try:
232
+ conn.sendall((json.dumps(reply, allow_nan=False) + '\n').encode('utf-8'))
233
+ except OSError:
234
+ pass
235
+
236
+ def worker(conn):
237
+ try:
238
+ with conn:
239
+ conn.settimeout(READ_DEADLINE_S)
240
+ reply, halt = _handle(conn, token, manifest, weights, stamp, guarded_score, config)
241
+ send(conn, reply)
242
+ if halt:
243
+ stop.set()
244
+ finally:
245
+ slots.release()
246
+
247
+ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
248
+ try:
249
+ server.bind(('127.0.0.1', 0))
250
+ server.listen(MAX_CLIENTS)
251
+ server.settimeout(0.25)
252
+ _write_private_json(state_path, {'schemaVersion': 1, 'pid': pid, 'port': server.getsockname()[1], 'token': token, 'startedAt': time.time()})
253
+ try:
254
+ lock_path.unlink()
255
+ except FileNotFoundError:
256
+ pass
257
+ last = time.monotonic()
258
+ # A server whose state file no longer names it is undiscoverable (replaced or corrupted): exit, never orphan.
259
+ while not stop.is_set() and time.monotonic() - last <= idle and _owns_state(state_path, pid, token):
260
+ try:
261
+ conn, _ = server.accept()
262
+ except socket.timeout:
263
+ continue
264
+ last = time.monotonic()
265
+ if not slots.acquire(blocking=False):
266
+ with conn:
267
+ conn.settimeout(0.2)
268
+ try:
269
+ _read_request_line(conn)
270
+ except (OSError, ValueError):
271
+ pass
272
+ send(conn, {'ok': False, 'reason': 'busy'})
273
+ continue
274
+ threading.Thread(target=worker, args=(conn,), daemon=True).start()
275
+ finally:
276
+ server.close()
277
+ if _owns_state(state_path, pid, token, missing=False):
278
+ try:
279
+ state_path.unlink()
280
+ except OSError:
281
+ pass
282
+ return 0
283
+
284
+
285
+ def _owns_state(state_path, pid, token, missing=False):
286
+ """True while the state file still names this server. A transient Windows sharing violation counts as owned."""
287
+ try:
288
+ data = json.loads(state_path.read_text(encoding='utf-8'))
289
+ except FileNotFoundError:
290
+ return missing
291
+ except OSError:
292
+ return True
293
+ except ValueError:
294
+ return False
295
+ return isinstance(data, dict) and data.get('pid') == pid and data.get('token') == token
296
+
297
+
298
+ def serve_main(argv, scorer_factory=None):
299
+ if len(argv) != 3:
300
+ return 2
301
+ return serve(json.loads(argv[0]), argv[1], argv[2], scorer_factory=scorer_factory)
302
+
303
+
47
304
  def source_revision(direct_url_text):
48
305
  """Return the PEP 610 VCS commit recorded at install time, or None when unprovable."""
49
306
  try:
@@ -76,6 +333,8 @@ def main():
76
333
  if sys.argv[1:] == ['--provenance']:
77
334
  print(json.dumps(provenance(), allow_nan=False))
78
335
  return 0
336
+ if sys.argv[1:2] == ['--serve']:
337
+ return serve_main(sys.argv[2:])
79
338
  manifest = json.loads(sys.argv[1])
80
339
  raw = sys.stdin.buffer.read(2 * 1024 * 1024 + 1)
81
340
  if len(raw) > 2 * 1024 * 1024:
@@ -8,7 +8,8 @@ const path = require('node:path');
8
8
  const { createHash } = require('node:crypto');
9
9
  const { spawnSync } = require('node:child_process');
10
10
  const { performance } = require('node:perf_hooks');
11
- const { validateManifest, provenance, score, PINNED_CUA_S1_REVISION } = require('./provider');
11
+ const { validateManifest, readManifest, provenance, score, PINNED_CUA_S1_REVISION } = require('./provider');
12
+ const resident = require('./resident');
12
13
  const { createRequest, decide } = require('./contract');
13
14
  const { TIER_OPTIONS } = require('./router');
14
15
 
@@ -64,8 +65,8 @@ function buildManifest(dir, python) {
64
65
  const manifest = { schemaVersion: 1, python, checkpoint: path.join(checkpointDir(dir), PINS.files.weights.name),
65
66
  weightsSha256: PINS.files.weights.sha256, configSha256: PINS.files.config.sha256,
66
67
  modelId: PINS.modelId, revision: PINS.modelRevision, domain: PINS.domain,
67
- // Each score starts a fresh Python process that imports torch; cold start dominates latency.
68
- timeoutMs: 10000 };
68
+ // One-shot fallback budget (fresh Python + torch import per call); resident keeps the model loaded.
69
+ timeoutMs: 10000, transport: 'resident', idleTimeoutMs: 1800000 };
69
70
  validateManifest(manifest);
70
71
  return manifest;
71
72
  }
@@ -85,7 +86,8 @@ async function ensureFile(url, dest, spec, fetcher = httpFetch) {
85
86
  return 'downloaded';
86
87
  }
87
88
  function exec(cmd, args, capture = false) {
88
- const result = spawnSync(cmd, args, { shell: false, windowsHide: true, encoding: 'utf8', stdio: capture ? 'pipe' : 'inherit' });
89
+ // Child progress goes to stderr (fd 2): stdout carries only the final JSON report.
90
+ const result = spawnSync(cmd, args, { shell: false, windowsHide: true, encoding: 'utf8', stdio: capture ? 'pipe' : ['ignore', 2, 2] });
89
91
  if (result.error || result.status !== 0) throw new Error(`command-failed: ${[cmd, ...args].join(' ')}`);
90
92
  return capture ? result.stdout.trim() : '';
91
93
  }
@@ -108,20 +110,27 @@ function plan(opts) {
108
110
  `${venvPy} -m pip install "${PINS.cuaS1Spec}"`,
109
111
  ...Object.values(PINS.files).map(f => `download ${fileUrl(f.name)} (sha256 ${f.sha256})`),
110
112
  `write ${manifestPath}`,
111
- 'verify provenance (pinned revision) and one real CPU inference',
113
+ 'verify provenance (pinned revision), start the resident server, one warm CPU inference, stop it',
112
114
  ],
113
115
  env: { HARNESS_SYSTEM_ONE_MODE: 'shadow', HARNESS_SYSTEM_ONE_CONFIG: manifestPath } };
114
116
  }
117
+ // Starts the resident server (if needed), scores once warm, then stops a server it started.
115
118
  function verify(manifestPath) {
116
119
  const source = provenance(manifestPath);
120
+ const manifest = readManifest(manifestPath);
121
+ const wasRunning = resident.status(manifest, manifestPath).running;
122
+ const startMs = performance.now();
123
+ const ready = resident.ensureReady(manifest, manifestPath, 60000);
124
+ const readyMs = Math.round(performance.now() - startMs);
117
125
  const request = createRequest('tier', 'fix a typo in README', TIER_OPTIONS);
118
126
  const start = performance.now();
119
127
  const result = score(request, manifestPath);
120
128
  const latencyMs = Math.round(performance.now() - start);
129
+ if (!wasRunning) resident.stop(manifest, manifestPath);
121
130
  const decision = result.status === 'scored' ? decide(request, result.response) : result;
122
131
  const scores = result.status === 'scored' && decision.status !== 'invalid-output' ? result.response.scores : null;
123
- const ok = source.status === 'recorded' && source.provenance.cuaS1.sourceRevisionStatus === 'pinned' && scores !== null;
124
- return { ok, source, decision, scores, latencyMs };
132
+ const ok = ready && source.status === 'recorded' && source.provenance.cuaS1.sourceRevisionStatus === 'pinned' && scores !== null;
133
+ return { ok, source, residentReady: ready, readyMs, decision, scores, latencyMs };
125
134
  }
126
135
  async function main(argv) {
127
136
  const opts = parseArgs(argv);
@@ -149,4 +158,4 @@ if (require.main === module) {
149
158
  console.error(`System One install failed: ${err.message}`); process.exitCode = 1;
150
159
  });
151
160
  }
152
- module.exports = { PINS, fileUrl, venvPython, supportedPython, pythonCandidates, parseArgs, buildManifest, ensureFile, plan, verify };
161
+ module.exports = { PINS, fileUrl, venvPython, supportedPython, pythonCandidates, parseArgs, buildManifest, ensureFile, exec, plan, verify };
@@ -13,11 +13,13 @@ const label = value => typeof value === 'string' && value.length > 0 && value.le
13
13
  function validateManifest(m) {
14
14
  const required = ['schemaVersion', 'python', 'checkpoint', 'weightsSha256', 'configSha256', 'modelId', 'revision', 'domain'];
15
15
  if (!m || typeof m !== 'object' || Array.isArray(m) || required.some(k => !Object.hasOwn(m, k))
16
- || Object.keys(m).some(k => ![...required, 'timeoutMs'].includes(k)) || m.schemaVersion !== 1
16
+ || Object.keys(m).some(k => ![...required, 'timeoutMs', 'transport', 'idleTimeoutMs'].includes(k)) || m.schemaVersion !== 1
17
17
  || ['python', 'modelId', 'revision', 'domain'].some(k => typeof m[k] !== 'string' || !m[k].trim() || m[k].length > 4096 || m[k].includes('\0'))
18
18
  || typeof m.checkpoint !== 'string' || !path.isAbsolute(m.checkpoint) || path.extname(m.checkpoint) !== '.safetensors'
19
19
  || ['weightsSha256', 'configSha256'].some(k => typeof m[k] !== 'string' || !/^[a-f0-9]{64}$/.test(m[k]))
20
20
  || (m.timeoutMs !== undefined && (!Number.isInteger(m.timeoutMs) || m.timeoutMs < 1 || m.timeoutMs > 10000))
21
+ || (m.transport !== undefined && !['oneshot', 'resident'].includes(m.transport))
22
+ || (m.idleTimeoutMs !== undefined && (!Number.isInteger(m.idleTimeoutMs) || m.idleTimeoutMs < 60000 || m.idleTimeoutMs > 14400000))
21
23
  || (m.modelId === 'cua-ai/cua-s1-forms' && m.domain !== 'forms-v1')) throw new TypeError('invalid-manifest');
22
24
  return true;
23
25
  }
@@ -48,7 +50,20 @@ function score(request, manifestPath) {
48
50
  validateRequest(request);
49
51
  let manifest;
50
52
  try { manifest = readManifest(manifestPath); } catch (_) { return unavailable('provider-config'); }
51
- // Send the exact validated manifest to the child on stdin: no config reread race.
53
+ if (manifest.transport === 'resident') return require('./resident').score({ ...request }, manifest, manifestPath);
54
+ return oneShot(request, manifest);
55
+ }
56
+ // Async callers (the hook entry) reach a resident server without the sync worker bridge.
57
+ // One-shot has no server to overlap with, so it runs the same child synchronously.
58
+ async function scoreAsync(request, manifestPath) {
59
+ validateRequest(request);
60
+ let manifest;
61
+ try { manifest = readManifest(manifestPath); } catch (_) { return unavailable('provider-config'); }
62
+ if (manifest.transport === 'resident') return require('./resident').scoreAsync({ ...request }, manifest, manifestPath);
63
+ return oneShot(request, manifest);
64
+ }
65
+ // Send the exact validated manifest to the child on stdin: no config reread race.
66
+ function oneShot(request, manifest) {
52
67
  return runProvider({ ...request }, manifest.python, [ADAPTER, JSON.stringify(manifest)], manifest.timeoutMs || 2000);
53
68
  }
54
69
  // The probe reports what is installed; only a PEP 610 VCS commit counts as a source revision.
@@ -73,4 +88,4 @@ function provenance(manifestPath) {
73
88
  try { return { status: 'recorded', provenance: validateProvenance(result.value) }; }
74
89
  catch (_) { return unavailable('provider-json'); }
75
90
  }
76
- module.exports = { score, runProvider, validateManifest, provenance, validateProvenance, PINNED_CUA_S1_REVISION };
91
+ module.exports = { score, scoreAsync, runProvider, validateManifest, readManifest, provenance, validateProvenance, PINNED_CUA_S1_REVISION };
@@ -0,0 +1,200 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ // Resident System One client: one verified model stays loaded in a loopback server.
4
+ // A score never blocks on model loading; a cold start returns provider-starting (lexical route wins).
5
+ const fs = require('node:fs');
6
+ const path = require('node:path');
7
+ const { createHash } = require('node:crypto');
8
+ const net = require('node:net');
9
+ const { spawn } = require('node:child_process');
10
+ const { Worker } = require('node:worker_threads');
11
+ const { validateRequest } = require('./contract');
12
+
13
+ const ADAPTER = path.join(__dirname, 'cua_adapter.py');
14
+ const LOCK_TTL_MS = 60000;
15
+ const CALL_TIMEOUT_MS = 1000;
16
+ const MAX_REPLY_BYTES = 1024 * 1024;
17
+ const unavailable = reason => ({ status: 'unavailable', reason });
18
+ const sleep = ms => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
19
+ const exact = (value, keys) => value && typeof value === 'object' && !Array.isArray(value)
20
+ && Object.keys(value).length === keys.length && keys.every(k => Object.hasOwn(value, k));
21
+ const defaultCommand = manifest => [manifest.python, [ADAPTER, '--serve']];
22
+
23
+ function stateFiles(manifest, manifestPath) {
24
+ const digest = createHash('sha256').update(JSON.stringify(manifest)).digest('hex').slice(0, 16);
25
+ const base = path.join(path.dirname(manifestPath), `system-one-resident-${digest}`);
26
+ return { state: `${base}.json`, lock: `${base}.starting` };
27
+ }
28
+ function readState(file) {
29
+ try {
30
+ const s = JSON.parse(fs.readFileSync(file, 'utf8'));
31
+ if (exact(s, ['schemaVersion', 'pid', 'port', 'token', 'startedAt']) && s.schemaVersion === 1
32
+ && Number.isInteger(s.pid) && s.pid > 0 && Number.isInteger(s.port) && s.port > 0 && s.port < 65536
33
+ && typeof s.token === 'string' && /^[0-9a-f]{64}$/.test(s.token) && Number.isFinite(s.startedAt)) return s;
34
+ } catch (_) { /* absent or unreadable state means no usable server */ }
35
+ return null;
36
+ }
37
+ function lockInfo(file) {
38
+ try {
39
+ const ageMs = Date.now() - fs.statSync(file).mtimeMs;
40
+ let failed = null;
41
+ try { const body = JSON.parse(fs.readFileSync(file, 'utf8')); if (typeof body.failed === 'string') failed = body.failed; } catch (_) { /* partial write */ }
42
+ return { ageMs, failed };
43
+ } catch (_) { return null; }
44
+ }
45
+ function pidAlive(pid) {
46
+ try { process.kill(pid, 0); return true; } catch (err) { return err.code === 'EPERM'; }
47
+ }
48
+ function removeIfSame(file, state) {
49
+ const current = readState(file);
50
+ if (current && current.pid === state.pid && current.token === state.token) { try { fs.unlinkSync(file); } catch (_) { /* raced */ } }
51
+ }
52
+
53
+ // One socket round trip on a worker thread; the caller blocks with Atomics.wait (router API is synchronous).
54
+ const WORKER = `
55
+ const net = require('node:net');
56
+ const { workerData } = require('node:worker_threads');
57
+ const { port, payload, signal, out } = workerData;
58
+ const view = new Uint8Array(out); const chunks = []; let size = 0; let done = false;
59
+ const finish = (code, buf) => {
60
+ if (done) return; done = true;
61
+ if (buf) { view.set(buf); Atomics.store(signal, 1, buf.length); }
62
+ Atomics.store(signal, 0, code); Atomics.notify(signal, 0);
63
+ };
64
+ const socket = net.connect({ port, host: '127.0.0.1' }, () => socket.end(payload));
65
+ socket.on('data', c => { size += c.length; if (size > view.length) { finish(4); socket.destroy(); } else chunks.push(c); });
66
+ socket.on('end', () => finish(1, Buffer.concat(chunks)));
67
+ socket.on('error', e => finish(e.code === 'ECONNREFUSED' ? 2 : 3));
68
+ `;
69
+ function callSync(port, message, timeoutMs = CALL_TIMEOUT_MS) {
70
+ const signal = new Int32Array(new SharedArrayBuffer(8));
71
+ const out = new SharedArrayBuffer(MAX_REPLY_BYTES);
72
+ let worker;
73
+ try {
74
+ worker = new Worker(WORKER, { eval: true, workerData: { port, payload: `${JSON.stringify(message)}\n`, signal, out } });
75
+ worker.unref();
76
+ } catch (_) { return { error: 'io' }; }
77
+ Atomics.wait(signal, 0, 0, timeoutMs);
78
+ const code = Atomics.load(signal, 0);
79
+ worker.terminate();
80
+ if (code === 0) return { error: 'timeout' };
81
+ if (code !== 1) return { error: code === 2 ? 'refused' : code === 4 ? 'limit' : 'io' };
82
+ try { return { reply: JSON.parse(Buffer.from(out, 0, Atomics.load(signal, 1)).toString('utf8')) }; }
83
+ catch (_) { return { error: 'json' }; }
84
+ }
85
+ // The same round trip for async callers (the hook entry): no worker thread to bootstrap.
86
+ function callAsync(port, message, timeoutMs = CALL_TIMEOUT_MS) {
87
+ return new Promise(resolve => {
88
+ const chunks = []; let size = 0; let done = false; let socket = null;
89
+ const finish = value => { if (done) return; done = true; clearTimeout(timer); if (socket) socket.destroy(); resolve(value); };
90
+ const timer = setTimeout(() => finish({ error: 'timeout' }), timeoutMs);
91
+ socket = net.connect({ port, host: '127.0.0.1' }, () => socket.end(`${JSON.stringify(message)}\n`));
92
+ socket.on('data', c => { size += c.length; if (size > MAX_REPLY_BYTES) finish({ error: 'limit' }); else chunks.push(c); });
93
+ socket.on('end', () => {
94
+ try { finish({ reply: JSON.parse(Buffer.concat(chunks).toString('utf8')) }); } catch (_) { finish({ error: 'json' }); }
95
+ });
96
+ socket.on('error', e => finish({ error: e.code === 'ECONNREFUSED' ? 'refused' : 'io' }));
97
+ });
98
+ }
99
+
100
+ function start(manifest, files, deps) {
101
+ const lock = lockInfo(files.lock);
102
+ if (lock && lock.ageMs < LOCK_TTL_MS) return unavailable(lock.failed ? 'provider-unavailable' : 'provider-starting');
103
+ if (lock) { try { fs.unlinkSync(files.lock); } catch (_) { /* another caller replaced it */ } }
104
+ try { fs.writeFileSync(files.lock, JSON.stringify({ createdAt: Date.now() }), { flag: 'wx', mode: 0o600 }); }
105
+ catch (_) { return unavailable('provider-starting'); }
106
+ let child = null;
107
+ try {
108
+ const [exe, prefix] = (deps.command || defaultCommand)(manifest);
109
+ child = spawn(exe, [...prefix, JSON.stringify(manifest), files.state, files.lock],
110
+ { detached: true, stdio: 'ignore', windowsHide: true, shell: false });
111
+ child.on('error', () => { /* recorded synchronously below: a failed spawn has no pid */ });
112
+ child.unref();
113
+ } catch (_) { child = null; }
114
+ // The async 'error' event cannot run while callers block in Atomics.wait, so record the failure now.
115
+ if (!child || !child.pid) {
116
+ try { fs.writeFileSync(files.lock, JSON.stringify({ createdAt: Date.now(), failed: 'spawn-failed' })); } catch (_) { /* best effort */ }
117
+ return unavailable('provider-unavailable');
118
+ }
119
+ return unavailable('provider-starting');
120
+ }
121
+ // Returns a live server state, or null after removing state that provably belongs to a dead server.
122
+ function liveState(files) {
123
+ const state = readState(files.state);
124
+ if (state && !pidAlive(state.pid)) { removeIfSame(files.state, state); return null; }
125
+ return state;
126
+ }
127
+ function score(request, manifest, manifestPath, deps = {}) {
128
+ validateRequest(request);
129
+ const files = stateFiles(manifest, manifestPath);
130
+ const state = liveState(files);
131
+ if (!state) return start(manifest, files, deps);
132
+ return outcome(callSync(state.port, { token: state.token, op: 'score', request }), files, state, manifest, deps);
133
+ }
134
+ async function scoreAsync(request, manifest, manifestPath, deps = {}) {
135
+ validateRequest(request);
136
+ const files = stateFiles(manifest, manifestPath);
137
+ const state = liveState(files);
138
+ if (!state) return start(manifest, files, deps);
139
+ return outcome(await callAsync(state.port, { token: state.token, op: 'score', request }), files, state, manifest, deps);
140
+ }
141
+ function outcome(result, files, state, manifest, deps) {
142
+ if (result.reply) {
143
+ if (result.reply.ok === true && Object.hasOwn(result.reply, 'response')) return { status: 'scored', response: result.reply.response };
144
+ if (result.reply.ok === false) return unavailable(result.reply.reason === 'unauthorized' ? 'provider-unavailable' : 'provider-exit');
145
+ return unavailable('provider-json');
146
+ }
147
+ if (result.error === 'refused') { removeIfSame(files.state, state); return start(manifest, files, deps); }
148
+ return unavailable({ timeout: 'provider-timeout', limit: 'provider-output-limit', json: 'provider-json' }[result.error] || 'provider-unavailable');
149
+ }
150
+ function ping(state) {
151
+ const result = callSync(state.port, { token: state.token, op: 'ping' });
152
+ return result.reply && result.reply.ok === true ? result.reply : null;
153
+ }
154
+ function ensureReady(manifest, manifestPath, waitMs, deps = {}) {
155
+ const files = stateFiles(manifest, manifestPath);
156
+ const deadline = Date.now() + waitMs;
157
+ while (Date.now() < deadline) {
158
+ const state = liveState(files);
159
+ if (state && ping(state)) return true;
160
+ if (!state) {
161
+ const lock = lockInfo(files.lock);
162
+ if (lock && lock.failed && lock.ageMs < LOCK_TTL_MS) return false;
163
+ if (!lock || lock.ageMs >= LOCK_TTL_MS) start(manifest, files, deps);
164
+ }
165
+ sleep(100);
166
+ }
167
+ return false;
168
+ }
169
+ function status(manifest, manifestPath) {
170
+ const state = liveState(stateFiles(manifest, manifestPath));
171
+ const reply = state && ping(state);
172
+ return reply ? { running: true, pid: state.pid, port: state.port, startedAt: state.startedAt, model: reply.model } : { running: false };
173
+ }
174
+ function stop(manifest, manifestPath) {
175
+ const files = stateFiles(manifest, manifestPath);
176
+ const state = liveState(files);
177
+ if (!state) return false;
178
+ const result = callSync(state.port, { token: state.token, op: 'shutdown' });
179
+ if (!(result.reply && result.reply.ok === true)) {
180
+ try { process.kill(state.pid); } catch (_) { /* already exited */ }
181
+ removeIfSame(files.state, state);
182
+ }
183
+ return true;
184
+ }
185
+
186
+ if (require.main === module) {
187
+ const [op, manifestPath, extra] = process.argv.slice(2);
188
+ let manifest = null;
189
+ try {
190
+ if (!['start', 'status', 'stop'].includes(op) || extra) throw new Error('usage');
191
+ manifest = require('./provider').readManifest(manifestPath);
192
+ } catch (_) {
193
+ console.error('Usage: resident.js start|status|stop <absolute manifest path>');
194
+ process.exit(2);
195
+ }
196
+ if (op === 'start') process.exitCode = ensureReady(manifest, manifestPath, 60000) ? 0 : 1;
197
+ if (op === 'stop') stop(manifest, manifestPath);
198
+ process.stdout.write(`${JSON.stringify(status(manifest, manifestPath))}\n`);
199
+ }
200
+ module.exports = { stateFiles, readState, callSync, callAsync, score, scoreAsync, ensureReady, status, stop, LOCK_TTL_MS, CALL_TIMEOUT_MS };
@@ -23,7 +23,7 @@ function selectTier(input, env = process.env, scorer = provider.score) {
23
23
  try { result = scorer(request, env.HARNESS_SYSTEM_ONE_CONFIG); }
24
24
  catch (_) { return fallback('provider-unavailable'); }
25
25
  if (!result || result.status !== 'scored') {
26
- const allowed = ['provider-timeout', 'provider-output-limit', 'provider-exit', 'provider-json', 'provider-config', 'provider-unavailable'];
26
+ const allowed = ['provider-timeout', 'provider-output-limit', 'provider-exit', 'provider-json', 'provider-config', 'provider-unavailable', 'provider-starting'];
27
27
  return fallback(allowed.includes(result?.reason) ? result.reason : 'provider-unavailable');
28
28
  }
29
29
  const decision = decide(request, result.response);
@@ -34,4 +34,20 @@ function selectTier(input, env = process.env, scorer = provider.score) {
34
34
  const reason = input.explicit && accepted ? 'explicit-precedence' : belowFloor ? 'structural-floor' : decision.reason;
35
35
  return { tier: applied ? LABELS[decision.selectedId] : input.tier, diagnostic: { mode, ...decision, applied, reason } };
36
36
  }
37
- module.exports = { selectTier, TIER_OPTIONS };
37
+ // Hook entry: score the same fixed-catalog request asynchronously before routing (no sync worker bridge).
38
+ // Returns a scorer for selectTier, or null when System One is off/invalid; nothing is called then.
39
+ async function prescoreTier(prompt, env = process.env, scoreAsync = provider.scoreAsync) {
40
+ if (!['shadow', 'prefer'].includes(env.HARNESS_SYSTEM_ONE_MODE)) return null;
41
+ let request;
42
+ try { request = createRequest('tier', prompt, TIER_OPTIONS); }
43
+ catch (_) { return null; }
44
+ let result; let failed = false;
45
+ try { result = await scoreAsync(request, env.HARNESS_SYSTEM_ONE_CONFIG); }
46
+ catch (_) { failed = true; }
47
+ return (req, config) => {
48
+ if (req.requestHash !== request.requestHash) return provider.score(req, config);
49
+ if (failed) throw new Error('provider-unavailable');
50
+ return result;
51
+ };
52
+ }
53
+ module.exports = { selectTier, prescoreTier, TIER_OPTIONS };