@lark-codem/codem-cli 0.1.281-dev.100104602537.0.gd02b8a3 → 0.1.281-dev.100104631660.0.gd9b8134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/codem.js +30 -107
- package/bin/tt-env.js +1 -1
- package/bin/worker-runtime-telemetry.js +1 -2
- package/package.json +9 -12
- package/profile/credential-errors.js +2 -1
- package/profile/profile-snapshot.js +1 -2
- package/profile/runtime-logs.js +18 -184
- package/auth/index.d.ts +0 -19
- package/auth/index.js +0 -5911
- package/bin/codem-startup-logs.js +0 -127
- package/bin/slardar-pc-transport.js +0 -42
- package/profile/realtime-log-id.js +0 -9
- package/profile/sandbox-observability.js +0 -17
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Image bootstrap's dedicated input. Never pipe business stdout/stderr here.
|
|
3
|
-
import os from "node:os";
|
|
4
|
-
import { randomUUID } from "node:crypto";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
import path from "node:path";
|
|
7
|
-
import { sendSlardarPcBatch } from "./slardar-pc-transport.js";
|
|
8
|
-
|
|
9
|
-
const id = value => typeof value === "string" && /^[a-z\d._:-]{1,128}$/iu.test(value) ? value : undefined;
|
|
10
|
-
const FIELDS = ["resource_id", "resource_env", "operation_id", "stage", "outcome", "startup_id", "controller_id", "logical_sandbox_id", "sandbox_id", "dispatch_id", "request_id", "tenant_id", "tenant_key"];
|
|
11
|
-
const NUMBERS = ["exit_code", "errno", "http_status", "duration_ms"];
|
|
12
|
-
|
|
13
|
-
export function createStartupLogger({ component, startupId, identity = {}, env = process.env, version = "unknown", send = sendSlardarPcBatch } = {}) {
|
|
14
|
-
const processId = randomUUID();
|
|
15
|
-
const startup = id(startupId ?? env.CODEM_STARTUP_ID) ?? processId;
|
|
16
|
-
const source = id(component ?? env.CODEM_LOG_COMPONENT) ?? "sandbox-bootstrap";
|
|
17
|
-
const fields = {
|
|
18
|
-
startup_id: startup,
|
|
19
|
-
...(env.CODEM_SANDBOX_VERSION === "resource" ? { resource_id: id(env.CODEM_RESOURCE_ID), resource_env: id(env.CODEM_RESOURCE_ENV), operation_id: id(env.CODEM_RESOURCE_OPERATION_ID) } : {}),
|
|
20
|
-
controller_id: id(identity.controllerId ?? env.CODEM_SANDBOX_CONTROLLER_ID),
|
|
21
|
-
logical_sandbox_id: id(identity.logicalSandboxId ?? env.CODEM_SANDBOX_LOGICAL_SANDBOX_ID),
|
|
22
|
-
sandbox_id: id(identity.sandboxId ?? env.CODEM_SANDBOX_DEVICE_ID),
|
|
23
|
-
dispatch_id: id(identity.dispatchId ?? env.CODEM_SANDBOX_DISPATCH_ID),
|
|
24
|
-
tenant_id: id(identity.tenantId ?? env.CODEM_SANDBOX_TENANT_ID),
|
|
25
|
-
tenant_key: id(identity.tenantKey ?? env.CODEM_SANDBOX_TENANT_KEY)
|
|
26
|
-
};
|
|
27
|
-
const header = {
|
|
28
|
-
aid: "1880", device_id: id(identity.deviceId ?? env.CODEM_SANDBOX_DEVICE_ID) ?? processId, user_id: "0",
|
|
29
|
-
parfait_version: "1.0.0.0", channel: "unknown", os: process.platform === "win32" ? "Windows" : process.platform === "darwin" ? "Mac OS X" : process.platform, os_version: os.release()
|
|
30
|
-
};
|
|
31
|
-
const queue = [];
|
|
32
|
-
let closed = env.CODEM_SANDBOX_LOG_UPLOAD_ENABLED !== "1" || /^(1|true|yes)$/iu.test(env.CODEM_TELEMETRY_DISABLED ?? "");
|
|
33
|
-
let inflight;
|
|
34
|
-
let dropped = 0;
|
|
35
|
-
let uploadFailures = 0;
|
|
36
|
-
let activeController;
|
|
37
|
-
const timer = setInterval(() => { void flush(); }, 10_000);
|
|
38
|
-
timer.unref();
|
|
39
|
-
|
|
40
|
-
function log(level, message, context = {}, timestamp = Date.now()) {
|
|
41
|
-
try {
|
|
42
|
-
if (closed || !["info", "warn", "error", "fatal"].includes(level)) return false;
|
|
43
|
-
const safe = { ...fields };
|
|
44
|
-
for (const key of FIELDS) if (id(context[key])) safe[key] = context[key];
|
|
45
|
-
for (const key of NUMBERS) if (Number.isFinite(context[key])) safe[key] = context[key];
|
|
46
|
-
const event = message === "sandbox bootstrap" ? "sandbox.bootstrap" : undefined;
|
|
47
|
-
if (!event) return false;
|
|
48
|
-
const entry = { level, event, fields: safe,
|
|
49
|
-
timestamp: Number.isSafeInteger(timestamp) && timestamp >= 0 ? timestamp : Date.now(), observationId: randomUUID() };
|
|
50
|
-
if (queue.length >= 256) { queue.shift(); dropped++; }
|
|
51
|
-
queue.push(entry);
|
|
52
|
-
return true;
|
|
53
|
-
} catch { dropped++; return false; }
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function flush({ timeoutMs = 3000 } = {}) {
|
|
57
|
-
if (inflight) return inflight;
|
|
58
|
-
if (closed || queue.length === 0) return Promise.resolve();
|
|
59
|
-
inflight = drain(timeoutMs).finally(() => { inflight = undefined; });
|
|
60
|
-
return inflight;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async function drain(timeoutMs) {
|
|
64
|
-
const controller = new AbortController();
|
|
65
|
-
activeController = controller;
|
|
66
|
-
let timeout;
|
|
67
|
-
const expired = new Promise((_, reject) => {
|
|
68
|
-
// stdin (or the one EOF deadline below) owns this helper's lifetime.
|
|
69
|
-
// A previous background upload must not extend the short EOF budget.
|
|
70
|
-
timeout = setTimeout(() => { controller.abort(); reject(new Error("upload timeout")); }, timeoutMs);
|
|
71
|
-
timeout.unref();
|
|
72
|
-
});
|
|
73
|
-
try {
|
|
74
|
-
while (!closed && queue.length && !controller.signal.aborted) {
|
|
75
|
-
const batch = queue.splice(0, 8);
|
|
76
|
-
const data = batch.map(entry => ({
|
|
77
|
-
app_version: version, app_minor_version: "", build_id: "", process_type: "codem-sandbox-bootstrap", session_id: "",
|
|
78
|
-
timestamp: entry.timestamp, log_type: "log", log_level: entry.level === "fatal" ? "error" : entry.level,
|
|
79
|
-
category: { reporter: "codem_bootstrap", runtime_role: "sandbox", component: source, event: "application.log", severity: entry.level,
|
|
80
|
-
...(fields.resource_env ? { resource_env: fields.resource_env } : {}),
|
|
81
|
-
...(id(env.CODEM_SANDBOX_TYPE) ? { sandbox_type: env.CODEM_SANDBOX_TYPE } : {}),
|
|
82
|
-
...(fields.tenant_id ? { tenant_id: fields.tenant_id } : {}) },
|
|
83
|
-
log_content: `${entry.event}\n${JSON.stringify({ schema_version: 1, timestamp: entry.timestamp,
|
|
84
|
-
process_instance_id: processId, fields: { ...entry.fields, observation_id: entry.observationId } })}`
|
|
85
|
-
}));
|
|
86
|
-
try { await Promise.race([send({ header, data }, { signal: controller.signal }), expired]); }
|
|
87
|
-
catch { uploadFailures++; dropped += batch.length; break; }
|
|
88
|
-
}
|
|
89
|
-
} catch { uploadFailures++; }
|
|
90
|
-
finally { clearTimeout(timeout); activeController = undefined; }
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return { log, flush, get pendingCount() { return queue.length; }, get droppedCount() { return dropped; },
|
|
94
|
-
get uploadFailureCount() { return uploadFailures; },
|
|
95
|
-
close() { closed = true; clearInterval(timer); activeController?.abort(); dropped += queue.length; queue.length = 0; } };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export async function runStartupLogInput({ input = process.stdin, ...options } = {}) {
|
|
99
|
-
const logger = createStartupLogger(options);
|
|
100
|
-
let pending = "";
|
|
101
|
-
// A dedicated pipe with bounded records. Oversized/malformed input is dropped;
|
|
102
|
-
// it cannot cause an unbounded readline allocation or affect the caller.
|
|
103
|
-
input.setEncoding("utf8");
|
|
104
|
-
input.on("data", chunk => {
|
|
105
|
-
pending += chunk;
|
|
106
|
-
let end;
|
|
107
|
-
while ((end = pending.indexOf("\n")) >= 0) {
|
|
108
|
-
const line = pending.slice(0, end);
|
|
109
|
-
pending = pending.slice(end + 1);
|
|
110
|
-
if (line.length > 8192) continue;
|
|
111
|
-
try {
|
|
112
|
-
const row = JSON.parse(line);
|
|
113
|
-
if (row && typeof row.message === "string") logger.log(row.level, row.message, row.fields, row.timestamp);
|
|
114
|
-
} catch { /* The pipe is optional diagnostic input. */ }
|
|
115
|
-
}
|
|
116
|
-
if (pending.length > 8192) pending = "";
|
|
117
|
-
});
|
|
118
|
-
await new Promise(resolve => { input.once("end", resolve); input.once("error", resolve); });
|
|
119
|
-
let deadline;
|
|
120
|
-
await Promise.race([logger.flush({ timeoutMs: 200 }), new Promise(resolve => { deadline = setTimeout(resolve, 200); })]);
|
|
121
|
-
clearTimeout(deadline);
|
|
122
|
-
logger.close();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
126
|
-
runStartupLogInput().catch(() => {});
|
|
127
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import https from "node:https";
|
|
2
|
-
import { gzip } from "node:zlib";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
|
-
|
|
5
|
-
// PC Custom Log / Custom Event wire format, verified against Parfait 1.7.0.0's
|
|
6
|
-
// public IUploader callback. This is not the Slardar Web/BID protocol. Keep the
|
|
7
|
-
// SDK capture fixture and the platform readback procedure with this adapter.
|
|
8
|
-
export const SLARDAR_PC_AID = "1880";
|
|
9
|
-
// Use the native envelope's four-part version convention in this legacy field.
|
|
10
|
-
// This is our JS adapter version; reporter=codem_js distinguishes it
|
|
11
|
-
// from the native Parfait SDK. Do not advertise a native SDK we do not run.
|
|
12
|
-
export const SLARDAR_PC_REPORTER_VERSION = "1.0.0.0";
|
|
13
|
-
const ENDPOINT = `https://pc-mon.zijieapi.com/monitor_pc/collect/api/pc_log?aid=${SLARDAR_PC_AID}`;
|
|
14
|
-
const compress = promisify(gzip);
|
|
15
|
-
|
|
16
|
-
export async function sendSlardarPcBatch(payload, { signal, request = https.request } = {}) {
|
|
17
|
-
signal?.throwIfAborted();
|
|
18
|
-
const body = await compress(Buffer.from(JSON.stringify(payload)));
|
|
19
|
-
signal?.throwIfAborted();
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
const req = request(ENDPOINT, {
|
|
22
|
-
method: "POST",
|
|
23
|
-
signal,
|
|
24
|
-
headers: {
|
|
25
|
-
"content-type": "application/json",
|
|
26
|
-
"content-encoding": "gzip",
|
|
27
|
-
"content-length": body.length
|
|
28
|
-
}
|
|
29
|
-
}, (res) => {
|
|
30
|
-
// The collector returns an empty 204. Never buffer arbitrary responses.
|
|
31
|
-
res.resume();
|
|
32
|
-
res.once("error", reject);
|
|
33
|
-
res.once("end", () => {
|
|
34
|
-
if (res.statusCode >= 200 && res.statusCode < 300) resolve();
|
|
35
|
-
else reject(new Error(`Slardar PC HTTP ${res.statusCode}`));
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
req.on("socket", (socket) => socket.unref?.());
|
|
39
|
-
req.once("error", reject);
|
|
40
|
-
req.end(body);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// Base bounds the JSON encoding of RPC IDs at 1024 bytes. Printable route
|
|
2
|
-
// keys include Go QueryEscape's percent escapes and plus sign. Only record
|
|
3
|
-
// identifier characters; never truncate two different IDs to the same value.
|
|
4
|
-
export function realtimeLogID(value) {
|
|
5
|
-
if (Number.isSafeInteger(value)) return value;
|
|
6
|
-
if (typeof value === "string" && value.length <= 1022 &&
|
|
7
|
-
/^[\p{L}\p{N}_.:%~+-]+$/u.test(value) && Buffer.byteLength(value) <= 1022) return value;
|
|
8
|
-
return undefined;
|
|
9
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// Server-resolved sandbox policy and correlation. These values never select an
|
|
2
|
-
// upload destination, authenticate a caller, or alter personal-device logging.
|
|
3
|
-
export function sandboxObservationContext(env = process.env, role) {
|
|
4
|
-
if (env.CODEM_SANDBOX_VERSION !== "resource") return {};
|
|
5
|
-
const fields = {};
|
|
6
|
-
for (const [name, key] of [["CODEM_RESOURCE_ID", "resource_id"], ["CODEM_RESOURCE_ENV", "resource_env"], ["CODEM_RESOURCE_OPERATION_ID", "operation_id"]]) {
|
|
7
|
-
const value = env[name];
|
|
8
|
-
if (typeof value === "string" && /^[A-Za-z0-9_.:-]{1,160}$/u.test(value)) fields[key] = value;
|
|
9
|
-
}
|
|
10
|
-
return fields;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function sandboxLogUploadAllowed(env = process.env, role) {
|
|
14
|
-
const sandbox = role === "sandbox" || env.CODEM_SANDBOX_VERSION === "resource" || env.CODEM_SESSION_SOURCE === "sandbox";
|
|
15
|
-
const policy = env.CODEM_SANDBOX_LOG_UPLOAD_ENABLED;
|
|
16
|
-
return !sandbox || policy === undefined || policy === "1";
|
|
17
|
-
}
|