Skip to content

Commit 80da728

Browse files
authored
Merge pull request #39 from browser-use/feat/laminar-parent-span-context-env
feat(bcode-laminar): wire LMNR_PARENT_SPAN_CONTEXT env (re-land plugin on main)
2 parents 1b55206 + fc6e3b6 commit 80da728

18 files changed

Lines changed: 863 additions & 7 deletions

File tree

bun.lock

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dev:console": "ulimit -n 10240 2>/dev/null; bun run --cwd packages/console/app dev",
1313
"dev:storybook": "bun --cwd packages/storybook storybook",
1414
"lint": "oxlint",
15-
"typecheck": "bun turbo typecheck --filter='@browser-use/browsercode-core...' --filter='@browser-use/bcode-browser'",
15+
"typecheck": "bun turbo typecheck --filter='@browser-use/browsercode-core...' --filter='@browser-use/bcode-browser' --filter='@browser-use/bcode-laminar'",
1616
"postinstall": "bun run --cwd packages/opencode fix-node-pty",
1717
"prepare": "husky",
1818
"random": "echo 'Random script'",

packages/bcode-browser/src/telemetry.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414
// wins; explicit empty string is respected as "no key please"), AND
1515
// - the embedded default is non-empty.
1616
//
17-
// `applyTelemetryKey()` is invoked as a side effect on module import (last
18-
// statement of this file). Because `packages/opencode/src/index.ts` imports
19-
// this module before any other module that might consume the env var, the
20-
// gate is guaranteed to run before any downstream module-load code can
21-
// observe `LMNR_PROJECT_API_KEY` — sidestepping ESM static-import hoisting
22-
// entirely.
17+
// No `if (telemetryEnabled)` branches downstream — `@browser-use/bcode-laminar`
18+
// reads `LMNR_PROJECT_API_KEY` and initializes only when present, so the gate
19+
// here decides everything.
20+
//
21+
// `applyTelemetryKey()` is invoked as a side effect on module import (bottom
22+
// of this file). `packages/opencode/src/index.ts` imports this module before
23+
// any other import; ESM evaluates an imported module's side effects before
24+
// continuing to the next import, so the gate is unambiguously ordered before
25+
// any downstream module-load code that might read `LMNR_PROJECT_API_KEY` —
26+
// sidestepping the static-import hoisting concern.
27+
28+
import { mkdirSync } from "fs"
29+
import { homedir } from "os"
30+
import { dirname, join } from "path"
2331

2432
declare const BCODE_DEFAULT_LMNR_KEY: string
2533

@@ -34,6 +42,35 @@ export const applyTelemetryKey = () => {
3442
// undeclared and a direct read throws ReferenceError.
3543
if (typeof BCODE_DEFAULT_LMNR_KEY === "undefined" || !BCODE_DEFAULT_LMNR_KEY) return
3644
process.env.LMNR_PROJECT_API_KEY = BCODE_DEFAULT_LMNR_KEY
45+
showFirstRunNoticeOnce()
46+
}
47+
48+
// Industry-standard short notice: one line stating what happens and how to
49+
// opt out. Prints once; subsequent launches stay quiet.
50+
//
51+
// Uses XDG_STATE_HOME / ~/.local/state on Linux, %LOCALAPPDATA% on Windows,
52+
// ~/Library/Application Support on macOS. Resolved without importing
53+
// @opencode-ai/core/global since this runs before that module loads.
54+
const showFirstRunNoticeOnce = () => {
55+
const marker = join(stateDir(), "bcode", "telemetry-notice-shown")
56+
if (Bun.file(marker).size > 0) return
57+
process.stderr.write(
58+
"BrowserCode sends anonymous usage traces. Set DO_NOT_TRACK=1 to opt out.\n",
59+
)
60+
try {
61+
mkdirSync(dirname(marker), { recursive: true })
62+
} catch {}
63+
Bun.write(marker, "1\n").catch(() => {})
64+
}
65+
66+
const stateDir = () => {
67+
if (process.platform === "win32") {
68+
return process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local")
69+
}
70+
if (process.platform === "darwin") {
71+
return join(homedir(), "Library", "Application Support")
72+
}
73+
return process.env.XDG_STATE_HOME ?? join(homedir(), ".local", "state")
3774
}
3875

3976
// Run as an import side effect: this module is imported as the very first

packages/bcode-laminar/LICENSE

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
3+
Apache License
4+
Version 2.0, January 2004
5+
http://www.apache.org/licenses/
6+
7+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8+
9+
1. Definitions.
10+
11+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
12+
13+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
14+
15+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
16+
17+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
18+
19+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
20+
21+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
22+
23+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
24+
25+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
26+
27+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
28+
29+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
30+
31+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
32+
33+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
34+
35+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
36+
37+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
38+
You must cause any modified files to carry prominent notices stating that You changed the files; and
39+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
41+
42+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
43+
44+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
45+
46+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
47+
48+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
49+
50+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
51+
52+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
53+
54+
END OF TERMS AND CONDITIONS
55+
56+
57+
APPENDIX: How to apply the Apache License to your work
58+
59+
Include a copy of the Apache License, typically in a file called LICENSE, in your work, and consider also including a NOTICE file that references the License.
60+
61+
To apply the Apache License to specific files in your work, attach the following boilerplate declaration, replacing the fields enclosed by brackets "[]" with your own identifying information. (Don't include the brackets!) Enclose the text in the appropriate comment syntax for the file format. We also recommend that you include a file or class name and description of purpose on the same "printed page" as the copyright notice for easier identification within third-party archives.
62+
63+
Copyright 2024 LMNR AI, Inc.
64+
65+
Licensed under the Apache License, Version 2.0 (the "License");
66+
you may not use this file except in compliance with the License.
67+
You may obtain a copy of the License at
68+
69+
http://www.apache.org/licenses/LICENSE-2.0
70+
71+
Unless required by applicable law or agreed to in writing, software
72+
distributed under the License is distributed on an "AS IS" BASIS,
73+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74+
See the License for the specific language governing permissions and
75+
limitations under the License.

packages/bcode-laminar/VENDOR.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Vendor record — @browser-use/bcode-laminar
2+
3+
This package is a vendored, trimmed combination of two upstream sources, both
4+
licensed Apache-2.0. Pulled once on **2026-05-02**; we do not maintain a
5+
recurring sync — Laminar's plugin shape changes rarely, and the OpenTelemetry
6+
contract is stable. Re-pull only when a behavior fix or new attribute is
7+
worth chasing.
8+
9+
## Sources
10+
11+
| Upstream | Commit | License |
12+
|---|---|---|
13+
| [`lmnr-ai/lmnr-opencode-plugin`](https://github.com/lmnr-ai/lmnr-opencode-plugin) | `bb2fceaff0d2b52161fb1c8a477a50b0b4789a0e` (v0.1.2) | Apache-2.0 |
14+
| [`lmnr-ai/lmnr-ts`](https://github.com/lmnr-ai/lmnr-ts) (selected files from `packages/lmnr/src/opentelemetry-lib/`) | `5ebe07a6284ce0bbfebcf10bdd9d1faa04ed6c64` (v0.8.22) | Apache-2.0 |
15+
16+
## File mapping
17+
18+
| `src/<file>.ts` | Upstream origin |
19+
|---|---|
20+
| `plugin.ts` | `lmnr-opencode-plugin/src/index.ts` |
21+
| `processor.ts` | merged: `lmnr-opencode-plugin/src/processor.ts` + base class from `lmnr-ts/.../tracing/processor.ts` |
22+
| `exporter.ts` | `lmnr-ts/.../tracing/exporter.ts` |
23+
| `span.ts` | `Laminar.startSpan` extracted from `lmnr-ts/src/laminar.ts` |
24+
| `attributes.ts` | `lmnr-ts/.../tracing/attributes.ts` (subset) |
25+
| `compat.ts` | `lmnr-ts/.../tracing/compat.ts` |
26+
| `state.ts` | `lmnr-opencode-plugin/src/state.ts` (dropped `sessionExternalContexts` — TS host injection isn't relevant for bcode) |
27+
| `utils.ts` | UUID helpers from `lmnr-ts/src/utils.ts` |
28+
29+
## Behavior trims (vs upstream)
30+
31+
- **No `LaminarClient` "rollout sessions"**`LMNR_ROLLOUT_SESSION_ID` branch removed. We don't use that feature.
32+
- **No HTTP/protobuf exporter fallback** — gRPC only. Drops `@opentelemetry/exporter-trace-otlp-proto`.
33+
- **No `parseOtelHeaders` / `OTEL_HEADERS` resolution** — we always have a Laminar API key when emitting; OTel-env paths are dead.
34+
- **No `pino` logger** — log via `client.app.log` (opencode-managed).
35+
- **No `loadEnv()`** — opencode loads `.env` already; second pass would surprise users.
36+
- **No caller-side context injection (`sessionExternalContexts`)** — bcode runs the agent locally, not driven by an external TS host.
37+
- **`Laminar.startSpan` reduced** — only the path needed for the per-turn span (sessionId + optional parentSpanContext); no `tracingLevel`, masked-input, or process-global activation stack.
38+
39+
## Behavior preserved
40+
41+
- `lmnr.span.path` / `lmnr.span.ids_path` ancestor stamping (Laminar UI nests by these, not by OTel parentSpanId).
42+
- OTel SDK v1/v2 compat shim (`makeSpanOtelV2Compatible`).
43+
- Per-`chat.message` "turn" span lifecycle, ended on `session.idle` / `session.deleted`.
44+
- Sub-agent tagging: spans created inside the `task` tool tag descendants with `lmnr.spawning_subagent.tool_use_id`.
45+
- gRPC bearer-token auth to `${LMNR_BASE_URL}:${LMNR_GRPC_PORT}`.
46+
47+
## Maintenance protocol
48+
49+
If/when we re-pull:
50+
51+
1. Clone both upstreams at the new tag.
52+
2. Diff each vendored file against its origin; bring across only behavior changes (not stylistic / build / dep churn).
53+
3. Update the commit hashes above; bump `version` in `package.json` only if behavior changed.
54+
4. Re-verify the smoke flow (see PR description).
55+
56+
Do not introduce a `dependencies` entry on `@lmnr-ai/lmnr` or `@lmnr-ai/opencode-plugin` — vendoring is the point.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package.json",
3+
"version": "0.0.0",
4+
"name": "@browser-use/bcode-laminar",
5+
"description": "Vendored Laminar OpenCode tracing plugin (Apache-2.0)",
6+
"type": "module",
7+
"license": "Apache-2.0",
8+
"private": true,
9+
"scripts": {
10+
"typecheck": "tsgo --noEmit"
11+
},
12+
"exports": {
13+
".": "./src/index.ts",
14+
"./*": "./src/*.ts"
15+
},
16+
"dependencies": {
17+
"@opencode-ai/plugin": "workspace:*",
18+
"@opentelemetry/api": "1.9.0",
19+
"@opentelemetry/sdk-node": "0.214.0",
20+
"@opentelemetry/sdk-trace-base": "2.6.1",
21+
"@opentelemetry/core": "2.6.1",
22+
"@opentelemetry/exporter-trace-otlp-grpc": "0.214.0",
23+
"@grpc/grpc-js": "1.14.3"
24+
},
25+
"devDependencies": {
26+
"@tsconfig/bun": "catalog:",
27+
"@types/bun": "catalog:"
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Vendored from lmnr-ts/packages/lmnr/src/opentelemetry-lib/tracing/attributes.ts
2+
// Trimmed to the constants the OpenCode plugin actually uses.
3+
4+
export const SPAN_TYPE = "lmnr.span.type"
5+
export const SPAN_INPUT = "lmnr.span.input"
6+
export const SPAN_PATH = "lmnr.span.path"
7+
export const SPAN_IDS_PATH = "lmnr.span.ids_path"
8+
export const PARENT_SPAN_PATH = "lmnr.span.parent_path"
9+
export const PARENT_SPAN_IDS_PATH = "lmnr.span.parent_ids_path"
10+
export const SPAN_INSTRUMENTATION_SOURCE = "lmnr.span.instrumentation_source"
11+
export const SPAN_SDK_VERSION = "lmnr.span.sdk_version"
12+
export const ASSOCIATION_PROPERTIES = "lmnr.association.properties"
13+
export const SESSION_ID = "lmnr.association.properties.session_id"

0 commit comments

Comments
 (0)