@tintinweb/pi-subagents 0.6.3 → 0.7.0
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/CHANGELOG.md +22 -0
- package/README.md +54 -10
- package/dist/agent-manager.d.ts +23 -1
- package/dist/agent-manager.js +33 -2
- package/dist/agent-runner.d.ts +27 -0
- package/dist/agent-runner.js +28 -4
- package/dist/index.js +199 -50
- package/dist/schedule-store.d.ts +36 -0
- package/dist/schedule-store.js +144 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +10 -0
- package/dist/settings.js +5 -0
- package/dist/types.d.ts +46 -0
- package/dist/ui/agent-widget.d.ts +15 -8
- package/dist/ui/agent-widget.js +28 -7
- package/dist/ui/conversation-viewer.js +6 -8
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +95 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/package.json +10 -6
- package/src/agent-manager.ts +55 -2
- package/src/agent-runner.ts +43 -5
- package/src/index.ts +207 -41
- package/src/schedule-store.ts +143 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +14 -0
- package/src/types.ts +52 -0
- package/src/ui/agent-widget.ts +36 -6
- package/src/ui/conversation-viewer.ts +6 -6
- package/src/ui/schedule-menu.ts +104 -0
- package/src/usage.ts +60 -0
- package/.github/workflows/ci.yml +0 -21
- package/biome.json +0 -26
- package/dist/ui/conversation-viewer.test.d.ts +0 -1
- package/dist/ui/conversation-viewer.test.js +0 -254
package/src/index.ts
CHANGED
|
@@ -24,6 +24,8 @@ import { GroupJoinManager } from "./group-join.js";
|
|
|
24
24
|
import { resolveAgentInvocationConfig, resolveJoinMode } from "./invocation-config.js";
|
|
25
25
|
import { type ModelRegistry, resolveModel } from "./model-resolver.js";
|
|
26
26
|
import { createOutputFilePath, streamToOutputFile, writeInitialEntry } from "./output-file.js";
|
|
27
|
+
import { SubagentScheduler } from "./schedule.js";
|
|
28
|
+
import { resolveStorePath, ScheduleStore } from "./schedule-store.js";
|
|
27
29
|
import { applyAndEmitLoaded, type SubagentsSettings, saveAndEmitChanged } from "./settings.js";
|
|
28
30
|
import { type AgentConfig, type AgentRecord, type JoinMode, type NotificationDetails, type SubagentType } from "./types.js";
|
|
29
31
|
import {
|
|
@@ -40,6 +42,8 @@ import {
|
|
|
40
42
|
SPINNER,
|
|
41
43
|
type UICtx,
|
|
42
44
|
} from "./ui/agent-widget.js";
|
|
45
|
+
import { showSchedulesMenu } from "./ui/schedule-menu.js";
|
|
46
|
+
import { addUsage, getLifetimeTotal, getSessionContextPercent, type LifetimeUsage } from "./usage.js";
|
|
43
47
|
|
|
44
48
|
// ---- Shared helpers ----
|
|
45
49
|
|
|
@@ -48,10 +52,10 @@ function textResult(msg: string, details?: AgentDetails) {
|
|
|
48
52
|
return { content: [{ type: "text" as const, text: msg }], details: details as any };
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
/**
|
|
52
|
-
function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
/** Format an agent's lifetime token total, or "" when zero. */
|
|
56
|
+
function formatLifetimeTokens(o: { lifetimeUsage: LifetimeUsage }): string {
|
|
57
|
+
const t = getLifetimeTotal(o.lifetimeUsage);
|
|
58
|
+
return t > 0 ? formatTokens(t) : "";
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
/**
|
|
@@ -59,7 +63,15 @@ function safeFormatTokens(session: { getSessionStats(): { tokens: { total: numbe
|
|
|
59
63
|
* Used by both foreground and background paths to avoid duplication.
|
|
60
64
|
*/
|
|
61
65
|
function createActivityTracker(maxTurns?: number, onStreamUpdate?: () => void) {
|
|
62
|
-
const state: AgentActivity = {
|
|
66
|
+
const state: AgentActivity = {
|
|
67
|
+
activeTools: new Map(),
|
|
68
|
+
toolUses: 0,
|
|
69
|
+
turnCount: 1,
|
|
70
|
+
maxTurns,
|
|
71
|
+
responseText: "",
|
|
72
|
+
session: undefined,
|
|
73
|
+
lifetimeUsage: { input: 0, output: 0, cacheWrite: 0 },
|
|
74
|
+
};
|
|
63
75
|
|
|
64
76
|
const callbacks = {
|
|
65
77
|
onToolActivity: (activity: { type: "start" | "end"; toolName: string }) => {
|
|
@@ -71,7 +83,6 @@ function createActivityTracker(maxTurns?: number, onStreamUpdate?: () => void) {
|
|
|
71
83
|
}
|
|
72
84
|
state.toolUses++;
|
|
73
85
|
}
|
|
74
|
-
state.tokens = safeFormatTokens(state.session);
|
|
75
86
|
onStreamUpdate?.();
|
|
76
87
|
},
|
|
77
88
|
onTextDelta: (_delta: string, fullText: string) => {
|
|
@@ -85,6 +96,10 @@ function createActivityTracker(maxTurns?: number, onStreamUpdate?: () => void) {
|
|
|
85
96
|
onSessionCreated: (session: any) => {
|
|
86
97
|
state.session = session;
|
|
87
98
|
},
|
|
99
|
+
onAssistantUsage: (usage: { input: number; output: number; cacheWrite: number }) => {
|
|
100
|
+
addUsage(state.lifetimeUsage, usage);
|
|
101
|
+
onStreamUpdate?.();
|
|
102
|
+
},
|
|
88
103
|
};
|
|
89
104
|
|
|
90
105
|
return { state, callbacks };
|
|
@@ -120,13 +135,10 @@ function escapeXml(s: string): string {
|
|
|
120
135
|
function formatTaskNotification(record: AgentRecord, resultMaxLen: number): string {
|
|
121
136
|
const status = getStatusLabel(record.status, record.error);
|
|
122
137
|
const durationMs = record.completedAt ? record.completedAt - record.startedAt : 0;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
totalTokens = stats.tokens?.total ?? 0;
|
|
128
|
-
}
|
|
129
|
-
} catch { /* session stats unavailable */ }
|
|
138
|
+
const totalTokens = getLifetimeTotal(record.lifetimeUsage);
|
|
139
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
140
|
+
const ctxXml = contextPercent !== null ? `<context_percent>${Math.round(contextPercent)}</context_percent>` : "";
|
|
141
|
+
const compactXml = record.compactionCount ? `<compactions>${record.compactionCount}</compactions>` : "";
|
|
130
142
|
|
|
131
143
|
const resultPreview = record.result
|
|
132
144
|
? record.result.length > resultMaxLen
|
|
@@ -142,7 +154,7 @@ function formatTaskNotification(record: AgentRecord, resultMaxLen: number): stri
|
|
|
142
154
|
`<status>${escapeXml(status)}</status>`,
|
|
143
155
|
`<summary>Agent "${escapeXml(record.description)}" ${record.status}</summary>`,
|
|
144
156
|
`<result>${escapeXml(resultPreview)}</result>`,
|
|
145
|
-
`<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses
|
|
157
|
+
`<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses>${ctxXml}${compactXml}<duration_ms>${durationMs}</duration_ms></usage>`,
|
|
146
158
|
`</task-notification>`,
|
|
147
159
|
].filter(Boolean).join('\n');
|
|
148
160
|
}
|
|
@@ -150,14 +162,14 @@ function formatTaskNotification(record: AgentRecord, resultMaxLen: number): stri
|
|
|
150
162
|
/** Build AgentDetails from a base + record-specific fields. */
|
|
151
163
|
function buildDetails(
|
|
152
164
|
base: Pick<AgentDetails, "displayName" | "description" | "subagentType" | "modelName" | "tags">,
|
|
153
|
-
record: { toolUses: number; startedAt: number; completedAt?: number; status: string; error?: string; id?: string; session?: any },
|
|
165
|
+
record: { toolUses: number; startedAt: number; completedAt?: number; status: string; error?: string; id?: string; session?: any; lifetimeUsage: LifetimeUsage },
|
|
154
166
|
activity?: AgentActivity,
|
|
155
167
|
overrides?: Partial<AgentDetails>,
|
|
156
168
|
): AgentDetails {
|
|
157
169
|
return {
|
|
158
170
|
...base,
|
|
159
171
|
toolUses: record.toolUses,
|
|
160
|
-
tokens:
|
|
172
|
+
tokens: formatLifetimeTokens(record),
|
|
161
173
|
turnCount: activity?.turnCount,
|
|
162
174
|
maxTurns: activity?.maxTurns,
|
|
163
175
|
durationMs: (record.completedAt ?? Date.now()) - record.startedAt,
|
|
@@ -170,10 +182,7 @@ function buildDetails(
|
|
|
170
182
|
|
|
171
183
|
/** Build notification details for the custom message renderer. */
|
|
172
184
|
function buildNotificationDetails(record: AgentRecord, resultMaxLen: number, activity?: AgentActivity): NotificationDetails {
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
if (record.session) totalTokens = record.session.getSessionStats().tokens?.total ?? 0;
|
|
176
|
-
} catch {}
|
|
185
|
+
const totalTokens = getLifetimeTotal(record.lifetimeUsage);
|
|
177
186
|
|
|
178
187
|
return {
|
|
179
188
|
id: record.id,
|
|
@@ -337,17 +346,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
337
346
|
/** Helper: build event data for lifecycle events from an AgentRecord. */
|
|
338
347
|
function buildEventData(record: AgentRecord) {
|
|
339
348
|
const durationMs = record.completedAt ? record.completedAt - record.startedAt : Date.now() - record.startedAt;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
}
|
|
350
|
-
} catch { /* session stats unavailable */ }
|
|
349
|
+
// All three fields are lifetime-accumulated (Σ over every assistant message_end),
|
|
350
|
+
// so they survive compaction together — input + output ≤ total always.
|
|
351
|
+
// tokens is omitted when nothing was ever produced (e.g. agent errored before
|
|
352
|
+
// any message_end fired), preserving prior payload shape.
|
|
353
|
+
const u = record.lifetimeUsage;
|
|
354
|
+
const total = getLifetimeTotal(u);
|
|
355
|
+
const tokens = total > 0
|
|
356
|
+
? { input: u.input, output: u.output, total }
|
|
357
|
+
: undefined;
|
|
351
358
|
return {
|
|
352
359
|
id: record.id,
|
|
353
360
|
type: record.type,
|
|
@@ -408,6 +415,16 @@ export default function (pi: ExtensionAPI) {
|
|
|
408
415
|
type: record.type,
|
|
409
416
|
description: record.description,
|
|
410
417
|
});
|
|
418
|
+
}, (record, info) => {
|
|
419
|
+
// Emit compacted event when agent's session compacts (preserves count on record).
|
|
420
|
+
pi.events.emit("subagents:compacted", {
|
|
421
|
+
id: record.id,
|
|
422
|
+
type: record.type,
|
|
423
|
+
description: record.description,
|
|
424
|
+
reason: info.reason,
|
|
425
|
+
tokensBefore: info.tokensBefore,
|
|
426
|
+
compactionCount: record.compactionCount,
|
|
427
|
+
});
|
|
411
428
|
});
|
|
412
429
|
|
|
413
430
|
// Expose manager via Symbol.for() global registry for cross-package access.
|
|
@@ -424,13 +441,38 @@ export default function (pi: ExtensionAPI) {
|
|
|
424
441
|
// --- Cross-extension RPC via pi.events ---
|
|
425
442
|
let currentCtx: ExtensionContext | undefined;
|
|
426
443
|
|
|
427
|
-
//
|
|
444
|
+
// ---- Subagent scheduler ----
|
|
445
|
+
// Session-scoped: store is constructed inside session_start once sessionId
|
|
446
|
+
// is available. Mirrors pi-chonky-tasks's session-scoped task store —
|
|
447
|
+
// schedules reset on /new, restore on /resume.
|
|
448
|
+
const scheduler = new SubagentScheduler();
|
|
449
|
+
|
|
450
|
+
function startScheduler(ctx: ExtensionContext) {
|
|
451
|
+
try {
|
|
452
|
+
const sessionId = ctx.sessionManager?.getSessionId?.();
|
|
453
|
+
if (!sessionId) return; // sessionId not yet available — try again on next event
|
|
454
|
+
const path = resolveStorePath(ctx.cwd, sessionId);
|
|
455
|
+
const store = new ScheduleStore(path);
|
|
456
|
+
scheduler.start(pi, ctx, manager, store);
|
|
457
|
+
pi.events.emit("subagents:scheduler_ready", { sessionId, jobCount: store.list().length });
|
|
458
|
+
} catch (err) {
|
|
459
|
+
// Scheduling is non-essential — log and move on so the rest of the
|
|
460
|
+
// extension keeps working if e.g. .pi/ is unwritable.
|
|
461
|
+
console.warn("[pi-subagents] Failed to start scheduler:", err);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Capture ctx from session_start for RPC spawn handler + start the scheduler.
|
|
428
466
|
pi.on("session_start", async (_event, ctx) => {
|
|
429
467
|
currentCtx = ctx;
|
|
430
|
-
manager.clearCompleted();
|
|
468
|
+
manager.clearCompleted();
|
|
469
|
+
if (isSchedulingEnabled() && !scheduler.isActive()) startScheduler(ctx);
|
|
431
470
|
});
|
|
432
471
|
|
|
433
|
-
pi.on("session_before_switch", () => {
|
|
472
|
+
pi.on("session_before_switch", () => {
|
|
473
|
+
manager.clearCompleted();
|
|
474
|
+
scheduler.stop();
|
|
475
|
+
});
|
|
434
476
|
|
|
435
477
|
const { unsubPing: unsubPingRpc, unsubSpawn: unsubSpawnRpc, unsubStop: unsubStopRpc } = registerRpcHandlers({
|
|
436
478
|
events: pi.events,
|
|
@@ -450,6 +492,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
450
492
|
unsubPingRpc();
|
|
451
493
|
currentCtx = undefined;
|
|
452
494
|
delete (globalThis as any)[MANAGER_KEY];
|
|
495
|
+
scheduler.stop();
|
|
453
496
|
manager.abortAll();
|
|
454
497
|
for (const timer of pendingNudges.values()) clearTimeout(timer);
|
|
455
498
|
pendingNudges.clear();
|
|
@@ -464,6 +507,16 @@ export default function (pi: ExtensionAPI) {
|
|
|
464
507
|
function getDefaultJoinMode(): JoinMode { return defaultJoinMode; }
|
|
465
508
|
function setDefaultJoinMode(mode: JoinMode) { defaultJoinMode = mode; }
|
|
466
509
|
|
|
510
|
+
// Master switch for the schedule subagent feature. Defaults to enabled.
|
|
511
|
+
// Read once at extension init (before tool registration) so the Agent tool's
|
|
512
|
+
// param schema reflects the persisted setting. Runtime toggles via /agents
|
|
513
|
+
// → Settings short-circuit the menu entry + the execute-time addJob path
|
|
514
|
+
// immediately, but the schema-level removal only takes effect on next
|
|
515
|
+
// extension load (next pi session). Documented in CHANGELOG/README.
|
|
516
|
+
let schedulingEnabled = true;
|
|
517
|
+
function isSchedulingEnabled(): boolean { return schedulingEnabled; }
|
|
518
|
+
function setSchedulingEnabled(b: boolean) { schedulingEnabled = b; }
|
|
519
|
+
|
|
467
520
|
// ---- Batch tracking for smart join mode ----
|
|
468
521
|
// Collects background agent IDs spawned in the current turn for smart grouping.
|
|
469
522
|
// Uses a debounced timer: each new agent resets the 100ms window so that all
|
|
@@ -557,12 +610,35 @@ export default function (pi: ExtensionAPI) {
|
|
|
557
610
|
setDefaultMaxTurns,
|
|
558
611
|
setGraceTurns,
|
|
559
612
|
setDefaultJoinMode,
|
|
613
|
+
setSchedulingEnabled,
|
|
560
614
|
},
|
|
561
615
|
(event, payload) => pi.events.emit(event, payload),
|
|
562
616
|
);
|
|
563
617
|
|
|
564
618
|
// ---- Agent tool ----
|
|
565
619
|
|
|
620
|
+
// Schedule param + its guideline are gated on `schedulingEnabled` (read once
|
|
621
|
+
// at registration; flipping the setting later requires next pi session for
|
|
622
|
+
// the schema to update). Defining the shape once and spreading it via Partial
|
|
623
|
+
// preserves Type.Object's inference when present and produces a
|
|
624
|
+
// `schedule`-free schema when absent — zero LLM-context cost in disabled mode.
|
|
625
|
+
const scheduleParamShape = {
|
|
626
|
+
schedule: Type.Optional(
|
|
627
|
+
Type.String({
|
|
628
|
+
description:
|
|
629
|
+
'Opt-in only — fire later instead of now. Omit to run immediately (the default, almost always correct). ' +
|
|
630
|
+
'Formats: 6-field cron ("0 0 9 * * 1" = 9am Mon), interval ("5m"/"1h"), one-shot ("+10m" or ISO). ' +
|
|
631
|
+
'Forces run_in_background; incompatible with inherit_context and resume. Returns job ID.',
|
|
632
|
+
}),
|
|
633
|
+
),
|
|
634
|
+
};
|
|
635
|
+
const scheduleParam: Partial<typeof scheduleParamShape> =
|
|
636
|
+
isSchedulingEnabled() ? scheduleParamShape : {};
|
|
637
|
+
|
|
638
|
+
const scheduleGuideline = isSchedulingEnabled()
|
|
639
|
+
? `\n- Use \`schedule\` only when the user explicitly asked for scheduled / recurring / delayed execution (e.g. "every Monday", "in an hour"). Don't auto-schedule from vague intent like "monitor X" — run once now or ask.`
|
|
640
|
+
: "";
|
|
641
|
+
|
|
566
642
|
pi.registerTool(defineTool({
|
|
567
643
|
name: "Agent",
|
|
568
644
|
label: "Agent",
|
|
@@ -586,7 +662,7 @@ Guidelines:
|
|
|
586
662
|
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
587
663
|
- Use thinking to control extended thinking level.
|
|
588
664
|
- Use inherit_context if the agent needs the parent conversation history.
|
|
589
|
-
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications)
|
|
665
|
+
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).${scheduleGuideline}`,
|
|
590
666
|
parameters: Type.Object({
|
|
591
667
|
prompt: Type.String({
|
|
592
668
|
description: "The task for the agent to perform.",
|
|
@@ -639,6 +715,7 @@ Guidelines:
|
|
|
639
715
|
description: 'Set to "worktree" to run the agent in a temporary git worktree (isolated copy of the repo). Changes are saved to a branch on completion.',
|
|
640
716
|
}),
|
|
641
717
|
),
|
|
718
|
+
...scheduleParam,
|
|
642
719
|
}),
|
|
643
720
|
|
|
644
721
|
// ---- Custom rendering: Claude Code style ----
|
|
@@ -792,6 +869,47 @@ Guidelines:
|
|
|
792
869
|
tags: agentTags.length > 0 ? agentTags : undefined,
|
|
793
870
|
};
|
|
794
871
|
|
|
872
|
+
// ---- Schedule: register a job, don't spawn now ----
|
|
873
|
+
if (params.schedule) {
|
|
874
|
+
if (!isSchedulingEnabled()) {
|
|
875
|
+
return textResult("Scheduling is disabled in this project. Enable via /agents → Settings → Scheduling.");
|
|
876
|
+
}
|
|
877
|
+
if (params.resume) {
|
|
878
|
+
return textResult("Cannot combine `schedule` with `resume` — schedules create fresh agents.");
|
|
879
|
+
}
|
|
880
|
+
if (params.inherit_context) {
|
|
881
|
+
return textResult("Cannot combine `schedule` with `inherit_context` — there is no parent conversation at fire time.");
|
|
882
|
+
}
|
|
883
|
+
if (params.run_in_background === false) {
|
|
884
|
+
return textResult("Cannot combine `schedule` with `run_in_background: false` — scheduled jobs always run in background.");
|
|
885
|
+
}
|
|
886
|
+
if (!scheduler.isActive()) {
|
|
887
|
+
return textResult("Scheduler is not active in this session yet. Try again after the session has fully started.");
|
|
888
|
+
}
|
|
889
|
+
try {
|
|
890
|
+
const job = scheduler.addJob({
|
|
891
|
+
name: params.description as string,
|
|
892
|
+
description: params.description as string,
|
|
893
|
+
schedule: params.schedule as string,
|
|
894
|
+
subagent_type: subagentType,
|
|
895
|
+
prompt: params.prompt as string,
|
|
896
|
+
model: params.model as string | undefined,
|
|
897
|
+
thinking: thinking,
|
|
898
|
+
max_turns: effectiveMaxTurns,
|
|
899
|
+
isolated: isolated,
|
|
900
|
+
isolation: isolation,
|
|
901
|
+
});
|
|
902
|
+
const next = scheduler.getNextRun(job.id);
|
|
903
|
+
return textResult(
|
|
904
|
+
`Scheduled "${job.name}" (id: ${job.id}, type: ${job.scheduleType}). ` +
|
|
905
|
+
`Next run: ${next ?? "(unknown)"}. ` +
|
|
906
|
+
`Manage via /agents → Scheduled jobs.`,
|
|
907
|
+
);
|
|
908
|
+
} catch (err) {
|
|
909
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
795
913
|
// Resume existing agent
|
|
796
914
|
if (params.resume) {
|
|
797
915
|
const existing = manager.getRecord(params.resume);
|
|
@@ -898,7 +1016,7 @@ Guidelines:
|
|
|
898
1016
|
const details: AgentDetails = {
|
|
899
1017
|
...detailBase,
|
|
900
1018
|
toolUses: fgState.toolUses,
|
|
901
|
-
tokens: fgState
|
|
1019
|
+
tokens: formatLifetimeTokens(fgState),
|
|
902
1020
|
turnCount: fgState.turnCount,
|
|
903
1021
|
maxTurns: fgState.maxTurns,
|
|
904
1022
|
durationMs: Date.now() - startedAt,
|
|
@@ -944,6 +1062,7 @@ Guidelines:
|
|
|
944
1062
|
inheritContext,
|
|
945
1063
|
thinkingLevel: thinking,
|
|
946
1064
|
isolation,
|
|
1065
|
+
signal,
|
|
947
1066
|
...fgCallbacks,
|
|
948
1067
|
});
|
|
949
1068
|
|
|
@@ -956,7 +1075,7 @@ Guidelines:
|
|
|
956
1075
|
}
|
|
957
1076
|
|
|
958
1077
|
// Get final token count
|
|
959
|
-
const tokenText =
|
|
1078
|
+
const tokenText = formatLifetimeTokens(fgState);
|
|
960
1079
|
|
|
961
1080
|
const details = buildDetails(detailBase, record, fgState, { tokens: tokenText });
|
|
962
1081
|
|
|
@@ -1019,12 +1138,17 @@ Guidelines:
|
|
|
1019
1138
|
|
|
1020
1139
|
const displayName = getDisplayName(record.type);
|
|
1021
1140
|
const duration = formatDuration(record.startedAt, record.completedAt);
|
|
1022
|
-
const tokens =
|
|
1023
|
-
const
|
|
1141
|
+
const tokens = formatLifetimeTokens(record);
|
|
1142
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
1143
|
+
const statsParts = [`Tool uses: ${record.toolUses}`];
|
|
1144
|
+
if (tokens) statsParts.push(tokens);
|
|
1145
|
+
if (contextPercent !== null) statsParts.push(`Context: ${Math.round(contextPercent)}%`);
|
|
1146
|
+
if (record.compactionCount) statsParts.push(`Compactions: ${record.compactionCount}`);
|
|
1147
|
+
statsParts.push(`Duration: ${duration}`);
|
|
1024
1148
|
|
|
1025
1149
|
let output =
|
|
1026
1150
|
`Agent: ${record.id}\n` +
|
|
1027
|
-
`Type: ${displayName} | Status: ${record.status} | ${
|
|
1151
|
+
`Type: ${displayName} | Status: ${record.status} | ${statsParts.join(" | ")}\n` +
|
|
1028
1152
|
`Description: ${record.description}\n\n`;
|
|
1029
1153
|
|
|
1030
1154
|
if (record.status === "running") {
|
|
@@ -1088,7 +1212,17 @@ Guidelines:
|
|
|
1088
1212
|
try {
|
|
1089
1213
|
await steerAgent(record.session, params.message);
|
|
1090
1214
|
pi.events.emit("subagents:steered", { id: record.id, message: params.message });
|
|
1091
|
-
|
|
1215
|
+
const tokens = formatLifetimeTokens(record);
|
|
1216
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
1217
|
+
const stateParts: string[] = [];
|
|
1218
|
+
if (tokens) stateParts.push(tokens);
|
|
1219
|
+
stateParts.push(`${record.toolUses} tool ${record.toolUses === 1 ? "use" : "uses"}`);
|
|
1220
|
+
if (contextPercent !== null) stateParts.push(`context ${Math.round(contextPercent)}% full`);
|
|
1221
|
+
if (record.compactionCount) stateParts.push(`${record.compactionCount} compaction${record.compactionCount === 1 ? "" : "s"}`);
|
|
1222
|
+
return textResult(
|
|
1223
|
+
`Steering message sent to agent ${record.id}. The agent will process it after its current tool execution.\n` +
|
|
1224
|
+
`Current state: ${stateParts.join(" · ")}`,
|
|
1225
|
+
);
|
|
1092
1226
|
} catch (err) {
|
|
1093
1227
|
return textResult(`Failed to steer agent: ${err instanceof Error ? err.message : String(err)}`);
|
|
1094
1228
|
}
|
|
@@ -1140,6 +1274,12 @@ Guidelines:
|
|
|
1140
1274
|
options.push(`Agent types (${allNames.length})`);
|
|
1141
1275
|
}
|
|
1142
1276
|
|
|
1277
|
+
// Scheduled jobs entry (always present when scheduler is active)
|
|
1278
|
+
if (scheduler.isActive()) {
|
|
1279
|
+
const jobCount = scheduler.list().length;
|
|
1280
|
+
options.push(`Scheduled jobs (${jobCount})`);
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1143
1283
|
// Actions
|
|
1144
1284
|
options.push("Create new agent");
|
|
1145
1285
|
options.push("Settings");
|
|
@@ -1163,6 +1303,9 @@ Guidelines:
|
|
|
1163
1303
|
} else if (choice.startsWith("Agent types (")) {
|
|
1164
1304
|
await showAllAgentsList(ctx);
|
|
1165
1305
|
await showAgentsMenu(ctx);
|
|
1306
|
+
} else if (choice.startsWith("Scheduled jobs (")) {
|
|
1307
|
+
await showSchedulesMenu(ctx, scheduler);
|
|
1308
|
+
await showAgentsMenu(ctx);
|
|
1166
1309
|
} else if (choice === "Create new agent") {
|
|
1167
1310
|
await showCreateWizard(ctx);
|
|
1168
1311
|
} else if (choice === "Settings") {
|
|
@@ -1626,6 +1769,7 @@ ${systemPrompt}
|
|
|
1626
1769
|
defaultMaxTurns: getDefaultMaxTurns() ?? 0,
|
|
1627
1770
|
graceTurns: getGraceTurns(),
|
|
1628
1771
|
defaultJoinMode: getDefaultJoinMode(),
|
|
1772
|
+
schedulingEnabled: isSchedulingEnabled(),
|
|
1629
1773
|
};
|
|
1630
1774
|
}
|
|
1631
1775
|
|
|
@@ -1635,6 +1779,7 @@ ${systemPrompt}
|
|
|
1635
1779
|
`Default max turns (current: ${getDefaultMaxTurns() ?? "unlimited"})`,
|
|
1636
1780
|
`Grace turns (current: ${getGraceTurns()})`,
|
|
1637
1781
|
`Join mode (current: ${getDefaultJoinMode()})`,
|
|
1782
|
+
`Scheduling (current: ${isSchedulingEnabled() ? "enabled" : "disabled"})`,
|
|
1638
1783
|
]);
|
|
1639
1784
|
if (!choice) return;
|
|
1640
1785
|
|
|
@@ -1685,6 +1830,27 @@ ${systemPrompt}
|
|
|
1685
1830
|
setDefaultJoinMode(mode);
|
|
1686
1831
|
notifyApplied(ctx, `Default join mode set to ${mode}`);
|
|
1687
1832
|
}
|
|
1833
|
+
} else if (choice.startsWith("Scheduling")) {
|
|
1834
|
+
const val = await ctx.ui.select(
|
|
1835
|
+
"Schedule subagent feature",
|
|
1836
|
+
[
|
|
1837
|
+
"enabled — Agent tool accepts a `schedule` param; /agents → Scheduled jobs visible",
|
|
1838
|
+
"disabled — `schedule` removed from Agent tool spec (no LLM-context cost); menu hidden",
|
|
1839
|
+
],
|
|
1840
|
+
);
|
|
1841
|
+
if (val) {
|
|
1842
|
+
const enabled = val.startsWith("enabled");
|
|
1843
|
+
if (enabled === isSchedulingEnabled()) {
|
|
1844
|
+
ctx.ui.notify(`Scheduling already ${enabled ? "enabled" : "disabled"}.`, "info");
|
|
1845
|
+
} else {
|
|
1846
|
+
setSchedulingEnabled(enabled);
|
|
1847
|
+
if (!enabled) scheduler.stop(); // immediate kill — outstanding fires stop ticking
|
|
1848
|
+
notifyApplied(
|
|
1849
|
+
ctx,
|
|
1850
|
+
`Scheduling ${enabled ? "enabled" : "disabled"}. Tool spec change takes effect on next pi session.`,
|
|
1851
|
+
);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1688
1854
|
}
|
|
1689
1855
|
}
|
|
1690
1856
|
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* schedule-store.ts — File-backed store for scheduled subagents.
|
|
3
|
+
*
|
|
4
|
+
* Session-scoped: each pi session owns its own schedules at
|
|
5
|
+
* `<cwd>/.pi/subagent-schedules/<sessionId>.json`. `/new` starts a fresh
|
|
6
|
+
* empty store; `/resume` reloads.
|
|
7
|
+
*
|
|
8
|
+
* Concurrency model lifted from pi-chonky-tasks/src/task-store.ts: every
|
|
9
|
+
* mutation acquires a PID-based exclusion lock, re-reads the latest state
|
|
10
|
+
* from disk, applies the change, atomic-writes via temp+rename, releases.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import type { ScheduledSubagent, ScheduleStoreData } from "./types.js";
|
|
16
|
+
|
|
17
|
+
const LOCK_RETRY_MS = 50;
|
|
18
|
+
const LOCK_MAX_RETRIES = 100;
|
|
19
|
+
|
|
20
|
+
function isProcessRunning(pid: number): boolean {
|
|
21
|
+
try { process.kill(pid, 0); return true; } catch { return false; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function acquireLock(lockPath: string): void {
|
|
25
|
+
for (let i = 0; i < LOCK_MAX_RETRIES; i++) {
|
|
26
|
+
try {
|
|
27
|
+
writeFileSync(lockPath, `${process.pid}`, { flag: "wx" });
|
|
28
|
+
return;
|
|
29
|
+
} catch (e: any) {
|
|
30
|
+
if (e.code === "EEXIST") {
|
|
31
|
+
try {
|
|
32
|
+
const pid = parseInt(readFileSync(lockPath, "utf-8"), 10);
|
|
33
|
+
if (pid && !isProcessRunning(pid)) {
|
|
34
|
+
unlinkSync(lockPath);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
} catch { /* ignore — try again */ }
|
|
38
|
+
const start = Date.now();
|
|
39
|
+
while (Date.now() - start < LOCK_RETRY_MS) { /* busy wait */ }
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`Failed to acquire schedule lock: ${lockPath}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function releaseLock(lockPath: string): void {
|
|
49
|
+
try { unlinkSync(lockPath); } catch { /* ignore */ }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Resolve the storage path for a session-scoped store. */
|
|
53
|
+
export function resolveStorePath(cwd: string, sessionId: string): string {
|
|
54
|
+
return join(cwd, ".pi", "subagent-schedules", `${sessionId}.json`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class ScheduleStore {
|
|
58
|
+
private filePath: string;
|
|
59
|
+
private lockPath: string;
|
|
60
|
+
private jobs = new Map<string, ScheduledSubagent>();
|
|
61
|
+
|
|
62
|
+
constructor(filePath: string) {
|
|
63
|
+
this.filePath = filePath;
|
|
64
|
+
this.lockPath = filePath + ".lock";
|
|
65
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
66
|
+
this.load();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Load from disk into the in-memory cache. Silent on parse errors. */
|
|
70
|
+
private load(): void {
|
|
71
|
+
if (!existsSync(this.filePath)) return;
|
|
72
|
+
try {
|
|
73
|
+
const data: ScheduleStoreData = JSON.parse(readFileSync(this.filePath, "utf-8"));
|
|
74
|
+
this.jobs.clear();
|
|
75
|
+
for (const j of data.jobs ?? []) this.jobs.set(j.id, j);
|
|
76
|
+
} catch { /* corrupt — start fresh, next save rewrites */ }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Atomic write via temp file + rename (POSIX-atomic). */
|
|
80
|
+
private save(): void {
|
|
81
|
+
const data: ScheduleStoreData = { version: 1, jobs: [...this.jobs.values()] };
|
|
82
|
+
const tmp = this.filePath + ".tmp";
|
|
83
|
+
writeFileSync(tmp, JSON.stringify(data, null, 2));
|
|
84
|
+
renameSync(tmp, this.filePath);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Acquire lock → reload → mutate → save → release. */
|
|
88
|
+
private withLock<T>(fn: () => T): T {
|
|
89
|
+
acquireLock(this.lockPath);
|
|
90
|
+
try {
|
|
91
|
+
this.load();
|
|
92
|
+
const result = fn();
|
|
93
|
+
this.save();
|
|
94
|
+
return result;
|
|
95
|
+
} finally {
|
|
96
|
+
releaseLock(this.lockPath);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Read-only — returns a snapshot of the in-memory cache. */
|
|
101
|
+
list(): ScheduledSubagent[] {
|
|
102
|
+
return [...this.jobs.values()];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Read-only check — uses the cache. */
|
|
106
|
+
hasName(name: string, exceptId?: string): boolean {
|
|
107
|
+
for (const j of this.jobs.values()) {
|
|
108
|
+
if (j.id !== exceptId && j.name === name) return true;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get(id: string): ScheduledSubagent | undefined {
|
|
114
|
+
return this.jobs.get(id);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
add(job: ScheduledSubagent): void {
|
|
118
|
+
this.withLock(() => {
|
|
119
|
+
this.jobs.set(job.id, job);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
|
|
124
|
+
return this.withLock(() => {
|
|
125
|
+
const existing = this.jobs.get(id);
|
|
126
|
+
if (!existing) return undefined;
|
|
127
|
+
const updated = { ...existing, ...patch };
|
|
128
|
+
this.jobs.set(id, updated);
|
|
129
|
+
return updated;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
remove(id: string): boolean {
|
|
134
|
+
return this.withLock(() => this.jobs.delete(id));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Delete the backing file (used when no jobs remain, optional cleanup). */
|
|
138
|
+
deleteFileIfEmpty(): void {
|
|
139
|
+
if (this.jobs.size === 0 && existsSync(this.filePath)) {
|
|
140
|
+
try { unlinkSync(this.filePath); } catch { /* ignore */ }
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|