dsh-seal-scripts 0.4.8 → 0.4.10

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.
package/README.en.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  > Seal the scripts that worked. Keep what compaction threw away.
6
6
 
7
- [![version](https://img.shields.io/badge/version-0.4.7-4a7cff)](https://github.com/gosutry/dsh-seal-scripts)
7
+ [![version](https://img.shields.io/npm/v/dsh-seal-scripts?color=4a7cff)](https://www.npmjs.com/package/dsh-seal-scripts)
8
8
  [![license](https://img.shields.io/badge/license-MIT-green)](./LICENSE)
9
9
  [![node](https://img.shields.io/badge/node-%3E%3D18-339933)](https://nodejs.org)
10
10
  [![dsh](https://img.shields.io/badge/dsh-%3E%3D0.2.1--rc.2%20%3C1.0.0-6f42c1)](https://github.com/deepseek-ai/dsh)
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  > 把跑通的脚本封存成工具,把被压缩的对话留成回顾。
6
6
 
7
- [![version](https://img.shields.io/badge/version-0.4.7-4a7cff)](https://github.com/gosutry/dsh-seal-scripts)
7
+ [![version](https://img.shields.io/npm/v/dsh-seal-scripts?color=4a7cff)](https://www.npmjs.com/package/dsh-seal-scripts)
8
8
  [![license](https://img.shields.io/badge/license-MIT-green)](./LICENSE)
9
9
  [![node](https://img.shields.io/badge/node-%3E%3D18-339933)](https://nodejs.org)
10
10
  [![dsh](https://img.shields.io/badge/dsh-%3E%3D0.2.1--rc.2%20%3C1.0.0-6f42c1)](https://github.com/deepseek-ai/dsh)
@@ -11,15 +11,16 @@
11
11
  */
12
12
 
13
13
  import fsp from 'node:fs/promises'
14
+ import fsSync from 'node:fs'
14
15
  import path from 'node:path'
15
16
  import { resolveMemoryFile, resolveMemoryByDay, estimateTokens } from './tool.js'
16
17
  import { parseMemory } from './memory-file.js'
17
18
  import { MARK } from './render.js'
18
19
  import {
19
20
  installToolScript, listToolScripts, loadToolScript, catalogText, assertToolScriptId, resolveInside,
20
- toolSequenceRoot, TOOL_SCRIPT_FILE
21
+ toolSequenceRoot, TOOL_SCRIPT_FILE, sha256OfFile
21
22
  } from './schema.js'
22
- import { runScript, normalizeTimeout, normalizeMaxOutput } from './schema-runner.js'
23
+ import { runScript, normalizeTimeout, normalizeMaxOutput, interpreterOf } from './schema-runner.js'
23
24
 
24
25
  export const TOOL_CREATE_READ_NAME = 'session_tool_create_read'
25
26
  export const TOOL_CREATE_WRITE_NAME = 'session_tool_create_write'
@@ -574,6 +575,51 @@ function missingToolScriptMessage(id, detail) {
574
575
  ].join('\n')
575
576
  }
576
577
 
578
+ /**
579
+ * 0.4.10:开跑前的契约,一次校验完。
580
+ *
581
+ * 0.4.6–0.4.9 这些错只会变成正文里的一行「✗ … 没跑起来」,宿主仍判定这次调用成功。
582
+ * 现在一律抛错:调用失败(红),而且整批一条都不跑——不会出现「前两条跑了、第三条才说不合格」。
583
+ * 唯一的例外是「脚本真的跑了,但 exit≠0 / 超时」:那是结果,不是契约错,仍按结果回报(带 stdout/stderr)。
584
+ * @param {{req:object, id:string, manifest:object, dir:string}} item
585
+ * @param {{cwd:string, args:object, allowDrift:boolean}} ctx
586
+ * @returns {{step:string, script:object, file:string, runCwd:string, timeoutMs:number, argvArgs:string[]}}
587
+ */
588
+ function planOne({ req, manifest, dir }, { cwd, args, allowDrift }) {
589
+ const scripts = Array.isArray(manifest.scripts) ? manifest.scripts : []
590
+ const step = req.step === undefined ? manifest.entry : String(req.step)
591
+ const script = scripts.find((s) => s && s.name === step)
592
+ if (!script) {
593
+ throw new Error('这个工具脚本里没有这个脚本:' + step + '(有:' + scripts.map((s) => s.name).join('、') + ')')
594
+ }
595
+ const file = resolveInside(dir, step)
596
+ if (!fsSync.existsSync(file)) {
597
+ throw new Error('清单里有这个脚本,磁盘上却没这个文件:' + step + '(重装这个工具脚本,别手删)')
598
+ }
599
+ const hashes = manifest.hashes && typeof manifest.hashes === 'object' ? manifest.hashes : {}
600
+ const expected = typeof hashes[step] === 'string' ? hashes[step] : ''
601
+ if (expected !== '' && !allowDrift && sha256OfFile(file) !== expected) {
602
+ throw new Error('脚本和清单里的 sha256 对不上,先不跑(' + step + ')。' +
603
+ '如果你刚手动改过它,重装这个工具脚本(' + TOOL_CREATE_WRITE_NAME + ' + replace),或者这次显式传 allowDrift: true。')
604
+ }
605
+ // 未知执行方式:run 只收 bash / node / ps1 / cmd;安装时已校验,这里是兜底。
606
+ interpreterOf(script.run)
607
+ const runCwd = resolveRunCwd(req.cwd === undefined ? args.cwd : req.cwd, { cwd, dir })
608
+ const timeoutMs = normalizeTimeout(req.timeoutMs === undefined
609
+ ? (args.timeoutMs === undefined ? manifest.timeoutMs : args.timeoutMs)
610
+ : req.timeoutMs)
611
+ // 参数契约:只在声明了 params 时校验,且只拦「比声明的多」——少传是合法的(可选参数、脚本默认值)。
612
+ const declared = Array.isArray(script.params) && script.params.length > 0
613
+ ? script.params
614
+ : (Array.isArray(manifest.params) ? manifest.params : [])
615
+ const argvArgs = (req.args ?? []).map(String)
616
+ if (declared.length > 0 && argvArgs.length > declared.length) {
617
+ throw new Error('这次给了 ' + argvArgs.length + ' 个参数,但 ' + step + ' 只声明了 ' + declared.length +
618
+ ' 个(' + declared.join(' ') + ');多出来的会错位。要传就把它们写进 toolScript 的 params,别硬塞')
619
+ }
620
+ return { step, script, file, runCwd, timeoutMs, argvArgs }
621
+ }
622
+
577
623
  /**
578
624
  * 工具定义:直接跑工具组里的工具脚本(一条或连着几条)。
579
625
  * @param {{dshHome:string, logger?:object}} options
@@ -688,57 +734,48 @@ export function buildToolCallToolDefinition({ dshHome, logger }) {
688
734
  }
689
735
  }
690
736
 
737
+ // 0.4.10:每一行的契约先全部校验(脚本名 / 文件在不在 / 哈希 / cwd / 参数个数 / 执行方式)。
738
+ // 任何一条不合格就抛错 → 这次调用失败,整批一条都不跑。
739
+ const planned = resolved.map((item) => ({ ...item, ...planOne(item, { cwd, args, allowDrift }) }))
740
+
691
741
  const runs = []
692
742
  let skipped = 0
693
743
  let stopped = false
694
744
 
695
- for (let i = 0; i < resolved.length; i++) {
745
+ for (const p of planned) {
696
746
  if (stopped) { skipped++; continue }
697
- const { req, id, manifest, dir } = resolved[i]
698
- try {
699
- const step = req.step === undefined ? manifest.entry : String(req.step)
700
- const script = (manifest.scripts ?? []).find((s) => s.name === step)
701
- if (!script) throw new Error('这个工具脚本里没有这个脚本:' + step + '(有:' + (manifest.scripts ?? []).map((s) => s.name).join('、') + ')')
702
- const file = resolveInside(dir, step)
703
- const runCwd = resolveRunCwd(req.cwd === undefined ? args.cwd : req.cwd, { cwd, dir })
704
- const timeoutMs = normalizeTimeout(req.timeoutMs === undefined ? (args.timeoutMs === undefined ? manifest.timeoutMs : args.timeoutMs) : req.timeoutMs)
705
- // 参数契约:只在声明了 params 时校验,且只拦「比声明的多」——少传是合法的(可选参数、脚本默认值)。
706
- const declared = Array.isArray(script.params) && script.params.length > 0
707
- ? script.params
708
- : (Array.isArray(manifest.params) ? manifest.params : [])
709
- const argvArgs = req.args ?? []
710
- if (declared.length > 0 && argvArgs.length > declared.length) {
711
- throw new Error('这次给了 ' + argvArgs.length + ' 个参数,但 ' + step + ' 只声明了 ' + declared.length +
712
- ' 个(' + declared.join(' ') + ');多出来的会错位。要传就把它们写进 toolScript 的 params,别硬塞')
713
- }
714
-
715
- if (dryRun) {
716
- runs.push({
717
- toolScript: id, step, run: script.run,
718
- argv: [step, ...argvArgs.map(String)],
719
- cwd: runCwd, timeoutMs, status: 'dry'
720
- })
721
- continue
722
- }
723
-
724
- const res = await runScript({
725
- dshHome, toolScriptId: id, dir, manifest, step,
726
- args: argvArgs, stdin: req.stdin,
727
- cwd: runCwd, timeoutMs, maxOutputBytes: cap, allowDrift
728
- })
729
- const ok = res.exitCode === 0 && !res.timedOut
747
+ if (dryRun) {
730
748
  runs.push({
731
- toolScript: id, step, status: ok ? 'ok' : 'fail',
732
- exitCode: res.exitCode, signal: res.signal, timedOut: res.timedOut,
733
- durationMs: res.durationMs, truncated: res.truncated, spill: res.spill,
734
- drift: res.drift,
735
- stdout: res.stdout, stderr: res.stderr, argv: res.argv
749
+ toolScript: p.id, step: p.step, run: p.script.run,
750
+ argv: [p.step, ...p.argvArgs],
751
+ cwd: p.runCwd, timeoutMs: p.timeoutMs, status: 'dry'
752
+ })
753
+ continue
754
+ }
755
+ let res
756
+ try {
757
+ res = await runScript({
758
+ dshHome, toolScriptId: p.id, dir: p.dir, manifest: p.manifest, step: p.step,
759
+ args: p.argvArgs, stdin: p.req.stdin,
760
+ cwd: p.runCwd, timeoutMs: p.timeoutMs, maxOutputBytes: cap, allowDrift
736
761
  })
737
- if (!ok && stopOnError) stopped = true
738
762
  } catch (err) {
739
- runs.push({ toolScript: String(req.toolScript), step: String(req.step ?? ''), status: 'error', error: String(err?.message ?? err) })
740
- if (stopOnError) stopped = true
763
+ // 只有真跑才会暴露的错(解释器不在、spawn 失败):补上现场后照样抛,不吞。
764
+ const done = runs.length > 0
765
+ ? '\n\n这次调用里先跑完的 ' + runs.length + ' 条(输出没丢):\n' + renderRuns({ runs, dryRun: false, skipped: 0 })
766
+ : ''
767
+ throw new Error('这条没跑起来,整次调用算失败:' + p.id + ' / ' + p.step + '\n' +
768
+ String(err?.message ?? err) + done, { cause: err })
741
769
  }
770
+ const ok = res.exitCode === 0 && !res.timedOut
771
+ runs.push({
772
+ toolScript: p.id, step: p.step, status: ok ? 'ok' : 'fail',
773
+ exitCode: res.exitCode, signal: res.signal, timedOut: res.timedOut,
774
+ durationMs: res.durationMs, truncated: res.truncated, spill: res.spill,
775
+ drift: res.drift,
776
+ stdout: res.stdout, stderr: res.stderr, argv: res.argv
777
+ })
778
+ if (!ok && stopOnError) stopped = true
742
779
  }
743
780
 
744
781
  const failed = runs.filter((r) => r.status === 'fail' || r.status === 'error').length
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-seal-scripts",
3
- "version": "0.4.8",
3
+ "version": "0.4.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -9,7 +9,7 @@
9
9
  "./client": "./lib/client.js",
10
10
  "./package.json": "./package.json"
11
11
  },
12
- "description": "DSH代码脚本固存,留住模型成功自实现的工作流。",
12
+ "description": "Seal proven command chains into reusable executable tool scripts for DSH agents. DSH 代码脚本固存,留住模型成功自实现的工作流。",
13
13
  "license": "MIT",
14
14
  "files": [
15
15
  "index.js",