dsh-self-context 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +112 -2
- package/cordis.patch.yml +11 -0
- package/lib/graph.d.ts +93 -0
- package/lib/graph.js +258 -0
- package/lib/index.d.ts +53 -0
- package/lib/index.js +237 -0
- package/lib/projection.d.ts +92 -0
- package/lib/projection.js +184 -0
- package/lib/prompt.d.ts +9 -0
- package/lib/prompt.js +28 -0
- package/lib/state.d.ts +52 -0
- package/lib/state.js +130 -0
- package/package.json +77 -3
- package/src/graph.ts +310 -0
- package/src/index.ts +271 -0
- package/src/projection.ts +224 -0
- package/src/prompt.ts +30 -0
- package/src/state.ts +160 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,113 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-self-context
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/natgurlain/dsh-self-context/actions/workflows/ci.yml) [](https://www.npmjs.com/package/dsh-self-context)
|
|
4
|
+
|
|
5
|
+
[Self-Context](https://github.com/XMUDeepLIT/Self-Context) ([arXiv:2612.01681](https://arxiv.org/abs/2612.01681)) for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness): the agent keeps a **working graph** of what it has established and **folds** finished steps of its own history into one-line notes, with **lossless unfold**.
|
|
6
|
+
|
|
7
|
+
The original project is a gateway (an HTTP proxy) placed between a harness and the model. This plugin moves the same idea into dsh. It runs inside the agent, so you don't need a proxy, and dsh's session log stays the record of everything the model saw.
|
|
8
|
+
|
|
9
|
+
## How it works
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
DeepSeek model ⇄ dsh agent loop
|
|
13
|
+
│ tool calls ▲ derived request (system + history + runtime context)
|
|
14
|
+
▼ │
|
|
15
|
+
┌─────────── dsh-self-context ───────────┐
|
|
16
|
+
│ context_graph → self-context/graph │ G_t: working graph (projection unit)
|
|
17
|
+
│ context_history → self-context/fold │ F_t: critical history
|
|
18
|
+
│ self-context/unfold │
|
|
19
|
+
│ message projections (fold / unfold) │ rewrite what the model sees, never the log
|
|
20
|
+
│ prompt section + runtime context │ protocol + current graph
|
|
21
|
+
└─────────────────────────────────────────┘
|
|
22
|
+
│ append-only events
|
|
23
|
+
▼
|
|
24
|
+
Session log (raw history H_t, never rewritten)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Paper | In dsh |
|
|
28
|
+
|---|---|
|
|
29
|
+
| Graph management 𝒢 (`<node op="add/edit/link/remove">`) | `context_graph` tool. Ops are logged as `self-context/graph` and folded by a Session projection unit. The graph is shown as dynamic runtime context, which dsh re-sends when it changes and restores after compaction. |
|
|
30
|
+
| History management 𝒯 (`<node op="fold/unfold" turns="k">`) | `context_history` tool (`list`, `fold`, `unfold`). A whole step (one model call, its reasoning, tool calls and paired results) becomes a summary note. Folds are `@messageProjection` events, like dsh's own image-offload plugin. |
|
|
31
|
+
| Native history H_t kept for lossless unfold | The session log is append-only. Unfold re-projects the original logged messages. |
|
|
32
|
+
| c_t = F_t ⊕ G_t | dsh derives each request as projected history plus the latest runtime-context snapshot holding the graph. |
|
|
33
|
+
|
|
34
|
+
Steps are labelled `turn.step`, so `3.2` means the second model call of the third turn. `context_history list` shows each step's label, size, tool calls and fold status.
|
|
35
|
+
|
|
36
|
+
What the model sees after a fold:
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
assistant: [Folded step 1.2] read app.log: pool exhausted -> f1 [Original text, reasoning, tool calls and results hidden. Restore with context_history op="unfold" steps=["1.2"].]
|
|
40
|
+
+ tool-call blocks kept with arguments "{}" so call/result pairing stays valid
|
|
41
|
+
tool: [Result folded into step 1.2.]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
dsh plugin --profile <profile> add ./dsh-self-context # or a tarball from `npm pack`
|
|
48
|
+
dsh --profile <profile> --dump-config # shows a "# == dsh-self-context" layer
|
|
49
|
+
dsh --profile <profile> web
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
To try it from a dsh source checkout without installing, add a patch file:
|
|
53
|
+
|
|
54
|
+
```yaml
|
|
55
|
+
- insert:
|
|
56
|
+
- id: self-context
|
|
57
|
+
name: '/absolute/path/to/dsh-self-context/src/index.ts'
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
pnpm dsh web --patch ./self-context.patch.yml
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Configuration
|
|
65
|
+
|
|
66
|
+
| Field | Default | Meaning |
|
|
67
|
+
|---|---|---|
|
|
68
|
+
| `instructions` | `true` | Add the Self-Context protocol to the system prompt. Set to `false` if your persona already explains it. |
|
|
69
|
+
| `instructionsOrder` | `9000` | System-prompt section order. |
|
|
70
|
+
| `showGraph` | `true` | Show the working graph as `required` runtime context. |
|
|
71
|
+
| `graphContextOrder` | `9000` | Runtime-context order. |
|
|
72
|
+
| `maxNodeContentChars` | `1000` | Characters kept per node. |
|
|
73
|
+
| `maxNodes` | `200` | Live node cap; `add` beyond it is rejected with a message to the model. |
|
|
74
|
+
| `maxSummaryChars` | `400` | Fold summary cap, the same as the paper's 400. |
|
|
75
|
+
|
|
76
|
+
## Behaviour and limits
|
|
77
|
+
|
|
78
|
+
- **Folds are validated twice.** The tool rejects unknown, running, already-folded or duplicate steps with a message to the model. The projection re-validates each fold before Session commits it, so a bad event can't enter the log.
|
|
79
|
+
- **Replay, resume and fork** rebuild the same view from the log. Detached readers pass `selfContextProjections` (exported from `dsh-self-context/projection`) to `foldSurface()`. If the plugin is unloaded, readers see the raw history, so no data is lost.
|
|
80
|
+
- **Prompt cache.** Changing an old message ends DeepSeek cache reuse from that point, which is the same trade-off as in the paper. Folding big, finished outputs in batches costs less than folding one step at a time.
|
|
81
|
+
- **Graph snapshots.** Each graph change appends a fresh runtime-context snapshot, and earlier snapshots stay in history marked as superseded. Compaction clears them.
|
|
82
|
+
- **Thinking replay.** Folded assistant messages drop their reasoning blocks and the DeepSeek `replayState` signatures for them. Tool-call ids and names are kept.
|
|
83
|
+
- **Compaction.** `dsh-compaction-basic` still runs as a backstop when the window fills. Steps it summarizes leave the surface and can no longer be unfolded.
|
|
84
|
+
- **Image offload.** Unfolding a step whose tool results had images offloaded earlier brings back the original images. The image-offload plugin will offload them again if the route needs it.
|
|
85
|
+
|
|
86
|
+
## Development
|
|
87
|
+
|
|
88
|
+
The tests run the real dsh agent loop with a scripted model, against the published dsh packages:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
pnpm install
|
|
92
|
+
pnpm run typecheck && pnpm test && pnpm run build
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Credits
|
|
96
|
+
|
|
97
|
+
This plugin implements **Self-Context: Graph-Based Context Self-Management for Long-Horizon Agents** by Ziheng Wang, Chengyi Yang, Renxu Bao, Zhishang Xiang, Chengsong Huang, Wenhao Li, Junnan Zhu, Delai Qiu, Yining Wang, Shengping Liu and Jinsong Su ([arXiv:2612.01681](https://arxiv.org/abs/2612.01681), [project page](https://self-context.github.io/), [Hugging Face](https://huggingface.co/papers/2612.01681), [code](https://github.com/XMUDeepLIT/Self-Context)).
|
|
98
|
+
|
|
99
|
+
The protocol text and graph semantics are adapted from [XMUDeepLIT/Self-Context](https://github.com/XMUDeepLIT/Self-Context) (Apache-2.0). The test model in `tests/mock-adapter.ts` is adapted from DeepSeek Harness, which is MIT-licensed.
|
|
100
|
+
|
|
101
|
+
If you use this plugin in research, please cite the paper:
|
|
102
|
+
|
|
103
|
+
```bibtex
|
|
104
|
+
@misc{wang2026self-context,
|
|
105
|
+
title = {Self-Context: Graph-Based Context Self-Management for Long-Horizon Agents},
|
|
106
|
+
author = {Ziheng Wang and Chengyi Yang and Renxu Bao and Zhishang Xiang and Chengsong Huang and Wenhao Li and Junnan Zhu and Delai Qiu and Yining Wang and Shengping Liu and Jinsong Su},
|
|
107
|
+
year = {2026},
|
|
108
|
+
eprint = {2612.01681},
|
|
109
|
+
archivePrefix = {arXiv},
|
|
110
|
+
primaryClass = {cs.AI},
|
|
111
|
+
url = {https://arxiv.org/abs/2612.01681}
|
|
112
|
+
}
|
|
113
|
+
```
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Inserts the Self-Context plugin into any profile that lists this bundle.
|
|
2
|
+
# Override the config from your profile's cordis.patch.yml by row id.
|
|
3
|
+
- insert:
|
|
4
|
+
- id: self-context
|
|
5
|
+
name: dsh-self-context
|
|
6
|
+
config:
|
|
7
|
+
instructions: true
|
|
8
|
+
showGraph: true
|
|
9
|
+
maxNodes: 200
|
|
10
|
+
maxNodeContentChars: 1000
|
|
11
|
+
maxSummaryChars: 400
|
package/lib/graph.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The working graph G_t from Self-Context (arXiv:2612.01681), ported to a pure,
|
|
3
|
+
* replayable TypeScript model. Nodes have stable ids and append-only revisions;
|
|
4
|
+
* `remove` tombstones a node so it leaves the model-facing view but stays in
|
|
5
|
+
* history. The same `applyGraphOps` both validates a model call and replays the
|
|
6
|
+
* durable `self-context/graph` events, so live state and replay never diverge.
|
|
7
|
+
*/
|
|
8
|
+
/** Graph operation kinds the model may issue. */
|
|
9
|
+
export declare const GRAPH_OPS: readonly ["add", "edit", "link", "remove"];
|
|
10
|
+
export type GraphOpKind = (typeof GRAPH_OPS)[number];
|
|
11
|
+
/** One model-issued graph operation, before normalization. */
|
|
12
|
+
export interface GraphOpInput {
|
|
13
|
+
op: GraphOpKind;
|
|
14
|
+
id?: string;
|
|
15
|
+
type?: string;
|
|
16
|
+
content?: string;
|
|
17
|
+
status?: string;
|
|
18
|
+
parents?: string[];
|
|
19
|
+
}
|
|
20
|
+
/** One normalized operation as persisted in the session log. */
|
|
21
|
+
export interface GraphOp {
|
|
22
|
+
op: GraphOpKind | 'restore';
|
|
23
|
+
id: string;
|
|
24
|
+
type?: string;
|
|
25
|
+
content?: string;
|
|
26
|
+
status?: string;
|
|
27
|
+
parents?: string[];
|
|
28
|
+
}
|
|
29
|
+
/** One node of the working graph. */
|
|
30
|
+
export interface GraphNode {
|
|
31
|
+
id: string;
|
|
32
|
+
type: string;
|
|
33
|
+
content: string;
|
|
34
|
+
status?: string | undefined;
|
|
35
|
+
parents: string[];
|
|
36
|
+
/** Monotonic revision number; 1 on creation. */
|
|
37
|
+
v: number;
|
|
38
|
+
/** Tombstoned nodes are hidden from the model but kept in history. */
|
|
39
|
+
removed: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** The whole working graph as plain JSON: nodes by id plus creation order. */
|
|
42
|
+
export interface GraphState {
|
|
43
|
+
nodes: Record<string, GraphNode>;
|
|
44
|
+
order: string[];
|
|
45
|
+
}
|
|
46
|
+
/** Result of applying one batch of model operations. */
|
|
47
|
+
export interface GraphApplyResult {
|
|
48
|
+
/** Normalized operations that took effect, in order; persist these. */
|
|
49
|
+
applied: GraphOp[];
|
|
50
|
+
/** Recoverable problems reported back to the model. */
|
|
51
|
+
errors: string[];
|
|
52
|
+
}
|
|
53
|
+
/** Limits applied while normalizing model input. */
|
|
54
|
+
export interface GraphLimits {
|
|
55
|
+
maxNodeContentChars: number;
|
|
56
|
+
maxNodes: number;
|
|
57
|
+
}
|
|
58
|
+
/** Own-property node lookup, safe against ids like `constructor`. */
|
|
59
|
+
export declare function getNode(state: GraphState, id: string): GraphNode | undefined;
|
|
60
|
+
/** A deep copy that later operations may mutate. */
|
|
61
|
+
export declare function cloneGraph(state: GraphState): GraphState;
|
|
62
|
+
/** An empty working graph. */
|
|
63
|
+
export declare function emptyGraph(): GraphState;
|
|
64
|
+
/**
|
|
65
|
+
* Apply model operations, mutating `state`. Invalid operations are skipped and
|
|
66
|
+
* reported; valid ones are normalized (fresh ids assigned, unknown parents
|
|
67
|
+
* dropped) so replaying `applied` reproduces exactly the same state.
|
|
68
|
+
* @param state - graph to mutate.
|
|
69
|
+
* @param ops - operations in model order.
|
|
70
|
+
* @param limits - content and size limits.
|
|
71
|
+
* @returns normalized applied operations and recoverable errors.
|
|
72
|
+
*/
|
|
73
|
+
export declare function applyGraphOps(state: GraphState, ops: readonly GraphOpInput[], limits: GraphLimits): GraphApplyResult;
|
|
74
|
+
/**
|
|
75
|
+
* Apply one already-normalized operation. Used for live application and replay.
|
|
76
|
+
* @param state - graph to mutate.
|
|
77
|
+
* @param op - normalized operation.
|
|
78
|
+
* @returns the same operation, for chaining into an applied list.
|
|
79
|
+
*/
|
|
80
|
+
export declare function applyOne(state: GraphState, op: GraphOp): GraphOp;
|
|
81
|
+
/**
|
|
82
|
+
* Rebuild a graph from persisted operation batches.
|
|
83
|
+
* @param batches - `ops` arrays of `self-context/graph` events in log order.
|
|
84
|
+
* @returns the reconstructed graph.
|
|
85
|
+
*/
|
|
86
|
+
export declare function replayGraph(batches: Iterable<readonly GraphOp[]>): GraphState;
|
|
87
|
+
/**
|
|
88
|
+
* Serialize live nodes in creation order, in the same `<node>` vocabulary the
|
|
89
|
+
* model writes with. Tombstoned nodes are omitted.
|
|
90
|
+
* @param state - graph to render.
|
|
91
|
+
* @returns XML-ish text, or an empty string for an empty graph.
|
|
92
|
+
*/
|
|
93
|
+
export declare function serializeGraph(state: GraphState): string;
|