@saadnvd1/agent-os 0.3.605 → 0.3.608

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.
@@ -4,6 +4,11 @@
4
4
  # background. Chat turns live in workers and outlive the restart; reviews
5
5
  # and task setups don't, so the restart waits up to 10 minutes for them.
6
6
  # sm add agentos-autodeploy "scripts/autodeploy" --cron "* * * * *"
7
+ # Restarts are spaced AGENTOS_MIN_GAP seconds apart (10 minutes; 0 turns it
8
+ # off): a head that lands inside the gap waits and is deployed, whatever it
9
+ # has become by then, when the gap ends. A commit message with [deploy-now]
10
+ # (in any commit the deploy would take) skips the gap. Each hold logs one line:
11
+ # HELD <sha> <seconds> left in the restart gap (since <last restart sha>)
7
12
  # Another machine (a server) runs it with AGENTOS_PUBLISH=0: deploy
8
13
  # only, and its own AGENTOS_RESTART. Only one machine publishes to npm.
9
14
  set -euo pipefail
@@ -44,6 +49,28 @@ if [ "$head" != "$deployed" ] && [ "$settle" -gt 0 ] &&
44
49
  exit 0
45
50
  fi
46
51
 
52
+ # Restarts kill simulator starts and reviews in flight, so they are spaced
53
+ # apart. The head isn't recorded as deployed while it waits, so it (or a
54
+ # newer one) goes out as soon as the gap is over; nothing is dropped.
55
+ gap="${AGENTOS_MIN_GAP:-600}"
56
+ if [ "$head" != "$deployed" ] && [ "$gap" -gt 0 ]; then
57
+ last=$(cat "$state/restarted" 2>/dev/null || echo 0)
58
+ left=$(( last + gap - $(date +%s) ))
59
+ if [ "$left" -gt 0 ]; then
60
+ msgs=$(git log --format=%B "${deployed:+$deployed..}$head" 2>/dev/null || git log -1 --format=%B "$head")
61
+ if printf '%s' "$msgs" | grep -qF '[deploy-now]'; then
62
+ log "Restart gap skipped for ${head:0:7}: [deploy-now]"
63
+ else
64
+ # Once per head, not once per minute, so the log counts holds.
65
+ if [ "$(cat "$state/held" 2>/dev/null)" != "$head" ]; then
66
+ echo "$head" > "$state/held"
67
+ log "HELD ${head:0:7} ${left}s left in the ${gap}s restart gap"
68
+ fi
69
+ exit 0
70
+ fi
71
+ fi
72
+ fi
73
+
47
74
  # A deploy that can't land says so once per commit, rather than retrying
48
75
  # quietly every minute (uncommitted edits in this checkout block the pull).
49
76
  alert() {
@@ -70,6 +97,7 @@ if [ "$head" != "$deployed" ]; then
70
97
  fi
71
98
  # The pull takes what landed while it waited, too.
72
99
  git rev-parse HEAD > "$state/deployed"
100
+ date +%s > "$state/restarted"
73
101
  fi
74
102
 
75
103
  # Publishing waits minutes for npm to serve a version, so it runs on its own
@@ -0,0 +1,106 @@
1
+ import { execFileSync } from "child_process";
2
+ import * as fs from "fs";
3
+ import * as os from "os";
4
+ import * as path from "path";
5
+ import { describe, expect, it } from "vitest";
6
+
7
+ // Runs the real scripts/autodeploy in a scratch clone with a stub redeploy
8
+ // (a pull) and wait-quiet, against a scratch origin and HOME.
9
+ const script = path.resolve(__dirname, "autodeploy");
10
+ const sh = (cwd: string, ...args: string[]) =>
11
+ execFileSync("git", args, { cwd, encoding: "utf-8" }).trim();
12
+
13
+ function setup() {
14
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "aos-autodeploy-"));
15
+ const origin = path.join(root, "origin.git");
16
+ const work = path.join(root, "work");
17
+ const clone = path.join(root, "live");
18
+ const home = path.join(root, "home");
19
+ fs.mkdirSync(home);
20
+ sh(root, "init", "-q", "--bare", "-b", "main", origin);
21
+ sh(root, "clone", "-q", origin, work);
22
+ fs.mkdirSync(path.join(work, "scripts"));
23
+ fs.copyFileSync(script, path.join(work, "scripts/autodeploy"));
24
+ fs.writeFileSync(path.join(work, "scripts/wait-quiet"), "#!/bin/sh\n");
25
+ fs.writeFileSync(
26
+ path.join(work, "scripts/redeploy"),
27
+ "#!/bin/sh\ngit pull -q origin main && echo x >> ../restarts\n"
28
+ );
29
+ fs.chmodSync(path.join(work, "scripts/autodeploy"), 0o755);
30
+ fs.chmodSync(path.join(work, "scripts/wait-quiet"), 0o755);
31
+ fs.chmodSync(path.join(work, "scripts/redeploy"), 0o755);
32
+ const commit = (msg: string) => {
33
+ fs.appendFileSync(path.join(work, "f"), msg + "\n");
34
+ sh(work, "add", "-A");
35
+ sh(work, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", msg);
36
+ sh(work, "push", "-q", "origin", "main");
37
+ return sh(work, "rev-parse", "HEAD");
38
+ };
39
+ commit("first");
40
+ sh(root, "clone", "-q", origin, clone);
41
+ const state = path.join(home, ".agent-os/autodeploy");
42
+ const run = () =>
43
+ execFileSync(path.join(clone, "scripts/autodeploy"), [], {
44
+ cwd: clone,
45
+ encoding: "utf-8",
46
+ env: {
47
+ ...process.env,
48
+ HOME: home,
49
+ AGENTOS_PUBLISH: "0",
50
+ AGENTOS_SETTLE: "0",
51
+ AGENTOS_RESTART: "true",
52
+ },
53
+ });
54
+ const restarts = () => {
55
+ try {
56
+ return (
57
+ fs.readFileSync(path.join(root, "restarts"), "utf-8").split("\n")
58
+ .length - 1
59
+ );
60
+ } catch {
61
+ return 0;
62
+ }
63
+ };
64
+ const deployed = () =>
65
+ fs.readFileSync(path.join(state, "deployed"), "utf-8").trim();
66
+ return { commit, run, restarts, deployed, state };
67
+ }
68
+
69
+ describe("autodeploy restart gap", () => {
70
+ it("holds a second deploy inside the gap, then deploys the newest head once it ends", () => {
71
+ const t = setup();
72
+ const a = t.commit("a");
73
+ expect(t.run()).toContain("Deploying");
74
+ expect(t.deployed()).toBe(a);
75
+ expect(t.restarts()).toBe(1);
76
+
77
+ t.commit("b");
78
+ const c = t.commit("c");
79
+ const held = t.run();
80
+ expect(held).toMatch(/HELD [0-9a-f]{7} \d+s left in the 600s restart gap/);
81
+ expect(t.deployed()).toBe(a);
82
+ expect(t.restarts()).toBe(1);
83
+ // Still waiting a minute later: no second log line for the same head.
84
+ expect(t.run()).toBe("");
85
+
86
+ // The gap ends: the head that was waiting goes out, once.
87
+ fs.writeFileSync(
88
+ path.join(t.state, "restarted"),
89
+ `${Math.floor(Date.now() / 1000) - 601}\n`
90
+ );
91
+ expect(t.run()).toContain("Deploying");
92
+ expect(t.deployed()).toBe(c);
93
+ expect(t.restarts()).toBe(2);
94
+ expect(t.run()).toBe("");
95
+ });
96
+
97
+ it("skips the gap for a [deploy-now] commit", () => {
98
+ const t = setup();
99
+ t.commit("a");
100
+ t.run();
101
+ const urgent = t.commit("fix: login loop [deploy-now]");
102
+ expect(t.run()).toContain("[deploy-now]");
103
+ expect(t.deployed()).toBe(urgent);
104
+ expect(t.restarts()).toBe(2);
105
+ });
106
+ });
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Times a new worktree's setup and its first typecheck, test and build, and
3
+ * the disk it takes, against a checkout. Run it with the seeding on, then
4
+ * with AGENTOS_NO_BUILD_CACHES=1 (and, to see an install on Linux, --install).
5
+ *
6
+ * npx tsx scripts/bench/worktree-setup.ts <checkout> [--test <file>]
7
+ * [--build] [--install] [--cwd <subdir>] [--drop <seeded path>]
8
+ *
9
+ * It makes a throwaway git worktree of the checkout's HEAD in the temp
10
+ * directory and removes it at the end; it touches nothing else.
11
+ */
12
+
13
+ import { execFileSync, spawnSync } from "child_process";
14
+ import * as fs from "fs";
15
+ import * as os from "os";
16
+ import * as path from "path";
17
+ import { setupWorktree } from "../../lib/env-setup";
18
+
19
+ const args = process.argv.slice(2);
20
+ const flag = (name: string) => args.includes(name);
21
+ const value = (name: string) => args[args.indexOf(name) + 1];
22
+ const source = path.resolve(args[0]);
23
+ const sub = flag("--cwd") ? value("--cwd") : ".";
24
+
25
+ const freeKb = (dir: string) =>
26
+ Number(
27
+ execFileSync("df", ["-Pk", dir], { encoding: "utf-8" })
28
+ .trim()
29
+ .split("\n")[1]
30
+ .split(/\s+/)[3]
31
+ );
32
+
33
+ function timed(command: string, cwd: string) {
34
+ const start = Date.now();
35
+ const run = spawnSync(command, { cwd, shell: true, stdio: "ignore" });
36
+ return { seconds: (Date.now() - start) / 1000, ok: run.status === 0 };
37
+ }
38
+
39
+ async function main() {
40
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "aos-bench-"));
41
+ const worktree = path.join(root, "wt");
42
+ execFileSync("git", ["worktree", "add", "-q", "--detach", worktree, "HEAD"], {
43
+ cwd: source,
44
+ });
45
+ const out: Record<string, unknown> = {
46
+ source: path.basename(source),
47
+ platform: process.platform,
48
+ seeding: process.env.AGENTOS_NO_BUILD_CACHES ? "off" : "on",
49
+ };
50
+ try {
51
+ const before = freeKb(root);
52
+ const start = Date.now();
53
+ let steps: string[] = [];
54
+ if (flag("--install")) {
55
+ const r = timed("npm ci --include=dev", worktree);
56
+ steps = [`npm ci ok=${r.ok}`];
57
+ } else {
58
+ const result = await setupWorktree({
59
+ worktreePath: worktree,
60
+ sourcePath: source,
61
+ });
62
+ steps = result.steps.map(
63
+ (s) => `${s.name}: ${s.success ? "ok" : "failed"}`
64
+ );
65
+ }
66
+ out.setupSeconds = (Date.now() - start) / 1000;
67
+ out.setupSteps = steps;
68
+ out.diskMbAfterSetup = Math.round((before - freeKb(root)) / 1024);
69
+ // Take a seeded cache back out to measure without it.
70
+ if (flag("--drop"))
71
+ fs.rmSync(path.join(worktree, value("--drop")), {
72
+ recursive: true,
73
+ force: true,
74
+ });
75
+ const dir = path.join(worktree, sub);
76
+ const pkg = path.join(dir, "package.json");
77
+ const typecheck = fs.existsSync(pkg)
78
+ ? JSON.parse(fs.readFileSync(pkg, "utf-8")).scripts?.typecheck
79
+ : undefined;
80
+ if (typecheck) out.typecheck = timed("npm run typecheck", dir);
81
+ else if (fs.existsSync(path.join(dir, "tsconfig.json")))
82
+ out.typecheck = timed("npx tsc --noEmit", dir);
83
+ if (flag("--test"))
84
+ out.test = timed(`npx vitest run ${value("--test")}`, dir);
85
+ if (flag("--build")) {
86
+ out.build = timed("npx next build", dir);
87
+ out.diskMbAfterBuild = Math.round((before - freeKb(root)) / 1024);
88
+ }
89
+ } finally {
90
+ execFileSync("git", ["worktree", "remove", "--force", worktree], {
91
+ cwd: source,
92
+ });
93
+ fs.rmSync(root, { recursive: true, force: true });
94
+ }
95
+ console.log(JSON.stringify(out));
96
+ }
97
+
98
+ main().catch((error) => {
99
+ console.error(error);
100
+ process.exit(1);
101
+ });
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env bash
2
2
  # Waits until the AgentOS server on $PORT has nothing in flight that a
3
- # restart would cut off (reviews, task setups: GET /api/busy), for at most
3
+ # restart would cut off (reviews, task setups, simulator starts: GET /api/busy), for at most
4
4
  # AGENTOS_QUIET_WAIT seconds; then returns, so a deploy always lands. A
5
5
  # server that doesn't answer (down, or a build without /api/busy) is quiet.
6
6
  set -uo pipefail