@nowcrew/daemon 0.6.88-beta.27 → 0.6.88-beta.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +7 -0
  2. package/dist/agent-ability/materializer.js +21 -10
  3. package/dist/agent-environment-contract.js +55 -0
  4. package/dist/agent-environment-control.js +24 -0
  5. package/dist/agent-environment-execution.js +98 -0
  6. package/dist/agent-environments.js +275 -0
  7. package/dist/config.js +7 -0
  8. package/dist/execution-runner.js +2 -0
  9. package/dist/list-models.js +12 -12
  10. package/dist/local-executor.js +20 -48
  11. package/dist/local-runtime-environment.js +50 -0
  12. package/dist/machine-environment-cache.js +147 -0
  13. package/dist/machine-environment-contract.js +10 -0
  14. package/dist/machine-environment-control.js +27 -0
  15. package/dist/machine-environment-policy.js +75 -0
  16. package/dist/machine-environment-process.js +110 -0
  17. package/dist/machine-info.js +24 -16
  18. package/dist/machine-runtime-environment.js +189 -0
  19. package/dist/machine-shell-environment-capture.js +135 -0
  20. package/dist/machine-windows-environment-capture.js +215 -0
  21. package/dist/machine-windows-environment-script.js +158 -0
  22. package/dist/marketplace-controller.js +75 -0
  23. package/dist/marketplace-protocol.js +38 -0
  24. package/dist/marketplace-runner.js +234 -0
  25. package/dist/marketplace-serve.js +17 -0
  26. package/dist/message-file-preview.js +239 -0
  27. package/dist/provider-env.js +26 -12
  28. package/dist/runner.js +14 -0
  29. package/dist/runtime-environment-merge.js +16 -0
  30. package/dist/runtime-health-handler.js +10 -0
  31. package/dist/runtime-health.js +3 -1
  32. package/dist/runtime-path.js +22 -12
  33. package/dist/runtime-probe.js +2 -2
  34. package/dist/runtimes/claude-models.js +20 -9
  35. package/dist/runtimes/deepseek-harness.js +17 -12
  36. package/dist/runtimes/hermes-models.js +2 -1
  37. package/dist/runtimes/kimi-acp-runner.js +7 -6
  38. package/dist/runtimes/opencode.js +2 -2
  39. package/dist/serve.js +58 -105
  40. package/dist/token.js +0 -1
  41. package/dist/workspace-control-handler.js +27 -0
  42. package/dist/workspace-fs.js +66 -15
  43. package/package.json +2 -2
@@ -1,15 +1,17 @@
1
1
  import { deepSeekHarnessLauncher } from "./runtimes/deepseek-harness.js";
2
+ import { buildLocalRuntimeEnvironment } from "./local-runtime-environment.js";
3
+ import { environmentForExecution } from "./agent-environments.js";
4
+ import { prepareEnvironmentExecutionSnapshot } from "./agent-environment-execution.js";
2
5
  import { createInterface } from "node:readline";
3
6
  import { readFile, rm, writeFile } from "node:fs/promises";
4
- import { delimiter, join } from "node:path";
7
+ import { join } from "node:path";
5
8
  import { cleanupSelfTrainingRunDir } from "./self-training-workspace.js";
6
9
  import { prepareWorkspace, rotateAgentSession, safeKey, } from "./workspace.js";
7
10
  import { CLAUDE_ADDITIONAL_DIRECTORY_INSTRUCTIONS_ENV, isClaudeAdditionalDirectoryInstructionsSupported, probeClaudeVersion, spawnClaude, } from "./runtimes/claude.js";
8
11
  import { spawnCodex } from "./runtimes/codex.js";
9
12
  import { DEEPSEEK_CODEX_MODEL, DEEPSEEK_CODEX_REASONING_LEVELS, materializeDeepSeekCodexHome, } from "./runtimes/codex-deepseek-config.js";
10
- import { spawnKimi, KIMI_EFFORT_LEVELS } from "./runtimes/kimi.js";
13
+ import { spawnKimi } from "./runtimes/kimi.js";
11
14
  import { applyDeepSeekHarnessMachineEnv, applyProviderEnv, providerFingerprint, } from "./provider-env.js";
12
- import { augmentedPath } from "./runtime-path.js";
13
15
  import { deliverCloudReports, prepareCloudReportExecution } from "./cloud-report-uploader.js";
14
16
  import { reportDeliveryContext } from "./cloud-report-receipt.js";
15
17
  import { extractFinalText, extractRunMeta, normalizeEvent, parseLine, } from "./normalize.js";
@@ -39,9 +41,7 @@ import { diffMemoryPruneNotes, evaluateMemoryPrunePostcondition, inspectMemoryPr
39
41
  import { createLocalMemoryTelemetry, logLocalMemoryContextPrepareFailure, logLocalMemoryDiagnosticsFailure, } from "./local-memory-telemetry.js";
40
42
  import { CodexStartupStageParser } from "./codex-startup-stage.js";
41
43
  import { formatRuntimeStartupError } from "./runtime-startup-error.js";
42
- import { machinePackageManagerEnv } from "./package-manager-env.js";
43
44
  import { logRuntimeEnvironmentContract } from "./runtime-environment-telemetry.js";
44
- import { sanitizeEnvVars } from "./runtime-env.js";
45
45
  import { commitLocalMemoryResultAfterExit, initializeLocalMemoryV2ForWorkspace, isLocalMemoryV2Eligible, localMemoryResultPath as makeLocalMemoryResultPath, } from "./local-memory/runtime.js";
46
46
  import { prepareLocalMemoryPrompt } from "./local-memory/prompt.js";
47
47
  import { reportLocalMemoryV2Deferred, resolveLocalMemoryV2Rollout } from "./local-memory/rollout.js";
@@ -252,10 +252,12 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
252
252
  let memoryPruneBeforeSnapshot = null;
253
253
  let memoryPruneSharedWriteKey = null;
254
254
  let localMemoryResultPath = null;
255
+ let environmentSnapshot;
255
256
  let executionWorkspace = workspace;
256
257
  let localMemoryTelemetry = null;
257
258
  let nativeThreadContext = null;
258
- const inheritedEnv = { ...process.env };
259
+ const machineSnapshot = dependencies.machineEnvironment ? await awaitWithCancellation(dependencies.machineEnvironment.acquire(), dependencies.cancellation) : undefined;
260
+ const inheritedEnv = { ...(machineSnapshot?.env ?? process.env) };
259
261
  const reportRoots = {
260
262
  root: workspace.dir,
261
263
  ...(input.launch.qaReportRoot === undefined ? {} : { qaReportRoot: input.launch.qaReportRoot }),
@@ -479,45 +481,8 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
479
481
  nativeThreadContext = new NativeThreadContext(join(executionWorkspace.runDir, `.crew-context-${input.executionId}.json`), contextThreadId, runtime.name, resumeSessionId, prior);
480
482
  await nativeThreadContext.initialize();
481
483
  }
482
- const baseEnv = {
483
- ...inheritedEnv,
484
- ...sanitizeEnvVars(providerConfig.envVars),
485
- ...(input.trainingProjectContext === undefined
486
- ? {}
487
- : machinePackageManagerEnv(inheritedEnv)),
488
- ...input.launch.systemEnv,
489
- PATH: `${workspace.crewDir}${delimiter}${augmentedPath()}`,
490
- CREW_SERVER_URL: input.launch.serverUrl,
491
- CREW_TOKEN: input.launch.token,
492
- CREW_CHANNEL: input.channelId,
493
- CREW_HOME: crewHome,
494
- // Empty values override any stale inherited hints from an enclosing runtime.
495
- CREW_THREAD_ROOT: contextThreadId ?? "",
496
- CREW_THREAD_CONTEXT_FILE: nativeThreadContext?.path ?? "",
497
- // Keep the daemon-selected storage root authoritative for runtimes.
498
- // In profile-backed NowWork sessions the daemon config may differ from
499
- // the daemon process environment, so inheriting CREW_AGENTS_ROOT is not
500
- // sufficient for QA/project diagnostics.
501
- CREW_AGENTS_ROOT: input.launch.agentsRoot,
502
- ...(input.launch.reposRoot === undefined ? {} : { NOWWORK_REPOS_ROOT: input.launch.reposRoot }),
503
- ...(input.launch.qaReportRoot === undefined ? {} : { QA_REPORT_HUB: input.launch.qaReportRoot }),
504
- // Expose only the training root; ability-specific workspace layout stays
505
- // owned by the training package rather than the NowWork daemon.
506
- QA_RUNTIME_ROOT: join(crewHome, "training"),
507
- QA_REPORT_DELIVERY_CONTEXT: JSON.stringify(reportContext),
508
- CREW_TASK_DIR: executionWorkspace.runDir,
509
- CREW_TASK_LOG: executionWorkspace.workLogPath,
510
- ...(input.wakeMessageId ? { CREW_WAKE_MESSAGE_ID: input.wakeMessageId } : {}),
511
- XDG_CONFIG_HOME: join(workspace.homeDir, ".config"),
512
- XDG_DATA_HOME: join(workspace.homeDir, ".local", "share"),
513
- XDG_CACHE_HOME: join(workspace.homeDir, ".cache"),
514
- GH_CONFIG_DIR: join(workspace.homeDir, ".config", "gh"),
515
- CLOUDSDK_CONFIG: join(workspace.homeDir, ".config", "gcloud"),
516
- ...(runtime.name === "kimi" && runtime.reasoning
517
- && KIMI_EFFORT_LEVELS.includes(runtime.reasoning)
518
- ? { KIMI_MODEL_THINKING_EFFORT: runtime.reasoning }
519
- : {}),
520
- };
484
+ const baseEnv = buildLocalRuntimeEnvironment({ input, workspace, executionWorkspace, inheritedEnv, providerConfig,
485
+ crewHome, contextThreadId, nativeThreadContextPath: nativeThreadContext?.path, reportContext, runtime });
521
486
  const providerEnv = applyProviderEnv(baseEnv, runtime.name, providerConfig, workspace.homeDir, materializedDeepSeekHome);
522
487
  const childEnv = runtime.name === "deepseek-harness"
523
488
  ? applyDeepSeekHarnessMachineEnv(providerEnv, inheritedEnv)
@@ -535,10 +500,15 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
535
500
  const abilityContext = input.abilityRelease !== undefined && dependencies.abilityRelease !== undefined
536
501
  ? await dependencies.abilityRelease.prepareAndLaunch(input.launch.agentsRoot, input.handle, input.abilityRelease, async (context) => context)
537
502
  : undefined;
503
+ if (providerConfig.environmentPlan !== undefined && !isolatedMaintenance) {
504
+ environmentSnapshot = await prepareEnvironmentExecutionSnapshot({ plan: providerConfig.environmentPlan, agentRoot: workspace.dir, runDir: executionWorkspace.runDir, executionId: input.executionId, ambient: inheritedEnv, runtime: runtime.name, providerConfig, runtimeEnvironment: childEnv, authoritativeKeys: Object.keys(input.launch.systemEnv ?? {}),
505
+ executionBaseEnvironment: childEnv,
506
+ ...(machineSnapshot === undefined ? {} : { machineEnvironmentVersion: { bootId: machineSnapshot.status.bootId, generation: machineSnapshot.status.generation } }),
507
+ selectedEnvironment: environmentForExecution(providerConfig.environmentPlan, workspace.dir, input.projectContext?.primary?.root ?? executionWorkspace.runDir, input.projectContext?.primary?.projectId),
508
+ projectRoots: [...(input.projectContext?.primary ? [input.projectContext.primary] : []), ...(input.projectContext?.secondary ?? [])] });
509
+ }
538
510
  const prepareRuntimeLaunch = async (runtimeRoot) => {
539
- const effectiveSystemPrompt = abilityContext === undefined
540
- ? systemPrompt
541
- : `${systemPrompt}\n\n${abilityContext.prompt}`;
511
+ const effectiveSystemPrompt = [systemPrompt, abilityContext?.prompt, environmentSnapshot?.prompt].filter(Boolean).join("\n\n");
542
512
  if (Buffer.byteLength(effectiveSystemPrompt, "utf8") + Buffer.byteLength(memoryPrompt.wakePromptSuffix, "utf8")
543
513
  > (input.maxSystemPromptBytes ?? Number.MAX_SAFE_INTEGER)) {
544
514
  throw new Error("ability_runtime_context_size_exceeded");
@@ -1123,6 +1093,8 @@ async function executeLocalUnlocked(input, callbacks, dependencies) {
1123
1093
  active_prune_count: activePruneCount,
1124
1094
  });
1125
1095
  }
1096
+ if (environmentSnapshot)
1097
+ await rm(environmentSnapshot.directory, { recursive: true, force: true }).catch(() => { });
1126
1098
  const attachmentDirectories = new Set([
1127
1099
  ...(knownAttachmentDirectory === null ? [] : [knownAttachmentDirectory]),
1128
1100
  ...(materialized === null ? [] : [materialized.directory]),
@@ -0,0 +1,50 @@
1
+ import { delimiter, join } from "node:path";
2
+ import { environmentForExecution } from "./agent-environments.js";
3
+ import { augmentedPath } from "./runtime-path.js";
4
+ import { sanitizeEnvVars } from "./runtime-env.js";
5
+ import { machinePackageManagerEnv } from "./package-manager-env.js";
6
+ import { KIMI_EFFORT_LEVELS } from "./runtimes/kimi.js";
7
+ import { mergeRuntimeEnvironments } from "./runtime-environment-merge.js";
8
+ /** 机器、目录与受控运行字段分层合并,Windows 不依赖对象中键名的偶然顺序。 */
9
+ export function buildLocalRuntimeEnvironment(context) {
10
+ const { input, workspace, executionWorkspace, inheritedEnv, providerConfig, crewHome, contextThreadId, nativeThreadContextPath, reportContext, runtime } = context;
11
+ const agentEnvironment = sanitizeEnvVars(providerConfig.environmentPlan === undefined ? providerConfig.envVars
12
+ : environmentForExecution(providerConfig.environmentPlan, workspace.dir, input.projectContext?.primary?.root ?? executionWorkspace.runDir, input.projectContext?.primary?.projectId));
13
+ return mergeRuntimeEnvironments([
14
+ inheritedEnv, agentEnvironment,
15
+ input.trainingProjectContext === undefined ? {} : machinePackageManagerEnv(inheritedEnv),
16
+ input.launch.systemEnv ?? {}, {
17
+ PATH: `${workspace.crewDir}${delimiter}${augmentedPath(inheritedEnv, {}, process.platform, "append")}`,
18
+ CREW_SERVER_URL: input.launch.serverUrl,
19
+ CREW_TOKEN: input.launch.token,
20
+ CREW_CHANNEL: input.channelId,
21
+ CREW_HOME: crewHome,
22
+ // Empty values override any stale inherited hints from an enclosing runtime.
23
+ CREW_THREAD_ROOT: contextThreadId ?? "",
24
+ CREW_THREAD_CONTEXT_FILE: nativeThreadContextPath ?? "",
25
+ // Keep the daemon-selected storage root authoritative for runtimes.
26
+ // In profile-backed NowWork sessions the daemon config may differ from
27
+ // the daemon process environment, so inheriting CREW_AGENTS_ROOT is not
28
+ // sufficient for QA/project diagnostics.
29
+ CREW_AGENTS_ROOT: input.launch.agentsRoot,
30
+ ...(input.launch.reposRoot === undefined ? {} : { NOWWORK_REPOS_ROOT: input.launch.reposRoot }),
31
+ ...(input.launch.qaReportRoot === undefined ? {} : { QA_REPORT_HUB: input.launch.qaReportRoot }),
32
+ // Expose only the training root; ability-specific workspace layout stays
33
+ // owned by the training package rather than the NowWork daemon.
34
+ QA_RUNTIME_ROOT: join(crewHome, "training"),
35
+ QA_REPORT_DELIVERY_CONTEXT: JSON.stringify(reportContext),
36
+ CREW_TASK_DIR: executionWorkspace.runDir,
37
+ CREW_TASK_LOG: executionWorkspace.workLogPath,
38
+ ...(input.wakeMessageId ? { CREW_WAKE_MESSAGE_ID: input.wakeMessageId } : {}),
39
+ XDG_CONFIG_HOME: join(workspace.homeDir, ".config"),
40
+ XDG_DATA_HOME: join(workspace.homeDir, ".local", "share"),
41
+ XDG_CACHE_HOME: join(workspace.homeDir, ".cache"),
42
+ GH_CONFIG_DIR: join(workspace.homeDir, ".config", "gh"),
43
+ CLOUDSDK_CONFIG: join(workspace.homeDir, ".config", "gcloud"),
44
+ ...(runtime.name === "kimi" && runtime.reasoning
45
+ && KIMI_EFFORT_LEVELS.includes(runtime.reasoning)
46
+ ? { KIMI_MODEL_THINKING_EFFORT: runtime.reasoning }
47
+ : {}),
48
+ },
49
+ ]);
50
+ }
@@ -0,0 +1,147 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { MachineEnvironmentCaptureError } from "./machine-environment-contract.js";
3
+ import { copyMachineEnvironment, machineCaptureStartupEnvironment, resolveMachineAccount, restoreMachineEnvironmentOwnership, selectMachineEnvironmentSource, validateMachineEnvironment } from "./machine-environment-policy.js";
4
+ import { captureShellMachineEnvironment, machineEnvironmentSourceFingerprint, machineShellSourceLocations } from "./machine-shell-environment-capture.js";
5
+ function frozenStatus(status) {
6
+ return Object.freeze({ ...status, sourceLocations: Object.freeze([...new Set(status.sourceLocations)].slice(0, 8)) });
7
+ }
8
+ /** 测试或明确关闭机器环境时使用;永远不会启动 shell,也不读取真实用户 rc。 */
9
+ export function createStaticMachineEnvironmentProvider(startupEnv = {}) {
10
+ const snapshot = Object.freeze({ env: copyMachineEnvironment(startupEnv), status: frozenStatus({ bootId: randomUUID(), generation: 0, state: "disabled", source: null, sourceLocations: [], capturedAt: null, attemptedAt: null, errorCode: null }) });
11
+ return { acquire: async () => snapshot, status: () => snapshot.status, stop: async () => { } };
12
+ }
13
+ const sameEnvironment = (left, right) => {
14
+ const keys = Object.keys(left);
15
+ return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && left[key] === right[key]);
16
+ };
17
+ export function createMachineEnvironmentProvider(options = {}) {
18
+ const startup = copyMachineEnvironment(options.startupEnv ?? process.env);
19
+ const mode = options.mode ?? "auto";
20
+ const platform = options.platform ?? process.platform;
21
+ const now = options.now ?? Date.now;
22
+ let account = Object.freeze({ ...(options.account ?? resolveMachineAccount()) });
23
+ const selected = selectMachineEnvironmentSource(mode, account, startup, platform);
24
+ const baseline = startup;
25
+ const captureStartup = machineCaptureStartupEnvironment(startup, account, platform);
26
+ let watchedLocations = machineShellSourceLocations({ source: selected.source ?? "windows-registry", account, startupEnv: captureStartup });
27
+ let sourceFingerprint = null;
28
+ let snapshot = Object.freeze({ env: baseline, status: frozenStatus({ bootId: randomUUID(), generation: 0, state: mode === "off" ? "disabled" : selected.source === null ? "unsupported" : "loading", source: selected.source, sourceLocations: watchedLocations, capturedAt: null, attemptedAt: null, errorCode: null }) });
29
+ let lastGood = null;
30
+ let lastSuccessTime = -Infinity;
31
+ let lastFailureTime = -Infinity;
32
+ let stopped = false;
33
+ let unsafeCleanup = false;
34
+ let flight;
35
+ let controller;
36
+ let captureEpoch = 0;
37
+ let capturing = false;
38
+ let forcedFollowup;
39
+ const sourceOwnedKeys = new Set();
40
+ const publish = (env, patch) => {
41
+ snapshot = Object.freeze({ env, status: frozenStatus({ ...snapshot.status, ...patch }) });
42
+ try {
43
+ options.onChange?.(snapshot);
44
+ }
45
+ catch { /* 观察者失败不能污染已获取的机器环境。 */ }
46
+ return snapshot;
47
+ };
48
+ const captureWindows = options.captureWindows ?? (async (request) => {
49
+ const module = await import("./machine-windows-environment-capture.js");
50
+ return module.captureWindowsMachineEnvironment(request);
51
+ });
52
+ const refresh = async (force, signal) => {
53
+ if (!selected.source || stopped || unsafeCleanup)
54
+ return snapshot;
55
+ if (!force && now() - lastFailureTime < (options.backoffMs ?? 30_000))
56
+ return snapshot;
57
+ const attemptedAt = new Date(now()).toISOString();
58
+ try {
59
+ if (!force && lastGood && now() - lastSuccessTime < (options.ttlMs ?? 60_000)) {
60
+ if (selected.source === "windows-registry" || sourceFingerprint === await machineEnvironmentSourceFingerprint(watchedLocations))
61
+ return snapshot;
62
+ }
63
+ if (stopped || signal.aborted)
64
+ return snapshot;
65
+ publish(snapshot.env, { state: "loading", attemptedAt, errorCode: null });
66
+ capturing = true;
67
+ const deadline = Date.now() + (options.captureTimeoutMs ?? (selected.source === "windows-registry" ? 8_000 : 5_000));
68
+ const capture = selected.source === "windows-registry" ? captureWindows : options.captureShell ?? captureShellMachineEnvironment;
69
+ for (let attempt = 0; attempt < 2; attempt++) {
70
+ try {
71
+ const before = selected.source === "windows-registry" ? null : await machineEnvironmentSourceFingerprint(watchedLocations);
72
+ captureEpoch++;
73
+ const captured = await capture({ startupEnv: captureStartup, account, source: selected.source, ...(selected.shellPath ? { shellPath: selected.shellPath } : {}), signal, deadline });
74
+ if (signal.aborted)
75
+ throw new MachineEnvironmentCaptureError("shell_capture_timeout");
76
+ if (before !== null && before !== await machineEnvironmentSourceFingerprint(watchedLocations))
77
+ throw new MachineEnvironmentCaptureError("shell_files_changed");
78
+ let env = validateMachineEnvironment(captured.env);
79
+ let currentOwned;
80
+ if (selected.source === "windows-registry") {
81
+ currentOwned = new Set((captured.sourceOwnedKeys ?? []).map((key) => key.toUpperCase()));
82
+ const merged = new Map();
83
+ for (const [key, value] of [...Object.entries(startup), ...Object.entries(env)]) {
84
+ if (!sourceOwnedKeys.has(key.toUpperCase()) || currentOwned.has(key.toUpperCase()))
85
+ merged.set(key.toUpperCase(), [key, value]);
86
+ }
87
+ env = Object.fromEntries(merged.values());
88
+ }
89
+ env = validateMachineEnvironment(restoreMachineEnvironmentOwnership(env, startup, account, platform));
90
+ const unchanged = lastGood !== null && sameEnvironment(lastGood.env, env);
91
+ const generation = unchanged && lastGood ? lastGood.status.generation : (lastGood?.status.generation ?? 0) + 1;
92
+ const locations = [...new Set(captured.sourceLocations)].slice(0, 8);
93
+ watchedLocations = locations;
94
+ sourceFingerprint = selected.source === "windows-registry" ? null : await machineEnvironmentSourceFingerprint(locations);
95
+ for (const key of currentOwned ?? [])
96
+ sourceOwnedKeys.add(key);
97
+ if (selected.source === "windows-registry" && captured.accountSid)
98
+ account = Object.freeze({ ...account, sid: captured.accountSid });
99
+ lastSuccessTime = now();
100
+ lastFailureTime = -Infinity;
101
+ lastGood = publish(unchanged && lastGood ? lastGood.env : env, { state: "ready", generation, sourceLocations: locations, capturedAt: new Date(lastSuccessTime).toISOString(), errorCode: null });
102
+ return lastGood;
103
+ }
104
+ catch (error) {
105
+ if (error instanceof MachineEnvironmentCaptureError && error.code === "shell_files_changed" && attempt === 0 && Date.now() < deadline && !signal.aborted)
106
+ continue;
107
+ throw error;
108
+ }
109
+ }
110
+ throw new MachineEnvironmentCaptureError("shell_files_changed");
111
+ }
112
+ catch (error) {
113
+ const errorCode = error instanceof MachineEnvironmentCaptureError ? error.code : selected.source === "windows-registry" ? "windows_environment_unavailable" : "shell_capture_failed";
114
+ if (errorCode === "shell_cleanup_failed")
115
+ unsafeCleanup = true;
116
+ lastFailureTime = now();
117
+ return publish(lastGood?.env ?? baseline, { state: lastGood ? "stale" : "fallback", errorCode, attemptedAt });
118
+ }
119
+ finally {
120
+ capturing = false;
121
+ }
122
+ };
123
+ const acquire = (acquireOptions) => {
124
+ if (flight) {
125
+ if (acquireOptions?.force && !capturing) {
126
+ const requestedEpoch = captureEpoch;
127
+ forcedFollowup ??= flight.then((value) => captureEpoch > requestedEpoch ? value : acquire({ force: true })).finally(() => { forcedFollowup = undefined; });
128
+ return forcedFollowup;
129
+ }
130
+ return flight;
131
+ }
132
+ if (stopped || unsafeCleanup || selected.source === null)
133
+ return Promise.resolve(snapshot);
134
+ controller = new AbortController();
135
+ flight = refresh(acquireOptions?.force === true, controller.signal).finally(() => { flight = undefined; controller = undefined; });
136
+ return flight;
137
+ };
138
+ return {
139
+ acquire,
140
+ status: () => snapshot.status,
141
+ async stop() {
142
+ stopped = true;
143
+ controller?.abort();
144
+ await flight;
145
+ },
146
+ };
147
+ }
@@ -0,0 +1,10 @@
1
+ export class MachineEnvironmentCaptureError extends Error {
2
+ code;
3
+ constructor(code) {
4
+ super(code);
5
+ this.code = code;
6
+ this.name = "MachineEnvironmentCaptureError";
7
+ }
8
+ }
9
+ export const MACHINE_ENVIRONMENT_MAX_BYTES = 1024 * 1024;
10
+ export const MACHINE_ENVIRONMENT_MAX_ENTRIES = 4096;
@@ -0,0 +1,27 @@
1
+ import { z } from "zod";
2
+ const Request = z.object({ type: z.enum(["machine:environment:get", "machine:environment:refresh"]), reqId: z.string().min(1).max(200) }).strict();
3
+ /** 发送回调由连接持有,刷新完成后不得把旧请求回复到新 socket。 */
4
+ export async function handleMachineEnvironmentFrame(decoded, provider, send) {
5
+ if (typeof decoded !== "object" || decoded === null || !("type" in decoded)
6
+ || (decoded.type !== "machine:environment:get" && decoded.type !== "machine:environment:refresh"))
7
+ return false;
8
+ const reqId = "reqId" in decoded && typeof decoded.reqId === "string" ? decoded.reqId.slice(0, 200) : "invalid";
9
+ const reply = (result) => { try {
10
+ send({ type: "fs:result", reqId, ...result });
11
+ }
12
+ catch { /* 连接失效由服务端超时处理。 */ } };
13
+ const parsed = Request.safeParse(decoded);
14
+ if (!parsed.success) {
15
+ reply({ ok: false, error: "machine_environment_request_invalid" });
16
+ return true;
17
+ }
18
+ try {
19
+ if (parsed.data.type === "machine:environment:refresh")
20
+ await provider.acquire({ force: true });
21
+ reply({ ok: true, data: provider.status() });
22
+ }
23
+ catch {
24
+ reply({ ok: false, error: "machine_environment_request_failed" });
25
+ }
26
+ return true;
27
+ }
@@ -0,0 +1,75 @@
1
+ import { userInfo } from "node:os";
2
+ import { basename, isAbsolute, join } from "node:path";
3
+ import { MACHINE_ENVIRONMENT_MAX_BYTES, MACHINE_ENVIRONMENT_MAX_ENTRIES, MachineEnvironmentCaptureError } from "./machine-environment-contract.js";
4
+ const ACCOUNT_KEYS = new Set(["HOME", "USER", "LOGNAME", "SHELL", "USERPROFILE", "USERNAME", "USERDOMAIN", "USERDOMAIN_ROAMINGPROFILE", "HOMEDRIVE", "HOMEPATH", "SYSTEMROOT", "WINDIR", "COMSPEC"]);
5
+ const CONFIGURED_KEYS = new Set(["QA_REPORT_HUB", "QA_RUNTIME_ROOT", "QA_REPORT_DELIVERY_CONTEXT"]);
6
+ export const isMachineControlKey = (key) => /^(CREW_|NOWWORK_)/iu.test(key);
7
+ export const isMachineTransientKey = (key) => ["_", "SHLVL", "PWD", "OLDPWD"].includes(key) || key.startsWith("BASH_FUNC_");
8
+ export function copyMachineEnvironment(env) {
9
+ return Object.freeze(Object.fromEntries(Object.entries(env).filter((entry) => typeof entry[1] === "string")));
10
+ }
11
+ export function resolveMachineAccount() {
12
+ const info = userInfo();
13
+ return Object.freeze({ home: info.homedir, username: info.username, shell: info.shell, uid: info.uid });
14
+ }
15
+ /** 账号来自操作系统,不受 daemon 的 profile HOME 或 rc 改写影响。 */
16
+ export function restoreMachineEnvironmentOwnership(captured, startup, account, platform) {
17
+ const protectedKeys = new Set(Object.keys(startup).filter((key) => CONFIGURED_KEYS.has(key.toUpperCase())).map((key) => key.toUpperCase()));
18
+ const owns = (key) => isMachineControlKey(key) || ACCOUNT_KEYS.has(key.toUpperCase()) || protectedKeys.has(key.toUpperCase());
19
+ const entries = Object.entries(captured).filter(([key]) => !owns(key) && !isMachineTransientKey(key));
20
+ entries.push(...Object.entries(startup).filter(([key]) => owns(key)));
21
+ const env = Object.fromEntries(entries);
22
+ if (platform === "win32") {
23
+ for (const key of Object.keys(env))
24
+ if (["HOME", "USERPROFILE", "USERNAME"].includes(key.toUpperCase()))
25
+ delete env[key];
26
+ env.USERPROFILE = account.home;
27
+ env.USERNAME = account.username;
28
+ if (startup.HOME !== undefined)
29
+ env.HOME = account.home;
30
+ }
31
+ else {
32
+ env.HOME = account.home;
33
+ env.USER = account.username;
34
+ env.LOGNAME = account.username;
35
+ const shell = account.shell || startup.SHELL;
36
+ if (shell)
37
+ env.SHELL = shell;
38
+ else
39
+ delete env.SHELL;
40
+ }
41
+ return Object.freeze(env);
42
+ }
43
+ export function machineCaptureStartupEnvironment(startup, account, platform) {
44
+ const owned = restoreMachineEnvironmentOwnership(startup, startup, account, platform);
45
+ return Object.freeze(Object.fromEntries(Object.entries(owned).filter(([key]) => !isMachineControlKey(key) && !isMachineTransientKey(key))));
46
+ }
47
+ export function selectMachineEnvironmentSource(mode, account, startup, platform) {
48
+ if (mode === "off")
49
+ return { source: null };
50
+ if (platform === "win32")
51
+ return { source: mode === "auto" || mode === "windows" ? "windows-registry" : null };
52
+ if (mode === "windows")
53
+ return { source: null };
54
+ const loginShell = account.shell || startup.SHELL;
55
+ const name = mode === "auto" ? basename(loginShell ?? "") : mode;
56
+ if (name !== "bash" && name !== "zsh")
57
+ return { source: null };
58
+ const shellPath = loginShell && isAbsolute(loginShell) && basename(loginShell) === name ? loginShell : join("/bin", name);
59
+ return { source: name, shellPath };
60
+ }
61
+ export function validateMachineEnvironment(value, bytes) {
62
+ if (bytes !== undefined && bytes > MACHINE_ENVIRONMENT_MAX_BYTES)
63
+ throw new MachineEnvironmentCaptureError("shell_capture_too_large");
64
+ if (typeof value !== "object" || value === null || Array.isArray(value))
65
+ throw new MachineEnvironmentCaptureError("shell_capture_failed");
66
+ const entries = Object.entries(value);
67
+ if (entries.length > MACHINE_ENVIRONMENT_MAX_ENTRIES)
68
+ throw new MachineEnvironmentCaptureError("shell_capture_too_large");
69
+ if (entries.some(([key, item]) => !key || key.includes("=") || key.includes("\0") || typeof item !== "string" || item.includes("\0"))) {
70
+ throw new MachineEnvironmentCaptureError("shell_capture_failed");
71
+ }
72
+ if (Buffer.byteLength(JSON.stringify(value), "utf8") > MACHINE_ENVIRONMENT_MAX_BYTES)
73
+ throw new MachineEnvironmentCaptureError("shell_capture_too_large");
74
+ return Object.freeze(Object.fromEntries(entries));
75
+ }
@@ -0,0 +1,110 @@
1
+ import { spawn } from "node:child_process";
2
+ import { setTimeout as delay } from "node:timers/promises";
3
+ import { MACHINE_ENVIRONMENT_MAX_BYTES, MachineEnvironmentCaptureError } from "./machine-environment-contract.js";
4
+ /** 只接收本模块 detached spawn 所创建的进程组,绝不复用 supervisor 的 PID 清理接口。 */
5
+ async function cleanupOwnedGroup(pid) {
6
+ const exists = () => {
7
+ try {
8
+ process.kill(-pid, 0);
9
+ return true;
10
+ }
11
+ catch (error) {
12
+ if (error.code === "ESRCH")
13
+ return false;
14
+ throw new MachineEnvironmentCaptureError("shell_cleanup_failed");
15
+ }
16
+ };
17
+ const signal = (value) => {
18
+ try {
19
+ process.kill(-pid, value);
20
+ }
21
+ catch (error) {
22
+ if (error.code !== "ESRCH")
23
+ throw new MachineEnvironmentCaptureError("shell_cleanup_failed");
24
+ }
25
+ };
26
+ if (!exists())
27
+ return;
28
+ for (const value of ["SIGTERM", "SIGKILL"]) {
29
+ signal(value);
30
+ const until = Date.now() + 1_000;
31
+ while (exists() && Date.now() < until)
32
+ await delay(20);
33
+ if (!exists())
34
+ return;
35
+ }
36
+ throw new MachineEnvironmentCaptureError("shell_cleanup_failed");
37
+ }
38
+ export async function runMachineEnvironmentProcess(options) {
39
+ if (options.signal.aborted || Date.now() >= options.deadline)
40
+ throw new MachineEnvironmentCaptureError("shell_capture_timeout");
41
+ const collectFd = options.collectFd;
42
+ const stdio = Array.from({ length: Math.max(3, (collectFd ?? 2) + 1, (options.metadataFd ?? 2) + 1) }, () => "ignore");
43
+ if (collectFd !== undefined)
44
+ stdio[collectFd] = "pipe";
45
+ if (options.metadataFd !== undefined)
46
+ stdio[options.metadataFd] = "pipe";
47
+ const child = spawn(options.file, [...options.args], { cwd: options.cwd, env: options.env, detached: true, stdio });
48
+ const chunks = [];
49
+ let size = 0;
50
+ const metadata = [];
51
+ let metadataSize = 0;
52
+ let timer;
53
+ let abort;
54
+ let cleanup;
55
+ const cleanupOnce = () => cleanup ??= child.pid === undefined ? Promise.resolve() : cleanupOwnedGroup(child.pid);
56
+ try {
57
+ return await new Promise((resolve, reject) => {
58
+ const fail = (code) => reject(new MachineEnvironmentCaptureError(code));
59
+ abort = () => fail("shell_capture_timeout");
60
+ options.signal.addEventListener("abort", abort, { once: true });
61
+ timer = setTimeout(abort, Math.max(0, options.deadline - Date.now()));
62
+ const channel = collectFd === undefined ? undefined : child.stdio[collectFd];
63
+ channel?.on("data", (chunk) => {
64
+ size += chunk.length;
65
+ if (size > (options.maxBytes ?? MACHINE_ENVIRONMENT_MAX_BYTES))
66
+ fail("shell_capture_too_large");
67
+ else
68
+ chunks.push(chunk);
69
+ });
70
+ channel?.on("error", () => fail("shell_capture_failed"));
71
+ const metadataChannel = options.metadataFd === undefined ? undefined : child.stdio[options.metadataFd];
72
+ metadataChannel?.on("data", (chunk) => {
73
+ metadataSize += chunk.length;
74
+ if (metadataSize > 16_384)
75
+ fail("shell_capture_too_large");
76
+ else
77
+ metadata.push(chunk);
78
+ });
79
+ metadataChannel?.on("error", () => fail("shell_capture_failed"));
80
+ child.once("error", () => fail("shell_unavailable"));
81
+ child.once("exit", () => {
82
+ // rc 的后台任务会继承 fd3;主进程退出即清理,不能等所有继承者自行 close。
83
+ void cleanupOnce().catch(() => fail("shell_cleanup_failed"));
84
+ });
85
+ child.once("close", (code) => {
86
+ if (code === 0) {
87
+ options.onMetadata?.(Buffer.concat(metadata));
88
+ resolve(Buffer.concat(chunks));
89
+ }
90
+ else
91
+ fail(options.exitErrorCode ?? "shell_capture_failed");
92
+ });
93
+ if (options.signal.aborted)
94
+ abort();
95
+ });
96
+ }
97
+ finally {
98
+ if (timer)
99
+ clearTimeout(timer);
100
+ if (abort)
101
+ options.signal.removeEventListener("abort", abort);
102
+ try {
103
+ await cleanupOnce();
104
+ }
105
+ finally {
106
+ for (const stream of child.stdio)
107
+ stream?.destroy();
108
+ }
109
+ }
110
+ }