agentrail-edge 0.3.0 → 0.4.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 CHANGED
@@ -17,6 +17,12 @@ wrote, lower-cased, without those your platform or a proxy added on the way
17
17
  (`cf-*`, `x-vercel-*`, `x-forwarded-*` and the like), at most 40 in 512
18
18
  characters.
19
19
 
20
+ A request from a CLI that uses
21
+ [`agentrail-cli-sdk`](https://www.npmjs.com/package/agentrail-cli-sdk) says
22
+ which command an agent ran, in which run and session. The edge records it
23
+ on the request's step, with the session as a keyed hash in place of the
24
+ visit cookie a CLI never sends back, and passes the header on to your origin.
25
+
20
26
  ## Install
21
27
 
22
28
  ```sh
package/dist/edge.d.ts CHANGED
@@ -2,8 +2,9 @@ import { type PrincipalKind } from 'agentrail-core';
2
2
  import { type MarkedHosts } from './links.js';
3
3
  /**
4
4
  * The header names no caller writes: what a proxy or the platform added on
5
- * the way (Cloudflare's `cf-*`, Vercel's `x-vercel-*`, `x-forwarded-*`), and
6
- * Agentrail's own first-party mark. The contract's
5
+ * the way (Cloudflare's `cf-*`, Vercel's `x-vercel-*`, and the `logs-url` and
6
+ * `x-invocation-id` Vercel adds on the way to a function, `x-forwarded-*`),
7
+ * and Agentrail's own first-party mark. The contract's
7
8
  * (`otlp.headerNames.addedOnTheWay`); the tests hold this copy to it.
8
9
  */
9
10
  export declare const ADDED_ON_THE_WAY: RegExp;
package/dist/edge.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createCollector, TRACE_HANDOFF_HEADER, signTraceHandoff, traceHandoffTarget, createSignatureVerifier, formatTraceparent, hashKeyIdOf, keyedHash, newSessionId, newSpanId, newTraceId, parseMarker, parseTraceparent, sessionCookie, VERSION, } from 'agentrail-core';
1
+ import { CLI_HEADER, createCollector, readCliHeader, TRACE_HANDOFF_HEADER, signTraceHandoff, traceHandoffTarget, createSignatureVerifier, formatTraceparent, hashKeyIdOf, keyedHash, newSessionId, newSpanId, newTraceId, parseMarker, parseTraceparent, sessionCookie, VERSION, } from 'agentrail-core';
2
2
  import { MARKER_PARAMETER, markLinks } from './links.js';
3
3
  /**
4
4
  * The edge adapter: in front of a customer's site, on the Fetch API, it
@@ -37,17 +37,20 @@ const MARKABLE_BYTES = 2 * 1024 * 1024;
37
37
  * `identify`.
38
38
  */
39
39
  const SEND_WITHIN_MS = 25_000;
40
+ /** A CLI's session ends with its UTC day, as the collector keys an MCP session's. */
41
+ const DAY_MS = 24 * 60 * 60 * 1000;
40
42
  /** The longest `Accept` the collector takes; a longer one is cut. */
41
43
  const ACCEPT_LENGTH = 256;
42
44
  /** The longest `Accept-Encoding` the collector takes; a longer one is cut. */
43
45
  const ACCEPT_ENCODING_LENGTH = 128;
44
46
  /**
45
47
  * The header names no caller writes: what a proxy or the platform added on
46
- * the way (Cloudflare's `cf-*`, Vercel's `x-vercel-*`, `x-forwarded-*`), and
47
- * Agentrail's own first-party mark. The contract's
48
+ * the way (Cloudflare's `cf-*`, Vercel's `x-vercel-*`, and the `logs-url` and
49
+ * `x-invocation-id` Vercel adds on the way to a function, `x-forwarded-*`),
50
+ * and Agentrail's own first-party mark. The contract's
48
51
  * (`otlp.headerNames.addedOnTheWay`); the tests hold this copy to it.
49
52
  */
50
- export const ADDED_ON_THE_WAY = /^(?:x-forwarded-.*|x-amzn-.*|x-vercel-.*|cf-.*|forwarded|via|x-real-ip|cdn-loop|true-client-ip|agentrail-first-party)$/iu;
53
+ export const ADDED_ON_THE_WAY = /^(?:x-forwarded-.*|x-amzn-.*|x-vercel-.*|cf-.*|forwarded|via|x-real-ip|cdn-loop|true-client-ip|logs-url|x-invocation-id|agentrail-first-party)$/iu;
51
54
  /** The most header names an event keeps, and the most characters they take joined (`otlp.headerNames`). */
52
55
  export const HEADER_NAMES_KEPT = { count: 40, characters: 512 };
53
56
  /** The longest `Sec-Fetch-*` value the collector takes; the browsers' are one word. */
@@ -282,6 +285,35 @@ export function createEdge(options) {
282
285
  headers,
283
286
  });
284
287
  }
288
+ /**
289
+ * What a customer's CLI said of itself (`x-agentrail-cli`, written by
290
+ * agentrail-cli-sdk), and the session it named: the keyed hash of its
291
+ * session and the UTC day the request started, in place of the visit
292
+ * cookie a CLI never sends back. Null for a request with no header that
293
+ * reads.
294
+ */
295
+ async function cliOf(request, start) {
296
+ const header = readCliHeader(request.headers.get(CLI_HEADER));
297
+ if (header === null)
298
+ return null;
299
+ const hash = await keyedHash(hashKey, header.session);
300
+ return {
301
+ facts: {
302
+ name: header.cli,
303
+ version: header.cliVersion ?? null,
304
+ command: header.command ?? null,
305
+ run: header.run,
306
+ sequence: header.sequence,
307
+ mode: header.mode,
308
+ sessionFrom: header.sessionFrom,
309
+ agent: header.agent ?? null,
310
+ agentVersion: header.agentVersion ?? null,
311
+ evidence: header.evidence ?? [],
312
+ sdkVersion: header.sdkVersion,
313
+ },
314
+ session: `cli:${hash}:${String(Math.floor(start / DAY_MS))}`,
315
+ };
316
+ }
285
317
  async function eventOf(input) {
286
318
  const { request, url, observed } = input;
287
319
  // With its port, as a link to it is recorded.
@@ -291,6 +323,7 @@ export function createEdge(options) {
291
323
  const principal = await principalOf(request);
292
324
  const actingUserId = principal?.actingUserId ?? null;
293
325
  const signature = await verify(request);
326
+ const cli = await cliOf(request, input.start);
294
327
  const { signals } = input;
295
328
  return {
296
329
  schemaVersion: 1,
@@ -314,7 +347,7 @@ export function createEdge(options) {
314
347
  subtreeSpanIds: [],
315
348
  links: input.caller === null ? [] : [input.caller],
316
349
  },
317
- session: { id: input.session, previousId: null },
350
+ session: { id: cli?.session ?? input.session, previousId: null },
318
351
  http: {
319
352
  method: request.method,
320
353
  host,
@@ -370,6 +403,7 @@ export function createEdge(options) {
370
403
  : await keyedHash(hashKey, actingUserId),
371
404
  },
372
405
  registration: null,
406
+ ...(cli === null ? {} : { cli: cli.facts }),
373
407
  markers: {
374
408
  served: observed.served === null
375
409
  ? []
@@ -383,6 +417,9 @@ export function createEdge(options) {
383
417
  ...(principal === null
384
418
  ? []
385
419
  : [{ field: 'principal.id', reason: 'keyed_hash' }]),
420
+ ...(cli === null
421
+ ? []
422
+ : [{ field: 'session.id', reason: 'keyed_hash' }]),
386
423
  ...(dropped
387
424
  ? [{ field: 'http.path', reason: 'query_outside_allowlist' }]
388
425
  : []),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentrail-edge",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "The Agentrail edge: in front of a site on Cloudflare Workers, Vercel or any Fetch API runtime, it records each request and marks the links agents follow.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -25,7 +25,7 @@
25
25
  "node": ">=22"
26
26
  },
27
27
  "dependencies": {
28
- "agentrail-core": "0.3.0"
28
+ "agentrail-core": "0.4.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/node": "24.13.3",