@workser/engine 0.1.18 → 0.1.20

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.
@@ -69,6 +69,27 @@ export declare function lowWater(budget: CompactionBudget): number;
69
69
  export declare function tailBudget(messages: ChatMessage[], budget: CompactionBudget, overhead: number): number;
70
70
  export declare function estimateTokens(messages: ChatMessage[]): number;
71
71
  export declare function overBudget(messages: ChatMessage[], budget: CompactionBudget, reportedPromptTokens?: number): boolean;
72
+ /**
73
+ * NEVER READ THE SAME FILE TWICE INTO THE CONTEXT (owner 17 Nov, trace 01a1e8ed: a restyle read page.tsx 32 times and
74
+ * the design skill 27 times, summarized twelve times in twelve minutes and wrote nothing). Three parts:
75
+ * - `dropSupersededReads`: a read of a file that was read again later, or changed after it, is out of date. It
76
+ * becomes a one-line note before anything else is pruned, so the room it frees costs nothing;
77
+ * - `openFiles` + `withSummary`: the latest read of each file still in play goes INTO the summary, so the job never
78
+ * needs to read it again after making room;
79
+ * - the session's stall guard (session.ts): two summaries with no change to any file in between double the next
80
+ * trigger and tell the job to write.
81
+ */
82
+ export declare const READ_TOOLS: ReadonlySet<string>;
83
+ export declare const WRITE_TOOLS: ReadonlySet<string>;
84
+ /** The most of the open files' latest contents a summary carries, in tokens. */
85
+ export declare const OPEN_FILES_TOKENS = 20000;
86
+ /** Step 0. Out-of-date reads become a one-line note (changed in place); returns how many. */
87
+ export declare function dropSupersededReads(messages: ChatMessage[]): number;
88
+ /** The latest read of each file still as it was read (not changed since), newest first, within `maxTokens`. */
89
+ export declare function openFiles(messages: ChatMessage[], maxTokens?: number): Array<{
90
+ path: string;
91
+ content: string;
92
+ }>;
72
93
  /** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
73
94
  export declare function pruneToolOutputs(messages: ChatMessage[], keepToolTokens: number): number;
74
95
  /** An old call's input longer than this (characters) keeps its head only. */
@@ -107,7 +128,25 @@ export interface RoomPlan {
107
128
  * that is at or under the low water the job is done, else the summary's kept tail is sized to land there.
108
129
  */
109
130
  export declare function makeRoom(messages: ChatMessage[], budget: CompactionBudget, reportedPromptTokens?: number): RoomPlan | null;
110
- /** The request that asks the model to summarize the part being replaced. */
111
- export declare function summaryRequest(part: ChatMessage[]): ChatMessage[];
112
- /** Step 2's result: the system prompt, the goal with the summary, then the kept tail. */
113
- export declare function withSummary(messages: ChatMessage[], cut: number, summary: string): ChatMessage[];
131
+ /**
132
+ * The request that asks the model to summarize the part being replaced. `goal`: the job's request (the first owner
133
+ * message), which the part never holds. Without it every summary of trace 01a1e93d (17 Nov) began "Goal: not specified"
134
+ * and ended "re-read AGENTS.md ... proceed once the task is established", so after each one the job started over:
135
+ * 33 summaries, 246 reads, 3 writes.
136
+ */
137
+ export declare function summaryRequest(part: ChatMessage[], goal?: string | null, earlier?: string | null): ChatMessage[];
138
+ /**
139
+ * The job's first owner message, split: the request as the owner gave it, and the summary an earlier compaction put
140
+ * after it (null before the first). The open files and the stall note are left out of `earlier`: the next summary gets
141
+ * the files again from `openFiles`, and a fresh note. So a second summary neither nests the first nor loses it.
142
+ */
143
+ export declare function splitHead(content: string | null | undefined): {
144
+ request: string;
145
+ earlier: string | null;
146
+ };
147
+ /**
148
+ * Step 2's result: the system prompt, the goal with the summary, then the kept tail. The files the job had read and not
149
+ * changed since, from the part summarized away (the tail keeps its own), go with the summary as they were read, so the
150
+ * job carries on without reading them again. `note`: the session's stall warning, when it has one.
151
+ */
152
+ export declare function withSummary(messages: ChatMessage[], cut: number, summary: string, note?: string): ChatMessage[];
@@ -71,6 +71,125 @@ export function overBudget(messages, budget, reportedPromptTokens) {
71
71
  const tokens = Math.max(estimateTokens(messages), reportedPromptTokens ?? 0);
72
72
  return tokens >= compactAt(budget);
73
73
  }
74
+ /**
75
+ * NEVER READ THE SAME FILE TWICE INTO THE CONTEXT (owner 17 Nov, trace 01a1e8ed: a restyle read page.tsx 32 times and
76
+ * the design skill 27 times, summarized twelve times in twelve minutes and wrote nothing). Three parts:
77
+ * - `dropSupersededReads`: a read of a file that was read again later, or changed after it, is out of date. It
78
+ * becomes a one-line note before anything else is pruned, so the room it frees costs nothing;
79
+ * - `openFiles` + `withSummary`: the latest read of each file still in play goes INTO the summary, so the job never
80
+ * needs to read it again after making room;
81
+ * - the session's stall guard (session.ts): two summaries with no change to any file in between double the next
82
+ * trigger and tell the job to write.
83
+ */
84
+ export const READ_TOOLS = new Set(['read']);
85
+ export const WRITE_TOOLS = new Set(['write', 'edit', 'multiedit', 'apply_patch']);
86
+ /** The most of the open files' latest contents a summary carries, in tokens. */
87
+ export const OPEN_FILES_TOKENS = 20_000;
88
+ /** A tool call's file path (`filePath`, `path`, `file_path`), or null. */
89
+ function pathOfCall(call) {
90
+ if (!call)
91
+ return null;
92
+ try {
93
+ const args = JSON.parse(call.function.arguments || '{}');
94
+ const path = args.filePath ?? args.path ?? args.file_path;
95
+ return typeof path === 'string' && path.trim() ? path.trim() : null;
96
+ }
97
+ catch {
98
+ return null;
99
+ }
100
+ }
101
+ /** The read's own window (a file read in parts is several reads): path, offset and limit. */
102
+ function readKey(call) {
103
+ const path = pathOfCall(call);
104
+ if (!path)
105
+ return null;
106
+ try {
107
+ const args = JSON.parse(call.function.arguments || '{}');
108
+ return `${path}#${args.offset ?? ''}#${args.limit ?? ''}`;
109
+ }
110
+ catch {
111
+ return `${path}##`;
112
+ }
113
+ }
114
+ /** Every tool call by its id. */
115
+ function callsById(messages) {
116
+ const calls = new Map();
117
+ for (const message of messages)
118
+ for (const call of message.tool_calls ?? [])
119
+ calls.set(call.id, call);
120
+ return calls;
121
+ }
122
+ /** Step 0. Out-of-date reads become a one-line note (changed in place); returns how many. */
123
+ export function dropSupersededReads(messages) {
124
+ const calls = callsById(messages);
125
+ const readLater = new Set();
126
+ const changedLater = new Set();
127
+ let dropped = 0;
128
+ for (let i = messages.length - 1; i >= 0; i--) {
129
+ const message = messages[i];
130
+ // Calls are seen before their results when walking back: a write here changes the files read before it.
131
+ for (const call of message.tool_calls ?? []) {
132
+ if (WRITE_TOOLS.has(call.function.name)) {
133
+ const path = pathOfCall(call);
134
+ if (path)
135
+ changedLater.add(path);
136
+ }
137
+ }
138
+ if (message.role !== 'tool' || !message.tool_call_id || !message.content || message.content.startsWith('['))
139
+ continue;
140
+ const call = calls.get(message.tool_call_id);
141
+ if (!call || !READ_TOOLS.has(call.function.name))
142
+ continue;
143
+ const key = readKey(call);
144
+ const path = pathOfCall(call);
145
+ if (!key || !path)
146
+ continue;
147
+ if (readLater.has(key)) {
148
+ message.content = `[Read again later: ${path}]`;
149
+ dropped++;
150
+ }
151
+ else if (changedLater.has(path)) {
152
+ message.content = `[${path} was changed after this read]`;
153
+ dropped++;
154
+ }
155
+ readLater.add(key);
156
+ }
157
+ return dropped;
158
+ }
159
+ /** The latest read of each file still as it was read (not changed since), newest first, within `maxTokens`. */
160
+ export function openFiles(messages, maxTokens = OPEN_FILES_TOKENS) {
161
+ const calls = callsById(messages);
162
+ const seen = new Set();
163
+ const changed = new Set();
164
+ const out = [];
165
+ let used = 0;
166
+ for (let i = messages.length - 1; i >= 0; i--) {
167
+ const message = messages[i];
168
+ for (const call of message.tool_calls ?? []) {
169
+ if (WRITE_TOOLS.has(call.function.name)) {
170
+ const path = pathOfCall(call);
171
+ if (path)
172
+ changed.add(path);
173
+ }
174
+ }
175
+ if (message.role !== 'tool' || !message.tool_call_id || !message.content || message.content.startsWith('['))
176
+ continue;
177
+ const call = calls.get(message.tool_call_id);
178
+ if (!call || !READ_TOOLS.has(call.function.name))
179
+ continue;
180
+ const path = pathOfCall(call);
181
+ const key = readKey(call);
182
+ if (!path || !key || seen.has(key) || changed.has(path))
183
+ continue;
184
+ seen.add(key);
185
+ const size = Math.ceil(message.content.length / 4);
186
+ if (used + size > maxTokens)
187
+ continue;
188
+ used += size;
189
+ out.push({ path, content: message.content });
190
+ }
191
+ return out;
192
+ }
74
193
  /** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
75
194
  export function pruneToolOutputs(messages, keepToolTokens) {
76
195
  let kept = 0;
@@ -174,14 +293,21 @@ export function makeRoom(messages, budget, reportedPromptTokens) {
174
293
  if (!overBudget(messages, budget, reportedPromptTokens))
175
294
  return null;
176
295
  const overhead = Math.max(0, (reportedPromptTokens ?? 0) - estimateTokens(messages));
177
- const cleared = pruneToolOutputs(messages, budget.keepToolTokens);
296
+ // Out-of-date reads go first: the room they free loses nothing, and the newest read of each file outlives the prune.
297
+ const superseded = dropSupersededReads(messages);
298
+ const cleared = pruneToolOutputs(messages, budget.keepToolTokens) + superseded;
178
299
  const shortened = shortenOldArguments(messages, budget.keepToolTokens);
179
300
  if (estimateTokens(messages) + overhead <= lowWater(budget))
180
301
  return { cleared, shortened, landed: true, cut: -1 };
181
302
  return { cleared, shortened, landed: false, cut: cutPoint(messages, tailBudget(messages, budget, overhead)) };
182
303
  }
183
- /** The request that asks the model to summarize the part being replaced. */
184
- export function summaryRequest(part) {
304
+ /**
305
+ * The request that asks the model to summarize the part being replaced. `goal`: the job's request (the first owner
306
+ * message), which the part never holds. Without it every summary of trace 01a1e93d (17 Nov) began "Goal: not specified"
307
+ * and ended "re-read AGENTS.md ... proceed once the task is established", so after each one the job started over:
308
+ * 33 summaries, 246 reads, 3 writes.
309
+ */
310
+ export function summaryRequest(part, goal, earlier) {
185
311
  const transcript = part
186
312
  .map((message) => {
187
313
  if (message.role === 'tool')
@@ -198,20 +324,53 @@ export function summaryRequest(part) {
198
324
  content: 'You summarize a coding session so another AI agent can continue it without the full history. ' +
199
325
  'Write plain text with these parts: the goal; what has been done; files created or changed (paths); ' +
200
326
  'commands that matter and their results; decisions and constraints from the owner; what is left to do next. ' +
201
- 'Be specific (names, paths, errors). Do not invent anything.',
327
+ 'Be specific (names, paths, errors). Do not invent anything. ' +
328
+ 'The goal is the job\'s request, given first: state it, never write that it is unknown. ' +
329
+ 'Write what was learned from each file that was read (its facts, not "it was read"), and never tell the agent ' +
330
+ 'to read again a file whose result was cleared: it continues from your summary, it does not start over.',
331
+ },
332
+ {
333
+ role: 'user',
334
+ content: (goal?.trim() ? `The job's request:\n\n${goal.trim().slice(0, 12_000)}\n\n---\n` : '') +
335
+ (earlier?.trim() ? `The summary of the work before this part (carry what still matters into yours):\n\n${earlier.trim().slice(0, 12_000)}\n\n---\n` : '') +
336
+ `Summarize this session so far:\n\n${transcript.slice(-400_000)}`,
202
337
  },
203
- { role: 'user', content: `Summarize this session so far:\n\n${transcript.slice(-400_000)}` },
204
338
  ];
205
339
  }
206
- /** Step 2's result: the system prompt, the goal with the summary, then the kept tail. */
207
- export function withSummary(messages, cut, summary) {
208
- const goal = (messages[1]?.content ?? '').slice(0, 20_000);
340
+ const SUMMARIZED_MARK = '\n\n---\nThe conversation after this request was summarized to save space. Summary of the work so far:\n\n';
341
+ const OPEN_FILES_MARK = '\n\n---\nFiles you have open, as you last read them';
342
+ /**
343
+ * The job's first owner message, split: the request as the owner gave it, and the summary an earlier compaction put
344
+ * after it (null before the first). The open files and the stall note are left out of `earlier`: the next summary gets
345
+ * the files again from `openFiles`, and a fresh note. So a second summary neither nests the first nor loses it.
346
+ */
347
+ export function splitHead(content) {
348
+ const text = content ?? '';
349
+ const at = text.indexOf(SUMMARIZED_MARK);
350
+ if (at < 0)
351
+ return { request: text, earlier: null };
352
+ const rest = text.slice(at + SUMMARIZED_MARK.length);
353
+ const files = rest.indexOf(OPEN_FILES_MARK);
354
+ return { request: text.slice(0, at), earlier: (files < 0 ? rest : rest.slice(0, files)).trim() || null };
355
+ }
356
+ /**
357
+ * Step 2's result: the system prompt, the goal with the summary, then the kept tail. The files the job had read and not
358
+ * changed since, from the part summarized away (the tail keeps its own), go with the summary as they were read, so the
359
+ * job carries on without reading them again. `note`: the session's stall warning, when it has one.
360
+ */
361
+ export function withSummary(messages, cut, summary, note) {
362
+ const goal = splitHead(messages[1]?.content).request.slice(0, 20_000);
363
+ const inTail = new Set(openFiles(messages.slice(cut), Number.MAX_SAFE_INTEGER).map((one) => one.path));
364
+ const open = openFiles(messages.slice(0, cut)).filter((one) => !inTail.has(one.path));
365
+ const files = open.length
366
+ ? `${OPEN_FILES_MARK} (you have not changed them since; do not read them again unless you change them):\n\n` +
367
+ open.map((one) => `${one.content}`).join('\n\n')
368
+ : '';
209
369
  const head = [
210
370
  messages[0],
211
371
  {
212
372
  role: 'user',
213
- content: `${goal}\n\n---\nThe conversation after this request was summarized to save space. ` +
214
- `Summary of the work so far:\n\n${summary.trim()}`,
373
+ content: `${goal}${SUMMARIZED_MARK}${summary.trim()}${note ? `\n\n${note}` : ''}${files}`,
215
374
  },
216
375
  ];
217
376
  // Two owner messages in a row read as one; an acknowledgement keeps them apart.
@@ -15,6 +15,8 @@ export interface CoreEngineOptions {
15
15
  onTurnEnded?: (summary: string) => Promise<void> | void;
16
16
  stream?: boolean;
17
17
  budget?: CoreSessionOptions['budget'];
18
+ /** The model that writes the job's summaries (a cheap one); the job's own model when unset. */
19
+ summaryModel?: string;
18
20
  askFor?: string[];
19
21
  extraTools?: CoreSessionOptions['extraTools'];
20
22
  extraPrompt?: string;
@@ -52,6 +54,9 @@ export interface RunEngineInput {
52
54
  modelBaseUrl: string;
53
55
  modelKey: string | (() => string);
54
56
  model?: string;
57
+ /** The model that writes the job's summaries (see CoreSessionOptions.summaryModel). */
58
+ summaryModel?: string;
59
+ budget?: CoreSessionOptions['budget'];
55
60
  fetch?: typeof fetch;
56
61
  maxSteps?: number;
57
62
  state?: EngineSessionState;
@@ -59,6 +59,7 @@ export class CoreEngine {
59
59
  ...(this.options.onTurnEnded ? { onTurnEnded: this.options.onTurnEnded } : {}),
60
60
  ...(this.options.stream !== undefined ? { stream: this.options.stream } : {}),
61
61
  ...(this.options.budget ? { budget: this.options.budget } : {}),
62
+ ...(this.options.summaryModel ? { summaryModel: this.options.summaryModel } : {}),
62
63
  ...(this.options.askFor ? { askFor: this.options.askFor } : {}),
63
64
  ...(this.options.extraTools?.length ? { extraTools: this.options.extraTools } : {}),
64
65
  ...(this.options.extraPrompt ? { extraPrompt: this.options.extraPrompt } : {}),
@@ -84,6 +85,8 @@ export async function runEngine(input) {
84
85
  ask: (ask) => input.runnerClient.ask(ask),
85
86
  ...serviceExtras(input),
86
87
  ...(input.model ? { model: input.model } : {}),
88
+ ...(input.summaryModel ? { summaryModel: input.summaryModel } : {}),
89
+ ...(input.budget ? { budget: input.budget } : {}),
87
90
  ...(input.fetch ? { fetch: input.fetch } : {}),
88
91
  ...(input.maxSteps ? { maxSteps: input.maxSteps } : {}),
89
92
  ...(input.onCheckpoint ? { onCheckpoint: input.onCheckpoint } : {}),
@@ -7,7 +7,7 @@ export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js
7
7
  export { needsAsk } from './tools/shell.js';
8
8
  export { ShellSession } from './tools/shell.js';
9
9
  export { applyPatchTool, parsePatch, applyChunks, usesPatch, PatchError } from './tools/patch.js';
10
- export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, type RoomPlan, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
10
+ export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, type RoomPlan, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, summaryRequest, splitHead, dropSupersededReads, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
11
11
  export { vaultFetchTool, type KeyRelay } from './tools/relay.js';
12
12
  export { workserTool, type ToolsClient } from './tools/workser-tool.js';
13
13
  export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES, type FileClient } from './tools/publish.js';
package/dist/src/index.js CHANGED
@@ -7,7 +7,7 @@ export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js
7
7
  export { needsAsk } from './tools/shell.js';
8
8
  export { ShellSession } from './tools/shell.js';
9
9
  export { applyPatchTool, parsePatch, applyChunks, usesPatch, PatchError } from './tools/patch.js';
10
- export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, PRUNED_NOTE, } from './compact.js';
10
+ export { DEFAULT_BUDGET, COST_COMPACT_TOKENS, compactAt, lowWater, tailBudget, makeRoom, LOW_WATER_SHARE, MIN_TAIL_TOKENS, SUMMARY_ALLOWANCE_TOKENS, estimateTokens, pruneToolOutputs, shortenOldArguments, cutPoint, withSummary, summaryRequest, splitHead, dropSupersededReads, openFiles, READ_TOOLS, WRITE_TOOLS, OPEN_FILES_TOKENS, PRUNED_NOTE, } from './compact.js';
11
11
  export { vaultFetchTool } from './tools/relay.js';
12
12
  export { workserTool } from './tools/workser-tool.js';
13
13
  export { publishFileTool, mimeOf, MAX_PUBLISH_BYTES } from './tools/publish.js';
@@ -12,6 +12,12 @@ export interface CoreSessionOptions {
12
12
  /** The run token — read on every call, so a rotation takes effect at once. */
13
13
  modelKey: string | (() => string);
14
14
  model?: string;
15
+ /**
16
+ * The model that writes the job's summaries (owner 17 Nov, trace 01a1e9c7: four summaries were 12k output tokens on
17
+ * Kimi at $8.80 a 1M). A summary is plain text work, so a cheap model does it; the job's own model when unset, and
18
+ * again when this one fails.
19
+ */
20
+ summaryModel?: string;
15
21
  /** Ask the owner (the runner's `ask`); the answer comes back through `answer()`. */
16
22
  ask?: (ask: Ask) => Promise<AskResult>;
17
23
  fetch?: typeof fetch;
@@ -73,6 +79,11 @@ export declare class CoreSession implements EngineSession {
73
79
  private compactions;
74
80
  /** Turns to wait before trying to make room again after a summary that failed (each try is a paid model call). */
75
81
  private compactCooldown;
82
+ /**
83
+ * Summaries in a row with no file changed between them (owner 17 Nov, trace 01a1e8ed: twelve in twelve minutes,
84
+ * nothing written). Each one doubles the next trigger (`budget`) and the summary tells the job to write.
85
+ */
86
+ private stalls;
76
87
  constructor(options: CoreSessionOptions);
77
88
  /** The id `engine.session` names, so a restarted runner can resume this job. */
78
89
  get id(): string;
@@ -116,6 +127,8 @@ export declare class CoreSession implements EngineSession {
116
127
  */
117
128
  private compactIfNeeded;
118
129
  private checkpoint;
130
+ /** Whether this exact text is still somewhere in the conversation (a tool result, or the summary's open files). */
131
+ private inConversation;
119
132
  private runTool;
120
133
  /**
121
134
  * READ ENOUGH, NOW WRITE (owner 12 Nov trace: a job read and re-planned for 20 minutes, about 5k tokens of reasoning
@@ -1,12 +1,14 @@
1
1
  import { chat, ModelError } from './model.js';
2
2
  import { environmentPrompt, providerPrompt } from './prompts.js';
3
3
  import { bashTool, ShellSession } from './tools/shell.js';
4
- import { editTool, readTool, todoWriteTool, writeTool } from './tools/files.js';
4
+ import { editTool, multiEditTool, readTool, todoWriteTool, writeTool } from './tools/files.js';
5
5
  import { applyPatchTool, usesPatch } from './tools/patch.js';
6
- import { DEFAULT_BUDGET, makeRoom, summaryRequest, withSummary, } from './compact.js';
6
+ import { DEFAULT_BUDGET, makeRoom, summaryRequest, splitHead, withSummary, WRITE_TOOLS, } from './compact.js';
7
7
  import { globTool, grepTool } from './tools/search.js';
8
8
  import { repairArgs, TOOL_ALIASES } from './tools/repair.js';
9
9
  import { PermissionDenied } from './tools/types.js';
10
+ /** A read this long or longer is checked against the conversation before it is sent again (a short one costs nothing). */
11
+ const SAME_READ_MIN_CHARS = 600;
10
12
  export const ENGINE_NAME = 'workser-core';
11
13
  const STATE_VERSION = 2;
12
14
  /** Deltas are sent in pieces of about this size or this often, not per token. */
@@ -54,6 +56,11 @@ export class CoreSession {
54
56
  compactions = 0;
55
57
  /** Turns to wait before trying to make room again after a summary that failed (each try is a paid model call). */
56
58
  compactCooldown = 0;
59
+ /**
60
+ * Summaries in a row with no file changed between them (owner 17 Nov, trace 01a1e8ed: twelve in twelve minutes,
61
+ * nothing written). Each one doubles the next trigger (`budget`) and the summary tells the job to write.
62
+ */
63
+ stalls = 0;
57
64
  constructor(options) {
58
65
  this.options = options;
59
66
  this.done = new Promise((resolve) => (this.settle = resolve));
@@ -80,11 +87,13 @@ export class CoreSession {
80
87
  }
81
88
  get budget() {
82
89
  const fromInput = Number(this.options.brief.run.input?.context_tokens);
83
- return {
90
+ const base = {
84
91
  ...DEFAULT_BUDGET,
85
92
  ...(Number.isFinite(fromInput) && fromInput >= 8_000 ? { contextTokens: fromInput } : {}),
86
93
  ...(this.options.budget ?? {}),
87
94
  };
95
+ // The stall guard: each summary with no change to any file since the one before doubles the room, up to 4x.
96
+ return this.stalls ? { ...base, contextTokens: base.contextTokens * Math.min(4, 2 ** this.stalls) } : base;
88
97
  }
89
98
  get idle() {
90
99
  return !this.working;
@@ -102,7 +111,7 @@ export class CoreSession {
102
111
  const info = await this.options.place.info();
103
112
  this.root = info.root;
104
113
  // Upstream's rule: GPT models edit with apply_patch, everyone else with edit and write.
105
- const editing = usesPatch(this.model) ? [applyPatchTool] : [writeTool, editTool];
114
+ const editing = usesPatch(this.model) ? [applyPatchTool] : [writeTool, editTool, multiEditTool];
106
115
  this.tools.push(bashTool({ os: info.os, tmp: '/tmp', shell: this.shell }), readTool, ...editing, globTool, grepTool, todoWriteTool, ...(this.options.extraTools ?? []));
107
116
  if (this.messages.length === 0) {
108
117
  const isGit = (await this.options.place.exec('git rev-parse --is-inside-work-tree', { timeoutMs: 10_000 }))
@@ -399,25 +408,47 @@ export class CoreSession {
399
408
  const cut = plan.cut;
400
409
  if (cut < 0)
401
410
  return;
411
+ // Nothing written since the last summary (its messages are all after the head): this one is a stall.
412
+ const wrote = this.messages.slice(2).some((message) => (message.tool_calls ?? []).some((call) => WRITE_TOOLS.has(call.function.name)));
413
+ const stalled = this.compactions > 0 && !wrote;
402
414
  try {
403
- const result = await chat({
415
+ const head = splitHead(this.messages[1]?.content);
416
+ const request = summaryRequest(this.messages.slice(2, cut), head.request, head.earlier);
417
+ const ask = (model) => chat({
404
418
  baseUrl: this.options.modelBaseUrl,
405
419
  key: this.key(),
406
- model: this.model,
407
- messages: summaryRequest(this.messages.slice(2, cut)),
420
+ model,
421
+ messages: request,
408
422
  tools: [],
409
423
  signal: this.abort.signal,
410
424
  ...(this.options.fetch ? { fetch: this.options.fetch } : {}),
411
425
  });
426
+ const cheap = this.options.summaryModel?.trim();
427
+ let model = cheap && cheap !== this.model ? cheap : this.model;
428
+ let result;
429
+ try {
430
+ result = await ask(model);
431
+ if (!result.message.content?.trim() && model !== this.model)
432
+ throw new Error('empty summary');
433
+ }
434
+ catch (error) {
435
+ if (model === this.model || this.abort.signal.aborted)
436
+ throw error;
437
+ model = this.model;
438
+ result = await ask(model);
439
+ }
412
440
  const summary = result.message.content?.trim();
413
441
  if (!summary) {
414
442
  this.compactCooldown = COMPACT_RETRY_AFTER_TURNS;
415
443
  return;
416
444
  }
417
445
  if (result.usage) {
418
- this.options.emit('usage', usageEvent(this.model, result.usage, result.provider));
446
+ this.options.emit('usage', usageEvent(model, result.usage, result.provider));
419
447
  }
420
- const next = withSummary(this.messages, cut, summary);
448
+ this.stalls = stalled ? this.stalls + 1 : 0;
449
+ const next = withSummary(this.messages, cut, summary, stalled
450
+ ? 'You made room twice without changing a file. The files you need are below: make your change now, without reading them again.'
451
+ : undefined);
421
452
  this.messages.splice(0, this.messages.length, ...next);
422
453
  this.compactions += 1;
423
454
  this.options.emit('log', { level: 'info', text: "Summarized the earlier conversation to keep the job in the model's window." });
@@ -440,6 +471,10 @@ export class CoreSession {
440
471
  // A missed checkpoint costs a resume point, never the job.
441
472
  }
442
473
  }
474
+ /** Whether this exact text is still somewhere in the conversation (a tool result, or the summary's open files). */
475
+ inConversation(text) {
476
+ return this.messages.some((message) => typeof message.content === 'string' && message.content.includes(text));
477
+ }
443
478
  async runTool(callId, name, rawArgs) {
444
479
  const stepId = `s${++this.stepSeq}`;
445
480
  // A name from another harness runs the tool it means (tools/repair.ts), and the result says which one ran.
@@ -489,6 +524,15 @@ export class CoreSession {
489
524
  : `Error: ${error?.message ?? String(error)}`;
490
525
  }
491
526
  }
527
+ // THE SAME READ, ALREADY IN HAND (owner 17 Nov, trace 01a1e9c7: after a summary the job read six files again that
528
+ // the summary carried word for word, the prompt went back to 58k and made it summarize again seven calls later).
529
+ // The read still runs; only a result identical to a copy still in the conversation (an earlier read, or the open
530
+ // files a summary carries) becomes a note. A file changed in any way (an edit, a script, a formatter) comes back
531
+ // whole. The note starts with "[", so it never counts as a newer read (compact.ts) and the full copy stays.
532
+ if (status === 'ok' && tool?.name === 'read' && output.length >= SAME_READ_MIN_CHARS && this.inConversation(output)) {
533
+ const path = typeof args.filePath === 'string' ? args.filePath : 'this file';
534
+ output = `[Unchanged since you read it: ${path} is already in this conversation word for word (an earlier read, or the files you have open). Use that copy; read it again only after you change it.]`;
535
+ }
492
536
  if (notes.length)
493
537
  output = `${output}\n\n${notes.join('\n')}`;
494
538
  this.messages.push({ role: 'tool', tool_call_id: callId, content: output });
@@ -580,7 +624,7 @@ function relativeTo(root, path) {
580
624
  return path.startsWith(`${base}/`) ? path.slice(base.length + 1) : path;
581
625
  }
582
626
  /** Tools that change a file in the job's folder (a turn with one is not "only reading"). */
583
- const CHANGING_TOOLS = new Set(['write', 'edit', 'apply_patch']);
627
+ const CHANGING_TOOLS = new Set(['write', 'edit', 'multiedit', 'apply_patch']);
584
628
  /** Turns in a row with no file changed before the job is told to write. */
585
629
  export const READ_ONLY_TURNS = 8;
586
630
  export const READ_ONLY_NUDGE = `(Workser: ${READ_ONLY_TURNS} turns in a row without changing a file. If this task needs code, you have read enough: ` +
@@ -5,6 +5,12 @@ export declare const readTool: ToolSpec;
5
5
  export declare const writeTool: ToolSpec;
6
6
  /** Upstream `edit`: oldString → newString with upstream's own matching (vendored). */
7
7
  export declare const editTool: ToolSpec;
8
+ /**
9
+ * SEVERAL CHANGES TO ONE FILE IN ONE CALL (owner 17 Nov, trace 01a1e9c7: eight edits to globals.css in eight calls, each
10
+ * resending the whole job; then the file was rewritten anyway). The edits apply in order, each to the result of the one
11
+ * before, with `edit`'s own matching; if any one fails, nothing is written and the error names it.
12
+ */
13
+ export declare const multiEditTool: ToolSpec;
8
14
  /** Upstream `todowrite`: the plan as a list; returned as JSON like upstream. */
9
15
  export declare const todoWriteTool: ToolSpec;
10
16
  /**
@@ -183,6 +183,72 @@ export const editTool = {
183
183
  return { title: filepath, output: 'Edit applied successfully.', changed: [{ path: filepath, change: 'modified' }] };
184
184
  },
185
185
  };
186
+ /**
187
+ * SEVERAL CHANGES TO ONE FILE IN ONE CALL (owner 17 Nov, trace 01a1e9c7: eight edits to globals.css in eight calls, each
188
+ * resending the whole job; then the file was rewritten anyway). The edits apply in order, each to the result of the one
189
+ * before, with `edit`'s own matching; if any one fails, nothing is written and the error names it.
190
+ */
191
+ export const multiEditTool = {
192
+ name: 'multiedit',
193
+ description: 'Make several changes to ONE file in one call: a list of edits applied in order, each to the result of the one before, ' +
194
+ 'all or nothing (if one fails, the file is left as it was and the error says which). Use it instead of several edit ' +
195
+ 'calls to the same file. Each edit works like edit: oldString must match the file exactly (whitespace and indentation ' +
196
+ 'included) and be unique unless replaceAll is set; read the file first. To change many files, call multiedit for each ' +
197
+ 'file in the same turn rather than one per turn.',
198
+ parameters: {
199
+ type: 'object',
200
+ properties: {
201
+ filePath: { type: 'string', description: 'The absolute path to the file to modify' },
202
+ edits: {
203
+ type: 'array',
204
+ minItems: 1,
205
+ description: 'The changes, applied in order',
206
+ items: {
207
+ type: 'object',
208
+ properties: {
209
+ oldString: { type: 'string', description: 'The text to replace' },
210
+ newString: { type: 'string', description: 'The text to replace it with (must be different from oldString)' },
211
+ replaceAll: { type: 'boolean', description: 'Replace all occurrences of oldString (default false)' },
212
+ },
213
+ required: ['oldString', 'newString'],
214
+ },
215
+ },
216
+ },
217
+ required: ['filePath', 'edits'],
218
+ },
219
+ async execute(args, ctx) {
220
+ const filepath = absolute(ctx.root, str(args.filePath, 'filePath'));
221
+ const edits = Array.isArray(args.edits) ? args.edits : [];
222
+ if (!edits.length)
223
+ throw new Error('No edits given: edits must list at least one change.');
224
+ const kind = await exists(ctx, filepath);
225
+ if (!kind)
226
+ throw new Error(`File ${filepath} not found`);
227
+ if (kind === 'dir')
228
+ throw new Error(`Path is a directory, not a file: ${filepath}`);
229
+ const before = new TextDecoder().decode(await ctx.place.readFile(filepath));
230
+ const ending = before.includes('\r\n') ? '\r\n' : '\n';
231
+ let text = before.replaceAll('\r\n', '\n');
232
+ edits.forEach((one, index) => {
233
+ const oldString = typeof one?.oldString === 'string' ? one.oldString : null;
234
+ const newString = typeof one?.newString === 'string' ? one.newString : null;
235
+ if (oldString === null || newString === null)
236
+ throw new Error(`Edit ${index + 1}: oldString and newString are required.`);
237
+ if (oldString === newString)
238
+ throw new Error(`Edit ${index + 1}: oldString and newString are identical.`);
239
+ try {
240
+ text = replace(text, oldString.replaceAll('\r\n', '\n'), newString.replaceAll('\r\n', '\n'), one.replaceAll === true);
241
+ }
242
+ catch (error) {
243
+ throw new Error(`Edit ${index + 1} of ${edits.length} failed, so none were applied: ${error?.message ?? String(error)}`);
244
+ }
245
+ });
246
+ await ctx.ask({ permission: 'edit', title: `Edit ${filepath}`, detail: filepath, patterns: [filepath] });
247
+ await ctx.place.writeFile(filepath, ending === '\r\n' ? text.replaceAll('\n', '\r\n') : text);
248
+ ctx.read.add(filepath);
249
+ return { title: filepath, output: `${edits.length} edit${edits.length === 1 ? '' : 's'} applied successfully.`, changed: [{ path: filepath, change: 'modified' }] };
250
+ },
251
+ };
186
252
  /** Upstream `todowrite`: the plan as a list; returned as JSON like upstream. */
187
253
  export const todoWriteTool = {
188
254
  name: 'todowrite',
@@ -19,6 +19,8 @@ export const TOOL_ALIASES = {
19
19
  create_file: 'write',
20
20
  edit_file: 'edit',
21
21
  str_replace: 'edit',
22
+ multi_edit: 'multiedit',
23
+ MultiEdit: 'multiedit',
22
24
  search: 'grep',
23
25
  find_files: 'glob',
24
26
  todo_write: 'todowrite',
@@ -1,6 +1,8 @@
1
1
  import { vendored } from '../vendor.js';
2
2
  import { absolute, str } from './types.js';
3
3
  const LIMIT = 100;
4
+ /** Left out of `glob` unless the pattern names them. */
5
+ const SKIPPED_DIRS = ['node_modules', '.next', 'dist', 'build', '.turbo', '.vercel'];
4
6
  const MAX_LINE_LENGTH = 2000;
5
7
  function quote(value) {
6
8
  return `'${value.replace(/'/g, `'\\''`)}'`;
@@ -23,10 +25,13 @@ export const globTool = {
23
25
  async execute(args, ctx) {
24
26
  const pattern = str(args.pattern, 'pattern');
25
27
  const search = typeof args.path === 'string' && args.path ? absolute(ctx.root, args.path) : ctx.root;
28
+ // Installed packages and build output are never the job's files (trace 01a1e93d: `**/*` answered with 100 paths
29
+ // under node_modules/date-fns). A pattern that names one of them still finds it.
30
+ const skip = /node_modules|\.next|dist\b|build\b/.test(pattern) ? [] : SKIPPED_DIRS;
26
31
  const result = await ctx.place.exec(
27
- // ripgrep, as upstream; plain `find` where a machine has no rg.
28
- `if command -v rg >/dev/null 2>&1; then rg --files --hidden --glob '!.git' --glob ${quote(pattern)} --sortr modified . 2>/dev/null; ` +
29
- `else find . -type f -not -path './.git/*' \\( -path ${quote(`./${pattern.replace(/^\.\//, '')}`)} -o -name ${quote(pattern)} \\) 2>/dev/null; fi | head -n ${LIMIT}`, { cwd: search, timeoutMs: 60_000, signal: ctx.signal });
32
+ // ripgrep, as upstream (a later --glob wins, so the skips go after the pattern); plain `find` where a machine has no rg.
33
+ `if command -v rg >/dev/null 2>&1; then rg --files --hidden --glob '!.git' --glob ${quote(pattern)} ${skip.map((dir) => `--glob '!${dir}'`).join(' ')} --sortr modified . 2>/dev/null; ` +
34
+ `else find . -type f -not -path './.git/*' ${skip.map((dir) => `-not -path '*/${dir}/*'`).join(' ')} \\( -path ${quote(`./${pattern.replace(/^\.\//, '')}`)} -o -name ${quote(pattern)} \\) 2>/dev/null; fi | head -n ${LIMIT}`, { cwd: search, timeoutMs: 60_000, signal: ctx.signal });
30
35
  const files = result.stdout.split('\n').map((one) => one.trim()).filter(Boolean).map((one) => absolute(search, one.replace(/^\.\//, '')));
31
36
  const output = [];
32
37
  if (files.length === 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workser/engine",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "Workser's coding engine for the 'loop in our service' mode: OpenCode's agent contract (its prompts, tool schemas and edit semantics, vendored from v1.19.5) running on Node, with every file and shell action going through a Place — so the machine can be a remote sandbox and no model key is ever on it.",
5
5
  "license": "MIT",
6
6
  "type": "module",