dsh-ris-ide 1.7.3 → 1.8.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.
- package/README.md +21 -7
- package/bin/proxy-janitor.mjs +106 -11
- package/bin/risd.mjs +284 -0
- package/bin/run-with-log.ps1 +43 -5
- package/lib/client.js +609 -1178
- package/lib/index.js +1124 -55
- package/lib/janitor.js +106 -15
- package/lib/proxy.js +86 -7
- package/lib/proxybridge-bundle.js +798 -0
- package/lib/proxybridge-health.js +206 -0
- package/lib/redirector.js +1615 -115
- package/lib/risd/build.js +91 -0
- package/lib/risd/debug.js +374 -0
- package/lib/risd/git.js +207 -0
- package/lib/risd/leases.js +124 -0
- package/lib/risd/lifecycle.js +148 -0
- package/lib/risd/log.js +59 -0
- package/lib/risd/ping.js +94 -0
- package/lib/risd/programs.js +348 -0
- package/lib/risd/server.js +1919 -0
- package/lib/risd/state.js +137 -0
- package/lib/risd/ui.js +1989 -0
- package/lib/risd/view.js +79 -0
- package/lib/risd-client.js +282 -0
- package/lib/standalone/window.html +14 -16
- package/lib/startup.js +114 -0
- package/package.json +4 -1
- package/test/client-build.test.mjs +113 -0
- package/test/client-viewer.test.mjs +279 -227
- package/test/dsh-origin.test.mjs +140 -0
- package/test/dsh-window-refresh.test.mjs +100 -0
- package/test/harness.test.mjs +9 -0
- package/test/host-noloop.test.mjs +74 -0
- package/test/host-tail.test.mjs +21 -4
- package/test/host-window.test.mjs +4 -4
- package/test/janitor.test.mjs +29 -1
- package/test/lifecycle.test.mjs +26 -5
- package/test/merge-owners.test.mjs +155 -0
- package/test/proxy-restart.test.mjs +39 -0
- package/test/proxy-window.test.mjs +45 -0
- package/test/proxy.test.mjs +41 -0
- package/test/proxybridge-bundle.test.mjs +687 -0
- package/test/proxybridge-health.test.mjs +116 -0
- package/test/redirector-bundle.test.mjs +391 -0
- package/test/redirector.test.mjs +413 -10
- package/test/risd-api.test.mjs +514 -0
- package/test/risd-bridge-degrade.test.mjs +101 -0
- package/test/risd-build.test.mjs +20 -0
- package/test/risd-cli-home.test.mjs +50 -0
- package/test/risd-client.test.mjs +130 -0
- package/test/risd-debug.test.mjs +261 -0
- package/test/risd-election.test.mjs +104 -0
- package/test/risd-engine.test.mjs +132 -0
- package/test/risd-entry.test.mjs +118 -0
- package/test/risd-git.test.mjs +109 -0
- package/test/risd-handoff.test.mjs +116 -0
- package/test/risd-heal.test.mjs +50 -0
- package/test/risd-helpers.mjs +108 -0
- package/test/risd-host-p2.test.mjs +147 -0
- package/test/risd-janitor-guard.test.mjs +46 -0
- package/test/risd-lease.test.mjs +83 -0
- package/test/risd-lifecycle.test.mjs +210 -0
- package/test/risd-log-follow.test.mjs +227 -0
- package/test/risd-mode.test.mjs +161 -0
- package/test/risd-ping.test.mjs +99 -0
- package/test/risd-programs.test.mjs +169 -0
- package/test/risd-restart.test.mjs +104 -0
- package/test/risd-review-p2.test.mjs +217 -0
- package/test/risd-shutdown.test.mjs +97 -0
- package/test/risd-teardown.test.mjs +87 -0
- package/test/risd-tools.test.mjs +211 -0
- package/test/risd-ui.test.mjs +607 -0
- package/test/risd-write.test.mjs +170 -0
- package/test/startup-reconcile.test.mjs +100 -0
- package/test/windows.test.mjs +37 -2
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regressions for the P2 findings of the 2026-11-09 expert review that live on
|
|
3
|
+
* the daemon's HTTP surface (A report) plus the client/log helpers.
|
|
4
|
+
*
|
|
5
|
+
* Each case names the finding it pins (A-P2-x) so a future reader can trace it
|
|
6
|
+
* back to work/reviews/20261109-risd/A-daemon-core.md.
|
|
7
|
+
*/
|
|
8
|
+
import assert from 'node:assert/strict'
|
|
9
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { request } from 'node:http'
|
|
11
|
+
import { tmpdir } from 'node:os'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
import test, { after } from 'node:test'
|
|
14
|
+
import { looksAbsolutePath, startRisd } from '../lib/risd/server.js'
|
|
15
|
+
import { createRisdLogger, risdLogPath } from '../lib/risd/log.js'
|
|
16
|
+
import { reconcileStartupLeftovers } from '../lib/startup.js'
|
|
17
|
+
import { ensureRisd } from '../lib/risd-client.js'
|
|
18
|
+
import { RISD_API_VERSION, risdStatePath, writeRisdState } from '../lib/risd/state.js'
|
|
19
|
+
import { makeHome, removeHome, runRisdCli, sleep, startRisdProcess } from './risd-helpers.mjs'
|
|
20
|
+
import { pidAlive } from '../lib/proxy.js'
|
|
21
|
+
|
|
22
|
+
const homes = []
|
|
23
|
+
function home() {
|
|
24
|
+
const dir = makeHome('risd-p2-')
|
|
25
|
+
homes.push(dir)
|
|
26
|
+
return dir
|
|
27
|
+
}
|
|
28
|
+
after(() => {
|
|
29
|
+
for (const dir of homes) removeHome(dir)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
function httpJson(port, path, { method = 'GET', body = null, raw = null, token = null } = {}) {
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
const payload = raw !== null ? raw : body === null ? null : JSON.stringify(body)
|
|
35
|
+
const req = request({
|
|
36
|
+
host: '127.0.0.1',
|
|
37
|
+
port,
|
|
38
|
+
path,
|
|
39
|
+
method,
|
|
40
|
+
headers: {
|
|
41
|
+
...(payload === null ? {} : { 'content-type': 'application/json', 'content-length': Buffer.byteLength(payload) }),
|
|
42
|
+
...(token === null ? {} : { authorization: `Bearer ${token}`, 'x-risd-api-version': String(RISD_API_VERSION) }),
|
|
43
|
+
},
|
|
44
|
+
}, (res) => {
|
|
45
|
+
const chunks = []
|
|
46
|
+
res.on('data', (chunk) => chunks.push(chunk))
|
|
47
|
+
res.on('end', () => {
|
|
48
|
+
const text = Buffer.concat(chunks).toString('utf8')
|
|
49
|
+
let parsed = null
|
|
50
|
+
try { parsed = JSON.parse(text) } catch { /* not json */ }
|
|
51
|
+
resolve({ status: res.statusCode, body: parsed, text })
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
req.on('error', (error) => resolve({ status: 0, body: null, text: '', error: String(error.message) }))
|
|
55
|
+
if (payload !== null) req.write(payload)
|
|
56
|
+
req.end()
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
test('absolute paths are recognised on both platforms (A-P1-3)', () => {
|
|
61
|
+
assert.equal(looksAbsolutePath('/tmp/demo'), true)
|
|
62
|
+
assert.equal(looksAbsolutePath('C:\\work\\demo'), true)
|
|
63
|
+
assert.equal(looksAbsolutePath('\\\\server\\share\\demo'), true)
|
|
64
|
+
assert.equal(looksAbsolutePath('relative/demo'), false)
|
|
65
|
+
assert.equal(looksAbsolutePath(''), false)
|
|
66
|
+
assert.equal(looksAbsolutePath(null), false)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('an oversized body gets a 413 answer instead of a broken socket (A-P2-6)', async () => {
|
|
70
|
+
const started = await startRisdProcess({ home: home() })
|
|
71
|
+
try {
|
|
72
|
+
const huge = JSON.stringify({ action: 'start', cwd: '/tmp', pad: 'x'.repeat(70 * 1024) })
|
|
73
|
+
const response = await httpJson(started.state.port, '/v1/leases', {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
raw: huge,
|
|
76
|
+
token: started.state.token,
|
|
77
|
+
})
|
|
78
|
+
assert.equal(response.status, 413)
|
|
79
|
+
assert.equal(response.body?.code, 'too-large')
|
|
80
|
+
} finally {
|
|
81
|
+
await started.stop()
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('/v1/config reports real values only (A-P2-3)', async () => {
|
|
86
|
+
const started = await startRisdProcess({ home: home() })
|
|
87
|
+
try {
|
|
88
|
+
const response = await httpJson(started.state.port, '/v1/config', { token: started.state.token })
|
|
89
|
+
assert.equal(response.status, 200)
|
|
90
|
+
assert.equal(response.body.idleExitMs, 10 * 60 * 1000)
|
|
91
|
+
assert.equal(response.body.engineRestart, undefined)
|
|
92
|
+
assert.ok(response.body.lifecycle)
|
|
93
|
+
} finally {
|
|
94
|
+
await started.stop()
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('/v1/dsh ignores windows whose url is not a loopback http(s) origin (A-P2-10)', async () => {
|
|
99
|
+
const dir = home()
|
|
100
|
+
const started = await startRisdProcess({ home: dir })
|
|
101
|
+
try {
|
|
102
|
+
writeFileSync(join(dir, 'dsh-window.json'), JSON.stringify({
|
|
103
|
+
windows: [
|
|
104
|
+
{ url: 'https://evil.example.com', sessionId: 's-evil', cwd: '/tmp/evil', at: new Date().toISOString() },
|
|
105
|
+
{ url: `http://127.0.0.1:${started.state.port}`, sessionId: 's-ok', cwd: '/tmp/demo-workspace', at: new Date().toISOString() },
|
|
106
|
+
],
|
|
107
|
+
}), { mode: 0o600 })
|
|
108
|
+
const response = await httpJson(started.state.port, '/v1/dsh', { token: started.state.token })
|
|
109
|
+
assert.equal(response.status, 200)
|
|
110
|
+
assert.deepEqual(response.body.windows.map((row) => row.sessionId), ['s-ok'])
|
|
111
|
+
} finally {
|
|
112
|
+
await started.stop()
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('a conflicting write reports the current view (A-P2-1)', async () => {
|
|
117
|
+
const dir = home()
|
|
118
|
+
const started = await startRisdProcess({ home: dir })
|
|
119
|
+
try {
|
|
120
|
+
const auth = { token: started.state.token }
|
|
121
|
+
const current = await httpJson(started.state.port, '/v1/proxy', auth)
|
|
122
|
+
assert.equal(current.status, 200)
|
|
123
|
+
const stale = await httpJson(started.state.port, '/v1/proxy', {
|
|
124
|
+
...auth,
|
|
125
|
+
method: 'POST',
|
|
126
|
+
body: {
|
|
127
|
+
cwd: '/tmp',
|
|
128
|
+
revision: 'definitely-stale',
|
|
129
|
+
config: { socks: { host: '127.0.0.1', port: 1 }, listenPort: 11081, rules: [] },
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
assert.equal(stale.status, 409)
|
|
133
|
+
assert.equal(stale.body?.conflict, true)
|
|
134
|
+
assert.ok(stale.body?.view, 'the 409 must carry a fresh view')
|
|
135
|
+
assert.equal(typeof stale.body.view.revision, 'string')
|
|
136
|
+
} finally {
|
|
137
|
+
await started.stop()
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('the log creates its home directory (A-P2-12)', () => {
|
|
142
|
+
const base = home()
|
|
143
|
+
const missing = join(base, 'nested', 'home')
|
|
144
|
+
const logger = createRisdLogger(missing)
|
|
145
|
+
logger.info('hello')
|
|
146
|
+
assert.equal(existsSync(risdLogPath(missing)), true)
|
|
147
|
+
assert.match(readFileSync(risdLogPath(missing), 'utf8'), /hello/)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('startup reconcile keeps the state file of the daemon asking (A-P1-5)', async () => {
|
|
151
|
+
const dir = home()
|
|
152
|
+
const previousHome = process.env.DSH_RIS_IDE_PROXY_HOME
|
|
153
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = dir
|
|
154
|
+
try {
|
|
155
|
+
const port = 55999
|
|
156
|
+
writeRisdState({ pid: process.pid, port, token: 'x'.repeat(64), apiVersion: RISD_API_VERSION, version: 'test', startedAt: new Date().toISOString() }, dir)
|
|
157
|
+
await reconcileStartupLeftovers({ ownPid: process.pid })
|
|
158
|
+
assert.equal(existsSync(risdStatePath(dir)), true, 'own risd.json must survive')
|
|
159
|
+
await reconcileStartupLeftovers({ ownPid: 1 })
|
|
160
|
+
assert.equal(existsSync(risdStatePath(dir)), false, 'a stale risd.json from another pid is removed')
|
|
161
|
+
} finally {
|
|
162
|
+
if (previousHome === undefined) delete process.env.DSH_RIS_IDE_PROXY_HOME
|
|
163
|
+
else process.env.DSH_RIS_IDE_PROXY_HOME = previousHome
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('a daemon that exits early reports why (A-P2-13)', async () => {
|
|
168
|
+
const dir = home()
|
|
169
|
+
const fake = join(mkdtempSync(join(tmpdir(), 'risd-fake-')), 'risd.mjs')
|
|
170
|
+
writeFileSync(fake, "process.stderr.write('port busy\\n'); process.exit(4)\n")
|
|
171
|
+
const ensured = await ensureRisd({ home: dir, spawnPath: fake, timeoutMs: 2000 })
|
|
172
|
+
assert.equal(ensured.ok, false)
|
|
173
|
+
assert.equal(ensured.reason, 'port-busy')
|
|
174
|
+
assert.equal(ensured.exitCode, 4)
|
|
175
|
+
assert.match(String(ensured.error), /port busy/)
|
|
176
|
+
rmSync(join(fake, '..'), { recursive: true, force: true })
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a second home on the same port is refused, not silently shadowed (A-P2-9)', async () => {
|
|
180
|
+
const started = await startRisdProcess({ home: home() })
|
|
181
|
+
try {
|
|
182
|
+
const other = home()
|
|
183
|
+
const second = await startRisd({ home: other, port: started.state.port, version: 'test' })
|
|
184
|
+
assert.equal(second.ok, false)
|
|
185
|
+
assert.equal(second.code, 'home-mismatch')
|
|
186
|
+
assert.equal(second.found, started.home)
|
|
187
|
+
assert.equal(second.home, other)
|
|
188
|
+
} finally {
|
|
189
|
+
await started.stop()
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test('risd stop waits for the pid, not for the state file (A-P2-7)', async () => {
|
|
194
|
+
const dir = home()
|
|
195
|
+
const started = await startRisdProcess({ home: dir })
|
|
196
|
+
const pid = started.state.pid
|
|
197
|
+
const result = await runRisdCli(['stop', '--home', dir])
|
|
198
|
+
assert.equal(result.code, 0, result.out + result.err)
|
|
199
|
+
assert.match(result.out, /risd: stopped/)
|
|
200
|
+
await sleep(50)
|
|
201
|
+
assert.equal(pidAlive(pid), false)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('/ui is served with frame, sniff and CSP protection (C-P2-5)', async () => {
|
|
205
|
+
const started = await startRisdProcess({ home: home() })
|
|
206
|
+
try {
|
|
207
|
+
const response = await fetch(`http://127.0.0.1:${started.state.port}/ui`)
|
|
208
|
+
assert.equal(response.status, 200)
|
|
209
|
+
assert.equal(response.headers.get('x-frame-options'), 'DENY')
|
|
210
|
+
assert.equal(response.headers.get('x-content-type-options'), 'nosniff')
|
|
211
|
+
const csp = response.headers.get('content-security-policy') ?? ''
|
|
212
|
+
assert.match(csp, /frame-ancestors 'none'/)
|
|
213
|
+
assert.match(csp, /frame-src http:\/\/127\.0\.0\.1:\*/)
|
|
214
|
+
} finally {
|
|
215
|
+
await started.stop()
|
|
216
|
+
}
|
|
217
|
+
})
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// "Stop the daemon" must stop what the daemon supervises.
|
|
2
|
+
//
|
|
3
|
+
// The maintenance page is the only UI when no dsh is open: if stopping the daemon
|
|
4
|
+
// left JVMs (or the proxy engine behind them) running with nothing to stop them,
|
|
5
|
+
// the only remedy would be `kill`. A handoff (upgrade) is different and is covered
|
|
6
|
+
// by risd-handoff.test.mjs — it must keep them alive.
|
|
7
|
+
import assert from 'node:assert/strict'
|
|
8
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { request } from 'node:http'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
import { after, before, test } from 'node:test'
|
|
13
|
+
import { sleep, startRisdProcess } from './risd-helpers.mjs'
|
|
14
|
+
|
|
15
|
+
let daemon
|
|
16
|
+
let workspace
|
|
17
|
+
|
|
18
|
+
function api(state, path, { method = 'GET', body } = {}) {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
const payload = body === undefined ? '' : JSON.stringify(body)
|
|
21
|
+
const req = request({
|
|
22
|
+
host: '127.0.0.1',
|
|
23
|
+
port: state.port,
|
|
24
|
+
path,
|
|
25
|
+
method,
|
|
26
|
+
timeout: 8000,
|
|
27
|
+
headers: Object.assign(
|
|
28
|
+
{ authorization: `Bearer ${state.token}`, 'x-risd-api-version': '1' },
|
|
29
|
+
payload === '' ? {} : { 'content-type': 'application/json', 'content-length': Buffer.byteLength(payload) },
|
|
30
|
+
),
|
|
31
|
+
}, (res) => {
|
|
32
|
+
let text = ''
|
|
33
|
+
res.setEncoding('utf8')
|
|
34
|
+
res.on('data', (chunk) => { text += chunk })
|
|
35
|
+
res.on('end', () => {
|
|
36
|
+
let parsed = null
|
|
37
|
+
try { parsed = JSON.parse(text) } catch { parsed = null }
|
|
38
|
+
resolve({ status: res.statusCode, body: parsed })
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
req.on('error', (error) => resolve({ status: 0, body: { error: String(error?.message) } }))
|
|
42
|
+
if (payload !== '') req.write(payload)
|
|
43
|
+
req.end()
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function alive(pid) {
|
|
48
|
+
try { process.kill(pid, 0); return true } catch { return false }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
before(async () => {
|
|
52
|
+
workspace = mkdtempSync(join(tmpdir(), 'risd-shutdown-ws-'))
|
|
53
|
+
mkdirSync(join(workspace, '.dsh-ris-ide'), { recursive: true })
|
|
54
|
+
writeFileSync(join(workspace, '.dsh-ris-ide', 'config.json'), `${JSON.stringify({
|
|
55
|
+
version: 1,
|
|
56
|
+
defaults: {},
|
|
57
|
+
programs: [{
|
|
58
|
+
id: 'demo',
|
|
59
|
+
name: 'demo',
|
|
60
|
+
kind: 'custom',
|
|
61
|
+
dir: workspace,
|
|
62
|
+
relDir: '.',
|
|
63
|
+
project: '',
|
|
64
|
+
logName: 'demo',
|
|
65
|
+
startCommand: `${JSON.stringify(process.execPath)} -e "setInterval(() => {}, 60000)"`,
|
|
66
|
+
stopGraceMs: 1500,
|
|
67
|
+
}],
|
|
68
|
+
}, null, 2)}\n`, 'utf8')
|
|
69
|
+
daemon = await startRisdProcess({ port: 0 })
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
after(async () => {
|
|
73
|
+
if (daemon !== undefined) await daemon.stop()
|
|
74
|
+
if (workspace !== undefined) rmSync(workspace, { recursive: true, force: true })
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('shutdown stops the programs it supervises before exiting', async () => {
|
|
78
|
+
const started = await api(daemon.state, '/v1/programs', {
|
|
79
|
+
method: 'POST',
|
|
80
|
+
body: { cwd: workspace, action: 'start', id: 'demo' },
|
|
81
|
+
})
|
|
82
|
+
assert.equal(started.status, 200, JSON.stringify(started.body))
|
|
83
|
+
const pid = started.body.pid
|
|
84
|
+
assert.ok(Number.isInteger(pid) && pid > 0, 'the start result carries the wrapper pid')
|
|
85
|
+
assert.equal(alive(pid), true, 'the program is running')
|
|
86
|
+
|
|
87
|
+
const shutdown = await api(daemon.state, '/v1/shutdown', { method: 'POST', body: { reason: 'test' } })
|
|
88
|
+
assert.equal(shutdown.status, 200)
|
|
89
|
+
|
|
90
|
+
// Give the wrapper's TERM grace a moment, then require the process to be gone.
|
|
91
|
+
let gone = false
|
|
92
|
+
for (let attempt = 0; attempt < 60; attempt += 1) {
|
|
93
|
+
if (!alive(pid)) { gone = true; break }
|
|
94
|
+
await sleep(200)
|
|
95
|
+
}
|
|
96
|
+
assert.equal(gone, true, 'stopping the daemon must not leave the program behind')
|
|
97
|
+
})
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session policy: the daemon owns the engine, so it also decides when the
|
|
3
|
+
* network has no consumer left. Last lease gone ⇒ stop the engine (P2 scope:
|
|
4
|
+
* engine only — git and the bridge move in P5).
|
|
5
|
+
*/
|
|
6
|
+
import assert from 'node:assert/strict'
|
|
7
|
+
import test from 'node:test'
|
|
8
|
+
import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { tmpdir } from 'node:os'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
import { RISD_API_VERSION } from '../lib/risd/state.js'
|
|
12
|
+
import { makeHome, removeHome, sleep, startRisdProcess } from './risd-helpers.mjs'
|
|
13
|
+
|
|
14
|
+
function headers(state) {
|
|
15
|
+
return { authorization: `Bearer ${state.token}`, 'x-risd-api-version': String(RISD_API_VERSION), 'content-type': 'application/json' }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Teardown is 300ms debounce + engine stop (5s grace) + bridge/git attempts; on a
|
|
19
|
+
// loaded machine (full suite, four files in parallel) that can exceed 8s even when
|
|
20
|
+
// the behaviour is correct, so the waits are generous on purpose.
|
|
21
|
+
async function waitFor(fn, timeoutMs = 15000) {
|
|
22
|
+
const deadline = Date.now() + timeoutMs
|
|
23
|
+
while (Date.now() < deadline) {
|
|
24
|
+
if (await fn()) return true
|
|
25
|
+
await sleep(120)
|
|
26
|
+
}
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function startFixture(t) {
|
|
31
|
+
const home = makeHome('risd-teardown-')
|
|
32
|
+
const cwd = mkdtempSync(join(tmpdir(), 'risd-teardown-ws-'))
|
|
33
|
+
const port = 20000 + Math.floor(Math.random() * 20000)
|
|
34
|
+
writeFileSync(join(home, 'proxy.json'), JSON.stringify({
|
|
35
|
+
socks: { host: '127.0.0.1', port: 1, dbPort: 0, redisPort: 0 },
|
|
36
|
+
listenPort: port,
|
|
37
|
+
rules: [{ id: 'rule-1', program: 'java', hosts: ['127.0.0.1'], ports: ['443'], enabled: true, shadow: false, source: 'user', workspace: cwd }],
|
|
38
|
+
}))
|
|
39
|
+
const daemon = await startRisdProcess({ home, env: { DSH_RIS_IDE_PROXY_HOME: home } })
|
|
40
|
+
t.after(async () => {
|
|
41
|
+
await daemon.stop()
|
|
42
|
+
removeHome(home)
|
|
43
|
+
rmSync(cwd, { recursive: true, force: true })
|
|
44
|
+
})
|
|
45
|
+
const h = headers(daemon.state)
|
|
46
|
+
const startEngine = async () => {
|
|
47
|
+
const res = await fetch(`${daemon.state.url}/v1/engine`, { method: 'POST', headers: h, body: JSON.stringify({ action: 'start', cwd }) })
|
|
48
|
+
return res.json()
|
|
49
|
+
}
|
|
50
|
+
const lease = async (sessionId) => (await (await fetch(`${daemon.state.url}/v1/leases`, { method: 'POST', headers: h, body: JSON.stringify({ ownerPid: process.pid, sessionId, cwd }) })).json()).lease
|
|
51
|
+
const release = async (id) => (await (await fetch(`${daemon.state.url}/v1/leases/${encodeURIComponent(id)}`, { method: 'DELETE', headers: h })).json()).released
|
|
52
|
+
const engine = async () => (await (await fetch(`${daemon.state.url}/v1/engine?cwd=${encodeURIComponent(cwd)}`, { headers: h })).json()).engine
|
|
53
|
+
return { daemon, home, cwd, startEngine, lease, release, engine }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
test('the last release stops the engine, the first one does not', async (t) => {
|
|
57
|
+
const fx = await startFixture(t)
|
|
58
|
+
const started = await fx.startEngine()
|
|
59
|
+
assert.equal(started.result.ok, true, JSON.stringify(started.result))
|
|
60
|
+
const pid = started.result.pid
|
|
61
|
+
|
|
62
|
+
const a = await fx.lease('A')
|
|
63
|
+
const b = await fx.lease('B')
|
|
64
|
+
assert.equal(await fx.release(a.id), true)
|
|
65
|
+
await sleep(600)
|
|
66
|
+
assert.equal((await fx.engine()).running, true, 'one lease left: the engine must stay up')
|
|
67
|
+
|
|
68
|
+
assert.equal(await fx.release(b.id), true)
|
|
69
|
+
assert.equal(await waitFor(async () => (await fx.engine()).running === false), true, 'last lease gone: the engine stops')
|
|
70
|
+
assert.equal(await waitFor(() => !existsSync(join(fx.home, 'proxy.pid'))), true)
|
|
71
|
+
assert.equal((await fx.engine()).pid, null)
|
|
72
|
+
assert.equal(pid > 0, true)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('a dead owner is swept and then the engine is torn down', async (t) => {
|
|
76
|
+
const fx = await startFixture(t)
|
|
77
|
+
const started = await fx.startEngine()
|
|
78
|
+
assert.equal(started.result.ok, true)
|
|
79
|
+
|
|
80
|
+
const orphan = (await import('node:child_process')).spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' })
|
|
81
|
+
t.after(() => orphan.kill('SIGKILL'))
|
|
82
|
+
const h = headers(fx.daemon.state)
|
|
83
|
+
await fetch(`${fx.daemon.state.url}/v1/leases`, { method: 'POST', headers: h, body: JSON.stringify({ ownerPid: orphan.pid, sessionId: 'gone', cwd: fx.cwd }) })
|
|
84
|
+
orphan.kill('SIGKILL')
|
|
85
|
+
|
|
86
|
+
assert.equal(await waitFor(async () => (await fx.engine()).running === false, 15000), true, 'swept lease must trigger teardown')
|
|
87
|
+
})
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// P4: the agent tools ask risd first when the plugin runs in daemonMode=auto.
|
|
2
|
+
//
|
|
3
|
+
// These tests never boot a real daemon: a tiny HTTP server records what the
|
|
4
|
+
// tools ask for, __test.setRisdBridge() points the plugin at it, and the local
|
|
5
|
+
// branch stays available as the fallback (checked by closing the server).
|
|
6
|
+
import test from 'node:test'
|
|
7
|
+
import assert from 'node:assert/strict'
|
|
8
|
+
import { createServer } from 'node:http'
|
|
9
|
+
import { mkdtempSync, rmSync } from 'node:fs'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
|
|
13
|
+
import { registerAgentTools, __test } from '../lib/index.js'
|
|
14
|
+
|
|
15
|
+
const ROW = {
|
|
16
|
+
id: 'demo',
|
|
17
|
+
name: 'demo',
|
|
18
|
+
kind: 'spring',
|
|
19
|
+
logName: 'demo',
|
|
20
|
+
dir: '.',
|
|
21
|
+
relDir: '.',
|
|
22
|
+
logPath: '/tmp/risd-tools.test/demo.log',
|
|
23
|
+
running: { pid: 11, alive: true, uptimeMs: 1000 },
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fakeDaemon() {
|
|
27
|
+
const requests = []
|
|
28
|
+
const server = createServer((req, res) => {
|
|
29
|
+
const chunks = []
|
|
30
|
+
req.on('data', (chunk) => chunks.push(chunk))
|
|
31
|
+
req.on('end', () => {
|
|
32
|
+
const raw = Buffer.concat(chunks).toString('utf8')
|
|
33
|
+
const body = raw === '' ? undefined : JSON.parse(raw)
|
|
34
|
+
requests.push({ method: req.method, url: req.url, body })
|
|
35
|
+
const json = (value) => {
|
|
36
|
+
res.writeHead(200, { 'content-type': 'application/json' })
|
|
37
|
+
res.end(JSON.stringify(value))
|
|
38
|
+
}
|
|
39
|
+
const url = String(req.url ?? '')
|
|
40
|
+
// C-P2-2: the tools probe /v1/health once per window before trusting the
|
|
41
|
+
// bridge's daemon pointer, so the fake daemon has to answer it like the real one.
|
|
42
|
+
if (req.method === 'GET' && url === '/v1/health') {
|
|
43
|
+
return json({ ok: true, apiVersion: 1, version: '1.8.0', pid: 4242, port: server.address().port, state: 'ready' })
|
|
44
|
+
}
|
|
45
|
+
if (req.method === 'GET' && url.startsWith('/v1/programs/log')) {
|
|
46
|
+
return json({ ok: true, path: ROW.logPath, exists: true, running: true, pid: 11, text: 'line1\nline2\n' })
|
|
47
|
+
}
|
|
48
|
+
if (req.method === 'GET' && url.startsWith('/v1/programs')) {
|
|
49
|
+
return json({ ok: true, cwd: '/tmp/ws', dir: 'logs', programs: [ROW] })
|
|
50
|
+
}
|
|
51
|
+
if (req.method === 'POST' && url === '/v1/programs') {
|
|
52
|
+
return json({ ok: true, cwd: '/tmp/ws', action: body?.action, alreadyRunning: false, program: ROW })
|
|
53
|
+
}
|
|
54
|
+
if (req.method === 'POST' && url === '/v1/debug') {
|
|
55
|
+
if (body?.action === 'status') return json({ ok: true, action: 'status', attached: false, port: 0 })
|
|
56
|
+
if (body?.action === 'breakpoints') return json({ ok: true, breakpoints: [{ id: 1, className: 'A', line: 12 }] })
|
|
57
|
+
if (body?.action === 'threads') return json({ ok: true, threads: [{ id: 't1', name: 'main' }] })
|
|
58
|
+
return json({ ok: true, action: body?.action })
|
|
59
|
+
}
|
|
60
|
+
res.writeHead(404, { 'content-type': 'application/json' })
|
|
61
|
+
res.end(JSON.stringify({ ok: false, code: 'not-found' }))
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
return new Promise((resolve) => {
|
|
65
|
+
server.listen(0, '127.0.0.1', () => {
|
|
66
|
+
resolve({
|
|
67
|
+
port: server.address().port,
|
|
68
|
+
requests,
|
|
69
|
+
close: () => new Promise((done) => { server.closeAllConnections?.(); server.close(() => done()) }),
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toolContext(cwd, warnings = []) {
|
|
76
|
+
const definitions = new Map()
|
|
77
|
+
const ctx = {
|
|
78
|
+
sessions: { get: () => ({ header: { cwd } }) },
|
|
79
|
+
tools: { register: (definition) => { definitions.set(definition.name, definition); return () => {} } },
|
|
80
|
+
logger: { warn(message) { warnings.push(String(message)) } },
|
|
81
|
+
}
|
|
82
|
+
return { ctx, definitions }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const exec = { agent: { session: { id: 'session-1' } } }
|
|
86
|
+
|
|
87
|
+
test('ris_dev_programs reads the daemon rows in auto mode', async () => {
|
|
88
|
+
const cwd = mkdtempSync(join(tmpdir(), 'risd-tools-'))
|
|
89
|
+
const daemon = await fakeDaemon()
|
|
90
|
+
try {
|
|
91
|
+
__test.setRisdBridge({ mode: 'auto', state: { url: `http://127.0.0.1:${daemon.port}`, port: daemon.port, token: 't', apiVersion: 1, pid: 4242, home: cwd }, leaseId: null, pending: null })
|
|
92
|
+
const { ctx, definitions } = toolContext(cwd)
|
|
93
|
+
registerAgentTools(ctx, { daemonMode: 'auto' })
|
|
94
|
+
const value = await definitions.get('ris_dev_programs').execute({}, exec)
|
|
95
|
+
assert.equal(value.ok, true)
|
|
96
|
+
assert.equal(value.programs[0].id, 'demo')
|
|
97
|
+
assert.ok(daemon.requests.some((request) => request.method === 'GET' && request.url.startsWith('/v1/programs?cwd=')))
|
|
98
|
+
} finally {
|
|
99
|
+
__test.setRisdBridge(null)
|
|
100
|
+
await daemon.close()
|
|
101
|
+
rmSync(cwd, { recursive: true, force: true })
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('ris_dev_start / ris_dev_logs go through the daemon', async () => {
|
|
106
|
+
const cwd = mkdtempSync(join(tmpdir(), 'risd-tools-'))
|
|
107
|
+
const daemon = await fakeDaemon()
|
|
108
|
+
try {
|
|
109
|
+
__test.setRisdBridge({ mode: 'auto', state: { url: `http://127.0.0.1:${daemon.port}`, port: daemon.port, token: 't', apiVersion: 1, pid: 4242, home: cwd }, leaseId: null, pending: null })
|
|
110
|
+
const { ctx, definitions } = toolContext(cwd)
|
|
111
|
+
registerAgentTools(ctx, { daemonMode: 'auto' })
|
|
112
|
+
|
|
113
|
+
const started = await definitions.get('ris_dev_start').execute({ id: 'demo' }, exec)
|
|
114
|
+
assert.equal(started.ok, true)
|
|
115
|
+
assert.equal(started.alreadyRunning, false)
|
|
116
|
+
assert.equal(started.program.id, 'demo')
|
|
117
|
+
assert.ok(daemon.requests.some((request) => request.method === 'POST' && request.body?.action === 'start'))
|
|
118
|
+
|
|
119
|
+
const logs = await definitions.get('ris_dev_logs').execute({ id: 'demo', lines: 10 }, exec)
|
|
120
|
+
assert.equal(logs.ok, true)
|
|
121
|
+
assert.equal(logs.running, true)
|
|
122
|
+
assert.match(logs.content, /line2/)
|
|
123
|
+
} finally {
|
|
124
|
+
__test.setRisdBridge(null)
|
|
125
|
+
await daemon.close()
|
|
126
|
+
rmSync(cwd, { recursive: true, force: true })
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('ris_dev_debug state and threads are answered by the daemon', async () => {
|
|
131
|
+
const cwd = mkdtempSync(join(tmpdir(), 'risd-tools-'))
|
|
132
|
+
const daemon = await fakeDaemon()
|
|
133
|
+
try {
|
|
134
|
+
__test.setRisdBridge({ mode: 'auto', state: { url: `http://127.0.0.1:${daemon.port}`, port: daemon.port, token: 't', apiVersion: 1, pid: 4242, home: cwd }, leaseId: null, pending: null })
|
|
135
|
+
const { ctx, definitions } = toolContext(cwd)
|
|
136
|
+
registerAgentTools(ctx, { daemonMode: 'auto' })
|
|
137
|
+
const debug = definitions.get('ris_dev_debug')
|
|
138
|
+
|
|
139
|
+
const state = await debug.execute({ action: 'state', id: 'demo' }, exec)
|
|
140
|
+
assert.equal(state.ok, true)
|
|
141
|
+
assert.equal(state.debug.attached, false)
|
|
142
|
+
assert.equal(state.breakpoints.length, 1)
|
|
143
|
+
const debugCalls = () => daemon.requests
|
|
144
|
+
.filter((request) => request.url === '/v1/debug')
|
|
145
|
+
.map((request) => request.body.action)
|
|
146
|
+
assert.deepEqual(debugCalls().slice(0, 2), ['status', 'breakpoints'])
|
|
147
|
+
|
|
148
|
+
const threads = await debug.execute({ action: 'threads', id: 'demo' }, exec)
|
|
149
|
+
assert.equal(threads.ok, true)
|
|
150
|
+
assert.equal(threads.threads[0].id, 't1')
|
|
151
|
+
assert.ok(debugCalls().includes('threads'))
|
|
152
|
+
} finally {
|
|
153
|
+
__test.setRisdBridge(null)
|
|
154
|
+
await daemon.close()
|
|
155
|
+
rmSync(cwd, { recursive: true, force: true })
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('a stopped daemon falls back to the local implementation', async () => {
|
|
160
|
+
const cwd = mkdtempSync(join(tmpdir(), 'risd-tools-'))
|
|
161
|
+
const daemon = await fakeDaemon()
|
|
162
|
+
__test.setRisdBridge({ mode: 'auto', state: { url: `http://127.0.0.1:${daemon.port}`, port: daemon.port, token: 't', apiVersion: 1, pid: 4242, home: cwd }, leaseId: null, pending: null })
|
|
163
|
+
const { ctx, definitions } = toolContext(cwd)
|
|
164
|
+
registerAgentTools(ctx, { daemonMode: 'auto' })
|
|
165
|
+
// Trip the heal breaker: without a daemon this call must fall back locally, not
|
|
166
|
+
// spawn a real one (which would also make the assertion meaningless).
|
|
167
|
+
for (let attempt = 0; attempt < 3; attempt += 1) __test.noteRisdHealFailure('probe failed', Date.now())
|
|
168
|
+
await daemon.close()
|
|
169
|
+
try {
|
|
170
|
+
const value = await definitions.get('ris_dev_programs').execute({}, exec)
|
|
171
|
+
assert.equal(value.ok, true)
|
|
172
|
+
// The local branch scans the (empty) workspace itself: same contract, no daemon.
|
|
173
|
+
assert.deepEqual(value.programs, [])
|
|
174
|
+
} finally {
|
|
175
|
+
__test.setRisdBridge(null)
|
|
176
|
+
rmSync(cwd, { recursive: true, force: true })
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('a half-dead daemon is demoted and the fallback is said out loud (C-P2-2)', async () => {
|
|
181
|
+
// Before: a non-null pointer was trusted forever, so each tool call paid a 2 s
|
|
182
|
+
// timeout and quietly ran locally — the programs it started were then invisible to
|
|
183
|
+
// the daemon (stopping the daemon would not stop them).
|
|
184
|
+
const cwd = mkdtempSync(join(tmpdir(), 'risd-tools-'))
|
|
185
|
+
const home = mkdtempSync(join(tmpdir(), 'risd-tools-home-'))
|
|
186
|
+
const previousHome = process.env.DSH_RIS_IDE_PROXY_HOME
|
|
187
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
188
|
+
const bridge = { mode: 'auto', state: { url: 'http://127.0.0.1:9', port: 9, token: 't', apiVersion: 1 }, leaseId: null, pending: null, degraded: false, lastProbeAt: 0 }
|
|
189
|
+
try {
|
|
190
|
+
// Trip the heal breaker so the call cannot spawn a real daemon: this test is about
|
|
191
|
+
// the probe + warning, not about the spawn path.
|
|
192
|
+
const now = Date.now()
|
|
193
|
+
__test.setRisdBridge(bridge)
|
|
194
|
+
for (let attempt = 0; attempt < 3; attempt += 1) __test.noteRisdHealFailure('probe failed', now)
|
|
195
|
+
const warnings = []
|
|
196
|
+
const { ctx, definitions } = toolContext(cwd, warnings)
|
|
197
|
+
registerAgentTools(ctx, { daemonMode: 'auto' })
|
|
198
|
+
const value = await definitions.get('ris_dev_programs').execute({}, exec)
|
|
199
|
+
assert.equal(value.ok, true, 'the local implementation still answers')
|
|
200
|
+
assert.equal(bridge.state, null, 'the dead pointer is dropped')
|
|
201
|
+
assert.equal(bridge.degraded, true)
|
|
202
|
+
assert.ok(warnings.some((message) => /不在 daemon 的托管范围内(停 daemon 不会停它)/.test(message)),
|
|
203
|
+
`expected a local-fallback warning, got ${JSON.stringify(warnings)}`)
|
|
204
|
+
} finally {
|
|
205
|
+
__test.setRisdBridge(null)
|
|
206
|
+
if (previousHome === undefined) delete process.env.DSH_RIS_IDE_PROXY_HOME
|
|
207
|
+
else process.env.DSH_RIS_IDE_PROXY_HOME = previousHome
|
|
208
|
+
rmSync(cwd, { recursive: true, force: true })
|
|
209
|
+
rmSync(home, { recursive: true, force: true })
|
|
210
|
+
}
|
|
211
|
+
})
|