@reticlehq/core 3.7.0 → 3.8.1

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.
Files changed (138) hide show
  1. package/dist/activation-entry.d.ts +9 -0
  2. package/dist/activation-entry.js +9 -0
  3. package/dist/artifacts/drive-script.d.ts +84 -1217
  4. package/dist/artifacts/drive-script.js +1 -1
  5. package/dist/artifacts/flow-constants.d.ts +15 -0
  6. package/dist/artifacts/flow-constants.js +15 -0
  7. package/dist/artifacts/flow-expect-flat.d.ts +8 -192
  8. package/dist/artifacts/flow-expect-flat.js +2 -2
  9. package/dist/artifacts/flow-results.d.ts +26 -0
  10. package/dist/artifacts/flow-types.d.ts +212 -2295
  11. package/dist/artifacts/flow-types.js +14 -2
  12. package/dist/artifacts/harness-credits.d.ts +6 -3
  13. package/dist/artifacts/harness-credits.js +9 -6
  14. package/dist/artifacts/harness-map.d.ts +8 -69
  15. package/dist/artifacts/harness-map.js +1 -1
  16. package/dist/artifacts/hud-notices.d.ts +8 -82
  17. package/dist/artifacts/hud-notices.js +1 -1
  18. package/dist/artifacts/impact.d.ts +104 -1665
  19. package/dist/artifacts/impact.js +38 -21
  20. package/dist/artifacts/journal.d.ts +15 -231
  21. package/dist/artifacts/journal.js +15 -2
  22. package/dist/artifacts/prompt-context.d.ts +7 -33
  23. package/dist/artifacts/prompt-context.js +15 -4
  24. package/dist/artifacts/screenshot-upload.d.ts +3 -26
  25. package/dist/artifacts/screenshot-upload.js +1 -1
  26. package/dist/graph/app-graph.d.ts +137 -0
  27. package/dist/graph/app-graph.js +498 -0
  28. package/dist/hooks/hook-events.d.ts +15 -171
  29. package/dist/hooks/hook-events.js +1 -1
  30. package/dist/hud-entry.js +1 -1
  31. package/dist/identity/checkout.d.ts +41 -0
  32. package/dist/identity/checkout.js +118 -0
  33. package/dist/identity/state-dir.d.ts +23 -0
  34. package/dist/identity/state-dir.js +43 -0
  35. package/dist/index.d.ts +10 -0
  36. package/dist/index.js +10 -0
  37. package/dist/onboarding.d.ts +23 -52
  38. package/dist/onboarding.js +20 -1
  39. package/dist/registry/daemon-registry.d.ts +2 -14
  40. package/dist/registry/daemon-registry.js +1 -1
  41. package/dist/registry/dev-server-registry.d.ts +2 -31
  42. package/dist/registry/dev-server-registry.js +1 -1
  43. package/dist/registry/project-registry.d.ts +4 -32
  44. package/dist/registry/project-registry.js +1 -1
  45. package/dist/schema/command-message.json +65 -68
  46. package/dist/schema/command-name.json +29 -33
  47. package/dist/schema/command-result.json +25 -30
  48. package/dist/schema/event-message.json +104 -101
  49. package/dist/schema/event-payloads.json +246 -108
  50. package/dist/schema/event-type.json +47 -48
  51. package/dist/schema/hello-message.json +150 -133
  52. package/dist/schema/reticle-event.json +98 -94
  53. package/dist/schema/reticle-message.json +338 -310
  54. package/dist/telemetry-bug.d.ts +4 -75
  55. package/dist/telemetry-bug.js +1 -1
  56. package/dist/telemetry-entry.d.ts +1 -1
  57. package/dist/telemetry-entry.js +1 -1
  58. package/dist/telemetry-feedback.d.ts +17 -91
  59. package/dist/telemetry-feedback.js +20 -1
  60. package/dist/telemetry-refusal.d.ts +4 -31
  61. package/dist/telemetry-refusal.js +1 -1
  62. package/dist/telemetry-session.d.ts +45 -624
  63. package/dist/telemetry-session.js +25 -12
  64. package/dist/telemetry-signals.d.ts +211 -86
  65. package/dist/telemetry-signals.js +100 -2
  66. package/dist/telemetry.d.ts +321 -1691
  67. package/dist/telemetry.js +72 -6
  68. package/dist/verdict/check-sentence.d.ts +34 -0
  69. package/dist/verdict/check-sentence.js +228 -0
  70. package/dist/verdict/compare-source.d.ts +1 -1
  71. package/dist/verdict/compare-source.js +1 -1
  72. package/dist/verdict/consequence.d.ts +15 -0
  73. package/dist/verdict/consequence.js +11 -0
  74. package/dist/verdict/disparity.d.ts +8 -56
  75. package/dist/verdict/disparity.js +1 -1
  76. package/dist/verdict/findings.d.ts +10 -0
  77. package/dist/verdict/findings.js +13 -0
  78. package/dist/verdict/instrumentation-coverage.d.ts +6 -37
  79. package/dist/verdict/instrumentation-coverage.js +15 -59
  80. package/dist/verdict/instrumentation-gap.d.ts +72 -0
  81. package/dist/verdict/instrumentation-gap.js +64 -0
  82. package/dist/verdict/intent.d.ts +25 -224
  83. package/dist/verdict/intent.js +1 -1
  84. package/dist/verdict/predicate-tree.d.ts +2 -0
  85. package/dist/verdict/predicate-tree.js +16 -0
  86. package/dist/verdict/predicate.d.ts +3 -2
  87. package/dist/verdict/predicate.js +107 -16
  88. package/dist/verdict/property-assertion.d.ts +23 -101
  89. package/dist/verdict/property-assertion.js +1 -1
  90. package/dist/verdict/run-report.d.ts +71 -0
  91. package/dist/verdict/run-report.js +53 -0
  92. package/dist/verdict/security.d.ts +48 -2
  93. package/dist/verdict/security.js +185 -18
  94. package/dist/verdict/verification-run.d.ts +124 -1623
  95. package/dist/verdict/verification-run.js +37 -6
  96. package/dist/verdict/verify-progress.d.ts +22 -64
  97. package/dist/verdict/verify-progress.js +1 -1
  98. package/dist/wire/constants/constants.d.ts +56 -141
  99. package/dist/wire/constants/constants.js +64 -144
  100. package/dist/wire/constants/discovery.d.ts +4 -4
  101. package/dist/wire/constants/discovery.js +4 -4
  102. package/dist/wire/constants/event-types.d.ts +37 -1
  103. package/dist/wire/constants/event-types.js +35 -1
  104. package/dist/wire/constants/hud-controls.js +0 -2
  105. package/dist/wire/constants/hud-use.d.ts +4 -0
  106. package/dist/wire/constants/hud-use.js +4 -0
  107. package/dist/wire/constants/mcp-wire.d.ts +16 -0
  108. package/dist/wire/constants/mcp-wire.js +16 -0
  109. package/dist/wire/constants/response-body-reason.d.ts +19 -0
  110. package/dist/wire/constants/response-body-reason.js +18 -0
  111. package/dist/wire/constants/reticle-env.d.ts +140 -0
  112. package/dist/wire/constants/reticle-env.js +140 -0
  113. package/dist/wire/constants/session-constants.d.ts +11 -0
  114. package/dist/wire/constants/session-constants.js +11 -0
  115. package/dist/wire/event-payloads.d.ts +173 -629
  116. package/dist/wire/event-payloads.js +107 -23
  117. package/dist/wire/event-priority.js +1 -0
  118. package/dist/wire/messages.d.ts +90 -713
  119. package/dist/wire/messages.js +19 -3
  120. package/dist/wire/narrow.d.ts +6 -0
  121. package/dist/wire/narrow.js +26 -0
  122. package/dist/wire/next-step.d.ts +50 -0
  123. package/dist/wire/next-step.js +37 -0
  124. package/dist/wire/platform-link.d.ts +7 -0
  125. package/dist/wire/platform-link.js +14 -8
  126. package/dist/wire/production/activation-ticket.d.ts +64 -0
  127. package/dist/wire/production/activation-ticket.js +148 -0
  128. package/dist/wire/production/hello-mode.d.ts +35 -0
  129. package/dist/wire/production/hello-mode.js +32 -0
  130. package/dist/wire/production/production-policy.d.ts +92 -0
  131. package/dist/wire/production/production-policy.js +133 -0
  132. package/dist/wire/tool-names.d.ts +2 -0
  133. package/dist/wire/tool-names.js +2 -0
  134. package/dist/wire/types.d.ts +102 -700
  135. package/dist/wire/types.js +5 -5
  136. package/dist/words/upgrade.d.ts +3 -13
  137. package/dist/words/upgrade.js +1 -1
  138. package/package.json +7 -4
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import * as z from 'zod';
2
2
  import { HudPanel, HudPromptSurface, HudToggle, HudView } from './constants/hud-use.js';
3
3
  import { EventAttribution, EventType, RETICLE_MIN_PROTOCOL_VERSION, RETICLE_PROTOCOL_VERSION, MessageKind, TRANSPORT_LIMITS, } from './constants/constants.js';
4
4
  import { HumanControlKind, MarkAnchorStrategy } from './constants/session-constants.js';
@@ -7,6 +7,7 @@ import { DOCUMENT_ID_LENGTH } from '../identity/document-identity.js';
7
7
  import { NO_EDITS_OBSERVED } from '../identity/edit-epoch.js';
8
8
  import { PlatformProfile } from './platform.js';
9
9
  import { ChannelId } from './channel.js';
10
+ import { HelloEnv, HelloInstrumentation, HelloScope } from './production/hello-mode.js';
10
11
  import { OutwardAction } from '../verdict/security.js';
11
12
  const sessionIdSchema = z.string().min(1).max(TRANSPORT_LIMITS.MAX_SESSION_ID_LENGTH);
12
13
  const refSchema = z.string().max(TRANSPORT_LIMITS.MAX_REF_LENGTH);
@@ -107,7 +108,7 @@ export const ReticleEventSchema = z.object({
107
108
  */
108
109
  editEpoch: z.number().int().min(NO_EDITS_OBSERVED).optional(),
109
110
  /** Event-type-specific payload. Kept open here; refined per observer at the edges. */
110
- data: z.record(z.unknown()).default({}),
111
+ data: z.record(z.string(), z.unknown()).default({}),
111
112
  });
112
113
  /** Browser announces itself to the bridge on connect. */
113
114
  export const HelloMessageSchema = z.object({
@@ -137,6 +138,16 @@ export const HelloMessageSchema = z.object({
137
138
  .max(TRANSPORT_LIMITS.MAX_ADAPTERS),
138
139
  /** Optional browser/bridge pairing token. Required when the bridge configures one. */
139
140
  token: z.string().max(TRANSPORT_LIMITS.MAX_TOKEN_LENGTH).optional(),
141
+ /**
142
+ * Where this page runs. ABSENT MEANS `development`: every SDK before this field was a dev build.
143
+ * The bridge accepts `production` only with a session nonce, never with the pairing token.
144
+ */
145
+ env: z.nativeEnum(HelloEnv).optional(),
146
+ /**
147
+ * What a drive may do here: `observe` or `act`. Absent means `act` in development. The bridge
148
+ * refuses a production HELLO without it, and a page can only narrow what its nonce granted.
149
+ */
150
+ scope: z.nativeEnum(HelloScope).optional(),
140
151
  /**
141
152
  * What kind of surface this is: a browser document, a web page in somebody else's window, a
142
153
  * native view tree, or no rendering surface at all. See platform.ts.
@@ -216,6 +227,11 @@ export const HelloMessageSchema = z.object({
216
227
  * would suppress the honest gap for every app that simply has no source mapping at all.
217
228
  */
218
229
  sourceMapping: z.boolean().optional(),
230
+ /**
231
+ * Who put the SDK in this page: `installed` by the app, or `injected` from outside by a hosted
232
+ * verifier. ABSENT means `installed`. See `HelloInstrumentation`.
233
+ */
234
+ instrumentation: z.nativeEnum(HelloInstrumentation).optional(),
219
235
  /**
220
236
  * The version of the SDK in the page, so a version-skewed pair can SAY so.
221
237
  *
@@ -260,7 +276,7 @@ export const CommandMessageSchema = z.object({
260
276
  id: z.string().min(1).max(TRANSPORT_LIMITS.MAX_COMMAND_ID_LENGTH),
261
277
  sessionId: sessionIdSchema.optional(),
262
278
  name: z.string().min(1).max(TRANSPORT_LIMITS.MAX_COMMAND_NAME_LENGTH),
263
- args: z.record(z.unknown()).default({}),
279
+ args: z.record(z.string(), z.unknown()).default({}),
264
280
  /** Set when the Harness sent it, so the HUD can say who is driving. */
265
281
  by: z.nativeEnum(CommandActor).optional(),
266
282
  /**
@@ -37,3 +37,9 @@ export declare function asNumber(value: unknown): number | undefined;
37
37
  * move, and a move that also changes behaviour is two changes wearing one message.
38
38
  */
39
39
  export declare function asRecord(value: unknown): Record<string, unknown>;
40
+ /** Whether the value is an object other than `null` or an array; `asRecord` lets arrays through. */
41
+ export declare function isPlainRecord(value: unknown): value is Record<string, unknown>;
42
+ /** The URL's origin, or nothing when it is absent or does not parse. */
43
+ export declare function originOf(url: string | undefined): string | undefined;
44
+ /** The URL's pathname, or nothing when it is absent or does not parse. */
45
+ export declare function pathOf(url: string | undefined): string | undefined;
@@ -43,3 +43,29 @@ export function asNumber(value) {
43
43
  export function asRecord(value) {
44
44
  return 'object' === typeof value && value !== null ? value : {};
45
45
  }
46
+ /** Whether the value is an object other than `null` or an array; `asRecord` lets arrays through. */
47
+ export function isPlainRecord(value) {
48
+ return 'object' === typeof value && value !== null && !Array.isArray(value);
49
+ }
50
+ /** The URL's origin, or nothing when it is absent or does not parse. */
51
+ export function originOf(url) {
52
+ if (url === undefined)
53
+ return undefined;
54
+ try {
55
+ return new URL(url).origin;
56
+ }
57
+ catch {
58
+ return undefined;
59
+ }
60
+ }
61
+ /** The URL's pathname, or nothing when it is absent or does not parse. */
62
+ export function pathOf(url) {
63
+ if (url === undefined)
64
+ return undefined;
65
+ try {
66
+ return new URL(url).pathname;
67
+ }
68
+ catch {
69
+ return undefined;
70
+ }
71
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * What to do next, and who does it, when Reticle refuses something.
3
+ *
4
+ * One file because the daemon, the HUD and the agent each say part of the same answer. When each
5
+ * wrote its own, the Harness refusal said `reticle connect` and the recovery beside it said
6
+ * `npx @reticlehq/server connect`: two spellings of one command, in one answer.
7
+ */
8
+ import * as z from 'zod';
9
+ /** The command that signs a person in and links a project. The docs spell it this way. */
10
+ export declare const CONNECT_COMMAND = "reticle connect";
11
+ /** The refusals that say what to do next as data rather than in a sentence. */
12
+ export declare const RefusalCode: {
13
+ /** The Harness was asked for, and this project is not linked to the platform it runs on. */
14
+ readonly HARNESS_NOT_LINKED: "harness_not_linked";
15
+ };
16
+ export type RefusalCode = (typeof RefusalCode)[keyof typeof RefusalCode];
17
+ /** Who takes a next step: the person (a command to run) or the agent (a tool to call). */
18
+ export declare const NextWho: {
19
+ readonly HUMAN: "human";
20
+ readonly AGENT: "agent";
21
+ };
22
+ export declare const NextStepSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
23
+ who: z.ZodLiteral<"human">;
24
+ run: z.ZodString;
25
+ }, z.core.$strip>, z.ZodObject<{
26
+ who: z.ZodLiteral<"agent">;
27
+ tool: z.ZodString;
28
+ }, z.core.$strip>], "who">;
29
+ export type NextStep = z.infer<typeof NextStepSchema>;
30
+ /** A coded refusal, as it rides on a tool's error envelope beside `error`. */
31
+ export declare const CodedRefusalSchema: z.ZodObject<{
32
+ code: z.ZodEnum<{
33
+ harness_not_linked: "harness_not_linked";
34
+ }>;
35
+ next: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
36
+ who: z.ZodLiteral<"human">;
37
+ run: z.ZodString;
38
+ }, z.core.$strip>, z.ZodObject<{
39
+ who: z.ZodLiteral<"agent">;
40
+ tool: z.ZodString;
41
+ }, z.core.$strip>], "who">>;
42
+ }, z.core.$strip>;
43
+ export type CodedRefusal = z.infer<typeof CodedRefusalSchema>;
44
+ /** Not linked: the person connects, or the agent drives the journey itself. */
45
+ export declare const HARNESS_NOT_LINKED_NEXT: readonly NextStep[];
46
+ /**
47
+ * The same refusal, for the person on the HUD: one sentence, the command, no tool names. No credit
48
+ * number either: free credit is granted once per person, so the engine cannot know what is left.
49
+ */
50
+ export declare const HARNESS_NOT_LINKED_HUMAN = "Connect your Reticle account to run the Harness: `reticle connect`.";
@@ -0,0 +1,37 @@
1
+ /**
2
+ * What to do next, and who does it, when Reticle refuses something.
3
+ *
4
+ * One file because the daemon, the HUD and the agent each say part of the same answer. When each
5
+ * wrote its own, the Harness refusal said `reticle connect` and the recovery beside it said
6
+ * `npx @reticlehq/server connect`: two spellings of one command, in one answer.
7
+ */
8
+ import * as z from 'zod';
9
+ import { ReticleTool } from './tool-names.js';
10
+ /** The command that signs a person in and links a project. The docs spell it this way. */
11
+ export const CONNECT_COMMAND = 'reticle connect';
12
+ /** The refusals that say what to do next as data rather than in a sentence. */
13
+ export const RefusalCode = {
14
+ /** The Harness was asked for, and this project is not linked to the platform it runs on. */
15
+ HARNESS_NOT_LINKED: 'harness_not_linked',
16
+ };
17
+ /** Who takes a next step: the person (a command to run) or the agent (a tool to call). */
18
+ export const NextWho = { HUMAN: 'human', AGENT: 'agent' };
19
+ export const NextStepSchema = z.discriminatedUnion('who', [
20
+ z.object({ who: z.literal(NextWho.HUMAN), run: z.string() }),
21
+ z.object({ who: z.literal(NextWho.AGENT), tool: z.string() }),
22
+ ]);
23
+ /** A coded refusal, as it rides on a tool's error envelope beside `error`. */
24
+ export const CodedRefusalSchema = z.object({
25
+ code: z.enum([RefusalCode.HARNESS_NOT_LINKED]),
26
+ next: z.array(NextStepSchema),
27
+ });
28
+ /** Not linked: the person connects, or the agent drives the journey itself. */
29
+ export const HARNESS_NOT_LINKED_NEXT = [
30
+ { who: NextWho.HUMAN, run: CONNECT_COMMAND },
31
+ { who: NextWho.AGENT, tool: ReticleTool.ACT_AND_WAIT },
32
+ ];
33
+ /**
34
+ * The same refusal, for the person on the HUD: one sentence, the command, no tool names. No credit
35
+ * number either: free credit is granted once per person, so the engine cannot know what is left.
36
+ */
37
+ export const HARNESS_NOT_LINKED_HUMAN = `Connect your Reticle account to run the Harness: \`${CONNECT_COMMAND}\`.`;
@@ -102,6 +102,13 @@ export interface ToolSessionResult {
102
102
  result?: unknown;
103
103
  error?: string;
104
104
  ms: number;
105
+ /**
106
+ * When the call started and ended on the daemon's clock (epoch ms). The platform saw only its own
107
+ * send and the reply, which folds the network and the queue into the tool's time. Optional: an
108
+ * older daemon does not send them.
109
+ */
110
+ startedAt?: number;
111
+ endedAt?: number;
105
112
  }
106
113
  /** The platform's answer to a session ask: the next calls, or that the drive is over. */
107
114
  export interface ToolSessionReply {
@@ -18,7 +18,7 @@
18
18
  * Every arrow points out from the daemon: it asks, the platform answers. Nothing listens for the
19
19
  * platform on the person's machine.
20
20
  */
21
- import { z } from 'zod';
21
+ import * as z from 'zod';
22
22
  import { HudVisibility } from './constants/hud-use.js';
23
23
  /** Bumped only for a change an older reader would MISREAD. A new field is not one. */
24
24
  export const PLATFORM_LINK_VERSION = 1;
@@ -89,8 +89,9 @@ const SPEC_FIELDS = {
89
89
  export function parseDriveSpec(raw) {
90
90
  const ignored = [];
91
91
  const spec = {};
92
- if ('object' !== typeof raw || null === raw || Array.isArray(raw))
92
+ if ('object' !== typeof raw || null === raw || Array.isArray(raw)) {
93
93
  return { spec, ignored };
94
+ }
94
95
  for (const [field, value] of Object.entries(raw)) {
95
96
  const schema = SPEC_FIELDS[field];
96
97
  if (schema === undefined) {
@@ -98,10 +99,12 @@ export function parseDriveSpec(raw) {
98
99
  continue;
99
100
  }
100
101
  const parsed = schema.safeParse(value);
101
- if (parsed.success)
102
+ if (parsed.success) {
102
103
  spec[field] = parsed.data;
103
- else
104
+ }
105
+ else {
104
106
  ignored.push({ field, reason: SpecIgnoredReason.INVALID });
107
+ }
105
108
  }
106
109
  return { spec, ignored };
107
110
  }
@@ -127,7 +130,7 @@ export const sessionAskRetryable = (status) => 500 <= status || 429 === status |
127
130
  const SessionCallSchema = z.object({
128
131
  seq: z.number().int().nonnegative(),
129
132
  tool: z.string().min(1).max(100),
130
- args: z.record(z.unknown()).default({}),
133
+ args: z.record(z.string(), z.unknown()).default({}),
131
134
  });
132
135
  /**
133
136
  * Read the platform's answer. Never throws: an answer that does not read is "done", because a drive
@@ -135,8 +138,9 @@ const SessionCallSchema = z.object({
135
138
  * dropped; the platform sees no result for its `seq` and decides what that means.
136
139
  */
137
140
  export function parseToolSessionReply(raw) {
138
- if ('object' !== typeof raw || null === raw)
141
+ if ('object' !== typeof raw || null === raw) {
139
142
  return { calls: [], done: true };
143
+ }
140
144
  const record = raw;
141
145
  const calls = (Array.isArray(record.calls) ? record.calls : [])
142
146
  .slice(0, TOOL_SESSION_LIMITS.MAX_BATCH)
@@ -160,12 +164,14 @@ export function parseLinkNotices(raw) {
160
164
  const levels = new Set(Object.values(LinkNoticeLevel));
161
165
  return (Array.isArray(raw) ? raw : [])
162
166
  .flatMap((entry) => {
163
- if ('object' !== typeof entry || null === entry)
167
+ if ('object' !== typeof entry || null === entry) {
164
168
  return [];
169
+ }
165
170
  const notice = entry;
166
171
  const text = 'string' === typeof notice.text ? notice.text.trim() : '';
167
- if ('string' !== typeof notice.level || !levels.has(notice.level) || 0 === text.length)
172
+ if ('string' !== typeof notice.level || !levels.has(notice.level) || 0 === text.length) {
168
173
  return [];
174
+ }
169
175
  const url = 'string' === typeof notice.url && /^https?:\/\//i.test(notice.url) ? notice.url : undefined;
170
176
  return [
171
177
  {
@@ -0,0 +1,64 @@
1
+ import { HelloScope } from './hello-mode.js';
2
+ /**
3
+ * The activation ticket a page checks for itself before it loads the SDK on a live site.
4
+ *
5
+ * The service mints it and signs it with Ed25519. The page carries only the public key, so it can
6
+ * check the ticket with no round trip and no secret. Format: `<payload>.<signature>`. Both parts
7
+ * are base64url. `payload` is the JSON of `ActivationTicketPayload`, and the signature covers the
8
+ * ASCII bytes of the payload part as it is written.
9
+ *
10
+ * Hand-narrowed and not a zod schema on purpose: the page-side loader imports this file, and zod
11
+ * would put a schema library into every page load of a live site to check six fields.
12
+ */
13
+ /** The query parameter the site opens with. The page loader removes it from the address bar. */
14
+ export declare const PRODUCTION_ACTIVATE_PARAM = "reticle_activate";
15
+ /** The only ticket format this file reads. A new format gets a new number. */
16
+ export declare const ACTIVATION_TICKET_VERSION = 1;
17
+ export interface ActivationTicketPayload {
18
+ v: typeof ACTIVATION_TICKET_VERSION;
19
+ /** The single-use HELLO token the bridge minted for this drive. The page sends it as `token`. */
20
+ nonce: string;
21
+ /** The page origin the ticket is for. Another origin refuses it. */
22
+ origin: string;
23
+ /** The bridge the page connects to. `wss://`, or `ws://` on a loopback host only. */
24
+ bridge: string;
25
+ scope: HelloScope;
26
+ /** When the ticket stops being valid, in epoch ms. */
27
+ exp: number;
28
+ }
29
+ /** Why a page refused a ticket. The page does nothing in each case. */
30
+ export declare const ActivationRefusal: {
31
+ readonly MALFORMED: "malformed ticket";
32
+ readonly BAD_SIGNATURE: "the signature does not verify";
33
+ readonly EXPIRED: "the ticket expired";
34
+ readonly WRONG_ORIGIN: "the ticket is for another origin";
35
+ readonly INSECURE_BRIDGE: "the bridge is not wss:// or a loopback ws://";
36
+ };
37
+ export type ActivationRefusal = (typeof ActivationRefusal)[keyof typeof ActivationRefusal];
38
+ export type ActivationVerdict = {
39
+ ok: true;
40
+ payload: ActivationTicketPayload;
41
+ } | {
42
+ ok: false;
43
+ reason: ActivationRefusal;
44
+ };
45
+ type Subtle = typeof globalThis.crypto.subtle;
46
+ type SigningKey = Parameters<Subtle['sign']>[1];
47
+ export declare function toBase64Url(bytes: Uint8Array): string;
48
+ /**
49
+ * Check a ticket against the service's public key (base64url, raw 32 bytes), the page origin and
50
+ * the clock. Never throws.
51
+ */
52
+ export declare function verifyActivationTicket(ticket: string, options: {
53
+ publicKey: string;
54
+ origin: string;
55
+ now: number;
56
+ subtle?: Subtle;
57
+ }): Promise<ActivationVerdict>;
58
+ /**
59
+ * Mint a ticket. For the service only: it holds the Ed25519 private key, and the key never leaves
60
+ * it. Import a PKCS#8 key with `crypto.subtle.importKey('pkcs8', der, { name: 'Ed25519' }, false,
61
+ * ['sign'])`.
62
+ */
63
+ export declare function signActivationTicket(privateKey: SigningKey, payload: ActivationTicketPayload, subtle?: Subtle): Promise<string>;
64
+ export {};
@@ -0,0 +1,148 @@
1
+ import { HelloScope } from './hello-mode.js';
2
+ import { isLoopbackHostname } from '../../verdict/security.js';
3
+ /**
4
+ * The activation ticket a page checks for itself before it loads the SDK on a live site.
5
+ *
6
+ * The service mints it and signs it with Ed25519. The page carries only the public key, so it can
7
+ * check the ticket with no round trip and no secret. Format: `<payload>.<signature>`. Both parts
8
+ * are base64url. `payload` is the JSON of `ActivationTicketPayload`, and the signature covers the
9
+ * ASCII bytes of the payload part as it is written.
10
+ *
11
+ * Hand-narrowed and not a zod schema on purpose: the page-side loader imports this file, and zod
12
+ * would put a schema library into every page load of a live site to check six fields.
13
+ */
14
+ /** The query parameter the site opens with. The page loader removes it from the address bar. */
15
+ export const PRODUCTION_ACTIVATE_PARAM = 'reticle_activate';
16
+ /** The only ticket format this file reads. A new format gets a new number. */
17
+ export const ACTIVATION_TICKET_VERSION = 1;
18
+ /** Joins the payload part and the signature part. */
19
+ const TICKET_SEPARATOR = '.';
20
+ const ED25519 = 'Ed25519';
21
+ const RAW_KEY_FORMAT = 'raw';
22
+ const WS_PROTOCOL = 'ws:';
23
+ const WSS_PROTOCOL = 'wss:';
24
+ /** Bounds, so a page never parses an unbounded string from its own address bar. */
25
+ const MAX_TICKET_LENGTH = 4096;
26
+ const MAX_FIELD_LENGTH = 2048;
27
+ /** Why a page refused a ticket. The page does nothing in each case. */
28
+ export const ActivationRefusal = {
29
+ MALFORMED: 'malformed ticket',
30
+ BAD_SIGNATURE: 'the signature does not verify',
31
+ EXPIRED: 'the ticket expired',
32
+ WRONG_ORIGIN: 'the ticket is for another origin',
33
+ INSECURE_BRIDGE: 'the bridge is not wss:// or a loopback ws://',
34
+ };
35
+ export function toBase64Url(bytes) {
36
+ let binary = '';
37
+ for (const byte of bytes)
38
+ binary += String.fromCharCode(byte);
39
+ return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
40
+ }
41
+ /** Undefined when the text is not base64url. */
42
+ function fromBase64Url(text) {
43
+ if (!/^[A-Za-z0-9_-]*$/.test(text))
44
+ return undefined;
45
+ try {
46
+ const binary = atob(text.replace(/-/g, '+').replace(/_/g, '/'));
47
+ const bytes = new Uint8Array(binary.length);
48
+ for (let i = 0; i < binary.length; i++)
49
+ bytes[i] = binary.charCodeAt(i);
50
+ return bytes;
51
+ }
52
+ catch {
53
+ return undefined;
54
+ }
55
+ }
56
+ const isText = (value) => 'string' === typeof value && 0 < value.length && value.length <= MAX_FIELD_LENGTH;
57
+ function readPayload(part) {
58
+ const bytes = fromBase64Url(part);
59
+ if (bytes === undefined)
60
+ return undefined;
61
+ let value;
62
+ try {
63
+ value = JSON.parse(new TextDecoder().decode(bytes));
64
+ }
65
+ catch {
66
+ return undefined;
67
+ }
68
+ if (typeof value !== 'object' || null === value)
69
+ return undefined;
70
+ const p = value;
71
+ const scope = Object.values(HelloScope).find((s) => s === p.scope);
72
+ if (p.v !== ACTIVATION_TICKET_VERSION ||
73
+ !isText(p.nonce) ||
74
+ !isText(p.origin) ||
75
+ !isText(p.bridge) ||
76
+ scope === undefined ||
77
+ typeof p.exp !== 'number' ||
78
+ !Number.isFinite(p.exp)) {
79
+ return undefined;
80
+ }
81
+ return {
82
+ v: ACTIVATION_TICKET_VERSION,
83
+ nonce: p.nonce,
84
+ origin: p.origin,
85
+ bridge: p.bridge,
86
+ scope,
87
+ exp: p.exp,
88
+ };
89
+ }
90
+ function secureBridge(bridge) {
91
+ try {
92
+ const url = new URL(bridge);
93
+ if (url.protocol === WSS_PROTOCOL)
94
+ return true;
95
+ return url.protocol === WS_PROTOCOL && isLoopbackHostname(url.hostname);
96
+ }
97
+ catch {
98
+ return false;
99
+ }
100
+ }
101
+ async function signatureVerifies(subtle, publicKey, signature, signed) {
102
+ const raw = fromBase64Url(publicKey);
103
+ if (raw === undefined)
104
+ return false;
105
+ try {
106
+ const key = await subtle.importKey(RAW_KEY_FORMAT, raw, { name: ED25519 }, false, ['verify']);
107
+ return await subtle.verify({ name: ED25519 }, key, signature, signed);
108
+ }
109
+ catch {
110
+ // A malformed key, or a browser with no Ed25519. Both mean: do not activate.
111
+ return false;
112
+ }
113
+ }
114
+ /**
115
+ * Check a ticket against the service's public key (base64url, raw 32 bytes), the page origin and
116
+ * the clock. Never throws.
117
+ */
118
+ export async function verifyActivationTicket(ticket, options) {
119
+ const parts = ticket.length <= MAX_TICKET_LENGTH ? ticket.split(TICKET_SEPARATOR) : [];
120
+ const [payloadPart, signaturePart] = parts;
121
+ const payload = 2 === parts.length && payloadPart !== undefined ? readPayload(payloadPart) : undefined;
122
+ const signature = signaturePart === undefined ? undefined : fromBase64Url(signaturePart);
123
+ if (payload === undefined || signature === undefined || payloadPart === undefined) {
124
+ return { ok: false, reason: ActivationRefusal.MALFORMED };
125
+ }
126
+ const subtle = options.subtle ?? globalThis.crypto.subtle;
127
+ const signed = new TextEncoder().encode(payloadPart);
128
+ if (!(await signatureVerifies(subtle, options.publicKey, signature, signed))) {
129
+ return { ok: false, reason: ActivationRefusal.BAD_SIGNATURE };
130
+ }
131
+ if (payload.exp <= options.now)
132
+ return { ok: false, reason: ActivationRefusal.EXPIRED };
133
+ if (payload.origin !== options.origin)
134
+ return { ok: false, reason: ActivationRefusal.WRONG_ORIGIN };
135
+ if (!secureBridge(payload.bridge))
136
+ return { ok: false, reason: ActivationRefusal.INSECURE_BRIDGE };
137
+ return { ok: true, payload };
138
+ }
139
+ /**
140
+ * Mint a ticket. For the service only: it holds the Ed25519 private key, and the key never leaves
141
+ * it. Import a PKCS#8 key with `crypto.subtle.importKey('pkcs8', der, { name: 'Ed25519' }, false,
142
+ * ['sign'])`.
143
+ */
144
+ export async function signActivationTicket(privateKey, payload, subtle = globalThis.crypto.subtle) {
145
+ const payloadPart = toBase64Url(new TextEncoder().encode(JSON.stringify(payload)));
146
+ const signature = await subtle.sign({ name: ED25519 }, privateKey, new TextEncoder().encode(payloadPart));
147
+ return `${payloadPart}${TICKET_SEPARATOR}${toBase64Url(new Uint8Array(signature))}`;
148
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Where a page runs and what a drive may do there, as a HELLO announces it.
3
+ *
4
+ * A leaf on purpose: the browser SDK sends these two words, and nothing else about production mode
5
+ * belongs in a page's first load. The policy that reads them lives in `production-policy.ts`.
6
+ */
7
+ /** The environment a page says it runs in. Absent means `development`, which is every older SDK. */
8
+ export declare const HelloEnv: {
9
+ readonly DEVELOPMENT: "development";
10
+ readonly PRODUCTION: "production";
11
+ };
12
+ export type HelloEnv = (typeof HelloEnv)[keyof typeof HelloEnv];
13
+ /**
14
+ * What a drive may do on the page. `observe` reads only. `act` also clicks and types.
15
+ * Absent means `act` in development. A production HELLO must say which.
16
+ */
17
+ export declare const HelloScope: {
18
+ readonly OBSERVE: "observe";
19
+ readonly ACT: "act";
20
+ };
21
+ export type HelloScope = (typeof HelloScope)[keyof typeof HelloScope];
22
+ /**
23
+ * Who put the SDK in this page. Absent means `installed`: every SDK before this field was one the app
24
+ * imported itself.
25
+ *
26
+ * `injected` is a page a hosted verifier instrumented from outside (a URL-only check), with nothing
27
+ * in the app's own code. Whatever such a session reports (a framework, stores found in the React
28
+ * tree) came from the verifier, not the app. A reader must not count it as the app having the SDK
29
+ * installed: signals, registered module stores and file:line still need the app to add them.
30
+ */
31
+ export declare const HelloInstrumentation: {
32
+ readonly INSTALLED: "installed";
33
+ readonly INJECTED: "injected";
34
+ };
35
+ export type HelloInstrumentation = (typeof HelloInstrumentation)[keyof typeof HelloInstrumentation];
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Where a page runs and what a drive may do there, as a HELLO announces it.
3
+ *
4
+ * A leaf on purpose: the browser SDK sends these two words, and nothing else about production mode
5
+ * belongs in a page's first load. The policy that reads them lives in `production-policy.ts`.
6
+ */
7
+ /** The environment a page says it runs in. Absent means `development`, which is every older SDK. */
8
+ export const HelloEnv = {
9
+ DEVELOPMENT: 'development',
10
+ PRODUCTION: 'production',
11
+ };
12
+ /**
13
+ * What a drive may do on the page. `observe` reads only. `act` also clicks and types.
14
+ * Absent means `act` in development. A production HELLO must say which.
15
+ */
16
+ export const HelloScope = {
17
+ OBSERVE: 'observe',
18
+ ACT: 'act',
19
+ };
20
+ /**
21
+ * Who put the SDK in this page. Absent means `installed`: every SDK before this field was one the app
22
+ * imported itself.
23
+ *
24
+ * `injected` is a page a hosted verifier instrumented from outside (a URL-only check), with nothing
25
+ * in the app's own code. Whatever such a session reports (a framework, stores found in the React
26
+ * tree) came from the verifier, not the app. A reader must not count it as the app having the SDK
27
+ * installed: signals, registered module stores and file:line still need the app to add them.
28
+ */
29
+ export const HelloInstrumentation = {
30
+ INSTALLED: 'installed',
31
+ INJECTED: 'injected',
32
+ };
@@ -0,0 +1,92 @@
1
+ import * as z from 'zod';
2
+ /**
3
+ * The production action policy: what a drive on a live site may not do unless its intent says so.
4
+ *
5
+ * A wire contract because two sides supply it. The engine ships `DEFAULT_PRODUCTION_POLICY`; the
6
+ * service that issues the activation ticket may send its own list, which replaces the default for
7
+ * that drive. The daemon enforces it where commands are dispatched, never in the page.
8
+ */
9
+ /** Each refusal names one of these. The text follows "production policy: " in the refusal. */
10
+ export declare const ProductionRule: {
11
+ readonly PAYMENT: "payment or checkout";
12
+ readonly DELETE_WORKSPACE: "delete a workspace or project";
13
+ readonly REVOKE_KEY: "revoke or rotate a key";
14
+ readonly SEND_INVITE: "send an invite or email";
15
+ readonly CANCEL_SUBSCRIPTION: "cancel a subscription";
16
+ readonly CHANGE_ROLE: "change a member's role";
17
+ /** `scope: "observe"` reads only. */
18
+ readonly OBSERVE_ONLY: "the session scope is observe, so it does not act";
19
+ /** The drive stays on the origin it was activated for. */
20
+ readonly OFF_ORIGIN: "leave the activated origin";
21
+ /** The nonce's time is up; the drive is over. */
22
+ readonly EXPIRED: "the session nonce expired";
23
+ };
24
+ export type ProductionRule = (typeof ProductionRule)[keyof typeof ProductionRule];
25
+ /** The rules an intent can allow. The other three are never lifted. */
26
+ export declare const LIFTABLE_PRODUCTION_RULES: readonly ["payment or checkout", "delete a workspace or project", "revoke or rotate a key", "send an invite or email", "cancel a subscription", "change a member's role"];
27
+ export type LiftableProductionRule = (typeof LIFTABLE_PRODUCTION_RULES)[number];
28
+ /**
29
+ * One request the policy refuses: an HTTP method and a path pattern (a RegExp source, matched
30
+ * case-insensitively anywhere in the URL's pathname).
31
+ */
32
+ export declare const ProductionPolicyEntrySchema: z.ZodObject<{
33
+ rule: z.ZodEnum<{
34
+ "payment or checkout": "payment or checkout";
35
+ "delete a workspace or project": "delete a workspace or project";
36
+ "revoke or rotate a key": "revoke or rotate a key";
37
+ "send an invite or email": "send an invite or email";
38
+ "cancel a subscription": "cancel a subscription";
39
+ "change a member's role": "change a member's role";
40
+ }>;
41
+ methods: z.ZodArray<z.ZodString>;
42
+ path: z.ZodString;
43
+ }, z.core.$strip>;
44
+ export type ProductionPolicyEntry = z.infer<typeof ProductionPolicyEntrySchema>;
45
+ export declare const ProductionPolicySchema: z.ZodArray<z.ZodObject<{
46
+ rule: z.ZodEnum<{
47
+ "payment or checkout": "payment or checkout";
48
+ "delete a workspace or project": "delete a workspace or project";
49
+ "revoke or rotate a key": "revoke or rotate a key";
50
+ "send an invite or email": "send an invite or email";
51
+ "cancel a subscription": "cancel a subscription";
52
+ "change a member's role": "change a member's role";
53
+ }>;
54
+ methods: z.ZodArray<z.ZodString>;
55
+ path: z.ZodString;
56
+ }, z.core.$strip>>;
57
+ /** The default list: the REST shapes of the six refused actions. A service may send its own. */
58
+ export declare const DEFAULT_PRODUCTION_POLICY: readonly ProductionPolicyEntry[];
59
+ /** What the refusal says. Every refusal is this sentence; nothing is a silent no-op. */
60
+ export declare const PRODUCTION_REFUSAL_PREFIX = "refused: production policy: ";
61
+ export declare const productionRefusal: (rule: ProductionRule) => string;
62
+ /** The service route that issues an activation ticket, authenticated with the person's API key. */
63
+ export declare const PRODUCTION_TICKET_PATH = "/v1/production-mode/tickets";
64
+ export declare const ProductionTicketRequestSchema: z.ZodObject<{
65
+ workspaceId: z.ZodString;
66
+ nonce: z.ZodString;
67
+ bridgePort: z.ZodNumber;
68
+ scope: z.ZodEnum<{
69
+ readonly OBSERVE: "observe";
70
+ readonly ACT: "act";
71
+ }>;
72
+ origin: z.ZodOptional<z.ZodString>;
73
+ bridge: z.ZodOptional<z.ZodString>;
74
+ }, z.core.$strip>;
75
+ export type ProductionTicketRequest = z.infer<typeof ProductionTicketRequestSchema>;
76
+ export declare const ProductionTicketResponseSchema: z.ZodObject<{
77
+ ticket: z.ZodString;
78
+ expiresAt: z.ZodOptional<z.ZodString>;
79
+ policy: z.ZodOptional<z.ZodArray<z.ZodObject<{
80
+ rule: z.ZodEnum<{
81
+ "payment or checkout": "payment or checkout";
82
+ "delete a workspace or project": "delete a workspace or project";
83
+ "revoke or rotate a key": "revoke or rotate a key";
84
+ "send an invite or email": "send an invite or email";
85
+ "cancel a subscription": "cancel a subscription";
86
+ "change a member's role": "change a member's role";
87
+ }>;
88
+ methods: z.ZodArray<z.ZodString>;
89
+ path: z.ZodString;
90
+ }, z.core.$strip>>>;
91
+ }, z.core.$strip>;
92
+ export type ProductionTicketResponse = z.infer<typeof ProductionTicketResponseSchema>;