@imdeadpool/guardex 8.4.10 → 8.4.11

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.
@@ -2,13 +2,20 @@
2
2
 
3
3
  const { runFromBin } = require('../src/cli/main');
4
4
  const args = process.argv.slice(2);
5
- const cleanup =
6
- args[0] === 'branch' && args[1] === 'finish'
7
- ? require('../src/finish/post-branch-finish-cleanup')
8
- : null;
5
+ const branchFinish = args[0] === 'branch' && args[1] === 'finish';
6
+ const cleanup = branchFinish ? require('../src/finish/post-branch-finish-cleanup') : null;
9
7
  const finishCleanup = cleanup?.prepareBranchFinishCleanup(args, process.cwd());
8
+ const analytics =
9
+ process.env.OPENCUE_RUN_ANALYTICS === '1' &&
10
+ process.env.DO_NOT_TRACK !== '1' &&
11
+ (branchFinish || args[0] === 'ship' || (args[0] === 'finish' && args[1] !== 'events')) &&
12
+ !args.some((arg) => arg === '--help' || arg === '-h' || arg === '--dry-run')
13
+ ? require('../src/run-analytics.cjs')
14
+ : null;
15
+ const start = analytics ? performance.now() : 0;
10
16
 
11
- void runFromBin().then(() => {
17
+ void runFromBin().then(async () => {
18
+ const durationMs = analytics ? Math.round(performance.now() - start) : 0;
12
19
  if (!process.exitCode && finishCleanup) {
13
20
  process.chdir(finishCleanup.repoRoot);
14
21
  const pending = cleanup.persistFinishedCleanup(finishCleanup);
@@ -16,4 +23,5 @@ void runFromBin().then(() => {
16
23
  cleanup.scheduleFinishedDetachedWorktreeCleanup(pending);
17
24
  }
18
25
  }
26
+ await analytics?.sendRun({ product: 'gitguardex', durationMs, ok: !process.exitCode });
19
27
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imdeadpool/guardex",
3
- "version": "8.4.10",
3
+ "version": "8.4.11",
4
4
  "description": "Guardian T-Rex for your multi-agent repo. Isolated worktrees, file locks, and PR-only merges stop parallel Codex & Claude agents from overwriting each other's work. Auto-wires Oh My Codex, Oh My Claude, OpenSpec, and Caveman.",
5
5
  "license": "MIT",
6
6
  "preferGlobal": true,
@@ -97,5 +97,5 @@
97
97
  "jsonc-parser": "3.3.1",
98
98
  "semver": "7.7.4"
99
99
  },
100
- "gitHead": "b15d3e4583fb463b711a0260377314e4c349964f"
100
+ "gitHead": "fbb056796cb74eb306ada433fa11533d474c30fc"
101
101
  }
@@ -0,0 +1,88 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // OpenCue run analytics v1. Keep this file identical in consumer repositories.
3
+ 'use strict';
4
+
5
+ const { randomUUID } = require('node:crypto');
6
+
7
+ /** @param {Record<string, string | undefined>} [env] */
8
+ function config(env = process.env) {
9
+ if (env.DO_NOT_TRACK === '1' || env.OPENCUE_RUN_ANALYTICS !== '1') return null;
10
+ const key = env.OPENCUE_RUN_ANALYTICS_PROJECT_KEY;
11
+ if (!key || !/^phc_[A-Za-z0-9_-]{5,150}$/.test(key)) return null;
12
+ try {
13
+ const url = new URL(env.OPENCUE_RUN_ANALYTICS_HOST);
14
+ if (url.protocol !== 'https:' || url.username || url.password || url.pathname !== '/') return null;
15
+ if (url.search || url.hash) return null;
16
+ return { key, url: new URL('/i/v0/e/', url).href };
17
+ } catch {
18
+ return null;
19
+ }
20
+ }
21
+
22
+ /**
23
+ * @typedef {Object} Run
24
+ * @property {string} product
25
+ * @property {number} durationMs
26
+ * @property {boolean} ok
27
+ * @property {string} [provider]
28
+ * @property {number} [inputTokens]
29
+ * @property {number} [outputTokens]
30
+ */
31
+
32
+ /** @param {Run} run @param {string} key */
33
+ function envelope(run, key) {
34
+ if (run.product !== 'gitguardex' && run.product !== 'cue') return null;
35
+ if (typeof run.ok !== 'boolean' || !Number.isSafeInteger(run.durationMs) || run.durationMs < 0) {
36
+ return null;
37
+ }
38
+ const properties = {
39
+ schema_version: 1,
40
+ product: run.product,
41
+ operation: run.product === 'gitguardex' ? 'finish' : 'launch',
42
+ outcome: run.ok ? 'success' : 'failure',
43
+ duration_ms: run.durationMs,
44
+ $process_person_profile: false,
45
+ $geoip_disable: true,
46
+ };
47
+ if (run.provider === 'codex' || run.provider === 'claude') properties.provider = run.provider;
48
+ if (Number.isSafeInteger(run.inputTokens) && run.inputTokens >= 0) {
49
+ properties.input_tokens = run.inputTokens;
50
+ }
51
+ if (Number.isSafeInteger(run.outputTokens) && run.outputTokens >= 0) {
52
+ properties.output_tokens = run.outputTokens;
53
+ }
54
+ return {
55
+ api_key: key,
56
+ event: 'opencue_run_completed',
57
+ distinct_id: randomUUID(),
58
+ properties,
59
+ };
60
+ }
61
+
62
+ /**
63
+ * Best-effort, no retries. Call only for a new run, never for local history.
64
+ * @param {Run} run
65
+ * @param {Record<string, string | undefined>} [env]
66
+ * @param {typeof fetch} [request]
67
+ */
68
+ async function sendRun(run, env = process.env, request = globalThis.fetch) {
69
+ const settings = config(env);
70
+ if (!settings) return false;
71
+ try {
72
+ const event = envelope(run, settings.key);
73
+ if (!event) return false;
74
+ const result = await request(settings.url, {
75
+ method: 'POST',
76
+ headers: { 'content-type': 'application/json' },
77
+ body: JSON.stringify(event),
78
+ redirect: 'error',
79
+ signal: AbortSignal.timeout(650),
80
+ });
81
+ if (result.body) await result.body.cancel();
82
+ return result.ok;
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ module.exports = { config, sendRun };