dsh-rwkv-plugin 0.0.0-stage → 0.2.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 (92) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -2
  3. package/cordis.patch.yml +59 -0
  4. package/lib/client.js +3148 -0
  5. package/lib/client.js.map +7 -0
  6. package/lib/index.js +6294 -0
  7. package/lib/index.js.map +7 -0
  8. package/lib/line-edit.js +1423 -0
  9. package/lib/line-edit.js.map +7 -0
  10. package/lib/todo.js +610 -0
  11. package/lib/todo.js.map +7 -0
  12. package/package.json +72 -4
  13. package/scripts/build.mjs +77 -0
  14. package/scripts/headless.ts +279 -0
  15. package/scripts/link-dsh.sh +31 -0
  16. package/scripts/relay-e2e.mjs +157 -0
  17. package/scripts/smoke.ts +116 -0
  18. package/scripts/state.ts +39 -0
  19. package/scripts/tokenizer-parity.ts +107 -0
  20. package/scripts/trace-stats.ts +33 -0
  21. package/src/adapter.ts +388 -0
  22. package/src/backend/generator.ts +221 -0
  23. package/src/backend/irwkv.ts +117 -0
  24. package/src/backend/lightning.ts +228 -0
  25. package/src/backend/stream.ts +97 -0
  26. package/src/client/AdapterPicker.tsx +264 -0
  27. package/src/client/DeveloperPanel.tsx +403 -0
  28. package/src/client/FoldNode.tsx +82 -0
  29. package/src/client/ModelCard.tsx +230 -0
  30. package/src/client/RwkvSettings.tsx +265 -0
  31. package/src/client/Segment.tsx +15 -0
  32. package/src/client/StatePicker.tsx +248 -0
  33. package/src/client/TraceFrames.tsx +462 -0
  34. package/src/client/TraceView.tsx +77 -0
  35. package/src/client/WirePanel.tsx +566 -0
  36. package/src/client/fields.tsx +362 -0
  37. package/src/client/index.tsx +109 -0
  38. package/src/client/styles.ts +277 -0
  39. package/src/client/traces-api.ts +222 -0
  40. package/src/client/url.ts +20 -0
  41. package/src/core/agent.ts +586 -0
  42. package/src/core/defaults.ts +51 -0
  43. package/src/core/dsh-prompt.ts +32 -0
  44. package/src/core/edit-modes/index.ts +108 -0
  45. package/src/core/edit-modes/line-edit.ts +793 -0
  46. package/src/core/edit-modes/literal.ts +26 -0
  47. package/src/core/edit-modes/shared.ts +213 -0
  48. package/src/core/edit-modes/tolerant.ts +558 -0
  49. package/src/core/fold-pairs.ts +407 -0
  50. package/src/core/fold-window.ts +563 -0
  51. package/src/core/line-edit.ts +33 -0
  52. package/src/core/parse.ts +290 -0
  53. package/src/core/policy.ts +500 -0
  54. package/src/core/prompt.ts +220 -0
  55. package/src/core/relay.ts +400 -0
  56. package/src/core/step-gates.ts +371 -0
  57. package/src/core/time-zone.ts +32 -0
  58. package/src/core/todo-state.ts +227 -0
  59. package/src/core/tolerant-edit.ts +14 -0
  60. package/src/core/tool-mappings.ts +209 -0
  61. package/src/core/tool-repairs.ts +255 -0
  62. package/src/core/tools.ts +162 -0
  63. package/src/core/worklist.ts +606 -0
  64. package/src/dsh-host.ts +48 -0
  65. package/src/edit-modes/folded-read-guard.ts +32 -0
  66. package/src/edit-modes/index.ts +26 -0
  67. package/src/edit-modes/line-edit.ts +154 -0
  68. package/src/edit-modes/literal.ts +9 -0
  69. package/src/edit-modes/observations.ts +234 -0
  70. package/src/edit-modes/syntax-check.ts +226 -0
  71. package/src/edit-modes/tolerant.ts +232 -0
  72. package/src/folded-read-guard.ts +6 -0
  73. package/src/history.ts +163 -0
  74. package/src/index.ts +287 -0
  75. package/src/line-edit.ts +15 -0
  76. package/src/live-wire.ts +133 -0
  77. package/src/paths.ts +91 -0
  78. package/src/relay-store.ts +59 -0
  79. package/src/result-budget.ts +225 -0
  80. package/src/routes.ts +324 -0
  81. package/src/session-setup.ts +150 -0
  82. package/src/syntax-check.ts +2 -0
  83. package/src/todo.ts +328 -0
  84. package/src/tokenizer.ts +48 -0
  85. package/src/tolerant-edit.ts +2 -0
  86. package/src/trace-stats.ts +225 -0
  87. package/src/traces.ts +289 -0
  88. package/vendor/rwkv-tokenizer/NOTICE.md +26 -0
  89. package/vendor/rwkv-tokenizer/package.json +5 -0
  90. package/vendor/rwkv-tokenizer/rwkv_tokenizer.d.ts +11 -0
  91. package/vendor/rwkv-tokenizer/rwkv_tokenizer.js +280 -0
  92. package/vendor/rwkv-tokenizer/rwkv_tokenizer_bg.wasm +0 -0
@@ -0,0 +1,26 @@
1
+ // Pure specification for `editMode: 'literal'`.
2
+ //
3
+ // Exposes DSH's native `edit` tool (`{ file_path, old_string, new_string, replace_all? }`)
4
+ // directly without recall-tolerant matching or pre-write syntax interception.
5
+ // Used as the stock literal baseline in A/B evaluations.
6
+
7
+ import type { EditMode } from '../defaults.ts'
8
+
9
+ export interface EditModeSpec {
10
+ readonly id: EditMode
11
+ /** Tools hidden from the model catalog when this mode is active. */
12
+ readonly excludedTools: readonly string[]
13
+ /** Whether a dedicated line-editing DSH tool plugin (`edit_lines`) must be mounted. */
14
+ readonly mountsEditLinesPlugin: boolean
15
+ /** Optional maximum lines per `read` result (keeps RWKV-7 inside its <=200-line recall cliff). */
16
+ readonly maxReadLines?: number
17
+ /** Whether the `tools/execute` recall-tolerant `edit` wrapper is enabled. */
18
+ readonly usesTolerantHook: boolean
19
+ }
20
+
21
+ export const LITERAL_EDIT_MODE: EditModeSpec = {
22
+ id: 'literal',
23
+ excludedTools: ['edit_lines'],
24
+ mountsEditLinesPlugin: false,
25
+ usesTolerantHook: false,
26
+ }
@@ -0,0 +1,213 @@
1
+ // Shared coordinate, window rendering, seen-line tracking, and sandbox policy
2
+ // primitives used by coordinate-based line editing (`line-edit`).
3
+ // Pure functions with zero DSH or Node runtime imports.
4
+
5
+ export interface LineEdit {
6
+ /** First line replaced (1-based). */
7
+ readonly startLine: number
8
+ /** Last line replaced; startLine - 1 replaces nothing (an insert before startLine). */
9
+ readonly endLine: number
10
+ /** Optional anchor text of line `startLine` (without line-number prefix) for drift verification & self-healing. */
11
+ readonly startText?: string
12
+ /** Optional anchor text of line `endLine` for multi-line spans. */
13
+ readonly endText?: string
14
+ /** Lines that take their place; empty deletes them. One trailing newline is ignored. */
15
+ readonly newText: string
16
+ }
17
+
18
+ export interface LineEditResult {
19
+ /** The whole new file text, in the file's own line endings. */
20
+ readonly content: string
21
+ /** Lines before and after the edit (LF, no trailing newline). */
22
+ readonly before: readonly string[]
23
+ readonly after: readonly string[]
24
+ readonly removed: number
25
+ readonly added: number
26
+ /** Resolved 1-based line range (may differ from requested if self-healed via `start_text` / `end_text`). */
27
+ readonly resolvedStart?: number
28
+ readonly resolvedEnd?: number
29
+ readonly alignedNote?: string
30
+ readonly anchorVerified?: boolean
31
+ readonly strippedLineNumbers?: boolean
32
+ readonly reindented?: boolean
33
+ }
34
+
35
+ export const pluralLines = (count: number): string => `${count} line${count === 1 ? '' : 's'}`
36
+
37
+ /** Split text into LF lines along with its detected line ending and trailing-newline flag. */
38
+ export function splitDocumentLines(text: string): {
39
+ readonly eol: '\r\n' | '\n'
40
+ readonly trailingNewline: boolean
41
+ readonly lines: readonly string[]
42
+ } {
43
+ const eol = text.includes('\r\n') ? '\r\n' : '\n'
44
+ const normalized = eol === '\r\n' ? text.replace(/\r\n/g, '\n') : text
45
+ const trailingNewline = normalized.endsWith('\n')
46
+ const lines = normalized === '' ? [] : (trailingNewline ? normalized.slice(0, -1) : normalized).split('\n')
47
+ return { eol, trailingNewline, lines }
48
+ }
49
+
50
+ /** Reassemble edited LF lines while preserving the original file's EOL and trailing newline. */
51
+ export function joinDocumentLines(
52
+ after: readonly string[],
53
+ eol: '\r\n' | '\n',
54
+ trailingNewline: boolean,
55
+ wasEmpty: boolean,
56
+ ): string {
57
+ const keepNewline = trailingNewline || (wasEmpty && after.length > 0)
58
+ const joined = after.join('\n') + (keepNewline && after.length > 0 ? '\n' : '')
59
+ return eol === '\r\n' ? joined.replace(/\n/g, '\r\n') : joined
60
+ }
61
+
62
+ /**
63
+ * Render lines `from..to` with neutral headers (`=== path (lines X-Y of Z) ===`).
64
+ * Avoids SWE-agent's `[File: path (N lines total)]` banner which empirically
65
+ * hijacks raw RWKV-7 weights into emitting `str_replace_editor` calls.
66
+ */
67
+ export function renderWindow(displayPath: string, lines: readonly string[], from: number, to: number): string {
68
+ if (lines.length === 0) return `=== ${displayPath} (empty file, 0 lines) ===`
69
+ const first = Math.max(1, Math.min(lines.length, from))
70
+ const last = Math.max(first, Math.min(lines.length, to))
71
+ const out = [`=== ${displayPath} (lines ${first}-${last} of ${lines.length}) ===`]
72
+ if (first > 1) out.push(`(${pluralLines(first - 1)} above)`)
73
+ for (let n = first; n <= last; n++) out.push(`${n}: ${lines[n - 1]}`)
74
+ if (last < lines.length) out.push(`(${pluralLines(lines.length - last)} below)`)
75
+ return out.join('\n')
76
+ }
77
+
78
+ // --- Seen-line window tracking ----------------------------------------------
79
+
80
+ /** Lines (1-based) of the current file version shown to the model; `'all'` when whole or untracked. */
81
+ export type SeenLines = ReadonlySet<number> | 'all'
82
+
83
+ /** Add lines `first..last` (inclusive) to `seen`. */
84
+ export function addSeen(seen: SeenLines, first: number, last: number): SeenLines {
85
+ if (seen === 'all') return seen
86
+ const next = new Set(seen)
87
+ for (let n = Math.max(1, first); n <= last; n++) next.add(n)
88
+ return next
89
+ }
90
+
91
+ /**
92
+ * Lines the edit needs the model to have seen that it has not: the replaced
93
+ * lines, or for an insert a neighbour (the line before or after the gap).
94
+ */
95
+ export function unseenLines(
96
+ seen: SeenLines,
97
+ edit: { readonly startLine: number; readonly endLine: number; readonly newText?: string },
98
+ total: number,
99
+ ): number[] {
100
+ if (seen === 'all') return []
101
+ const { startLine: start, endLine: end } = edit
102
+ if (end >= start) {
103
+ const missing: number[] = []
104
+ for (let n = start; n <= end; n++) if (!seen.has(n)) missing.push(n)
105
+ return missing
106
+ }
107
+ const neighbours = [start - 1, start].filter(n => n >= 1 && n <= total)
108
+ return neighbours.length === 0 || neighbours.some(n => seen.has(n)) ? [] : neighbours
109
+ }
110
+
111
+ /** Lines seen after a single edit: old ones renumbered, plus the new lines and result window. */
112
+ export function seenAfterEdit(
113
+ seen: SeenLines,
114
+ edit: { readonly startLine: number; readonly endLine: number; readonly newText?: string },
115
+ result: Pick<LineEditResult, 'added' | 'removed' | 'after'>,
116
+ contextLines = 3,
117
+ ): SeenLines {
118
+ if (seen === 'all') return seen
119
+ const { startLine: start, endLine: end } = edit
120
+ const delta = result.added - result.removed
121
+ const next = new Set<number>()
122
+ for (const n of seen) {
123
+ if (n < start) next.add(n)
124
+ else if (n > end) next.add(n + delta)
125
+ }
126
+ const total = result.after.length
127
+ const from = Math.max(1, start - contextLines)
128
+ const to = Math.min(total, start + Math.max(result.added, 1) - 1 + contextLines)
129
+ for (let n = from; n <= to; n++) next.add(n)
130
+ return next
131
+ }
132
+
133
+ /** Lines seen after an atomic multi-hunk edit (hunks given in pre-edit ascending order). */
134
+ export function seenAfterBatchEdit(
135
+ seen: SeenLines,
136
+ hunks: readonly { readonly startLine: number; readonly endLine: number; readonly removed: number; readonly added: number }[],
137
+ afterTotal: number,
138
+ contextLines = 3,
139
+ ): SeenLines {
140
+ if (seen === 'all') return seen
141
+ const ordered = [...hunks].sort((a, b) => a.startLine - b.startLine)
142
+ // Remap existing seen line numbers across all pre-edit hunks in one pass.
143
+ const next = new Set<number>()
144
+ for (const n of seen) {
145
+ let shift = 0
146
+ let overwritten = false
147
+ for (const h of ordered) {
148
+ if (h.endLine >= h.startLine && n >= h.startLine && n <= h.endLine) {
149
+ overwritten = true
150
+ break
151
+ }
152
+ if (n > h.endLine || (h.endLine < h.startLine && n >= h.startLine)) {
153
+ shift += h.added - h.removed
154
+ }
155
+ }
156
+ if (!overwritten && n + shift >= 1 && n + shift <= afterTotal) {
157
+ next.add(n + shift)
158
+ }
159
+ }
160
+ // Add every edited hunk's post-edit lines plus ±contextLines window.
161
+ let cumulativeDelta = 0
162
+ for (const h of ordered) {
163
+ const postStart = h.startLine + cumulativeDelta
164
+ const from = Math.max(1, postStart - contextLines)
165
+ const to = Math.min(afterTotal, postStart + Math.max(h.added, 1) - 1 + contextLines)
166
+ for (let n = from; n <= to; n++) next.add(n)
167
+ cumulativeDelta += h.added - h.removed
168
+ }
169
+ return next
170
+ }
171
+
172
+ /** Format a set of 1-based line numbers into compact `"1-150, 200-230"` ranges. */
173
+ export function formatRanges(lines: Iterable<number>): string {
174
+ const sorted = [...lines].sort((a, b) => a - b)
175
+ const parts: string[] = []
176
+ for (let i = 0; i < sorted.length;) {
177
+ let j = i
178
+ while (j + 1 < sorted.length && sorted[j + 1] === sorted[j]! + 1) j++
179
+ parts.push(sorted[i] === sorted[j] ? `${sorted[i]}` : `${sorted[i]}-${sorted[j]}`)
180
+ i = j + 1
181
+ }
182
+ return parts.join(', ')
183
+ }
184
+
185
+ /** Model-facing refusal of an edit that touches lines outside the model's read window. */
186
+ export function unseenRejection(
187
+ displayPath: string,
188
+ missing: readonly number[],
189
+ seen: ReadonlySet<number>,
190
+ total: number,
191
+ contextLines = 3,
192
+ ): string {
193
+ const offset = Math.max(1, missing[0]! - contextLines)
194
+ const shown = seen.size === 0 ? 'you have seen none of it' : `you have seen lines ${formatRanges(seen)}`
195
+ const which = missing.length === 1 ? `line ${missing[0]} of ${displayPath} has` : `lines ${formatRanges(missing)} of ${displayPath} have`
196
+ return `Your edit was NOT applied: ${which} not been shown to you `
197
+ + `(the file has ${total} lines; ${shown}). `
198
+ + `Read them first (read with offset=${offset}), then edit by the numbers you see.`
199
+ }
200
+
201
+ // --- Sandbox policy helper --------------------------------------------------
202
+
203
+ /**
204
+ * Resolve the calling session's sandbox policy (never the deployment cwd
205
+ * fallback) before mutating via `ctx.fs`.
206
+ */
207
+ export function sandboxPolicyFor(
208
+ service: { resolve(request?: { session?: object }): unknown } | undefined,
209
+ session: object | undefined,
210
+ ): unknown {
211
+ if (service === undefined) return undefined
212
+ return service.resolve(session === undefined ? {} : { session })
213
+ }