@hugesb/dsh-stream-agent 0.0.0-stage → 0.3.1

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 (158) hide show
  1. package/CHANGELOG.md +497 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +202 -0
  4. package/README.en.md +349 -0
  5. package/README.md +300 -3
  6. package/cordis.patch.yml +77 -0
  7. package/dist/entry/action-gate.js +206 -0
  8. package/dist/entry/adapter-kit.js +192 -0
  9. package/dist/entry/context-render.js +119 -0
  10. package/dist/entry/control-bar-cli.js +289 -0
  11. package/dist/entry/control-bar.js +123 -0
  12. package/dist/entry/control-session.js +115 -0
  13. package/dist/entry/control-state.js +125 -0
  14. package/dist/entry/cordis-compat.js +95 -0
  15. package/dist/entry/eval-probe.js +112 -0
  16. package/dist/entry/intent-log.js +121 -0
  17. package/dist/entry/json-persistence.js +306 -0
  18. package/dist/entry/loop.js +3852 -0
  19. package/dist/entry/mock-provider.js +536 -0
  20. package/dist/entry/obs-broker.js +178 -0
  21. package/dist/entry/ptc-bridge.js +405 -0
  22. package/dist/entry/state-handle.js +217 -0
  23. package/dist/entry/takeover-registry.js +60 -0
  24. package/dist/kernel/control-bar.js +559 -0
  25. package/dist/kernel/final.js +62 -0
  26. package/dist/kernel/gate.js +464 -0
  27. package/dist/kernel/modes.js +253 -0
  28. package/dist/kernel/obs.js +517 -0
  29. package/dist/kernel/parser.js +632 -0
  30. package/dist/kernel/render.js +444 -0
  31. package/dist/kernel/state.js +416 -0
  32. package/dist/version.js +9 -0
  33. package/docs/TEST-PROPOSITIONS.md +139 -0
  34. package/docs/adr-001-segment-boundary.md +61 -0
  35. package/docs/architecture.md +104 -0
  36. package/docs/desktop-ui-feasibility.md +277 -0
  37. package/docs/disabled-lines-audit.md +53 -0
  38. package/docs/limitations.md +181 -0
  39. package/docs/protocol.md +99 -0
  40. package/docs/v0.4.0-plan.md +50 -0
  41. package/eval/METRICS.md +128 -0
  42. package/eval/RESULTS.md +246 -0
  43. package/eval/tasks/fixtures/big-report.md +298 -0
  44. package/eval/tasks/fixtures/build-manifest.txt +5 -0
  45. package/eval/tasks/fixtures/deploy.yaml +4 -0
  46. package/eval/tasks/fixtures/error.log +200 -0
  47. package/eval/tasks/fixtures/features.yaml +4 -0
  48. package/eval/tasks/fixtures/pipeline.yaml +4 -0
  49. package/eval/tasks/fixtures/sales-q1.csv +61 -0
  50. package/eval/tasks/fixtures/settings.yaml +4 -0
  51. package/eval/tasks/fixtures/users-roster.json +304 -0
  52. package/eval/tasks/fixtures/users-scores.json +20 -0
  53. package/eval/tasks/gen-fixtures.cjs +80 -0
  54. package/eval/tasks/tasks.json +150 -0
  55. package/eval/tools/f6-kill-drill.cjs +176 -0
  56. package/eval/tools/f6-kill-drill.worker.cjs +131 -0
  57. package/eval/tools/gen-results.cjs +191 -0
  58. package/eval/tools/machine-test.cjs +200 -0
  59. package/eval/tools/manual-code-scenarios.md +59 -0
  60. package/eval/tools/replay-check.cjs +311 -0
  61. package/eval/tools/runner.cjs +219 -0
  62. package/eval/tools/session-log.cjs +106 -0
  63. package/eval/tools/summarize.cjs +185 -0
  64. package/eval/tools/t3-cells.cjs +173 -0
  65. package/package.json +73 -6
  66. package/patches/adapters-smoke.yml +41 -0
  67. package/patches/eval-account.yml +36 -0
  68. package/patches/eval-baseline.yml +33 -0
  69. package/patches/eval-stream-code-off.yml +46 -0
  70. package/patches/eval-stream-code-on.yml +47 -0
  71. package/patches/eval-stream.yml +54 -0
  72. package/patches/takeover.yml +42 -0
  73. package/patches/test-preset.yml +23 -0
  74. package/preset/stream/cordis.patch.yml +39 -0
  75. package/preset/stream/package.json +13 -0
  76. package/preset.yml +56 -0
  77. package/profiles/stream/README.md +66 -0
  78. package/profiles/stream/cordis.patch.yml +12 -0
  79. package/profiles/stream/package.json +17 -0
  80. package/profiles/stream-headless/package.json +16 -0
  81. package/scripts/build.mjs +103 -0
  82. package/scripts/check-consistency.mjs +146 -0
  83. package/scripts/gate.mjs +138 -0
  84. package/scripts/install-check.mjs +116 -0
  85. package/scripts/install-cycle.mjs +88 -0
  86. package/scripts/release.mjs +415 -0
  87. package/scripts/repair-session-logs.mjs +403 -0
  88. package/src/entry/action-gate.ts +206 -0
  89. package/src/entry/adapter-kit.ts +192 -0
  90. package/src/entry/context-render.ts +119 -0
  91. package/src/entry/control-bar-cli.ts +289 -0
  92. package/src/entry/control-bar.ts +123 -0
  93. package/src/entry/control-session.ts +115 -0
  94. package/src/entry/control-state.ts +125 -0
  95. package/src/entry/cordis-compat.ts +95 -0
  96. package/src/entry/eval-probe.ts +112 -0
  97. package/src/entry/intent-log.ts +121 -0
  98. package/src/entry/json-persistence.ts +306 -0
  99. package/src/entry/loop.ts +3852 -0
  100. package/src/entry/mock-provider.ts +536 -0
  101. package/src/entry/obs-broker.ts +178 -0
  102. package/src/entry/ptc-bridge.ts +405 -0
  103. package/src/entry/state-handle.ts +217 -0
  104. package/src/entry/takeover-registry.ts +60 -0
  105. package/src/kernel/control-bar.ts +559 -0
  106. package/src/kernel/final.ts +62 -0
  107. package/src/kernel/gate.ts +464 -0
  108. package/src/kernel/modes.ts +253 -0
  109. package/src/kernel/obs.ts +517 -0
  110. package/src/kernel/parser.ts +632 -0
  111. package/src/kernel/render.ts +444 -0
  112. package/src/kernel/state.ts +416 -0
  113. package/src/version.ts +9 -0
  114. package/test/activation.test.ts +187 -0
  115. package/test/adapters.test.ts +676 -0
  116. package/test/control-bar.test.ts +486 -0
  117. package/test/desktop-ui.test.ts +359 -0
  118. package/test/eval-probe.test.ts +139 -0
  119. package/test/f5-plan.test.ts +221 -0
  120. package/test/f6-intent.test.ts +168 -0
  121. package/test/f7-segment.test.ts +190 -0
  122. package/test/fix-limits.test.ts +599 -0
  123. package/test/gate.test.ts +380 -0
  124. package/test/helpers/stub-loop.ts +240 -0
  125. package/test/kernel/obs.test.ts +329 -0
  126. package/test/kernel/parser.test.ts +450 -0
  127. package/test/loop.test.ts +1393 -0
  128. package/test/mock-provider.test.ts +386 -0
  129. package/test/ptc-bridge.test.ts +356 -0
  130. package/test/render.test.ts +418 -0
  131. package/test/s2-install-check.test.ts +46 -0
  132. package/test/scenarios/3-file-basic.json +13 -0
  133. package/test/scenarios/5-file-basic.json +22 -0
  134. package/test/scenarios/burn-more.json +9 -0
  135. package/test/scenarios/degraded-final.json +10 -0
  136. package/test/scenarios/fault-abort-xml.json +11 -0
  137. package/test/scenarios/fault-malformed-command.json +12 -0
  138. package/test/scenarios/fault-midstream-abort.json +11 -0
  139. package/test/scenarios/fault-saturating-output.json +9 -0
  140. package/test/scenarios/fault-timeout.json +9 -0
  141. package/test/scenarios/peek-first.json +11 -0
  142. package/test/scenarios/peek-second.json +11 -0
  143. package/test/scenarios/peek-third.json +10 -0
  144. package/test/scenarios/recover-final.json +10 -0
  145. package/test/scenarios/reroute-first.json +17 -0
  146. package/test/scenarios/reroute-second.json +16 -0
  147. package/test/scenarios/starvation-final.json +10 -0
  148. package/test/scenarios/starvation-promote.json +11 -0
  149. package/test/scenarios/wait-tools-continue.json +10 -0
  150. package/test/scenarios/wait-tools-xml.json +22 -0
  151. package/test/scenarios/wait-tools.json +15 -0
  152. package/test/session-format.test.ts +234 -0
  153. package/test/state.test.ts +335 -0
  154. package/test/stream-v02.test.ts +264 -0
  155. package/test/v031-display.test.ts +333 -0
  156. package/ui/client.js +576 -0
  157. package/ui/index.js +282 -0
  158. package/ui/package.json +26 -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 2026 54dsb1
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.en.md ADDED
@@ -0,0 +1,349 @@
1
+ # dsh-stream-agent
2
+
3
+ [![ci](https://github.com/54dsb1/dsh-stream-agent/actions/workflows/ci.yml/badge.svg)](https://github.com/54dsb1/dsh-stream-agent/actions/workflows/ci.yml)
4
+ [![license](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](./LICENSE)
5
+
6
+ [简体中文](./README.md) | English
7
+
8
+ **A single-stream heart for DeepSeek Harness.** Instead of the turn-based loop, the model
9
+ writes every `tf-*` command inside one streamed response; commands execute the moment they
10
+ close; results are handle-ized and fetched on demand; the context only ever appends — never
11
+ rewrites. The goal: drive per-task tokens down to a fraction of the native loop while turning
12
+ every failure, cancellation, and sandbox denial into an **auditable fact** instead of an
13
+ error string.
14
+
15
+ Measured on real connections (10 tasks, two arms, same `deepseek-flash` model, stamp
16
+ `2026-11-15-v02`, four-component totals recomputed by `summarize.cjs`): **28.9% of the
17
+ native loop's tokens with parity on correctness**. The historical 22.3% figure comes from an
18
+ earlier batch — the two are not comparable; see the honest note in
19
+ [eval/RESULTS.md](eval/RESULTS.md) §6. The R3 rerun will re-measure both arms in one batch.
20
+
21
+ | Dimension | Status |
22
+ | ----- | ----- |
23
+ | Host | DeepSeek Harness `0.2.2-rc.1` (type anchor) / `rc.3` (runtime-tested) |
24
+ | Dependencies | Zero npm dependencies; Node ≥ 22.18 (native type stripping) |
25
+ | Tests | 275 cases green, zero API calls (mock full-chain + 20 scenarios + activation/desktop/new-capability/session-format regressions) |
26
+ | Shape | Single bundle + **dedicated profile** (`profiles/stream` template); `dsh plugin remove` unwinds the whole layer and restores the host loop atomically |
27
+ | Control bar | `dsh-stream-agent bar`: four modes + zh/en + intent triage, onboarding banner on load |
28
+ | Desktop page | Left-rail **Control bar**: status + four mode cards + zh/en switch, **without touching the Electron shell** |
29
+ | Metrics | Four-component totals generated by script; replay consistency 10/10 on the plugin arm; 6-item release gate |
30
+ | Release | npm [`@hugesb/dsh-stream-agent`](https://www.npmjs.com/package/@hugesb/dsh-stream-agent) · GitHub [Releases](https://github.com/54dsb1/dsh-stream-agent/releases) |
31
+
32
+ > **0.3.0 install change (important)**: in standalone mode the plugin installs **only into a
33
+ > dedicated profile** (`profiles/stream` / `profiles/stream-headless`). The 0.2.x style
34
+ > "plugin add into an existing profile" is no longer supported — the install precheck rejects
35
+ > `web` / `desktop`, and double-loop conflicts fail with a named `E_DOUBLE_LOOP`.
36
+ > Migration: remove the old install → create the dedicated profile → reinstall (see §2).
37
+
38
+ ---
39
+
40
+ ## 1. What problem does it solve
41
+
42
+ The native loop is turn-based: one request per step, tool results stuffed back into context
43
+ in full, the model reads everything before deciding what's next. Three things happen at
44
+ once — **too many requests, observations re-injected repeatedly, and the prefix cache broken
45
+ over and over**.
46
+
47
+ This plugin takes over that layer and changes exactly three things:
48
+
49
+ 1. **Single stream** — the model writes all its `tf-*` commands consecutively inside one
50
+ stream; the parser parses chunk by chunk and dispatches each command the moment it
51
+ closes, executing independent commands in parallel without waiting for the stream to end.
52
+ 2. **No observation backflow** — tool output returns a handle by default; the model sends
53
+ `tf-peek` to fetch details by line range. Injection volume flips from "we guess" to "the
54
+ model decides".
55
+ 3. **Monotonic prefix** — context assembly only ever appends to the tail; a byte-level
56
+ assertion runs before every request and refuses to send on any unrecorded drift
57
+ (fail-closed). Better a clean failure than a burnt cache.
58
+
59
+ No model retraining, no upstream source changes — everything exists as plugin entries +
60
+ patches.
61
+
62
+ ## 2. Install: the dedicated profile (the only supported shape)
63
+
64
+ ```bash
65
+ # 1) Copy the dedicated profile template (local dev uses link:; npm installs pin exact
66
+ # versions — no ^/~ ranges)
67
+ cp -r profiles/stream ~/.dsh/profiles/stream
68
+
69
+ # 2) Install precheck (S2): read-only; the target must be stream / stream-headless
70
+ node scripts/install-check.mjs --profile ~/.dsh/profiles/stream --dry-run
71
+
72
+ # 3) Startup self-proof: the banner should read TAKEOVER-ACTIVE … code=off … profile=stream
73
+ dsh --profile stream
74
+
75
+ # 4) Install/uninstall regression (optional, 10 rounds of add → self-proof → remove → restore)
76
+ node scripts/install-cycle.mjs --dsh dsh --profile stream --rounds 10
77
+
78
+ # Uninstall: the bundle layer disappears as a whole; the host loop is restored atomically.
79
+ # Full removal = delete the profile directory.
80
+ dsh plugin remove @hugesb/dsh-stream-agent --profile stream
81
+ ```
82
+
83
+ - Headless entry: use `profiles/stream-headless` (no preset bundle — headless hosts have no
84
+ preset registry; the tool surface is decided by the profile).
85
+ - The composition order `dsh-base → main bundle → preset bundle → entry` is a contract;
86
+ reordering counts as an assembly change and needs re-acceptance. Details in
87
+ [profiles/stream/README.md](profiles/stream/README.md).
88
+ - The `agent-loop` disable lives **only in the main bundle's cordis.patch.yml** — `plugin
89
+ remove` unwinds it atomically (C4). The profile patch layer only overrides the preset
90
+ registry default and never takes over the loop.
91
+ - Installing into `web` / `desktop` is rejected by `install-check`; bypassing the check
92
+ still fails fast with `E_DOUBLE_LOOP` (profile id + remediation hint) — never a silent
93
+ coexistence (C2/S3).
94
+ - `dsh plugin` needs `pnpm` on PATH (or a configured `pnpmCommand`).
95
+ - **Old session logs (0.2.x and anything written before 0.3.0 shipped)**: 0.3.0 fixes how the
96
+ v4 tool lifecycle is written — logs written earlier make the desktop app show
97
+ "failed to load history" (`tool/call … has no advertised tool lifecycle`; the host marks the
98
+ whole log corrupt — no data was lost). After upgrading, run
99
+ `node scripts/repair-session-logs.mjs --write` once (drop `--write` to preview the list;
100
+ it backs up to `.bak-<timestamp>` and clears the projection cache), then history loads.
101
+
102
+ For development you can still stack temporarily without installing:
103
+ `dsh --profile headless --patch ./patches/takeover.yml headless "task"`.
104
+
105
+ ## 3. Run modes (four control-bar modes)
106
+
107
+ The interactive panel (`dsh-stream-agent bar`), the CLI (`dsh-stream-agent mode lean`) and
108
+ the desktop page all read the same `kernel/modes.ts`. Modes only provide defaults;
109
+ **explicit config always wins over mode values**.
110
+
111
+ How much does each save? Honest answer: only "default vs native loop" has a measured figure
112
+ (~29%, 10 tasks, two arms); the other tiers are directional and labelled "not separately
113
+ benchmarked" — we do not present unmeasured numbers as percentages.
114
+
115
+ | Mode | Token saving | Availability | Notes |
116
+ | --- | --- | --- | --- |
117
+ | `balanced` (default) | Baseline (~29% of the native loop) | Default, works out of the box | tf-code off by default (K1) |
118
+ | `lean` | Higher | Ready | 60k budget ladder + parallelism 2 + no tf-code; the gap widens on long sessions |
119
+ | `compat` | Lowest | Ready | Full read-back restored, parallelism 1; still below the native loop — the single-stream protocol itself is the saving |
120
+ | `power` | Highest (best on data-processing) | Needs host PTC runtime | The only mode that turns tf-code on; on plain read/write tasks it costs slightly more |
121
+
122
+ Mode → config mapping (explicit config > mode > built-in defaults):
123
+
124
+ | Mode | `selfReadFeedback` | `enableCode` | `persistFallback` | Parallel | Reentries | `budgetTokens` |
125
+ | --- | --- | --- | --- | --- | --- | --- |
126
+ | `balanced` | `receipt` | `false` | `true` | 3 | 8 | — |
127
+ | `lean` | `receipt` | `false` | `true` | 2 | 6 | 60,000 |
128
+ | `compat` | `full` | `false` | `true` | 1 | 12 | — |
129
+ | `power` | `receipt` | `true` | `true` | 3 | 10 | — |
130
+
131
+ One-line decision guide: **stay on `balanced`; switch to `lean` when the bill matters;
132
+ `compat` when diffing against the native loop; `power` for programmatic data work.**
133
+
134
+ ## 4. The tf-* protocol (11 commands)
135
+
136
+ The parser (`src/kernel/parser.ts`) is the authority; full semantics in
137
+ [docs/protocol.md](docs/protocol.md).
138
+
139
+ | Command | Purpose | Where results go |
140
+ | --- | --- | --- |
141
+ | `tf-write` | Write a file (body = raw content) | Success = ledger only; failure injects `[tf-stream:error]` |
142
+ | `tf-read` | Read a file (optional offset/limit line ranges) | First/changed results injected in full (≤8000 chars, head+tail kept); reading a path you wrote this task returns a receipt only (lines+sha256, O7) |
143
+ | `tf-edit` | Exact replace (body uses `<<<<<<< OLD` / `=======` / `>>>>>>> NEW` markers; maps to host edit) | Success = ledger; match failure is a structured error — **no fuzzy matching, never silently rewrites the whole file** |
144
+ | `tf-append` | Append-only (optional anchor; maps to host edit / apply_patch EOF anchor) | Success = ledger; position and newline rules are deterministic |
145
+ | `tf-grep` | Content search (maps to host fs-search), returns matching lines with numbers | First/changed injected in full; over-limit → handle |
146
+ | `tf-glob` | Filename search, returns paths only | Same |
147
+ | `tf-bash` | Run a command (pwsh on Windows) | Success = ledger; use `tf-peek` to see output |
148
+ | `tf-peek` | Fetch a line range from a handle | Injected on demand |
149
+ | `tf-plan` | Declare/revise the plan and acceptance items (`- [id] description` lines) | State lives driver-side; requests carry **diffs only** |
150
+ | `tf-final` | Finish (summary must contain the answer verbatim) | Flagged premature while acceptance items are unverified (F5) |
151
+ | `tf-code` | Programmatic tool calls (**off by default**; `power` mode or explicit opt-in) | Values within the cap injected in full; over-limit → skeleton+handle; failures carry `kind` + retry verdict |
152
+
153
+ Common attributes: `id` (auto-numbered when omitted), `depends_on` (cycles detected and
154
+ named), `accepts` (acceptance links, F5). Two token-saving disciplines are written into the
155
+ protocol text: **don't re-read files you just wrote** (you get a receipt) and **don't use
156
+ tf-bash as a search tool** (tf-grep/tf-glob are cheaper and return matching lines only).
157
+
158
+ ## 5. Plan, recovery, long sessions (on by default, each with an off switch)
159
+
160
+ **Plan & acceptance (F5)** — completion semantics for composite tasks; "claims done without
161
+ a receipt" is structurally intercepted:
162
+
163
+ ```
164
+ <tf-plan id="p1">
165
+ - [a1] generate intro.md
166
+ - [a2] generate summary.md
167
+ </tf-plan>
168
+ <tf-write path="intro.md" id="w1" accepts="a1">…</tf-write>
169
+ ```
170
+
171
+ - Acceptance items can only be verified by **successful command receipts** (`accepts="..."`);
172
+ the model saying "done" is not evidence;
173
+ - `tf-final` converges only after every acceptance item has a receipt; otherwise a premature
174
+ hint guides the model to finish or revise the plan;
175
+ - Plan state lives driver-side; requests carry **diffs only** (full plan once, then only
176
+ added/verified/removed lines);
177
+ - Turn off: `enablePlan: false`.
178
+
179
+ **Recovery intent (F6)** — honest state after a hard kill, zero injection:
180
+
181
+ - Before dispatching a mutating command (tf-write/tf-edit/tf-append) the intent
182
+ (op/path/content fingerprint) is fsync'd to `~/.dsh-stream-agent/intents/<session>.jsonl`;
183
+ the receipt is appended the moment the command settles;
184
+ - Three-state semantics under kill -9: killed before the write = didn't happen; killed
185
+ mid-write = uncertain; killed after the write but before the receipt = file is complete on
186
+ disk yet the ledger shows "intent without receipt" — **no automatic replay**;
187
+ - On resume, "intent without receipt" blocks the turn immediately (no request, zero
188
+ injection); triage with `dsh-stream-agent intents`, close with `--ack <session>` after a
189
+ human check;
190
+ - Kill drill (runs offline): `node eval/tools/f6-kill-drill.cjs --rounds 10` — 10 SIGKILL
191
+ rounds across three kill points; 0 replays, 0 duplicate writes, all three states
192
+ distinguishable;
193
+ - Turn off: `enableIntents: false`; warn-only: `resumeIntentPolicy: 'ignore'`.
194
+
195
+ **Long-session segment boundary (F7)** — bounded context growth
196
+ ([ADR-001](docs/adr-001-segment-boundary.md)):
197
+
198
+ - When the append region exceeds `segmentMaxChars` (default 160k chars ≈ 40k tokens), whole
199
+ blocks of the old segment are dropped at the next turn boundary — **never summarized,
200
+ never rewritten** (ADR chose whole-block discard and rejected host compaction);
201
+ - Discarded originals are handle-ized (`obs:read:seg-*`) and retrievable via `tf-peek` /
202
+ `peekHandle()`;
203
+ - The cost is stated honestly: the first request after a drop is a cold cache request; every
204
+ drop is recorded in policyLog and the ledger — never silent; drops only land on turn
205
+ boundaries (tool calls and their results never split);
206
+ - Measured: 50 turns × 2 requests = 100 requests with 0 prefix violations and
207
+ last/first ≤1.5×;
208
+ - Turn off: `segmentMaxChars: 0`.
209
+
210
+ ## 6. tf-code: the opt-in channel (K1–K6)
211
+
212
+ - **Enable**: `power` mode, or `enableCode: true` in config (requires the host to mount
213
+ `@deepseek-ai/dsh-ptc-runtime`). The default assembly declares 0 characters of tf-code —
214
+ the model cannot hallucinate it; no session-level dynamic toggles (the protocol section is
215
+ frozen at construction).
216
+ - **Execution boundary (stated honestly, no isolation promises)**: the sandbox covers **file
217
+ effects only** — programs can reach the network and see processes; the Windows ACL tier
218
+ reports `enforcement=partial`; `danger-full-access` has no constraint at all. These facts
219
+ are recorded per run (`mode` / `denied` / `enforcement`) and `partial` is always visible.
220
+ - **Failure semantics (K3)**: the eight runtime `kind`s are preserved verbatim (exception /
221
+ timeout / abort / worker-exit / invalid-output / output-limit / protocol /
222
+ sandbox-unavailable) plus the plugin-level `quota-exceeded`; each kind carries a
223
+ retryable/not-retryable verdict; sandbox denials are not code errors — the hint never says
224
+ "fix your code" for permission problems.
225
+ - **Quota (K5)**: ≤64 subcalls, ≤256 KiB output, ≤300 s wall clock (`codeQuota` configurable);
226
+ breaches terminate structurally, not with a hint.
227
+ - **Cancellation (K4)**: the turn signal reaches resolve and bound calls; after abort the
228
+ driver waits for in-flight cleanup before settling; abort and timeout are accounted
229
+ separately.
230
+ - **Result visibility (F3)**: the first successful/changed value is injected in full when
231
+ within the 4000-char cap — saving a tf-peek round trip; over the cap it becomes skeleton +
232
+ handle. The full-echo channel is never restored.
233
+ - Real-machine five scenarios (success / failure / cancel / denial / partial):
234
+ [eval/tools/manual-code-scenarios.md](eval/tools/manual-code-scenarios.md).
235
+
236
+ ## 7. Metrics & evaluation (where the numbers come from)
237
+
238
+ Every tool runs offline with zero API calls; real-machine batches are executed manually
239
+ (propositions in [docs/TEST-PROPOSITIONS.md](docs/TEST-PROPOSITIONS.md), ≈¥2 per task).
240
+
241
+ | Tool | Purpose | Current evidence |
242
+ | --- | --- | --- |
243
+ | `eval/tools/summarize.cjs` | M1 four-component totals (input/cache-read/cache-write/output; missing components marked "unavailable") | Stamp 2026-11-15-v02: baseline 446,410 → plugin 129,178 (28.9%), per-task rows in summary.json |
244
+ | `eval/tools/replay-check.cjs` | M3 replay consistency (replayed tree hash == live tree hash) | Plugin arm **10/10** after fixing two extractor root causes; the native-loop arm's limitation is recorded honestly |
245
+ | `eval/tools/t3-cells.cjs` | M2 T3 three-cell baseline (B / A-1a / A-1b first-request split) | Script + three patch arms ready; the tf-code section measures 1,175 chars; cell numbers await a same-batch real run |
246
+ | `eval/tools/runner.cjs` | R3 eval matrix (two arms × 10 tasks + sentinels) | 0.3.0 rerun pending (new protocol surface) |
247
+ | `eval/tools/f6-kill-drill.cjs` | F6 kill drill (SIGKILL × 10 rounds × three points) | Passed offline: 0 replays, 0 duplicate writes, three-state verdicts |
248
+ | `npm run check` | R2 assembly/doc consistency (4 checks) | Re-runnable with one command |
249
+ | `npm run gate` | R4 release gate (6 items) | Offline items judged in place; real-machine items (G5/G6) marked honestly — no release until green |
250
+
251
+ Discipline: per-task tokens = Σ(input + cacheRead + cacheWrite + output) (four components,
252
+ per-request usage kept verbatim); missing cache components are marked "unavailable", never
253
+ zero-filled; title side-requests are excluded from both arms; the two-component figure is
254
+ reference-only and always labelled.
255
+
256
+ ## 8. Control bar & desktop page
257
+
258
+ - **Load banner**: stderr onboarding panel (takeover state / current mode / first four steps
259
+ / intent hint); `announce: true` prints one mounted line per entry — the plugin proves it
260
+ is loaded.
261
+ - **CLI** (`dsh-stream-agent`):
262
+ - `bar` interactive panel: 1-4 to switch modes, ↑↓ to move, `l` for zh/en, `?` full guide,
263
+ `r` reset, `q` quit;
264
+ - `status` / `mode <balanced|lean|compat|power>` / `lang <zh|en>` / `--json`;
265
+ - `intents` lists per-session "intent without receipt" (F6 triage); `intents --ack
266
+ <session>` closes them after a human check;
267
+ - `help` prints the full mode guide.
268
+ - **Desktop page**: the client sidebar panel shows takeover state, current mode and source,
269
+ six effective parameters, four mode cards (when to choose / trade-offs + saving tier +
270
+ availability), zh/en switch; writes go through the `streamAgentRuntime` service back to
271
+ the loop (in-memory + `control.json` single source of truth) with a multi-window 409
272
+ fence; config/env-locked controls render disabled; read-only fallback when the loop is
273
+ not taken over.
274
+ - **State file**: `<stateDir>/control.json`; precedence `config > env > control.json >
275
+ defaults`; a corrupt file warns and falls back — it never blocks loading.
276
+
277
+ ## 9. Repository layout
278
+
279
+ ```
280
+ dsh-stream-agent/
281
+ ├── src/
282
+ │ ├── kernel/ # parser / render / obs / gate / state / modes / control-bar
283
+ │ │ # (zero deps, no IO, unit-testable in isolation)
284
+ │ └── entry/ # Cordis shell: loop (driver+factory) / ptc-bridge / intent-log /
285
+ │ # json-persistence / state-handle / obs-broker / action-gate /
286
+ │ # context-render / eval-probe / mock-provider / control-bar* /
287
+ │ # cordis-compat / takeover-registry
288
+ ├── ui/ # Desktop control page subpackage (own dsh.client; the main package
289
+ │ # must not declare one)
290
+ ├── dist/ # Build output (25 files, committed, referenced by patches)
291
+ ├── profiles/
292
+ │ ├── stream/ # S1 dedicated profile template (web entry + registry override)
293
+ │ └── stream-headless/ # S1 headless entry variant
294
+ ├── preset/stream/ # S4 preset-stream bundle (mapped tool surface; never in the main patch)
295
+ ├── patches/ # Dev/eval composition patches (takeover / eval-baseline / eval-stream /
296
+ │ # eval-stream-code-on / eval-stream-code-off)
297
+ ├── eval/ # METRICS (definitions) / RESULTS (numbers) / tasks / tools
298
+ │ # (runner / summarize / replay-check / t3-cells / f6 kill drill)
299
+ ├── docs/ # protocol / limitations / architecture / adr-001 /
300
+ │ # disabled-lines-audit / v0.4.0-plan / TEST-PROPOSITIONS
301
+ ├── scripts/ # build / check-consistency / gate / install-check / install-cycle /
302
+ │ # repair-session-logs / release
303
+ └── test/ # 275 cases (2 kernel + 15 entry suites + 20 mock scenarios; zero API)
304
+ ```
305
+
306
+ ## 10. Development
307
+
308
+ ```bash
309
+ npm test # 275/275, zero API calls
310
+ npm run build # emits dist/ (25 files) + smoke imports + desktop-page build gates
311
+ npm run check # R2: assembly & doc consistency (4 checks)
312
+ npm run gate # R4: release gate (6 items; real-machine items marked honestly)
313
+ node eval/tools/f6-kill-drill.cjs --rounds 10 # F6 kill drill (offline, zero API)
314
+ npm run release:dry
315
+ ```
316
+
317
+ ## 11. Docs index
318
+
319
+ | Doc | Content |
320
+ | --- | --- |
321
+ | [docs/protocol.md](docs/protocol.md) | Protocol grammar and closed-loop semantics (11 commands + F5/F6/F7) |
322
+ | [docs/limitations.md](docs/limitations.md) | Known limitations (ordered by "will you hit it", each with evidence) |
323
+ | [docs/adr-001-segment-boundary.md](docs/adr-001-segment-boundary.md) | Segment boundary ADR (whole-block discard vs host compaction) |
324
+ | [docs/disabled-lines-audit.md](docs/disabled-lines-audit.md) | Disabled-lines audit table (S5: why each row is kept/disabled) |
325
+ | [docs/v0.4.0-plan.md](docs/v0.4.0-plan.md) | 0.4.0 plan → implementation mapping (F5–F7 shipped with 0.3.0) |
326
+ | [docs/TEST-PROPOSITIONS.md](docs/TEST-PROPOSITIONS.md) | Manual test propositions & prompts (≈¥2 per task) |
327
+ | [profiles/stream/README.md](profiles/stream/README.md) | Dedicated profile install/select/uninstall/rollback |
328
+ | [eval/RESULTS.md](eval/RESULTS.md) · [eval/METRICS.md](eval/METRICS.md) | Numbers and definitions (all recomputable) |
329
+ | [CONTRIBUTING.md](CONTRIBUTING.md) | Commit discipline (R1: per-version commit, tag, CHANGELOG section) |
330
+
331
+ ## 12. Known limitations (summary)
332
+
333
+ The full list lives in [docs/limitations.md](docs/limitations.md). Highlights:
334
+
335
+ 1. **Write-batch tasks** that exhaust the reentry budget exit with code 1 while the artifacts
336
+ are correct — the guard means "budget exhausted", not "task failed".
337
+ 2. **The tf-code sandbox covers file effects only**: programs can reach the network;
338
+ Windows reports `partial`; `danger-full-access` is unconstrained — verify before use.
339
+ 3. **Segment-boundary cache reset**: the first request after a drop is a cold cache request
340
+ (inherent cost of the whole-block-discard route).
341
+ 4. **The native-loop arm's replay evidence is limited**: write calls in v4 session logs are
342
+ not parseable (marked "not replayable"; checks remain the correctness oracle).
343
+ 5. **Queued tasks + segment boundary**: consecutive `pushUserTask` calls anchor the boundary
344
+ at the last task block; merge related goals into one task text when they need shared
345
+ context.
346
+
347
+ ## License
348
+
349
+ Apache-2.0