@oyasmi/pipiclaw 0.9.2-beta.3 → 0.9.2-beta.6

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 (142) hide show
  1. package/README.md +3 -0
  2. package/dist/agent/channel-runner.d.ts +45 -19
  3. package/dist/agent/channel-runner.js +156 -102
  4. package/dist/agent/command-extension.js +1 -1
  5. package/dist/agent/context-budget.d.ts +17 -1
  6. package/dist/agent/context-budget.js +20 -6
  7. package/dist/agent/job-manager.d.ts +3 -1
  8. package/dist/agent/job-manager.js +7 -1
  9. package/dist/agent/prompt/builder.d.ts +0 -1
  10. package/dist/agent/prompt/builder.js +1 -14
  11. package/dist/agent/prompt/manifest.js +2 -1
  12. package/dist/agent/prompt/sections.js +1 -1
  13. package/dist/agent/runner-factory.d.ts +19 -1
  14. package/dist/agent/runner-factory.js +8 -1
  15. package/dist/agent/session-events.d.ts +2 -2
  16. package/dist/agent/session-events.js +2 -22
  17. package/dist/agent/turn-prompt.d.ts +35 -0
  18. package/dist/agent/turn-prompt.js +37 -0
  19. package/dist/agent/turn-recovery.js +1 -1
  20. package/dist/agent/turn-state.d.ts +60 -0
  21. package/dist/agent/turn-state.js +95 -0
  22. package/dist/agent/types.d.ts +5 -4
  23. package/dist/{runtime → channel}/channel-context.d.ts +10 -2
  24. package/dist/{runtime → channel}/channel-context.js +6 -2
  25. package/dist/{runtime → channel}/channel-event.d.ts +39 -0
  26. package/dist/channel/channel-event.js +11 -0
  27. package/dist/channel/channel-paths.d.ts +16 -0
  28. package/dist/{runtime → channel}/channel-paths.js +20 -0
  29. package/dist/{runtime → channel}/project-scope-store.js +6 -11
  30. package/dist/{runtime → channel}/store.d.ts +7 -0
  31. package/dist/{agent/commands.d.ts → commands/catalog.d.ts} +17 -2
  32. package/dist/{agent/commands.js → commands/catalog.js} +30 -0
  33. package/dist/index.d.ts +1 -1
  34. package/dist/log.js +14 -5
  35. package/dist/main.js +29 -0
  36. package/dist/memory/bootstrap.js +3 -5
  37. package/dist/memory/candidates.js +2 -4
  38. package/dist/memory/commands.js +2 -2
  39. package/dist/memory/files.d.ts +0 -1
  40. package/dist/memory/files.js +6 -9
  41. package/dist/memory/maintenance-state.d.ts +8 -0
  42. package/dist/memory/maintenance-state.js +51 -4
  43. package/dist/memory/scheduler.d.ts +6 -0
  44. package/dist/memory/scheduler.js +19 -16
  45. package/dist/playbooks/agent-delegation.md +2 -2
  46. package/dist/runtime/bootstrap.d.ts +14 -8
  47. package/dist/runtime/bootstrap.js +158 -81
  48. package/dist/runtime/delivery.d.ts +3 -3
  49. package/dist/runtime/delivery.js +1 -0
  50. package/dist/runtime/dingtalk.d.ts +62 -6
  51. package/dist/runtime/dingtalk.js +194 -85
  52. package/dist/runtime/durable-dispatch.d.ts +7 -1
  53. package/dist/runtime/durable-dispatch.js +12 -2
  54. package/dist/runtime/event-commands.js +2 -2
  55. package/dist/runtime/events.d.ts +8 -1
  56. package/dist/runtime/events.js +15 -3
  57. package/dist/runtime/inbound-media.d.ts +60 -0
  58. package/dist/runtime/inbound-media.js +95 -0
  59. package/dist/runtime/project-commands.js +2 -2
  60. package/dist/runtime/skill-commands.d.ts +27 -0
  61. package/dist/runtime/skill-commands.js +72 -0
  62. package/dist/runtime/subagent-commands.js +1 -1
  63. package/dist/runtime/task-commands.js +2 -2
  64. package/dist/runtime/task-driver.d.ts +12 -1
  65. package/dist/runtime/task-driver.js +36 -56
  66. package/dist/runtime/task-migration.d.ts +11 -0
  67. package/dist/runtime/task-migration.js +14 -2
  68. package/dist/runtime/task-wake.d.ts +1 -1
  69. package/dist/runtime/task-wake.js +1 -1
  70. package/dist/security/command-guard.js +1 -3
  71. package/dist/security/config.d.ts +1 -1
  72. package/dist/security/config.js +8 -32
  73. package/dist/security/path-guard.js +1 -6
  74. package/dist/security/project-scope.js +1 -3
  75. package/dist/settings.d.ts +3 -2
  76. package/dist/settings.js +82 -20
  77. package/dist/shared/atomic-file.d.ts +2 -3
  78. package/dist/shared/atomic-file.js +8 -9
  79. package/dist/shared/config-diagnostic.d.ts +21 -0
  80. package/dist/shared/config-diagnostic.js +32 -0
  81. package/dist/shared/numeric.d.ts +7 -0
  82. package/dist/shared/numeric.js +19 -0
  83. package/dist/shared/text-utils.d.ts +10 -0
  84. package/dist/shared/text-utils.js +16 -0
  85. package/dist/shared/token-estimate.d.ts +1 -0
  86. package/dist/shared/token-estimate.js +19 -0
  87. package/dist/shared/types.d.ts +23 -0
  88. package/dist/shared/types.js +12 -0
  89. package/dist/shared/with-timeout.d.ts +3 -0
  90. package/dist/shared/with-timeout.js +8 -0
  91. package/dist/subagents/discovery.d.ts +7 -7
  92. package/dist/subagents/discovery.js +27 -59
  93. package/dist/subagents/runs.d.ts +15 -4
  94. package/dist/subagents/runs.js +118 -11
  95. package/dist/subagents/tool.js +199 -131
  96. package/dist/subagents/workspace-lease.js +1 -3
  97. package/dist/tasks/control.d.ts +2 -0
  98. package/dist/tasks/control.js +2 -0
  99. package/dist/tasks/ledger.d.ts +0 -9
  100. package/dist/tasks/ledger.js +92 -158
  101. package/dist/tools/config.d.ts +1 -1
  102. package/dist/tools/config.js +9 -46
  103. package/dist/tools/glob-match.d.ts +25 -4
  104. package/dist/tools/glob-match.js +175 -53
  105. package/dist/tools/glob.js +5 -2
  106. package/dist/tools/index.d.ts +1 -1
  107. package/dist/tools/registry.d.ts +1 -1
  108. package/dist/tools/send-media.d.ts +1 -1
  109. package/dist/tools/skill.d.ts +15 -0
  110. package/dist/tools/skill.js +17 -11
  111. package/dist/tools/truncate.js +24 -28
  112. package/dist/tui/app.js +6 -4
  113. package/dist/tui/commands.d.ts +1 -0
  114. package/dist/tui/commands.js +3 -1
  115. package/dist/tui/terminal-context.d.ts +2 -2
  116. package/dist/tui/terminal-media-sender.d.ts +1 -1
  117. package/dist/tui/turn-controller.d.ts +2 -1
  118. package/dist/tui/turn-controller.js +2 -1
  119. package/dist/web/config.js +3 -15
  120. package/examples/sub-agents/README.md +2 -2
  121. package/examples/sub-agents/builder.md +1 -1
  122. package/examples/sub-agents/documenter.md +1 -1
  123. package/examples/sub-agents/explorer.md +4 -4
  124. package/examples/sub-agents/git-committer.md +5 -4
  125. package/examples/sub-agents/log-sifter.md +4 -4
  126. package/examples/sub-agents/planner.md +1 -1
  127. package/examples/sub-agents/reviewer.md +1 -1
  128. package/examples/sub-agents/verifier.md +1 -1
  129. package/examples/sub-agents/worker.md +3 -3
  130. package/package.json +3 -2
  131. package/dist/agent/index.d.ts +0 -2
  132. package/dist/agent/index.js +0 -1
  133. package/dist/runtime/channel-event.js +0 -1
  134. package/dist/runtime/channel-paths.d.ts +0 -4
  135. /package/dist/{runtime → channel}/active-session-store.d.ts +0 -0
  136. /package/dist/{runtime → channel}/active-session-store.js +0 -0
  137. /package/dist/{runtime → channel}/channel-index.d.ts +0 -0
  138. /package/dist/{runtime → channel}/channel-index.js +0 -0
  139. /package/dist/{runtime → channel}/project-scope-store.d.ts +0 -0
  140. /package/dist/{runtime → channel}/store.js +0 -0
  141. /package/dist/{agent → commands}/reply-limits.d.ts +0 -0
  142. /package/dist/{agent → commands}/reply-limits.js +0 -0
@@ -1,5 +1,5 @@
1
- import type { ChannelEvent } from "../runtime/channel-event.js";
2
- import type { ChannelStore } from "../runtime/store.js";
1
+ import type { ChannelEvent } from "../channel/channel-event.js";
2
+ import type { ChannelStore } from "../channel/store.js";
3
3
  import { type UsageTotals } from "../shared/types.js";
4
4
  import type { UsageLedger } from "../usage/ledger.js";
5
5
  /**
@@ -24,6 +24,12 @@ export type RunMutates = "read" | "write";
24
24
  * rule for numeric thresholds.
25
25
  */
26
26
  export declare const SYNC_GRACE_MS = 120000;
27
+ /**
28
+ * Effective sync grace window. A test-only env override (`PIPICLAW_TEST_SUBAGENT_SYNC_GRACE_MS`)
29
+ * lets the deterministic e2e layer exercise the detached-settlement + completion-wake path
30
+ * (spec 048 A14/A15/A16) without a multi-minute wait; production always uses `SYNC_GRACE_MS`.
31
+ */
32
+ export declare function resolveSyncGraceMs(override?: number): number;
27
33
  /** D10.2: per-channel cap on concurrently running delegation runs. */
28
34
  export declare const MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL = 6;
29
35
  /** D10.2: host-wide cap across every channel. Bounds unbounded process/worker growth only —
@@ -216,7 +222,10 @@ export interface RunNotice {
216
222
  }
217
223
  export type RunNotifier = (channelId: string, notice: RunNotice) => void | Promise<void>;
218
224
  export interface RunManagerOptions {
219
- /** Root of the per-channel record directories (`<stateDir>/<channelId>/`). Omit to skip persistence. */
225
+ /**
226
+ * Root of the per-channel record directories (`<stateDir>/<escaped channelId>/`, escaped by
227
+ * `getChannelDir`). Omit to skip persistence.
228
+ */
220
229
  stateDir?: string;
221
230
  dispatch?: (event: ChannelEvent) => boolean | Promise<boolean>;
222
231
  /** Best-effort out-of-band notice sink (see `RunNotice`). Never awaited by settlement or the
@@ -251,6 +260,7 @@ export declare class SubAgentRunManager {
251
260
  private readonly externalProgressTails;
252
261
  private readonly queue;
253
262
  constructor(channelId: string, options: RunManagerOptions);
263
+ private recordDir;
254
264
  private recordPath;
255
265
  private persist;
256
266
  /**
@@ -408,7 +418,8 @@ export declare function channelDelegationTaskIds(channelId: string): Set<string>
408
418
  /**
409
419
  * Re-adopt every channel's persisted runs at startup, settling any internal run still marked
410
420
  * `running` as `lost` (D10.3). Channels are discovered from the state directory itself, exactly
411
- * like `restoreChannelJobs`.
421
+ * like `restoreChannelJobs` — but by reading the records rather than trusting directory names,
422
+ * so a channel is adopted under the id the rest of the runtime can act on.
412
423
  */
413
424
  export declare function restoreAllSubAgentRuns(): Promise<number>;
414
425
  export {};
@@ -1,5 +1,6 @@
1
- import { mkdir, readdir, readFile, rmdir, stat, unlink } from "node:fs/promises";
2
- import { join } from "node:path";
1
+ import { mkdir, readdir, readFile, rename, rmdir, stat, unlink } from "node:fs/promises";
2
+ import { dirname, join, relative, sep } from "node:path";
3
+ import { getChannelDir, isChannelId } from "../channel/channel-paths.js";
3
4
  import * as log from "../log.js";
4
5
  import { writeFileAtomically } from "../shared/atomic-file.js";
5
6
  import { formatDuration } from "../shared/duration.js";
@@ -19,6 +20,17 @@ import { acquireWorkspaceLease, formatWorkspaceLeaseConflict, releaseWorkspaceLe
19
20
  * rule for numeric thresholds.
20
21
  */
21
22
  export const SYNC_GRACE_MS = 120_000;
23
+ /**
24
+ * Effective sync grace window. A test-only env override (`PIPICLAW_TEST_SUBAGENT_SYNC_GRACE_MS`)
25
+ * lets the deterministic e2e layer exercise the detached-settlement + completion-wake path
26
+ * (spec 048 A14/A15/A16) without a multi-minute wait; production always uses `SYNC_GRACE_MS`.
27
+ */
28
+ export function resolveSyncGraceMs(override) {
29
+ if (override !== undefined)
30
+ return override;
31
+ const env = Number(process.env.PIPICLAW_TEST_SUBAGENT_SYNC_GRACE_MS);
32
+ return Number.isFinite(env) && env > 0 ? env : SYNC_GRACE_MS;
33
+ }
22
34
  /** D10.2: per-channel cap on concurrently running delegation runs. */
23
35
  export const MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL = 6;
24
36
  /** D10.2: host-wide cap across every channel. Bounds unbounded process/worker growth only —
@@ -151,8 +163,15 @@ export class SubAgentRunManager {
151
163
  this.externalProgressTails = new Map();
152
164
  this.queue = createSerialQueue();
153
165
  }
166
+ recordDir() {
167
+ // `getChannelDir`, not a raw join: a DingTalk group id routinely contains `/`, which used
168
+ // to nest the records one directory deeper than the startup scan ever looked — so a
169
+ // restart silently abandoned every delegation run of every such channel.
170
+ return this.options.stateDir ? getChannelDir(this.options.stateDir, this.channelId) : undefined;
171
+ }
154
172
  recordPath(runId) {
155
- return this.options.stateDir ? join(this.options.stateDir, this.channelId, `${runId}.json`) : undefined;
173
+ const dir = this.recordDir();
174
+ return dir ? join(dir, `${runId}.json`) : undefined;
156
175
  }
157
176
  async persist(record, required = false) {
158
177
  const path = this.recordPath(record.runId);
@@ -719,10 +738,9 @@ export class SubAgentRunManager {
719
738
  * without mutual exclusion.
720
739
  */
721
740
  async restore() {
722
- const stateDir = this.options.stateDir;
723
- if (!stateDir)
741
+ const dir = this.recordDir();
742
+ if (!dir)
724
743
  return 0;
725
- const dir = join(stateDir, this.channelId);
726
744
  let filenames;
727
745
  try {
728
746
  filenames = (await readdir(dir)).filter((name) => name.endsWith(".json"));
@@ -1098,26 +1116,115 @@ export function getSubAgentRunManager(channelId) {
1098
1116
  export function channelDelegationTaskIds(channelId) {
1099
1117
  return managers.get(channelId)?.runningTaskIds() ?? new Set();
1100
1118
  }
1119
+ /**
1120
+ * How deep the scan below looks for record files.
1121
+ *
1122
+ * Canonical records sit one level under the state root. Records written before the id was
1123
+ * escaped sit one level deeper per `/` in the channel id, and a base64 conversation id carries
1124
+ * very few; three is generous, and the bound keeps a corrupted or hand-made tree from turning
1125
+ * startup into an unbounded walk.
1126
+ */
1127
+ const MAX_RECORD_SCAN_DEPTH = 3;
1128
+ /** Every directory under `root` that directly holds run records, canonical or not. */
1129
+ async function findRecordDirectories(root, dir = root, depth = 0) {
1130
+ const entries = await readdir(dir, { withFileTypes: true });
1131
+ const found = entries.some((entry) => entry.isFile() && entry.name.endsWith(".json")) ? [dir] : [];
1132
+ if (depth >= MAX_RECORD_SCAN_DEPTH)
1133
+ return found;
1134
+ for (const entry of entries) {
1135
+ if (!entry.isDirectory())
1136
+ continue;
1137
+ try {
1138
+ found.push(...(await findRecordDirectories(root, join(dir, entry.name), depth + 1)));
1139
+ }
1140
+ catch (error) {
1141
+ // One unreadable subtree must not hide every other channel's runs.
1142
+ log.logWarning(`Failed to scan sub-agent run directory ${entry.name}`, errorMessage(error));
1143
+ }
1144
+ }
1145
+ return found;
1146
+ }
1147
+ /**
1148
+ * The channel a directory of records belongs to, and the records that are not where they belong.
1149
+ *
1150
+ * The id is read out of the records themselves — the one lossless source, since a directory name
1151
+ * is the *escaped* id and `__` cannot be turned back into `/`. Records written before escaping
1152
+ * are moved to the canonical directory here, so a run started by an older build is adopted
1153
+ * rather than abandoned; without the move it would stay invisible to `restore()` forever, and an
1154
+ * external process would keep running with nothing supervising, settling or billing it.
1155
+ */
1156
+ async function adoptRecordDirectory(stateDir, dir) {
1157
+ const filenames = (await readdir(dir)).filter((name) => name.endsWith(".json"));
1158
+ const channelIds = new Set();
1159
+ let moved = 0;
1160
+ for (const filename of filenames) {
1161
+ const path = join(dir, filename);
1162
+ let channelId;
1163
+ try {
1164
+ channelId = parseRunRecord(await readFile(path, "utf-8"))?.channelId;
1165
+ }
1166
+ catch {
1167
+ channelId = undefined;
1168
+ }
1169
+ // A record is only trusted to name its own directory if it is a well-formed id; anything
1170
+ // else keeps the path-derived fallback, which cannot escape the state root.
1171
+ if (!channelId || !isChannelId(channelId)) {
1172
+ channelIds.add(relative(stateDir, dir).split(sep).join("/"));
1173
+ continue;
1174
+ }
1175
+ channelIds.add(channelId);
1176
+ const canonicalDir = getChannelDir(stateDir, channelId);
1177
+ if (canonicalDir === dir)
1178
+ continue;
1179
+ try {
1180
+ await mkdir(canonicalDir, { recursive: true });
1181
+ await rename(path, join(canonicalDir, filename));
1182
+ moved++;
1183
+ }
1184
+ catch (error) {
1185
+ log.logWarning(`Failed to relocate sub-agent run record ${filename}`, errorMessage(error));
1186
+ }
1187
+ }
1188
+ if (moved > 0) {
1189
+ log.logInfo(`Relocated ${moved} sub-agent run record(s) from ${relative(stateDir, dir)}`);
1190
+ // Tidy the directory the unescaped id created. `rmdir` refuses a non-empty one, which is
1191
+ // exactly the guard wanted: records that could not be moved keep their home.
1192
+ await rmdir(dir).catch(() => undefined);
1193
+ const parent = dirname(dir);
1194
+ if (parent !== stateDir)
1195
+ await rmdir(parent).catch(() => undefined);
1196
+ }
1197
+ return Array.from(channelIds);
1198
+ }
1101
1199
  /**
1102
1200
  * Re-adopt every channel's persisted runs at startup, settling any internal run still marked
1103
1201
  * `running` as `lost` (D10.3). Channels are discovered from the state directory itself, exactly
1104
- * like `restoreChannelJobs`.
1202
+ * like `restoreChannelJobs` — but by reading the records rather than trusting directory names,
1203
+ * so a channel is adopted under the id the rest of the runtime can act on.
1105
1204
  */
1106
1205
  export async function restoreAllSubAgentRuns() {
1107
1206
  const stateDir = runtimeConfig.stateDir;
1108
1207
  if (!stateDir)
1109
1208
  return 0;
1110
- let channelIds;
1209
+ const channelIds = new Set();
1210
+ let recordDirs;
1111
1211
  try {
1112
1212
  await mkdir(stateDir, { recursive: true });
1113
- channelIds = (await readdir(stateDir, { withFileTypes: true }))
1114
- .filter((entry) => entry.isDirectory())
1115
- .map((entry) => entry.name);
1213
+ recordDirs = await findRecordDirectories(stateDir);
1116
1214
  }
1117
1215
  catch (error) {
1118
1216
  log.logWarning("Failed to scan persisted sub-agent runs", errorMessage(error));
1119
1217
  return 0;
1120
1218
  }
1219
+ for (const dir of recordDirs) {
1220
+ try {
1221
+ for (const channelId of await adoptRecordDirectory(stateDir, dir))
1222
+ channelIds.add(channelId);
1223
+ }
1224
+ catch (error) {
1225
+ log.logWarning(`Failed to adopt sub-agent run records in ${relative(stateDir, dir)}`, errorMessage(error));
1226
+ }
1227
+ }
1121
1228
  let restored = 0;
1122
1229
  for (const channelId of channelIds) {
1123
1230
  restored += await getSubAgentRunManager(channelId).restore();