teapot-coding-agent 0.29.27 → 0.29.29

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.
@@ -13,6 +13,8 @@ import { chat, chatStream } from "./llm.js";
13
13
  import { executeTool, toolSpecs, currentSkills, safeJoin, isContextOverflow, hasRunningBgShells, killBgShells } from "./tools.js";
14
14
  import { effortChoosable } from "./llm.js";
15
15
  import { sessionTitleFrom } from "../session-title.js";
16
+ import { mcp } from "./mcp-registry.js";
17
+ import { flattenToolName } from "./mcp-registry.js";
16
18
  import { skillRootsFingerprint, foreignSkillRoots } from "./skills.js";
17
19
  import { bus } from "../bus.js";
18
20
  /**
@@ -84,6 +86,36 @@ export const PROGRESS_REQUEST =
84
86
  * accurately, and a verbatim copy beats a summary of them.
85
87
  */
86
88
  const KEEP_RECENT_TOKENS = 15_000;
89
+ /**
90
+ * When pruning old tool output becomes worthwhile, as a share of the budget.
91
+ *
92
+ * Just under 1: pruning is DESTRUCTIVE (a clipped tool result leaves the model's
93
+ * context entirely — the log keeps it, but the model cannot see it without re-issuing
94
+ * the call), so it must only spend that information when the next request would
95
+ * otherwise not fit. The old value was 0.6 of a budget that is itself 75% of the
96
+ * window, i.e. history was being clipped at 45% of the real window with a quarter of
97
+ * the window still free — which is what the operator noticed ("25%余ってる").
98
+ *
99
+ * A share rather than an absolute count, so it tracks whatever window the session is
100
+ * configured for.
101
+ */
102
+ const PRUNE_AT_SHARE = 0.95;
103
+ /**
104
+ * Short-context mode (#161): the same knobs, retuned for a window where the default
105
+ * tail is a large share of everything.
106
+ *
107
+ * Measured reasoning, not a guess: `KEEP_RECENT_TOKENS` is 15k, which is 5.7% of a
108
+ * 262k window (fine) but 47% of a 32k one — so on a small model the verbatim tail
109
+ * alone nearly fills the budget and the first prune does almost nothing useful.
110
+ * The mode therefore (a) prunes at 0.6 of the budget instead of 0.95, (b) keeps a
111
+ * QUARTER of the budget verbatim instead of a fixed 15k, and (c) clips tool results
112
+ * down to 200 chars instead of 400, because on a small window the head of an output
113
+ * is most of what fits.
114
+ *
115
+ * Opt-in by design: on a large window this throws away real information early.
116
+ */
117
+ const SHORT_PRUNE_AT_SHARE = 0.6;
118
+ const SHORT_PRUNE_KEEP_CHARS = 200;
87
119
  /**
88
120
  * The summarize call is skipped when the summarizable prefix is a small share of
89
121
  * the budget, because the call cannot reuse any prefix cache (its system prompt is
@@ -419,6 +451,8 @@ export class Agent {
419
451
  contextWindowTokens: 0,
420
452
  restoreSession: true,
421
453
  globalSkillsDir: "",
454
+ pluginSkillRoots: [],
455
+ shortContext: false,
422
456
  bundledSkillsDir: "",
423
457
  spawnDepth: 0,
424
458
  readOnlyTools: false,
@@ -439,6 +473,7 @@ export class Agent {
439
473
  this.skillRoots = [
440
474
  { dir: path.join(opts.workspace, "skills"), source: "workspace" },
441
475
  ...(opts.globalSkillsDir ? [{ dir: opts.globalSkillsDir, source: "global" }] : []),
476
+ ...(opts.pluginSkillRoots ?? []),
442
477
  // OTHER HARNESSES' skill directories (#28). The SKILL.md format is shared,
443
478
  // so these are readable as-is — verified against real third-party skills
444
479
  // (terminalskills/skills, trailofbits/skills, …). They sit below the
@@ -1084,7 +1119,11 @@ export class Agent {
1084
1119
  // reload. It is a size fix, not a semantic one: the model has already
1085
1120
  // seen and acted on that output, and the pruned form keeps the head plus
1086
1121
  // a marker, exactly as it did live.
1087
- this.maybePrune();
1122
+ //
1123
+ // FORCED: this reproduces what the live session had already done, so it must
1124
+ // not consult the "is there room" bar — a restored context that happens to sit
1125
+ // below it still has to match the prompt the provider last accepted.
1126
+ await this.maybePrune(true);
1088
1127
  // #54: answer tool_calls the log says are still open.
1089
1128
  //
1090
1129
  // The restore path rebuilds `msgs` straight from the log, so a call whose
@@ -3310,14 +3349,39 @@ export class Agent {
3310
3349
  * summarize, clip OLD oversized tool outputs — they are the usual bulk and
3311
3350
  * their details rarely matter once executed. The most recent window is
3312
3351
  * protected so current work never loses its footing.
3352
+ *
3353
+ * WHEN it may run is the subtle part. It used to fire at `budget * 0.6`, and
3354
+ * because the budget is itself 75% of the model's window that meant clipping
3355
+ * history at **45% of the real window** — well before the auto-compact trigger at
3356
+ * 75%, i.e. while a quarter of the window was still free. The operator's report:
3357
+ * "compactで最初に過去履歴を落としてるけど…25%余ってるから多分あまりそれする必要ない".
3358
+ *
3359
+ * Pruning is destructive in a way compaction is not: a clipped tool result is GONE
3360
+ * from the model's context (the log keeps it, but the model cannot see it without
3361
+ * re-issuing the call), so doing it early trades real information for room that is
3362
+ * not needed yet. It is now a PRE-STEP OF COMPACTION — called from `maybeCompact()`
3363
+ * only once that pass has decided it is actually over budget — plus a margin so a
3364
+ * mid-size context is still tidy. `PRUNE_AT_SHARE` is a share of the BUDGET, and a
3365
+ * share just under 1 means "only when the next request would not fit".
3366
+ *
3367
+ * The separate restore-time call (#50) keeps its own rule: a restored history is
3368
+ * re-pruned so a reload does not hand the provider a strictly larger prompt than
3369
+ * the one that had just succeeded.
3313
3370
  */
3314
- maybePrune() {
3371
+ async maybePrune(force = false) {
3315
3372
  const est = this.contextSizeForBudgeting(); // floored — see #73
3316
3373
  const budget = this.opts.contextTokenBudget;
3317
- if (!budget || est < budget * 0.6)
3318
- return 0; // prune only when it matters
3319
- // protect the recent tail (~half the budget in chars) from any pruning
3320
- const protectChars = budget * 2;
3374
+ if (!budget)
3375
+ return 0;
3376
+ // Below the bar there is enough room for the next request, so clipping now would
3377
+ // spend information to buy space that is already free.
3378
+ const pruneShare = this.opts.shortContext ? SHORT_PRUNE_AT_SHARE : PRUNE_AT_SHARE;
3379
+ if (!force && est < budget * pruneShare)
3380
+ return 0;
3381
+ // protect the recent tail (~half the budget in chars) from any pruning. Short mode
3382
+ // protects a THIRD of it: a 2x-budget tail on a small window is most of the window,
3383
+ // so pruning would find nothing to do exactly when it is needed (#161).
3384
+ const protectChars = this.opts.shortContext ? Math.floor(budget * 1.2) : budget * 2;
3321
3385
  let seen = 0;
3322
3386
  let boundary = this.messages.length;
3323
3387
  for (let i = this.messages.length - 1; i >= 0; i--) {
@@ -3352,13 +3416,19 @@ export class Agent {
3352
3416
  PRUNABLE_TOOLS.has(toolName) &&
3353
3417
  (m.content?.length ?? 0) > PRUNE_MIN) {
3354
3418
  const len = m.content.length;
3355
- saved += len - 400;
3356
- m.content = m.content.slice(0, 400) + `\n…[pruned ${len} bytes of ${toolName || "tool"} output]`;
3419
+ const keep = this.opts.shortContext ? SHORT_PRUNE_KEEP_CHARS : 400;
3420
+ saved += len - keep;
3421
+ m.content =
3422
+ m.content.slice(0, keep) + `\n…[pruned ${len} bytes of ${toolName || "tool"} output]`;
3357
3423
  count++;
3358
3424
  }
3359
3425
  }
3360
3426
  if (count > 0)
3361
- void this.log.append("system_note", this.currentSession, this.currentBranch, {
3427
+ // AWAITED, not fire-and-forget: the caller decides what to do next based on the
3428
+ // count, and a compaction pass that reports its outcome must have its reasons on
3429
+ // disk first — a `void` append could land after the pass returned, so the log
3430
+ // read the order "compacted" → "pruned".
3431
+ await this.log.append("system_note", this.currentSession, this.currentBranch, {
3362
3432
  event: "context-pruned",
3363
3433
  outputs: count,
3364
3434
  savedBytes: saved,
@@ -3375,13 +3445,37 @@ export class Agent {
3375
3445
  // prefer the provider's own count from the last response — it is exactly
3376
3446
  // what would overflow the window; the char heuristic is only a fallback
3377
3447
  // for providers that omit usage
3378
- // stage 1: clip oversized old tool outputs before considering a summarize
3379
- this.maybePrune();
3380
3448
  const before = this.contextSizeForBudgeting(); // floored — see #73
3449
+ // SHORT-CONTEXT MODE (#161) prunes INSIDE the budget, so it must run before the
3450
+ // "are we over budget" return below — otherwise its lower threshold is unreachable
3451
+ // and the mode does nothing at all. This is the one path where pruning deliberately
3452
+ // spends information early, and it only exists because the operator asked for it.
3453
+ if (this.opts.shortContext)
3454
+ await this.maybePrune();
3381
3455
  if (!force && before < this.opts.contextTokenBudget)
3382
3456
  return;
3383
3457
  if (!force && this.opts.autoCompact === false)
3384
3458
  return;
3459
+ // stage 1: clip oversized old tool outputs before considering a summarize.
3460
+ // ONLY reached once this pass is actually over budget — pruning is destructive,
3461
+ // so it must not run on a context that still has room (see maybePrune()).
3462
+ const pruned = await this.maybePrune(true);
3463
+ // If clipping alone brought the request back under budget, STOP: that is the
3464
+ // cheapest possible compaction (no LLM call, no history rewritten, the tool
3465
+ // results are re-issuable) and everything below would discard context the model
3466
+ // can still use. This is the ordering the operator asked for — "どうしても足りなかったら
3467
+ // 過去履歴落とす程度でいい" — and it is what Anthropic calls the lightest form of
3468
+ // compaction. A manual `/compact` still means "summarize", so it is exempt.
3469
+ if (pruned > 0 && !force && this.contextSizeForBudgeting() < this.opts.contextTokenBudget) {
3470
+ await this.log.append("system_note", this.currentSession, this.currentBranch, {
3471
+ event: "context-pruned-enough",
3472
+ outputs: pruned,
3473
+ tokensAfter: this.contextSizeForBudgeting(),
3474
+ budget: this.opts.contextTokenBudget,
3475
+ });
3476
+ this.compactedAtLen = this.messages.length;
3477
+ return;
3478
+ }
3385
3479
  // CIRCUIT BREAKER (ZCode: `max auto failures: 3`). A compaction that keeps
3386
3480
  // failing would otherwise retry on EVERY turn, re-burning the provider for a pass
3387
3481
  // that cannot succeed — and the failure is usually environmental (an unavailable
@@ -3411,6 +3505,9 @@ export class Agent {
3411
3505
  // of the budget — a fixed floor GREATER than the budget would keep everything
3412
3506
  // and compaction would never fire at all (measured: a 500-token budget kept
3413
3507
  // a 7.5k-token history intact)
3508
+ // already scaled: on a small budget a quarter is below the 15k default, so a
3509
+ // 32k-window session keeps ~6k rather than 15k. Short mode does NOT need to
3510
+ // change this — checked while implementing #161 rather than assumed.
3414
3511
  : Math.min(KEEP_RECENT_TOKENS, Math.max(1, Math.floor(this.opts.contextTokenBudget / 4)));
3415
3512
  let cut = -1;
3416
3513
  for (let i = this.messages.length - 1; i >= 1; i--) {
@@ -3971,7 +4068,14 @@ export class Agent {
3971
4068
  this.currentBranch = branch;
3972
4069
  }
3973
4070
  }
3974
- /** workspace tools + agent-meta tools (finish / report_progress / set_goal / set_memory) */
4071
+ /**
4072
+ * workspace tools + agent-meta tools + every tool from every CONNECTED MCP server.
4073
+ *
4074
+ * The MCP tools are appended LAST and their names are namespaced (`mcp__server__tool`),
4075
+ * so adding one can never shadow a built-in — a server exposing `bash` must not be able
4076
+ * to take over the shell. `mcpSpecs()` is empty when nothing is configured, which is
4077
+ * the common case, so the cost of the feature is one empty spread.
4078
+ */
3975
4079
  function allToolSpecs() {
3976
4080
  return [
3977
4081
  ...toolSpecs(),
@@ -4205,8 +4309,27 @@ function allToolSpecs() {
4205
4309
  parameters: { type: "object", properties: {} },
4206
4310
  },
4207
4311
  },
4312
+ ...mcpSpecs(),
4208
4313
  ];
4209
4314
  }
4315
+ /**
4316
+ * MCP tools as function specs, flattened and namespaced.
4317
+ *
4318
+ * Descriptions are prefixed with their origin so the model can reason about which
4319
+ * server a result came from — with three servers exposing `search`, a bare description
4320
+ * gives it nothing to choose with. The server's own JSON Schema is forwarded as-is:
4321
+ * rewriting it would drop constraints the server enforces.
4322
+ */
4323
+ function mcpSpecs() {
4324
+ return mcp.allTools().map((t) => ({
4325
+ type: "function",
4326
+ function: {
4327
+ name: flattenToolName(t.server, t.name),
4328
+ description: `[MCP: ${t.server}] ${t.description || `${t.name} (no description provided)`}`,
4329
+ parameters: t.inputSchema,
4330
+ },
4331
+ }));
4332
+ }
4210
4333
  function str(v) {
4211
4334
  return typeof v === "string" ? v : "";
4212
4335
  }
@@ -0,0 +1,163 @@
1
+ /**
2
+ * The MCP server registry (#67): keeps connections, discovers tools, and dispatches
3
+ * calls.
4
+ *
5
+ * Why a registry instead of connecting per agent: several agents usually want the same
6
+ * server, and a stdio server is a PROCESS. One connection per agent would spawn one
7
+ * server per agent, each with its own state and its own startup cost. The pool keys by
8
+ * server name, so N agents share one connection — and a `tools/call` is still
9
+ * per-agent, because the arguments are.
10
+ *
11
+ * Naming. Two servers may both expose `search`, and a server may expose a name that
12
+ * collides with a built-in tool (`bash`, `read_file`). Every MCP tool is therefore
13
+ * exposed to the model as `mcp__<server>__<tool>`, which cannot collide with a
14
+ * built-in (those have no `__`) and tells the model where the result came from. The
15
+ * original name is what gets sent to the server.
16
+ */
17
+ import { connectMcp, McpError } from "./mcp.js";
18
+ /** the prefix a model-visible MCP tool name carries */
19
+ export const MCP_PREFIX = "mcp__";
20
+ /** `mcp__server__tool` — the flat name handed to the LLM */
21
+ export function flattenToolName(server, tool) {
22
+ // `__` is the separator, so a server or tool name containing it would be ambiguous.
23
+ // Collapse runs rather than rejecting: a server called "my__server" is unusual but
24
+ // not worth failing over, and the mapping stays stable.
25
+ const s = server.replace(/__/g, "_");
26
+ return `${MCP_PREFIX}${s}__${tool}`;
27
+ }
28
+ /** pull the parts back out of a flat name, or null when it is not an MCP tool */
29
+ export function splitToolName(flat) {
30
+ if (!flat.startsWith(MCP_PREFIX))
31
+ return null;
32
+ const rest = flat.slice(MCP_PREFIX.length);
33
+ const at = rest.indexOf("__");
34
+ if (at <= 0 || at === rest.length - 2)
35
+ return null;
36
+ return { server: rest.slice(0, at), tool: rest.slice(at + 2) };
37
+ }
38
+ export class McpRegistry {
39
+ servers = new Map();
40
+ /** replace the configured server list (on boot and after a settings save) */
41
+ apply(configs) {
42
+ const wanted = new Set(configs.map((c) => c.name));
43
+ // drop connections for servers that were removed or renamed
44
+ for (const [name, st] of this.servers) {
45
+ if (!wanted.has(name)) {
46
+ void st.connection?.close();
47
+ this.servers.delete(name);
48
+ }
49
+ }
50
+ for (const cfg of configs) {
51
+ const prev = this.servers.get(cfg.name);
52
+ // a config EDIT means the old connection is pointing at the wrong thing
53
+ const changed = Boolean(prev) && JSON.stringify(prev.config) !== JSON.stringify(cfg);
54
+ if (changed)
55
+ void prev.connection?.close();
56
+ const reuse = prev && !changed;
57
+ const status = cfg.enabled === false ? "disabled" : reuse ? prev.status : "disconnected";
58
+ const next = {
59
+ config: cfg,
60
+ tools: reuse ? prev.tools : [],
61
+ status,
62
+ };
63
+ if (reuse && prev.connection)
64
+ next.connection = prev.connection;
65
+ if (reuse && prev.lastError)
66
+ next.lastError = prev.lastError;
67
+ if (reuse && prev.lastChecked)
68
+ next.lastChecked = prev.lastChecked;
69
+ this.servers.set(cfg.name, next);
70
+ }
71
+ }
72
+ names() {
73
+ return [...this.servers.keys()];
74
+ }
75
+ states() {
76
+ return [...this.servers.values()];
77
+ }
78
+ /** connect one server (idempotent) and discover its tools */
79
+ async connect(name) {
80
+ const st = this.servers.get(name);
81
+ if (!st)
82
+ throw new McpError(`no such MCP server: ${name}`);
83
+ if (st.config.enabled === false) {
84
+ st.status = "disabled";
85
+ return st;
86
+ }
87
+ if (st.status === "connected" && st.connection)
88
+ return st;
89
+ st.status = "connecting";
90
+ st.lastError = undefined;
91
+ try {
92
+ const conn = await connectMcp(st.config);
93
+ const tools = await conn.tools();
94
+ st.connection = conn;
95
+ st.tools = tools;
96
+ st.status = "connected";
97
+ st.lastChecked = Date.now();
98
+ return st;
99
+ }
100
+ catch (err) {
101
+ st.status = "error";
102
+ st.lastError = err.message;
103
+ st.lastChecked = Date.now();
104
+ st.connection = undefined;
105
+ st.tools = [];
106
+ throw err;
107
+ }
108
+ }
109
+ /** connect every enabled server; failures are recorded, never thrown */
110
+ async connectAll() {
111
+ await Promise.all([...this.servers.keys()].map(async (n) => {
112
+ try {
113
+ await this.connect(n);
114
+ }
115
+ catch {
116
+ /* recorded in the state; one dead server must not stop the others */
117
+ }
118
+ }));
119
+ }
120
+ /** every tool from every CONNECTED server, flattened */
121
+ allTools() {
122
+ return [...this.servers.values()]
123
+ .filter((s) => s.status === "connected")
124
+ .flatMap((s) => s.tools);
125
+ }
126
+ /** dispatch a flattened name; returns null when it is not an MCP tool */
127
+ async call(flatName, args) {
128
+ const parts = splitToolName(flatName);
129
+ if (!parts)
130
+ return null;
131
+ const st = this.servers.get(parts.server);
132
+ if (!st)
133
+ return { ok: false, text: `no such MCP server: ${parts.server}` };
134
+ // a call for a server that is configured but not yet connected should connect on
135
+ // demand rather than fail: the operator adding a tool mid-session must not need a
136
+ // restart, and auto-connect is cheap for the HTTP transports
137
+ if (st.status !== "connected" || !st.connection) {
138
+ try {
139
+ await this.connect(parts.server);
140
+ }
141
+ catch (err) {
142
+ return { ok: false, text: `MCP server "${parts.server}" is not connected: ${err.message}` };
143
+ }
144
+ }
145
+ try {
146
+ const r = await this.servers.get(parts.server).connection.call(parts.tool, args);
147
+ return { ok: r.ok, text: r.text };
148
+ }
149
+ catch (err) {
150
+ return { ok: false, text: `MCP call failed: ${err.message}` };
151
+ }
152
+ }
153
+ async closeAll() {
154
+ for (const st of this.servers.values())
155
+ await st.connection?.close();
156
+ for (const st of this.servers.values()) {
157
+ st.connection = undefined;
158
+ st.status = st.config.enabled === false ? "disabled" : "disconnected";
159
+ }
160
+ }
161
+ }
162
+ /** process-wide registry: one connection per server, shared by every agent */
163
+ export const mcp = new McpRegistry();