@giladbarnea/pi-simple-team 2.0.14 → 2.0.16
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/README.md +1 -0
- package/child-tools.ts +9 -3
- package/index.ts +45 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -148,6 +148,7 @@ Your main uses `team_log` when it needs to understand the chain of events in hig
|
|
|
148
148
|
|
|
149
149
|
Provides main a timestamped, filterable, append-only record of the team’s messages, tool calls, and lifecycle events.
|
|
150
150
|
Attempts can end before retries finish. A separate run row reports final settlement as ended or cancelled. Ended does not imply success.
|
|
151
|
+
A run that ends on a provider error adds an error row with the provider's message and sets the teammate's status to `error` with that message, until a later run completes.
|
|
151
152
|
|
|
152
153
|
### 🌅 Herdr Support
|
|
153
154
|
|
package/child-tools.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as http from "node:http";
|
|
2
|
-
import { defineTool, getMarkdownTheme, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { defineTool, getMarkdownTheme, type AgentActivityOutcome, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { Type } from "typebox";
|
|
4
4
|
import { formatContextWindowReport, requireKnownContextUsage } from "./context-window.ts";
|
|
5
5
|
import { formatModelReference } from "./model-preflight.ts";
|
|
@@ -146,6 +146,7 @@ function startChildRuntime(pi: ExtensionAPI, config: ChildRuntimeConfig): void {
|
|
|
146
146
|
let idleWaiters: Array<() => void> = [];
|
|
147
147
|
let lifecycleQueue: Promise<void> = Promise.resolve();
|
|
148
148
|
let lifecycleError: Error | undefined;
|
|
149
|
+
let lastTurnOutcome: { outcome: AgentActivityOutcome; errorMessage?: string } = { outcome: "completed" };
|
|
149
150
|
|
|
150
151
|
const notifyParent = (event: JsonRecord): Promise<void> => {
|
|
151
152
|
lifecycleQueue = lifecycleQueue.then(async () => {
|
|
@@ -189,17 +190,22 @@ function startChildRuntime(pi: ExtensionAPI, config: ChildRuntimeConfig): void {
|
|
|
189
190
|
pi.on("agent_start", (event, context) => {
|
|
190
191
|
idle = false;
|
|
191
192
|
activeContext = context;
|
|
193
|
+
lastTurnOutcome = { outcome: "completed" };
|
|
192
194
|
notifyParent({ type: "agent_start" });
|
|
193
195
|
});
|
|
194
196
|
pi.on("agent_end", (event) => {
|
|
195
197
|
notifyParent({ type: "agent_end", messages: event.messages });
|
|
196
198
|
});
|
|
199
|
+
/** Pi settles a run on its last turn's outcome, so a retried attempt that then succeeds settles as completed. */
|
|
197
200
|
pi.on("agent_settled", (event) => {
|
|
198
|
-
notifyParent({ type: "agent_settled", aborted: event.aborted });
|
|
201
|
+
notifyParent({ type: "agent_settled", aborted: event.aborted, ...lastTurnOutcome });
|
|
199
202
|
markIdle();
|
|
200
203
|
});
|
|
201
204
|
const reportRuntimeFacts = (context: ExtensionContext): Promise<void> => notifyParent({ type: "runtime_facts", ...runtimeFacts(context) });
|
|
202
|
-
pi.on("turn_end", (
|
|
205
|
+
pi.on("turn_end", (event, context) => {
|
|
206
|
+
lastTurnOutcome = { outcome: event.outcome, errorMessage: event.message.role === "assistant" ? event.message.errorMessage : undefined };
|
|
207
|
+
void reportRuntimeFacts(context);
|
|
208
|
+
});
|
|
203
209
|
pi.on("session_compact", (_event, context) => { void reportRuntimeFacts(context); });
|
|
204
210
|
pi.on("model_select", (_event, context) => { void reportRuntimeFacts(context); });
|
|
205
211
|
pi.on("thinking_level_select", (_event, context) => { void reportRuntimeFacts(context); });
|
package/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import * as fs from "node:fs";
|
|
|
5
5
|
import http from "node:http";
|
|
6
6
|
import * as path from "node:path";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
|
-
import { defineTool, type ContextUsage, type ExtensionAPI, getMarkdownTheme, getPackageDir } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { defineTool, type ContextUsage, type ExtensionAPI, type ExtensionContext, getMarkdownTheme, getPackageDir } from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import { formatContextWindowReport, requireKnownContextUsage, type KnownContextUsage } from "./context-window.ts";
|
|
10
10
|
import { formatScopedModelGuidance, validateTeammateModels, type ModelReference } from "./model-preflight.ts";
|
|
11
11
|
import { composeTeamBriefing } from "./team-briefing.ts";
|
|
@@ -42,6 +42,12 @@ interface TeamStatus {
|
|
|
42
42
|
|
|
43
43
|
type TeammateTransport = "rpc" | "herdr";
|
|
44
44
|
|
|
45
|
+
/** The error status a failed run set, and the status it replaced. */
|
|
46
|
+
interface RunFailure {
|
|
47
|
+
failure: TeamStatus;
|
|
48
|
+
replaced: TeamStatus;
|
|
49
|
+
}
|
|
50
|
+
|
|
45
51
|
interface TeammateState {
|
|
46
52
|
name: string;
|
|
47
53
|
prompt: string;
|
|
@@ -64,6 +70,7 @@ interface TeammateState {
|
|
|
64
70
|
alive: boolean;
|
|
65
71
|
active: boolean;
|
|
66
72
|
contextPercent?: number;
|
|
73
|
+
runFailure?: RunFailure;
|
|
67
74
|
pendingTurnDeliveries: number;
|
|
68
75
|
deliveryQueue: Promise<void>;
|
|
69
76
|
}
|
|
@@ -795,9 +802,30 @@ function applyRuntimeFacts(teammate: TeammateState, facts: JsonRecord): void {
|
|
|
795
802
|
teammate.contextPercent = (facts.contextPercent as number | null) ?? undefined;
|
|
796
803
|
}
|
|
797
804
|
|
|
805
|
+
/**
|
|
806
|
+
* A failed run shows through the same error status and log row as a failed delivery. A later completed run restores the status the failure replaced, unless the teammate has set its own since.
|
|
807
|
+
* A cancelled run is neither: Pi can record an aborted run's last turn as an error, so the final cancellation flag decides.
|
|
808
|
+
*/
|
|
809
|
+
function recordRunOutcome(team: TeamState, teammate: TeammateState, event: JsonRecord): void {
|
|
810
|
+
if (event.aborted === true) return;
|
|
811
|
+
const current = team.statuses.get(teammate.name)!;
|
|
812
|
+
if (event.outcome === "error") {
|
|
813
|
+
const errorMessage = String(event.errorMessage);
|
|
814
|
+
const failure = status("error", errorMessage);
|
|
815
|
+
teammate.runFailure = { failure, replaced: current === teammate.runFailure?.failure ? teammate.runFailure.replaced : current };
|
|
816
|
+
team.statuses.set(teammate.name, failure);
|
|
817
|
+
appendTeamLog(team, { team: team.name, teammate: teammate.name, direction: "runtime", kind: "error", summary: preview(`${teammate.name} run failed: ${errorMessage}`), details: { error: errorMessage } });
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (event.outcome !== "completed" || !teammate.runFailure) return;
|
|
821
|
+
if (current === teammate.runFailure.failure) team.statuses.set(teammate.name, teammate.runFailure.replaced);
|
|
822
|
+
teammate.runFailure = undefined;
|
|
823
|
+
}
|
|
824
|
+
|
|
798
825
|
function handleChildEvent(team: TeamState, teammate: TeammateState, event: JsonRecord): void {
|
|
799
826
|
if (event.type === "agent_start" || event.type === "work_queued") teammate.active = true;
|
|
800
827
|
if (event.type === "agent_settled" || event.type === "session_shutdown") teammate.active = false;
|
|
828
|
+
if (event.type === "agent_settled") recordRunOutcome(team, teammate, event);
|
|
801
829
|
if (event.type === "runtime_facts") applyRuntimeFacts(teammate, event);
|
|
802
830
|
if (event.type === "session_shutdown") {
|
|
803
831
|
teammate.alive = false;
|
|
@@ -1003,6 +1031,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
1003
1031
|
const owner = Symbol("pi-simple-team-owner");
|
|
1004
1032
|
const reminderTimers = new Set<ReturnType<typeof setTimeout>>();
|
|
1005
1033
|
const sessionTeammateRoster = childRuntimeConfig?.participants ?? [];
|
|
1034
|
+
|
|
1035
|
+
/** Current-project manifests that team_resume can select: all for main, only its own for a managing teammate. */
|
|
1036
|
+
function resumableTeamManifests(context: ExtensionContext, toolName: string): TeamManifest[] {
|
|
1037
|
+
const projectDirectory = context.sessionManager?.getCwd?.() ?? context.cwd;
|
|
1038
|
+
if (!projectDirectory) throw new Error(`${toolName} requires a project directory`);
|
|
1039
|
+
const managerSessionId = childRuntimeConfig ? context.sessionManager?.getSessionId?.() : undefined;
|
|
1040
|
+
if (childRuntimeConfig && !managerSessionId) throw new Error(`${toolName} requires a persistent managing teammate Pi session`);
|
|
1041
|
+
return listTeamManifests(projectDirectory).manifests.filter(
|
|
1042
|
+
(candidate) => !managerSessionId || candidate.originMainSessionId === managerSessionId,
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1006
1046
|
pi.registerMessageRenderer(teamMessageType, (message, options, theme) => renderTeamMessage(message, options, theme, getMarkdownTheme(), sessionTeammateRoster));
|
|
1007
1047
|
|
|
1008
1048
|
pi.registerCommand("team", {
|
|
@@ -1229,14 +1269,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
1229
1269
|
}, { additionalProperties: false }),
|
|
1230
1270
|
async execute(_toolCallId, params, signal, _onUpdate, context) {
|
|
1231
1271
|
signal?.throwIfAborted();
|
|
1232
|
-
const
|
|
1233
|
-
if (!rawProjectDirectory) throw new Error("team_resume requires a project directory");
|
|
1234
|
-
const managerSessionId = childRuntimeConfig ? context.sessionManager?.getSessionId?.() : undefined;
|
|
1235
|
-
if (childRuntimeConfig && !managerSessionId) throw new Error("team_resume requires a persistent managing teammate Pi session");
|
|
1236
|
-
const availableManifests = listTeamManifests(rawProjectDirectory).manifests.filter(
|
|
1237
|
-
(candidate) => !managerSessionId || candidate.originMainSessionId === managerSessionId,
|
|
1238
|
-
);
|
|
1239
|
-
const manifest = resolveTeamIdentifier(availableManifests, params.team);
|
|
1272
|
+
const manifest = resolveTeamIdentifier(resumableTeamManifests(context, "team_resume"), params.team);
|
|
1240
1273
|
if (!manifest) throw new Error(`Unknown current-project team: ${JSON.stringify(params.team)}. Call team_list and pass a listed teamId or unique teamName in the team parameter.`);
|
|
1241
1274
|
const members = manifest.members;
|
|
1242
1275
|
const requestedNames = resolveTeammates(members, params.teammates ?? members.map((member) => member.teammateId)).map((member) => member.name);
|
|
@@ -1339,6 +1372,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
1339
1372
|
}, { additionalProperties: false }),
|
|
1340
1373
|
async execute(_toolCallId, params, signal, _onUpdate, context) {
|
|
1341
1374
|
signal?.throwIfAborted();
|
|
1375
|
+
const ownsSelectedTeam = !params.team || resolveTeamIdentifier([...teams.values()].filter((team) => team.owner === owner), params.team);
|
|
1376
|
+
const stoppedTeam = ownsSelectedTeam ? undefined : resolveTeamIdentifier(resumableTeamManifests(context, "team_add_teammates").filter((manifest) => manifest.state === "dormant"), params.team!);
|
|
1377
|
+
if (stoppedTeam) throw new Error(`Team "${stoppedTeam.name}" (team ID: ${stoppedTeam.id}) is stopped. Resume it with team_resume before adding teammates.`);
|
|
1342
1378
|
const team = resolveTeam(owner, params.team);
|
|
1343
1379
|
if (!team || !team.manifest || !team.lease || team.manifest.state !== "active") {
|
|
1344
1380
|
throw new Error(`team_add_teammates requires a running team owned by this main session: ${params.team}`);
|