@tintinweb/pi-subagents 0.6.3 → 0.7.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/CHANGELOG.md +37 -0
- package/README.md +55 -11
- package/dist/agent-manager.d.ts +23 -1
- package/dist/agent-manager.js +71 -20
- package/dist/agent-runner.d.ts +27 -0
- package/dist/agent-runner.js +28 -4
- package/dist/index.js +236 -72
- 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 +90 -20
- package/src/agent-runner.ts +43 -5
- package/src/index.ts +239 -63
- 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/dist/index.js
CHANGED
|
@@ -23,30 +23,36 @@ import { GroupJoinManager } from "./group-join.js";
|
|
|
23
23
|
import { resolveAgentInvocationConfig, resolveJoinMode } from "./invocation-config.js";
|
|
24
24
|
import { resolveModel } from "./model-resolver.js";
|
|
25
25
|
import { createOutputFilePath, streamToOutputFile, writeInitialEntry } from "./output-file.js";
|
|
26
|
+
import { SubagentScheduler } from "./schedule.js";
|
|
27
|
+
import { resolveStorePath, ScheduleStore } from "./schedule-store.js";
|
|
26
28
|
import { applyAndEmitLoaded, saveAndEmitChanged } from "./settings.js";
|
|
27
29
|
import { AgentWidget, describeActivity, formatDuration, formatMs, formatTokens, formatTurns, getDisplayName, getPromptModeLabel, SPINNER, } from "./ui/agent-widget.js";
|
|
30
|
+
import { showSchedulesMenu } from "./ui/schedule-menu.js";
|
|
31
|
+
import { addUsage, getLifetimeTotal, getSessionContextPercent } from "./usage.js";
|
|
28
32
|
// ---- Shared helpers ----
|
|
29
33
|
/** Tool execute return value for a text response. */
|
|
30
34
|
function textResult(msg, details) {
|
|
31
35
|
return { content: [{ type: "text", text: msg }], details: details };
|
|
32
36
|
}
|
|
33
|
-
/**
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
return formatTokens(session.getSessionStats().tokens.total);
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return "";
|
|
42
|
-
}
|
|
37
|
+
/** Format an agent's lifetime token total, or "" when zero. */
|
|
38
|
+
function formatLifetimeTokens(o) {
|
|
39
|
+
const t = getLifetimeTotal(o.lifetimeUsage);
|
|
40
|
+
return t > 0 ? formatTokens(t) : "";
|
|
43
41
|
}
|
|
44
42
|
/**
|
|
45
43
|
* Create an AgentActivity state and spawn callbacks for tracking tool usage.
|
|
46
44
|
* Used by both foreground and background paths to avoid duplication.
|
|
47
45
|
*/
|
|
48
46
|
function createActivityTracker(maxTurns, onStreamUpdate) {
|
|
49
|
-
const state = {
|
|
47
|
+
const state = {
|
|
48
|
+
activeTools: new Map(),
|
|
49
|
+
toolUses: 0,
|
|
50
|
+
turnCount: 1,
|
|
51
|
+
maxTurns,
|
|
52
|
+
responseText: "",
|
|
53
|
+
session: undefined,
|
|
54
|
+
lifetimeUsage: { input: 0, output: 0, cacheWrite: 0 },
|
|
55
|
+
};
|
|
50
56
|
const callbacks = {
|
|
51
57
|
onToolActivity: (activity) => {
|
|
52
58
|
if (activity.type === "start") {
|
|
@@ -61,7 +67,6 @@ function createActivityTracker(maxTurns, onStreamUpdate) {
|
|
|
61
67
|
}
|
|
62
68
|
state.toolUses++;
|
|
63
69
|
}
|
|
64
|
-
state.tokens = safeFormatTokens(state.session);
|
|
65
70
|
onStreamUpdate?.();
|
|
66
71
|
},
|
|
67
72
|
onTextDelta: (_delta, fullText) => {
|
|
@@ -75,6 +80,10 @@ function createActivityTracker(maxTurns, onStreamUpdate) {
|
|
|
75
80
|
onSessionCreated: (session) => {
|
|
76
81
|
state.session = session;
|
|
77
82
|
},
|
|
83
|
+
onAssistantUsage: (usage) => {
|
|
84
|
+
addUsage(state.lifetimeUsage, usage);
|
|
85
|
+
onStreamUpdate?.();
|
|
86
|
+
},
|
|
78
87
|
};
|
|
79
88
|
return { state, callbacks };
|
|
80
89
|
}
|
|
@@ -105,14 +114,10 @@ function escapeXml(s) {
|
|
|
105
114
|
function formatTaskNotification(record, resultMaxLen) {
|
|
106
115
|
const status = getStatusLabel(record.status, record.error);
|
|
107
116
|
const durationMs = record.completedAt ? record.completedAt - record.startedAt : 0;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
totalTokens = stats.tokens?.total ?? 0;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
catch { /* session stats unavailable */ }
|
|
117
|
+
const totalTokens = getLifetimeTotal(record.lifetimeUsage);
|
|
118
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
119
|
+
const ctxXml = contextPercent !== null ? `<context_percent>${Math.round(contextPercent)}</context_percent>` : "";
|
|
120
|
+
const compactXml = record.compactionCount ? `<compactions>${record.compactionCount}</compactions>` : "";
|
|
116
121
|
const resultPreview = record.result
|
|
117
122
|
? record.result.length > resultMaxLen
|
|
118
123
|
? record.result.slice(0, resultMaxLen) + "\n...(truncated, use get_subagent_result for full output)"
|
|
@@ -126,7 +131,7 @@ function formatTaskNotification(record, resultMaxLen) {
|
|
|
126
131
|
`<status>${escapeXml(status)}</status>`,
|
|
127
132
|
`<summary>Agent "${escapeXml(record.description)}" ${record.status}</summary>`,
|
|
128
133
|
`<result>${escapeXml(resultPreview)}</result>`,
|
|
129
|
-
`<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses
|
|
134
|
+
`<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses>${ctxXml}${compactXml}<duration_ms>${durationMs}</duration_ms></usage>`,
|
|
130
135
|
`</task-notification>`,
|
|
131
136
|
].filter(Boolean).join('\n');
|
|
132
137
|
}
|
|
@@ -135,7 +140,7 @@ function buildDetails(base, record, activity, overrides) {
|
|
|
135
140
|
return {
|
|
136
141
|
...base,
|
|
137
142
|
toolUses: record.toolUses,
|
|
138
|
-
tokens:
|
|
143
|
+
tokens: formatLifetimeTokens(record),
|
|
139
144
|
turnCount: activity?.turnCount,
|
|
140
145
|
maxTurns: activity?.maxTurns,
|
|
141
146
|
durationMs: (record.completedAt ?? Date.now()) - record.startedAt,
|
|
@@ -147,12 +152,7 @@ function buildDetails(base, record, activity, overrides) {
|
|
|
147
152
|
}
|
|
148
153
|
/** Build notification details for the custom message renderer. */
|
|
149
154
|
function buildNotificationDetails(record, resultMaxLen, activity) {
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
if (record.session)
|
|
153
|
-
totalTokens = record.session.getSessionStats().tokens?.total ?? 0;
|
|
154
|
-
}
|
|
155
|
-
catch { }
|
|
155
|
+
const totalTokens = getLifetimeTotal(record.lifetimeUsage);
|
|
156
156
|
return {
|
|
157
157
|
id: record.id,
|
|
158
158
|
description: record.description,
|
|
@@ -235,7 +235,10 @@ export default function (pi) {
|
|
|
235
235
|
cancelNudge(key);
|
|
236
236
|
pendingNudges.set(key, setTimeout(() => {
|
|
237
237
|
pendingNudges.delete(key);
|
|
238
|
-
|
|
238
|
+
try {
|
|
239
|
+
send();
|
|
240
|
+
}
|
|
241
|
+
catch { /* ignore stale completion side-effect errors */ }
|
|
239
242
|
}, delay));
|
|
240
243
|
}
|
|
241
244
|
function cancelNudge(key) {
|
|
@@ -299,18 +302,15 @@ export default function (pi) {
|
|
|
299
302
|
/** Helper: build event data for lifecycle events from an AgentRecord. */
|
|
300
303
|
function buildEventData(record) {
|
|
301
304
|
const durationMs = record.completedAt ? record.completedAt - record.startedAt : Date.now() - record.startedAt;
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
catch { /* session stats unavailable */ }
|
|
305
|
+
// All three fields are lifetime-accumulated (Σ over every assistant message_end),
|
|
306
|
+
// so they survive compaction together — input + output ≤ total always.
|
|
307
|
+
// tokens is omitted when nothing was ever produced (e.g. agent errored before
|
|
308
|
+
// any message_end fired), preserving prior payload shape.
|
|
309
|
+
const u = record.lifetimeUsage;
|
|
310
|
+
const total = getLifetimeTotal(u);
|
|
311
|
+
const tokens = total > 0
|
|
312
|
+
? { input: u.input, output: u.output, total }
|
|
313
|
+
: undefined;
|
|
314
314
|
return {
|
|
315
315
|
id: record.id,
|
|
316
316
|
type: record.type,
|
|
@@ -367,6 +367,16 @@ export default function (pi) {
|
|
|
367
367
|
type: record.type,
|
|
368
368
|
description: record.description,
|
|
369
369
|
});
|
|
370
|
+
}, (record, info) => {
|
|
371
|
+
// Emit compacted event when agent's session compacts (preserves count on record).
|
|
372
|
+
pi.events.emit("subagents:compacted", {
|
|
373
|
+
id: record.id,
|
|
374
|
+
type: record.type,
|
|
375
|
+
description: record.description,
|
|
376
|
+
reason: info.reason,
|
|
377
|
+
tokensBefore: info.tokensBefore,
|
|
378
|
+
compactionCount: record.compactionCount,
|
|
379
|
+
});
|
|
370
380
|
});
|
|
371
381
|
// Expose manager via Symbol.for() global registry for cross-package access.
|
|
372
382
|
// Standard Node.js pattern for cross-package singletons (used by OpenTelemetry, etc.).
|
|
@@ -379,12 +389,38 @@ export default function (pi) {
|
|
|
379
389
|
};
|
|
380
390
|
// --- Cross-extension RPC via pi.events ---
|
|
381
391
|
let currentCtx;
|
|
382
|
-
//
|
|
392
|
+
// ---- Subagent scheduler ----
|
|
393
|
+
// Session-scoped: store is constructed inside session_start once sessionId
|
|
394
|
+
// is available. Mirrors pi-chonky-tasks's session-scoped task store —
|
|
395
|
+
// schedules reset on /new, restore on /resume.
|
|
396
|
+
const scheduler = new SubagentScheduler();
|
|
397
|
+
function startScheduler(ctx) {
|
|
398
|
+
try {
|
|
399
|
+
const sessionId = ctx.sessionManager?.getSessionId?.();
|
|
400
|
+
if (!sessionId)
|
|
401
|
+
return; // sessionId not yet available — try again on next event
|
|
402
|
+
const path = resolveStorePath(ctx.cwd, sessionId);
|
|
403
|
+
const store = new ScheduleStore(path);
|
|
404
|
+
scheduler.start(pi, ctx, manager, store);
|
|
405
|
+
pi.events.emit("subagents:scheduler_ready", { sessionId, jobCount: store.list().length });
|
|
406
|
+
}
|
|
407
|
+
catch (err) {
|
|
408
|
+
// Scheduling is non-essential — log and move on so the rest of the
|
|
409
|
+
// extension keeps working if e.g. .pi/ is unwritable.
|
|
410
|
+
console.warn("[pi-subagents] Failed to start scheduler:", err);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
// Capture ctx from session_start for RPC spawn handler + start the scheduler.
|
|
383
414
|
pi.on("session_start", async (_event, ctx) => {
|
|
384
415
|
currentCtx = ctx;
|
|
385
|
-
manager.clearCompleted();
|
|
416
|
+
manager.clearCompleted();
|
|
417
|
+
if (isSchedulingEnabled() && !scheduler.isActive())
|
|
418
|
+
startScheduler(ctx);
|
|
419
|
+
});
|
|
420
|
+
pi.on("session_before_switch", () => {
|
|
421
|
+
manager.clearCompleted();
|
|
422
|
+
scheduler.stop();
|
|
386
423
|
});
|
|
387
|
-
pi.on("session_before_switch", () => { manager.clearCompleted(); });
|
|
388
424
|
const { unsubPing: unsubPingRpc, unsubSpawn: unsubSpawnRpc, unsubStop: unsubStopRpc } = registerRpcHandlers({
|
|
389
425
|
events: pi.events,
|
|
390
426
|
pi,
|
|
@@ -401,6 +437,7 @@ export default function (pi) {
|
|
|
401
437
|
unsubPingRpc();
|
|
402
438
|
currentCtx = undefined;
|
|
403
439
|
delete globalThis[MANAGER_KEY];
|
|
440
|
+
scheduler.stop();
|
|
404
441
|
manager.abortAll();
|
|
405
442
|
for (const timer of pendingNudges.values())
|
|
406
443
|
clearTimeout(timer);
|
|
@@ -413,6 +450,15 @@ export default function (pi) {
|
|
|
413
450
|
let defaultJoinMode = 'smart';
|
|
414
451
|
function getDefaultJoinMode() { return defaultJoinMode; }
|
|
415
452
|
function setDefaultJoinMode(mode) { defaultJoinMode = mode; }
|
|
453
|
+
// Master switch for the schedule subagent feature. Defaults to enabled.
|
|
454
|
+
// Read once at extension init (before tool registration) so the Agent tool's
|
|
455
|
+
// param schema reflects the persisted setting. Runtime toggles via /agents
|
|
456
|
+
// → Settings short-circuit the menu entry + the execute-time addJob path
|
|
457
|
+
// immediately, but the schema-level removal only takes effect on next
|
|
458
|
+
// extension load (next pi session). Documented in CHANGELOG/README.
|
|
459
|
+
let schedulingEnabled = true;
|
|
460
|
+
function isSchedulingEnabled() { return schedulingEnabled; }
|
|
461
|
+
function setSchedulingEnabled(b) { schedulingEnabled = b; }
|
|
416
462
|
// ---- Batch tracking for smart join mode ----
|
|
417
463
|
// Collects background agent IDs spawned in the current turn for smart grouping.
|
|
418
464
|
// Uses a debounced timer: each new agent resets the 100ms window so that all
|
|
@@ -497,8 +543,25 @@ export default function (pi) {
|
|
|
497
543
|
setDefaultMaxTurns,
|
|
498
544
|
setGraceTurns,
|
|
499
545
|
setDefaultJoinMode,
|
|
546
|
+
setSchedulingEnabled,
|
|
500
547
|
}, (event, payload) => pi.events.emit(event, payload));
|
|
501
548
|
// ---- Agent tool ----
|
|
549
|
+
// Schedule param + its guideline are gated on `schedulingEnabled` (read once
|
|
550
|
+
// at registration; flipping the setting later requires next pi session for
|
|
551
|
+
// the schema to update). Defining the shape once and spreading it via Partial
|
|
552
|
+
// preserves Type.Object's inference when present and produces a
|
|
553
|
+
// `schedule`-free schema when absent — zero LLM-context cost in disabled mode.
|
|
554
|
+
const scheduleParamShape = {
|
|
555
|
+
schedule: Type.Optional(Type.String({
|
|
556
|
+
description: 'Opt-in only — fire later instead of now. Omit to run immediately (the default, almost always correct). ' +
|
|
557
|
+
'Formats: 6-field cron ("0 0 9 * * 1" = 9am Mon), interval ("5m"/"1h"), one-shot ("+10m" or ISO). ' +
|
|
558
|
+
'Forces run_in_background; incompatible with inherit_context and resume. Returns job ID.',
|
|
559
|
+
})),
|
|
560
|
+
};
|
|
561
|
+
const scheduleParam = isSchedulingEnabled() ? scheduleParamShape : {};
|
|
562
|
+
const scheduleGuideline = isSchedulingEnabled()
|
|
563
|
+
? `\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.`
|
|
564
|
+
: "";
|
|
502
565
|
pi.registerTool(defineTool({
|
|
503
566
|
name: "Agent",
|
|
504
567
|
label: "Agent",
|
|
@@ -522,7 +585,7 @@ Guidelines:
|
|
|
522
585
|
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
523
586
|
- Use thinking to control extended thinking level.
|
|
524
587
|
- Use inherit_context if the agent needs the parent conversation history.
|
|
525
|
-
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications)
|
|
588
|
+
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).${scheduleGuideline}`,
|
|
526
589
|
parameters: Type.Object({
|
|
527
590
|
prompt: Type.String({
|
|
528
591
|
description: "The task for the agent to perform.",
|
|
@@ -558,6 +621,7 @@ Guidelines:
|
|
|
558
621
|
isolation: Type.Optional(Type.Literal("worktree", {
|
|
559
622
|
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.',
|
|
560
623
|
})),
|
|
624
|
+
...scheduleParam,
|
|
561
625
|
}),
|
|
562
626
|
// ---- Custom rendering: Claude Code style ----
|
|
563
627
|
renderCall(args, theme) {
|
|
@@ -700,6 +764,45 @@ Guidelines:
|
|
|
700
764
|
modelName: agentModelName,
|
|
701
765
|
tags: agentTags.length > 0 ? agentTags : undefined,
|
|
702
766
|
};
|
|
767
|
+
// ---- Schedule: register a job, don't spawn now ----
|
|
768
|
+
if (params.schedule) {
|
|
769
|
+
if (!isSchedulingEnabled()) {
|
|
770
|
+
return textResult("Scheduling is disabled in this project. Enable via /agents → Settings → Scheduling.");
|
|
771
|
+
}
|
|
772
|
+
if (params.resume) {
|
|
773
|
+
return textResult("Cannot combine `schedule` with `resume` — schedules create fresh agents.");
|
|
774
|
+
}
|
|
775
|
+
if (params.inherit_context) {
|
|
776
|
+
return textResult("Cannot combine `schedule` with `inherit_context` — there is no parent conversation at fire time.");
|
|
777
|
+
}
|
|
778
|
+
if (params.run_in_background === false) {
|
|
779
|
+
return textResult("Cannot combine `schedule` with `run_in_background: false` — scheduled jobs always run in background.");
|
|
780
|
+
}
|
|
781
|
+
if (!scheduler.isActive()) {
|
|
782
|
+
return textResult("Scheduler is not active in this session yet. Try again after the session has fully started.");
|
|
783
|
+
}
|
|
784
|
+
try {
|
|
785
|
+
const job = scheduler.addJob({
|
|
786
|
+
name: params.description,
|
|
787
|
+
description: params.description,
|
|
788
|
+
schedule: params.schedule,
|
|
789
|
+
subagent_type: subagentType,
|
|
790
|
+
prompt: params.prompt,
|
|
791
|
+
model: params.model,
|
|
792
|
+
thinking: thinking,
|
|
793
|
+
max_turns: effectiveMaxTurns,
|
|
794
|
+
isolated: isolated,
|
|
795
|
+
isolation: isolation,
|
|
796
|
+
});
|
|
797
|
+
const next = scheduler.getNextRun(job.id);
|
|
798
|
+
return textResult(`Scheduled "${job.name}" (id: ${job.id}, type: ${job.scheduleType}). ` +
|
|
799
|
+
`Next run: ${next ?? "(unknown)"}. ` +
|
|
800
|
+
`Manage via /agents → Scheduled jobs.`);
|
|
801
|
+
}
|
|
802
|
+
catch (err) {
|
|
803
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
804
|
+
}
|
|
805
|
+
}
|
|
703
806
|
// Resume existing agent
|
|
704
807
|
if (params.resume) {
|
|
705
808
|
const existing = manager.getRecord(params.resume);
|
|
@@ -730,17 +833,22 @@ Guidelines:
|
|
|
730
833
|
rec.outputCleanup = streamToOutputFile(session, rec.outputFile, id, ctx.cwd);
|
|
731
834
|
}
|
|
732
835
|
};
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
836
|
+
try {
|
|
837
|
+
id = manager.spawn(pi, ctx, subagentType, params.prompt, {
|
|
838
|
+
description: params.description,
|
|
839
|
+
model,
|
|
840
|
+
maxTurns: effectiveMaxTurns,
|
|
841
|
+
isolated,
|
|
842
|
+
inheritContext,
|
|
843
|
+
thinkingLevel: thinking,
|
|
844
|
+
isBackground: true,
|
|
845
|
+
isolation,
|
|
846
|
+
...bgCallbacks,
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
catch (err) {
|
|
850
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
851
|
+
}
|
|
744
852
|
// Set output file + join mode synchronously after spawn, before the
|
|
745
853
|
// event loop yields — onSessionCreated is async so this is safe.
|
|
746
854
|
const joinMode = resolveJoinMode(defaultJoinMode, true);
|
|
@@ -792,7 +900,7 @@ Guidelines:
|
|
|
792
900
|
const details = {
|
|
793
901
|
...detailBase,
|
|
794
902
|
toolUses: fgState.toolUses,
|
|
795
|
-
tokens: fgState
|
|
903
|
+
tokens: formatLifetimeTokens(fgState),
|
|
796
904
|
turnCount: fgState.turnCount,
|
|
797
905
|
maxTurns: fgState.maxTurns,
|
|
798
906
|
durationMs: Date.now() - startedAt,
|
|
@@ -825,16 +933,24 @@ Guidelines:
|
|
|
825
933
|
streamUpdate();
|
|
826
934
|
}, 80);
|
|
827
935
|
streamUpdate();
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
936
|
+
let record;
|
|
937
|
+
try {
|
|
938
|
+
record = await manager.spawnAndWait(pi, ctx, subagentType, params.prompt, {
|
|
939
|
+
description: params.description,
|
|
940
|
+
model,
|
|
941
|
+
maxTurns: effectiveMaxTurns,
|
|
942
|
+
isolated,
|
|
943
|
+
inheritContext,
|
|
944
|
+
thinkingLevel: thinking,
|
|
945
|
+
isolation,
|
|
946
|
+
signal,
|
|
947
|
+
...fgCallbacks,
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
catch (err) {
|
|
951
|
+
clearInterval(spinnerInterval);
|
|
952
|
+
return textResult(err instanceof Error ? err.message : String(err));
|
|
953
|
+
}
|
|
838
954
|
clearInterval(spinnerInterval);
|
|
839
955
|
// Clean up foreground agent from widget
|
|
840
956
|
if (fgId) {
|
|
@@ -842,7 +958,7 @@ Guidelines:
|
|
|
842
958
|
widget.markFinished(fgId);
|
|
843
959
|
}
|
|
844
960
|
// Get final token count
|
|
845
|
-
const tokenText =
|
|
961
|
+
const tokenText = formatLifetimeTokens(fgState);
|
|
846
962
|
const details = buildDetails(detailBase, record, fgState, { tokens: tokenText });
|
|
847
963
|
const fallbackNote = fellBack
|
|
848
964
|
? `Note: Unknown agent type "${rawType}" — using general-purpose.\n\n`
|
|
@@ -890,10 +1006,18 @@ Guidelines:
|
|
|
890
1006
|
}
|
|
891
1007
|
const displayName = getDisplayName(record.type);
|
|
892
1008
|
const duration = formatDuration(record.startedAt, record.completedAt);
|
|
893
|
-
const tokens =
|
|
894
|
-
const
|
|
1009
|
+
const tokens = formatLifetimeTokens(record);
|
|
1010
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
1011
|
+
const statsParts = [`Tool uses: ${record.toolUses}`];
|
|
1012
|
+
if (tokens)
|
|
1013
|
+
statsParts.push(tokens);
|
|
1014
|
+
if (contextPercent !== null)
|
|
1015
|
+
statsParts.push(`Context: ${Math.round(contextPercent)}%`);
|
|
1016
|
+
if (record.compactionCount)
|
|
1017
|
+
statsParts.push(`Compactions: ${record.compactionCount}`);
|
|
1018
|
+
statsParts.push(`Duration: ${duration}`);
|
|
895
1019
|
let output = `Agent: ${record.id}\n` +
|
|
896
|
-
`Type: ${displayName} | Status: ${record.status} | ${
|
|
1020
|
+
`Type: ${displayName} | Status: ${record.status} | ${statsParts.join(" | ")}\n` +
|
|
897
1021
|
`Description: ${record.description}\n\n`;
|
|
898
1022
|
if (record.status === "running") {
|
|
899
1023
|
output += "Agent is still running. Use wait: true or check back later.";
|
|
@@ -952,7 +1076,18 @@ Guidelines:
|
|
|
952
1076
|
try {
|
|
953
1077
|
await steerAgent(record.session, params.message);
|
|
954
1078
|
pi.events.emit("subagents:steered", { id: record.id, message: params.message });
|
|
955
|
-
|
|
1079
|
+
const tokens = formatLifetimeTokens(record);
|
|
1080
|
+
const contextPercent = getSessionContextPercent(record.session);
|
|
1081
|
+
const stateParts = [];
|
|
1082
|
+
if (tokens)
|
|
1083
|
+
stateParts.push(tokens);
|
|
1084
|
+
stateParts.push(`${record.toolUses} tool ${record.toolUses === 1 ? "use" : "uses"}`);
|
|
1085
|
+
if (contextPercent !== null)
|
|
1086
|
+
stateParts.push(`context ${Math.round(contextPercent)}% full`);
|
|
1087
|
+
if (record.compactionCount)
|
|
1088
|
+
stateParts.push(`${record.compactionCount} compaction${record.compactionCount === 1 ? "" : "s"}`);
|
|
1089
|
+
return textResult(`Steering message sent to agent ${record.id}. The agent will process it after its current tool execution.\n` +
|
|
1090
|
+
`Current state: ${stateParts.join(" · ")}`);
|
|
956
1091
|
}
|
|
957
1092
|
catch (err) {
|
|
958
1093
|
return textResult(`Failed to steer agent: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -1000,6 +1135,11 @@ Guidelines:
|
|
|
1000
1135
|
if (allNames.length > 0) {
|
|
1001
1136
|
options.push(`Agent types (${allNames.length})`);
|
|
1002
1137
|
}
|
|
1138
|
+
// Scheduled jobs entry (always present when scheduler is active)
|
|
1139
|
+
if (scheduler.isActive()) {
|
|
1140
|
+
const jobCount = scheduler.list().length;
|
|
1141
|
+
options.push(`Scheduled jobs (${jobCount})`);
|
|
1142
|
+
}
|
|
1003
1143
|
// Actions
|
|
1004
1144
|
options.push("Create new agent");
|
|
1005
1145
|
options.push("Settings");
|
|
@@ -1022,6 +1162,10 @@ Guidelines:
|
|
|
1022
1162
|
await showAllAgentsList(ctx);
|
|
1023
1163
|
await showAgentsMenu(ctx);
|
|
1024
1164
|
}
|
|
1165
|
+
else if (choice.startsWith("Scheduled jobs (")) {
|
|
1166
|
+
await showSchedulesMenu(ctx, scheduler);
|
|
1167
|
+
await showAgentsMenu(ctx);
|
|
1168
|
+
}
|
|
1025
1169
|
else if (choice === "Create new agent") {
|
|
1026
1170
|
await showCreateWizard(ctx);
|
|
1027
1171
|
}
|
|
@@ -1480,6 +1624,7 @@ ${systemPrompt}
|
|
|
1480
1624
|
defaultMaxTurns: getDefaultMaxTurns() ?? 0,
|
|
1481
1625
|
graceTurns: getGraceTurns(),
|
|
1482
1626
|
defaultJoinMode: getDefaultJoinMode(),
|
|
1627
|
+
schedulingEnabled: isSchedulingEnabled(),
|
|
1483
1628
|
};
|
|
1484
1629
|
}
|
|
1485
1630
|
async function showSettings(ctx) {
|
|
@@ -1488,6 +1633,7 @@ ${systemPrompt}
|
|
|
1488
1633
|
`Default max turns (current: ${getDefaultMaxTurns() ?? "unlimited"})`,
|
|
1489
1634
|
`Grace turns (current: ${getGraceTurns()})`,
|
|
1490
1635
|
`Join mode (current: ${getDefaultJoinMode()})`,
|
|
1636
|
+
`Scheduling (current: ${isSchedulingEnabled() ? "enabled" : "disabled"})`,
|
|
1491
1637
|
]);
|
|
1492
1638
|
if (!choice)
|
|
1493
1639
|
return;
|
|
@@ -1546,6 +1692,24 @@ ${systemPrompt}
|
|
|
1546
1692
|
notifyApplied(ctx, `Default join mode set to ${mode}`);
|
|
1547
1693
|
}
|
|
1548
1694
|
}
|
|
1695
|
+
else if (choice.startsWith("Scheduling")) {
|
|
1696
|
+
const val = await ctx.ui.select("Schedule subagent feature", [
|
|
1697
|
+
"enabled — Agent tool accepts a `schedule` param; /agents → Scheduled jobs visible",
|
|
1698
|
+
"disabled — `schedule` removed from Agent tool spec (no LLM-context cost); menu hidden",
|
|
1699
|
+
]);
|
|
1700
|
+
if (val) {
|
|
1701
|
+
const enabled = val.startsWith("enabled");
|
|
1702
|
+
if (enabled === isSchedulingEnabled()) {
|
|
1703
|
+
ctx.ui.notify(`Scheduling already ${enabled ? "enabled" : "disabled"}.`, "info");
|
|
1704
|
+
}
|
|
1705
|
+
else {
|
|
1706
|
+
setSchedulingEnabled(enabled);
|
|
1707
|
+
if (!enabled)
|
|
1708
|
+
scheduler.stop(); // immediate kill — outstanding fires stop ticking
|
|
1709
|
+
notifyApplied(ctx, `Scheduling ${enabled ? "enabled" : "disabled"}. Tool spec change takes effect on next pi session.`);
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1549
1713
|
}
|
|
1550
1714
|
// Persist the current snapshot, emit `subagents:settings_changed`, and surface
|
|
1551
1715
|
// the right toast. Successful saves show info; persistence failures downgrade
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
import type { ScheduledSubagent } from "./types.js";
|
|
13
|
+
/** Resolve the storage path for a session-scoped store. */
|
|
14
|
+
export declare function resolveStorePath(cwd: string, sessionId: string): string;
|
|
15
|
+
export declare class ScheduleStore {
|
|
16
|
+
private filePath;
|
|
17
|
+
private lockPath;
|
|
18
|
+
private jobs;
|
|
19
|
+
constructor(filePath: string);
|
|
20
|
+
/** Load from disk into the in-memory cache. Silent on parse errors. */
|
|
21
|
+
private load;
|
|
22
|
+
/** Atomic write via temp file + rename (POSIX-atomic). */
|
|
23
|
+
private save;
|
|
24
|
+
/** Acquire lock → reload → mutate → save → release. */
|
|
25
|
+
private withLock;
|
|
26
|
+
/** Read-only — returns a snapshot of the in-memory cache. */
|
|
27
|
+
list(): ScheduledSubagent[];
|
|
28
|
+
/** Read-only check — uses the cache. */
|
|
29
|
+
hasName(name: string, exceptId?: string): boolean;
|
|
30
|
+
get(id: string): ScheduledSubagent | undefined;
|
|
31
|
+
add(job: ScheduledSubagent): void;
|
|
32
|
+
update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined;
|
|
33
|
+
remove(id: string): boolean;
|
|
34
|
+
/** Delete the backing file (used when no jobs remain, optional cleanup). */
|
|
35
|
+
deleteFileIfEmpty(): void;
|
|
36
|
+
}
|