@skrr-ai/cli 0.1.164 → 0.1.166

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 (111) hide show
  1. package/README.md +13 -0
  2. package/dist/base-command.d.ts +14 -1
  3. package/dist/base-command.js +166 -46
  4. package/dist/commands/agent/message/send.d.ts +1 -0
  5. package/dist/commands/agent/message/send.js +3 -0
  6. package/dist/commands/agents/mini-computer/app/set.js +1 -1
  7. package/dist/commands/agents/mini-computer/tasks-view.js +1 -1
  8. package/dist/commands/apps/check.js +3 -8
  9. package/dist/commands/apps/data/batch.d.ts +1 -0
  10. package/dist/commands/apps/data/batch.js +19 -2
  11. package/dist/commands/apps/data/delete.d.ts +1 -0
  12. package/dist/commands/apps/data/delete.js +14 -2
  13. package/dist/commands/apps/data/patch.d.ts +1 -0
  14. package/dist/commands/apps/data/patch.js +11 -2
  15. package/dist/commands/apps/data/put.d.ts +1 -0
  16. package/dist/commands/apps/data/put.js +17 -3
  17. package/dist/commands/apps/install.js +7 -2
  18. package/dist/commands/apps/installations/compatibility.d.ts +13 -0
  19. package/dist/commands/apps/installations/compatibility.js +60 -0
  20. package/dist/commands/apps/installations/migrate/abort.d.ts +13 -0
  21. package/dist/commands/apps/installations/migrate/abort.js +45 -0
  22. package/dist/commands/apps/installations/migrate/cutover.d.ts +15 -0
  23. package/dist/commands/apps/installations/migrate/cutover.js +102 -0
  24. package/dist/commands/apps/installations/migrate/plan.d.ts +13 -0
  25. package/dist/commands/apps/installations/migrate/plan.js +51 -0
  26. package/dist/commands/apps/installations/migrate/rollback.d.ts +16 -0
  27. package/dist/commands/apps/installations/migrate/rollback.js +103 -0
  28. package/dist/commands/apps/installations/migrate/stage.d.ts +15 -0
  29. package/dist/commands/apps/installations/migrate/stage.js +96 -0
  30. package/dist/commands/apps/installations/migrate/status.d.ts +12 -0
  31. package/dist/commands/apps/installations/migrate/status.js +52 -0
  32. package/dist/commands/apps/installations/migrate/validate.d.ts +14 -0
  33. package/dist/commands/apps/installations/migrate/validate.js +58 -0
  34. package/dist/commands/apps/installations/show.js +14 -9
  35. package/dist/commands/apps/installations/snapshot.js +5 -2
  36. package/dist/commands/apps/operations/check.d.ts +16 -0
  37. package/dist/commands/apps/operations/check.js +127 -0
  38. package/dist/commands/apps/publish/propose.js +33 -4
  39. package/dist/commands/apps/reconsent.js +7 -1
  40. package/dist/commands/apps/reference.d.ts +4 -2
  41. package/dist/commands/apps/reference.js +7 -4
  42. package/dist/commands/apps/resume.js +1 -1
  43. package/dist/commands/apps/rollback.d.ts +9 -0
  44. package/dist/commands/apps/rollback.js +81 -2
  45. package/dist/commands/apps/settings/set.js +22 -10
  46. package/dist/commands/apps/upgrade.d.ts +8 -0
  47. package/dist/commands/apps/upgrade.js +67 -2
  48. package/dist/commands/apps/version/cat.d.ts +22 -0
  49. package/dist/commands/apps/version/cat.js +220 -0
  50. package/dist/commands/apps/version/show.js +3 -1
  51. package/dist/commands/commerce/incidents/release.d.ts +24 -0
  52. package/dist/commands/commerce/incidents/release.js +46 -0
  53. package/dist/commands/duties/cases.js +12 -1
  54. package/dist/commands/duties/hypotheses.js +10 -3
  55. package/dist/commands/duties/observations.d.ts +4 -0
  56. package/dist/commands/duties/observations.js +14 -3
  57. package/dist/commands/outbox/list.d.ts +11 -4
  58. package/dist/commands/outbox/list.js +56 -9
  59. package/dist/commands/outbox/retry.d.ts +8 -2
  60. package/dist/commands/outbox/retry.js +76 -16
  61. package/dist/commands/outbox/show.d.ts +20 -0
  62. package/dist/commands/outbox/show.js +83 -0
  63. package/dist/commands/proposals/list.js +13 -9
  64. package/dist/commands/requests/show.d.ts +12 -0
  65. package/dist/commands/requests/show.js +42 -23
  66. package/dist/commands/spaces/deletion/cancel.d.ts +21 -0
  67. package/dist/commands/spaces/deletion/cancel.js +70 -0
  68. package/dist/commands/spaces/workflows/run.js +12 -0
  69. package/dist/commands/widgets/check.d.ts +9 -0
  70. package/dist/commands/widgets/check.js +16 -3
  71. package/dist/generated/agent-guides.json +17 -17
  72. package/dist/lib/api-fetch.d.ts +5 -0
  73. package/dist/lib/api-fetch.js +81 -22
  74. package/dist/lib/appCompatibility.d.ts +31 -0
  75. package/dist/lib/appCompatibility.js +66 -0
  76. package/dist/lib/appData.d.ts +46 -4
  77. package/dist/lib/appData.js +161 -32
  78. package/dist/lib/appDataMigration.d.ts +204 -0
  79. package/dist/lib/appDataMigration.js +753 -0
  80. package/dist/lib/appDataMigrationCommand.d.ts +39 -0
  81. package/dist/lib/appDataMigrationCommand.js +90 -0
  82. package/dist/lib/appInstallations.d.ts +40 -5
  83. package/dist/lib/appInstallations.js +251 -35
  84. package/dist/lib/appVersionFile.d.ts +82 -0
  85. package/dist/lib/appVersionFile.js +195 -0
  86. package/dist/lib/apps-command.d.ts +14 -3
  87. package/dist/lib/apps-command.js +80 -65
  88. package/dist/lib/appsCheck.js +16 -2
  89. package/dist/lib/commerce.d.ts +6 -0
  90. package/dist/lib/commerce.js +9 -0
  91. package/dist/lib/duties.d.ts +15 -1
  92. package/dist/lib/duties.js +21 -3
  93. package/dist/lib/json-output.d.ts +2 -0
  94. package/dist/lib/json-output.js +8 -4
  95. package/dist/lib/node-adapter.js +69 -38
  96. package/dist/lib/outbox.d.ts +110 -1
  97. package/dist/lib/outbox.js +261 -35
  98. package/dist/lib/rate-limit-retry.d.ts +4 -5
  99. package/dist/lib/rate-limit-retry.js +6 -10
  100. package/dist/lib/refresh.d.ts +2 -0
  101. package/dist/lib/refresh.js +6 -0
  102. package/dist/lib/space-deletion-cancel.d.ts +47 -0
  103. package/dist/lib/space-deletion-cancel.js +141 -0
  104. package/dist/lib/widgetCheck.js +1 -1
  105. package/dist/lib/widgetStaticScan.d.ts +11 -5
  106. package/dist/lib/widgetStaticScan.js +73 -165
  107. package/dist/lib/write-outcome.d.ts +76 -0
  108. package/dist/lib/write-outcome.js +190 -0
  109. package/dist/node_modules/@skrr-ai/data-provider/index.js +11955 -11231
  110. package/oclif.manifest.json +3019 -499
  111. package/package.json +12 -3
@@ -0,0 +1,31 @@
1
+ import type { AppCompatibilityView } from '@skrr-ai/data-provider';
2
+ /**
3
+ * Is an installation's stored data compatible with another version? The terminal half of step 6a (docs/architecture/
4
+ * apps-installation-lifecycle-2026-11-10.md, section 6g; the verdict and its words are `@skrr-ai/data-provider`'s `appCompatibility.ts`).
5
+ *
6
+ * WHAT A TERMINAL SHOWS. The verdict, the reason when it is `unknown`, the counts, the findings (a collection, a document id, a code, and a
7
+ * path of names the schema declares: never a value), and, always, the contract's three sentences: it proves conformance to the DECLARED schemas
8
+ * and not that the app will work, it is a snapshot and not a guarantee, and it is separate from approval of what the app may do. The document
9
+ * id is shown because the person who asks can list it with `apps data query`; it is printed here and nowhere else (not in an error, not in a
10
+ * hint, not in the command a hint suggests), and every value from the server is written out safely.
11
+ *
12
+ * Exit codes of `apps installations compatibility` (and of `apps upgrade | rollback --check-data` when it stops before moving): 0 compatible,
13
+ * 1 incompatible, 5 unknown. Unknown is NOT zero, so a script that treats a zero exit as "fine" cannot read an unproven answer as a proof, and
14
+ * it is not the incompatible code, so a script can tell "it does not fit" from "nobody can tell". A request that fails, an installation that
15
+ * is not there, or an answer this client cannot read are the CLI's ordinary failures (an unreadable answer is `unknown`, exit 5).
16
+ */
17
+ export declare const APPS_COMPATIBILITY_EXIT: {
18
+ readonly compatible: 0;
19
+ readonly incompatible: 1;
20
+ readonly unknown: 5;
21
+ };
22
+ /** The exit a verdict gets. */
23
+ export declare function compatibilityExit(view: Pick<AppCompatibilityView, 'verdict'>): number;
24
+ /** Read the server's answer as a verdict about THIS installation and THIS version, or `null` (it then says nothing). */
25
+ export declare function readCompatibility(raw: unknown, installationId: string, versionId: string): AppCompatibilityView | null;
26
+ /** Ask the server and read the answer. Throws what the request throws; `null` is an answer this client does not understand. */
27
+ export declare function fetchCompatibility(installationId: string, versionId: string): Promise<AppCompatibilityView | null>;
28
+ /** The verdict as the lines a person reads. Every value that came from the server is written out safely. */
29
+ export declare function formatCompatibility(view: AppCompatibilityView): string[];
30
+ /** What is said when the server's answer is not a verdict this client understands. It is not a pass. */
31
+ export declare const COMPATIBILITY_UNREADABLE_MESSAGE = "The server answered, but not with a compatibility result this client understands, so nothing is known about whether the data fits. This is not a pass; do not treat it as one. Update the CLI, or read the installation\u2019s data yourself.";
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.COMPATIBILITY_UNREADABLE_MESSAGE = exports.APPS_COMPATIBILITY_EXIT = void 0;
4
+ exports.compatibilityExit = compatibilityExit;
5
+ exports.readCompatibility = readCompatibility;
6
+ exports.fetchCompatibility = fetchCompatibility;
7
+ exports.formatCompatibility = formatCompatibility;
8
+ const data_provider_1 = require("@skrr-ai/data-provider");
9
+ const appData_1 = require("./appData");
10
+ /**
11
+ * Is an installation's stored data compatible with another version? The terminal half of step 6a (docs/architecture/
12
+ * apps-installation-lifecycle-2026-11-10.md, section 6g; the verdict and its words are `@skrr-ai/data-provider`'s `appCompatibility.ts`).
13
+ *
14
+ * WHAT A TERMINAL SHOWS. The verdict, the reason when it is `unknown`, the counts, the findings (a collection, a document id, a code, and a
15
+ * path of names the schema declares: never a value), and, always, the contract's three sentences: it proves conformance to the DECLARED schemas
16
+ * and not that the app will work, it is a snapshot and not a guarantee, and it is separate from approval of what the app may do. The document
17
+ * id is shown because the person who asks can list it with `apps data query`; it is printed here and nowhere else (not in an error, not in a
18
+ * hint, not in the command a hint suggests), and every value from the server is written out safely.
19
+ *
20
+ * Exit codes of `apps installations compatibility` (and of `apps upgrade | rollback --check-data` when it stops before moving): 0 compatible,
21
+ * 1 incompatible, 5 unknown. Unknown is NOT zero, so a script that treats a zero exit as "fine" cannot read an unproven answer as a proof, and
22
+ * it is not the incompatible code, so a script can tell "it does not fit" from "nobody can tell". A request that fails, an installation that
23
+ * is not there, or an answer this client cannot read are the CLI's ordinary failures (an unreadable answer is `unknown`, exit 5).
24
+ */
25
+ exports.APPS_COMPATIBILITY_EXIT = { compatible: 0, incompatible: 1, unknown: 5 };
26
+ /** The exit a verdict gets. */
27
+ function compatibilityExit(view) {
28
+ return exports.APPS_COMPATIBILITY_EXIT[view.verdict];
29
+ }
30
+ /** Read the server's answer as a verdict about THIS installation and THIS version, or `null` (it then says nothing). */
31
+ function readCompatibility(raw, installationId, versionId) {
32
+ const view = (0, data_provider_1.parseAppCompatibility)(raw);
33
+ return view !== null &&
34
+ view.installationId === installationId &&
35
+ view.targetVersionId === versionId
36
+ ? view
37
+ : null;
38
+ }
39
+ /** Ask the server and read the answer. Throws what the request throws; `null` is an answer this client does not understand. */
40
+ async function fetchCompatibility(installationId, versionId) {
41
+ return readCompatibility(await data_provider_1.dataService.getAppInstallationCompatibility(installationId, versionId), installationId, versionId);
42
+ }
43
+ const FINDINGS_SHOWN = 20;
44
+ /** The verdict as the lines a person reads. Every value that came from the server is written out safely. */
45
+ function formatCompatibility(view) {
46
+ const where = `${(0, appData_1.safeForTerminal)(view.installationId)} -> version ${(0, appData_1.safeForTerminal)(view.targetVersionId)}`;
47
+ const lines = [];
48
+ if (view.verdict === 'compatible') {
49
+ lines.push(`compatible: ${where}. The ${view.scanned.documents} stored document${view.scanned.documents === 1 ? '' : 's'} fit${view.scanned.documents === 1 ? 's' : ''} the data schemas that version declares (${view.scanned.collections} collection${view.scanned.collections === 1 ? '' : 's'} checked).`);
50
+ }
51
+ else if (view.verdict === 'incompatible') {
52
+ lines.push(`incompatible: ${where}. Stored data does not fit what that version declares${view.complete ? '' : ' (the list below may be partial, or may have changed since it was read)'}:`);
53
+ for (const finding of view.findings.slice(0, FINDINGS_SHOWN)) {
54
+ lines.push(` ${(0, appData_1.safeForTerminal)(finding.collection)}/${(0, appData_1.safeForTerminal)(finding.documentId)} ${finding.code}${finding.path === undefined ? '' : ` at ${(0, appData_1.safeForTerminal)(finding.path)}`}: ${data_provider_1.APP_COMPATIBILITY_FINDING_WORDS[finding.code]}`);
55
+ }
56
+ }
57
+ else {
58
+ lines.push(`unknown: ${where}. This is NOT a pass: ${data_provider_1.APP_COMPATIBILITY_UNKNOWN_WORDS[view.reason ?? 'database_error']}.`);
59
+ }
60
+ lines.push(`checked at ${(0, appData_1.safeForTerminal)(view.computedAt)}, installation generation ${view.lifecycleGen}, data epoch ${view.dataEpoch}${view.epochEnforced ? '' : ' (the data epoch is not known to be enforced by every writer)'}`);
61
+ for (const sentence of view.disclaimers)
62
+ lines.push(sentence);
63
+ return lines;
64
+ }
65
+ /** What is said when the server's answer is not a verdict this client understands. It is not a pass. */
66
+ exports.COMPATIBILITY_UNREADABLE_MESSAGE = 'The server answered, but not with a compatibility result this client understands, so nothing is known about whether the data fits. This is not a pass; do not treat it as one. Update the CLI, or read the installation’s data yourself.';
@@ -19,13 +19,49 @@
19
19
  *
20
20
  * NEVER PRINTED: who wrote a row. The server returns the KIND of writer (`app`, `agent`, `person`) and never an id.
21
21
  */
22
- import { APP_STORE_CURSOR_INVALID_REASON } from '@skrr-ai/data-provider';
22
+ import { APP_STORE_CURSOR_INVALID_REASON, APP_STORE_CURSOR_MISMATCH_REASON } from '@skrr-ai/data-provider';
23
23
  import type { AppInstallationDataCatalog, AppPersonDataRefusal, AppStoreConflict, AppStoreDocView, AppStoreQueryResult } from '@skrr-ai/data-provider';
24
24
  /** Why the input could not be used; never carries what was in it. */
25
25
  export declare class AppDataInputError extends Error {
26
- readonly code: 'APP_DATA_SOURCE' | 'APP_DATA_NOT_JSON' | 'APP_DATA_NOT_OBJECT' | 'APP_DATA_WHERE';
27
- constructor(code: 'APP_DATA_SOURCE' | 'APP_DATA_NOT_JSON' | 'APP_DATA_NOT_OBJECT' | 'APP_DATA_WHERE', message: string);
26
+ readonly code: 'APP_DATA_SOURCE' | 'APP_DATA_NOT_JSON' | 'APP_DATA_NOT_OBJECT' | 'APP_DATA_WHERE' | 'APP_DATA_IDEMPOTENCY_KEY';
27
+ constructor(code: 'APP_DATA_SOURCE' | 'APP_DATA_NOT_JSON' | 'APP_DATA_NOT_OBJECT' | 'APP_DATA_WHERE' | 'APP_DATA_IDEMPOTENCY_KEY', message: string);
28
28
  }
29
+ /**
30
+ * A fresh key for ONE write the person just asked for. The CLI mints it itself (not the transport's per-call key) so that it can SAY it when
31
+ * the answer is lost: a terminal that cannot tell what happened must be able to ask the server by this key (`skrr apps operations check`),
32
+ * and to send the same write again under the same key (`--idempotency-key`), which the server answers from the receipt it kept, if it kept one.
33
+ */
34
+ export declare function mintDataWriteKey(): string;
35
+ /** The key the person gave, or an error that names the rule. */
36
+ export declare function checkIdempotencyKey(value: string): string;
37
+ /**
38
+ * The operation reference a write's answer carries, or nothing. `operationId` is a SUPPORT REFERENCE: it says a receipt exists for this
39
+ * write, and it is the one thing a person can quote about it. It is checked before it is printed (an answer is data, and what a terminal
40
+ * prints must not be whatever a server said). `replayed` says the answer is the stored outcome of an earlier identical request, which this
41
+ * run did not repeat.
42
+ */
43
+ export declare function readOperationReference(result: unknown): {
44
+ operationId: string;
45
+ replayed: boolean;
46
+ } | null;
47
+ /** The line a successful write adds on STDERR (stdout stays what it was): the reference, and whether this run wrote anything. */
48
+ export declare function describeOperationReference(reference: {
49
+ operationId: string;
50
+ replayed: boolean;
51
+ }): string;
52
+ /**
53
+ * What to say when the outcome of a write is unknown and a key was sent: how to ask, by that key. Honest in both directions: a receipt that
54
+ * exists proves the write landed, and the absence of one proves nothing (the server may keep none, the write may never have arrived, or it
55
+ * may have been refused).
56
+ */
57
+ export declare function describeKeyedUnknown(input: {
58
+ bin: string;
59
+ installation: string;
60
+ verb: 'put' | 'patch' | 'delete' | 'batch';
61
+ collection?: string;
62
+ id?: string;
63
+ key: string;
64
+ }): string;
29
65
  /** A 2xx is not a receipt unless its body identifies the requested operation. */
30
66
  export declare function assertAppDataWriteResult(value: unknown, target: {
31
67
  verb: 'put' | 'patch' | 'delete';
@@ -52,7 +88,7 @@ export type PersonDataFailure = {
52
88
  status?: number;
53
89
  message?: string;
54
90
  /** A state proven by the same authorized request, when a newer server provides it. */
55
- reason?: 'installation_suspended' | typeof APP_STORE_CURSOR_INVALID_REASON;
91
+ reason?: 'installation_suspended' | 'installation_changed' | typeof APP_STORE_CURSOR_INVALID_REASON | typeof APP_STORE_CURSOR_MISMATCH_REASON;
56
92
  /** Zero-based index of the batch operation, when the server can identify one. */
57
93
  index?: number;
58
94
  retryAfterMs?: number;
@@ -64,6 +100,12 @@ export type PersonDataFailure = {
64
100
  /** A batch: which write (by position) could not apply. */
65
101
  index?: number;
66
102
  };
103
+ /**
104
+ * The stable reason of one of the two cursor refusals (`invalid_cursor`: not a cursor this server handed out;
105
+ * `cursor_mismatch`: a cursor issued for another query), or `undefined`. Each constant is checked to be text first, so a
106
+ * contract build that predates one cannot make an absent reason read as a match.
107
+ */
108
+ export declare function cursorRefusalReason(reason: unknown): typeof APP_STORE_CURSOR_INVALID_REASON | typeof APP_STORE_CURSOR_MISMATCH_REASON | undefined;
67
109
  /**
68
110
  * What the person channel answered, read out of an API error: a refusal (`{ error, code, message }`, the code from
69
111
  * the contract's closed set) or a conflict (`409 { error: "conflict", conflict, currentRev? }`). `null` for anything
@@ -1,9 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PERSON_DATA_QUERY_LIMIT_MAX = exports.PERSON_DATA_CONFLICT_CODES = exports.PERSON_DATA_REFUSAL_CODES = exports.AppDataInputError = void 0;
4
+ exports.mintDataWriteKey = mintDataWriteKey;
5
+ exports.checkIdempotencyKey = checkIdempotencyKey;
6
+ exports.readOperationReference = readOperationReference;
7
+ exports.describeOperationReference = describeOperationReference;
8
+ exports.describeKeyedUnknown = describeKeyedUnknown;
4
9
  exports.assertAppDataWriteResult = assertAppDataWriteResult;
5
10
  exports.readDocumentInput = readDocumentInput;
6
11
  exports.readWhereInput = readWhereInput;
12
+ exports.cursorRefusalReason = cursorRefusalReason;
7
13
  exports.readPersonDataFailure = readPersonDataFailure;
8
14
  exports.personDataFailureExit = personDataFailureExit;
9
15
  exports.personDataFailureRetryable = personDataFailureRetryable;
@@ -37,6 +43,7 @@ exports.formatCollections = formatCollections;
37
43
  * NEVER PRINTED: who wrote a row. The server returns the KIND of writer (`app`, `agent`, `person`) and never an id.
38
44
  */
39
45
  const data_provider_1 = require("@skrr-ai/data-provider");
46
+ const node_crypto_1 = require("node:crypto");
40
47
  const appsFs_1 = require("./appsFs");
41
48
  // eslint-disable-next-line no-control-regex, no-misleading-character-class -- the point of the pattern is to match control, combining and invisible characters
42
49
  const SAFE_FOR_TERMINAL_RE =
@@ -59,6 +66,64 @@ function asPlainObject(value) {
59
66
  ? value
60
67
  : null;
61
68
  }
69
+ // ── the idempotency key of a write, and the operation receipt it may leave (step 4b) ───────────────
70
+ /**
71
+ * The server's grammar for a client idempotency key (`normalizeKey`): printable, up to 200 characters. A key outside it is not a key to the
72
+ * server: the write would be sent, answered, and leave no receipt, so a key the person asked for is refused here rather than quietly dropped.
73
+ */
74
+ const IDEMPOTENCY_KEY_RE = /^[A-Za-z0-9._:/-]{1,200}$/;
75
+ /**
76
+ * A fresh key for ONE write the person just asked for. The CLI mints it itself (not the transport's per-call key) so that it can SAY it when
77
+ * the answer is lost: a terminal that cannot tell what happened must be able to ask the server by this key (`skrr apps operations check`),
78
+ * and to send the same write again under the same key (`--idempotency-key`), which the server answers from the receipt it kept, if it kept one.
79
+ */
80
+ function mintDataWriteKey() {
81
+ return `skrr-data:${(0, node_crypto_1.randomUUID)()}`;
82
+ }
83
+ /** The key the person gave, or an error that names the rule. */
84
+ function checkIdempotencyKey(value) {
85
+ const key = value.trim();
86
+ if (!IDEMPOTENCY_KEY_RE.test(key)) {
87
+ throw new AppDataInputError('APP_DATA_IDEMPOTENCY_KEY', '--idempotency-key must be 1 to 200 characters from letters, digits and . _ : / - (anything else is not a key to the server, and no receipt would be kept).');
88
+ }
89
+ return key;
90
+ }
91
+ const OPERATION_ID_RE = /^opr_[0-9a-f]{48}$/;
92
+ /**
93
+ * The operation reference a write's answer carries, or nothing. `operationId` is a SUPPORT REFERENCE: it says a receipt exists for this
94
+ * write, and it is the one thing a person can quote about it. It is checked before it is printed (an answer is data, and what a terminal
95
+ * prints must not be whatever a server said). `replayed` says the answer is the stored outcome of an earlier identical request, which this
96
+ * run did not repeat.
97
+ */
98
+ function readOperationReference(result) {
99
+ const record = asPlainObject(result);
100
+ if (!record ||
101
+ typeof record.operationId !== 'string' ||
102
+ !OPERATION_ID_RE.test(record.operationId)) {
103
+ return null;
104
+ }
105
+ return { operationId: record.operationId, replayed: record.replayed === true };
106
+ }
107
+ /** The line a successful write adds on STDERR (stdout stays what it was): the reference, and whether this run wrote anything. */
108
+ function describeOperationReference(reference) {
109
+ return reference.replayed
110
+ ? `operation ${reference.operationId} (answered from the receipt of an earlier identical request; nothing was written again)`
111
+ : `operation ${reference.operationId} (a reference for support; the server kept a receipt of this write)`;
112
+ }
113
+ /**
114
+ * What to say when the outcome of a write is unknown and a key was sent: how to ask, by that key. Honest in both directions: a receipt that
115
+ * exists proves the write landed, and the absence of one proves nothing (the server may keep none, the write may never have arrived, or it
116
+ * may have been refused).
117
+ */
118
+ function describeKeyedUnknown(input) {
119
+ const where = input.verb === 'batch'
120
+ ? 'batch'
121
+ : `${input.verb} ${safeForTerminal(input.collection ?? '<collection>')} ${safeForTerminal(input.id ?? '<id>')}`;
122
+ const check = `${input.bin} apps operations check ${safeForTerminal(input.installation)} ${where} --key ${input.key}`;
123
+ return (` This write was sent with the idempotency key ${input.key}. If the server keeps operation receipts, \`${check}\` says whether it was applied ("completed", with what it applied` +
124
+ `${input.verb === 'batch' ? ', including the IDs of documents the server named' : ''}); "unknown" there is NOT "not applied", it only means no receipt is on record. ` +
125
+ `Sending it again with --idempotency-key ${input.key} is answered from the receipt when there is one; without one, it writes again.`);
126
+ }
62
127
  /** A 2xx is not a receipt unless its body identifies the requested operation. */
63
128
  function assertAppDataWriteResult(value, target) {
64
129
  const result = asPlainObject(value);
@@ -68,28 +133,47 @@ function assertAppDataWriteResult(value, target) {
68
133
  let valid = false;
69
134
  if (result && result.ok !== false) {
70
135
  const outcome = target.verb === 'delete' ? result.deleted : result.written;
71
- const contradictsSuccess = result.conflict != null || result.error != null ||
72
- result.code === 'conflict' || (target.verb === 'delete' ? result.written === true : result.deleted === true);
136
+ const contradictsSuccess = result.conflict != null ||
137
+ result.error != null ||
138
+ result.code === 'conflict' ||
139
+ (target.verb === 'delete' ? result.written === true : result.deleted === true);
73
140
  if (outcome === false) {
74
- valid = validConflict(result.conflict) &&
75
- (result.currentRev === undefined || validRev(result.currentRev)) &&
76
- (target.verb !== 'batch' || (Number.isSafeInteger(result.index) &&
77
- result.index >= 0 && result.index < target.writes.length));
141
+ valid =
142
+ validConflict(result.conflict) &&
143
+ (result.currentRev === undefined || validRev(result.currentRev)) &&
144
+ (target.verb !== 'batch' ||
145
+ (Number.isSafeInteger(result.index) &&
146
+ result.index >= 0 &&
147
+ result.index < target.writes.length));
78
148
  }
79
149
  else if (outcome === true && !contradictsSuccess && target.verb === 'batch') {
80
- valid = target.writes.length > 0 && Array.isArray(result.results) && result.results.length === target.writes.length &&
81
- result.results.every((entry, index) => {
82
- const item = asPlainObject(entry);
83
- const requested = target.writes[index];
84
- return Boolean(item && ['put', 'patch', 'delete'].includes(String(requested.op)) &&
85
- typeof requested.collection === 'string' && requested.collection.length > 0 &&
86
- item.op === requested.op && item.collection === requested.collection &&
87
- validId(item.id) && (requested.id === undefined || item.id === requested.id) && validRev(item.rev));
88
- });
150
+ valid =
151
+ target.writes.length > 0 &&
152
+ Array.isArray(result.results) &&
153
+ result.results.length === target.writes.length &&
154
+ result.results.every((entry, index) => {
155
+ const item = asPlainObject(entry);
156
+ const requested = target.writes[index];
157
+ return Boolean(item &&
158
+ ['put', 'patch', 'delete'].includes(String(requested.op)) &&
159
+ typeof requested.collection === 'string' &&
160
+ requested.collection.length > 0 &&
161
+ item.op === requested.op &&
162
+ item.collection === requested.collection &&
163
+ validId(item.id) &&
164
+ (requested.id === undefined || item.id === requested.id) &&
165
+ validRev(item.rev));
166
+ });
89
167
  }
90
168
  else if (outcome === true && !contradictsSuccess && target.verb !== 'batch') {
91
- valid = result.id === target.id && validId(result.id) && validRev(result.rev) &&
92
- (target.verb === 'delete' || (typeof result.created === 'boolean' && typeof result.updatedAt === 'string' && result.updatedAt.length > 0));
169
+ valid =
170
+ result.id === target.id &&
171
+ validId(result.id) &&
172
+ validRev(result.rev) &&
173
+ (target.verb === 'delete' ||
174
+ (typeof result.created === 'boolean' &&
175
+ typeof result.updatedAt === 'string' &&
176
+ result.updatedAt.length > 0));
93
177
  }
94
178
  }
95
179
  if (!valid) {
@@ -148,6 +232,22 @@ function readWhereInput(text) {
148
232
  }
149
233
  return checked.value.where;
150
234
  }
235
+ /**
236
+ * The stable reason of one of the two cursor refusals (`invalid_cursor`: not a cursor this server handed out;
237
+ * `cursor_mismatch`: a cursor issued for another query), or `undefined`. Each constant is checked to be text first, so a
238
+ * contract build that predates one cannot make an absent reason read as a match.
239
+ */
240
+ function cursorRefusalReason(reason) {
241
+ if (typeof data_provider_1.APP_STORE_CURSOR_INVALID_REASON === 'string' &&
242
+ reason === data_provider_1.APP_STORE_CURSOR_INVALID_REASON) {
243
+ return data_provider_1.APP_STORE_CURSOR_INVALID_REASON;
244
+ }
245
+ if (typeof data_provider_1.APP_STORE_CURSOR_MISMATCH_REASON === 'string' &&
246
+ reason === data_provider_1.APP_STORE_CURSOR_MISMATCH_REASON) {
247
+ return data_provider_1.APP_STORE_CURSOR_MISMATCH_REASON;
248
+ }
249
+ return undefined;
250
+ }
151
251
  /**
152
252
  * What the person channel answered, read out of an API error: a refusal (`{ error, code, message }`, the code from
153
253
  * the contract's closed set) or a conflict (`409 { error: "conflict", conflict, currentRev? }`). `null` for anything
@@ -183,7 +283,8 @@ function readPersonDataFailure(error) {
183
283
  ...status,
184
284
  ...(typeof body.currentRev === 'number' ? { currentRev: body.currentRev } : {}),
185
285
  ...(typeof body.index === 'number' && Number.isSafeInteger(body.index) && body.index >= 0
186
- ? { index: body.index } : {}),
286
+ ? { index: body.index }
287
+ : {}),
187
288
  };
188
289
  }
189
290
  const code = data_provider_1.APP_PERSON_DATA_REFUSALS.includes(body.code)
@@ -197,12 +298,16 @@ function readPersonDataFailure(error) {
197
298
  code,
198
299
  ...status,
199
300
  ...(typeof body.message === 'string' ? { message: body.message } : {}),
200
- ...(code === 'unavailable' && body.reason === 'installation_suspended'
201
- ? { reason: 'installation_suspended' } : {}),
202
- ...(code === 'schema' && typeof data_provider_1.APP_STORE_CURSOR_INVALID_REASON === 'string' && body.reason === data_provider_1.APP_STORE_CURSOR_INVALID_REASON
203
- ? { reason: data_provider_1.APP_STORE_CURSOR_INVALID_REASON } : {}),
301
+ ...(code === 'unavailable' &&
302
+ (body.reason === 'installation_suspended' || body.reason === 'installation_changed')
303
+ ? { reason: body.reason }
304
+ : {}),
305
+ ...(code === 'schema' && cursorRefusalReason(body.reason) !== undefined
306
+ ? { reason: cursorRefusalReason(body.reason) }
307
+ : {}),
204
308
  ...(typeof body.index === 'number' && Number.isInteger(body.index) && body.index >= 0
205
- ? { index: body.index } : {}),
309
+ ? { index: body.index }
310
+ : {}),
206
311
  ...(typeof body.retryAfterMs === 'number' ? { retryAfterMs: body.retryAfterMs } : {}),
207
312
  };
208
313
  }
@@ -248,12 +353,25 @@ function describePersonDataRefusal(failure, bin, operation) {
248
353
  if (failure.code === 'unavailable' && failure.reason === 'installation_suspended') {
249
354
  return `This installation is paused, so the operation was not run. If you paused it, resume it with \`${bin} apps resume <installation>\`. A platform pause can only be lifted by the platform.`;
250
355
  }
356
+ if (failure.code === 'unavailable' && failure.reason === 'installation_changed') {
357
+ return 'The installation changed before the write could be applied. Refresh the installation and document revision before deciding whether to try again.';
358
+ }
251
359
  if (operation === 'query' && failure.code === 'schema') {
252
- if (typeof data_provider_1.APP_STORE_CURSOR_INVALID_MESSAGE === 'string' && data_provider_1.APP_STORE_CURSOR_INVALID_MESSAGE.length > 0 &&
253
- ((typeof data_provider_1.APP_STORE_CURSOR_INVALID_REASON === 'string' && failure.reason === data_provider_1.APP_STORE_CURSOR_INVALID_REASON) ||
360
+ if (typeof data_provider_1.APP_STORE_CURSOR_INVALID_MESSAGE === 'string' &&
361
+ data_provider_1.APP_STORE_CURSOR_INVALID_MESSAGE.length > 0 &&
362
+ ((typeof data_provider_1.APP_STORE_CURSOR_INVALID_REASON === 'string' &&
363
+ failure.reason === data_provider_1.APP_STORE_CURSOR_INVALID_REASON) ||
254
364
  failure.message === data_provider_1.APP_STORE_CURSOR_INVALID_MESSAGE)) {
255
365
  return data_provider_1.APP_STORE_CURSOR_INVALID_MESSAGE;
256
366
  }
367
+ // a cursor that is fine but belongs to another query: say that, not "the query is invalid" (the query IS valid)
368
+ const cursorReason = cursorRefusalReason(failure.reason);
369
+ if (typeof data_provider_1.APP_STORE_CURSOR_MISMATCH_MESSAGE === 'string' &&
370
+ data_provider_1.APP_STORE_CURSOR_MISMATCH_MESSAGE.length > 0 &&
371
+ ((cursorReason !== undefined && cursorReason === data_provider_1.APP_STORE_CURSOR_MISMATCH_REASON) ||
372
+ failure.message === data_provider_1.APP_STORE_CURSOR_MISMATCH_MESSAGE)) {
373
+ return data_provider_1.APP_STORE_CURSOR_MISMATCH_MESSAGE;
374
+ }
257
375
  return 'The query is invalid. Check the collection and query options; --id cannot be combined with --where or --cursor.';
258
376
  }
259
377
  if (failure.code === 'schema' && failure.message?.trim()) {
@@ -279,13 +397,19 @@ function describePersonDataConflict(failure, target, bin) {
279
397
  const verb = target.verb ?? 'put';
280
398
  if (verb === 'batch') {
281
399
  const index = Number.isSafeInteger(failure.index) && failure.index >= 0
282
- ? ` at index ${failure.index} (zero-based)` : '';
400
+ ? ` at index ${failure.index} (zero-based)`
401
+ : '';
283
402
  const revision = Number.isSafeInteger(failure.currentRev) && failure.currentRev >= 0
284
- ? ` The server reported revision ${failure.currentRev}.` : '';
285
- const reason = failure.conflict === 'rev_mismatch' ? 'has changed'
286
- : failure.conflict === 'exists' ? 'already exists' : 'was not found';
403
+ ? ` The server reported revision ${failure.currentRev}.`
404
+ : '';
405
+ const reason = failure.conflict === 'rev_mismatch'
406
+ ? 'has changed'
407
+ : failure.conflict === 'exists'
408
+ ? 'already exists'
409
+ : 'was not found';
287
410
  const item = Number.isSafeInteger(failure.index) && failure.index >= 0
288
- ? target.batchTargets?.[failure.index] : undefined;
411
+ ? target.batchTargets?.[failure.index]
412
+ : undefined;
289
413
  const label = (value) => typeof value === 'string' && value.length > 0
290
414
  ? safeForTerminal(JSON.stringify(value.slice(0, 128))) + (value.length > 128 ? '…' : '')
291
415
  : null;
@@ -293,7 +417,11 @@ function describePersonDataConflict(failure, target, bin) {
293
417
  const id = label(item?.id);
294
418
  return [
295
419
  `Nothing in the batch was written. The document for the write${index} ${reason}.${revision}`,
296
- ...(collection || id ? [` ${collection ? `collection ${collection}` : 'collection not provided'}; ${id ? `document ${id}` : 'document ID not provided'}.`] : []),
420
+ ...(collection || id
421
+ ? [
422
+ ` ${collection ? `collection ${collection}` : 'collection not provided'}; ${id ? `document ${id}` : 'document ID not provided'}.`,
423
+ ]
424
+ : []),
297
425
  '→ Inspect that write in your batch input. Use its collection and document ID to query the current data, then reconcile and update the affected operation before submitting the batch again.',
298
426
  ].join('\n');
299
427
  }
@@ -460,7 +588,8 @@ function formatCollections(catalog) {
460
588
  lines.push(` fields: ${fields || '(none named)'}${collection.schema.moreFields > 0 ? `, +${collection.schema.moreFields} more` : ''}${collection.schema.closed ? ' (other fields are refused)' : ''}`);
461
589
  }
462
590
  }
463
- if (typeof catalog.notice !== 'string' || !catalog.notice.includes(data_provider_1.APP_DATA_WRITER_DECLARATION_NOTICE)) {
591
+ if (typeof catalog.notice !== 'string' ||
592
+ !catalog.notice.includes(data_provider_1.APP_DATA_WRITER_DECLARATION_NOTICE)) {
464
593
  lines.push('Declared writers are not a permission check. Each call also depends on the current grant, installation state and caller permissions.');
465
594
  }
466
595
  lines.push(safeForTerminal(catalog.notice));