@lzn1998/dsh-langgraph 1.0.1 → 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.
Files changed (44) hide show
  1. package/README.md +172 -51
  2. package/bench/README.md +54 -0
  3. package/bench/demo-video.mjs +208 -0
  4. package/bench/results-harness.json +125 -0
  5. package/bench/run.mjs +139 -0
  6. package/compose/analysis.js +359 -0
  7. package/compose/build.js +274 -0
  8. package/compose/constants.js +83 -0
  9. package/compose/guard.js +72 -0
  10. package/compose/nodes.js +214 -0
  11. package/compose/resolve.js +97 -0
  12. package/compose/schema.js +975 -0
  13. package/compose/specs.js +218 -0
  14. package/compose/template.js +260 -0
  15. package/compose/tool.js +713 -0
  16. package/docs/BENCHMARK.md +116 -0
  17. package/docs/COMPOSE.md +376 -0
  18. package/docs/CONTEXT.md +34 -18
  19. package/docs/DETAIL.md +48 -1
  20. package/docs/ECOSYSTEM.md +30 -5
  21. package/gallery/README.md +69 -0
  22. package/gallery/assets/gallery.png +0 -0
  23. package/gallery/assets/session.png +0 -0
  24. package/gallery/index.html +516 -0
  25. package/gallery/specs/approval-plan.lg.json +51 -0
  26. package/gallery/specs/data-extract.lg.json +75 -0
  27. package/gallery/specs/incident-triage.lg.json +64 -0
  28. package/gallery/specs/pr-review.lg.json +49 -0
  29. package/gallery/specs/repo-scan.lg.json +54 -0
  30. package/gallery/specs/research-brief.lg.json +94 -0
  31. package/index.d.ts +136 -0
  32. package/index.js +423 -46
  33. package/media/dsh-langgraph-demo.webm +0 -0
  34. package/media/raw/page@0d0adc4d6eb7b946d722093988db7e67.webm +0 -0
  35. package/media/raw/page@0fceaacbda6d8f2da8485736c565ed87.webm +0 -0
  36. package/media/raw/page@49c6bb9135c0c35ed8cf9054a8e05711.webm +0 -0
  37. package/media/raw/page@86f5e37e1c1218697f8724a6bafda8d2.webm +0 -0
  38. package/media/raw/page@9b5b70b442c0ace7d4b135fc80edba73.webm +0 -0
  39. package/media/raw/page@beb9164240f13ad017c3fa7e8f20a772.webm +0 -0
  40. package/media/raw/page@db4ee1834adcc9e8d5b4708eabe4eb5c.webm +0 -0
  41. package/media/raw/page@e9c63a0029d3181e41506fc4bf8da05a.webm +0 -0
  42. package/media/raw/page@eaf729b06925fe86e9e9e4bf48e2cdcc.webm +0 -0
  43. package/media/raw/page@f0d6fd1a2534e69bc2f228b30ae19a9c.webm +0 -0
  44. package/package.json +7 -3
package/docs/DETAIL.md CHANGED
@@ -9,6 +9,10 @@ Run a LangGraph graph from inside a DSH agent session, and continue it later.
9
9
  - `langgraph_threads` — `list`, `state`, `history`, `edit`, `topology`, and `delete`.
10
10
  A checkpoint can be inspected, forked with new values, and replayed; a graph's nodes
11
11
  and edges are available as JSON for a client view.
12
+ - `langgraph_compose` — compose a session-owned graph from a strict JSON `GraphSpec`,
13
+ validate/list/get/delete it, and `fork` an editable downstream spec after a failure.
14
+ A composed graph uses the same compile, run, checkpoint, trace, background, and
15
+ time-travel path; see [COMPOSE.md](COMPOSE.md) for the IR reference.
12
16
  - **Durable threads.** Every superstep is checkpointed in a DSH storage domain
13
17
  (`langgraph_checkpoints`), so a thread survives the turn and the Host process. Pass
14
18
  `thread` back to continue it; a profile without `storageDomain` falls back to an
@@ -44,6 +48,46 @@ Run a LangGraph graph from inside a DSH agent session, and continue it later.
44
48
  and `helpers.createReactAgent({...})` is LangGraph's prebuilt ReAct agent wired to both.
45
49
  `helpers.messages` re-exports the LangChain message classes.
46
50
 
51
+ ## Composed graphs (GraphSpec)
52
+
53
+ The runtime has one execution path. A composed graph is another **entry**: a stored spec
54
+ resolves to `{ id, description, input, build }`, where `build(api)` calls the plugin's
55
+ deterministic JSON-to-`StateGraph` compiler. It then uses the same `compile`, `executeRun`,
56
+ checkpointer, store, helpers, trace, background job, and thread ownership as a registered
57
+ module.
58
+
59
+ - **IR.** `ir: 1`, state fields with types and reducers, and nodes `tool`, `model`,
60
+ `agent`, `gate`, `map`, `set`, `subagent`. `out` transforms convert a result into a
61
+ typed state value; guards are structured (`eq`, `in`, `gt`, …) and never `eval`.
62
+ Edges are sequence (one unconditional), choice (conditional in order), or static
63
+ fan-out (two or more unconditional). A node with multiple incoming edges joins.
64
+ - **Compiler.** `compose/schema.js` validates with exact IR paths and budgets,
65
+ `compose/analysis.js` computes Tarjan SCCs, reachability, and the writer/reducer
66
+ contract, `compose/build.js` emits the `StateGraph`, and `compose/nodes.js` executes
67
+ one node. The compiler calls no model and performs no I/O.
68
+ - **Specs.** `langgraph_specs` stores one immutable record per content id
69
+ (`lg_adhoc_<24 hex>`), capped at 64 per session and owned by the composing session.
70
+ Without a storage domain the store is process-local. `compose/resolve.js` turns a spec
71
+ id into a runtime entry and caches it per owner.
72
+ - **Map.** A map compiles to a hidden fanout dispatcher, a body node (the spec's id), and
73
+ a hidden join. `Send` runs the body once per item; the join runs once with the
74
+ aggregated state and then follows the spec's out-edges. `append` follows the Send/input
75
+ order, not completion order. An empty array uses a sentinel item so the join still
76
+ runs. The body may be `tool`, `model`, `agent`, or `subagent`; its `out` fields need
77
+ mergeable reducers.
78
+ - **Failure.** A failed run returns `status: "failed"` with `failedNode` (and
79
+ `itemIndex` for a map item), `failedScc`, `upstream`, `downstream`, `pending`, the last
80
+ three checkpoint snapshots with bounded values, the error, and a state copy bounded by
81
+ `output`. A registered graph gets the error and history without the IR dependency sets.
82
+ Background jobs carry the same structure.
83
+ - **Recovery.** Resume the same thread for a transient error; `edit` the failure
84
+ checkpoint and replay for a bad state; `fork` the downstream (or the whole SCC) into a
85
+ new editable spec for a graph bug. The runtime never mutates a spec.
86
+ - **Compose tool.** `describe` is the on-demand guide/law/reference/examples; `validate` stores nothing;
87
+ `compose` previews and stores; `list`, `get`, and `delete` are owner-scoped; `fork`
88
+ returns the downstream skeleton and the thread's current state. A workspace `.json`
89
+ path is read only inside the session cwd.
90
+
47
91
  ## Model-facing guidance
48
92
 
49
93
  While the plugin is active it registers a system-prompt section through DSH's public
@@ -53,7 +97,10 @@ a description match, or a graph-specific need such as a durable thread, backgrou
53
97
  interrupt/resume, checkpoint replay, or Trajectory sub-calls), and the registry (up to 24
54
98
  ids and descriptions; input schemas stay out of the prompt). The model therefore knows
55
99
  what exists without spending a discovery call; `langgraph_run` with no `graph` still
56
- returns the full list and input schemas.
100
+ returns the full list and input schemas. The section also states the compose principle
101
+ (seams, coarse graph, open work in `agent`), that a composed graph is immutable and
102
+ session-owned, and how to read a failed run and fork its downstream. The numeric
103
+ thresholds and node-kind heuristics live in the advisory lint and `describe`.
57
104
 
58
105
  - The section sits just before the subagent guidance, after the `workflow` tool's
59
106
  "only when the user asks" rule. An earlier placement at `TOOL_WORKFLOW` made the two
package/docs/ECOSYSTEM.md CHANGED
@@ -12,7 +12,7 @@ wired and how to add an integration that is not bundled.
12
12
  | Expose DSH tools to LangGraph | `helpers.tools([...names])` | Real LangChain `tool()` instances; visible names only. |
13
13
  | One text completion | `helpers.callModel(prompt, options)` | Deployment model route; text only. |
14
14
  | A LangChain chat model | `helpers.chatModel(options)` | Supports `bindTools`; reports usage. |
15
- | Prebuilt ReAct agent | `helpers.createReactAgent(options)` | Wraps the DSH model and tools; returns an uncompiled graph. |
15
+ | Prebuilt ReAct agent | `helpers.createReactAgent(options)` | Wraps the DSH model and tools; returns a compiled graph (LangGraph 1.7.5). |
16
16
  | Message classes | `helpers.messages` | `AIMessage`, `HumanMessage`, `SystemMessage`, `ToolMessage`. |
17
17
  | Durable checkpoints | `api.checkpointer` | The plugin compiles your graph with it by default. |
18
18
  | Long-term memory | `getStore(config)`, `api.store`, `helpers.store()` | `DshStore` over `langgraph_store`; filters/pagination, no vector search. |
@@ -107,12 +107,37 @@ const agent = await helpers.createReactAgent({
107
107
  llm: helpers.chatModel(),
108
108
  tools: helpers.tools(['read', 'glob', 'bash']),
109
109
  })
110
- return agent // a StateGraph; the plugin compiles it with its checkpointer and store
111
110
  ```
112
111
 
113
- Pass the uncompiled graph back. LangGraph only accepts LangChain Runnables as tools, so
114
- use `helpers.tools` rather than raw DSH tool names; the original DSH JSON schema is what
115
- the model sees.
112
+ In LangGraph 1.7.5 this already returns a **compiled** graph. `compile(entry, ...)`
113
+ detects that and does not inject the plugin's checkpointer or store, so returning it
114
+ directly as the whole graph loses the durable thread. Use it inside a node (as the
115
+ `ask-tools` example and the IR `agent` node do):
116
+
117
+ ```js
118
+ return new StateGraph(State)
119
+ .addNode('react', async state => {
120
+ const result = await agent.invoke({ messages: [['human', state.question]] })
121
+ const last = result.messages.at(-1)
122
+ return { answer: typeof last.content === 'string' ? last.content : '' }
123
+ })
124
+ .addEdge(START, 'react')
125
+ .addEdge('react', END)
126
+ ```
127
+
128
+ If you do use it as the graph itself, pass `checkpointer` and `store` explicitly to
129
+ `createReactAgent`. LangGraph only accepts LangChain Runnables as tools, so use
130
+ `helpers.tools` rather than raw DSH tool names; the original DSH JSON schema is what the
131
+ model sees.
132
+
133
+ ## Composed graphs
134
+
135
+ `langgraph_compose` is a declarative front end over the same runtime, not a second
136
+ ecosystem. A composed node may be `tool`, `model`, `agent` (this same prebuilt ReAct
137
+ agent), `gate`, `map`, `set`, or `subagent`; it cannot import a package or construct a
138
+ custom retriever. When a shape needs an arbitrary LangChain package, a vector store, or
139
+ custom streaming, write a graph module, or keep that work inside an `agent` node. A
140
+ composed spec is never written into `graphs/*.js` and never loads code.
116
141
 
117
142
  ## Streaming
118
143
 
@@ -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