@lornatang/dsh-caveman 0.1.10 → 0.1.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lornatang/dsh-caveman",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Caveman integration for DeepSeek Harness.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -92,29 +92,29 @@
92
92
  "devDependencies": {
93
93
  "@deepseek-ai/cordis": "~4.1.0",
94
94
  "@deepseek-ai/cordis-plugin-group": "1.1.0",
95
- "@deepseek-ai/dsh-agent": "0.2.2-rc.3",
96
- "@deepseek-ai/dsh-agent-loop": "^0.2.2-rc.3",
97
- "@deepseek-ai/dsh-agent-preset": "0.2.2-rc.3",
98
- "@deepseek-ai/dsh-agent-preset-registry": "0.2.2-rc.3",
99
- "@deepseek-ai/dsh-fs-local": "0.2.2-rc.3",
100
- "@deepseek-ai/dsh-llm": "0.2.2-rc.3",
101
- "@deepseek-ai/dsh-llm-pi-ai": "0.2.2-rc.3",
102
- "@deepseek-ai/dsh-mcp-manager": "0.2.2-rc.3",
103
- "@deepseek-ai/dsh-ptc-runtime": "0.2.2-rc.3",
104
- "@deepseek-ai/dsh-sandbox-policy": "0.2.2-rc.3",
105
- "@deepseek-ai/dsh-session-persistence-jsonl": "0.2.2-rc.3",
106
- "@deepseek-ai/dsh-skill": "0.2.2-rc.3",
107
- "@deepseek-ai/dsh-subagent": "0.2.2-rc.3",
108
- "@deepseek-ai/dsh-subagent-spawn-in-process": "0.2.2-rc.3",
109
- "@deepseek-ai/dsh-subprocess-local": "0.2.2-rc.3",
110
- "@deepseek-ai/dsh-system-prompt": "0.2.2-rc.3",
111
- "@deepseek-ai/dsh-tool-executor-native": "^0.2.2-rc.3",
112
- "@deepseek-ai/dsh-tool-executor-ptc": "0.2.2-rc.3",
113
- "@deepseek-ai/dsh-tool-fs": "0.2.2-rc.3",
114
- "@deepseek-ai/dsh-tool-fs-search": "0.2.2-rc.3",
115
- "@deepseek-ai/dsh-tool-subagent": "0.2.2-rc.3",
116
- "@deepseek-ai/dsh-tools": "^0.2.2-rc.3",
117
- "@deepseek-ai/dsh-working-directory": "0.2.2-rc.3",
95
+ "@deepseek-ai/dsh-agent": "*",
96
+ "@deepseek-ai/dsh-agent-loop": "*",
97
+ "@deepseek-ai/dsh-agent-preset": "*",
98
+ "@deepseek-ai/dsh-agent-preset-registry": "*",
99
+ "@deepseek-ai/dsh-fs-local": "*",
100
+ "@deepseek-ai/dsh-llm": "*",
101
+ "@deepseek-ai/dsh-llm-pi-ai": "*",
102
+ "@deepseek-ai/dsh-mcp-manager": "*",
103
+ "@deepseek-ai/dsh-ptc-runtime": "*",
104
+ "@deepseek-ai/dsh-sandbox-policy": "*",
105
+ "@deepseek-ai/dsh-session-persistence-jsonl": "*",
106
+ "@deepseek-ai/dsh-skill": "*",
107
+ "@deepseek-ai/dsh-subagent": "*",
108
+ "@deepseek-ai/dsh-subagent-spawn-in-process": "*",
109
+ "@deepseek-ai/dsh-subprocess-local": "*",
110
+ "@deepseek-ai/dsh-system-prompt": "*",
111
+ "@deepseek-ai/dsh-tool-executor-native": "*",
112
+ "@deepseek-ai/dsh-tool-executor-ptc": "*",
113
+ "@deepseek-ai/dsh-tool-fs": "*",
114
+ "@deepseek-ai/dsh-tool-fs-search": "*",
115
+ "@deepseek-ai/dsh-tool-subagent": "*",
116
+ "@deepseek-ai/dsh-tools": "*",
117
+ "@deepseek-ai/dsh-working-directory": "*",
118
118
  "@types/node": "^26.6.6",
119
119
  "typescript": "^7.0.2"
120
120
  },
@@ -1,54 +1,54 @@
1
- # Cavecrew agent presets and their in-process delegation tools. The read-only
2
- # roles use a read-only Git tool instead of a shell.
3
- - insert:
4
- - id: preset-cavecrew-investigator
5
- name: '@deepseek-ai/dsh-agent-preset'
6
- config:
7
- id: cavecrew-investigator
8
- name: Cavecrew Investigator
9
- plugins:
10
- - id: cavecrew
11
- name: '@lornatang/dsh-caveman/cavecrew'
12
- config: { role: investigator }
13
- - id: tool-fs
14
- name: '@deepseek-ai/dsh-tool-fs'
15
- config: { readImage: false, write: false, edit: false, applyPatch: false }
16
- - id: tool-fs-search
17
- name: '@deepseek-ai/dsh-tool-fs-search'
18
- config: { sampleOverCapGlobResults: false }
19
- - id: preset-cavecrew-builder
20
- name: '@deepseek-ai/dsh-agent-preset'
21
- config:
22
- id: cavecrew-builder
23
- name: Cavecrew Builder
24
- plugins:
25
- - id: cavecrew
26
- name: '@lornatang/dsh-caveman/cavecrew'
27
- config: { role: builder }
28
- - id: tool-fs
29
- name: '@deepseek-ai/dsh-tool-fs'
30
- config: { readImage: false, write: false, applyPatch: false }
31
- - id: preset-cavecrew-reviewer
32
- name: '@deepseek-ai/dsh-agent-preset'
33
- config:
34
- id: cavecrew-reviewer
35
- name: Cavecrew Reviewer
36
- plugins:
37
- - id: cavecrew
38
- name: '@lornatang/dsh-caveman/cavecrew'
39
- config: { role: reviewer }
40
- - id: tool-fs
41
- name: '@deepseek-ai/dsh-tool-fs'
42
- config: { readImage: false, write: false, edit: false, applyPatch: false }
43
- - id: tool-fs-search
44
- name: '@deepseek-ai/dsh-tool-fs-search'
45
- config: { sampleOverCapGlobResults: false }
46
- - id: tool-cavecrew-investigator
47
- name: '@deepseek-ai/dsh-tool-subagent'
48
- config: { provider: spawn, toolName: cavecrew-investigator, agentPreset: cavecrew-investigator }
49
- - id: tool-cavecrew-builder
50
- name: '@deepseek-ai/dsh-tool-subagent'
51
- config: { provider: spawn, toolName: cavecrew-builder, agentPreset: cavecrew-builder }
52
- - id: tool-cavecrew-reviewer
53
- name: '@deepseek-ai/dsh-tool-subagent'
54
- config: { provider: spawn, toolName: cavecrew-reviewer, agentPreset: cavecrew-reviewer }
1
+ # Cavecrew agent presets and their in-process delegation tools. The read-only
2
+ # roles use a read-only Git tool instead of a shell.
3
+ - insert:
4
+ - id: preset-cavecrew-investigator
5
+ name: '@deepseek-ai/dsh-agent-preset'
6
+ config:
7
+ id: cavecrew-investigator
8
+ name: Cavecrew Investigator
9
+ plugins:
10
+ - id: cavecrew
11
+ name: '@lornatang/dsh-caveman/cavecrew'
12
+ config: { role: investigator }
13
+ - id: tool-fs
14
+ name: '@deepseek-ai/dsh-tool-fs'
15
+ config: { readImage: false, write: false, edit: false, applyPatch: false }
16
+ - id: tool-fs-search
17
+ name: '@deepseek-ai/dsh-tool-fs-search'
18
+ config: { sampleOverCapGlobResults: false }
19
+ - id: preset-cavecrew-builder
20
+ name: '@deepseek-ai/dsh-agent-preset'
21
+ config:
22
+ id: cavecrew-builder
23
+ name: Cavecrew Builder
24
+ plugins:
25
+ - id: cavecrew
26
+ name: '@lornatang/dsh-caveman/cavecrew'
27
+ config: { role: builder }
28
+ - id: tool-fs
29
+ name: '@deepseek-ai/dsh-tool-fs'
30
+ config: { readImage: false, write: false, applyPatch: false }
31
+ - id: preset-cavecrew-reviewer
32
+ name: '@deepseek-ai/dsh-agent-preset'
33
+ config:
34
+ id: cavecrew-reviewer
35
+ name: Cavecrew Reviewer
36
+ plugins:
37
+ - id: cavecrew
38
+ name: '@lornatang/dsh-caveman/cavecrew'
39
+ config: { role: reviewer }
40
+ - id: tool-fs
41
+ name: '@deepseek-ai/dsh-tool-fs'
42
+ config: { readImage: false, write: false, edit: false, applyPatch: false }
43
+ - id: tool-fs-search
44
+ name: '@deepseek-ai/dsh-tool-fs-search'
45
+ config: { sampleOverCapGlobResults: false }
46
+ - id: tool-cavecrew-investigator
47
+ name: '@deepseek-ai/dsh-tool-subagent'
48
+ config: { provider: spawn, toolName: cavecrew-investigator, agentPreset: cavecrew-investigator }
49
+ - id: tool-cavecrew-builder
50
+ name: '@deepseek-ai/dsh-tool-subagent'
51
+ config: { provider: spawn, toolName: cavecrew-builder, agentPreset: cavecrew-builder }
52
+ - id: tool-cavecrew-reviewer
53
+ name: '@deepseek-ai/dsh-tool-subagent'
54
+ config: { provider: spawn, toolName: cavecrew-reviewer, agentPreset: cavecrew-reviewer }
@@ -1,79 +1,79 @@
1
- import { performance } from 'node:perf_hooks'
2
- import { resolve } from 'node:path'
3
- import { SessionModeTracker, resolveMode } from '../lib/mode.js'
4
- import { createSkillProvider } from '../lib/skills.js'
5
- import { CompressionHistoryIndex } from '../lib/compression.js'
6
-
7
- function median(values) {
8
- const sorted = [...values].sort((a, b) => a - b)
9
- return sorted[Math.floor(sorted.length / 2)]
10
- }
11
-
12
- const events = Array.from({ length: 1000 }, (_, index) => ({
13
- type: 'user/message', data: {
14
- source: { kind: 'user' },
15
- content: [{ type: 'text', text: index % 150 === 0 ? '/caveman ultra' : 'Inspect the current code and report the results.' }],
16
- },
17
- }))
18
- const session = {
19
- get seq() { return events.length },
20
- snapshotEvents(from = 0, to = events.length) { return events.slice(from, to) },
21
- }
22
- const pending = [{ source: { kind: 'user' }, content: [{ type: 'text', text: 'continue' }] }]
23
- const tracker = new SessionModeTracker()
24
- tracker.resolve('manual', session, pending)
25
- const full = []
26
- const incremental = []
27
- for (let index = 0; index < 101; index++) {
28
- events.push({ type: 'turn/start', data: {} })
29
- let start = performance.now()
30
- const expected = resolveMode('manual', events, pending)
31
- full.push(performance.now() - start)
32
- start = performance.now()
33
- const actual = tracker.resolve('manual', session, pending)
34
- incremental.push(performance.now() - start)
35
- if (actual.mode !== expected.mode || actual.action !== expected.action) throw new Error('mode results differ')
36
- }
37
- const provider = createSkillProvider(resolve('vendor/caveman/skills'))
38
- const discovery = []
39
- for (let index = 0; index < 31; index++) {
40
- const start = performance.now()
41
- const result = await provider.list({})
42
- if (!result.complete || result.candidates.length !== 22) throw new Error('skill catalog is incomplete')
43
- discovery.push(performance.now() - start)
44
- }
45
- const historyEvents = Array.from({ length: 5000 }, (_, index) => index % 2 === 0
46
- ? { type: 'assistant/message', data: { message: { content: [{ type: 'tool-call', id: `call-${index}`, name: 'read' }] } } }
47
- : { type: 'tool/result', data: { message: { content: [{ type: 'text', text: 'ordinary result' }] } } })
48
- const historySession = {
49
- get seq() { return historyEvents.length },
50
- snapshotEvents(from = 0, to = historyEvents.length) { return historyEvents.slice(from, to) },
51
- eventAt() { return undefined },
52
- }
53
- const history = new CompressionHistoryIndex()
54
- history.read(historySession)
55
- const fullHistory = []
56
- const incrementalHistory = []
57
- for (let index = 0; index < 101; index++) {
58
- historyEvents.push({ type: 'turn/start', data: {} })
59
- let start = performance.now()
60
- const hasMarker = historySession.snapshotEvents().some(event => event.type === 'tool/result' &&
61
- event.data.message.content.some(block => block.type === 'text' && block.text.includes('[caveman: shortened; exact original via caveman_retrieve handle=')))
62
- const names = new Map()
63
- for (const event of historySession.snapshotEvents()) {
64
- if (event.type === 'assistant/message') for (const block of event.data.message.content) {
65
- if (block.type === 'tool-call' && !names.has(block.id)) names.set(block.id, block.name)
66
- }
67
- }
68
- fullHistory.push(performance.now() - start)
69
- start = performance.now()
70
- const actual = history.read(historySession)
71
- history.read(historySession)
72
- incrementalHistory.push(performance.now() - start)
73
- if (actual.hasMarker !== hasMarker || actual.toolNames.size !== names.size) throw new Error('compression history differs')
74
- }
75
- console.log(JSON.stringify({
76
- mode: { initialMessages: 1000, runs: full.length, fullReplayMedianMs: median(full), incrementalMedianMs: median(incremental) },
77
- skills: { count: 22, runs: discovery.length, discoveryMedianMs: median(discovery) },
78
- compressionHistory: { initialEvents: 5000, runs: fullHistory.length, fullScanMedianMs: median(fullHistory), incrementalMedianMs: median(incrementalHistory) },
79
- }, null, 2))
1
+ import { performance } from 'node:perf_hooks'
2
+ import { resolve } from 'node:path'
3
+ import { SessionModeTracker, resolveMode } from '../lib/mode.js'
4
+ import { createSkillProvider } from '../lib/skills.js'
5
+ import { CompressionHistoryIndex } from '../lib/compression.js'
6
+
7
+ function median(values) {
8
+ const sorted = [...values].sort((a, b) => a - b)
9
+ return sorted[Math.floor(sorted.length / 2)]
10
+ }
11
+
12
+ const events = Array.from({ length: 1000 }, (_, index) => ({
13
+ type: 'user/message', data: {
14
+ source: { kind: 'user' },
15
+ content: [{ type: 'text', text: index % 150 === 0 ? '/caveman ultra' : 'Inspect the current code and report the results.' }],
16
+ },
17
+ }))
18
+ const session = {
19
+ get seq() { return events.length },
20
+ snapshotEvents(from = 0, to = events.length) { return events.slice(from, to) },
21
+ }
22
+ const pending = [{ source: { kind: 'user' }, content: [{ type: 'text', text: 'continue' }] }]
23
+ const tracker = new SessionModeTracker()
24
+ tracker.resolve('manual', session, pending)
25
+ const full = []
26
+ const incremental = []
27
+ for (let index = 0; index < 101; index++) {
28
+ events.push({ type: 'turn/start', data: {} })
29
+ let start = performance.now()
30
+ const expected = resolveMode('manual', events, pending)
31
+ full.push(performance.now() - start)
32
+ start = performance.now()
33
+ const actual = tracker.resolve('manual', session, pending)
34
+ incremental.push(performance.now() - start)
35
+ if (actual.mode !== expected.mode || actual.action !== expected.action) throw new Error('mode results differ')
36
+ }
37
+ const provider = createSkillProvider(resolve('vendor/caveman/skills'))
38
+ const discovery = []
39
+ for (let index = 0; index < 31; index++) {
40
+ const start = performance.now()
41
+ const result = await provider.list({})
42
+ if (!result.complete || result.candidates.length !== 22) throw new Error('skill catalog is incomplete')
43
+ discovery.push(performance.now() - start)
44
+ }
45
+ const historyEvents = Array.from({ length: 5000 }, (_, index) => index % 2 === 0
46
+ ? { type: 'assistant/message', data: { message: { content: [{ type: 'tool-call', id: `call-${index}`, name: 'read' }] } } }
47
+ : { type: 'tool/result', data: { message: { content: [{ type: 'text', text: 'ordinary result' }] } } })
48
+ const historySession = {
49
+ get seq() { return historyEvents.length },
50
+ snapshotEvents(from = 0, to = historyEvents.length) { return historyEvents.slice(from, to) },
51
+ eventAt() { return undefined },
52
+ }
53
+ const history = new CompressionHistoryIndex()
54
+ history.read(historySession)
55
+ const fullHistory = []
56
+ const incrementalHistory = []
57
+ for (let index = 0; index < 101; index++) {
58
+ historyEvents.push({ type: 'turn/start', data: {} })
59
+ let start = performance.now()
60
+ const hasMarker = historySession.snapshotEvents().some(event => event.type === 'tool/result' &&
61
+ event.data.message.content.some(block => block.type === 'text' && block.text.includes('[caveman: shortened; exact original via caveman_retrieve handle=')))
62
+ const names = new Map()
63
+ for (const event of historySession.snapshotEvents()) {
64
+ if (event.type === 'assistant/message') for (const block of event.data.message.content) {
65
+ if (block.type === 'tool-call' && !names.has(block.id)) names.set(block.id, block.name)
66
+ }
67
+ }
68
+ fullHistory.push(performance.now() - start)
69
+ start = performance.now()
70
+ const actual = history.read(historySession)
71
+ history.read(historySession)
72
+ incrementalHistory.push(performance.now() - start)
73
+ if (actual.hasMarker !== hasMarker || actual.toolNames.size !== names.size) throw new Error('compression history differs')
74
+ }
75
+ console.log(JSON.stringify({
76
+ mode: { initialMessages: 1000, runs: full.length, fullReplayMedianMs: median(full), incrementalMedianMs: median(incremental) },
77
+ skills: { count: 22, runs: discovery.length, discoveryMedianMs: median(discovery) },
78
+ compressionHistory: { initialEvents: 5000, runs: fullHistory.length, fullScanMedianMs: median(fullHistory), incrementalMedianMs: median(incrementalHistory) },
79
+ }, null, 2))
@@ -1,30 +1,30 @@
1
- #!/usr/bin/env node
2
- import { spawn } from 'node:child_process'
3
- import { constants } from 'node:os'
4
- import { createRequire } from 'node:module'
5
- import { dirname, join } from 'node:path'
6
-
7
- const require = createRequire(import.meta.url)
8
- const cli = join(dirname(require.resolve('@caveman-ai/cli/package.json')), 'dist', 'index.js')
9
- const args = process.argv.slice(2)
10
- let learnBinary
11
- if (args[0] === 'learn' && !['--help', '-h', 'help'].includes(args[1])) {
12
- try { learnBinary = await (await import('./learn-binary.mjs')).resolveLearnBinary() }
13
- catch (error) { console.error(`dsh-caveman: ${error.message}`); process.exit(1) }
14
- }
15
- const direct = learnBinary && args[1] === 'locate'
16
- const child = spawn(direct ? learnBinary : process.execPath, direct ? args : [cli, ...args], {
17
- stdio: 'inherit', windowsHide: true,
18
- env: learnBinary ? { ...process.env, CAVEMAN_PROXY_BIN: learnBinary } : process.env,
19
- })
20
-
21
- for (const signal of ['SIGINT', 'SIGTERM']) {
22
- process.on(signal, () => { if (child.exitCode === null && child.signalCode === null) child.kill(signal) })
23
- }
24
- child.once('error', error => {
25
- console.error(`dsh-caveman: could not start the CLI: ${error.message}`)
26
- process.exitCode = 1
27
- })
28
- child.once('exit', (code, signal) => {
29
- process.exitCode = code ?? (signal ? 128 + (constants.signals[signal] ?? 0) : 1)
30
- })
1
+ #!/usr/bin/env node
2
+ import { spawn } from 'node:child_process'
3
+ import { constants } from 'node:os'
4
+ import { createRequire } from 'node:module'
5
+ import { dirname, join } from 'node:path'
6
+
7
+ const require = createRequire(import.meta.url)
8
+ const cli = join(dirname(require.resolve('@caveman-ai/cli/package.json')), 'dist', 'index.js')
9
+ const args = process.argv.slice(2)
10
+ let learnBinary
11
+ if (args[0] === 'learn' && !['--help', '-h', 'help'].includes(args[1])) {
12
+ try { learnBinary = await (await import('./learn-binary.mjs')).resolveLearnBinary() }
13
+ catch (error) { console.error(`dsh-caveman: ${error.message}`); process.exit(1) }
14
+ }
15
+ const direct = learnBinary && args[1] === 'locate'
16
+ const child = spawn(direct ? learnBinary : process.execPath, direct ? args : [cli, ...args], {
17
+ stdio: 'inherit', windowsHide: true,
18
+ env: learnBinary ? { ...process.env, CAVEMAN_PROXY_BIN: learnBinary } : process.env,
19
+ })
20
+
21
+ for (const signal of ['SIGINT', 'SIGTERM']) {
22
+ process.on(signal, () => { if (child.exitCode === null && child.signalCode === null) child.kill(signal) })
23
+ }
24
+ child.once('error', error => {
25
+ console.error(`dsh-caveman: could not start the CLI: ${error.message}`)
26
+ process.exitCode = 1
27
+ })
28
+ child.once('exit', (code, signal) => {
29
+ process.exitCode = code ?? (signal ? 128 + (constants.signals[signal] ?? 0) : 1)
30
+ })
@@ -1,82 +1,82 @@
1
- import { createHash, randomBytes } from 'node:crypto'
2
- import { createReadStream } from 'node:fs'
3
- import { chmod, mkdir, open, readFile, rename, rm } from 'node:fs/promises'
4
- import { homedir } from 'node:os'
5
- import { join } from 'node:path'
6
-
7
- const MAX_BINARY_BYTES = 120 * 1024 * 1024
8
- const manifestUrl = new URL('./learn-checksums.json', import.meta.url)
9
- const packageUrl = new URL('../package.json', import.meta.url)
10
-
11
- async function fileDigest(path) {
12
- try {
13
- const hash = createHash('sha256')
14
- for await (const chunk of createReadStream(path)) hash.update(chunk)
15
- return hash.digest('hex')
16
- } catch (error) {
17
- if (error?.code === 'ENOENT') return undefined
18
- throw error
19
- }
20
- }
21
-
22
- /** Download and verify the executable once for the current platform and version. */
23
- export async function resolveLearnBinary(options = {}) {
24
- const { env = process.env, platform = process.platform, arch = process.arch } = options
25
- if (env.DSH_CAVEMAN_LEARN_BINARY) return env.DSH_CAVEMAN_LEARN_BINARY
26
- const manifest = options.manifest ?? JSON.parse(await readFile(manifestUrl, 'utf8'))
27
- const version = JSON.parse(await readFile(packageUrl, 'utf8')).version
28
- if (manifest.version !== version || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/u.test(version)) throw new Error('Invalid analyzer version manifest')
29
- const os = { linux: 'linux', darwin: 'darwin', win32: 'win32' }[platform]
30
- const cpu = { x64: 'amd64', arm64: 'arm64' }[arch]
31
- const entry = manifest.binaries?.[`${platform}-${arch}`]
32
- const expectedName = os && cpu ? `dsh-caveman-learn_${os}_${cpu}` : undefined
33
- if (!expectedName || entry?.name !== expectedName || !/^[a-f0-9]{64}$/u.test(entry.sha256 ?? '')) {
34
- throw new Error(`No verified analyzer is available for ${platform}/${arch}`)
35
- }
36
- const directory = join(options.cacheRoot ?? join(homedir(), '.dsh', 'cache', 'dsh-caveman', 'learn'), version)
37
- const target = join(directory, `${expectedName}${platform === 'win32' ? '.exe' : ''}`)
38
- if (await fileDigest(target) === entry.sha256) return target
39
- await mkdir(directory, { recursive: true, mode: 0o700 })
40
- const temporary = join(directory, `.${expectedName}.${randomBytes(12).toString('hex')}.tmp`)
41
- let handle
42
- let dispatcher
43
- try {
44
- const url = `https://github.com/Lornatang/dsh-plugin/releases/download/dsh-caveman-v${version}/${expectedName}`
45
- let fetcher = options.fetcher ?? fetch
46
- if (!options.fetcher && (env.https_proxy || env.HTTPS_PROXY || env.http_proxy || env.HTTP_PROXY)) {
47
- const { EnvHttpProxyAgent, fetch: proxyFetch } = await import('undici')
48
- dispatcher = new EnvHttpProxyAgent({
49
- httpProxy: env.http_proxy ?? env.HTTP_PROXY ?? '',
50
- httpsProxy: env.https_proxy ?? env.HTTPS_PROXY ?? '',
51
- noProxy: env.no_proxy ?? env.NO_PROXY ?? '',
52
- })
53
- fetcher = (input, init) => proxyFetch(input, { ...init, dispatcher })
54
- }
55
- const response = await fetcher(url, { signal: AbortSignal.timeout(120_000) })
56
- if (!response.ok || !response.body) throw new Error(`Analyzer download failed (HTTP ${response.status})`)
57
- const advertised = Number(response.headers.get('content-length'))
58
- if (advertised > MAX_BINARY_BYTES) throw new Error('Analyzer download exceeds the size limit')
59
- handle = await open(temporary, 'wx', 0o700)
60
- const hash = createHash('sha256')
61
- let size = 0
62
- for await (const chunk of response.body) {
63
- size += chunk.length
64
- if (size > MAX_BINARY_BYTES) throw new Error('Analyzer download exceeds the size limit')
65
- hash.update(chunk)
66
- await handle.writeFile(chunk)
67
- }
68
- await handle.close()
69
- handle = undefined
70
- if (hash.digest('hex') !== entry.sha256) throw new Error('Analyzer checksum mismatch')
71
- await chmod(temporary, 0o700)
72
- try { await rename(temporary, target) } catch (error) {
73
- // Another invocation may have finished the same verified download.
74
- if (await fileDigest(target) !== entry.sha256) throw error
75
- }
76
- return target
77
- } finally {
78
- await handle?.close().catch(() => {})
79
- await rm(temporary, { force: true }).catch(() => {})
80
- await dispatcher?.destroy().catch(() => {})
81
- }
82
- }
1
+ import { createHash, randomBytes } from 'node:crypto'
2
+ import { createReadStream } from 'node:fs'
3
+ import { chmod, mkdir, open, readFile, rename, rm } from 'node:fs/promises'
4
+ import { homedir } from 'node:os'
5
+ import { join } from 'node:path'
6
+
7
+ const MAX_BINARY_BYTES = 120 * 1024 * 1024
8
+ const manifestUrl = new URL('./learn-checksums.json', import.meta.url)
9
+ const packageUrl = new URL('../package.json', import.meta.url)
10
+
11
+ async function fileDigest(path) {
12
+ try {
13
+ const hash = createHash('sha256')
14
+ for await (const chunk of createReadStream(path)) hash.update(chunk)
15
+ return hash.digest('hex')
16
+ } catch (error) {
17
+ if (error?.code === 'ENOENT') return undefined
18
+ throw error
19
+ }
20
+ }
21
+
22
+ /** Download and verify the executable once for the current platform and version. */
23
+ export async function resolveLearnBinary(options = {}) {
24
+ const { env = process.env, platform = process.platform, arch = process.arch } = options
25
+ if (env.DSH_CAVEMAN_LEARN_BINARY) return env.DSH_CAVEMAN_LEARN_BINARY
26
+ const manifest = options.manifest ?? JSON.parse(await readFile(manifestUrl, 'utf8'))
27
+ const version = JSON.parse(await readFile(packageUrl, 'utf8')).version
28
+ if (manifest.version !== version || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/u.test(version)) throw new Error('Invalid analyzer version manifest')
29
+ const os = { linux: 'linux', darwin: 'darwin', win32: 'win32' }[platform]
30
+ const cpu = { x64: 'amd64', arm64: 'arm64' }[arch]
31
+ const entry = manifest.binaries?.[`${platform}-${arch}`]
32
+ const expectedName = os && cpu ? `dsh-caveman-learn_${os}_${cpu}` : undefined
33
+ if (!expectedName || entry?.name !== expectedName || !/^[a-f0-9]{64}$/u.test(entry.sha256 ?? '')) {
34
+ throw new Error(`No verified analyzer is available for ${platform}/${arch}`)
35
+ }
36
+ const directory = join(options.cacheRoot ?? join(homedir(), '.dsh', 'cache', 'dsh-caveman', 'learn'), version)
37
+ const target = join(directory, `${expectedName}${platform === 'win32' ? '.exe' : ''}`)
38
+ if (await fileDigest(target) === entry.sha256) return target
39
+ await mkdir(directory, { recursive: true, mode: 0o700 })
40
+ const temporary = join(directory, `.${expectedName}.${randomBytes(12).toString('hex')}.tmp`)
41
+ let handle
42
+ let dispatcher
43
+ try {
44
+ const url = `https://github.com/Lornatang/dsh-plugin/releases/download/dsh-caveman-v${version}/${expectedName}`
45
+ let fetcher = options.fetcher ?? fetch
46
+ if (!options.fetcher && (env.https_proxy || env.HTTPS_PROXY || env.http_proxy || env.HTTP_PROXY)) {
47
+ const { EnvHttpProxyAgent, fetch: proxyFetch } = await import('undici')
48
+ dispatcher = new EnvHttpProxyAgent({
49
+ httpProxy: env.http_proxy ?? env.HTTP_PROXY ?? '',
50
+ httpsProxy: env.https_proxy ?? env.HTTPS_PROXY ?? '',
51
+ noProxy: env.no_proxy ?? env.NO_PROXY ?? '',
52
+ })
53
+ fetcher = (input, init) => proxyFetch(input, { ...init, dispatcher })
54
+ }
55
+ const response = await fetcher(url, { signal: AbortSignal.timeout(120_000) })
56
+ if (!response.ok || !response.body) throw new Error(`Analyzer download failed (HTTP ${response.status})`)
57
+ const advertised = Number(response.headers.get('content-length'))
58
+ if (advertised > MAX_BINARY_BYTES) throw new Error('Analyzer download exceeds the size limit')
59
+ handle = await open(temporary, 'wx', 0o700)
60
+ const hash = createHash('sha256')
61
+ let size = 0
62
+ for await (const chunk of response.body) {
63
+ size += chunk.length
64
+ if (size > MAX_BINARY_BYTES) throw new Error('Analyzer download exceeds the size limit')
65
+ hash.update(chunk)
66
+ await handle.writeFile(chunk)
67
+ }
68
+ await handle.close()
69
+ handle = undefined
70
+ if (hash.digest('hex') !== entry.sha256) throw new Error('Analyzer checksum mismatch')
71
+ await chmod(temporary, 0o700)
72
+ try { await rename(temporary, target) } catch (error) {
73
+ // Another invocation may have finished the same verified download.
74
+ if (await fileDigest(target) !== entry.sha256) throw error
75
+ }
76
+ return target
77
+ } finally {
78
+ await handle?.close().catch(() => {})
79
+ await rm(temporary, { force: true }).catch(() => {})
80
+ await dispatcher?.destroy().catch(() => {})
81
+ }
82
+ }
@@ -1,29 +1,29 @@
1
1
  {
2
- "version": "0.1.10",
2
+ "version": "0.1.11",
3
3
  "binaries": {
4
4
  "linux-x64": {
5
5
  "name": "dsh-caveman-learn_linux_amd64",
6
- "sha256": "777dc6016207a25c7aa20c50b60140a81655e66795f239591a440838dcd7ff0c"
6
+ "sha256": "44ba840775952ee1e8f568a25f43fac236926b0d1ea2aebfdf5d0ce8e8c497af"
7
7
  },
8
8
  "linux-arm64": {
9
9
  "name": "dsh-caveman-learn_linux_arm64",
10
- "sha256": "b302c89852481fcf0da0450530079edfca87f63c4fa190948b184302bc831238"
10
+ "sha256": "74efd959b4a684abfd70a56b65b5d515e42d961d49d8cd6420e829ad8aea2f3d"
11
11
  },
12
12
  "darwin-x64": {
13
13
  "name": "dsh-caveman-learn_darwin_amd64",
14
- "sha256": "33b851d8defc27b5d5e20df3797c5e338ad5c730a2acab886388396e261433e7"
14
+ "sha256": "12b351a47a3b0eaec6ce874315bb27feb7a01db2215c81c0f8fcd3358f6cbd8f"
15
15
  },
16
16
  "darwin-arm64": {
17
17
  "name": "dsh-caveman-learn_darwin_arm64",
18
- "sha256": "ab3a063099fcbc49401daa0ad615999fda2ebad56ea17a89a2fbbcc57f5b438d"
18
+ "sha256": "b3e4566e3d04170cd777c9793a71995edfe91cabccf6a87897109cebaa39a105"
19
19
  },
20
20
  "win32-x64": {
21
21
  "name": "dsh-caveman-learn_win32_amd64",
22
- "sha256": "315cbf8a19e5b2e9357c248cf4616daa7f82dcea4b30cfdcbb6795c886b3be1f"
22
+ "sha256": "bf305bfaff6e4ad2f1a6e399d2191e0a179f7096de05283d027d99f2a9b3f2af"
23
23
  },
24
24
  "win32-arm64": {
25
25
  "name": "dsh-caveman-learn_win32_arm64",
26
- "sha256": "4b693fcde17ceb0de7600a58fa51d935f585891c68aa7f9c0bfba10fc38574f1"
26
+ "sha256": "a03b3c3cc71264a8d5777cd807a69e7ea28a02e0d502b228578b30c9161c1593"
27
27
  }
28
28
  }
29
29
  }