@catamorphic/work-server 0.1.0-alpha.41 → 0.1.0-alpha.43
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/agents.d.ts.map +1 -1
- package/dist/execution-config.d.ts +2 -1
- package/dist/execution-config.d.ts.map +1 -1
- package/dist/index.js +213 -106
- package/dist/index.js.map +9 -9
- package/dist/machines/work-machines.d.ts +20 -0
- package/dist/machines/work-machines.d.ts.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/workers/disk-care.d.ts +36 -1
- package/dist/workers/disk-care.d.ts.map +1 -1
- package/dist/workers/machine-store.d.ts.map +1 -1
- package/dist/workers/worker-runtime.d.ts.map +1 -1
- package/package.json +18 -18
- package/src/agents.ts +5 -0
- package/src/execution-config.ts +2 -1
- package/src/machines/work-machines.ts +38 -0
- package/src/server.ts +14 -20
- package/src/workers/disk-care.ts +157 -6
- package/src/workers/machine-store.ts +4 -2
- package/src/workers/worker-runtime.ts +4 -2
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { type WorkWakeups } from "@catamorphic/core";
|
|
1
2
|
import type { DB } from "@catamorphic/db";
|
|
2
3
|
import type { Kysely } from "kysely";
|
|
3
4
|
import type { WorkWorkerRegistry } from "../workers/worker-registry.js";
|
|
4
5
|
import type { MachineEvent, MachinesHook, PersonFacts } from "./contract.js";
|
|
5
6
|
import { type MachinePassSummary, type MachineWorld } from "./sequencer.js";
|
|
7
|
+
/** How often a replica looks for change a plan reads (see `check`). */
|
|
8
|
+
export declare const MACHINE_CHECK_INTERVAL_MS = 10000;
|
|
9
|
+
/** How often it looks for a while after work began waiting on it. */
|
|
10
|
+
export declare const MACHINE_CHECK_QUICK_MS = 500;
|
|
6
11
|
/**
|
|
7
12
|
* The sequencer's world on the Work server (ADR 0349): machines, their
|
|
8
13
|
* workers and what they hold in Postgres, by the database's clock.
|
|
@@ -55,6 +60,21 @@ export declare class WorkMachines {
|
|
|
55
60
|
* tenant's claim either way.
|
|
56
61
|
*/
|
|
57
62
|
check(): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* {@link check} until `signal` aborts: every 10 seconds, and at once
|
|
65
|
+
* when work begins waiting for a machine on this replica, which a
|
|
66
|
+
* `placement` hint says (ADR 0229). The wait may not be committed when
|
|
67
|
+
* its hint arrives, so the looks stay half a second apart for a while
|
|
68
|
+
* after one; waits that begin together share those looks. Work that
|
|
69
|
+
* began waiting on another replica is seen at that replica's hint, or
|
|
70
|
+
* here within 10 seconds.
|
|
71
|
+
*/
|
|
72
|
+
watch(args: {
|
|
73
|
+
signal: AbortSignal;
|
|
74
|
+
wakeups: WorkWakeups;
|
|
75
|
+
intervalMs?: number;
|
|
76
|
+
quickMs?: number;
|
|
77
|
+
}): Promise<void>;
|
|
58
78
|
private get leaseMs();
|
|
59
79
|
private guardedPass;
|
|
60
80
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"work-machines.d.ts","sourceRoot":"","sources":["../../src/machines/work-machines.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"work-machines.d.ts","sourceRoot":"","sources":["../../src/machines/work-machines.ts"],"names":[],"mappings":"AACA,OAAO,EAML,KAAK,WAAW,EACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EAGL,KAAK,kBAAkB,EACvB,KAAK,YAAY,EAElB,MAAM,gBAAgB,CAAC;AAOxB,uEAAuE;AACvE,eAAO,MAAM,yBAAyB,QAAS,CAAC;AAChD,qEAAqE;AACrE,eAAO,MAAM,sBAAsB,MAAM,CAAC;A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;IAED;;;;;;;;OAQG;IACG,KAAK,CAAC,IAAI,EAAE;QAChB,MAAM,EAAE,WAAW,CAAC;QACpB,OAAO,EAAE,WAAW,CAAC;QACrB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,GAAG,OAAO,CAAC,IAAI,CAAC,CAehB;IAED,OAAO,KAAK,OAAO,GAElB;YAEa,WAAW;CAyE1B"}
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,EAIvB,KAAK,mBAAmB,EAKzB,MAAM,mBAAmB,CAAC;AAe3B,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,uBAAuB,EAQ5B,KAAK,uBAAuB,
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,EAIvB,KAAK,mBAAmB,EAKzB,MAAM,mBAAmB,CAAC;AAe3B,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,uBAAuB,EAQ5B,KAAK,uBAAuB,EAW7B,MAAM,yBAAyB,CAAC;AAKjC,OAAgB,EAAE,KAAK,eAAe,EAAuB,MAAM,SAAS,CAAC;AAgB7E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAQ1D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAWpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAGnE,OAAO,KAAK,EAAE,iBAAiB,E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,gBAAgB,CAAC;IACzB,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B;AAED,MAAM,WAAW,UAAU;IACzB,kEAAkE;IAClE,GAAG,EAAE,eAAe,CAAC;IACrB,yEAAyE;IACzE,WAAW,EAAE,eAAe,CAAC;IAC7B,WAAW,EAAE,WAAW,CAAC;IACzB,iEAAiE;IACjE,QAAQ,EAAE,QAAQ,CAAC;IACnB,2EAA2E;IAC3E,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,iBAAiB,EAAE,MAAM,CAAC;IAC1B;;;;;OAKG;IACH,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACpB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,UAAU,CAAC,CAsBrB"}
|
|
@@ -5,6 +5,29 @@ export declare const DISK_MEASURE_INTERVAL_MS = 30000;
|
|
|
5
5
|
export declare const DISK_CLEAN_INTERVAL_MS: number;
|
|
6
6
|
/** How soon it cleans again while it is under its floor. */
|
|
7
7
|
export declare const DISK_LOW_CLEAN_INTERVAL_MS: number;
|
|
8
|
+
/**
|
|
9
|
+
* A machine that noted nothing for this long was not running: stopped
|
|
10
|
+
* (ADR 0350), asleep or down. Its notes are 30 seconds apart.
|
|
11
|
+
*/
|
|
12
|
+
export declare const DISK_AWAY_AFTER_MS: number;
|
|
13
|
+
/** The file in a machine's data folder where it notes that it runs. */
|
|
14
|
+
export declare const RUNNING_TIME_FILE = "running-time.json";
|
|
15
|
+
/** A stretch a machine was not running, in milliseconds since the epoch. */
|
|
16
|
+
export interface AwayStretch {
|
|
17
|
+
from: number;
|
|
18
|
+
to: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* How far back from `now` a machine must look to cover `ranMs` of the time
|
|
22
|
+
* it ran: `ranMs`, and every stretch it was not running that falls inside.
|
|
23
|
+
* A stretch that ended more than `ranMs` of running time ago adds nothing,
|
|
24
|
+
* and one a clock that went back left ahead of `now` counts up to `now`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function reachOfRunningTime(args: {
|
|
27
|
+
ranMs: number;
|
|
28
|
+
now: number;
|
|
29
|
+
away: readonly AwayStretch[];
|
|
30
|
+
}): number;
|
|
8
31
|
export interface DiskCare {
|
|
9
32
|
/** The latest measurement, if any filesystem could be measured. */
|
|
10
33
|
latest(): MachineDisk | undefined;
|
|
@@ -19,7 +42,9 @@ export interface DiskCare {
|
|
|
19
42
|
* with the least room above its floor for the machine to report, and
|
|
20
43
|
* removes what its provider left that nothing used for `graceMs`: once at
|
|
21
44
|
* start, then hourly, and at once (at most every 10 minutes) while under
|
|
22
|
-
* its floor.
|
|
45
|
+
* its floor. With `runningFile`, `graceMs` is time the machine ran: a
|
|
46
|
+
* stretch it was stopped (ADR 0350), asleep or down does not count against
|
|
47
|
+
* what it keeps, so a machine resumed after days still has its images.
|
|
23
48
|
*/
|
|
24
49
|
export declare function startDiskCare(args: {
|
|
25
50
|
provider: Pick<SandboxProvider, "disk">;
|
|
@@ -31,11 +56,21 @@ export declare function startDiskCare(args: {
|
|
|
31
56
|
defaultFloor?: DiskFloorDefault;
|
|
32
57
|
/** `WORK_IMAGE_RETENTION_HOURS`, in milliseconds. */
|
|
33
58
|
graceMs: number;
|
|
59
|
+
/**
|
|
60
|
+
* Where the machine notes that it runs, on the disk a stop retains.
|
|
61
|
+
* Absent, or with a provider that removes nothing, all time counts as
|
|
62
|
+
* running.
|
|
63
|
+
*/
|
|
64
|
+
runningFile?: string;
|
|
65
|
+
/** {@link DISK_AWAY_AFTER_MS} when absent. */
|
|
66
|
+
awayAfterMs?: number;
|
|
34
67
|
measureEveryMs?: number;
|
|
35
68
|
cleanEveryMs?: number;
|
|
36
69
|
lowCleanEveryMs?: number;
|
|
37
70
|
log?: (line: string) => void;
|
|
38
71
|
/** For tests: measure as {@link measureDisk} does. */
|
|
39
72
|
measureDisk?: typeof measureDisk;
|
|
73
|
+
/** The clock, for tests. */
|
|
74
|
+
now?: () => number;
|
|
40
75
|
}): DiskCare;
|
|
41
76
|
//# sourceMappingURL=disk-care.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"disk-care.d.ts","sourceRoot":"","sources":["../../src/workers/disk-care.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"disk-care.d.ts","sourceRoot":"","sources":["../../src/workers/disk-care.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,gBAAgB,EAGrB,KAAK,WAAW,EAChB,WAAW,EACX,KAAK,eAAe,EACrB,MAAM,sBAAsB,CAAC;AAG9B,wDAAwD;AACxD,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAC/C,6DAA6D;AAC7D,eAAO,MAAM,sBAAsB,QAAc,CAAC;AAClD,4DAA4D;AAC5D,eAAO,MAAM,0BAA0B,QAAc,CAAC;AACtD;;;GAGG;AACH,eAAO,MAAM,kBAAkB,QAAa,CAAC;AAC7C,uEAAuE;AACvE,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AAErD,4EAA4E;AAC5E,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,SAAS,WAAW,EAAE,CAAC;CAC9B,GAAG,MAAM,CAYT;AAwFD,MAAM,WAAW,QAAQ;IACvB,mEAAmE;IACnE,MAAM,IAAI,WAAW,GAAG,SAAS,CAAC;IAClC,sEAAsE;IACtE,OAAO,IAAI,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAC;IAC5C,IAAI,IAAI,IAAI,CAAC;CACd;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE;IAClC,QAAQ,EAAE,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;IACxC,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oFAAoF;IACpF,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC,qDAAqD;IACrD,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,sDAAsD;IACtD,WAAW,CAAC,EAAE,OAAO,WAAW,CAAC;IACjC,4BAA4B;IAC5B,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB,GAAG,QAAQ,CAoFX"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"machine-store.d.ts","sourceRoot":"","sources":["../../src/workers/machine-store.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"machine-store.d.ts","sourceRoot":"","sources":["../../src/workers/machine-store.ts"],"names":[],"mappings":"AACA,OAAO,EAKL,KAAK,kBAAkB,EACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EACL,KAAK,MAAM,EACX,KAAK,UAAU,EAEf,KAAK,WAAW,EAEjB,MAAM,QAAQ,CAAC;AAEhB,OAAO,EAGL,KAAK,gBAAgB,EACrB,KAAK,YAAY,EACjB,KAAK,WAAW,EAEhB,KAAK,WA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-
{"version":3,"file":"worker-runtime.d.ts","sourceRoot":"","sources":["../../src/workers/worker-runtime.ts"],"names":[],"mappings":"AAUA,OAAO,EAIL,uBAAuB,EAExB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAEL,KAAK,qBAAqB,EAE3B,MAAM,wBAAwB,CAAC;AAmBhC,KAAK,KAAK,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEtE,MAAM,WAAW,iBAAiB;IAChC,2EAA2E;IAC3E,eAAe,EAAE,MAAM,CAAC;IACxB;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,qBAAqB,CAAC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B;AAmBD;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC;IACzE,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB,CAAC,
|
|
1
|
+
{"version":3,"file":"worker-runtime.d.ts","sourceRoot":"","sources":["../../src/workers/worker-runtime.ts"],"names":[],"mappings":"AAUA,OAAO,EAIL,uBAAuB,EAExB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAEL,KAAK,qBAAqB,EAE3B,MAAM,wBAAwB,CAAC;AAmBhC,KAAK,KAAK,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEtE,MAAM,WAAW,iBAAiB;IAChC,2EAA2E;IAC3E,eAAe,EAAE,MAAM,CAAC;IACxB;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,qBAAqB,CAAC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B;AAmBD;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC;IACzE,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB,CAAC,CA+iBD;AA2JD;;;GAGG;AACH,qBAAa,0BAA2B,SAAQ,uBAAuB;IACrE,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catamorphic/work-server",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.43",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -33,22 +33,22 @@
|
|
|
33
33
|
"@ai-sdk/openai": "^4.0.115",
|
|
34
34
|
"@better-auth/mcp": "1.7.21",
|
|
35
35
|
"@better-auth/oauth-provider": "1.7.21",
|
|
36
|
-
"@catamorphic/agent-protocol": "0.1.0-alpha.
|
|
37
|
-
"@catamorphic/agent-runner": "0.1.0-alpha.
|
|
38
|
-
"@catamorphic/ai-sdk": "0.1.0-alpha.
|
|
39
|
-
"@catamorphic/app": "0.1.0-alpha.
|
|
40
|
-
"@catamorphic/container": "0.1.0-alpha.
|
|
41
|
-
"@catamorphic/core": "0.1.0-alpha.
|
|
42
|
-
"@catamorphic/db": "0.1.0-alpha.
|
|
43
|
-
"@catamorphic/fastify-plugin": "0.1.0-alpha.
|
|
44
|
-
"@catamorphic/local-process": "0.1.0-alpha.
|
|
45
|
-
"@catamorphic/mcp": "0.1.0-alpha.
|
|
46
|
-
"@catamorphic/microsandbox": "0.1.0-alpha.
|
|
47
|
-
"@catamorphic/otel": "0.1.0-alpha.
|
|
48
|
-
"@catamorphic/registry": "0.1.0-alpha.
|
|
49
|
-
"@catamorphic/sandbox": "0.1.0-alpha.
|
|
50
|
-
"@catamorphic/server-sdk": "0.1.0-alpha.
|
|
51
|
-
"@catamorphic/workflow": "0.1.0-alpha.
|
|
36
|
+
"@catamorphic/agent-protocol": "0.1.0-alpha.43",
|
|
37
|
+
"@catamorphic/agent-runner": "0.1.0-alpha.43",
|
|
38
|
+
"@catamorphic/ai-sdk": "0.1.0-alpha.43",
|
|
39
|
+
"@catamorphic/app": "0.1.0-alpha.43",
|
|
40
|
+
"@catamorphic/container": "0.1.0-alpha.43",
|
|
41
|
+
"@catamorphic/core": "0.1.0-alpha.43",
|
|
42
|
+
"@catamorphic/db": "0.1.0-alpha.43",
|
|
43
|
+
"@catamorphic/fastify-plugin": "0.1.0-alpha.43",
|
|
44
|
+
"@catamorphic/local-process": "0.1.0-alpha.43",
|
|
45
|
+
"@catamorphic/mcp": "0.1.0-alpha.43",
|
|
46
|
+
"@catamorphic/microsandbox": "0.1.0-alpha.43",
|
|
47
|
+
"@catamorphic/otel": "0.1.0-alpha.43",
|
|
48
|
+
"@catamorphic/registry": "0.1.0-alpha.43",
|
|
49
|
+
"@catamorphic/sandbox": "0.1.0-alpha.43",
|
|
50
|
+
"@catamorphic/server-sdk": "0.1.0-alpha.43",
|
|
51
|
+
"@catamorphic/workflow": "0.1.0-alpha.43",
|
|
52
52
|
"@electric-sql/pglite": "^0.5.16",
|
|
53
53
|
"@openai/codex": "0.169.0",
|
|
54
54
|
"better-auth": "1.7.21",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@aws-sdk/client-ec2": "3.1171.0",
|
|
63
|
-
"@catamorphic/claude-code": "0.1.0-alpha.
|
|
63
|
+
"@catamorphic/claude-code": "0.1.0-alpha.43",
|
|
64
64
|
"@modelcontextprotocol/client": "^2.4.0",
|
|
65
65
|
"@types/node": "^26.6.6",
|
|
66
66
|
"@types/pg": "8.23.1"
|
package/src/agents.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { createAiSdkAdapter } from "@catamorphic/ai-sdk";
|
|
|
4
4
|
import {
|
|
5
5
|
type AgentDefinition,
|
|
6
6
|
type AgentHarness,
|
|
7
|
+
acpToolTimeoutMs,
|
|
7
8
|
type CodingAgentRegistry,
|
|
8
9
|
normalizeConnectionRequirement,
|
|
9
10
|
type RegisteredCodingAgent,
|
|
@@ -277,6 +278,10 @@ function sandboxProjectAgent(input: {
|
|
|
277
278
|
const common = {
|
|
278
279
|
id: input.id,
|
|
279
280
|
...harness,
|
|
281
|
+
// Its client gives a tool call up after its own timeout (ADR 0351).
|
|
282
|
+
...(definition.kind === "acp"
|
|
283
|
+
? { toolTimeoutMs: acpToolTimeoutMs(definition.acp) }
|
|
284
|
+
: {}),
|
|
280
285
|
topology: "controller" as const,
|
|
281
286
|
sandboxing: definition.sandboxing ?? "propose",
|
|
282
287
|
environment: definition.environment,
|
package/src/execution-config.ts
CHANGED
|
@@ -117,7 +117,8 @@ export interface WorkExecutionSettings {
|
|
|
117
117
|
/**
|
|
118
118
|
* Hours an image Work built or pulled, and the Docker data inside
|
|
119
119
|
* sandboxes, may go unused before the machine removes it (ADR 0336,
|
|
120
|
-
* `WORK_IMAGE_RETENTION_HOURS`, default 24).
|
|
120
|
+
* `WORK_IMAGE_RETENTION_HOURS`, default 24). Hours the machine ran: the
|
|
121
|
+
* time it was stopped does not count (ADR 0350).
|
|
121
122
|
*/
|
|
122
123
|
imageRetentionHours: number;
|
|
123
124
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import {
|
|
3
3
|
PlacementWaitsService,
|
|
4
|
+
PollPacer,
|
|
4
5
|
releaseReplicaClaim,
|
|
5
6
|
renewReplicaClaim,
|
|
6
7
|
takeReplicaClaim,
|
|
8
|
+
type WorkWakeups,
|
|
7
9
|
} from "@catamorphic/core";
|
|
8
10
|
import type { DB } from "@catamorphic/db";
|
|
9
11
|
import { getTracer, withSpan } from "@catamorphic/otel";
|
|
@@ -23,6 +25,11 @@ const tracer = getTracer("@catamorphic/work-server");
|
|
|
23
25
|
/** How long a pass waits for one machine's reset; a slower one finishes later. */
|
|
24
26
|
const RESET_WAIT_MS = 3 * 60_000;
|
|
25
27
|
|
|
28
|
+
/** How often a replica looks for change a plan reads (see `check`). */
|
|
29
|
+
export const MACHINE_CHECK_INTERVAL_MS = 10_000;
|
|
30
|
+
/** How often it looks for a while after work began waiting on it. */
|
|
31
|
+
export const MACHINE_CHECK_QUICK_MS = 500;
|
|
32
|
+
|
|
26
33
|
/**
|
|
27
34
|
* The sequencer's world on the Work server (ADR 0349): machines, their
|
|
28
35
|
* workers and what they hold in Postgres, by the database's clock.
|
|
@@ -147,6 +154,37 @@ export class WorkMachines {
|
|
|
147
154
|
this.passSoon();
|
|
148
155
|
}
|
|
149
156
|
|
|
157
|
+
/**
|
|
158
|
+
* {@link check} until `signal` aborts: every 10 seconds, and at once
|
|
159
|
+
* when work begins waiting for a machine on this replica, which a
|
|
160
|
+
* `placement` hint says (ADR 0229). The wait may not be committed when
|
|
161
|
+
* its hint arrives, so the looks stay half a second apart for a while
|
|
162
|
+
* after one; waits that begin together share those looks. Work that
|
|
163
|
+
* began waiting on another replica is seen at that replica's hint, or
|
|
164
|
+
* here within 10 seconds.
|
|
165
|
+
*/
|
|
166
|
+
async watch(args: {
|
|
167
|
+
signal: AbortSignal;
|
|
168
|
+
wakeups: WorkWakeups;
|
|
169
|
+
intervalMs?: number;
|
|
170
|
+
quickMs?: number;
|
|
171
|
+
}): Promise<void> {
|
|
172
|
+
const pacer = new PollPacer({
|
|
173
|
+
quickMs: args.quickMs ?? MACHINE_CHECK_QUICK_MS,
|
|
174
|
+
idleMs: args.intervalMs ?? MACHINE_CHECK_INTERVAL_MS,
|
|
175
|
+
signal: args.signal,
|
|
176
|
+
wakeups: args.wakeups,
|
|
177
|
+
topic: "placement",
|
|
178
|
+
});
|
|
179
|
+
while (!args.signal.aborted) {
|
|
180
|
+
await pacer.wait();
|
|
181
|
+
if (args.signal.aborted) return;
|
|
182
|
+
await this.check().catch((error: unknown) =>
|
|
183
|
+
this.deps.log?.(`Machine check failed: ${message(error)}`),
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
150
188
|
private get leaseMs(): number {
|
|
151
189
|
return this.deps.leaseMs ?? 120_000;
|
|
152
190
|
}
|
package/src/server.ts
CHANGED
|
@@ -48,6 +48,7 @@ import {
|
|
|
48
48
|
machineProjectEvent,
|
|
49
49
|
ObjectRemoteBackend,
|
|
50
50
|
PostgresObjectStore,
|
|
51
|
+
PROJECT_TRIGGER_KINDS,
|
|
51
52
|
ProjectManager,
|
|
52
53
|
SESSION_TRIGGER_KINDS,
|
|
53
54
|
schedule,
|
|
@@ -126,7 +127,7 @@ import {
|
|
|
126
127
|
import { registerShareRoutes } from "./shares/share-routes.js";
|
|
127
128
|
import { shareTools } from "./shares/share-tools.js";
|
|
128
129
|
import { WorkSharesService } from "./shares/shares-service.js";
|
|
129
|
-
import { startDiskCare } from "./workers/disk-care.js";
|
|
130
|
+
import { RUNNING_TIME_FILE, startDiskCare } from "./workers/disk-care.js";
|
|
130
131
|
import { HarnessSignIns } from "./workers/harness-sign-ins.js";
|
|
131
132
|
import { registerMemberMachineRoutes } from "./workers/member-machine-routes.js";
|
|
132
133
|
import { MemberMachines } from "./workers/member-machines.js";
|
|
@@ -430,6 +431,7 @@ async function createWorkServerInner(
|
|
|
430
431
|
? { criticalMb: execution.diskCriticalMb }
|
|
431
432
|
: {}),
|
|
432
433
|
graceMs: execution.imageRetentionMs,
|
|
434
|
+
runningFile: path.join(data, RUNNING_TIME_FILE),
|
|
433
435
|
log,
|
|
434
436
|
});
|
|
435
437
|
disposers.push(async () => diskCare.stop());
|
|
@@ -700,6 +702,8 @@ async function createWorkServerInner(
|
|
|
700
702
|
schedule,
|
|
701
703
|
webhook,
|
|
702
704
|
...SESSION_TRIGGER_KINDS,
|
|
705
|
+
// A revision's deploy is an event workflows bind (ADR 0355).
|
|
706
|
+
...PROJECT_TRIGGER_KINDS,
|
|
703
707
|
...DIRECTORY_TRIGGER_KINDS,
|
|
704
708
|
// What happens to machines, for projects' own workflows (ADR 0349).
|
|
705
709
|
...MACHINE_TRIGGER_KINDS,
|
|
@@ -981,30 +985,20 @@ async function createWorkServerInner(
|
|
|
981
985
|
if (workMachines) {
|
|
982
986
|
// A pass every minute, and at once when what a plan reads changed:
|
|
983
987
|
// waiting work, what machines hold, a machine connecting or going
|
|
984
|
-
// unseen, accounts and roles. That is one cheap look every few seconds
|
|
988
|
+
// unseen, accounts and roles. That is one cheap look every few seconds,
|
|
989
|
+
// and one as soon as work begins waiting for a machine on this replica.
|
|
985
990
|
const machineTimer = setInterval(planMachines, 60_000);
|
|
986
991
|
machineTimer.unref();
|
|
987
|
-
|
|
988
|
-
const
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
log(
|
|
993
|
-
`Machine check failed: ${
|
|
994
|
-
error instanceof Error ? error.message : String(error)
|
|
995
|
-
}`,
|
|
996
|
-
),
|
|
997
|
-
)
|
|
998
|
-
.finally(() => {
|
|
999
|
-
checking = undefined;
|
|
1000
|
-
});
|
|
1001
|
-
}, 10_000);
|
|
1002
|
-
checkTimer.unref();
|
|
992
|
+
const watching = new AbortController();
|
|
993
|
+
const watched = workMachines.watch({
|
|
994
|
+
signal: watching.signal,
|
|
995
|
+
wakeups: core.wakeups,
|
|
996
|
+
});
|
|
1003
997
|
planMachines();
|
|
1004
998
|
disposers.push(async () => {
|
|
1005
999
|
clearInterval(machineTimer);
|
|
1006
|
-
|
|
1007
|
-
await
|
|
1000
|
+
watching.abort();
|
|
1001
|
+
await watched;
|
|
1008
1002
|
await workMachines.settle();
|
|
1009
1003
|
});
|
|
1010
1004
|
}
|
package/src/workers/disk-care.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
1
3
|
import {
|
|
2
4
|
type DiskFloorDefault,
|
|
3
5
|
describeDisk,
|
|
@@ -6,6 +8,7 @@ import {
|
|
|
6
8
|
measureDisk,
|
|
7
9
|
type SandboxProvider,
|
|
8
10
|
} from "@catamorphic/sandbox";
|
|
11
|
+
import { z } from "zod";
|
|
9
12
|
|
|
10
13
|
/** How often a machine measures its disk (ADR 0336). */
|
|
11
14
|
export const DISK_MEASURE_INTERVAL_MS = 30_000;
|
|
@@ -13,6 +16,129 @@ export const DISK_MEASURE_INTERVAL_MS = 30_000;
|
|
|
13
16
|
export const DISK_CLEAN_INTERVAL_MS = 60 * 60_000;
|
|
14
17
|
/** How soon it cleans again while it is under its floor. */
|
|
15
18
|
export const DISK_LOW_CLEAN_INTERVAL_MS = 10 * 60_000;
|
|
19
|
+
/**
|
|
20
|
+
* A machine that noted nothing for this long was not running: stopped
|
|
21
|
+
* (ADR 0350), asleep or down. Its notes are 30 seconds apart.
|
|
22
|
+
*/
|
|
23
|
+
export const DISK_AWAY_AFTER_MS = 5 * 60_000;
|
|
24
|
+
/** The file in a machine's data folder where it notes that it runs. */
|
|
25
|
+
export const RUNNING_TIME_FILE = "running-time.json";
|
|
26
|
+
|
|
27
|
+
/** A stretch a machine was not running, in milliseconds since the epoch. */
|
|
28
|
+
export interface AwayStretch {
|
|
29
|
+
from: number;
|
|
30
|
+
to: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* How far back from `now` a machine must look to cover `ranMs` of the time
|
|
35
|
+
* it ran: `ranMs`, and every stretch it was not running that falls inside.
|
|
36
|
+
* A stretch that ended more than `ranMs` of running time ago adds nothing,
|
|
37
|
+
* and one a clock that went back left ahead of `now` counts up to `now`.
|
|
38
|
+
*/
|
|
39
|
+
export function reachOfRunningTime(args: {
|
|
40
|
+
ranMs: number;
|
|
41
|
+
now: number;
|
|
42
|
+
away: readonly AwayStretch[];
|
|
43
|
+
}): number {
|
|
44
|
+
let left = args.ranMs;
|
|
45
|
+
let cursor = args.now;
|
|
46
|
+
for (const stretch of [...args.away].sort((a, b) => b.to - a.to)) {
|
|
47
|
+
const to = Math.min(stretch.to, cursor);
|
|
48
|
+
if (stretch.from >= to) continue;
|
|
49
|
+
const ran = cursor - to;
|
|
50
|
+
if (ran >= left) break;
|
|
51
|
+
left -= ran;
|
|
52
|
+
cursor = stretch.from;
|
|
53
|
+
}
|
|
54
|
+
return args.now - (cursor - left);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** What a machine keeps of when it ran: its last note, and each stretch away. */
|
|
58
|
+
const RunningNotes = z.object({
|
|
59
|
+
seenAt: z.number(),
|
|
60
|
+
away: z.array(z.tuple([z.number(), z.number()])),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* When a machine was not running, kept in one small JSON file on the disk a
|
|
65
|
+
* stop retains (ADR 0350). The machine notes that it runs every time it
|
|
66
|
+
* looks at its disk; a note more than `awayAfterMs` after the one before
|
|
67
|
+
* closes a stretch it was away. A machine that crashed left its last note,
|
|
68
|
+
* so the time after it counts as away too. A machine without a file, or
|
|
69
|
+
* whose clock went back, has no stretch to add: it starts from this note.
|
|
70
|
+
*/
|
|
71
|
+
class RunningTime {
|
|
72
|
+
private seenAt: number | undefined;
|
|
73
|
+
private away: AwayStretch[] = [];
|
|
74
|
+
private failed = false;
|
|
75
|
+
|
|
76
|
+
constructor(
|
|
77
|
+
private readonly args: {
|
|
78
|
+
file: string;
|
|
79
|
+
/** Stretches older than this much running time are forgotten. */
|
|
80
|
+
keepMs: number;
|
|
81
|
+
awayAfterMs: number;
|
|
82
|
+
now: () => number;
|
|
83
|
+
log?: (line: string) => void;
|
|
84
|
+
},
|
|
85
|
+
) {
|
|
86
|
+
const notes = RunningNotes.safeParse(read(args.file)).data;
|
|
87
|
+
if (!notes) return;
|
|
88
|
+
this.seenAt = notes.seenAt;
|
|
89
|
+
this.away = notes.away.map(([from, to]) => ({ from, to }));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The machine runs now. Answers how far back `keepMs` of its running
|
|
94
|
+
* time reaches.
|
|
95
|
+
*/
|
|
96
|
+
note(): number {
|
|
97
|
+
const now = this.args.now();
|
|
98
|
+
if (this.seenAt !== undefined && now - this.seenAt > this.args.awayAfterMs)
|
|
99
|
+
this.away.push({ from: this.seenAt, to: now });
|
|
100
|
+
this.seenAt = now;
|
|
101
|
+
const reach = reachOfRunningTime({
|
|
102
|
+
ranMs: this.args.keepMs,
|
|
103
|
+
now,
|
|
104
|
+
away: this.away,
|
|
105
|
+
});
|
|
106
|
+
this.away = this.away.filter(
|
|
107
|
+
(stretch) => stretch.to > now - reach && stretch.from < now,
|
|
108
|
+
);
|
|
109
|
+
try {
|
|
110
|
+
fs.mkdirSync(path.dirname(this.args.file), { recursive: true });
|
|
111
|
+
const temporary = `${this.args.file}.${process.pid}.tmp`;
|
|
112
|
+
fs.writeFileSync(
|
|
113
|
+
temporary,
|
|
114
|
+
JSON.stringify({
|
|
115
|
+
seenAt: this.seenAt,
|
|
116
|
+
away: this.away.map((stretch) => [stretch.from, stretch.to]),
|
|
117
|
+
}),
|
|
118
|
+
{ mode: 0o600 },
|
|
119
|
+
);
|
|
120
|
+
fs.renameSync(temporary, this.args.file);
|
|
121
|
+
this.failed = false;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
// Said once. The next start counts from the last note that was kept.
|
|
124
|
+
if (!this.failed)
|
|
125
|
+
this.args.log?.(
|
|
126
|
+
`Could not note that this machine is running: ${error instanceof Error ? error.message : String(error)}`,
|
|
127
|
+
);
|
|
128
|
+
this.failed = true;
|
|
129
|
+
}
|
|
130
|
+
return reach;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A JSON file's content, or undefined for one that is absent or unreadable. */
|
|
135
|
+
function read(file: string): unknown {
|
|
136
|
+
try {
|
|
137
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
138
|
+
} catch {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
16
142
|
|
|
17
143
|
export interface DiskCare {
|
|
18
144
|
/** The latest measurement, if any filesystem could be measured. */
|
|
@@ -29,7 +155,9 @@ export interface DiskCare {
|
|
|
29
155
|
* with the least room above its floor for the machine to report, and
|
|
30
156
|
* removes what its provider left that nothing used for `graceMs`: once at
|
|
31
157
|
* start, then hourly, and at once (at most every 10 minutes) while under
|
|
32
|
-
* its floor.
|
|
158
|
+
* its floor. With `runningFile`, `graceMs` is time the machine ran: a
|
|
159
|
+
* stretch it was stopped (ADR 0350), asleep or down does not count against
|
|
160
|
+
* what it keeps, so a machine resumed after days still has its images.
|
|
33
161
|
*/
|
|
34
162
|
export function startDiskCare(args: {
|
|
35
163
|
provider: Pick<SandboxProvider, "disk">;
|
|
@@ -41,14 +169,34 @@ export function startDiskCare(args: {
|
|
|
41
169
|
defaultFloor?: DiskFloorDefault;
|
|
42
170
|
/** `WORK_IMAGE_RETENTION_HOURS`, in milliseconds. */
|
|
43
171
|
graceMs: number;
|
|
172
|
+
/**
|
|
173
|
+
* Where the machine notes that it runs, on the disk a stop retains.
|
|
174
|
+
* Absent, or with a provider that removes nothing, all time counts as
|
|
175
|
+
* running.
|
|
176
|
+
*/
|
|
177
|
+
runningFile?: string;
|
|
178
|
+
/** {@link DISK_AWAY_AFTER_MS} when absent. */
|
|
179
|
+
awayAfterMs?: number;
|
|
44
180
|
measureEveryMs?: number;
|
|
45
181
|
cleanEveryMs?: number;
|
|
46
182
|
lowCleanEveryMs?: number;
|
|
47
183
|
log?: (line: string) => void;
|
|
48
184
|
/** For tests: measure as {@link measureDisk} does. */
|
|
49
185
|
measureDisk?: typeof measureDisk;
|
|
186
|
+
/** The clock, for tests. */
|
|
187
|
+
now?: () => number;
|
|
50
188
|
}): DiskCare {
|
|
51
189
|
const measure = args.measureDisk ?? measureDisk;
|
|
190
|
+
const running =
|
|
191
|
+
args.runningFile && args.provider.disk?.clean
|
|
192
|
+
? new RunningTime({
|
|
193
|
+
file: args.runningFile,
|
|
194
|
+
keepMs: args.graceMs,
|
|
195
|
+
awayAfterMs: args.awayAfterMs ?? DISK_AWAY_AFTER_MS,
|
|
196
|
+
now: args.now ?? Date.now,
|
|
197
|
+
...(args.log ? { log: args.log } : {}),
|
|
198
|
+
})
|
|
199
|
+
: undefined;
|
|
52
200
|
let latest: MachineDisk | undefined;
|
|
53
201
|
let wasLow = false;
|
|
54
202
|
let stopped = false;
|
|
@@ -58,7 +206,9 @@ export function startDiskCare(args: {
|
|
|
58
206
|
const run = args.provider.disk?.clean;
|
|
59
207
|
if (!run || cleaning || stopped) return;
|
|
60
208
|
lastClean = Date.now();
|
|
61
|
-
|
|
209
|
+
// Noted as it is read: a machine that slept through its last note
|
|
210
|
+
// counts that stretch before it removes anything.
|
|
211
|
+
cleaning = run({ graceMs: running?.note() ?? args.graceMs })
|
|
62
212
|
.then((lines) => {
|
|
63
213
|
for (const line of lines) args.log?.(line);
|
|
64
214
|
})
|
|
@@ -104,11 +254,12 @@ export function startDiskCare(args: {
|
|
|
104
254
|
});
|
|
105
255
|
return measuring;
|
|
106
256
|
};
|
|
257
|
+
running?.note();
|
|
107
258
|
void measureNow();
|
|
108
|
-
const timer = setInterval(
|
|
109
|
-
()
|
|
110
|
-
|
|
111
|
-
);
|
|
259
|
+
const timer = setInterval(() => {
|
|
260
|
+
running?.note();
|
|
261
|
+
void measureNow();
|
|
262
|
+
}, args.measureEveryMs ?? DISK_MEASURE_INTERVAL_MS);
|
|
112
263
|
timer.unref();
|
|
113
264
|
return {
|
|
114
265
|
latest: () => latest,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash, createHmac, randomBytes, randomUUID } from "node:crypto";
|
|
2
2
|
import {
|
|
3
3
|
isProjectPrincipal,
|
|
4
|
+
PLACEMENT_FREE_JOB_KINDS,
|
|
4
5
|
PLACEMENT_WAIT_FRESH_SECONDS,
|
|
5
6
|
type WorkerNode,
|
|
6
7
|
type WorkerNodesService,
|
|
@@ -1031,7 +1032,8 @@ function waitFresh() {
|
|
|
1031
1032
|
/**
|
|
1032
1033
|
* Chats and runs reuse placement demand, and a cache fill that waits for a
|
|
1033
1034
|
* machine with room for its size (ADR 0336); waiting journals alone are no
|
|
1034
|
-
* demand
|
|
1035
|
+
* demand, and neither is a job that needs no placement (a pause timeout, a
|
|
1036
|
+
* host call's follower): any replica runs it without a machine.
|
|
1035
1037
|
*/
|
|
1036
1038
|
function queuedPlacementWait(db: Kysely<DB>) {
|
|
1037
1039
|
const turns = db
|
|
@@ -1052,7 +1054,7 @@ function queuedPlacementWait(db: Kysely<DB>) {
|
|
|
1052
1054
|
.where("root.status", "in", ["pending", "running", "waiting"])
|
|
1053
1055
|
.where("run.status", "in", ["pending", "running", "waiting"])
|
|
1054
1056
|
.where("job.status", "=", "pending")
|
|
1055
|
-
.where("job.kind", "
|
|
1057
|
+
.where("job.kind", "not in", [...PLACEMENT_FREE_JOB_KINDS])
|
|
1056
1058
|
.where("job.available_at", "<=", sql<Date>`now()`);
|
|
1057
1059
|
const fills = db
|
|
1058
1060
|
.selectFrom("environment_cache_fills")
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
type WorkExecutionSettings,
|
|
22
22
|
workExecution,
|
|
23
23
|
} from "../execution-config.js";
|
|
24
|
-
import { startDiskCare } from "./disk-care.js";
|
|
24
|
+
import { RUNNING_TIME_FILE, startDiskCare } from "./disk-care.js";
|
|
25
25
|
import { HarnessSignIns } from "./harness-sign-ins.js";
|
|
26
26
|
import { removeMachineSignIns, signInCapabilities } from "./sign-ins.js";
|
|
27
27
|
import { startVolumePruning } from "./volume-pruning.js";
|
|
@@ -220,7 +220,8 @@ export async function startWorkWorker(options: WorkWorkerOptions): Promise<{
|
|
|
220
220
|
log,
|
|
221
221
|
});
|
|
222
222
|
// Its disk, measured and reported with its offer and its renewals, and
|
|
223
|
-
// what Work left there removed (ADR 0336).
|
|
223
|
+
// what Work left there removed (ADR 0336). The time it was stopped does
|
|
224
|
+
// not count against what it keeps (ADR 0350).
|
|
224
225
|
const diskCare = startDiskCare({
|
|
225
226
|
provider,
|
|
226
227
|
...(execution.diskFloorMb !== undefined
|
|
@@ -230,6 +231,7 @@ export async function startWorkWorker(options: WorkWorkerOptions): Promise<{
|
|
|
230
231
|
? { criticalMb: execution.diskCriticalMb }
|
|
231
232
|
: {}),
|
|
232
233
|
graceMs: execution.imageRetentionMs,
|
|
234
|
+
runningFile: path.join(options.dataDir, RUNNING_TIME_FILE),
|
|
233
235
|
log,
|
|
234
236
|
});
|
|
235
237
|
await diskCare.measure();
|