@yefengr/steward 0.2.0 → 0.3.1
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/extension.js +360 -22
- package/dist/index.js +681 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { accessSync, closeSync, constants, existsSync, fstatSync, fsyncSync, linkSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { accessSync, closeSync, constants, existsSync, fstatSync, fsyncSync, linkSync, mkdirSync, openSync, readFileSync, readSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { homedir, hostname } from "node:os";
|
|
4
4
|
import { basename, delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
|
5
5
|
import { setTimeout } from "node:timers/promises";
|
|
@@ -31,7 +31,11 @@ var errorCodeSchema = z.enum([
|
|
|
31
31
|
"machine_required",
|
|
32
32
|
"attempt_ended",
|
|
33
33
|
"rate_limited",
|
|
34
|
-
"internal_error"
|
|
34
|
+
"internal_error",
|
|
35
|
+
"session_machine_offline",
|
|
36
|
+
"session_unsupported",
|
|
37
|
+
"session_record_missing",
|
|
38
|
+
"session_slots_full"
|
|
35
39
|
]);
|
|
36
40
|
var errorResponseSchema = z.strictObject({ error: z.strictObject({
|
|
37
41
|
code: errorCodeSchema,
|
|
@@ -48,10 +52,80 @@ z.strictObject({});
|
|
|
48
52
|
var okResponseSchema = z.strictObject({ ok: z.literal(true) });
|
|
49
53
|
var requestIdSchema = z.strictObject({ requestId: uuidSchema });
|
|
50
54
|
var HEARTBEAT_INTERVAL_MS = 15e3;
|
|
55
|
+
// packages/contract/dist/session-control.js
|
|
56
|
+
var absoluteWorkspacePathSchema = z.string().max(4096).regex(/^\/[^\0]*$/u);
|
|
57
|
+
var piSessionIdSchema = z.string().max(256).regex(/^[a-zA-Z0-9](?:[a-zA-Z0-9._-]*[a-zA-Z0-9])?$/u);
|
|
58
|
+
var piSessionLocatorSchema = z.strictObject({
|
|
59
|
+
path: absoluteWorkspacePathSchema,
|
|
60
|
+
sessionId: piSessionIdSchema,
|
|
61
|
+
sessionFile: absoluteWorkspacePathSchema
|
|
62
|
+
});
|
|
63
|
+
requestIdSchema.extend({ locator: piSessionLocatorSchema });
|
|
64
|
+
var sessionOperationModeSchema = z.enum(["restart", "restore"]);
|
|
65
|
+
var sessionOperationFailureSchema = z.enum([
|
|
66
|
+
"session_workspace_unavailable",
|
|
67
|
+
"session_record_missing",
|
|
68
|
+
"session_config_unsupported",
|
|
69
|
+
"session_stop_failed",
|
|
70
|
+
"session_launch_failed",
|
|
71
|
+
"session_timeout"
|
|
72
|
+
]);
|
|
73
|
+
var operation = z.strictObject({
|
|
74
|
+
id: uuidSchema,
|
|
75
|
+
attemptId: uuidSchema,
|
|
76
|
+
mode: sessionOperationModeSchema
|
|
77
|
+
});
|
|
78
|
+
var webSessionOperationSchema = operation.extend({
|
|
79
|
+
status: z.enum([
|
|
80
|
+
"pending",
|
|
81
|
+
"succeeded",
|
|
82
|
+
"failed"
|
|
83
|
+
]),
|
|
84
|
+
failure: sessionOperationFailureSchema.nullable(),
|
|
85
|
+
needsExitVerification: z.literal(true).optional()
|
|
86
|
+
});
|
|
87
|
+
requestIdSchema.extend({
|
|
88
|
+
attemptId: uuidSchema,
|
|
89
|
+
mode: sessionOperationModeSchema
|
|
90
|
+
});
|
|
91
|
+
var permission = operation.extend({
|
|
92
|
+
locator: piSessionLocatorSchema,
|
|
93
|
+
validForMs: z.int().positive()
|
|
94
|
+
});
|
|
95
|
+
var launcherSessionDirectiveSchema = z.discriminatedUnion("step", [
|
|
96
|
+
permission.extend({
|
|
97
|
+
step: z.literal("stop"),
|
|
98
|
+
sourceEnded: z.boolean()
|
|
99
|
+
}),
|
|
100
|
+
permission.extend({
|
|
101
|
+
step: z.literal("launch"),
|
|
102
|
+
replacementId: uuidSchema
|
|
103
|
+
}),
|
|
104
|
+
operation.extend({
|
|
105
|
+
step: z.literal("cleanup"),
|
|
106
|
+
replacementId: uuidSchema
|
|
107
|
+
})
|
|
108
|
+
]);
|
|
109
|
+
var launcherSessionReportSchema = z.discriminatedUnion("result", [requestIdSchema.extend({
|
|
110
|
+
result: z.literal("stopped"),
|
|
111
|
+
step: z.literal("stop")
|
|
112
|
+
}), requestIdSchema.extend({
|
|
113
|
+
result: z.literal("failed"),
|
|
114
|
+
step: z.enum([
|
|
115
|
+
"stop",
|
|
116
|
+
"launch",
|
|
117
|
+
"cleanup"
|
|
118
|
+
]),
|
|
119
|
+
failure: sessionOperationFailureSchema
|
|
120
|
+
})]);
|
|
51
121
|
// packages/contract/dist/models.js
|
|
52
122
|
var titleSchema = z.string().trim().min(1).max(500);
|
|
53
123
|
var taskTypeSchema = z.enum(["discussion", "implementation"]);
|
|
54
|
-
var attemptKindSchema = z.enum([
|
|
124
|
+
var attemptKindSchema = z.enum([
|
|
125
|
+
"new",
|
|
126
|
+
"rework",
|
|
127
|
+
"resume"
|
|
128
|
+
]);
|
|
55
129
|
var attemptResultSchema = z.enum([
|
|
56
130
|
"delivered",
|
|
57
131
|
"interrupted",
|
|
@@ -102,7 +176,7 @@ var attemptSchema = z.strictObject({
|
|
|
102
176
|
machineName: z.string(),
|
|
103
177
|
agent: agentSchema,
|
|
104
178
|
kind: attemptKindSchema,
|
|
105
|
-
sessionId:
|
|
179
|
+
sessionId: piSessionIdSchema,
|
|
106
180
|
tmuxSession: tmuxSessionSchema.nullable(),
|
|
107
181
|
claimedAt: timestampSchema,
|
|
108
182
|
startedAt: timestampSchema.nullable(),
|
|
@@ -185,7 +259,8 @@ z.strictObject({
|
|
|
185
259
|
questions: z.array(questionSchema),
|
|
186
260
|
deliveries: z.array(deliverySchema),
|
|
187
261
|
attempts: z.array(attemptSchema),
|
|
188
|
-
events: z.array(taskEventSchema)
|
|
262
|
+
events: z.array(taskEventSchema),
|
|
263
|
+
sessionOperation: webSessionOperationSchema.optional()
|
|
189
264
|
});
|
|
190
265
|
taskContentSchema.partial().required({ title: true }).extend({ projectId: projectIdSchema });
|
|
191
266
|
taskContentSchema.omit({ type: true }).partial().extend({ version: z.int().positive() }).refine((input) => Object.keys(input).some((key) => key !== "version"));
|
|
@@ -217,6 +292,7 @@ z.strictObject({ machines: z.array(webMachineSchema) });
|
|
|
217
292
|
var launcherHeartbeatRequestSchema = requestIdSchema.extend({
|
|
218
293
|
launcherVersion: z.string().trim().min(1).max(128),
|
|
219
294
|
slots: slotsSchema,
|
|
295
|
+
sessionControl: z.literal(true).optional(),
|
|
220
296
|
projects: z.array(z.strictObject({
|
|
221
297
|
id: projectIdSchema,
|
|
222
298
|
name: z.string().trim().min(1).max(500),
|
|
@@ -228,24 +304,28 @@ var launcherHeartbeatRequestSchema = requestIdSchema.extend({
|
|
|
228
304
|
running: z.boolean()
|
|
229
305
|
})).max(1e3).refine((items) => new Set(items.map(({ id }) => id)).size === items.length)
|
|
230
306
|
});
|
|
231
|
-
var launcherHeartbeatResponseSchema = z.strictObject({
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
307
|
+
var launcherHeartbeatResponseSchema = z.strictObject({
|
|
308
|
+
directives: z.array(z.strictObject({
|
|
309
|
+
attemptId: uuidSchema,
|
|
310
|
+
tmuxSession: tmuxSessionSchema,
|
|
311
|
+
action: z.enum([
|
|
312
|
+
"continue",
|
|
313
|
+
"cancel",
|
|
314
|
+
"unknown"
|
|
315
|
+
])
|
|
316
|
+
})),
|
|
317
|
+
sessionOperations: z.array(launcherSessionDirectiveSchema).optional()
|
|
318
|
+
});
|
|
240
319
|
var claimedAttemptSchema = z.strictObject({
|
|
241
320
|
id: uuidSchema,
|
|
242
321
|
taskId: uuidSchema,
|
|
243
322
|
projectId: projectIdSchema,
|
|
244
323
|
agent: z.literal("pi"),
|
|
245
|
-
sessionId:
|
|
324
|
+
sessionId: piSessionIdSchema,
|
|
246
325
|
restoreSession: z.boolean(),
|
|
326
|
+
sessionLocator: piSessionLocatorSchema.optional(),
|
|
247
327
|
displayName: z.string().min(1).max(80),
|
|
248
|
-
kind:
|
|
328
|
+
kind: z.enum(["new", "rework"])
|
|
249
329
|
});
|
|
250
330
|
var launcherClaimResponseSchema = z.strictObject({ attempt: claimedAttemptSchema.nullable() });
|
|
251
331
|
var launcherStartedRequestSchema = requestIdSchema.extend({
|
|
@@ -258,6 +338,17 @@ var launcherExitedRequestSchema = requestIdSchema.extend({
|
|
|
258
338
|
});
|
|
259
339
|
var launcherStartedResponseSchema = okResponseSchema;
|
|
260
340
|
var launcherExitedResponseSchema = okResponseSchema;
|
|
341
|
+
// packages/contract/dist/agent.js
|
|
342
|
+
var gitBranch = z.string().min(1).max(1024).regex(/^[^\0\r\n]+$/u);
|
|
343
|
+
var attemptWorkspaceSchema = piSessionLocatorSchema.extend({
|
|
344
|
+
gitKind: z.enum([
|
|
345
|
+
"non_git",
|
|
346
|
+
"primary",
|
|
347
|
+
"linked"
|
|
348
|
+
]),
|
|
349
|
+
gitBranch: gitBranch.nullable()
|
|
350
|
+
}).refine(({ gitKind, gitBranch }) => gitKind !== "non_git" || gitBranch === null);
|
|
351
|
+
requestIdSchema.extend({ workspace: attemptWorkspaceSchema });
|
|
261
352
|
z.strictObject({
|
|
262
353
|
task: taskContentSchema.pick({
|
|
263
354
|
title: true,
|
|
@@ -275,7 +366,13 @@ z.strictObject({
|
|
|
275
366
|
}),
|
|
276
367
|
workspace: z.strictObject({
|
|
277
368
|
path: z.string(),
|
|
278
|
-
requirements: z.array(nonEmptyTextSchema)
|
|
369
|
+
requirements: z.array(nonEmptyTextSchema),
|
|
370
|
+
previous: z.strictObject({
|
|
371
|
+
attemptId: uuidSchema,
|
|
372
|
+
machineId: uuidSchema,
|
|
373
|
+
sameMachine: z.boolean(),
|
|
374
|
+
workspace: attemptWorkspaceSchema.nullable()
|
|
375
|
+
}).nullable().optional()
|
|
279
376
|
}),
|
|
280
377
|
decisions: z.array(decisionSchema.extend({ confirmedByUser: z.literal(true) })),
|
|
281
378
|
deliveries: z.array(deliverySchema),
|
|
@@ -568,6 +665,7 @@ async function platformFetch(url, init, proxy = SYSTEM_PROXY) {
|
|
|
568
665
|
}
|
|
569
666
|
// apps/launcher/dist/platform.js
|
|
570
667
|
var REQUEST_TIMEOUT_MS$1 = 4e3;
|
|
668
|
+
var legacyHeartbeatResponse = launcherHeartbeatResponseSchema.omit({ sessionOperations: true }).strip();
|
|
571
669
|
var PlatformError = class extends Error {
|
|
572
670
|
code;
|
|
573
671
|
startedAlreadySaved;
|
|
@@ -617,7 +715,20 @@ function platformClient(config, fetcher, getMachineToken = () => config.machineT
|
|
|
617
715
|
}
|
|
618
716
|
return {
|
|
619
717
|
claim: (requestId) => post("claim", { requestId }, launcherClaimResponseSchema),
|
|
620
|
-
heartbeat: (body) =>
|
|
718
|
+
heartbeat: async (body) => {
|
|
719
|
+
if (!body.sessionControl) return post("heartbeat", body, legacyHeartbeatResponse);
|
|
720
|
+
try {
|
|
721
|
+
return await post("heartbeat", body, launcherHeartbeatResponseSchema);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
if (!(error instanceof PlatformError) || error.code !== "invalid_request") throw error;
|
|
724
|
+
return post("heartbeat", {
|
|
725
|
+
...body,
|
|
726
|
+
sessionControl: void 0,
|
|
727
|
+
requestId: randomUUID()
|
|
728
|
+
}, legacyHeartbeatResponse);
|
|
729
|
+
}
|
|
730
|
+
},
|
|
731
|
+
reportSession: (id, body) => post(`session-operations/${id}/report`, body, okResponseSchema),
|
|
621
732
|
report: (e) => post(`attempts/${e.id}/${e.kind}`, e.body, e.kind === "started" ? launcherStartedResponseSchema : launcherExitedResponseSchema, e.kind === "started")
|
|
622
733
|
};
|
|
623
734
|
}
|
|
@@ -635,10 +746,23 @@ function commandFailure(error) {
|
|
|
635
746
|
return error.code === "ENOENT" ? "missing" : "unavailable";
|
|
636
747
|
}
|
|
637
748
|
var TmuxError = class extends Error {
|
|
638
|
-
|
|
749
|
+
notStarted;
|
|
750
|
+
constructor(notStarted = false) {
|
|
639
751
|
super("无法完成 tmux 操作,将在下次循环重新核对。");
|
|
752
|
+
this.notStarted = notStarted;
|
|
640
753
|
}
|
|
641
754
|
};
|
|
755
|
+
function parsePid(value) {
|
|
756
|
+
const pid = /^[1-9][0-9]*$/u.test(value) ? Number(value) : NaN;
|
|
757
|
+
return pid > 1 && pid <= 2147483647 ? pid : void 0;
|
|
758
|
+
}
|
|
759
|
+
function resumeCommandDidNotStart(error, target) {
|
|
760
|
+
const { code, killed, signal, stderr } = error;
|
|
761
|
+
if (code === "ENOENT" || code === "EACCES") return true;
|
|
762
|
+
if (code !== 1 || killed || signal || typeof stderr !== "string") return false;
|
|
763
|
+
const message = stderr.trimEnd();
|
|
764
|
+
return message === `duplicate session: ${target}` || message === `invalid session name: ${target}` || /^(?:create window failed: )?fork failed(?:: [^\r\n]+)?$/u.test(message);
|
|
765
|
+
}
|
|
642
766
|
function displayName(title) {
|
|
643
767
|
return title.replace(/[^\p{L}\p{N} _-]/gu, " ").trim().replace(/^[ -]+/u, "").slice(0, 79).replace(/[\uD800-\uDBFF]$/u, "").trimEnd() || "Pi task";
|
|
644
768
|
}
|
|
@@ -693,8 +817,54 @@ function tmuxClient(config, options) {
|
|
|
693
817
|
if (!Array.isArray(sessions)) return "unavailable";
|
|
694
818
|
return sessions.includes(target) ? "present" : "absent";
|
|
695
819
|
},
|
|
820
|
+
async panePid(target) {
|
|
821
|
+
try {
|
|
822
|
+
const { stdout } = await exec([
|
|
823
|
+
"list-panes",
|
|
824
|
+
"-s",
|
|
825
|
+
"-t",
|
|
826
|
+
`=${target}`,
|
|
827
|
+
"-F",
|
|
828
|
+
"#{session_name} #{pane_pid}"
|
|
829
|
+
]);
|
|
830
|
+
const [name, pid, extra] = stdout.trimEnd().split(" ");
|
|
831
|
+
return name === target && pid !== void 0 && extra === void 0 ? parsePid(pid) : void 0;
|
|
832
|
+
} catch {
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
},
|
|
836
|
+
async hasResumeIdentity(attemptId) {
|
|
837
|
+
const target = `=${targetFor(attemptId)}`;
|
|
838
|
+
try {
|
|
839
|
+
if ((await exec([
|
|
840
|
+
"show-environment",
|
|
841
|
+
"-t",
|
|
842
|
+
target,
|
|
843
|
+
"STEWARD_ATTEMPT_ID"
|
|
844
|
+
])).stdout !== `STEWARD_ATTEMPT_ID=${attemptId}\n`) return false;
|
|
845
|
+
return (await exec([
|
|
846
|
+
"show-environment",
|
|
847
|
+
"-t",
|
|
848
|
+
target,
|
|
849
|
+
"STEWARD_RESUME"
|
|
850
|
+
])).stdout === "STEWARD_RESUME=1\n";
|
|
851
|
+
} catch {
|
|
852
|
+
return false;
|
|
853
|
+
}
|
|
854
|
+
},
|
|
855
|
+
processStatus(pid) {
|
|
856
|
+
if (parsePid(String(pid)) === void 0) return "unavailable";
|
|
857
|
+
try {
|
|
858
|
+
process.kill(pid, 0);
|
|
859
|
+
return "alive";
|
|
860
|
+
} catch (error) {
|
|
861
|
+
return error.code === "ESRCH" ? "dead" : "unavailable";
|
|
862
|
+
}
|
|
863
|
+
},
|
|
696
864
|
async launch(target, claim, projectPath) {
|
|
697
865
|
const message = `${claim.kind === "rework" ? "返修本任务。" : "开始执行本任务。"}${instructions}`;
|
|
866
|
+
const locator = claim.sessionLocator;
|
|
867
|
+
if (claim.kind === "rework" && !locator) throw new TmuxError(true);
|
|
698
868
|
try {
|
|
699
869
|
await exec([
|
|
700
870
|
"new-session",
|
|
@@ -702,7 +872,8 @@ function tmuxClient(config, options) {
|
|
|
702
872
|
"-s",
|
|
703
873
|
target,
|
|
704
874
|
"-c",
|
|
705
|
-
`${projectPath.replaceAll("#", "##")}/.`,
|
|
875
|
+
`${(locator?.path ?? projectPath).replaceAll("#", "##")}/.`,
|
|
876
|
+
...locator ? ["-e", "STEWARD_RESUME=1"] : [],
|
|
706
877
|
"-e",
|
|
707
878
|
`STEWARD_ATTEMPT_ID=${claim.id}`,
|
|
708
879
|
"-e",
|
|
@@ -714,8 +885,7 @@ function tmuxClient(config, options) {
|
|
|
714
885
|
...config.pi.extraArgs,
|
|
715
886
|
"-e",
|
|
716
887
|
options.extensionPath,
|
|
717
|
-
"--session-id",
|
|
718
|
-
claim.sessionId,
|
|
888
|
+
...locator ? ["--session", escapedTmuxArg(locator.sessionFile)] : ["--session-id", claim.sessionId],
|
|
719
889
|
"-n",
|
|
720
890
|
displayName(claim.displayName),
|
|
721
891
|
"--",
|
|
@@ -725,6 +895,44 @@ function tmuxClient(config, options) {
|
|
|
725
895
|
throw new TmuxError();
|
|
726
896
|
}
|
|
727
897
|
},
|
|
898
|
+
async launchResume(target, attemptId, locator) {
|
|
899
|
+
if (target !== targetFor(attemptId)) throw new TmuxError(true);
|
|
900
|
+
try {
|
|
901
|
+
const { stdout } = await exec([
|
|
902
|
+
"new-session",
|
|
903
|
+
"-d",
|
|
904
|
+
"-s",
|
|
905
|
+
target,
|
|
906
|
+
"-P",
|
|
907
|
+
"-F",
|
|
908
|
+
"#{pane_pid}",
|
|
909
|
+
"-c",
|
|
910
|
+
`${locator.path.replaceAll("#", "##")}/.`,
|
|
911
|
+
"-e",
|
|
912
|
+
`STEWARD_ATTEMPT_ID=${attemptId}`,
|
|
913
|
+
"-e",
|
|
914
|
+
escapedTmuxArg(`STEWARD_CONFIG=${options.configPath}`),
|
|
915
|
+
"-e",
|
|
916
|
+
"STEWARD_RESUME=1",
|
|
917
|
+
"-e",
|
|
918
|
+
escapedTmuxArg(`PATH=${path}`),
|
|
919
|
+
"--",
|
|
920
|
+
...[
|
|
921
|
+
config.pi.command,
|
|
922
|
+
...config.pi.extraArgs,
|
|
923
|
+
"-e",
|
|
924
|
+
options.extensionPath,
|
|
925
|
+
"--session",
|
|
926
|
+
locator.sessionFile
|
|
927
|
+
].map(escapedTmuxArg)
|
|
928
|
+
]);
|
|
929
|
+
const pid = parsePid(stdout.trim());
|
|
930
|
+
if (pid !== void 0) return pid;
|
|
931
|
+
} catch (error) {
|
|
932
|
+
throw new TmuxError(resumeCommandDidNotStart(error, target));
|
|
933
|
+
}
|
|
934
|
+
throw new TmuxError();
|
|
935
|
+
},
|
|
728
936
|
async cancel(target) {
|
|
729
937
|
try {
|
|
730
938
|
await exec([
|
|
@@ -738,7 +946,325 @@ function tmuxClient(config, options) {
|
|
|
738
946
|
}
|
|
739
947
|
};
|
|
740
948
|
}
|
|
949
|
+
// apps/launcher/dist/session-control.js
|
|
950
|
+
var MAX_HEADER_BYTES = 65536;
|
|
951
|
+
var HEADER_READ_BLOCK_BYTES = 4096;
|
|
952
|
+
var validElapsed = (elapsed, validForMs) => elapsed >= 0 && elapsed < validForMs;
|
|
953
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
954
|
+
"--provider",
|
|
955
|
+
"--model",
|
|
956
|
+
"--models",
|
|
957
|
+
"--thinking",
|
|
958
|
+
"--extension",
|
|
959
|
+
"-e",
|
|
960
|
+
"--tools",
|
|
961
|
+
"-t",
|
|
962
|
+
"--exclude-tools",
|
|
963
|
+
"-xt",
|
|
964
|
+
"--theme",
|
|
965
|
+
"--use-theme"
|
|
966
|
+
]);
|
|
967
|
+
var SWITCH_FLAGS = /* @__PURE__ */ new Set([
|
|
968
|
+
"--approve",
|
|
969
|
+
"-a",
|
|
970
|
+
"--no-approve",
|
|
971
|
+
"-na",
|
|
972
|
+
"--no-tools",
|
|
973
|
+
"-nt",
|
|
974
|
+
"--no-builtin-tools",
|
|
975
|
+
"-nbt",
|
|
976
|
+
"--offline",
|
|
977
|
+
"--no-extensions",
|
|
978
|
+
"-ne",
|
|
979
|
+
"--no-context-files",
|
|
980
|
+
"-nc",
|
|
981
|
+
"--no-skills",
|
|
982
|
+
"-ns"
|
|
983
|
+
]);
|
|
984
|
+
function supportsResumeArgs(args) {
|
|
985
|
+
for (let i = 0; i < args.length; i++) {
|
|
986
|
+
const flag = args[i];
|
|
987
|
+
if (flag === void 0) return false;
|
|
988
|
+
if (SWITCH_FLAGS.has(flag)) continue;
|
|
989
|
+
if (!VALUE_FLAGS.has(flag)) return false;
|
|
990
|
+
const value = args[++i];
|
|
991
|
+
if (!value || /^[-@]|[\0\r\n]/u.test(value)) return false;
|
|
992
|
+
}
|
|
993
|
+
return true;
|
|
994
|
+
}
|
|
995
|
+
function readHeader(file) {
|
|
996
|
+
const fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
997
|
+
try {
|
|
998
|
+
if (!fstatSync(fd).isFile()) throw new Error();
|
|
999
|
+
const bytes = Buffer.alloc(MAX_HEADER_BYTES);
|
|
1000
|
+
for (let offset = 0; offset < bytes.length;) {
|
|
1001
|
+
const count = readSync(fd, bytes, offset, Math.min(HEADER_READ_BLOCK_BYTES, bytes.length - offset), null);
|
|
1002
|
+
if (!count) return JSON.parse(bytes.toString("utf8", 0, offset));
|
|
1003
|
+
const newline = bytes.subarray(offset, offset + count).indexOf(10);
|
|
1004
|
+
if (newline >= 0) return JSON.parse(bytes.toString("utf8", 0, offset + newline));
|
|
1005
|
+
offset += count;
|
|
1006
|
+
}
|
|
1007
|
+
throw new Error();
|
|
1008
|
+
} finally {
|
|
1009
|
+
closeSync(fd);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
function validateLocator(locator) {
|
|
1013
|
+
try {
|
|
1014
|
+
if (realpathSync(locator.path) !== locator.path || !statSync(locator.path).isDirectory()) return "session_workspace_unavailable";
|
|
1015
|
+
accessSync(locator.path, constants.R_OK | constants.X_OK);
|
|
1016
|
+
} catch {
|
|
1017
|
+
return "session_workspace_unavailable";
|
|
1018
|
+
}
|
|
1019
|
+
let header;
|
|
1020
|
+
try {
|
|
1021
|
+
if (realpathSync(locator.sessionFile) !== locator.sessionFile) return "session_record_missing";
|
|
1022
|
+
header = readHeader(locator.sessionFile);
|
|
1023
|
+
} catch {
|
|
1024
|
+
return "session_record_missing";
|
|
1025
|
+
}
|
|
1026
|
+
if (header?.type !== "session" || header.id !== locator.sessionId) return "session_record_missing";
|
|
1027
|
+
try {
|
|
1028
|
+
if (typeof header.cwd !== "string" || !isAbsolute(header.cwd) || realpathSync(header.cwd) !== locator.path) return "session_workspace_unavailable";
|
|
1029
|
+
} catch {
|
|
1030
|
+
return "session_workspace_unavailable";
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
var SessionControl = class {
|
|
1034
|
+
options;
|
|
1035
|
+
constructor(options) {
|
|
1036
|
+
this.options = options;
|
|
1037
|
+
}
|
|
1038
|
+
get state() {
|
|
1039
|
+
return this.options.state;
|
|
1040
|
+
}
|
|
1041
|
+
get tmux() {
|
|
1042
|
+
return this.options.tmux;
|
|
1043
|
+
}
|
|
1044
|
+
save() {
|
|
1045
|
+
this.options.save();
|
|
1046
|
+
}
|
|
1047
|
+
record(id) {
|
|
1048
|
+
return this.state.executions.find((item) => item.id === id);
|
|
1049
|
+
}
|
|
1050
|
+
rememberPid(record, pid) {
|
|
1051
|
+
if (record.pid === void 0) {
|
|
1052
|
+
record.pid = pid;
|
|
1053
|
+
this.save();
|
|
1054
|
+
}
|
|
1055
|
+
return record.pid === pid;
|
|
1056
|
+
}
|
|
1057
|
+
finish(op, failure) {
|
|
1058
|
+
if (!failure && op.step !== "stop") return;
|
|
1059
|
+
op.report = failure ? {
|
|
1060
|
+
requestId: randomUUID(),
|
|
1061
|
+
result: "failed",
|
|
1062
|
+
step: op.step,
|
|
1063
|
+
failure
|
|
1064
|
+
} : {
|
|
1065
|
+
requestId: randomUUID(),
|
|
1066
|
+
result: "stopped",
|
|
1067
|
+
step: "stop"
|
|
1068
|
+
};
|
|
1069
|
+
this.save();
|
|
1070
|
+
}
|
|
1071
|
+
definitelyDead(pid) {
|
|
1072
|
+
return pid !== void 0 && this.tmux.processStatus(pid) === "dead";
|
|
1073
|
+
}
|
|
1074
|
+
async sourceGone(attemptId) {
|
|
1075
|
+
if (await this.tmux.inspect(targetFor(attemptId)) !== "absent") return false;
|
|
1076
|
+
const pid = this.record(attemptId)?.pid;
|
|
1077
|
+
return pid === void 0 || this.definitelyDead(pid);
|
|
1078
|
+
}
|
|
1079
|
+
async replacementGone(op) {
|
|
1080
|
+
const id = op.replacementId;
|
|
1081
|
+
if (id === void 0 || id === op.attemptId) return false;
|
|
1082
|
+
const record = this.record(id);
|
|
1083
|
+
if (record && record.sessionOperationId !== op.id) return false;
|
|
1084
|
+
if (await this.tmux.inspect(targetFor(id)) !== "absent" || !await this.sourceGone(op.attemptId)) return false;
|
|
1085
|
+
return record?.pid !== void 0 ? this.definitelyDead(record.pid) : op.launchIssued === false;
|
|
1086
|
+
}
|
|
1087
|
+
async remainsStopped(target, pid, mayRecheck = () => true) {
|
|
1088
|
+
for (let i = 0; i < 3; i++) {
|
|
1089
|
+
if (await this.tmux.inspect(target) === "absent" && this.definitelyDead(pid)) return true;
|
|
1090
|
+
if (i === 2 || !mayRecheck()) break;
|
|
1091
|
+
await setTimeout(50);
|
|
1092
|
+
if (!mayRecheck()) break;
|
|
1093
|
+
}
|
|
1094
|
+
return false;
|
|
1095
|
+
}
|
|
1096
|
+
invalid(locator) {
|
|
1097
|
+
return validateLocator(locator) ?? (supportsResumeArgs(this.options.extraArgs) ? void 0 : "session_config_unsupported");
|
|
1098
|
+
}
|
|
1099
|
+
async stop(directive, op, valid) {
|
|
1100
|
+
const invalid = this.invalid(directive.locator);
|
|
1101
|
+
if (invalid) {
|
|
1102
|
+
this.finish(op, invalid);
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
const target = targetFor(directive.attemptId);
|
|
1106
|
+
const existing = this.record(directive.attemptId);
|
|
1107
|
+
const status = await this.tmux.inspect(target);
|
|
1108
|
+
if (status === "absent") {
|
|
1109
|
+
const pid = existing?.pid;
|
|
1110
|
+
if (pid === void 0 && directive.sourceEnded || this.definitelyDead(pid) || op.stopIssued && pid !== void 0 && await this.remainsStopped(target, pid, valid)) this.finish(op);
|
|
1111
|
+
else this.finish(op, "session_stop_failed");
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
if (op.stopIssued && existing?.pid !== void 0 && await this.remainsStopped(target, existing.pid, valid)) {
|
|
1115
|
+
this.finish(op);
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
if (status !== "present" || directive.sourceEnded || op.stopIssued || existing?.phase === "exited") {
|
|
1119
|
+
this.finish(op, "session_stop_failed");
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
const pid = await this.tmux.panePid(target);
|
|
1123
|
+
if (pid === void 0 || this.tmux.processStatus(pid) !== "alive") {
|
|
1124
|
+
this.finish(op, "session_stop_failed");
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
const record = existing ?? {
|
|
1128
|
+
id: directive.attemptId,
|
|
1129
|
+
target,
|
|
1130
|
+
phase: "observed"
|
|
1131
|
+
};
|
|
1132
|
+
if (!existing) this.state.executions.push(record);
|
|
1133
|
+
if (!this.rememberPid(record, pid)) {
|
|
1134
|
+
this.finish(op, "session_stop_failed");
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
if (!valid()) return;
|
|
1138
|
+
op.stopIssued = true;
|
|
1139
|
+
this.save();
|
|
1140
|
+
if (!valid()) {
|
|
1141
|
+
delete op.stopIssued;
|
|
1142
|
+
this.save();
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
await this.tryCancel(target);
|
|
1146
|
+
this.finish(op, await this.remainsStopped(target, pid, valid) ? void 0 : "session_stop_failed");
|
|
1147
|
+
}
|
|
1148
|
+
async launch(directive, op, valid) {
|
|
1149
|
+
const id = directive.replacementId;
|
|
1150
|
+
const target = targetFor(id);
|
|
1151
|
+
if (id === directive.attemptId) return;
|
|
1152
|
+
const existing = this.record(id);
|
|
1153
|
+
if (op.launchIssued || existing) {
|
|
1154
|
+
if (existing?.sessionOperationId === op.id && op.launchIssued === false && await this.replacementGone(op)) {
|
|
1155
|
+
existing.phase = "exited";
|
|
1156
|
+
this.finish(op, "session_launch_failed");
|
|
1157
|
+
}
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
if (await this.tmux.inspect(target) !== "absent" || !await this.sourceGone(directive.attemptId)) return;
|
|
1161
|
+
const invalid = this.invalid(directive.locator);
|
|
1162
|
+
if (invalid) {
|
|
1163
|
+
op.launchIssued = false;
|
|
1164
|
+
this.finish(op, invalid);
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
if (!valid()) return;
|
|
1168
|
+
const record = {
|
|
1169
|
+
id,
|
|
1170
|
+
target,
|
|
1171
|
+
phase: "launching",
|
|
1172
|
+
startedAt: this.options.now(),
|
|
1173
|
+
sessionOperationId: op.id
|
|
1174
|
+
};
|
|
1175
|
+
const previouslyIssued = op.launchIssued;
|
|
1176
|
+
op.launchIssued = true;
|
|
1177
|
+
this.state.executions.push(record);
|
|
1178
|
+
this.save();
|
|
1179
|
+
if (!valid()) {
|
|
1180
|
+
op.launchIssued = previouslyIssued;
|
|
1181
|
+
this.state.executions = this.state.executions.filter((item) => item !== record);
|
|
1182
|
+
this.save();
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
try {
|
|
1186
|
+
const pid = await this.tmux.launchResume(target, id, directive.locator);
|
|
1187
|
+
this.rememberPid(record, pid);
|
|
1188
|
+
if (await this.tmux.inspect(target) === "present" && await this.tmux.panePid(target) === pid) this.options.started(record);
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
if (!(error instanceof TmuxError)) throw error;
|
|
1191
|
+
if (error.notStarted) {
|
|
1192
|
+
op.launchIssued = false;
|
|
1193
|
+
this.save();
|
|
1194
|
+
if (await this.replacementGone(op)) {
|
|
1195
|
+
record.phase = "exited";
|
|
1196
|
+
this.finish(op, "session_launch_failed");
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
this.options.warn?.(error.message);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
async cleanup(directive, op) {
|
|
1203
|
+
if (directive.replacementId === directive.attemptId) return;
|
|
1204
|
+
const target = targetFor(directive.replacementId);
|
|
1205
|
+
const record = this.record(directive.replacementId);
|
|
1206
|
+
if (record && record.sessionOperationId !== op.id) return;
|
|
1207
|
+
const status = await this.tmux.inspect(target);
|
|
1208
|
+
if (status === "absent") {
|
|
1209
|
+
if (await this.replacementGone(op)) this.finish(op, "session_timeout");
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
if (status !== "present") return;
|
|
1213
|
+
const pid = await this.tmux.panePid(target);
|
|
1214
|
+
if (pid === void 0 || record?.pid !== void 0 && record.pid !== pid || op.stopIssued && record?.pid !== pid) return;
|
|
1215
|
+
if (!await this.tmux.hasResumeIdentity(directive.replacementId)) return;
|
|
1216
|
+
const candidate = record ?? {
|
|
1217
|
+
id: directive.replacementId,
|
|
1218
|
+
target,
|
|
1219
|
+
phase: "observed",
|
|
1220
|
+
sessionOperationId: op.id
|
|
1221
|
+
};
|
|
1222
|
+
if (await this.tmux.panePid(target) !== pid || this.tmux.processStatus(pid) !== "alive") return;
|
|
1223
|
+
if (!record) this.state.executions.push(candidate);
|
|
1224
|
+
candidate.pid = pid;
|
|
1225
|
+
op.stopIssued = true;
|
|
1226
|
+
this.save();
|
|
1227
|
+
await this.tryCancel(target);
|
|
1228
|
+
if (await this.remainsStopped(target, pid) && await this.sourceGone(directive.attemptId)) this.finish(op, "session_timeout");
|
|
1229
|
+
}
|
|
1230
|
+
async tryCancel(target) {
|
|
1231
|
+
try {
|
|
1232
|
+
await this.tmux.cancel(target);
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
if (!(error instanceof TmuxError)) throw error;
|
|
1235
|
+
this.options.warn?.(error.message);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
async apply(directive, requestStartedAt) {
|
|
1239
|
+
const { id, attemptId, step } = directive;
|
|
1240
|
+
const valid = () => step === "cleanup" || validElapsed(this.options.now() - requestStartedAt.wall, directive.validForMs) && validElapsed(this.options.monotonicNow() - requestStartedAt.monotonic, directive.validForMs);
|
|
1241
|
+
if (!valid()) return;
|
|
1242
|
+
const operations = this.state.sessionOperations ??= [];
|
|
1243
|
+
let op = operations.find((item) => item.id === id);
|
|
1244
|
+
const replacementId = step === "stop" ? void 0 : directive.replacementId;
|
|
1245
|
+
if (op && (op.attemptId !== attemptId || op.replacementId !== void 0 && op.replacementId !== replacementId)) return;
|
|
1246
|
+
if (op && op.step !== step && (step === "stop" || op.step === "cleanup")) return;
|
|
1247
|
+
if (op?.step !== step) {
|
|
1248
|
+
op = {
|
|
1249
|
+
id,
|
|
1250
|
+
attemptId,
|
|
1251
|
+
step,
|
|
1252
|
+
replacementId,
|
|
1253
|
+
launchIssued: op?.launchIssued ?? (op && op.step === "stop" ? false : void 0)
|
|
1254
|
+
};
|
|
1255
|
+
const previous = operations.findIndex((item) => item.id === id);
|
|
1256
|
+
if (previous < 0) operations.push(op);
|
|
1257
|
+
else operations[previous] = op;
|
|
1258
|
+
this.save();
|
|
1259
|
+
}
|
|
1260
|
+
if (op.report) return;
|
|
1261
|
+
if (step === "stop") await this.stop(directive, op, valid);
|
|
1262
|
+
else if (step === "launch") await this.launch(directive, op, valid);
|
|
1263
|
+
else await this.cleanup(directive, op);
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
741
1266
|
// apps/launcher/dist/launcher.js
|
|
1267
|
+
var managedId = (operation) => operation.step === "stop" ? operation.attemptId : operation.replacementId;
|
|
742
1268
|
var TERMINAL_REPORT_ERRORS = /* @__PURE__ */ new Set([
|
|
743
1269
|
"attempt_ended",
|
|
744
1270
|
"forbidden",
|
|
@@ -752,8 +1278,19 @@ var Launcher = class {
|
|
|
752
1278
|
nextOutboxRetryAt = 0;
|
|
753
1279
|
outboxBackoff = HEARTBEAT_INTERVAL_MS;
|
|
754
1280
|
lastTmuxHealth = "ready";
|
|
1281
|
+
control;
|
|
755
1282
|
constructor(options) {
|
|
756
1283
|
this.options = options;
|
|
1284
|
+
this.control = new SessionControl({
|
|
1285
|
+
state: options.state,
|
|
1286
|
+
tmux: options.tmux,
|
|
1287
|
+
save: () => this.persist(),
|
|
1288
|
+
now: () => this.now(),
|
|
1289
|
+
monotonicNow: () => this.monotonicNow(),
|
|
1290
|
+
extraArgs: options.config.pi.extraArgs,
|
|
1291
|
+
started: (record) => this.started(record),
|
|
1292
|
+
warn: options.warn
|
|
1293
|
+
});
|
|
757
1294
|
}
|
|
758
1295
|
get state() {
|
|
759
1296
|
return this.options.state;
|
|
@@ -764,6 +1301,9 @@ var Launcher = class {
|
|
|
764
1301
|
now() {
|
|
765
1302
|
return (this.options.now ?? Date.now)();
|
|
766
1303
|
}
|
|
1304
|
+
monotonicNow() {
|
|
1305
|
+
return this.options.monotonicNow?.() ?? performance.now();
|
|
1306
|
+
}
|
|
767
1307
|
async retryable(run) {
|
|
768
1308
|
try {
|
|
769
1309
|
return await run();
|
|
@@ -773,9 +1313,18 @@ var Launcher = class {
|
|
|
773
1313
|
return;
|
|
774
1314
|
}
|
|
775
1315
|
}
|
|
1316
|
+
pendingOperationFor(attemptId) {
|
|
1317
|
+
return this.state.sessionOperations?.find(({ replacementId }) => replacementId === attemptId);
|
|
1318
|
+
}
|
|
776
1319
|
async flush() {
|
|
777
1320
|
if (this.now() < this.nextOutboxRetryAt) return false;
|
|
1321
|
+
let deferred = false;
|
|
778
1322
|
for (const event of [...this.state.outbox]) {
|
|
1323
|
+
const operation = this.pendingOperationFor(event.id);
|
|
1324
|
+
if (event.kind === "exited" && operation && !await this.control.replacementGone(operation)) {
|
|
1325
|
+
deferred = true;
|
|
1326
|
+
continue;
|
|
1327
|
+
}
|
|
779
1328
|
let confirmed = true;
|
|
780
1329
|
try {
|
|
781
1330
|
await this.options.platform.report(event);
|
|
@@ -793,10 +1342,10 @@ var Launcher = class {
|
|
|
793
1342
|
this.outboxBackoff = HEARTBEAT_INTERVAL_MS;
|
|
794
1343
|
this.nextOutboxRetryAt = 0;
|
|
795
1344
|
this.state.outbox = this.state.outbox.filter(({ body }) => body.requestId !== event.body.requestId);
|
|
796
|
-
if (event.kind === "exited" && confirmed) this.remove(event.id);
|
|
1345
|
+
if (event.kind === "exited" && confirmed && !operation) this.remove(event.id);
|
|
797
1346
|
else this.persist();
|
|
798
1347
|
}
|
|
799
|
-
return
|
|
1348
|
+
return !deferred;
|
|
800
1349
|
}
|
|
801
1350
|
remove(id) {
|
|
802
1351
|
this.state.executions = this.state.executions.filter((record) => record.id !== id);
|
|
@@ -830,6 +1379,24 @@ var Launcher = class {
|
|
|
830
1379
|
});
|
|
831
1380
|
this.persist();
|
|
832
1381
|
}
|
|
1382
|
+
async observePresent(record) {
|
|
1383
|
+
const pendingResume = record.sessionOperationId && record.phase === "launching";
|
|
1384
|
+
if (record.pid === void 0 || pendingResume) {
|
|
1385
|
+
const pid = await this.options.tmux.panePid(record.target);
|
|
1386
|
+
if (pendingResume && (pid === void 0 || record.pid !== void 0 && record.pid !== pid)) return false;
|
|
1387
|
+
if (pid !== void 0 && record.pid === void 0) {
|
|
1388
|
+
if (pendingResume && (!await this.options.tmux.hasResumeIdentity(record.id) || await this.options.tmux.panePid(record.target) !== pid)) return false;
|
|
1389
|
+
record.pid = pid;
|
|
1390
|
+
this.persist();
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
this.started(record);
|
|
1394
|
+
return true;
|
|
1395
|
+
}
|
|
1396
|
+
absenceConfirmed(record) {
|
|
1397
|
+
if (record.pid !== void 0) return this.options.tmux.processStatus(record.pid) === "dead";
|
|
1398
|
+
return record.sessionOperationId === void 0;
|
|
1399
|
+
}
|
|
833
1400
|
async observe() {
|
|
834
1401
|
const attempts = [];
|
|
835
1402
|
for (const record of this.state.executions) {
|
|
@@ -839,8 +1406,13 @@ var Launcher = class {
|
|
|
839
1406
|
this.options.warn?.("tmux 会话核对失败,保留本地尝试。");
|
|
840
1407
|
continue;
|
|
841
1408
|
}
|
|
842
|
-
if (status === "present"
|
|
843
|
-
if (status === "absent" && record.phase !== "prepared")
|
|
1409
|
+
if (status === "present" && !await this.observePresent(record)) continue;
|
|
1410
|
+
if (status === "absent" && record.phase !== "prepared") {
|
|
1411
|
+
if (!this.absenceConfirmed(record)) continue;
|
|
1412
|
+
const operation = this.pendingOperationFor(record.id);
|
|
1413
|
+
if (operation && !await this.control.replacementGone(operation)) continue;
|
|
1414
|
+
this.exited(record);
|
|
1415
|
+
}
|
|
844
1416
|
attempts.push({
|
|
845
1417
|
id: record.id,
|
|
846
1418
|
tmuxSession: record.target,
|
|
@@ -851,18 +1423,25 @@ var Launcher = class {
|
|
|
851
1423
|
}
|
|
852
1424
|
async heartbeat() {
|
|
853
1425
|
const attempts = await this.observe();
|
|
1426
|
+
const requestStartedAt = {
|
|
1427
|
+
wall: this.now(),
|
|
1428
|
+
monotonic: this.monotonicNow()
|
|
1429
|
+
};
|
|
854
1430
|
const response = await this.retryable(() => this.options.platform.heartbeat({
|
|
855
1431
|
requestId: randomUUID(),
|
|
856
1432
|
launcherVersion: this.options.version,
|
|
1433
|
+
sessionControl: true,
|
|
857
1434
|
slots: this.options.config.slots,
|
|
858
1435
|
projects: this.options.config.projects,
|
|
859
1436
|
attempts
|
|
860
1437
|
}));
|
|
861
|
-
if (response)
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
1438
|
+
if (!response) return;
|
|
1439
|
+
this.state.lastHeartbeatAt = this.now();
|
|
1440
|
+
this.persist();
|
|
1441
|
+
return {
|
|
1442
|
+
response,
|
|
1443
|
+
requestStartedAt
|
|
1444
|
+
};
|
|
866
1445
|
}
|
|
867
1446
|
async startPrepared(record) {
|
|
868
1447
|
const claim = record.claim;
|
|
@@ -871,11 +1450,19 @@ var Launcher = class {
|
|
|
871
1450
|
this.exited(record);
|
|
872
1451
|
return;
|
|
873
1452
|
}
|
|
1453
|
+
if (claim.kind === "rework") {
|
|
1454
|
+
const failure = !claim.sessionLocator ? "session_record_missing" : validateLocator(claim.sessionLocator) ?? (supportsResumeArgs(this.options.config.pi.extraArgs) ? void 0 : "session_config_unsupported");
|
|
1455
|
+
if (failure) {
|
|
1456
|
+
this.options.warn?.(`返修无法启动(${failure}),未回退到登记目录或其他会话。`);
|
|
1457
|
+
this.exited(record);
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
874
1461
|
record.phase = "launching";
|
|
875
1462
|
record.startedAt = this.now();
|
|
876
1463
|
this.persist();
|
|
877
1464
|
if (await this.retryable(async () => {
|
|
878
|
-
await this.options.tmux.launch(record.target, claim, project.path);
|
|
1465
|
+
await this.options.tmux.launch(record.target, claim, claim.sessionLocator?.path ?? project.path);
|
|
879
1466
|
return true;
|
|
880
1467
|
})) this.started(record);
|
|
881
1468
|
}
|
|
@@ -900,17 +1487,51 @@ var Launcher = class {
|
|
|
900
1487
|
await this.retryable(() => this.options.tmux.cancel(target));
|
|
901
1488
|
if (await this.options.tmux.inspect(target) !== "absent") return;
|
|
902
1489
|
}
|
|
903
|
-
this.exited(record);
|
|
904
|
-
} else if (status === "present") this.
|
|
1490
|
+
if (this.absenceConfirmed(record)) this.exited(record);
|
|
1491
|
+
} else if (status === "present") await this.observePresent(record);
|
|
905
1492
|
else if (action === "continue" && record.phase === "prepared") await this.startPrepared(record);
|
|
906
|
-
else if (record.phase !== "prepared") this.exited(record);
|
|
1493
|
+
else if (record.phase !== "prepared" && this.absenceConfirmed(record)) this.exited(record);
|
|
907
1494
|
}
|
|
908
|
-
async
|
|
1495
|
+
async reconcileSessionOperations(operations, requestStartedAt) {
|
|
1496
|
+
const protectedIds = /* @__PURE__ */ new Set();
|
|
1497
|
+
for (const directive of operations) {
|
|
1498
|
+
protectedIds.add(directive.attemptId);
|
|
1499
|
+
protectedIds.add(managedId(directive));
|
|
1500
|
+
await this.control.apply(directive, requestStartedAt);
|
|
1501
|
+
const operation = this.state.sessionOperations?.find(({ id, step, attemptId, replacementId }) => id === directive.id && step === directive.step && attemptId === directive.attemptId && (step === "stop" || replacementId === managedId(directive)));
|
|
1502
|
+
if (!operation?.report || operation.reportAcknowledged) continue;
|
|
1503
|
+
if (operation.step !== "stop" && !await this.control.replacementGone(operation)) continue;
|
|
1504
|
+
try {
|
|
1505
|
+
await this.options.platform.reportSession(operation.id, operation.report);
|
|
1506
|
+
} catch (error) {
|
|
1507
|
+
if (!(error instanceof PlatformError)) throw error;
|
|
1508
|
+
this.options.warn?.(error.message);
|
|
1509
|
+
if (!TERMINAL_REPORT_ERRORS.has(error.code)) continue;
|
|
1510
|
+
}
|
|
1511
|
+
operation.reportAcknowledged = true;
|
|
1512
|
+
this.persist();
|
|
1513
|
+
}
|
|
1514
|
+
return protectedIds;
|
|
1515
|
+
}
|
|
1516
|
+
async reconcile({ response, requestStartedAt }, claimPending = false) {
|
|
1517
|
+
const operations = response.sessionOperations ?? [];
|
|
1518
|
+
const protectedIds = await this.reconcileSessionOperations(operations, requestStartedAt);
|
|
909
1519
|
for (const directive of response.directives) {
|
|
1520
|
+
if (protectedIds.has(directive.attemptId)) continue;
|
|
910
1521
|
if (claimPending && !this.state.executions.some(({ id }) => id === directive.attemptId)) continue;
|
|
911
1522
|
await this.applyDirective(directive);
|
|
912
1523
|
}
|
|
913
|
-
const active = new Set(response.directives.map(({ attemptId }) => attemptId));
|
|
1524
|
+
const active = /* @__PURE__ */ new Set([...response.directives.map(({ attemptId }) => attemptId), ...protectedIds]);
|
|
1525
|
+
const localOperations = this.state.sessionOperations;
|
|
1526
|
+
if (localOperations) {
|
|
1527
|
+
const live = new Set(operations.map(({ id }) => id));
|
|
1528
|
+
const ordinary = new Set(response.directives.map(({ attemptId }) => attemptId));
|
|
1529
|
+
const keep = localOperations.filter((item) => live.has(item.id) || !item.reportAcknowledged && (item.replacementId === void 0 || !ordinary.has(item.replacementId)));
|
|
1530
|
+
if (keep.length !== localOperations.length) {
|
|
1531
|
+
this.state.sessionOperations = keep;
|
|
1532
|
+
this.persist();
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
914
1535
|
const pending = new Set(this.state.outbox.map(({ id }) => id));
|
|
915
1536
|
for (const record of [...this.state.executions]) {
|
|
916
1537
|
if (active.has(record.id)) continue;
|
|
@@ -952,7 +1573,11 @@ var Launcher = class {
|
|
|
952
1573
|
await this.reconcile(heartbeat, !!this.state.claimRequestId);
|
|
953
1574
|
if (!flushed || !await this.flush() || replayed) return;
|
|
954
1575
|
const slots = this.options.config.slots;
|
|
955
|
-
if ((/* @__PURE__ */ new Set([
|
|
1576
|
+
if ((/* @__PURE__ */ new Set([
|
|
1577
|
+
...heartbeat.response.directives.map(({ attemptId }) => attemptId),
|
|
1578
|
+
...(heartbeat.response.sessionOperations ?? []).map(managedId),
|
|
1579
|
+
...this.state.executions.filter(({ phase }) => phase !== "exited").map(({ id }) => id)
|
|
1580
|
+
])).size >= slots || !await this.canClaim()) return;
|
|
956
1581
|
if (!await this.claim()) return;
|
|
957
1582
|
const afterClaim = await this.heartbeat();
|
|
958
1583
|
if (!afterClaim) return;
|
|
@@ -1198,7 +1823,9 @@ var execution = z.strictObject({
|
|
|
1198
1823
|
"exited"
|
|
1199
1824
|
]),
|
|
1200
1825
|
claim: claimedAttemptSchema.optional(),
|
|
1201
|
-
startedAt: timestampSchema.optional()
|
|
1826
|
+
startedAt: timestampSchema.optional(),
|
|
1827
|
+
pid: z.int().min(2).max(2147483647).optional(),
|
|
1828
|
+
sessionOperationId: uuidSchema.optional()
|
|
1202
1829
|
}).refine(({ id, target, phase, claim, startedAt }) => target === `steward-${id}` && (!claim || claim.id === id) && (phase !== "prepared" || !!claim) && (phase !== "launching" || startedAt !== void 0));
|
|
1203
1830
|
var event = z.discriminatedUnion("kind", [z.strictObject({
|
|
1204
1831
|
kind: z.literal("started"),
|
|
@@ -1209,11 +1836,26 @@ var event = z.discriminatedUnion("kind", [z.strictObject({
|
|
|
1209
1836
|
id: uuidSchema,
|
|
1210
1837
|
body: launcherExitedRequestSchema
|
|
1211
1838
|
})]);
|
|
1839
|
+
var sessionOperation = z.strictObject({
|
|
1840
|
+
id: uuidSchema,
|
|
1841
|
+
attemptId: uuidSchema,
|
|
1842
|
+
replacementId: uuidSchema.optional(),
|
|
1843
|
+
step: z.enum([
|
|
1844
|
+
"stop",
|
|
1845
|
+
"launch",
|
|
1846
|
+
"cleanup"
|
|
1847
|
+
]),
|
|
1848
|
+
stopIssued: z.boolean().optional(),
|
|
1849
|
+
launchIssued: z.boolean().optional(),
|
|
1850
|
+
report: launcherSessionReportSchema.optional(),
|
|
1851
|
+
reportAcknowledged: z.boolean().optional()
|
|
1852
|
+
}).refine(({ step, replacementId, report }) => step === "stop" === (replacementId === void 0) && (!report || report.step === step));
|
|
1212
1853
|
var stateSchema = z.strictObject({
|
|
1213
1854
|
version: z.literal(1),
|
|
1214
1855
|
claimRequestId: uuidSchema.optional(),
|
|
1215
1856
|
lastHeartbeatAt: timestampSchema.optional(),
|
|
1216
1857
|
executions: z.array(execution),
|
|
1858
|
+
sessionOperations: z.array(sessionOperation).optional(),
|
|
1217
1859
|
outbox: z.array(event)
|
|
1218
1860
|
});
|
|
1219
1861
|
function loadState(path) {
|