@alvax-ai/adapter-utils 0.1.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.
Files changed (61) hide show
  1. package/dist/billing.d.ts +2 -0
  2. package/dist/billing.d.ts.map +1 -0
  3. package/dist/billing.js +16 -0
  4. package/dist/billing.js.map +1 -0
  5. package/dist/command-managed-runtime.d.ts +45 -0
  6. package/dist/command-managed-runtime.d.ts.map +1 -0
  7. package/dist/command-managed-runtime.js +164 -0
  8. package/dist/command-managed-runtime.js.map +1 -0
  9. package/dist/command-redaction.d.ts +3 -0
  10. package/dist/command-redaction.d.ts.map +1 -0
  11. package/dist/command-redaction.js +17 -0
  12. package/dist/command-redaction.js.map +1 -0
  13. package/dist/execution-target.d.ts +150 -0
  14. package/dist/execution-target.d.ts.map +1 -0
  15. package/dist/execution-target.js +791 -0
  16. package/dist/execution-target.js.map +1 -0
  17. package/dist/index.d.ts +8 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +5 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/log-redaction.d.ts +9 -0
  22. package/dist/log-redaction.d.ts.map +1 -0
  23. package/dist/log-redaction.js +88 -0
  24. package/dist/log-redaction.js.map +1 -0
  25. package/dist/remote-execution-env.d.ts +2 -0
  26. package/dist/remote-execution-env.d.ts.map +1 -0
  27. package/dist/remote-execution-env.js +46 -0
  28. package/dist/remote-execution-env.js.map +1 -0
  29. package/dist/remote-managed-runtime.d.ts +31 -0
  30. package/dist/remote-managed-runtime.d.ts.map +1 -0
  31. package/dist/remote-managed-runtime.js +81 -0
  32. package/dist/remote-managed-runtime.js.map +1 -0
  33. package/dist/sandbox-callback-bridge.d.ts +132 -0
  34. package/dist/sandbox-callback-bridge.d.ts.map +1 -0
  35. package/dist/sandbox-callback-bridge.js +928 -0
  36. package/dist/sandbox-callback-bridge.js.map +1 -0
  37. package/dist/sandbox-managed-runtime.d.ts +54 -0
  38. package/dist/sandbox-managed-runtime.d.ts.map +1 -0
  39. package/dist/sandbox-managed-runtime.js +234 -0
  40. package/dist/sandbox-managed-runtime.js.map +1 -0
  41. package/dist/sandbox-shell.d.ts +2 -0
  42. package/dist/sandbox-shell.d.ts.map +1 -0
  43. package/dist/sandbox-shell.js +4 -0
  44. package/dist/sandbox-shell.js.map +1 -0
  45. package/dist/server-utils.d.ts +253 -0
  46. package/dist/server-utils.d.ts.map +1 -0
  47. package/dist/server-utils.js +1580 -0
  48. package/dist/server-utils.js.map +1 -0
  49. package/dist/session-compaction.d.ts +25 -0
  50. package/dist/session-compaction.d.ts.map +1 -0
  51. package/dist/session-compaction.js +154 -0
  52. package/dist/session-compaction.js.map +1 -0
  53. package/dist/ssh.d.ts +111 -0
  54. package/dist/ssh.d.ts.map +1 -0
  55. package/dist/ssh.js +1098 -0
  56. package/dist/ssh.js.map +1 -0
  57. package/dist/types.d.ts +465 -0
  58. package/dist/types.d.ts.map +1 -0
  59. package/dist/types.js +5 -0
  60. package/dist/types.js.map +1 -0
  61. package/package.json +45 -0
@@ -0,0 +1,1580 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createHash, randomUUID } from "node:crypto";
3
+ import { constants as fsConstants, promises as fs } from "node:fs";
4
+ import path from "node:path";
5
+ import { sanitizeRemoteExecutionEnv } from "./remote-execution-env.js";
6
+ import { buildSshSpawnTarget } from "./ssh.js";
7
+ import { redactCommandText } from "./command-redaction.js";
8
+ function resolveProcessGroupId(child) {
9
+ if (process.platform === "win32")
10
+ return null;
11
+ return typeof child.pid === "number" && child.pid > 0 ? child.pid : null;
12
+ }
13
+ function signalRunningProcess(running, signal) {
14
+ if (process.platform !== "win32" && running.processGroupId && running.processGroupId > 0) {
15
+ try {
16
+ process.kill(-running.processGroupId, signal);
17
+ return;
18
+ }
19
+ catch {
20
+ // Fall back to the direct child signal if group signaling fails.
21
+ }
22
+ }
23
+ if (!running.child.killed) {
24
+ running.child.kill(signal);
25
+ }
26
+ }
27
+ export const runningProcesses = new Map();
28
+ export const MAX_CAPTURE_BYTES = 4 * 1024 * 1024;
29
+ export const MAX_EXCERPT_BYTES = 32 * 1024;
30
+ const TERMINAL_RESULT_SCAN_OVERLAP_CHARS = 64 * 1024;
31
+ const SENSITIVE_ENV_KEY = /(key|token|secret|password|passwd|authorization|cookie)/i;
32
+ const REDACTED_LOG_VALUE = "***REDACTED***";
33
+ const ALVAX_SKILL_ROOT_RELATIVE_CANDIDATES = [
34
+ "../../skills",
35
+ "../../../../../skills",
36
+ ];
37
+ const MATERIALIZED_SKILL_SENTINEL = ".alvax-materialized-skill.json";
38
+ const MATERIALIZED_SKILL_LOCK_OWNER = "owner.json";
39
+ const MATERIALIZED_SKILL_LOCK_STALE_MS = 30_000;
40
+ export const DEFAULT_ALVAX_AGENT_PROMPT_TEMPLATE = [
41
+ "You are agent {{agent.id}} ({{agent.name}}). Continue your Alvax work.",
42
+ "",
43
+ "Execution contract:",
44
+ "- Start actionable work in this heartbeat; do not stop at a plan unless the issue asks for planning.",
45
+ "- Use the current issue context from the prompt and environment variables.",
46
+ "- If the issue context says the subtree is paused, respond or triage the explicit human wake only; do not resume or cancel the subtree yourself.",
47
+ "- Write durable progress with POST /api/issues/{{context.issueId}}/comments.",
48
+ "- Update issue status with PATCH /api/issues/{{context.issueId}}.",
49
+ "- Read issue documents with GET /api/issues/{{context.issueId}}/documents and GET /api/issues/{{context.issueId}}/documents/:key.",
50
+ "- Create or update issue documents with PUT /api/issues/{{context.issueId}}/documents/:key.",
51
+ "- List issue document revisions with GET /api/issues/{{context.issueId}}/documents/:key/revisions.",
52
+ "- Read issue interactions with GET /api/issues/{{context.issueId}}/interactions.",
53
+ "- Create child issues directly with POST /api/issues/{{context.issueId}}/children when the needed work is clear.",
54
+ "- Use POST /api/issues/{{context.issueId}}/interactions with kind: suggest_tasks when the board/user must choose from proposed child work before issues are created.",
55
+ "- Use POST /api/issues/{{context.issueId}}/interactions with kind: ask_user_questions for structured choices and kind: request_confirmation for concrete approvals.",
56
+ "- For planning issues, use the `plan` issue document as the source of truth. After a board accepts the plan, create child issues from the approved plan instead of implementing on the planning issue.",
57
+ "- Register deliverables with POST /api/issues/{{context.issueId}}/work-products.",
58
+ "- Update deliverables with PATCH /api/work-products/{workProductId}.",
59
+ "- Delete mistaken deliverables with DELETE /api/work-products/{workProductId}.",
60
+ "- When asked to make or revise a plan, use document key `plan`. For durable reports, notes, or diagnoses, use a clear lowercase key such as `report`, `notes`, or `diagnosis`. Before updating an existing document, fetch it and send its `latestRevisionId` as `baseRevisionId`. After changing a document, leave a normal issue comment that summarizes progress and names the updated document.",
61
+ "- Use child issues for parallel or delegated work instead of polling agents, sessions, or processes.",
62
+ "- For plan approval, update the `plan` issue document first, then create `request_confirmation` targeting the latest plan revision with target type `issue_document`, key `plan`, revisionId set to `latestRevisionId`, and idempotencyKey `confirmation:{issueId}:plan:{revisionId}`.",
63
+ "- Use `ask_user_questions` for bounded choices.",
64
+ "- Use continuationPolicy `wake_assignee` when you need to resume after a response.",
65
+ "- Use continuationPolicy `wake_assignee_on_accept` only when rejection should end the path.",
66
+ "- Register user-visible deliverables as issue work products.",
67
+ "- Mark the issue done when complete, blocked when waiting on a named unblock owner/action, or in_review when human review or an issue interaction is genuinely needed.",
68
+ "- Use Authorization: Bearer $ALVAX_API_KEY and X-Alvax-Run-Id: $ALVAX_RUN_ID on Alvax API requests.",
69
+ "- Company Knowledge Documents listed in context are read-only company files. Use their localPath when relevant. Do not call Knowledge upload/delete APIs or binary storage mutation APIs.",
70
+ "- Company Knowledge Data Tables contain structured reusable company knowledge. Use agent-safe Data Table APIs to read rows and to create/update non-destructive table data. Do not store secrets in Data Tables. For destructive Data Table operations, create a linked knowledge_data_table_change_request approval and wait for board approval.",
71
+ "- If a missing secret blocks the issue, use the $alvax credential_request workflow: create a linked credential_request approval, comment on the source issue, set it to in_review, and wait for board approval. Never ask for secret values in comments.",
72
+ "- Do not create budget, tool permission, or policy approvals.",
73
+ "- Do not call approval approve/reject/request-revision routes, secret CRUD APIs, budgets, liveness, routines, goals, or issue-tree-control APIs.",
74
+ "- Respect company boundaries and the checked-out issue.",
75
+ ].join("\n");
76
+ function normalizePathSlashes(value) {
77
+ return value.replaceAll("\\", "/");
78
+ }
79
+ function isMaintainerOnlySkillTarget(candidate) {
80
+ return normalizePathSlashes(candidate).includes("/.agents/skills/");
81
+ }
82
+ function skillLocationLabel(value) {
83
+ if (typeof value !== "string")
84
+ return null;
85
+ const trimmed = value.trim();
86
+ return trimmed.length > 0 ? trimmed : null;
87
+ }
88
+ function buildManagedSkillOrigin(entry) {
89
+ if (entry.required) {
90
+ return {
91
+ origin: "alvax_required",
92
+ originLabel: "Required by Alvax",
93
+ readOnly: false,
94
+ };
95
+ }
96
+ return {
97
+ origin: "company_managed",
98
+ originLabel: "Managed by Alvax",
99
+ readOnly: false,
100
+ };
101
+ }
102
+ function resolveInstalledEntryTarget(skillsHome, entryName, dirent, linkedPath) {
103
+ const fullPath = path.join(skillsHome, entryName);
104
+ if (dirent.isSymbolicLink()) {
105
+ return {
106
+ targetPath: linkedPath ? path.resolve(path.dirname(fullPath), linkedPath) : null,
107
+ kind: "symlink",
108
+ };
109
+ }
110
+ if (dirent.isDirectory()) {
111
+ return { targetPath: fullPath, kind: "directory" };
112
+ }
113
+ return { targetPath: fullPath, kind: "file" };
114
+ }
115
+ export function parseObject(value) {
116
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
117
+ return {};
118
+ }
119
+ return value;
120
+ }
121
+ export function asString(value, fallback) {
122
+ return typeof value === "string" && value.length > 0 ? value : fallback;
123
+ }
124
+ export function asNumber(value, fallback) {
125
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
126
+ }
127
+ export function asBoolean(value, fallback) {
128
+ return typeof value === "boolean" ? value : fallback;
129
+ }
130
+ export function asStringArray(value) {
131
+ return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
132
+ }
133
+ export function parseJson(value) {
134
+ try {
135
+ return JSON.parse(value);
136
+ }
137
+ catch {
138
+ return null;
139
+ }
140
+ }
141
+ export function appendWithCap(prev, chunk, cap = MAX_CAPTURE_BYTES) {
142
+ const combined = prev + chunk;
143
+ return combined.length > cap ? combined.slice(combined.length - cap) : combined;
144
+ }
145
+ export function appendWithByteCap(prev, chunk, cap = MAX_CAPTURE_BYTES) {
146
+ const combined = prev + chunk;
147
+ const bytes = Buffer.byteLength(combined, "utf8");
148
+ if (bytes <= cap)
149
+ return combined;
150
+ const buffer = Buffer.from(combined, "utf8");
151
+ let start = Math.max(0, bytes - cap);
152
+ while (start < buffer.length && (buffer[start] & 0xc0) === 0x80)
153
+ start += 1;
154
+ return buffer.subarray(start).toString("utf8");
155
+ }
156
+ function resumeReadable(readable) {
157
+ if (!readable || readable.destroyed)
158
+ return;
159
+ readable.resume();
160
+ }
161
+ export function resolvePathValue(obj, dottedPath) {
162
+ const parts = dottedPath.split(".");
163
+ let cursor = obj;
164
+ for (const part of parts) {
165
+ if (typeof cursor !== "object" || cursor === null || Array.isArray(cursor)) {
166
+ return "";
167
+ }
168
+ cursor = cursor[part];
169
+ }
170
+ if (cursor === null || cursor === undefined)
171
+ return "";
172
+ if (typeof cursor === "string")
173
+ return cursor;
174
+ if (typeof cursor === "number" || typeof cursor === "boolean")
175
+ return String(cursor);
176
+ try {
177
+ return JSON.stringify(cursor);
178
+ }
179
+ catch {
180
+ return "";
181
+ }
182
+ }
183
+ export function renderTemplate(template, data) {
184
+ return template.replace(/{{\s*([a-zA-Z0-9_.-]+)\s*}}/g, (_, path) => resolvePathValue(data, path));
185
+ }
186
+ export function joinPromptSections(sections, separator = "\n\n") {
187
+ return sections
188
+ .map((value) => (typeof value === "string" ? value.trim() : ""))
189
+ .filter(Boolean)
190
+ .join(separator);
191
+ }
192
+ function normalizeAlvaxWakeIssue(value) {
193
+ const issue = parseObject(value);
194
+ const id = asString(issue.id, "").trim() || null;
195
+ const identifier = asString(issue.identifier, "").trim() || null;
196
+ const title = asString(issue.title, "").trim() || null;
197
+ const status = asString(issue.status, "").trim() || null;
198
+ const workMode = asString(issue.workMode, "").trim() || null;
199
+ const priority = asString(issue.priority, "").trim() || null;
200
+ if (!id && !identifier && !title)
201
+ return null;
202
+ return {
203
+ id,
204
+ identifier,
205
+ title,
206
+ status,
207
+ workMode,
208
+ priority,
209
+ };
210
+ }
211
+ function normalizeAlvaxWakeComment(value) {
212
+ const comment = parseObject(value);
213
+ const author = parseObject(comment.author);
214
+ const body = asString(comment.body, "");
215
+ if (!body.trim())
216
+ return null;
217
+ return {
218
+ id: asString(comment.id, "").trim() || null,
219
+ issueId: asString(comment.issueId, "").trim() || null,
220
+ body,
221
+ bodyTruncated: asBoolean(comment.bodyTruncated, false),
222
+ createdAt: asString(comment.createdAt, "").trim() || null,
223
+ authorType: asString(author.type, "").trim() || null,
224
+ authorId: asString(author.id, "").trim() || null,
225
+ };
226
+ }
227
+ function normalizeAlvaxWakeContinuationSummary(value) {
228
+ const summary = parseObject(value);
229
+ const body = asString(summary.body, "").trim();
230
+ if (!body)
231
+ return null;
232
+ return {
233
+ key: asString(summary.key, "").trim() || null,
234
+ title: asString(summary.title, "").trim() || null,
235
+ body,
236
+ bodyTruncated: asBoolean(summary.bodyTruncated, false),
237
+ updatedAt: asString(summary.updatedAt, "").trim() || null,
238
+ };
239
+ }
240
+ function normalizeAlvaxWakeLivenessContinuation(value) {
241
+ const continuation = parseObject(value);
242
+ const attempt = asNumber(continuation.attempt, 0);
243
+ const maxAttempts = asNumber(continuation.maxAttempts, 0);
244
+ const sourceRunId = asString(continuation.sourceRunId, "").trim() || null;
245
+ const state = asString(continuation.state, "").trim() || null;
246
+ const reason = asString(continuation.reason, "").trim() || null;
247
+ const instruction = asString(continuation.instruction, "").trim() || null;
248
+ if (!attempt && !maxAttempts && !sourceRunId && !state && !reason && !instruction)
249
+ return null;
250
+ return {
251
+ attempt: attempt > 0 ? attempt : null,
252
+ maxAttempts: maxAttempts > 0 ? maxAttempts : null,
253
+ sourceRunId,
254
+ state,
255
+ reason,
256
+ instruction,
257
+ };
258
+ }
259
+ function normalizeAlvaxWakeChildIssueSummary(value) {
260
+ const child = parseObject(value);
261
+ const id = asString(child.id, "").trim() || null;
262
+ const identifier = asString(child.identifier, "").trim() || null;
263
+ const title = asString(child.title, "").trim() || null;
264
+ const status = asString(child.status, "").trim() || null;
265
+ const priority = asString(child.priority, "").trim() || null;
266
+ const summary = asString(child.summary, "").trim() || null;
267
+ if (!id && !identifier && !title && !status && !summary)
268
+ return null;
269
+ return { id, identifier, title, status, priority, summary };
270
+ }
271
+ function normalizeAlvaxWakeBlockerSummary(value) {
272
+ const blocker = parseObject(value);
273
+ const id = asString(blocker.id, "").trim() || null;
274
+ const identifier = asString(blocker.identifier, "").trim() || null;
275
+ const title = asString(blocker.title, "").trim() || null;
276
+ const status = asString(blocker.status, "").trim() || null;
277
+ const priority = asString(blocker.priority, "").trim() || null;
278
+ if (!id && !identifier && !title && !status)
279
+ return null;
280
+ return { id, identifier, title, status, priority };
281
+ }
282
+ function normalizeAlvaxWakeTreeHoldSummary(value) {
283
+ const hold = parseObject(value);
284
+ const holdId = asString(hold.holdId, "").trim() || null;
285
+ const rootIssueId = asString(hold.rootIssueId, "").trim() || null;
286
+ const mode = asString(hold.mode, "").trim() || null;
287
+ const reason = asString(hold.reason, "").trim() || null;
288
+ if (!holdId && !rootIssueId && !mode && !reason)
289
+ return null;
290
+ return { holdId, rootIssueId, mode, reason };
291
+ }
292
+ function normalizeAlvaxWakeExecutionPrincipal(value) {
293
+ const principal = parseObject(value);
294
+ const typeRaw = asString(principal.type, "").trim().toLowerCase();
295
+ if (typeRaw !== "agent" && typeRaw !== "user")
296
+ return null;
297
+ return {
298
+ type: typeRaw,
299
+ agentId: asString(principal.agentId, "").trim() || null,
300
+ userId: asString(principal.userId, "").trim() || null,
301
+ };
302
+ }
303
+ function normalizeAlvaxWakeExecutionStage(value) {
304
+ const stage = parseObject(value);
305
+ const wakeRoleRaw = asString(stage.wakeRole, "").trim().toLowerCase();
306
+ const wakeRole = wakeRoleRaw === "reviewer" || wakeRoleRaw === "approver" || wakeRoleRaw === "executor"
307
+ ? wakeRoleRaw
308
+ : null;
309
+ const allowedActions = Array.isArray(stage.allowedActions)
310
+ ? stage.allowedActions
311
+ .filter((entry) => typeof entry === "string" && entry.trim().length > 0)
312
+ .map((entry) => entry.trim())
313
+ : [];
314
+ const currentParticipant = normalizeAlvaxWakeExecutionPrincipal(stage.currentParticipant);
315
+ const returnAssignee = normalizeAlvaxWakeExecutionPrincipal(stage.returnAssignee);
316
+ const reviewRequestRaw = parseObject(stage.reviewRequest);
317
+ const reviewInstructions = asString(reviewRequestRaw.instructions, "").trim();
318
+ const reviewRequest = reviewInstructions ? { instructions: reviewInstructions } : null;
319
+ const stageId = asString(stage.stageId, "").trim() || null;
320
+ const stageType = asString(stage.stageType, "").trim() || null;
321
+ const lastDecisionOutcome = asString(stage.lastDecisionOutcome, "").trim() || null;
322
+ if (!wakeRole && !stageId && !stageType && !currentParticipant && !returnAssignee && !reviewRequest && !lastDecisionOutcome && allowedActions.length === 0) {
323
+ return null;
324
+ }
325
+ return {
326
+ wakeRole,
327
+ stageId,
328
+ stageType,
329
+ currentParticipant,
330
+ returnAssignee,
331
+ reviewRequest,
332
+ lastDecisionOutcome,
333
+ allowedActions,
334
+ };
335
+ }
336
+ export function normalizeAlvaxWakePayload(value) {
337
+ const payload = parseObject(value);
338
+ const comments = Array.isArray(payload.comments)
339
+ ? payload.comments
340
+ .map((entry) => normalizeAlvaxWakeComment(entry))
341
+ .filter((entry) => Boolean(entry))
342
+ : [];
343
+ const commentWindow = parseObject(payload.commentWindow);
344
+ const commentIds = Array.isArray(payload.commentIds)
345
+ ? payload.commentIds
346
+ .filter((entry) => typeof entry === "string" && entry.trim().length > 0)
347
+ .map((entry) => entry.trim())
348
+ : [];
349
+ const executionStage = normalizeAlvaxWakeExecutionStage(payload.executionStage);
350
+ const continuationSummary = normalizeAlvaxWakeContinuationSummary(payload.continuationSummary);
351
+ const livenessContinuation = normalizeAlvaxWakeLivenessContinuation(payload.livenessContinuation);
352
+ const childIssueSummaries = Array.isArray(payload.childIssueSummaries)
353
+ ? payload.childIssueSummaries
354
+ .map((entry) => normalizeAlvaxWakeChildIssueSummary(entry))
355
+ .filter((entry) => Boolean(entry))
356
+ : [];
357
+ const unresolvedBlockerIssueIds = Array.isArray(payload.unresolvedBlockerIssueIds)
358
+ ? payload.unresolvedBlockerIssueIds
359
+ .map((entry) => asString(entry, "").trim())
360
+ .filter(Boolean)
361
+ : [];
362
+ const unresolvedBlockerSummaries = Array.isArray(payload.unresolvedBlockerSummaries)
363
+ ? payload.unresolvedBlockerSummaries
364
+ .map((entry) => normalizeAlvaxWakeBlockerSummary(entry))
365
+ .filter((entry) => Boolean(entry))
366
+ : [];
367
+ const activeTreeHold = normalizeAlvaxWakeTreeHoldSummary(payload.activeTreeHold);
368
+ if (comments.length === 0 && commentIds.length === 0 && childIssueSummaries.length === 0 && unresolvedBlockerIssueIds.length === 0 && unresolvedBlockerSummaries.length === 0 && !activeTreeHold && !executionStage && !continuationSummary && !livenessContinuation && !normalizeAlvaxWakeIssue(payload.issue)) {
369
+ return null;
370
+ }
371
+ return {
372
+ reason: asString(payload.reason, "").trim() || null,
373
+ issue: normalizeAlvaxWakeIssue(payload.issue),
374
+ checkedOutByHarness: asBoolean(payload.checkedOutByHarness, false),
375
+ dependencyBlockedInteraction: asBoolean(payload.dependencyBlockedInteraction, false),
376
+ treeHoldInteraction: asBoolean(payload.treeHoldInteraction, false),
377
+ activeTreeHold,
378
+ unresolvedBlockerIssueIds,
379
+ unresolvedBlockerSummaries,
380
+ executionStage,
381
+ continuationSummary,
382
+ livenessContinuation,
383
+ interactionKind: asString(payload.interactionKind, "").trim() || null,
384
+ interactionStatus: asString(payload.interactionStatus, "").trim() || null,
385
+ childIssueSummaries,
386
+ childIssueSummaryTruncated: asBoolean(payload.childIssueSummaryTruncated, false),
387
+ commentIds,
388
+ latestCommentId: asString(payload.latestCommentId, "").trim() || null,
389
+ comments,
390
+ requestedCount: asNumber(commentWindow.requestedCount, comments.length || commentIds.length),
391
+ includedCount: asNumber(commentWindow.includedCount, comments.length),
392
+ missingCount: asNumber(commentWindow.missingCount, 0),
393
+ truncated: asBoolean(payload.truncated, false),
394
+ fallbackFetchNeeded: asBoolean(payload.fallbackFetchNeeded, false),
395
+ };
396
+ }
397
+ export function stringifyAlvaxWakePayload(value) {
398
+ const normalized = normalizeAlvaxWakePayload(value);
399
+ if (!normalized)
400
+ return null;
401
+ return JSON.stringify(normalized);
402
+ }
403
+ export function readAlvaxIssueWorkModeFromContext(value) {
404
+ const context = parseObject(value);
405
+ const issue = parseObject(context.alvaxIssue);
406
+ const direct = asString(issue.workMode, "").trim();
407
+ if (direct)
408
+ return direct;
409
+ const wake = normalizeAlvaxWakePayload(context.alvaxWake);
410
+ return wake?.issue?.workMode ?? null;
411
+ }
412
+ export function renderAlvaxWakePrompt(value, options = {}) {
413
+ const normalized = normalizeAlvaxWakePayload(value);
414
+ if (!normalized)
415
+ return "";
416
+ const resumedSession = options.resumedSession === true;
417
+ const executionStage = normalized.executionStage;
418
+ const principalLabel = (principal) => {
419
+ if (!principal || !principal.type)
420
+ return "unknown";
421
+ if (principal.type === "agent")
422
+ return principal.agentId ? `agent ${principal.agentId}` : "agent";
423
+ return principal.userId ? `user ${principal.userId}` : "user";
424
+ };
425
+ const lines = resumedSession
426
+ ? [
427
+ "## Alvax Resume Delta",
428
+ "",
429
+ "You are resuming an existing Alvax session.",
430
+ "This heartbeat is scoped to the issue below. Do not switch to another issue until you have handled this wake.",
431
+ "Focus on the new wake delta below and continue the current task without restating the full heartbeat boilerplate.",
432
+ "Fetch the API thread only when `fallbackFetchNeeded` is true or you need broader history than this batch.",
433
+ "",
434
+ "Execution contract: take concrete action in this heartbeat when the issue is actionable; do not stop at a plan unless planning was requested. Leave durable progress with comments, issue documents, or issue work products, then set a clear final status before ending the heartbeat: `done`, `blocked` with a named unblock owner/action, `in_review` when human review or an issue interaction is genuinely needed, or `in_progress` only when the current run has a real continuation path. Comments, documents, work products, and `Remaining` bullets are evidence, not valid continuation paths by themselves.",
435
+ "",
436
+ `- reason: ${normalized.reason ?? "unknown"}`,
437
+ `- issue: ${normalized.issue?.identifier ?? normalized.issue?.id ?? "unknown"}${normalized.issue?.title ? ` ${normalized.issue.title}` : ""}`,
438
+ `- pending comments: ${normalized.includedCount}/${normalized.requestedCount}`,
439
+ `- latest comment id: ${normalized.latestCommentId ?? "unknown"}`,
440
+ `- fallback fetch needed: ${normalized.fallbackFetchNeeded ? "yes" : "no"}`,
441
+ ]
442
+ : [
443
+ "## Alvax Wake Payload",
444
+ "",
445
+ "Treat this wake payload as the highest-priority change for the current heartbeat.",
446
+ "This heartbeat is scoped to the issue below. Do not switch to another issue until you have handled this wake.",
447
+ "Before generic repo exploration or boilerplate heartbeat updates, acknowledge the latest comment and explain how it changes your next action.",
448
+ "Use this inline wake data first before refetching the issue thread.",
449
+ "Only fetch the API thread when `fallbackFetchNeeded` is true or you need broader history than this batch.",
450
+ "",
451
+ "Execution contract: take concrete action in this heartbeat when the issue is actionable; do not stop at a plan unless planning was requested. Leave durable progress with comments, issue documents, or issue work products, then set a clear final status before ending the heartbeat: `done`, `blocked` with a named unblock owner/action, `in_review` when human review or an issue interaction is genuinely needed, or `in_progress` only when the current run has a real continuation path. Comments, documents, work products, and `Remaining` bullets are evidence, not valid continuation paths by themselves.",
452
+ "",
453
+ `- reason: ${normalized.reason ?? "unknown"}`,
454
+ `- issue: ${normalized.issue?.identifier ?? normalized.issue?.id ?? "unknown"}${normalized.issue?.title ? ` ${normalized.issue.title}` : ""}`,
455
+ `- pending comments: ${normalized.includedCount}/${normalized.requestedCount}`,
456
+ `- latest comment id: ${normalized.latestCommentId ?? "unknown"}`,
457
+ `- fallback fetch needed: ${normalized.fallbackFetchNeeded ? "yes" : "no"}`,
458
+ ];
459
+ if (normalized.issue?.status) {
460
+ lines.push(`- issue status: ${normalized.issue.status}`);
461
+ }
462
+ if (normalized.issue?.workMode) {
463
+ lines.push(`- issue work mode: ${normalized.issue.workMode}`);
464
+ }
465
+ if (normalized.issue?.priority) {
466
+ lines.push(`- issue priority: ${normalized.issue.priority}`);
467
+ }
468
+ if (normalized.issue?.workMode === "planning") {
469
+ const hasWakeComments = normalized.comments.length > 0;
470
+ const acceptedPlanContinuation = !hasWakeComments &&
471
+ normalized.interactionKind === "request_confirmation" && normalized.interactionStatus === "accepted";
472
+ let directive = "Make the plan only. Do not write code or perform implementation work.";
473
+ if (hasWakeComments) {
474
+ directive = "Update the plan only. Do not write code or perform implementation work.";
475
+ }
476
+ if (acceptedPlanContinuation) {
477
+ directive = "Create child issues from the approved plan only. Do not write code or perform implementation work on the planning issue.";
478
+ }
479
+ lines.push(`- planning directive: ${directive}`);
480
+ if (acceptedPlanContinuation) {
481
+ lines.push("- accepted-plan continuation: you may create child implementation issues from the approved plan, but must not start implementation work on the planning issue itself");
482
+ }
483
+ }
484
+ if (normalized.checkedOutByHarness) {
485
+ lines.push("- checkout: already claimed by the harness for this run");
486
+ }
487
+ if (normalized.dependencyBlockedInteraction) {
488
+ lines.push("- dependency-blocked interaction: yes");
489
+ lines.push("- execution scope: respond or triage the human comment; do not treat blocker-dependent deliverable work as unblocked");
490
+ if (normalized.unresolvedBlockerSummaries.length > 0) {
491
+ const blockers = normalized.unresolvedBlockerSummaries
492
+ .map((blocker) => `${blocker.identifier ?? blocker.id ?? "unknown"}${blocker.title ? ` ${blocker.title}` : ""}${blocker.status ? ` (${blocker.status})` : ""}`)
493
+ .join("; ");
494
+ lines.push(`- unresolved blockers: ${blockers}`);
495
+ }
496
+ else if (normalized.unresolvedBlockerIssueIds.length > 0) {
497
+ lines.push(`- unresolved blocker issue ids: ${normalized.unresolvedBlockerIssueIds.join(", ")}`);
498
+ }
499
+ }
500
+ if (normalized.treeHoldInteraction) {
501
+ lines.push("- tree-hold interaction: yes");
502
+ lines.push("- execution scope: respond or triage the human comment; the subtree remains paused until an explicit resume action");
503
+ if (normalized.activeTreeHold) {
504
+ const hold = normalized.activeTreeHold;
505
+ lines.push(`- active tree hold: ${hold.holdId ?? "unknown"}${hold.rootIssueId ? ` rooted at ${hold.rootIssueId}` : ""}${hold.mode ? ` (${hold.mode})` : ""}`);
506
+ }
507
+ }
508
+ if (normalized.missingCount > 0) {
509
+ lines.push(`- omitted comments: ${normalized.missingCount}`);
510
+ }
511
+ if (executionStage) {
512
+ lines.push(`- execution wake role: ${executionStage.wakeRole ?? "unknown"}`, `- execution stage: ${executionStage.stageType ?? "unknown"}`, `- execution participant: ${principalLabel(executionStage.currentParticipant)}`, `- execution return assignee: ${principalLabel(executionStage.returnAssignee)}`, `- last decision outcome: ${executionStage.lastDecisionOutcome ?? "none"}`);
513
+ if (executionStage.allowedActions.length > 0) {
514
+ lines.push(`- allowed actions: ${executionStage.allowedActions.join(", ")}`);
515
+ }
516
+ if (executionStage.reviewRequest) {
517
+ lines.push("", "Review request instructions:", executionStage.reviewRequest.instructions);
518
+ }
519
+ lines.push("");
520
+ if (executionStage.wakeRole === "reviewer" || executionStage.wakeRole === "approver") {
521
+ lines.push(`You are waking as the active ${executionStage.wakeRole} for this issue.`, "Do not execute the task itself or continue executor work.", "Review the issue and choose one of the allowed actions above.", "If you request changes, the workflow routes back to the stored return assignee.", "");
522
+ }
523
+ else if (executionStage.wakeRole === "executor") {
524
+ lines.push("You are waking because changes were requested in the execution workflow.", "Address the requested changes on this issue and resubmit when the work is ready.", "");
525
+ }
526
+ }
527
+ if (normalized.continuationSummary) {
528
+ lines.push("", "Issue continuation summary:", normalized.continuationSummary.body);
529
+ if (normalized.continuationSummary.bodyTruncated) {
530
+ lines.push("[continuation summary truncated]");
531
+ }
532
+ }
533
+ if (normalized.livenessContinuation) {
534
+ const continuation = normalized.livenessContinuation;
535
+ lines.push("", "Run liveness continuation:");
536
+ if (continuation.attempt) {
537
+ lines.push(`- attempt: ${continuation.attempt}${continuation.maxAttempts ? `/${continuation.maxAttempts}` : ""}`);
538
+ }
539
+ if (continuation.sourceRunId) {
540
+ lines.push(`- source run: ${continuation.sourceRunId}`);
541
+ }
542
+ if (continuation.state) {
543
+ lines.push(`- liveness state: ${continuation.state}`);
544
+ }
545
+ if (continuation.reason) {
546
+ lines.push(`- reason: ${continuation.reason}`);
547
+ }
548
+ if (continuation.instruction) {
549
+ lines.push(`- instruction: ${continuation.instruction}`);
550
+ }
551
+ }
552
+ if (normalized.childIssueSummaries.length > 0) {
553
+ lines.push("", "Direct child issue summaries:");
554
+ for (const child of normalized.childIssueSummaries) {
555
+ const label = child.identifier ?? child.id ?? "unknown";
556
+ lines.push(`- ${label}${child.title ? ` ${child.title}` : ""}${child.status ? ` (${child.status})` : ""}`);
557
+ if (child.summary) {
558
+ lines.push(` ${child.summary}`);
559
+ }
560
+ }
561
+ if (normalized.childIssueSummaryTruncated) {
562
+ lines.push("[child issue summaries truncated]");
563
+ }
564
+ }
565
+ if (normalized.checkedOutByHarness) {
566
+ lines.push("", "The harness already checked out this issue for the current run.", "Do not call `/api/issues/{id}/checkout` again unless you intentionally switch to a different task.", "");
567
+ }
568
+ if (normalized.comments.length > 0) {
569
+ lines.push("New comments in order:");
570
+ }
571
+ for (const [index, comment] of normalized.comments.entries()) {
572
+ const authorLabel = comment.authorId
573
+ ? `${comment.authorType ?? "unknown"} ${comment.authorId}`
574
+ : comment.authorType ?? "unknown";
575
+ lines.push(`${index + 1}. comment ${comment.id ?? "unknown"} at ${comment.createdAt ?? "unknown"} by ${authorLabel}`, comment.body);
576
+ if (comment.bodyTruncated) {
577
+ lines.push("[comment body truncated]");
578
+ }
579
+ lines.push("");
580
+ }
581
+ return lines.join("\n").trim();
582
+ }
583
+ export function redactEnvForLogs(env) {
584
+ const redacted = {};
585
+ for (const [key, value] of Object.entries(env)) {
586
+ redacted[key] = SENSITIVE_ENV_KEY.test(key) ? REDACTED_LOG_VALUE : value;
587
+ }
588
+ return redacted;
589
+ }
590
+ export function redactCommandTextForLogs(command) {
591
+ return redactCommandText(command, REDACTED_LOG_VALUE);
592
+ }
593
+ export function buildInvocationEnvForLogs(env, options = {}) {
594
+ const merged = { ...env };
595
+ const runtimeEnv = options.runtimeEnv ?? {};
596
+ for (const key of options.includeRuntimeKeys ?? []) {
597
+ if (key in merged)
598
+ continue;
599
+ const value = runtimeEnv[key];
600
+ if (typeof value !== "string" || value.length === 0)
601
+ continue;
602
+ merged[key] = value;
603
+ }
604
+ const resolvedCommand = options.resolvedCommand?.trim();
605
+ if (resolvedCommand) {
606
+ merged[options.resolvedCommandEnvKey ?? "ALVAX_RESOLVED_COMMAND"] = redactCommandTextForLogs(resolvedCommand);
607
+ }
608
+ return redactEnvForLogs(merged);
609
+ }
610
+ export function buildAlvaxEnv(agent) {
611
+ const resolveHostForUrl = (rawHost) => {
612
+ const host = rawHost.trim();
613
+ if (!host || host === "0.0.0.0" || host === "::")
614
+ return "localhost";
615
+ if (host.includes(":") && !host.startsWith("[") && !host.endsWith("]"))
616
+ return `[${host}]`;
617
+ return host;
618
+ };
619
+ const vars = {
620
+ ALVAX_AGENT_ID: agent.id,
621
+ ALVAX_COMPANY_ID: agent.companyId,
622
+ };
623
+ const runtimeHost = resolveHostForUrl(process.env.ALVAX_LISTEN_HOST ?? process.env.HOST ?? "localhost");
624
+ const runtimePort = process.env.ALVAX_LISTEN_PORT ?? process.env.PORT ?? "3100";
625
+ const apiUrl = process.env.ALVAX_RUNTIME_API_URL ??
626
+ process.env.ALVAX_API_URL ??
627
+ `http://${runtimeHost}:${runtimePort}`;
628
+ vars.ALVAX_API_URL = apiUrl;
629
+ return vars;
630
+ }
631
+ export function applyAlvaxWorkspaceEnv(env, input) {
632
+ const mappings = [
633
+ ["ALVAX_WORKSPACE_CWD", input.workspaceCwd],
634
+ ["ALVAX_WORKSPACE_SOURCE", input.workspaceSource],
635
+ ["ALVAX_WORKSPACE_STRATEGY", input.workspaceStrategy],
636
+ ["ALVAX_WORKSPACE_ID", input.workspaceId],
637
+ ["ALVAX_WORKSPACE_REPO_URL", input.workspaceRepoUrl],
638
+ ["ALVAX_WORKSPACE_REPO_REF", input.workspaceRepoRef],
639
+ ["ALVAX_WORKSPACE_BRANCH", input.workspaceBranch],
640
+ ["ALVAX_WORKSPACE_WORKTREE_PATH", input.workspaceWorktreePath],
641
+ ["AGENT_HOME", input.agentHome],
642
+ ];
643
+ for (const [key, value] of mappings) {
644
+ if (typeof value === "string" && value.length > 0) {
645
+ env[key] = value;
646
+ }
647
+ }
648
+ return env;
649
+ }
650
+ export function shapeAlvaxWorkspaceEnvForExecution(input) {
651
+ const workspaceCwd = typeof input.workspaceCwd === "string" && input.workspaceCwd.trim().length > 0
652
+ ? input.workspaceCwd.trim()
653
+ : null;
654
+ const workspaceWorktreePath = typeof input.workspaceWorktreePath === "string" && input.workspaceWorktreePath.trim().length > 0
655
+ ? input.workspaceWorktreePath.trim()
656
+ : null;
657
+ const workspaceHints = Array.isArray(input.workspaceHints) ? input.workspaceHints : [];
658
+ if (!input.executionTargetIsRemote) {
659
+ return {
660
+ workspaceCwd,
661
+ workspaceWorktreePath,
662
+ workspaceHints,
663
+ };
664
+ }
665
+ const executionCwd = typeof input.executionCwd === "string" && input.executionCwd.trim().length > 0
666
+ ? input.executionCwd.trim()
667
+ : null;
668
+ // On a remote target we must never fall back to the local workspaceCwd —
669
+ // doing so leaks host paths into the remote env (the exact failure mode
670
+ // this helper exists to prevent). Callers are expected to resolve
671
+ // executionCwd via adapterExecutionTargetRemoteCwd before calling this
672
+ // helper, which always returns a non-empty string. Surface a warning so
673
+ // future callers don't silently regress to the leak.
674
+ if (executionCwd === null) {
675
+ // eslint-disable-next-line no-console
676
+ console.warn("[alvax] shapeAlvaxWorkspaceEnvForExecution called with executionCwd=null on a remote target; " +
677
+ "stripping workspaceCwd to avoid leaking local paths into the remote environment.");
678
+ }
679
+ const realizedWorkspaceCwd = executionCwd;
680
+ const localWorkspaceCwd = workspaceCwd ? path.resolve(workspaceCwd) : null;
681
+ const shapedWorkspaceHints = workspaceHints.map((hint) => {
682
+ const nextHint = { ...hint };
683
+ const hintCwd = typeof nextHint.cwd === "string" ? nextHint.cwd.trim() : "";
684
+ if (!hintCwd)
685
+ return nextHint;
686
+ if (localWorkspaceCwd && path.resolve(hintCwd) === localWorkspaceCwd) {
687
+ if (realizedWorkspaceCwd) {
688
+ nextHint.cwd = realizedWorkspaceCwd;
689
+ }
690
+ else {
691
+ delete nextHint.cwd;
692
+ }
693
+ return nextHint;
694
+ }
695
+ delete nextHint.cwd;
696
+ return nextHint;
697
+ });
698
+ return {
699
+ workspaceCwd: realizedWorkspaceCwd,
700
+ workspaceWorktreePath: null,
701
+ workspaceHints: shapedWorkspaceHints,
702
+ };
703
+ }
704
+ export function sanitizeInheritedAlvaxEnv(baseEnv) {
705
+ const env = { ...baseEnv };
706
+ for (const key of Object.keys(env)) {
707
+ if (!key.startsWith("ALVAX_"))
708
+ continue;
709
+ if (key === "ALVAX_RUNTIME_API_URL")
710
+ continue;
711
+ if (key === "ALVAX_LISTEN_HOST")
712
+ continue;
713
+ if (key === "ALVAX_LISTEN_PORT")
714
+ continue;
715
+ delete env[key];
716
+ }
717
+ return env;
718
+ }
719
+ export function defaultPathForPlatform() {
720
+ if (process.platform === "win32") {
721
+ return "C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\Wbem";
722
+ }
723
+ return "/usr/local/bin:/opt/homebrew/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin";
724
+ }
725
+ function windowsPathExts(env) {
726
+ return (env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean);
727
+ }
728
+ async function pathExists(candidate) {
729
+ try {
730
+ await fs.access(candidate, process.platform === "win32" ? fsConstants.F_OK : fsConstants.X_OK);
731
+ return true;
732
+ }
733
+ catch {
734
+ return false;
735
+ }
736
+ }
737
+ async function resolveCommandPath(command, cwd, env) {
738
+ const hasPathSeparator = command.includes("/") || command.includes("\\");
739
+ if (hasPathSeparator) {
740
+ const absolute = path.isAbsolute(command) ? command : path.resolve(cwd, command);
741
+ return (await pathExists(absolute)) ? absolute : null;
742
+ }
743
+ const pathValue = env.PATH ?? env.Path ?? "";
744
+ const delimiter = process.platform === "win32" ? ";" : ":";
745
+ const dirs = pathValue.split(delimiter).filter(Boolean);
746
+ const exts = process.platform === "win32" ? windowsPathExts(env) : [""];
747
+ const hasExtension = process.platform === "win32" && path.extname(command).length > 0;
748
+ for (const dir of dirs) {
749
+ const candidates = process.platform === "win32"
750
+ ? hasExtension
751
+ ? [path.join(dir, command)]
752
+ : exts.map((ext) => path.join(dir, `${command}${ext}`))
753
+ : [path.join(dir, command)];
754
+ for (const candidate of candidates) {
755
+ if (await pathExists(candidate))
756
+ return candidate;
757
+ }
758
+ }
759
+ return null;
760
+ }
761
+ export async function resolveCommandForLogs(command, cwd, env, options = {}) {
762
+ const remote = options.remoteExecution ?? null;
763
+ if (remote) {
764
+ return `ssh://${remote.username}@${remote.host}:${remote.port}/${remote.remoteCwd} :: ${command}`;
765
+ }
766
+ return (await resolveCommandPath(command, cwd, env)) ?? command;
767
+ }
768
+ function quoteForCmd(arg) {
769
+ if (!arg.length)
770
+ return '""';
771
+ const escaped = arg.replace(/"/g, '""');
772
+ return /[\s"&<>|^()]/.test(escaped) ? `"${escaped}"` : escaped;
773
+ }
774
+ export function sanitizeSshRemoteEnv(env, inheritedEnv = process.env) {
775
+ return sanitizeRemoteExecutionEnv(env, inheritedEnv);
776
+ }
777
+ function resolveWindowsCmdShell(env) {
778
+ const fallbackRoot = env.SystemRoot || process.env.SystemRoot || "C:\\Windows";
779
+ return path.join(fallbackRoot, "System32", "cmd.exe");
780
+ }
781
+ async function resolveSpawnTarget(command, args, cwd, env, options = {}) {
782
+ const remote = options.remoteExecution ?? null;
783
+ if (remote) {
784
+ const sshResolved = await resolveCommandPath("ssh", process.cwd(), env);
785
+ if (!sshResolved) {
786
+ throw new Error('Command not found in PATH: "ssh"');
787
+ }
788
+ const spawnTarget = await buildSshSpawnTarget({
789
+ spec: remote,
790
+ command,
791
+ args,
792
+ env: Object.fromEntries(Object.entries(options.remoteEnv ?? {}).filter((entry) => typeof entry[1] === "string")),
793
+ });
794
+ return {
795
+ command: sshResolved,
796
+ args: spawnTarget.args,
797
+ cwd: process.cwd(),
798
+ cleanup: spawnTarget.cleanup,
799
+ };
800
+ }
801
+ const resolved = await resolveCommandPath(command, cwd, env);
802
+ const executable = resolved ?? command;
803
+ if (process.platform !== "win32") {
804
+ return { command: executable, args };
805
+ }
806
+ if (/\.(cmd|bat)$/i.test(executable)) {
807
+ // Always use cmd.exe for .cmd/.bat wrappers. Some environments override
808
+ // ComSpec to PowerShell, which breaks cmd-specific flags like /d /s /c.
809
+ const shell = resolveWindowsCmdShell(env);
810
+ const commandLine = [quoteForCmd(executable), ...args.map(quoteForCmd)].join(" ");
811
+ return {
812
+ command: shell,
813
+ args: ["/d", "/s", "/c", commandLine],
814
+ };
815
+ }
816
+ return { command: executable, args };
817
+ }
818
+ export function ensurePathInEnv(env) {
819
+ if (typeof env.PATH === "string" && env.PATH.length > 0)
820
+ return env;
821
+ if (typeof env.Path === "string" && env.Path.length > 0)
822
+ return env;
823
+ return { ...env, PATH: defaultPathForPlatform() };
824
+ }
825
+ export async function ensureAbsoluteDirectory(cwd, opts = {}) {
826
+ if (!path.isAbsolute(cwd)) {
827
+ throw new Error(`Working directory must be an absolute path: "${cwd}"`);
828
+ }
829
+ const assertDirectory = async () => {
830
+ const stats = await fs.stat(cwd);
831
+ if (!stats.isDirectory()) {
832
+ throw new Error(`Working directory is not a directory: "${cwd}"`);
833
+ }
834
+ };
835
+ try {
836
+ await assertDirectory();
837
+ return;
838
+ }
839
+ catch (err) {
840
+ const code = err.code;
841
+ if (!opts.createIfMissing || code !== "ENOENT") {
842
+ if (code === "ENOENT") {
843
+ throw new Error(`Working directory does not exist: "${cwd}"`);
844
+ }
845
+ throw err instanceof Error ? err : new Error(String(err));
846
+ }
847
+ }
848
+ try {
849
+ await fs.mkdir(cwd, { recursive: true });
850
+ await assertDirectory();
851
+ }
852
+ catch (err) {
853
+ const reason = err instanceof Error ? err.message : String(err);
854
+ throw new Error(`Could not create working directory "${cwd}": ${reason}`);
855
+ }
856
+ }
857
+ export async function resolveAlvaxSkillsDir(moduleDir, additionalCandidates = []) {
858
+ const candidates = [
859
+ ...ALVAX_SKILL_ROOT_RELATIVE_CANDIDATES.map((relativePath) => path.resolve(moduleDir, relativePath)),
860
+ ...additionalCandidates.map((candidate) => path.resolve(candidate)),
861
+ ];
862
+ const seenRoots = new Set();
863
+ for (const root of candidates) {
864
+ if (seenRoots.has(root))
865
+ continue;
866
+ seenRoots.add(root);
867
+ const isDirectory = await fs.stat(root).then((stats) => stats.isDirectory()).catch(() => false);
868
+ if (isDirectory)
869
+ return root;
870
+ }
871
+ return null;
872
+ }
873
+ async function readSkillRequired(skillDir) {
874
+ try {
875
+ const content = await fs.readFile(path.join(skillDir, "SKILL.md"), "utf8");
876
+ const normalized = content.replace(/\r\n/g, "\n");
877
+ if (!normalized.startsWith("---\n"))
878
+ return true;
879
+ const closing = normalized.indexOf("\n---\n", 4);
880
+ if (closing < 0)
881
+ return true;
882
+ const frontmatter = normalized.slice(4, closing);
883
+ return !/^\s*required\s*:\s*false\s*$/m.test(frontmatter);
884
+ }
885
+ catch {
886
+ return true;
887
+ }
888
+ }
889
+ export async function listAlvaxSkillEntries(moduleDir, additionalCandidates = []) {
890
+ const root = await resolveAlvaxSkillsDir(moduleDir, additionalCandidates);
891
+ if (!root)
892
+ return [];
893
+ try {
894
+ const entries = await fs.readdir(root, { withFileTypes: true });
895
+ const dirs = entries.filter((entry) => entry.isDirectory());
896
+ return Promise.all(dirs.map(async (entry) => {
897
+ const skillDir = path.join(root, entry.name);
898
+ const required = await readSkillRequired(skillDir);
899
+ return {
900
+ key: `alvaxai/alvax/${entry.name}`,
901
+ runtimeName: entry.name,
902
+ source: skillDir,
903
+ required,
904
+ requiredReason: required
905
+ ? "Bundled Alvax skills are always available for local adapters."
906
+ : null,
907
+ };
908
+ }));
909
+ }
910
+ catch {
911
+ return [];
912
+ }
913
+ }
914
+ export async function readInstalledSkillTargets(skillsHome) {
915
+ const entries = await fs.readdir(skillsHome, { withFileTypes: true }).catch(() => []);
916
+ const out = new Map();
917
+ for (const entry of entries) {
918
+ const fullPath = path.join(skillsHome, entry.name);
919
+ const linkedPath = entry.isSymbolicLink() ? await fs.readlink(fullPath).catch(() => null) : null;
920
+ out.set(entry.name, resolveInstalledEntryTarget(skillsHome, entry.name, entry, linkedPath));
921
+ }
922
+ return out;
923
+ }
924
+ export function buildPersistentSkillSnapshot(options) {
925
+ const { adapterType, availableEntries, desiredSkills, installed, skillsHome, locationLabel, installedDetail, missingDetail, externalConflictDetail, externalDetail, } = options;
926
+ const availableByKey = new Map(availableEntries.map((entry) => [entry.key, entry]));
927
+ const desiredSet = new Set(desiredSkills);
928
+ const entries = [];
929
+ const warnings = [...(options.warnings ?? [])];
930
+ for (const available of availableEntries) {
931
+ const installedEntry = installed.get(available.runtimeName) ?? null;
932
+ const desired = desiredSet.has(available.key);
933
+ let state = "available";
934
+ let managed = false;
935
+ let detail = null;
936
+ if (installedEntry?.targetPath === available.source) {
937
+ managed = true;
938
+ state = desired ? "installed" : "stale";
939
+ detail = installedDetail ?? null;
940
+ }
941
+ else if (installedEntry) {
942
+ state = "external";
943
+ detail = desired ? externalConflictDetail : externalDetail;
944
+ }
945
+ else if (desired) {
946
+ state = "missing";
947
+ detail = missingDetail;
948
+ }
949
+ entries.push({
950
+ key: available.key,
951
+ runtimeName: available.runtimeName,
952
+ desired,
953
+ managed,
954
+ state,
955
+ sourcePath: available.source,
956
+ targetPath: path.join(skillsHome, available.runtimeName),
957
+ detail,
958
+ required: Boolean(available.required),
959
+ requiredReason: available.requiredReason ?? null,
960
+ ...buildManagedSkillOrigin(available),
961
+ });
962
+ }
963
+ for (const desiredSkill of desiredSkills) {
964
+ if (availableByKey.has(desiredSkill))
965
+ continue;
966
+ warnings.push(`Desired skill "${desiredSkill}" is not available from the Alvax skills directory.`);
967
+ entries.push({
968
+ key: desiredSkill,
969
+ runtimeName: null,
970
+ desired: true,
971
+ managed: true,
972
+ state: "missing",
973
+ sourcePath: null,
974
+ targetPath: null,
975
+ detail: "Alvax cannot find this skill in the local runtime skills directory.",
976
+ origin: "external_unknown",
977
+ originLabel: "External or unavailable",
978
+ readOnly: false,
979
+ });
980
+ }
981
+ for (const [name, installedEntry] of installed.entries()) {
982
+ if (availableEntries.some((entry) => entry.runtimeName === name))
983
+ continue;
984
+ entries.push({
985
+ key: name,
986
+ runtimeName: name,
987
+ desired: false,
988
+ managed: false,
989
+ state: "external",
990
+ origin: "user_installed",
991
+ originLabel: "User-installed",
992
+ locationLabel: skillLocationLabel(locationLabel),
993
+ readOnly: true,
994
+ sourcePath: null,
995
+ targetPath: installedEntry.targetPath ?? path.join(skillsHome, name),
996
+ detail: externalDetail,
997
+ });
998
+ }
999
+ entries.sort((left, right) => left.key.localeCompare(right.key));
1000
+ return {
1001
+ adapterType,
1002
+ supported: true,
1003
+ mode: "persistent",
1004
+ desiredSkills,
1005
+ entries,
1006
+ warnings,
1007
+ };
1008
+ }
1009
+ function normalizeConfiguredAlvaxRuntimeSkills(value) {
1010
+ if (!Array.isArray(value))
1011
+ return [];
1012
+ const out = [];
1013
+ for (const rawEntry of value) {
1014
+ const entry = parseObject(rawEntry);
1015
+ const key = asString(entry.key, asString(entry.name, "")).trim();
1016
+ const runtimeName = asString(entry.runtimeName, asString(entry.name, "")).trim();
1017
+ const source = asString(entry.source, "").trim();
1018
+ if (!key || !runtimeName || !source)
1019
+ continue;
1020
+ out.push({
1021
+ key,
1022
+ runtimeName,
1023
+ source,
1024
+ required: asBoolean(entry.required, false),
1025
+ requiredReason: typeof entry.requiredReason === "string" && entry.requiredReason.trim().length > 0
1026
+ ? entry.requiredReason.trim()
1027
+ : null,
1028
+ });
1029
+ }
1030
+ return out;
1031
+ }
1032
+ export async function readAlvaxRuntimeSkillEntries(config, moduleDir, additionalCandidates = []) {
1033
+ const configuredEntries = normalizeConfiguredAlvaxRuntimeSkills(config.alvaxRuntimeSkills);
1034
+ if (configuredEntries.length > 0)
1035
+ return configuredEntries;
1036
+ return listAlvaxSkillEntries(moduleDir, additionalCandidates);
1037
+ }
1038
+ export async function readAlvaxSkillMarkdown(moduleDir, skillKey) {
1039
+ const normalized = skillKey.trim().toLowerCase();
1040
+ if (!normalized)
1041
+ return null;
1042
+ const entries = await listAlvaxSkillEntries(moduleDir);
1043
+ const match = entries.find((entry) => entry.key === normalized);
1044
+ if (!match)
1045
+ return null;
1046
+ try {
1047
+ return await fs.readFile(path.join(match.source, "SKILL.md"), "utf8");
1048
+ }
1049
+ catch {
1050
+ return null;
1051
+ }
1052
+ }
1053
+ export function readAlvaxSkillSyncPreference(config) {
1054
+ const raw = config.alvaxSkillSync;
1055
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
1056
+ return { explicit: false, desiredSkills: [] };
1057
+ }
1058
+ const syncConfig = raw;
1059
+ const desiredValues = syncConfig.desiredSkills;
1060
+ const desired = Array.isArray(desiredValues)
1061
+ ? desiredValues
1062
+ .filter((value) => typeof value === "string")
1063
+ .map((value) => value.trim())
1064
+ .filter(Boolean)
1065
+ : [];
1066
+ return {
1067
+ explicit: Object.prototype.hasOwnProperty.call(raw, "desiredSkills"),
1068
+ desiredSkills: Array.from(new Set(desired)),
1069
+ };
1070
+ }
1071
+ function canonicalizeDesiredAlvaxSkillReference(reference, availableEntries) {
1072
+ const normalizedReference = reference.trim().toLowerCase();
1073
+ if (!normalizedReference)
1074
+ return "";
1075
+ const exactKey = availableEntries.find((entry) => entry.key.trim().toLowerCase() === normalizedReference);
1076
+ if (exactKey)
1077
+ return exactKey.key;
1078
+ const byRuntimeName = availableEntries.filter((entry) => typeof entry.runtimeName === "string" && entry.runtimeName.trim().toLowerCase() === normalizedReference);
1079
+ if (byRuntimeName.length === 1)
1080
+ return byRuntimeName[0].key;
1081
+ const slugMatches = availableEntries.filter((entry) => entry.key.trim().toLowerCase().split("/").pop() === normalizedReference);
1082
+ if (slugMatches.length === 1)
1083
+ return slugMatches[0].key;
1084
+ return normalizedReference;
1085
+ }
1086
+ export function resolveAlvaxDesiredSkillNames(config, availableEntries) {
1087
+ const preference = readAlvaxSkillSyncPreference(config);
1088
+ const requiredSkills = availableEntries
1089
+ .filter((entry) => entry.required)
1090
+ .map((entry) => entry.key);
1091
+ if (!preference.explicit) {
1092
+ return Array.from(new Set(requiredSkills));
1093
+ }
1094
+ const desiredSkills = preference.desiredSkills
1095
+ .map((reference) => canonicalizeDesiredAlvaxSkillReference(reference, availableEntries))
1096
+ .filter(Boolean);
1097
+ return Array.from(new Set([...requiredSkills, ...desiredSkills]));
1098
+ }
1099
+ export function writeAlvaxSkillSyncPreference(config, desiredSkills) {
1100
+ const next = { ...config };
1101
+ const raw = next.alvaxSkillSync;
1102
+ const current = typeof raw === "object" && raw !== null && !Array.isArray(raw)
1103
+ ? { ...raw }
1104
+ : {};
1105
+ current.desiredSkills = Array.from(new Set(desiredSkills
1106
+ .map((value) => value.trim())
1107
+ .filter(Boolean)));
1108
+ next.alvaxSkillSync = current;
1109
+ return next;
1110
+ }
1111
+ export async function ensureAlvaxSkillSymlink(source, target, linkSkill = (linkSource, linkTarget) => fs.symlink(linkSource, linkTarget)) {
1112
+ const resolvedSource = path.resolve(source);
1113
+ const existing = await fs.lstat(target).catch(() => null);
1114
+ if (!existing) {
1115
+ await linkSkill(resolvedSource, target);
1116
+ return "created";
1117
+ }
1118
+ if (!existing.isSymbolicLink()) {
1119
+ return "skipped";
1120
+ }
1121
+ const linkedPath = await fs.readlink(target).catch(() => null);
1122
+ if (!linkedPath)
1123
+ return "skipped";
1124
+ const resolvedLinkedPath = path.resolve(path.dirname(target), linkedPath);
1125
+ if (resolvedLinkedPath === resolvedSource) {
1126
+ return "skipped";
1127
+ }
1128
+ const linkedPathExists = await fs.stat(resolvedLinkedPath).then(() => true).catch(() => false);
1129
+ if (linkedPathExists
1130
+ && !isPreviousAlvaxManagedSkillSymlinkTarget(resolvedSource, target, resolvedLinkedPath)) {
1131
+ return "skipped";
1132
+ }
1133
+ const replaced = await replaceSymlinkAtomically(resolvedSource, target, resolvedLinkedPath, linkSkill);
1134
+ return replaced ? "repaired" : "skipped";
1135
+ }
1136
+ function isAlvaxManagedMaterializedSkillPath(candidate) {
1137
+ const normalized = normalizePathSlashes(path.resolve(candidate));
1138
+ return normalized.includes("/companies/")
1139
+ && (normalized.includes("/skills/__catalog__/")
1140
+ || normalized.includes("/skills/__runtime__/"));
1141
+ }
1142
+ function isSkillVersionDirName(runtimeName, candidateName) {
1143
+ return candidateName === runtimeName || candidateName.startsWith(`${runtimeName}--`);
1144
+ }
1145
+ function isPreviousAlvaxManagedSkillSymlinkTarget(source, target, linkedPath) {
1146
+ if (!isAlvaxManagedMaterializedSkillPath(source) || !isAlvaxManagedMaterializedSkillPath(linkedPath)) {
1147
+ return false;
1148
+ }
1149
+ if (path.dirname(source) !== path.dirname(linkedPath))
1150
+ return false;
1151
+ const runtimeName = path.basename(target);
1152
+ return isSkillVersionDirName(runtimeName, path.basename(source))
1153
+ && isSkillVersionDirName(runtimeName, path.basename(linkedPath));
1154
+ }
1155
+ async function replaceSymlinkAtomically(source, target, expectedLinkedPath, linkSkill) {
1156
+ const targetDir = path.dirname(target);
1157
+ const tempTarget = path.join(targetDir, `.${path.basename(target)}.tmp-${randomUUID()}`);
1158
+ await linkSkill(source, tempTarget);
1159
+ try {
1160
+ const current = await fs.lstat(target).catch(() => null);
1161
+ if (!current?.isSymbolicLink())
1162
+ return false;
1163
+ const currentLinkedPath = await fs.readlink(target).catch(() => null);
1164
+ if (!currentLinkedPath)
1165
+ return false;
1166
+ if (path.resolve(targetDir, currentLinkedPath) !== expectedLinkedPath)
1167
+ return false;
1168
+ await fs.rename(tempTarget, target);
1169
+ return true;
1170
+ }
1171
+ finally {
1172
+ await fs.rm(tempTarget, { recursive: true, force: true }).catch(() => undefined);
1173
+ }
1174
+ }
1175
+ async function hashSkillDirectory(root) {
1176
+ const hash = createHash("sha256");
1177
+ async function visit(candidate, relativePath) {
1178
+ const stat = await fs.lstat(candidate);
1179
+ if (stat.isSymbolicLink()) {
1180
+ hash.update(`symlink:${relativePath}\n`);
1181
+ return;
1182
+ }
1183
+ if (stat.isDirectory()) {
1184
+ hash.update(`dir:${relativePath}\n`);
1185
+ const entries = await fs.readdir(candidate, { withFileTypes: true });
1186
+ entries.sort((left, right) => left.name.localeCompare(right.name));
1187
+ for (const entry of entries) {
1188
+ const childRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
1189
+ await visit(path.join(candidate, entry.name), childRelativePath);
1190
+ }
1191
+ return;
1192
+ }
1193
+ if (stat.isFile()) {
1194
+ hash.update(`file:${relativePath}:${stat.mode}\n`);
1195
+ hash.update(await fs.readFile(candidate));
1196
+ hash.update("\n");
1197
+ return;
1198
+ }
1199
+ hash.update(`other:${relativePath}:${stat.mode}\n`);
1200
+ }
1201
+ await visit(root, "");
1202
+ return hash.digest("hex");
1203
+ }
1204
+ async function materializedSkillFingerprintMatches(targetRoot, sourceFingerprint) {
1205
+ try {
1206
+ const raw = JSON.parse(await fs.readFile(path.join(targetRoot, MATERIALIZED_SKILL_SENTINEL), "utf8"));
1207
+ const parsed = parseObject(raw);
1208
+ return parsed.version === 1 && parsed.sourceFingerprint === sourceFingerprint;
1209
+ }
1210
+ catch {
1211
+ return false;
1212
+ }
1213
+ }
1214
+ async function acquireMaterializeLock(lockDir) {
1215
+ await fs.mkdir(path.dirname(lockDir), { recursive: true });
1216
+ const deadline = Date.now() + MATERIALIZED_SKILL_LOCK_STALE_MS;
1217
+ while (true) {
1218
+ try {
1219
+ await fs.mkdir(lockDir);
1220
+ await fs.writeFile(path.join(lockDir, MATERIALIZED_SKILL_LOCK_OWNER), `${JSON.stringify({ pid: process.pid, createdAt: new Date().toISOString() })}\n`, "utf8");
1221
+ return async () => {
1222
+ await fs.rm(lockDir, { recursive: true, force: true });
1223
+ };
1224
+ }
1225
+ catch (err) {
1226
+ const code = err && typeof err === "object" ? err.code : null;
1227
+ if (code !== "EEXIST")
1228
+ throw err;
1229
+ if (await removeStaleMaterializeLock(lockDir, MATERIALIZED_SKILL_LOCK_STALE_MS))
1230
+ continue;
1231
+ if (Date.now() >= deadline) {
1232
+ throw new Error(`Timed out waiting for Alvax skill materialization lock at ${lockDir}`);
1233
+ }
1234
+ await new Promise((resolve) => setTimeout(resolve, 50));
1235
+ }
1236
+ }
1237
+ }
1238
+ function isPidAlive(pid) {
1239
+ if (!Number.isInteger(pid) || pid <= 0)
1240
+ return false;
1241
+ try {
1242
+ process.kill(pid, 0);
1243
+ return true;
1244
+ }
1245
+ catch (err) {
1246
+ const code = err && typeof err === "object" ? err.code : null;
1247
+ return code === "EPERM";
1248
+ }
1249
+ }
1250
+ async function removeStaleMaterializeLock(lockDir, staleMs) {
1251
+ const ownerPath = path.join(lockDir, MATERIALIZED_SKILL_LOCK_OWNER);
1252
+ let shouldRemove = false;
1253
+ try {
1254
+ const raw = JSON.parse(await fs.readFile(ownerPath, "utf8"));
1255
+ const owner = parseObject(raw);
1256
+ const pid = typeof owner.pid === "number" ? owner.pid : 0;
1257
+ const createdAt = typeof owner.createdAt === "string" ? Date.parse(owner.createdAt) : Number.NaN;
1258
+ const ageMs = Number.isFinite(createdAt) ? Date.now() - createdAt : staleMs + 1;
1259
+ shouldRemove = !isPidAlive(pid) || ageMs > staleMs;
1260
+ }
1261
+ catch {
1262
+ const stat = await fs.stat(lockDir).catch(() => null);
1263
+ shouldRemove = !stat || Date.now() - stat.mtimeMs > staleMs;
1264
+ }
1265
+ if (!shouldRemove)
1266
+ return false;
1267
+ await fs.rm(lockDir, { recursive: true, force: true }).catch(() => { });
1268
+ return true;
1269
+ }
1270
+ export async function materializeAlvaxSkillCopy(source, target) {
1271
+ const sourceRoot = path.resolve(source);
1272
+ const targetRoot = path.resolve(target);
1273
+ const relativeTarget = path.relative(sourceRoot, targetRoot);
1274
+ const relativeSource = path.relative(targetRoot, sourceRoot);
1275
+ if (!relativeTarget ||
1276
+ (!relativeTarget.startsWith("..") && !path.isAbsolute(relativeTarget)) ||
1277
+ !relativeSource ||
1278
+ (!relativeSource.startsWith("..") && !path.isAbsolute(relativeSource))) {
1279
+ throw new Error("Refusing to materialize a skill into itself, an ancestor, or one of its descendants.");
1280
+ }
1281
+ const rootStat = await fs.lstat(sourceRoot);
1282
+ if (rootStat.isSymbolicLink()) {
1283
+ throw new Error("Refusing to materialize a skill root that is itself a symlink.");
1284
+ }
1285
+ if (!rootStat.isDirectory()) {
1286
+ throw new Error("Alvax skills must be directories.");
1287
+ }
1288
+ const result = {
1289
+ copiedFiles: 0,
1290
+ skippedSymlinks: [],
1291
+ };
1292
+ const lockDir = `${targetRoot}.lock`;
1293
+ const releaseLock = await acquireMaterializeLock(lockDir);
1294
+ const tempRoot = `${targetRoot}.tmp-${process.pid}-${randomUUID()}`;
1295
+ async function copyEntry(sourcePath, targetPath, relativePath) {
1296
+ const stat = await fs.lstat(sourcePath);
1297
+ if (stat.isSymbolicLink()) {
1298
+ result.skippedSymlinks.push(relativePath || ".");
1299
+ return;
1300
+ }
1301
+ if (stat.isDirectory()) {
1302
+ await fs.mkdir(targetPath, { recursive: true });
1303
+ const entries = await fs.readdir(sourcePath, { withFileTypes: true });
1304
+ entries.sort((left, right) => left.name.localeCompare(right.name));
1305
+ for (const entry of entries) {
1306
+ const childRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
1307
+ await copyEntry(path.join(sourcePath, entry.name), path.join(targetPath, entry.name), childRelativePath);
1308
+ }
1309
+ return;
1310
+ }
1311
+ if (stat.isFile()) {
1312
+ await fs.mkdir(path.dirname(targetPath), { recursive: true });
1313
+ await fs.copyFile(sourcePath, targetPath, fsConstants.COPYFILE_FICLONE).catch(async () => {
1314
+ await fs.copyFile(sourcePath, targetPath);
1315
+ });
1316
+ await fs.chmod(targetPath, stat.mode).catch(() => { });
1317
+ result.copiedFiles += 1;
1318
+ }
1319
+ }
1320
+ try {
1321
+ const sourceFingerprint = await hashSkillDirectory(sourceRoot);
1322
+ if (await materializedSkillFingerprintMatches(targetRoot, sourceFingerprint))
1323
+ return result;
1324
+ await copyEntry(sourceRoot, tempRoot, "");
1325
+ await fs.writeFile(path.join(tempRoot, MATERIALIZED_SKILL_SENTINEL), `${JSON.stringify({
1326
+ version: 1,
1327
+ sourceFingerprint,
1328
+ copiedFiles: result.copiedFiles,
1329
+ skippedSymlinks: result.skippedSymlinks,
1330
+ }, null, 2)}\n`, "utf8");
1331
+ if (await materializedSkillFingerprintMatches(targetRoot, sourceFingerprint))
1332
+ return result;
1333
+ await fs.rm(targetRoot, { recursive: true, force: true });
1334
+ await fs.rename(tempRoot, targetRoot);
1335
+ return result;
1336
+ }
1337
+ finally {
1338
+ await fs.rm(tempRoot, { recursive: true, force: true }).catch(() => { });
1339
+ await releaseLock();
1340
+ }
1341
+ }
1342
+ export async function removeMaintainerOnlySkillSymlinks(skillsHome, allowedSkillNames) {
1343
+ const allowed = new Set(Array.from(allowedSkillNames));
1344
+ try {
1345
+ const entries = await fs.readdir(skillsHome, { withFileTypes: true });
1346
+ const removed = [];
1347
+ for (const entry of entries) {
1348
+ if (allowed.has(entry.name))
1349
+ continue;
1350
+ const target = path.join(skillsHome, entry.name);
1351
+ const existing = await fs.lstat(target).catch(() => null);
1352
+ if (!existing?.isSymbolicLink())
1353
+ continue;
1354
+ const linkedPath = await fs.readlink(target).catch(() => null);
1355
+ if (!linkedPath)
1356
+ continue;
1357
+ const resolvedLinkedPath = path.isAbsolute(linkedPath)
1358
+ ? linkedPath
1359
+ : path.resolve(path.dirname(target), linkedPath);
1360
+ if (!isMaintainerOnlySkillTarget(linkedPath) &&
1361
+ !isMaintainerOnlySkillTarget(resolvedLinkedPath)) {
1362
+ continue;
1363
+ }
1364
+ await fs.unlink(target);
1365
+ removed.push(entry.name);
1366
+ }
1367
+ return removed;
1368
+ }
1369
+ catch {
1370
+ return [];
1371
+ }
1372
+ }
1373
+ export async function ensureCommandResolvable(command, cwd, env, options = {}) {
1374
+ if (options.remoteExecution) {
1375
+ const resolvedSsh = await resolveCommandPath("ssh", process.cwd(), env);
1376
+ if (resolvedSsh)
1377
+ return;
1378
+ throw new Error('Command not found in PATH: "ssh"');
1379
+ }
1380
+ const resolved = await resolveCommandPath(command, cwd, env);
1381
+ if (resolved)
1382
+ return;
1383
+ if (command.includes("/") || command.includes("\\")) {
1384
+ const absolute = path.isAbsolute(command) ? command : path.resolve(cwd, command);
1385
+ throw new Error(`Command is not executable: "${command}" (resolved: "${absolute}")`);
1386
+ }
1387
+ throw new Error(`Command not found in PATH: "${command}"`);
1388
+ }
1389
+ export async function runChildProcess(runId, command, args, opts) {
1390
+ const onLogError = opts.onLogError ?? ((err, id, msg) => console.warn({ err, runId: id }, msg));
1391
+ return new Promise((resolve, reject) => {
1392
+ const rawMerged = {
1393
+ ...sanitizeInheritedAlvaxEnv(process.env),
1394
+ ...opts.env,
1395
+ };
1396
+ // Strip Claude Code nesting-guard env vars so spawned `claude` processes
1397
+ // don't refuse to start with "cannot be launched inside another session".
1398
+ // These vars leak in when the Alvax server itself is started from
1399
+ // within a Claude Code session (e.g. `npx alvaxai run` in a terminal
1400
+ // owned by Claude Code) or when cron inherits a contaminated shell env.
1401
+ const CLAUDE_CODE_NESTING_VARS = [
1402
+ "CLAUDECODE",
1403
+ "CLAUDE_CODE_ENTRYPOINT",
1404
+ "CLAUDE_CODE_SESSION",
1405
+ "CLAUDE_CODE_PARENT_SESSION",
1406
+ ];
1407
+ for (const key of CLAUDE_CODE_NESTING_VARS) {
1408
+ delete rawMerged[key];
1409
+ }
1410
+ const mergedEnv = ensurePathInEnv(rawMerged);
1411
+ void resolveSpawnTarget(command, args, opts.cwd, mergedEnv, {
1412
+ remoteExecution: opts.remoteExecution ?? null,
1413
+ remoteEnv: opts.remoteExecution ? opts.env : null,
1414
+ })
1415
+ .then((target) => {
1416
+ const child = spawn(target.command, target.args, {
1417
+ cwd: target.cwd ?? opts.cwd,
1418
+ env: mergedEnv,
1419
+ detached: process.platform !== "win32",
1420
+ shell: false,
1421
+ stdio: [opts.stdin != null ? "pipe" : "ignore", "pipe", "pipe"],
1422
+ });
1423
+ const startedAt = new Date().toISOString();
1424
+ const processGroupId = resolveProcessGroupId(child);
1425
+ const spawnPersistPromise = typeof child.pid === "number" && child.pid > 0 && opts.onSpawn
1426
+ ? opts.onSpawn({ pid: child.pid, processGroupId, startedAt }).catch((err) => {
1427
+ onLogError(err, runId, "failed to record child process metadata");
1428
+ })
1429
+ : Promise.resolve();
1430
+ runningProcesses.set(runId, { child, graceSec: opts.graceSec, processGroupId });
1431
+ let timedOut = false;
1432
+ let stdout = "";
1433
+ let stderr = "";
1434
+ let logChain = Promise.resolve();
1435
+ let terminalResultSeen = false;
1436
+ let terminalCleanupStarted = false;
1437
+ let terminalCleanupTimer = null;
1438
+ let terminalCleanupKillTimer = null;
1439
+ let terminalResultStdoutScanOffset = 0;
1440
+ let terminalResultStderrScanOffset = 0;
1441
+ const clearTerminalCleanupTimers = () => {
1442
+ if (terminalCleanupTimer)
1443
+ clearTimeout(terminalCleanupTimer);
1444
+ if (terminalCleanupKillTimer)
1445
+ clearTimeout(terminalCleanupKillTimer);
1446
+ terminalCleanupTimer = null;
1447
+ terminalCleanupKillTimer = null;
1448
+ };
1449
+ const maybeArmTerminalResultCleanup = () => {
1450
+ const terminalCleanup = opts.terminalResultCleanup;
1451
+ if (!terminalCleanup || terminalCleanupStarted || timedOut)
1452
+ return;
1453
+ if (!terminalResultSeen) {
1454
+ const stdoutStart = Math.max(0, terminalResultStdoutScanOffset - TERMINAL_RESULT_SCAN_OVERLAP_CHARS);
1455
+ const stderrStart = Math.max(0, terminalResultStderrScanOffset - TERMINAL_RESULT_SCAN_OVERLAP_CHARS);
1456
+ const scanOutput = {
1457
+ stdout: stdout.slice(stdoutStart),
1458
+ stderr: stderr.slice(stderrStart),
1459
+ };
1460
+ terminalResultStdoutScanOffset = stdout.length;
1461
+ terminalResultStderrScanOffset = stderr.length;
1462
+ if (scanOutput.stdout.length === 0 && scanOutput.stderr.length === 0)
1463
+ return;
1464
+ try {
1465
+ terminalResultSeen = terminalCleanup.hasTerminalResult(scanOutput);
1466
+ }
1467
+ catch (err) {
1468
+ onLogError(err, runId, "failed to inspect terminal adapter output");
1469
+ }
1470
+ }
1471
+ if (!terminalResultSeen)
1472
+ return;
1473
+ if (terminalCleanupTimer)
1474
+ return;
1475
+ const graceMs = Math.max(0, terminalCleanup.graceMs ?? 5_000);
1476
+ terminalCleanupTimer = setTimeout(() => {
1477
+ terminalCleanupTimer = null;
1478
+ if (terminalCleanupStarted || timedOut)
1479
+ return;
1480
+ terminalCleanupStarted = true;
1481
+ signalRunningProcess({ child, processGroupId }, "SIGTERM");
1482
+ terminalCleanupKillTimer = setTimeout(() => {
1483
+ terminalCleanupKillTimer = null;
1484
+ signalRunningProcess({ child, processGroupId }, "SIGKILL");
1485
+ }, Math.max(1, opts.graceSec) * 1000);
1486
+ }, graceMs);
1487
+ };
1488
+ const timeout = opts.timeoutSec > 0
1489
+ ? setTimeout(() => {
1490
+ timedOut = true;
1491
+ clearTerminalCleanupTimers();
1492
+ signalRunningProcess({ child, processGroupId }, "SIGTERM");
1493
+ setTimeout(() => {
1494
+ signalRunningProcess({ child, processGroupId }, "SIGKILL");
1495
+ }, Math.max(1, opts.graceSec) * 1000);
1496
+ }, opts.timeoutSec * 1000)
1497
+ : null;
1498
+ child.stdout?.on("data", (chunk) => {
1499
+ const readable = child.stdout;
1500
+ if (!readable)
1501
+ return;
1502
+ readable.pause();
1503
+ const text = String(chunk);
1504
+ stdout = appendWithCap(stdout, text);
1505
+ maybeArmTerminalResultCleanup();
1506
+ logChain = logChain
1507
+ .then(() => opts.onLog("stdout", text))
1508
+ .catch((err) => onLogError(err, runId, "failed to append stdout log chunk"))
1509
+ .finally(() => {
1510
+ maybeArmTerminalResultCleanup();
1511
+ resumeReadable(readable);
1512
+ });
1513
+ });
1514
+ child.stderr?.on("data", (chunk) => {
1515
+ const readable = child.stderr;
1516
+ if (!readable)
1517
+ return;
1518
+ readable.pause();
1519
+ const text = String(chunk);
1520
+ stderr = appendWithCap(stderr, text);
1521
+ maybeArmTerminalResultCleanup();
1522
+ logChain = logChain
1523
+ .then(() => opts.onLog("stderr", text))
1524
+ .catch((err) => onLogError(err, runId, "failed to append stderr log chunk"))
1525
+ .finally(() => {
1526
+ maybeArmTerminalResultCleanup();
1527
+ resumeReadable(readable);
1528
+ });
1529
+ });
1530
+ const stdin = child.stdin;
1531
+ if (opts.stdin != null && stdin) {
1532
+ void spawnPersistPromise.finally(() => {
1533
+ if (child.killed || stdin.destroyed)
1534
+ return;
1535
+ stdin.write(opts.stdin);
1536
+ stdin.end();
1537
+ });
1538
+ }
1539
+ child.on("error", (err) => {
1540
+ if (timeout)
1541
+ clearTimeout(timeout);
1542
+ clearTerminalCleanupTimers();
1543
+ runningProcesses.delete(runId);
1544
+ void target.cleanup?.();
1545
+ const errno = err.code;
1546
+ const pathValue = mergedEnv.PATH ?? mergedEnv.Path ?? "";
1547
+ const msg = errno === "ENOENT"
1548
+ ? `Failed to start command "${command}" in "${opts.cwd}". Verify adapter command, working directory, and PATH (${pathValue}).`
1549
+ : `Failed to start command "${command}" in "${opts.cwd}": ${err.message}`;
1550
+ reject(new Error(msg));
1551
+ });
1552
+ child.on("exit", () => {
1553
+ maybeArmTerminalResultCleanup();
1554
+ });
1555
+ child.on("close", (code, signal) => {
1556
+ if (timeout)
1557
+ clearTimeout(timeout);
1558
+ clearTerminalCleanupTimers();
1559
+ runningProcesses.delete(runId);
1560
+ void logChain.finally(() => {
1561
+ void Promise.resolve()
1562
+ .then(() => target.cleanup?.())
1563
+ .finally(() => {
1564
+ resolve({
1565
+ exitCode: code,
1566
+ signal,
1567
+ timedOut,
1568
+ stdout,
1569
+ stderr,
1570
+ pid: child.pid ?? null,
1571
+ startedAt,
1572
+ });
1573
+ });
1574
+ });
1575
+ });
1576
+ })
1577
+ .catch(reject);
1578
+ });
1579
+ }
1580
+ //# sourceMappingURL=server-utils.js.map