log-llm-config-staging 1.4.4 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,14 @@
1
- import { execFileSync } from 'node:child_process';
2
- import { homedir } from 'node:os';
1
+ import { spawnSync } from 'node:child_process';
2
+ import { closeSync, mkdtempSync, openSync, readFileSync, rmSync } from 'node:fs';
3
+ import { homedir, tmpdir } from 'node:os';
3
4
  import { join } from 'node:path';
4
5
  export const SKILLS_CLI_FILE_TYPE = 'skills_cli_installed';
5
6
  export const SKILLS_CLI_INSTALLED_PATH = join(homedir(), '.agents', '.skills-cli-installed.json');
6
7
  /** Override the skills package spec, e.g. `skills@1.5.10`. Default uses whatever is on the machine. */
7
8
  export const SKILLS_CLI_NPX_PACKAGE_ENV = 'SKILLS_CLI_NPX_PACKAGE';
8
9
  const LIST_TIMEOUT_MS = 120_000;
10
+ /** stderr from npx/npm only; stdout is streamed to a temp file (avoids pipe/maxBuffer truncation). */
11
+ const LIST_STDERR_MAX_BUFFER = 16 * 1024 * 1024;
9
12
  function listExecEnv() {
10
13
  return {
11
14
  ...process.env,
@@ -16,20 +19,61 @@ function npxPackageSpec() {
16
19
  const fromEnv = (process.env[SKILLS_CLI_NPX_PACKAGE_ENV] || '').trim();
17
20
  return fromEnv || 'skills';
18
21
  }
19
- export function runSkillsListJson(args, cwd) {
20
- const out = execFileSync('npx', [npxPackageSpec(), 'list', ...args, '--json'], {
21
- encoding: 'utf8',
22
- cwd,
23
- timeout: LIST_TIMEOUT_MS,
24
- env: listExecEnv(),
25
- });
26
- const trimmed = out.trim();
22
+ /** Strip leading npx/npm noise and parse the skills `list --json` array payload. */
23
+ export function parseSkillsListJsonStdout(stdout) {
24
+ const trimmed = stdout.trim();
27
25
  if (!trimmed)
28
26
  return [];
29
- const parsed = JSON.parse(trimmed);
30
- if (!Array.isArray(parsed))
31
- return [];
32
- return parsed;
27
+ const start = trimmed.indexOf('[');
28
+ if (start < 0) {
29
+ throw new SyntaxError('skills list --json: no JSON array in stdout');
30
+ }
31
+ let end = trimmed.lastIndexOf(']');
32
+ let lastErr;
33
+ while (end > start) {
34
+ const slice = trimmed.slice(start, end + 1);
35
+ try {
36
+ const parsed = JSON.parse(slice);
37
+ if (!Array.isArray(parsed))
38
+ return [];
39
+ return parsed;
40
+ }
41
+ catch (err) {
42
+ lastErr = err instanceof SyntaxError ? err : new SyntaxError(String(err));
43
+ end = trimmed.lastIndexOf(']', end - 1);
44
+ }
45
+ }
46
+ throw lastErr ?? new SyntaxError('skills list --json: could not parse stdout');
47
+ }
48
+ export function runSkillsListJson(args, cwd) {
49
+ const tmpDir = mkdtempSync(join(tmpdir(), 'optimus-skills-list-'));
50
+ const outPath = join(tmpDir, 'stdout.json');
51
+ const outFd = openSync(outPath, 'w');
52
+ try {
53
+ const child = spawnSync('npx', [npxPackageSpec(), 'list', ...args, '--json'], {
54
+ cwd,
55
+ timeout: LIST_TIMEOUT_MS,
56
+ env: listExecEnv(),
57
+ stdio: ['ignore', outFd, 'pipe'],
58
+ maxBuffer: LIST_STDERR_MAX_BUFFER,
59
+ });
60
+ if (child.error)
61
+ throw child.error;
62
+ if (child.status !== 0) {
63
+ const stderr = (child.stderr ?? '').toString().trim();
64
+ throw new Error(stderr || `npx skills list exited ${child.status ?? 'unknown'}`);
65
+ }
66
+ }
67
+ finally {
68
+ closeSync(outFd);
69
+ }
70
+ try {
71
+ const out = readFileSync(outPath, 'utf8');
72
+ return parseSkillsListJsonStdout(out);
73
+ }
74
+ finally {
75
+ rmSync(tmpDir, { recursive: true, force: true });
76
+ }
33
77
  }
34
78
  function normalizeListRows(rows, scope) {
35
79
  const out = [];
@@ -60,39 +104,43 @@ export function formatSkillsListScopeForHookLog(scopeLabel, entries) {
60
104
  .join(' | ');
61
105
  return `skills_cli list ${scopeLabel}: ${entries.length} skill(s) — ${detail}`;
62
106
  }
107
+ function runSkillsListForScope(scopeLabel, args, projectRoot, logLine) {
108
+ try {
109
+ return runSkillsListJson(args, projectRoot);
110
+ }
111
+ catch (err) {
112
+ const msg = err instanceof Error ? err.message : String(err);
113
+ logLine(`skills_cli: list ${scopeLabel} --json failed: ${msg}`);
114
+ return [];
115
+ }
116
+ }
63
117
  export function collectSkillsCliInstalled(projectRoot, log) {
64
118
  const logLine = (message) => {
65
119
  log?.(message);
66
120
  };
67
- try {
68
- const packageSpec = npxPackageSpec();
69
- logLine(`skills_cli: npx ${packageSpec} list -g --json && list --json (projectRoot=${projectRoot})`);
70
- const globalRows = runSkillsListJson(['-g'], projectRoot);
71
- const projectRows = runSkillsListJson([], projectRoot);
72
- const global = normalizeListRows(globalRows, 'global');
73
- const project = normalizeListRows(projectRows, 'project');
74
- logLine(formatSkillsListScopeForHookLog('-g', global));
75
- logLine(formatSkillsListScopeForHookLog('project', project));
76
- const payload = {
77
- version: 1,
78
- skills_cli_version: packageSpec,
79
- generated_at: new Date().toISOString(),
80
- global,
81
- project,
82
- };
83
- if (payload.global.length === 0 && payload.project.length === 0) {
84
- logLine('skills_cli_installed: not uploaded (no global or project skills)');
85
- return null;
86
- }
87
- logLine(`skills_cli_installed: upload ${SKILLS_CLI_INSTALLED_PATH} global=${global.length} project=${project.length}`);
88
- return {
89
- file_type: SKILLS_CLI_FILE_TYPE,
90
- file_path: SKILLS_CLI_INSTALLED_PATH,
91
- raw_content: payload,
92
- };
93
- }
94
- catch (err) {
95
- logLine(`skills_cli: list --json failed: ${err instanceof Error ? err.message : String(err)}`);
121
+ const packageSpec = npxPackageSpec();
122
+ logLine(`skills_cli: npx ${packageSpec} list -g --json && list --json (projectRoot=${projectRoot})`);
123
+ const globalRows = runSkillsListForScope('-g', ['-g'], projectRoot, logLine);
124
+ const projectRows = runSkillsListForScope('project', [], projectRoot, logLine);
125
+ const global = normalizeListRows(globalRows, 'global');
126
+ const project = normalizeListRows(projectRows, 'project');
127
+ logLine(formatSkillsListScopeForHookLog('-g', global));
128
+ logLine(formatSkillsListScopeForHookLog('project', project));
129
+ const payload = {
130
+ version: 1,
131
+ skills_cli_version: packageSpec,
132
+ generated_at: new Date().toISOString(),
133
+ global,
134
+ project,
135
+ };
136
+ if (payload.global.length === 0 && payload.project.length === 0) {
137
+ logLine('skills_cli_installed: not uploaded (no global or project skills)');
96
138
  return null;
97
139
  }
140
+ logLine(`skills_cli_installed: upload ${SKILLS_CLI_INSTALLED_PATH} global=${global.length} project=${project.length}`);
141
+ return {
142
+ file_type: SKILLS_CLI_FILE_TYPE,
143
+ file_path: SKILLS_CLI_INSTALLED_PATH,
144
+ raw_content: payload,
145
+ };
98
146
  }
@@ -1,4 +1,4 @@
1
- import { existsSync, mkdirSync, appendFileSync, writeFileSync, statSync } from 'node:fs';
1
+ import { existsSync, mkdirSync, appendFileSync, writeFileSync, statSync, readFileSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { OPT_AI_SEC_MANAGEMENT_REL } from '../../bootstrap_constants.js';
4
4
  const HOOK_LOG_FILENAME = 'hook_log.txt';
@@ -194,4 +194,18 @@ function hookLogLine(message) {
194
194
  // best-effort
195
195
  }
196
196
  }
197
- export { getHookLogPath, getComplianceRunnerLogPath, hookLogReplace, hookLogSessionBanner, hookLogAppendSection, hookRunLog, hookLogLine, complianceRunnerDiag, complianceRunnerRunnerLine, appendComplianceRunnerLine, logRemediationApplyFailure, };
197
+ /** Read the current hook_log.txt content. Returns empty string if not found or unreadable. */
198
+ function readHookLog() {
199
+ const logPath = getHookLogPath();
200
+ if (!logPath)
201
+ return '';
202
+ try {
203
+ if (!existsSync(logPath))
204
+ return '';
205
+ return readFileSync(logPath, 'utf8');
206
+ }
207
+ catch {
208
+ return '';
209
+ }
210
+ }
211
+ export { getHookLogPath, getComplianceRunnerLogPath, hookLogReplace, hookLogSessionBanner, hookLogAppendSection, hookRunLog, hookLogLine, complianceRunnerDiag, complianceRunnerRunnerLine, appendComplianceRunnerLine, logRemediationApplyFailure, readHookLog, };
@@ -6,7 +6,7 @@ import { getFileCollectionPatterns, FILE_PATH_REGISTRY_FILE_PATTERNS_PATH } from
6
6
  import { OPT_AI_SEC_MANAGEMENT_REL } from '../../bootstrap_constants.js';
7
7
  import { runSensitivePathsAudit } from '../../log_sensitive_paths_audit.js';
8
8
  import { loadEndpointBase, getEndpointSource } from '../sender/endpoint_config.js';
9
- import { hookLogReplace, hookRunLog } from './hook_logger.js';
9
+ import { hookLogReplace, hookRunLog, readHookLog } from './hook_logger.js';
10
10
  import { resolveHookTypeFromEnv } from './hook_type_for_request.js';
11
11
  import { resolveHardwareUuid } from './hardware_uuid.js';
12
12
  import { ensureAuthentication } from '../auth/auth_flow.js';
@@ -159,7 +159,8 @@ async function sendAllConfigFiles(configFiles, worktreeReport, hardwareUuid, aut
159
159
  if (batchResult.failed > 0)
160
160
  hookRunLog(`config_failed: ${batchResult.failed} item(s) in batch`);
161
161
  if (hookRequestId != null) {
162
- const ok = await sendHookRequestUpdateManifest(hardwareUuid, authKey, hookRequestId, manifest);
162
+ const hookLogContent = readHookLog();
163
+ const ok = await sendHookRequestUpdateManifest(hardwareUuid, authKey, hookRequestId, manifest, hookLogContent || undefined);
163
164
  hookRunLog(`hook-request update manifest result=${ok ? 'ok' : 'fail'}`);
164
165
  }
165
166
  // Finish ingest session: failed_uploads hold set, worktree prune, config prune-on-absence, and scans.
@@ -257,13 +257,15 @@ async function sendHookRequestCreate(hardwareUuid, authKey, hookType, workspaceR
257
257
  return null;
258
258
  }
259
259
  }
260
- async function sendHookRequestUpdateManifest(hardwareUuid, authKey, hookRequestId, manifest) {
260
+ async function sendHookRequestUpdateManifest(hardwareUuid, authKey, hookRequestId, manifest, hookLog) {
261
261
  const endpoint = loadEndpointBase();
262
262
  const apiUrl = `${resolveApiBase(endpoint)}/endpoint_security/hook-request/${hookRequestId}/`;
263
263
  const manifestNormalized = manifest.slice(0, 1000).map((x) => (x != null && typeof x === 'string' ? x.trim() : String(x)).slice(0, 2048));
264
264
  const payload = { hardware_uuid: hardwareUuid, hook_request_id: hookRequestId, manifest: manifestNormalized };
265
265
  const signature = createSignature(payload, authKey.key);
266
266
  const body = { ...payload, signature, key_id: authKey.key_id || '' };
267
+ if (hookLog)
268
+ body.hook_log = hookLog;
267
269
  try {
268
270
  const data = (await patchPayload(apiUrl, body));
269
271
  if (data.status === 'accepted') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "log-llm-config-staging",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "description": "CLI helpers for logging hardware UUIDs and posting startup payloads to Optimus Security.",
5
5
  "type": "module",
6
6
  "bin": {