motifcode 0.4.0 → 0.4.1

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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.4.0",
3
- "bytes": 2729229,
4
- "node": "v22.23.2"
2
+ "version": "0.4.1",
3
+ "bytes": 2732434,
4
+ "node": "v22.23.3"
5
5
  }
package/dist/motif.js CHANGED
@@ -48192,6 +48192,8 @@ var TransportError = class extends Error {
48192
48192
  * limit hit three more times. The loop waits at least this long.
48193
48193
  */
48194
48194
  retryAfterMs;
48195
+ /** The server's own error code, for a `generation` error. */
48196
+ code;
48195
48197
  constructor(message2, opts) {
48196
48198
  super(message2, opts.cause !== void 0 ? { cause: opts.cause } : void 0);
48197
48199
  this.name = "TransportError";
@@ -48199,6 +48201,7 @@ var TransportError = class extends Error {
48199
48201
  if (opts.status !== void 0) this.status = opts.status;
48200
48202
  if (opts.body !== void 0) this.body = opts.body;
48201
48203
  if (opts.retryAfterMs !== void 0) this.retryAfterMs = opts.retryAfterMs;
48204
+ if (opts.code !== void 0) this.code = opts.code;
48202
48205
  this.retryable = opts.retryable ?? defaultRetryable(opts.kind, opts.status);
48203
48206
  }
48204
48207
  };
@@ -48206,6 +48209,7 @@ function defaultRetryable(kind, status) {
48206
48209
  switch (kind) {
48207
48210
  case "network":
48208
48211
  case "timeout":
48212
+ case "generation":
48209
48213
  return true;
48210
48214
  case "http":
48211
48215
  return status !== void 0 && (status >= 500 || status === 429);
@@ -48290,6 +48294,7 @@ function requestBody(req, model) {
48290
48294
  if (req.maxTokens !== void 0) body["max_tokens"] = req.maxTokens;
48291
48295
  if (req.seed !== void 0) body["seed"] = req.seed;
48292
48296
  if (req.stop) body["stop"] = req.stop;
48297
+ if (req.repetitionPenalty !== void 0) body["repetition_penalty"] = req.repetitionPenalty;
48293
48298
  if (req.raw) {
48294
48299
  if (req.prompt === void 0) throw new Error("raw completion requires a prompt");
48295
48300
  body["prompt"] = req.prompt;
@@ -48469,6 +48474,7 @@ async function readStream(res, onDelta, signal) {
48469
48474
  let finish;
48470
48475
  let usage;
48471
48476
  let cost;
48477
+ let stopped;
48472
48478
  const calls = /* @__PURE__ */ new Map();
48473
48479
  const handle2 = (line2) => {
48474
48480
  if (!line2.startsWith("data:")) return false;
@@ -48483,6 +48489,13 @@ async function readStream(res, onDelta, signal) {
48483
48489
  if (typeof chunk !== "object" || chunk === null) return false;
48484
48490
  if (chunk.usage) usage = chunk.usage;
48485
48491
  cost = parseCost(chunk) ?? cost;
48492
+ if (chunk.error && typeof chunk.error === "object") {
48493
+ stopped = {
48494
+ code: typeof chunk.error.code === "string" ? chunk.error.code : "generation_error",
48495
+ message: typeof chunk.error.message === "string" ? chunk.error.message : ""
48496
+ };
48497
+ return true;
48498
+ }
48486
48499
  const choice = chunk.choices?.[0];
48487
48500
  if (!choice) return false;
48488
48501
  if (choice.finish_reason) finish = choice.finish_reason;
@@ -48532,6 +48545,13 @@ async function readStream(res, onDelta, signal) {
48532
48545
  if (finished) break;
48533
48546
  }
48534
48547
  if (!finished && buffer.trim() !== "") handle2(buffer.trim());
48548
+ if (stopped) {
48549
+ await reader.cancel().catch(() => void 0);
48550
+ throw new TransportError(`the server stopped the generation: ${stopped.code}${stopped.message ? ` (${stopped.message})` : ""}`, {
48551
+ kind: "generation",
48552
+ code: stopped.code
48553
+ });
48554
+ }
48535
48555
  const toolCalls = [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, c]) => ({ ...c.id ? { id: c.id } : {}, type: "function", function: { name: c.name, arguments: c.arguments } }));
48536
48556
  return {
48537
48557
  choices: [
@@ -48997,6 +49017,8 @@ var EmptyTaskError = class extends Error {
48997
49017
  function normalizeSummary(s) {
48998
49018
  return s.trim().replace(/\s+/g, " ");
48999
49019
  }
49020
+ var MAX_REGENERATIONS = 2;
49021
+ var RETRY_REPETITION_PENALTY = 1.05;
49000
49022
  async function runLoop(opts) {
49001
49023
  const {
49002
49024
  transport,
@@ -49175,15 +49197,32 @@ async function runLoop(opts) {
49175
49197
  emit({ type: "prefix", sharedChars, totalChars });
49176
49198
  let response;
49177
49199
  let attempt = 0;
49200
+ let regenerations = 0;
49201
+ let current = request;
49178
49202
  for (; ; ) {
49179
49203
  try {
49180
- response = await transport.complete(request);
49204
+ response = await transport.complete(current);
49181
49205
  break;
49182
49206
  } catch (err) {
49183
49207
  if (!TransportError.is(err)) throw err;
49184
49208
  const te = err;
49185
49209
  transportErrors++;
49186
49210
  if (te.kind === "aborted") return finish("aborted");
49211
+ if (te.kind === "generation") {
49212
+ if (regenerations < MAX_REGENERATIONS) {
49213
+ regenerations++;
49214
+ current = { ...request, repetitionPenalty: RETRY_REPETITION_PENALTY };
49215
+ emit({
49216
+ type: "notice",
49217
+ level: "warn",
49218
+ text: `${te.message}; retry ${regenerations}/${MAX_REGENERATIONS} with repetition_penalty ${RETRY_REPETITION_PENALTY}`
49219
+ });
49220
+ continue;
49221
+ }
49222
+ emit({ type: "notice", level: "warn", text: `${te.message}; no reply after ${MAX_REGENERATIONS} retries` });
49223
+ response = { content: "", rawText: "", finishReason: "stop", ms: 0 };
49224
+ break;
49225
+ }
49187
49226
  if (!te.retryable || attempt >= maxServerRetries) {
49188
49227
  emit({ type: "notice", level: "error", text: `${te.kind}: ${te.message}` });
49189
49228
  return finish("transport_error");
@@ -49267,7 +49306,7 @@ async function runLoop(opts) {
49267
49306
  checkpoint();
49268
49307
  continue;
49269
49308
  }
49270
- if (replyEnds && !leaked && response.finishReason !== "length") {
49309
+ if (replyEnds && !leaked && response.finishReason !== "length" && response.content.trim() !== "") {
49271
49310
  session.appendAll(codec.serializeAssistant(split.content, split.reasoning, parsed));
49272
49311
  checkpoint();
49273
49312
  const reply = split.content.trim();
@@ -49420,7 +49459,20 @@ Repeat that summary verbatim with \`confirm: true\`, or keep working.`,
49420
49459
  session.appendAll(codec.serializeAssistant(split.content, split.reasoning, parsed, calls));
49421
49460
  let anyFailure = false;
49422
49461
  let combinedOutput = "";
49423
- for (const call of calls) {
49462
+ for (const [index, call] of calls.entries()) {
49463
+ if (signal?.aborted) {
49464
+ for (const skipped of calls.slice(index)) {
49465
+ session.appendAll(codec.serializeObservation({
49466
+ callId: skipped.id,
49467
+ name: skipped.name,
49468
+ arguments: skipped.arguments,
49469
+ output: "Tool call not executed: the session was cancelled before this call started.",
49470
+ ok: false
49471
+ }));
49472
+ }
49473
+ checkpoint();
49474
+ return finish("aborted");
49475
+ }
49424
49476
  emit({ type: "tool_start", call });
49425
49477
  checkpoint({
49426
49478
  id: call.id,
@@ -49446,6 +49498,7 @@ Repeat that summary verbatim with \`confirm: true\`, or keep working.`,
49446
49498
  if (!result.ok) anyFailure = true;
49447
49499
  checkpoint();
49448
49500
  }
49501
+ if (signal?.aborted) return finish("aborted");
49449
49502
  const verdict = guard.observe(LoopGuard.signature(calls), combinedOutput);
49450
49503
  if (verdict.tripped) {
49451
49504
  emit({ type: "loop_detected", signature: verdict.signature.slice(0, 120), repeats: verdict.repeats });
@@ -52203,8 +52256,12 @@ var Composer = class {
52203
52256
  * Claude Code's behaviour, and for the same reason: a pasted stack trace
52204
52257
  * or file turns the composer into a wall the person cannot see their own
52205
52258
  * words in. The text goes to the model whole when the draft is sent.
52259
+ *
52260
+ * Line breaks are normalized to LF first: terminals send a pasted break as
52261
+ * CR (xterm.js) or CRLF, and a raw CR would overwrite rows when drawn.
52206
52262
  */
52207
- paste(text) {
52263
+ paste(raw) {
52264
+ const text = raw.replace(/\r\n?/g, "\n");
52208
52265
  const lines = text.split("\n").length;
52209
52266
  if (lines <= PASTE_COLLAPSE_LINES && text.length <= PASTE_COLLAPSE_CHARS) {
52210
52267
  this.insert(text);
@@ -55409,7 +55466,7 @@ var ToolExecutor = class {
55409
55466
  return runBash(
55410
55467
  str2(args, "command"),
55411
55468
  cwd,
55412
- (num(args, "timeout_s") ?? 120) * 1e3,
55469
+ (num(args, "timeout_s") ?? this.timeoutMs / 1e3) * 1e3,
55413
55470
  this.policy,
55414
55471
  () => this.scratchDir(),
55415
55472
  signal
@@ -59599,6 +59656,7 @@ ${output.slice(0, 8e3)}
59599
59656
  hooks: this.opts.hooks,
59600
59657
  skills: this.opts.skills,
59601
59658
  policy: policyForAgent({ root: cwd, tools: toolNames, readOnly: false }),
59659
+ timeoutMs: (this.settings.bashTimeout ?? 120) * 1e3,
59602
59660
  callMcp: this.mcpInvoker("root"),
59603
59661
  confirm: (call) => this.confirm(call),
59604
59662
  onHook: (event, label3, ok2) => this.screen.apply({ type: "hook", event, label: label3, ok: ok2 }),
@@ -59623,6 +59681,7 @@ ${output.slice(0, 8e3)}
59623
59681
  const childExecutor = new ToolExecutor({
59624
59682
  cwd: this.settings.cwd,
59625
59683
  skills: this.opts.skills,
59684
+ timeoutMs: (this.settings.bashTimeout ?? 120) * 1e3,
59626
59685
  callMcp: this.mcpInvoker(childScope.scopeId),
59627
59686
  confirm: (call) => this.confirm(call),
59628
59687
  policy: policyForAgent({
@@ -60004,7 +60063,8 @@ ${output.slice(0, 8e3)}
60004
60063
  ["thinking", String(this.screen.thinkingShown), src("thinking")],
60005
60064
  ["verbose", String(this.screen.verboseOutput), src("verbose")],
60006
60065
  ["compactAt", String(s.compactAt), src("compactAt")],
60007
- ["permissions", s.permissions, src("permissions")]
60066
+ ["permissions", s.permissions, src("permissions")],
60067
+ ["bashTimeout", `${s.bashTimeout ?? 120}s`, src("bashTimeout")]
60008
60068
  ];
60009
60069
  const lines = rows.map(([k, v, from]) => `${k.padEnd(16)} ${v.padEnd(40)} ${from}`);
60010
60070
  lines.push("");
@@ -60251,7 +60311,8 @@ var KEYS2 = [
60251
60311
  "thinking",
60252
60312
  "verbose",
60253
60313
  "compactAt",
60254
- "permissions"
60314
+ "permissions",
60315
+ "bashTimeout"
60255
60316
  ];
60256
60317
  function userSettingsPath(home = homedir7()) {
60257
60318
  return join13(home, ".motif", "settings.json");
@@ -60291,6 +60352,7 @@ function parseSettings(text) {
60291
60352
  int2("maxTurns", 1);
60292
60353
  int2("maxOutputTokens", 1);
60293
60354
  int2("seed", 0);
60355
+ int2("bashTimeout", 1);
60294
60356
  for (const key of ["thinking", "verbose"]) {
60295
60357
  if (obj2[key] !== void 0) {
60296
60358
  if (typeof obj2[key] === "boolean") values[key] = obj2[key];
@@ -62461,7 +62523,7 @@ function pluginConnectionUi(signal) {
62461
62523
  }
62462
62524
 
62463
62525
  // packages/cli/src/main.ts
62464
- var VERSION = "0.4.0";
62526
+ var VERSION = "0.4.1";
62465
62527
  var SHORT_FLAGS = { p: "print", c: "continue", i: "interactive", v: "verbose", h: "help" };
62466
62528
  var BOOLEAN_FLAGS = /* @__PURE__ */ new Set([
62467
62529
  "print",
@@ -62707,6 +62769,7 @@ Flags
62707
62769
  --channel-policy <p> fixed | adaptive (default fixed)
62708
62770
  --max-turns <n> turn ceiling (default 100)
62709
62771
  --max-output-tokens <n> cap on each model step
62772
+ --bash-timeout <s> seconds a shell command may run when the model sets no timeout (default 120)
62710
62773
  --seed <n> sampling seed, passed to the server
62711
62774
  --cwd <path> working directory
62712
62775
  --journal <path> write the session record here instead of .motif/sessions
@@ -62991,7 +63054,8 @@ ${"".padEnd(12)} tools: ${tools}
62991
63054
  ["thinking", showThinking ? "shown" : "hidden", args.flags["thinking"] === true ? "flag" : stored.sources.thinking ?? "default"],
62992
63055
  ["verbose", String(args.flags["verbose"] === true || stored.values.verbose === true), args.flags["verbose"] === true ? "flag" : stored.sources.verbose ?? "default"],
62993
63056
  ["compactAt", String(compactAt), stored.sources.compactAt ?? "default"],
62994
- ["permissions", permissions, typeof args.flags["permissions"] === "string" ? "flag" : stored.sources.permissions ?? "default"]
63057
+ ["permissions", permissions, typeof args.flags["permissions"] === "string" ? "flag" : stored.sources.permissions ?? "default"],
63058
+ ["bashTimeout", `${flagInt(args.flags, "bash-timeout", stored.values.bashTimeout ?? 120, 1)}s`, args.flags["bash-timeout"] !== void 0 ? "flag" : stored.sources.bashTimeout ?? "default"]
62995
63059
  ];
62996
63060
  for (const [k, v, from] of rows) process.stdout.write(`${k.padEnd(16)} ${v.padEnd(40)} ${from}
62997
63061
  `);
@@ -63088,6 +63152,7 @@ user file ${stored.userPath}${existsSync16(stored.userPath) ? "" : " (absent
63088
63152
  const maxTurns = flagInt(args.flags, "max-turns", stored.values.maxTurns ?? 100, 1);
63089
63153
  const maxOutputTokens = args.flags["max-output-tokens"] !== void 0 ? flagInt(args.flags, "max-output-tokens", 0, 1) : stored.values.maxOutputTokens;
63090
63154
  const seed = args.flags["seed"] !== void 0 ? flagInt(args.flags, "seed", 0, 0) : stored.values.seed;
63155
+ const bashTimeout = flagInt(args.flags, "bash-timeout", stored.values.bashTimeout ?? 120, 1);
63091
63156
  const channelFromFlag = typeof args.flags["channel"] === "string";
63092
63157
  if (channel !== "toolcall" && channelFromFlag && args.flags["experimental-channel"] !== true) {
63093
63158
  throw new UsageError(
@@ -63191,7 +63256,8 @@ user file ${stored.userPath}${existsSync16(stored.userPath) ? "" : " (absent
63191
63256
  cwd,
63192
63257
  theme: applyTheme(themeName) ? themeName : "motif",
63193
63258
  compactAt,
63194
- permissions
63259
+ permissions,
63260
+ bashTimeout
63195
63261
  },
63196
63262
  settingsInfo: stored,
63197
63263
  settingSources: {
@@ -63205,6 +63271,7 @@ user file ${stored.userPath}${existsSync16(stored.userPath) ? "" : " (absent
63205
63271
  ["seed", "seed"],
63206
63272
  ["theme", "theme"],
63207
63273
  ["thinking", "thinking"],
63274
+ ["bashTimeout", "bash-timeout"],
63208
63275
  ["verbose", "verbose"],
63209
63276
  ["permissions", "permissions"]
63210
63277
  ].filter(([, flag]) => args.flags[flag] !== void 0).map(([key]) => [key, "flag"]))
@@ -63285,6 +63352,7 @@ user file ${stored.userPath}${existsSync16(stored.userPath) ? "" : " (absent
63285
63352
  hooks,
63286
63353
  skills,
63287
63354
  policy: policyForAgent({ root: cwd, tools: activeToolNames, readOnly: false }),
63355
+ timeoutMs: bashTimeout * 1e3,
63288
63356
  callMcp: (server, method, values, signal, observe) => mcp.invoke(server, method, values, { scopeId: "root", signal, observe })
63289
63357
  });
63290
63358
  try {
@@ -63377,6 +63445,7 @@ user file ${stored.userPath}${existsSync16(stored.userPath) ? "" : " (absent
63377
63445
  hooks,
63378
63446
  skills,
63379
63447
  policy: policyForAgent({ root: cwd, tools: activeToolNames, readOnly: false }),
63448
+ timeoutMs: bashTimeout * 1e3,
63380
63449
  callMcp: (server, method, values, signal, observe) => mcp.invoke(server, method, values, { scopeId: "root", signal, observe }),
63381
63450
  onHook: (event, label3, ok2) => emit({ type: "hook", event, label: label3, ok: ok2 }),
63382
63451
  runAgent: async (name, prompt) => {
@@ -63399,6 +63468,7 @@ user file ${stored.userPath}${existsSync16(stored.userPath) ? "" : " (absent
63399
63468
  const childExecutor = new ToolExecutor({
63400
63469
  cwd,
63401
63470
  skills,
63471
+ timeoutMs: bashTimeout * 1e3,
63402
63472
  callMcp: (server, method, values, signal, observe) => mcp.invoke(server, method, values, { scopeId: childScope.scopeId, signal, observe }),
63403
63473
  policy: policyForAgent({
63404
63474
  root: cwd,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "motifcode",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "description": "Unofficial coding agent harness built for Motif-3: a Claude Code-style terminal session over the hosted endpoint",
6
6
  "keywords": [