@chatcode/chatcode-cli-test 1.0.47 → 3.0.2

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.
@@ -1,142 +0,0 @@
1
- import { spawnSync } from 'node:child_process'
2
- import { randomUUID } from 'node:crypto'
3
- import {
4
- existsSync,
5
- readFileSync,
6
- statSync,
7
- unlinkSync,
8
- } from 'node:fs'
9
- import { tmpdir } from 'node:os'
10
- import { isAbsolute, join, resolve } from 'node:path'
11
-
12
- export const PROCESS_GENERATION_ROTATE_EXIT_CODE = 75
13
- export const GENERATION_CONTROL_FILE_ENV =
14
- 'CHATCODE_CLI_GENERATION_CONTROL_FILE'
15
- export const GENERATION_CONTROL_TOKEN_ENV =
16
- 'CHATCODE_CLI_GENERATION_CONTROL_TOKEN'
17
- export const INTERNAL_RESUME_SESSION_ENV =
18
- 'CHATCODE_CLI_INTERNAL_RESUME_SESSION_ID'
19
- export const PROCESS_GENERATION_INDEX_ENV =
20
- 'CHATCODE_CLI_PROCESS_GENERATION_INDEX'
21
- export const GENERATION_SUPERVISOR_PID_ENV =
22
- 'CHATCODE_CLI_GENERATION_SUPERVISOR_PID'
23
-
24
- const UUID_PATTERN =
25
- /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
26
- const MAX_CONTROL_FILE_BYTES = 8 * 1024
27
-
28
- function removeControlFile(controlFile) {
29
- try {
30
- if (existsSync(controlFile)) unlinkSync(controlFile)
31
- } catch {
32
- // The file is private, contains no conversation content, and lives only
33
- // for this supervisor lifetime. Failure to remove it must not hide the
34
- // child process's real exit status.
35
- }
36
- }
37
-
38
- function readRotationRequest(controlFile, token) {
39
- try {
40
- const stat = statSync(controlFile)
41
- if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_CONTROL_FILE_BYTES) {
42
- return null
43
- }
44
-
45
- const request = JSON.parse(readFileSync(controlFile, 'utf8'))
46
- if (
47
- request?.version !== 1 ||
48
- request.token !== token ||
49
- typeof request.sessionId !== 'string' ||
50
- !UUID_PATTERN.test(request.sessionId) ||
51
- typeof request.cwd !== 'string' ||
52
- request.cwd.length === 0
53
- ) {
54
- return null
55
- }
56
-
57
- const cwd = isAbsolute(request.cwd)
58
- ? resolve(request.cwd)
59
- : resolve(process.cwd(), request.cwd)
60
- if (!statSync(cwd).isDirectory()) return null
61
-
62
- return { sessionId: request.sessionId, cwd }
63
- } catch {
64
- return null
65
- }
66
- }
67
-
68
- /**
69
- * Run a ChatCode worker and replace it only after an authenticated, explicit
70
- * generation-rotation request. The supervisor itself holds no transcript or
71
- * UI graph, so replacing the worker returns JSC/V8, Yoga/WASM and native
72
- * allocator high-water pages to the operating system.
73
- */
74
- export function runSupervised(command, commandArgs) {
75
- // Mirror the worker opt-in gate. The supervisor must also fail closed so
76
- // exit code 75 cannot silently replace a desktop host's worker by default.
77
- const truthy = value => /^(1|true|yes|on)$/i.test(value?.trim() ?? '')
78
- const rotationEnabled = truthy(process.env.CHATCODE_CLI_ENABLE_PROCESS_GENERATION_ROTATION) &&
79
- !truthy(process.env.CHATCODE_CLI_DISABLE_PROCESS_GENERATION_ROTATION)
80
- const token = randomUUID()
81
- const controlFile = join(
82
- tmpdir(),
83
- `chatcode-cli-generation-${process.pid}-${randomUUID()}.json`,
84
- )
85
- let cwd = process.cwd()
86
- let resumeSessionId
87
- let generation = 0
88
-
89
- try {
90
- while (true) {
91
- removeControlFile(controlFile)
92
- const env = {
93
- ...process.env,
94
- [GENERATION_CONTROL_FILE_ENV]: controlFile,
95
- [GENERATION_CONTROL_TOKEN_ENV]: token,
96
- [PROCESS_GENERATION_INDEX_ENV]: String(generation),
97
- [GENERATION_SUPERVISOR_PID_ENV]: String(process.pid),
98
- }
99
- // Never trust an inherited internal-resume value. Only a request whose
100
- // one-time control token was validated above can populate this channel.
101
- delete env[INTERNAL_RESUME_SESSION_ENV]
102
- if (!rotationEnabled) {
103
- delete env[GENERATION_CONTROL_FILE_ENV]
104
- delete env[GENERATION_CONTROL_TOKEN_ENV]
105
- delete env[GENERATION_SUPERVISOR_PID_ENV]
106
- }
107
- if (resumeSessionId) {
108
- env[INTERNAL_RESUME_SESSION_ENV] = resumeSessionId
109
- }
110
-
111
- const result = spawnSync(command, commandArgs, {
112
- cwd,
113
- env,
114
- stdio: 'inherit',
115
- windowsHide: false,
116
- })
117
-
118
- if (
119
- !rotationEnabled ||
120
- result.error ||
121
- result.signal ||
122
- result.status !== PROCESS_GENERATION_ROTATE_EXIT_CODE
123
- ) {
124
- return result
125
- }
126
-
127
- const request = readRotationRequest(controlFile, token)
128
- if (!request) {
129
- console.error(
130
- 'ChatCode CLI rejected an invalid process-generation rotation request.',
131
- )
132
- return result
133
- }
134
-
135
- resumeSessionId = request.sessionId
136
- cwd = request.cwd
137
- generation += 1
138
- }
139
- } finally {
140
- removeControlFile(controlFile)
141
- }
142
- }