@anhlp/alp-daemon 0.0.1-beta.4 → 0.0.1-beta.5
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/THIRD-PARTY-NOTICES.txt +26 -1
- package/dist/alpd.js +11047 -3021
- package/dist/attachments.d.ts +25 -49
- package/dist/attachments.js +45 -8
- package/dist/build-manifest.json +14 -1
- package/dist/conversation.js +18 -2
- package/dist/disk.d.ts +67 -0
- package/dist/disk.js +152 -0
- package/dist/files.d.ts +141 -2
- package/dist/files.js +377 -68
- package/dist/git.d.ts +8 -2
- package/dist/git.js +111 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/main.js +37 -11
- package/dist/native-terminal.d.ts +45 -0
- package/dist/native-terminal.js +198 -0
- package/dist/operations.d.ts +8 -2
- package/dist/operations.js +20 -10
- package/dist/registry.js +1 -1
- package/dist/scheduler.js +8 -2
- package/dist/schedules.d.ts +2 -1
- package/dist/schedules.js +6 -4
- package/dist/server.d.ts +4 -2
- package/dist/server.js +166 -42
- package/dist/store.d.ts +3 -1
- package/dist/store.js +4 -3
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal-screen.d.ts +24 -0
- package/dist/terminal-screen.js +99 -0
- package/dist/terminals.d.ts +43 -7
- package/dist/terminals.js +222 -139
- package/dist/windows-terminal.d.ts +29 -0
- package/dist/windows-terminal.js +122 -0
- package/dist/worktrees.d.ts +25 -1
- package/dist/worktrees.js +74 -2
- package/native/README.md +201 -0
- package/native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/native/prebuilt/darwin-x64/manifest.json +11 -0
- package/native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/native/terminal-owner.c +389 -0
- package/package.json +11 -7
- package/src/attachments.ts +46 -8
- package/src/conversation.ts +22 -2
- package/src/disk.ts +175 -0
- package/src/files.ts +505 -80
- package/src/git.ts +113 -18
- package/src/index.ts +1 -0
- package/src/main.ts +30 -11
- package/src/native-terminal.ts +122 -0
- package/src/operations.ts +27 -11
- package/src/registry.ts +1 -1
- package/src/scheduler.ts +13 -3
- package/src/schedules.ts +5 -2
- package/src/server.ts +157 -35
- package/src/store.ts +2 -1
- package/src/terminal-screen.ts +74 -0
- package/src/terminals.ts +205 -155
- package/src/windows-terminal.ts +123 -0
- package/src/worktrees.ts +97 -2
package/dist/attachments.d.ts
CHANGED
|
@@ -97,20 +97,15 @@ export type AttachmentStoreOptions = {
|
|
|
97
97
|
};
|
|
98
98
|
export type AttachmentStore = ReturnType<typeof createAttachmentStore>;
|
|
99
99
|
export declare function createAttachmentStore(options: AttachmentStoreOptions): {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/** Settles when start-up recovery is done (or failed: the methods then answer 1005). */
|
|
103
|
-
ready: () => Promise<void>;
|
|
104
|
-
/** One collection now, after start-up recovery (as the timer's). */
|
|
105
|
-
sweep(): Promise<void>;
|
|
106
|
-
begin(principal: Principal, params: {
|
|
100
|
+
sweep: () => Promise<void>;
|
|
101
|
+
begin: (principal: Principal, params: {
|
|
107
102
|
sessionId: string;
|
|
108
103
|
name: string;
|
|
109
104
|
size: number;
|
|
110
105
|
mimeType: string;
|
|
111
106
|
sha256?: string;
|
|
112
107
|
requestId: string;
|
|
113
|
-
})
|
|
108
|
+
}) => Promise<{
|
|
114
109
|
expiresAt: string;
|
|
115
110
|
uploadId: string;
|
|
116
111
|
state: "uploading" | "committed";
|
|
@@ -125,70 +120,51 @@ export declare function createAttachmentStore(options: AttachmentStoreOptions):
|
|
|
125
120
|
size: number;
|
|
126
121
|
maxChunkBytes: number;
|
|
127
122
|
}>;
|
|
128
|
-
chunk(principal: Principal, params: {
|
|
123
|
+
chunk: (principal: Principal, params: {
|
|
129
124
|
uploadId: string;
|
|
130
125
|
offset: number;
|
|
131
126
|
data: string;
|
|
132
|
-
})
|
|
127
|
+
}) => Promise<{
|
|
133
128
|
received: number;
|
|
134
129
|
size: number;
|
|
135
130
|
}>;
|
|
136
|
-
commit(principal: Principal, params: {
|
|
131
|
+
commit: (principal: Principal, params: {
|
|
137
132
|
uploadId: string;
|
|
138
133
|
sha256: string;
|
|
139
|
-
})
|
|
134
|
+
}) => Promise<{
|
|
140
135
|
attachment: AttachmentInfo;
|
|
141
136
|
}>;
|
|
142
|
-
abort(principal: Principal, params: {
|
|
137
|
+
abort: (principal: Principal, params: {
|
|
143
138
|
uploadId: string;
|
|
144
|
-
})
|
|
139
|
+
}) => Promise<{
|
|
145
140
|
aborted: true;
|
|
146
141
|
}>;
|
|
147
|
-
get(principal: Principal, params: {
|
|
142
|
+
get: (principal: Principal, params: {
|
|
148
143
|
attachmentId: string;
|
|
149
|
-
})
|
|
144
|
+
}) => Promise<{
|
|
150
145
|
attachment: AttachmentInfo;
|
|
151
146
|
}>;
|
|
152
|
-
|
|
153
|
-
remove(principal: Principal, params: {
|
|
147
|
+
remove: (principal: Principal, params: {
|
|
154
148
|
attachmentId: string;
|
|
155
|
-
})
|
|
149
|
+
}) => Promise<{
|
|
156
150
|
deleted: true;
|
|
157
151
|
}>;
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Pins `attachmentIds` for `holder` durably (a prompt's receipt key; t-15b0.4's fork adds the
|
|
166
|
-
* new session's holder the same way after re-authorizing the source's attachments for it). Each
|
|
167
|
-
* must still be committed; a write failure pins nothing.
|
|
168
|
-
*/
|
|
169
|
-
pin(holder: string, sessionId: string, attachmentIds: string[], kind?: string): Promise<void>;
|
|
170
|
-
/** The attachments these holders pin, as ids (a fork's kept prompts, §12.7); unknown holders hold none. */
|
|
171
|
-
heldBy(holders: string[]): Promise<string[]>;
|
|
172
|
-
/**
|
|
173
|
-
* Re-authorizes attachments for `principal` before another session pins them (a fork, §12.7): each
|
|
174
|
-
* must be committed and bound to this principal, its grant epoch and an authorized project, as every
|
|
175
|
-
* other call checks; otherwise refused as an unknown one.
|
|
176
|
-
*/
|
|
177
|
-
authorize(principal: Principal, attachmentIds: string[], forks?: string[]): Promise<void>;
|
|
178
|
-
/** Drops `holder`'s references (a prompt refused before it reached the runtime). */
|
|
179
|
-
release(holder: string): Promise<void>;
|
|
180
|
-
/**
|
|
181
|
-
* Releases every reference of these sessions: the seam for deleting a session's history (today
|
|
182
|
-
* only retention removes a tree; archive keeps it). Released objects stay `releasedGraceMs`.
|
|
183
|
-
*/
|
|
184
|
-
releaseSessions(sessionIds: string[]): Promise<void>;
|
|
185
|
-
/** The runtime's reader (AttachmentReader). */
|
|
152
|
+
resolve: (principal: Principal, sessionId: string, attachmentIds: unknown[]) => Promise<RuntimeAttachment[]>;
|
|
153
|
+
pin: (holder: string, sessionId: string, attachmentIds: string[], kind?: string | undefined) => Promise<void>;
|
|
154
|
+
heldBy: (holders: string[]) => Promise<string[]>;
|
|
155
|
+
authorize: (principal: Principal, attachmentIds: string[], forks?: string[] | undefined) => Promise<void>;
|
|
156
|
+
release: (holder: string) => Promise<void>;
|
|
157
|
+
releaseSessions: (sessionIds: string[]) => Promise<void>;
|
|
186
158
|
reader: {
|
|
187
|
-
open(attachmentId: string, sessionId: string)
|
|
159
|
+
open: (attachmentId: string, sessionId: string) => Promise<{
|
|
188
160
|
stream: Readable;
|
|
189
161
|
attachment: RuntimeAttachment;
|
|
190
162
|
}>;
|
|
191
163
|
};
|
|
164
|
+
/** Effective limits, advertised in daemon.hello. */
|
|
165
|
+
limits: AttachmentLimits;
|
|
166
|
+
/** Settles when start-up recovery is done (or failed: the methods then answer 1005). */
|
|
167
|
+
ready: () => Promise<void>;
|
|
192
168
|
/** Usage, for tests and status: bytes reserved and uploads in progress, of one principal or all. */
|
|
193
169
|
usage: (principalId?: string) => {
|
|
194
170
|
bytes: number;
|
|
@@ -196,7 +172,7 @@ export declare function createAttachmentStore(options: AttachmentStoreOptions):
|
|
|
196
172
|
};
|
|
197
173
|
/** The holders of the reference index, for tests. */
|
|
198
174
|
holders: () => Record<string, Holder>;
|
|
199
|
-
close(): void
|
|
175
|
+
close(): Promise<void>;
|
|
200
176
|
};
|
|
201
177
|
/**
|
|
202
178
|
* The operator's attachment limits: `<ALP_HOME>/attachments.json`, `{ "limits": { "<name>": n } }`
|
package/dist/attachments.js
CHANGED
|
@@ -75,6 +75,19 @@ function createAttachmentStore(options) {
|
|
|
75
75
|
let refs = { version: 1, holders: {}, released: {} };
|
|
76
76
|
const beginning = /* @__PURE__ */ new Map();
|
|
77
77
|
const queues = /* @__PURE__ */ new Map();
|
|
78
|
+
const pending = /* @__PURE__ */ new Set();
|
|
79
|
+
function admitted(run) {
|
|
80
|
+
return async (...args) => {
|
|
81
|
+
usable();
|
|
82
|
+
const operation = run(...args);
|
|
83
|
+
pending.add(operation);
|
|
84
|
+
try {
|
|
85
|
+
return await operation;
|
|
86
|
+
} finally {
|
|
87
|
+
pending.delete(operation);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
78
91
|
let failure;
|
|
79
92
|
let sweeper;
|
|
80
93
|
const unreadable = /* @__PURE__ */ new Set();
|
|
@@ -189,6 +202,7 @@ function createAttachmentStore(options) {
|
|
|
189
202
|
return await projectOfSession(meta.sessionId) === meta.project;
|
|
190
203
|
}
|
|
191
204
|
function usable() {
|
|
205
|
+
if (closed) throw unavailable("attachment_unavailable", "alpd attachment store is closed");
|
|
192
206
|
if (failure) throw unavailable("attachment_unavailable", "alpd cannot use its attachment store; its log says why");
|
|
193
207
|
}
|
|
194
208
|
async function discard(meta, why) {
|
|
@@ -218,7 +232,7 @@ function createAttachmentStore(options) {
|
|
|
218
232
|
refs = next;
|
|
219
233
|
}
|
|
220
234
|
async function sweep() {
|
|
221
|
-
if (failure) return;
|
|
235
|
+
if (failure || closed) return;
|
|
222
236
|
await withRefs(async () => {
|
|
223
237
|
const at = now();
|
|
224
238
|
const released = { ...refs.released };
|
|
@@ -328,7 +342,7 @@ function createAttachmentStore(options) {
|
|
|
328
342
|
if (options.sweepMs !== 0) {
|
|
329
343
|
void recovered.then(() => {
|
|
330
344
|
if (closed || failure) return;
|
|
331
|
-
sweeper = setInterval(() => void sweep().catch((error) => log?.warn("attachment collection failed", { code: "ALPD_ATTACHMENT_SWEEP_FAILED", error })), options.sweepMs ?? Math.min(15 * 60 * 1e3, Math.max(1e3, Math.floor(limits.uploadTimeoutMs / 4))));
|
|
345
|
+
sweeper = setInterval(() => void admitted(sweep)().catch((error) => log?.warn("attachment collection failed", { code: "ALPD_ATTACHMENT_SWEEP_FAILED", error })), options.sweepMs ?? Math.min(15 * 60 * 1e3, Math.max(1e3, Math.floor(limits.uploadTimeoutMs / 4))));
|
|
332
346
|
sweeper.unref();
|
|
333
347
|
});
|
|
334
348
|
}
|
|
@@ -354,7 +368,7 @@ function createAttachmentStore(options) {
|
|
|
354
368
|
done += bytesWritten;
|
|
355
369
|
}
|
|
356
370
|
}
|
|
357
|
-
|
|
371
|
+
const api = {
|
|
358
372
|
/** Effective limits, advertised in daemon.hello. */
|
|
359
373
|
limits,
|
|
360
374
|
/** Settles when start-up recovery is done (or failed: the methods then answer 1005). */
|
|
@@ -636,7 +650,8 @@ function createAttachmentStore(options) {
|
|
|
636
650
|
/**
|
|
637
651
|
* Pins `attachmentIds` for `holder` durably (a prompt's receipt key; t-15b0.4's fork adds the
|
|
638
652
|
* new session's holder the same way after re-authorizing the source's attachments for it). Each
|
|
639
|
-
* must still be committed; a write failure pins nothing.
|
|
653
|
+
* must still be committed; a write failure pins nothing. If interrupted by crash before the receipt
|
|
654
|
+
* is written, the pin is held conservatively until session cleanup (releaseSessions).
|
|
640
655
|
*/
|
|
641
656
|
async pin(holder, sessionId, attachmentIds, kind = "prompt") {
|
|
642
657
|
await recovered;
|
|
@@ -734,7 +749,7 @@ function createAttachmentStore(options) {
|
|
|
734
749
|
const { handle, info: stat } = await openPayload(meta, OPEN_READ);
|
|
735
750
|
if (stat.size !== meta.size) {
|
|
736
751
|
await handle.close();
|
|
737
|
-
throw unavailable("attachment_unavailable", "The attachment's bytes changed");
|
|
752
|
+
throw unavailable("attachment_unavailable", "The attachment's bytes changed", { attachmentId: `att_${meta.id}` });
|
|
738
753
|
}
|
|
739
754
|
const hash = createHash("sha256");
|
|
740
755
|
const stream = handle.createReadStream({ start: 0, ...meta.size ? { end: meta.size - 1 } : {}, autoClose: true });
|
|
@@ -755,12 +770,34 @@ function createAttachmentStore(options) {
|
|
|
755
770
|
usage,
|
|
756
771
|
/** The holders of the reference index, for tests. */
|
|
757
772
|
holders: () => structuredClone(refs.holders),
|
|
758
|
-
close() {
|
|
773
|
+
async close() {
|
|
759
774
|
closed = true;
|
|
760
|
-
if (sweeper)
|
|
761
|
-
|
|
775
|
+
if (sweeper) {
|
|
776
|
+
clearInterval(sweeper);
|
|
777
|
+
sweeper = void 0;
|
|
778
|
+
}
|
|
779
|
+
await recovered;
|
|
780
|
+
await Promise.allSettled([...pending]);
|
|
781
|
+
await Promise.allSettled([...queues.values()]);
|
|
762
782
|
}
|
|
763
783
|
};
|
|
784
|
+
return {
|
|
785
|
+
...api,
|
|
786
|
+
sweep: admitted(api.sweep),
|
|
787
|
+
begin: admitted(api.begin),
|
|
788
|
+
chunk: admitted(api.chunk),
|
|
789
|
+
commit: admitted(api.commit),
|
|
790
|
+
abort: admitted(api.abort),
|
|
791
|
+
get: admitted(api.get),
|
|
792
|
+
remove: admitted(api.remove),
|
|
793
|
+
resolve: admitted(api.resolve),
|
|
794
|
+
pin: admitted(api.pin),
|
|
795
|
+
heldBy: admitted(api.heldBy),
|
|
796
|
+
authorize: admitted(api.authorize),
|
|
797
|
+
release: admitted(api.release),
|
|
798
|
+
releaseSessions: admitted(api.releaseSessions),
|
|
799
|
+
reader: { ...api.reader, open: admitted(api.reader.open) }
|
|
800
|
+
};
|
|
764
801
|
}
|
|
765
802
|
var LIMIT_KEYS = ["maxObjectBytes", "maxPerPrompt", "maxPromptBytes", "maxChunkBytes", "maxActiveUploads", "maxPrincipalBytes", "maxDaemonBytes", "uploadTimeoutMs", "unreferencedRetentionMs", "releasedGraceMs"];
|
|
766
803
|
async function readAttachmentConfig(home) {
|
package/dist/build-manifest.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.1-beta.
|
|
2
|
+
"version": "0.0.1-beta.5",
|
|
3
3
|
"entries": {
|
|
4
4
|
"packages/daemon/dist/alpd.js": {
|
|
5
5
|
"inputs": [
|
|
@@ -12,15 +12,18 @@
|
|
|
12
12
|
"packages/client/src/rpc/handshake.js",
|
|
13
13
|
"packages/client/src/rpc/reconnect.js",
|
|
14
14
|
"packages/client/src/rpc/registry.js",
|
|
15
|
+
"packages/client/src/rpc/terminal-directory.js",
|
|
15
16
|
"packages/client/src/rpc/timers.js",
|
|
16
17
|
"packages/client/src/websocket.js",
|
|
17
18
|
"packages/core/src/adapter.js",
|
|
18
19
|
"packages/core/src/atomic.js",
|
|
19
20
|
"packages/core/src/backup.js",
|
|
20
21
|
"packages/core/src/beads.js",
|
|
22
|
+
"packages/core/src/bridges.js",
|
|
21
23
|
"packages/core/src/delegation.js",
|
|
22
24
|
"packages/core/src/errors.js",
|
|
23
25
|
"packages/core/src/formulas.js",
|
|
26
|
+
"packages/core/src/gate-ledger.js",
|
|
24
27
|
"packages/core/src/hook-run.js",
|
|
25
28
|
"packages/core/src/hooks.js",
|
|
26
29
|
"packages/core/src/index.js",
|
|
@@ -30,6 +33,7 @@
|
|
|
30
33
|
"packages/core/src/log.js",
|
|
31
34
|
"packages/core/src/migrations.js",
|
|
32
35
|
"packages/core/src/models.js",
|
|
36
|
+
"packages/core/src/packaged.js",
|
|
33
37
|
"packages/core/src/permissions.js",
|
|
34
38
|
"packages/core/src/profiles.js",
|
|
35
39
|
"packages/core/src/promptsafe.js",
|
|
@@ -51,6 +55,7 @@
|
|
|
51
55
|
"packages/core/src/workflow.js",
|
|
52
56
|
"packages/daemon/src/attachments.ts",
|
|
53
57
|
"packages/daemon/src/conversation.ts",
|
|
58
|
+
"packages/daemon/src/disk.ts",
|
|
54
59
|
"packages/daemon/src/files.ts",
|
|
55
60
|
"packages/daemon/src/git.ts",
|
|
56
61
|
"packages/daemon/src/gitignore.ts",
|
|
@@ -58,6 +63,7 @@
|
|
|
58
63
|
"packages/daemon/src/lifecycle.ts",
|
|
59
64
|
"packages/daemon/src/lock.ts",
|
|
60
65
|
"packages/daemon/src/main.ts",
|
|
66
|
+
"packages/daemon/src/native-terminal.ts",
|
|
61
67
|
"packages/daemon/src/operations.ts",
|
|
62
68
|
"packages/daemon/src/registry.ts",
|
|
63
69
|
"packages/daemon/src/rpc-error.ts",
|
|
@@ -65,7 +71,9 @@
|
|
|
65
71
|
"packages/daemon/src/schedules.ts",
|
|
66
72
|
"packages/daemon/src/server.ts",
|
|
67
73
|
"packages/daemon/src/store.ts",
|
|
74
|
+
"packages/daemon/src/terminal-screen.ts",
|
|
68
75
|
"packages/daemon/src/terminals.ts",
|
|
76
|
+
"packages/daemon/src/windows-terminal.ts",
|
|
69
77
|
"packages/daemon/src/worktrees.ts",
|
|
70
78
|
"packages/protocol/src/contract-v1.js",
|
|
71
79
|
"packages/protocol/src/contract.js",
|
|
@@ -80,16 +88,20 @@
|
|
|
80
88
|
"packages/runtime/src/board.ts",
|
|
81
89
|
"packages/runtime/src/catalog.ts",
|
|
82
90
|
"packages/runtime/src/claude-transport.ts",
|
|
91
|
+
"packages/runtime/src/compact.ts",
|
|
83
92
|
"packages/runtime/src/configure.ts",
|
|
84
93
|
"packages/runtime/src/contracts.ts",
|
|
85
94
|
"packages/runtime/src/conversation.ts",
|
|
86
95
|
"packages/runtime/src/github.ts",
|
|
87
96
|
"packages/runtime/src/handoff.ts",
|
|
88
97
|
"packages/runtime/src/index.ts",
|
|
98
|
+
"packages/runtime/src/input-limits.ts",
|
|
89
99
|
"packages/runtime/src/language.ts",
|
|
90
100
|
"packages/runtime/src/live.ts",
|
|
91
101
|
"packages/runtime/src/mailbox.ts",
|
|
92
102
|
"packages/runtime/src/mcp-tools.ts",
|
|
103
|
+
"packages/runtime/src/model-catalog.ts",
|
|
104
|
+
"packages/runtime/src/model-probes.ts",
|
|
93
105
|
"packages/runtime/src/permission-path.ts",
|
|
94
106
|
"packages/runtime/src/process-info.ts",
|
|
95
107
|
"packages/runtime/src/providers.ts",
|
|
@@ -122,6 +134,7 @@
|
|
|
122
134
|
"node:stream",
|
|
123
135
|
"node:stream/promises",
|
|
124
136
|
"node:string_decoder",
|
|
137
|
+
"node:timers/promises",
|
|
125
138
|
"node:url",
|
|
126
139
|
"node:zlib",
|
|
127
140
|
"path"
|
package/dist/conversation.js
CHANGED
|
@@ -59,6 +59,7 @@ function configureRefusal(error) {
|
|
|
59
59
|
function createConversation(ctx) {
|
|
60
60
|
const { runtime, store, journal, records } = ctx;
|
|
61
61
|
const lifecycle = createLifecycle(journal, { crashPoint: ctx.crashPoint, completed });
|
|
62
|
+
const reservedForkIds = /* @__PURE__ */ new Set();
|
|
62
63
|
async function completed(entry) {
|
|
63
64
|
if (entry.state !== "applied") return void 0;
|
|
64
65
|
const saved = (await store.get(entry.resource))?.lifecycleCommits?.[entry.key];
|
|
@@ -411,6 +412,7 @@ function createConversation(ctx) {
|
|
|
411
412
|
const fingerprint = ctx.digest({ boundary: boundary ?? null, branchId: branchId ?? null, workspaceId: workspaceId ?? null, newSessionId: newSessionId ?? null, title: title ?? null });
|
|
412
413
|
let release;
|
|
413
414
|
let releaseWakes;
|
|
415
|
+
let reservedId;
|
|
414
416
|
try {
|
|
415
417
|
const { result, duplicate } = await lifecycle.run(
|
|
416
418
|
{ resource: sessionId, operation: "fork", requestId, fingerprint },
|
|
@@ -459,8 +461,17 @@ function createConversation(ctx) {
|
|
|
459
461
|
}
|
|
460
462
|
const latestTurn = turns.at(-1)?.id;
|
|
461
463
|
const cutoff = kind === "through" ? { throughTurnId: turnId } : kind === "before" ? { beforeTurnId: turnId } : latestTurn ? { throughTurnId: latestTurn } : void 0;
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
+
const pendingIds = new Set((await journal.list()).filter((entry) => entry.operation === "fork" && ["prepared", "applied", "unknown"].includes(entry.state)).map((entry) => entry.plan.newSessionId));
|
|
465
|
+
let id = newSessionId;
|
|
466
|
+
if (id !== void 0) {
|
|
467
|
+
if (records.has(id) || runtime.snapshot(id) || reservedForkIds.has(id) || pendingIds.has(id)) throw new RpcError(ERROR.conflict, `Session ${id} already exists`, { reason: "request_conflict" });
|
|
468
|
+
} else {
|
|
469
|
+
do {
|
|
470
|
+
id = `ses_${randomBytes(8).toString("hex")}`;
|
|
471
|
+
} while (records.has(id) || runtime.snapshot(id) || reservedForkIds.has(id) || pendingIds.has(id));
|
|
472
|
+
}
|
|
473
|
+
reservedForkIds.add(id);
|
|
474
|
+
reservedId = id;
|
|
464
475
|
const cwd = await destinationOf(record, workspaceId);
|
|
465
476
|
const attachmentIds = await reauthorize(sessionId, log, cut, principal);
|
|
466
477
|
if (latest && begun(sessionId)) throw busy(sessionId, "a turn began meanwhile");
|
|
@@ -498,6 +509,10 @@ function createConversation(ctx) {
|
|
|
498
509
|
);
|
|
499
510
|
return { ...result, ...duplicate ? { duplicate: true } : {} };
|
|
500
511
|
} finally {
|
|
512
|
+
if (reservedId && !records.has(reservedId)) {
|
|
513
|
+
const entry = await journal.get(sessionId, "fork", requestId).catch(() => ({ state: "unknown" }));
|
|
514
|
+
if (!entry || entry.state === "failed") reservedForkIds.delete(reservedId);
|
|
515
|
+
}
|
|
501
516
|
releaseWakes?.();
|
|
502
517
|
release?.();
|
|
503
518
|
}
|
|
@@ -595,6 +610,7 @@ function createConversation(ctx) {
|
|
|
595
610
|
await store.put(record);
|
|
596
611
|
records.set(record.id, record);
|
|
597
612
|
}
|
|
613
|
+
reservedForkIds.delete(plan.newSessionId);
|
|
598
614
|
await place(plan, entry.key);
|
|
599
615
|
if (plan.attachmentIds.length) await ctx.attachments?.pin(`fork:${plan.newSessionId}`, plan.newSessionId, plan.attachmentIds, "fork");
|
|
600
616
|
const result = { session: ctx.summary(plan.newSessionId), forkedFrom };
|
package/dist/disk.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** Whether `error` (or the error it wraps) says the disk cannot take writes. */
|
|
2
|
+
export declare function isDiskError(error: unknown): boolean;
|
|
3
|
+
export declare const GiB: number;
|
|
4
|
+
export type DiskRuntime = {
|
|
5
|
+
holdForDisk(reason: string): unknown;
|
|
6
|
+
releaseDisk(by: string): unknown;
|
|
7
|
+
diskHeld(): boolean;
|
|
8
|
+
diskLow(free: string): void;
|
|
9
|
+
};
|
|
10
|
+
export type DiskGuardOptions = {
|
|
11
|
+
/** The directory whose disk alpd writes: ALP_HOME. */
|
|
12
|
+
home: string;
|
|
13
|
+
runtime: DiskRuntime;
|
|
14
|
+
log: {
|
|
15
|
+
warn(message: string, fields?: Record<string, unknown>): void;
|
|
16
|
+
info(message: string, fields?: Record<string, unknown>): void;
|
|
17
|
+
};
|
|
18
|
+
/** How often space is checked; ALP checks every minute. */
|
|
19
|
+
intervalMs?: number;
|
|
20
|
+
/** Below this, alpd warns once that space runs low (2 GiB). */
|
|
21
|
+
lowBytes?: number;
|
|
22
|
+
/** Below this, alpd holds work before writes fail (100 MiB). */
|
|
23
|
+
criticalBytes?: number;
|
|
24
|
+
/** Free space alpd needs to resume work it held (256 MiB); above criticalBytes, so it does not flap. */
|
|
25
|
+
resumeBytes?: number;
|
|
26
|
+
/** How long one statfs or probe may take (10 s). */
|
|
27
|
+
checkTimeoutMs?: number;
|
|
28
|
+
/** Free bytes on the disk of a directory; undefined when unknown. */
|
|
29
|
+
free?: (directory: string) => Promise<number | undefined>;
|
|
30
|
+
/** Writes and removes a small file; rejects when the disk takes no writes. */
|
|
31
|
+
probe?: (directory: string) => Promise<void>;
|
|
32
|
+
};
|
|
33
|
+
export declare function formatBytes(bytes: number): string;
|
|
34
|
+
/**
|
|
35
|
+
* Keeps alpd running when its disk fills (t-2831): a write failing with ENOSPC, EDQUOT or
|
|
36
|
+
* EROFS pauses all of ALP and parks running assignments (as a usage limit does), instead of
|
|
37
|
+
* stopping alpd. Reads go on. Every minute alpd checks free space and probes a write; when
|
|
38
|
+
* the disk takes writes again it lifts the pause it made and parked work continues. A pause
|
|
39
|
+
* the user made, and their alp resume, stay theirs. alpd deletes nothing to make room.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createDiskGuard(options: DiskGuardOptions): {
|
|
42
|
+
report: (error: unknown) => boolean;
|
|
43
|
+
check: () => Promise<void>;
|
|
44
|
+
status: () => {
|
|
45
|
+
last?: {
|
|
46
|
+
at: string;
|
|
47
|
+
free?: number;
|
|
48
|
+
writable: boolean;
|
|
49
|
+
} | undefined;
|
|
50
|
+
held: boolean;
|
|
51
|
+
full: boolean;
|
|
52
|
+
};
|
|
53
|
+
start(): void;
|
|
54
|
+
stop(): void;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Makes process.stdout and stderr survive a full disk when they are files (alpd's log, as the
|
|
58
|
+
* detached launcher opens it): a failed write is dropped, not thrown later as an 'error' event
|
|
59
|
+
* that would end alpd, and logging continues once space returns. Other streams (a terminal,
|
|
60
|
+
* a pipe) keep Node's own writes but no longer turn a write error into a crash.
|
|
61
|
+
*/
|
|
62
|
+
export declare function guardOutput(onError: (error: unknown) => void, fs: {
|
|
63
|
+
fstatSync(fd: number): {
|
|
64
|
+
isFile(): boolean;
|
|
65
|
+
};
|
|
66
|
+
writeSync(fd: number, data: string | Uint8Array): number;
|
|
67
|
+
}): void;
|
package/dist/disk.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// packages/daemon/src/disk.ts
|
|
2
|
+
import { statfs, unlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
var DISK_CODES = /* @__PURE__ */ new Set(["ENOSPC", "EDQUOT", "EROFS"]);
|
|
5
|
+
function isDiskError(error) {
|
|
6
|
+
for (let current = error, depth = 0; current && depth < 4; current = current.cause, depth++) {
|
|
7
|
+
if (typeof current.code === "string" && DISK_CODES.has(current.code)) return true;
|
|
8
|
+
}
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
var GiB = 1024 ** 3;
|
|
12
|
+
var MiB = 1024 ** 2;
|
|
13
|
+
async function freeBytes(directory) {
|
|
14
|
+
try {
|
|
15
|
+
const info = await statfs(directory);
|
|
16
|
+
return Number(info.bavail) * Number(info.bsize);
|
|
17
|
+
} catch {
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function probeWrite(directory) {
|
|
22
|
+
const file = path.join(directory, `.alpd-disk-probe-${process.pid}`);
|
|
23
|
+
try {
|
|
24
|
+
await writeFile(file, "x".repeat(4096), { mode: 384 });
|
|
25
|
+
} finally {
|
|
26
|
+
await unlink(file).catch(() => {
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function formatBytes(bytes) {
|
|
31
|
+
return bytes >= GiB ? `${(bytes / GiB).toFixed(1)} GB` : `${Math.max(0, Math.round(bytes / MiB))} MB`;
|
|
32
|
+
}
|
|
33
|
+
function createDiskGuard(options) {
|
|
34
|
+
const { home, runtime, log } = options;
|
|
35
|
+
const lowBytes = options.lowBytes ?? 2 * GiB;
|
|
36
|
+
const criticalBytes = options.criticalBytes ?? 100 * MiB;
|
|
37
|
+
const resumeBytes = Math.max(options.resumeBytes ?? 256 * MiB, criticalBytes);
|
|
38
|
+
const free = options.free ?? freeBytes;
|
|
39
|
+
const probe = options.probe ?? probeWrite;
|
|
40
|
+
let warnedLow = false;
|
|
41
|
+
let timer;
|
|
42
|
+
let checking;
|
|
43
|
+
let last;
|
|
44
|
+
let full = false;
|
|
45
|
+
let good = 0;
|
|
46
|
+
function hold(reason) {
|
|
47
|
+
good = 0;
|
|
48
|
+
if (full && runtime.diskHeld()) return;
|
|
49
|
+
const first = !full;
|
|
50
|
+
full = true;
|
|
51
|
+
runtime.holdForDisk(reason);
|
|
52
|
+
if (first) log.warn(`${reason}; ALP pauses its work until the disk takes writes again`, { code: "ALP_DISK_FULL" });
|
|
53
|
+
}
|
|
54
|
+
function report(error) {
|
|
55
|
+
if (!isDiskError(error)) return false;
|
|
56
|
+
const code = error?.code ?? error?.cause?.code;
|
|
57
|
+
hold(code === "EROFS" ? "the disk is read-only" : "the disk is full");
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
async function checkOnce() {
|
|
61
|
+
const bytes = await bounded(free(home), void 0);
|
|
62
|
+
const writable = await bounded(probe(home).then(() => true, (error) => isDiskError(error) ? false : true), !full);
|
|
63
|
+
last = { at: (/* @__PURE__ */ new Date()).toISOString(), ...bytes === void 0 ? {} : { free: bytes }, writable };
|
|
64
|
+
const shown = bytes === void 0 ? "unknown" : formatBytes(bytes);
|
|
65
|
+
if (full) {
|
|
66
|
+
if (!writable || bytes !== void 0 && bytes < resumeBytes) {
|
|
67
|
+
good = 0;
|
|
68
|
+
if (!runtime.diskHeld()) runtime.holdForDisk(writable ? `the disk is almost full (${shown} free)` : "the disk is full");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (++good < 2) return;
|
|
72
|
+
full = false;
|
|
73
|
+
good = 0;
|
|
74
|
+
warnedLow = false;
|
|
75
|
+
runtime.releaseDisk("alpd, after disk space returned");
|
|
76
|
+
log.info(`the disk takes writes again (${shown} free); ALP resumes its work`, { code: "ALP_DISK_BACK", free: bytes });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!writable) {
|
|
80
|
+
hold("the disk is full");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (bytes !== void 0 && bytes < criticalBytes) {
|
|
84
|
+
hold(`the disk is almost full (${shown} free)`);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (bytes !== void 0 && bytes < lowBytes) {
|
|
88
|
+
if (!warnedLow) {
|
|
89
|
+
warnedLow = true;
|
|
90
|
+
log.warn(`disk space is low: ${shown} free`, { code: "ALP_DISK_LOW", free: bytes });
|
|
91
|
+
runtime.diskLow(shown);
|
|
92
|
+
}
|
|
93
|
+
} else if (bytes !== void 0 && bytes >= lowBytes * 1.25) warnedLow = false;
|
|
94
|
+
}
|
|
95
|
+
function bounded(work, fallback) {
|
|
96
|
+
let timer2;
|
|
97
|
+
const late = new Promise((resolve) => {
|
|
98
|
+
timer2 = setTimeout(() => resolve(fallback), options.checkTimeoutMs ?? 1e4);
|
|
99
|
+
});
|
|
100
|
+
return Promise.race([work, late]).finally(() => clearTimeout(timer2));
|
|
101
|
+
}
|
|
102
|
+
function check() {
|
|
103
|
+
checking ??= checkOnce().catch(() => {
|
|
104
|
+
}).finally(() => {
|
|
105
|
+
checking = void 0;
|
|
106
|
+
});
|
|
107
|
+
return checking;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
report,
|
|
111
|
+
check,
|
|
112
|
+
status: () => ({ held: runtime.diskHeld(), full, ...last ? { last } : {} }),
|
|
113
|
+
start() {
|
|
114
|
+
if (timer) return;
|
|
115
|
+
timer = setInterval(() => void check(), options.intervalMs ?? 6e4);
|
|
116
|
+
timer.unref?.();
|
|
117
|
+
void check();
|
|
118
|
+
},
|
|
119
|
+
stop() {
|
|
120
|
+
if (timer) clearInterval(timer);
|
|
121
|
+
timer = void 0;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function guardOutput(onError, fs) {
|
|
126
|
+
for (const [stream, fd] of [[process.stdout, 1], [process.stderr, 2]]) {
|
|
127
|
+
stream.on("error", onError);
|
|
128
|
+
let file = false;
|
|
129
|
+
try {
|
|
130
|
+
file = fs.fstatSync(fd).isFile();
|
|
131
|
+
} catch {
|
|
132
|
+
}
|
|
133
|
+
if (!file) continue;
|
|
134
|
+
stream.write = ((chunk, encoding, callback) => {
|
|
135
|
+
try {
|
|
136
|
+
fs.writeSync(fd, chunk);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
onError(error);
|
|
139
|
+
}
|
|
140
|
+
const done = typeof encoding === "function" ? encoding : callback;
|
|
141
|
+
if (typeof done === "function") done();
|
|
142
|
+
return true;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export {
|
|
147
|
+
GiB,
|
|
148
|
+
createDiskGuard,
|
|
149
|
+
formatBytes,
|
|
150
|
+
guardOutput,
|
|
151
|
+
isDiskError
|
|
152
|
+
};
|