hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
hop / Omega / tests / HopFactCheck.swift
| 1 | // Native caller check for the hopfact core (Omega/hopfact.om, contract in Omega/README.md). |
| 2 | // Calls omega_hopfact through the generated C binding exactly as Hop's Swift host does: |
| 3 | // two reused [UInt64](256) buffers, no per-call allocation, status != 0 treated as INVALID. |
| 4 | // Prints PASS/FAIL per check; the last line is HOPFACT-CHECK PASS or FAIL n. |
| 5 | import Foundation |
| 6 | |
| 7 | let magic: UInt64 = 1213157446 |
| 8 | let none = UInt64.max // -1: absent / unknown |
| 9 | let now: UInt64 = 1_000_000_000_000 |
| 10 | |
| 11 | var fails = 0 |
| 12 | func check(_ name: String, _ ok: Bool, _ detail: @autoclosure () -> String = "") { |
| 13 | if ok { print("PASS \(name)") } else { fails += 1; print("FAIL \(name) \(detail())") } |
| 14 | } |
| 15 | |
| 16 | struct Row { var id: UInt64; var kind: UInt64; var state: UInt64; var count: UInt64; var valid: UInt64 = 1 } |
| 17 | |
| 18 | func snapshot(_ rows: [Row], link: UInt64 = 1, mask: UInt64 = 1, gen: UInt64 = now - 5000, n: UInt64? = nil) -> [UInt64] { |
| 19 | var w = [UInt64](repeating: 0, count: 256) |
| 20 | w[0] = magic; w[1] = 1; w[2] = n ?? UInt64(rows.count); w[3] = now; w[4] = gen; w[5] = link; w[6] = mask |
| 21 | for (r, x) in rows.enumerated() { |
| 22 | let b = 8 + 5 * r |
| 23 | w[b] = x.id; w[b + 1] = x.kind; w[b + 2] = x.state; w[b + 3] = x.count; w[b + 4] = x.valid |
| 24 | } |
| 25 | return w |
| 26 | } |
| 27 | |
| 28 | var res = [UInt64](repeating: 0xAAAA_AAAA_AAAA_AAAA, count: 256) |
| 29 | |
| 30 | @discardableResult |
| 31 | func call(_ inp: [UInt64], inLen: UInt64 = 256) -> (status: UInt32, verdict: UInt64) { |
| 32 | var v: UInt64 = 0xDEAD |
| 33 | let st = inp.withUnsafeBytes { i in |
| 34 | res.withUnsafeMutableBytes { o in |
| 35 | omega_hopfact(&v, i.baseAddress, inLen, o.baseAddress, 256) |
| 36 | } |
| 37 | } |
| 38 | return (st, v) |
| 39 | } |
| 40 | |
| 41 | // Base (what Hop sends): two stations (running, idle) and the six reported review/merge groups. |
| 42 | let base: [Row] = [ |
| 43 | Row(id: 100, kind: 1, state: 4, count: 3), |
| 44 | Row(id: 101, kind: 1, state: 1, count: 0), |
| 45 | Row(id: 500, kind: 2, state: 1, count: 1), // review bay |
| 46 | Row(id: 501, kind: 3, state: 1, count: 0), // review quota |
| 47 | Row(id: 502, kind: 4, state: 1, count: 0), // review decision |
| 48 | Row(id: 503, kind: 5, state: 1, count: 0), // merge ready |
| 49 | Row(id: 504, kind: 6, state: 1, count: 0), // merge owner |
| 50 | Row(id: 505, kind: 7, state: 1, count: 0), // merge decision |
| 51 | ] |
| 52 | |
| 53 | var r = call(snapshot(base)) |
| 54 | check("working: status 0, verdict 7 WORKING reason 60", r.status == 0 && r.verdict == 7 && res[2] == 7 && res[3] == 60, "\(r) \(res[2]) \(res[3])") |
| 55 | check("working: header magic/version/nrows/age", res[0] == magic && res[1] == 1 && res[5] == 8 && res[6] == 5000) |
| 56 | check("working: presence mask kinds 1..7", res[7] == 0xFE, "\(res[7])") |
| 57 | check("working: category counts WORK 2, IDLE 6", res[16] == 2 && res[20] == 6 && res[8...23].reduce(0, +) == 8) |
| 58 | check("working: kind totals; unsent kinds 8..15 are -1, not 0", |
| 59 | res[25] == 3 && res[26] == 1 && res[27...31].allSatisfy { $0 == 0 } && res[32...39].allSatisfy { $0 == none }) |
| 60 | check("working: agents, quota unknown (-1); attention 0; fresh admission 1", res[40] == none && res[41] == none && res[42] == none && res[43] == 0 && res[45] == 1) |
| 61 | check("working: order 100,500,101,501..505", Array(res[48..<56]) == [100, 500, 101, 501, 502, 503, 504, 505] && res[56] == 0, "\(Array(res[48..<56]))") |
| 62 | check("working: per-row categories", Array(res[96..<104]) == [8, 12, 8, 12, 12, 12, 12, 12]) |
| 63 | |
| 64 | var clear = base; clear[0].state = 1; clear[2].count = 0 |
| 65 | r = call(snapshot(clear)) |
| 66 | check("clear: verdict 8 reason 0 (no reported blockers in declared coverage)", r.verdict == 8 && res[3] == 0 && res[20] == 8) |
| 67 | |
| 68 | var groups = base |
| 69 | groups[4].count = 2 // review decision |
| 70 | groups[5].count = 1 // merge ready |
| 71 | groups += [ |
| 72 | Row(id: 506, kind: 10, state: 1, count: 4), // unrecognized group: kept visible |
| 73 | Row(id: 507, kind: 9, state: 1, count: 7), // superseded: neutral |
| 74 | Row(id: 508, kind: 15, state: 1, count: 2), // merge triage |
| 75 | ] |
| 76 | r = call(snapshot(groups)) |
| 77 | check("triage: verdict 6 ATTENTION reason 42 (decision first)", r.verdict == 6 && res[3] == 42, "\(r) \(res[3])") |
| 78 | check("triage: order decision, triage, ready, work, other, neutral, idle", |
| 79 | Array(res[48..<59]) == [502, 508, 503, 100, 500, 506, 507, 101, 501, 504, 505], "\(Array(res[48..<59]))") |
| 80 | check("triage: attention rows 4, TRIAGE 1, OTHER 1, NEUTRAL 1", res[43] == 4 && res[13] == 1 && res[18] == 1 && res[19] == 1) |
| 81 | check("triage: totals decision 2, ready 1, superseded 7, other 4, triage 2, elsewhere -1", |
| 82 | res[28] == 2 && res[29] == 1 && res[33] == 7 && res[34] == 4 && res[39] == 2 && res[32] == none) |
| 83 | |
| 84 | let opt = base + [Row(id: 200, kind: 11, state: 1, count: 30), Row(id: 300, kind: 12, state: 1, count: 650)] |
| 85 | r = call(snapshot(opt, mask: 3)) |
| 86 | check("optional kinds: agent working 1/1, min quota 650, verdict 7", r.verdict == 7 && res[40] == 1 && res[41] == 1 && res[42] == 650) |
| 87 | |
| 88 | var unk = base; unk[0].state = 0 |
| 89 | r = call(snapshot(unk)) |
| 90 | check("negative: unknown station state is UNKNOWN, never idle/clear (verdict 6 reason 41)", r.verdict == 6 && res[3] == 41 && res[96] == 1 && res[48] == 100, "\(r) \(res[3]) \(res[96])") |
| 91 | var ug = clear; ug[3].state = 0 |
| 92 | r = call(snapshot(ug)) |
| 93 | check("negative: unknown group state is UNKNOWN (verdict 6 reason 41)", r.verdict == 6 && res[3] == 41 && res[99] == 1) |
| 94 | var dec = clear; dec[1].valid = 0 |
| 95 | r = call(snapshot(dec)) |
| 96 | check("negative: host decode failure (valid 0) is UNKNOWN", r.verdict == 6 && res[97] == 1) |
| 97 | |
| 98 | // Fill the output, then refuse: an INVALID result must leave only the header. |
| 99 | call(snapshot(groups)) |
| 100 | r = call(snapshot(base, n: 49)) |
| 101 | check("negative: overcapacity nrows 49 -> INVALID reason 3 at word 2", r.status == 0 && r.verdict == 1 && res[3] == 3 && res[4] == 2, "\(r) \(res[3]) \(res[4])") |
| 102 | check("negative: INVALID output zeroed past header", res[0] == magic && res[2] == 1 && res[5...].allSatisfy { $0 == 0 }) |
| 103 | var neg = base; neg[0].count = UInt64(bitPattern: -1) |
| 104 | r = call(snapshot(neg)) |
| 105 | check("negative: count -1 -> INVALID reason 4 at word 11", r.verdict == 1 && res[3] == 4 && res[4] == 11, "\(res[3]) \(res[4])") |
| 106 | var big = base; big[0].count = 1_000_001 |
| 107 | r = call(snapshot(big)) |
| 108 | check("negative: station count over 1e6 -> INVALID reason 5 at word 11", r.verdict == 1 && res[3] == 5 && res[4] == 11) |
| 109 | var qbig = opt; qbig[9].count = 1001 |
| 110 | r = call(snapshot(qbig, mask: 3)) |
| 111 | check("negative: quota permille 1001 -> INVALID reason 5 at word 56", r.verdict == 1 && res[3] == 5 && res[4] == 56, "\(res[3]) \(res[4])") |
| 112 | var st6 = base; st6[0].state = 6 |
| 113 | r = call(snapshot(st6)) |
| 114 | check("negative: undefined state code -> INVALID reason 14", r.verdict == 1 && res[3] == 14 && res[4] == 10) |
| 115 | var k16 = base; k16[7].kind = 16 |
| 116 | r = call(snapshot(k16)) |
| 117 | check("negative: undefined kind 16 -> INVALID reason 6 at word 44", r.verdict == 1 && res[3] == 6 && res[4] == 44) |
| 118 | var dup = base; dup[3].id = 100 |
| 119 | r = call(snapshot(dup)) |
| 120 | check("negative: duplicate id -> INVALID reason 8 at the second row", r.verdict == 1 && res[3] == 8 && res[4] == 23) |
| 121 | var pad = snapshot(base); pad[200] = 1 |
| 122 | r = call(pad) |
| 123 | check("negative: nonzero padding -> INVALID reason 10 at word 200", r.verdict == 1 && res[3] == 10 && res[4] == 200) |
| 124 | var bad = snapshot(base); bad[0] = 0 |
| 125 | r = call(bad) |
| 126 | check("negative: bad magic -> INVALID reason 1", r.verdict == 1 && res[3] == 1 && res[4] == 0) |
| 127 | r = call(snapshot(opt, mask: 1)) |
| 128 | check("negative: quota row without usage source -> INVALID reason 9 at word 54", r.verdict == 1 && res[3] == 9 && res[4] == 54) |
| 129 | r = call(snapshot(base, mask: 0)) |
| 130 | check("negative: station rows without floor source -> INVALID reason 9", r.verdict == 1 && res[3] == 9 && res[4] == 9) |
| 131 | |
| 132 | r = call(snapshot(base, link: 2)) |
| 133 | check("unavailable: health failed -> OFFLINE 2 reason 20, admission 0", r.verdict == 2 && res[3] == 20 && res[45] == 0) |
| 134 | r = call(snapshot(clear, link: 3)) |
| 135 | check("unavailable: stream disconnected -> UNAVAILABLE 3 reason 22 even with idle rows, admission 0", r.verdict == 3 && res[3] == 22 && res[45] == 0) |
| 136 | r = call(snapshot(clear, link: 0)) |
| 137 | check("unavailable: link unknown -> UNAVAILABLE 3 reason 21, admission 0", r.verdict == 3 && res[3] == 21 && res[45] == 0) |
| 138 | r = call(snapshot([], mask: 0)) |
| 139 | check("partial: floor stream not received -> PARTIAL 5 reason 26, admission 0", r.verdict == 5 && res[3] == 26 && res[45] == 0) |
| 140 | r = call(snapshot([])) |
| 141 | check("negative: empty rows are PARTIAL 5 reason 29 kind 1, never CLEAR; station total -1", |
| 142 | r.verdict == 5 && res[3] == 29 && res[4] == 1 && res[25] == none) |
| 143 | r = call(snapshot(clear.filter { $0.kind != 4 })) |
| 144 | check("partial: review decision group absent -> PARTIAL reason 30 kind 4, admission still 1", r.verdict == 5 && res[3] == 30 && res[4] == 4 && res[28] == none && res[45] == 1) |
| 145 | r = call(snapshot(clear.filter { $0.kind != 6 })) |
| 146 | check("partial: merge owner group absent -> PARTIAL reason 31 kind 6", r.verdict == 5 && res[3] == 31 && res[4] == 6) |
| 147 | r = call(snapshot(clear, gen: now - 60001)) |
| 148 | check("stale: 60001 ms old -> STALE 4 reason 24, age 60001, admission 0", r.verdict == 4 && res[3] == 24 && res[6] == 60001 && res[45] == 0) |
| 149 | r = call(snapshot(clear, gen: 0)) |
| 150 | check("stale: generated absent -> STALE reason 23, age -1, admission 0", r.verdict == 4 && res[3] == 23 && res[6] == none && res[45] == 0) |
| 151 | r = call(snapshot(clear, gen: now + 30001)) |
| 152 | check("stale: generated 30001 ms ahead -> STALE reason 25, admission 0", r.verdict == 4 && res[3] == 25 && res[6] == none && res[45] == 0) |
| 153 | |
| 154 | // Bridge refusals: status 32, result and output untouched. |
| 155 | call(snapshot(clear)); let before = res |
| 156 | r = call(snapshot(clear), inLen: 255) |
| 157 | check("bridge: wrong length -> status 32, result and output untouched", r.status == 32 && r.verdict == 0xDEAD && res == before, "\(r)") |
| 158 | var same = snapshot(clear); var v: UInt64 = 0xDEAD |
| 159 | let ov = same.withUnsafeMutableBytes { b in omega_hopfact(&v, UnsafeRawPointer(b.baseAddress), 256, b.baseAddress, 256) } |
| 160 | check("bridge: overlapping in/out -> status 32", ov == 32 && v == 0xDEAD) |
| 161 | |
| 162 | // Latency on reused buffers (the call path Hop uses). |
| 163 | let inp = snapshot(groups) |
| 164 | let iters = 200_000 |
| 165 | let t0 = DispatchTime.now().uptimeNanoseconds |
| 166 | var acc: UInt64 = 0 |
| 167 | for _ in 0..<iters { acc &+= call(inp).verdict } |
| 168 | let ns = Double(DispatchTime.now().uptimeNanoseconds - t0) / Double(iters) |
| 169 | check("latency: \(iters) calls, \(String(format: "%.0f", ns)) ns/call", acc == UInt64(iters) * 6) |
| 170 | |
| 171 | print(fails == 0 ? "HOPFACT-CHECK PASS" : "HOPFACT-CHECK FAIL \(fails)") |
| 172 | exit(fails == 0 ? 0 : 1) |