@oyasmi/pipiclaw 0.8.11 → 0.9.0-beta.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.
Files changed (72) hide show
  1. package/dist/agent/channel-runner.d.ts +3 -0
  2. package/dist/agent/channel-runner.js +9 -0
  3. package/dist/agent/commands.d.ts +1 -1
  4. package/dist/agent/commands.js +9 -0
  5. package/dist/agent/job-manager.d.ts +5 -3
  6. package/dist/agent/job-manager.js +8 -5
  7. package/dist/agent/prompt/sections.js +46 -7
  8. package/dist/agent/prompt/types.d.ts +9 -0
  9. package/dist/agent/session-events.js +7 -42
  10. package/dist/agent/types.d.ts +4 -0
  11. package/dist/playbooks/agent-delegation.md +46 -21
  12. package/dist/playbooks/runtime-orientation.md +2 -2
  13. package/dist/runtime/bootstrap.js +45 -1
  14. package/dist/runtime/dingtalk.d.ts +1 -0
  15. package/dist/runtime/dingtalk.js +3 -0
  16. package/dist/runtime/store.d.ts +7 -0
  17. package/dist/runtime/subagent-commands.d.ts +15 -0
  18. package/dist/runtime/subagent-commands.js +122 -0
  19. package/dist/runtime/task-commands.js +5 -1
  20. package/dist/security/logger.js +3 -1
  21. package/dist/security/types.d.ts +17 -1
  22. package/dist/shared/host-process.d.ts +15 -0
  23. package/dist/shared/host-process.js +40 -0
  24. package/dist/shared/shell-words.d.ts +9 -0
  25. package/dist/shared/shell-words.js +52 -0
  26. package/dist/subagents/discovery.d.ts +42 -1
  27. package/dist/subagents/discovery.js +302 -69
  28. package/dist/subagents/external/claude-code.d.ts +2 -0
  29. package/dist/subagents/external/claude-code.js +152 -0
  30. package/dist/subagents/external/codex-cli.d.ts +2 -0
  31. package/dist/subagents/external/codex-cli.js +133 -0
  32. package/dist/subagents/external/exec.d.ts +2 -0
  33. package/dist/subagents/external/exec.js +38 -0
  34. package/dist/subagents/external/harness.d.ts +102 -0
  35. package/dist/subagents/external/harness.js +76 -0
  36. package/dist/subagents/external/registry.d.ts +2 -0
  37. package/dist/subagents/external/registry.js +11 -0
  38. package/dist/subagents/external/run.d.ts +39 -0
  39. package/dist/subagents/external/run.js +220 -0
  40. package/dist/subagents/runs.d.ts +216 -0
  41. package/dist/subagents/runs.js +522 -0
  42. package/dist/subagents/tool.d.ts +8 -0
  43. package/dist/subagents/tool.js +350 -124
  44. package/dist/subagents/workspace-lease.d.ts +36 -0
  45. package/dist/subagents/workspace-lease.js +48 -0
  46. package/dist/tasks/artifact-subject.js +50 -2
  47. package/dist/tasks/verification.d.ts +8 -0
  48. package/dist/tasks/verification.js +10 -2
  49. package/dist/tools/index.d.ts +1 -1
  50. package/dist/tools/index.js +10 -1
  51. package/dist/tools/registry.js +1 -0
  52. package/dist/tools/subagent-manage.d.ts +18 -0
  53. package/dist/tools/subagent-manage.js +141 -0
  54. package/dist/tools/tool-details.d.ts +1 -1
  55. package/dist/tui/app.js +2 -0
  56. package/dist/tui/commands.d.ts +1 -0
  57. package/dist/tui/commands.js +2 -0
  58. package/dist/tui/turn-controller.d.ts +1 -0
  59. package/dist/tui/turn-controller.js +1 -0
  60. package/dist/usage/ledger.d.ts +6 -0
  61. package/examples/sub-agents/README.md +59 -12
  62. package/examples/sub-agents/builder-hard.md +45 -0
  63. package/examples/sub-agents/builder.md +41 -0
  64. package/examples/sub-agents/documenter.md +53 -0
  65. package/examples/sub-agents/log-sifter.md +43 -0
  66. package/examples/sub-agents/planner.md +42 -0
  67. package/examples/sub-agents/reviewer.md +33 -39
  68. package/examples/sub-agents/scout.md +31 -0
  69. package/examples/sub-agents/verifier.md +29 -36
  70. package/examples/sub-agents/worker.md +36 -0
  71. package/package.json +1 -1
  72. package/examples/sub-agents/researcher.md +0 -41
@@ -0,0 +1,216 @@
1
+ import type { DingTalkEvent } from "../runtime/dingtalk.js";
2
+ import type { ChannelStore } from "../runtime/store.js";
3
+ import type { UsageTotals } from "../shared/types.js";
4
+ import type { UsageLedger } from "../usage/ledger.js";
5
+ /**
6
+ * The run: one abstraction level for delegation, whether it executes as an in-process sub-agent
7
+ * or (from spec 040 phase 2 onward) a short-lived external CLI process. See
8
+ * `docs/specs/040-async-delegation-and-external-agents/design.md`, D1.
9
+ *
10
+ * State machine vocabulary is borrowed from `job-manager.ts`'s `JobStatus` rather than inventing
11
+ * a second one. Only three facts are persisted as idempotent markers (D1/P5) because only three
12
+ * side effects cannot be safely replayed: settling (writes output, frees a lease), recording
13
+ * usage (a ledger write is a real charge), and enqueuing the completion wake (redelivery makes
14
+ * the parent agent re-process the same result). Everything else — duration, tool counts, verdict
15
+ * — is plain data that can be overwritten harmlessly.
16
+ */
17
+ export type RunStatus = "running" | "completed" | "failed" | "cancelled" | "lost";
18
+ export type RunRuntime = "internal" | "external";
19
+ export type RunHarness = "claude-code" | "codex-cli" | "exec";
20
+ export type RunMutates = "read" | "write";
21
+ /**
22
+ * Longest a dispatching tool call waits for its run to settle before returning a "still running"
23
+ * placeholder instead of the result (D2). A code constant, not a settings key, per CLAUDE.md's
24
+ * rule for numeric thresholds.
25
+ */
26
+ export declare const SYNC_GRACE_MS = 120000;
27
+ /** D10.2: per-channel cap on concurrently running delegation runs, mirrors job-manager's MAX_RUNNING_JOBS. */
28
+ export declare const MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL = 5;
29
+ /** D10.2: host-wide cap across every channel. Bounds unbounded process/worker growth only —
30
+ * not the full cross-subsystem (task/job/sidecar) admission scheme the design doc leaves open. */
31
+ export declare const MAX_RUNNING_SUBAGENT_RUNS_PER_HOST = 20;
32
+ export interface RunUsage {
33
+ usage: UsageTotals;
34
+ /** False only for harnesses that cannot report tokens at all (`exec`). */
35
+ usageKnown: boolean;
36
+ /** False for harnesses that omit a cost field (`codex-cli`, `exec`). */
37
+ costKnown: boolean;
38
+ }
39
+ export interface RunRecord extends RunUsage {
40
+ runId: string;
41
+ channelId: string;
42
+ runtime: RunRuntime;
43
+ harness?: RunHarness;
44
+ agent: string;
45
+ label: string;
46
+ source: "predefined" | "inline";
47
+ tools: string[];
48
+ model?: string;
49
+ purpose: "work" | "verify";
50
+ taskId?: string;
51
+ workingDirectory: string;
52
+ artifactDir: string;
53
+ status: RunStatus;
54
+ startedAt: number;
55
+ finishedAt?: number;
56
+ durationMs?: number;
57
+ failureReason?: string;
58
+ turns?: number;
59
+ toolCalls?: number;
60
+ verificationVerdict?: "pass" | "fail";
61
+ /** `enforced` for a built-in verifier (tools structurally removed); `advisory` for external (D9). */
62
+ verificationStrength?: "enforced" | "advisory";
63
+ /** Set once settlement (write output, free resources) has happened; guards against replay. */
64
+ settledAt?: number;
65
+ /** Set once this run's usage has been written to the ledger; guards against double billing. */
66
+ usageRecorded?: boolean;
67
+ /** Set once the completion wake has been hallmarked for delivery; guards against a duplicate wake. */
68
+ wakeEnqueued?: boolean;
69
+ pid?: number;
70
+ fingerprint?: string;
71
+ argv?: string[];
72
+ sessionId?: string;
73
+ leaseKey?: string;
74
+ mutates?: RunMutates;
75
+ }
76
+ export interface RegisterRunInput {
77
+ runId: string;
78
+ channelId: string;
79
+ runtime: RunRuntime;
80
+ harness?: RunHarness;
81
+ agent: string;
82
+ label: string;
83
+ source: "predefined" | "inline";
84
+ tools: string[];
85
+ model?: string;
86
+ purpose: "work" | "verify";
87
+ taskId?: string;
88
+ workingDirectory: string;
89
+ artifactDir: string;
90
+ /** Set only when the caller already holds a workspace write lease for this run (D10.1). */
91
+ leaseKey?: string;
92
+ }
93
+ export interface SettleInput {
94
+ status: "completed" | "failed" | "cancelled" | "lost";
95
+ failureReason?: string;
96
+ usage: UsageTotals;
97
+ usageKnown: boolean;
98
+ costKnown: boolean;
99
+ turns: number;
100
+ toolCalls: number;
101
+ durationMs: number;
102
+ /** Full reply text. Settlement saves it to `output.md`; only its tail goes in the wake. */
103
+ outputText: string;
104
+ verificationVerdict?: "pass" | "fail";
105
+ verificationStrength?: "enforced" | "advisory";
106
+ /** The harness's own session/thread id, captured even on failure so a later resume can still use it. */
107
+ sessionId?: string;
108
+ }
109
+ export interface RunManagerOptions {
110
+ /** Root of the per-channel record directories (`<stateDir>/<channelId>/`). Omit to skip persistence. */
111
+ stateDir?: string;
112
+ dispatch?: (event: DingTalkEvent) => boolean | Promise<boolean>;
113
+ ledger?: UsageLedger;
114
+ store?: ChannelStore;
115
+ }
116
+ /**
117
+ * Per-channel manager for delegation runs — the single settlement/usage/wake authority for both
118
+ * internal sub-agents and (once wired) external harnesses (D7). Modeled directly on
119
+ * `ChannelJobManager`; the two are not unified because a run's terminal status depends on a
120
+ * protocol verdict, not just process exit (D4), so the state machines genuinely differ.
121
+ */
122
+ export declare class SubAgentRunManager {
123
+ private readonly channelId;
124
+ private readonly options;
125
+ private readonly runs;
126
+ /** In-memory cancel handles for runs alive in *this* process (internal runs; external process
127
+ * kill goes through pid, not this map, once phase 2/3 lands). Never persisted: a restart loses
128
+ * the only thing that could reach an in-process worker anyway (D10.3). */
129
+ private readonly cancelHandles;
130
+ private readonly queue;
131
+ constructor(channelId: string, options: RunManagerOptions);
132
+ private recordPath;
133
+ private persist;
134
+ /**
135
+ * The full reply text always lands in `<artifactDir>/output.md` (spec 032 D4, carried into 040
136
+ * D1's settlement step). It is written here, in the one place every runtime settles, rather than
137
+ * in each caller: the completion wake points at this file, and it is what makes an inline-returned
138
+ * result self-recoverable after a crash (D2). A failed write must not block settlement — losing
139
+ * the artifact is bad, leaving the run stuck in `running` is worse.
140
+ */
141
+ private writeOutputFile;
142
+ private forget;
143
+ get(runId: string): RunRecord | undefined;
144
+ list(): RunRecord[];
145
+ /** Task ids a still-running delegation on this channel will eventually wake. Feeds `/tasks doctor`. */
146
+ runningTaskIds(): Set<string>;
147
+ runningCount(): number;
148
+ /** Register the run's lifecycle before any work starts, and persist it immediately (D1). */
149
+ register(input: RegisterRunInput): Promise<RunRecord>;
150
+ /**
151
+ * Persist the pid once a spawn actually succeeds — the second half of D1's launch ordering.
152
+ * `register()` already durably recorded the intent (argv, working directory, artifact dir)
153
+ * with pid unknown; a crash between that persist and this one leaves an intent-only record
154
+ * that restore treats as `lost` rather than guessing (D1/D10.3).
155
+ */
156
+ setLaunched(runId: string, info: {
157
+ pid: number;
158
+ fingerprint: string;
159
+ argv: string[];
160
+ sessionId?: string;
161
+ }): Promise<void>;
162
+ /** Let a caller (subagent tool) register a way to abort this run's in-process worker. */
163
+ registerCancelHandle(runId: string, cancel: () => void): void;
164
+ clearCancelHandle(runId: string): void;
165
+ /**
166
+ * Settle a run exactly once (D1/D7 order: settle → record usage/archive → enqueue wake).
167
+ * `announce: false` is the inline-return path (D2): the model already has the result in this
168
+ * same tool call, so `wakeEnqueued` is set without ever dispatching — waking the channel for a
169
+ * result it is already holding would burn a turn to say nothing.
170
+ */
171
+ settle(runId: string, input: SettleInput, options: {
172
+ announce: boolean;
173
+ }): Promise<void>;
174
+ /**
175
+ * Dispatch the completion wake. Durable-dispatch persists its own pending record before this
176
+ * call can be interrupted (`DurableDispatchService.dispatch`), and `dispatch()` is idempotent on
177
+ * `dispatchId`, so calling it again after a crash between here and marking `wakeEnqueued` is
178
+ * harmless — this is the same fix as job-manager's `announce()` reordering (D7).
179
+ */
180
+ private announce;
181
+ /** Explicit cancel (`subagent_manage op=cancel`, spec 040 D6). Does not wake — it is the model's own decision. */
182
+ cancel(runId: string): Promise<RunStatus | "not_found">;
183
+ /**
184
+ * Rebuild in-memory state after a restart. Internal runs cannot survive a restart by
185
+ * construction (D10.3): the worker lived in the process that just disappeared, so any record
186
+ * still `running` is settled as `lost` and the channel is woken to say so. External runs are
187
+ * left alone here — the harness-specific probe (spec 040 phase 2/3) reconciles those.
188
+ */
189
+ restore(): Promise<number>;
190
+ /**
191
+ * Restart reconciliation for an external run still marked `running` (D10.3). Unlike internal
192
+ * runs, a `detached` external process outlives the daemon, so the first move is a liveness
193
+ * probe, not an automatic `lost`:
194
+ *
195
+ * - no `pid` at all: the intent was persisted but spawn was never confirmed — cannot prove it
196
+ * ever started, so it is judged `lost` the same as an internal run (D1).
197
+ * - `pid` alive: genuinely still running. Left alone; it will settle itself when it exits.
198
+ * - `pid` gone: the process finished or died while the daemon was down. `events.jsonl` is the
199
+ * only remaining evidence, so it drives the terminal judgement via the same D4 status table
200
+ * the live post-exit path uses.
201
+ */
202
+ private reconcileExternalRun;
203
+ /** Drop a settled record once its retention window has passed, mirroring job-manager's GC. */
204
+ private collectGarbageIfExpired;
205
+ }
206
+ /** Give runs their persistence root, wake delivery, ledger, and archive. Called once from bootstrap. */
207
+ export declare function configureSubAgentRuntime(config: RunManagerOptions): void;
208
+ export declare function getSubAgentRunManager(channelId: string): SubAgentRunManager;
209
+ /** Read-only view for `/tasks doctor`, mirroring `channelJobTaskIds`. */
210
+ export declare function channelDelegationTaskIds(channelId: string): Set<string>;
211
+ /**
212
+ * Re-adopt every channel's persisted runs at startup, settling any internal run still marked
213
+ * `running` as `lost` (D10.3). Channels are discovered from the state directory itself, exactly
214
+ * like `restoreChannelJobs`.
215
+ */
216
+ export declare function restoreAllSubAgentRuns(): Promise<number>;