@fro.bot/opencode-plugin 0.3.0 → 0.3.2

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 CHANGED
@@ -42,9 +42,11 @@ To turn it on, create `~/.config/fro-bot/opencode-plugin/tracing.json` (under `$
42
42
 
43
43
  Every span carries `langfuse.session.id` set to the **root** session's id, so Langfuse's Sessions view groups a subagent's spans with its parent's, while the standard `session.id` stays each span's own session. In the Action, each span also carries the run's repository, run id, attempt, job and trigger as `langfuse.trace.metadata.*` (`repository`, `run_id`, `run_attempt`, `job`, `trigger`), so Langfuse shows and filters by them; a local session has none of these.
44
44
 
45
+ The root span of every trace (a turn's `invoke_agent`, and the root of a late continuation trace) also carries `langfuse.trace.name` set to the fixed string `fro-bot run`, which Langfuse shows as the trace's name. No other span has it, and it holds no content.
46
+
45
47
  **Joining a turn to its Action run.** The Action's launcher document (`OPENCODE_FROBOT_TRACING`) may carry three optional fields. The plugin accepts the document with or without them, refuses any other key, and turns tracing off, with a fixed reason and no value in the log line (`launcher-run-invalid`), when a field it carries is malformed:
46
48
 
47
- - `sessionId`: the telemetry session the Action computed, `<owner>/<repo>:<logical key>`: 1 to 200 characters, no control characters. The plugin sets `langfuse.session.id` to it **verbatim** on every span, and never derives or alters it. Every exported string is scrubbed, so a session id the scrub would change (a repository named `password-manager` reads as a name and a value) is never exported redacted, which would merge conversations into one session: that span falls back to the OpenCode root session instead. The Action avoids the fallback by passing the id through `scrubSafeSessionId` (`@fro-bot/runtime/tracing/session-id`) first, which returns the id as is when the scrub leaves it alone, else `sha256:` and 32 hex characters of its SHA-256, else none. Without it, and in every local session, `langfuse.session.id` stays the OpenCode root session as described above. `session.id` and `gen_ai.conversation.id` keep each span's own OpenCode session either way.
49
+ - `sessionId`: the telemetry session the Action computed, `<owner>/<repo>:<logical key>`: 1 to 200 characters, no control characters. The plugin sets `langfuse.session.id` to it **verbatim** on every span, and never derives or alters it. Every exported string is scrubbed, so a session id the scrub would change or withhold (a repository named `password-manager` reads as a name and a value) is never exported redacted, which would merge conversations into one session: that span falls back to the OpenCode root session instead. The Action avoids the fallback by passing the id through `scrubSafeSessionId` (`@fro-bot/runtime/tracing/session-id`) first, which returns the id as is when the scrub leaves it alone, else `sha256:` and 32 hex characters of its SHA-256, else none. Without it, and in every local session, `langfuse.session.id` stays the OpenCode root session as described above. **Upgrading:** turns exported before the Action sent a `sessionId` stay in Langfuse under the OpenCode root session id, and new Action turns use the qualified conversation key, so one conversation shows as two sessions across the upgrade. Every span of both kinds carries `fro_bot.root_session.id` (the OpenCode root session id, whatever `langfuse.session.id` is), so a dashboard or a query can still join the old and the new turns of a conversation on it. `session.id` and `gen_ai.conversation.id` keep each span's own OpenCode session either way.
48
50
  - `invocationId`: a 1 to 200 character id for the Action step that started the server, with no control characters. Every span carries it as `fro_bot.invocation.id`, mirrored to `langfuse.trace.metadata.invocation_id`. It tells apart two steps of one job, which share `run_id`, `run_attempt` and `job`.
49
51
  - `rootSpan`: `{"traceId": "<32 hex>", "spanId": "<16 hex>"}`, the run's root span. Lower-case hex only, neither id all zero, no other key. Each turn's root span (the `invoke_agent` span of the turn's root session, standalone compaction turns included) carries one OpenTelemetry **link** to it and no link attributes. Tempo follows links; Langfuse ignores them. The turn stays a trace of its own: nothing is re-parented.
50
52
 
package/dist/index.mjs CHANGED
@@ -1092,7 +1092,7 @@ function resolveTracingSettings(input) {
1092
1092
  */
1093
1093
  async function startTracing(host, settings, env, log) {
1094
1094
  try {
1095
- const handle = await (await import("./tracing-DMDmxAqE.mjs")).startTracing({
1095
+ const handle = await (await import("./tracing-CHrTlTyx.mjs")).startTracing({
1096
1096
  session: host.session,
1097
1097
  tool: { hook: async (name, callback) => name === "execute.before" ? host.tool.hook(name, callback) : host.tool.hook(name, callback) },
1098
1098
  event: host.event
@@ -3834,8 +3834,6 @@ const SCRUB_PATTERNS = [
3834
3834
  ];
3835
3835
  /** Env names whose value the literal pass removes. `OPENCODE_PASSWORD` is the server's own credential. */
3836
3836
  const SECRET_ENV_NAME = /SECRET|TOKEN|PASSWORD|PASSWD|PASSPHRASE|CREDENTIAL|API_?KEY|PRIVATE_?KEY|ACCESS_?KEY|AUTHORIZATION/i;
3837
- const MIN_LITERAL_CHARS = 8;
3838
- const MAX_LITERAL_CHARS = 4096;
3839
3837
  const MAX_LITERALS = 128;
3840
3838
  /**
3841
3839
  * The forms of one secret to remove: the value, and its JSON-escaped text when that differs. None for a value outside the
@@ -3843,9 +3841,9 @@ const MAX_LITERALS = 128;
3843
3841
  * secret anyone typed.
3844
3842
  */
3845
3843
  function literalForms(value) {
3846
- if (value === void 0 || value.length < MIN_LITERAL_CHARS || value.length > MAX_LITERAL_CHARS) return [];
3844
+ if (value === void 0 || value.length < 8 || value.length > 65536) return [];
3847
3845
  const escaped = JSON.stringify(value).slice(1, -1);
3848
- return escaped !== value && escaped.length <= MAX_LITERAL_CHARS * 2 ? [value, escaped] : [value];
3846
+ return escaped !== value && escaped.length <= 131072 ? [value, escaped] : [value];
3849
3847
  }
3850
3848
  /**
3851
3849
  * The literal secrets to remove: from the environment once, raw and JSON-escaped, longest first and at most `MAX_LITERALS`.
@@ -3862,14 +3860,74 @@ function collectLiterals(env, extra = []) {
3862
3860
  return [...found].toSorted((a, b) => b.length - a.length).slice(0, MAX_LITERALS);
3863
3861
  }
3864
3862
  /**
3863
+ * Literals longer than this are searched with a search that cannot go quadratic. `String.prototype.indexOf` is fast (native,
3864
+ * and skips ahead on a mismatch) but not linear in the worst case: V8 takes seconds to search a megabyte for a 64 KiB literal
3865
+ * when each alignment matches almost to the end. Short literals are bounded by their own length, so they stay native.
3866
+ */
3867
+ const NATIVE_SEARCH_MAX_CHARS = 64;
3868
+ /** Where a literal can resume after a partial match: `table[i]` is the length of the longest proper border of `literal[0..i]`. */
3869
+ function failureTable(literal) {
3870
+ const table = new Int32Array(literal.length);
3871
+ for (let index = 1, matched = 0; index < literal.length; index += 1) {
3872
+ while (matched > 0 && literal.charCodeAt(index) !== literal.charCodeAt(matched)) matched = table[matched - 1] ?? 0;
3873
+ if (literal.charCodeAt(index) === literal.charCodeAt(matched)) matched += 1;
3874
+ table[index] = matched;
3875
+ }
3876
+ return table;
3877
+ }
3878
+ function createLiteralSearch() {
3879
+ const tables = /* @__PURE__ */ new Map();
3880
+ const tableOf = (literal) => {
3881
+ let table = tables.get(literal);
3882
+ if (table === void 0) {
3883
+ table = failureTable(literal);
3884
+ tables.set(literal, table);
3885
+ }
3886
+ return table;
3887
+ };
3888
+ const scan = (text, literal, found) => {
3889
+ const table = tableOf(literal);
3890
+ for (let index = 0, matched = 0; index < text.length; index += 1) {
3891
+ const code = text.charCodeAt(index);
3892
+ while (matched > 0 && code !== literal.charCodeAt(matched)) matched = table[matched - 1] ?? 0;
3893
+ if (code === literal.charCodeAt(matched)) matched += 1;
3894
+ if (matched === literal.length) {
3895
+ if (found(index + 1)) return;
3896
+ matched = table[matched - 1] ?? 0;
3897
+ }
3898
+ }
3899
+ };
3900
+ return {
3901
+ each: (text, literal, found) => {
3902
+ if (literal.length <= NATIVE_SEARCH_MAX_CHARS) {
3903
+ for (let at = text.indexOf(literal); at !== -1; at = text.indexOf(literal, at + 1)) found(at, at + literal.length);
3904
+ return;
3905
+ }
3906
+ scan(text, literal, (end) => {
3907
+ found(end - literal.length, end);
3908
+ return false;
3909
+ });
3910
+ },
3911
+ has: (text, literal) => {
3912
+ if (literal.length <= NATIVE_SEARCH_MAX_CHARS) return text.includes(literal);
3913
+ let seen = false;
3914
+ scan(text, literal, () => {
3915
+ seen = true;
3916
+ return true;
3917
+ });
3918
+ return seen;
3919
+ }
3920
+ };
3921
+ }
3922
+ /**
3865
3923
  * The environment's literal secrets as a matcher: every occurrence of each, overlapping ones included (the union covers a
3866
3924
  * secret that overlaps itself or another). The literals were collected once at start; they are never named.
3867
3925
  */
3868
- function literalMatcher(literals) {
3926
+ function literalMatcher(literals, search = createLiteralSearch()) {
3869
3927
  return {
3870
3928
  id: "environment-literal",
3871
3929
  find: (text, _lower, emit) => {
3872
- for (const literal of literals) for (let at = text.indexOf(literal); at !== -1; at = text.indexOf(literal, at + 1)) emit(at, at + literal.length);
3930
+ for (const literal of literals) search.each(text, literal, emit);
3873
3931
  }
3874
3932
  };
3875
3933
  }
@@ -3946,7 +4004,8 @@ function createScrubber(options = {}) {
3946
4004
  const ceiling = options.limits?.inputCeilingChars ?? DEFAULT_CONTENT_LIMITS.inputCeilingChars;
3947
4005
  const literals = [...collectLiterals(options.env ?? {}, options.extraSecrets)];
3948
4006
  const known = new Set(literals);
3949
- const matchers = [...options.patterns ?? SCRUB_PATTERNS, literalMatcher(literals)];
4007
+ const search = createLiteralSearch();
4008
+ const matchers = [...options.patterns ?? SCRUB_PATTERNS, literalMatcher(literals, search)];
3950
4009
  let survivals = 0;
3951
4010
  return {
3952
4011
  addLiterals: (values) => {
@@ -3958,7 +4017,7 @@ function createScrubber(options = {}) {
3958
4017
  },
3959
4018
  inputCeilingChars: ceiling,
3960
4019
  survived: (text) => {
3961
- for (const literal of literals) if (text.includes(literal)) {
4020
+ for (const literal of literals) if (search.has(text, literal)) {
3962
4021
  survivals += 1;
3963
4022
  return true;
3964
4023
  }
@@ -5674,6 +5733,40 @@ function toPendingSpan(record, options = {}) {
5674
5733
  case "invoke_agent": return invokeSpan(record, options);
5675
5734
  }
5676
5735
  }
5736
+ const SESSION_ATTRIBUTE = "langfuse.session.id";
5737
+ /**
5738
+ * The trace's name in Langfuse, whose OTLP ingestion reads it from this attribute on the trace's root span (the span names
5739
+ * `invoke_agent <agent>` name observations, not the trace). One fixed string for every trace, so it holds no content.
5740
+ */
5741
+ const TRACE_NAME_ATTRIBUTE = "langfuse.trace.name";
5742
+ const TRACE_NAME = "fro-bot run";
5743
+ /**
5744
+ * Names the trace on a span that is its root, and on nothing else. A span with no parent is the root: the turn model opens
5745
+ * every trace with a parentless `invoke_agent` (a continuation of a closed trace included) and gives every other span a parent.
5746
+ *
5747
+ * Added after the scrub, never through it: the string is fixed text, and a scrubber that rewrote it (a registered value that
5748
+ * happens to match a word of it) would split one name into several in Langfuse. A span that is not a root is returned as is.
5749
+ */
5750
+ function withTraceName(span) {
5751
+ if (span.parentSpanId !== void 0) return span;
5752
+ return {
5753
+ ...span,
5754
+ attributes: {
5755
+ ...span.attributes,
5756
+ [TRACE_NAME_ATTRIBUTE]: TRACE_NAME
5757
+ }
5758
+ };
5759
+ }
5760
+ /**
5761
+ * Whether the span carries the launcher's session id exactly as given. Equal text is not enough: a value the scrubber withheld
5762
+ * is exported as the fixed marker `[omitted]`, so an id that is literally `[omitted]` would equal its own withheld form. The
5763
+ * omission status the scrub wrote (`<attribute>:<reason>`) is what says whether it was withheld.
5764
+ */
5765
+ function sessionIdExportedAsGiven(span, sessionId) {
5766
+ if (span.attributes[SESSION_ATTRIBUTE] !== sessionId) return false;
5767
+ const omitted = span.attributes[MARKER.omitted];
5768
+ return !(Array.isArray(omitted) && omitted.some((entry) => typeof entry === "string" && entry.startsWith(`${SESSION_ATTRIBUTE}:`)));
5769
+ }
5677
5770
  /**
5678
5771
  * Record to an exportable span: attributes, then the one scrubbing pass.
5679
5772
  *
@@ -5684,11 +5777,11 @@ function toPendingSpan(record, options = {}) {
5684
5777
  */
5685
5778
  function toExportSpan(record, options, scrubber, limits) {
5686
5779
  const span = scrubSpan(toPendingSpan(record, options), scrubber, limits);
5687
- if (options.sessionId === void 0 || span.attributes["langfuse.session.id"] === options.sessionId) return span;
5688
- return scrubSpan(toPendingSpan(record, {
5780
+ if (options.sessionId === void 0 || sessionIdExportedAsGiven(span, options.sessionId)) return withTraceName(span);
5781
+ return withTraceName(scrubSpan(toPendingSpan(record, {
5689
5782
  ...options,
5690
5783
  sessionId: void 0
5691
- }), scrubber, limits);
5784
+ }), scrubber, limits));
5692
5785
  }
5693
5786
  // src/tracing/turn-model.ts
5694
5787
  const DEFAULT_LIMITS = {
@@ -7526,19 +7619,26 @@ function observeHook(name, payload, at, snapshot = {}) {
7526
7619
  function toolErrorType(type, message) {
7527
7620
  return message === "Session reuse is disabled in this environment. Start a new task and include the context it needs." ? "plugin.reuse-guard" : type;
7528
7621
  }
7529
- /** How far into a report's text the `<subagent …>` header is looked for. */
7622
+ /** How far into a report's text the subagent header is looked for. */
7530
7623
  const REPORT_HEADER_WINDOW = 400;
7531
7624
  /**
7532
- * The state a subagent report carries. The report's own header is the truth (`<subagent … state="running" …>`): the
7533
- * item's `metadata.state` says `completed` even on the interim report a parent gets while the child's own background work
7534
- * still runs. Reads only the head of the text, by `indexOf`, so cost does not grow with a report's size.
7625
+ * The two header forms a subagent report opens with. OpenCode 2.3.2 writes `<subagent sessionID=… state=… …>`; 2.4.1
7626
+ * (upstream #64971) writes `<background-result kind="subagent" sessionID=… state=… …>`. A `<background-result>` of any
7627
+ * other kind (`shell`, `image`, `codemode`) is not a subagent report.
7628
+ */
7629
+ const REPORT_HEADER_PREFIXES = ["<subagent ", "<background-result kind=\"subagent\" "];
7630
+ /**
7631
+ * The state a subagent report carries. The report's own header is the truth (`<subagent … state="running" …>` on 2.3.2,
7632
+ * `<background-result kind="subagent" … state="running" …>` on 2.4.1): the item's `metadata.state` says `completed` even
7633
+ * on the interim report a parent gets while the child's own background work still runs. Reads only the head of the text,
7634
+ * by `indexOf`, so cost does not grow with a report's size.
7535
7635
  */
7536
7636
  function reportStateOf(text, metadata) {
7537
7637
  if (typeof text === "string") {
7538
7638
  const head = text.slice(0, REPORT_HEADER_WINDOW);
7539
7639
  const close = head.indexOf(">");
7540
7640
  const marker = head.indexOf(" state=\"");
7541
- if (head.startsWith("<subagent ") && marker !== -1 && (close === -1 || marker < close)) {
7641
+ if (REPORT_HEADER_PREFIXES.some((prefix) => head.startsWith(prefix)) && marker !== -1 && (close === -1 || marker < close)) {
7542
7642
  const from = marker + 8;
7543
7643
  const to = head.indexOf("\"", from);
7544
7644
  if (to !== -1) return head.slice(from, to);
@@ -7877,4 +7977,4 @@ async function startTracing(host, settings, options) {
7877
7977
  }
7878
7978
  export { startTracing };
7879
7979
 
7880
- //# sourceMappingURL=tracing-DMDmxAqE.mjs.map
7980
+ //# sourceMappingURL=tracing-CHrTlTyx.mjs.map