@north-light/api 0.3.446 → 0.3.448

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.
@@ -1,5 +1,5 @@
1
1
  /** The contract level this build serves and requires. Written by `scripts/contract.mjs release`; never edit by hand. */
2
- export declare const CONTRACT_LEVEL = "1.1";
2
+ export declare const CONTRACT_LEVEL = "1.2";
3
3
  /** Request header naming the one contract level the caller needs, e.g. `1.3`. Optional: absent means no check. */
4
4
  export declare const REQUIRED_CONTRACT_HEADER = "Crouter-Required-Contract";
5
5
  /** Response header listing the contract levels the server serves, comma-separated, one per major, e.g. `1.3`. */
@@ -12,7 +12,7 @@
12
12
  //
13
13
  // PURITY (spec §3.1): Node built-ins + `src/api/*` only.
14
14
  /** The contract level this build serves and requires. Written by `scripts/contract.mjs release`; never edit by hand. */
15
- export const CONTRACT_LEVEL = '1.1';
15
+ export const CONTRACT_LEVEL = '1.2';
16
16
  /** Request header naming the one contract level the caller needs, e.g. `1.3`. Optional: absent means no check. */
17
17
  export const REQUIRED_CONTRACT_HEADER = 'Crouter-Required-Contract';
18
18
  /** Response header listing the contract levels the server serves, comma-separated, one per major, e.g. `1.3`. */
@@ -210,6 +210,11 @@ export interface NodeFaultDTO {
210
210
  /** The full node view — summary ∪ identity extras ∪ edges ∪ paths. Returned by
211
211
  * `GET /v1/nodes/{id}` and by the create/lifecycle actions that yield a node. */
212
212
  export interface NodeDetailDTO extends Omit<NodeSummaryDTO, 'outcome'> {
213
+ /** Set when another of your machines homes this node and answered: `{id, name}` of that machine. */
214
+ machine?: {
215
+ id: string;
216
+ name: string;
217
+ };
213
218
  outcome: NodeOutcomeDTO | null;
214
219
  /** Node that created this node, or null when an external process did. */
215
220
  creator: NodeIdDTO | null;
@@ -74,6 +74,8 @@ export interface OwnerReadData {
74
74
  * structured fields (for the built-in kinds, the `*ReadData` shapes above). */
75
75
  export interface ObjectLiveReadDTO {
76
76
  state: 'object';
77
+ /** Set when another of your machines homes the object and answered: `{id, name}` of that machine. */
78
+ machine?: MachineRefDTO;
77
79
  type: string;
78
80
  object: ObjectDTO;
79
81
  text: string;
@@ -83,15 +85,28 @@ export interface ObjectLiveReadDTO {
83
85
  export interface ObjectDeletedDTO {
84
86
  state: 'deleted';
85
87
  deleted: ObjectDTO;
88
+ machine?: MachineRefDTO;
89
+ }
90
+ /** A machine of your account, as an answer names it. */
91
+ export interface MachineRefDTO {
92
+ id: string;
93
+ name: string;
94
+ }
95
+ /** No machine answered for `ref`, and the ones that could not answer are listed: the object may be on one of them. Never a "missing". */
96
+ export interface ObjectUnknownDTO {
97
+ state: 'unknown';
98
+ ref: string;
99
+ unreachable: string[];
86
100
  }
87
101
  /** The name's first segment is no handle on this runtime; the object may live on another runtime. */
88
102
  export interface ObjectRemoteDTO {
89
103
  state: 'remote';
90
104
  remote: string;
91
105
  }
92
- export type ObjectReadDTO = ObjectLiveReadDTO | ObjectDeletedDTO | ObjectRemoteDTO;
106
+ export type ObjectReadDTO = ObjectLiveReadDTO | ObjectDeletedDTO | ObjectRemoteDTO | ObjectUnknownDTO;
93
107
  export interface ObjectReadQuery {
94
- fields?: boolean;
108
+ fields?: boolean; /** Read on this machine (`runtime:<id>`), as a merged listing or inbox entry names it. */
109
+ machine?: string;
95
110
  }
96
111
  export interface ObjectListRequest {
97
112
  /** A kind name. */
@@ -105,15 +120,29 @@ export interface ObjectListRequest {
105
120
  deleted?: boolean;
106
121
  limit?: number;
107
122
  cursor?: string;
123
+ /** List only this machine (`runtime:<id>`), paging through its cursor. `self` lists only this machine without labels. */
124
+ machine?: string;
108
125
  }
109
126
  export interface ObjectListItemDTO extends ObjectDTO {
127
+ /** The machine of your account that homes the row; set on a merged or `--machine` listing. */
128
+ machine?: MachineRefDTO;
110
129
  summary?: string | null;
111
130
  /** A document's rule count. */
112
131
  rules?: number;
113
132
  }
133
+ export type MachineListState = 'ok' | 'unreachable' | 'too_old';
134
+ /** How one machine answered a merged listing; `more` says its first page was not all of it (page it with `--machine`). */
135
+ export interface MachineListEntryDTO {
136
+ machine_id: string;
137
+ name: string;
138
+ state: MachineListState;
139
+ more: boolean;
140
+ }
141
+ /** `machines` is present when this runtime belongs to an account; `next_cursor` is always this machine's (or, with `machine`, that machine's). */
114
142
  export interface ObjectListDTO {
115
143
  items: ObjectListItemDTO[];
116
144
  next_cursor: string | null;
145
+ machines?: MachineListEntryDTO[];
117
146
  }
118
147
  export interface ObjectSearchRequest {
119
148
  text: string;
@@ -151,10 +180,12 @@ export interface UnwatchQuery {
151
180
  export interface WatchResultDTO {
152
181
  object: ObjectDTO;
153
182
  patterns: string[];
183
+ machine?: MachineRefDTO;
154
184
  }
155
185
  export interface UnwatchResultDTO {
156
186
  object: ObjectDTO;
157
187
  removed: number;
188
+ machine?: MachineRefDTO;
158
189
  }
159
190
  export type EdgeTypeName = 'acted-on' | 'links-to' | 'watches';
160
191
  export interface EdgesQuery {
@@ -89,6 +89,11 @@ export declare const errorCodes: {
89
89
  readonly status: 403;
90
90
  readonly retryable: false;
91
91
  };
92
+ readonly app_not_granted: {
93
+ readonly type: "permission_error";
94
+ readonly status: 403;
95
+ readonly retryable: false;
96
+ };
92
97
  readonly isolated_run: {
93
98
  readonly type: "permission_error";
94
99
  readonly status: 403;
@@ -114,6 +119,16 @@ export declare const errorCodes: {
114
119
  readonly status: 416;
115
120
  readonly retryable: false;
116
121
  };
122
+ readonly not_here: {
123
+ readonly type: "invalid_request_error";
124
+ readonly status: 404;
125
+ readonly retryable: false;
126
+ };
127
+ readonly peer_version_too_old: {
128
+ readonly type: "invalid_request_error";
129
+ readonly status: 426;
130
+ readonly retryable: false;
131
+ };
117
132
  readonly conflict: {
118
133
  readonly type: "invalid_request_error";
119
134
  readonly status: 409;
@@ -25,11 +25,17 @@ export const errorCodes = {
25
25
  question_pages_off: { type: 'permission_error', status: 403, retryable: false },
26
26
  // An unsandboxed machine (a computer joined to a Northlight account) runs only the person's own terminal agents; app grantees run on the hosted machine.
27
27
  unsandboxed_machine: { type: 'permission_error', status: 403, retryable: false },
28
+ // A forwarded app actor (an app node on another of the person's machines) whose app holds no active mirrored grant on this machine; refused before any row dispatch.
29
+ app_not_granted: { type: 'permission_error', status: 403, retryable: false },
28
30
  isolated_run: { type: 'permission_error', status: 403, retryable: false },
29
31
  provider_version_retired: { type: 'permission_error', status: 403, retryable: false },
30
32
  invalid_scope: { type: 'invalid_request_error', status: 400, retryable: false },
31
33
  invalid_request: { type: 'invalid_request_error', status: 400, retryable: false },
32
34
  range_not_satisfiable: { type: 'invalid_request_error', status: 416, retryable: false },
35
+ // A forwarded call landed on a machine that holds only a stub of the object; the caller asks the other machines again. Details `{ id, home_machine }`.
36
+ not_here: { type: 'invalid_request_error', status: 404, retryable: false },
37
+ // A peer machine runs a crtr release below the peer-protocol floor; details `{ peer_version, floor, receiver_version }`. Update it with `crtr sys update`.
38
+ peer_version_too_old: { type: 'invalid_request_error', status: 426, retryable: false },
33
39
  conflict: { type: 'invalid_request_error', status: 409, retryable: false },
34
40
  not_found: { type: 'invalid_request_error', status: 404, retryable: false },
35
41
  model_unavailable: { type: 'invalid_request_error', status: 404, retryable: false },
@@ -1,13 +1,31 @@
1
1
  import { prepared as cachedStatement } from '../sqlite-statements.js';
2
2
  import { createHash } from 'node:crypto';
3
3
  import { openScopedStore, withScopedWrite } from './db.js';
4
- function ensureOwner(db, owner) {
4
+ /**
5
+ * The owner row ties a store to the thing it belongs to. For user and profile
6
+ * owners the recorded root is the guard: those rows share one store, and a row
7
+ * whose root differs belongs to something else. A project store is different —
8
+ * it lives inside the root that opens it (`<root>/operations.db`), so the
9
+ * location is the identity and the recorded root is only a note of where the
10
+ * directory last was. When the directory is moved or renamed the note goes
11
+ * stale; the owner is still the same project, so a write rebinds the note and a
12
+ * read accepts it. Kind and profile still have to match, so a user or profile
13
+ * store is never accepted as a project owner.
14
+ */
15
+ function ensureOwner(db, owner, access) {
5
16
  const row = cachedStatement(db, 'SELECT kind, root, profile_id FROM scope_owners WHERE owner_key = ?').get(owner.key);
6
17
  if (!row)
7
18
  throw new Error(`scoped SQL owner is missing: ${owner.key}`);
8
- if (row.kind !== owner.kind || row.root !== owner.root || (owner.profileId ?? null) !== row.profile_id) {
19
+ if (row.kind !== owner.kind || (owner.profileId ?? null) !== row.profile_id) {
9
20
  throw new Error(`scoped SQL owner does not match its root: ${owner.key}`);
10
21
  }
22
+ if (row.root === owner.root)
23
+ return;
24
+ if (owner.kind !== 'project' || owner.store.kind !== 'project' || owner.store.root !== owner.root) {
25
+ throw new Error(`scoped SQL owner does not match its root: ${owner.key}`);
26
+ }
27
+ if (access === 'write')
28
+ cachedStatement(db, 'UPDATE scope_owners SET root = ? WHERE owner_key = ?').run(owner.root, owner.key);
11
29
  }
12
30
  function revisionFor(row) {
13
31
  return row === undefined ? 'absent' : createHash('sha256').update(row.raw_json).digest('hex');
@@ -23,7 +41,7 @@ export class SettingsRevisionConflictError extends Error {
23
41
  }
24
42
  export function readRawSettingsWithRevision(owner) {
25
43
  const db = openScopedStore(owner.store);
26
- ensureOwner(db, owner);
44
+ ensureOwner(db, owner, 'read');
27
45
  const row = readRawSettingsRow(db, owner);
28
46
  return { revision: revisionFor(row), settings: row === undefined ? null : JSON.parse(row.raw_json) };
29
47
  }
@@ -32,7 +50,7 @@ export function readRawSettings(owner) {
32
50
  }
33
51
  export function replaceRawSettings(owner, next, ifRevision, validate) {
34
52
  return withScopedWrite(owner.store, (db) => {
35
- ensureOwner(db, owner);
53
+ ensureOwner(db, owner, 'write');
36
54
  const current = readRawSettingsRow(db, owner);
37
55
  if (ifRevision !== revisionFor(current))
38
56
  throw new SettingsRevisionConflictError();
@@ -46,13 +64,13 @@ export function replaceRawSettings(owner, next, ifRevision, validate) {
46
64
  }
47
65
  export function writeRawSettings(owner, raw) {
48
66
  withScopedWrite(owner.store, (db) => {
49
- ensureOwner(db, owner);
67
+ ensureOwner(db, owner, 'write');
50
68
  cachedStatement(db, `INSERT INTO scope_settings(owner_key, raw_json) VALUES (?, ?) ON CONFLICT(owner_key) DO UPDATE SET raw_json=excluded.raw_json`).run(owner.key, JSON.stringify(raw));
51
69
  });
52
70
  }
53
71
  export function mutateRawSettings(owner, mutate) {
54
72
  return withScopedWrite(owner.store, (db) => {
55
- ensureOwner(db, owner);
73
+ ensureOwner(db, owner, 'write');
56
74
  const row = cachedStatement(db, 'SELECT raw_json FROM scope_settings WHERE owner_key = ?').get(owner.key);
57
75
  const result = mutate(row ? JSON.parse(row.raw_json) : {});
58
76
  cachedStatement(db, `INSERT INTO scope_settings(owner_key, raw_json) VALUES (?, ?) ON CONFLICT(owner_key) DO UPDATE SET raw_json=excluded.raw_json`).run(owner.key, JSON.stringify(result.next));
@@ -61,7 +79,7 @@ export function mutateRawSettings(owner, mutate) {
61
79
  }
62
80
  export function readBookkeeping(owner) {
63
81
  const db = openScopedStore(owner.store);
64
- ensureOwner(db, owner);
82
+ ensureOwner(db, owner, 'read');
65
83
  const bookkeeping = cachedStatement(db, 'SELECT last_self_check, active_canvas FROM scope_bookkeeping WHERE owner_key = ?').get(owner.key);
66
84
  const marketplaces = Object.fromEntries(cachedStatement(db, 'SELECT name, last_updated FROM scope_marketplaces WHERE owner_key = ?').all(owner.key).map((row) => [row.name, row.last_updated === null ? {} : { last_updated: row.last_updated }]));
67
85
  const plugins = Object.fromEntries(cachedStatement(db, 'SELECT name, last_updated, etag, checked_at FROM scope_plugins WHERE owner_key = ?').all(owner.key).map((row) => [row.name, { ...(row.last_updated === null ? {} : { last_updated: row.last_updated }), ...(row.etag === null ? {} : { etag: row.etag }), ...(row.checked_at === null ? {} : { checked_at: row.checked_at }) }]));
@@ -69,7 +87,7 @@ export function readBookkeeping(owner) {
69
87
  }
70
88
  export function writeBookkeeping(owner, state) {
71
89
  withScopedWrite(owner.store, (db) => {
72
- ensureOwner(db, owner);
90
+ ensureOwner(db, owner, 'write');
73
91
  cachedStatement(db, `INSERT INTO scope_bookkeeping(owner_key, last_self_check, active_canvas) VALUES (?, ?, ?) ON CONFLICT(owner_key) DO UPDATE SET last_self_check=excluded.last_self_check, active_canvas=excluded.active_canvas`).run(owner.key, state.last_self_check ?? null, state.activeCanvas ?? null);
74
92
  cachedStatement(db, 'DELETE FROM scope_marketplaces WHERE owner_key = ?').run(owner.key);
75
93
  cachedStatement(db, 'DELETE FROM scope_plugins WHERE owner_key = ?').run(owner.key);
@@ -53,6 +53,8 @@ export interface InboxCardEntry {
53
53
  /** An already-resolved sender section supplied to the pure digest formatter. */
54
54
  export interface InboxCardSection {
55
55
  id: string;
56
+ /** The machine of the account the sender lives on, when it is not this one. */
57
+ machine?: string;
56
58
  entries: readonly InboxCardEntry[];
57
59
  }
58
60
  export interface GeneratedCard {
@@ -80,4 +82,10 @@ export declare function isHumanWordsEntry(entry: CardEntry): boolean;
80
82
  /** Explain how an inbox entry's ref is read without guessing its filesystem location. */
81
83
  export declare function formatInboxRefInstruction(ref: string): string;
82
84
  /** Format a complete inbox card from report bodies resolved by the caller. */
85
+ /** The machine a section's sender lives on, when its entries say it is another one (`data.from_machine`, set by the home side of a forwarded send). */
86
+ export declare function machineOfEntries(items: readonly {
87
+ data?: Record<string, unknown>;
88
+ }[]): {
89
+ machine?: string;
90
+ };
83
91
  export declare function formatInboxCard(sections: readonly InboxCardSection[]): string;
@@ -221,6 +221,11 @@ export function formatInboxRefInstruction(ref) {
221
221
  : `Read this report with \`crtr canvas read ${ref}\`.`;
222
222
  }
223
223
  /** Format a complete inbox card from report bodies resolved by the caller. */
224
+ /** The machine a section's sender lives on, when its entries say it is another one (`data.from_machine`, set by the home side of a forwarded send). */
225
+ export function machineOfEntries(items) {
226
+ const machine = items.map((i) => i.data?.['from_machine']).find((m) => typeof m === 'string');
227
+ return machine === undefined ? {} : { machine };
228
+ }
224
229
  export function formatInboxCard(sections) {
225
230
  const body = sections.map((section) => {
226
231
  const entries = section.entries.map((entry) => {
@@ -229,7 +234,7 @@ export function formatInboxCard(sections) {
229
234
  : undefined;
230
235
  return `<entry${formatAttributes({ kind: entry.kind, ref: entry.ref, disposition })}>${escapeXmlText(entry.body)}</entry>`;
231
236
  }).join('\n');
232
- return `<from${formatAttributes({ id: section.id })}>${entries}</from>`;
237
+ return `<from${formatAttributes({ id: section.id, machine: section.machine })}>${entries}</from>`;
233
238
  }).join('\n');
234
239
  return formatCard('inbox', {}, body);
235
240
  }
@@ -42,7 +42,7 @@ function objectText(o) {
42
42
  if (o.type === 'job')
43
43
  return `job ${o.id} (\`${o.name}\`)`;
44
44
  if (o.type === 'node')
45
- return `node ${o.name} (${o.id})`;
45
+ return `node ${o.name} (${o.id})${o.machine ? ` on ${o.machine}` : ''}`;
46
46
  return `${o.type} ${o.name}`;
47
47
  }
48
48
  function actorText(entry, push) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/api",
3
- "version": "0.3.446",
3
+ "version": "0.3.448",
4
4
  "description": "Typed crtrd /v1 API contract — DTOs, route builders, the error contract, the CrtrClient, and the command-plugin manifest format. Zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/api/index.js",