@giladbarnea/pi-simple-team 2.0.20 → 2.0.21
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/child-tools.ts +4 -0
- package/index.ts +19 -10
- package/package.json +1 -1
package/child-tools.ts
CHANGED
|
@@ -32,6 +32,8 @@ export interface ChildRuntimeConfig {
|
|
|
32
32
|
teamId?: string;
|
|
33
33
|
teamName: string;
|
|
34
34
|
teammateName: string;
|
|
35
|
+
/** Names this start of the teammate's process to main. A main that predates process ids sets none and checks none. */
|
|
36
|
+
processId?: string;
|
|
35
37
|
participants: string[];
|
|
36
38
|
canManageOwnTeams: boolean;
|
|
37
39
|
interruptWaitTimeoutMilliseconds?: number;
|
|
@@ -60,6 +62,7 @@ function readRequiredChildRuntimeConfig(): ChildRuntimeConfig {
|
|
|
60
62
|
teamId,
|
|
61
63
|
teamName: requiredEnvironmentVariable("PI_SIMPLE_TEAM_TEAM_NAME"),
|
|
62
64
|
teammateName: requiredEnvironmentVariable("PI_SIMPLE_TEAM_MEMBER"),
|
|
65
|
+
processId: process.env.PI_SIMPLE_TEAM_PROCESS,
|
|
63
66
|
participants: readParticipants(),
|
|
64
67
|
canManageOwnTeams: process.env.PI_SIMPLE_TEAM_CAN_MANAGE_OWN_TEAMS === "1",
|
|
65
68
|
};
|
|
@@ -94,6 +97,7 @@ export async function callParent(config: ChildRuntimeConfig, tool: string, args:
|
|
|
94
97
|
token: config.callbackToken,
|
|
95
98
|
team: config.teamId ?? config.teamName,
|
|
96
99
|
from: config.teammateName,
|
|
100
|
+
process: config.processId,
|
|
97
101
|
tool,
|
|
98
102
|
args,
|
|
99
103
|
}),
|
package/index.ts
CHANGED
|
@@ -49,8 +49,12 @@ interface RunFailure {
|
|
|
49
49
|
replaced: TeamStatus;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
/**
|
|
53
|
-
|
|
52
|
+
/**
|
|
53
|
+
* One start of a teammate's process. Main gives the process this record's id, and the process sends it with every callback,
|
|
54
|
+
* so main never mistakes a replaced process for the current one. A background process's exit is judged by its own record.
|
|
55
|
+
*/
|
|
56
|
+
interface ProcessRecord {
|
|
57
|
+
id: string;
|
|
54
58
|
registered: boolean;
|
|
55
59
|
stopRequested: boolean;
|
|
56
60
|
quitReported: boolean;
|
|
@@ -70,7 +74,7 @@ interface TeammateState {
|
|
|
70
74
|
sessionMaterialized: boolean;
|
|
71
75
|
lastModelActivityAt?: string;
|
|
72
76
|
process?: ChildProcess;
|
|
73
|
-
processRecord?:
|
|
77
|
+
processRecord?: ProcessRecord;
|
|
74
78
|
paneId?: string;
|
|
75
79
|
deliveryUrl?: string;
|
|
76
80
|
ready?: Promise<void>;
|
|
@@ -376,7 +380,7 @@ function notifyOwner(team: TeamState, teammate: TeammateState, notification: str
|
|
|
376
380
|
* A notice for an exit nobody asked for. A self-quit (code 0 after a quit shutdown) and a stop by the parent are excluded.
|
|
377
381
|
* @example isUnexpectedExit({ registered: true, stopRequested: false, quitReported: true }, 143, null) // true
|
|
378
382
|
*/
|
|
379
|
-
function isUnexpectedExit(record:
|
|
383
|
+
function isUnexpectedExit(record: ProcessRecord, code: number | null, signal: NodeJS.Signals | null): boolean {
|
|
380
384
|
const selfQuit = code === 0 && signal === null && record.quitReported;
|
|
381
385
|
return record.registered && !record.stopRequested && !selfQuit;
|
|
382
386
|
}
|
|
@@ -563,6 +567,7 @@ function childEnvironmentOverrides(team: TeamState, teammate: TeammateState, par
|
|
|
563
567
|
PI_SIMPLE_TEAM_TEAM: team.id ?? team.name,
|
|
564
568
|
PI_SIMPLE_TEAM_TEAM_NAME: team.name,
|
|
565
569
|
PI_SIMPLE_TEAM_MEMBER: teammate.name,
|
|
570
|
+
PI_SIMPLE_TEAM_PROCESS: teammate.processRecord!.id,
|
|
566
571
|
PI_SIMPLE_TEAM_PARTICIPANTS: JSON.stringify(participants),
|
|
567
572
|
PI_SIMPLE_TEAM_CAN_MANAGE_OWN_TEAMS: teammate.canManageOwnTeams ? "1" : "0",
|
|
568
573
|
};
|
|
@@ -617,9 +622,9 @@ function attachRpcTeammate(team: TeamState, teammate: TeammateState, participant
|
|
|
617
622
|
stdio: ["pipe", "ignore", "pipe"],
|
|
618
623
|
env: { ...process.env, ...childEnvironmentOverrides(team, teammate, participants) },
|
|
619
624
|
});
|
|
620
|
-
const record
|
|
625
|
+
const record = teammate.processRecord!;
|
|
626
|
+
const rejectReady = teammate.rejectReady;
|
|
621
627
|
teammate.process = proc;
|
|
622
|
-
teammate.processRecord = record;
|
|
623
628
|
teammate.alive = true;
|
|
624
629
|
|
|
625
630
|
proc.stderr?.on("data", (chunk: Buffer | string) => {
|
|
@@ -628,7 +633,7 @@ function attachRpcTeammate(team: TeamState, teammate: TeammateState, participant
|
|
|
628
633
|
});
|
|
629
634
|
proc.on("exit", (code, signal) => {
|
|
630
635
|
teammate.alive = false;
|
|
631
|
-
|
|
636
|
+
rejectReady?.(new Error(`${teammate.name} exited (code=${code}, signal=${signal})`));
|
|
632
637
|
team.statuses.set(teammate.name, status("stopped", `Exited code=${code} signal=${signal}`));
|
|
633
638
|
appendTeamLog(team, { team: team.name, teammate: teammate.name, direction: "runtime", kind: "exit", summary: `exited (code=${code}, signal=${signal})`, details: { code, signal } });
|
|
634
639
|
if (isUnexpectedExit(record, code, signal)) scheduleExitNotice(team, teammate, code, signal);
|
|
@@ -711,6 +716,9 @@ async function startTeammate(
|
|
|
711
716
|
startOptions: ChildStartOptions = {},
|
|
712
717
|
): Promise<void> {
|
|
713
718
|
startOptions.signal?.throwIfAborted();
|
|
719
|
+
// A Pi session has no lock against a second writer, so a restart ends the previous process before it starts the next.
|
|
720
|
+
await stopRpcTeammate(teammate);
|
|
721
|
+
teammate.processRecord = { id: crypto.randomUUID(), registered: false, stopRequested: false, quitReported: false };
|
|
714
722
|
if (teammate.transport === "herdr") await attachHerdrTeammate(team, teammate, participants, herdrParentPaneId!, startOptions);
|
|
715
723
|
else attachRpcTeammate(team, teammate, participants, startOptions);
|
|
716
724
|
await awaitChildRegistration(teammate, startOptions.signal);
|
|
@@ -762,7 +770,7 @@ async function closeHerdrPane(teammate: TeammateState): Promise<void> {
|
|
|
762
770
|
|
|
763
771
|
async function stopRpcTeammate(teammate: TeammateState): Promise<void> {
|
|
764
772
|
const processToStop = teammate.process;
|
|
765
|
-
if (!processToStop ||
|
|
773
|
+
if (!processToStop || processToStop.exitCode !== null || processToStop.signalCode !== null) return;
|
|
766
774
|
|
|
767
775
|
await new Promise<void>((resolve) => {
|
|
768
776
|
let forceKillTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -919,7 +927,7 @@ function handleChildEvent(team: TeamState, teammate: TeammateState, event: JsonR
|
|
|
919
927
|
teammate.alive = false;
|
|
920
928
|
teammate.deliveryUrl = undefined;
|
|
921
929
|
if (event.reason === "quit") {
|
|
922
|
-
|
|
930
|
+
teammate.processRecord!.quitReported = true;
|
|
923
931
|
team.statuses.set(teammate.name, status("stopped", "Session shut down"));
|
|
924
932
|
appendTeamLog(team, { team: team.name, teammate: teammate.name, direction: "runtime", kind: "exit", summary: "session shut down", details: { reason: event.reason } });
|
|
925
933
|
}
|
|
@@ -942,6 +950,7 @@ async function handleCallbackRequest(request: http.IncomingMessage, response: ht
|
|
|
942
950
|
const tool = String(body.tool);
|
|
943
951
|
const args = (body.args ?? {}) as JsonRecord;
|
|
944
952
|
const teammate = team.members.get(from);
|
|
953
|
+
if (teammate && body.process !== teammate.processRecord?.id) throw new Error(`Teammate ${from} on team ${team.name} was restarted. This process is no longer that teammate.`);
|
|
945
954
|
|
|
946
955
|
if (tool === "register") {
|
|
947
956
|
if (!teammate) throw new Error(`Unknown teammate: ${from}`);
|
|
@@ -966,7 +975,7 @@ async function handleCallbackRequest(request: http.IncomingMessage, response: ht
|
|
|
966
975
|
teammate.alive = true;
|
|
967
976
|
teammate.active = false;
|
|
968
977
|
team.statuses.set(teammate.name, status("idle", "Spawned"));
|
|
969
|
-
|
|
978
|
+
teammate.processRecord!.registered = true;
|
|
970
979
|
teammate.resolveReady?.();
|
|
971
980
|
writeJson(response, 200, { accepted: true, team: team.name, from });
|
|
972
981
|
return;
|