note-state-id.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
10 days ago
| 1 | /** |
| 2 | * Canonical optimistic-concurrency id for vault notes (Hub proposals approve check). |
| 3 | * Format: kn1_<16 hex chars> = FNV-1a 64-bit over UTF-8 bytes (documented in docs/PROPOSAL-LIFECYCLE.md). |
| 4 | * Absent note: hash of single byte 0x00 (so new-file proposals can require "still absent"). |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @param {Buffer} buf |
| 9 | * @returns {string} 16 lowercase hex chars |
| 10 | */ |
| 11 | export function fnv1a64Hex(buf) { |
| 12 | let h = 0xcbf29ce484222325n; |
| 13 | const prime = 0x100000001b3n; |
| 14 | for (let i = 0; i < buf.length; i++) { |
| 15 | h ^= BigInt(buf[i]); |
| 16 | h = (h * prime) & 0xffffffffffffffffn; |
| 17 | } |
| 18 | return h.toString(16).padStart(16, '0'); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Deterministic JSON stringify with sorted object keys (no arrays in frontmatter required for v1). |
| 23 | * @param {unknown} value |
| 24 | * @returns {string} |
| 25 | */ |
| 26 | export function stableStringify(value) { |
| 27 | if (value === null || value === undefined) return 'null'; |
| 28 | const t = typeof value; |
| 29 | if (t === 'number' || t === 'boolean') return JSON.stringify(value); |
| 30 | if (t === 'string') return JSON.stringify(value); |
| 31 | if (Array.isArray(value)) { |
| 32 | return '[' + value.map((x) => stableStringify(x)).join(',') + ']'; |
| 33 | } |
| 34 | if (t === 'object') { |
| 35 | const keys = Object.keys(value).sort(); |
| 36 | return '{' + keys.map((k) => JSON.stringify(k) + ':' + stableStringify(value[k])).join(',') + '}'; |
| 37 | } |
| 38 | return JSON.stringify(value); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * State id when the note path has no file (create flow). |
| 43 | * @returns {string} |
| 44 | */ |
| 45 | export function absentNoteStateId() { |
| 46 | return 'kn1_' + fnv1a64Hex(Buffer.from([0x00])); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * State id from parsed frontmatter object + body (matches Hub readNote semantics). |
| 51 | * @param {Record<string, unknown>} frontmatter |
| 52 | * @param {string} body |
| 53 | * @returns {string} |
| 54 | */ |
| 55 | export function noteStateIdFromParts(frontmatter, body) { |
| 56 | const fm = stableStringify(frontmatter && typeof frontmatter === 'object' ? frontmatter : {}); |
| 57 | const payload = `${fm}\0${body ?? ''}`; |
| 58 | return 'kn1_' + fnv1a64Hex(Buffer.from(payload, 'utf8')); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Hash exact frontmatter JSON text + body (hosted canister / string responses). |
| 63 | * @param {string} frontmatterJsonText |
| 64 | * @param {string} body |
| 65 | * @returns {string} |
| 66 | */ |
| 67 | export function noteStateIdFromRawStrings(frontmatterJsonText, body) { |
| 68 | const fm = typeof frontmatterJsonText === 'string' ? frontmatterJsonText : ''; |
| 69 | const payload = `${fm}\0${body ?? ''}`; |
| 70 | return 'kn1_' + fnv1a64Hex(Buffer.from(payload, 'utf8')); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Derive kn1_ id from a Hub GET /notes/:path JSON payload (object or string frontmatter). |
| 75 | * @param {{ frontmatter?: unknown, body?: string }} data |
| 76 | * @returns {string} |
| 77 | */ |
| 78 | export function noteStateIdFromHubNoteJson(data) { |
| 79 | const body = data?.body ?? ''; |
| 80 | const fm = data?.frontmatter; |
| 81 | if (typeof fm === 'string') { |
| 82 | return noteStateIdFromRawStrings(fm, body); |
| 83 | } |
| 84 | return noteStateIdFromParts(fm && typeof fm === 'object' ? fm : {}, body); |
| 85 | } |
File History
4 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
10 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b
fix(7C-L1b): route hosted delegation proposals through cani…
Human
minor
⚠
30 days ago
sha256:2827ba9e7632a4b141c50caf1e8f7d77abbc3515be20e7465f2bccb0ac4edf91
fix: repair endpoint now sets has_active_subscription when …
Human
minor
⚠
49 days ago
sha256:6a102aafafdfe7e70a24f4e59740200f0ee713ce7915f1b53e9d4ba5ee8b4410
Initial Muse snapshot
Human
82 days ago