@dynamicagents/plugins 0.14.0 → 0.16.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.
Files changed (141) hide show
  1. package/README.md +14 -14
  2. package/dist/claude-code/cards.d.ts +2 -0
  3. package/dist/claude-code/cards.js +11 -0
  4. package/dist/claude-code/config.d.ts +10 -83
  5. package/dist/claude-code/config.js +0 -2
  6. package/dist/claude-code/credentials.d.ts +32 -50
  7. package/dist/claude-code/credentials.js +115 -74
  8. package/dist/claude-code/daemon/index.d.ts +24 -0
  9. package/dist/claude-code/daemon/index.js +203 -0
  10. package/dist/claude-code/egress.d.ts +35 -117
  11. package/dist/claude-code/egress.js +80 -325
  12. package/dist/claude-code/events.d.ts +14 -17
  13. package/dist/claude-code/events.js +19 -15
  14. package/dist/claude-code/harness-wire.d.ts +45 -0
  15. package/dist/claude-code/harness-wire.js +47 -0
  16. package/dist/claude-code/index.d.ts +19 -29
  17. package/dist/claude-code/index.js +17 -25
  18. package/dist/claude-code/language-model.d.ts +19 -0
  19. package/dist/claude-code/language-model.js +71 -0
  20. package/dist/claude-code/model.d.ts +77 -70
  21. package/dist/claude-code/model.js +327 -327
  22. package/dist/claude-code/placeholder.d.ts +12 -0
  23. package/dist/claude-code/placeholder.js +12 -0
  24. package/dist/claude-code/spec.d.ts +4 -4
  25. package/dist/claude-code/spec.js +4 -4
  26. package/dist/computer/client.d.ts +88 -0
  27. package/dist/computer/client.js +6 -0
  28. package/dist/computer/context.d.ts +7 -7
  29. package/dist/computer/context.js +14 -18
  30. package/dist/computer/gate.d.ts +4 -23
  31. package/dist/computer/gate.js +1 -37
  32. package/dist/computer/index.d.ts +44 -43
  33. package/dist/computer/index.js +29 -50
  34. package/dist/computer/paths.d.ts +5 -27
  35. package/dist/computer/paths.js +7 -30
  36. package/dist/computer/proxy.d.ts +17 -31
  37. package/dist/computer/proxy.js +9 -44
  38. package/dist/computer/render.d.ts +3 -40
  39. package/dist/computer/render.js +3 -36
  40. package/dist/computer/sandbox.d.ts +55 -0
  41. package/dist/computer/sandbox.js +236 -0
  42. package/dist/computer/shell.d.ts +36 -0
  43. package/dist/computer/shell.js +41 -0
  44. package/dist/computer/tools-exec.js +3 -9
  45. package/dist/computer/tools-file.d.ts +1 -1
  46. package/dist/computer/tools-file.js +1 -1
  47. package/dist/{repo → repo-github}/checkout.d.ts +3 -4
  48. package/dist/{repo → repo-github}/checkout.js +3 -4
  49. package/dist/{repo → repo-github}/context.d.ts +14 -32
  50. package/dist/{repo → repo-github}/context.js +46 -81
  51. package/dist/{repo → repo-github}/index.d.ts +30 -13
  52. package/dist/{repo → repo-github}/index.js +25 -6
  53. package/dist/repo-github/mcp.d.ts +95 -0
  54. package/dist/repo-github/mcp.js +107 -0
  55. package/dist/repo-github/sandbox-egress.d.ts +68 -0
  56. package/dist/repo-github/sandbox-egress.js +272 -0
  57. package/dist/repo-github/sandbox-git.d.ts +30 -0
  58. package/dist/repo-github/sandbox-git.js +113 -0
  59. package/dist/{repo → repo-github}/tools-checks.js +43 -4
  60. package/dist/{repo → repo-github}/tools-clone.js +2 -21
  61. package/dist/{repo → repo-github}/tools-pr.d.ts +0 -1
  62. package/dist/{repo → repo-github}/tools-pr.js +85 -20
  63. package/dist/{repo → repo-github}/tools-review.d.ts +6 -0
  64. package/dist/{repo → repo-github}/tools-review.js +62 -176
  65. package/dist/{repo → repo-github}/url.d.ts +11 -0
  66. package/dist/{repo → repo-github}/url.js +12 -1
  67. package/dist/{workspace → sandbox}/advisory.d.ts +16 -35
  68. package/dist/{workspace → sandbox}/advisory.js +2 -29
  69. package/dist/sandbox/agent-exec.d.ts +31 -0
  70. package/dist/sandbox/agent-exec.js +62 -0
  71. package/dist/sandbox/ca-trust.d.ts +9 -0
  72. package/dist/sandbox/ca-trust.js +21 -0
  73. package/dist/sandbox/constants.d.ts +22 -0
  74. package/dist/sandbox/constants.js +22 -0
  75. package/dist/sandbox/daemon/composite.d.ts +9 -0
  76. package/dist/sandbox/daemon/composite.js +35 -0
  77. package/dist/sandbox/daemon/index.d.ts +47 -0
  78. package/dist/sandbox/daemon/index.js +116 -0
  79. package/dist/sandbox/egress.d.ts +81 -0
  80. package/dist/sandbox/egress.js +109 -0
  81. package/dist/sandbox/exec.d.ts +57 -0
  82. package/dist/sandbox/exec.js +52 -0
  83. package/dist/sandbox/format.d.ts +2 -0
  84. package/dist/sandbox/format.js +7 -0
  85. package/dist/sandbox/header.d.ts +50 -0
  86. package/dist/sandbox/header.js +94 -0
  87. package/dist/sandbox/index.d.ts +17 -0
  88. package/dist/sandbox/index.js +17 -0
  89. package/dist/{workspace/install.d.ts → sandbox/install-plan.d.ts} +2 -2
  90. package/dist/{workspace/install.js → sandbox/install-plan.js} +2 -2
  91. package/dist/sandbox/name.d.ts +27 -0
  92. package/dist/sandbox/name.js +32 -0
  93. package/dist/sandbox/object.d.ts +244 -0
  94. package/dist/sandbox/object.js +1139 -0
  95. package/dist/scratch/index.d.ts +1 -9
  96. package/dist/scratch/index.js +2 -18
  97. package/package.json +24 -20
  98. package/dist/claude-code/github.d.ts +0 -39
  99. package/dist/claude-code/github.js +0 -82
  100. package/dist/claude-code/run.d.ts +0 -491
  101. package/dist/claude-code/run.js +0 -913
  102. package/dist/claude-code/session.d.ts +0 -131
  103. package/dist/claude-code/session.js +0 -288
  104. package/dist/workspace/ca-trust.d.ts +0 -95
  105. package/dist/workspace/ca-trust.js +0 -312
  106. package/dist/workspace/container-deps.d.ts +0 -58
  107. package/dist/workspace/container-deps.js +0 -249
  108. package/dist/workspace/container-fault.d.ts +0 -38
  109. package/dist/workspace/container-fault.js +0 -73
  110. package/dist/workspace/deps-snapshot.d.ts +0 -110
  111. package/dist/workspace/deps-snapshot.js +0 -424
  112. package/dist/workspace/exec.d.ts +0 -94
  113. package/dist/workspace/exec.js +0 -153
  114. package/dist/workspace/format.d.ts +0 -36
  115. package/dist/workspace/format.js +0 -57
  116. package/dist/workspace/git-host.d.ts +0 -79
  117. package/dist/workspace/git-host.js +0 -226
  118. package/dist/workspace/git-identity.d.ts +0 -57
  119. package/dist/workspace/git-identity.js +0 -147
  120. package/dist/workspace/index.d.ts +0 -50
  121. package/dist/workspace/index.js +0 -58
  122. package/dist/workspace/install-job.d.ts +0 -170
  123. package/dist/workspace/install-job.js +0 -934
  124. package/dist/workspace/object.d.ts +0 -522
  125. package/dist/workspace/object.js +0 -1541
  126. package/dist/workspace/open.d.ts +0 -95
  127. package/dist/workspace/open.js +0 -63
  128. package/dist/workspace/read.d.ts +0 -16
  129. package/dist/workspace/read.js +0 -22
  130. package/dist/workspace/shell.d.ts +0 -45
  131. package/dist/workspace/shell.js +0 -80
  132. package/dist/workspace/sync.d.ts +0 -111
  133. package/dist/workspace/sync.js +0 -124
  134. package/dist/workspace/wake.d.ts +0 -21
  135. package/dist/workspace/wake.js +0 -10
  136. /package/dist/{repo → repo-github}/tools-checks.d.ts +0 -0
  137. /package/dist/{repo → repo-github}/tools-clone.d.ts +0 -0
  138. /package/dist/{repo → repo-github}/tools-worktree.d.ts +0 -0
  139. /package/dist/{repo → repo-github}/tools-worktree.js +0 -0
  140. /package/dist/{repo → repo-github}/tools-worktrees.d.ts +0 -0
  141. /package/dist/{repo → repo-github}/tools-worktrees.js +0 -0
@@ -1,73 +0,0 @@
1
- /**
2
- * The container failures that are the deployment's fault, not the workspace's.
3
- *
4
- * Everything else this host catches is the kind of thing a retry clears: a
5
- * container that is starting, a connection that dropped, a command that died
6
- * with it. These are not. They describe a Worker and a container application
7
- * that cannot work together no matter how many times they are asked, and they
8
- * end only when an operator deploys.
9
- *
10
- * **This changes what is said, not what is done.** No caller branches its
11
- * control flow on the answer, and none should start: the match is on a string
12
- * the library is free to reword, so a misread has to cost a misleading sentence
13
- * rather than a workspace that stopped trying. What it earns is the log line.
14
- * The cause is already in the thrown message, but a caller logs its own
15
- * sentence and the cause travels in a detail field a log digest truncates — so
16
- * an operator scanning for what broke reads the symptom ("could not trust the
17
- * interception CA") and not the reason. {@link deploymentFault} is how a caller
18
- * puts the reason in the message instead, and how the install record stops
19
- * offering advice that cannot work.
20
- */
21
- /**
22
- * Match on the shape the backend formats, not on the prose after it.
23
- *
24
- * `[stage=auth]` is the stage label `ContainerBackend` writes into every
25
- * message it throws from that stage, and the container-application sentence is
26
- * the runtime's own, surfaced verbatim through the backend's `priorExit=` field.
27
- * `prepared images` is in both of the backend's refusals to start without an
28
- * image — none prepared at all, and none under the key it was asked for — and
29
- * survives the reconnect wrapper's truncation of either. All are stabler than
30
- * the explanations they introduce, and none has a `code` to key on the way a
31
- * lost exec does.
32
- */
33
- const AUTH_STAGE = "[stage=auth]";
34
- const NO_APPLICATION = "no container application";
35
- const NO_IMAGE = "prepared images";
36
- /**
37
- * Whether this error says the deployment is wrong, and what to say about it.
38
- *
39
- * `undefined` for everything else, which is the answer that keeps the existing
40
- * behaviour: a caller that gets it logs and retries exactly as before.
41
- */
42
- export function deploymentFault(err) {
43
- const text = String(err);
44
- if (text.includes(AUTH_STAGE))
45
- return {
46
- summary: "the container image is older than this Worker",
47
- remedy: "the container is running an image that predates this Worker, so the " +
48
- "two cannot authenticate and nothing can reach it. The image and the " +
49
- "Worker are released as a pair and have to be deployed together, so " +
50
- "this clears when an operator redeploys the Worker with its container " +
51
- "image — no command here can recover it."
52
- };
53
- if (text.includes(NO_APPLICATION))
54
- return {
55
- summary: "this Worker has no container application",
56
- remedy: "the container application this workspace starts containers from does " +
57
- "not exist, so no container can start. Deploying the Worker is what " +
58
- "creates it — deleting it does not cause it to be rebuilt — so this " +
59
- "clears when an operator redeploys, and not before."
60
- };
61
- if (text.includes(NO_IMAGE))
62
- return {
63
- summary: "this Worker prepared no image for the workspace container",
64
- remedy: "the container application has no image under the name this workspace " +
65
- "starts, so no container can start. The workspace schedules its own " +
66
- 'containers, which takes `scheduling_policy: "durable_object"` and an ' +
67
- "`images` entry for that name in the Worker's containers block, and an " +
68
- "application created under another policy is deleted before that " +
69
- "deploy rather than switched. This clears when an operator redeploys " +
70
- "with both in place, and not before."
71
- };
72
- return undefined;
73
- }
@@ -1,110 +0,0 @@
1
- import type { Workspace } from "@cloudflare/computer";
2
- export interface DepsSnapshotRecord {
3
- /** The platform's snapshot id. */
4
- id: string;
5
- /** In bytes, as the platform reported it. */
6
- size: number;
7
- /** The image reference it was taken on — see the file comment. */
8
- image: string;
9
- /** Where the install ran. */
10
- dir: string;
11
- /** The install fingerprint its marker holds. */
12
- fingerprint: string;
13
- /** Every package root with a tree on the disk, and its inputs' digest then. */
14
- roots: {
15
- dir: string;
16
- fingerprint: string | null;
17
- }[];
18
- /** {@link packageRootSet} of the checkout then. */
19
- rootSet: string;
20
- at: number;
21
- }
22
- /** What a restore is checked against: the install a cold start would run. */
23
- export interface ExpectedTree {
24
- dir: string;
25
- fingerprint: string;
26
- }
27
- /** A tree an install finished, as its record names it — `at` tells two apart. */
28
- export interface InstalledTree extends ExpectedTree {
29
- at: number;
30
- }
31
- export interface DepsSnapshotDeps {
32
- storage: DurableObjectStorage;
33
- container: () => Container | undefined;
34
- /** The image a start from no snapshot would boot. */
35
- image: () => string | undefined;
36
- workspace: () => Workspace;
37
- rootFingerprint: (dir: string) => Promise<string | null>;
38
- /** The tree the install record says this container holds. */
39
- tree: () => Promise<{
40
- dir: string;
41
- fingerprint: string | null;
42
- at: number;
43
- } | undefined>;
44
- tag: () => string;
45
- id: () => string;
46
- }
47
- /**
48
- * Which package roots a checkout has, as one digest: every directory under
49
- * `dir` with a `package.json`, and the submodules `.gitmodules` declares — one a
50
- * bootstrap clones in the container has no `package.json` here until then.
51
- */
52
- export declare function packageRootSet(fs: Workspace["fs"], dir: string): Promise<string>;
53
- export declare class DepsSnapshot {
54
- #private;
55
- private readonly deps;
56
- readonly launch: {
57
- readonly containerSnapshot: ContainerSnapshotRestoreParams | undefined;
58
- };
59
- constructor(deps: DepsSnapshotDeps);
60
- /**
61
- * Restore with the next start if the record fits it, and say which record.
62
- *
63
- * `expected` is a thunk because it reads the checkout, and a workspace with no
64
- * snapshot or one from another image should not pay for that.
65
- */
66
- arm(expected: () => Promise<ExpectedTree | undefined>): Promise<DepsSnapshotRecord | undefined>;
67
- /** Forget an armed restore, taken or not. */
68
- disarm(): void;
69
- /**
70
- * Whether the container that came up holds `record`'s tree, judged by the
71
- * marker the setup read in it.
72
- *
73
- * Drops the record when a start took it and the tree is not there; leaves it
74
- * when no start did, since nothing was learned about it.
75
- */
76
- restored(record: DepsSnapshotRecord, marker: string | null | undefined): Promise<boolean>;
77
- /**
78
- * Snapshot the running container, which has just installed `tree`, after any
79
- * snapshot already in flight — a later install's request is never dropped for
80
- * an earlier one's.
81
- *
82
- * Never throws: a container that cannot be snapshotted installs again next
83
- * time, which is today's cost and no worse.
84
- */
85
- take(tree: InstalledTree): Promise<void>;
86
- /** Snapshots in flight, finished — for whatever is about to stop the container. */
87
- settled(): Promise<void>;
88
- /**
89
- * The entries that ask for `tree`'s snapshot, for the install to write with
90
- * its verdict — see the file comment, and {@link takeDue}.
91
- */
92
- dueEntries(tree: InstalledTree): Record<string, unknown>;
93
- /** Take the snapshot an install asked for, if one is waiting. */
94
- takeDue(): Promise<void>;
95
- /** The container went, and the disk an asked-for snapshot was of with it. */
96
- forgetDue(): Promise<void>;
97
- get(): Promise<DepsSnapshotRecord | undefined>;
98
- /**
99
- * Start from another workspace's snapshot — one taken for the same checkout
100
- * path on the same image, as a worktree shares with the checkout it was cut
101
- * from.
102
- *
103
- * Kept out when this workspace's own snapshot already fits, and refused when
104
- * the offered one does not.
105
- */
106
- seed(record: DepsSnapshotRecord, expected: () => Promise<ExpectedTree | undefined>): Promise<{
107
- seeded: boolean;
108
- reason?: string;
109
- }>;
110
- }
@@ -1,424 +0,0 @@
1
- var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
- if (value !== null && value !== void 0) {
3
- if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
- var dispose, inner;
5
- if (async) {
6
- if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
- dispose = value[Symbol.asyncDispose];
8
- }
9
- if (dispose === void 0) {
10
- if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
- dispose = value[Symbol.dispose];
12
- if (async) inner = dispose;
13
- }
14
- if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
- if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
- env.stack.push({ value: value, dispose: dispose, async: async });
17
- }
18
- else if (async) {
19
- env.stack.push({ async: true });
20
- }
21
- return value;
22
- };
23
- var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
- return function (env) {
25
- function fail(e) {
26
- env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
- env.hasError = true;
28
- }
29
- var r, s = 0;
30
- function next() {
31
- while (r = env.stack.pop()) {
32
- try {
33
- if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
- if (r.dispose) {
35
- var result = r.dispose.call(r.value);
36
- if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
- }
38
- else s |= 1;
39
- }
40
- catch (e) {
41
- fail(e);
42
- }
43
- }
44
- if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
- if (env.hasError) throw env.error;
46
- }
47
- return next();
48
- };
49
- })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
- var e = new Error(message);
51
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
- });
53
- import { DEPS_ROOTS_COMMAND, parseDepsRoots } from "./container-deps.js";
54
- /**
55
- * A snapshot of a container that has just installed, so the next container
56
- * starts from it instead of installing again.
57
- *
58
- * Dependency trees live on the container's disk (see `./container-deps.ts`), so
59
- * a new container reinstalls before its first command can use them — what that
60
- * costs is in `./install-job.ts`. A Containers snapshot is that disk: restored,
61
- * the trees are back. It holds nothing else worth having — the checkout is this
62
- * object's storage, mounted over FUSE, and a mount is not in a snapshot.
63
- *
64
- * ## When a snapshot is restored
65
- *
66
- * Only into a start that would otherwise install the same thing, which takes
67
- * all of:
68
- *
69
- * - **The image it was taken on.** A snapshot replaces the image rather than
70
- * layering on it, so one from before a deploy boots the old image's
71
- * `computerd`, which nothing downstream catches.
72
- * - **The same checkout and install fingerprint**, so the marker it carries is
73
- * the one an install would write now.
74
- * - **Every package root's inputs unchanged.** A bootstrap installs roots the
75
- * fingerprint does not cover — a submodule's lockfile — and a restore brings
76
- * all of them back at once.
77
- * - **The same set of package roots.** A root added since — a new submodule —
78
- * has no tree in the snapshot, and restoring it would skip the install that
79
- * gives it one: see {@link packageRootSet}.
80
- *
81
- * What came up is then checked, not assumed: the setup mounts the restored trees
82
- * and reads the marker, and only a match is adopted as installed. Anything else —
83
- * an expired snapshot, which the platform reports as a failed start, after which
84
- * the backend's restart takes the image — drops the record, and the install runs
85
- * and snapshots again.
86
- *
87
- * ## Taken of the tree it was asked for, and nothing since
88
- *
89
- * An install asks for its snapshot in the same write as its verdict, so a stop
90
- * that comes before the wake still sees the request. Snapshots run one at a
91
- * time, and one is recorded only if the install record still names the tree it
92
- * was taken of: a reinstall empties the tree before it rebuilds it, and a
93
- * snapshot taken across that is of neither.
94
- *
95
- * ## The record outlives the container, and the snapshot outlives the record
96
- *
97
- * Unlike the tree record, which goes with its container, this is what a later
98
- * container starts from. The platform keeps a snapshot for thirty days from its
99
- * last restore and offers no way to delete one, so a dropped record leaves its
100
- * snapshot to expire.
101
- */
102
- /** Where the record lives. */
103
- const RECORD_KEY = "deps:snapshot";
104
- /** The tree a finished install asked to have snapshotted, until it is. */
105
- const DUE_KEY = "deps:snapshot-due";
106
- /**
107
- * Which package roots a checkout has, as one digest: every directory under
108
- * `dir` with a `package.json`, and the submodules `.gitmodules` declares — one a
109
- * bootstrap clones in the container has no `package.json` here until then.
110
- */
111
- export async function packageRootSet(fs, dir) {
112
- const found = await fs
113
- .find(dir, "**/package.json", { exclude: ["**/.git", "**/node_modules"] })
114
- .catch((err) => {
115
- if (err?.code === "ENOENT")
116
- return [];
117
- throw err;
118
- });
119
- const gitmodules = await fs
120
- .readFile(`${dir}/.gitmodules`, "utf8")
121
- .catch(() => "");
122
- const input = [
123
- ...found.map((entry) => entry.path).sort(),
124
- "\0.gitmodules",
125
- gitmodules
126
- ].join("\n");
127
- const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(input));
128
- return [...new Uint8Array(digest)]
129
- .map((b) => b.toString(16).padStart(2, "0"))
130
- .join("");
131
- }
132
- /**
133
- * The launch options `ContainerBackend` spreads into every start, whose
134
- * `containerSnapshot` is read at that moment.
135
- *
136
- * A getter, because the backend keeps this object for its lifetime and builds
137
- * each start from it — the initial one and every restart after a failed health
138
- * check — and only the first of those may restore: a restart is the backend
139
- * recovering from a start that did not come up, which an expired snapshot is.
140
- */
141
- function launchOptions(take) {
142
- return {
143
- get containerSnapshot() {
144
- return take();
145
- }
146
- };
147
- }
148
- export class DepsSnapshot {
149
- deps;
150
- /** Restore with the next start, once. */
151
- #pending;
152
- /** Whether a start has taken {@link #pending} since it was armed. */
153
- #launched = false;
154
- #taking;
155
- launch = launchOptions(() => {
156
- const pending = this.#pending;
157
- if (!pending)
158
- return undefined;
159
- this.#pending = undefined;
160
- this.#launched = true;
161
- return pending;
162
- });
163
- constructor(deps) {
164
- this.deps = deps;
165
- }
166
- /**
167
- * Restore with the next start if the record fits it, and say which record.
168
- *
169
- * `expected` is a thunk because it reads the checkout, and a workspace with no
170
- * snapshot or one from another image should not pay for that.
171
- */
172
- async arm(expected) {
173
- this.disarm();
174
- const record = await this.get();
175
- if (!record)
176
- return undefined;
177
- const refused = await this.#refusal(record, expected);
178
- if (refused) {
179
- console.info(`[${this.deps.tag()}] not restoring the dependency snapshot`, {
180
- id: this.deps.id(),
181
- snapshot: record.id,
182
- reason: refused
183
- });
184
- return undefined;
185
- }
186
- this.#pending = { id: record.id };
187
- console.info(`[${this.deps.tag()}] restoring the dependency snapshot`, {
188
- id: this.deps.id(),
189
- snapshot: record.id,
190
- bytes: record.size,
191
- dir: record.dir
192
- });
193
- return record;
194
- }
195
- /** Forget an armed restore, taken or not. */
196
- disarm() {
197
- this.#pending = undefined;
198
- this.#launched = false;
199
- }
200
- /**
201
- * Whether the container that came up holds `record`'s tree, judged by the
202
- * marker the setup read in it.
203
- *
204
- * Drops the record when a start took it and the tree is not there; leaves it
205
- * when no start did, since nothing was learned about it.
206
- */
207
- async restored(record, marker) {
208
- if (!this.#launched)
209
- return false;
210
- if (marker === record.fingerprint)
211
- return true;
212
- await this.#drop(record, marker === undefined
213
- ? "the restored container could not be read"
214
- : "the restored container does not hold the tree");
215
- return false;
216
- }
217
- /**
218
- * Snapshot the running container, which has just installed `tree`, after any
219
- * snapshot already in flight — a later install's request is never dropped for
220
- * an earlier one's.
221
- *
222
- * Never throws: a container that cannot be snapshotted installs again next
223
- * time, which is today's cost and no worse.
224
- */
225
- async take(tree) {
226
- const turn = (this.#taking ?? Promise.resolve()).then(() => this.#take(tree));
227
- this.#taking = turn;
228
- try {
229
- await turn;
230
- }
231
- finally {
232
- if (this.#taking === turn)
233
- this.#taking = undefined;
234
- }
235
- }
236
- /** Snapshots in flight, finished — for whatever is about to stop the container. */
237
- async settled() {
238
- await this.#taking;
239
- }
240
- /**
241
- * The entries that ask for `tree`'s snapshot, for the install to write with
242
- * its verdict — see the file comment, and {@link takeDue}.
243
- */
244
- dueEntries(tree) {
245
- return { [DUE_KEY]: tree };
246
- }
247
- /** Take the snapshot an install asked for, if one is waiting. */
248
- async takeDue() {
249
- const due = await this.deps.storage.get(DUE_KEY);
250
- if (!due)
251
- return;
252
- await this.deps.storage.delete(DUE_KEY);
253
- await this.take(due);
254
- }
255
- /** The container went, and the disk an asked-for snapshot was of with it. */
256
- async forgetDue() {
257
- await this.deps.storage.delete(DUE_KEY);
258
- }
259
- async get() {
260
- return await this.deps.storage.get(RECORD_KEY);
261
- }
262
- /**
263
- * Start from another workspace's snapshot — one taken for the same checkout
264
- * path on the same image, as a worktree shares with the checkout it was cut
265
- * from.
266
- *
267
- * Kept out when this workspace's own snapshot already fits, and refused when
268
- * the offered one does not.
269
- */
270
- async seed(record, expected) {
271
- if (!isRecord(record))
272
- return { seeded: false, reason: "not a snapshot" };
273
- const now = await expected();
274
- const fits = async () => now;
275
- const own = await this.get();
276
- if (own && !(await this.#refusal(own, fits)))
277
- return { seeded: false, reason: "this workspace's own snapshot fits" };
278
- const refused = await this.#refusal(record, fits);
279
- if (refused)
280
- return { seeded: false, reason: refused };
281
- await this.deps.storage.put(RECORD_KEY, record);
282
- console.info(`[${this.deps.tag()}] seeded a dependency snapshot`, {
283
- id: this.deps.id(),
284
- snapshot: record.id,
285
- dir: record.dir
286
- });
287
- return { seeded: true };
288
- }
289
- /**
290
- * Why `record` must not be restored now, or `undefined` if it may. A check
291
- * that throws refuses: a start must not fail for a snapshot it can go without.
292
- */
293
- async #refusal(record, expected) {
294
- if (record.image !== this.deps.image())
295
- return "taken on another image";
296
- try {
297
- const tree = await expected();
298
- if (!tree)
299
- return "there is nothing to install here";
300
- if (record.dir !== tree.dir)
301
- return "taken for another checkout";
302
- if (record.fingerprint !== tree.fingerprint)
303
- return "the install's inputs have changed";
304
- for (const root of record.roots) {
305
- if ((await this.deps.rootFingerprint(root.dir)) !== root.fingerprint)
306
- return `${root.dir} has changed`;
307
- }
308
- if ((await packageRootSet(this.deps.workspace().fs, tree.dir)) !==
309
- record.rootSet)
310
- return "the checkout's package roots have changed";
311
- return undefined;
312
- }
313
- catch (err) {
314
- return `it could not be checked: ${String(err)}`;
315
- }
316
- }
317
- /** Whether the install record still names `tree`. */
318
- async #holds(tree) {
319
- const now = await this.deps.tree();
320
- return (now?.dir === tree.dir &&
321
- now.fingerprint === tree.fingerprint &&
322
- now.at === tree.at);
323
- }
324
- async #drop(record, reason) {
325
- // Only this record: a later install may have put a newer one there.
326
- if ((await this.get())?.id === record.id)
327
- await this.deps.storage.delete(RECORD_KEY);
328
- console.warn(`[${this.deps.tag()}] dropped the dependency snapshot`, {
329
- id: this.deps.id(),
330
- snapshot: record.id,
331
- reason
332
- });
333
- }
334
- async #take(tree) {
335
- const container = this.deps.container();
336
- const image = this.deps.image();
337
- if (!container?.running || image === undefined)
338
- return;
339
- // Replaced, or reinstalling, since the install asked.
340
- if (!(await this.#holds(tree)))
341
- return;
342
- const startedAt = Date.now();
343
- try {
344
- const roots = await this.#roots();
345
- // Without a root to mount, a restore would read no marker and be dropped.
346
- if (roots.length === 0) {
347
- console.warn(`[${this.deps.tag()}] no dependency tree to snapshot on this disk`, { id: this.deps.id(), dir: tree.dir });
348
- return;
349
- }
350
- const fingerprinted = [];
351
- for (const dir of roots)
352
- fingerprinted.push({
353
- dir,
354
- fingerprint: await this.deps.rootFingerprint(dir)
355
- });
356
- const rootSet = await packageRootSet(this.deps.workspace().fs, tree.dir);
357
- const snapshot = await container.snapshotContainer({
358
- name: `deps-${tree.fingerprint.slice(0, 16)}`
359
- });
360
- if (!(await this.#holds(tree))) {
361
- console.warn(`[${this.deps.tag()}] discarded a dependency snapshot taken while the tree changed`, { id: this.deps.id(), snapshot: snapshot.id, dir: tree.dir });
362
- return;
363
- }
364
- await this.deps.storage.put(RECORD_KEY, {
365
- id: snapshot.id,
366
- size: snapshot.size,
367
- image,
368
- dir: tree.dir,
369
- fingerprint: tree.fingerprint,
370
- roots: fingerprinted,
371
- rootSet,
372
- at: Date.now()
373
- });
374
- console.info(`[${this.deps.tag()}] took a dependency snapshot`, {
375
- id: this.deps.id(),
376
- snapshot: snapshot.id,
377
- bytes: snapshot.size,
378
- roots,
379
- ms: Date.now() - startedAt
380
- });
381
- }
382
- catch (err) {
383
- console.warn(`[${this.deps.tag()}] could not take a dependency snapshot`, {
384
- id: this.deps.id(),
385
- err: String(err)
386
- });
387
- }
388
- }
389
- /** The package roots whose trees are on this container's disk. */
390
- async #roots() {
391
- const env_1 = { stack: [], error: void 0, hasError: false };
392
- try {
393
- const handle = __addDisposableResource(env_1, await this.deps
394
- .workspace()
395
- .runtime.exec(DEPS_ROOTS_COMMAND, {
396
- cwd: "/",
397
- encoding: "utf8",
398
- timeoutMs: 30_000
399
- }), false);
400
- const result = await handle.result();
401
- if (result.exitCode !== 0)
402
- throw new Error(`could not list the dependency trees: ${result.stderr || `exit ${result.exitCode}`}`);
403
- return parseDepsRoots(result.stdout ?? "");
404
- }
405
- catch (e_1) {
406
- env_1.error = e_1;
407
- env_1.hasError = true;
408
- }
409
- finally {
410
- __disposeResources(env_1);
411
- }
412
- }
413
- }
414
- /** Enough of a record's shape to restore from — it arrives over RPC. */
415
- function isRecord(value) {
416
- const r = value;
417
- return (typeof r?.id === "string" &&
418
- typeof r.image === "string" &&
419
- typeof r.dir === "string" &&
420
- typeof r.fingerprint === "string" &&
421
- typeof r.rootSet === "string" &&
422
- Array.isArray(r.roots) &&
423
- r.roots.every((root) => typeof root?.dir === "string"));
424
- }