|
26 | 26 | #include "yyjson/yyjson.h" |
27 | 27 |
|
28 | 28 | #include <ctype.h> |
| 29 | +#include <limits.h> |
| 30 | +#include <stdatomic.h> |
29 | 31 | #include <stdbool.h> |
30 | 32 | #include <stdint.h> |
31 | 33 | #include <stdio.h> |
@@ -1414,6 +1416,129 @@ static void ha_tool_call_free(ha_tool_call_t *call) { |
1414 | 1416 | call->input = NULL; |
1415 | 1417 | } |
1416 | 1418 |
|
| 1419 | +/* Drift reminder (#509). A Claude Code session that keeps searching with Grep, |
| 1420 | + * Glob or Bash and makes no codebase-memory-mcp tool call gets one factual line |
| 1421 | + * on its next augmentation once HA_DRIFT_FIRST searches have run; the threshold |
| 1422 | + * then doubles (3, 6, 12, ...), and a graph tool call resets the session. The |
| 1423 | + * managed PostToolUse hook matches the graph tools for that reset. The state |
| 1424 | + * lives in the process that runs hook requests, which is the daemon, keyed by |
| 1425 | + * the payload's session_id in a bounded table that evicts the least recently |
| 1426 | + * used session. Codex sends the same event payload, with a turn_id, but has no |
| 1427 | + * reset hook, so its sessions are not tracked. */ |
| 1428 | +enum { |
| 1429 | + HA_DRIFT_SESSIONS = 64, |
| 1430 | + HA_DRIFT_FIRST = 3, |
| 1431 | + HA_DRIFT_SESSION_MAX = 128, |
| 1432 | +}; |
| 1433 | + |
| 1434 | +typedef struct { |
| 1435 | + char session[HA_DRIFT_SESSION_MAX + 1]; |
| 1436 | + unsigned searches; |
| 1437 | + unsigned threshold; |
| 1438 | + uint64_t used; |
| 1439 | +} ha_drift_slot_t; |
| 1440 | + |
| 1441 | +static ha_drift_slot_t g_ha_drift[HA_DRIFT_SESSIONS]; |
| 1442 | +static uint64_t g_ha_drift_clock; |
| 1443 | +static atomic_flag g_ha_drift_spin = ATOMIC_FLAG_INIT; |
| 1444 | + |
| 1445 | +static void ha_drift_lock(void) { |
| 1446 | + while (atomic_flag_test_and_set_explicit(&g_ha_drift_spin, memory_order_acquire)) {} |
| 1447 | +} |
| 1448 | + |
| 1449 | +static void ha_drift_unlock(void) { |
| 1450 | + atomic_flag_clear_explicit(&g_ha_drift_spin, memory_order_release); |
| 1451 | +} |
| 1452 | + |
| 1453 | +static const char *ha_drift_session(yyjson_val *root, ha_lifecycle_dialect_t dialect) { |
| 1454 | + if (dialect != HA_DIALECT_EVENT || !root || yyjson_obj_get(root, "turn_id")) { |
| 1455 | + return NULL; |
| 1456 | + } |
| 1457 | + const char *session = ha_obj_str(root, "session_id"); |
| 1458 | + size_t length = session ? strlen(session) : 0U; |
| 1459 | + if (length == 0U || length > HA_DRIFT_SESSION_MAX) { |
| 1460 | + return NULL; |
| 1461 | + } |
| 1462 | + for (size_t i = 0U; i < length; i++) { |
| 1463 | + unsigned char ch = (unsigned char)session[i]; |
| 1464 | + if (!isalnum(ch) && ch != '-' && ch != '_' && ch != '.') { |
| 1465 | + return NULL; |
| 1466 | + } |
| 1467 | + } |
| 1468 | + return session; |
| 1469 | +} |
| 1470 | + |
| 1471 | +/* Claude names MCP tools mcp__<server>__<tool>, and a plugin's servers |
| 1472 | + * mcp__plugin_<plugin>_<server>__<tool>. */ |
| 1473 | +static bool ha_is_graph_tool(const char *tool) { |
| 1474 | + return tool && strncmp(tool, "mcp__", 5) == 0 && strstr(tool, "codebase-memory-mcp__"); |
| 1475 | +} |
| 1476 | + |
| 1477 | +/* The caller holds the lock. An unknown session takes an empty slot, or else |
| 1478 | + * the least recently used one. */ |
| 1479 | +static ha_drift_slot_t *ha_drift_find(const char *session, bool create) { |
| 1480 | + ha_drift_slot_t *victim = NULL; |
| 1481 | + for (size_t i = 0U; i < HA_DRIFT_SESSIONS; i++) { |
| 1482 | + ha_drift_slot_t *slot = &g_ha_drift[i]; |
| 1483 | + if (slot->session[0] && strcmp(slot->session, session) == 0) { |
| 1484 | + slot->used = ++g_ha_drift_clock; |
| 1485 | + return slot; |
| 1486 | + } |
| 1487 | + if (!victim || (victim->session[0] && (!slot->session[0] || slot->used < victim->used))) { |
| 1488 | + victim = slot; |
| 1489 | + } |
| 1490 | + } |
| 1491 | + if (!create) { |
| 1492 | + return NULL; |
| 1493 | + } |
| 1494 | + snprintf(victim->session, sizeof(victim->session), "%s", session); |
| 1495 | + victim->searches = 0U; |
| 1496 | + victim->threshold = HA_DRIFT_FIRST; |
| 1497 | + victim->used = ++g_ha_drift_clock; |
| 1498 | + return victim; |
| 1499 | +} |
| 1500 | + |
| 1501 | +static void ha_drift_reset(const char *session) { |
| 1502 | + ha_drift_lock(); |
| 1503 | + ha_drift_slot_t *slot = ha_drift_find(session, false); |
| 1504 | + if (slot) { |
| 1505 | + memset(slot, 0, sizeof(*slot)); |
| 1506 | + } |
| 1507 | + ha_drift_unlock(); |
| 1508 | +} |
| 1509 | + |
| 1510 | +/* Counts one search. The reminder is due when the count has reached the |
| 1511 | + * threshold and this search has an augmentation to carry it; delivering it |
| 1512 | + * raises the threshold past the count by doubling. */ |
| 1513 | +static bool ha_drift_note_search(const char *session, bool augmented, unsigned *searches) { |
| 1514 | + ha_drift_lock(); |
| 1515 | + ha_drift_slot_t *slot = ha_drift_find(session, true); |
| 1516 | + if (slot->searches < UINT_MAX) { |
| 1517 | + slot->searches++; |
| 1518 | + } |
| 1519 | + bool due = augmented && slot->searches >= slot->threshold; |
| 1520 | + while (due && slot->threshold <= slot->searches && slot->threshold <= UINT_MAX / 2U) { |
| 1521 | + slot->threshold *= 2U; |
| 1522 | + } |
| 1523 | + *searches = slot->searches; |
| 1524 | + ha_drift_unlock(); |
| 1525 | + return due; |
| 1526 | +} |
| 1527 | + |
| 1528 | +/* Writes the context followed by the drift line into out. Returns false when |
| 1529 | + * they do not fit, and the caller then sends the context alone. The context is |
| 1530 | + * at most 4096 bytes plus the follow-up line, so HA_DRIFT_CONTEXT_CAP fits it. */ |
| 1531 | +enum { HA_DRIFT_CONTEXT_CAP = 5120 }; |
| 1532 | + |
| 1533 | +static bool ha_drift_context(char *out, size_t out_size, const char *context, unsigned searches) { |
| 1534 | + int written = snprintf(out, out_size, |
| 1535 | + "%s\n[codebase-memory] This session has run %u searches since its last " |
| 1536 | + "codebase-memory-mcp tool call. Questions about callers, callees, call " |
| 1537 | + "paths and definitions are one graph call each.", |
| 1538 | + context, searches); |
| 1539 | + return written > 0 && (size_t)written < out_size; |
| 1540 | +} |
| 1541 | + |
1417 | 1542 | static bool ha_tool_event_supported(ha_lifecycle_dialect_t dialect, const char *event, |
1418 | 1543 | const char *tool, bool *coverage) { |
1419 | 1544 | if (coverage) { |
@@ -1752,6 +1877,13 @@ static char *ha_process(cbm_mcp_server_t *srv, const char *input_json, const cha |
1752 | 1877 | ha_tool_call_read(root, forced_event, dialect, &call); |
1753 | 1878 | const char *event = call.event; |
1754 | 1879 | const char *tool = call.tool; |
| 1880 | + const char *session = ha_drift_session(root, dialect); |
| 1881 | + if (session && event && strcmp(event, "PostToolUse") == 0 && ha_is_graph_tool(tool)) { |
| 1882 | + ha_drift_reset(session); |
| 1883 | + ha_tool_call_free(&call); |
| 1884 | + yyjson_doc_free(doc); |
| 1885 | + return NULL; |
| 1886 | + } |
1755 | 1887 | bool coverage = false; |
1756 | 1888 | if (!ha_tool_event_supported(dialect, event, tool, &coverage)) { |
1757 | 1889 | ha_tool_call_free(&call); |
@@ -1809,10 +1941,17 @@ static char *ha_process(cbm_mcp_server_t *srv, const char *input_json, const cha |
1809 | 1941 | } |
1810 | 1942 |
|
1811 | 1943 | char *ctx = ha_resolve_and_query(srv, cwd, token); |
| 1944 | + unsigned searches = 0U; |
| 1945 | + char drift_context[HA_DRIFT_CONTEXT_CAP]; |
| 1946 | + const char *text = ctx; |
| 1947 | + if (session && ha_drift_note_search(session, ctx != NULL, &searches) && |
| 1948 | + ha_drift_context(drift_context, sizeof(drift_context), ctx, searches)) { |
| 1949 | + text = drift_context; |
| 1950 | + } |
1812 | 1951 | char *output = NULL; |
1813 | | - if (ctx) { |
1814 | | - output = dialect == HA_DIALECT_COPILOT ? ha_build_copilot_json(ctx) |
1815 | | - : ha_build_event_json(event, ctx); |
| 1952 | + if (text) { |
| 1953 | + output = dialect == HA_DIALECT_COPILOT ? ha_build_copilot_json(text) |
| 1954 | + : ha_build_event_json(event, text); |
1816 | 1955 | } |
1817 | 1956 | free(ctx); |
1818 | 1957 | yyjson_doc_free(doc); |
|
0 commit comments