@cohortapp/agent-sdk 2.11.15 → 2.13.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/.env.example +37 -22
- package/README.md +2 -0
- package/bin/maestro.mjs +117 -39
- package/bin/maestro.test.mjs +175 -5
- package/docs/guides/front-door-session.md +313 -0
- package/docs/guides/mac-mini.md +100 -28
- package/docs/guides/org-onboarding.md +1 -1
- package/docs/guides/setup-wizard.md +9 -5
- package/docs/runbooks/cohort-cutover.md +11 -1
- package/docs/runbooks/mac-mini-bootstrap.md +38 -63
- package/lib/cadence-bus-requeue.test.mjs +83 -0
- package/lib/cadence-bus.mjs +43 -7
- package/lib/channels/inbox-item.mjs +59 -2
- package/lib/cli/board.mjs +285 -0
- package/lib/cli/board.test.mjs +227 -0
- package/lib/cli/design.mjs +185 -0
- package/lib/cli/design.test.mjs +270 -0
- package/lib/cli/doctor-checks.mjs +441 -0
- package/lib/cli/doctor-checks.test.mjs +336 -0
- package/lib/cli/global-setup-extras.mjs +454 -0
- package/lib/cli/global-setup-extras.test.mjs +462 -0
- package/lib/cli/inbox.mjs +304 -0
- package/lib/cli/inbox.test.mjs +230 -0
- package/lib/cli/session-ack.mjs +63 -0
- package/lib/cli/session-ack.test.mjs +63 -0
- package/lib/cli/session.mjs +760 -0
- package/lib/cli/session.test.mjs +613 -0
- package/lib/collective/global-config.mjs +209 -6
- package/lib/collective/global-config.test.mjs +145 -0
- package/lib/collective/global-skills.mjs +145 -0
- package/lib/collective/global-skills.test.mjs +126 -0
- package/lib/collective/presence.mjs +4 -3
- package/lib/collective/vendor-skills.mjs +305 -0
- package/lib/collective/vendor-skills.test.mjs +306 -0
- package/lib/comms/send-gate.mjs +115 -0
- package/lib/comms/send-gate.test.mjs +113 -0
- package/lib/design/design-md.mjs +793 -0
- package/lib/design/design-md.test.mjs +318 -0
- package/lib/design/fixtures/DESIGN.golden.md +238 -0
- package/lib/design/fixtures/PRODUCT.golden.md +67 -0
- package/lib/design/fixtures/foundation.json +133 -0
- package/lib/design/refresh-gate.mjs +154 -0
- package/lib/design/refresh-gate.test.mjs +144 -0
- package/lib/design/write.mjs +275 -0
- package/lib/design/write.test.mjs +241 -0
- package/lib/feature-init.mjs +2 -2
- package/lib/mcp/server.test.mjs +9 -4
- package/lib/model-router/spawn.test.mjs +21 -0
- package/lib/org/board-mine-cache.mjs +99 -0
- package/lib/org/board-mine-cache.test.mjs +53 -0
- package/lib/org/board.mjs +11 -0
- package/lib/org/board.test.mjs +11 -1
- package/lib/org/client.mjs +36 -0
- package/lib/org/client.test.mjs +46 -0
- package/lib/org/inbound/directedness.mjs +18 -2
- package/lib/org/inbound/directedness.test.mjs +58 -0
- package/lib/org/inbound/index.mjs +8 -1
- package/lib/org/inbound/index.test.mjs +22 -0
- package/lib/org/mesh-directives.test.mjs +110 -0
- package/lib/org/mesh.mjs +61 -1
- package/lib/org/protocol.checksum +1 -1
- package/lib/org/protocol.mjs +52 -0
- package/lib/org/protocol.test.mjs +12 -1
- package/lib/org/registry.mjs +3 -2
- package/lib/org/tool-surface.mjs +120 -0
- package/lib/org/tool-surface.test.mjs +118 -5
- package/lib/prompts/parallelism.mjs +79 -0
- package/lib/prompts/parallelism.test.mjs +177 -0
- package/lib/security/external-content.mjs +1 -1
- package/lib/security/external-content.test.mjs +17 -0
- package/lib/session/config.mjs +137 -0
- package/lib/session/config.test.mjs +92 -0
- package/lib/session/feed-core.mjs +229 -0
- package/lib/session/feed-core.test.mjs +198 -0
- package/lib/session/first-run.mjs +126 -0
- package/lib/session/first-run.test.mjs +121 -0
- package/lib/session/frontdoor.mjs +266 -0
- package/lib/session/frontdoor.test.mjs +205 -0
- package/lib/session/handoffs.mjs +295 -0
- package/lib/session/handoffs.test.mjs +183 -0
- package/lib/session/identity.mjs +220 -0
- package/lib/session/identity.test.mjs +180 -0
- package/lib/session/inbox-claims.mjs +434 -0
- package/lib/session/inbox-claims.test.mjs +286 -0
- package/lib/session/launch-args.mjs +161 -0
- package/lib/session/launch-args.test.mjs +157 -0
- package/lib/session/liveness.mjs +174 -0
- package/lib/session/liveness.test.mjs +100 -0
- package/lib/session/status-summary.mjs +172 -0
- package/lib/session/status-summary.test.mjs +118 -0
- package/lib/session-permissions.mjs +39 -3
- package/lib/session-permissions.test.mjs +20 -0
- package/lib/setup/claude-probe.mjs +161 -24
- package/lib/setup/claude-probe.test.mjs +187 -0
- package/lib/setup/sections/learning.mjs +2 -1
- package/lib/setup/sections/model.mjs +104 -24
- package/lib/setup/sections/model.test.mjs +240 -0
- package/lib/setup/sections/org.mjs +27 -2
- package/lib/setup/sections/org.test.mjs +35 -2
- package/lib/setup/sections/verify.mjs +5 -0
- package/lib/setup/state.mjs +30 -10
- package/lib/setup/state.test.mjs +24 -1
- package/lib/singleton.js +11 -3
- package/lib/singleton.test.mjs +16 -0
- package/lib/subagents/lock.mjs +1 -1
- package/lib/telemetry/collect.mjs +270 -6
- package/lib/telemetry/collect.test.mjs +196 -1
- package/lib/upgrade/global-refresh.mjs +108 -0
- package/lib/upgrade/global-refresh.test.mjs +65 -0
- package/lib/upgrade/launchd-reconcile.mjs +327 -0
- package/lib/upgrade/launchd-reconcile.test.mjs +272 -0
- package/lib/upgrade/post-steps.mjs +151 -0
- package/lib/upgrade/post-steps.test.mjs +200 -0
- package/lib/upgrade/verify.mjs +215 -0
- package/lib/upgrade/verify.test.mjs +164 -0
- package/lib/voice/outbound.mjs +3 -2
- package/lib/voice/post-call-brief.mjs +2 -1
- package/lib/voice/session-rotation.mjs +6 -1
- package/lib/voice/session-rotation.test.mjs +114 -0
- package/package.json +3 -3
- package/plugins/maestro-skills/plugin.json +25 -1
- package/plugins/maestro-skills/skills/board-work.md +63 -0
- package/plugins/maestro-skills/skills/cohort-design.md +153 -0
- package/plugins/maestro-skills/skills/inbound-triage.md +80 -0
- package/plugins/maestro-skills/skills/main-session.md +102 -0
- package/plugins/maestro-skills/skills/peer-sessions.md +65 -0
- package/plugins/maestro-skills/skills/persona-discipline.md +75 -0
- package/plugins/maestro-skills/vendor/emilkowalski/LICENSE +21 -0
- package/plugins/maestro-skills/vendor/emilkowalski/UPSTREAM.json +70 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/animate/RECIPES.md +324 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/animate/SKILL.md +199 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/animation-vocabulary/SKILL.md +173 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/apple-design/SKILL.md +282 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/emil-design-eng/SKILL.md +674 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/find-animation-opportunities/SKILL.md +132 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/improve-animations/AUDIT.md +115 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/improve-animations/PLAN-TEMPLATE.md +73 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/improve-animations/SKILL.md +101 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/prototype/PICKER.md +197 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/prototype/SKILL.md +90 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/review-animations/SKILL.md +112 -0
- package/plugins/maestro-skills/vendor/emilkowalski/skills/review-animations/STANDARDS.md +187 -0
- package/plugins/maestro-skills/vendor/impeccable/LICENSE +191 -0
- package/plugins/maestro-skills/vendor/impeccable/NOTICE.md +11 -0
- package/plugins/maestro-skills/vendor/impeccable/SKILL.md +86 -0
- package/plugins/maestro-skills/vendor/impeccable/UPSTREAM.json +201 -0
- package/plugins/maestro-skills/vendor/impeccable/agents/impeccable-asset-producer.md +42 -0
- package/plugins/maestro-skills/vendor/impeccable/agents/impeccable-documenter.md +29 -0
- package/plugins/maestro-skills/vendor/impeccable/agents/impeccable-finish-reviewer.md +43 -0
- package/plugins/maestro-skills/vendor/impeccable/agents/impeccable-manual-edit-applier.md +97 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/adapt.md +312 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/adapt.native.md +58 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/android.md +46 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/animate.md +89 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/audit.md +136 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/audit.native.md +139 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/bolder.md +33 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/clarify.md +94 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/colorize.md +86 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/craft-floor.md +44 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/craft.md +5 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/critique.md +806 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/degraded/asset-producer.md +37 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/degraded/documenter.md +24 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/degraded/finish-reviewer.md +38 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/degraded/manual-edit-applier.md +92 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/delight.md +70 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/distill.md +111 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/doctor.md +54 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/document.md +416 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/extract.md +69 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/harden.md +336 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/hooks.md +111 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/init.md +131 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/ios.md +51 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/layout.md +84 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/live-setup.md +104 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/live.md +325 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/new-work.md +147 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/onboard.md +234 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/operate.md +61 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/optimize.md +258 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/overdrive.md +127 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/polish.md +105 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/quieter.md +99 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/routing.md +24 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/shape.md +59 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/typeset.md +80 -0
- package/plugins/maestro-skills/vendor/impeccable/reference/visualize.md +46 -0
- package/plugins/maestro-skills/vendor/taste-skill/LICENSE +21 -0
- package/plugins/maestro-skills/vendor/taste-skill/UPSTREAM.json +37 -0
- package/plugins/maestro-skills/vendor/taste-skill/skills/minimalist-skill/SKILL.md +85 -0
- package/plugins/maestro-skills/vendor/taste-skill/skills/redesign-skill/SKILL.md +178 -0
- package/plugins/maestro-skills/vendor/taste-skill/skills/soft-skill/SKILL.md +98 -0
- package/plugins/maestro-skills/vendor/taste-skill/skills/taste-skill/SKILL.md +1206 -0
- package/plugins/maestro-skills/vendor/unlazy/LICENSE +21 -0
- package/plugins/maestro-skills/vendor/unlazy/SECURITY.md +72 -0
- package/plugins/maestro-skills/vendor/unlazy/SKILL.md +104 -0
- package/plugins/maestro-skills/vendor/unlazy/UPSTREAM.json +94 -0
- package/plugins/maestro-skills/vendor/unlazy/references/dispatch.md +82 -0
- package/plugins/maestro-skills/vendor/unlazy/references/gates.md +149 -0
- package/plugins/maestro-skills/vendor/unlazy/references/method.md +49 -0
- package/plugins/maestro-skills/vendor/unlazy/references/orchestration.md +107 -0
- package/plugins/maestro-skills/vendor/unlazy/references/parallel.md +133 -0
- package/plugins/maestro-skills/vendor/unlazy/references/token-economy.md +48 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/dispatch-check.mjs +139 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/gate-check.mjs +960 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/gate-lint.mjs +245 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/lib/check-supervisor.mjs +46 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/lib/dispatch.mjs +293 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/lib/gates.mjs +953 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/lib/process-tree.mjs +161 -0
- package/plugins/maestro-skills/vendor/unlazy/scripts/lib/regex-worker.mjs +9 -0
- package/plugins/maestro-skills/vendor/unlazy/templates/PLAN.md +116 -0
- package/plugins/maestro-skills/vendor/unlazy/templates/gates-leaf.md +51 -0
- package/plugins/maestro-skills/vendor/unlazy/templates/gates-node.md +51 -0
- package/scaffold/CLAUDE.md +24 -0
- package/scripts/ci/check-durable-write-seam.mjs +147 -0
- package/scripts/ci/check-durable-write-seam.test.mjs +90 -0
- package/scripts/ci/check-skill-packs.mjs +388 -0
- package/scripts/ci/check-skill-packs.test.mjs +495 -0
- package/scripts/ci/check.mjs +6 -0
- package/scripts/collective/hook-runner.mjs +39 -4
- package/scripts/collective/hook-runner.test.mjs +85 -2
- package/scripts/daemon/agent-daemon-board-mine.test.mjs +96 -0
- package/scripts/daemon/agent-daemon-design.test.mjs +238 -0
- package/scripts/daemon/agent-daemon-frontdoor.test.mjs +60 -0
- package/scripts/daemon/agent-daemon.mjs +249 -10
- package/scripts/daemon/agent-daemon.test.mjs +73 -0
- package/scripts/daemon/assurance-e2e.test.mjs +141 -6
- package/scripts/daemon/assurance.mjs +461 -37
- package/scripts/daemon/assurance.test.mjs +408 -43
- package/scripts/daemon/cadence-consumer-frontdoor.test.mjs +393 -0
- package/scripts/daemon/cadence-consumer.mjs +289 -89
- package/scripts/daemon/cadence-handlers.mjs +53 -0
- package/scripts/daemon/classifier.mjs +1 -1
- package/scripts/daemon/dispatcher-resume.test.mjs +166 -0
- package/scripts/daemon/dispatcher.mjs +127 -19
- package/scripts/daemon/health.mjs +12 -1
- package/scripts/daemon/inbox-deferral-session.test.mjs +49 -0
- package/scripts/daemon/inbox-deferral.mjs +6 -0
- package/scripts/daemon/lib/self-echo.mjs +201 -0
- package/scripts/daemon/lib/self-echo.test.mjs +153 -0
- package/scripts/daemon/maestro-daemon.mjs +3 -0
- package/scripts/daemon/prompt-builder.mjs +19 -3
- package/scripts/daemon/responder.mjs +51 -40
- package/scripts/daemon/sdk-version.mjs +51 -0
- package/scripts/daemon/sdk-version.test.mjs +31 -0
- package/scripts/hooks/pre-send-audit.sh +97 -4
- package/scripts/hooks/pre-send-audit.test.mjs +140 -1
- package/scripts/local-triggers/autoupdate.sh +243 -19
- package/scripts/local-triggers/autoupdate.test.mjs +518 -0
- package/scripts/local-triggers/generate-plists.sh +24 -1
- package/scripts/local-triggers/generate-plists.test.mjs +49 -11
- package/scripts/org/send-orgmail.first-contact.test.mjs +102 -0
- package/scripts/org/send-orgmail.mjs +27 -3
- package/scripts/poller/inbox-privilege-injection.test.mjs +167 -0
- package/scripts/poller/slack-poller.mjs +13 -1
- package/scripts/poller/utils.mjs +46 -1
- package/scripts/poller-launchd/install.sh +19 -11
- package/scripts/poller-launchd/install.test.mjs +243 -0
- package/scripts/poller-launchd/launchd-poller-wrapper.sh +92 -0
- package/scripts/poller-launchd/migrate.sh +66 -0
- package/scripts/poller-launchd/poller.plist.template +4 -2
- package/scripts/session/feed.mjs +237 -0
- package/scripts/session/feed.test.mjs +196 -0
- package/scripts/session/supervisor-sh.test.mjs +218 -0
- package/scripts/session/supervisor.mjs +328 -0
- package/scripts/session/supervisor.sh +141 -0
- package/scripts/session/supervisor.test.mjs +482 -0
- package/scripts/setup/configure-macos.sh +250 -55
- package/scripts/setup/configure-macos.test.mjs +306 -0
- package/scripts/setup/init-agent.sh +112 -7
- package/scripts/setup/init-agent.test.mjs +220 -1
- package/scripts/vendor/skill-packs.mjs +354 -0
- package/scripts/vendor/sync-skill-packs.mjs +242 -0
- package/scripts/vendor/sync-skill-packs.test.mjs +103 -0
- package/scripts/watchdog/memory-watchdog.sh +37 -1
- package/scripts/watchdog/memory-watchdog.test.mjs +64 -0
- package/scripts/setup/boot-claude-session.sh +0 -94
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autoupdate.test.mjs — scripts/local-triggers/autoupdate.sh
|
|
3
|
+
*
|
|
4
|
+
* Front-door session design §3.10: a fleet upgrade must NOT kill the agent's
|
|
5
|
+
* main interactive session. After a successful, health-gated upgrade the
|
|
6
|
+
* script writes state/session/upgrade-notice.json {from,to,at}; the session's
|
|
7
|
+
* feed surfaces it as a directive and the session restarts ITSELF at an idle
|
|
8
|
+
* moment (`maestro session restart`). The daemon health gate + rollback stay
|
|
9
|
+
* exactly as before.
|
|
10
|
+
*
|
|
11
|
+
* Everything the script shells out to (npm, launchctl, pgrep) is a stub on
|
|
12
|
+
* PATH that logs its argv; `node` is the real one (the script uses `node -p`
|
|
13
|
+
* to read the installed version). Jitter and the health wait are zeroed via
|
|
14
|
+
* the MAESTRO_AUTOUPDATE_JITTER_MAX / MAESTRO_AUTOUPDATE_HEALTH_WAIT knobs.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { test } from "node:test";
|
|
18
|
+
import assert from "node:assert/strict";
|
|
19
|
+
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, readdirSync, existsSync, chmodSync, rmSync } from "node:fs";
|
|
20
|
+
import { spawnSync } from "node:child_process";
|
|
21
|
+
import { join, dirname } from "node:path";
|
|
22
|
+
import { tmpdir } from "node:os";
|
|
23
|
+
import { fileURLToPath } from "node:url";
|
|
24
|
+
|
|
25
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
26
|
+
const SCRIPT = join(__dirname, "autoupdate.sh");
|
|
27
|
+
const scriptSrc = readFileSync(SCRIPT, "utf8");
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Today, in the SAME basis the code under test uses: `date +%Y-%m-%d`, which is
|
|
31
|
+
* LOCAL. Both log files this suite fabricates are read back by shell that names
|
|
32
|
+
* them that way — `autoupdate.sh` for its own log, and for the daemon log the
|
|
33
|
+
* health gate tails (which `scripts/daemon/launchd-wrapper.sh` also writes with
|
|
34
|
+
* local `date`).
|
|
35
|
+
*
|
|
36
|
+
* This used to be `new Date().toISOString().slice(0, 10)` — UTC — and the two
|
|
37
|
+
* agree for exactly as long as the machine sits at or west of Greenwich. On a
|
|
38
|
+
* UTC+4 developer machine they diverge from 20:00 to midnight local, so the
|
|
39
|
+
* fixture wrote `daemon-<yesterday>.log`, the health gate looked for
|
|
40
|
+
* `daemon-<today>.log`, found nothing, and all twelve upgrade cases failed with
|
|
41
|
+
* "UNHEALTHY … ROLLBACK" — a four-hour window each evening in which a green
|
|
42
|
+
* suite turned red with no code change. Keep the basis matched to the shell.
|
|
43
|
+
*/
|
|
44
|
+
function localDay(d = new Date()) {
|
|
45
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
46
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function stub(bin, name, body) {
|
|
50
|
+
writeFileSync(join(bin, name), `#!/bin/bash\n${body}\n`);
|
|
51
|
+
chmodSync(join(bin, name), 0o755);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Build a fixture: an agent dir with the SDK "installed" at 1.0.0, a healthy
|
|
56
|
+
* daemon log, a HOME with daemon + session plists, and stubs for npm/launchctl/pgrep.
|
|
57
|
+
*/
|
|
58
|
+
function fixture({ latest = "2.0.0", healthy = true, supervisorAlive = true, sessionPlist = false, loadedLabels = ["ai.maestro.ivy-daemon"], npmViewFailures = 0, npmInstallFailures = 0, firstName = null, installedSessionPlist = true } = {}) {
|
|
59
|
+
const root = mkdtempSync(join(tmpdir(), "autoupdate-test-"));
|
|
60
|
+
const agent = join(root, "agent");
|
|
61
|
+
const home = join(root, "home");
|
|
62
|
+
const bin = join(root, "bin");
|
|
63
|
+
const log = join(root, "calls.log");
|
|
64
|
+
mkdirSync(join(agent, "node_modules", "@cohortapp", "agent-sdk", "bin"), { recursive: true });
|
|
65
|
+
mkdirSync(join(agent, "logs", "daemon"), { recursive: true });
|
|
66
|
+
mkdirSync(join(agent, "scripts", "local-triggers"), { recursive: true });
|
|
67
|
+
mkdirSync(join(home, "Library", "LaunchAgents"), { recursive: true });
|
|
68
|
+
mkdirSync(bin, { recursive: true });
|
|
69
|
+
|
|
70
|
+
writeFileSync(join(agent, "node_modules", "@cohortapp", "agent-sdk", "package.json"), JSON.stringify({ version: "1.0.0" }));
|
|
71
|
+
writeFileSync(join(agent, "node_modules", "@cohortapp", "agent-sdk", "bin", "maestro.mjs"), "process.exit(0);\n");
|
|
72
|
+
const day = localDay();
|
|
73
|
+
writeFileSync(join(agent, "logs", "daemon", `daemon-${day}.log`), "[daemon] Running\n");
|
|
74
|
+
writeFileSync(join(home, "Library", "LaunchAgents", "ai.maestro.ivy-daemon.plist"), "<plist/>");
|
|
75
|
+
if (installedSessionPlist) writeFileSync(join(home, "Library", "LaunchAgents", "ai.maestro.ivy-session.plist"), "<plist/>");
|
|
76
|
+
writeFileSync(join(agent, "scripts", "local-triggers", "autoupdate.sh"), scriptSrc);
|
|
77
|
+
if (firstName) { mkdirSync(join(agent, "config"), { recursive: true }); writeFileSync(join(agent, "config", "agent.json"), JSON.stringify({ firstName })); }
|
|
78
|
+
// A generated -session plist (what the NEW upgrade renders) turns the
|
|
79
|
+
// session half of the health gate on.
|
|
80
|
+
mkdirSync(join(agent, "scripts", "local-triggers", "plists"), { recursive: true });
|
|
81
|
+
if (sessionPlist) writeFileSync(join(agent, "scripts", "local-triggers", "plists", "ai.maestro.ivy-session.plist"), "<plist/>");
|
|
82
|
+
|
|
83
|
+
// npm: `view <pkg> version` prints latest; `install <pkg>@v` bumps the
|
|
84
|
+
// installed package.json to the requested version (so a rollback is
|
|
85
|
+
// observable); `install -g …` only logs. The first N view/install calls can
|
|
86
|
+
// be made to fail so the retry/backoff path is observable.
|
|
87
|
+
stub(bin, "npm", `echo "npm $*" >> "${log}"
|
|
88
|
+
count(){ grep -c "^npm $1" "${log}" 2>/dev/null || echo 0; }
|
|
89
|
+
if [ "$1" = "view" ]; then
|
|
90
|
+
if [ "$(count view)" -le ${npmViewFailures} ]; then echo "npm ERR! network" >&2; exit 1; fi
|
|
91
|
+
echo "${latest}"; exit 0
|
|
92
|
+
fi
|
|
93
|
+
if [ "$1" = "install" ]; then
|
|
94
|
+
if [ "$2" = "-g" ]; then exit 0; fi
|
|
95
|
+
if [ "$(count install)" -le ${npmInstallFailures} ]; then echo "npm ERR! network" >&2; exit 1; fi
|
|
96
|
+
v="\${2##*@}"
|
|
97
|
+
printf '{"version":"%s"}' "$v" > "${join(agent, "node_modules", "@cohortapp", "agent-sdk", "package.json")}"
|
|
98
|
+
exit 0
|
|
99
|
+
fi
|
|
100
|
+
exit 0`);
|
|
101
|
+
// launchctl: `list` prints the loaded labels; `print gui/<uid>/<label>`
|
|
102
|
+
// answers per label the way launchd does; everything else only logs.
|
|
103
|
+
stub(bin, "launchctl", `echo "launchctl $*" >> "${log}"
|
|
104
|
+
if [ "$1" = "list" ]; then
|
|
105
|
+
if [ -n "\${2:-}" ]; then for k in ${loadedLabels.join(" ")}; do [ "$k" = "$2" ] && exit 0; done; exit 113; fi
|
|
106
|
+
printf 'PID\tStatus\tLabel\n'; for l in ${loadedLabels.join(" ")}; do printf -- '-\t0\t%s\n' "$l"; done
|
|
107
|
+
fi
|
|
108
|
+
if [ "$1" = "print" ]; then l="\${2##*/}"; for k in ${loadedLabels.join(" ")}; do [ "$k" = "$l" ] && { echo "$l = {}"; exit 0; }; done; echo "Could not find service \"$l\" in domain for user gui: 501" >&2; exit 113; fi
|
|
109
|
+
exit 0`);
|
|
110
|
+
// pgrep: the daemon pattern answers \`healthy\`; the supervisor pattern answers \`supervisorAlive\`.
|
|
111
|
+
stub(bin, "pgrep", `echo "pgrep $*" >> "${log}"
|
|
112
|
+
case "$*" in *supervisor*) exit ${supervisorAlive ? 0 : 1} ;; esac
|
|
113
|
+
exit ${healthy ? 0 : 1}`);
|
|
114
|
+
// Any kill-ish tool reaching a stub is a test failure signal, not a no-op.
|
|
115
|
+
for (const k of ["pkill", "kill", "killall"]) stub(bin, k, `echo "${k} $*" >> "${log}"; exit 0`);
|
|
116
|
+
|
|
117
|
+
return { root, agent, home, bin, log };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function run(fx, extraEnv) {
|
|
121
|
+
return spawnSync("/bin/bash", [join(fx.agent, "scripts", "local-triggers", "autoupdate.sh")], {
|
|
122
|
+
encoding: "utf8",
|
|
123
|
+
env: {
|
|
124
|
+
HOME: fx.home,
|
|
125
|
+
AGENT_ROOT: fx.agent,
|
|
126
|
+
PATH: `${fx.bin}:${dirname(process.execPath)}:/usr/bin:/bin`,
|
|
127
|
+
MAESTRO_AUTOUPDATE_REEXEC: "1",
|
|
128
|
+
// The script prepends the standard tool dirs to PATH; this prefix wins
|
|
129
|
+
// over them so the stubs — never the real npm/launchctl — are reached.
|
|
130
|
+
MAESTRO_AUTOUPDATE_PATH_PREFIX: fx.bin,
|
|
131
|
+
// The child shell names its log files with `date +%Y-%m-%d` and this
|
|
132
|
+
// suite fabricates those names with `localDay()`. `env` here is an
|
|
133
|
+
// explicit whitelist, so without this line the shell inherits NO TZ and
|
|
134
|
+
// falls back to the system zone while Node honours the ambient one — the
|
|
135
|
+
// two then disagree whenever a run sets TZ (e.g. `TZ=UTC npm test` on a
|
|
136
|
+
// UTC+4 machine), and every upgrade case fails on a log file neither
|
|
137
|
+
// side is wrong about. Pass the zone through so both agree by construction.
|
|
138
|
+
...(process.env.TZ ? { TZ: process.env.TZ } : {}),
|
|
139
|
+
MAESTRO_AUTOUPDATE_JITTER_MAX: "0",
|
|
140
|
+
MAESTRO_AUTOUPDATE_HEALTH_WAIT: "0",
|
|
141
|
+
MAESTRO_AUTOUPDATE_RETRY_SLEEP: "0",
|
|
142
|
+
// Never a real `npm install -g` from a test — the script logs the
|
|
143
|
+
// command it would have run instead.
|
|
144
|
+
MAESTRO_SKIP_GLOBAL_INSTALL: "1",
|
|
145
|
+
...(extraEnv || {}),
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const calls = (fx) => (existsSync(fx.log) ? readFileSync(fx.log, "utf8") : "");
|
|
151
|
+
const noticePath = (fx) => join(fx.agent, "state", "session", "upgrade-notice.json");
|
|
152
|
+
const lastPath = (fx) => join(fx.agent, "state", "autoupdate", "last.json");
|
|
153
|
+
const readLast = (fx) => JSON.parse(readFileSync(lastPath(fx), "utf8"));
|
|
154
|
+
const scriptLog = (fx) => {
|
|
155
|
+
const dir = join(fx.agent, "logs", "autoupdate");
|
|
156
|
+
try { return readFileSync(join(dir, `${localDay()}.log`), "utf8"); } catch { return ""; }
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
test("a healthy upgrade writes state/session/upgrade-notice.json {from,to,at} and never touches the session job", () => {
|
|
160
|
+
const fx = fixture();
|
|
161
|
+
try {
|
|
162
|
+
const r = run(fx);
|
|
163
|
+
assert.equal(r.status, 0, r.stderr + r.stdout);
|
|
164
|
+
assert.ok(existsSync(noticePath(fx)), "upgrade-notice.json must be written after a healthy upgrade");
|
|
165
|
+
const notice = JSON.parse(readFileSync(noticePath(fx), "utf8"));
|
|
166
|
+
assert.equal(notice.from, "1.0.0");
|
|
167
|
+
assert.equal(notice.to, "2.0.0");
|
|
168
|
+
assert.match(notice.at, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/, "at must be an ISO-8601 UTC timestamp");
|
|
169
|
+
const c = calls(fx);
|
|
170
|
+
// The daemon IS restarted (health gate unchanged) …
|
|
171
|
+
assert.match(c, /^launchctl kickstart -k gui\/\d+\/ai\.maestro\.ivy-daemon$/m, `daemon must be kickstarted; calls:\n${c}`);
|
|
172
|
+
// … the session is NOT: no kickstart/bootout of the session label, no kill.
|
|
173
|
+
assert.ok(!/-session/.test(c), `must not touch the session job; calls:\n${c}`);
|
|
174
|
+
assert.ok(!/^(pkill|kill|killall) /m.test(c), `must not kill anything; calls:\n${c}`);
|
|
175
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("an unhealthy upgrade rolls back and leaves NO upgrade notice", () => {
|
|
179
|
+
const fx = fixture({ healthy: false });
|
|
180
|
+
try {
|
|
181
|
+
run(fx);
|
|
182
|
+
assert.ok(!existsSync(noticePath(fx)), "no notice after a rollback — the session must not restart onto a bad version");
|
|
183
|
+
const installed = JSON.parse(readFileSync(join(fx.agent, "node_modules", "@cohortapp", "agent-sdk", "package.json"), "utf8"));
|
|
184
|
+
assert.equal(installed.version, "1.0.0", "rollback must reinstall the prior version");
|
|
185
|
+
assert.ok(!/-session/.test(calls(fx)));
|
|
186
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("up to date: no install, no restart, no notice", () => {
|
|
190
|
+
const fx = fixture({ latest: "1.0.0" });
|
|
191
|
+
try {
|
|
192
|
+
const r = run(fx);
|
|
193
|
+
assert.equal(r.status, 0);
|
|
194
|
+
assert.ok(!existsSync(noticePath(fx)));
|
|
195
|
+
assert.ok(!/npm install/.test(calls(fx)));
|
|
196
|
+
assert.ok(!/launchctl/.test(calls(fx)));
|
|
197
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("source: the daemon health gate is intact and the session label is never kickstarted or killed", () => {
|
|
201
|
+
assert.match(scriptSrc, /is_healthy\(\)/, "health gate function must remain");
|
|
202
|
+
assert.match(scriptSrc, /ROLLBACK/, "rollback path must remain");
|
|
203
|
+
assert.ok(!/kickstart[^\n]*-session/.test(scriptSrc), "must not kickstart the session job");
|
|
204
|
+
// The ONE bootout in the script is the rollback's revert of a session job
|
|
205
|
+
// THIS RUN installed (guarded by the PRE_SESSION_PLISTS snapshot). Nothing
|
|
206
|
+
// else may bootout, and never a pre-existing job.
|
|
207
|
+
assert.equal((scriptSrc.match(/launchctl bootout/g) || []).length, 1, "exactly one bootout: the rollback revert");
|
|
208
|
+
assert.match(scriptSrc, /revert_session_install\(\)\{[^]*?PRE_SESSION_PLISTS[^]*?launchctl bootout "gui\/\$UID_\/\$label"/, "the bootout lives inside revert_session_install, behind the pre-run snapshot");
|
|
209
|
+
assert.ok(!/\b(pkill|killall)\b/.test(scriptSrc), "must not kill processes");
|
|
210
|
+
assert.match(scriptSrc, /upgrade-notice\.json/, "must write the upgrade notice");
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test("a healthy upgrade runs scripts/poller-launchd/migrate.sh (audit F6 follow-through); a rollback does not", () => {
|
|
214
|
+
const fx = fixture();
|
|
215
|
+
try {
|
|
216
|
+
mkdirSync(join(fx.agent, "scripts", "poller-launchd"), { recursive: true });
|
|
217
|
+
writeFileSync(join(fx.agent, "scripts", "poller-launchd", "migrate.sh"), `#!/bin/bash\necho "migrate $*" >> "${fx.log}"\n`);
|
|
218
|
+
chmodSync(join(fx.agent, "scripts", "poller-launchd", "migrate.sh"), 0o755);
|
|
219
|
+
const r = run(fx);
|
|
220
|
+
assert.equal(r.status, 0, r.stderr + r.stdout);
|
|
221
|
+
assert.match(calls(fx), /^migrate/m, `expected the poller migration after a healthy upgrade; calls:\n${calls(fx)}`);
|
|
222
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
223
|
+
const bad = fixture({ healthy: false });
|
|
224
|
+
try {
|
|
225
|
+
mkdirSync(join(bad.agent, "scripts", "poller-launchd"), { recursive: true });
|
|
226
|
+
writeFileSync(join(bad.agent, "scripts", "poller-launchd", "migrate.sh"), `#!/bin/bash\necho "migrate $*" >> "${bad.log}"\n`);
|
|
227
|
+
chmodSync(join(bad.agent, "scripts", "poller-launchd", "migrate.sh"), 0o755);
|
|
228
|
+
run(bad);
|
|
229
|
+
assert.ok(!/^migrate/m.test(calls(bad)), "no migration on a rolled-back upgrade");
|
|
230
|
+
} finally { rmSync(bad.root, { recursive: true, force: true }); }
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
// WP-M6 hardening: lock, retries, session half of the health gate, last.json,
|
|
235
|
+
// rollback refreshes the global install, no daemon plist → still upgrade.
|
|
236
|
+
// ---------------------------------------------------------------------------
|
|
237
|
+
|
|
238
|
+
test("a healthy upgrade writes state/autoupdate/last.json {from,to,at,ok,healthy,reason} and releases the overlap lock", () => {
|
|
239
|
+
const fx = fixture();
|
|
240
|
+
try {
|
|
241
|
+
const r = run(fx);
|
|
242
|
+
assert.equal(r.status, 0, r.stderr + r.stdout);
|
|
243
|
+
const last = readLast(fx);
|
|
244
|
+
assert.equal(last.from, "1.0.0");
|
|
245
|
+
assert.equal(last.to, "2.0.0");
|
|
246
|
+
assert.equal(last.ok, true);
|
|
247
|
+
assert.equal(last.healthy, true);
|
|
248
|
+
assert.equal(last.reason, "");
|
|
249
|
+
assert.match(last.at, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/);
|
|
250
|
+
assert.ok(!existsSync(join(fx.agent, "state", "locks", "autoupdate")), "lock released on exit");
|
|
251
|
+
assert.match(calls(fx), /^npm install @cohortapp\/agent-sdk@2\.0\.0 --save --no-audit --no-fund --prefer-online$/m, calls(fx));
|
|
252
|
+
assert.match(calls(fx), /^npm view @cohortapp\/agent-sdk version --no-audit --no-fund --prefer-online$/m);
|
|
253
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("overlap lock: a live holder younger than the stale bar makes the run skip; a dead holder is reclaimed", () => {
|
|
257
|
+
const live = fixture();
|
|
258
|
+
try {
|
|
259
|
+
mkdirSync(join(live.agent, "state", "locks", "autoupdate"), { recursive: true });
|
|
260
|
+
writeFileSync(join(live.agent, "state", "locks", "autoupdate", "pid"), String(process.pid));
|
|
261
|
+
const r = run(live);
|
|
262
|
+
assert.equal(r.status, 0);
|
|
263
|
+
assert.ok(!/npm view/.test(calls(live)), "must not even check npm while another run holds the lock");
|
|
264
|
+
assert.match(scriptLog(live), /SKIP: another autoupdate \(pid \d+\) holds/);
|
|
265
|
+
assert.ok(existsSync(join(live.agent, "state", "locks", "autoupdate")), "the other run's lock is left in place");
|
|
266
|
+
} finally { rmSync(live.root, { recursive: true, force: true }); }
|
|
267
|
+
const dead = fixture();
|
|
268
|
+
try {
|
|
269
|
+
mkdirSync(join(dead.agent, "state", "locks", "autoupdate"), { recursive: true });
|
|
270
|
+
writeFileSync(join(dead.agent, "state", "locks", "autoupdate", "pid"), "999999");
|
|
271
|
+
const r = run(dead);
|
|
272
|
+
assert.equal(r.status, 0, r.stderr);
|
|
273
|
+
assert.match(scriptLog(dead), /reclaiming stale autoupdate lock/);
|
|
274
|
+
assert.match(calls(dead), /npm install/, "the reclaimed run proceeds");
|
|
275
|
+
assert.ok(!existsSync(join(dead.agent, "state", "locks", "autoupdate")));
|
|
276
|
+
} finally { rmSync(dead.root, { recursive: true, force: true }); }
|
|
277
|
+
const stale = fixture();
|
|
278
|
+
try {
|
|
279
|
+
mkdirSync(join(stale.agent, "state", "locks", "autoupdate"), { recursive: true });
|
|
280
|
+
writeFileSync(join(stale.agent, "state", "locks", "autoupdate", "pid"), String(process.pid));
|
|
281
|
+
const r = run(stale, { MAESTRO_AUTOUPDATE_LOCK_STALE_S: "0" });
|
|
282
|
+
assert.equal(r.status, 0, r.stderr);
|
|
283
|
+
assert.match(scriptLog(stale), /reclaiming stale autoupdate lock/, "a holder older than the stale bar is reclaimed even when alive");
|
|
284
|
+
} finally { rmSync(stale.root, { recursive: true, force: true }); }
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test("npm view and npm install are retried 3x with backoff; a third failure aborts with last.json reason install-failed and no notice", () => {
|
|
288
|
+
const flaky = fixture({ npmViewFailures: 2, npmInstallFailures: 2 });
|
|
289
|
+
try {
|
|
290
|
+
const r = run(flaky);
|
|
291
|
+
assert.equal(r.status, 0, r.stderr);
|
|
292
|
+
const c = calls(flaky);
|
|
293
|
+
assert.equal((c.match(/^npm view/gm) || []).length, 3, `3 view attempts:\n${c}`);
|
|
294
|
+
assert.equal((c.match(/^npm install @cohortapp/gm) || []).length, 3, `3 install attempts:\n${c}`);
|
|
295
|
+
assert.match(scriptLog(flaky), /npm view: attempt 1 failed; retrying in 0s/);
|
|
296
|
+
assert.match(scriptLog(flaky), /attempt 2 failed; retrying/);
|
|
297
|
+
assert.ok(existsSync(noticePath(flaky)), "third attempt succeeded → normal healthy path");
|
|
298
|
+
} finally { rmSync(flaky.root, { recursive: true, force: true }); }
|
|
299
|
+
const dead = fixture({ npmInstallFailures: 3 });
|
|
300
|
+
try {
|
|
301
|
+
const r = run(dead);
|
|
302
|
+
assert.equal(r.status, 1);
|
|
303
|
+
assert.equal((calls(dead).match(/^npm install @cohortapp/gm) || []).length, 3);
|
|
304
|
+
assert.ok(!existsSync(noticePath(dead)));
|
|
305
|
+
assert.ok(!/launchctl kickstart/.test(calls(dead)), "no restart when nothing was installed");
|
|
306
|
+
const last = readLast(dead);
|
|
307
|
+
assert.equal(last.ok, false);
|
|
308
|
+
assert.equal(last.healthy, false);
|
|
309
|
+
assert.equal(last.reason, "install-failed");
|
|
310
|
+
} finally { rmSync(dead.root, { recursive: true, force: true }); }
|
|
311
|
+
const offline = fixture({ npmViewFailures: 3 });
|
|
312
|
+
try {
|
|
313
|
+
const r = run(offline);
|
|
314
|
+
assert.equal(r.status, 0);
|
|
315
|
+
assert.ok(!/npm install/.test(calls(offline)));
|
|
316
|
+
assert.match(scriptLog(offline), /npm view failed after 3 attempts/);
|
|
317
|
+
} finally { rmSync(offline.root, { recursive: true, force: true }); }
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("session half of the health gate: with a generated -session plist, a loaded label + live supervisor is fully healthy; absence is logged as reconcile-failed but does NOT roll back", () => {
|
|
321
|
+
const ok = fixture({ sessionPlist: true, loadedLabels: ["ai.maestro.ivy-daemon", "ai.maestro.ivy-session"] });
|
|
322
|
+
try {
|
|
323
|
+
run(ok);
|
|
324
|
+
const last = readLast(ok);
|
|
325
|
+
assert.equal(last.ok, true);
|
|
326
|
+
assert.equal(last.reason, "");
|
|
327
|
+
assert.match(calls(ok), /^launchctl print gui\/\d+\/ai\.maestro\.ivy-session$/m, "loaded-ness is asked in the gui domain (right from ssh too)");
|
|
328
|
+
assert.ok(!/^launchctl list/m.test(calls(ok)), "no domain-less list when print answered");
|
|
329
|
+
assert.match(calls(ok), new RegExp(`^pgrep -f ${ok.agent.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/scripts/session/supervisor$`, "m"), "the supervisor question is scoped to THIS agent dir");
|
|
330
|
+
assert.ok(existsSync(noticePath(ok)));
|
|
331
|
+
} finally { rmSync(ok.root, { recursive: true, force: true }); }
|
|
332
|
+
const notLoaded = fixture({ sessionPlist: true, loadedLabels: ["ai.maestro.ivy-daemon"] });
|
|
333
|
+
try {
|
|
334
|
+
run(notLoaded);
|
|
335
|
+
const last = readLast(notLoaded);
|
|
336
|
+
assert.equal(last.ok, true, "the daemon is healthy → the upgrade stands");
|
|
337
|
+
assert.equal(last.healthy, true);
|
|
338
|
+
assert.equal(last.reason, "session-reconcile-failed");
|
|
339
|
+
assert.match(scriptLog(notLoaded), /reconcile-failed: session job ai\.maestro\.ivy-session is not loaded/);
|
|
340
|
+
assert.ok(!/ROLLBACK/.test(scriptLog(notLoaded)), "session absence never rolls back");
|
|
341
|
+
assert.ok(existsSync(noticePath(notLoaded)), "the notice is still written — the daemon is on the new version");
|
|
342
|
+
const installed = JSON.parse(readFileSync(join(notLoaded.agent, "node_modules", "@cohortapp", "agent-sdk", "package.json"), "utf8"));
|
|
343
|
+
assert.equal(installed.version, "2.0.0");
|
|
344
|
+
assert.ok(!/-session/.test(calls(notLoaded).replace(/^launchctl (list|print) [^\n]*$/gm, "")), "the session job itself is never touched");
|
|
345
|
+
} finally { rmSync(notLoaded.root, { recursive: true, force: true }); }
|
|
346
|
+
const noSupervisor = fixture({ sessionPlist: true, loadedLabels: ["ai.maestro.ivy-daemon", "ai.maestro.ivy-session"], supervisorAlive: false });
|
|
347
|
+
try {
|
|
348
|
+
run(noSupervisor);
|
|
349
|
+
assert.equal(readLast(noSupervisor).reason, "session-reconcile-failed");
|
|
350
|
+
assert.match(scriptLog(noSupervisor), /no supervisor process is alive/);
|
|
351
|
+
} finally { rmSync(noSupervisor.root, { recursive: true, force: true }); }
|
|
352
|
+
const noPlist = fixture({ sessionPlist: false, loadedLabels: ["ai.maestro.ivy-daemon"] });
|
|
353
|
+
try {
|
|
354
|
+
run(noPlist);
|
|
355
|
+
assert.equal(readLast(noPlist).reason, "", "no generated -session plist → the session half is not asked");
|
|
356
|
+
assert.ok(!/launchctl (list|print)/.test(calls(noPlist)));
|
|
357
|
+
} finally { rmSync(noPlist.root, { recursive: true, force: true }); }
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test("rollback: last.json says unhealthy-rolled-back, and the global install is put back to the previous version (skipped-but-logged under MAESTRO_SKIP_GLOBAL_INSTALL=1)", () => {
|
|
361
|
+
const fx = fixture({ healthy: false });
|
|
362
|
+
try {
|
|
363
|
+
run(fx);
|
|
364
|
+
const last = readLast(fx);
|
|
365
|
+
assert.equal(last.from, "1.0.0");
|
|
366
|
+
assert.equal(last.to, "2.0.0");
|
|
367
|
+
assert.equal(last.ok, false);
|
|
368
|
+
assert.equal(last.healthy, false, "the daemon stub is unhealthy on both versions here");
|
|
369
|
+
assert.equal(last.reason, "rollback-unhealthy");
|
|
370
|
+
assert.match(scriptLog(fx), /global refresh skipped \(MAESTRO_SKIP_GLOBAL_INSTALL=1\): npm install -g @cohortapp\/agent-sdk@1\.0\.0 --no-audit --no-fund/);
|
|
371
|
+
assert.ok(!/npm install -g/.test(calls(fx)), "no real global install from a test");
|
|
372
|
+
assert.equal((calls(fx).match(/^launchctl kickstart -k gui\/\d+\/ai\.maestro\.ivy-daemon$/gm) || []).length, 2, "kickstart after the upgrade and after the rollback");
|
|
373
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
test("no daemon plist installed at all: the upgrade still runs, and whatever -daemon label the upgrade installed is kickstarted", () => {
|
|
377
|
+
const fx = fixture();
|
|
378
|
+
try {
|
|
379
|
+
rmSync(join(fx.home, "Library", "LaunchAgents", "ai.maestro.ivy-daemon.plist"));
|
|
380
|
+
// The (stubbed) maestro.mjs upgrade installs the daemon plist — as the real
|
|
381
|
+
// one's launchd reconcile does — so the label exists by the time restart runs.
|
|
382
|
+
writeFileSync(join(fx.agent, "node_modules", "@cohortapp", "agent-sdk", "bin", "maestro.mjs"),
|
|
383
|
+
`import { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(join(fx.home, "Library", "LaunchAgents", "ai.maestro.ivy-daemon.plist"))}, "<plist/>");\n`);
|
|
384
|
+
const r = run(fx);
|
|
385
|
+
assert.equal(r.status, 0, r.stderr);
|
|
386
|
+
assert.match(calls(fx), /^npm install @cohortapp\/agent-sdk@2\.0\.0/m, "the upgrade runs without a daemon plist");
|
|
387
|
+
assert.match(calls(fx), /^launchctl kickstart -k gui\/\d+\/ai\.maestro\.ivy-daemon$/m, "the label the upgrade installed is kickstarted");
|
|
388
|
+
assert.equal(readLast(fx).ok, true);
|
|
389
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
390
|
+
const none = fixture();
|
|
391
|
+
try {
|
|
392
|
+
rmSync(join(none.home, "Library", "LaunchAgents", "ai.maestro.ivy-daemon.plist"));
|
|
393
|
+
const r = run(none);
|
|
394
|
+
assert.equal(r.status, 0, r.stderr);
|
|
395
|
+
assert.match(scriptLog(none), /no -daemon plist under ~\/Library\/LaunchAgents — nothing to kickstart/);
|
|
396
|
+
assert.ok(!/kickstart/.test(calls(none)));
|
|
397
|
+
} finally { rmSync(none.root, { recursive: true, force: true }); }
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
test("source: the overlap lock, the retry, the session gate and the last.json writer are all present", () => {
|
|
401
|
+
assert.match(scriptSrc, /mkdir "\$LOCK_DIR"/, "mkdir-based overlap lock");
|
|
402
|
+
assert.match(scriptSrc, /state\/locks\/autoupdate/);
|
|
403
|
+
assert.match(scriptSrc, /--no-audit --no-fund --prefer-online/);
|
|
404
|
+
assert.match(scriptSrc, /RETRIES=3/);
|
|
405
|
+
assert.match(scriptSrc, /org-mesh connected\|daemon\\\] Running/, "the daemon health line must stay intact");
|
|
406
|
+
assert.match(scriptSrc, /scripts\/session\/supervisor/);
|
|
407
|
+
assert.match(scriptSrc, /state\/autoupdate\/last\.json|"\$dir\/last\.json"/);
|
|
408
|
+
assert.match(scriptSrc, /MAESTRO_SKIP_GLOBAL_INSTALL/);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
// ---------------------------------------------------------------------------
|
|
412
|
+
// Review fixes: failed-target hold, rollback reverts only what this run
|
|
413
|
+
// installed, per-agent scoping, the re-exec copy cleans itself up.
|
|
414
|
+
// ---------------------------------------------------------------------------
|
|
415
|
+
|
|
416
|
+
test("a version that failed the health gate here is held (no install, no restart) while last.json is younger than the hold; an old failure, another version, or an install-failed entry do not hold", () => {
|
|
417
|
+
const writeLast = (fx, o) => { mkdirSync(join(fx.agent, "state", "autoupdate"), { recursive: true }); writeFileSync(lastPath(fx), JSON.stringify({ from: "1.0.0", to: "2.0.0", at: new Date().toISOString(), ok: false, healthy: true, reason: "unhealthy-rolled-back", ...o })); };
|
|
418
|
+
const held = fixture();
|
|
419
|
+
try {
|
|
420
|
+
writeLast(held, {});
|
|
421
|
+
const r = run(held);
|
|
422
|
+
assert.equal(r.status, 0, r.stderr);
|
|
423
|
+
assert.ok(!/npm install/.test(calls(held)), `must not reinstall a version that already failed here:\n${calls(held)}`);
|
|
424
|
+
assert.ok(!/kickstart/.test(calls(held)));
|
|
425
|
+
assert.match(scriptLog(held), /SKIP: 2\.0\.0 already failed the health gate here \(unhealthy-rolled-back at .*\); holding 86400s \(still 1\.0\.0\)/);
|
|
426
|
+
assert.equal(readLast(held).reason, "unhealthy-rolled-back", "last.json is left as the fleet's evidence");
|
|
427
|
+
assert.ok(!existsSync(join(held.agent, "state", "locks", "autoupdate")));
|
|
428
|
+
} finally { rmSync(held.root, { recursive: true, force: true }); }
|
|
429
|
+
const heldTwice = fixture();
|
|
430
|
+
try { writeLast(heldTwice, { reason: "rollback-unhealthy", healthy: false }); run(heldTwice); assert.ok(!/npm install/.test(calls(heldTwice)), "a failed rollback holds too"); } finally { rmSync(heldTwice.root, { recursive: true, force: true }); }
|
|
431
|
+
const expired = fixture();
|
|
432
|
+
try { writeLast(expired, {}); run(expired, { MAESTRO_AUTOUPDATE_FAILED_HOLD_S: "0" }); assert.match(calls(expired), /npm install @cohortapp\/agent-sdk@2\.0\.0/, "past the hold the version is tried again"); } finally { rmSync(expired.root, { recursive: true, force: true }); }
|
|
433
|
+
const other = fixture({ latest: "2.0.1" });
|
|
434
|
+
try { writeLast(other, {}); run(other); assert.match(calls(other), /npm install @cohortapp\/agent-sdk@2\.0\.1/, "a NEWER version is not held by 2.0.0's failure"); } finally { rmSync(other.root, { recursive: true, force: true }); }
|
|
435
|
+
const network = fixture();
|
|
436
|
+
try { writeLast(network, { reason: "install-failed", healthy: false }); run(network); assert.match(calls(network), /npm install @cohortapp\/agent-sdk@2\.0\.0/, "install-failed is transient: retried"); } finally { rmSync(network.root, { recursive: true, force: true }); }
|
|
437
|
+
const corrupt = fixture();
|
|
438
|
+
try { mkdirSync(join(corrupt.agent, "state", "autoupdate"), { recursive: true }); writeFileSync(lastPath(corrupt), "{not json"); run(corrupt); assert.match(calls(corrupt), /npm install/, "an unreadable last.json holds nothing (fail-open)"); } finally { rmSync(corrupt.root, { recursive: true, force: true }); }
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
test("rollback reverts the -session job ONLY when this run installed it: bootout + plist removed; a pre-existing session plist is never touched", () => {
|
|
442
|
+
// The (stubbed) new upgrade installs the session plist, as the real reconcile does; the daemon is unhealthy → rollback.
|
|
443
|
+
const installedByRun = fixture({ healthy: false, installedSessionPlist: false, sessionPlist: true, loadedLabels: ["ai.maestro.ivy-daemon", "ai.maestro.ivy-session"] });
|
|
444
|
+
try {
|
|
445
|
+
const plist = join(installedByRun.home, "Library", "LaunchAgents", "ai.maestro.ivy-session.plist");
|
|
446
|
+
writeFileSync(join(installedByRun.agent, "node_modules", "@cohortapp", "agent-sdk", "bin", "maestro.mjs"),
|
|
447
|
+
`import { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(plist)}, "<plist/>");\n`);
|
|
448
|
+
const r = run(installedByRun);
|
|
449
|
+
assert.equal(r.status, 0, r.stderr);
|
|
450
|
+
assert.match(scriptLog(installedByRun), /ROLLBACK/);
|
|
451
|
+
assert.match(calls(installedByRun), /^launchctl bootout gui\/\d+\/ai\.maestro\.ivy-session$/m, `the job this run installed is booted out on rollback:\n${calls(installedByRun)}`);
|
|
452
|
+
assert.ok(!existsSync(plist), "and its plist is removed — the previous version has no front-door session");
|
|
453
|
+
assert.match(scriptLog(installedByRun), /rollback: reverted session job ai\.maestro\.ivy-session — this run installed it/);
|
|
454
|
+
assert.equal(readLast(installedByRun).reason, "rollback-unhealthy");
|
|
455
|
+
assert.ok(!existsSync(noticePath(installedByRun)));
|
|
456
|
+
} finally { rmSync(installedByRun.root, { recursive: true, force: true }); }
|
|
457
|
+
const preExisting = fixture({ healthy: false, installedSessionPlist: true, sessionPlist: true, loadedLabels: ["ai.maestro.ivy-daemon", "ai.maestro.ivy-session"] });
|
|
458
|
+
try {
|
|
459
|
+
run(preExisting);
|
|
460
|
+
assert.match(scriptLog(preExisting), /ROLLBACK/);
|
|
461
|
+
assert.ok(!/bootout/.test(calls(preExisting)), `a session job that was there before the run is the operator's — never booted out:\n${calls(preExisting)}`);
|
|
462
|
+
assert.ok(existsSync(join(preExisting.home, "Library", "LaunchAgents", "ai.maestro.ivy-session.plist")));
|
|
463
|
+
} finally { rmSync(preExisting.root, { recursive: true, force: true }); }
|
|
464
|
+
const healthy = fixture({ installedSessionPlist: false, sessionPlist: true, loadedLabels: ["ai.maestro.ivy-daemon", "ai.maestro.ivy-session"] });
|
|
465
|
+
try {
|
|
466
|
+
const plist = join(healthy.home, "Library", "LaunchAgents", "ai.maestro.ivy-session.plist");
|
|
467
|
+
writeFileSync(join(healthy.agent, "node_modules", "@cohortapp", "agent-sdk", "bin", "maestro.mjs"), `import { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(plist)}, "<plist/>");\n`);
|
|
468
|
+
run(healthy);
|
|
469
|
+
assert.ok(!/bootout/.test(calls(healthy)), "a healthy upgrade never boots anything out");
|
|
470
|
+
assert.ok(existsSync(plist));
|
|
471
|
+
} finally { rmSync(healthy.root, { recursive: true, force: true }); }
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
test("two seats on one Mac: every launchd / pgrep question is scoped to THIS agent's first name — the other seat's daemon is not kickstarted and its session does not satisfy the gate", () => {
|
|
475
|
+
const fx = fixture({ firstName: "Ivy", sessionPlist: true, supervisorAlive: true, loadedLabels: ["ai.maestro.zed-daemon", "ai.maestro.zed-session", "ai.maestro.ivy-daemon"] });
|
|
476
|
+
try {
|
|
477
|
+
// Seat B (zed) is installed on the same machine, and sorts BEFORE ivy.
|
|
478
|
+
for (const l of ["ai.maestro.zed-daemon", "ai.maestro.zed-session"]) writeFileSync(join(fx.home, "Library", "LaunchAgents", `${l}.plist`), "<plist/>");
|
|
479
|
+
// Seat A's own generated set also carries its daemon plist (as the real generator renders it).
|
|
480
|
+
writeFileSync(join(fx.agent, "scripts", "local-triggers", "plists", "ai.maestro.ivy-daemon.plist"), "<plist/>");
|
|
481
|
+
// Make the ivy-session plist sort AFTER zed's in LaunchAgents too — the scoping, not the sort order, must pick ivy.
|
|
482
|
+
const r = run(fx);
|
|
483
|
+
assert.equal(r.status, 0, r.stderr);
|
|
484
|
+
const c = calls(fx);
|
|
485
|
+
assert.match(c, /^launchctl kickstart -k gui\/\d+\/ai\.maestro\.ivy-daemon$/m, c);
|
|
486
|
+
assert.ok(!/zed/.test(c), `seat B must never be touched or consulted:\n${c}`);
|
|
487
|
+
assert.match(c, /^launchctl print gui\/\d+\/ai\.maestro\.ivy-session$/m);
|
|
488
|
+
assert.ok(!/pgrep -f .*supervisor/.test(c), "ivy's label is not loaded → the gate stops there; zed's supervisor is never consulted");
|
|
489
|
+
assert.equal(readLast(fx).reason, "session-reconcile-failed", "ivy's session is not loaded — zed's loaded session must not count");
|
|
490
|
+
assert.match(scriptLog(fx), /agent: ivy \(launchd\/pgrep questions are scoped to ai\.maestro\.ivy-\*\)/);
|
|
491
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
492
|
+
const bothLoaded = fixture({ firstName: "Ivy", sessionPlist: true, supervisorAlive: true, loadedLabels: ["ai.maestro.zed-daemon", "ai.maestro.zed-session", "ai.maestro.ivy-daemon", "ai.maestro.ivy-session"] });
|
|
493
|
+
try {
|
|
494
|
+
for (const l of ["ai.maestro.zed-daemon", "ai.maestro.zed-session"]) writeFileSync(join(bothLoaded.home, "Library", "LaunchAgents", `${l}.plist`), "<plist/>");
|
|
495
|
+
run(bothLoaded);
|
|
496
|
+
const c = calls(bothLoaded);
|
|
497
|
+
assert.match(c, new RegExp(`^pgrep -f ${bothLoaded.agent.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/scripts/session/supervisor$`, "m"), "the supervisor question names THIS agent dir, so seat B's supervisor cannot answer it");
|
|
498
|
+
assert.equal(readLast(bothLoaded).reason, "");
|
|
499
|
+
assert.ok(!/zed/.test(c));
|
|
500
|
+
} finally { rmSync(bothLoaded.root, { recursive: true, force: true }); }
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
test("the /tmp re-exec copy removes itself on exit (and a run with MAESTRO_AUTOUPDATE_REEXEC=1 never deletes the script it was given)", () => {
|
|
504
|
+
const fx = fixture({ latest: "1.0.0" });
|
|
505
|
+
try {
|
|
506
|
+
const tmp = join(fx.root, "tmp"); mkdirSync(tmp, { recursive: true });
|
|
507
|
+
const r = spawnSync("/bin/bash", [join(fx.agent, "scripts", "local-triggers", "autoupdate.sh")], {
|
|
508
|
+
encoding: "utf8",
|
|
509
|
+
// This case spawns directly rather than through `run()`, so it needs the
|
|
510
|
+
// same TZ passthrough for the same reason — see the note in `run()`.
|
|
511
|
+
env: { HOME: fx.home, AGENT_ROOT: fx.agent, TMPDIR: tmp, PATH: `${fx.bin}:${dirname(process.execPath)}:/usr/bin:/bin`, ...(process.env.TZ ? { TZ: process.env.TZ } : {}), MAESTRO_AUTOUPDATE_PATH_PREFIX: fx.bin, MAESTRO_AUTOUPDATE_JITTER_MAX: "0", MAESTRO_AUTOUPDATE_HEALTH_WAIT: "0", MAESTRO_AUTOUPDATE_RETRY_SLEEP: "0", MAESTRO_SKIP_GLOBAL_INSTALL: "1" },
|
|
512
|
+
});
|
|
513
|
+
assert.equal(r.status, 0, r.stderr);
|
|
514
|
+
assert.match(scriptLog(fx), /up to date \(1\.0\.0\)/, "the re-exec'd copy ran the script");
|
|
515
|
+
assert.deepEqual(readdirSync(tmp), [], "no maestro-autoupdate.XXXXXX copy left behind");
|
|
516
|
+
assert.ok(existsSync(join(fx.agent, "scripts", "local-triggers", "autoupdate.sh")), "the agent dir's script is untouched");
|
|
517
|
+
} finally { rmSync(fx.root, { recursive: true, force: true }); }
|
|
518
|
+
});
|
|
@@ -95,6 +95,11 @@ generate_plist() {
|
|
|
95
95
|
local SCHEDULE="$3" # Optional: StartCalendarInterval dict content
|
|
96
96
|
local INTERVAL="$4" # Optional: StartInterval in seconds
|
|
97
97
|
local KEEP_ALIVE="$5" # Optional: true for daemon-style
|
|
98
|
+
local LOG_SUBDIR="${6:-daemon}" # Optional: logs/<subdir>/ for launchd's
|
|
99
|
+
# StandardOut/ErrorPath. Defaults to logs/daemon
|
|
100
|
+
# (every job shared it historically); the session
|
|
101
|
+
# job uses logs/session so its launchd output never
|
|
102
|
+
# interleaves with the daemon's.
|
|
98
103
|
|
|
99
104
|
local FILE="$PLIST_DIR/$LABEL.plist"
|
|
100
105
|
|
|
@@ -195,7 +200,10 @@ PLIST_ENV_END
|
|
|
195
200
|
# Resolve symlinked log path to a real path so launchd can write to it.
|
|
196
201
|
# If logs/ is a symlink (e.g. when redirected to an external SSD), launchd's
|
|
197
202
|
# StandardErrorPath/StandardOutPath sometimes fail with EX_CONFIG (78).
|
|
198
|
-
|
|
203
|
+
# launchd creates the log FILE but not its directory, so make sure the
|
|
204
|
+
# directory exists (idempotent; logs/daemon already does via init-agent).
|
|
205
|
+
mkdir -p "$AGENT_DIR/logs/$LOG_SUBDIR" 2>/dev/null || true
|
|
206
|
+
LOG_REAL=$(cd -P "$AGENT_DIR/logs/$LOG_SUBDIR" 2>/dev/null && pwd -P || echo "$AGENT_DIR/logs/$LOG_SUBDIR")
|
|
199
207
|
|
|
200
208
|
cat >> "$FILE" << PLIST_FOOTER
|
|
201
209
|
|
|
@@ -269,6 +277,21 @@ generate_plist "ai.maestro.${AGENT_FIRST}-daemon" \
|
|
|
269
277
|
"${AGENT_DIR}/scripts/daemon/launchd-wrapper.sh" \
|
|
270
278
|
"" "" "true"
|
|
271
279
|
|
|
280
|
+
# 1b. Front-door Claude Code session (KeepAlive) — the agent's BRAIN, next to
|
|
281
|
+
# the daemon's PLUMBING. scripts/session/supervisor.sh sources .env, takes
|
|
282
|
+
# the `session` singleton lock and runs the main interactive session inside
|
|
283
|
+
# a foreground multiplexer (screen/tmux), so the supervisor lives exactly as
|
|
284
|
+
# long as the session: when the session ends it exits 75 (EX_TEMPFAIL) and
|
|
285
|
+
# launchd's KeepAlive {SuccessfulExit:false, Crashed:true} + ThrottleInterval
|
|
286
|
+
# 30 bring it back. A second supervisor (lock held) exits 0 and is NOT
|
|
287
|
+
# restarted, so launchd never thrashes. No cron, no periodic kill: relaunch
|
|
288
|
+
# happens only on death. Replaces the Terminal.app boot-claude-session job.
|
|
289
|
+
# Logs go to logs/session/ (not logs/daemon/).
|
|
290
|
+
# See docs/superpowers/specs/2026-09-08-front-door-session-design.md §3.1.
|
|
291
|
+
generate_plist "ai.maestro.${AGENT_FIRST}-session" \
|
|
292
|
+
"${AGENT_DIR}/scripts/session/supervisor.sh" \
|
|
293
|
+
"" "" "true" "session"
|
|
294
|
+
|
|
272
295
|
# 2. Slack events relay polling job (every 5 sec).
|
|
273
296
|
# The local Mac mini polls the Railway-hosted webhook relay for buffered
|
|
274
297
|
# Slack events. This replaces the older local slack-events-server.mjs which
|