@lzn1998/dsh-langgraph 1.5.0 → 1.6.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.
package/bench/run.mjs ADDED
@@ -0,0 +1,139 @@
1
+ /**
2
+ * A small, reproducible DSH benchmark.
3
+ *
4
+ * DSH_ROOT=/path/to/deepseek-harness node bench/run.mjs
5
+ * DSH_ROOT=... node bench/run.mjs fanout official
6
+ *
7
+ * `official` uses the stock `headless` profile (no plugin). `plain` uses the
8
+ * `lgh` profile with the plugin linked but asks for ordinary tools. `default`
9
+ * leaves the tool choice to the plugin prompt. `coarse`/`fine` force the graph
10
+ * conditions from the matrix. One run per cell; see docs/BENCHMARK.md.
11
+ */
12
+ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
13
+ import { spawn } from 'node:child_process'
14
+ import { resolve, join } from 'node:path'
15
+
16
+ const DSH = process.env.DSH_ROOT ?? '/Users/zhinan/deepseek-harness'
17
+ const WORK = process.env.BENCH_WORK ?? resolve(DSH, '.lg-bench/work')
18
+ const CLI = ['--import', 'tsx/esm', 'apps/cli/src/bin.ts']
19
+
20
+ const TASKS = {
21
+ fanout: {
22
+ files: Object.fromEntries(Array.from({ length: 8 }, (_, i) => [
23
+ `item-0${i + 1}.txt`,
24
+ `Score: ${[3, 5, 2, 7, 1, 8, 4, 6][i]}\n`,
25
+ ])),
26
+ expected: '36',
27
+ output: 'result.txt',
28
+ prompt: (dir, out, suffix) => `In ${dir} there are files named item-01.txt through item-08.txt. Read every file, sum the numeric values after \`Score:\`, and write only the sum to:\n${out}\n\n${suffix}`,
29
+ },
30
+ report: {
31
+ files: {
32
+ 'alpha.txt': 'Project: Alpha\nOwner: Alice\nServices: 3\n',
33
+ 'beta.txt': 'Project: Beta\nOwner: Chen\nServices: 5\n',
34
+ 'gamma.txt': 'Project: Gamma\nOwner: Dana\nServices: 4\n',
35
+ },
36
+ expected: 'Beta: Chen',
37
+ output: 'result.md',
38
+ prompt: (dir, out, suffix) => `Read these three project files:\n- ${join(dir, 'alpha.txt')}\n- ${join(dir, 'beta.txt')}\n- ${join(dir, 'gamma.txt')}\n\nWhich project has the most services? Write exactly \`ProjectName: OwnerName\` (omit the word "Project" from the project name) to:\n${out}\n\n${suffix}`,
39
+ },
40
+ }
41
+
42
+ const CONDITIONS = {
43
+ official: {
44
+ profile: 'headless',
45
+ suffix: 'Use ordinary DSH tools.',
46
+ },
47
+ plain: {
48
+ profile: 'lgh',
49
+ suffix: 'Use ordinary DSH tools. Do not use `langgraph_compose` or `langgraph_run` for this task.',
50
+ },
51
+ default: {
52
+ profile: 'lgh',
53
+ suffix: '',
54
+ },
55
+ coarse: {
56
+ profile: 'lgh',
57
+ suffix: 'Use `langgraph_compose` to define a coarse LangGraph graph, then run it with `langgraph_run`. Keep the graph at the seams: put the open-ended work inside `agent` nodes; do not create one node per file or per tool call.',
58
+ },
59
+ fine: {
60
+ profile: 'lgh',
61
+ suffix: 'Use `langgraph_compose` to define a fine-grained LangGraph graph, then run it with `langgraph_run`. Model each concrete step as explicit `tool` or `model` nodes with explicit edges. Use `map` for the per-file work if it fits.',
62
+ },
63
+ }
64
+
65
+ function parseJsonl(text) {
66
+ const events = []
67
+ for (const line of text.split('\n')) {
68
+ if (!line.trim()) continue
69
+ try { events.push(JSON.parse(line)) } catch { /* ignore non-JSON */ }
70
+ }
71
+ return events
72
+ }
73
+
74
+ function summarize(text) {
75
+ const events = parseJsonl(text)
76
+ let steps = 0
77
+ let total = 0
78
+ let input = 0
79
+ let output = 0
80
+ const tools = {}
81
+ let final
82
+ for (const event of events) {
83
+ if (event.type === 'status' && event.phase === 'step_end' && event.usage) {
84
+ steps += 1
85
+ input += event.usage.inputTokens ?? 0
86
+ output += event.usage.outputTokens ?? 0
87
+ total += event.usage.totalTokens ?? 0
88
+ }
89
+ if (event.type === 'tool_call' && typeof event.tool === 'string') {
90
+ tools[event.tool] = (tools[event.tool] ?? 0) + 1
91
+ }
92
+ if (event.type === 'final') final = event
93
+ }
94
+ return { steps, tokens: { input, output, total }, tools, final: final?.reason ?? null }
95
+ }
96
+
97
+ async function runCell(taskName, conditionName) {
98
+ const task = TASKS[taskName]
99
+ const condition = CONDITIONS[conditionName]
100
+ if (!task || !condition) throw new Error(`unknown task/condition ${taskName}/${conditionName}`)
101
+ const root = resolve(WORK, `${taskName}-${conditionName}`)
102
+ const output = join(root, task.output)
103
+ await rm(root, { recursive: true, force: true })
104
+ await mkdir(root, { recursive: true })
105
+ for (const [name, content] of Object.entries(task.files)) {
106
+ await writeFile(join(root, name), content, 'utf8')
107
+ }
108
+ const suffix = condition.suffix
109
+ const prompt = task.prompt(root, output, suffix).trim()
110
+ const started = Date.now()
111
+ const child = spawn(process.execPath, [...CLI, '--profile', condition.profile, 'headless', '--json', '--permission-mode', 'workspace-write', '--approval-policy', 'never', prompt], {
112
+ cwd: DSH,
113
+ env: { ...process.env },
114
+ })
115
+ let stdout = ''
116
+ let stderr = ''
117
+ child.stdout.on('data', chunk => { stdout += chunk })
118
+ child.stderr.on('data', chunk => { stderr += chunk })
119
+ const code = await new Promise(resolve => child.on('close', resolve))
120
+ const seconds = Math.round((Date.now() - started) / 1000)
121
+ const summary = summarize(stdout)
122
+ let result
123
+ try { result = (await readFile(output, 'utf8')).trim() } catch { result = undefined }
124
+ const success = result !== undefined && result.includes(task.expected)
125
+ return { task: taskName, condition: conditionName, profile: condition.profile, success, seconds, steps: summary.steps, tokens: summary.tokens, tools: summary.tools, result: result?.slice(0, 120) ?? null, exit: code, stderr: stderr.slice(-300) }
126
+ }
127
+
128
+ const [taskArg, conditionArg] = process.argv.slice(2)
129
+ const pairs = taskArg && conditionArg
130
+ ? [[taskArg, conditionArg]]
131
+ : Object.keys(TASKS).flatMap(task => ['official', 'plain', 'default'].map(condition => [task, condition]))
132
+ const results = []
133
+ for (const [task, condition] of pairs) {
134
+ process.stderr.write(`\n== ${task} / ${condition} ==\n`)
135
+ const result = await runCell(task, condition)
136
+ results.push(result)
137
+ process.stderr.write(JSON.stringify(result) + '\n')
138
+ }
139
+ console.log(JSON.stringify(results, null, 2))
@@ -0,0 +1,116 @@
1
+ # A small DSH benchmark: ordinary loop vs dsh-langgraph graphs
2
+
3
+ **Date:** 2026-11-27 · **Plugin:** dsh-langgraph 1.5.0 (runtime unchanged in 1.6.0) ·
4
+ **DSH:** local `deepseek-harness` checkout · **Model:** DeepSeek-V4.1-Flash · **Host:** one
5
+ macOS machine.
6
+
7
+ This is a directional comparison, not a leaderboard. The question: when does an explicit
8
+ graph buy enough at a seam to justify its cost over the stock DSH ordinary loop?
9
+
10
+ ## Setup
11
+
12
+ The stock **official** arm uses the `headless` profile with no plugin. The **plugin**
13
+ arms use the `lgh` profile with this package linked. Both use the same model, workspace
14
+ policy, and task text. The headless tasks have no human gate, so they can run unattended.
15
+
16
+ Two deterministic tasks:
17
+
18
+ | Task | Input | Output check |
19
+ |---|---|---|
20
+ | `fanout` | 8 files, each `Score: N` | exactly `36` |
21
+ | `report` | 3 project files with a service count | exactly `Beta: Chen` |
22
+
23
+ The task text for `plain` explicitly asks for ordinary DSH tools and forbids the graph
24
+ tools. `default` leaves the choice to the plugin's prompt. `coarse` and `fine` force a
25
+ graph in the task text. One run per cell. The harness is in
26
+ [`bench/run.mjs`](../bench/run.mjs); the commands are in [`bench/README.md`](../bench/README.md).
27
+
28
+ `tokens` is the sum of DSH `step_end` usage (input + output + cache reads). It is
29
+ comparable between cells but is not a bill. Graph-internal model calls may not all appear
30
+ as `step_end`; treat the graph arms as a lower bound.
31
+
32
+ ## Headless results
33
+
34
+ `time / reported tokens / steps`; every cell produced the expected file.
35
+
36
+ | Task | Official DSH (headless) | Plugin `plain` | Plugin `default` | Forced coarse | Forced fine |
37
+ |---|---:|---:|---:|---:|---:|
38
+ | `fanout` (8 files, sum) | 16s / 72.5K / 5 | 9s / 65.2K / 4 | 9s / 64.0K / 4 | 45s / 264.9K / 12 | 60s / 393.4K / 15 |
39
+ | `report` (3 files, pick) | 7s / 41.1K / 3 | 9s / 63.3K / 4 | 10s / 79.7K / 5 | 30s / 124.2K / 6 | 39s / 229.0K / 10 |
40
+
41
+ The six ordinary-loop cells come from one `bench/run.mjs` run
42
+ ([results](../bench/results-harness.json)); the forced-graph columns are the earlier
43
+ 1.5.0 matrix, because that run deliberately forced coarse/fine graphs. A second official
44
+ run measured 12s / 73.9K / 5 and 7s / 41.2K / 3; a second plain run measured 7s / 81.1K
45
+ and 9s / 46.9K. The variance at this size is large.
46
+
47
+ At n=1 there is **no consistent winner** between the official loop and the plugin's
48
+ ordinary loop: they sit in the same roughly 40–80K reported-token band, and the faster
49
+ arm changes between tasks. The plugin's system prompt and three tools do not impose a
50
+ reliable tax; the extra tokens are within run-to-run noise. The default plugin prompt did
51
+ **not** compose on either small task — it stayed with the ordinary loop. That is the
52
+ intended conservative behavior.
53
+
54
+ The forced graphs cost roughly **3–5×** the ordinary loop in tokens. The coarse graph is
55
+ agent-heavy; the fine graph has more tool/model rows. Both forced arms are the earlier
56
+ 1.5.0 runs; they first hit a strict `bool` / `No files found` failure and recovered
57
+ through fork or re-compose, so their time/token totals include that recovery. The current
58
+ guide/law, `file_path` correction and compose-time tool-argument validation remove two of
59
+ those first-failure classes; the numbers are still a useful upper bound on the cost of
60
+ forcing a graph at a small seam.
61
+
62
+ ## The 12-topic Web case
63
+
64
+ The larger case is Web-only because it has a human gate; a headless profile has no
65
+ answerer. It is the same 12-topic brief used by the gallery and the demo video:
66
+
67
+ | Approach | Wall | Steps | Tokens | Result |
68
+ |---|---:|---:|---:|---|
69
+ | Ordinary loop + native question | 53s | 7 | 124K | correct 12-row brief |
70
+ | Coarse agent graph + gate | 1m38s | 17 | 462K | correct after a manual heading fix |
71
+ | Fine map graph + gate | 55s | 9 | 226K | correct 12 rows; gate/resume |
72
+
73
+ The fine graph is two 12-item maps (read + extract), an outline model, a human gate, a
74
+ compose model and a write. It is slower than the ordinary loop by ~2s, uses ~1.8× the
75
+ reported tokens, and is about **half** the coarse agent graph. The coarse graph's write
76
+ node dropped headings and needed a manual fix; the fine graph wrote the approved outline
77
+ faithfully.
78
+
79
+ ## What this says
80
+
81
+ - **Do not reach for a graph on a one- or two-step task.** The ordinary loop — official
82
+ or plugin — is the baseline; at these sizes the two are within noise.
83
+ - **A graph is a seam tax.** At 8 files and 3 files the tax does not pay. The 12-topic
84
+ map/extract case is the first shape where the explicit fan-out, the human signature and
85
+ the durable thread start to earn their overhead; even there, the loop is cheaper in
86
+ tokens.
87
+ - **Fine is not automatically worse than coarse.** For a genuine per-item fan-out, a
88
+ map of the same role (fine) was about half the cost of four open-ended agent phases
89
+ (coarse), and produced a more faithful file. The earlier "fine is harmful" result was
90
+ the manual and validation, not the shape.
91
+ - **The only robust signal is the graph tax.** Small ordinary-loop differences move with
92
+ the run; the 3–5× graph cost and the 12-topic fine/coarse gap do not.
93
+ - **The honest default is the product.** The plugin's system prompt says "compose only
94
+ at seams"; the benchmark shows why.
95
+
96
+ ## Threats to validity
97
+
98
+ One run per cell, one model, one host, two small headless tasks plus one Web case. The
99
+ official and plugin profiles differ in system prompt/tool surface by construction; that is
100
+ the intervention. Small-task variance is large: a repeated ordinary-loop cell moved by
101
+ 15–50K tokens and several seconds. The forced graph arms include recovery from the earlier
102
+ 1.5.0 manual. Token accounting includes cache reads and may undercount graph-internal
103
+ model calls. The 12-topic case is a single Web session. Treat the table as a decision aid,
104
+ not a statistical claim.
105
+
106
+ ## Reproduce
107
+
108
+ ```sh
109
+ DSH_ROOT=/Users/zhinan/deepseek-harness node bench/run.mjs
110
+ DSH_ROOT=/Users/zhinan/deepseek-harness node bench/run.mjs fanout official
111
+ DSH_ROOT=/Users/zhinan/deepseek-harness node bench/run.mjs fanout coarse
112
+ ```
113
+
114
+ The 12-topic case is the `research-brief` gallery graph, run in the `lgtest` Web profile;
115
+ [the demo video](../media/dsh-langgraph-demo.webm) is a recording of that completed
116
+ session. The gallery page is [`gallery/index.html`](../gallery/index.html).
@@ -0,0 +1,69 @@
1
+ # The dsh-langgraph gallery
2
+
3
+ Six small graphs that each earn their keep at a real seam. Open
4
+ [`index.html`](index.html) for the visual gallery — it is a static page, no server or build
5
+ step. Every graph is a plain JSON spec, so it can be read, copied, reviewed, and shared
6
+ without shipping JavaScript.
7
+
8
+ | Graph | The seam | Gate | Tools | Spec |
9
+ |---|---|---|:---:|---|
10
+ | **Research brief** | fan out over files, extract one row per file, approve an outline, write | yes | `glob`, `read`, `write` | [`specs/research-brief.lg.json`](specs/research-brief.lg.json) |
11
+ | **Repo scan** | read a set of source files in parallel and synthesize one review | no | `glob`, `read`, `write` | [`specs/repo-scan.lg.json`](specs/repo-scan.lg.json) |
12
+ | **PR review** | read the current diff, draft a review, ask before posting | yes | `bash`, `write` | [`specs/pr-review.lg.json`](specs/pr-review.lg.json) |
13
+ | **Incident triage** | read many logs, classify, ask whether to page | yes | `glob`, `read`, `write` | [`specs/incident-triage.lg.json`](specs/incident-triage.lg.json) |
14
+ | **Data extract** | one JSON record per file, then one JSON array | no | `glob`, `read`, `write` | [`specs/data-extract.lg.json`](specs/data-extract.lg.json) |
15
+ | **Approval plan** | plan with tools, human approval, execute, write | yes | `glob`, `read`, `write`, `bash` | [`specs/approval-plan.lg.json`](specs/approval-plan.lg.json) |
16
+
17
+ The graphs are deliberately coarse: a map for real fan-out, an agent for open work, a gate
18
+ for a decision that should not be re-decided by a model. They are not a node catalogue.
19
+
20
+ ## Run one
21
+
22
+ The spec is loaded from the session workspace. Validate first; `compose` stores an
23
+ immutable, session-owned graph; `run` starts a durable thread.
24
+
25
+ ```js
26
+ langgraph_compose({ action: "validate", path: "gallery/specs/research-brief.lg.json" })
27
+ langgraph_compose({ action: "compose", path: "gallery/specs/research-brief.lg.json" })
28
+ langgraph_run({ graph: "lg_adhoc_…", input: { pattern: "topics/*.txt" } })
29
+ ```
30
+
31
+ A gate returns `status: "interrupted"`. Read the outline from the thread state, ask the
32
+ human, then resume the same thread with the raw answer:
33
+
34
+ ```js
35
+ langgraph_run({ graph: "lg_adhoc_…", thread: "lg_lg_adhoc_…", resume: "Approve" })
36
+ ```
37
+
38
+ The tools must be visible in the calling session. The gallery uses only `glob`, `read`,
39
+ `write`, and `bash`; the agent nodes list their own tools. A spec can be copied into any
40
+ workspace and edited as JSON.
41
+
42
+ ## Why these six
43
+
44
+ They cover the shapes that actually change the work:
45
+
46
+ - **fan-out** over a list you do not know until runtime (research, repo scan, logs, data);
47
+ - **aggregation** after the fan-out;
48
+ - **human signatures** before an irreversible action (write, page, post);
49
+ - **open work inside a role** rather than one node per tool call (approval plan);
50
+ - **durable state** when the run must survive a turn or a Host restart.
51
+
52
+ ## When not to use one
53
+
54
+ Do not start with a graph for a one- or two-step task, a single tool call, or a task a
55
+ registered graph already matches. The ordinary DSH loop is still the default. The plugin's
56
+ own comparison on a 12-topic brief is intentionally honest: the ordinary loop was
57
+ 53s / 124K tokens; a forced coarse graph was 1m38s / 462K; the fine map graph was
58
+ 55s / 226K. A graph is a seam tax, not a token discount.
59
+
60
+ ## Make one
61
+
62
+ 1. Copy a spec from [`specs/`](specs/).
63
+ 2. Call `langgraph_compose({ action: "validate", spec })` and fix the IR paths it reports.
64
+ 3. `compose` it, run it on a small input, and pin the prompt and expected output next to it.
65
+ 4. Open a PR with the `.lg.json`, a short README row, and the validated output. No arbitrary
66
+ JS is needed; a graph spec is reviewable data.
67
+
68
+ `npm test` includes `test/gallery.test.js`, which validates every spec in this folder
69
+ against the tools it should be able to see.
Binary file
Binary file