larkin 0.2.70
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/CONTRIBUTING.md +48 -0
- package/LICENSE +201 -0
- package/README.md +122 -0
- package/SECURITY.md +14 -0
- package/artifacts/release/THIRD_PARTY_NOTICES.txt +5488 -0
- package/dist/agent/agent-cli-capabilities.cjs +43 -0
- package/dist/agent/agent-cli-capabilities.mjs +35 -0
- package/dist/agent/agent-state-store.cjs +961 -0
- package/dist/agent/agent-state-store.mjs +923 -0
- package/dist/agent/agent-transport.cjs +201 -0
- package/dist/agent/agent-transport.mjs +194 -0
- package/dist/agent/config-cli-contract.cjs +19 -0
- package/dist/agent/config-cli-contract.mjs +16 -0
- package/dist/agent/context-prompt.cjs +159 -0
- package/dist/agent/context-prompt.mjs +155 -0
- package/dist/agent/freshness-gate.mjs +24 -0
- package/dist/agent/history-projection.cjs +72 -0
- package/dist/agent/history-projection.mjs +65 -0
- package/dist/agent/host-reminder-orchestrator.cjs +289 -0
- package/dist/agent/host-reminder-orchestrator.mjs +252 -0
- package/dist/agent/inbox-projection.cjs +92 -0
- package/dist/agent/inbox-projection.mjs +85 -0
- package/dist/agent/interaction-state-machine.cjs +710 -0
- package/dist/agent/interaction-state-machine.mjs +705 -0
- package/dist/agent/reminder-routes.cjs +192 -0
- package/dist/agent/reminder-routes.mjs +156 -0
- package/dist/agent/reminder-store.cjs +220 -0
- package/dist/agent/reminder-store.mjs +205 -0
- package/dist/agent/transport-business-context.cjs +486 -0
- package/dist/agent/transport-business-context.mjs +446 -0
- package/dist/agent/transport-shell.cjs +45 -0
- package/dist/agent/transport-shell.mjs +39 -0
- package/dist/app/agent-cli.mjs +596 -0
- package/dist/app/agent-config.mjs +633 -0
- package/dist/app/agent-lark-cli-workspace.cjs +95 -0
- package/dist/app/agent-lark-cli-workspace.mjs +85 -0
- package/dist/app/agent-readiness.cjs +42 -0
- package/dist/app/agent-readiness.mjs +38 -0
- package/dist/app/binary-entry.mjs +86 -0
- package/dist/app/cli.mjs +176 -0
- package/dist/app/dashboard.mjs +205 -0
- package/dist/app/internal-command.cjs +57 -0
- package/dist/app/internal-command.mjs +48 -0
- package/dist/app/lark-channel-secret.mjs +22 -0
- package/dist/app/lark-cli.mjs +887 -0
- package/dist/app/lark.mjs +45 -0
- package/dist/app/local-control.mjs +728 -0
- package/dist/app/official-lark-cli.cjs +138 -0
- package/dist/app/official-lark-cli.mjs +128 -0
- package/dist/app/pi-auth-cli.mjs +67 -0
- package/dist/app/run.mjs +364 -0
- package/dist/app/runtime-agent-config.mjs +416 -0
- package/dist/app/runtime-model-directory.mjs +78 -0
- package/dist/app/runtime-process.mjs +113 -0
- package/dist/app/session-cli.mjs +83 -0
- package/dist/app/setup-dashboard.mjs +26 -0
- package/dist/app/setup.mjs +166 -0
- package/dist/app/telemetry.mjs +83 -0
- package/dist/dashboard/dashboard-assets.mjs +21 -0
- package/dist/dashboard/dashboard-avatar.mjs +128 -0
- package/dist/dashboard/dashboard-config-controller.mjs +525 -0
- package/dist/dashboard/dashboard-lifecycle.cjs +97 -0
- package/dist/dashboard/dashboard-lifecycle.mjs +88 -0
- package/dist/dashboard/dashboard-template.mjs +29 -0
- package/dist/dashboard/dashboard-view-model.mjs +556 -0
- package/dist/dashboard/dashboard-workspace.mjs +104 -0
- package/dist/dashboard/web/assets/dashboard.css +3 -0
- package/dist/dashboard/web/assets/dashboard.js +49 -0
- package/dist/feishu/channel-transport.cjs +151 -0
- package/dist/feishu/channel-transport.mjs +148 -0
- package/dist/feishu/document-comment.cjs +120 -0
- package/dist/feishu/document-comment.mjs +109 -0
- package/dist/feishu/host-business-state.cjs +477 -0
- package/dist/feishu/host-business-state.mjs +462 -0
- package/dist/feishu/host-channel-business.cjs +161 -0
- package/dist/feishu/host-channel-business.mjs +157 -0
- package/dist/feishu/host-processing-eye.cjs +185 -0
- package/dist/feishu/host-processing-eye.mjs +181 -0
- package/dist/feishu/host-shell.cjs +1425 -0
- package/dist/feishu/host-shell.mjs +1385 -0
- package/dist/feishu/im-freshness-adapter.mjs +89 -0
- package/dist/feishu/interaction-orchestrator.cjs +279 -0
- package/dist/feishu/interaction-orchestrator.mjs +242 -0
- package/dist/feishu/lark-passthrough.mjs +70 -0
- package/dist/feishu/message-policy.cjs +111 -0
- package/dist/feishu/message-policy.mjs +98 -0
- package/dist/feishu/outbound-transport.cjs +221 -0
- package/dist/feishu/outbound-transport.mjs +215 -0
- package/dist/platform/build-info.cjs +25 -0
- package/dist/platform/build-info.mjs +18 -0
- package/dist/platform/callback-capability.cjs +301 -0
- package/dist/platform/callback-capability.mjs +252 -0
- package/dist/platform/check-bun-version.cjs +12 -0
- package/dist/platform/config.cjs +751 -0
- package/dist/platform/config.mjs +694 -0
- package/dist/platform/process-inspect.cjs +89 -0
- package/dist/platform/process-state.cjs +237 -0
- package/dist/platform/process-state.mjs +223 -0
- package/dist/platform/release-artifacts.mjs +61 -0
- package/dist/platform/root-layout.cjs +111 -0
- package/dist/platform/root-layout.mjs +72 -0
- package/dist/platform/telemetry-config.cjs +64 -0
- package/dist/platform/telemetry-config.mjs +56 -0
- package/dist/platform/telemetry-spool.cjs +661 -0
- package/dist/platform/telemetry-spool.mjs +653 -0
- package/dist/platform/telemetry-tracing.cjs +602 -0
- package/dist/platform/telemetry-tracing.mjs +596 -0
- package/dist/platform/telemetry-uploader.cjs +150 -0
- package/dist/platform/telemetry-uploader.mjs +146 -0
- package/dist/platform/workspace-service.cjs +469 -0
- package/dist/platform/workspace-service.mjs +430 -0
- package/dist/runtime/builtin-pi-assets.mjs +33 -0
- package/dist/runtime/claude-model-catalog.cjs +102 -0
- package/dist/runtime/claude-model-catalog.mjs +99 -0
- package/dist/runtime/codex-model-catalog.cjs +108 -0
- package/dist/runtime/codex-model-catalog.mjs +105 -0
- package/dist/runtime/pi-bash-timeout-extension.mjs +60 -0
- package/dist/runtime/pi-bash-timeout-injection.mjs +60 -0
- package/dist/runtime/pi-bash-timeout.bundle.js +43 -0
- package/dist/runtime/pi-model-catalog.cjs +116 -0
- package/dist/runtime/pi-model-catalog.mjs +75 -0
- package/dist/runtime/pi-official-auth.mjs +480 -0
- package/dist/runtime/pi-provider-config.cjs +252 -0
- package/dist/runtime/pi-provider-config.mjs +239 -0
- package/dist/runtime/pi-rpc-client.cjs +255 -0
- package/dist/runtime/pi-rpc-client.mjs +251 -0
- package/dist/runtime/pi-subagent-injection.mjs +129 -0
- package/dist/runtime/pi-subagents.bundle.js +8990 -0
- package/dist/runtime/runtime-adapters.mjs +1019 -0
- package/dist/runtime/runtime-contracts.cjs +2 -0
- package/dist/runtime/runtime-contracts.mjs +1 -0
- package/dist/runtime/runtime-host.cjs +930 -0
- package/dist/runtime/runtime-host.mjs +922 -0
- package/dist/runtime/runtime-model-catalog.cjs +43 -0
- package/dist/runtime/runtime-model-catalog.mjs +40 -0
- package/dist/runtime/runtime-readiness.cjs +177 -0
- package/dist/runtime/runtime-readiness.mjs +133 -0
- package/dist/setup/bot-register.mjs +655 -0
- package/dist/setup/grant-scopes.mjs +133 -0
- package/dist/setup/run-credential-preflight.mjs +89 -0
- package/dist/setup/setup-agent-choice.mjs +172 -0
- package/dist/setup/setup-bind.mjs +337 -0
- package/dist/setup/setup-binding.mjs +52 -0
- package/package.json +112 -0
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TelemetrySpool = void 0;
|
|
7
|
+
exports.validateOtlpPayload = validateOtlpPayload;
|
|
8
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
9
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
11
|
+
const node_zlib_1 = __importDefault(require("node:zlib"));
|
|
12
|
+
const process_state_js_1 = require("./process-state.cjs");
|
|
13
|
+
const READY = /^(?:span-[0-9a-f-]+|import-[0-9a-f]{64})\.json$/;
|
|
14
|
+
const REMNANT = /^\.(?:corrupt|write|ack|delete|stale-lock|purge)-[0-9A-Za-z-]+(?:\.(?:json|tmp|dir))?$/;
|
|
15
|
+
const HEX_TRACE = /^[0-9a-f]{32}$/;
|
|
16
|
+
const HEX_SPAN = /^[0-9a-f]{16}$/;
|
|
17
|
+
const SPAN_NAMES = new Set(["larkin.message.process", "feishu.receive", "runtime.deliver", "agent.turn", "model.activity", "tool.execute", "inbox.consume", "feishu.send",
|
|
18
|
+
"document.comment.receive", "document.comment.gate", "document.comment.pending", "document.comment.replay", "document.comment.resolve", "document.comment.inbox", "document.comment.reply",
|
|
19
|
+
"pi.prompt.wait", "pi.compaction", "pi.rpc.submit", "pi.rpc.lifecycle", "pi.output.wait", "pi.generation", "pi.tool.wait", "pi.rpc.settle"]);
|
|
20
|
+
const ATTRIBUTE_KEYS = new Set(["service.name", "service.version", "service.instance.id", "larkin.agent.id_hash", "messaging.message.id_hash", "larkin.message.relation", "larkin.message.source",
|
|
21
|
+
"larkin.observation.boundary", "larkin.activity.type", "larkin.filter.reason", "larkin.runtime.id", "larkin.runtime.distribution", "larkin.operation.outcome",
|
|
22
|
+
"larkin.pi.preflight.outcome", "larkin.pi.preflight.progress"]);
|
|
23
|
+
const SENSITIVE = /(?:bearer\s|(?:api[_-]?key|authorization|password|token|secret|cookie)\s*[=:]|(?:sk|ghp|github_pat)-?[a-z0-9_-]{8})/i;
|
|
24
|
+
const ABSOLUTE_PATH = /(?:^|[\s"'=])(?:\/(?!\/)[^\s"']+|[A-Za-z]:[\\/][^\s"']+)/;
|
|
25
|
+
const emptyDiagnostics = () => ({ droppedFiles: 0, cleanedRemnantFiles: 0, droppedSpans: 0, lastUploadAt: null, lastErrorCategory: null });
|
|
26
|
+
const invalidPayload = () => { throw new Error("invalid telemetry payload"); };
|
|
27
|
+
function validateValue(value, depth = 0) {
|
|
28
|
+
if (depth > 6 || !value || typeof value !== "object" || Array.isArray(value))
|
|
29
|
+
invalidPayload();
|
|
30
|
+
const object = value;
|
|
31
|
+
const keys = Object.keys(object);
|
|
32
|
+
if (keys.length !== 1 || !["stringValue", "boolValue", "intValue", "doubleValue", "arrayValue"].includes(keys[0]))
|
|
33
|
+
invalidPayload();
|
|
34
|
+
if ("stringValue" in object) {
|
|
35
|
+
const text = object.stringValue;
|
|
36
|
+
if (typeof text !== "string" || text.length > 128 || ABSOLUTE_PATH.test(text) || SENSITIVE.test(text))
|
|
37
|
+
invalidPayload();
|
|
38
|
+
}
|
|
39
|
+
else if ("boolValue" in object && typeof object.boolValue !== "boolean")
|
|
40
|
+
invalidPayload();
|
|
41
|
+
else if ("intValue" in object && !/^-?[0-9]{1,20}$/.test(String(object.intValue)))
|
|
42
|
+
invalidPayload();
|
|
43
|
+
else if ("doubleValue" in object && (typeof object.doubleValue !== "number" || !Number.isFinite(object.doubleValue)))
|
|
44
|
+
invalidPayload();
|
|
45
|
+
else if ("arrayValue" in object) {
|
|
46
|
+
const arrayValue = object.arrayValue;
|
|
47
|
+
if (!arrayValue || Object.keys(arrayValue).some((key) => key !== "values") || !Array.isArray(arrayValue.values) || arrayValue.values.length > 32)
|
|
48
|
+
invalidPayload();
|
|
49
|
+
for (const item of arrayValue.values)
|
|
50
|
+
validateValue(item, depth + 1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function validateAttributes(value) {
|
|
54
|
+
if (!Array.isArray(value) || value.length > 32)
|
|
55
|
+
invalidPayload();
|
|
56
|
+
for (const item of value) {
|
|
57
|
+
if (!item || typeof item !== "object" || Array.isArray(item))
|
|
58
|
+
invalidPayload();
|
|
59
|
+
const attribute = item;
|
|
60
|
+
if (Object.keys(attribute).some((key) => key !== "key" && key !== "value") || !ATTRIBUTE_KEYS.has(String(attribute.key)))
|
|
61
|
+
invalidPayload();
|
|
62
|
+
validateValue(attribute.value);
|
|
63
|
+
const stringValue = attribute.value.stringValue;
|
|
64
|
+
if (typeof stringValue !== "string")
|
|
65
|
+
invalidPayload();
|
|
66
|
+
{
|
|
67
|
+
const key = String(attribute.key);
|
|
68
|
+
const text = stringValue;
|
|
69
|
+
if (key === "service.name" && text !== "larkin")
|
|
70
|
+
invalidPayload();
|
|
71
|
+
if (key === "service.version" && !/^[A-Za-z0-9][A-Za-z0-9._+-]{0,63}$/.test(text))
|
|
72
|
+
invalidPayload();
|
|
73
|
+
if (key === "service.instance.id" && !/^[0-9a-f-]{16,64}$/i.test(text))
|
|
74
|
+
invalidPayload();
|
|
75
|
+
if (["larkin.agent.id_hash", "messaging.message.id_hash"].includes(key) && !/^[0-9a-f]{24}$/.test(text))
|
|
76
|
+
invalidPayload();
|
|
77
|
+
if (key === "larkin.message.relation" && text !== "fan_in")
|
|
78
|
+
invalidPayload();
|
|
79
|
+
if (key === "larkin.observation.boundary" && !["runtime_host", "runtime_event_interval", "agent_cli", "agent_transport", "pi_rpc", "comment_cli"].includes(text))
|
|
80
|
+
invalidPayload();
|
|
81
|
+
if (key === "larkin.activity.type" && !["thinking", "text", "tool", "internal"].includes(text))
|
|
82
|
+
invalidPayload();
|
|
83
|
+
if (key === "larkin.message.source" && !["im", "document_comment"].includes(text))
|
|
84
|
+
invalidPayload();
|
|
85
|
+
if (key === "larkin.filter.reason" && !["subscription_unverified", "self_or_missing_operator", "unsupported_file_type", "duplicate"].includes(text))
|
|
86
|
+
invalidPayload();
|
|
87
|
+
if (key === "larkin.runtime.id" && text !== "pi")
|
|
88
|
+
invalidPayload();
|
|
89
|
+
if (key === "larkin.runtime.distribution" && !["builtin", "external"].includes(text))
|
|
90
|
+
invalidPayload();
|
|
91
|
+
if (key === "larkin.operation.outcome" && !["success", "error"].includes(text))
|
|
92
|
+
invalidPayload();
|
|
93
|
+
if (key === "larkin.pi.preflight.outcome" && !["accepted", "timeout", "error"].includes(text))
|
|
94
|
+
invalidPayload();
|
|
95
|
+
if (key === "larkin.pi.preflight.progress" && !["compaction", "retry"].includes(text))
|
|
96
|
+
invalidPayload();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function validateOtlpPayload(payload) {
|
|
101
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload) || Object.keys(payload).some((key) => key !== "resourceSpans"))
|
|
102
|
+
invalidPayload();
|
|
103
|
+
const resourceSpans = payload.resourceSpans;
|
|
104
|
+
if (!Array.isArray(resourceSpans) || resourceSpans.length < 1 || resourceSpans.length > 1024)
|
|
105
|
+
invalidPayload();
|
|
106
|
+
for (const resourceSpanValue of resourceSpans) {
|
|
107
|
+
const resourceSpan = resourceSpanValue;
|
|
108
|
+
if (!resourceSpan || typeof resourceSpan !== "object" || Object.keys(resourceSpan).some((key) => key !== "resource" && key !== "scopeSpans"))
|
|
109
|
+
invalidPayload();
|
|
110
|
+
const resource = resourceSpan.resource;
|
|
111
|
+
if (!resource || Object.keys(resource).some((key) => key !== "attributes"))
|
|
112
|
+
invalidPayload();
|
|
113
|
+
validateAttributes(resource.attributes);
|
|
114
|
+
if (!Array.isArray(resourceSpan.scopeSpans) || resourceSpan.scopeSpans.length > 16)
|
|
115
|
+
invalidPayload();
|
|
116
|
+
for (const scopeSpanValue of resourceSpan.scopeSpans) {
|
|
117
|
+
const scopeSpan = scopeSpanValue;
|
|
118
|
+
if (!scopeSpan || Object.keys(scopeSpan).some((key) => key !== "scope" && key !== "spans"))
|
|
119
|
+
invalidPayload();
|
|
120
|
+
const scope = scopeSpan.scope;
|
|
121
|
+
if (!scope || Object.keys(scope).some((key) => key !== "name" && key !== "version") || typeof scope.name !== "string" || typeof scope.version !== "string")
|
|
122
|
+
invalidPayload();
|
|
123
|
+
if (!Array.isArray(scopeSpan.spans) || scopeSpan.spans.length > 4096)
|
|
124
|
+
invalidPayload();
|
|
125
|
+
for (const spanValue of scopeSpan.spans) {
|
|
126
|
+
const span = spanValue;
|
|
127
|
+
const allowed = new Set(["traceId", "spanId", "parentSpanId", "name", "kind", "startTimeUnixNano", "endTimeUnixNano", "attributes", "links", "status", "flags"]);
|
|
128
|
+
if (!span || Object.keys(span).some((key) => !allowed.has(key)) || !HEX_TRACE.test(String(span.traceId)) || !HEX_SPAN.test(String(span.spanId))
|
|
129
|
+
|| (span.parentSpanId !== undefined && !HEX_SPAN.test(String(span.parentSpanId))) || !SPAN_NAMES.has(String(span.name))
|
|
130
|
+
|| !Number.isInteger(span.kind) || Number(span.kind) < 1 || Number(span.kind) > 5
|
|
131
|
+
|| !/^[0-9]{1,24}$/.test(String(span.startTimeUnixNano)) || !/^[0-9]{1,24}$/.test(String(span.endTimeUnixNano))
|
|
132
|
+
|| BigInt(String(span.endTimeUnixNano)) < BigInt(String(span.startTimeUnixNano)))
|
|
133
|
+
invalidPayload();
|
|
134
|
+
validateAttributes(span.attributes);
|
|
135
|
+
if (!Array.isArray(span.links) || span.links.length > 8)
|
|
136
|
+
invalidPayload();
|
|
137
|
+
for (const linkValue of span.links) {
|
|
138
|
+
const link = linkValue;
|
|
139
|
+
if (!link || Object.keys(link).some((key) => !["traceId", "spanId", "traceState", "flags", "attributes"].includes(key))
|
|
140
|
+
|| !HEX_TRACE.test(String(link.traceId)) || !HEX_SPAN.test(String(link.spanId)))
|
|
141
|
+
invalidPayload();
|
|
142
|
+
validateAttributes(link.attributes);
|
|
143
|
+
}
|
|
144
|
+
const status = span.status;
|
|
145
|
+
if (!status || Object.keys(status).some((key) => key !== "code") || !Number.isInteger(status.code) || Number(status.code) < 0 || Number(status.code) > 2)
|
|
146
|
+
invalidPayload();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
class TelemetrySpool {
|
|
152
|
+
config;
|
|
153
|
+
constructor(config) {
|
|
154
|
+
this.config = config;
|
|
155
|
+
}
|
|
156
|
+
ensure() {
|
|
157
|
+
const parent = node_path_1.default.dirname(this.config.spoolDir);
|
|
158
|
+
node_fs_1.default.mkdirSync(parent, { recursive: true, mode: 0o700 });
|
|
159
|
+
try {
|
|
160
|
+
if (node_fs_1.default.lstatSync(this.config.spoolDir).isSymbolicLink())
|
|
161
|
+
throw new Error("invalid telemetry spool");
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (error.code !== "ENOENT")
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
node_fs_1.default.mkdirSync(this.config.spoolDir, { recursive: true, mode: 0o700 });
|
|
168
|
+
const stat = node_fs_1.default.lstatSync(this.config.spoolDir);
|
|
169
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
170
|
+
throw new Error("invalid telemetry spool");
|
|
171
|
+
node_fs_1.default.chmodSync(this.config.spoolDir, 0o700);
|
|
172
|
+
}
|
|
173
|
+
acquireLease() {
|
|
174
|
+
this.ensure();
|
|
175
|
+
const lease = node_path_1.default.join(this.config.spoolDir, ".queue.lock");
|
|
176
|
+
const token = node_crypto_1.default.randomUUID();
|
|
177
|
+
const currentStartToken = (0, process_state_js_1.inspectProcess)(process.pid).startToken;
|
|
178
|
+
const create = () => {
|
|
179
|
+
node_fs_1.default.mkdirSync(lease, { mode: 0o700 });
|
|
180
|
+
node_fs_1.default.writeFileSync(node_path_1.default.join(lease, "owner.json"), JSON.stringify({ version: 1, pid: process.pid, processStartToken: currentStartToken,
|
|
181
|
+
token, createdAt: Date.now() }), { mode: 0o600, flag: "wx" });
|
|
182
|
+
};
|
|
183
|
+
try {
|
|
184
|
+
create();
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (error.code !== "EEXIST")
|
|
188
|
+
throw error;
|
|
189
|
+
try {
|
|
190
|
+
const stat = node_fs_1.default.lstatSync(lease);
|
|
191
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
192
|
+
return null;
|
|
193
|
+
let owner = null;
|
|
194
|
+
try {
|
|
195
|
+
owner = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(lease, "owner.json"), "utf8"));
|
|
196
|
+
}
|
|
197
|
+
catch { /* reclaim only after timeout */ }
|
|
198
|
+
const inspected = owner && Number.isSafeInteger(Number(owner.pid)) && Number(owner.pid) > 0 ? (0, process_state_js_1.inspectProcess)(Number(owner.pid)) : { ok: false };
|
|
199
|
+
const hasStartToken = typeof owner?.processStartToken === "string" && owner.processStartToken.length > 0;
|
|
200
|
+
const sameProcess = hasStartToken && inspected.ok && inspected.startToken === owner?.processStartToken;
|
|
201
|
+
const identityMismatch = hasStartToken && inspected.ok && inspected.startToken !== owner?.processStartToken;
|
|
202
|
+
const age = Date.now() - (owner && Number.isFinite(Number(owner.createdAt)) ? Number(owner.createdAt) : stat.mtimeMs);
|
|
203
|
+
if (sameProcess || (!identityMismatch && !inspected.dead && age < 2 * 60_000))
|
|
204
|
+
return null;
|
|
205
|
+
const stale = node_path_1.default.join(this.config.spoolDir, `.stale-lock-${node_crypto_1.default.randomUUID()}`);
|
|
206
|
+
node_fs_1.default.renameSync(lease, stale);
|
|
207
|
+
const moved = node_fs_1.default.lstatSync(stale);
|
|
208
|
+
if (!moved.isDirectory() || moved.isSymbolicLink() || moved.dev !== stat.dev || moved.ino !== stat.ino)
|
|
209
|
+
return null;
|
|
210
|
+
node_fs_1.default.rmSync(stale, { recursive: true });
|
|
211
|
+
create();
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
let released = false;
|
|
218
|
+
return () => {
|
|
219
|
+
if (released)
|
|
220
|
+
return;
|
|
221
|
+
released = true;
|
|
222
|
+
try {
|
|
223
|
+
const ownerFile = node_path_1.default.join(lease, "owner.json");
|
|
224
|
+
const owner = JSON.parse(node_fs_1.default.readFileSync(ownerFile, "utf8"));
|
|
225
|
+
if (owner.token !== token)
|
|
226
|
+
return;
|
|
227
|
+
node_fs_1.default.unlinkSync(ownerFile);
|
|
228
|
+
node_fs_1.default.rmdirSync(lease);
|
|
229
|
+
}
|
|
230
|
+
catch { /* isolated */ }
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
readyFiles() {
|
|
234
|
+
this.ensure();
|
|
235
|
+
return node_fs_1.default.readdirSync(this.config.spoolDir).filter((name) => READY.test(name)).flatMap((name) => {
|
|
236
|
+
const file = node_path_1.default.join(this.config.spoolDir, name);
|
|
237
|
+
try {
|
|
238
|
+
const stat = node_fs_1.default.lstatSync(file);
|
|
239
|
+
return stat.isFile() && !stat.isSymbolicLink() ? [{ file, size: stat.size, mtimeMs: stat.mtimeMs, device: stat.dev, inode: stat.ino }] : [];
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
}).sort((a, b) => a.mtimeMs - b.mtimeMs || a.file.localeCompare(b.file));
|
|
245
|
+
}
|
|
246
|
+
remnantFiles() {
|
|
247
|
+
this.ensure();
|
|
248
|
+
return node_fs_1.default.readdirSync(this.config.spoolDir).filter((name) => REMNANT.test(name)).flatMap((name) => {
|
|
249
|
+
const file = node_path_1.default.join(this.config.spoolDir, name);
|
|
250
|
+
try {
|
|
251
|
+
const stat = node_fs_1.default.lstatSync(file);
|
|
252
|
+
const directory = stat.isDirectory() && !stat.isSymbolicLink();
|
|
253
|
+
return (stat.isFile() || directory) && !stat.isSymbolicLink()
|
|
254
|
+
? [{ file, size: directory ? this.directoryBytes(file, stat) : stat.size, mtimeMs: stat.mtimeMs,
|
|
255
|
+
device: stat.dev, inode: stat.ino, directory, remnant: true }] : [];
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
return [];
|
|
259
|
+
}
|
|
260
|
+
}).sort((a, b) => a.mtimeMs - b.mtimeMs || a.file.localeCompare(b.file));
|
|
261
|
+
}
|
|
262
|
+
directoryBytes(root, rootStat) {
|
|
263
|
+
const saturation = this.config.maxBytes + 1;
|
|
264
|
+
const seen = new Set();
|
|
265
|
+
let entries = 0;
|
|
266
|
+
const spoolRoot = node_fs_1.default.realpathSync(this.config.spoolDir);
|
|
267
|
+
const prefix = `${spoolRoot}${node_path_1.default.sep}`;
|
|
268
|
+
const visit = (directory, stat, depth) => {
|
|
269
|
+
const identity = `${stat.dev}:${stat.ino}`;
|
|
270
|
+
if (seen.has(identity))
|
|
271
|
+
return 0;
|
|
272
|
+
seen.add(identity);
|
|
273
|
+
if (depth > 8 || entries > 1024)
|
|
274
|
+
return saturation;
|
|
275
|
+
try {
|
|
276
|
+
const real = node_fs_1.default.realpathSync(directory);
|
|
277
|
+
if (real !== spoolRoot && !real.startsWith(prefix))
|
|
278
|
+
return saturation;
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return saturation;
|
|
282
|
+
}
|
|
283
|
+
let total = stat.size;
|
|
284
|
+
let names;
|
|
285
|
+
try {
|
|
286
|
+
names = node_fs_1.default.readdirSync(directory);
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return saturation;
|
|
290
|
+
}
|
|
291
|
+
for (const name of names) {
|
|
292
|
+
entries += 1;
|
|
293
|
+
if (entries > 1024)
|
|
294
|
+
return saturation;
|
|
295
|
+
const child = node_path_1.default.join(directory, name);
|
|
296
|
+
let childStat;
|
|
297
|
+
try {
|
|
298
|
+
childStat = node_fs_1.default.lstatSync(child);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (childStat.isSymbolicLink()) {
|
|
304
|
+
total += childStat.size;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (childStat.isFile())
|
|
308
|
+
total += childStat.size;
|
|
309
|
+
else if (childStat.isDirectory())
|
|
310
|
+
total += visit(child, childStat, depth + 1);
|
|
311
|
+
}
|
|
312
|
+
try {
|
|
313
|
+
const after = node_fs_1.default.lstatSync(directory);
|
|
314
|
+
if (after.isSymbolicLink() || after.dev !== stat.dev || after.ino !== stat.ino)
|
|
315
|
+
return saturation;
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
return saturation;
|
|
319
|
+
}
|
|
320
|
+
return total;
|
|
321
|
+
};
|
|
322
|
+
return visit(root, rootStat, 0);
|
|
323
|
+
}
|
|
324
|
+
atomicWrite(file, bytes) {
|
|
325
|
+
this.ensure();
|
|
326
|
+
const temporary = node_path_1.default.join(this.config.spoolDir, `.write-${process.pid}-${node_crypto_1.default.randomUUID()}.tmp`);
|
|
327
|
+
let descriptor;
|
|
328
|
+
try {
|
|
329
|
+
descriptor = node_fs_1.default.openSync(temporary, node_fs_1.default.constants.O_WRONLY | node_fs_1.default.constants.O_CREAT | node_fs_1.default.constants.O_EXCL | node_fs_1.default.constants.O_NOFOLLOW, 0o600);
|
|
330
|
+
node_fs_1.default.writeFileSync(descriptor, bytes);
|
|
331
|
+
node_fs_1.default.fsyncSync(descriptor);
|
|
332
|
+
node_fs_1.default.closeSync(descriptor);
|
|
333
|
+
descriptor = undefined;
|
|
334
|
+
node_fs_1.default.renameSync(temporary, file);
|
|
335
|
+
node_fs_1.default.chmodSync(file, 0o600);
|
|
336
|
+
const directory = node_fs_1.default.openSync(this.config.spoolDir, node_fs_1.default.constants.O_RDONLY);
|
|
337
|
+
try {
|
|
338
|
+
node_fs_1.default.fsyncSync(directory);
|
|
339
|
+
}
|
|
340
|
+
finally {
|
|
341
|
+
node_fs_1.default.closeSync(directory);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
if (descriptor !== undefined)
|
|
346
|
+
try {
|
|
347
|
+
node_fs_1.default.closeSync(descriptor);
|
|
348
|
+
}
|
|
349
|
+
catch { /* isolated */ }
|
|
350
|
+
try {
|
|
351
|
+
node_fs_1.default.unlinkSync(temporary);
|
|
352
|
+
}
|
|
353
|
+
catch { /* isolated */ }
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
removeVerified(entry) {
|
|
357
|
+
const quarantine = node_path_1.default.join(this.config.spoolDir, `.delete-${node_crypto_1.default.randomUUID()}.tmp`);
|
|
358
|
+
try {
|
|
359
|
+
node_fs_1.default.renameSync(entry.file, quarantine);
|
|
360
|
+
const stat = node_fs_1.default.lstatSync(quarantine);
|
|
361
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.dev !== entry.device || stat.ino !== entry.inode) {
|
|
362
|
+
try {
|
|
363
|
+
node_fs_1.default.renameSync(quarantine, entry.file);
|
|
364
|
+
}
|
|
365
|
+
catch { /* preserve replacement */ }
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
node_fs_1.default.unlinkSync(quarantine);
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
try {
|
|
373
|
+
node_fs_1.default.renameSync(quarantine, entry.file);
|
|
374
|
+
}
|
|
375
|
+
catch { /* isolated */ }
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
removeManaged(entry) {
|
|
380
|
+
if (!entry.directory)
|
|
381
|
+
return this.removeVerified(entry);
|
|
382
|
+
const quarantine = node_path_1.default.join(this.config.spoolDir, `.purge-${node_crypto_1.default.randomUUID()}.dir`);
|
|
383
|
+
try {
|
|
384
|
+
node_fs_1.default.renameSync(entry.file, quarantine);
|
|
385
|
+
const stat = node_fs_1.default.lstatSync(quarantine);
|
|
386
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || stat.dev !== entry.device || stat.ino !== entry.inode) {
|
|
387
|
+
try {
|
|
388
|
+
node_fs_1.default.renameSync(quarantine, entry.file);
|
|
389
|
+
}
|
|
390
|
+
catch { /* preserve */ }
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
node_fs_1.default.rmSync(quarantine, { recursive: true });
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
try {
|
|
398
|
+
node_fs_1.default.renameSync(quarantine, entry.file);
|
|
399
|
+
}
|
|
400
|
+
catch { /* isolated */ }
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
enqueue(payload) {
|
|
405
|
+
validateOtlpPayload(payload);
|
|
406
|
+
const file = node_path_1.default.join(this.config.spoolDir, `span-${node_crypto_1.default.randomUUID()}.json`);
|
|
407
|
+
this.atomicWrite(file, Buffer.from(JSON.stringify(payload)));
|
|
408
|
+
try {
|
|
409
|
+
this.prune();
|
|
410
|
+
}
|
|
411
|
+
catch { /* telemetry cannot alter business behavior */ }
|
|
412
|
+
return file;
|
|
413
|
+
}
|
|
414
|
+
list(limit = Number.MAX_SAFE_INTEGER, quarantineCorrupt = false) {
|
|
415
|
+
const output = [];
|
|
416
|
+
let dropped = 0;
|
|
417
|
+
for (const entry of this.readyFiles().slice(0, limit)) {
|
|
418
|
+
let descriptor;
|
|
419
|
+
let invalid = false;
|
|
420
|
+
try {
|
|
421
|
+
descriptor = node_fs_1.default.openSync(entry.file, node_fs_1.default.constants.O_RDONLY | node_fs_1.default.constants.O_NOFOLLOW);
|
|
422
|
+
const stat = node_fs_1.default.fstatSync(descriptor);
|
|
423
|
+
if (!stat.isFile() || stat.dev !== entry.device || stat.ino !== entry.inode)
|
|
424
|
+
throw new Error("telemetry spool changed");
|
|
425
|
+
const payload = JSON.parse(node_fs_1.default.readFileSync(descriptor, "utf8"));
|
|
426
|
+
validateOtlpPayload(payload);
|
|
427
|
+
output.push({ file: entry.file, payload, device: stat.dev, inode: stat.ino });
|
|
428
|
+
}
|
|
429
|
+
catch {
|
|
430
|
+
invalid = true;
|
|
431
|
+
}
|
|
432
|
+
finally {
|
|
433
|
+
if (descriptor !== undefined)
|
|
434
|
+
try {
|
|
435
|
+
node_fs_1.default.closeSync(descriptor);
|
|
436
|
+
}
|
|
437
|
+
catch { /* isolated */ }
|
|
438
|
+
}
|
|
439
|
+
if (invalid) {
|
|
440
|
+
if (!quarantineCorrupt)
|
|
441
|
+
throw new Error("invalid telemetry spool record");
|
|
442
|
+
const quarantine = node_path_1.default.join(this.config.spoolDir, `.corrupt-${node_crypto_1.default.randomUUID()}.json`);
|
|
443
|
+
try {
|
|
444
|
+
node_fs_1.default.renameSync(entry.file, quarantine);
|
|
445
|
+
const stat = node_fs_1.default.lstatSync(quarantine);
|
|
446
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.dev !== entry.device || stat.ino !== entry.inode)
|
|
447
|
+
throw new Error("record changed");
|
|
448
|
+
dropped += 1;
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
try {
|
|
452
|
+
node_fs_1.default.renameSync(quarantine, entry.file);
|
|
453
|
+
}
|
|
454
|
+
catch { /* preserve */ }
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (dropped)
|
|
459
|
+
try {
|
|
460
|
+
this.updateDiagnostics({ droppedFiles: this.diagnostics().droppedFiles + dropped, lastErrorCategory: "corrupt_spool" });
|
|
461
|
+
}
|
|
462
|
+
catch { /* isolated */ }
|
|
463
|
+
return output;
|
|
464
|
+
}
|
|
465
|
+
acknowledge(records) {
|
|
466
|
+
const root = `${node_path_1.default.resolve(this.config.spoolDir)}${node_path_1.default.sep}`;
|
|
467
|
+
const moved = [];
|
|
468
|
+
try {
|
|
469
|
+
for (const record of records) {
|
|
470
|
+
const resolved = node_path_1.default.resolve(record.file);
|
|
471
|
+
const quarantine = node_path_1.default.join(this.config.spoolDir, `.ack-${node_crypto_1.default.randomUUID()}.tmp`);
|
|
472
|
+
if (!resolved.startsWith(root) || !READY.test(node_path_1.default.basename(resolved)))
|
|
473
|
+
throw new Error("invalid record");
|
|
474
|
+
node_fs_1.default.renameSync(resolved, quarantine);
|
|
475
|
+
moved.push({ original: resolved, quarantine });
|
|
476
|
+
const stat = node_fs_1.default.lstatSync(quarantine);
|
|
477
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.dev !== record.device || stat.ino !== record.inode)
|
|
478
|
+
throw new Error("record changed");
|
|
479
|
+
}
|
|
480
|
+
for (const entry of moved)
|
|
481
|
+
node_fs_1.default.unlinkSync(entry.quarantine);
|
|
482
|
+
const directory = node_fs_1.default.openSync(this.config.spoolDir, node_fs_1.default.constants.O_RDONLY);
|
|
483
|
+
try {
|
|
484
|
+
node_fs_1.default.fsyncSync(directory);
|
|
485
|
+
}
|
|
486
|
+
finally {
|
|
487
|
+
node_fs_1.default.closeSync(directory);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
catch {
|
|
491
|
+
for (const entry of moved.reverse())
|
|
492
|
+
try {
|
|
493
|
+
node_fs_1.default.renameSync(entry.quarantine, entry.original);
|
|
494
|
+
}
|
|
495
|
+
catch { /* preserve without deleting */ }
|
|
496
|
+
throw new Error("telemetry acknowledgement failed");
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
diagnostics() {
|
|
500
|
+
try {
|
|
501
|
+
const parsed = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(this.config.spoolDir, "diagnostics.json"), "utf8"));
|
|
502
|
+
return { droppedFiles: Number.isSafeInteger(parsed.droppedFiles) && Number(parsed.droppedFiles) >= 0 ? Number(parsed.droppedFiles) : 0,
|
|
503
|
+
cleanedRemnantFiles: Number.isSafeInteger(parsed.cleanedRemnantFiles) && Number(parsed.cleanedRemnantFiles) >= 0 ? Number(parsed.cleanedRemnantFiles) : 0,
|
|
504
|
+
droppedSpans: Number.isSafeInteger(parsed.droppedSpans) && Number(parsed.droppedSpans) >= 0 ? Number(parsed.droppedSpans) : 0,
|
|
505
|
+
lastUploadAt: typeof parsed.lastUploadAt === "string" ? parsed.lastUploadAt : null,
|
|
506
|
+
lastErrorCategory: typeof parsed.lastErrorCategory === "string" ? parsed.lastErrorCategory : null };
|
|
507
|
+
}
|
|
508
|
+
catch {
|
|
509
|
+
return emptyDiagnostics();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
updateDiagnostics(patch) {
|
|
513
|
+
this.atomicWrite(node_path_1.default.join(this.config.spoolDir, "diagnostics.json"), Buffer.from(JSON.stringify({ ...this.diagnostics(), ...patch })));
|
|
514
|
+
}
|
|
515
|
+
prune(now = Date.now()) {
|
|
516
|
+
const release = this.acquireLease();
|
|
517
|
+
if (!release)
|
|
518
|
+
return;
|
|
519
|
+
try {
|
|
520
|
+
let files = [...this.readyFiles().map((entry) => ({ ...entry, directory: false, remnant: false })), ...this.remnantFiles()]
|
|
521
|
+
.sort((a, b) => a.mtimeMs - b.mtimeMs || a.file.localeCompare(b.file));
|
|
522
|
+
let dropped = 0;
|
|
523
|
+
let cleaned = 0;
|
|
524
|
+
const remove = (entry) => { if (this.removeManaged(entry)) {
|
|
525
|
+
if (entry.remnant)
|
|
526
|
+
cleaned += 1;
|
|
527
|
+
else
|
|
528
|
+
dropped += 1;
|
|
529
|
+
} };
|
|
530
|
+
for (const entry of files.filter((item) => now - item.mtimeMs > this.config.maxAgeMs))
|
|
531
|
+
remove(entry);
|
|
532
|
+
files = [...this.readyFiles().map((entry) => ({ ...entry, directory: false, remnant: false })), ...this.remnantFiles()]
|
|
533
|
+
.sort((a, b) => a.mtimeMs - b.mtimeMs || a.file.localeCompare(b.file));
|
|
534
|
+
let bytes = files.reduce((sum, entry) => sum + entry.size, 0);
|
|
535
|
+
while (files.length > this.config.maxFiles || bytes > this.config.maxBytes) {
|
|
536
|
+
const oldest = files.shift();
|
|
537
|
+
if (!oldest)
|
|
538
|
+
break;
|
|
539
|
+
const before = dropped + cleaned;
|
|
540
|
+
remove(oldest);
|
|
541
|
+
if (dropped + cleaned > before)
|
|
542
|
+
bytes -= oldest.size;
|
|
543
|
+
}
|
|
544
|
+
if (dropped || cleaned)
|
|
545
|
+
try {
|
|
546
|
+
const diagnostic = this.diagnostics();
|
|
547
|
+
this.updateDiagnostics({ droppedFiles: diagnostic.droppedFiles + dropped,
|
|
548
|
+
cleanedRemnantFiles: diagnostic.cleanedRemnantFiles + cleaned });
|
|
549
|
+
}
|
|
550
|
+
catch { /* isolated */ }
|
|
551
|
+
}
|
|
552
|
+
finally {
|
|
553
|
+
release();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
status(now = Date.now()) {
|
|
557
|
+
try {
|
|
558
|
+
const files = this.readyFiles();
|
|
559
|
+
const remnants = this.remnantFiles();
|
|
560
|
+
const diagnostic = this.diagnostics();
|
|
561
|
+
return { queuedFiles: files.length, queuedBytes: files.reduce((sum, item) => sum + item.size, 0),
|
|
562
|
+
oldestAgeMs: files[0] ? Math.max(0, now - files[0].mtimeMs) : null,
|
|
563
|
+
remnantFiles: remnants.length, remnantBytes: remnants.reduce((sum, item) => sum + item.size, 0),
|
|
564
|
+
oldestRemnantAgeMs: remnants[0] ? Math.max(0, now - remnants[0].mtimeMs) : null, ...diagnostic };
|
|
565
|
+
}
|
|
566
|
+
catch {
|
|
567
|
+
return { queuedFiles: 0, queuedBytes: 0, oldestAgeMs: null, remnantFiles: 0, remnantBytes: 0,
|
|
568
|
+
oldestRemnantAgeMs: null, ...emptyDiagnostics(), lastErrorCategory: "spool" };
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
exportBundle(destination) {
|
|
572
|
+
const release = this.acquireLease();
|
|
573
|
+
if (!release)
|
|
574
|
+
throw new Error("telemetry queue busy");
|
|
575
|
+
try {
|
|
576
|
+
const records = this.list().map(({ payload }) => { const bytes = Buffer.from(JSON.stringify(payload)); return { sha256: node_crypto_1.default.createHash("sha256").update(bytes).digest("hex"), payload }; });
|
|
577
|
+
const bundle = { format: "larkin-otlp-bundle", version: 1, createdAt: new Date().toISOString(), records };
|
|
578
|
+
const compressed = node_zlib_1.default.gzipSync(Buffer.from(JSON.stringify(bundle)), { level: 9 });
|
|
579
|
+
const descriptor = node_fs_1.default.openSync(destination, node_fs_1.default.constants.O_WRONLY | node_fs_1.default.constants.O_CREAT | node_fs_1.default.constants.O_EXCL | node_fs_1.default.constants.O_NOFOLLOW, 0o600);
|
|
580
|
+
try {
|
|
581
|
+
node_fs_1.default.writeFileSync(descriptor, compressed);
|
|
582
|
+
node_fs_1.default.fsyncSync(descriptor);
|
|
583
|
+
}
|
|
584
|
+
finally {
|
|
585
|
+
node_fs_1.default.closeSync(descriptor);
|
|
586
|
+
}
|
|
587
|
+
return { records: records.length, sha256: node_crypto_1.default.createHash("sha256").update(compressed).digest("hex") };
|
|
588
|
+
}
|
|
589
|
+
finally {
|
|
590
|
+
release();
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
importBundle(source, maxBundleBytes = 128 * 1024 * 1024) {
|
|
594
|
+
let descriptor;
|
|
595
|
+
let bytes;
|
|
596
|
+
try {
|
|
597
|
+
descriptor = node_fs_1.default.openSync(source, node_fs_1.default.constants.O_RDONLY | node_fs_1.default.constants.O_NOFOLLOW);
|
|
598
|
+
const stat = node_fs_1.default.fstatSync(descriptor);
|
|
599
|
+
if (!stat.isFile() || stat.size > maxBundleBytes)
|
|
600
|
+
throw new Error("invalid telemetry bundle");
|
|
601
|
+
bytes = node_fs_1.default.readFileSync(descriptor);
|
|
602
|
+
}
|
|
603
|
+
catch {
|
|
604
|
+
throw new Error("invalid telemetry bundle");
|
|
605
|
+
}
|
|
606
|
+
finally {
|
|
607
|
+
if (descriptor !== undefined)
|
|
608
|
+
node_fs_1.default.closeSync(descriptor);
|
|
609
|
+
}
|
|
610
|
+
let parsed;
|
|
611
|
+
try {
|
|
612
|
+
parsed = JSON.parse(node_zlib_1.default.gunzipSync(bytes, { maxOutputLength: maxBundleBytes }).toString("utf8"));
|
|
613
|
+
}
|
|
614
|
+
catch {
|
|
615
|
+
throw new Error("invalid telemetry bundle");
|
|
616
|
+
}
|
|
617
|
+
if (parsed.format !== "larkin-otlp-bundle" || parsed.version !== 1 || !Array.isArray(parsed.records) || parsed.records.length > 100_000)
|
|
618
|
+
throw new Error("invalid telemetry bundle");
|
|
619
|
+
const validated = parsed.records.map((record) => {
|
|
620
|
+
validateOtlpPayload(record?.payload);
|
|
621
|
+
const payloadBytes = Buffer.from(JSON.stringify(record.payload));
|
|
622
|
+
const sha256 = node_crypto_1.default.createHash("sha256").update(payloadBytes).digest("hex");
|
|
623
|
+
if (sha256 !== record.sha256)
|
|
624
|
+
throw new Error("invalid telemetry bundle");
|
|
625
|
+
return { sha256, payloadBytes };
|
|
626
|
+
});
|
|
627
|
+
const release = this.acquireLease();
|
|
628
|
+
if (!release)
|
|
629
|
+
throw new Error("telemetry queue busy");
|
|
630
|
+
const added = [];
|
|
631
|
+
let duplicates = 0;
|
|
632
|
+
try {
|
|
633
|
+
for (const record of validated) {
|
|
634
|
+
const destination = node_path_1.default.join(this.config.spoolDir, `import-${record.sha256}.json`);
|
|
635
|
+
if (node_fs_1.default.existsSync(destination)) {
|
|
636
|
+
duplicates += 1;
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
this.atomicWrite(destination, record.payloadBytes);
|
|
640
|
+
added.push(destination);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
catch (error) {
|
|
644
|
+
for (const file of added)
|
|
645
|
+
try {
|
|
646
|
+
node_fs_1.default.unlinkSync(file);
|
|
647
|
+
}
|
|
648
|
+
catch { /* rollback best effort */ }
|
|
649
|
+
throw error;
|
|
650
|
+
}
|
|
651
|
+
finally {
|
|
652
|
+
release();
|
|
653
|
+
}
|
|
654
|
+
try {
|
|
655
|
+
this.prune();
|
|
656
|
+
}
|
|
657
|
+
catch { /* isolated */ }
|
|
658
|
+
return { imported: added.length, duplicates };
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
exports.TelemetrySpool = TelemetrySpool;
|