@chankov/agent-fleet 0.0.1 → 0.0.2

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.
@@ -23,7 +23,8 @@ import * as path from "node:path";
23
23
  import { fileURLToPath } from "node:url";
24
24
 
25
25
  import { buildTeamLayout, parsePeersYaml, type Peer } from "./lib/herdr-layout.ts";
26
- import { DEFAULT_PROJECT, hubCommand, parseProjectFlag, teamSnapshotPath, teamWorkspaceLabel, validateTeamName } from "./lib/team-project.ts";
26
+ import { planSpawnDelays } from "./lib/spawn-stagger.ts";
27
+ import { DEFAULT_PROJECT, hubCommand, parseProjectFlag, teamSnapshotPath, teamWorkspaceLabel, validateTeamName, worktreeTag } from "./lib/team-project.ts";
27
28
  import {
28
29
  assertSnapshotProject,
29
30
  buildSnapshot,
@@ -35,6 +36,8 @@ import {
35
36
 
36
37
  const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
37
38
  const REPO_ROOT = path.resolve(SCRIPT_DIR, "..");
39
+ // Must match team-up's tag so down/resume target the workspace it created.
40
+ const WORKTREE_TAG = worktreeTag(REPO_ROOT);
38
41
  const PEERS_YAML = path.join(REPO_ROOT, ".pi", "agents", "peers.yaml");
39
42
  const SNAPSHOT_DIR = path.join(os.homedir(), ".pi", "team-snapshots");
40
43
 
@@ -68,8 +71,8 @@ async function findTeamWorkspace(client: Client, team: string, project = DEFAULT
68
71
  let peersLabel: string;
69
72
  let hubLabel: string;
70
73
  try {
71
- peersLabel = teamWorkspaceLabel("peers", team, project);
72
- hubLabel = teamWorkspaceLabel("hub", team, project);
74
+ peersLabel = teamWorkspaceLabel("peers", team, project, WORKTREE_TAG);
75
+ hubLabel = teamWorkspaceLabel("hub", team, project, WORKTREE_TAG);
73
76
  } catch (err) {
74
77
  die(err instanceof Error ? err.message : String(err));
75
78
  }
@@ -78,8 +81,8 @@ async function findTeamWorkspace(client: Client, team: string, project = DEFAULT
78
81
  }
79
82
 
80
83
  async function captureSnapshot(client: Client, team: string, project = DEFAULT_PROJECT): Promise<TeamSnapshot> {
81
- const peersLabel = teamWorkspaceLabel("peers", team, project);
82
- const hubLabel = teamWorkspaceLabel("hub", team, project);
84
+ const peersLabel = teamWorkspaceLabel("peers", team, project, WORKTREE_TAG);
85
+ const hubLabel = teamWorkspaceLabel("hub", team, project, WORKTREE_TAG);
83
86
  const ws = await findTeamWorkspace(client, team, project);
84
87
  if (!ws) {
85
88
  const projectArgs = project === DEFAULT_PROJECT ? "" : ` --project ${project}`;
@@ -104,6 +107,7 @@ async function captureSnapshot(client: Client, team: string, project = DEFAULT_P
104
107
  peers,
105
108
  panes,
106
109
  agents: inWorkspace as Parameters<typeof buildSnapshot>[0]["agents"],
110
+ tag: WORKTREE_TAG,
107
111
  });
108
112
  const file = snapshotPath(team, project);
109
113
  fs.mkdirSync(path.dirname(file), { recursive: true });
@@ -165,12 +169,25 @@ async function cmdResume(team: string, project = DEFAULT_PROJECT): Promise<void>
165
169
  if (ref && peer.name) resumedNames.add(peer.name);
166
170
  return ref;
167
171
  };
168
- const workspaceLabel = teamWorkspaceLabel(snap.hub ? "hub" : "peers", snap.team, project);
172
+ const workspaceLabel = teamWorkspaceLabel(snap.hub ? "hub" : "peers", snap.team, project, WORKTREE_TAG);
173
+ // Resume relaunches every pane simultaneously, so it needs the same
174
+ // pre-warm/stagger as team-up (see scripts/lib/spawn-stagger.ts).
175
+ let authRaw: string | undefined;
176
+ try {
177
+ authRaw = fs.readFileSync(path.join(os.homedir(), ".pi", "agent", "auth.json"), "utf-8");
178
+ } catch {
179
+ authRaw = undefined;
180
+ }
181
+ const { needed: staggerNeeded, delayForPeer } = planSpawnDelays(snap.peers, snap.hub, authRaw);
182
+ if (staggerNeeded) {
183
+ console.log("Stale pi OAuth token detected — staggering pane starts so one pane can refresh it first.");
184
+ }
169
185
  const layout = buildTeamLayout({
170
186
  team: snap.team,
171
187
  peers: snap.peers,
172
188
  repoRoot: REPO_ROOT,
173
189
  resumeForPeer: resumeFor,
190
+ delayForPeer,
174
191
  project,
175
192
  ...(snap.hub ? { hub: { command: hubCommand(project), label: "hub", ratio: 0.4 } } : {}),
176
193
  });
@@ -22,6 +22,7 @@
22
22
  // - Never clobber an existing workspace of the same label; refuse and explain.
23
23
 
24
24
  import * as fs from "node:fs";
25
+ import * as os from "node:os";
25
26
  import * as path from "node:path";
26
27
  import { fileURLToPath } from "node:url";
27
28
 
@@ -33,10 +34,14 @@ import {
33
34
  type LayoutNode,
34
35
  type Peer,
35
36
  } from "./lib/herdr-layout.ts";
36
- import { DEFAULT_PROJECT, conductorCommand, hubCommand, parseProjectFlag, teamWorkspaceLabel, validateTeamName } from "./lib/team-project.ts";
37
+ import { planSpawnDelays } from "./lib/spawn-stagger.ts";
38
+ import { DEFAULT_PROJECT, conductorCommand, hubCommand, parseProjectFlag, teamWorkspaceLabel, validateTeamName, worktreeTag } from "./lib/team-project.ts";
37
39
 
38
40
  const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url));
39
41
  const REPO_ROOT = path.resolve(SCRIPT_DIR, "..");
42
+ // Scopes the workspace label to this checkout so the same team from another
43
+ // repo/worktree gets its own workspace instead of colliding (see worktreeTag).
44
+ const WORKTREE_TAG = worktreeTag(REPO_ROOT);
40
45
  const DEFAULT_PEERS_YAML = path.join(REPO_ROOT, ".pi", "agents", "peers.yaml");
41
46
 
42
47
  // The guarded hub or Hermes conductor occupies this share of the workspace in
@@ -101,12 +106,35 @@ function makeEnvLoader(
101
106
  };
102
107
  }
103
108
 
109
+ // Pre-warm/stagger (see scripts/lib/spawn-stagger.ts): when auth.json holds a
110
+ // stale OAuth token, simultaneous pi boots race on its file lock and the
111
+ // losers come up with every provider "unconfigured". One pi pane (the hub
112
+ // when present, else the first pi peer) starts immediately and refreshes the
113
+ // token; the others get a small AGENT_FLEET_SPAWN_DELAY via pane env. The
114
+ // auth file is only read for `type`/`expires` — values never leave this
115
+ // process. Dry-run never reaches this (no live-state reads on that path).
116
+ function makeDelayLoader(peers: Peer[], mode: "peers" | "hub" | "conductor"): (peer: Peer) => number | undefined {
117
+ let raw: string | undefined;
118
+ try {
119
+ raw = fs.readFileSync(path.join(os.homedir(), ".pi", "agent", "auth.json"), "utf-8");
120
+ } catch {
121
+ raw = undefined;
122
+ }
123
+ // The conductor root is Hermes, not pi — the first pi peer warms instead.
124
+ const { needed, delayForPeer } = planSpawnDelays(peers, mode === "hub", raw);
125
+ if (needed) {
126
+ console.log("Stale pi OAuth token detected — staggering pane starts so one pane can refresh it first.");
127
+ }
128
+ return delayForPeer;
129
+ }
130
+
104
131
  function buildLayoutOrDie(
105
132
  team: string,
106
133
  peers: Peer[],
107
134
  envForPeer: (p: Peer) => Record<string, string> | undefined,
108
135
  mode: "peers" | "hub" | "conductor",
109
136
  project: string,
137
+ delayForPeer?: (p: Peer) => number | undefined,
110
138
  ): LayoutNode {
111
139
  try {
112
140
  const rootPane = mode === "hub"
@@ -120,6 +148,7 @@ function buildLayoutOrDie(
120
148
  repoRoot: REPO_ROOT,
121
149
  envForPeer,
122
150
  project,
151
+ ...(delayForPeer ? { delayForPeer } : {}),
123
152
  ...(rootPane ? { hub: rootPane } : {}),
124
153
  });
125
154
  } catch (err) {
@@ -159,7 +188,7 @@ async function main(): Promise<void> {
159
188
  const peers = loadTeam(team, peersYaml);
160
189
  let label: string;
161
190
  try {
162
- label = teamWorkspaceLabel(mode, team, project);
191
+ label = teamWorkspaceLabel(mode, team, project, WORKTREE_TAG);
163
192
  } catch (err) {
164
193
  die(err instanceof Error ? err.message : String(err));
165
194
  }
@@ -182,7 +211,7 @@ async function main(): Promise<void> {
182
211
  return;
183
212
  }
184
213
 
185
- const layout = buildLayoutOrDie(team, peers, makeEnvLoader(peers, false), mode, project);
214
+ const layout = buildLayoutOrDie(team, peers, makeEnvLoader(peers, false), mode, project, makeDelayLoader(peers, mode));
186
215
 
187
216
  // Import lazily so --dry-run never touches the client (or the socket).
188
217
  const { herdr, requireHerdr, HerdrUnavailableError } = await import(