sumo-agents 0.5.0 → 0.7.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/src/verify.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import { execFileSync, spawnSync } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
- import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync } from 'node:fs';
3
+ import { closeSync, existsSync, fstatSync, ftruncateSync, openSync, readFileSync, readSync, renameSync, writeFileSync, writeSync } from 'node:fs';
4
4
  import { join } from 'node:path';
5
5
  import { isSecretPath } from './guard.mjs';
6
6
  import { UsageError } from './memory.mjs';
7
7
  import { secretShape } from './redact.mjs';
8
- import { verifyCommands } from './scan.mjs';
8
+ import { readPlainFile, verifyCommands } from './scan.mjs';
9
9
  import { killGroup } from './tools.mjs';
10
10
 
11
11
  /**
@@ -129,19 +129,14 @@ function timeoutMs() {
129
129
  }
130
130
 
131
131
  /** The last lines of a check's log, read from its end rather than whole. */
132
- function tailOf(file) {
133
- const fd = openSync(file, 'r');
134
- try {
135
- const size = fstatSync(fd).size;
136
- const start = Math.max(0, size - OUTPUT_TAIL_BYTES);
137
- const bytes = Buffer.alloc(size - start);
138
- const lines = bytes.subarray(0, readSync(fd, bytes, 0, bytes.length, start)).toString('utf8').trimEnd().split('\n');
139
- // Read from inside a line, the first piece is half of one.
140
- if (start > 0 && lines.length > 1) lines.shift();
141
- return lines.slice(-OUTPUT_TAIL_LINES).join('\n');
142
- } finally {
143
- closeSync(fd);
144
- }
132
+ function tailOf(fd) {
133
+ const size = fstatSync(fd).size;
134
+ const start = Math.max(0, size - OUTPUT_TAIL_BYTES);
135
+ const bytes = Buffer.alloc(size - start);
136
+ const lines = bytes.subarray(0, readSync(fd, bytes, 0, bytes.length, start)).toString('utf8').trimEnd().split('\n');
137
+ // Read from inside a line, the first piece is half of one.
138
+ if (start > 0 && lines.length > 1) lines.shift();
139
+ return lines.slice(-OUTPUT_TAIL_LINES).join('\n');
145
140
  }
146
141
 
147
142
  /** The sandbox a job's process hands down for its checks: the wrap's words, or null — none given, or one that is not a list of words. */
@@ -167,25 +162,26 @@ function runChecks(root, commands, dir, prefix) {
167
162
  named.set(name, nth);
168
163
  const file = join(dir, `${prefix}-${name}${nth > 1 ? `-${nth}` : ''}.txt`);
169
164
  // Printed into the file, not a pipe: a pipe stays open while anything the check left running holds it, and would be waited on to the time limit.
170
- const into = openSync(file, 'w', 0o600);
171
- let run;
165
+ // Its end is read and written back through the same descriptor, so the file kept is the file the check printed into.
166
+ const into = openSync(file, 'w+', 0o600);
172
167
  try {
173
168
  // A job's checks run the project's code, perhaps tests the model wrote: in the sandbox its shell is in, handed down by the
174
169
  // job's own process (SUMO_CHECKS_WRAP), while this process — which records the verdict in sumo's memory — stays outside it.
175
170
  const wrap = checksWrapOf(process.env);
176
171
  const script = `${watcher}${command}`;
177
- run = wrap
172
+ const run = wrap
178
173
  ? spawnSync(wrap[0], [...wrap.slice(1), '/bin/sh', '-c', script], { cwd: root, detached: true, stdio: ['ignore', into, into], timeout: timeoutMs(), killSignal: 'SIGKILL' })
179
174
  : spawnSync(script, { cwd: root, shell: true, detached: true, stdio: ['ignore', into, into], timeout: timeoutMs(), killSignal: 'SIGKILL' });
175
+ const timedOut = run.error?.code === 'ETIMEDOUT';
176
+ // Whatever it left running goes with it, so nothing writes to the file once it is read.
177
+ killGroup(run.pid);
178
+ const output = tailOf(into);
179
+ ftruncateSync(into);
180
+ writeSync(into, `$ ${command}\n${output}\n${timedOut ? '\n(stopped: it ran past the time limit)\n' : ''}`, 0);
181
+ return { name, command, ok: run.status === 0, status: run.status, timedOut, ms: Date.now() - started, file };
180
182
  } finally {
181
183
  closeSync(into);
182
184
  }
183
- const timedOut = run.error?.code === 'ETIMEDOUT';
184
- // Whatever it left running goes with it, so nothing writes to the file once it is read.
185
- killGroup(run.pid);
186
- const output = tailOf(file);
187
- writeFileSync(file, `$ ${command}\n${output}\n${timedOut ? '\n(stopped: it ran past the time limit)\n' : ''}`, { mode: 0o600 });
188
- return { name, command, ok: run.status === 0, status: run.status, timedOut, ms: Date.now() - started, file };
189
185
  });
190
186
  }
191
187
 
@@ -253,9 +249,9 @@ function scanAdded(root, snap, changes) {
253
249
  // A new file is read whole, and so is one that git never tracked: there is no diff to take its added lines from.
254
250
  for (const created of [...changes.created, ...changes.tracked.filter((c) => c.untracked && c.status === 'M').map((c) => c.path)]) {
255
251
  try {
256
- const full = join(root, created);
257
- if (statSync(full).size > MAX_SCANNED_FILE_BYTES) continue;
258
- readFileSync(full, 'utf8').split('\n').forEach((l, i) => look(`${created}:${i + 1}`, l));
252
+ const text = readPlainFile(join(root, created), MAX_SCANNED_FILE_BYTES);
253
+ if (text === null) continue;
254
+ text.split('\n').forEach((l, i) => look(`${created}:${i + 1}`, l));
259
255
  } catch {
260
256
  // unreadable or binary: nothing to say about it
261
257
  }