@code-yeongyu/senpi-codemode 2026.9.3 → 2026.9.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/CHANGELOG.md CHANGED
@@ -12,6 +12,44 @@
12
12
 
13
13
  ### Removed
14
14
 
15
+ ## [2026.9.4] - 2026-09-04
16
+
17
+ ### Breaking Changes
18
+
19
+ ### Added
20
+
21
+ ### Changed
22
+
23
+ ### Fixed
24
+
25
+ ### Removed
26
+
27
+ ## [2026.9.3-3] - 2026-09-03
28
+
29
+ ### Breaking Changes
30
+
31
+ ### Added
32
+
33
+ ### Changed
34
+
35
+ ### Fixed
36
+
37
+ ### Removed
38
+
39
+ ## [2026.9.3-2] - 2026-09-03
40
+
41
+ ### Breaking Changes
42
+
43
+ ### Added
44
+
45
+ ### Changed
46
+
47
+ ### Fixed
48
+
49
+ - JavaScript eval cells no longer leak child-process output onto the host terminal under Bun: `Bun.$` commands awaited without `.quiet()`/`.text()` and `Bun.spawn` children with the default stderr now route their output into the cell's stdout/stderr streams instead of the inherited fd 1/2 that the interactive TUI owns.
50
+
51
+ ### Removed
52
+
15
53
  ## [2026.9.3] - 2026-09-03
16
54
 
17
55
  ### Breaking Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-yeongyu/senpi-codemode",
3
- "version": "2026.9.3",
3
+ "version": "2026.9.4",
4
4
  "description": "Source-only senpi extension package for codemode evaluation tools",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -30,14 +30,14 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@babel/parser": "8.0.4",
33
- "@earendil-works/pi-ai": "npm:@code-yeongyu/senpi-ai@2026.9.3",
33
+ "@earendil-works/pi-ai": "npm:@code-yeongyu/senpi-ai@2026.9.4",
34
34
  "typebox": "1.3.18"
35
35
  },
36
36
  "peerDependencies": {
37
- "@code-yeongyu/senpi": "2026.9.3"
37
+ "@code-yeongyu/senpi": "2026.9.4"
38
38
  },
39
39
  "devDependencies": {
40
- "@code-yeongyu/senpi": "2026.9.3"
40
+ "@code-yeongyu/senpi": "2026.9.4"
41
41
  },
42
42
  "keywords": [
43
43
  "senpi",
package/src/index.ts CHANGED
@@ -64,6 +64,15 @@ export interface SenpiCodemodeOptions {
64
64
  readonly now?: () => number;
65
65
  }
66
66
 
67
+ /** Whether the session registry holds `monitor`; false when the runtime cannot be read yet. */
68
+ function monitorIsRegistered(pi: CodemodeExtensionAPI): boolean {
69
+ try {
70
+ return pi.getAllTools().some((tool) => tool.name === "monitor");
71
+ } catch {
72
+ return false;
73
+ }
74
+ }
75
+
67
76
  export default function senpiCodemode(pi: CodemodeExtensionAPI, options: SenpiCodemodeOptions = {}): void {
68
77
  const manager = new SessionManagerProxy();
69
78
  const complete = options.complete ?? ((request, ctx) => createCompletionHandler()(ctx)(request));
@@ -108,6 +117,11 @@ export default function senpiCodemode(pi: CodemodeExtensionAPI, options: SenpiCo
108
117
  pi.rpc?.emit(EVAL_EXECUTION_EVENT, toEvalExecutionRpcPayload(payload));
109
118
  pi.events?.emit(EVAL_EXECUTION_EVENT, payload);
110
119
  };
120
+ // `listTools` below survives because it is lazy; this read is eager, and the loader's
121
+ // action methods throw while extensions are still loading (the bundled codemode path
122
+ // reaches this before the runtime is bound). An unreadable registry means "do not teach
123
+ // a tool we cannot confirm"; session_start / model_select re-register once it is live.
124
+ const monitor = monitorIsRegistered(pi);
111
125
  pi.registerTool(
112
126
  createEvalTool({
113
127
  enabledLanguages: runtime.enabledLanguages,
@@ -122,6 +136,7 @@ export default function senpiCodemode(pi: CodemodeExtensionAPI, options: SenpiCo
122
136
  executionTracker: manager,
123
137
  onCellSettled,
124
138
  renderers,
139
+ monitor,
125
140
  spawns: runtime.spawns,
126
141
  spawnDefaultAgent: runtime.settings.taskTools.task,
127
142
  hostLine: hostLine(),
@@ -159,6 +174,8 @@ export default function senpiCodemode(pi: CodemodeExtensionAPI, options: SenpiCo
159
174
  }),
160
175
  executionTracker: manager,
161
176
  renderers,
177
+ // The baseline tool is registered before extensions such as monitor load.
178
+ monitor: false,
162
179
  hostLine: hostLine(),
163
180
  runtimes: { js: jsRuntimeInfo() },
164
181
  ...(bunSkillPath === undefined ? {} : { bunSkillPath }),
@@ -3,6 +3,7 @@ import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
3
3
  import { dirname, isAbsolute, join, normalize, resolve, sep } from "node:path";
4
4
  import { inspect } from "node:util";
5
5
  import { awaitMaybePromise, indirectEval, wrapUserCode } from "./worker-indirect-eval.js";
6
+ import { installShellCapture } from "./worker-shell-capture.js";
6
7
 
7
8
  const PREPARED_CELL_PREFIX = "/*senpi:prepared-cell*/";
8
9
  const INTERNAL_URL = /^([a-z][a-z0-9+.-]*):\/\/(.*)$/iu;
@@ -84,11 +85,16 @@ export class JsWorkerRuntime {
84
85
  process.stderr.write = routeWrite(process.stderr, originalStderrWrite, "stderr");
85
86
  console.log = (...values) => this.#emitText("stdout", `${values.map(formatValue).join(" ")}\n`);
86
87
  console.error = (...values) => this.#emitText("stderr", `${values.map(formatValue).join(" ")}\n`);
88
+ const restoreShellCapture = installShellCapture({
89
+ isActive: () => this.#hooks !== null,
90
+ emitText: (stream, data) => this.#emitText(stream, data),
91
+ });
87
92
  globalThis.__senpi_restore_console__ = () => {
88
93
  console.log = originalLog;
89
94
  console.error = originalError;
90
95
  process.stdout.write = originalStdoutWrite;
91
96
  process.stderr.write = originalStderrWrite;
97
+ restoreShellCapture();
92
98
  };
93
99
  }
94
100
 
@@ -0,0 +1,10 @@
1
+ export type ShellCaptureStream = "stdout" | "stderr";
2
+
3
+ export type ShellCaptureRestore = () => void;
4
+
5
+ export interface ShellCaptureOptions {
6
+ readonly isActive: () => boolean;
7
+ readonly emitText: (stream: ShellCaptureStream, data: string) => void;
8
+ }
9
+
10
+ export function installShellCapture(options: ShellCaptureOptions): ShellCaptureRestore;
@@ -0,0 +1,126 @@
1
+ const SHELL_CONFIG_METHODS = ["env", "cwd", "nothrow", "throws"];
2
+ const SHELL_READ_METHODS = ["text", "json", "lines", "arrayBuffer", "bytes", "blob"];
3
+
4
+ export function installShellCapture(options) {
5
+ const bun = globalThis.Bun;
6
+ if (!isBunRuntime(bun)) return () => {};
7
+ const originalShell = bun.$;
8
+ const originalSpawn = bun.spawn;
9
+ bun.$ = capturedShell(originalShell, options);
10
+ bun.spawn = capturedSpawn(originalSpawn, options);
11
+ return () => {
12
+ bun.$ = originalShell;
13
+ bun.spawn = originalSpawn;
14
+ };
15
+ }
16
+
17
+ function isBunRuntime(bun) {
18
+ return bun !== null && typeof bun === "object" && typeof bun.$ === "function" && typeof bun.spawn === "function";
19
+ }
20
+
21
+ function capturedShell(originalShell, options) {
22
+ const shell = (strings, ...expressions) => {
23
+ const promise = originalShell(strings, ...expressions);
24
+ return options.isActive() ? captureShellPromise(promise, options.emitText) : promise;
25
+ };
26
+ for (const key of Object.keys(originalShell)) shell[key] = originalShell[key];
27
+ for (const method of SHELL_CONFIG_METHODS) {
28
+ shell[method] = (...args) => {
29
+ originalShell[method](...args);
30
+ return shell;
31
+ };
32
+ }
33
+ return shell;
34
+ }
35
+
36
+ function captureShellPromise(promise, emitText) {
37
+ const prototype = Object.getPrototypeOf(promise);
38
+ let echo = true;
39
+ const echoOnce = (output) => {
40
+ if (!echo) return;
41
+ echo = false;
42
+ emitShellOutput(output, emitText);
43
+ };
44
+ prototype.quiet.call(promise);
45
+ promise.quiet = function quiet() {
46
+ echo = false;
47
+ return prototype.quiet.call(this);
48
+ };
49
+ for (const method of SHELL_READ_METHODS) {
50
+ if (typeof prototype[method] !== "function") continue;
51
+ promise[method] = function read(...args) {
52
+ echo = false;
53
+ return prototype[method].apply(this, args);
54
+ };
55
+ }
56
+ promise.then = function then(onFulfilled, onRejected) {
57
+ return prototype.then.call(
58
+ this,
59
+ (output) => {
60
+ echoOnce(output);
61
+ return onFulfilled ? onFulfilled(output) : output;
62
+ },
63
+ (error) => {
64
+ echoOnce(error);
65
+ if (onRejected) return onRejected(error);
66
+ throw error;
67
+ },
68
+ );
69
+ };
70
+ return promise;
71
+ }
72
+
73
+ function emitShellOutput(output, emitText) {
74
+ if (output === null || typeof output !== "object") return;
75
+ const stdout = outputText(output.stdout);
76
+ if (stdout) emitText("stdout", stdout);
77
+ const stderr = outputText(output.stderr);
78
+ if (stderr) emitText("stderr", stderr);
79
+ }
80
+
81
+ function outputText(value) {
82
+ if (value instanceof Uint8Array) return new TextDecoder().decode(value);
83
+ return typeof value === "string" ? value : "";
84
+ }
85
+
86
+ function capturedSpawn(originalSpawn, options) {
87
+ return (...args) => {
88
+ if (!options.isActive()) return originalSpawn(...args);
89
+ const [first, second] = args;
90
+ if (Array.isArray(first)) {
91
+ const spawnOptions = second === undefined ? {} : second;
92
+ if (!needsStderrCapture(spawnOptions)) return originalSpawn(...args);
93
+ return drainStderr(originalSpawn(first, { ...spawnOptions, stderr: "pipe" }), options.emitText);
94
+ }
95
+ if (!needsStderrCapture(first)) return originalSpawn(...args);
96
+ return drainStderr(originalSpawn({ ...first, stderr: "pipe" }), options.emitText);
97
+ };
98
+ }
99
+
100
+ function needsStderrCapture(spawnOptions) {
101
+ return (
102
+ spawnOptions !== null &&
103
+ typeof spawnOptions === "object" &&
104
+ spawnOptions.stdio === undefined &&
105
+ spawnOptions.stderr === undefined
106
+ );
107
+ }
108
+
109
+ function drainStderr(child, emitText) {
110
+ const stream = child?.stderr;
111
+ if (!(stream instanceof ReadableStream)) return child;
112
+ void readStream(stream, emitText).catch((error) => {
113
+ emitText("stderr", `[spawn stderr capture failed: ${String(error)}]\n`);
114
+ });
115
+ return child;
116
+ }
117
+
118
+ async function readStream(stream, emitText) {
119
+ const decoder = new TextDecoder();
120
+ for await (const chunk of stream) {
121
+ const text = decoder.decode(chunk, { stream: true });
122
+ if (text) emitText("stderr", text);
123
+ }
124
+ const tail = decoder.decode();
125
+ if (tail) emitText("stderr", tail);
126
+ }
@@ -15,6 +15,8 @@ export interface EvalPromptParts {
15
15
 
16
16
  export interface EvalPromptOptions {
17
17
  readonly spawns: boolean;
18
+ /** Whether the session registry exposes the monitor tool through eval. */
19
+ readonly monitor?: boolean;
18
20
  readonly spawnDefaultAgent?: string;
19
21
  /** Active model id; selects the emphasis dialect of the batching guidance. */
20
22
  readonly modelId?: string;
@@ -105,21 +107,26 @@ Work incrementally: imports in one call, define in the next, test, then use —
105
107
  \`eval\` is your default execution surface: if a step needs more than one tool call, write ONE cell that performs the whole step — never issue the calls one at a time.
106
108
  - Enumerate every lookup the step needs, then run all independent ones simultaneously with \`parallel(thunks)\` inside the cell; keep calls sequential only when one result feeds the next.
107
109
  - Write real code around the calls: loop or comprehend over file sets with \`read()\`/stdlib, branch per case, and wrap risky calls in try/except so one failure degrades only its item — recover or retry inside the cell, keep the batch alive.
108
- - Post-process \`tool.<name>()\` results programmatically and return distilled facts, not raw dumps.
110
+ - Post-process \`tool.<name>()\` results programmatically — filter, join, aggregate — and return distilled facts, not raw dumps.
111
+ {{#if monitor}}- Start long-running work (build, test run, deploy, or watch) through \`tool.monitor({ command, filter })\`, putting the decisive-line filter inside the same cell, then keep working until its event wakes the turn; sleeping, timed retries, and repeated status reads are not waiting.{{/if}}
109
112
  </eval_first_batching>{{/if}}{{#if styleGpt}}<gpt_eval_dialect>
110
113
  GPT eval: compose multi-tool work inside one cell with \`tool.<name>(args)\` and \`parallel(thunks)\`; do not split a planned step into serial tool calls.
111
114
  - Long pure-compute cells detach on timeout and notify on completion. Do not poll or re-run them; use \`eval({ action: "peek"|"stop", cell_id })\` only to inspect or stop a detached cell.
112
- - Reduce tool results in the cell and return only decision-relevant facts.
115
+ - Filter, join, and aggregate tool results in the cell; return only decision-relevant facts.
116
+ {{#if monitor}}- For long-running build, test run, deploy, or watch work, start \`tool.monitor({ command, filter })\` with the decisive-line filter in the same cell; keep working while its event wakes the turn; sleeping, timed retries, and repeated status reads are not waiting.{{/if}}
113
117
  </gpt_eval_dialect>{{/if}}{{#if styleCodex}}Route multi-call steps through eval: one cell per step, independent lookups dispatched together via \`parallel(thunks)\`; keep work sequential only when one result determines the next action.
114
- - Loop or comprehend over file sets with \`read()\`/stdlib instead of reading files one call at a time; post-process \`tool.<name>()\` results programmatically.
118
+ - Loop or comprehend over file sets with \`read()\`/stdlib instead of reading files one call at a time; post-process \`tool.<name>()\` results programmatically — filter, join, aggregate.
115
119
  - Wrap failable calls in try/except inside the cell; a failed item degrades only itself. After two distinct failed strategies for the same fact, fall back to direct tool calls.
116
- - Reduce large results in-kernel to the facts the task needs before returning.{{/if}}{{#if styleKimi}}**EVAL IS YOUR SUPERPOWER — MAKE IT YOUR DEFAULT WAY TO ACT.** Before any step, think: "how do I execute this WHOLE step in ONE parallelized cell?" — then write that ONE cell.
120
+ - Reduce large results in-kernel to the facts the task needs before returning.
121
+ {{#if monitor}}- Long-running build/test/deploy/watch work: start \`tool.monitor({ command, filter })\` with the decisive-line filter inside the same cell, then continue working; its event wakes the turn — sleeping, timed retries, and repeated status reads are not waiting.{{/if}}{{/if}}{{#if styleKimi}}**EVAL IS YOUR SUPERPOWER — MAKE IT YOUR DEFAULT WAY TO ACT.** Before any step, think: "how do I execute this WHOLE step in ONE parallelized cell?" — then write that ONE cell.
117
122
  - **BATCH EVERYTHING AT ONCE:** enumerate EVERY independent lookup the step needs and dispatch them ALL simultaneously with \`parallel(thunks)\` in that cell; keep calls sequential only when one result feeds the next.
118
123
  - **WRITE REAL CODE, NOT CALL CHAINS:** loop or comprehend over file sets with \`read()\`/stdlib, post-process \`tool.<name>()\` results programmatically, and put try/except around each risky call so the rest of the batch completes.
119
- - **DISTILL IN-KERNEL:** filter and aggregate results in code, then return ONLY the distilled facts.{{/if}}{{#if styleDefault}}**EVAL IS YOUR PRIMARY EXECUTION SURFACE.** Any step that needs MORE THAN ONE tool call MUST be written as ONE cell — NEVER as a chain of single tool calls.
124
+ - **DISTILL IN-KERNEL:** filter, join, and aggregate \`tool.<name>()\` results in code, then return ONLY the distilled facts.
125
+ {{#if monitor}}- **DO start long-running build, test run, deploy, or watch work with \`tool.monitor({ command, filter })\`, put the decisive-line filter INSIDE THE SAME CELL, and KEEP WORKING until its event wakes the turn; sleeping, timed retries, and repeated status reads are not waiting.**{{/if}}{{/if}}{{#if styleDefault}}**EVAL IS YOUR PRIMARY EXECUTION SURFACE.** Any step that needs MORE THAN ONE tool call MUST be written as ONE cell — NEVER as a chain of single tool calls.
120
126
  - **PLAN THE WHOLE STEP, THEN BATCH IT.** Enumerate every read/search/lookup the step needs and dispatch ALL independent ones through \`parallel(thunks)\` in one cell.
121
127
  - **WRITE REAL CODE, NOT CALL LISTS.** Loop or comprehend over file sets with \`read()\`/stdlib, branch \`if\`/\`else\` per case, post-process \`tool.<name>()\` results programmatically, and wrap EVERY risky call in try/except so ONE failure NEVER kills the batch.
122
- - **DISTILL IN-KERNEL.** Filter, diff, and aggregate in code before returning; return facts, NOT dumps.{{/if}}
128
+ - **DISTILL IN-KERNEL.** Filter, join, diff, and aggregate in code before returning; return facts, NOT dumps.
129
+ {{#if monitor}}- **LONG-RUNNING build, test run, deploy, or watch work MUST start with \`tool.monitor({ command, filter })\`, with the decisive-line filter INSIDE THE SAME CELL; KEEP WORKING until its event wakes the turn — SLEEPING, TIMED RETRIES, AND REPEATED STATUS READS ARE NOT WAITING.**{{/if}}{{/if}}
123
130
  {{#if hostLine}}
124
131
  Host: {{hostLine}} — cells execute here. Size \`parallel(thunks)\` pools to its cores; \`tool.<name>()\` shell commands must fit this platform, even when the code you are writing targets another machine.
125
132
  {{/if}}
@@ -211,6 +218,7 @@ export function buildEvalPrompt(
211
218
  rb: enabled.rb,
212
219
  jl: enabled.jl,
213
220
  spawns: options.spawns,
221
+ monitor: options.monitor === true,
214
222
  spawnDefaultAgent,
215
223
  styleClaude: style === "claude",
216
224
  styleCodex: style === "codex",
@@ -36,6 +36,8 @@ export interface CreateEvalToolOptions {
36
36
  readonly proxyExecutor?: (params: EvalToolInput, signal?: AbortSignal) => Promise<AgentToolResult<EvalToolDetails>>;
37
37
  readonly renderers?: Pick<ToolDefinition<EvalInputSchema, EvalToolDetails>, "renderCall" | "renderResult">;
38
38
  readonly spawns?: boolean;
39
+ /** Whether the session registry exposes the monitor tool through eval. */
40
+ readonly monitor?: boolean;
39
41
  readonly spawnDefaultAgent?: string;
40
42
  readonly modelId?: string;
41
43
  readonly hostLine?: string;
@@ -28,6 +28,7 @@ export function createEvalTool(options: CreateEvalToolOptions): ToolDefinition<E
28
28
  const parameters = createEvalInputSchema(options.enabledLanguages);
29
29
  const prompt = buildEvalPrompt(options.enabledLanguages, {
30
30
  spawns: options.spawns ?? false,
31
+ monitor: options.monitor,
31
32
  ...(options.spawnDefaultAgent === undefined ? {} : { spawnDefaultAgent: options.spawnDefaultAgent }),
32
33
  ...(options.modelId === undefined ? {} : { modelId: options.modelId }),
33
34
  ...(options.hostLine === undefined ? {} : { hostLine: options.hostLine }),