@triflux/remote 10.50.2 → 10.52.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.
- package/cto/brief.mjs +0 -20
- package/cto/collect.mjs +1 -200
- package/cto/current.schema.json +2 -23
- package/cto/hygiene.mjs +2 -61
- package/cto/lake-root.mjs +0 -56
- package/cto/status.mjs +5 -187
- package/hub/index.mjs +0 -17
- package/hub/team/agent-map.json +0 -1
- package/hub/team/agents-row-attach.py +120 -0
- package/hub/team/agents-row.mjs +200 -0
- package/hub/team/ansi.mjs +1 -35
- package/hub/team/backend.mjs +9 -145
- package/hub/team/claude-daemon-control.mjs +1 -2
- package/hub/team/claude-session-projection.mjs +5 -73
- package/hub/team/cli/commands/attach.mjs +1 -17
- package/hub/team/cli/commands/control.mjs +12 -38
- package/hub/team/cli/commands/debug.mjs +2 -39
- package/hub/team/cli/commands/focus.mjs +5 -31
- package/hub/team/cli/commands/interrupt.mjs +6 -25
- package/hub/team/cli/commands/kill.mjs +1 -30
- package/hub/team/cli/commands/list.mjs +0 -16
- package/hub/team/cli/commands/send.mjs +6 -26
- package/hub/team/cli/commands/start/index.mjs +42 -133
- package/hub/team/cli/commands/start/parse-args.mjs +15 -40
- package/hub/team/cli/commands/start/start-headless.mjs +138 -105
- package/hub/team/cli/commands/start/start-mux.mjs +2 -7
- package/hub/team/cli/commands/status.mjs +2 -57
- package/hub/team/cli/commands/stop.mjs +14 -19
- package/hub/team/cli/help.mjs +4 -5
- package/hub/team/cli/manifest.mjs +5 -1
- package/hub/team/cli/render.mjs +0 -12
- package/hub/team/cli/services/runtime-mode.mjs +27 -98
- package/hub/team/cli/services/state-store.mjs +5 -20
- package/hub/team/handoff.mjs +3 -34
- package/hub/team/headless.mjs +108 -1296
- package/hub/team/orchestrator.mjs +4 -30
- package/hub/team/pane.mjs +6 -28
- package/hub/team/psmux.mjs +3 -34
- package/hub/team/remote-session.mjs +1 -2
- package/hub/team/session-context.mjs +1 -1
- package/hub/team/session.mjs +7 -91
- package/hub/team/tui-core.mjs +1 -1
- package/hub/team/tui-viewer.mjs +11 -58
- package/hub/team/tui.mjs +8 -306
- package/hub/team/uds-orchestrator.mjs +2 -580
- package/hub/team/wt-manager.mjs +2 -2
- package/hub/workers/factory.mjs +6 -84
- package/hub/workers/interface.mjs +1 -7
- package/hub/workers/lib/jsonrpc-core.mjs +11 -0
- package/hub/workers/lib/jsonrpc-ws-uds.mjs +5 -8
- package/package.json +3 -8
- package/scripts/lib/agent-route-policy.mjs +143 -62
- package/scripts/lib/cli-claude.mjs +0 -14
- package/scripts/lib/env-probe.mjs +2 -131
- package/scripts/lib/gemini-profiles.mjs +2 -69
- package/scripts/lib/legacy-mcp-cleanup.mjs +283 -0
- package/scripts/lib/machine-profile.mjs +3 -17
- package/scripts/lib/mcp-filter.mjs +109 -53
- package/scripts/lib/mcp-guard-engine.mjs +40 -98
- package/scripts/lib/mcp-health.mjs +2 -2
- package/scripts/lib/mcp-server-catalog.mjs +2 -2
- package/scripts/lib/psmux-info.mjs +11 -59
- package/scripts/lib/skill-template.mjs +0 -245
- package/hub/hub-lifecycle.mjs +0 -248
- package/hub/pipe.mjs +0 -825
- package/hub/promote-penalties.mjs +0 -138
- package/hub/public/dashboard.html +0 -416
- package/hub/schema.sql +0 -209
- package/hub/server.mjs +0 -3048
- package/hub/store-adapter.mjs +0 -182
- package/hub/store.mjs +0 -1736
- package/hub/team/build-worker-prompt.mjs +0 -92
- package/hub/team/check-mcp-hub.mjs +0 -49
- package/hub/team/claude-native-bridge.mjs +0 -987
- package/hub/team/cli/commands/start/start-in-process.mjs +0 -50
- package/hub/team/cli/commands/start/start-wt.mjs +0 -86
- package/hub/team/cli/services/hub-client.mjs +0 -264
- package/hub/team/cli/services/native-control.mjs +0 -145
- package/hub/team/codex-review.mjs +0 -520
- package/hub/team/conductor-mesh-bridge.mjs +0 -122
- package/hub/team/conductor-registry.mjs +0 -121
- package/hub/team/conductor.mjs +0 -1529
- package/hub/team/consensus-meta.mjs +0 -176
- package/hub/team/cto-auto-collect.mjs +0 -115
- package/hub/team/cto-pull.mjs +0 -31
- package/hub/team/daemon-pty-tmux-bridge.mjs +0 -146
- package/hub/team/dashboard.mjs +0 -322
- package/hub/team/event-log.mjs +0 -84
- package/hub/team/execution-mode.mjs +0 -324
- package/hub/team/extract-completion-payload.mjs +0 -110
- package/hub/team/headless-bridge-session.mjs +0 -17
- package/hub/team/health-probe.mjs +0 -435
- package/hub/team/interactive-native-launcher.mjs +0 -119
- package/hub/team/interactive-native-worker.mjs +0 -80
- package/hub/team/interactive-tui-transport.mjs +0 -479
- package/hub/team/launcher-template.mjs +0 -144
- package/hub/team/lead-control.mjs +0 -191
- package/hub/team/mcp-selector.mjs +0 -145
- package/hub/team/native-supervisor.mjs +0 -460
- package/hub/team/native.mjs +0 -727
- package/hub/team/recovery-store.mjs +0 -149
- package/hub/team/remote-probe.mjs +0 -292
- package/hub/team/remote-watcher.mjs +0 -506
- package/hub/team/runtime-strategy.mjs +0 -200
- package/hub/team/schemas/worker-completion.json +0 -39
- package/hub/team/sentinel-capture.mjs +0 -127
- package/hub/team/session-sync.mjs +0 -316
- package/hub/team/swarm-cli.mjs +0 -390
- package/hub/team/swarm-hypervisor.mjs +0 -2206
- package/hub/team/swarm-intent.mjs +0 -216
- package/hub/team/swarm-locks.mjs +0 -335
- package/hub/team/swarm-planner.mjs +0 -371
- package/hub/team/swarm-preflight.mjs +0 -366
- package/hub/team/swarm-reconciler.mjs +0 -242
- package/hub/team/synapse-cli.mjs +0 -458
- package/hub/team/synapse-http.mjs +0 -311
- package/hub/team/synapse-registry.mjs +0 -618
- package/hub/team/tui-remote-adapter.mjs +0 -377
- package/hub/team/tui-synapse.mjs +0 -223
- package/hub/team/worker-completion-validator.mjs +0 -79
- package/hub/team/worker-sandbox.mjs +0 -125
- package/hub/team/worktree-lifecycle.mjs +0 -639
- package/hub/team/wt-templates.json +0 -43
- package/hub/tools.mjs +0 -900
- package/hub/workers/antigravity-route-worker.mjs +0 -179
- package/hub/workers/codex-app-server-worker.mjs +0 -1067
- package/hub/workers/delegator-mcp.mjs +0 -1098
- package/hub/workers/lib/jsonrpc-stdio.mjs +0 -204
- package/scripts/lib/claudemd-scanner.mjs +0 -223
- package/scripts/lib/context.mjs +0 -67
- package/scripts/lib/dynamic-route-cli.mjs +0 -107
- package/scripts/lib/handoff.mjs +0 -187
- package/scripts/lib/logger.mjs +0 -122
- package/scripts/lib/stealth-fetch.mjs +0 -176
|
@@ -1,31 +1,5 @@
|
|
|
1
|
-
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
-
import {
|
|
3
|
-
basename,
|
|
4
|
-
extname,
|
|
5
|
-
isAbsolute,
|
|
6
|
-
join,
|
|
7
|
-
relative,
|
|
8
|
-
resolve,
|
|
9
|
-
} from "node:path";
|
|
10
|
-
|
|
11
|
-
const IF_TAG_RE = /{{\s*(#if\s+([A-Za-z0-9_.-]+)|\/if)\s*}}/g;
|
|
12
|
-
const PARTIAL_RE = /{{>\s*([A-Za-z0-9_./-]+)\s*}}/g;
|
|
13
|
-
const FILE_INCLUDE_RE = /{{#include\s+([A-Za-z0-9_./-]+)\s*}}/g;
|
|
14
|
-
const VARIABLE_RE = /{{\s*([A-Za-z0-9_.-]+)\s*}}/g;
|
|
15
1
|
const FRONTMATTER_RE = /^---\r?\n(?:([\s\S]*?)\r?\n)?---\r?\n?/;
|
|
16
2
|
|
|
17
|
-
function isTruthy(value) {
|
|
18
|
-
if (typeof value === "string") return value.trim().length > 0;
|
|
19
|
-
return Boolean(value);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function normalizeVariable(context, key) {
|
|
23
|
-
if (Object.hasOwn(context, key)) return context[key];
|
|
24
|
-
const upper = key.toUpperCase();
|
|
25
|
-
if (Object.hasOwn(context, upper)) return context[upper];
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
3
|
function parseBoolean(raw) {
|
|
30
4
|
if (typeof raw !== "string") return undefined;
|
|
31
5
|
if (raw === "true") return true;
|
|
@@ -120,175 +94,6 @@ function parseFrontmatterBlock(block) {
|
|
|
120
94
|
return data;
|
|
121
95
|
}
|
|
122
96
|
|
|
123
|
-
function evaluateConditionals(source, context) {
|
|
124
|
-
const matcher = new RegExp(IF_TAG_RE.source, "g");
|
|
125
|
-
|
|
126
|
-
function walk(startIndex, nested) {
|
|
127
|
-
let output = "";
|
|
128
|
-
let cursor = startIndex;
|
|
129
|
-
|
|
130
|
-
while (true) {
|
|
131
|
-
const found = matcher.exec(source);
|
|
132
|
-
if (!found) {
|
|
133
|
-
if (nested) throw new Error("Unclosed {{#if ...}} block in template");
|
|
134
|
-
output += source.slice(cursor);
|
|
135
|
-
return { output, index: source.length };
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
output += source.slice(cursor, found.index);
|
|
139
|
-
cursor = matcher.lastIndex;
|
|
140
|
-
|
|
141
|
-
const directive = found[1];
|
|
142
|
-
const flag = found[2];
|
|
143
|
-
if (directive.startsWith("#if")) {
|
|
144
|
-
const nestedResult = walk(cursor, true);
|
|
145
|
-
cursor = nestedResult.index;
|
|
146
|
-
matcher.lastIndex = cursor;
|
|
147
|
-
if (isTruthy(normalizeVariable(context, flag))) {
|
|
148
|
-
output += nestedResult.output;
|
|
149
|
-
}
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (!nested) {
|
|
154
|
-
throw new Error("Unexpected {{/if}} without matching {{#if ...}}");
|
|
155
|
-
}
|
|
156
|
-
return { output, index: cursor };
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return walk(0, false).output;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function renderPartials(source, context, partials, options, includeStack) {
|
|
164
|
-
return source.replace(PARTIAL_RE, (_full, partialName) => {
|
|
165
|
-
const partial = partials[partialName];
|
|
166
|
-
if (partial == null) {
|
|
167
|
-
throw new Error(`Missing partial: ${partialName}`);
|
|
168
|
-
}
|
|
169
|
-
if (includeStack.includes(partialName)) {
|
|
170
|
-
const chain = [...includeStack, partialName].join(" -> ");
|
|
171
|
-
throw new Error(`Circular partial include: ${chain}`);
|
|
172
|
-
}
|
|
173
|
-
return renderWithContext(partial, context, partials, options, [
|
|
174
|
-
...includeStack,
|
|
175
|
-
partialName,
|
|
176
|
-
]);
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function normalizeIncludeName(includeName) {
|
|
181
|
-
return includeName.replace(/\\/g, "/");
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function resolveIncludeContent(includeName, options) {
|
|
185
|
-
const normalizedName = normalizeIncludeName(includeName);
|
|
186
|
-
if (Object.hasOwn(options.includes, normalizedName)) {
|
|
187
|
-
return options.includes[normalizedName];
|
|
188
|
-
}
|
|
189
|
-
if (!options.includeBaseDir) {
|
|
190
|
-
throw new Error(`Missing include: ${normalizedName}`);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const baseDir = resolve(options.includeBaseDir);
|
|
194
|
-
const fullPath = resolve(baseDir, normalizedName);
|
|
195
|
-
const relativePath = relative(baseDir, fullPath);
|
|
196
|
-
const isInsideBaseDir =
|
|
197
|
-
relativePath === "" ||
|
|
198
|
-
(!relativePath.startsWith("..") && !isAbsolute(relativePath));
|
|
199
|
-
|
|
200
|
-
if (!isInsideBaseDir || !existsSync(fullPath)) {
|
|
201
|
-
throw new Error(`Missing include: ${normalizedName}`);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const content = readFileSync(fullPath, "utf8");
|
|
205
|
-
options.includes[normalizedName] = content;
|
|
206
|
-
return content;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function renderFileIncludes(source, context, partials, options, includeStack) {
|
|
210
|
-
return source.replace(FILE_INCLUDE_RE, (_full, includeName) => {
|
|
211
|
-
const normalizedName = normalizeIncludeName(includeName);
|
|
212
|
-
const content = resolveIncludeContent(normalizedName, options);
|
|
213
|
-
if (includeStack.includes(normalizedName)) {
|
|
214
|
-
const chain = [...includeStack, normalizedName].join(" -> ");
|
|
215
|
-
throw new Error(`Circular template include: ${chain}`);
|
|
216
|
-
}
|
|
217
|
-
return renderWithContext(content, context, partials, options, [
|
|
218
|
-
...includeStack,
|
|
219
|
-
normalizedName,
|
|
220
|
-
]);
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function renderVariables(source, context) {
|
|
225
|
-
return source.replace(VARIABLE_RE, (full, key) => {
|
|
226
|
-
const value = normalizeVariable(context, key);
|
|
227
|
-
if (value == null) {
|
|
228
|
-
throw new Error(`Missing template variable: ${key}`);
|
|
229
|
-
}
|
|
230
|
-
return String(value);
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function renderWithContext(
|
|
235
|
-
source,
|
|
236
|
-
context,
|
|
237
|
-
partials,
|
|
238
|
-
options,
|
|
239
|
-
includeStack = [],
|
|
240
|
-
) {
|
|
241
|
-
const afterIf = evaluateConditionals(source, context);
|
|
242
|
-
const afterPartials = renderPartials(
|
|
243
|
-
afterIf,
|
|
244
|
-
context,
|
|
245
|
-
partials,
|
|
246
|
-
options,
|
|
247
|
-
includeStack,
|
|
248
|
-
);
|
|
249
|
-
const afterFileIncludes = renderFileIncludes(
|
|
250
|
-
afterPartials,
|
|
251
|
-
context,
|
|
252
|
-
partials,
|
|
253
|
-
options,
|
|
254
|
-
includeStack,
|
|
255
|
-
);
|
|
256
|
-
return renderVariables(afterFileIncludes, context);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function readAllTemplateFiles(rootDir, currentDir = rootDir) {
|
|
260
|
-
if (!rootDir || !existsSync(rootDir)) return [];
|
|
261
|
-
|
|
262
|
-
const entries = readdirSync(currentDir, { withFileTypes: true }).sort(
|
|
263
|
-
(left, right) => left.name.localeCompare(right.name),
|
|
264
|
-
);
|
|
265
|
-
const files = [];
|
|
266
|
-
|
|
267
|
-
for (const entry of entries) {
|
|
268
|
-
const fullPath = join(currentDir, entry.name);
|
|
269
|
-
if (entry.isDirectory()) {
|
|
270
|
-
files.push(...readAllTemplateFiles(rootDir, fullPath));
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (!entry.isFile()) continue;
|
|
275
|
-
if (!entry.name.endsWith(".md") && !entry.name.endsWith(".tmpl")) continue;
|
|
276
|
-
|
|
277
|
-
const relativePath = relative(rootDir, fullPath).replace(/\\/g, "/");
|
|
278
|
-
const content = readFileSync(fullPath, "utf8");
|
|
279
|
-
files.push({ fullPath, relativePath, content });
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
return files;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function setPartial(partials, key, value) {
|
|
286
|
-
if (!key) return;
|
|
287
|
-
if (!Object.hasOwn(partials, key)) {
|
|
288
|
-
partials[key] = value;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
97
|
export function parseFrontmatter(source) {
|
|
293
98
|
const match = source.match(FRONTMATTER_RE);
|
|
294
99
|
if (!match) return { data: {}, body: source };
|
|
@@ -297,53 +102,3 @@ export function parseFrontmatter(source) {
|
|
|
297
102
|
const body = source.slice(match[0].length);
|
|
298
103
|
return { data, body };
|
|
299
104
|
}
|
|
300
|
-
|
|
301
|
-
export function buildSkillTemplateContext({
|
|
302
|
-
frontmatter = {},
|
|
303
|
-
skillDirName = "",
|
|
304
|
-
} = {}) {
|
|
305
|
-
const context = { ...frontmatter };
|
|
306
|
-
const skillName = String(frontmatter.name || skillDirName || "").trim();
|
|
307
|
-
const skillDescription = String(frontmatter.description || "").trim();
|
|
308
|
-
|
|
309
|
-
let deep = frontmatter.DEEP ?? frontmatter.deep;
|
|
310
|
-
if (typeof deep === "string") {
|
|
311
|
-
const parsed = parseBoolean(deep.trim().toLowerCase());
|
|
312
|
-
deep = parsed ?? deep;
|
|
313
|
-
}
|
|
314
|
-
if (typeof deep !== "boolean") {
|
|
315
|
-
deep = /(^|[-_])deep($|[-_])/i.test(skillName);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
context.SKILL_NAME = skillName;
|
|
319
|
-
context.SKILL_DESCRIPTION = skillDescription;
|
|
320
|
-
context.DEEP = deep;
|
|
321
|
-
return context;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export function loadTemplatePartials(partialsDir) {
|
|
325
|
-
const files = readAllTemplateFiles(partialsDir);
|
|
326
|
-
const partials = {};
|
|
327
|
-
|
|
328
|
-
for (const file of files) {
|
|
329
|
-
const extension = extname(file.relativePath);
|
|
330
|
-
const withoutExt = extension
|
|
331
|
-
? file.relativePath.slice(0, -extension.length)
|
|
332
|
-
: file.relativePath;
|
|
333
|
-
const normalized = withoutExt.replace(/\\/g, "/");
|
|
334
|
-
const base = basename(normalized);
|
|
335
|
-
|
|
336
|
-
setPartial(partials, normalized, file.content);
|
|
337
|
-
setPartial(partials, base, file.content);
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
return partials;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
export function renderSkillTemplate(template, context = {}, options = {}) {
|
|
344
|
-
const { partials = {}, includes = {}, includeBaseDir = "" } = options;
|
|
345
|
-
return renderWithContext(template, context, partials, {
|
|
346
|
-
includeBaseDir,
|
|
347
|
-
includes: { ...includes },
|
|
348
|
-
});
|
|
349
|
-
}
|
package/hub/hub-lifecycle.mjs
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
// hub/hub-lifecycle.mjs — Hub process lifecycle helpers
|
|
2
|
-
|
|
3
|
-
import { execFileSync } from "node:child_process";
|
|
4
|
-
import { readFileSync } from "node:fs";
|
|
5
|
-
import { homedir } from "node:os";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
|
|
8
|
-
export const HUB_DEFAULT_PORT = 27888;
|
|
9
|
-
|
|
10
|
-
const HUB_PID_FILE = join(homedir(), ".claude", "cache", "tfx-hub", "hub.pid");
|
|
11
|
-
const EPHEMERAL_ENV_KEYS = [
|
|
12
|
-
"TFX_WORKER_SANDBOX_SCOPE",
|
|
13
|
-
"TFX_WORKER_INDEX",
|
|
14
|
-
"TFX_TEAM_TASK_ID",
|
|
15
|
-
"TFX_TEAM_AGENT_NAME",
|
|
16
|
-
"TFX_EPHEMERAL",
|
|
17
|
-
];
|
|
18
|
-
const WORKTREE_CWD_PATTERNS = [
|
|
19
|
-
/\/\.claude\/worktrees\//u,
|
|
20
|
-
/\/\.worktrees\//u,
|
|
21
|
-
/\/\.codex-swarm\/wt-[^/]+(?:\/|$)/u,
|
|
22
|
-
/(^|\/)wt-[^/]+(?:\/|$)/u,
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
function parsePositivePort(value) {
|
|
26
|
-
const parsed = Number.parseInt(String(value ?? ""), 10);
|
|
27
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function parsePortFromHubUrl(value) {
|
|
31
|
-
try {
|
|
32
|
-
return parsePositivePort(new URL(String(value)).port);
|
|
33
|
-
} catch {
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isHubServerCommand(command) {
|
|
39
|
-
return /(^|[\\/,\s])hub[\\/]server\.mjs(?=$|[\s"'`])/i.test(
|
|
40
|
-
String(command || ""),
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function isWorktreeOrEphemeralHubContext({
|
|
45
|
-
cwd = process.cwd(),
|
|
46
|
-
env = process.env,
|
|
47
|
-
} = {}) {
|
|
48
|
-
const normalizedCwd = String(cwd || "").replace(/\\/g, "/");
|
|
49
|
-
if (WORKTREE_CWD_PATTERNS.some((pattern) => pattern.test(normalizedCwd))) {
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
return EPHEMERAL_ENV_KEYS.some((key) => String(env?.[key] || "").length > 0);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function resolveHubPortForContext({
|
|
56
|
-
port,
|
|
57
|
-
env = process.env,
|
|
58
|
-
cwd = process.cwd(),
|
|
59
|
-
defaultPort = HUB_DEFAULT_PORT,
|
|
60
|
-
} = {}) {
|
|
61
|
-
const envPort =
|
|
62
|
-
parsePositivePort(port) ?? parsePositivePort(env?.TFX_HUB_PORT);
|
|
63
|
-
const urlPort = parsePortFromHubUrl(env?.TFX_HUB_URL);
|
|
64
|
-
const resolvedPort = envPort ?? urlPort ?? defaultPort;
|
|
65
|
-
if (
|
|
66
|
-
resolvedPort !== defaultPort &&
|
|
67
|
-
// Test-only opt-in seam: TFX_HUB_ALLOW_EPHEMERAL_PORT=1 lets an ephemeral
|
|
68
|
-
// context (worktree cwd / TFX_TEAM_* env) honor the resolved port instead
|
|
69
|
-
// of clamping to the canonical default. Default-off — production unchanged.
|
|
70
|
-
String(env?.TFX_HUB_ALLOW_EPHEMERAL_PORT ?? "") !== "1" &&
|
|
71
|
-
isWorktreeOrEphemeralHubContext({ cwd, env })
|
|
72
|
-
) {
|
|
73
|
-
return defaultPort;
|
|
74
|
-
}
|
|
75
|
-
return resolvedPort;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function collectHubProcesses(
|
|
79
|
-
psOutput = "",
|
|
80
|
-
{ currentPid = process.pid } = {},
|
|
81
|
-
) {
|
|
82
|
-
const ownPid = Number(currentPid);
|
|
83
|
-
return String(psOutput)
|
|
84
|
-
.split(/\r?\n/u)
|
|
85
|
-
.flatMap((line) => {
|
|
86
|
-
const match = line.match(/^\s*(\d+)\s+(\d+)\s+(.+)$/u);
|
|
87
|
-
if (!match) return [];
|
|
88
|
-
const pid = Number.parseInt(match[1], 10);
|
|
89
|
-
const ppid = Number.parseInt(match[2], 10);
|
|
90
|
-
const command = match[3].trim();
|
|
91
|
-
if (!Number.isFinite(pid) || pid === ownPid) return [];
|
|
92
|
-
if (!/\bnode(?:\s|$)|\/node(?:\s|$)/u.test(command)) return [];
|
|
93
|
-
if (!isHubServerCommand(command)) return [];
|
|
94
|
-
return [{ pid, ppid, command }];
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function readPidFilePid({
|
|
99
|
-
pidFilePath = HUB_PID_FILE,
|
|
100
|
-
readFile = readFileSync,
|
|
101
|
-
} = {}) {
|
|
102
|
-
try {
|
|
103
|
-
const info = JSON.parse(readFile(pidFilePath, "utf8"));
|
|
104
|
-
const pid = Number(info?.pid);
|
|
105
|
-
return Number.isFinite(pid) && pid > 0 ? pid : null;
|
|
106
|
-
} catch {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function findListeningPidsForPort(
|
|
112
|
-
port = HUB_DEFAULT_PORT,
|
|
113
|
-
{ execFileSyncFn = execFileSync } = {},
|
|
114
|
-
) {
|
|
115
|
-
try {
|
|
116
|
-
const output = execFileSyncFn(
|
|
117
|
-
"lsof",
|
|
118
|
-
["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-t"],
|
|
119
|
-
{
|
|
120
|
-
encoding: "utf8",
|
|
121
|
-
timeout: 1000,
|
|
122
|
-
maxBuffer: 1024 * 1024,
|
|
123
|
-
},
|
|
124
|
-
);
|
|
125
|
-
return String(output)
|
|
126
|
-
.split(/\r?\n/u)
|
|
127
|
-
.map((line) => Number.parseInt(line.trim(), 10))
|
|
128
|
-
.filter((pid) => Number.isFinite(pid) && pid > 0);
|
|
129
|
-
} catch {
|
|
130
|
-
return [];
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async function waitForExit(
|
|
135
|
-
pid,
|
|
136
|
-
{ killFn = process.kill, graceMs = 5000, pollMs = 100 } = {},
|
|
137
|
-
) {
|
|
138
|
-
const deadline = Date.now() + Math.max(0, graceMs);
|
|
139
|
-
while (Date.now() <= deadline) {
|
|
140
|
-
try {
|
|
141
|
-
killFn(pid, 0);
|
|
142
|
-
} catch {
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
146
|
-
}
|
|
147
|
-
try {
|
|
148
|
-
killFn(pid, 0);
|
|
149
|
-
return false;
|
|
150
|
-
} catch {
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export async function reapExistingHubProcesses({
|
|
156
|
-
currentPid = process.pid,
|
|
157
|
-
pidFilePath = HUB_PID_FILE,
|
|
158
|
-
execFileSyncFn = execFileSync,
|
|
159
|
-
killFn = process.kill,
|
|
160
|
-
readPidFileFn,
|
|
161
|
-
findListeningPidsForPortFn,
|
|
162
|
-
waitForExitFn,
|
|
163
|
-
graceMs = 5000,
|
|
164
|
-
pollMs = 100,
|
|
165
|
-
} = {}) {
|
|
166
|
-
let output = "";
|
|
167
|
-
try {
|
|
168
|
-
output = execFileSyncFn("ps", ["-axo", "pid,ppid,command"], {
|
|
169
|
-
encoding: "utf8",
|
|
170
|
-
timeout: 1000,
|
|
171
|
-
maxBuffer: 1024 * 1024,
|
|
172
|
-
});
|
|
173
|
-
} catch {
|
|
174
|
-
return {
|
|
175
|
-
candidates: [],
|
|
176
|
-
reaped: [],
|
|
177
|
-
preserved: {
|
|
178
|
-
currentPid: Number(currentPid),
|
|
179
|
-
pidFilePid: null,
|
|
180
|
-
defaultPortPids: [],
|
|
181
|
-
},
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const current = Number(currentPid);
|
|
186
|
-
const pidFileInfo =
|
|
187
|
-
typeof readPidFileFn === "function"
|
|
188
|
-
? readPidFileFn()
|
|
189
|
-
: { pid: readPidFilePid({ pidFilePath }) };
|
|
190
|
-
const pidFilePid = parsePositivePort(pidFileInfo?.pid);
|
|
191
|
-
const defaultPortPids = [
|
|
192
|
-
...new Set(
|
|
193
|
-
(typeof findListeningPidsForPortFn === "function"
|
|
194
|
-
? findListeningPidsForPortFn(HUB_DEFAULT_PORT)
|
|
195
|
-
: findListeningPidsForPort(HUB_DEFAULT_PORT, { execFileSyncFn })
|
|
196
|
-
).map(Number),
|
|
197
|
-
),
|
|
198
|
-
].filter((pid) => Number.isFinite(pid) && pid > 0);
|
|
199
|
-
|
|
200
|
-
const preservedPids = new Set(
|
|
201
|
-
[current, pidFilePid, ...defaultPortPids].filter(
|
|
202
|
-
(pid) => Number.isFinite(pid) && pid > 0,
|
|
203
|
-
),
|
|
204
|
-
);
|
|
205
|
-
const candidates = collectHubProcesses(output, { currentPid: current });
|
|
206
|
-
const reaped = [];
|
|
207
|
-
const failed = [];
|
|
208
|
-
|
|
209
|
-
for (const processInfo of candidates) {
|
|
210
|
-
if (preservedPids.has(processInfo.pid)) continue;
|
|
211
|
-
try {
|
|
212
|
-
killFn(processInfo.pid, "SIGTERM");
|
|
213
|
-
const exited =
|
|
214
|
-
typeof waitForExitFn === "function"
|
|
215
|
-
? await waitForExitFn(processInfo.pid, { killFn, graceMs, pollMs })
|
|
216
|
-
: await waitForExit(processInfo.pid, { killFn, graceMs, pollMs });
|
|
217
|
-
if (!exited) {
|
|
218
|
-
try {
|
|
219
|
-
killFn(processInfo.pid, "SIGKILL");
|
|
220
|
-
} catch (error) {
|
|
221
|
-
failed.push({
|
|
222
|
-
pid: processInfo.pid,
|
|
223
|
-
signal: "SIGKILL",
|
|
224
|
-
error: String(error?.message || error),
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
reaped.push(processInfo);
|
|
229
|
-
} catch (error) {
|
|
230
|
-
failed.push({
|
|
231
|
-
pid: processInfo.pid,
|
|
232
|
-
signal: "SIGTERM",
|
|
233
|
-
error: String(error?.message || error),
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return {
|
|
239
|
-
candidates,
|
|
240
|
-
reaped,
|
|
241
|
-
failed,
|
|
242
|
-
preserved: {
|
|
243
|
-
currentPid: current,
|
|
244
|
-
pidFilePid,
|
|
245
|
-
defaultPortPids,
|
|
246
|
-
},
|
|
247
|
-
};
|
|
248
|
-
}
|