@fro.bot/opencode-plugin 0.1.0 → 0.2.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.
- package/README.md +31 -0
- package/THIRD_PARTY_NOTICES.md +224 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +418 -16
- package/dist/index.mjs.map +1 -1
- package/dist/tracing-D4bUTAUS.mjs +7769 -0
- package/dist/tracing-D4bUTAUS.mjs.map +1 -0
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import { Plugin } from "@opencode/plugin";
|
|
2
|
+
import process from "node:process";
|
|
2
3
|
import { Schema } from "effect";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { closeSync, constants, fstatSync, openSync, readSync, realpathSync } from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
3
8
|
// src/plugin-id.ts
|
|
4
9
|
/** Stable plugin id; the surfaces look it up in `GET /api/plugin` to confirm the plugin is active. */
|
|
5
10
|
const FRO_BOT_PLUGIN_ID = "fro-bot.opencode-plugin";
|
|
11
|
+
// src/log.ts
|
|
12
|
+
/**
|
|
13
|
+
* The plugin's one log channel. `Plugin.Context` has no logger, and stdout carries the host's protocol in `serve --stdio`,
|
|
14
|
+
* so a line goes to stderr, where the server's own diagnostics go. Never throws, and takes only fixed text: callers pass
|
|
15
|
+
* no setting, header or error message.
|
|
16
|
+
*/
|
|
17
|
+
function logPluginLine(message) {
|
|
18
|
+
try {
|
|
19
|
+
process.stderr.write(`[fro-bot/opencode-plugin] ${message}\n`);
|
|
20
|
+
} catch {}
|
|
21
|
+
}
|
|
6
22
|
// src/session-client.ts
|
|
7
23
|
const SESSION_ID_PREFIX = "ses";
|
|
8
24
|
const NOT_FOUND_TAG = "Session.NotFoundError";
|
|
@@ -38,21 +54,21 @@ function err(error) {
|
|
|
38
54
|
};
|
|
39
55
|
}
|
|
40
56
|
// ../runtime/src/session/storage-mappers-v2.ts
|
|
41
|
-
const isRecord = (v) => typeof v === "object" && v != null;
|
|
57
|
+
const isRecord$1 = (v) => typeof v === "object" && v != null;
|
|
42
58
|
const readString = (v) => typeof v === "string" ? v : null;
|
|
43
59
|
const readNumber = (v) => typeof v === "number" && Number.isFinite(v) ? v : null;
|
|
44
60
|
/** Strips a `{data}` envelope. A bare payload passes through. */
|
|
45
61
|
function unwrapData(v) {
|
|
46
|
-
return isRecord(v) && !Array.isArray(v) && "data" in v ? v.data : v;
|
|
62
|
+
return isRecord$1(v) && !Array.isArray(v) && "data" in v ? v.data : v;
|
|
47
63
|
}
|
|
48
64
|
function mapSessionInfoV2(v) {
|
|
49
65
|
const payload = unwrapData(v);
|
|
50
|
-
if (!isRecord(payload) || Array.isArray(payload)) return null;
|
|
66
|
+
if (!isRecord$1(payload) || Array.isArray(payload)) return null;
|
|
51
67
|
const id = readString(payload.id);
|
|
52
68
|
const projectID = readString(payload.projectID);
|
|
53
69
|
const time = payload.time;
|
|
54
70
|
const location = payload.location;
|
|
55
|
-
if (id == null || projectID == null || !isRecord(time) || !isRecord(location)) return null;
|
|
71
|
+
if (id == null || projectID == null || !isRecord$1(time) || !isRecord$1(location)) return null;
|
|
56
72
|
const created = readNumber(time.created);
|
|
57
73
|
const updated = readNumber(time.updated);
|
|
58
74
|
const directory = readString(location.directory);
|
|
@@ -77,15 +93,15 @@ function mapSessionInfoV2(v) {
|
|
|
77
93
|
* for "no sessions". Entries that fail to map are dropped.
|
|
78
94
|
*/
|
|
79
95
|
function mapSessionPageV2(v) {
|
|
80
|
-
const items = Array.isArray(v) ? v : isRecord(v) && Array.isArray(v.data) ? v.data : null;
|
|
96
|
+
const items = Array.isArray(v) ? v : isRecord$1(v) && Array.isArray(v.data) ? v.data : null;
|
|
81
97
|
if (items == null) return null;
|
|
82
98
|
const sessions = [];
|
|
83
99
|
for (const item of items) {
|
|
84
100
|
const mapped = mapSessionInfoV2(item);
|
|
85
101
|
if (mapped != null) sessions.push(mapped);
|
|
86
102
|
}
|
|
87
|
-
const cursor = isRecord(v) ? v.cursor : void 0;
|
|
88
|
-
const nextCursor = isRecord(cursor) ? readString(cursor.next) : null;
|
|
103
|
+
const cursor = isRecord$1(v) ? v.cursor : void 0;
|
|
104
|
+
const nextCursor = isRecord$1(cursor) ? readString(cursor.next) : null;
|
|
89
105
|
return {
|
|
90
106
|
sessions,
|
|
91
107
|
...nextCursor == null || nextCursor.length === 0 ? {} : { nextCursor }
|
|
@@ -109,17 +125,17 @@ function textPart(messageID, index, kind, text) {
|
|
|
109
125
|
/** Joins the text items of a tool result. File items carry no searchable text. */
|
|
110
126
|
function readToolText(content) {
|
|
111
127
|
if (!Array.isArray(content)) return null;
|
|
112
|
-
const texts = content.filter(isRecord).flatMap((item) => {
|
|
128
|
+
const texts = content.filter(isRecord$1).flatMap((item) => {
|
|
113
129
|
const text = item.type === "text" ? readString(item.text) : null;
|
|
114
130
|
return text == null ? [] : [text];
|
|
115
131
|
});
|
|
116
132
|
return texts.length === 0 ? null : texts.join("\n");
|
|
117
133
|
}
|
|
118
134
|
function readTodos(input) {
|
|
119
|
-
if (!isRecord(input) || !Array.isArray(input.todos)) return null;
|
|
135
|
+
if (!isRecord$1(input) || !Array.isArray(input.todos)) return null;
|
|
120
136
|
const todos = [];
|
|
121
137
|
for (const item of input.todos) {
|
|
122
|
-
if (!isRecord(item)) continue;
|
|
138
|
+
if (!isRecord$1(item)) continue;
|
|
123
139
|
const content = readString(item.content);
|
|
124
140
|
if (content == null) continue;
|
|
125
141
|
const status = TODO_STATUSES.find((candidate) => candidate === item.status);
|
|
@@ -135,7 +151,7 @@ function mapAssistantContent(messageID, content) {
|
|
|
135
151
|
const parts = [];
|
|
136
152
|
let todos;
|
|
137
153
|
for (const item of content) {
|
|
138
|
-
if (!isRecord(item)) continue;
|
|
154
|
+
if (!isRecord$1(item)) continue;
|
|
139
155
|
const index = parts.length;
|
|
140
156
|
if (item.type === "text") {
|
|
141
157
|
const text = readString(item.text);
|
|
@@ -143,7 +159,7 @@ function mapAssistantContent(messageID, content) {
|
|
|
143
159
|
} else if (item.type === "reasoning") {
|
|
144
160
|
const text = readString(item.text);
|
|
145
161
|
if (text != null) parts.push(textPart(messageID, index, "reasoning", text));
|
|
146
|
-
} else if (item.type === "tool" && isRecord(item.state) && item.state.status === "completed") {
|
|
162
|
+
} else if (item.type === "tool" && isRecord$1(item.state) && item.state.status === "completed") {
|
|
147
163
|
const name = readString(item.name);
|
|
148
164
|
const output = readToolText(item.state.content);
|
|
149
165
|
if (name != null && output != null) parts.push(textPart(messageID, index, "tool", `${name}: ${output}`));
|
|
@@ -156,10 +172,10 @@ function mapAssistantContent(messageID, content) {
|
|
|
156
172
|
};
|
|
157
173
|
}
|
|
158
174
|
function mapMessageV2(v) {
|
|
159
|
-
if (!isRecord(v)) return null;
|
|
175
|
+
if (!isRecord$1(v)) return null;
|
|
160
176
|
const id = readString(v.id);
|
|
161
177
|
const time = v.time;
|
|
162
|
-
const createdAt = isRecord(time) ? readNumber(time.created) : null;
|
|
178
|
+
const createdAt = isRecord$1(time) ? readNumber(time.created) : null;
|
|
163
179
|
if (id == null || createdAt == null) return null;
|
|
164
180
|
if (v.type === "user" || v.type === "synthetic") {
|
|
165
181
|
const text = readString(v.text);
|
|
@@ -235,6 +251,9 @@ function toError(error) {
|
|
|
235
251
|
const SESSION_LIST_PAGE_SIZE = 100;
|
|
236
252
|
/** Lists root, unarchived sessions created in `directory` (exact string match), newest first, up to a page bound. */
|
|
237
253
|
async function listSessionsForDirectoryV2(client, directory, logger) {
|
|
254
|
+
return listRootSessionsV2(client, directory, logger, false);
|
|
255
|
+
}
|
|
256
|
+
async function listRootSessionsV2(client, directory, logger, archivedOnly) {
|
|
238
257
|
const sessions = [];
|
|
239
258
|
let cursor;
|
|
240
259
|
for (let page = 0; page < 10; page += 1) {
|
|
@@ -245,7 +264,7 @@ async function listSessionsForDirectoryV2(client, directory, logger) {
|
|
|
245
264
|
limit: SESSION_LIST_PAGE_SIZE,
|
|
246
265
|
order: "desc",
|
|
247
266
|
parentID: null,
|
|
248
|
-
archived:
|
|
267
|
+
archived: archivedOnly,
|
|
249
268
|
...cursor == null ? {} : { cursor }
|
|
250
269
|
});
|
|
251
270
|
} catch (error) {
|
|
@@ -667,8 +686,383 @@ function rejectSubagentReuse(event) {
|
|
|
667
686
|
if (event.tool !== "subagent") return;
|
|
668
687
|
if (carriesResume(event.input)) throw new Error(TASK_REUSE_REJECTION);
|
|
669
688
|
}
|
|
689
|
+
// src/tracing/settings.ts
|
|
690
|
+
/**
|
|
691
|
+
* Host mode and tracing settings: one function decides whether tracing is on and where spans go, and fails closed.
|
|
692
|
+
*
|
|
693
|
+
* This module sits OUTSIDE the lazy tracing chunk: it decides whether the chunk loads at all, so it must stay small and
|
|
694
|
+
* import nothing from the OpenTelemetry SDK. Resolving to off never evaluates the chunk.
|
|
695
|
+
*
|
|
696
|
+
* Where settings come from, and nowhere else:
|
|
697
|
+
* - The launcher variable `OPENCODE_FROBOT_TRACING`, set by whatever started the server (the Action, the workspace).
|
|
698
|
+
* - Otherwise, only in a local session, a user-scope file under the user's config directory.
|
|
699
|
+
* Plugin options, project config, the checkout, restored state and session content are never read.
|
|
700
|
+
*
|
|
701
|
+
* The user file names the machines it is for (`hosts`, required): a file synced to another machine, or left behind on a
|
|
702
|
+
* shared one, does nothing there. And a remote collector in it must be `https`, with the platform's certificate
|
|
703
|
+
* verification left on: the exporter sends the file's headers (a credential) to that URL, and the file has no switch to
|
|
704
|
+
* relax verification, to follow a redirect, or to use plain `http` anywhere but a loopback literal.
|
|
705
|
+
*/
|
|
706
|
+
/**
|
|
707
|
+
* Name of the launcher variable. `OPENCODE_`-prefixed so the Action's environment filter admits it, and not ending in a
|
|
708
|
+
* denied suffix (`_TOKEN`, `_KEY`, `_SECRET`, `_PASSWORD`, …). It carries no credential: only host, on/off, a loopback
|
|
709
|
+
* relay address and run identity.
|
|
710
|
+
*/
|
|
711
|
+
const TRACING_LAUNCHER_ENV = "OPENCODE_FROBOT_TRACING";
|
|
712
|
+
/** Variables a headless server (the Action, the workspace, any `opencode serve`) always has and a local session lacks. */
|
|
713
|
+
const HEADLESS_MARKERS = ["OPENCODE_PASSWORD", "OPENCODE_CONFIG_CONTENT"];
|
|
714
|
+
/**
|
|
715
|
+
* True when the command line is `opencode … serve … --stdio`: a server a parent process embeds (an SDK client, a gateway,
|
|
716
|
+
* the Action). `opencode serve --stdio` deletes `OPENCODE_PASSWORD` and `OPENCODE_SERVER_PASSWORD` from the process
|
|
717
|
+
* environment before a plugin loads (measured on 2.3.2), so such a server started with only a password shows none of the
|
|
718
|
+
* markers above. The command line is what still says so. A `serve` started by hand, or the background service
|
|
719
|
+
* (`serve --service`; `--stdio` cannot be combined with it), is not embedded: the user's file is consent for those too.
|
|
720
|
+
* The subcommand is the first non-option word after the program path; `--stdio` must come after it.
|
|
721
|
+
*/
|
|
722
|
+
function isEmbeddedServe(argv) {
|
|
723
|
+
const rest = argv.slice(2);
|
|
724
|
+
const serve = rest.indexOf("serve");
|
|
725
|
+
return serve !== -1 && rest.slice(serve + 1).includes("--stdio");
|
|
726
|
+
}
|
|
727
|
+
/** The user file, relative to the user's config directory. Not under `opencode/`: OpenCode reads nothing from here. */
|
|
728
|
+
const USER_SETTINGS_RELATIVE_PATH = path.join("fro-bot", "opencode-plugin", "tracing.json");
|
|
729
|
+
const SILENT_REASONS = /* @__PURE__ */ new Set([
|
|
730
|
+
"action-off",
|
|
731
|
+
"workspace-host",
|
|
732
|
+
"unsupported-platform",
|
|
733
|
+
"no-settings-file",
|
|
734
|
+
"machine-not-listed"
|
|
735
|
+
]);
|
|
736
|
+
const off = (reason) => ({
|
|
737
|
+
mode: "off",
|
|
738
|
+
reason
|
|
739
|
+
});
|
|
740
|
+
function isRecord(value) {
|
|
741
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
742
|
+
}
|
|
743
|
+
/** True when `value` has exactly the listed keys, no more and no fewer. */
|
|
744
|
+
function hasExactKeys(value, keys) {
|
|
745
|
+
return Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
746
|
+
}
|
|
747
|
+
function hasControlCharacter(value) {
|
|
748
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
749
|
+
const code = value.charCodeAt(index);
|
|
750
|
+
if (code < 32 || code === 127) return true;
|
|
751
|
+
}
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
/** `127.0.0.0/8` literals or `[::1]`. A hostname such as `localhost` is not accepted: it resolves through the host. */
|
|
755
|
+
function isLoopbackHostname(hostname) {
|
|
756
|
+
if (hostname === "[::1]") return true;
|
|
757
|
+
const octets = hostname.split(".");
|
|
758
|
+
return octets.length === 4 && octets[0] === "127" && octets.every((octet) => /^\d{1,3}$/.test(octet) && Number(octet) <= 255);
|
|
759
|
+
}
|
|
760
|
+
function parseUrl(value) {
|
|
761
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 2048) return void 0;
|
|
762
|
+
if (hasControlCharacter(value)) return void 0;
|
|
763
|
+
try {
|
|
764
|
+
const url = new URL(value);
|
|
765
|
+
if (url.username !== "" || url.password !== "" || url.hash !== "") return void 0;
|
|
766
|
+
return url;
|
|
767
|
+
} catch {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
/** The launcher's relay: plain HTTP to a loopback literal with an explicit port. */
|
|
772
|
+
function parseRelayUrl(value) {
|
|
773
|
+
const url = parseUrl(value);
|
|
774
|
+
if (url === void 0 || url.protocol !== "http:" || !isLoopbackHostname(url.hostname) || url.port === "") return;
|
|
775
|
+
if (url.search !== "") return void 0;
|
|
776
|
+
return url.href;
|
|
777
|
+
}
|
|
778
|
+
/** A user-file endpoint: HTTPS, or plain HTTP to a loopback literal. */
|
|
779
|
+
function parseCollectorUrl(value) {
|
|
780
|
+
const url = parseUrl(value);
|
|
781
|
+
if (url === void 0) return void 0;
|
|
782
|
+
if (url.protocol === "https:") return url;
|
|
783
|
+
if (url.protocol === "http:" && isLoopbackHostname(url.hostname)) return url;
|
|
784
|
+
}
|
|
785
|
+
function parseIdentity(value) {
|
|
786
|
+
if (!isRecord(value) || !hasExactKeys(value, [
|
|
787
|
+
"repository",
|
|
788
|
+
"runId",
|
|
789
|
+
"attempt",
|
|
790
|
+
"job",
|
|
791
|
+
"trigger"
|
|
792
|
+
])) return void 0;
|
|
793
|
+
const fields = [];
|
|
794
|
+
for (const key of [
|
|
795
|
+
"repository",
|
|
796
|
+
"runId",
|
|
797
|
+
"attempt",
|
|
798
|
+
"job",
|
|
799
|
+
"trigger"
|
|
800
|
+
]) {
|
|
801
|
+
const field = value[key];
|
|
802
|
+
if (typeof field !== "string" || field.length === 0 || field.length > 200) return void 0;
|
|
803
|
+
if (hasControlCharacter(field)) return void 0;
|
|
804
|
+
fields.push(field);
|
|
805
|
+
}
|
|
806
|
+
const [repository = "", runId = "", attempt = "", job = "", trigger = ""] = fields;
|
|
807
|
+
return {
|
|
808
|
+
repository,
|
|
809
|
+
runId,
|
|
810
|
+
attempt,
|
|
811
|
+
job,
|
|
812
|
+
trigger
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
/** Resolves the launcher variable's value. Pure: no environment, no file. */
|
|
816
|
+
function resolveLauncher(raw) {
|
|
817
|
+
if (raw.length === 0 || raw.length > 4096) return off("launcher-malformed");
|
|
818
|
+
let document;
|
|
819
|
+
try {
|
|
820
|
+
document = JSON.parse(raw);
|
|
821
|
+
} catch {
|
|
822
|
+
return off("launcher-malformed");
|
|
823
|
+
}
|
|
824
|
+
if (!isRecord(document)) return off("launcher-malformed");
|
|
825
|
+
if (document.version !== 1) return off("launcher-unsupported");
|
|
826
|
+
if (document.host === "workspace") return off("workspace-host");
|
|
827
|
+
if (document.host !== "action") return off("launcher-unsupported");
|
|
828
|
+
if (document.tracing === "off") return off("action-off");
|
|
829
|
+
if (document.tracing !== "on") return off("launcher-malformed");
|
|
830
|
+
if (!hasExactKeys(document, [
|
|
831
|
+
"version",
|
|
832
|
+
"host",
|
|
833
|
+
"tracing",
|
|
834
|
+
"relay",
|
|
835
|
+
"identity"
|
|
836
|
+
])) return off("launcher-malformed");
|
|
837
|
+
const endpoint = parseRelayUrl(document.relay);
|
|
838
|
+
if (endpoint === void 0) return off("launcher-relay-invalid");
|
|
839
|
+
const identity = parseIdentity(document.identity);
|
|
840
|
+
if (identity === void 0) return off("launcher-identity-invalid");
|
|
841
|
+
return {
|
|
842
|
+
mode: "on",
|
|
843
|
+
host: "action",
|
|
844
|
+
endpoint,
|
|
845
|
+
identity
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
/** Header names the exporter or the transport owns. A user file cannot set them. */
|
|
849
|
+
const REFUSED_HEADER_NAMES = /* @__PURE__ */ new Set([
|
|
850
|
+
"host",
|
|
851
|
+
"content-length",
|
|
852
|
+
"content-type",
|
|
853
|
+
"content-encoding",
|
|
854
|
+
"transfer-encoding",
|
|
855
|
+
"connection",
|
|
856
|
+
"keep-alive",
|
|
857
|
+
"upgrade",
|
|
858
|
+
"te",
|
|
859
|
+
"trailer",
|
|
860
|
+
"expect",
|
|
861
|
+
"proxy-authorization",
|
|
862
|
+
"proxy-authenticate"
|
|
863
|
+
]);
|
|
864
|
+
const HEADER_NAME = /^[\w!#$%&'*+.^`|~-]+$/;
|
|
865
|
+
function parseHeaders(value) {
|
|
866
|
+
if (value === void 0) return {};
|
|
867
|
+
if (!isRecord(value)) return void 0;
|
|
868
|
+
const entries = Object.entries(value);
|
|
869
|
+
if (entries.length > 16) return void 0;
|
|
870
|
+
const headers = {};
|
|
871
|
+
for (const [name, headerValue] of entries) {
|
|
872
|
+
const lower = name.toLowerCase();
|
|
873
|
+
if (name.length > 64 || !HEADER_NAME.test(name) || REFUSED_HEADER_NAMES.has(lower)) return void 0;
|
|
874
|
+
if (typeof headerValue !== "string" || headerValue.length === 0 || headerValue.length > 4096) return;
|
|
875
|
+
if (hasControlCharacter(headerValue) || Object.hasOwn(headers, lower)) return void 0;
|
|
876
|
+
headers[lower] = headerValue;
|
|
877
|
+
}
|
|
878
|
+
return headers;
|
|
879
|
+
}
|
|
880
|
+
/** `$XDG_CONFIG_HOME` when it is an absolute path, else `~/.config`. */
|
|
881
|
+
function userSettingsFilePath(env) {
|
|
882
|
+
const configured = env.XDG_CONFIG_HOME;
|
|
883
|
+
if (configured !== void 0 && path.isAbsolute(configured)) return path.join(configured, USER_SETTINGS_RELATIVE_PATH);
|
|
884
|
+
const home = env.HOME !== void 0 && path.isAbsolute(env.HOME) ? env.HOME : os.homedir();
|
|
885
|
+
return path.join(home, ".config", USER_SETTINGS_RELATIVE_PATH);
|
|
886
|
+
}
|
|
887
|
+
function errorCode(error) {
|
|
888
|
+
return isRecord(error) && typeof error.code === "string" ? error.code : void 0;
|
|
889
|
+
}
|
|
890
|
+
function isInside(child, parent) {
|
|
891
|
+
const relative = path.relative(parent, child);
|
|
892
|
+
if (relative === "") return true;
|
|
893
|
+
return !(relative === ".." || relative.startsWith(`..${path.sep}`)) && !path.isAbsolute(relative);
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
896
|
+
* Reads the user file, checking the OPENED handle: a regular file, owned by the current user, private to its owner (no group
|
|
897
|
+
* or other permission bit at all: it can hold a collector credential, so readable is as bad as writable), small, and whose
|
|
898
|
+
* real path is outside the project. A symlink is resolved first and its target is what gets opened and checked.
|
|
899
|
+
*/
|
|
900
|
+
function readSettingsFile(filePath, directory, uid) {
|
|
901
|
+
let realPath;
|
|
902
|
+
try {
|
|
903
|
+
realPath = realpathSync(filePath);
|
|
904
|
+
} catch (error) {
|
|
905
|
+
const code = errorCode(error);
|
|
906
|
+
return {
|
|
907
|
+
ok: false,
|
|
908
|
+
reason: code === "ENOENT" || code === "ENOTDIR" ? "no-settings-file" : "settings-file-unreadable"
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
let realDirectory;
|
|
912
|
+
try {
|
|
913
|
+
realDirectory = realpathSync(directory);
|
|
914
|
+
} catch {
|
|
915
|
+
realDirectory = path.resolve(directory);
|
|
916
|
+
}
|
|
917
|
+
if (isInside(realPath, realDirectory)) return {
|
|
918
|
+
ok: false,
|
|
919
|
+
reason: "settings-file-unsafe"
|
|
920
|
+
};
|
|
921
|
+
let fd;
|
|
922
|
+
try {
|
|
923
|
+
fd = openSync(realPath, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
924
|
+
} catch {
|
|
925
|
+
return {
|
|
926
|
+
ok: false,
|
|
927
|
+
reason: "settings-file-unreadable"
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
try {
|
|
931
|
+
const stat = fstatSync(fd);
|
|
932
|
+
if (!stat.isFile() || stat.uid !== uid || (stat.mode & 63) !== 0 || stat.size > 16384) return {
|
|
933
|
+
ok: false,
|
|
934
|
+
reason: "settings-file-unsafe"
|
|
935
|
+
};
|
|
936
|
+
const buffer = Buffer.alloc(stat.size);
|
|
937
|
+
let offset = 0;
|
|
938
|
+
while (offset < buffer.length) {
|
|
939
|
+
const read = readSync(fd, buffer, offset, buffer.length - offset, offset);
|
|
940
|
+
if (read === 0) break;
|
|
941
|
+
offset += read;
|
|
942
|
+
}
|
|
943
|
+
return {
|
|
944
|
+
ok: true,
|
|
945
|
+
text: buffer.subarray(0, offset).toString("utf8")
|
|
946
|
+
};
|
|
947
|
+
} catch {
|
|
948
|
+
return {
|
|
949
|
+
ok: false,
|
|
950
|
+
reason: "settings-file-unreadable"
|
|
951
|
+
};
|
|
952
|
+
} finally {
|
|
953
|
+
closeSync(fd);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
/** A name as compared: trimmed, lower case, without one trailing dot. */
|
|
957
|
+
const normalizeHostname = (value) => {
|
|
958
|
+
const lower = value.trim().toLowerCase();
|
|
959
|
+
return lower.endsWith(".") ? lower.slice(0, -1) : lower;
|
|
960
|
+
};
|
|
961
|
+
/** Letters, digits, dot, hyphen and underscore: what a machine name holds. No wildcard, port, path or space. */
|
|
962
|
+
const HOSTNAME_ENTRY = /^[\da-z._-]+$/;
|
|
963
|
+
/** The `hosts` list: one to 16 names, each normalized. Undefined when the list or any entry is not acceptable. */
|
|
964
|
+
function parseHosts(value) {
|
|
965
|
+
if (!Array.isArray(value) || value.length === 0 || value.length > 16) return void 0;
|
|
966
|
+
const hosts = [];
|
|
967
|
+
for (const entry of value) {
|
|
968
|
+
if (typeof entry !== "string" || entry.length > 255) return void 0;
|
|
969
|
+
const host = normalizeHostname(entry);
|
|
970
|
+
if (host.length === 0 || host.length > 253 || !HOSTNAME_ENTRY.test(host)) return void 0;
|
|
971
|
+
hosts.push(host);
|
|
972
|
+
}
|
|
973
|
+
return hosts;
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Whether `machine` is one of `hosts`. An entry matches the whole name, or the part of the name before its first dot. That
|
|
977
|
+
* part never holds a dot, so an entry with a dot can only ever match the whole name: `build-box.example.test` does not
|
|
978
|
+
* match a machine called `build-box`. No wildcards, no prefix or suffix matching.
|
|
979
|
+
*/
|
|
980
|
+
function namesMachine(hosts, machine) {
|
|
981
|
+
const name = normalizeHostname(machine);
|
|
982
|
+
if (name.length === 0) return false;
|
|
983
|
+
const short = name.split(".")[0];
|
|
984
|
+
return hosts.some((host) => host === name || host === short);
|
|
985
|
+
}
|
|
986
|
+
function resolveSettingsFile(text, hostname) {
|
|
987
|
+
let document;
|
|
988
|
+
try {
|
|
989
|
+
document = JSON.parse(text);
|
|
990
|
+
} catch {
|
|
991
|
+
return { settings: off("settings-file-invalid") };
|
|
992
|
+
}
|
|
993
|
+
if (!isRecord(document) || document.version !== 1) return { settings: off("settings-file-invalid") };
|
|
994
|
+
const allowed = Object.keys(document).every((key) => key === "version" || key === "endpoint" || key === "headers" || key === "hosts");
|
|
995
|
+
const url = parseCollectorUrl(document.endpoint);
|
|
996
|
+
const headers = parseHeaders(document.headers);
|
|
997
|
+
const hosts = parseHosts(document.hosts);
|
|
998
|
+
if (!allowed || url === void 0 || headers === void 0 || hosts === void 0) return { settings: off("settings-file-invalid") };
|
|
999
|
+
if (!namesMachine(hosts, hostname())) return { settings: off("machine-not-listed") };
|
|
1000
|
+
return {
|
|
1001
|
+
settings: {
|
|
1002
|
+
mode: "on",
|
|
1003
|
+
host: "local",
|
|
1004
|
+
endpoint: url.href,
|
|
1005
|
+
headers
|
|
1006
|
+
},
|
|
1007
|
+
host: url.host
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
/**
|
|
1011
|
+
* Decides whether tracing is on. Never throws. Logs at most one line: an off with a reason worth knowing, or, for a local
|
|
1012
|
+
* session, the destination host. Lines carry fixed text and a host name, never a header, a path or a settings value.
|
|
1013
|
+
*/
|
|
1014
|
+
function resolveTracingSettings(input) {
|
|
1015
|
+
const { env, directory, log } = input;
|
|
1016
|
+
const finish = (settings) => {
|
|
1017
|
+
if (settings.mode === "off" && !SILENT_REASONS.has(settings.reason)) log(`tracing off (${settings.reason})`);
|
|
1018
|
+
return settings;
|
|
1019
|
+
};
|
|
1020
|
+
try {
|
|
1021
|
+
const launcher = env[TRACING_LAUNCHER_ENV];
|
|
1022
|
+
if (launcher !== void 0) return finish(resolveLauncher(launcher));
|
|
1023
|
+
if (HEADLESS_MARKERS.some((marker) => env[marker] !== void 0) || isEmbeddedServe(input.argv ?? process.argv)) return finish(off("headless-host"));
|
|
1024
|
+
const platform = input.platform ?? process.platform;
|
|
1025
|
+
const uid = input.uid ?? process.getuid?.();
|
|
1026
|
+
if (platform === "win32" || uid === void 0) return finish(off("unsupported-platform"));
|
|
1027
|
+
const file = readSettingsFile(userSettingsFilePath(env), directory, uid);
|
|
1028
|
+
if (!file.ok) return finish(off(file.reason));
|
|
1029
|
+
const resolved = resolveSettingsFile(file.text, input.hostname ?? os.hostname);
|
|
1030
|
+
if (resolved.settings.mode === "on" && resolved.host !== void 0) log(`tracing on, destination host ${resolved.host}`);
|
|
1031
|
+
return finish(resolved.settings);
|
|
1032
|
+
} catch {
|
|
1033
|
+
return finish(off("settings-error"));
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
670
1036
|
// src/setup.ts
|
|
671
|
-
|
|
1037
|
+
/**
|
|
1038
|
+
* The only way into the tracing chunk. Called only when settings resolved to on, so with tracing off the dynamic import never
|
|
1039
|
+
* runs and the chunk and the SDK inside it are never evaluated. Every step (the import, SDK construction, hook
|
|
1040
|
+
* registration, and later the cleanup) is wrapped: a failure leaves the plugin exactly as it was, and the one log line
|
|
1041
|
+
* names the failure class only, since an error message can carry a path or URL.
|
|
1042
|
+
*/
|
|
1043
|
+
async function startTracing(host, settings, env, log) {
|
|
1044
|
+
try {
|
|
1045
|
+
const handle = await (await import("./tracing-D4bUTAUS.mjs")).startTracing({
|
|
1046
|
+
session: host.session,
|
|
1047
|
+
tool: { hook: async (name, callback) => name === "execute.before" ? host.tool.hook(name, callback) : host.tool.hook(name, callback) },
|
|
1048
|
+
event: host.event
|
|
1049
|
+
}, settings, {
|
|
1050
|
+
env,
|
|
1051
|
+
log
|
|
1052
|
+
});
|
|
1053
|
+
return async () => {
|
|
1054
|
+
try {
|
|
1055
|
+
await handle.stop();
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
log(`tracing cleanup failed (${error instanceof Error ? error.name : "unknown error"})`);
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
log(`tracing could not start (${error instanceof Error ? error.name : "unknown error"})`);
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
async function setupFroBotPlugin(host, options = {}) {
|
|
672
1066
|
await host.tool.hook("execute.before", async (event) => {
|
|
673
1067
|
rejectSubagentReuse(event);
|
|
674
1068
|
});
|
|
@@ -693,6 +1087,14 @@ async function setupFroBotPlugin(host) {
|
|
|
693
1087
|
editor.add(tool);
|
|
694
1088
|
}
|
|
695
1089
|
});
|
|
1090
|
+
const log = options.log ?? logPluginLine;
|
|
1091
|
+
const settings = resolveTracingSettings({
|
|
1092
|
+
env: options.env ?? process.env,
|
|
1093
|
+
directory: host.location.directory,
|
|
1094
|
+
log
|
|
1095
|
+
});
|
|
1096
|
+
if (settings.mode !== "on") return void 0;
|
|
1097
|
+
return startTracing(host, settings, options.env ?? process.env, log);
|
|
696
1098
|
}
|
|
697
1099
|
// src/index.ts
|
|
698
1100
|
/** `@fro.bot/opencode-plugin`: the session tools and the subagent session-reuse guard. */
|