@7h3/protocol 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (294) hide show
  1. package/README.md +6 -1317
  2. package/agentAdapter.d.ts +26 -0
  3. package/auditLog.d.ts +54 -0
  4. package/capability.d.ts +67 -0
  5. package/cborCodec.d.ts +34 -0
  6. package/conformanceVectors.d.ts +20 -0
  7. package/encryption.d.ts +85 -0
  8. package/envelopeCbor.d.ts +33 -0
  9. package/frameworkAdapters.d.ts +72 -0
  10. package/gateway.d.ts +53 -0
  11. package/grpcBinding.d.ts +29 -0
  12. package/httpBinding.d.ts +50 -0
  13. package/index.d.ts +39 -0
  14. package/index.js +3662 -0
  15. package/keyInfra.d.ts +64 -0
  16. package/keyRegistry.d.ts +9 -0
  17. package/keyRotation.d.ts +20 -0
  18. package/mcpGateway.d.ts +37 -0
  19. package/mcpTransports.d.ts +62 -0
  20. package/mcpWrapper.d.ts +83 -0
  21. package/otel.d.ts +65 -0
  22. package/package.json +13 -121
  23. package/policyEnforcer.d.ts +50 -0
  24. package/policyTelemetryFeedback.d.ts +11 -0
  25. package/{src/protocol.d.ts → protocol.d.ts} +1 -2
  26. package/protocolAgent.d.ts +58 -0
  27. package/protocolBinary.d.ts +8 -0
  28. package/protocolCapabilities.d.ts +24 -0
  29. package/protocolReplay.d.ts +35 -0
  30. package/protocolTransport.d.ts +73 -0
  31. package/queueBinding.d.ts +43 -0
  32. package/rateLimiter.d.ts +18 -0
  33. package/{src/redisClient.ts → redisClient.d.ts} +25 -75
  34. package/replayStores.d.ts +98 -0
  35. package/revocation.d.ts +71 -0
  36. package/routePolicy.d.ts +30 -0
  37. package/runtimePolicy.d.ts +24 -0
  38. package/runtimePolicyManager.d.ts +15 -0
  39. package/runtimePolicyPresets.d.ts +11 -0
  40. package/signedResponse.d.ts +21 -0
  41. package/stream.d.ts +59 -0
  42. package/telemetry.d.ts +79 -0
  43. package/webhookBinding.d.ts +30 -0
  44. package/wsBinding.d.ts +52 -0
  45. package/.dockerignore +0 -19
  46. package/.github/dependabot.yml +0 -32
  47. package/.github/workflows/ci.yml +0 -31
  48. package/.github/workflows/publish.yml +0 -59
  49. package/.github/workflows/scorecard.yml +0 -37
  50. package/7h3.example.yaml +0 -125
  51. package/CHANGELOG.md +0 -152
  52. package/CONTRIBUTING.md +0 -82
  53. package/Dockerfile +0 -73
  54. package/GOVERNANCE.md +0 -62
  55. package/SECURITY.md +0 -70
  56. package/bench-results/replay-cache-full-1777891033256.json +0 -10
  57. package/bench-results/replay-cache-full-1777896317488.json +0 -10
  58. package/bench-results/replay-cache-full-1777900993184.json +0 -10
  59. package/bench-results/replay-cache-full-1777901019285.json +0 -10
  60. package/bench-results/replay-cache-quick-1777870170126.json +0 -10
  61. package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
  62. package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
  63. package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
  64. package/bench-results/wire-codecs-full-1777891019803.json +0 -93
  65. package/bench-results/wire-codecs-full-1777896260964.json +0 -93
  66. package/bench-results/wire-codecs-full-1777901004247.json +0 -93
  67. package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
  68. package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
  69. package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
  70. package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
  71. package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
  72. package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
  73. package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
  74. package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
  75. package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
  76. package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
  77. package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
  78. package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
  79. package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
  80. package/bin/7h3.ts +0 -406
  81. package/conformance/7h3_v0_1.json +0 -77
  82. package/conformance/7h3_v0_1_binary.json +0 -20
  83. package/conformance/aip_v0_1_binary.json +0 -20
  84. package/docker-compose.yaml +0 -77
  85. package/docs/ADOPTION_PLAN.md +0 -120
  86. package/docs/AGENTS.md +0 -77
  87. package/docs/AIP_RFC_v0.1.md +0 -97
  88. package/docs/AI_DECISION_CARD.md +0 -122
  89. package/docs/AI_RUNTIME_POLICY.json +0 -126
  90. package/docs/AI_RUNTIME_POLICY.yaml +0 -110
  91. package/docs/BACKPRESSURE_TUNING.md +0 -65
  92. package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
  93. package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
  94. package/docs/BINARY_CODEC_BENCH.md +0 -23
  95. package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
  96. package/docs/CLOCK_SKEW_POLICY.md +0 -30
  97. package/docs/DISTRIBUTED_REPLAY.md +0 -142
  98. package/docs/FUZZ_CAMPAIGN.md +0 -121
  99. package/docs/GATEWAY.md +0 -195
  100. package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
  101. package/docs/KEY_REVOCATION.md +0 -69
  102. package/docs/MCP_WRAPPER.md +0 -159
  103. package/docs/MIGRATION_GUIDE.md +0 -40
  104. package/docs/OPERATORS.md +0 -184
  105. package/docs/PERF_REGRESSION_POLICY.md +0 -34
  106. package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
  107. package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
  108. package/docs/RELEASE_GATE.md +0 -25
  109. package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
  110. package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
  111. package/docs/TELEMETRY.md +0 -41
  112. package/docs/THREAT_MODEL.md +0 -89
  113. package/docs/VERSIONING_POLICY.md +0 -30
  114. package/docs/assets/banner-github.png +0 -0
  115. package/docs/assets/banner.png +0 -0
  116. package/docs/assets/banner.svg +0 -123
  117. package/eslint.config.js +0 -15
  118. package/fuzz/ts/harness-decode.ts +0 -136
  119. package/fuzz/ts/harness-verify.ts +0 -121
  120. package/fuzz/ts/run.ts +0 -35
  121. package/mcp-server/README.md +0 -38
  122. package/mcp-server/package-lock.json +0 -1187
  123. package/mcp-server/package.json +0 -35
  124. package/mcp-server/src/index.ts +0 -236
  125. package/mcp-server/tsconfig.json +0 -14
  126. package/scripts/aip-framework-quickstart.ts +0 -110
  127. package/scripts/aip-mcp-gateway.ts +0 -38
  128. package/scripts/aip-mcp-wrap-demo.ts +0 -72
  129. package/scripts/aip-quickstart.ts +0 -60
  130. package/scripts/bench-diff.ts +0 -118
  131. package/scripts/bench-protocol-e2e.ts +0 -937
  132. package/scripts/bench-protocol-openloop.ts +0 -1397
  133. package/scripts/bench-replay-cache.ts +0 -76
  134. package/scripts/bench-signature-profiles.ts +0 -180
  135. package/scripts/bench-wire-codecs.ts +0 -161
  136. package/scripts/build-binary-conformance.ts +0 -36
  137. package/scripts/build-release-dashboard.ts +0 -175
  138. package/scripts/canary-rollout.ts +0 -38
  139. package/scripts/mcpGatewayCli.test.ts +0 -116
  140. package/scripts/prepare-aip-package.ts +0 -88
  141. package/scripts/regen-conformance-sigs.ts +0 -18
  142. package/scripts/release-gate.ts +0 -19
  143. package/scripts/validate-runtime-policy.ts +0 -18
  144. package/sdk/browser/index.test.ts +0 -162
  145. package/sdk/browser/index.ts +0 -257
  146. package/sdk/browser/package.json +0 -13
  147. package/sdk/go/cbor.go +0 -551
  148. package/sdk/go/cbor_test.go +0 -232
  149. package/sdk/go/encryption.go +0 -280
  150. package/sdk/go/encryption_test.go +0 -318
  151. package/sdk/go/go.mod +0 -7
  152. package/sdk/go/go.sum +0 -4
  153. package/sdk/go/http.go +0 -135
  154. package/sdk/go/protocol.go +0 -324
  155. package/sdk/go/protocol_test.go +0 -334
  156. package/sdk/go/replay.go +0 -121
  157. package/sdk/go/replay_test.go +0 -149
  158. package/sdk/go/webhook.go +0 -136
  159. package/sdk/pq/package-lock.json +0 -1358
  160. package/sdk/pq/package.json +0 -42
  161. package/sdk/pq/src/index.test.ts +0 -143
  162. package/sdk/pq/src/index.ts +0 -166
  163. package/sdk/pq/tsconfig.json +0 -14
  164. package/sdk/pq/vitest.config.ts +0 -7
  165. package/sdk/python/README.md +0 -18
  166. package/sdk/python/protocol_7h3/__init__.py +0 -46
  167. package/sdk/python/protocol_7h3/encryption.py +0 -252
  168. package/sdk/python/protocol_7h3/http.py +0 -212
  169. package/sdk/python/protocol_7h3/keys.py +0 -149
  170. package/sdk/python/protocol_7h3/pq.py +0 -244
  171. package/sdk/python/protocol_7h3/protocol.py +0 -525
  172. package/sdk/python/protocol_7h3/queue.py +0 -118
  173. package/sdk/python/protocol_7h3/replay.py +0 -98
  174. package/sdk/python/protocol_7h3/webhook.py +0 -116
  175. package/sdk/python/pyproject.toml +0 -40
  176. package/sdk/python/tests/test_conformance.py +0 -110
  177. package/sdk/python/tests/test_encryption.py +0 -206
  178. package/sdk/python/tests/test_http.py +0 -305
  179. package/sdk/python/tests/test_keys.py +0 -417
  180. package/sdk/python/tests/test_queue.py +0 -120
  181. package/sdk/python/tests/test_webhook.py +0 -345
  182. package/sdk/rust/Cargo.lock +0 -371
  183. package/sdk/rust/Cargo.toml +0 -25
  184. package/sdk/rust/README.md +0 -31
  185. package/sdk/rust/fuzz/Cargo.toml +0 -29
  186. package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
  187. package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
  188. package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
  189. package/sdk/rust/src/http.rs +0 -145
  190. package/sdk/rust/src/keys.rs +0 -161
  191. package/sdk/rust/src/lib.rs +0 -688
  192. package/sdk/rust/src/queue.rs +0 -79
  193. package/sdk/rust/src/webhook.rs +0 -86
  194. package/sdk/rust/tests/conformance.rs +0 -148
  195. package/sdk/rust/tests/gateway.rs +0 -130
  196. package/sdk/rust/tests/http_webhook_queue.rs +0 -201
  197. package/sdk/rust/tests/keys.rs +0 -189
  198. package/sdk/threshold/index.d.ts +0 -68
  199. package/sdk/threshold/index.d.ts.map +0 -1
  200. package/sdk/threshold/index.js +0 -254
  201. package/sdk/threshold/package-lock.json +0 -1361
  202. package/sdk/threshold/package.json +0 -39
  203. package/sdk/threshold/src/index.d.ts +0 -68
  204. package/sdk/threshold/src/index.d.ts.map +0 -1
  205. package/sdk/threshold/src/index.js +0 -254
  206. package/sdk/threshold/src/index.test.ts +0 -238
  207. package/sdk/threshold/src/index.ts +0 -355
  208. package/sdk/threshold/tsconfig.json +0 -19
  209. package/sdk/threshold/vitest.config.ts +0 -12
  210. package/src/agentAdapter.test.ts +0 -48
  211. package/src/agentAdapter.ts +0 -56
  212. package/src/auditLog.test.ts +0 -145
  213. package/src/auditLog.ts +0 -147
  214. package/src/capability.test.ts +0 -504
  215. package/src/capability.ts +0 -380
  216. package/src/cborCodec.test.ts +0 -263
  217. package/src/cborCodec.ts +0 -339
  218. package/src/conformance.test.ts +0 -136
  219. package/src/conformanceVectors.ts +0 -99
  220. package/src/encryption.test.ts +0 -206
  221. package/src/encryption.ts +0 -245
  222. package/src/envelopeCbor.ts +0 -140
  223. package/src/frameworkAdapters.test.ts +0 -290
  224. package/src/frameworkAdapters.ts +0 -261
  225. package/src/gateway.test.ts +0 -343
  226. package/src/gateway.ts +0 -246
  227. package/src/grpcBinding.test.ts +0 -211
  228. package/src/grpcBinding.ts +0 -103
  229. package/src/httpBinding.test.ts +0 -376
  230. package/src/httpBinding.ts +0 -189
  231. package/src/index.ts +0 -39
  232. package/src/keyInfra.test.ts +0 -278
  233. package/src/keyInfra.ts +0 -228
  234. package/src/keyRegistry.ts +0 -59
  235. package/src/keyRotation.test.ts +0 -78
  236. package/src/keyRotation.ts +0 -72
  237. package/src/mcpGateway.test.ts +0 -129
  238. package/src/mcpGateway.ts +0 -250
  239. package/src/mcpTransports.test.ts +0 -92
  240. package/src/mcpTransports.ts +0 -169
  241. package/src/mcpWrapper.test.ts +0 -179
  242. package/src/mcpWrapper.ts +0 -206
  243. package/src/otel.ts +0 -136
  244. package/src/policyEnforcer.test.ts +0 -99
  245. package/src/policyEnforcer.ts +0 -169
  246. package/src/policyTelemetryFeedback.test.ts +0 -25
  247. package/src/policyTelemetryFeedback.ts +0 -38
  248. package/src/protocol.bench.ts +0 -37
  249. package/src/protocol.d.ts.map +0 -1
  250. package/src/protocol.js +0 -294
  251. package/src/protocol.test.ts +0 -155
  252. package/src/protocol.ts +0 -414
  253. package/src/protocolAgent.test.ts +0 -105
  254. package/src/protocolAgent.ts +0 -169
  255. package/src/protocolBinary.test.ts +0 -165
  256. package/src/protocolBinary.ts +0 -312
  257. package/src/protocolCapabilities.ts +0 -70
  258. package/src/protocolFuzz.advanced.test.ts +0 -235
  259. package/src/protocolFuzz.test.ts +0 -111
  260. package/src/protocolNegative.test.ts +0 -97
  261. package/src/protocolReplay.test.ts +0 -71
  262. package/src/protocolReplay.ts +0 -194
  263. package/src/protocolTransport.test.ts +0 -556
  264. package/src/protocolTransport.ts +0 -483
  265. package/src/queueBinding.test.ts +0 -130
  266. package/src/queueBinding.ts +0 -102
  267. package/src/rateLimiter.test.ts +0 -96
  268. package/src/rateLimiter.ts +0 -46
  269. package/src/redisIntegration.test.ts +0 -134
  270. package/src/replayStores.test.ts +0 -273
  271. package/src/replayStores.ts +0 -215
  272. package/src/revocation.test.ts +0 -98
  273. package/src/revocation.ts +0 -0
  274. package/src/routePolicy.test.ts +0 -87
  275. package/src/routePolicy.ts +0 -72
  276. package/src/runtimePolicy.test.ts +0 -49
  277. package/src/runtimePolicy.ts +0 -81
  278. package/src/runtimePolicyManager.test.ts +0 -29
  279. package/src/runtimePolicyManager.ts +0 -50
  280. package/src/runtimePolicyPresets.ts +0 -43
  281. package/src/signedResponse.test.ts +0 -111
  282. package/src/signedResponse.ts +0 -83
  283. package/src/stream.test.ts +0 -254
  284. package/src/stream.ts +0 -417
  285. package/src/telemetry.test.ts +0 -251
  286. package/src/telemetry.ts +0 -299
  287. package/src/webhookBinding.test.ts +0 -144
  288. package/src/webhookBinding.ts +0 -115
  289. package/src/wsBinding.test.ts +0 -221
  290. package/src/wsBinding.ts +0 -200
  291. package/tsconfig.json +0 -15
  292. package/tsconfig.lib.json +0 -23
  293. package/vite.lib.config.ts +0 -16
  294. package/vitest.config.ts +0 -11
@@ -1,165 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { createEnvelope, signEnvelopeEd25519, signEnvelopeHmac } from './protocol'
3
- import { DEFAULT_MAX_BINARY_ENVELOPE_BYTES, decodeEnvelopeBinary, encodeEnvelopeBinary } from './protocolBinary'
4
-
5
- function stringFieldOffset(raw: Uint8Array, fieldIndex: number): { lengthOffset: number; valueOffset: number; length: number } {
6
- let offset = 4 + 1 + 1 + 8 + 4
7
- const view = new DataView(raw.buffer, raw.byteOffset, raw.byteLength)
8
- for (let index = 0; index <= fieldIndex; index += 1) {
9
- const lengthOffset = offset
10
- const length = view.getUint32(lengthOffset, false)
11
- const valueOffset = lengthOffset + 4
12
- if (index === fieldIndex) return { lengthOffset, valueOffset, length }
13
- offset = valueOffset + length
14
- }
15
- throw new Error(`missing string field ${fieldIndex}`)
16
- }
17
-
18
- function replaceAscii(raw: Uint8Array, fieldIndex: number, value: string): Uint8Array {
19
- const copy = raw.slice()
20
- const field = stringFieldOffset(copy, fieldIndex)
21
- if (value.length !== field.length) throw new Error('replacement must preserve encoded length')
22
- for (let index = 0; index < value.length; index += 1) {
23
- copy[field.valueOffset + index] = value.charCodeAt(index)
24
- }
25
- return copy
26
- }
27
-
28
- describe('AIP binary wire codec', () => {
29
- it('roundtrips HS256 signed envelope', async () => {
30
- const signed = await signEnvelopeHmac(
31
- createEnvelope({
32
- sender: 'agent.alpha',
33
- recipient: 'agent.beta',
34
- intent: 'TASK',
35
- content: 'route:binary',
36
- capability: 'task.plan',
37
- correlationId: 'corr-b1',
38
- messageId: 'bin-1',
39
- nonce: 'nb1',
40
- nowMs: 1000,
41
- ttlMs: 60_000,
42
- }),
43
- 'shared-secret',
44
- 'k1',
45
- )
46
-
47
- const raw = encodeEnvelopeBinary(signed)
48
- const decoded = decodeEnvelopeBinary(raw)
49
- expect(decoded.ok).toBe(true)
50
- expect(decoded.envelope).toEqual(signed)
51
- })
52
-
53
- it('roundtrips ED25519 signed envelope', async () => {
54
- const privateKey = 'MC4CAQAwBQYDK2VwBCIEICheZbQGuDVb6hezIlcs0QnCHGxz6IhiLkC9M0qr8OOZ'
55
- const signed = await signEnvelopeEd25519(
56
- createEnvelope({
57
- sender: 'agent.ed',
58
- recipient: 'agent.verify',
59
- intent: 'TASK',
60
- content: 'route:ed25519-binary',
61
- capability: 'task.sign',
62
- correlationId: 'corr-ed-b1',
63
- messageId: 'bin-ed-1',
64
- nonce: 'n-ed-b1',
65
- nowMs: 1000,
66
- ttlMs: 45_000,
67
- }),
68
- privateKey,
69
- 'ed-k1',
70
- )
71
-
72
- const raw = encodeEnvelopeBinary(signed)
73
- const decoded = decodeEnvelopeBinary(raw)
74
- expect(decoded.ok).toBe(true)
75
- expect(decoded.envelope).toEqual(signed)
76
- })
77
-
78
- it('rejects bad magic and trailing bytes', async () => {
79
- const signed = await signEnvelopeHmac(
80
- createEnvelope({
81
- sender: 'agent.alpha',
82
- intent: 'PING',
83
- content: 'x',
84
- messageId: 'bin-2',
85
- nonce: 'nb2',
86
- nowMs: 1000,
87
- ttlMs: 60_000,
88
- }),
89
- 'shared-secret',
90
- 'k1',
91
- )
92
-
93
- const raw = encodeEnvelopeBinary(signed)
94
- const badMagic = raw.slice()
95
- badMagic[0] = 0
96
- const decodedMagic = decodeEnvelopeBinary(badMagic)
97
- expect(decodedMagic.ok).toBe(false)
98
- expect(decodedMagic.diagnostics.some((d) => d.message.includes('magic'))).toBe(true)
99
-
100
- const trailing = new Uint8Array(raw.byteLength + 1)
101
- trailing.set(raw)
102
- trailing[raw.byteLength] = 1
103
- const decodedTrailing = decodeEnvelopeBinary(trailing)
104
- expect(decodedTrailing.ok).toBe(false)
105
- expect(decodedTrailing.diagnostics.some((d) => d.message.includes('Trailing bytes'))).toBe(true)
106
- })
107
-
108
- it('rejects oversized binary frames before parsing', () => {
109
- const oversized = new Uint8Array(DEFAULT_MAX_BINARY_ENVELOPE_BYTES + 1)
110
- const decoded = decodeEnvelopeBinary(oversized)
111
-
112
- expect(decoded.ok).toBe(false)
113
- expect(decoded.diagnostics.some((d) => d.message.includes('maximum frame size'))).toBe(true)
114
- })
115
-
116
- it('rejects malformed string lengths', async () => {
117
- const signed = await signEnvelopeHmac(
118
- createEnvelope({
119
- sender: 'agent.alpha',
120
- intent: 'PING',
121
- content: 'x',
122
- messageId: 'bin-malformed-length',
123
- nonce: 'nb-malformed-length',
124
- nowMs: 1000,
125
- ttlMs: 60_000,
126
- }),
127
- 'shared-secret',
128
- 'k1',
129
- )
130
-
131
- const raw = encodeEnvelopeBinary(signed)
132
- const malformed = raw.slice()
133
- const version = stringFieldOffset(malformed, 0)
134
- new DataView(malformed.buffer, malformed.byteOffset, malformed.byteLength).setUint32(version.lengthOffset, raw.byteLength, false)
135
-
136
- const decoded = decodeEnvelopeBinary(malformed)
137
- expect(decoded.ok).toBe(false)
138
- expect(decoded.diagnostics.some((d) => d.message.includes('Unexpected end'))).toBe(true)
139
- })
140
-
141
- it('rejects unsupported decoded protocol fields', async () => {
142
- const signed = await signEnvelopeHmac(
143
- createEnvelope({
144
- sender: 'agent.alpha',
145
- intent: 'TASK',
146
- content: 'x',
147
- messageId: 'bin-invalid-fields',
148
- nonce: 'nb-invalid-fields',
149
- nowMs: 1000,
150
- ttlMs: 60_000,
151
- }),
152
- 'shared-secret',
153
- 'k1',
154
- )
155
-
156
- const raw = encodeEnvelopeBinary(signed)
157
- const badVersion = decodeEnvelopeBinary(replaceAscii(raw, 0, 'aip/0.2'))
158
- const badIntent = decodeEnvelopeBinary(replaceAscii(raw, 4, 'NOPE'))
159
-
160
- expect(badVersion.ok).toBe(false)
161
- expect(badVersion.diagnostics.some((d) => d.message.includes('Unsupported protocol version'))).toBe(true)
162
- expect(badIntent.ok).toBe(false)
163
- expect(badIntent.diagnostics.some((d) => d.message.includes('Unsupported intent'))).toBe(true)
164
- })
165
- })
@@ -1,312 +0,0 @@
1
- import type { ProtocolEnvelope, ProtocolSignature } from './protocol'
2
- import type { ReceiveEnvelopeResult } from './protocolTransport'
3
-
4
- const MAGIC = [0x41, 0x49, 0x50, 0x42] as const // AIPB
5
- const MAGIC_BYTES = Uint8Array.from(MAGIC)
6
- const FORMAT_VERSION = 1
7
- export const DEFAULT_MAX_BINARY_ENVELOPE_BYTES = 1024 * 1024
8
-
9
- const FLAG_RECIPIENT = 1 << 0
10
- const FLAG_CAPABILITY = 1 << 1
11
- const FLAG_CORRELATION = 1 << 2
12
- const FLAG_SIGNATURE = 1 << 3
13
- const textEncoder = new TextEncoder()
14
- const textDecoder = new TextDecoder()
15
- const INTENT_KINDS = new Set(['PING', 'PONG', 'CAPS', 'TASK', 'RESULT', 'ERROR'])
16
-
17
- export interface BinaryDecodeOptions {
18
- maxFrameBytes?: number
19
- }
20
-
21
- function utf8Encode(value: string): Uint8Array {
22
- return textEncoder.encode(value)
23
- }
24
-
25
- function utf8Decode(value: Uint8Array): string {
26
- return textDecoder.decode(value)
27
- }
28
-
29
- function signatureAlgCode(alg: ProtocolSignature['alg']): number {
30
- return alg === 'ED25519' ? 2 : 1
31
- }
32
-
33
- function signatureAlgFromCode(code: number): ProtocolSignature['alg'] | null {
34
- if (code === 1) return 'HS256'
35
- if (code === 2) return 'ED25519'
36
- return null
37
- }
38
-
39
- interface EncodedString {
40
- bytes: Uint8Array
41
- }
42
-
43
- function encodeString(value: string): EncodedString {
44
- return { bytes: utf8Encode(value) }
45
- }
46
-
47
- function encodedStringSize(encoded: EncodedString | null): number {
48
- return encoded ? 4 + encoded.bytes.byteLength : 0
49
- }
50
-
51
- class BinaryWriter {
52
- private readonly bytes: Uint8Array
53
-
54
- private readonly view: DataView
55
-
56
- private offset = 0
57
-
58
- constructor(size: number) {
59
- this.bytes = new Uint8Array(size)
60
- this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength)
61
- }
62
-
63
- writeU8(value: number): void {
64
- this.bytes[this.offset] = value & 0xff
65
- this.offset += 1
66
- }
67
-
68
- writeU32(value: number): void {
69
- this.view.setUint32(this.offset, value, false)
70
- this.offset += 4
71
- }
72
-
73
- writeU64(value: number): void {
74
- this.view.setBigUint64(this.offset, BigInt(value), false)
75
- this.offset += 8
76
- }
77
-
78
- writeBytes(value: Uint8Array): void {
79
- this.bytes.set(value, this.offset)
80
- this.offset += value.byteLength
81
- }
82
-
83
- writeString(encoded: EncodedString): void {
84
- this.writeU32(encoded.bytes.byteLength)
85
- this.writeBytes(encoded.bytes)
86
- }
87
-
88
- finish(): Uint8Array {
89
- return this.bytes
90
- }
91
- }
92
-
93
- class BinaryReader {
94
- private readonly bytes: Uint8Array
95
-
96
- private offset = 0
97
-
98
- constructor(bytes: Uint8Array) {
99
- this.bytes = bytes
100
- }
101
-
102
- remaining(): number {
103
- return this.bytes.byteLength - this.offset
104
- }
105
-
106
- readU8(): number {
107
- if (this.remaining() < 1) throw new Error('Unexpected end of binary envelope')
108
- const value = this.bytes[this.offset]
109
- this.offset += 1
110
- return value ?? 0
111
- }
112
-
113
- readU32(): number {
114
- if (this.remaining() < 4) throw new Error('Unexpected end of binary envelope')
115
- const value = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.offset, 4).getUint32(0, false)
116
- this.offset += 4
117
- return value
118
- }
119
-
120
- readU64(): number {
121
- if (this.remaining() < 8) throw new Error('Unexpected end of binary envelope')
122
- const value = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.offset, 8).getBigUint64(0, false)
123
- this.offset += 8
124
- return Number(value)
125
- }
126
-
127
- readBytes(length: number): Uint8Array {
128
- if (length < 0 || this.remaining() < length) throw new Error('Unexpected end of binary envelope')
129
- const value = this.bytes.slice(this.offset, this.offset + length)
130
- this.offset += length
131
- return value
132
- }
133
-
134
- readString(): string {
135
- const length = this.readU32()
136
- const value = this.readBytes(length)
137
- return utf8Decode(value)
138
- }
139
- }
140
-
141
- export function encodeEnvelopeBinary(envelope: ProtocolEnvelope): Uint8Array {
142
- const flags =
143
- (envelope.header.recipient ? FLAG_RECIPIENT : 0) |
144
- (envelope.body.capability ? FLAG_CAPABILITY : 0) |
145
- (envelope.body.correlationId ? FLAG_CORRELATION : 0) |
146
- (envelope.signature ? FLAG_SIGNATURE : 0)
147
-
148
- const version = encodeString(envelope.header.version)
149
- const messageId = encodeString(envelope.header.messageId)
150
- const sender = encodeString(envelope.header.sender)
151
- const nonce = encodeString(envelope.header.nonce)
152
- const intent = encodeString(envelope.body.intent)
153
- const content = encodeString(envelope.body.content)
154
- const recipient = envelope.header.recipient ? encodeString(envelope.header.recipient) : null
155
- const capability = envelope.body.capability ? encodeString(envelope.body.capability) : null
156
- const correlationId = envelope.body.correlationId ? encodeString(envelope.body.correlationId) : null
157
- const signatureKeyId = envelope.signature ? encodeString(envelope.signature.keyId) : null
158
- const signatureValue = envelope.signature ? encodeString(envelope.signature.value) : null
159
-
160
- const size =
161
- MAGIC_BYTES.byteLength +
162
- 1 +
163
- 1 +
164
- 8 +
165
- 4 +
166
- encodedStringSize(version) +
167
- encodedStringSize(messageId) +
168
- encodedStringSize(sender) +
169
- encodedStringSize(nonce) +
170
- encodedStringSize(intent) +
171
- encodedStringSize(content) +
172
- encodedStringSize(recipient) +
173
- encodedStringSize(capability) +
174
- encodedStringSize(correlationId) +
175
- (envelope.signature ? 1 : 0) +
176
- encodedStringSize(signatureKeyId) +
177
- encodedStringSize(signatureValue)
178
-
179
- const writer = new BinaryWriter(size)
180
- writer.writeBytes(MAGIC_BYTES)
181
- writer.writeU8(FORMAT_VERSION)
182
- writer.writeU8(flags)
183
- writer.writeU64(envelope.header.timestampMs)
184
- writer.writeU32(envelope.header.ttlMs)
185
-
186
- writer.writeString(version)
187
- writer.writeString(messageId)
188
- writer.writeString(sender)
189
- writer.writeString(nonce)
190
- writer.writeString(intent)
191
- writer.writeString(content)
192
-
193
- if (recipient) writer.writeString(recipient)
194
- if (capability) writer.writeString(capability)
195
- if (correlationId) writer.writeString(correlationId)
196
-
197
- if (envelope.signature && signatureKeyId && signatureValue) {
198
- writer.writeU8(signatureAlgCode(envelope.signature.alg))
199
- writer.writeString(signatureKeyId)
200
- writer.writeString(signatureValue)
201
- }
202
-
203
- return writer.finish()
204
- }
205
-
206
- export function decodeEnvelopeBinary(payload: Uint8Array, options: BinaryDecodeOptions = {}): ReceiveEnvelopeResult {
207
- try {
208
- const maxFrameBytes = options.maxFrameBytes ?? DEFAULT_MAX_BINARY_ENVELOPE_BYTES
209
- if (payload.byteLength > maxFrameBytes) {
210
- return {
211
- ok: false,
212
- diagnostics: [{ level: 'error', message: `Binary envelope exceeds maximum frame size '${maxFrameBytes}'` }],
213
- envelope: null,
214
- }
215
- }
216
-
217
- const reader = new BinaryReader(payload)
218
- const magic = [reader.readU8(), reader.readU8(), reader.readU8(), reader.readU8()]
219
- if (magic.some((value, idx) => value !== MAGIC[idx])) {
220
- return {
221
- ok: false,
222
- diagnostics: [{ level: 'error', message: 'Invalid binary envelope magic' }],
223
- envelope: null,
224
- }
225
- }
226
-
227
- const version = reader.readU8()
228
- if (version !== FORMAT_VERSION) {
229
- return {
230
- ok: false,
231
- diagnostics: [{ level: 'error', message: `Unsupported binary envelope version '${version}'` }],
232
- envelope: null,
233
- }
234
- }
235
-
236
- const flags = reader.readU8()
237
- const timestampMs = reader.readU64()
238
- const ttlMs = reader.readU32()
239
-
240
- const envelope: ProtocolEnvelope = {
241
- header: {
242
- version: reader.readString() as '7h3/0.1',
243
- messageId: reader.readString(),
244
- timestampMs,
245
- ttlMs,
246
- sender: reader.readString(),
247
- nonce: reader.readString(),
248
- },
249
- body: {
250
- intent: reader.readString() as ProtocolEnvelope['body']['intent'],
251
- content: reader.readString(),
252
- },
253
- }
254
-
255
- if (envelope.header.version !== '7h3/0.1') {
256
- return {
257
- ok: false,
258
- diagnostics: [{ level: 'error', message: `Unsupported protocol version '${envelope.header.version}'` }],
259
- envelope: null,
260
- }
261
- }
262
-
263
- if (!INTENT_KINDS.has(envelope.body.intent)) {
264
- return {
265
- ok: false,
266
- diagnostics: [{ level: 'error', message: `Unsupported intent '${envelope.body.intent}'` }],
267
- envelope: null,
268
- }
269
- }
270
-
271
- if (flags & FLAG_RECIPIENT) envelope.header.recipient = reader.readString()
272
- if (flags & FLAG_CAPABILITY) envelope.body.capability = reader.readString()
273
- if (flags & FLAG_CORRELATION) envelope.body.correlationId = reader.readString()
274
-
275
- if (flags & FLAG_SIGNATURE) {
276
- const algCode = reader.readU8()
277
- const alg = signatureAlgFromCode(algCode)
278
- if (!alg) {
279
- return {
280
- ok: false,
281
- diagnostics: [{ level: 'error', message: `Unsupported binary signature algorithm '${algCode}'` }],
282
- envelope: null,
283
- }
284
- }
285
- envelope.signature = {
286
- alg,
287
- keyId: reader.readString(),
288
- value: reader.readString(),
289
- }
290
- }
291
-
292
- if (reader.remaining() !== 0) {
293
- return {
294
- ok: false,
295
- diagnostics: [{ level: 'error', message: 'Trailing bytes found in binary envelope' }],
296
- envelope: null,
297
- }
298
- }
299
-
300
- return {
301
- ok: true,
302
- diagnostics: [],
303
- envelope,
304
- }
305
- } catch (error: unknown) {
306
- return {
307
- ok: false,
308
- diagnostics: [{ level: 'error', message: error instanceof Error ? error.message : 'Invalid binary envelope' }],
309
- envelope: null,
310
- }
311
- }
312
- }
@@ -1,70 +0,0 @@
1
- import type { WireFormat } from './protocolTransport'
2
-
3
- export type AckMode = 'fast' | 'receipt'
4
-
5
- export interface AipCapabilities {
6
- agent: string
7
- capabilities: string[]
8
- wireFormats: WireFormat[]
9
- batchMax: number
10
- ackModes: AckMode[]
11
- }
12
-
13
- export interface NegotiatedAipCapabilities {
14
- wireFormat: WireFormat
15
- batchMax: number
16
- ackMode: AckMode
17
- }
18
-
19
- const WIRE_FORMAT_ORDER: WireFormat[] = ['binary', 'compact', 'json']
20
- const ACK_MODE_ORDER: AckMode[] = ['fast', 'receipt']
21
-
22
- export function createAipCapabilities(input: {
23
- agent: string
24
- capabilities?: string[]
25
- wireFormats?: WireFormat[]
26
- batchMax?: number
27
- ackModes?: AckMode[]
28
- }): AipCapabilities {
29
- return {
30
- agent: input.agent,
31
- capabilities: input.capabilities ?? [],
32
- wireFormats: input.wireFormats ?? ['compact', 'json'],
33
- batchMax: input.batchMax ?? 1,
34
- ackModes: input.ackModes ?? ['receipt'],
35
- }
36
- }
37
-
38
- export function encodeAipCapabilities(capabilities: AipCapabilities): string {
39
- return JSON.stringify(capabilities)
40
- }
41
-
42
- export function parseAipCapabilities(raw: string): AipCapabilities | null {
43
- try {
44
- const parsed = JSON.parse(raw) as Partial<AipCapabilities>
45
- if (typeof parsed.agent !== 'string') return null
46
- if (!Array.isArray(parsed.capabilities)) return null
47
- if (!Array.isArray(parsed.wireFormats)) return null
48
- if (!Array.isArray(parsed.ackModes)) return null
49
- if (typeof parsed.batchMax !== 'number' || parsed.batchMax < 1) return null
50
- return {
51
- agent: parsed.agent,
52
- capabilities: parsed.capabilities.filter((item): item is string => typeof item === 'string'),
53
- wireFormats: parsed.wireFormats.filter((item): item is WireFormat => item === 'json' || item === 'compact' || item === 'binary'),
54
- batchMax: Math.floor(parsed.batchMax),
55
- ackModes: parsed.ackModes.filter((item): item is AckMode => item === 'fast' || item === 'receipt'),
56
- }
57
- } catch {
58
- return null
59
- }
60
- }
61
-
62
- export function negotiateAipCapabilities(local: AipCapabilities, remote: AipCapabilities): NegotiatedAipCapabilities {
63
- const wireFormat = WIRE_FORMAT_ORDER.find((format) => local.wireFormats.includes(format) && remote.wireFormats.includes(format)) ?? 'compact'
64
- const ackMode = ACK_MODE_ORDER.find((mode) => local.ackModes.includes(mode) && remote.ackModes.includes(mode)) ?? 'receipt'
65
- return {
66
- wireFormat,
67
- batchMax: Math.max(1, Math.min(local.batchMax, remote.batchMax)),
68
- ackMode,
69
- }
70
- }