@henryqw/pi-subagent 27.11.0 → 27.12.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/CONTEXT.md +21 -0
- package/README.md +2 -0
- package/extensions/isolated.ts +9 -5
- package/extensions/subagent.ts +14 -0
- package/package.json +1 -1
package/CONTEXT.md
CHANGED
|
@@ -67,3 +67,24 @@ Repository-scoped ownership that excludes concurrent execution or resumption whi
|
|
|
67
67
|
|
|
68
68
|
**Safety budget**:
|
|
69
69
|
A finite limit for child execution or a safety operation, not a deadline for the whole isolated request.
|
|
70
|
+
|
|
71
|
+
## Pending-result event contract
|
|
72
|
+
|
|
73
|
+
`pi-subagent:pending-result` is an optional synchronous query through `pi.events`.
|
|
74
|
+
The caller emits a mutable `{ sessionId: string, pending: false }` object, then reads
|
|
75
|
+
`pending` when `emit` returns. Pi Subagent changes only `pending`, and only to `true`.
|
|
76
|
+
No listener means no pending result. Listeners must not wait for asynchronous work.
|
|
77
|
+
|
|
78
|
+
A positive answer means that this process owns live direct or isolated work for
|
|
79
|
+
that exact current Pi session, with a future result message that triggers a turn.
|
|
80
|
+
Direct ownership starts after a verified launch. Isolated ownership starts after
|
|
81
|
+
durable acknowledgement. Ownership ends on completion or failure, before result
|
|
82
|
+
delivery. Cancellation, shutdown, and session replacement cannot grant ownership.
|
|
83
|
+
Retained tabs, checkout admission, saved requests, interrupted or unknown work,
|
|
84
|
+
and results whose delivery failed do not grant ownership.
|
|
85
|
+
|
|
86
|
+
Pi Goal checks this query after its budget and continuation limits, before its
|
|
87
|
+
no-progress check. A positive answer lets Pi settle without another continuation
|
|
88
|
+
or a no-progress warning. The result message starts ordinary goal handling again.
|
|
89
|
+
No package dependency is required. Background-command notices do not trigger a
|
|
90
|
+
turn and do not participate in this contract.
|
package/README.md
CHANGED
|
@@ -25,6 +25,8 @@ Configure the profiles that your Roles use in `/task-models`. Direct writer term
|
|
|
25
25
|
|
|
26
26
|
Ask Main: “Have a scout trace sign-in without editing files.” Main calls `delegate_task` and stays available until progress and results arrive.
|
|
27
27
|
|
|
28
|
+
If Pi Goal is installed, it waits for live direct or isolated results in the current session instead of adding empty continuation turns. Budget and continuation limits still apply. Retained or interrupted work does not cause a wait. The optional `pi-subagent:pending-result` event needs no package dependency; see the [pending-result contract](./CONTEXT.md#pending-result-event-contract).
|
|
29
|
+
|
|
28
30
|
Commands, prompts, and UI are for you. Tools and the packaged skill are for Main, except `git_read`, which is only for Role-declared children.
|
|
29
31
|
|
|
30
32
|
| Surface | Type | Purpose |
|
package/extensions/isolated.ts
CHANGED
|
@@ -514,6 +514,7 @@ export function formatExecutePlan({ request, root, main, routes }: ExecutePlan,
|
|
|
514
514
|
}
|
|
515
515
|
|
|
516
516
|
export interface IsolatedSurface {
|
|
517
|
+
hasPendingResult(sessionId: string): boolean;
|
|
517
518
|
/** Dry run of execute: validates and resolves without durable state or Herdr calls. */
|
|
518
519
|
plan(request: ExecuteRequest, signal: AbortSignal | undefined, ctx: ExtensionContext): Promise<ReturnType<typeof formatExecutePlan>>;
|
|
519
520
|
execute(request: ExecuteRequest, signal: AbortSignal | undefined, ctx: ExtensionContext): Promise<ReturnType<typeof toolResult>>;
|
|
@@ -547,7 +548,7 @@ export function registerIsolatedExtension(pi: ExtensionAPI, options: RegisterIso
|
|
|
547
548
|
let savedRevision = 0;
|
|
548
549
|
const stateListeners = new Set<(state: RunState) => void>();
|
|
549
550
|
const activeJobs = new Set<AbortController>();
|
|
550
|
-
const jobOwners = new Map<string, ()
|
|
551
|
+
const jobOwners = new Map<string, { canDeliver(): boolean; pendingResult(): boolean }>();
|
|
551
552
|
let sessionEpoch = 0;
|
|
552
553
|
let sessionClosed = false;
|
|
553
554
|
|
|
@@ -570,7 +571,7 @@ export function registerIsolatedExtension(pi: ExtensionAPI, options: RegisterIso
|
|
|
570
571
|
savedRevision += 1;
|
|
571
572
|
for (const listener of stateListeners) listener(state);
|
|
572
573
|
const owner = jobOwners.get(`${state.root}\0${state.request.id}`);
|
|
573
|
-
if (!sessionClosed && (owner === undefined || owner())) {
|
|
574
|
+
if (!sessionClosed && (owner === undefined || owner.canDeliver())) {
|
|
574
575
|
updateWorkspaceWidgetSafely(latestContext(), state, workspaceRowsByRequest);
|
|
575
576
|
}
|
|
576
577
|
},
|
|
@@ -632,21 +633,22 @@ export function registerIsolatedExtension(pi: ExtensionAPI, options: RegisterIso
|
|
|
632
633
|
: undefined;
|
|
633
634
|
const canDeliver = () => deliverySuppression() === undefined;
|
|
634
635
|
const controller = new AbortController();
|
|
636
|
+
let durableAcknowledged = false;
|
|
635
637
|
activeJobs.add(controller);
|
|
636
|
-
|
|
638
|
+
const owner = { canDeliver, pendingResult: () => durableAcknowledged && !controller.signal.aborted && canDeliver() };
|
|
639
|
+
jobOwners.set(key, owner);
|
|
637
640
|
const notified = new Set<string>();
|
|
638
641
|
const attentionNotified = new Set<string>();
|
|
639
642
|
let candidateListener: ((state: RunState) => void) | undefined;
|
|
640
643
|
const finish = () => {
|
|
641
644
|
activeJobs.delete(controller);
|
|
642
645
|
if (candidateListener) stateListeners.delete(candidateListener);
|
|
643
|
-
if (jobOwners.get(key) ===
|
|
646
|
+
if (jobOwners.get(key) === owner) jobOwners.delete(key);
|
|
644
647
|
};
|
|
645
648
|
const abortBeforeAck = () => controller.abort(signal?.reason);
|
|
646
649
|
if (signal?.aborted) abortBeforeAck();
|
|
647
650
|
else signal?.addEventListener("abort", abortBeforeAck, { once: true });
|
|
648
651
|
let latestState: RunState | undefined;
|
|
649
|
-
let durableAcknowledged = false;
|
|
650
652
|
let accept!: (state: RunState) => void;
|
|
651
653
|
let reject!: (error: unknown) => void;
|
|
652
654
|
const durable = new Promise<RunState>((resolve, fail) => { accept = resolve; reject = fail; });
|
|
@@ -828,6 +830,8 @@ export function registerIsolatedExtension(pi: ExtensionAPI, options: RegisterIso
|
|
|
828
830
|
},
|
|
829
831
|
});
|
|
830
832
|
return {
|
|
833
|
+
hasPendingResult: (sessionId) => latestCtx?.sessionManager?.getSessionId() === sessionId
|
|
834
|
+
&& [...jobOwners.values()].some((owner) => owner.pendingResult()),
|
|
831
835
|
async inventory(cwd, current) {
|
|
832
836
|
const root = await lookupRoot(cwd);
|
|
833
837
|
const revision = savedRevision;
|
package/extensions/subagent.ts
CHANGED
|
@@ -256,6 +256,7 @@ export default function subagentExtension(pi: ExtensionAPI): void {
|
|
|
256
256
|
let directSequence = 0;
|
|
257
257
|
const directTasks = new Map<string, {
|
|
258
258
|
controller: AbortController; settled: Promise<void>; handles: DirectHandle[]; tabs: DirectTabRecord[];
|
|
259
|
+
pendingResult?: (sessionId: string) => boolean;
|
|
259
260
|
close?: (current: () => boolean) => Promise<void>; closeScope?: () => boolean; allocation?: DirectAllocationRecord; recovery?: string;
|
|
260
261
|
/** Saved sessions this task continues; claimed before any await so a second request cannot reuse them. */
|
|
261
262
|
continues?: ReadonlySet<string>;
|
|
@@ -268,6 +269,14 @@ export default function subagentExtension(pi: ExtensionAPI): void {
|
|
|
268
269
|
&& tab.paneId === saved.paneId && tab.sessionFile === saved.sessionFile;
|
|
269
270
|
});
|
|
270
271
|
let sessionEpoch = 0;
|
|
272
|
+
pi.events.on("pi-subagent:pending-result", (data) => {
|
|
273
|
+
if (!data || typeof data !== "object" || !("sessionId" in data) || typeof data.sessionId !== "string"
|
|
274
|
+
|| !("pending" in data) || typeof data.pending !== "boolean") return;
|
|
275
|
+
const { sessionId } = data;
|
|
276
|
+
if ([...directTasks.values()].some((task) => task.pendingResult?.(sessionId)) || isolatedSurface.hasPendingResult(sessionId)) {
|
|
277
|
+
data.pending = true;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
271
280
|
// Shutdown still saves outgoing identities; only a session start defers records to its carry loop.
|
|
272
281
|
let directRecordEpoch = 0;
|
|
273
282
|
let directWorkflow = 0;
|
|
@@ -767,6 +776,7 @@ export default function subagentExtension(pi: ExtensionAPI): void {
|
|
|
767
776
|
});
|
|
768
777
|
const controller = new AbortController();
|
|
769
778
|
const launchEpoch = sessionEpoch;
|
|
779
|
+
const launchSessionId = ctx.sessionManager.getSessionId();
|
|
770
780
|
const recordEpoch = directRecordEpoch;
|
|
771
781
|
const handles: DirectHandle[] = [];
|
|
772
782
|
const tabs: DirectTabRecord[] = [];
|
|
@@ -887,6 +897,9 @@ export default function subagentExtension(pi: ExtensionAPI): void {
|
|
|
887
897
|
resolveSettled();
|
|
888
898
|
throw boundedError(error);
|
|
889
899
|
} finally { signal?.removeEventListener("abort", abortLaunch); }
|
|
900
|
+
local.pendingResult = (sessionId) => !controller.signal.aborted && launchEpoch === sessionEpoch
|
|
901
|
+
&& sessionId === launchSessionId && ctx.sessionManager.getSessionId() === launchSessionId
|
|
902
|
+
&& latestCtx?.sessionManager.getSessionId() === launchSessionId;
|
|
890
903
|
void (async () => {
|
|
891
904
|
try {
|
|
892
905
|
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
@@ -944,6 +957,7 @@ export default function subagentExtension(pi: ExtensionAPI): void {
|
|
|
944
957
|
try { await local.close!(() => directTasks.get(taskId) === local); }
|
|
945
958
|
catch { /* Retain ownership and admission for an explicit guarded retry. */ }
|
|
946
959
|
} else directTasks.delete(taskId);
|
|
960
|
+
local.pendingResult = undefined;
|
|
947
961
|
if (!controller.signal.aborted) reportDirect(launchEpoch, taskId, ref, workflow.mode, [...states.values()], tabs, local.recovery);
|
|
948
962
|
resolveSettled();
|
|
949
963
|
}
|