@app-vitals/agent-runtime 1.93.1 → 1.94.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/boot.ts +40 -18
- package/package.json +3 -3
- package/plugin-sync-gate.ts +89 -0
- package/plugin-sync-gate.unit.test.ts +147 -0
package/boot.ts
CHANGED
|
@@ -87,17 +87,23 @@ import {
|
|
|
87
87
|
import { HttpChatServiceClient } from "./http-chat-service-client.ts";
|
|
88
88
|
import { buildLogPrefix } from "./log-prefix.ts";
|
|
89
89
|
import { validatePiperVoice } from "./piper-voice.ts";
|
|
90
|
+
import { createPluginSyncGate, trackExecFailures } from "./plugin-sync-gate.ts";
|
|
90
91
|
import {
|
|
91
92
|
createRepoAccessProbe,
|
|
92
93
|
createRepoAccessTokenFor,
|
|
93
94
|
} from "./repo-access-probe.ts";
|
|
95
|
+
import { SCRIPTS_BIN } from "./scripts-bin.ts";
|
|
94
96
|
import {
|
|
95
97
|
createFileSessionStore,
|
|
96
98
|
startSessionPruner,
|
|
97
99
|
threadKey,
|
|
98
100
|
} from "./sessions.ts";
|
|
99
|
-
import {
|
|
100
|
-
|
|
101
|
+
import {
|
|
102
|
+
defaultExec,
|
|
103
|
+
ensureAgentHome,
|
|
104
|
+
installPlugins,
|
|
105
|
+
runMiseStartup,
|
|
106
|
+
} from "./setup.ts";
|
|
101
107
|
import { checkCredentialHelper, setupGitHubAuth } from "./setup-github-auth.ts";
|
|
102
108
|
import { HttpShipwrightRuntimeClient } from "./shipwright-runtime-client.ts";
|
|
103
109
|
import { singleFlight } from "./single-flight.ts";
|
|
@@ -515,6 +521,7 @@ export async function startAgent(
|
|
|
515
521
|
};
|
|
516
522
|
let bundleVoiceKeys = new Set<string>();
|
|
517
523
|
|
|
524
|
+
const pluginSyncGate = createPluginSyncGate();
|
|
518
525
|
async function syncConfig() {
|
|
519
526
|
if (!runtimeClient || !agentId) return;
|
|
520
527
|
try {
|
|
@@ -555,22 +562,37 @@ export async function startAgent(
|
|
|
555
562
|
|
|
556
563
|
// Install agent-specific plugins from bundle (non-fatal)
|
|
557
564
|
// An empty bundle still reconciles (the last row may have been removed).
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
565
|
+
// Gated: only when the plugins or marketplaces changed, or the refresh
|
|
566
|
+
// interval has passed (see plugin-sync-gate.ts).
|
|
567
|
+
const agentPlugins = bundle.plugins ?? [];
|
|
568
|
+
await pluginSyncGate
|
|
569
|
+
.run(
|
|
570
|
+
{ plugins: agentPlugins, marketplaces: bundle.marketplaces },
|
|
571
|
+
async () => {
|
|
572
|
+
// installPlugins never throws and only warns on a failed
|
|
573
|
+
// command, so watch the exit codes: a failed run must retry on
|
|
574
|
+
// the next tick, not wait out the refresh interval.
|
|
575
|
+
const tracked = trackExecFailures(defaultExec);
|
|
576
|
+
await installPlugins(
|
|
577
|
+
tracked.exec,
|
|
578
|
+
undefined,
|
|
579
|
+
agentPlugins,
|
|
580
|
+
undefined,
|
|
581
|
+
undefined,
|
|
582
|
+
undefined,
|
|
583
|
+
undefined,
|
|
584
|
+
undefined,
|
|
585
|
+
bundle.marketplaces,
|
|
586
|
+
);
|
|
587
|
+
return !tracked.failed();
|
|
588
|
+
},
|
|
589
|
+
)
|
|
590
|
+
.catch((err) =>
|
|
591
|
+
console.warn(
|
|
592
|
+
"[config-sync] agent plugin install failed (non-fatal):",
|
|
593
|
+
(err as Error).message,
|
|
594
|
+
),
|
|
595
|
+
);
|
|
574
596
|
|
|
575
597
|
// Sync the agent's scoped repos live ref
|
|
576
598
|
agentReposRef.set(bundle.repos);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.94.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"./workspace-paths": "./workspace-paths.ts"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@app-vitals/agent-admin": "1.
|
|
45
|
-
"@app-vitals/agent-lib": "1.
|
|
44
|
+
"@app-vitals/agent-admin": "1.94.0",
|
|
45
|
+
"@app-vitals/agent-lib": "1.94.0",
|
|
46
46
|
"@octokit/auth-app": "^8.2.0",
|
|
47
47
|
"@sentry/bun": "^10.63.0",
|
|
48
48
|
"@slack/bolt": "^5.0.0",
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decides whether a config-sync tick needs to run the plugin install.
|
|
3
|
+
*
|
|
4
|
+
* The install spawns a `claude plugin …` subprocess per marketplace and per
|
|
5
|
+
* plugin, which costs seconds of CPU even when nothing changed. Running it on
|
|
6
|
+
* every 60s tick kept agents busy re-installing identical plugins and, on a
|
|
7
|
+
* CPU-starved node, pushed a tick past the interval. The gate runs the install
|
|
8
|
+
* when the bundle's plugins or marketplaces change, and otherwise only once per
|
|
9
|
+
* refresh interval so new plugin versions on a remote marketplace still land.
|
|
10
|
+
*
|
|
11
|
+
* A run that throws or reports failure is forgotten, so the next tick retries
|
|
12
|
+
* it. `installPlugins` swallows its own errors (a non-zero `claude plugin` exit
|
|
13
|
+
* only warns), so callers detect failure with `trackExecFailures`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** How often an unchanged bundle is re-installed to pick up plugin updates. */
|
|
17
|
+
export const PLUGIN_REFRESH_INTERVAL_MS = 10 * 60 * 1000;
|
|
18
|
+
|
|
19
|
+
export interface PluginSyncGateOptions {
|
|
20
|
+
refreshIntervalMs?: number;
|
|
21
|
+
now?: () => number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PluginSyncGate {
|
|
25
|
+
/**
|
|
26
|
+
* Runs `install` when `desired` differs from the last successful run or the
|
|
27
|
+
* refresh interval has passed. `install` resolves false to report a failed
|
|
28
|
+
* run. Resolves true when it ran, false when skipped.
|
|
29
|
+
*/
|
|
30
|
+
run(desired: unknown, install: () => Promise<boolean>): Promise<boolean>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createPluginSyncGate(
|
|
34
|
+
options: PluginSyncGateOptions = {},
|
|
35
|
+
): PluginSyncGate {
|
|
36
|
+
const refreshIntervalMs =
|
|
37
|
+
options.refreshIntervalMs ?? PLUGIN_REFRESH_INTERVAL_MS;
|
|
38
|
+
const now = options.now ?? Date.now;
|
|
39
|
+
let lastKey: string | null = null;
|
|
40
|
+
let lastRunAt = 0;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
async run(desired, install) {
|
|
44
|
+
const key = JSON.stringify(desired) ?? "";
|
|
45
|
+
const startedAt = now();
|
|
46
|
+
const unchanged = key === lastKey;
|
|
47
|
+
if (unchanged && startedAt - lastRunAt < refreshIntervalMs) return false;
|
|
48
|
+
let succeeded = false;
|
|
49
|
+
try {
|
|
50
|
+
succeeded = await install();
|
|
51
|
+
} finally {
|
|
52
|
+
if (succeeded) {
|
|
53
|
+
lastKey = key;
|
|
54
|
+
lastRunAt = startedAt;
|
|
55
|
+
} else {
|
|
56
|
+
lastKey = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type Exec<A extends unknown[]> = (
|
|
65
|
+
...args: A
|
|
66
|
+
) => Promise<{ stdout: string; exitCode: number }>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Wraps a subprocess runner so a caller can tell afterwards whether any command
|
|
70
|
+
* exited non-zero or threw. The wrapped runner behaves exactly like `exec`.
|
|
71
|
+
*/
|
|
72
|
+
export function trackExecFailures<A extends unknown[]>(
|
|
73
|
+
exec: Exec<A>,
|
|
74
|
+
): { exec: Exec<A>; failed: () => boolean } {
|
|
75
|
+
let failed = false;
|
|
76
|
+
return {
|
|
77
|
+
exec: async (...args) => {
|
|
78
|
+
try {
|
|
79
|
+
const result = await exec(...args);
|
|
80
|
+
if (result.exitCode !== 0) failed = true;
|
|
81
|
+
return result;
|
|
82
|
+
} catch (err) {
|
|
83
|
+
failed = true;
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
failed: () => failed,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
createPluginSyncGate,
|
|
4
|
+
PLUGIN_REFRESH_INTERVAL_MS,
|
|
5
|
+
trackExecFailures,
|
|
6
|
+
} from "./plugin-sync-gate.ts";
|
|
7
|
+
|
|
8
|
+
function setup(refreshIntervalMs?: number) {
|
|
9
|
+
let clock = 1_000_000;
|
|
10
|
+
let runs = 0;
|
|
11
|
+
const gate = createPluginSyncGate({ refreshIntervalMs, now: () => clock });
|
|
12
|
+
return {
|
|
13
|
+
gate,
|
|
14
|
+
install: async () => {
|
|
15
|
+
runs++;
|
|
16
|
+
return true;
|
|
17
|
+
},
|
|
18
|
+
advance: (ms: number) => {
|
|
19
|
+
clock += ms;
|
|
20
|
+
},
|
|
21
|
+
runs: () => runs,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const bundle = (plugins: string[], marketplaces: string[] = []) => ({
|
|
26
|
+
plugins,
|
|
27
|
+
marketplaces,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("createPluginSyncGate", () => {
|
|
31
|
+
test("runs the install on the first tick", async () => {
|
|
32
|
+
const t = setup();
|
|
33
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(true);
|
|
34
|
+
expect(t.runs()).toBe(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("skips an unchanged bundle inside the refresh interval", async () => {
|
|
38
|
+
const t = setup();
|
|
39
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
40
|
+
t.advance(60_000);
|
|
41
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(false);
|
|
42
|
+
expect(t.runs()).toBe(1);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("runs again as soon as the plugins change", async () => {
|
|
46
|
+
const t = setup();
|
|
47
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
48
|
+
t.advance(60_000);
|
|
49
|
+
expect(await t.gate.run(bundle(["a@m", "b@m"]), t.install)).toBe(true);
|
|
50
|
+
expect(t.runs()).toBe(2);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("runs again as soon as the marketplaces change", async () => {
|
|
54
|
+
const t = setup();
|
|
55
|
+
await t.gate.run(bundle(["a@m"], ["m@main"]), t.install);
|
|
56
|
+
t.advance(60_000);
|
|
57
|
+
expect(await t.gate.run(bundle(["a@m"], ["m@next"]), t.install)).toBe(true);
|
|
58
|
+
expect(t.runs()).toBe(2);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("runs when a plugin is removed, including down to an empty bundle", async () => {
|
|
62
|
+
const t = setup();
|
|
63
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
64
|
+
expect(await t.gate.run(bundle([]), t.install)).toBe(true);
|
|
65
|
+
expect(t.runs()).toBe(2);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("re-runs an unchanged bundle once the refresh interval has passed", async () => {
|
|
69
|
+
const t = setup();
|
|
70
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
71
|
+
t.advance(PLUGIN_REFRESH_INTERVAL_MS - 1);
|
|
72
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(false);
|
|
73
|
+
t.advance(1);
|
|
74
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(true);
|
|
75
|
+
expect(t.runs()).toBe(2);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("measures the refresh interval from the last run, not the last skip", async () => {
|
|
79
|
+
const t = setup(120_000);
|
|
80
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
81
|
+
t.advance(60_000);
|
|
82
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
83
|
+
t.advance(60_000);
|
|
84
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("retries on the next tick after the install throws", async () => {
|
|
88
|
+
const t = setup();
|
|
89
|
+
const failing = async () => {
|
|
90
|
+
throw new Error("claude not found");
|
|
91
|
+
};
|
|
92
|
+
await expect(t.gate.run(bundle(["a@m"]), failing)).rejects.toThrow(
|
|
93
|
+
"claude not found",
|
|
94
|
+
);
|
|
95
|
+
t.advance(60_000);
|
|
96
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(true);
|
|
97
|
+
expect(t.runs()).toBe(1);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("retries on the next tick after the install reports failure", async () => {
|
|
101
|
+
const t = setup();
|
|
102
|
+
expect(await t.gate.run(bundle(["a@m"]), async () => false)).toBe(true);
|
|
103
|
+
t.advance(60_000);
|
|
104
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(true);
|
|
105
|
+
expect(t.runs()).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("a failed refresh of an unchanged bundle also retries next tick", async () => {
|
|
109
|
+
const t = setup();
|
|
110
|
+
await t.gate.run(bundle(["a@m"]), t.install);
|
|
111
|
+
t.advance(PLUGIN_REFRESH_INTERVAL_MS);
|
|
112
|
+
await t.gate.run(bundle(["a@m"]), async () => false);
|
|
113
|
+
t.advance(60_000);
|
|
114
|
+
expect(await t.gate.run(bundle(["a@m"]), t.install)).toBe(true);
|
|
115
|
+
expect(t.runs()).toBe(2);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("trackExecFailures", () => {
|
|
120
|
+
const ok = async (_cmd: string) => ({ stdout: "", exitCode: 0 });
|
|
121
|
+
|
|
122
|
+
test("reports no failure while every command exits 0", async () => {
|
|
123
|
+
const tracked = trackExecFailures(ok);
|
|
124
|
+
await tracked.exec("claude");
|
|
125
|
+
await tracked.exec("claude");
|
|
126
|
+
expect(tracked.failed()).toBe(false);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("reports failure once any command exits non-zero, and passes the result through", async () => {
|
|
130
|
+
const tracked = trackExecFailures(async (cmd: string) => ({
|
|
131
|
+
stdout: cmd,
|
|
132
|
+
exitCode: cmd === "bad" ? 1 : 0,
|
|
133
|
+
}));
|
|
134
|
+
await tracked.exec("good");
|
|
135
|
+
expect(await tracked.exec("bad")).toEqual({ stdout: "bad", exitCode: 1 });
|
|
136
|
+
await tracked.exec("good");
|
|
137
|
+
expect(tracked.failed()).toBe(true);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("reports failure when a command throws, and rethrows", async () => {
|
|
141
|
+
const tracked = trackExecFailures(async (_cmd: string) => {
|
|
142
|
+
throw new Error("spawn ENOENT");
|
|
143
|
+
});
|
|
144
|
+
await expect(tracked.exec("claude")).rejects.toThrow("spawn ENOENT");
|
|
145
|
+
expect(tracked.failed()).toBe(true);
|
|
146
|
+
});
|
|
147
|
+
});
|