@botlearn-course/daemon 0.0.29-beta.2 → 0.0.29-beta.3

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.
@@ -4,7 +4,7 @@ import { existsSync } from "node:fs";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { startCourseSkillsMcpServer } from "../mcp/course-skills-server.js";
6
6
  import { runtimeChildEnv, runtimeChildLaunch } from "../runtime-env.js";
7
- import { MANAGED_PI_BINARY, MANAGED_PI_WEB_ACCESS_EXTENSION, managedPiVersion, prepareManagedPi, } from "./pi-managed.js";
7
+ import { MANAGED_PI_BINARY, MANAGED_PI_SUBAGENT_EXTENSION, MANAGED_PI_WEB_ACCESS_EXTENSION, managedPiVersion, prepareManagedPi, } from "./pi-managed.js";
8
8
  import { piSessionHandle, preparePiSession } from "./pi-session.js";
9
9
  import { sanitizeRuntimeFailureText } from "../redaction.js";
10
10
  import { wrapEngineAdapter } from "./engine.js";
@@ -33,6 +33,31 @@ function record(value) {
33
33
  function count(value) {
34
34
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
35
35
  }
36
+ /**
37
+ * Subagents run as separate Pi processes, so their `message_end` usage never reaches this
38
+ * RPC stream. The delegation tool reports each child's totals in its result details; fold
39
+ * them into this turn using the same cache-inclusive input convention as the parent.
40
+ */
41
+ function addSubagentUsage(usage, result) {
42
+ const results = record(record(result).details).results;
43
+ if (!Array.isArray(results))
44
+ return false;
45
+ let counted = false;
46
+ for (const entry of results) {
47
+ const child = record(record(entry).usage);
48
+ if (Object.keys(child).length === 0)
49
+ continue;
50
+ counted = true;
51
+ const cached = count(child.cacheRead);
52
+ const input = count(child.input) + cached + count(child.cacheWrite);
53
+ usage.input_tokens += input;
54
+ usage.output_tokens += count(child.output);
55
+ usage.total_tokens += input + count(child.output);
56
+ usage.cached_input_tokens += cached;
57
+ usage.cost_usd += count(child.cost);
58
+ }
59
+ return counted;
60
+ }
36
61
  /**
37
62
  * One RPC process per Course turn, resuming a workspace-scoped native session cache.
38
63
  * Course facts bootstrap new/missing sessions; healthy sessions receive only this turn.
@@ -97,13 +122,18 @@ export class PiAgentAdapter {
97
122
  args.push("--system-prompt", system);
98
123
  if (skillsSocket)
99
124
  args.push("--extension", fileURLToPath(new URL("./pi-course-skills-extension.js", import.meta.url)), "--botlearn-course-skills-socket", skillsSocket);
100
- // Web access ships in the image; a missing file means the build dropped it, so fail
101
- // instead of silently running a turn without the search tools the course expects.
125
+ // Both extensions ship in the image; a missing file means the build dropped it, so fail
126
+ // instead of silently running a turn without the tools the course expects.
102
127
  if (managed) {
103
- if (!existsSync(MANAGED_PI_WEB_ACCESS_EXTENSION)) {
104
- throw new Error("pi-agent web access extension is missing from the sandbox image");
128
+ for (const [extension, missing] of [
129
+ [MANAGED_PI_WEB_ACCESS_EXTENSION, "web access"],
130
+ [MANAGED_PI_SUBAGENT_EXTENSION, "subagent"],
131
+ ]) {
132
+ if (!existsSync(extension)) {
133
+ throw new Error(`pi-agent ${missing} extension is missing from the sandbox image`);
134
+ }
135
+ args.push("--extension", extension);
105
136
  }
106
- args.push("--extension", MANAGED_PI_WEB_ACCESS_EXTENSION);
107
137
  }
108
138
  const session = await preparePiSession(opts);
109
139
  args.push("--session-dir", session.dir);
@@ -224,6 +254,8 @@ export class PiAgentAdapter {
224
254
  else if (event.type === "tool_execution_start" || event.type === "tool_execution_end") {
225
255
  const name = typeof event.toolName === "string" ? event.toolName : "tool";
226
256
  const ending = event.type === "tool_execution_end";
257
+ if (ending && name === "subagent" && addSubagentUsage(usage, event.result))
258
+ hasUsage = true;
227
259
  emit(ending ? "tool_result" : "tool_use", {
228
260
  name,
229
261
  ...(ending ? { status: event.isError === true ? "error" : "completed" } : {}),
@@ -1,6 +1,8 @@
1
1
  import type { EngineRunOptions } from "./engine.js";
2
2
  export declare const MANAGED_PI_BINARY = "/opt/pi-agent/0.85.1/bin/pi";
3
3
  export declare const MANAGED_PI_WEB_ACCESS_EXTENSION = "/opt/pi-agent/0.85.1/node_modules/pi-web-access/dist/index.js";
4
+ /** Ships with the pinned Pi package; its `files` field publishes `examples`. */
5
+ export declare const MANAGED_PI_SUBAGENT_EXTENSION = "/opt/pi-agent/0.85.1/node_modules/@earendil-works/pi-coding-agent/examples/extensions/subagent/index.ts";
4
6
  /** The image verifies the CLI once; per-turn probes read immutable installed metadata. */
5
7
  export declare function managedPiVersion(): string | null;
6
8
  export declare function prepareManagedPi(opts: EngineRunOptions): Promise<{
@@ -3,6 +3,31 @@ import path from "node:path";
3
3
  import { runtimeChildEnv } from "../runtime-env.js";
4
4
  export const MANAGED_PI_BINARY = "/opt/pi-agent/0.85.1/bin/pi";
5
5
  export const MANAGED_PI_WEB_ACCESS_EXTENSION = "/opt/pi-agent/0.85.1/node_modules/pi-web-access/dist/index.js";
6
+ /** Ships with the pinned Pi package; its `files` field publishes `examples`. */
7
+ export const MANAGED_PI_SUBAGENT_EXTENSION = "/opt/pi-agent/0.85.1/node_modules/@earendil-works/pi-coding-agent/examples/extensions/subagent/index.ts";
8
+ /**
9
+ * The only delegation role. The extension resolves roles by name from the controlled
10
+ * agent directory and cannot take an inline profile, so task-specific detail travels in
11
+ * the delegated task text. Declaring no `tools` or `model` makes each subagent inherit
12
+ * this turn's model and thinking level.
13
+ */
14
+ const SUBAGENT_WORKER_ROLE = `---
15
+ name: worker
16
+ description: 通用子任务执行者。在独立上下文中完成一项明确的委派任务并返回结果。
17
+ ---
18
+
19
+ 你是练习场的子任务执行者。你在独立上下文中运行,只处理本次委派的任务。
20
+
21
+ 委派你的智能体看不到你读过的内容。返回结论、依据和它继续工作所需的事实,
22
+ 不要复制无关的大段原文。
23
+
24
+ 所有回复使用简体中文。代码、命令、文件路径、标识符和日志原文保持原样。
25
+
26
+ 只做委派任务要求的事。不要判断学员的学习进度,不要声称检查点或课程完成,
27
+ 不要修改与任务无关的文件。
28
+
29
+ 如实报告结果。任务无法完成时说明卡在哪里,不要把未验证的结果写成已经成功。
30
+ `;
6
31
  /** The image verifies the CLI once; per-turn probes read immutable installed metadata. */
7
32
  export function managedPiVersion() {
8
33
  try {
@@ -47,6 +72,14 @@ export async function prepareManagedPi(opts) {
47
72
  || contract.max_output_tokens_limit > contract.context_window) {
48
73
  throw new Error("pi-agent managed model selection is invalid");
49
74
  }
75
+ // The extension discovers roles under the controlled agent directory only. The workspace
76
+ // never participates, so course content cannot introduce a delegation role.
77
+ const agentsDir = path.join(agentDir, "agents");
78
+ mkdirSync(agentsDir, { recursive: true, mode: 0o750 });
79
+ chmodSync(agentsDir, 0o750);
80
+ const roleFile = path.join(agentsDir, "worker.md");
81
+ writeFileSync(roleFile, SUBAGENT_WORKER_ROLE, { mode: 0o640 });
82
+ chmodSync(roleFile, 0o640);
50
83
  const modelsFile = path.join(agentDir, "models.json");
51
84
  writeFileSync(modelsFile, JSON.stringify({ providers: {
52
85
  botlearn: {
@@ -19,6 +19,7 @@ const ACTIONS = {
19
19
  tool_search: 'discover_tools', tool_search_tool_regex: 'discover_tools',
20
20
  mcp_courseskills_list_skills: 'load_skill', mcp_courseskills_load_skill: 'load_skill', mcp_courseskills_load_reference: 'load_skill',
21
21
  load_skill: 'load_skill',
22
+ subagent: 'delegate',
22
23
  todo_write: 'plan', update_plan: 'plan', request_user_input: 'request_input',
23
24
  };
24
25
  export function toolAction(name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botlearn-course/daemon",
3
- "version": "0.0.29-beta.2",
3
+ "version": "0.0.29-beta.3",
4
4
  "description": "Lightweight BotLearn Course daemon: run course tasks on your own machine with your own agent runtime (BYOA).",
5
5
  "type": "module",
6
6
  "bin": {