{"version":3,"file":"negotiation.d.ts","sourceRoot":"","sources":["../src/negotiation.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0C,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AAE5F,eAAO,MAAM,gBAAgB,oOAanB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE9D,MAAM,WAAW,oBAAoB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC5B,QAAQ,EAAE,eAAe,CAAC;IAC1B,eAAe,EAAE,CAAC,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,SAAS,oBAAoB,EAAE,CAAC,CAAC,CAAC;IAC7E,YAAY,EAAE,SAAS,aAAa,EAAE,CAAC;CACvC;AAED,MAAM,WAAW,yBAAyB;IACzC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,oBAAoB;IACpC,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,yBAAyB,CAAC,CAAC,CAAC;IACvE,YAAY,EAAE,aAAa,EAAE,CAAC;CAC9B;AAED,eAAO,MAAM,iBAAiB,EAAE,YAkB/B,CAAC;AAEF,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,GAAG,oBAAoB,CAyBrG;AAED,wBAAgB,2BAA2B,CAC1C,MAAM,EAAE,oBAAoB,EAC5B,QAAQ,EAAE,eAAe,EACzB,UAAU,EAAE,aAAa,GACvB,yBAAyB,CAK3B","sourcesContent":["import { ASM_CONTRACT_MAJOR, ASM_CONTRACT_MINOR, type AsmContractName } from \"./schemas.js\";\n\nexport const ASM_CAPABILITIES = [\n\t\"memory.read\",\n\t\"memory.write\",\n\t\"evidence.read\",\n\t\"retrieval.query\",\n\t\"intent.parse\",\n\t\"intent.route\",\n\t\"policy.decide\",\n\t\"policy.approval\",\n\t\"execution.idempotent\",\n\t\"feedback.write\",\n\t\"state.snapshot\",\n\t\"inference.manifest\",\n] as const;\n\nexport type AsmCapability = (typeof ASM_CAPABILITIES)[number];\n\nexport interface ContractVersionRange {\n\tmajor: number;\n\tminMinor: number;\n\tmaxMinor: number;\n}\n\nexport interface AsmPeerHello {\n\tprotocol: \"asm-contracts\";\n\tprotocolVersion: 1;\n\tcontracts: Partial<Record<AsmContractName, readonly ContractVersionRange[]>>;\n\tcapabilities: readonly AsmCapability[];\n}\n\nexport interface NegotiatedContractVersion {\n\tmajor: number;\n\tminor: number;\n}\n\nexport interface AsmNegotiationResult {\n\tcontracts: Partial<Record<AsmContractName, NegotiatedContractVersion>>;\n\tcapabilities: AsmCapability[];\n}\n\nexport const DEFAULT_ASM_HELLO: AsmPeerHello = {\n\tprotocol: \"asm-contracts\",\n\tprotocolVersion: 1,\n\tcontracts: Object.fromEntries(\n\t\t(\n\t\t\t[\n\t\t\t\t\"memory\",\n\t\t\t\t\"evidence\",\n\t\t\t\t\"retrieval\",\n\t\t\t\t\"intent\",\n\t\t\t\t\"policy\",\n\t\t\t\t\"feedback\",\n\t\t\t\t\"stateSnapshot\",\n\t\t\t\t\"inferenceManifest\",\n\t\t\t] as const\n\t\t).map((name) => [name, [{ major: ASM_CONTRACT_MAJOR, minMinor: 0, maxMinor: ASM_CONTRACT_MINOR }]]),\n\t),\n\tcapabilities: ASM_CAPABILITIES,\n};\n\nexport function negotiateAsmContracts(local: AsmPeerHello, remote: AsmPeerHello): AsmNegotiationResult {\n\tif (local.protocol !== remote.protocol || local.protocolVersion !== remote.protocolVersion) {\n\t\tthrow new Error(\"Incompatible ASM contract protocol\");\n\t}\n\tconst contracts: Partial<Record<AsmContractName, NegotiatedContractVersion>> = {};\n\tfor (const name of Object.keys(local.contracts) as AsmContractName[]) {\n\t\tconst localRanges = local.contracts[name] ?? [];\n\t\tconst remoteRanges = remote.contracts[name] ?? [];\n\t\tconst candidates: NegotiatedContractVersion[] = [];\n\t\tfor (const left of localRanges) {\n\t\t\tfor (const right of remoteRanges) {\n\t\t\t\tif (left.major !== right.major) continue;\n\t\t\t\tconst minMinor = Math.max(left.minMinor, right.minMinor);\n\t\t\t\tconst maxMinor = Math.min(left.maxMinor, right.maxMinor);\n\t\t\t\tif (minMinor <= maxMinor) candidates.push({ major: left.major, minor: maxMinor });\n\t\t\t}\n\t\t}\n\t\tcandidates.sort((left, right) => right.major - left.major || right.minor - left.minor);\n\t\tif (candidates[0]) contracts[name] = candidates[0];\n\t}\n\tconst remoteCapabilities = new Set(remote.capabilities);\n\treturn {\n\t\tcontracts,\n\t\tcapabilities: local.capabilities.filter((capability) => remoteCapabilities.has(capability)),\n\t};\n}\n\nexport function requireNegotiatedCapability(\n\tresult: AsmNegotiationResult,\n\tcontract: AsmContractName,\n\tcapability: AsmCapability,\n): NegotiatedContractVersion {\n\tconst version = result.contracts[contract];\n\tif (!version) throw new Error(`ASM contract not negotiated: ${contract}`);\n\tif (!result.capabilities.includes(capability)) throw new Error(`ASM capability not negotiated: ${capability}`);\n\treturn version;\n}\n"]}