@workser/engine 0.1.19 → 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.
@@ -82,7 +82,7 @@ export function overBudget(messages, budget, reportedPromptTokens) {
82
82
  * trigger and tell the job to write.
83
83
  */
84
84
  export const READ_TOOLS = new Set(['read']);
85
- export const WRITE_TOOLS = new Set(['write', 'edit', 'apply_patch']);
85
+ export const WRITE_TOOLS = new Set(['write', 'edit', 'multiedit', 'apply_patch']);
86
86
  /** The most of the open files' latest contents a summary carries, in tokens. */
87
87
  export const OPEN_FILES_TOKENS = 20_000;
88
88
  /** A tool call's file path (`filePath`, `path`, `file_path`), or null. */
@@ -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 } : {}),
@@ -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;
@@ -121,6 +127,8 @@ export declare class CoreSession implements EngineSession {
121
127
  */
122
128
  private compactIfNeeded;
123
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;
124
132
  private runTool;
125
133
  /**
126
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
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. */
@@ -109,7 +111,7 @@ export class CoreSession {
109
111
  const info = await this.options.place.info();
110
112
  this.root = info.root;
111
113
  // Upstream's rule: GPT models edit with apply_patch, everyone else with edit and write.
112
- const editing = usesPatch(this.model) ? [applyPatchTool] : [writeTool, editTool];
114
+ const editing = usesPatch(this.model) ? [applyPatchTool] : [writeTool, editTool, multiEditTool];
113
115
  this.tools.push(bashTool({ os: info.os, tmp: '/tmp', shell: this.shell }), readTool, ...editing, globTool, grepTool, todoWriteTool, ...(this.options.extraTools ?? []));
114
116
  if (this.messages.length === 0) {
115
117
  const isGit = (await this.options.place.exec('git rev-parse --is-inside-work-tree', { timeoutMs: 10_000 }))
@@ -410,22 +412,38 @@ export class CoreSession {
410
412
  const wrote = this.messages.slice(2).some((message) => (message.tool_calls ?? []).some((call) => WRITE_TOOLS.has(call.function.name)));
411
413
  const stalled = this.compactions > 0 && !wrote;
412
414
  try {
413
- 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({
414
418
  baseUrl: this.options.modelBaseUrl,
415
419
  key: this.key(),
416
- model: this.model,
417
- messages: summaryRequest(this.messages.slice(2, cut), splitHead(this.messages[1]?.content).request, splitHead(this.messages[1]?.content).earlier),
420
+ model,
421
+ messages: request,
418
422
  tools: [],
419
423
  signal: this.abort.signal,
420
424
  ...(this.options.fetch ? { fetch: this.options.fetch } : {}),
421
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
+ }
422
440
  const summary = result.message.content?.trim();
423
441
  if (!summary) {
424
442
  this.compactCooldown = COMPACT_RETRY_AFTER_TURNS;
425
443
  return;
426
444
  }
427
445
  if (result.usage) {
428
- this.options.emit('usage', usageEvent(this.model, result.usage, result.provider));
446
+ this.options.emit('usage', usageEvent(model, result.usage, result.provider));
429
447
  }
430
448
  this.stalls = stalled ? this.stalls + 1 : 0;
431
449
  const next = withSummary(this.messages, cut, summary, stalled
@@ -453,6 +471,10 @@ export class CoreSession {
453
471
  // A missed checkpoint costs a resume point, never the job.
454
472
  }
455
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
+ }
456
478
  async runTool(callId, name, rawArgs) {
457
479
  const stepId = `s${++this.stepSeq}`;
458
480
  // A name from another harness runs the tool it means (tools/repair.ts), and the result says which one ran.
@@ -502,6 +524,15 @@ export class CoreSession {
502
524
  : `Error: ${error?.message ?? String(error)}`;
503
525
  }
504
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
+ }
505
536
  if (notes.length)
506
537
  output = `${output}\n\n${notes.join('\n')}`;
507
538
  this.messages.push({ role: 'tool', tool_call_id: callId, content: output });
@@ -593,7 +624,7 @@ function relativeTo(root, path) {
593
624
  return path.startsWith(`${base}/`) ? path.slice(base.length + 1) : path;
594
625
  }
595
626
  /** Tools that change a file in the job's folder (a turn with one is not "only reading"). */
596
- const CHANGING_TOOLS = new Set(['write', 'edit', 'apply_patch']);
627
+ const CHANGING_TOOLS = new Set(['write', 'edit', 'multiedit', 'apply_patch']);
597
628
  /** Turns in a row with no file changed before the job is told to write. */
598
629
  export const READ_ONLY_TURNS = 8;
599
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',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workser/engine",
3
- "version": "0.1.19",
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",