Skip to content

Commit 100876c

Browse files
Claude/resume repos migration 9 o2 u1 (#31)
<!-- SPDX-License-Identifier: PMPL-1.0-or-later --> ## Summary <!-- Briefly describe what this PR does and why. Link to related issues with "Closes #N". --> ## Changes <!-- List the key changes introduced by this PR. --> - ## RSR Quality Checklist <!-- Check all that apply. PRs that fail required checks will not be merged. --> ### Required - [ ] Tests pass (`just test` or equivalent) - [ ] Code is formatted (`just fmt` or equivalent) - [ ] Linter is clean (no new warnings or errors) - [ ] No banned language patterns (no TypeScript, no npm/bun, no Go/Python) - [ ] No `unsafe` blocks without `// SAFETY:` comments - [ ] No banned functions (`believe_me`, `unsafeCoerce`, `Obj.magic`, `Admitted`, `sorry`) - [ ] SPDX license headers present on all new/modified source files - [ ] No secrets, credentials, or `.env` files included ### As Applicable - [ ] `.machine_readable/STATE.a2ml` updated (if project state changed) - [ ] `.machine_readable/ECOSYSTEM.a2ml` updated (if integrations changed) - [ ] `.machine_readable/META.a2ml` updated (if architectural decisions changed) - [ ] Documentation updated for user-facing changes - [ ] `TOPOLOGY.md` updated (if architecture changed) - [ ] `CHANGELOG` or release notes updated - [ ] New dependencies reviewed for license compatibility (PMPL-1.0-or-later / MPL-2.0) - [ ] ABI/FFI changes validated (`src/abi/` and `ffi/zig/` consistent) ## Testing <!-- Describe how you tested these changes. --> ## Screenshots <!-- If applicable, add screenshots or terminal output demonstrating the change. --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 408bf7f commit 100876c

3 files changed

Lines changed: 83 additions & 13 deletions

File tree

cartridges/local-coord-mcp/ffi/local_coord_ffi.zig

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,11 @@ pub export fn coord_get_affinities(
17461746
//
17471747
// Suggestion kinds:
17481748
// - "overclaim": avg_confidence > 0.8 AND effective_affinity < 0.3
1749+
// → routing FYI (op_kind=fyi, tier 1)
1750+
// - "drift": same condition as overclaim, but framed as a self-
1751+
// assessment monitoring flag (DD-9 layer D). Emitted
1752+
// alongside overclaim with op_kind=warn, tier 2, and a
1753+
// drift_pct field carrying the confidence-vs-affinity gap.
17491754
// - "promote": effective_affinity >= 0.7 AND tag not in declared set
17501755
// - "remove": effective_affinity <= 0.2 AND attempts >= 5
17511756
//
@@ -1791,6 +1796,20 @@ fn enqueueServerSuggestion(
17911796
return -1;
17921797
}
17931798

1799+
/// Internal kind→string for engine-side rendering. Mirrors the adapter's
1800+
/// kindName but lives in FFI so server-origin envelopes can name peers
1801+
/// correctly. Updated when ClientKind grows (Task #33 added openai/mistral).
1802+
fn kindStr(k: u8) []const u8 {
1803+
return switch (k) {
1804+
0 => "claude",
1805+
1 => "gemini",
1806+
2 => "copilot",
1807+
4 => "openai",
1808+
5 => "mistral",
1809+
else => "custom",
1810+
};
1811+
}
1812+
17941813
fn affinityPct(attempts: u16, successes: u16) u32 {
17951814
if (attempts == 0) return 0;
17961815
return (@as(u32, successes) * 100) / @as(u32, attempts);
@@ -1874,21 +1893,29 @@ pub export fn coord_scan_suggestions(
18741893
if (agg.attempts == 0) continue;
18751894
const pct = affinityPct(agg.attempts, agg.successes);
18761895
const tag_slice: []const u8 = agg.tag[0..agg.tag_len];
1877-
const kind_str = switch (agg.client_kind) {
1878-
0 => "claude",
1879-
1 => "gemini",
1880-
2 => "copilot",
1881-
else => "custom",
1882-
};
1896+
const kind_str = kindStr(agg.client_kind);
18831897

18841898
// Overclaim: high self-confidence + low real affinity.
1899+
// (Routing suggestion — op_kind=fyi, tier 1.)
18851900
const avg_conf = windowedAvgConfidence(agg.client_kind, tag_slice);
18861901
if (avg_conf != 256 and avg_conf >= OVERCLAIM_CONF_MIN and pct < OVERCLAIM_AFFINITY_MAX) {
18871902
const msg = std.fmt.bufPrint(&msg_buf,
18881903
"{{\"kind\":\"overclaim\",\"client_kind\":\"{s}\",\"tag\":\"{s}\",\"attempts\":{d},\"successes\":{d},\"effective_affinity_pct\":{d},\"avg_confidence_pct\":{d},\"op_kind\":\"fyi\",\"rationale\":\"high self-confidence with low track-record success — reassignment suggested\"}}",
18891904
.{ kind_str, tag_slice, agg.attempts, agg.successes, pct, avg_conf },
18901905
) catch continue;
18911906
if (enqueueServerSuggestion(-1, 1, msg) >= 0) emitted += 1;
1907+
1908+
// Drift detector (DD-9 layer D / TODO P1): same condition, but
1909+
// framed as a self-assessment monitoring flag for the master
1910+
// to act on at the peer-trust level (op_kind=warn, tier 2).
1911+
// Carries `drift_pct = avg_conf - effective_affinity` so the
1912+
// master sees the magnitude of the gap at a glance.
1913+
const drift_pct: u32 = avg_conf - pct;
1914+
const drift_msg = std.fmt.bufPrint(&msg_buf,
1915+
"{{\"kind\":\"drift\",\"client_kind\":\"{s}\",\"tag\":\"{s}\",\"attempts\":{d},\"successes\":{d},\"effective_affinity_pct\":{d},\"avg_confidence_pct\":{d},\"drift_pct\":{d},\"op_kind\":\"warn\",\"rationale\":\"self-assessment drift — confidence consistently outpaces track-record success\"}}",
1916+
.{ kind_str, tag_slice, agg.attempts, agg.successes, pct, avg_conf, drift_pct },
1917+
) catch continue;
1918+
if (enqueueServerSuggestion(-1, 2, drift_msg) >= 0) emitted += 1;
18921919
}
18931920

18941921
// Promote: high effective affinity, but tag not in any same-kind peer's declared list.
@@ -3139,23 +3166,62 @@ test "scan flags overclaim: high confidence + low effective_affinity" {
31393166
const n = coord_scan_suggestions(&tok, TOKEN_LEN);
31403167
try std.testing.expect(n >= 1);
31413168

3142-
// The quarantine entry should be a server-origin envelope with
3143-
// "overclaim" in the body.
3169+
// The quarantine should contain server-origin envelopes for both
3170+
// overclaim (routing FYI, tier 1) and drift (monitoring warn, tier 2).
31443171
var found_overclaim: bool = false;
31453172
var found_remove: bool = false;
3173+
var found_drift: bool = false;
3174+
var drift_tier: u8 = 0;
31463175
for (&quarantine) |*q| {
31473176
if (!q.active) continue;
31483177
try std.testing.expectEqual(SERVER_ORIGIN_SENTINEL, q.sender_idx);
3149-
if (std.mem.indexOf(u8, q.msg[0..q.msg_len], "overclaim") != null) found_overclaim = true;
3150-
if (std.mem.indexOf(u8, q.msg[0..q.msg_len], "remove") != null) found_remove = true;
3178+
if (std.mem.indexOf(u8, q.msg[0..q.msg_len], "\"kind\":\"overclaim\"") != null) found_overclaim = true;
3179+
if (std.mem.indexOf(u8, q.msg[0..q.msg_len], "\"kind\":\"remove\"") != null) found_remove = true;
3180+
if (std.mem.indexOf(u8, q.msg[0..q.msg_len], "\"kind\":\"drift\"") != null) {
3181+
found_drift = true;
3182+
drift_tier = q.risk_tier;
3183+
// Drift envelope must carry the gap magnitude.
3184+
try std.testing.expect(std.mem.indexOf(u8, q.msg[0..q.msg_len], "\"drift_pct\":") != null);
3185+
try std.testing.expect(std.mem.indexOf(u8, q.msg[0..q.msg_len], "\"op_kind\":\"warn\"") != null);
3186+
}
31513187
}
31523188
try std.testing.expect(found_overclaim);
3189+
try std.testing.expect(found_drift);
3190+
try std.testing.expectEqual(@as(u8, 2), drift_tier);
31533191
// 5 attempts + 20% affinity also fires the remove rule.
31543192
try std.testing.expect(found_remove);
31553193

31563194
coord_reset();
31573195
}
31583196

3197+
test "drift uses Task #33 kind names (openai)" {
3198+
coord_reset();
3199+
var tok: [TOKEN_LEN]u8 = undefined;
3200+
var suf: [4]u8 = undefined;
3201+
_ = coord_register(4, -1, &tok, &suf); // openai
3202+
3203+
const tag = "rust-codegen";
3204+
_ = coord_report_outcome(&tok, TOKEN_LEN, tag.ptr, @intCast(tag.len), 1, 100, 2, 90);
3205+
for (0..4) |_| {
3206+
_ = coord_report_outcome(&tok, TOKEN_LEN, tag.ptr, @intCast(tag.len), 0, 100, 2, 90);
3207+
}
3208+
_ = coord_scan_suggestions(&tok, TOKEN_LEN);
3209+
3210+
var found_openai_drift: bool = false;
3211+
for (&quarantine) |*q| {
3212+
if (!q.active) continue;
3213+
const body = q.msg[0..q.msg_len];
3214+
if (std.mem.indexOf(u8, body, "\"kind\":\"drift\"") != null and
3215+
std.mem.indexOf(u8, body, "\"client_kind\":\"openai\"") != null)
3216+
{
3217+
found_openai_drift = true;
3218+
}
3219+
}
3220+
try std.testing.expect(found_openai_drift);
3221+
3222+
coord_reset();
3223+
}
3224+
31593225
test "scan flags promote: high affinity on undeclared tag" {
31603226
coord_reset();
31613227
var tok: [TOKEN_LEN]u8 = undefined;

docs/handover/COORD-MCP-STATE.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ mission drift.
105105
- Layer A (static affinity, register-time) — **done** (DD-34 landed as Task #34 pending, but declared affinities already accepted).
106106
- Layer B (per-claim `confidence` + `reasoning`) — **done** (DD-9, Task #15).
107107
- Layer C (server-computed `effective_affinity` from track record) — **done** (DD-9, Task #13).
108-
- Layer D (drift detector: confidence ≫ track record → flag) — **pending P1**.
108+
- Layer D (drift detector: confidence ≫ track record → flag) — **done**.
109+
Implemented in `coord_scan_suggestions` as a parallel `kind:"drift"`
110+
envelope (op_kind=warn, tier 2, carries `drift_pct = avg_conf − eff_affinity`).
111+
Co-emitted with `overclaim` so the master sees a routing FYI **and** a
112+
self-assessment monitoring flag from the same condition.
109113

110114
## Architectural spine — decisions taken
111115

docs/handover/COORD-MCP-TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Last updated: 2026-04-20.
2727
- **Warn-drift broadcast on auto-release** via Opus review. (DD-21.)
2828
- **Quarantine queue spill to VeriSimDB** when full; currently `MAX_QUARANTINE=32` hot cache only. (DD-17.)
2929
- **Audit-echo anchor** — preserve old chain head on peer crash/restart; new peer = fresh chain. (DD-29.)
30-
- **Drift detector** — flag `confidence > 0.8 AND effective_affinity < 0.3`. (DD-9 layer D.)
31-
- **`coord_health` metrics tool** — active peers, pending quarantine, reject rate, claim depth.
30+
- ~~**Drift detector** — flag `confidence > 0.8 AND effective_affinity < 0.3`. (DD-9 layer D.)~~**done**: `coord_scan_suggestions` now emits a parallel `kind:"drift"` envelope (op_kind=warn, tier 2, includes `drift_pct`) alongside the routing-focused `overclaim`.
31+
- ~~**`coord_health` metrics tool** — active peers, pending quarantine, reject rate, claim depth.~~**done**: `coord_health` returns peers (active/by_kind/by_role), quarantine, claims, track-record fill, and per-kind rejects + cooldown flags.
3232
- **Rejection rate limit hardening** — 5 rejects / 10 min per `client_kind` already lands cooldown; audit whether per-peer is better on heavy multi-session load.
3333

3434
### 007-mcp + 007-lang

0 commit comments

Comments
 (0)