@wopr-network/platform-core 1.17.0 → 1.19.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/dist/api/routes/admin-audit-helper.d.ts +1 -1
- package/dist/billing/crypto/evm/__tests__/config.test.js +10 -0
- package/dist/billing/crypto/evm/config.js +12 -0
- package/dist/billing/crypto/evm/types.d.ts +1 -1
- package/dist/fleet/__tests__/rollout-strategy.test.d.ts +1 -0
- package/dist/fleet/__tests__/rollout-strategy.test.js +157 -0
- package/dist/fleet/__tests__/volume-snapshot-manager.test.d.ts +1 -0
- package/dist/fleet/__tests__/volume-snapshot-manager.test.js +171 -0
- package/dist/fleet/index.d.ts +2 -0
- package/dist/fleet/index.js +2 -0
- package/dist/fleet/rollout-strategy.d.ts +52 -0
- package/dist/fleet/rollout-strategy.js +91 -0
- package/dist/fleet/volume-snapshot-manager.d.ts +35 -0
- package/dist/fleet/volume-snapshot-manager.js +185 -0
- package/docs/superpowers/specs/2026-03-14-fleet-auto-update-design.md +300 -0
- package/docs/superpowers/specs/2026-03-14-paperclip-org-integration-design.md +359 -0
- package/docs/superpowers/specs/2026-03-14-role-permissions-design.md +346 -0
- package/package.json +1 -1
- package/src/api/routes/admin-audit-helper.ts +1 -1
- package/src/billing/crypto/evm/__tests__/config.test.ts +12 -0
- package/src/billing/crypto/evm/config.ts +13 -1
- package/src/billing/crypto/evm/types.ts +1 -1
- package/src/fleet/__tests__/rollout-strategy.test.ts +192 -0
- package/src/fleet/__tests__/volume-snapshot-manager.test.ts +218 -0
- package/src/fleet/index.ts +2 -0
- package/src/fleet/rollout-strategy.ts +128 -0
- package/src/fleet/volume-snapshot-manager.ts +213 -0
- package/src/marketplace/volume-installer.test.ts +8 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AuditEntry } from "../../admin/audit-log.js";
|
|
2
2
|
/** Minimal interface for admin audit logging in route factories. */
|
|
3
3
|
export interface AdminAuditLogger {
|
|
4
|
-
log(entry: AuditEntry):
|
|
4
|
+
log(entry: AuditEntry): undefined | Promise<unknown>;
|
|
5
5
|
}
|
|
6
6
|
/** Safely log an admin audit entry — never throws. */
|
|
7
7
|
export declare function safeAuditLog(logger: (() => AdminAuditLogger) | undefined, entry: AuditEntry): void;
|
|
@@ -19,6 +19,16 @@ describe("getTokenConfig", () => {
|
|
|
19
19
|
expect(cfg.contractAddress).toMatch(/^0x/);
|
|
20
20
|
expect(cfg.contractAddress).toBe("0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913");
|
|
21
21
|
});
|
|
22
|
+
it("returns USDT on Base", () => {
|
|
23
|
+
const cfg = getTokenConfig("USDT", "base");
|
|
24
|
+
expect(cfg.decimals).toBe(6);
|
|
25
|
+
expect(cfg.contractAddress).toBe("0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2");
|
|
26
|
+
});
|
|
27
|
+
it("returns DAI on Base", () => {
|
|
28
|
+
const cfg = getTokenConfig("DAI", "base");
|
|
29
|
+
expect(cfg.decimals).toBe(18);
|
|
30
|
+
expect(cfg.contractAddress).toBe("0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb");
|
|
31
|
+
});
|
|
22
32
|
it("throws on unsupported token/chain combo", () => {
|
|
23
33
|
// biome-ignore lint/suspicious/noExplicitAny: testing invalid input
|
|
24
34
|
expect(() => getTokenConfig("USDC", "ethereum")).toThrow("Unsupported token");
|
|
@@ -14,6 +14,18 @@ const TOKENS = {
|
|
|
14
14
|
contractAddress: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
15
15
|
decimals: 6,
|
|
16
16
|
},
|
|
17
|
+
"USDT:base": {
|
|
18
|
+
token: "USDT",
|
|
19
|
+
chain: "base",
|
|
20
|
+
contractAddress: "0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2",
|
|
21
|
+
decimals: 6,
|
|
22
|
+
},
|
|
23
|
+
"DAI:base": {
|
|
24
|
+
token: "DAI",
|
|
25
|
+
chain: "base",
|
|
26
|
+
contractAddress: "0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb",
|
|
27
|
+
decimals: 18,
|
|
28
|
+
},
|
|
17
29
|
};
|
|
18
30
|
export function getChainConfig(chain) {
|
|
19
31
|
const cfg = CHAINS[chain];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Supported EVM chains. */
|
|
2
2
|
export type EvmChain = "base";
|
|
3
3
|
/** Supported stablecoin tokens. */
|
|
4
|
-
export type StablecoinToken = "USDC";
|
|
4
|
+
export type StablecoinToken = "USDC" | "USDT" | "DAI";
|
|
5
5
|
/** Chain configuration. */
|
|
6
6
|
export interface ChainConfig {
|
|
7
7
|
readonly chain: EvmChain;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { createRolloutStrategy, ImmediateStrategy, RollingWaveStrategy, SingleBotStrategy, } from "../rollout-strategy.js";
|
|
4
|
+
function makeBots(count) {
|
|
5
|
+
return Array.from({ length: count }, (_, i) => ({
|
|
6
|
+
id: randomUUID(),
|
|
7
|
+
tenantId: "tenant-1",
|
|
8
|
+
name: `bot-${i}`,
|
|
9
|
+
description: "",
|
|
10
|
+
image: "ghcr.io/wopr-network/test:latest",
|
|
11
|
+
env: {},
|
|
12
|
+
restartPolicy: "unless-stopped",
|
|
13
|
+
releaseChannel: "stable",
|
|
14
|
+
updatePolicy: "manual",
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
describe("RollingWaveStrategy", () => {
|
|
18
|
+
it("returns batchPercent of remaining bots", () => {
|
|
19
|
+
const s = new RollingWaveStrategy({ batchPercent: 25 });
|
|
20
|
+
const bots = makeBots(10);
|
|
21
|
+
const batch = s.nextBatch(bots);
|
|
22
|
+
// 25% of 10 = 2.5, ceil = 3
|
|
23
|
+
expect(batch).toHaveLength(3);
|
|
24
|
+
expect(batch).toEqual(bots.slice(0, 3));
|
|
25
|
+
});
|
|
26
|
+
it("returns minimum 1 bot even with low percentage", () => {
|
|
27
|
+
const s = new RollingWaveStrategy({ batchPercent: 1 });
|
|
28
|
+
const bots = makeBots(2);
|
|
29
|
+
expect(s.nextBatch(bots)).toHaveLength(1);
|
|
30
|
+
});
|
|
31
|
+
it("returns empty array for empty remaining", () => {
|
|
32
|
+
const s = new RollingWaveStrategy();
|
|
33
|
+
expect(s.nextBatch([])).toHaveLength(0);
|
|
34
|
+
});
|
|
35
|
+
it("handles 1 bot remaining", () => {
|
|
36
|
+
const s = new RollingWaveStrategy({ batchPercent: 50 });
|
|
37
|
+
const bots = makeBots(1);
|
|
38
|
+
expect(s.nextBatch(bots)).toHaveLength(1);
|
|
39
|
+
});
|
|
40
|
+
it("handles batchPercent > 100", () => {
|
|
41
|
+
const s = new RollingWaveStrategy({ batchPercent: 200 });
|
|
42
|
+
const bots = makeBots(5);
|
|
43
|
+
// 200% of 5 = 10, ceil = 10, but slice(0,10) on 5 items = 5
|
|
44
|
+
expect(s.nextBatch(bots)).toHaveLength(5);
|
|
45
|
+
});
|
|
46
|
+
it("returns correct pause duration", () => {
|
|
47
|
+
expect(new RollingWaveStrategy().pauseDuration()).toBe(60_000);
|
|
48
|
+
expect(new RollingWaveStrategy({ pauseMs: 30_000 }).pauseDuration()).toBe(30_000);
|
|
49
|
+
});
|
|
50
|
+
it("retries on failure until maxRetries", () => {
|
|
51
|
+
const s = new RollingWaveStrategy({ maxFailures: 2 });
|
|
52
|
+
const err = new Error("fail");
|
|
53
|
+
expect(s.onBotFailure("b1", err, 0)).toBe("retry");
|
|
54
|
+
expect(s.onBotFailure("b1", err, 1)).toBe("retry");
|
|
55
|
+
});
|
|
56
|
+
it("skips after maxRetries when under maxFailures", () => {
|
|
57
|
+
const s = new RollingWaveStrategy({ maxFailures: 3 });
|
|
58
|
+
const err = new Error("fail");
|
|
59
|
+
// attempt >= maxRetries (2), first total failure
|
|
60
|
+
expect(s.onBotFailure("b1", err, 2)).toBe("skip");
|
|
61
|
+
});
|
|
62
|
+
it("aborts when total failures reach maxFailures", () => {
|
|
63
|
+
const s = new RollingWaveStrategy({ maxFailures: 2 });
|
|
64
|
+
const err = new Error("fail");
|
|
65
|
+
// exhaust retries for bot1 → skip (totalFailures=1)
|
|
66
|
+
expect(s.onBotFailure("b1", err, 2)).toBe("skip");
|
|
67
|
+
// exhaust retries for bot2 → abort (totalFailures=2 >= maxFailures=2)
|
|
68
|
+
expect(s.onBotFailure("b2", err, 2)).toBe("abort");
|
|
69
|
+
});
|
|
70
|
+
it("has maxRetries of 2", () => {
|
|
71
|
+
expect(new RollingWaveStrategy().maxRetries()).toBe(2);
|
|
72
|
+
});
|
|
73
|
+
it("has healthCheckTimeout of 120_000", () => {
|
|
74
|
+
expect(new RollingWaveStrategy().healthCheckTimeout()).toBe(120_000);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe("SingleBotStrategy", () => {
|
|
78
|
+
it("returns exactly 1 bot", () => {
|
|
79
|
+
const s = new SingleBotStrategy();
|
|
80
|
+
const bots = makeBots(10);
|
|
81
|
+
expect(s.nextBatch(bots)).toHaveLength(1);
|
|
82
|
+
expect(s.nextBatch(bots)[0]).toBe(bots[0]);
|
|
83
|
+
});
|
|
84
|
+
it("returns empty for empty remaining", () => {
|
|
85
|
+
expect(new SingleBotStrategy().nextBatch([])).toHaveLength(0);
|
|
86
|
+
});
|
|
87
|
+
it("has pauseDuration of 0", () => {
|
|
88
|
+
expect(new SingleBotStrategy().pauseDuration()).toBe(0);
|
|
89
|
+
});
|
|
90
|
+
it("always retries on failure", () => {
|
|
91
|
+
const s = new SingleBotStrategy();
|
|
92
|
+
const err = new Error("fail");
|
|
93
|
+
expect(s.onBotFailure("b1", err, 0)).toBe("retry");
|
|
94
|
+
expect(s.onBotFailure("b1", err, 1)).toBe("retry");
|
|
95
|
+
expect(s.onBotFailure("b1", err, 2)).toBe("retry");
|
|
96
|
+
// After maxRetries (3), aborts instead of retrying forever
|
|
97
|
+
expect(s.onBotFailure("b1", err, 99)).toBe("abort");
|
|
98
|
+
});
|
|
99
|
+
it("has maxRetries of 3", () => {
|
|
100
|
+
expect(new SingleBotStrategy().maxRetries()).toBe(3);
|
|
101
|
+
});
|
|
102
|
+
it("has healthCheckTimeout of 120_000", () => {
|
|
103
|
+
expect(new SingleBotStrategy().healthCheckTimeout()).toBe(120_000);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe("ImmediateStrategy", () => {
|
|
107
|
+
it("returns all remaining bots", () => {
|
|
108
|
+
const s = new ImmediateStrategy();
|
|
109
|
+
const bots = makeBots(10);
|
|
110
|
+
expect(s.nextBatch(bots)).toHaveLength(10);
|
|
111
|
+
expect(s.nextBatch(bots)).toEqual(bots);
|
|
112
|
+
});
|
|
113
|
+
it("returns empty for empty remaining", () => {
|
|
114
|
+
expect(new ImmediateStrategy().nextBatch([])).toHaveLength(0);
|
|
115
|
+
});
|
|
116
|
+
it("does not mutate the input array", () => {
|
|
117
|
+
const s = new ImmediateStrategy();
|
|
118
|
+
const bots = makeBots(3);
|
|
119
|
+
const result = s.nextBatch(bots);
|
|
120
|
+
expect(result).not.toBe(bots);
|
|
121
|
+
expect(result).toEqual(bots);
|
|
122
|
+
});
|
|
123
|
+
it("has pauseDuration of 0", () => {
|
|
124
|
+
expect(new ImmediateStrategy().pauseDuration()).toBe(0);
|
|
125
|
+
});
|
|
126
|
+
it("always skips on failure", () => {
|
|
127
|
+
const s = new ImmediateStrategy();
|
|
128
|
+
const err = new Error("fail");
|
|
129
|
+
expect(s.onBotFailure("b1", err, 0)).toBe("skip");
|
|
130
|
+
expect(s.onBotFailure("b1", err, 5)).toBe("skip");
|
|
131
|
+
});
|
|
132
|
+
it("has maxRetries of 1", () => {
|
|
133
|
+
expect(new ImmediateStrategy().maxRetries()).toBe(1);
|
|
134
|
+
});
|
|
135
|
+
it("has healthCheckTimeout of 60_000", () => {
|
|
136
|
+
expect(new ImmediateStrategy().healthCheckTimeout()).toBe(60_000);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe("createRolloutStrategy", () => {
|
|
140
|
+
it("creates RollingWaveStrategy", () => {
|
|
141
|
+
const s = createRolloutStrategy("rolling-wave");
|
|
142
|
+
expect(s).toBeInstanceOf(RollingWaveStrategy);
|
|
143
|
+
});
|
|
144
|
+
it("creates RollingWaveStrategy with options", () => {
|
|
145
|
+
const s = createRolloutStrategy("rolling-wave", { batchPercent: 50, pauseMs: 10_000 });
|
|
146
|
+
expect(s).toBeInstanceOf(RollingWaveStrategy);
|
|
147
|
+
expect(s.pauseDuration()).toBe(10_000);
|
|
148
|
+
});
|
|
149
|
+
it("creates SingleBotStrategy", () => {
|
|
150
|
+
const s = createRolloutStrategy("single-bot");
|
|
151
|
+
expect(s).toBeInstanceOf(SingleBotStrategy);
|
|
152
|
+
});
|
|
153
|
+
it("creates ImmediateStrategy", () => {
|
|
154
|
+
const s = createRolloutStrategy("immediate");
|
|
155
|
+
expect(s).toBeInstanceOf(ImmediateStrategy);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { VolumeSnapshotManager } from "../volume-snapshot-manager.js";
|
|
3
|
+
// Mock fs/promises
|
|
4
|
+
vi.mock("node:fs/promises", () => ({
|
|
5
|
+
mkdir: vi.fn().mockResolvedValue(undefined),
|
|
6
|
+
stat: vi.fn().mockResolvedValue({ size: 1024, mtime: new Date("2026-03-14T10:00:00Z") }),
|
|
7
|
+
readdir: vi.fn().mockResolvedValue([]),
|
|
8
|
+
rm: vi.fn().mockResolvedValue(undefined),
|
|
9
|
+
}));
|
|
10
|
+
// Mock logger
|
|
11
|
+
vi.mock("../../config/logger.js", () => ({
|
|
12
|
+
logger: {
|
|
13
|
+
info: vi.fn(),
|
|
14
|
+
warn: vi.fn(),
|
|
15
|
+
error: vi.fn(),
|
|
16
|
+
debug: vi.fn(),
|
|
17
|
+
},
|
|
18
|
+
}));
|
|
19
|
+
import { mkdir, readdir, rm, stat } from "node:fs/promises";
|
|
20
|
+
function mockContainer() {
|
|
21
|
+
return {
|
|
22
|
+
start: vi.fn().mockResolvedValue(undefined),
|
|
23
|
+
wait: vi.fn().mockResolvedValue({ StatusCode: 0 }),
|
|
24
|
+
remove: vi.fn().mockResolvedValue(undefined),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function mockDocker() {
|
|
28
|
+
return {
|
|
29
|
+
createContainer: vi.fn().mockResolvedValue(mockContainer()),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
describe("VolumeSnapshotManager", () => {
|
|
33
|
+
let docker;
|
|
34
|
+
let manager;
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
vi.clearAllMocks();
|
|
37
|
+
docker = mockDocker();
|
|
38
|
+
manager = new VolumeSnapshotManager(docker, "/data/fleet/snapshots");
|
|
39
|
+
});
|
|
40
|
+
describe("snapshot()", () => {
|
|
41
|
+
it("creates container with correct binds and runs tar", async () => {
|
|
42
|
+
await manager.snapshot("my-volume");
|
|
43
|
+
expect(docker.createContainer).toHaveBeenCalledWith(expect.objectContaining({
|
|
44
|
+
Image: "alpine:latest",
|
|
45
|
+
Cmd: expect.arrayContaining(["tar", "cf"]),
|
|
46
|
+
HostConfig: expect.objectContaining({
|
|
47
|
+
Binds: expect.arrayContaining(["my-volume:/source:ro", "/data/fleet/snapshots:/backup"]),
|
|
48
|
+
AutoRemove: true,
|
|
49
|
+
}),
|
|
50
|
+
}));
|
|
51
|
+
const container = await docker.createContainer.mock.results[0].value;
|
|
52
|
+
expect(container.start).toHaveBeenCalled();
|
|
53
|
+
expect(container.wait).toHaveBeenCalled();
|
|
54
|
+
});
|
|
55
|
+
it("returns a VolumeSnapshot with correct fields", async () => {
|
|
56
|
+
const result = await manager.snapshot("my-volume");
|
|
57
|
+
expect(result.volumeName).toBe("my-volume");
|
|
58
|
+
expect(result.id).toMatch(/^my-volume-\d{4}-\d{2}-\d{2}T/);
|
|
59
|
+
expect(result.archivePath).toMatch(/^\/data\/fleet\/snapshots\/my-volume-.*\.tar$/);
|
|
60
|
+
expect(result.sizeBytes).toBe(1024);
|
|
61
|
+
expect(result.createdAt).toBeInstanceOf(Date);
|
|
62
|
+
});
|
|
63
|
+
it("ensures backup directory exists", async () => {
|
|
64
|
+
await manager.snapshot("my-volume");
|
|
65
|
+
expect(mkdir).toHaveBeenCalledWith("/data/fleet/snapshots", { recursive: true });
|
|
66
|
+
});
|
|
67
|
+
it("cleans up container on start failure", async () => {
|
|
68
|
+
const container = mockContainer();
|
|
69
|
+
container.start.mockRejectedValue(new Error("start failed"));
|
|
70
|
+
docker.createContainer.mockResolvedValue(container);
|
|
71
|
+
await expect(manager.snapshot("my-volume")).rejects.toThrow("start failed");
|
|
72
|
+
expect(container.remove).toHaveBeenCalledWith({ force: true });
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("restore()", () => {
|
|
76
|
+
it("creates container with correct binds and runs tar xf", async () => {
|
|
77
|
+
const snapshotId = "my-volume-2026-03-14T10-00-00-000Z";
|
|
78
|
+
await manager.restore(snapshotId);
|
|
79
|
+
expect(docker.createContainer).toHaveBeenCalledWith(expect.objectContaining({
|
|
80
|
+
Image: "alpine:latest",
|
|
81
|
+
Cmd: ["sh", "-c", `cd /target && rm -rf ./* ./.??* && tar xf /backup/${snapshotId}.tar -C /target`],
|
|
82
|
+
HostConfig: expect.objectContaining({
|
|
83
|
+
Binds: expect.arrayContaining(["my-volume:/target", "/data/fleet/snapshots:/backup:ro"]),
|
|
84
|
+
AutoRemove: true,
|
|
85
|
+
}),
|
|
86
|
+
}));
|
|
87
|
+
});
|
|
88
|
+
it("starts and waits for container", async () => {
|
|
89
|
+
const container = mockContainer();
|
|
90
|
+
docker.createContainer.mockResolvedValue(container);
|
|
91
|
+
await manager.restore("my-volume-2026-03-14T10-00-00-000Z");
|
|
92
|
+
expect(container.start).toHaveBeenCalled();
|
|
93
|
+
expect(container.wait).toHaveBeenCalled();
|
|
94
|
+
});
|
|
95
|
+
it("throws if archive does not exist", async () => {
|
|
96
|
+
stat.mockRejectedValueOnce(Object.assign(new Error("ENOENT"), { code: "ENOENT" }));
|
|
97
|
+
await expect(manager.restore("nonexistent-2026-03-14T10-00-00-000Z")).rejects.toThrow("ENOENT");
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
describe("list()", () => {
|
|
101
|
+
it("returns snapshots sorted by date, newest first", async () => {
|
|
102
|
+
readdir.mockResolvedValue([
|
|
103
|
+
"my-volume-2026-03-12T08-00-00-000Z.tar",
|
|
104
|
+
"my-volume-2026-03-14T10-00-00-000Z.tar",
|
|
105
|
+
"my-volume-2026-03-13T09-00-00-000Z.tar",
|
|
106
|
+
]);
|
|
107
|
+
const oldDate = new Date("2026-03-12T08:00:00Z");
|
|
108
|
+
const midDate = new Date("2026-03-13T09:00:00Z");
|
|
109
|
+
const newDate = new Date("2026-03-14T10:00:00Z");
|
|
110
|
+
stat
|
|
111
|
+
.mockResolvedValueOnce({ size: 100, mtime: oldDate })
|
|
112
|
+
.mockResolvedValueOnce({ size: 300, mtime: newDate })
|
|
113
|
+
.mockResolvedValueOnce({ size: 200, mtime: midDate });
|
|
114
|
+
const result = await manager.list("my-volume");
|
|
115
|
+
expect(result).toHaveLength(3);
|
|
116
|
+
expect(result[0].createdAt).toEqual(newDate);
|
|
117
|
+
expect(result[1].createdAt).toEqual(midDate);
|
|
118
|
+
expect(result[2].createdAt).toEqual(oldDate);
|
|
119
|
+
});
|
|
120
|
+
it("filters to only matching volume name", async () => {
|
|
121
|
+
readdir.mockResolvedValue([
|
|
122
|
+
"my-volume-2026-03-14T10-00-00-000Z.tar",
|
|
123
|
+
"other-volume-2026-03-14T10-00-00-000Z.tar",
|
|
124
|
+
]);
|
|
125
|
+
const result = await manager.list("my-volume");
|
|
126
|
+
expect(result).toHaveLength(1);
|
|
127
|
+
expect(result[0].volumeName).toBe("my-volume");
|
|
128
|
+
});
|
|
129
|
+
it("returns empty array when backup dir does not exist", async () => {
|
|
130
|
+
readdir.mockRejectedValue(Object.assign(new Error("ENOENT"), { code: "ENOENT" }));
|
|
131
|
+
const result = await manager.list("my-volume");
|
|
132
|
+
expect(result).toEqual([]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
describe("delete()", () => {
|
|
136
|
+
it("removes the archive file", async () => {
|
|
137
|
+
await manager.delete("my-volume-2026-03-14T10-00-00-000Z");
|
|
138
|
+
expect(rm).toHaveBeenCalledWith("/data/fleet/snapshots/my-volume-2026-03-14T10-00-00-000Z.tar", { force: true });
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
describe("cleanup()", () => {
|
|
142
|
+
it("removes old snapshots and keeps recent ones", async () => {
|
|
143
|
+
const now = Date.now();
|
|
144
|
+
const oldTime = new Date(now - 2 * 60 * 60 * 1000); // 2 hours ago
|
|
145
|
+
const recentTime = new Date(now - 10 * 60 * 1000); // 10 minutes ago
|
|
146
|
+
readdir.mockResolvedValue([
|
|
147
|
+
"vol-2026-03-14T08-00-00-000Z.tar",
|
|
148
|
+
"vol-2026-03-14T09-50-00-000Z.tar",
|
|
149
|
+
]);
|
|
150
|
+
stat
|
|
151
|
+
.mockResolvedValueOnce({ size: 100, mtime: oldTime })
|
|
152
|
+
.mockResolvedValueOnce({ size: 200, mtime: recentTime });
|
|
153
|
+
const maxAge = 60 * 60 * 1000; // 1 hour
|
|
154
|
+
const deleted = await manager.cleanup(maxAge);
|
|
155
|
+
expect(deleted).toBe(1);
|
|
156
|
+
expect(rm).toHaveBeenCalledTimes(1);
|
|
157
|
+
expect(rm).toHaveBeenCalledWith("/data/fleet/snapshots/vol-2026-03-14T08-00-00-000Z.tar", { force: true });
|
|
158
|
+
});
|
|
159
|
+
it("returns 0 when backup dir does not exist", async () => {
|
|
160
|
+
readdir.mockRejectedValue(Object.assign(new Error("ENOENT"), { code: "ENOENT" }));
|
|
161
|
+
const result = await manager.cleanup(60_000);
|
|
162
|
+
expect(result).toBe(0);
|
|
163
|
+
});
|
|
164
|
+
it("skips non-tar files", async () => {
|
|
165
|
+
readdir.mockResolvedValue(["readme.txt", ".gitkeep"]);
|
|
166
|
+
const result = await manager.cleanup(60_000);
|
|
167
|
+
expect(result).toBe(0);
|
|
168
|
+
expect(stat).not.toHaveBeenCalled();
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
package/dist/fleet/index.d.ts
CHANGED
package/dist/fleet/index.js
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { BotProfile } from "./types.js";
|
|
2
|
+
export interface IRolloutStrategy {
|
|
3
|
+
/** Select next batch from remaining bots */
|
|
4
|
+
nextBatch(remaining: BotProfile[]): BotProfile[];
|
|
5
|
+
/** Milliseconds to wait between waves */
|
|
6
|
+
pauseDuration(): number;
|
|
7
|
+
/** What to do when a single bot update fails */
|
|
8
|
+
onBotFailure(botId: string, error: Error, attempt: number): "abort" | "skip" | "retry";
|
|
9
|
+
/** Max retries per bot before skip/abort */
|
|
10
|
+
maxRetries(): number;
|
|
11
|
+
/** Health check timeout per bot (ms) */
|
|
12
|
+
healthCheckTimeout(): number;
|
|
13
|
+
}
|
|
14
|
+
export interface RollingWaveOptions {
|
|
15
|
+
batchPercent?: number;
|
|
16
|
+
pauseMs?: number;
|
|
17
|
+
maxFailures?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Rolling wave strategy — processes bots in configurable percentage batches.
|
|
21
|
+
* Create a new instance per rollout; totalFailures accumulates across waves
|
|
22
|
+
* within a single rollout. Call reset() if reusing across rollouts.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RollingWaveStrategy implements IRolloutStrategy {
|
|
25
|
+
private readonly batchPercent;
|
|
26
|
+
private readonly pauseMs;
|
|
27
|
+
private readonly maxFailures;
|
|
28
|
+
private totalFailures;
|
|
29
|
+
constructor(opts?: RollingWaveOptions);
|
|
30
|
+
nextBatch(remaining: BotProfile[]): BotProfile[];
|
|
31
|
+
pauseDuration(): number;
|
|
32
|
+
onBotFailure(_botId: string, _error: Error, attempt: number): "abort" | "skip" | "retry";
|
|
33
|
+
maxRetries(): number;
|
|
34
|
+
healthCheckTimeout(): number;
|
|
35
|
+
/** Reset failure counters for reuse across rollouts. */
|
|
36
|
+
reset(): void;
|
|
37
|
+
}
|
|
38
|
+
export declare class SingleBotStrategy implements IRolloutStrategy {
|
|
39
|
+
nextBatch(remaining: BotProfile[]): BotProfile[];
|
|
40
|
+
pauseDuration(): number;
|
|
41
|
+
onBotFailure(_botId: string, _error: Error, attempt: number): "abort" | "skip" | "retry";
|
|
42
|
+
maxRetries(): number;
|
|
43
|
+
healthCheckTimeout(): number;
|
|
44
|
+
}
|
|
45
|
+
export declare class ImmediateStrategy implements IRolloutStrategy {
|
|
46
|
+
nextBatch(remaining: BotProfile[]): BotProfile[];
|
|
47
|
+
pauseDuration(): number;
|
|
48
|
+
onBotFailure(_botId: string, _error: Error, _attempt: number): "abort" | "skip" | "retry";
|
|
49
|
+
maxRetries(): number;
|
|
50
|
+
healthCheckTimeout(): number;
|
|
51
|
+
}
|
|
52
|
+
export declare function createRolloutStrategy(type: "rolling-wave" | "single-bot" | "immediate", options?: RollingWaveOptions): IRolloutStrategy;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rolling wave strategy — processes bots in configurable percentage batches.
|
|
3
|
+
* Create a new instance per rollout; totalFailures accumulates across waves
|
|
4
|
+
* within a single rollout. Call reset() if reusing across rollouts.
|
|
5
|
+
*/
|
|
6
|
+
export class RollingWaveStrategy {
|
|
7
|
+
batchPercent;
|
|
8
|
+
pauseMs;
|
|
9
|
+
maxFailures;
|
|
10
|
+
totalFailures = 0;
|
|
11
|
+
constructor(opts = {}) {
|
|
12
|
+
this.batchPercent = opts.batchPercent ?? 25;
|
|
13
|
+
this.pauseMs = opts.pauseMs ?? 60_000;
|
|
14
|
+
this.maxFailures = opts.maxFailures ?? 3;
|
|
15
|
+
}
|
|
16
|
+
nextBatch(remaining) {
|
|
17
|
+
if (remaining.length === 0)
|
|
18
|
+
return [];
|
|
19
|
+
const count = Math.max(1, Math.ceil((remaining.length * this.batchPercent) / 100));
|
|
20
|
+
return remaining.slice(0, count);
|
|
21
|
+
}
|
|
22
|
+
pauseDuration() {
|
|
23
|
+
return this.pauseMs;
|
|
24
|
+
}
|
|
25
|
+
onBotFailure(_botId, _error, attempt) {
|
|
26
|
+
if (attempt < this.maxRetries())
|
|
27
|
+
return "retry";
|
|
28
|
+
this.totalFailures++;
|
|
29
|
+
if (this.totalFailures >= this.maxFailures)
|
|
30
|
+
return "abort";
|
|
31
|
+
return "skip";
|
|
32
|
+
}
|
|
33
|
+
maxRetries() {
|
|
34
|
+
return 2;
|
|
35
|
+
}
|
|
36
|
+
healthCheckTimeout() {
|
|
37
|
+
return 120_000;
|
|
38
|
+
}
|
|
39
|
+
/** Reset failure counters for reuse across rollouts. */
|
|
40
|
+
reset() {
|
|
41
|
+
this.totalFailures = 0;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class SingleBotStrategy {
|
|
45
|
+
nextBatch(remaining) {
|
|
46
|
+
if (remaining.length === 0)
|
|
47
|
+
return [];
|
|
48
|
+
return remaining.slice(0, 1);
|
|
49
|
+
}
|
|
50
|
+
pauseDuration() {
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
onBotFailure(_botId, _error, attempt) {
|
|
54
|
+
if (attempt < this.maxRetries())
|
|
55
|
+
return "retry";
|
|
56
|
+
return "abort";
|
|
57
|
+
}
|
|
58
|
+
maxRetries() {
|
|
59
|
+
return 3;
|
|
60
|
+
}
|
|
61
|
+
healthCheckTimeout() {
|
|
62
|
+
return 120_000;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export class ImmediateStrategy {
|
|
66
|
+
nextBatch(remaining) {
|
|
67
|
+
return [...remaining];
|
|
68
|
+
}
|
|
69
|
+
pauseDuration() {
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
onBotFailure(_botId, _error, _attempt) {
|
|
73
|
+
return "skip";
|
|
74
|
+
}
|
|
75
|
+
maxRetries() {
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
78
|
+
healthCheckTimeout() {
|
|
79
|
+
return 60_000;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function createRolloutStrategy(type, options) {
|
|
83
|
+
switch (type) {
|
|
84
|
+
case "rolling-wave":
|
|
85
|
+
return new RollingWaveStrategy(options);
|
|
86
|
+
case "single-bot":
|
|
87
|
+
return new SingleBotStrategy();
|
|
88
|
+
case "immediate":
|
|
89
|
+
return new ImmediateStrategy();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type Docker from "dockerode";
|
|
2
|
+
export interface VolumeSnapshot {
|
|
3
|
+
id: string;
|
|
4
|
+
volumeName: string;
|
|
5
|
+
archivePath: string;
|
|
6
|
+
createdAt: Date;
|
|
7
|
+
sizeBytes: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Snapshots and restores Docker named volumes using temporary alpine containers.
|
|
11
|
+
* Used for nuclear rollback during fleet updates — if a container update fails,
|
|
12
|
+
* we roll back both the image AND the data volumes.
|
|
13
|
+
*/
|
|
14
|
+
export declare class VolumeSnapshotManager {
|
|
15
|
+
private readonly docker;
|
|
16
|
+
private readonly backupDir;
|
|
17
|
+
constructor(docker: Docker, backupDir?: string);
|
|
18
|
+
/** Create a snapshot of a Docker named volume */
|
|
19
|
+
snapshot(volumeName: string): Promise<VolumeSnapshot>;
|
|
20
|
+
/** Restore a volume from a snapshot */
|
|
21
|
+
restore(snapshotId: string): Promise<void>;
|
|
22
|
+
/** List all snapshots for a volume */
|
|
23
|
+
list(volumeName: string): Promise<VolumeSnapshot[]>;
|
|
24
|
+
/** Delete a snapshot archive */
|
|
25
|
+
delete(snapshotId: string): Promise<void>;
|
|
26
|
+
/** Delete all snapshots older than maxAge ms */
|
|
27
|
+
cleanup(maxAgeMs: number): Promise<number>;
|
|
28
|
+
/**
|
|
29
|
+
* Extract volume name from snapshot ID.
|
|
30
|
+
* Snapshot IDs are `${volumeName}-${ISO timestamp with colons/dots replaced}`.
|
|
31
|
+
* ISO timestamps start with 4 digits (year), so we find the last occurrence
|
|
32
|
+
* of `-YYYY` pattern to split.
|
|
33
|
+
*/
|
|
34
|
+
private extractVolumeName;
|
|
35
|
+
}
|