sakura-infinity 0.1.2 → 0.1.3

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,7 @@
1
+ <p align="center">
2
+ <img src="./assets/readme-banner.png" alt="Infinity — Long-horizon research that runs to completion." width="100%" />
3
+ </p>
4
+
1
5
  # Infinity
2
6
 
3
7
  [English](README.md) · [简体中文](README.zh-CN.md)
package/README.zh-CN.md CHANGED
@@ -1,3 +1,7 @@
1
+ <p align="center">
2
+ <img src="./assets/readme-banner.png" alt="Infinity — Long-horizon research that runs to completion." width="100%" />
3
+ </p>
4
+
1
5
  # Infinity
2
6
 
3
7
  [English](README.md) · [简体中文](README.zh-CN.md)
@@ -1,6 +1,6 @@
1
1
  // CPU slots are a resource of the machine, not of one run. Every run that has a slot gate writes down how many slots it holds; a run that starts while
2
2
  // others are live takes only what the cores leave over, so two runs on one machine cannot starve each other by both counting all the cores.
3
- import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
+ import { mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
4
4
  import { availableParallelism, tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
6
  /** One folder per machine (INFINITY_SLOT_LEASE_DIR moves it, for tests). */
@@ -41,22 +41,63 @@ function liveLeases(dir, self) {
41
41
  }
42
42
  return leases;
43
43
  }
44
+ /** A lock older than this was left by a process that died while claiming (a claim takes milliseconds). */
45
+ const STALE_LOCK_MS = 10_000;
46
+ /**
47
+ * Runs `fn` while holding the folder's lock (a directory: mkdir is atomic). Reading the leases and writing one's own must be one step, or two runs
48
+ * starting together both see the cores free and both take them all (formal/SlotLeases.tla). Without the lock after `waitMs`, it goes on anyway:
49
+ * a slot count that is too high is better than a run that cannot start.
50
+ */
51
+ function withLock(dir, fn, waitMs = 2_000) {
52
+ const lock = join(dir, ".lock");
53
+ const deadline = Date.now() + waitMs;
54
+ let held = false;
55
+ for (;;) {
56
+ try {
57
+ mkdirSync(dir, { recursive: true });
58
+ mkdirSync(lock);
59
+ held = true;
60
+ break;
61
+ }
62
+ catch {
63
+ try {
64
+ if (Date.now() - statSync(lock).mtimeMs > STALE_LOCK_MS)
65
+ rmSync(lock, { recursive: true, force: true });
66
+ }
67
+ catch {
68
+ // gone in the meantime
69
+ }
70
+ if (Date.now() > deadline)
71
+ break;
72
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5);
73
+ }
74
+ }
75
+ try {
76
+ return fn();
77
+ }
78
+ finally {
79
+ if (held)
80
+ rmSync(lock, { recursive: true, force: true });
81
+ }
82
+ }
44
83
  /** Takes up to `wanted` slots, leaving room for the slots of the other live runs on this machine (at least one slot is always given). */
45
84
  export function claimSlots(wanted, runDir, options = {}) {
46
85
  const dir = options.dir ?? defaultLeaseDir();
47
86
  const pid = options.pid ?? process.pid;
48
87
  const cores = options.cores ?? availableParallelism();
49
88
  const name = `${pid}-${runDir.replace(/[^A-Za-z0-9_.-]/g, "_").slice(-80)}.json`;
50
- const others = liveLeases(dir, name);
51
- const held = others.reduce((sum, lease) => sum + lease.slots, 0);
52
- const slots = Math.max(1, Math.min(wanted, cores - held));
53
- try {
54
- mkdirSync(dir, { recursive: true });
55
- writeFileSync(join(dir, name), JSON.stringify({ pid, runDir, slots }));
56
- }
57
- catch {
58
- // without a lease the other runs simply do not see this one
59
- }
89
+ const { others, held, slots } = withLock(dir, () => {
90
+ const others = liveLeases(dir, name);
91
+ const held = others.reduce((sum, lease) => sum + lease.slots, 0);
92
+ const slots = Math.max(1, Math.min(wanted, cores - held));
93
+ try {
94
+ writeFileSync(join(dir, name), JSON.stringify({ pid, runDir, slots }));
95
+ }
96
+ catch {
97
+ // without a lease the other runs simply do not see this one
98
+ }
99
+ return { others, held, slots };
100
+ });
60
101
  const note = slots < wanted
61
102
  ? `CPU slots: ${slots} instead of ${wanted}, because ${others.length} other run${others.length === 1 ? "" : "s"} on this machine hold${others.length === 1 ? "s" : ""} ${held} of the ${cores} cores (${others.map((lease) => lease.runDir).join(", ")}).`
62
103
  : undefined;
@@ -91,6 +91,7 @@ export class TaskMode {
91
91
  this.sharedLinks[name] = to;
92
92
  }
93
93
  if (keeper && originals.length > 0) {
94
+ keeper.restore(); // a resumed run: a killed earlier life of this run may still hold them in this keeper
94
95
  keeper.hide(originals);
95
96
  this.hiddenPaths = originals.map((path) => keeper.privatePath(path));
96
97
  this.guard = restoreOnExit(keeper, process, this.options.signal);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sakura-infinity",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "Run many agents in parallel on one task and keep the best result: a code task scored by your evaluator, or a Lean 4 proof. Built on sakura-agent.",
6
6
  "license": "MIT",