@oyasmi/pipiclaw 0.9.3-beta.3 → 0.9.3-beta.4
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/dist/agent/channel-runner.d.ts +4 -18
- package/dist/agent/channel-runner.js +25 -120
- package/dist/agent/context-budget.js +1 -1
- package/dist/agent/job-manager.d.ts +11 -17
- package/dist/agent/job-manager.js +76 -65
- package/dist/agent/prompt/builder.js +1 -17
- package/dist/agent/prompt/manifest.d.ts +0 -1
- package/dist/agent/prompt/manifest.js +2 -2
- package/dist/agent/prompt/sections.js +0 -9
- package/dist/agent/prompt/types.d.ts +2 -18
- package/dist/agent/prompt/types.js +1 -1
- package/dist/agent/runner-factory.d.ts +4 -0
- package/dist/agent/session-adapter.d.ts +19 -0
- package/dist/agent/session-adapter.js +77 -0
- package/dist/app-services.d.ts +18 -0
- package/dist/app-services.js +43 -0
- package/dist/{runtime → events}/events.d.ts +1 -1
- package/dist/memory/journal.js +1 -1
- package/dist/memory/maintenance-gates.d.ts +0 -3
- package/dist/memory/maintenance-gates.js +2 -2
- package/dist/memory/maintenance-migration.d.ts +5 -0
- package/dist/memory/maintenance-migration.js +91 -0
- package/dist/memory/maintenance-state.js +5 -44
- package/dist/memory/migrate.js +1 -1
- package/dist/memory/reflect-job.d.ts +1 -9
- package/dist/memory/reflect-job.js +1 -3
- package/dist/memory/reflect.js +1 -1
- package/dist/memory/session-corpus.js +1 -1
- package/dist/memory/store.js +1 -1
- package/dist/models/auth-cli.js +1 -1
- package/dist/runtime/bootstrap.d.ts +5 -15
- package/dist/runtime/bootstrap.js +32 -56
- package/dist/runtime/subagent-commands.d.ts +2 -1
- package/dist/runtime/subagent-commands.js +10 -12
- package/dist/runtime/task-wake.d.ts +4 -5
- package/dist/runtime/task-wake.js +2 -12
- package/dist/security/types.d.ts +7 -0
- package/dist/security/types.js +16 -1
- package/dist/settings.d.ts +8 -0
- package/dist/settings.js +24 -1
- package/dist/shared/exact-keys.d.ts +13 -0
- package/dist/shared/exact-keys.js +1 -0
- package/dist/shared/format.d.ts +20 -0
- package/dist/shared/format.js +64 -0
- package/dist/shared/{fs-utils.d.ts → os.d.ts} +6 -0
- package/dist/shared/{fs-utils.js → os.js} +8 -0
- package/dist/subagents/discovery.d.ts +16 -3
- package/dist/subagents/discovery.js +9 -7
- package/dist/subagents/external/run.d.ts +2 -1
- package/dist/subagents/external/run.js +1 -2
- package/dist/subagents/external/settlement.js +10 -5
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/runs.d.ts +13 -12
- package/dist/subagents/runs.js +108 -89
- package/dist/subagents/tool.d.ts +7 -7
- package/dist/subagents/tool.js +11 -11
- package/dist/tasks/brief.js +1 -0
- package/dist/tools/command-optimizer.js +1 -1
- package/dist/tools/config.d.ts +7 -0
- package/dist/tools/config.js +16 -1
- package/dist/tools/event-manage.d.ts +1 -1
- package/dist/tools/event-manage.js +3 -3
- package/dist/tools/grep.js +1 -1
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.js +4 -2
- package/dist/tools/job.js +1 -1
- package/dist/tools/read.js +1 -1
- package/dist/tools/registry.d.ts +3 -3
- package/dist/tools/registry.js +18 -56
- package/dist/tools/skill.js +1 -1
- package/dist/tools/subagent-manage.d.ts +2 -0
- package/dist/tools/subagent-manage.js +7 -7
- package/dist/tools/subagent-tool-names.d.ts +2 -2
- package/dist/tools/subagent-tool-names.js +2 -2
- package/dist/tools/task-manage/shared.js +4 -9
- package/dist/tools/task-manage/types.d.ts +4 -0
- package/dist/tui/app.js +22 -7
- package/dist/web/config.js +1 -1
- package/package.json +2 -1
- package/dist/shared/duration.d.ts +0 -2
- package/dist/shared/duration.js +0 -7
- package/dist/shared/file-stamp.d.ts +0 -10
- package/dist/shared/file-stamp.js +0 -19
- package/dist/shared/numeric.d.ts +0 -7
- package/dist/shared/numeric.js +0 -19
- package/dist/shared/shell-escape.d.ts +0 -5
- package/dist/shared/shell-escape.js +0 -7
- package/dist/shared/token-estimate.d.ts +0 -1
- package/dist/shared/token-estimate.js +0 -19
- /package/dist/{runtime → events}/event-commands.d.ts +0 -0
- /package/dist/{runtime → events}/event-commands.js +0 -0
- /package/dist/{runtime → events}/event-validation.d.ts +0 -0
- /package/dist/{runtime → events}/event-validation.js +0 -0
- /package/dist/{runtime → events}/events.js +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Small, dependency-free helpers for talking to the host filesystem and shell. */
|
|
1
2
|
export declare function isNodeError(error: unknown): error is NodeJS.ErrnoException;
|
|
2
3
|
/**
|
|
3
4
|
* Read a UTF-8 text file, returning "" when it does not exist.
|
|
@@ -6,3 +7,8 @@ export declare function isNodeError(error: unknown): error is NodeJS.ErrnoExcept
|
|
|
6
7
|
* silently treat a real read failure as an absent file.
|
|
7
8
|
*/
|
|
8
9
|
export declare function readOptionalTextFile(path: string): Promise<string>;
|
|
10
|
+
/**
|
|
11
|
+
* Shell-escape a string for safe use in sh -c commands.
|
|
12
|
+
* Wraps in single quotes and escapes internal single quotes.
|
|
13
|
+
*/
|
|
14
|
+
export declare function shellEscape(s: string): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { readFile } from "fs/promises";
|
|
2
|
+
/** Small, dependency-free helpers for talking to the host filesystem and shell. */
|
|
2
3
|
export function isNodeError(error) {
|
|
3
4
|
return error instanceof Error && "code" in error;
|
|
4
5
|
}
|
|
@@ -19,3 +20,10 @@ export async function readOptionalTextFile(path) {
|
|
|
19
20
|
throw error;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Shell-escape a string for safe use in sh -c commands.
|
|
25
|
+
* Wraps in single quotes and escapes internal single quotes.
|
|
26
|
+
*/
|
|
27
|
+
export function shellEscape(s) {
|
|
28
|
+
return `'${s.replace(/'/g, "'\\''")}'`;
|
|
29
|
+
}
|
|
@@ -52,7 +52,7 @@ export type SubAgentEffort = keyof typeof SUB_AGENT_EFFORT_PRESETS;
|
|
|
52
52
|
*/
|
|
53
53
|
declare const ALLOWED_CONTEXT_CHOICES: readonly ["none", "index"];
|
|
54
54
|
export type SubAgentContextChoice = (typeof ALLOWED_CONTEXT_CHOICES)[number];
|
|
55
|
-
|
|
55
|
+
interface SubAgentConfigFields {
|
|
56
56
|
name: string;
|
|
57
57
|
description: string;
|
|
58
58
|
systemPrompt: string;
|
|
@@ -92,10 +92,23 @@ export interface SubAgentConfig {
|
|
|
92
92
|
* The role is still listed — never silently dropped (D5) — and invocation must explain why. */
|
|
93
93
|
unavailable?: string;
|
|
94
94
|
}
|
|
95
|
-
export interface
|
|
95
|
+
export interface InternalSubAgentConfig extends SubAgentConfigFields {
|
|
96
|
+
runtime: "internal";
|
|
97
|
+
}
|
|
98
|
+
export interface ExternalSubAgentConfig extends SubAgentConfigFields {
|
|
99
|
+
runtime: "external";
|
|
100
|
+
harness: SubAgentHarness;
|
|
101
|
+
command: string;
|
|
102
|
+
mutates: SubAgentMutates;
|
|
103
|
+
model?: never;
|
|
104
|
+
modelRef?: never;
|
|
105
|
+
}
|
|
106
|
+
export type SubAgentConfig = InternalSubAgentConfig | ExternalSubAgentConfig;
|
|
107
|
+
export interface ResolvedInternalSubAgentConfig extends InternalSubAgentConfig {
|
|
96
108
|
model: Model<Api>;
|
|
97
109
|
modelRef: string;
|
|
98
110
|
}
|
|
111
|
+
export type ResolvedSubAgentConfig = ResolvedInternalSubAgentConfig | ExternalSubAgentConfig;
|
|
99
112
|
export interface SubAgentDiscoveryResult {
|
|
100
113
|
directory: string;
|
|
101
114
|
agents: SubAgentConfig[];
|
|
@@ -192,7 +205,7 @@ subagentDefaultModelRef?: string): {
|
|
|
192
205
|
export declare function resolveInlineAgent(availableModels: Model<Api>[], currentModel: Model<Api>, overrides: InlineAgentOverrides,
|
|
193
206
|
/** `settings.subagentModel` (spec 032 D5): used only when this call does not name a model. */
|
|
194
207
|
subagentDefaultModelRef?: string): {
|
|
195
|
-
config?:
|
|
208
|
+
config?: ResolvedInternalSubAgentConfig;
|
|
196
209
|
error?: string;
|
|
197
210
|
warning?: string;
|
|
198
211
|
};
|
|
@@ -635,6 +635,14 @@ subagentDefaultModelRef) {
|
|
|
635
635
|
if (baseConfig.unavailable) {
|
|
636
636
|
return { error: `Sub-agent "${baseConfig.name}" is currently unavailable: ${baseConfig.unavailable}` };
|
|
637
637
|
}
|
|
638
|
+
if (baseConfig.runtime === "external") {
|
|
639
|
+
return {
|
|
640
|
+
config: {
|
|
641
|
+
...baseConfig,
|
|
642
|
+
thinkingLevel: baseConfig.thinkingLevel ?? (overrides.purpose === "verify" ? DEFAULT_THINKING_LEVEL : undefined),
|
|
643
|
+
},
|
|
644
|
+
};
|
|
645
|
+
}
|
|
638
646
|
let model = baseConfig.model;
|
|
639
647
|
let modelRef = baseConfig.modelRef;
|
|
640
648
|
if (!model && subagentDefaultModelRef) {
|
|
@@ -645,13 +653,7 @@ subagentDefaultModelRef) {
|
|
|
645
653
|
model = resolved.model;
|
|
646
654
|
modelRef = formatModelReference(resolved.model);
|
|
647
655
|
}
|
|
648
|
-
const
|
|
649
|
-
// External work is not defaulted: pipiclaw has no standing to pick a reasoning effort for
|
|
650
|
-
// another CLI's own configuration. External verify still defaults — it is the last unattended
|
|
651
|
-
// gate before an attestation is trusted, and "whatever that machine happens to have configured"
|
|
652
|
-
// is not an acceptable substitute for real reasoning.
|
|
653
|
-
const thinkingLevel = baseConfig.thinkingLevel ??
|
|
654
|
-
(baseConfig.runtime === "external" && purpose !== "verify" ? undefined : DEFAULT_THINKING_LEVEL);
|
|
656
|
+
const thinkingLevel = baseConfig.thinkingLevel ?? DEFAULT_THINKING_LEVEL;
|
|
655
657
|
const mutates = baseConfig.mutates ?? inferMutatesFromTools(baseConfig.tools);
|
|
656
658
|
return {
|
|
657
659
|
config: {
|
|
@@ -2,7 +2,7 @@ import { spawn as nodeSpawn } from "node:child_process";
|
|
|
2
2
|
import type { SecurityConfig } from "../../security/types.js";
|
|
3
3
|
import { probeCliVersion, readProcessStartTime } from "../../shared/host-process.js";
|
|
4
4
|
import type { SubAgentThinkingLevel } from "../discovery.js";
|
|
5
|
-
import {
|
|
5
|
+
import type { RunMutates, SubAgentRunManager } from "../runs.js";
|
|
6
6
|
/**
|
|
7
7
|
* The external-run orchestrator (spec 040, D1/D3/D4). One prompt, one short-lived, argv-direct,
|
|
8
8
|
* detached process. The launch order matters (D1): admission and the lease are the caller's job
|
|
@@ -15,6 +15,7 @@ import { type RunMutates } from "../runs.js";
|
|
|
15
15
|
* `sweep()`) reads the same files back once the process is confirmed gone.
|
|
16
16
|
*/
|
|
17
17
|
export interface LaunchExternalRunInput {
|
|
18
|
+
runManager: SubAgentRunManager;
|
|
18
19
|
runId: string;
|
|
19
20
|
channelId: string;
|
|
20
21
|
/** Needed only for `purpose=verify`: where the attestation gets written. */
|
|
@@ -9,7 +9,6 @@ import { splitShellWords } from "../../shared/shell-words.js";
|
|
|
9
9
|
import { errorMessage } from "../../shared/text-utils.js";
|
|
10
10
|
import { createEmptyUsageTotals } from "../../shared/types.js";
|
|
11
11
|
import { workspaceSubjectSnapshot } from "../../tasks/artifact-subject.js";
|
|
12
|
-
import { getSubAgentRunManager } from "../runs.js";
|
|
13
12
|
import { getExternalHarness } from "./registry.js";
|
|
14
13
|
import { finalizeExternalRun } from "./settlement.js";
|
|
15
14
|
/**
|
|
@@ -151,7 +150,7 @@ export async function launchExternalRun(input) {
|
|
|
151
150
|
// or host-wide cap still left behind an `external-agent` audit record claiming a process that
|
|
152
151
|
// never existed. `external-agent` records what was *executed* (D8.1), so the record must not
|
|
153
152
|
// precede the point where this run is actually admitted to run.
|
|
154
|
-
const runManager =
|
|
153
|
+
const runManager = input.runManager;
|
|
155
154
|
await runManager.register({
|
|
156
155
|
runId: input.runId,
|
|
157
156
|
channelId: input.channelId,
|
|
@@ -120,8 +120,13 @@ export async function finalizeExternalRun(input, settle, options) {
|
|
|
120
120
|
finalText: outcome.finalText,
|
|
121
121
|
runFailed: false, // shouldVerify already gates on settleInput.status === "completed".
|
|
122
122
|
});
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
// `verifySubjectBefore` and `verifyBaseCommit` are always set together at launch (both come
|
|
124
|
+
// from the one all-or-nothing `workspaceSubjectSnapshot()` call in `external/run.ts`), so a
|
|
125
|
+
// freshly launched run can never carry a subject hash without a base commit — there is no
|
|
126
|
+
// live path left that produces a "legacy-head" attestation. `verification.ts` still reads and
|
|
127
|
+
// compares `legacy-head` attestations written before base-relative subjects existed; this is
|
|
128
|
+
// only the write side, for a run happening now.
|
|
129
|
+
const subjectAvailable = !verification.workspaceChanged && subjectAfter !== undefined && baseSubjectAvailable;
|
|
125
130
|
await writeVerificationAttestation(input.channelDir, {
|
|
126
131
|
runId: input.runId,
|
|
127
132
|
taskId: input.taskId,
|
|
@@ -131,9 +136,9 @@ export async function finalizeExternalRun(input, settle, options) {
|
|
|
131
136
|
workspaceChanged: verification.workspaceChanged,
|
|
132
137
|
subjectHash: subjectAvailable ? subjectAfter : undefined,
|
|
133
138
|
subjectDir: subjectAvailable ? input.workingDirectory : undefined,
|
|
134
|
-
subjectMode: subjectAvailable ?
|
|
135
|
-
subjectBaseCommit: subjectAvailable
|
|
136
|
-
subjectBaselineUntrackedPaths: subjectAvailable
|
|
139
|
+
subjectMode: subjectAvailable ? "base-relative" : undefined,
|
|
140
|
+
subjectBaseCommit: subjectAvailable ? input.verifyBaseCommit : undefined,
|
|
141
|
+
subjectBaselineUntrackedPaths: subjectAvailable ? input.verifyBaselineUntrackedPaths : undefined,
|
|
137
142
|
// External verifiers cannot have their tools structurally removed the way an internal
|
|
138
143
|
// verifier's are — advisory, not enforced (D9).
|
|
139
144
|
verificationStrength: "advisory",
|
package/dist/subagents/format.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { formatDuration } from "../shared/
|
|
1
|
+
import { formatDuration } from "../shared/format.js";
|
|
2
2
|
/**
|
|
3
3
|
* Shared display primitives for delegation runs (spec 041), used by both the model-facing
|
|
4
4
|
* `subagent_list`/`subagent_run` tools and the human-facing `/subagents` runtime command. Only the pieces that
|
package/dist/subagents/runs.d.ts
CHANGED
|
@@ -240,6 +240,8 @@ export interface RunManagerOptions {
|
|
|
240
240
|
notify?: RunNotifier;
|
|
241
241
|
ledger?: UsageLedger;
|
|
242
242
|
store?: ChannelStore;
|
|
243
|
+
/** Application-wide admission count, supplied by the owning runtime. */
|
|
244
|
+
runningCount?: () => number;
|
|
243
245
|
}
|
|
244
246
|
/**
|
|
245
247
|
* Per-channel manager for delegation runs — the single settlement/usage/wake authority for both
|
|
@@ -264,9 +266,12 @@ export declare class SubAgentRunManager {
|
|
|
264
266
|
/** Live external runs' tail-the-artifact-file progress pollers (P1a). Never persisted or
|
|
265
267
|
* reconstructed on restore — an adopted run has no fresher information to poll for than
|
|
266
268
|
* restart reconciliation already reads once from the finished process's own output. */
|
|
269
|
+
private stopped;
|
|
267
270
|
private readonly externalProgressTails;
|
|
268
271
|
private readonly queue;
|
|
269
272
|
constructor(channelId: string, options: RunManagerOptions);
|
|
273
|
+
/** Release observation timers without cancelling dispatched work or its settlement. */
|
|
274
|
+
stop(): void;
|
|
270
275
|
private recordDir;
|
|
271
276
|
private recordPath;
|
|
272
277
|
private persist;
|
|
@@ -427,16 +432,12 @@ export declare class SubAgentRunManager {
|
|
|
427
432
|
private collectGarbageIfExpired;
|
|
428
433
|
collectGarbage(now?: number): Promise<void>;
|
|
429
434
|
}
|
|
430
|
-
/**
|
|
431
|
-
export declare function
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
* like `restoreChannelJobs` — but by reading the records rather than trusting directory names,
|
|
439
|
-
* so a channel is adopted under the id the rest of the runtime can act on.
|
|
440
|
-
*/
|
|
441
|
-
export declare function restoreAllSubAgentRuns(): Promise<number>;
|
|
435
|
+
/** Owns this application's channel managers and their maintenance timer. */
|
|
436
|
+
export declare function createSubAgentRuntime(options?: RunManagerOptions): {
|
|
437
|
+
get: (channelId: string) => SubAgentRunManager;
|
|
438
|
+
restore: () => Promise<number>;
|
|
439
|
+
start: () => void;
|
|
440
|
+
stop: () => void;
|
|
441
|
+
};
|
|
442
|
+
export type SubAgentRuntime = ReturnType<typeof createSubAgentRuntime>;
|
|
442
443
|
export {};
|
package/dist/subagents/runs.js
CHANGED
|
@@ -3,7 +3,7 @@ import { dirname, join, relative, sep } from "node:path";
|
|
|
3
3
|
import { getChannelDir, isChannelId } from "../channel/channel-paths.js";
|
|
4
4
|
import * as log from "../log.js";
|
|
5
5
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
6
|
-
import { formatDuration } from "../shared/
|
|
6
|
+
import { formatDuration } from "../shared/format.js";
|
|
7
7
|
import { isProcessAlive, killProcessGroup, readProcessStartTime } from "../shared/host-process.js";
|
|
8
8
|
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
9
9
|
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
@@ -164,9 +164,18 @@ export class SubAgentRunManager {
|
|
|
164
164
|
/** Live external runs' tail-the-artifact-file progress pollers (P1a). Never persisted or
|
|
165
165
|
* reconstructed on restore — an adopted run has no fresher information to poll for than
|
|
166
166
|
* restart reconciliation already reads once from the finished process's own output. */
|
|
167
|
+
this.stopped = false;
|
|
167
168
|
this.externalProgressTails = new Map();
|
|
168
169
|
this.queue = createSerialQueue();
|
|
169
170
|
}
|
|
171
|
+
/** Release observation timers without cancelling dispatched work or its settlement. */
|
|
172
|
+
stop() {
|
|
173
|
+
this.stopped = true;
|
|
174
|
+
for (const runId of this.externalRecoveryTimers.keys())
|
|
175
|
+
this.clearExternalRecoveryTimer(runId);
|
|
176
|
+
for (const runId of this.externalProgressTails.keys())
|
|
177
|
+
this.stopExternalProgressTail(runId);
|
|
178
|
+
}
|
|
170
179
|
recordDir() {
|
|
171
180
|
// `getChannelDir`, not a raw join: a DingTalk group id routinely contains `/`, which used
|
|
172
181
|
// to nest the records one directory deeper than the startup scan ever looked — so a
|
|
@@ -221,6 +230,8 @@ export class SubAgentRunManager {
|
|
|
221
230
|
/** Start tailing a just-launched external run's artifact file for a human-readable step label
|
|
222
231
|
* (P1a). Best-effort only — see `progress-tail.ts` for the polling/pacing contract. */
|
|
223
232
|
startExternalProgressTail(record) {
|
|
233
|
+
if (this.stopped)
|
|
234
|
+
return;
|
|
224
235
|
if (!record.harness)
|
|
225
236
|
return;
|
|
226
237
|
this.stopExternalProgressTail(record.runId);
|
|
@@ -321,7 +332,7 @@ export class SubAgentRunManager {
|
|
|
321
332
|
throw new RecoverableToolError(`Too many delegation runs already running on this channel (>= ${MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL}). ` +
|
|
322
333
|
"Wait for one to finish, or cancel one with subagent_run op=cancel first.");
|
|
323
334
|
}
|
|
324
|
-
if (
|
|
335
|
+
if ((this.options.runningCount?.() ?? this.runningCount()) >= MAX_RUNNING_SUBAGENT_RUNS_PER_HOST) {
|
|
325
336
|
throw new RecoverableToolError(`Too many delegation runs already running host-wide (>= ${MAX_RUNNING_SUBAGENT_RUNS_PER_HOST}). ` +
|
|
326
337
|
"Wait for one to finish before dispatching another.");
|
|
327
338
|
}
|
|
@@ -1093,6 +1104,8 @@ export class SubAgentRunManager {
|
|
|
1093
1104
|
this.externalRecoveryTimers.delete(runId);
|
|
1094
1105
|
}
|
|
1095
1106
|
scheduleExternalRecovery(record) {
|
|
1107
|
+
if (this.stopped)
|
|
1108
|
+
return;
|
|
1096
1109
|
if (!record.deadlineAt)
|
|
1097
1110
|
return;
|
|
1098
1111
|
this.clearExternalRecoveryTimer(record.runId);
|
|
@@ -1130,61 +1143,6 @@ export class SubAgentRunManager {
|
|
|
1130
1143
|
}
|
|
1131
1144
|
}
|
|
1132
1145
|
}
|
|
1133
|
-
const managers = new Map();
|
|
1134
|
-
/** Host-wide running count for the D10.2 admission check, summed across every known channel. */
|
|
1135
|
-
function totalRunningSubAgentRuns() {
|
|
1136
|
-
let total = 0;
|
|
1137
|
-
for (const manager of managers.values()) {
|
|
1138
|
-
total += manager.runningCount();
|
|
1139
|
-
}
|
|
1140
|
-
return total;
|
|
1141
|
-
}
|
|
1142
|
-
let runtimeConfig = {};
|
|
1143
|
-
const GARBAGE_COLLECTION_INTERVAL_MS = 24 * 60 * 60_000;
|
|
1144
|
-
let garbageCollectionTimer;
|
|
1145
|
-
let collectingGarbage = false;
|
|
1146
|
-
async function collectGarbageAllChannels() {
|
|
1147
|
-
if (collectingGarbage)
|
|
1148
|
-
return;
|
|
1149
|
-
collectingGarbage = true;
|
|
1150
|
-
try {
|
|
1151
|
-
for (const manager of managers.values()) {
|
|
1152
|
-
await manager
|
|
1153
|
-
.collectGarbage()
|
|
1154
|
-
.catch((error) => log.logWarning("Sub-agent garbage collection failed", errorMessage(error)));
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
finally {
|
|
1158
|
-
collectingGarbage = false;
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
function startSubAgentGarbageCollector() {
|
|
1162
|
-
if (garbageCollectionTimer)
|
|
1163
|
-
clearInterval(garbageCollectionTimer);
|
|
1164
|
-
garbageCollectionTimer = setInterval(() => void collectGarbageAllChannels(), GARBAGE_COLLECTION_INTERVAL_MS);
|
|
1165
|
-
garbageCollectionTimer.unref?.();
|
|
1166
|
-
}
|
|
1167
|
-
/** Stop the host-wide daily garbage collector during shutdown or test teardown. */
|
|
1168
|
-
export function stopSubAgentGarbageCollector() {
|
|
1169
|
-
if (garbageCollectionTimer) {
|
|
1170
|
-
clearInterval(garbageCollectionTimer);
|
|
1171
|
-
garbageCollectionTimer = undefined;
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
/** Give runs their persistence root, wake delivery, ledger, and archive. Called once from bootstrap. */
|
|
1175
|
-
export function configureSubAgentRuntime(config) {
|
|
1176
|
-
runtimeConfig = config;
|
|
1177
|
-
stopSubAgentGarbageCollector();
|
|
1178
|
-
startSubAgentGarbageCollector();
|
|
1179
|
-
}
|
|
1180
|
-
export function getSubAgentRunManager(channelId) {
|
|
1181
|
-
let manager = managers.get(channelId);
|
|
1182
|
-
if (!manager) {
|
|
1183
|
-
manager = new SubAgentRunManager(channelId, runtimeConfig);
|
|
1184
|
-
managers.set(channelId, manager);
|
|
1185
|
-
}
|
|
1186
|
-
return manager;
|
|
1187
|
-
}
|
|
1188
1146
|
/**
|
|
1189
1147
|
* How deep the scan below looks for record files.
|
|
1190
1148
|
*
|
|
@@ -1265,43 +1223,104 @@ async function adoptRecordDirectory(stateDir, dir) {
|
|
|
1265
1223
|
}
|
|
1266
1224
|
return Array.from(channelIds);
|
|
1267
1225
|
}
|
|
1268
|
-
/**
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
*/
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
try {
|
|
1281
|
-
await mkdir(stateDir, { recursive: true });
|
|
1282
|
-
recordDirs = await findRecordDirectories(stateDir);
|
|
1283
|
-
}
|
|
1284
|
-
catch (error) {
|
|
1285
|
-
log.logWarning("Failed to scan persisted sub-agent runs", errorMessage(error));
|
|
1286
|
-
return 0;
|
|
1226
|
+
/** Owns this application's channel managers and their maintenance timer. */
|
|
1227
|
+
export function createSubAgentRuntime(options = {}) {
|
|
1228
|
+
const config = { ...options };
|
|
1229
|
+
const managers = new Map();
|
|
1230
|
+
let stopped = false;
|
|
1231
|
+
/** Host-wide running count for the D10.2 admission check, summed across every known channel. */
|
|
1232
|
+
function totalRunningSubAgentRuns() {
|
|
1233
|
+
let total = 0;
|
|
1234
|
+
for (const manager of managers.values()) {
|
|
1235
|
+
total += manager.runningCount();
|
|
1236
|
+
}
|
|
1237
|
+
return total;
|
|
1287
1238
|
}
|
|
1288
|
-
|
|
1239
|
+
const GARBAGE_COLLECTION_INTERVAL_MS = 24 * 60 * 60_000;
|
|
1240
|
+
let garbageCollectionTimer;
|
|
1241
|
+
let collectingGarbage = false;
|
|
1242
|
+
async function collectGarbageAllChannels() {
|
|
1243
|
+
if (collectingGarbage)
|
|
1244
|
+
return;
|
|
1245
|
+
collectingGarbage = true;
|
|
1289
1246
|
try {
|
|
1290
|
-
for (const
|
|
1291
|
-
|
|
1247
|
+
for (const manager of managers.values()) {
|
|
1248
|
+
await manager
|
|
1249
|
+
.collectGarbage()
|
|
1250
|
+
.catch((error) => log.logWarning("Sub-agent garbage collection failed", errorMessage(error)));
|
|
1251
|
+
}
|
|
1292
1252
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1253
|
+
finally {
|
|
1254
|
+
collectingGarbage = false;
|
|
1295
1255
|
}
|
|
1296
1256
|
}
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1257
|
+
function startSubAgentGarbageCollector() {
|
|
1258
|
+
if (stopped)
|
|
1259
|
+
return;
|
|
1260
|
+
if (garbageCollectionTimer)
|
|
1261
|
+
clearInterval(garbageCollectionTimer);
|
|
1262
|
+
garbageCollectionTimer = setInterval(() => void collectGarbageAllChannels(), GARBAGE_COLLECTION_INTERVAL_MS);
|
|
1263
|
+
garbageCollectionTimer.unref?.();
|
|
1264
|
+
}
|
|
1265
|
+
/** Stop the host-wide daily garbage collector during shutdown or test teardown. */
|
|
1266
|
+
function stop() {
|
|
1267
|
+
stopped = true;
|
|
1268
|
+
for (const manager of managers.values())
|
|
1269
|
+
manager.stop();
|
|
1270
|
+
if (garbageCollectionTimer) {
|
|
1271
|
+
clearInterval(garbageCollectionTimer);
|
|
1272
|
+
garbageCollectionTimer = undefined;
|
|
1273
|
+
}
|
|
1300
1274
|
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1275
|
+
function get(channelId) {
|
|
1276
|
+
let manager = managers.get(channelId);
|
|
1277
|
+
if (!manager) {
|
|
1278
|
+
manager = new SubAgentRunManager(channelId, { ...config, runningCount: totalRunningSubAgentRuns });
|
|
1279
|
+
managers.set(channelId, manager);
|
|
1280
|
+
if (stopped)
|
|
1281
|
+
manager.stop();
|
|
1282
|
+
}
|
|
1283
|
+
return manager;
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Re-adopt every channel's persisted runs at startup, settling any internal run still marked
|
|
1287
|
+
* `running` as `lost` (D10.3). Channels are discovered from the state directory itself, exactly
|
|
1288
|
+
* like `restoreChannelJobs` — but by reading the records rather than trusting directory names,
|
|
1289
|
+
* so a channel is adopted under the id the rest of the runtime can act on.
|
|
1290
|
+
*/
|
|
1291
|
+
async function restore() {
|
|
1292
|
+
const stateDir = config.stateDir;
|
|
1293
|
+
if (!stateDir)
|
|
1294
|
+
return 0;
|
|
1295
|
+
const channelIds = new Set();
|
|
1296
|
+
let recordDirs;
|
|
1297
|
+
try {
|
|
1298
|
+
await mkdir(stateDir, { recursive: true });
|
|
1299
|
+
recordDirs = await findRecordDirectories(stateDir);
|
|
1300
|
+
}
|
|
1301
|
+
catch (error) {
|
|
1302
|
+
log.logWarning("Failed to scan persisted sub-agent runs", errorMessage(error));
|
|
1303
|
+
return 0;
|
|
1304
|
+
}
|
|
1305
|
+
for (const dir of recordDirs) {
|
|
1306
|
+
try {
|
|
1307
|
+
for (const channelId of await adoptRecordDirectory(stateDir, dir))
|
|
1308
|
+
channelIds.add(channelId);
|
|
1309
|
+
}
|
|
1310
|
+
catch (error) {
|
|
1311
|
+
log.logWarning(`Failed to adopt sub-agent run records in ${relative(stateDir, dir)}`, errorMessage(error));
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
let restored = 0;
|
|
1315
|
+
for (const channelId of channelIds) {
|
|
1316
|
+
restored += await get(channelId).restore();
|
|
1317
|
+
}
|
|
1318
|
+
// Reclaim stale runs once as a host-wide startup batch; subsequent batches run daily.
|
|
1319
|
+
await collectGarbageAllChannels();
|
|
1320
|
+
if (restored > 0) {
|
|
1321
|
+
log.logInfo(`Restored ${restored} sub-agent run record(s)`);
|
|
1322
|
+
}
|
|
1323
|
+
return restored;
|
|
1305
1324
|
}
|
|
1306
|
-
return
|
|
1325
|
+
return { get, restore, start: startSubAgentGarbageCollector, stop };
|
|
1307
1326
|
}
|
package/dist/subagents/tool.d.ts
CHANGED
|
@@ -6,8 +6,8 @@ import type { FileStore } from "../file-store.js";
|
|
|
6
6
|
import type { SecurityConfig } from "../security/types.js";
|
|
7
7
|
import { type UsageTotals } from "../shared/types.js";
|
|
8
8
|
import type { PipiclawWebToolsConfig } from "../tools/config.js";
|
|
9
|
-
import { type ResolvedSubAgentConfig, type SubAgentConfig, type SubAgentDiscoveryResult } from "./discovery.js";
|
|
10
|
-
import {
|
|
9
|
+
import { type ResolvedInternalSubAgentConfig, type ResolvedSubAgentConfig, type SubAgentConfig, type SubAgentDiscoveryResult } from "./discovery.js";
|
|
10
|
+
import { type SubAgentRunManager } from "./runs.js";
|
|
11
11
|
/**
|
|
12
12
|
* Role-based delegation (spec 046, D2.1): the common path. No override fields beyond routing
|
|
13
13
|
* ones — tools, model, effort, context, thinkingLevel and mutates all live in the role file, so
|
|
@@ -94,7 +94,7 @@ export interface SubAgentToolOptions {
|
|
|
94
94
|
channelId: string;
|
|
95
95
|
};
|
|
96
96
|
createWorker?: (config: {
|
|
97
|
-
subAgent:
|
|
97
|
+
subAgent: ResolvedInternalSubAgentConfig;
|
|
98
98
|
apiKey: string;
|
|
99
99
|
tools: AgentTool<any>[];
|
|
100
100
|
}) => SubAgentWorker;
|
|
@@ -104,8 +104,8 @@ export interface SubAgentToolOptions {
|
|
|
104
104
|
convergenceWallClockMs?: number;
|
|
105
105
|
/** Test-only override for the D2 sync grace window; defaults to SYNC_GRACE_MS. */
|
|
106
106
|
syncGraceMs?: number;
|
|
107
|
-
/**
|
|
108
|
-
getRunManager
|
|
107
|
+
/** Channel manager owned by the application; also a seam for admission failures. */
|
|
108
|
+
getRunManager: (channelId: string) => SubAgentRunManager;
|
|
109
109
|
}
|
|
110
110
|
interface SubAgentWorker {
|
|
111
111
|
state: {
|
|
@@ -150,8 +150,8 @@ export interface SubAgentRunContext {
|
|
|
150
150
|
*/
|
|
151
151
|
export declare function assertWithinProjectBoundary(target: string, base: string, requested: string): void;
|
|
152
152
|
/**
|
|
153
|
-
* Build a sub-agent's tool set from the shared tool registry, filtered
|
|
154
|
-
*
|
|
153
|
+
* Build a sub-agent's tool set from the shared tool registry, filtered by the shared
|
|
154
|
+
* sub-agent whitelist (files + web). Sub-agents run with their own security context
|
|
155
155
|
* (rooted at the sub-agent workspace) and their own per-invocation bash timeout.
|
|
156
156
|
*/
|
|
157
157
|
declare function buildSubagentTools(executor: Executor, bashTimeoutSec: number, options: SubAgentToolOptions, runContext: SubAgentRunContext): AgentTool<any>[];
|
package/dist/subagents/tool.js
CHANGED
|
@@ -26,7 +26,7 @@ import { describeToolCall } from "../tools/presentation.js";
|
|
|
26
26
|
import { buildToolSet } from "../tools/registry.js";
|
|
27
27
|
import { DEFAULT_THINKING_LEVEL, externalRoleFingerprint, formatSubAgentList, resolveConfiguredRole, resolveInlineAgent, validateSubAgentTask, withSubAgentsDirWriteDeny, } from "./discovery.js";
|
|
28
28
|
import { launchExternalRun } from "./external/run.js";
|
|
29
|
-
import {
|
|
29
|
+
import { resolveSyncGraceMs } from "./runs.js";
|
|
30
30
|
import { resolveVerificationOutcome } from "./verification-outcome.js";
|
|
31
31
|
import { acquireWorkspaceLease, findWorkspaceLeaseHolder, formatWorkspaceLeaseConflict, releaseWorkspaceLease, } from "./workspace-lease.js";
|
|
32
32
|
/** Shared by both invocation surfaces (spec 046, D2.1). */
|
|
@@ -312,8 +312,8 @@ function withSubagentMemoryWriteDeny(securityConfig, channelDir, workspaceDir) {
|
|
|
312
312
|
}, workspaceDir);
|
|
313
313
|
}
|
|
314
314
|
/**
|
|
315
|
-
* Build a sub-agent's tool set from the shared tool registry, filtered
|
|
316
|
-
*
|
|
315
|
+
* Build a sub-agent's tool set from the shared tool registry, filtered by the shared
|
|
316
|
+
* sub-agent whitelist (files + web). Sub-agents run with their own security context
|
|
317
317
|
* (rooted at the sub-agent workspace) and their own per-invocation bash timeout.
|
|
318
318
|
*/
|
|
319
319
|
function buildSubagentTools(executor, bashTimeoutSec, options, runContext) {
|
|
@@ -452,7 +452,10 @@ function createDetails(input) {
|
|
|
452
452
|
return {
|
|
453
453
|
agent: input.config.name,
|
|
454
454
|
source: input.config.source,
|
|
455
|
-
model: input.modelOverride ??
|
|
455
|
+
model: input.modelOverride ??
|
|
456
|
+
(input.config.runtime === "external"
|
|
457
|
+
? (input.config.externalModelRef ?? "default")
|
|
458
|
+
: formatModelReference(input.config.model)),
|
|
456
459
|
tools: input.toolsOverride ?? [...input.config.tools],
|
|
457
460
|
turns: input.turns,
|
|
458
461
|
toolCalls: input.toolCalls,
|
|
@@ -492,7 +495,7 @@ async function dispatchSubAgentRun(options, currentModel, config, warning, param
|
|
|
492
495
|
// Only ever set for an inline (internal) delegation with `bash` in `tools` and no explicit
|
|
493
496
|
// `mutates` — surfaced to the model, not just a log, since it has no role file to fix.
|
|
494
497
|
const mutatesNote = warning ? `Note: ${warning}\n\n` : "";
|
|
495
|
-
const runManager =
|
|
498
|
+
const runManager = options.getRunManager(options.runtimeContext.channelId);
|
|
496
499
|
// A short, human-typeable id (spec 041) — never the dispatching tool call's own id, which
|
|
497
500
|
// on some providers is a long `call_<id>|fc_<id>` composite unreadable in a chat UI.
|
|
498
501
|
const runContext = await prepareRunContext(runManager.mintRunId(), params, options);
|
|
@@ -516,7 +519,7 @@ async function dispatchSubAgentRun(options, currentModel, config, warning, param
|
|
|
516
519
|
leaseKey = lease.leaseKey;
|
|
517
520
|
}
|
|
518
521
|
if (config.runtime === "external") {
|
|
519
|
-
return dispatchExternalRun({ options, currentModel, config, runLabel, runContext, leaseKey, params });
|
|
522
|
+
return dispatchExternalRun({ options, currentModel, config, runLabel, runContext, leaseKey, params, runManager });
|
|
520
523
|
}
|
|
521
524
|
return dispatchInternalRun({
|
|
522
525
|
options,
|
|
@@ -538,10 +541,6 @@ async function dispatchSubAgentRun(options, currentModel, config, warning, param
|
|
|
538
541
|
*/
|
|
539
542
|
async function dispatchExternalRun(input) {
|
|
540
543
|
const { options, config, runLabel, runContext, leaseKey, params } = input;
|
|
541
|
-
if (!config.harness) {
|
|
542
|
-
releaseWorkspaceLease(leaseKey, runContext.runId);
|
|
543
|
-
throw new Error(`Sub-agent "${config.name}" has runtime: external but no harness configured.`);
|
|
544
|
-
}
|
|
545
544
|
let launchResult;
|
|
546
545
|
try {
|
|
547
546
|
// D9/T5: external roles get the same task envelope internal workers do — runtime
|
|
@@ -550,6 +549,7 @@ async function dispatchExternalRun(input) {
|
|
|
550
549
|
const contextualBlocks = await resolveContextualBlocks(config, options);
|
|
551
550
|
const envelopedTask = buildSubAgentTask(params.task, config, options.runtimeContext, contextualBlocks, runContext);
|
|
552
551
|
launchResult = await launchExternalRun({
|
|
552
|
+
runManager: input.runManager,
|
|
553
553
|
runId: runContext.runId,
|
|
554
554
|
channelId: options.runtimeContext.channelId,
|
|
555
555
|
channelDir: options.channelDir,
|
|
@@ -557,7 +557,7 @@ async function dispatchExternalRun(input) {
|
|
|
557
557
|
agent: config.name,
|
|
558
558
|
source: config.source,
|
|
559
559
|
harness: config.harness,
|
|
560
|
-
command: config.command
|
|
560
|
+
command: config.command,
|
|
561
561
|
shell: config.shell,
|
|
562
562
|
env: config.env,
|
|
563
563
|
externalModelRef: config.externalModelRef,
|
package/dist/tasks/brief.js
CHANGED
|
@@ -43,6 +43,7 @@ export async function buildTaskStepBrief(input) {
|
|
|
43
43
|
blocks.push(`<task_state>\n${renderState(document.fields)}\n</task_state>`);
|
|
44
44
|
blocks.push("推进这个任务的下一个具体步骤,然后必须调用 task_step_end 收尾:" +
|
|
45
45
|
"continue(还能继续)/ park(等一个真实来源)/ done(本周期完成)/ blocked(需要用户决定)。" +
|
|
46
|
+
`派发 run/job 时,首个调用就必须带 taskId=${input.taskId};达成 DoD 后先用 edit 勾选对应验收项,再调用 done。` +
|
|
46
47
|
"默认不向用户发言;契约要求交付、告知、汇报或回复时,必须把实际内容放进 notify,note 不会发送给用户。");
|
|
47
48
|
return blocks.join("\n\n");
|
|
48
49
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { shellEscape } from "../shared/
|
|
1
|
+
import { shellEscape } from "../shared/os.js";
|
|
2
2
|
/**
|
|
3
3
|
* Optional "outbound command transform" for the bash tool. When enabled, a command is
|
|
4
4
|
* handed to the `rtk` binary (https://github.com/rtk-ai/rtk) which rewrites known
|
package/dist/tools/config.d.ts
CHANGED
|
@@ -51,6 +51,13 @@ export interface LoadedToolsConfig {
|
|
|
51
51
|
config: PipiclawToolsConfig;
|
|
52
52
|
diagnostics: ConfigDiagnostic[];
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Every `tools.<key>` section in `tools.json`, read by `test/config-reference-coverage.test.ts`
|
|
56
|
+
* to check that `docs/configuration-reference.md` still mentions each one. Kept in sync with
|
|
57
|
+
* `PipiclawToolsConfig["tools"]` by `satisfies` (rejects an extra/misspelled entry) and
|
|
58
|
+
* `AssertNoMissingKeys` (rejects a section with no entry here) — both at `npm run typecheck`.
|
|
59
|
+
*/
|
|
60
|
+
export declare const TOOLS_CONFIG_SECTION_KEYS: readonly ["web", "tasks", "bashInterceptor", "rtk", "subagentInline"];
|
|
54
61
|
export declare const DEFAULT_TOOLS_CONFIG: PipiclawToolsConfig;
|
|
55
62
|
export declare function getToolsConfigPath(appHomeDir?: string): string;
|
|
56
63
|
export declare function loadToolsConfigWithDiagnostics(appHomeDir?: string): LoadedToolsConfig;
|