@llm4ts/shell 2.54.0 → 3.0.0

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.
@@ -228,13 +228,7 @@ export const shellWords = (command) => {
228
228
  * or a comment.
229
229
  */
230
230
  export const declaredCommands = (packageJson, commandsMarkdown) => {
231
- const scripts = new Set();
232
- if (packageJson !== undefined) {
233
- const parsed = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Struct({ scripts: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)) })))(packageJson);
234
- if (parsed._tag === "Some")
235
- for (const name of Object.keys(parsed.value.scripts ?? {}))
236
- scripts.add(name);
237
- }
231
+ const scripts = new Set(Object.keys(scriptsIn(packageJson)));
238
232
  const lines = new Set((commandsMarkdown ?? "")
239
233
  .split(/\r?\n/)
240
234
  .map((line) => line
@@ -243,25 +237,65 @@ export const declaredCommands = (packageJson, commandsMarkdown) => {
243
237
  .replace(/^`(.*)`$/, "$1"))
244
238
  .filter((line) => line.length > 0 && !line.startsWith("#"))
245
239
  .map((line) => shellWords(line).join("\u0000")));
240
+ // Exactly the script invocation, nothing appended: extra arguments reach the script.
246
241
  return (argv) => {
247
242
  if (argv.length === 0)
248
243
  return false;
249
244
  if (lines.has(argv.join("\u0000")))
250
245
  return true;
251
- const [runner, second, third] = argv;
252
- if (runner === "npm") {
253
- return second === "test"
254
- ? scripts.has("test")
255
- : second === "run" && third !== undefined && scripts.has(third);
256
- }
257
- if (runner === "pnpm" || runner === "yarn") {
258
- return second === "run"
259
- ? third !== undefined && scripts.has(third)
260
- : second !== undefined && scripts.has(second);
261
- }
262
- return false;
246
+ return scriptOf(argv, scripts) !== undefined;
263
247
  };
264
248
  };
249
+ /** The package script an argv runs through npm, pnpm or yarn, when it is exactly that and nothing more. */
250
+ const scriptOf = (argv, scripts) => {
251
+ const [runner, second, third] = argv;
252
+ if (runner === "npm") {
253
+ if (argv.length === 2 && second === "test" && scripts.has("test"))
254
+ return "test";
255
+ return argv.length === 3 && second === "run" && third !== undefined && scripts.has(third)
256
+ ? third
257
+ : undefined;
258
+ }
259
+ if (runner === "pnpm" || runner === "yarn") {
260
+ if (argv.length === 2 && second !== undefined && second !== "run" && scripts.has(second)) {
261
+ return second;
262
+ }
263
+ return argv.length === 3 && second === "run" && third !== undefined && scripts.has(third)
264
+ ? third
265
+ : undefined;
266
+ }
267
+ return undefined;
268
+ };
269
+ const scriptsIn = (packageJson) => {
270
+ if (packageJson === undefined)
271
+ return {};
272
+ const parsed = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Struct({ scripts: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)) })))(packageJson);
273
+ return parsed._tag === "Some" ? { ...(parsed.value.scripts ?? {}) } : {};
274
+ };
275
+ /**
276
+ * Why a package script may not run where it is about to: the worktree's
277
+ * package.json names a different body for it than the repository's, or
278
+ * none, or a `pre`/`post` hook the repository does not have. A command
279
+ * that is not a script invocation is not checked here.
280
+ */
281
+ export const scriptChanged = (trusted, local, argv) => {
282
+ const known = scriptsIn(trusted);
283
+ const script = scriptOf(argv, new Set(Object.keys(known)));
284
+ if (script === undefined)
285
+ return undefined;
286
+ const here = scriptsIn(local);
287
+ if (here[script] === undefined) {
288
+ return `script ${script} is missing from the worktree's package.json`;
289
+ }
290
+ for (const name of [script, `pre${script}`, `post${script}`]) {
291
+ if (here[name] === known[name])
292
+ continue;
293
+ return known[name] === undefined
294
+ ? `the worktree adds a ${name} script`
295
+ : `script ${name} was changed in the worktree`;
296
+ }
297
+ return undefined;
298
+ };
265
299
  // ---- Prompts ------------------------------------------------------------------
266
300
  const personaBrief = (persona) => [
267
301
  `You are the ${persona.name} of a taskforce.`,
@@ -602,7 +636,8 @@ export const runTaskforce = Effect.fn("taskforce.run")(function* (deps) {
602
636
  answers: [],
603
637
  feedback: "",
604
638
  drafts: 0,
605
- folded: []
639
+ folded: [],
640
+ approved: []
606
641
  });
607
642
  yield* save(plan);
608
643
  yield* say(`taskforce: ${paths.plan} opened — ${plan.cast.join(", ")}`);
@@ -669,13 +704,28 @@ export const runTaskforce = Effect.fn("taskforce.run")(function* (deps) {
669
704
  body: proposal.body
670
705
  });
671
706
  /** Runs the commands a brief names; the log, and a reason it could not. */
707
+ /**
708
+ * Runs the commands a brief names; the log, and a reason it could not. A
709
+ * brief the owner had in the file before the run opened it runs its
710
+ * commands as written; any other (added by a check, a review or an
711
+ * answer, or every brief under --no-pause) runs only what the project
712
+ * declares (decision 5). In a worktree, a declared script whose body the
713
+ * worktree changed is not the declared one and is refused.
714
+ */
672
715
  const runCommands = (brief, cwd) => Effect.gen(function* () {
673
716
  const lines = [];
717
+ const approved = plan.approved.includes(brief.id);
718
+ const trusted = yield* files.read(join(deps.workDir, "package.json"));
719
+ const local = cwd === deps.workDir ? trusted : yield* files.read(join(cwd, "package.json"));
674
720
  for (const command of verifyCommands(brief)) {
675
721
  const argv = shellWords(command);
676
- if (deps.noPause && !deps.allowedCommand(argv)) {
722
+ if (!approved && !deps.allowedCommand(argv)) {
677
723
  return { log: lines.join("\n"), refused: command };
678
724
  }
725
+ const changed = scriptChanged(trusted, local, argv);
726
+ if (changed !== undefined) {
727
+ return { log: lines.join("\n"), refused: `${command} (${changed})` };
728
+ }
679
729
  const result = yield* deps.processes.run(argv, cwd, {}).pipe(Effect.map((done) => ({
680
730
  exitCode: done.exitCode,
681
731
  stdout: done.stdout,
@@ -742,7 +792,7 @@ export const runTaskforce = Effect.fn("taskforce.run")(function* (deps) {
742
792
  if (ran.log.length > 0)
743
793
  yield* files.writeAtomic(paths.commandLog(brief.id), ran.log);
744
794
  if (ran.refused !== undefined) {
745
- return blocked(brief, attempt, `command not declared, not run under --no-pause: ${ran.refused}`, `Declare it in package.json scripts or ${commandsPath(deps.workDir)}, or run without --no-pause.\n\n${ran.log}`);
795
+ return blocked(brief, attempt, `command not declared and not approved by the owner, not run: ${ran.refused}`, `Declare it in package.json scripts or ${commandsPath(deps.workDir)}, or put the brief in plan.md before the run opens it.\n\n${ran.log}`);
746
796
  }
747
797
  commandOutput = ran.log;
748
798
  }
@@ -848,7 +898,10 @@ export const runTaskforce = Effect.fn("taskforce.run")(function* (deps) {
848
898
  if (violations.length > 0) {
849
899
  return yield* PlanInvalid.make({ path: paths.plan, violations });
850
900
  }
851
- plan = Plan.make({ ...plan, status: "working" });
901
+ // The owner had the file before this run and asked for the pause: what it
902
+ // holds is approved as written. Under --no-pause nothing is, by decision 5.
903
+ const approved = loaded === undefined || deps.noPause ? [] : plan.briefs.map((brief) => brief.id);
904
+ plan = Plan.make({ ...plan, status: "working", approved });
852
905
  yield* save(plan);
853
906
  yield* trail("plan", "plan opened", `${plan.briefs.length} briefs, ${plan.cast.length} in the cast`, "plan.md", "working");
854
907
  yield* say(`taskforce: working ${plan.briefs.length} brief(s), up to ${deps.parallel} at once, ${caps.waves} wave(s) at most`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llm4ts/shell",
3
- "version": "2.54.0",
3
+ "version": "3.0.0",
4
4
  "description": "Interactive shell and CLI for llm4ts: flow discovery, run-a-flow, and view",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -61,9 +61,9 @@
61
61
  "dependencies": {
62
62
  "@effect/platform-node": "4.0.0",
63
63
  "@effect/platform-node-shared": "4.0.0",
64
- "@llm4ts/core": "2.54.0",
65
- "@llm4ts/flow": "2.54.0",
66
- "@llm4ts/runner": "2.54.0"
64
+ "@llm4ts/core": "3.0.0",
65
+ "@llm4ts/runner": "3.0.0",
66
+ "@llm4ts/flow": "3.0.0"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "effect": "4.0.0"