@intentic/sandbox-contract 1.432.0 → 1.434.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/README.md +15 -0
  2. package/dist/contracts/agent.contract.d.ts +252 -72
  3. package/dist/contracts/agent.contract.d.ts.map +1 -1
  4. package/dist/contracts/agents.contract.d.ts +67 -0
  5. package/dist/contracts/agents.contract.d.ts.map +1 -1
  6. package/dist/contracts/approvals.contract.d.ts +35 -0
  7. package/dist/contracts/approvals.contract.d.ts.map +1 -1
  8. package/dist/contracts/approvals.contract.js +13 -3
  9. package/dist/contracts/approvals.contract.js.map +1 -1
  10. package/dist/contracts/automations.contract.d.ts +1 -0
  11. package/dist/contracts/automations.contract.d.ts.map +1 -1
  12. package/dist/contracts/capabilities.contract.d.ts +3 -0
  13. package/dist/contracts/capabilities.contract.d.ts.map +1 -1
  14. package/dist/contracts/extensions.contract.d.ts +4 -0
  15. package/dist/contracts/extensions.contract.d.ts.map +1 -1
  16. package/dist/contracts/needs.contract.d.ts +198 -0
  17. package/dist/contracts/needs.contract.d.ts.map +1 -1
  18. package/dist/contracts/runner.contract.d.ts +156 -36
  19. package/dist/contracts/runner.contract.d.ts.map +1 -1
  20. package/dist/contracts/seal.contract.d.ts +5 -0
  21. package/dist/contracts/seal.contract.d.ts.map +1 -1
  22. package/dist/contracts/sessions.contract.d.ts +22 -0
  23. package/dist/contracts/sessions.contract.d.ts.map +1 -1
  24. package/dist/contracts/settings.contract.d.ts +16 -0
  25. package/dist/contracts/settings.contract.d.ts.map +1 -1
  26. package/dist/contracts/stack-grants.contract.d.ts +22 -0
  27. package/dist/contracts/stack-grants.contract.d.ts.map +1 -1
  28. package/dist/contracts/system.contract.d.ts +7 -4
  29. package/dist/contracts/system.contract.d.ts.map +1 -1
  30. package/dist/events/agent-events.d.ts +88 -0
  31. package/dist/events/agent-events.d.ts.map +1 -1
  32. package/dist/events/agent-transcript.d.ts +22 -0
  33. package/dist/events/agent-transcript.d.ts.map +1 -1
  34. package/dist/events/system-events.d.ts +4 -0
  35. package/dist/events/system-events.d.ts.map +1 -1
  36. package/dist/events/transcript.d.ts +110 -0
  37. package/dist/events/transcript.d.ts.map +1 -1
  38. package/dist/ids/conversation-ids.d.ts.map +1 -1
  39. package/dist/ids/conversation-ids.js.map +1 -1
  40. package/dist/protocol/raw/extension-protocol.d.ts +116 -0
  41. package/dist/protocol/raw/extension-protocol.d.ts.map +1 -1
  42. package/dist/protocol/raw/extension-protocol.js +17 -1
  43. package/dist/protocol/raw/extension-protocol.js.map +1 -1
  44. package/dist/protocol/raw/raw-json-routes.d.ts +2974 -0
  45. package/dist/protocol/raw/raw-json-routes.d.ts.map +1 -1
  46. package/dist/protocol/raw/raw-json-routes.js +39 -0
  47. package/dist/protocol/raw/raw-json-routes.js.map +1 -1
  48. package/dist/protocol/raw/raw-routes.d.ts +179 -0
  49. package/dist/protocol/raw/raw-routes.d.ts.map +1 -1
  50. package/dist/protocol/raw/raw-routes.js +41 -0
  51. package/dist/protocol/raw/raw-routes.js.map +1 -1
  52. package/dist/protocol/s2s/address.d.ts +53 -0
  53. package/dist/protocol/s2s/address.d.ts.map +1 -0
  54. package/dist/protocol/s2s/address.js +111 -0
  55. package/dist/protocol/s2s/address.js.map +1 -0
  56. package/dist/protocol/s2s/attestation.d.ts +41 -0
  57. package/dist/protocol/s2s/attestation.d.ts.map +1 -0
  58. package/dist/protocol/s2s/attestation.js +96 -0
  59. package/dist/protocol/s2s/attestation.js.map +1 -0
  60. package/dist/protocol/s2s/canonical.d.ts +10 -0
  61. package/dist/protocol/s2s/canonical.d.ts.map +1 -0
  62. package/dist/protocol/s2s/canonical.js +94 -0
  63. package/dist/protocol/s2s/canonical.js.map +1 -0
  64. package/dist/protocol/s2s/envelope.d.ts +40 -0
  65. package/dist/protocol/s2s/envelope.d.ts.map +1 -0
  66. package/dist/protocol/s2s/envelope.js +116 -0
  67. package/dist/protocol/s2s/envelope.js.map +1 -0
  68. package/dist/protocol/s2s/extension-sign.d.ts +21 -0
  69. package/dist/protocol/s2s/extension-sign.d.ts.map +1 -0
  70. package/dist/protocol/s2s/extension-sign.js +41 -0
  71. package/dist/protocol/s2s/extension-sign.js.map +1 -0
  72. package/dist/protocol/s2s/identity-doc.d.ts +50 -0
  73. package/dist/protocol/s2s/identity-doc.d.ts.map +1 -0
  74. package/dist/protocol/s2s/identity-doc.js +89 -0
  75. package/dist/protocol/s2s/identity-doc.js.map +1 -0
  76. package/dist/protocol/s2s/index.d.ts +12 -0
  77. package/dist/protocol/s2s/index.d.ts.map +1 -0
  78. package/dist/protocol/s2s/index.js +12 -0
  79. package/dist/protocol/s2s/index.js.map +1 -0
  80. package/dist/protocol/s2s/kem.d.ts +15 -0
  81. package/dist/protocol/s2s/kem.d.ts.map +1 -0
  82. package/dist/protocol/s2s/kem.js +34 -0
  83. package/dist/protocol/s2s/kem.js.map +1 -0
  84. package/dist/protocol/s2s/keys.d.ts +34 -0
  85. package/dist/protocol/s2s/keys.d.ts.map +1 -0
  86. package/dist/protocol/s2s/keys.js +81 -0
  87. package/dist/protocol/s2s/keys.js.map +1 -0
  88. package/dist/protocol/s2s/passkey-cosign.d.ts +44 -0
  89. package/dist/protocol/s2s/passkey-cosign.d.ts.map +1 -0
  90. package/dist/protocol/s2s/passkey-cosign.js +139 -0
  91. package/dist/protocol/s2s/passkey-cosign.js.map +1 -0
  92. package/dist/protocol/s2s/session.d.ts +61 -0
  93. package/dist/protocol/s2s/session.d.ts.map +1 -0
  94. package/dist/protocol/s2s/session.js +98 -0
  95. package/dist/protocol/s2s/session.js.map +1 -0
  96. package/dist/protocol/s2s/sign.d.ts +12 -0
  97. package/dist/protocol/s2s/sign.d.ts.map +1 -0
  98. package/dist/protocol/s2s/sign.js +33 -0
  99. package/dist/protocol/s2s/sign.js.map +1 -0
  100. package/dist/schemas/agents-list.d.ts +2 -0
  101. package/dist/schemas/agents-list.d.ts.map +1 -1
  102. package/dist/schemas/agents.d.ts +4 -0
  103. package/dist/schemas/agents.d.ts.map +1 -1
  104. package/dist/schemas/approvals.d.ts +100 -0
  105. package/dist/schemas/approvals.d.ts.map +1 -1
  106. package/dist/schemas/approvals.js +21 -0
  107. package/dist/schemas/approvals.js.map +1 -1
  108. package/dist/schemas/automations.d.ts +2 -0
  109. package/dist/schemas/automations.d.ts.map +1 -1
  110. package/dist/schemas/automations.js +1 -1
  111. package/dist/schemas/automations.js.map +1 -1
  112. package/dist/schemas/capabilities.d.ts +1 -0
  113. package/dist/schemas/capabilities.d.ts.map +1 -1
  114. package/dist/schemas/command-admission.d.ts +6 -0
  115. package/dist/schemas/command-admission.d.ts.map +1 -1
  116. package/dist/schemas/command-admission.js +2 -1
  117. package/dist/schemas/command-admission.js.map +1 -1
  118. package/dist/schemas/contact-need.d.ts +50 -0
  119. package/dist/schemas/contact-need.d.ts.map +1 -0
  120. package/dist/schemas/contact-need.js +19 -0
  121. package/dist/schemas/contact-need.js.map +1 -0
  122. package/dist/schemas/contacts-settings.d.ts +5 -0
  123. package/dist/schemas/contacts-settings.d.ts.map +1 -0
  124. package/dist/schemas/contacts-settings.js +11 -0
  125. package/dist/schemas/contacts-settings.js.map +1 -0
  126. package/dist/schemas/extension-updates.d.ts +8 -0
  127. package/dist/schemas/extension-updates.d.ts.map +1 -1
  128. package/dist/schemas/issues.d.ts +2 -0
  129. package/dist/schemas/issues.d.ts.map +1 -1
  130. package/dist/schemas/issues.js +1 -0
  131. package/dist/schemas/issues.js.map +1 -1
  132. package/dist/schemas/links.d.ts +8 -0
  133. package/dist/schemas/links.d.ts.map +1 -1
  134. package/dist/schemas/links.js +4 -0
  135. package/dist/schemas/links.js.map +1 -1
  136. package/dist/schemas/need-kinds.d.ts +2 -1
  137. package/dist/schemas/need-kinds.d.ts.map +1 -1
  138. package/dist/schemas/need-kinds.js +2 -2
  139. package/dist/schemas/need-kinds.js.map +1 -1
  140. package/dist/schemas/needs.d.ts +156 -13
  141. package/dist/schemas/needs.d.ts.map +1 -1
  142. package/dist/schemas/needs.js +5 -12
  143. package/dist/schemas/needs.js.map +1 -1
  144. package/dist/schemas/redirect-need.d.ts +15 -0
  145. package/dist/schemas/redirect-need.d.ts.map +1 -0
  146. package/dist/schemas/redirect-need.js +14 -0
  147. package/dist/schemas/redirect-need.js.map +1 -0
  148. package/dist/schemas/s2s-identity.d.ts +521 -0
  149. package/dist/schemas/s2s-identity.d.ts.map +1 -0
  150. package/dist/schemas/s2s-identity.js +208 -0
  151. package/dist/schemas/s2s-identity.js.map +1 -0
  152. package/dist/schemas/s2s-views.d.ts +2127 -0
  153. package/dist/schemas/s2s-views.d.ts.map +1 -0
  154. package/dist/schemas/s2s-views.js +217 -0
  155. package/dist/schemas/s2s-views.js.map +1 -0
  156. package/dist/schemas/s2s-wire.d.ts +1616 -0
  157. package/dist/schemas/s2s-wire.d.ts.map +1 -0
  158. package/dist/schemas/s2s-wire.js +276 -0
  159. package/dist/schemas/s2s-wire.js.map +1 -0
  160. package/dist/schemas/s2s.d.ts +5 -0
  161. package/dist/schemas/s2s.d.ts.map +1 -0
  162. package/dist/schemas/s2s.js +5 -0
  163. package/dist/schemas/s2s.js.map +1 -0
  164. package/dist/schemas/settings.d.ts +16 -0
  165. package/dist/schemas/settings.d.ts.map +1 -1
  166. package/dist/schemas/settings.js +2 -0
  167. package/dist/schemas/settings.js.map +1 -1
  168. package/dist/schemas/terminal.d.ts +2 -0
  169. package/dist/schemas/terminal.d.ts.map +1 -1
  170. package/dist/schemas/terminal.js +4 -0
  171. package/dist/schemas/terminal.js.map +1 -1
  172. package/dist/schemas/vpn.d.ts +4 -0
  173. package/dist/schemas/vpn.d.ts.map +1 -1
  174. package/dist/schemas/vpn.js +9 -0
  175. package/dist/schemas/vpn.js.map +1 -1
  176. package/dist/state/auth-state.d.ts.map +1 -1
  177. package/dist/state/auth-state.js +7 -0
  178. package/dist/state/auth-state.js.map +1 -1
  179. package/dist/state/definition.d.ts +18 -0
  180. package/dist/state/definition.d.ts.map +1 -1
  181. package/package.json +20 -5
  182. package/src/contracts/approvals.contract.ts +24 -3
  183. package/src/ids/conversation-ids.test.ts +22 -3
  184. package/src/ids/conversation-ids.ts +5 -7
  185. package/src/protocol/ingress-contract.fixture.json +6 -1
  186. package/src/protocol/raw/extension-protocol.ts +45 -1
  187. package/src/protocol/raw/raw-json-routes.ts +85 -0
  188. package/src/protocol/raw/raw-routes.ts +66 -3
  189. package/src/protocol/s2s/address.test.ts +157 -0
  190. package/src/protocol/s2s/address.ts +193 -0
  191. package/src/protocol/s2s/attestation.test.ts +262 -0
  192. package/src/protocol/s2s/attestation.ts +173 -0
  193. package/src/protocol/s2s/canonical.test.ts +70 -0
  194. package/src/protocol/s2s/canonical.ts +119 -0
  195. package/src/protocol/s2s/envelope.test.ts +162 -0
  196. package/src/protocol/s2s/envelope.ts +194 -0
  197. package/src/protocol/s2s/extension-sign.test.ts +117 -0
  198. package/src/protocol/s2s/extension-sign.ts +76 -0
  199. package/src/protocol/s2s/identity-doc.test.ts +187 -0
  200. package/src/protocol/s2s/identity-doc.ts +169 -0
  201. package/src/protocol/s2s/index.ts +15 -0
  202. package/src/protocol/s2s/kem.ts +62 -0
  203. package/src/protocol/s2s/keys.test.ts +76 -0
  204. package/src/protocol/s2s/keys.ts +155 -0
  205. package/src/protocol/s2s/passkey-cosign.test.ts +98 -0
  206. package/src/protocol/s2s/passkey-cosign.testing.ts +86 -0
  207. package/src/protocol/s2s/passkey-cosign.ts +192 -0
  208. package/src/protocol/s2s/s2s.testing.ts +22 -0
  209. package/src/protocol/s2s/session.test.ts +133 -0
  210. package/src/protocol/s2s/session.ts +179 -0
  211. package/src/protocol/s2s/sign.test.ts +66 -0
  212. package/src/protocol/s2s/sign.ts +53 -0
  213. package/src/schemas/approvals.ts +41 -0
  214. package/src/schemas/automations.ts +4 -3
  215. package/src/schemas/command-admission.ts +4 -1
  216. package/src/schemas/contact-need.ts +26 -0
  217. package/src/schemas/contacts-settings.ts +16 -0
  218. package/src/schemas/issues.ts +1 -0
  219. package/src/schemas/links.ts +4 -0
  220. package/src/schemas/need-kinds.ts +2 -2
  221. package/src/schemas/needs.ts +5 -18
  222. package/src/schemas/redirect-need.ts +19 -0
  223. package/src/schemas/s2s-identity.ts +329 -0
  224. package/src/schemas/s2s-views.ts +326 -0
  225. package/src/schemas/s2s-wire.ts +401 -0
  226. package/src/schemas/s2s.test.ts +175 -0
  227. package/src/schemas/s2s.ts +9 -0
  228. package/src/schemas/settings.ts +2 -0
  229. package/src/schemas/terminal.ts +6 -0
  230. package/src/schemas/version-seam.test.ts +9 -0
  231. package/src/schemas/vpn.ts +13 -0
  232. package/src/state/auth-state.ts +13 -0
@@ -0,0 +1,329 @@
1
+ import { z } from "zod";
2
+ import { LinkTermsSchema } from "./links.js";
3
+
4
+ // SANDBOX-TO-SANDBOX (docs/architecture/messages.md), the identity half: what a sandbox is to another one (an address
5
+ // and a hybrid post-quantum key set, published as a signed document), and what it lets a contact do here. Re-exported
6
+ // from schemas/s2s.ts with the wire and editor shapes; the primitives that make and check these live in protocol/s2s/.
7
+
8
+ // The protocol a daemon speaks when it answers GET /s2s/identity, named in every identity document it publishes.
9
+ export const S2S_PROTOCOL = "s2s/1";
10
+ // The HKDF info and signing prefix: every key this protocol derives and every signature it makes is bound to it.
11
+ export const S2S_INFO = "intentic-s2s/1";
12
+ // A key set's fingerprint: this prefix and the lowercase base32 (RFC 4648, no padding) of a SHA-256, 52 characters.
13
+ export const S2S_FINGERPRINT_PREFIX = "isk1:";
14
+ // A recovery key's fingerprint, prefixed apart so the one can never be read as the other.
15
+ export const S2S_RECOVERY_PREFIX = "isr1:";
16
+ // What a recovery secret looks like: this prefix and the base64url of its two signing seeds.
17
+ export const S2S_RECOVERY_SECRET_PREFIX = "isrs1.";
18
+ // The DNS name an own domain publishes its record under: `_intentic.<domain>`.
19
+ export const S2S_TXT_LABEL = "_intentic";
20
+ // How many earlier key sets the key store keeps, so an envelope sealed to a key just rotated away still opens.
21
+ export const S2S_PREVIOUS_KEYS_MAX = 3;
22
+ // How many own domains one identity document lists.
23
+ export const S2S_DOMAINS_MAX = 16;
24
+
25
+ // How long a person's note on a request, a removal or a contact need may be.
26
+ export const S2S_NOTE_MAX = 1_000;
27
+
28
+ // A contact's, a thread's or a task's id, or another id one side gave and the other repeats back.
29
+ export const S2sRefSchema = z
30
+ .string()
31
+ .min(1)
32
+ .max(100)
33
+ .regex(/^[A-Za-z0-9][A-Za-z0-9_.:-]*$/u);
34
+
35
+ // Bytes on the wire: base64url (RFC 4648 §5), no padding.
36
+ export const S2sBase64UrlSchema = z.string().regex(/^[A-Za-z0-9_-]*$/u, "base64url, without padding");
37
+
38
+ export const S2sFingerprintSchema = z
39
+ .string()
40
+ .regex(/^isk1:[a-z2-7]{52}$/u)
41
+ .describe("A key set's fingerprint: `isk1:` and the base32 of the SHA-256 over its canonical public keys. What a person compares and a contact pins.");
42
+ export type S2sFingerprint = z.infer<typeof S2sFingerprintSchema>;
43
+
44
+ export const S2sRecoveryFingerprintSchema = z
45
+ .string()
46
+ .regex(/^isr1:[a-z2-7]{52}$/u)
47
+ .describe("A recovery key's fingerprint: `isr1:` and the base32 of the SHA-256 over its canonical public signing keys.");
48
+
49
+ // The two signature algorithms' public keys, base64url: what a recovery key is, and half of a key set.
50
+ export const S2sSigningKeysSchema = z.object({
51
+ ed25519: S2sBase64UrlSchema.describe("Ed25519 public key, 32 bytes."),
52
+ mldsa65: S2sBase64UrlSchema.describe("ML-DSA-65 public key, 1952 bytes."),
53
+ });
54
+ export type S2sSigningKeys = z.infer<typeof S2sSigningKeysSchema>;
55
+
56
+ // A whole public key set: the signing pair and the key-agreement pair.
57
+ export const S2sPublicKeysSchema = S2sSigningKeysSchema.extend({
58
+ x25519: S2sBase64UrlSchema.describe("X25519 public key, 32 bytes."),
59
+ mlkem768: S2sBase64UrlSchema.describe("ML-KEM-768 encapsulation key, 1184 bytes."),
60
+ });
61
+ export type S2sPublicKeys = z.infer<typeof S2sPublicKeysSchema>;
62
+
63
+ // Every signature is hybrid: both halves over the same bytes, and both must verify.
64
+ export const HybridSigSchema = z
65
+ .object({
66
+ ed25519: S2sBase64UrlSchema.describe("Ed25519 signature, 64 bytes."),
67
+ mldsa65: S2sBase64UrlSchema.describe("ML-DSA-65 signature, 3309 bytes."),
68
+ })
69
+ .describe("A hybrid signature: Ed25519 and ML-DSA-65 over the same bytes. Either alone verifies nothing.");
70
+ export type HybridSig = z.infer<typeof HybridSigSchema>;
71
+
72
+ // A DNS name in lowercase ASCII (an IDN in its punycode): at least two labels, each 1-63 of a-z, 0-9 and inner hyphens.
73
+ const HOSTNAME = String.raw`(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?`;
74
+
75
+ export const S2sDomainSchema = z
76
+ .string()
77
+ .max(253)
78
+ .regex(new RegExp(`^${HOSTNAME}$`, "u"))
79
+ .describe("An own domain, lowercase, no port: what a TXT record under `_intentic.<domain>` vouches for.");
80
+
81
+ // A host or an own domain; a host may carry a port, which only a loopback or a test daemon has.
82
+ export const S2sAddressSchema = z
83
+ .string()
84
+ .max(259)
85
+ .regex(new RegExp(`^(?:${HOSTNAME}|localhost)(?::[0-9]{1,5})?$`, "u"))
86
+ .describe("Where a sandbox is reached: its host (`<id>.<zone>`) or an own domain, lowercase.");
87
+ export type S2sAddress = z.infer<typeof S2sAddressSchema>;
88
+
89
+ // What an identity document's `prev` proves: that the key set before this one, or the recovery key, vouched for this
90
+ // one as its successor. The vouch is over the sandbox, the new fingerprint and itself, not the whole document, so the
91
+ // document can be signed again as its facts change without the voucher: a recovery key is never kept on the sandbox.
92
+ const vouchedAt = z.number().int().nonnegative().describe("When the vouch was made, in epoch milliseconds.");
93
+ const vouchSig = HybridSigSchema.describe("The voucher's signature over the canonical `{ sandbox, successor, by, fingerprint, at }`.");
94
+ export const S2sIdentityPrevSchema = z.discriminatedUnion("by", [
95
+ z.object({
96
+ by: z.literal("key"),
97
+ fingerprint: S2sFingerprintSchema.describe("The previous key set's fingerprint."),
98
+ keys: S2sPublicKeysSchema.describe("The previous key set's public keys, which the fingerprint is over."),
99
+ at: vouchedAt,
100
+ sig: vouchSig,
101
+ }),
102
+ z.object({
103
+ by: z.literal("recovery"),
104
+ fingerprint: S2sRecoveryFingerprintSchema.describe("The recovery key's fingerprint, as the earlier document published it."),
105
+ keys: S2sSigningKeysSchema.describe("The recovery key's public signing keys, which the fingerprint is over."),
106
+ at: vouchedAt,
107
+ sig: vouchSig,
108
+ }),
109
+ ]);
110
+ export type S2sIdentityPrev = z.infer<typeof S2sIdentityPrevSchema>;
111
+
112
+ /* ATTESTATIONS (docs/architecture/messages.md, "Attestations"): another sandbox's signed word about this one, carried in
113
+ this one's document. It says who vouched and for what, never that the claim is true; which issuers count is each
114
+ reader's own choice, by fingerprint. Every object in one is loose: the document's signature covers it whole, so a
115
+ field a later build adds must survive this build's parse, or the document would stop verifying here. */
116
+
117
+ // What a daemon reads of another's identity document at most (s2s-wiring.ts IDENTITY_BODY_MAX, which older daemons keep).
118
+ export const S2S_IDENTITY_MAX_BYTES = 64 * 1024;
119
+ // The most attestations a document lists as any reader parses it. A vouch carries its issuer's four public keys and a
120
+ // hybrid signature, about 10 KB, so no document under S2S_IDENTITY_MAX_BYTES comes near: this bound only refuses junk.
121
+ export const S2S_ATTESTATIONS_MAX = 16;
122
+ // The most this sandbox keeps and publishes: four of the largest vouches fit beside the largest document there is (sixteen
123
+ // domains and a `prev`) inside S2S_IDENTITY_MAX_BYTES (attestation.test.ts measures it).
124
+ export const S2S_ATTESTATIONS_KEPT_MAX = 4;
125
+ // How long one attestation may be valid from when it was issued: a year, so a vouch nobody renews lapses by itself.
126
+ export const S2S_ATTESTATION_VALID_MAX_MS = 365 * 24 * 60 * 60 * 1000;
127
+ // Claims: how many, how long a word may be, and how many bytes of JSON they come to whole. The bytes are what keeps four
128
+ // of the largest vouches inside S2S_IDENTITY_MAX_BYTES: at 1,024 the fullest document came to 66,104 bytes.
129
+ export const S2S_CLAIMS_MAX = 12;
130
+ export const S2S_CLAIM_VALUE_MAX = 120;
131
+ export const S2S_CLAIMS_BYTES_MAX = 768;
132
+ // The claims a reader can expect to mean the same whoever wrote them; any other key is the issuer's own word.
133
+ // `kind`: what the subject is (S2S_CLAIM_KINDS). `verifiedBy`: how the issuer checked (S2S_CLAIM_METHODS). `country`: an
134
+ // ISO 3166-1 alpha-2 code. `over18`: true when the issuer checked the person is an adult. `displayName`: the name the
135
+ // issuer knows the subject by. `organisation`: the organisation the subject is or belongs to.
136
+ export const S2S_WELL_KNOWN_CLAIMS = ["kind", "verifiedBy", "country", "over18", "displayName", "organisation"] as const;
137
+ export const S2S_CLAIM_KINDS = ["person", "organisation"] as const;
138
+ export const S2S_CLAIM_METHODS = ["in-person", "document", "domain", "employment"] as const;
139
+ // The types this build knows. `sandbox-vouch` it verifies; `eudi-pid` is a slot for an EU digital identity wallet's
140
+ // person identification data, parsed and never verified yet (it needs relying-party registration and a real wallet).
141
+ export const S2S_ATTESTATION_TYPES = ["sandbox-vouch", "eudi-pid"] as const;
142
+
143
+ const utf8Length = (text: string): number => new TextEncoder().encode(text).length;
144
+
145
+ export const S2sClaimKeySchema = z
146
+ .string()
147
+ .regex(/^[a-z][A-Za-z0-9_-]{0,39}$/u)
148
+ .describe("A claim's name: a lowercase letter, then up to 39 letters, digits, `_` or `-` (S2S_WELL_KNOWN_CLAIMS).");
149
+ export const S2sClaimValueSchema = z.union([z.string().max(S2S_CLAIM_VALUE_MAX), z.number(), z.boolean()]);
150
+ export const S2sClaimsSchema = z
151
+ .record(S2sClaimKeySchema, S2sClaimValueSchema)
152
+ .refine((claims) => Object.keys(claims).length <= S2S_CLAIMS_MAX, `at most ${S2S_CLAIMS_MAX} claims`)
153
+ .refine((claims) => utf8Length(JSON.stringify(claims)) <= S2S_CLAIMS_BYTES_MAX, `at most ${S2S_CLAIMS_BYTES_MAX} bytes of claims`)
154
+ .describe("What the issuer says of the subject, by name: a word, a number or a yes/no each.");
155
+ export type S2sClaims = z.infer<typeof S2sClaimsSchema>;
156
+
157
+ const attestedSandbox = {
158
+ sandbox: z.string().min(1).max(100).describe("Its sandbox id, as its own document names it."),
159
+ address: S2sAddressSchema.describe("Where it was reached: its host or an own domain."),
160
+ fingerprint: S2sFingerprintSchema.describe("Its key set's fingerprint when the attestation was made."),
161
+ };
162
+ const attestedAt = {
163
+ issuedAt: z.number().int().nonnegative().describe("When it was made, in epoch milliseconds."),
164
+ validUntil: z.number().int().nonnegative().describe("When it lapses, in epoch milliseconds: at most a year after `issuedAt`."),
165
+ };
166
+ export const S2sAttestationSubjectSchema = z.looseObject(attestedSandbox).describe("The sandbox it is about.");
167
+ export const S2sAttestationIssuerSchema = z
168
+ .looseObject({
169
+ ...attestedSandbox,
170
+ // All four, not the signing pair alone: a key set's fingerprint is over all four, and the fingerprint is what a
171
+ // reader trusts an issuer by.
172
+ keys: S2sPublicKeysSchema.loose().describe("The issuer's public keys, which its fingerprint is over."),
173
+ name: z.string().max(120).optional().describe("The issuer's display name, as its document gave it."),
174
+ })
175
+ .describe("The sandbox that vouches, with the keys its signature verifies under.");
176
+
177
+ const vouchFields = {
178
+ v: z.literal(1),
179
+ type: z.literal("sandbox-vouch"),
180
+ issuer: S2sAttestationIssuerSchema,
181
+ subject: S2sAttestationSubjectSchema,
182
+ claims: S2sClaimsSchema,
183
+ ...attestedAt,
184
+ sig: HybridSigSchema.loose().describe("The issuer's signature over the canonical attestation without `sig`, signed for `attestation`."),
185
+ };
186
+ export const S2sSandboxVouchSchema = z
187
+ .looseObject(vouchFields)
188
+ .describe("Another sandbox's signed word about this one: who vouched, about which key, for what, until when.");
189
+ export type S2sSandboxVouch = z.infer<z.ZodObject<typeof vouchFields>>;
190
+
191
+ const eudiPidFields = {
192
+ v: z.literal(1),
193
+ type: z.literal("eudi-pid"),
194
+ format: z.enum(["dc+sd-jwt", "mso_mdoc"]).describe("The credential's format, as OpenID for Verifiable Presentations names it."),
195
+ presentation: z.string().min(1).max(8192).describe("The wallet's presentation, as it gave it."),
196
+ subject: S2sAttestationSubjectSchema,
197
+ ...attestedAt,
198
+ };
199
+ export const S2sEudiPidAttestationSchema = z
200
+ .looseObject(eudiPidFields)
201
+ .describe("Reserved: a person identification credential from an EU digital identity wallet. Parsed, never verified yet.");
202
+ export type S2sEudiPidAttestation = z.infer<z.ZodObject<typeof eudiPidFields>>;
203
+
204
+ export const S2sOtherAttestationSchema = z
205
+ .looseObject({ type: z.string().min(1).max(64) })
206
+ .describe("An attestation this build cannot read (a later type, or a known one it cannot parse), kept whole so the document carrying it still verifies.");
207
+ export type S2sOtherAttestation = z.infer<typeof S2sOtherAttestationSchema>;
208
+
209
+ // The first that parses: a vouch, the wallet slot, else anything with a type, kept as it came.
210
+ export const S2sAttestationSchema = z.union([S2sSandboxVouchSchema, S2sEudiPidAttestationSchema, S2sOtherAttestationSchema]);
211
+ export type S2sAttestation = S2sSandboxVouch | S2sEudiPidAttestation | S2sOtherAttestation;
212
+
213
+ // The public identity document, served at GET /s2s/identity.
214
+ const identityFields = {
215
+ v: z.literal(1),
216
+ sandbox: z.string().min(1).max(100).describe("The sandbox's own id."),
217
+ name: z.string().max(120).describe("Its display name."),
218
+ host: S2sAddressSchema.describe("Its current host, where /s2s/* answers."),
219
+ domains: z.array(S2sDomainSchema).max(S2S_DOMAINS_MAX).describe("The own domains it verified (each with a matching TXT record), sorted."),
220
+ keys: S2sPublicKeysSchema,
221
+ fingerprint: S2sFingerprintSchema,
222
+ recovery: S2sRecoveryFingerprintSchema.optional().describe("Its recovery key's fingerprint, when it made one."),
223
+ protocols: z.array(z.string().min(1).max(40)).min(1).max(16).describe("The protocols it speaks: `s2s/1`."),
224
+ issuedAt: z.number().int().nonnegative().describe("When this document was signed, in epoch milliseconds."),
225
+ prev: S2sIdentityPrevSchema.optional(),
226
+ attestations: z
227
+ .array(S2sAttestationSchema)
228
+ .max(S2S_ATTESTATIONS_MAX)
229
+ .optional()
230
+ .describe("What other sandboxes signed about this one, as it chose to publish them; absent when none. Each reader decides which issuers it trusts."),
231
+ sig: HybridSigSchema.describe("Its own key set's signature over the canonical document without `sig`."),
232
+ };
233
+ // Loose on purpose: a later document may carry fields this build does not know, and its signature is over all of them,
234
+ // so none may be dropped on the way in. The type names only the fields this build knows.
235
+ export const SandboxIdentitySchema = z
236
+ .looseObject(identityFields)
237
+ .describe("A sandbox's signed public identity: who it is, where, and the keys every message to and from it uses.");
238
+ export type SandboxIdentity = z.infer<z.ZodObject<typeof identityFields>>;
239
+
240
+ // A domain's TXT record, parsed: `v=isk1 fp=<fingerprint> host=<host>`.
241
+ export const S2sTxtRecordSchema = z.object({
242
+ fingerprint: S2sFingerprintSchema,
243
+ host: S2sAddressSchema,
244
+ });
245
+ export type S2sTxtRecord = z.infer<typeof S2sTxtRecordSchema>;
246
+
247
+ /* GRANTS: what this sandbox lets a contact do here, decided here and only told to the other side. */
248
+
249
+ // The link terms a task grant carries (schemas/links.ts), the same fields under the same names with the same defaults,
250
+ // so an upgraded link keeps its terms exactly: `actsAs` is the role its tasks wear.
251
+ export const S2sTaskGrantSchema = LinkTermsSchema.pick({ actsAs: true, runAs: true, approve: true, turnsPerHour: true }).describe(
252
+ "Tasks the contact may hand this sandbox: the role they wear (`actsAs`), whose authority they use (`runAs`), whether each waits for the owner, and how many turns an hour.",
253
+ );
254
+ export type S2sTaskGrant = z.infer<typeof S2sTaskGrantSchema>;
255
+
256
+ export const S2S_GRANT_KINDS = ["message", "report", "task", "records", "operate", "wake"] as const;
257
+ export const S2sGrantKindSchema = z.enum(S2S_GRANT_KINDS);
258
+ export type S2sGrantKind = z.infer<typeof S2sGrantKindSchema>;
259
+
260
+ export const S2sGrantsSchema = z
261
+ .object({
262
+ message: z.boolean().default(true).describe("Send messages here. On by default once accepted."),
263
+ report: z.boolean().default(false).describe("Report problems into this sandbox's issues inbox."),
264
+ task: S2sTaskGrantSchema.optional().describe("Hand this sandbox tasks, under these terms; absent, none."),
265
+ records: z.boolean().default(false).describe("Read this sandbox's records (its conversations, logs and diagnostics) by call, read-only."),
266
+ operate: z
267
+ .boolean()
268
+ .default(false)
269
+ .describe("Run commands and read and write files here as the owner's own terminal does. Sandbox-scoped contacts only."),
270
+ wake: z.boolean().default(false).describe("Hold this sandbox's wake secret, so a message to it starts it when it sleeps."),
271
+ })
272
+ .describe("What this sandbox lets a contact do here.");
273
+ export type S2sGrants = z.infer<typeof S2sGrantsSchema>;
274
+ export type S2sGrantsInput = z.input<typeof S2sGrantsSchema>;
275
+
276
+ // A change to the grants: whole fields replaced, the rest kept; `task: null` takes the task grant away.
277
+ export const S2sGrantsUpdateSchema = z.object({
278
+ message: z.boolean().optional(),
279
+ report: z.boolean().optional(),
280
+ task: S2sTaskGrantSchema.nullable().optional(),
281
+ records: z.boolean().optional(),
282
+ operate: z.boolean().optional(),
283
+ wake: z.boolean().optional(),
284
+ });
285
+ export type S2sGrantsUpdate = z.infer<typeof S2sGrantsUpdateSchema>;
286
+
287
+ /* CONTACTS: another sandbox this one knows, pinned to the key it showed. */
288
+
289
+ export const S2sPersonSchema = z.string().min(3).max(254).describe("A person, by the email this sandbox knows them by.");
290
+
291
+ export const S2sContactScopeSchema = z
292
+ .discriminatedUnion("kind", [
293
+ z.object({ kind: z.literal("sandbox") }),
294
+ z.object({ kind: z.literal("person"), person: S2sPersonSchema }),
295
+ ])
296
+ .describe(
297
+ "Who speaks over the contact: the sandbox as itself (made by its owner or a maintainer), or one person's route between two sandboxes they are in (made by their own press, used only for them, held to their floor on the receiving side).",
298
+ );
299
+ export type S2sContactScope = z.infer<typeof S2sContactScopeSchema>;
300
+
301
+ export const S2sContactStateSchema = z
302
+ .enum(["pending-out", "pending-in", "accepted", "key-changed", "blocked"])
303
+ .describe(
304
+ "pending-out: asked, waiting for their accept. pending-in: they asked, waiting for a person here. accepted: traffic flows. key-changed: their key changed with no proof; held both ways until a person presses Trust new key. blocked: refused, and nothing they send is kept.",
305
+ );
306
+ export type S2sContactState = z.infer<typeof S2sContactStateSchema>;
307
+
308
+ export const S2sContactNoticeKindSchema = z
309
+ .enum(["rotated", "recovered", "rekeyed-by-domain", "moved", "removed"])
310
+ .describe(
311
+ "rotated: a new key signed by the pinned one. recovered: a new key signed by the pinned recovery key. rekeyed-by-domain: a new key with no proof but its domain's TXT record (a warning). moved: a new address proven by the pinned key. removed: they removed this sandbox as a contact.",
312
+ );
313
+ export type S2sContactNoticeKind = z.infer<typeof S2sContactNoticeKindSchema>;
314
+
315
+ export const S2sContactNoticeSchema = z.object({
316
+ kind: S2sContactNoticeKindSchema,
317
+ at: z.number().int().describe("When it was noticed, in epoch milliseconds."),
318
+ from: z.string().max(260).optional().describe("What it was before: the earlier fingerprint, or the earlier address."),
319
+ to: z.string().max(260).optional().describe("What it is now."),
320
+ seen: z.boolean().default(false).describe("A person has read it."),
321
+ });
322
+ export type S2sContactNotice = z.infer<typeof S2sContactNoticeSchema>;
323
+
324
+ export const S2sContactOriginSchema = z
325
+ .enum(["request", "paired", "link-upgrade", "pairing-upgrade", "report-bootstrap"])
326
+ .describe(
327
+ "How it was made: a handshake request (either side), an editor pairing, an upgraded link, an upgraded route pairing, or a first report's introduction.",
328
+ );
329
+ export type S2sContactOrigin = z.infer<typeof S2sContactOriginSchema>;
@@ -0,0 +1,326 @@
1
+ import { z } from "zod";
2
+ import { DropTakeSchema, DropTakenSchema } from "./account-routes.js";
3
+ import {
4
+ S2sAddressSchema,
5
+ S2sAttestationSchema,
6
+ S2sClaimsSchema,
7
+ S2sContactNoticeSchema,
8
+ S2sContactOriginSchema,
9
+ S2sContactScopeSchema,
10
+ S2sContactStateSchema,
11
+ S2sDomainSchema,
12
+ S2sFingerprintSchema,
13
+ S2sGrantsSchema,
14
+ S2sGrantsUpdateSchema,
15
+ S2sPersonSchema,
16
+ S2S_NOTE_MAX,
17
+ S2sRecoveryFingerprintSchema,
18
+ S2sRefSchema,
19
+ SandboxIdentitySchema,
20
+ } from "./s2s-identity.js";
21
+ import { S2S_MESSAGE_FILES_MAX, S2S_MESSAGE_TEXT_MAX, S2S_SUBJECT_MAX, S2sAttachmentSchema, S2sEnvelopeIdSchema, S2sPayloadKindSchema } from "./s2s-wire.js";
22
+
23
+ // SANDBOX-TO-SANDBOX (docs/architecture/messages.md), what the editor and the `messages` command read and send: the
24
+ // Messages tab's contacts, threads, outbox and identity, and the pairing and upgrade exchanges.
25
+ // Re-exported from schemas/s2s.ts.
26
+
27
+ /* CONTACTS, as the Contacts section lists them. */
28
+
29
+ const contactId = S2sRefSchema.describe("The contact's id here.");
30
+
31
+ export const S2sContactSchema = z.object({
32
+ id: contactId,
33
+ address: S2sAddressSchema.describe("What it is addressed by: its host or an own domain."),
34
+ name: z.string().max(120).describe("Its display name, from its document unless a person renamed it here."),
35
+ host: S2sAddressSchema.describe("Its current host, from the last verified document."),
36
+ domains: z.array(S2sDomainSchema).describe("The own domains its last verified document lists."),
37
+ fingerprint: S2sFingerprintSchema.describe("The key pinned for it."),
38
+ recovery: S2sRecoveryFingerprintSchema.optional().describe("Its recovery key's fingerprint, pinned with it."),
39
+ scope: S2sContactScopeSchema,
40
+ state: S2sContactStateSchema,
41
+ grants: S2sGrantsSchema.describe("What this sandbox lets it do here."),
42
+ theirGrants: S2sGrantsSchema.optional().describe("What it said it lets this sandbox do there; informational."),
43
+ holdsWake: z.boolean().describe("This sandbox holds its wake secret, so messages to it wake it."),
44
+ notices: z.array(S2sContactNoticeSchema),
45
+ origin: S2sContactOriginSchema,
46
+ note: z.string().max(S2S_NOTE_MAX).optional().describe("What its request said, for one waiting on a person here."),
47
+ pendingKey: S2sFingerprintSchema.optional().describe("A key-changed contact's new fingerprint, which Trust new key pins."),
48
+ createdAt: z.number().int(),
49
+ updatedAt: z.number().int(),
50
+ lastSeenAt: z.number().int().optional().describe("When something last arrived from it."),
51
+ });
52
+ export type S2sContact = z.infer<typeof S2sContactSchema>;
53
+ export const S2sContactsListSchema = z.object({ contacts: z.array(S2sContactSchema) });
54
+ export type S2sContactsList = z.infer<typeof S2sContactsListSchema>;
55
+
56
+ // POST /s2s/contacts: the handshake, sandbox-scoped. The address is resolved, its key pinned provisionally and a request sent.
57
+ export const S2sContactAddSchema = z.object({
58
+ address: z.string().trim().min(1).max(300).describe("A sandbox host or an own domain, as a person typed it."),
59
+ note: z.string().max(S2S_NOTE_MAX).optional(),
60
+ grants: S2sGrantsSchema.prefault({}).describe("What this sandbox offers it."),
61
+ });
62
+ export type S2sContactAdd = z.infer<typeof S2sContactAddSchema>;
63
+ // POST /s2s/contacts/{id}: a name of this sandbox's own for it, and changed grants (sent on as `contact.update`).
64
+ export const S2sContactChangeSchema = z.object({
65
+ name: z.string().trim().min(1).max(120).optional(),
66
+ grants: S2sGrantsUpdateSchema.optional(),
67
+ });
68
+ export type S2sContactChange = z.infer<typeof S2sContactChangeSchema>;
69
+ // POST /s2s/contacts/{id}/accept: the grants it is accepted with; absent, messages only.
70
+ export const S2sContactAcceptInputSchema = z.object({ grants: S2sGrantsSchema.prefault({}) });
71
+ export type S2sContactAcceptInput = z.infer<typeof S2sContactAcceptInputSchema>;
72
+ // POST /s2s/contacts/{id}/trust-key: the fingerprint the person compared, which must be the one waiting.
73
+ export const S2sTrustKeySchema = z.object({ fingerprint: S2sFingerprintSchema });
74
+ export type S2sTrustKey = z.infer<typeof S2sTrustKeySchema>;
75
+
76
+ /* PAIRING, one person signed in to both sandboxes: the editor carries each side's document to the other. */
77
+
78
+ // POST /s2s/pairings, on the sandbox being paired with, under the person's session there.
79
+ export const S2sPairingRequestSchema = z.object({
80
+ document: SandboxIdentitySchema.describe("The other sandbox's document, as the editor read it there (GET /s2s/me)."),
81
+ scope: z.enum(["sandbox", "person"]).describe("The sandbox as itself (a maintainer's press), or the signed-in person's own route."),
82
+ grants: S2sGrantsSchema.prefault({}).describe("What this sandbox lets the other one do here."),
83
+ theirGrants: S2sGrantsSchema.optional().describe("What the other sandbox will let this one do there; informational."),
84
+ wake: z.string().min(32).max(200).optional().describe("The other sandbox's wake secret, when it grants this one `wake`."),
85
+ });
86
+ export type S2sPairingRequest = z.infer<typeof S2sPairingRequestSchema>;
87
+ export const S2sPairingAnswerSchema = z.object({
88
+ contact: contactId.describe("The contact it filed, there."),
89
+ document: SandboxIdentitySchema.describe("Its own document."),
90
+ grants: S2sGrantsSchema.describe("What it lets the other sandbox do there."),
91
+ wake: z.string().min(32).max(200).optional().describe("Its wake secret, when it granted `wake`."),
92
+ });
93
+ export type S2sPairingAnswer = z.infer<typeof S2sPairingAnswerSchema>;
94
+ // POST /s2s/contacts/paired, back on the first sandbox: the answer, and what this one grants the other.
95
+ export const S2sPairedSchema = z.object({
96
+ answer: S2sPairingAnswerSchema,
97
+ scope: z.enum(["sandbox", "person"]),
98
+ grants: S2sGrantsSchema.prefault({}).describe("What this sandbox lets the other one do here."),
99
+ });
100
+ export type S2sPaired = z.infer<typeof S2sPairedSchema>;
101
+
102
+ /* UPGRADES: an existing link or route pairing vouching, once, for the asking side's key. */
103
+
104
+ // POST /link/upgrade (under the link's own token) and POST /account/pairings/upgrade (under the pass and the pairing).
105
+ export const S2sUpgradeRequestSchema = z.object({
106
+ document: SandboxIdentitySchema.describe("The asking sandbox's document."),
107
+ grants: S2sGrantsSchema.prefault({}).describe("What the asking sandbox lets the taking one do there; a link gave nothing back, so messages."),
108
+ wake: z.string().min(32).max(200).optional().describe("The asking sandbox's wake secret, when it grants `wake`."),
109
+ });
110
+ export type S2sUpgradeRequest = z.infer<typeof S2sUpgradeRequestSchema>;
111
+ export const S2sUpgradeAnswerSchema = z.object({
112
+ contact: contactId.describe("The contact the taking side filed from the link's terms or the pairing."),
113
+ document: SandboxIdentitySchema.describe("The taking sandbox's document."),
114
+ grants: S2sGrantsSchema.describe("What it lets the asking sandbox do: the link's terms, or a person's route."),
115
+ wake: z.string().min(32).max(200).optional(),
116
+ });
117
+ export type S2sUpgradeAnswer = z.infer<typeof S2sUpgradeAnswerSchema>;
118
+ export const S2sLinkUpgradeRequestSchema = S2sUpgradeRequestSchema;
119
+ export const S2sLinkUpgradeAnswerSchema = S2sUpgradeAnswerSchema;
120
+ export const S2sPairingUpgradeRequestSchema = S2sUpgradeRequestSchema;
121
+ export const S2sPairingUpgradeAnswerSchema = S2sUpgradeAnswerSchema;
122
+
123
+ // POST /account/contact-bootstrap, under a report pass: the first report to a sandbox with no contact files one holding
124
+ // only the `report` grant, and every later report is a message.
125
+ export const S2sContactBootstrapRequestSchema = z.object({ document: SandboxIdentitySchema });
126
+ export type S2sContactBootstrapRequest = z.infer<typeof S2sContactBootstrapRequestSchema>;
127
+ export const S2sContactBootstrapAnswerSchema = z.object({
128
+ contact: contactId,
129
+ document: SandboxIdentitySchema,
130
+ grants: S2sGrantsSchema,
131
+ });
132
+ export type S2sContactBootstrapAnswer = z.infer<typeof S2sContactBootstrapAnswerSchema>;
133
+
134
+ /* IDENTITY, as the Messages tab's Identity section shows it. */
135
+
136
+ export const S2sDomainClaimSchema = z.object({
137
+ domain: S2sDomainSchema,
138
+ verified: z.boolean().describe("Its TXT record matched at the last check, so the document lists it."),
139
+ record: z.object({
140
+ name: z.string().describe("Where to publish it: `_intentic.<domain>`."),
141
+ value: z.string().describe("What to publish: `v=isk1 fp=<fingerprint> host=<host>`."),
142
+ }),
143
+ claimedAt: z.number().int(),
144
+ checkedAt: z.number().int().optional(),
145
+ problem: z.string().max(500).optional().describe("What the last check found instead, in words."),
146
+ });
147
+ export type S2sDomainClaim = z.infer<typeof S2sDomainClaimSchema>;
148
+
149
+ export const S2sMeSchema = z.object({
150
+ document: SandboxIdentitySchema.describe("This sandbox's own document, as GET /s2s/identity serves it."),
151
+ domains: z.array(S2sDomainClaimSchema).describe("Every own domain claimed here, verified or waiting for its record."),
152
+ recovery: z.boolean().describe("A recovery key is set."),
153
+ previousKeys: z.number().int().nonnegative().describe("How many earlier key sets are kept to open late arrivals."),
154
+ });
155
+ export type S2sMe = z.infer<typeof S2sMeSchema>;
156
+ export const S2sDomainInputSchema = z.object({ domain: z.string().trim().min(1).max(253) });
157
+ export type S2sDomainInput = z.infer<typeof S2sDomainInputSchema>;
158
+ // POST /s2s/me/recovery: the recovery secret, shown this once.
159
+ export const S2sRecoveryMintedSchema = z.object({
160
+ secret: z.string().describe("Keep it off this sandbox: it is what recovers this identity if the sandbox's volume is lost."),
161
+ fingerprint: S2sRecoveryFingerprintSchema,
162
+ });
163
+ export type S2sRecoveryMinted = z.infer<typeof S2sRecoveryMintedSchema>;
164
+ export const S2sRecoverInputSchema = z.object({ secret: z.string().trim().min(1).max(400) });
165
+ export type S2sRecoverInput = z.infer<typeof S2sRecoverInputSchema>;
166
+
167
+ /* ATTESTATIONS, as the Identity section lists them (messages.md, "Attestations"): what other sandboxes signed about this
168
+ one and a person here added to its identity, and the two sends that ask for one and issue one. */
169
+
170
+ export const S2sAttestationRowSchema = z.object({
171
+ id: z.string().min(1).max(64).describe("The attestation's id (attestationIdOf): what removing it names."),
172
+ attestation: S2sAttestationSchema,
173
+ contact: S2sRefSchema.optional().describe("The contact it came from."),
174
+ message: S2sEnvelopeIdSchema.optional().describe("The message that carried it, in that contact's thread."),
175
+ acceptedAt: z.number().int().describe("When a person here added it."),
176
+ verified: z.boolean().describe("It verifies against this sandbox's document now: its issuer's keys and signature, its dates and its subject."),
177
+ problem: z.string().max(500).optional().describe("Why it does not, in words: lapsed, or about a key two rotations back."),
178
+ published: z.boolean().describe("This sandbox's document carries it now, which it does exactly while it verifies."),
179
+ });
180
+ export type S2sAttestationRow = z.infer<typeof S2sAttestationRowSchema>;
181
+ export const S2sAttestationsSchema = z.object({ attestations: z.array(S2sAttestationRowSchema).describe("Newest first.") });
182
+ export type S2sAttestations = z.infer<typeof S2sAttestationsSchema>;
183
+ // POST /s2s/me/attestations: add the attestation a contact sent (its `attestation.issued` message) to this identity, once
184
+ // it verifies against this sandbox's own document. One per issuer: a newer one from the same issuer replaces the older.
185
+ export const S2sAttestationAcceptSchema = z.object({ message: S2sEnvelopeIdSchema.describe("The attestation.issued message, by its id.") });
186
+ export type S2sAttestationAccept = z.infer<typeof S2sAttestationAcceptSchema>;
187
+ // POST /s2s/me/attestations/ask: ask a sandbox-scoped contact to vouch for this sandbox (`attestation.request`).
188
+ export const S2sAttestationAskSchema = z.object({
189
+ contact: S2sRefSchema.describe("The contact asked, by its id here."),
190
+ claims: S2sClaimsSchema.prefault({}).describe("What it is asked to vouch for; the contact decides what it signs."),
191
+ note: z.string().max(S2S_NOTE_MAX).optional(),
192
+ });
193
+ export type S2sAttestationAsk = z.infer<typeof S2sAttestationAskSchema>;
194
+ // POST /s2s/attestations/issue: vouch for a sandbox-scoped contact, signed now by this sandbox's key for the contact's
195
+ // pinned key, and sent to it (`attestation.issued`).
196
+ export const S2sAttestationIssueSchema = z.object({
197
+ contact: S2sRefSchema.describe("The contact vouched for, by its id here."),
198
+ claims: S2sClaimsSchema,
199
+ validUntil: z.number().int().describe("When it lapses, in epoch milliseconds: later than now, at most a year from now."),
200
+ note: z.string().max(S2S_NOTE_MAX).optional(),
201
+ replyTo: S2sEnvelopeIdSchema.optional().describe("The contact's attestation.request it answers; the vouch goes into that thread."),
202
+ });
203
+ export type S2sAttestationIssue = z.infer<typeof S2sAttestationIssueSchema>;
204
+
205
+ /* THREADS AND MESSAGES, as the Inbox shows them. */
206
+
207
+ export const S2sDirectionSchema = z.enum(["in", "out"]);
208
+ export const S2sDeliveryStateSchema = z
209
+ .enum(["queued", "sent", "undelivered"])
210
+ .describe("queued: in the outbox. sent: the far side took it. undelivered: refused, or not taken within a week.");
211
+ export type S2sDeliveryState = z.infer<typeof S2sDeliveryStateSchema>;
212
+
213
+ export const S2sAttachmentMetaSchema = z.object({
214
+ name: z.string().min(1).max(200),
215
+ size: z.number().int().nonnegative(),
216
+ });
217
+ export type S2sAttachmentMeta = z.infer<typeof S2sAttachmentMetaSchema>;
218
+
219
+ export const S2sThreadMessageSchema = z.object({
220
+ id: S2sEnvelopeIdSchema,
221
+ thread: S2sRefSchema,
222
+ contact: contactId,
223
+ direction: S2sDirectionSchema,
224
+ kind: S2sPayloadKindSchema.describe("message, report, contact.request, a task's kind or an attestation's: what the thread shows."),
225
+ subject: z.string().max(S2S_SUBJECT_MAX).optional(),
226
+ text: z.string().describe("The words, or for a report or a task the line it is shown by."),
227
+ attachments: z.array(S2sAttachmentMetaSchema),
228
+ as: S2sPersonSchema.optional().describe("The person a person-scoped contact carried it for."),
229
+ replyTo: S2sEnvelopeIdSchema.optional(),
230
+ claims: S2sClaimsSchema.optional().describe("An attestation.request's claims: what it asks to be vouched for."),
231
+ attestation: S2sAttestationSchema.optional().describe("An attestation.issued's attestation, whole."),
232
+ sentAt: z.number().int(),
233
+ receivedAt: z.number().int().optional(),
234
+ read: z.boolean(),
235
+ delivery: z
236
+ .object({
237
+ state: S2sDeliveryStateSchema,
238
+ attempts: z.number().int().nonnegative(),
239
+ reason: z.string().max(500).optional().describe("Why it waits, or why it was not delivered."),
240
+ })
241
+ .optional()
242
+ .describe("A sent message's delivery; absent on one received."),
243
+ });
244
+ export type S2sThreadMessage = z.infer<typeof S2sThreadMessageSchema>;
245
+
246
+ export const S2sThreadSummarySchema = z.object({
247
+ id: S2sRefSchema,
248
+ contact: contactId,
249
+ contactName: z.string(),
250
+ scope: S2sContactScopeSchema,
251
+ subject: z.string().max(S2S_SUBJECT_MAX).optional(),
252
+ last: z.object({ at: z.number().int(), direction: S2sDirectionSchema, preview: z.string().max(200) }),
253
+ unread: z.number().int().nonnegative(),
254
+ messages: z.number().int().nonnegative(),
255
+ undelivered: z.number().int().nonnegative().describe("Sent messages in it that were not delivered."),
256
+ });
257
+ export type S2sThreadSummary = z.infer<typeof S2sThreadSummarySchema>;
258
+ export const S2sThreadsListSchema = z.object({
259
+ threads: z.array(S2sThreadSummarySchema),
260
+ requests: z.number().int().nonnegative().describe("Contact requests waiting on a person, for the tab's badge."),
261
+ });
262
+ export type S2sThreadsList = z.infer<typeof S2sThreadsListSchema>;
263
+ export const S2sThreadSchema = z.object({ thread: S2sThreadSummarySchema, messages: z.array(S2sThreadMessageSchema) });
264
+ export type S2sThread = z.infer<typeof S2sThreadSchema>;
265
+
266
+ /* SENDING, and the outbox. */
267
+
268
+ // POST /s2s/messages: a person in the editor, or an agent through `messages send` with its turn proof.
269
+ export const S2sMessageSendSchema = z.object({
270
+ to: contactId.describe("The contact, by its id here."),
271
+ text: z.string().max(S2S_MESSAGE_TEXT_MAX),
272
+ subject: z.string().max(S2S_SUBJECT_MAX).optional(),
273
+ thread: S2sRefSchema.optional().describe("The thread it continues; absent opens a new one."),
274
+ replyTo: S2sEnvelopeIdSchema.optional(),
275
+ files: z.array(S2sAttachmentSchema).max(S2S_MESSAGE_FILES_MAX).default([]).describe("Each file's own name and its bytes in base64."),
276
+ });
277
+ export type S2sMessageSend = z.infer<typeof S2sMessageSendSchema>;
278
+ export const S2sMessageSendOutcomeSchema = z.discriminatedUnion("state", [
279
+ z.object({ state: z.literal("queued"), id: S2sEnvelopeIdSchema, thread: S2sRefSchema }),
280
+ z.object({ state: z.literal("refused"), reason: z.string() }),
281
+ z.object({ state: z.literal("asked"), reason: z.string(), needId: z.string().describe("The card a person answers before it goes.") }),
282
+ ]);
283
+ export type S2sMessageSendOutcome = z.infer<typeof S2sMessageSendOutcomeSchema>;
284
+
285
+ // POST /s2s/messages/{id}/take: copy a received message's files into a folder of the workspace.
286
+ export const S2sTakeSchema = DropTakeSchema.extend({
287
+ files: z.array(z.string().min(1).max(200)).max(S2S_MESSAGE_FILES_MAX).optional().describe("Only these, by name; absent, all."),
288
+ });
289
+ export type S2sTake = z.infer<typeof S2sTakeSchema>;
290
+ export const S2sTakenSchema = DropTakenSchema;
291
+
292
+ export const S2sOutboxEntrySchema = z.object({
293
+ id: S2sEnvelopeIdSchema,
294
+ contact: contactId,
295
+ contactName: z.string(),
296
+ kind: S2sPayloadKindSchema,
297
+ thread: S2sRefSchema.optional(),
298
+ state: z.enum(["waiting", "undelivered"]),
299
+ attempts: z.number().int().nonnegative(),
300
+ firstAt: z.number().int(),
301
+ nextAt: z.number().int().optional().describe("When it is tried next, while waiting."),
302
+ lastError: z.string().max(500).optional(),
303
+ });
304
+ export type S2sOutboxEntry = z.infer<typeof S2sOutboxEntrySchema>;
305
+ export const S2sOutboxSchema = z.object({ entries: z.array(S2sOutboxEntrySchema) });
306
+ export type S2sOutbox = z.infer<typeof S2sOutboxSchema>;
307
+
308
+ // TASKS CONTACTS HANDED THIS SANDBOX (messages.md, "Tasks"): what `GET /s2s/tasks` lists for the Messages tab, each shown
309
+ // to whoever may use its contact, and what `POST /s2s/contacts/{id}/tasks/{request}` answers with. A sandbox-scoped
310
+ // contact's task may wait for the owner (its `task` grant's `approve`); a person-scoped one never does.
311
+ export const S2sContactTaskSchema = z.object({
312
+ contact: contactId,
313
+ contactName: z.string(),
314
+ requestId: z.string().describe("The asking side's own id for the task; the decision names it."),
315
+ conversationId: z.string().describe("The conversation it opened here."),
316
+ title: z.string().optional(),
317
+ task: z.string().max(2_000).optional().describe("Its words while its first turn has not been said (cut), the Allow's to read."),
318
+ state: z.enum(["held", "running", "finished", "failed", "declined", "stopped"]).describe("held waits for the owner's Allow."),
319
+ askedAt: z.number().int(),
320
+ updatedAt: z.number().int(),
321
+ });
322
+ export type S2sContactTask = z.infer<typeof S2sContactTaskSchema>;
323
+ export const S2sContactTasksSchema = z.object({ tasks: z.array(S2sContactTaskSchema).describe("Waiting ones first, then the newest.") });
324
+ export type S2sContactTasks = z.infer<typeof S2sContactTasksSchema>;
325
+ export const S2sContactTaskDecisionSchema = z.object({ approve: z.boolean().describe("Start it now, or decline it: nothing of it runs.") });
326
+ export type S2sContactTaskDecision = z.infer<typeof S2sContactTaskDecisionSchema>;