length-extrapolating-architecture-data / dev /code_SpecterOps__Blacklight.txt
ag2003's picture
Upload folder using huggingface_hub
5b48487 verified
Raw History Blame Contribute Delete
92.2 kB
# ===== file: blacklight-scout/native/ai_path_scout_bof.c ===== /* * Blacklight Scout - Windows BOF wrapper for AI path scout * * Snapshot BOF for Windows AI tooling artifacts. Reports bounded filesystem * metadata without reading file contents. */ #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #define BLACKLIGHT_SCOUT_WINDOWS 1 #include <stddef.h> #include <stdint.h> #include "../catalog/static_targets_generated.h" #include "ai_path_scout_core.h" #include "beacon.h" #define MAX_BOF_TRIAGE_TARGETS 256 #define BOF_SESSION_SCAN_LIMIT 10000 #define BOF_SESSION_SCAN_MAX_DEPTH 32 #define BOF_SESSION_TOOL_COUNT 5 #define BOF_TOP_SESSIONS_PER_TOOL 3 #define BOF_TOP_SESSION_COUNT (BOF_SESSION_TOOL_COUNT * BOF_TOP_SESSIONS_PER_TOOL) #define BOF_DYNAMIC_DISCOVERY_LIMIT 5000 #define BOF_DYNAMIC_ROOT_LIMIT (BOF_DYNAMIC_DISCOVERY_LIMIT / 4) #define BOF_DYNAMIC_DISCOVERY_MAX_DEPTH 6 #define BOF_CHILD_ENTRY_SCAN_LIMIT 10000 DECLSPEC_IMPORT DWORD WINAPI KERNEL32$ExpandEnvironmentStringsW(LPCWSTR, wchar_t *, DWORD); DECLSPEC_IMPORT DWORD WINAPI KERNEL32$GetFileAttributesW(LPCWSTR); DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$FindFirstFileW(LPCWSTR, LPWIN32_FIND_DATAW); DECLSPEC_IMPORT BOOL WINAPI KERNEL32$FindNextFileW(HANDLE, LPWIN32_FIND_DATAW); DECLSPEC_IMPORT BOOL WINAPI KERNEL32$FindClose(HANDLE); DECLSPEC_IMPORT DWORD WINAPI KERNEL32$GetLastError(void); DECLSPEC_IMPORT BOOL WINAPI KERNEL32$FileTimeToSystemTime(const FILETIME *, SYSTEMTIME *); static int g_auto_select_count = 0; typedef struct { int priority_tier; int auto_select; long long size_bytes; long long child_count; const char *tool; const char *family; const char *type; const char *parser_hint; wchar_t path[BL_MAX_PATH_LEN]; } bof_triage_target_t; static bof_triage_target_t g_bof_triage_targets[MAX_BOF_TRIAGE_TARGETS]; static int g_bof_triage_target_count = 0; static int g_bof_triage_target_overflow = 0; typedef struct { const char *tool; long long size_bytes; FILETIME last_write_time; wchar_t path[BL_MAX_PATH_LEN]; } bof_session_candidate_t; static bof_session_candidate_t g_top_sessions[BOF_TOP_SESSION_COUNT]; static int g_top_session_count = 0; static int g_session_artifact_counts[BOF_SESSION_TOOL_COUNT]; static int g_session_entries_scanned = 0; static int g_session_scan_partial = 0; static int g_session_scan_stop = 0; static int g_dynamic_entries_scanned = 0; static int g_dynamic_root_entries_scanned = 0; static int g_dynamic_scan_partial = 0; static int g_child_entries_scanned = 0; static int g_child_scan_partial = 0; static void inline_memset(void *dest, int value, size_t count) { unsigned char *d = (unsigned char *)dest; while (count--) { *d++ = (unsigned char)value; } } static size_t inline_wcslen(const wchar_t *s) { size_t i = 0; if (!s) { return 0; } while (s[i] != L'\0') { i++; } return i; } static int build_path(const wchar_t *left, const wchar_t *right, wchar_t *out, size_t out_size) { size_t idx = 0; size_t left_idx = 0; size_t right_idx = 0; if (!left || !right || !out || out_size == 0) { return 0; } while (left[left_idx] && idx + 1 < out_size) { out[idx++] = left[left_idx++]; } while (right[right_idx] && idx + 1 < out_size) { out[idx++] = right[right_idx++]; } if (left[left_idx] || right[right_idx]) { out[0] = L'\0'; return 0; } out[idx] = L'\0'; return 1; } static int append_wide_in_place(wchar_t *dst, const wchar_t *suffix, size_t out_size) { size_t idx; size_t i = 0; if (!dst || !suffix || out_size == 0) { return 0; } idx = inline_wcslen(dst); while (suffix[i] && idx + 1 < out_size) { dst[idx++] = suffix[i++]; } if (suffix[i]) { dst[0] = L'\0'; return 0; } dst[idx] = L'\0'; return 1; } static int is_dot_directory(const wchar_t *name) { if (!name || name[0] != L'.') { return 0; } if (name[1] == L'\0') { return 1; } return name[1] == L'.' && name[2] == L'\0'; } static int ascii_lower(int value) { if (value >= 'A' && value <= 'Z') { return value + ('a' - 'A'); } return value; } static wchar_t wide_lower(wchar_t value) { if (value >= L'A' && value <= L'Z') { return value + (L'a' - L'A'); } return value; } static int wide_contains_ascii_n_ci(const wchar_t *text, const char *needle, int needle_len) { int i; int j; int text_len; if (!text || !needle || needle_len <= 0) { return 0; } text_len = (int)inline_wcslen(text); if (needle_len > text_len) { return 0; } for (i = 0; i <= text_len - needle_len; i++) { for (j = 0; j < needle_len; j++) { if ((wchar_t)ascii_lower((unsigned char)needle[j]) != wide_lower(text[i + j])) { break; } } if (j == needle_len) { return 1; } } return 0; } static int ascii_equal_n_ci(const char *left, int left_len, const char *right) { int i = 0; if (!left || left_len <= 0 || !right) { return 0; } while (i < left_len && right[i]) { if (ascii_lower((unsigned char)left[i]) != ascii_lower((unsigned char)right[i])) { return 0; } i++; } return i == left_len && right[i] == '\0'; } static int c_string_len(const char *value) { int len = 0; if (!value) { return 0; } while (value[len]) { len++; } return len; } static long long count_immediate_children(const wchar_t *path) { wchar_t search[BL_MAX_PATH_LEN]; WIN32_FIND_DATAW find_data; HANDLE find_handle = INVALID_HANDLE_VALUE; long long count = 0; if (!path) return -1; inline_memset(search, 0, sizeof(search)); if (!build_path(path, L"\\*", search, BL_MAX_PATH_LEN)) { return -1; } inline_memset(&find_data, 0, sizeof(find_data)); find_handle = KERNEL32$FindFirstFileW(search, &find_data); if (find_handle == INVALID_HANDLE_VALUE) { return -1; } do { if (is_dot_directory(find_data.cFileName)) { continue; } if (g_child_entries_scanned >= BOF_CHILD_ENTRY_SCAN_LIMIT) { g_child_scan_partial = 1; break; } g_child_entries_scanned++; count++; } while (KERNEL32$FindNextFileW(find_handle, &find_data)); KERNEL32$FindClose(find_handle); return count; } static void copy_wide_path(wchar_t *dst, const wchar_t *src, size_t dst_count) { size_t i = 0; if (!dst || dst_count == 0) { return; } if (!src) { dst[0] = L'\0'; return; } while (src[i] && i + 1 < dst_count) { dst[i] = src[i] == L'/' ? L'\\' : src[i]; i++; } dst[i] = L'\0'; } static int target_tool_is(const bof_triage_target_t *target, const char *tool) { int len = 0; if (!target || !target->tool || !tool) { return 0; } while (target->tool[len]) { len++; } return ascii_equal_n_ci(target->tool, len, tool); } static int target_family_is(const bof_triage_target_t *target, const char *family) { int len = 0; if (!target || !target->family || !family) { return 0; } while (target->family[len]) { len++; } return ascii_equal_n_ci(target->family, len, family); } static int target_is_file(const bof_triage_target_t *target) { return target && target->type && target->type[0] == 'f'; } static int target_is_directory(const bof_triage_target_t *target) { return target && target->type && target->type[0] == 'd'; } static int target_is_tier2_review(const bof_triage_target_t *target) { return target && target->priority_tier == 2; } static int target_is_session_context(const bof_triage_target_t *target) { return target && target->priority_tier == 3; } static int wide_ends_with_ascii_ci(const wchar_t *text, const char *suffix) { size_t text_len; size_t suffix_len = 0; size_t i; if (!text || !suffix) return 0; text_len = inline_wcslen(text); while (suffix[suffix_len]) suffix_len++; if (suffix_len > text_len) return 0; for (i = 0; i < suffix_len; i++) { if (wide_lower(text[text_len - suffix_len + i]) != (wchar_t)ascii_lower((unsigned char)suffix[i])) return 0; } return 1; } static int session_file_recognized(const char *tool, const wchar_t *path) { if (ascii_equal_n_ci(tool, c_string_len(tool), "codex")) { return wide_contains_ascii_n_ci(path, "sessions", 8) && wide_ends_with_ascii_ci(path, ".jsonl"); } if (ascii_equal_n_ci(tool, c_string_len(tool), "claude_code")) { return (wide_contains_ascii_n_ci(path, "sessions", 8) || wide_contains_ascii_n_ci(path, "projects", 8)) && wide_ends_with_ascii_ci(path, ".jsonl"); } if (ascii_equal_n_ci(tool, c_string_len(tool), "cursor")) { return wide_ends_with_ascii_ci(path, "store.db") || (wide_contains_ascii_n_ci(path, "agent-transcripts", 17) && wide_ends_with_ascii_ci(path, ".jsonl")); } if (ascii_equal_n_ci(tool, c_string_len(tool), "antigravity_cli")) { return wide_ends_with_ascii_ci(path, "transcript_full.jsonl") || wide_ends_with_ascii_ci(path, "transcript.jsonl") || wide_ends_with_ascii_ci(path, "conversation_summaries.db") || (wide_contains_ascii_n_ci(path, "conversations", 13) && wide_ends_with_ascii_ci(path, ".db")); } if (ascii_equal_n_ci(tool, c_string_len(tool), "grok")) { return wide_ends_with_ascii_ci(path, "updates.jsonl"); } return 0; } static int wide_path_compare_ci(const wchar_t *left, const wchar_t *right) { size_t i = 0; while (left[i] && right[i]) { int difference = (int)wide_lower(left[i]) - (int)wide_lower(right[i]); if (difference) return difference; i++; } return (int)left[i] - (int)right[i]; } static int session_candidate_better(const bof_session_candidate_t *left, const bof_session_candidate_t *right) { int comparison; if (left->last_write_time.dwHighDateTime != right->last_write_time.dwHighDateTime) return left->last_write_time.dwHighDateTime > right->last_write_time.dwHighDateTime; if (left->last_write_time.dwLowDateTime != right->last_write_time.dwLowDateTime) return left->last_write_time.dwLowDateTime > right->last_write_time.dwLowDateTime; if (left->size_bytes != right->size_bytes) return left->size_bytes > right->size_bytes; comparison = ascii_lower((unsigned char)left->tool[0]) - ascii_lower((unsigned char)right->tool[0]); if (comparison == 0) { int i = 0; while (left->tool[i] && right->tool[i]) { comparison = ascii_lower((unsigned char)left->tool[i]) - ascii_lower((unsigned char)right->tool[i]); if (comparison) break; i++; } if (!comparison) comparison = (unsigned char)left->tool[i] - (unsigned char)right->tool[i]; } if (comparison != 0) return comparison < 0; return wide_path_compare_ci(left->path, right->path) < 0; } static int session_tool_slot(const char *tool) { int length = c_string_len(tool); if (ascii_equal_n_ci(tool, length, "codex")) return 0; if (ascii_equal_n_ci(tool, length, "claude_code")) return 1; if (ascii_equal_n_ci(tool, length, "cursor")) return 2; if (ascii_equal_n_ci(tool, length, "antigravity_cli")) return 3; if (ascii_equal_n_ci(tool, length, "grok")) return 4; return -1; } static void consider_session_candidate(const char *tool, const wchar_t *path, const WIN32_FIND_DATAW *data) { bof_session_candidate_t candidate; int tool_slot; int base; int insert = -1; int i; if (!tool || !path || !data || !session_file_recognized(tool, path)) return; tool_slot = session_tool_slot(tool); if (tool_slot < 0) return; g_session_artifact_counts[tool_slot]++; base = tool_slot * BOF_TOP_SESSIONS_PER_TOOL; inline_memset(&candidate, 0, sizeof(candidate)); candidate.tool = tool; candidate.size_bytes = ((long long)data->nFileSizeHigh << 32) | data->nFileSizeLow; candidate.last_write_time = data->ftLastWriteTime; copy_wide_path(candidate.path, path, BL_MAX_PATH_LEN); for (i = 0; i < BOF_TOP_SESSIONS_PER_TOOL; i++) { if (g_top_sessions[base + i].tool && wide_path_compare_ci(g_top_sessions[base + i].path, path) == 0) return; if (insert < 0 && (!g_top_sessions[base + i].tool || session_candidate_better(&candidate, &g_top_sessions[base + i]))) insert = i; } if (insert < 0) return; if (!g_top_sessions[base + BOF_TOP_SESSIONS_PER_TOOL - 1].tool) g_top_session_count++; for (i = BOF_TOP_SESSIONS_PER_TOOL - 1; i > insert; i--) g_top_sessions[base + i] = g_top_sessions[base + i - 1]; g_top_sessions[base + insert] = candidate; } static void walk_session_files(const char *tool, const wchar_t *directory, int depth) { wchar_t search[BL_MAX_PATH_LEN]; wchar_t child[BL_MAX_PATH_LEN]; WIN32_FIND_DATAW find_data; HANDLE find_handle; if (g_session_scan_stop) return; if (depth > BOF_SESSION_SCAN_MAX_DEPTH) { g_session_scan_partial = 1; return; } inline_memset(search, 0, sizeof(search)); if (!build_path(directory, L"\\*", search, BL_MAX_PATH_LEN)) { g_session_scan_partial = 1; return; } inline_memset(&find_data, 0, sizeof(find_data)); find_handle = KERNEL32$FindFirstFileW(search, &find_data); if (find_handle == INVALID_HANDLE_VALUE) { if (KERNEL32$GetLastError() != ERROR_FILE_NOT_FOUND) g_session_scan_partial = 1; return; } do { if (is_dot_directory(find_data.cFileName)) continue; if (g_session_entries_scanned >= BOF_SESSION_SCAN_LIMIT) { g_session_scan_partial = 1; g_session_scan_stop = 1; break; } g_session_entries_scanned++; inline_memset(child, 0, sizeof(child)); if (!build_path(directory, L"\\", child, BL_MAX_PATH_LEN) || !append_wide_in_place(child, find_data.cFileName, BL_MAX_PATH_LEN)) { g_session_scan_partial = 1; continue; } if (find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) continue; if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) walk_session_files(tool, child, depth + 1); else consider_session_candidate(tool, child, &find_data); } while (!g_session_scan_stop && KERNEL32$FindNextFileW(find_handle, &find_data)); if (!g_session_scan_stop && KERNEL32$GetLastError() != ERROR_NO_MORE_FILES) g_session_scan_partial = 1; KERNEL32$FindClose(find_handle); } static int target_can_contain_sessions(const bof_triage_target_t *target) { if (!target_is_directory(target)) return 0; if (target_family_is(target, "sessions") || target_family_is(target, "history")) return 1; return target_family_is(target, "workspace") && (target_tool_is(target, "claude_code") || target_tool_is(target, "cursor")); } static void scan_session_candidates(void) { int i; inline_memset(g_top_sessions, 0, sizeof(g_top_sessions)); inline_memset(g_session_artifact_counts, 0, sizeof(g_session_artifact_counts)); g_top_session_count = 0; g_session_entries_scanned = 0; g_session_scan_partial = 0; g_session_scan_stop = 0; for (i = 0; i < g_bof_triage_target_count && !g_session_scan_stop; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; if (target_can_contain_sessions(target)) { int j; int seen = 0; for (j = 0; j < i; j++) { if (target_can_contain_sessions(&g_bof_triage_targets[j]) && target_tool_is(&g_bof_triage_targets[j], target->tool) && wide_path_compare_ci(g_bof_triage_targets[j].path, target->path) == 0) { seen = 1; break; } } if (!seen) walk_session_files(target->tool, target->path, 0); } else if (target_is_file(target) && session_file_recognized(target->tool, target->path)) { WIN32_FIND_DATAW find_data; HANDLE find_handle; inline_memset(&find_data, 0, sizeof(find_data)); find_handle = KERNEL32$FindFirstFileW(target->path, &find_data); if (find_handle != INVALID_HANDLE_VALUE) { if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) consider_session_candidate(target->tool, target->path, &find_data); KERNEL32$FindClose(find_handle); } else { g_session_scan_partial = 1; } } } } static void record_bof_triage_target( const char *tool, const char *family, const char *type, const wchar_t *path, int priority_tier, const char *parser_hint, long long size_bytes, long long child_count, int auto_select ) { bof_triage_target_t *target; if (g_bof_triage_target_count >= MAX_BOF_TRIAGE_TARGETS) { g_bof_triage_target_overflow = 1; return; } target = &g_bof_triage_targets[g_bof_triage_target_count++]; target->priority_tier = priority_tier; target->auto_select = auto_select; target->size_bytes = size_bytes; target->child_count = child_count; target->tool = tool; target->family = family; target->type = type; target->parser_hint = parser_hint; copy_wide_path(target->path, path, BL_MAX_PATH_LEN); } static int wide_path_token_is_boundary(wchar_t value) { return value == 0 || value == L'/' || value == L'\\' || value == L'.' || value == L'_' || value == L'-'; } static int wide_contains_ascii_token_ci(const wchar_t *text, const char *needle, int needle_len) { int i; int j; int text_len; if (!text || !needle || needle_len <= 0) { return 0; } text_len = (int)inline_wcslen(text); if (needle_len > text_len) { return 0; } for (i = 0; i <= text_len - needle_len; i++) { if (i > 0 && !wide_path_token_is_boundary(text[i - 1])) { continue; } for (j = 0; j < needle_len; j++) { if ((wchar_t)ascii_lower((unsigned char)needle[j]) != wide_lower(text[i + j])) { break; } } if (j == needle_len && wide_path_token_is_boundary(text[i + needle_len])) { return 1; } } return 0; } static void print_bof_collection_first(void) { int i; int printed = 0; BeaconPrintf(CALLBACK_OUTPUT, "[i] Collect first: credential/session metadata and auth-adjacent configs\n"); for (i = 0; i < g_bof_triage_target_count; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; if (!target->auto_select) { continue; } if (target->size_bytes < 1024) { BeaconPrintf(CALLBACK_OUTPUT, "[i] [%d] %s %s %s %lld B\n", target->priority_tier, target->tool, target->family, target->type, target->size_bytes); } else if (target->size_bytes < 1024LL * 1024LL) { BeaconPrintf(CALLBACK_OUTPUT, "[i] [%d] %s %s %s %lld.%lld KB\n", target->priority_tier, target->tool, target->family, target->type, target->size_bytes / 1024, ((target->size_bytes % 1024) * 10) / 1024); } else { BeaconPrintf(CALLBACK_OUTPUT, "[i] [%d] %s %s %s %lld.%lld MB\n", target->priority_tier, target->tool, target->family, target->type, target->size_bytes / (1024LL * 1024LL), ((target->size_bytes % (1024LL * 1024LL)) * 10) / (1024LL * 1024LL)); } BeaconPrintf(CALLBACK_OUTPUT, "[i] %S\n", target->path); printed++; } if (!printed) { BeaconPrintf(CALLBACK_OUTPUT, "[i] none\n"); } for (i = 0, printed = 0; i < g_bof_triage_target_count; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; if (target->priority_tier != 1 || target->auto_select) continue; if (!printed) BeaconPrintf(CALLBACK_OUTPUT, "[i] Auth containers requiring review\n"); BeaconPrintf(CALLBACK_OUTPUT, "[i] [1] %s %s - auth_container_review\n[i] %S\n", target->tool, target->family, target->path); printed++; } BeaconPrintf(CALLBACK_OUTPUT, "[i]\n"); } static void print_bof_tier2_group(const char *tool) { int i; int count = 0; int files = 0; int dirs = 0; int has_config = 0; int has_rules = 0; int has_sandbox = 0; int has_plugins = 0; int has_other = 0; for (i = 0; i < g_bof_triage_target_count; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; if (!target_tool_is(target, tool) || !target_is_tier2_review(target)) { continue; } count++; files += target_is_file(target); dirs += target_is_directory(target); has_config |= target_family_is(target, "config") || target_family_is(target, "mcp") || target_family_is(target, "mcp_config") || target_family_is(target, "project_config") || target_family_is(target, "project_mcp_config") || target_family_is(target, "permissions"); has_rules |= target_family_is(target, "rules"); has_sandbox |= target_family_is(target, "sandbox") || wide_contains_ascii_n_ci(target->path, ".sandbox-secrets", 16); has_plugins |= target_family_is(target, "plugins") || target_family_is(target, "extensions") || target_family_is(target, "skills"); has_other |= !(target_family_is(target, "config") || target_family_is(target, "mcp") || target_family_is(target, "mcp_config") || target_family_is(target, "project_config") || target_family_is(target, "project_mcp_config") || target_family_is(target, "permissions") || target_family_is(target, "rules") || target_family_is(target, "sandbox") || target_family_is(target, "plugins") || target_family_is(target, "extensions") || target_family_is(target, "skills") || wide_contains_ascii_n_ci(target->path, ".sandbox-secrets", 16)); } if (count <= 0) { return; } BeaconPrintf(CALLBACK_OUTPUT, "[i] [2] %s %s%s%s%s%s %s x%d\n", tool, has_config ? "config" : "", has_rules ? "/rules" : "", has_plugins ? "/plugins" : "", has_sandbox ? "/sandbox" : "", has_other ? "/review" : "", dirs == count ? "dirs" : (files == count ? "files" : "artifacts"), count ); for (i = 0; i < g_bof_triage_target_count; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; if (target_tool_is(target, tool) && target_is_tier2_review(target)) { BeaconPrintf(CALLBACK_OUTPUT, "[i] %S\n", target->path); } } } static void print_bof_review_next(void) { int i; BeaconPrintf(CALLBACK_OUTPUT, "[i] Review next: config, rules, plugins, sandbox state\n"); for (i = 0; i < g_bof_triage_target_count; i++) { int seen = 0; int j; if (!target_is_tier2_review(&g_bof_triage_targets[i])) { continue; } for (j = 0; j < i; j++) { if (target_is_tier2_review(&g_bof_triage_targets[j]) && target_tool_is(&g_bof_triage_targets[j], g_bof_triage_targets[i].tool)) { seen = 1; break; } } if (!seen) { print_bof_tier2_group(g_bof_triage_targets[i].tool); } } BeaconPrintf(CALLBACK_OUTPUT, "[i]\n"); } static void print_bof_session_context(void) { int i; int j; int printed = 0; int group_count = 0; typedef struct { const char *tool; int count; long long largest; int printed; } session_group_t; session_group_t groups[32]; inline_memset(groups, 0, sizeof(groups)); BeaconPrintf(CALLBACK_OUTPUT, "[i] Session activity (collect selectively)\n"); for (i = 0; i < g_bof_triage_target_count; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; int index = -1; if (!target_is_session_context(target)) { continue; } for (j = 0; j < group_count; j++) { if (target_tool_is(target, groups[j].tool)) { index = j; break; } } if (index < 0) { if (group_count >= 32) { continue; } index = group_count++; groups[index].tool = target->tool; } groups[index].count++; if (target->size_bytes > groups[index].largest) { groups[index].largest = target->size_bytes; } } while (printed < group_count) { int best = -1; for (i = 0; i < group_count; i++) { if (groups[i].printed) { continue; } if (best < 0 || groups[i].count > groups[best].count || (groups[i].count == groups[best].count && groups[i].largest > groups[best].largest)) { best = i; } } if (best < 0) { break; } { int tool_slot = session_tool_slot(groups[best].tool); int session_artifacts = tool_slot >= 0 ? g_session_artifact_counts[tool_slot] : 0; BeaconPrintf(CALLBACK_OUTPUT, "[i] [3] %s history/session locations x%d | %d session artifact%s%s\n", groups[best].tool, groups[best].count, session_artifacts, session_artifacts == 1 ? "" : "s", g_session_scan_partial ? " (partial scan)" : ""); } for (i = 0; i < g_bof_triage_target_count; i++) { const bof_triage_target_t *target = &g_bof_triage_targets[i]; if (!target_is_session_context(target) || !target_tool_is(target, groups[best].tool)) { continue; } BeaconPrintf(CALLBACK_OUTPUT, "[i] %S\n", target->path); } groups[best].printed = 1; printed++; } if (!printed) { BeaconPrintf(CALLBACK_OUTPUT, "[i] none\n"); } BeaconPrintf(CALLBACK_OUTPUT, "[i]\n"); } static void print_bof_top_session_files(void) { int i; int rank = 0; BeaconPrintf(CALLBACK_OUTPUT, "[i] PRIORITIZED SESSION ARTIFACTS (newest first)%s\n", g_session_scan_partial ? " (partial scan)" : ""); if (g_top_session_count == 0) BeaconPrintf(CALLBACK_OUTPUT, "[i] none\n"); for (i = 0; i < BOF_TOP_SESSION_COUNT; i++) { const bof_session_candidate_t *target = &g_top_sessions[i]; SYSTEMTIME utc; char size[32]; if (!target->tool) continue; rank = (i % BOF_TOP_SESSIONS_PER_TOOL) + 1; if (target->size_bytes < 1024) BeaconPrintf(CALLBACK_OUTPUT, "[+] [%d] %s | %lld B", rank, target->tool, target->size_bytes); else if (target->size_bytes < 1024 * 1024) BeaconPrintf(CALLBACK_OUTPUT, "[+] [%d] %s | %lld.%lld KB", rank, target->tool, target->size_bytes / 1024, ((target->size_bytes % 1024) * 10) / 1024); else BeaconPrintf(CALLBACK_OUTPUT, "[+] [%d] %s | %lld.%lld MB", rank, target->tool, target->size_bytes / (1024 * 1024), ((target->size_bytes % (1024 * 1024)) * 10) / (1024 * 1024)); inline_memset(&utc, 0, sizeof(utc)); if (KERNEL32$FileTimeToSystemTime(&target->last_write_time, &utc)) BeaconPrintf(CALLBACK_OUTPUT, " | modified %04d-%02d-%02d", utc.wYear, utc.wMonth, utc.wDay); BeaconPrintf(CALLBACK_OUTPUT, "\n %S\n", target->path); } BeaconPrintf(CALLBACK_OUTPUT, "[i]\n"); } static void print_bof_low_priority(void) { int tier; int printed = 0; BeaconPrintf(CALLBACK_OUTPUT, "[i] Other inventory\n"); for (tier = 4; tier <= 5; tier++) { int i; for (i = 0; i < g_bof_triage_target_count; i++) { int j; int seen = 0; int count = 0; int dirs = 0; int files = 0; const char *family = g_bof_triage_targets[i].family; if (g_bof_triage_targets[i].priority_tier != tier) { continue; } for (j = 0; j < i; j++) { if (g_bof_triage_targets[j].priority_tier == tier && target_family_is(&g_bof_triage_targets[j], family)) { seen = 1; break; } } if (seen) { continue; } for (j = 0; j < g_bof_triage_target_count; j++) { const bof_triage_target_t *target = &g_bof_triage_targets[j]; if (target->priority_tier == tier && target_family_is(target, family)) { count++; dirs += target_is_directory(target); files += target_is_file(target); } } BeaconPrintf(CALLBACK_OUTPUT, "[i] [%d] %s %s x%d\n", tier, family, dirs == count ? "dirs" : (files == count ? "files" : "artifacts"), count); for (j = 0; j < g_bof_triage_target_count; j++) { const bof_triage_target_t *target = &g_bof_triage_targets[j]; if (target->priority_tier == tier && target_family_is(target, family)) { BeaconPrintf(CALLBACK_OUTPUT, "[i] %S\n", target->path); } } printed++; } } if (!printed) { BeaconPrintf(CALLBACK_OUTPUT, "[i] none\n"); } BeaconPrintf(CALLBACK_OUTPUT, "[i]\n"); } static void print_bof_human_triage_summary(const bl_scan_results_t *results) { int i, tools = 0, auth = 0, sessions = 0, session_artifacts = 0; BeaconPrintf(CALLBACK_OUTPUT, "[i] Blacklight endpoint assessment\n[i]\n[i] ASSESSMENT SUMMARY\n"); for (i = 0; i < g_bof_triage_target_count; i++) { int j, seen = 0; if (target_family_is(&g_bof_triage_targets[i], "auth") && target_is_file(&g_bof_triage_targets[i])) auth++; if (target_is_session_context(&g_bof_triage_targets[i])) sessions++; for (j = 0; j < i; j++) if (target_tool_is(&g_bof_triage_targets[j], g_bof_triage_targets[i].tool)) { seen = 1; break; } if (!seen) tools++; } BeaconPrintf(CALLBACK_OUTPUT, "[i] Tools detected: %d\n", tools); if (auth) BeaconPrintf(CALLBACK_OUTPUT, "[+] Credential stores: %d file%s\n", auth, auth == 1 ? "" : "s"); if (sessions) BeaconPrintf(CALLBACK_OUTPUT, "[i] Session locations: %d\n", sessions); for (i = 0; i < BOF_SESSION_TOOL_COUNT; i++) session_artifacts += g_session_artifact_counts[i]; if (sessions || g_session_scan_partial) BeaconPrintf(CALLBACK_OUTPUT, "[i] Session artifacts: %d%s\n", session_artifacts, g_session_scan_partial ? " (partial scan)" : ""); BeaconPrintf(CALLBACK_OUTPUT, "%s Discovery status: %s\n[i]\n", (g_dynamic_scan_partial || g_bof_triage_target_overflow || g_session_scan_partial) ? "[!]" : "[i]", (g_dynamic_scan_partial || g_bof_triage_target_overflow || g_session_scan_partial) ? "PARTIAL" : "COMPLETE"); print_bof_collection_first(); print_bof_review_next(); print_bof_top_session_files(); print_bof_session_context(); print_bof_low_priority(); if (g_bof_triage_target_overflow) BeaconPrintf(CALLBACK_OUTPUT, "[!] Result storage cap reached at %d artifacts. Results are incomplete.\n", MAX_BOF_TRIAGE_TARGETS); if (g_dynamic_scan_partial) BeaconPrintf(CALLBACK_OUTPUT, "[!] Discovery cap reached after %d candidate entries (per-root cap %d; run cap %d). Results are incomplete.\n", g_dynamic_entries_scanned, BOF_DYNAMIC_ROOT_LIMIT, BOF_DYNAMIC_DISCOVERY_LIMIT); (void)results; } static void bof_emit_triage(const char *tool, const char *family, const char *type, const void *path) { const wchar_t *wide_path = (const wchar_t *)path; WIN32_FIND_DATAW find_data; HANDLE find_handle; long long size_bytes = 0; long long child_count = 0; int priority_tier; int sandbox_secret_path; int auth_adjacent_config_path; inline_memset(&find_data, 0, sizeof(find_data)); find_handle = KERNEL32$FindFirstFileW(wide_path, &find_data); if (find_handle != INVALID_HANDLE_VALUE) { if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { child_count = count_immediate_children(wide_path); if (child_count < 0) { child_count = 0; } } else { size_bytes = ((long long)find_data.nFileSizeHigh << 32) | find_data.nFileSizeLow; } KERNEL32$FindClose(find_handle); } sandbox_secret_path = wide_contains_ascii_n_ci(wide_path, ".sandbox-secrets", 16); auth_adjacent_config_path = wide_contains_ascii_n_ci(wide_path, ".claude.json", 12) && !wide_contains_ascii_n_ci(wide_path, ".claude/.claude.json", 20) && !wide_contains_ascii_n_ci(wide_path, ".claude\\.claude.json", 20); priority_tier = bl_triage_priority_tier( family, (!sandbox_secret_path && !wide_contains_ascii_n_ci(wide_path, "mcp-needs-auth-cache", 21) && ( wide_contains_ascii_token_ci(wide_path, "auth", 4) || wide_contains_ascii_token_ci(wide_path, "credential", 10) || wide_contains_ascii_token_ci(wide_path, "token", 5) || wide_contains_ascii_token_ci(wide_path, "secret", 6) )) || auth_adjacent_config_path ); if (sandbox_secret_path && priority_tier == 1) { priority_tier = 2; } record_bof_triage_target( tool, family, type, wide_path, priority_tier, bl_triage_parser_hint(family, wide_ends_with_ascii_ci(wide_path, ".db") || wide_ends_with_ascii_ci(wide_path, ".sqlite") || wide_ends_with_ascii_ci(wide_path, ".sqlite3")), size_bytes, child_count, priority_tier == 1 && type && type[0] == 'f' ); if (priority_tier == 1 && type && type[0] == 'f') { g_auto_select_count++; } } static int bof_expand_path(const void *pattern, void *expanded, size_t expanded_size) { const wchar_t *wide_pattern = (const wchar_t *)pattern; wchar_t *wide_expanded = (wchar_t *)expanded; DWORD needed; if (!wide_pattern || !wide_expanded || expanded_size == 0) { return 0; } inline_memset(wide_expanded, 0, expanded_size * sizeof(wchar_t)); needed = KERNEL32$ExpandEnvironmentStringsW(wide_pattern, wide_expanded, (DWORD)expanded_size); if (needed == 0 || needed > expanded_size) { wide_expanded[0] = L'\0'; return 0; } return 1; } static int bof_report_path_if_exists( const char *tool, const char *family, const void *path, bl_scan_results_t *results, const bl_scan_filters_t *filters, bl_emit_fn emit ) { const wchar_t *wide_path = (const wchar_t *)path; DWORD attributes; const char *type; if (!tool || !family || !wide_path || !results || !emit) { return 0; } attributes = KERNEL32$GetFileAttributesW(wide_path); if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { return 0; } if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { type = "directory"; results->directory_count++; } else { type = "file"; results->file_count++; } results->hit_count++; emit(tool, family, type, wide_path); return 1; } static int bof_path_recorded(const wchar_t *path) { int i; for (i = 0; i < g_bof_triage_target_count; i++) { if (wide_path_compare_ci(g_bof_triage_targets[i].path, path) == 0) return 1; } return 0; } static void bof_report_dynamic_file( const char *tool, const char *family, const wchar_t *path, bl_scan_results_t *results, const bl_scan_filters_t *filters ) { if (!bof_path_recorded(path)) { bof_report_path_if_exists(tool, family, path, results, filters, bof_emit_triage); } } static void bof_scan_dynamic_tree( const char *tool, const wchar_t *directory, int depth, int include_plan_metadata, bl_scan_results_t *results, const bl_scan_filters_t *filters ) { wchar_t search[BL_MAX_PATH_LEN]; wchar_t child[BL_MAX_PATH_LEN]; WIN32_FIND_DATAW find_data; HANDLE find_handle; if (depth < 0 || g_dynamic_entries_scanned >= BOF_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BOF_DYNAMIC_ROOT_LIMIT) { if (g_dynamic_entries_scanned >= BOF_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BOF_DYNAMIC_ROOT_LIMIT) g_dynamic_scan_partial = 1; return; } inline_memset(search, 0, sizeof(search)); if (!build_path(directory, L"\\*", search, BL_MAX_PATH_LEN)) return; inline_memset(&find_data, 0, sizeof(find_data)); find_handle = KERNEL32$FindFirstFileW(search, &find_data); if (find_handle == INVALID_HANDLE_VALUE) return; do { if (is_dot_directory(find_data.cFileName)) continue; if (g_dynamic_entries_scanned >= BOF_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BOF_DYNAMIC_ROOT_LIMIT) { g_dynamic_scan_partial = 1; break; } g_dynamic_entries_scanned++; g_dynamic_root_entries_scanned++; inline_memset(child, 0, sizeof(child)); if (!build_path(directory, L"\\", child, BL_MAX_PATH_LEN) || !append_wide_in_place(child, find_data.cFileName, BL_MAX_PATH_LEN)) continue; if (find_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) continue; if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (depth > 0) bof_scan_dynamic_tree(tool, child, depth - 1, include_plan_metadata, results, filters); } else if (wide_ends_with_ascii_ci(child, "\\.mcp.json")) { bof_report_dynamic_file(tool, "mcp", child, results, filters); } else if (ascii_equal_n_ci(tool, c_string_len(tool), "claude_code") && wide_ends_with_ascii_ci(child, "\\sessions-index.json")) { bof_report_dynamic_file(tool, "sessions", child, results, filters); } else if (include_plan_metadata && wide_ends_with_ascii_ci(find_data.cFileName, ".plan.md")) { bof_report_dynamic_file(tool, "plans", child, results, filters); } } while (KERNEL32$FindNextFileW(find_handle, &find_data)); KERNEL32$FindClose(find_handle); } static void bof_scan_dynamic_targets(bl_scan_results_t *results, const bl_scan_filters_t *filters) { wchar_t profile[BL_MAX_PATH_LEN]; wchar_t root[BL_MAX_PATH_LEN]; wchar_t pattern[BL_MAX_PATH_LEN]; wchar_t path[BL_MAX_PATH_LEN]; WIN32_FIND_DATAW find_data; HANDLE find_handle; DWORD needed; inline_memset(profile, 0, sizeof(profile)); needed = KERNEL32$ExpandEnvironmentStringsW(L"%USERPROFILE%", profile, BL_MAX_PATH_LEN); if (needed == 0 || needed > BL_MAX_PATH_LEN) return; inline_memset(root, 0, sizeof(root)); g_dynamic_root_entries_scanned = 0; if (build_path(profile, L"\\.codex\\rules", root, BL_MAX_PATH_LEN) && build_path(root, L"\\*.rules", pattern, BL_MAX_PATH_LEN)) { inline_memset(&find_data, 0, sizeof(find_data)); find_handle = KERNEL32$FindFirstFileW(pattern, &find_data); if (find_handle != INVALID_HANDLE_VALUE) { do { if (g_dynamic_entries_scanned >= BOF_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BOF_DYNAMIC_ROOT_LIMIT) { g_dynamic_scan_partial = 1; break; } g_dynamic_entries_scanned++; g_dynamic_root_entries_scanned++; if ((find_data.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) != 0) continue; inline_memset(path, 0, sizeof(path)); if (build_path(root, L"\\", path, BL_MAX_PATH_LEN) && append_wide_in_place(path, find_data.cFileName, BL_MAX_PATH_LEN)) { bof_report_dynamic_file("codex", "rules", path, results, filters); } } while (KERNEL32$FindNextFileW(find_handle, &find_data)); KERNEL32$FindClose(find_handle); } } inline_memset(root, 0, sizeof(root)); g_dynamic_root_entries_scanned = 0; if (build_path(profile, L"\\.claude\\projects", root, BL_MAX_PATH_LEN)) bof_scan_dynamic_tree("claude_code", root, BOF_DYNAMIC_DISCOVERY_MAX_DEPTH, 0, results, filters); inline_memset(root, 0, sizeof(root)); g_dynamic_root_entries_scanned = 0; if (build_path(profile, L"\\.cursor\\projects", root, BL_MAX_PATH_LEN)) bof_scan_dynamic_tree("cursor", root, BOF_DYNAMIC_DISCOVERY_MAX_DEPTH, 0, results, filters); inline_memset(root, 0, sizeof(root)); g_dynamic_root_entries_scanned = 0; if (build_path(profile, L"\\.cursor\\plans", root, BL_MAX_PATH_LEN)) bof_scan_dynamic_tree("cursor", root, 0, 1, results, filters); } void go(char *args, unsigned long alen) { bl_scan_results_t results; bl_scan_filters_t filters; (void)args; (void)alen; inline_memset(&results, 0, sizeof(results)); inline_memset(&filters, 0, sizeof(filters)); g_bof_triage_target_count = 0; g_bof_triage_target_overflow = 0; filters.max_depth = 1; filters.triage = 1; g_auto_select_count = 0; g_dynamic_entries_scanned = 0; g_dynamic_root_entries_scanned = 0; g_dynamic_scan_partial = 0; g_child_entries_scanned = 0; g_child_scan_partial = 0; bl_scan_static_targets( BL_STATIC_TARGETS, BL_STATIC_TARGETS_COUNT, &results, &filters, bof_expand_path, bof_report_path_if_exists, bof_emit_triage ); bof_scan_dynamic_targets(&results, &filters); scan_session_candidates(); print_bof_human_triage_summary(&results); } # ===== file: blacklight-scout/native/ai_path_scout_core.c ===== /* * Blacklight Scout - shared AI path scout core */ #include "ai_path_scout_core.h" static int bl_ascii_lower(int value) { if (value >= 'A' && value <= 'Z') { return value + ('a' - 'A'); } return value; } static int bl_ascii_equal_n(const char *left, int left_len, const char *right) { int i = 0; if (!left || left_len < 0 || !right) { return 0; } while (i < left_len && right[i]) { if (bl_ascii_lower((unsigned char)left[i]) != bl_ascii_lower((unsigned char)right[i])) { return 0; } i++; } return i == left_len && right[i] == '\0'; } static int bl_csv_contains_token(const char *csv, int csv_len, const char *value) { int start = 0; int end; if (!csv || csv_len <= 0 || !value) { return 1; } while (start < csv_len) { while (start < csv_len && (csv[start] == ',' || csv[start] == ' ' || csv[start] == '\t')) { start++; } end = start; while (end < csv_len && csv[end] != ',') { end++; } while (end > start && (csv[end - 1] == ' ' || csv[end - 1] == '\t')) { end--; } if (end > start && bl_ascii_equal_n(csv + start, end - start, value)) { return 1; } start = end + 1; } return 0; } static int bl_ascii_equals(const char *left, const char *right) { int len = 0; if (!left || !right) { return 0; } while (left[len]) { len++; } return bl_ascii_equal_n(left, len, right); } int bl_loader_filter_tool(const char *value, int value_len, const char **canonical) { if (canonical) { *canonical = 0; } if (!value || value_len <= 0) { return 0; } if (bl_ascii_equal_n(value, value_len, "codex")) { if (canonical) { *canonical = "codex"; } return 1; } if (bl_ascii_equal_n(value, value_len, "claude") || bl_ascii_equal_n(value, value_len, "claude_code") || bl_ascii_equal_n(value, value_len, "claude-code")) { if (canonical) { *canonical = "claude_code"; } return 1; } if (bl_ascii_equal_n(value, value_len, "cursor")) { if (canonical) { *canonical = "cursor"; } return 1; } if (bl_ascii_equal_n(value, value_len, "grok") || bl_ascii_equal_n(value, value_len, "grok-cli")) { if (canonical) { *canonical = "grok"; } return 1; } if (bl_ascii_equal_n(value, value_len, "antigravity") || bl_ascii_equal_n(value, value_len, "antigravity_cli") || bl_ascii_equal_n(value, value_len, "antigravity-cli") || bl_ascii_equal_n(value, value_len, "gemini-antigravity-cli")) { if (canonical) { *canonical = "antigravity_cli"; } return 1; } return 0; } static int bl_tool_filter_matches(const char *csv, int csv_len, const char *tool) { if (!csv || csv_len <= 0 || !tool) { return 1; } if (bl_csv_contains_token(csv, csv_len, tool)) { return 1; } if (bl_ascii_equals(tool, "claude_code") && (bl_csv_contains_token(csv, csv_len, "claude") || bl_csv_contains_token(csv, csv_len, "claude-code"))) { return 1; } if (bl_ascii_equals(tool, "antigravity_cli") && (bl_csv_contains_token(csv, csv_len, "antigravity") || bl_csv_contains_token(csv, csv_len, "antigravity-cli") || bl_csv_contains_token(csv, csv_len, "gemini-antigravity-cli"))) { return 1; } if (bl_ascii_equals(tool, "grok") && bl_csv_contains_token(csv, csv_len, "grok-cli")) { return 1; } return 0; } int bl_target_allowed(const char *tool, const bl_scan_filters_t *filters) { if (!filters) { return 1; } if (!bl_tool_filter_matches(filters->tools, filters->tools_len, tool)) { return 0; } if (filters->exclude_tools_len > 0 && bl_tool_filter_matches(filters->exclude_tools, filters->exclude_tools_len, tool)) { return 0; } return 1; } const char *bl_parser_hint_for_family(const char *family) { if (!family) { return "unknown"; } if (bl_ascii_equals(family, "auth") || bl_ascii_equals(family, "credential_metadata")) { return "credential-session-metadata"; } if (bl_ascii_equals(family, "sessions") || bl_ascii_equals(family, "history") || bl_ascii_equals(family, "transcripts")) { return "session-detail"; } if (bl_ascii_equals(family, "config") || bl_ascii_equals(family, "mcp") || bl_ascii_equals(family, "mcp_config") || bl_ascii_equals(family, "project_config") || bl_ascii_equals(family, "project_mcp_config") || bl_ascii_equals(family, "rules") || bl_ascii_equals(family, "sandbox") || bl_ascii_equals(family, "permissions") || bl_ascii_equals(family, "plugins") || bl_ascii_equals(family, "extensions") || bl_ascii_equals(family, "skills")) { return "config-rules"; } if (bl_ascii_equals(family, "state") || bl_ascii_equals(family, "telemetry")) { return "sqlite-metadata"; } if (bl_ascii_equals(family, "workspace")) { return "inventory-only"; } return "unknown"; } int bl_triage_priority_tier(const char *family, int secret_like_path) { if (bl_ascii_equals(family, "auth") || bl_ascii_equals(family, "credential_metadata") || secret_like_path) { return 1; } if (bl_ascii_equals(family, "config") || bl_ascii_equals(family, "mcp") || bl_ascii_equals(family, "mcp_config") || bl_ascii_equals(family, "project_config") || bl_ascii_equals(family, "project_mcp_config") || bl_ascii_equals(family, "rules") || bl_ascii_equals(family, "sandbox") || bl_ascii_equals(family, "permissions") || bl_ascii_equals(family, "plugins") || bl_ascii_equals(family, "extensions") || bl_ascii_equals(family, "skills")) { return 2; } if (bl_ascii_equals(family, "sessions") || bl_ascii_equals(family, "history") || bl_ascii_equals(family, "transcripts")) { return 3; } if (bl_ascii_equals(family, "workspace") || bl_ascii_equals(family, "state") || bl_ascii_equals(family, "telemetry") || bl_ascii_equals(family, "plans") || bl_ascii_equals(family, "worktrees") || bl_ascii_equals(family, "snapshots")) { return 4; } return 5; } const char *bl_triage_parser_hint(const char *family, int sqlite_path) { if (sqlite_path || bl_ascii_equals(family, "state") || bl_ascii_equals(family, "telemetry")) { return "sqlite-metadata"; } return bl_parser_hint_for_family(family); } void bl_scan_static_targets( const bl_path_target_t *targets, size_t target_count, bl_scan_results_t *results, const bl_scan_filters_t *filters, int (*expand_path)(const void *pattern, void *expanded, size_t expanded_size), int (*report_path_if_exists)( const char *tool, const char *family, const void *path, bl_scan_results_t *results, const bl_scan_filters_t *filters, bl_emit_fn emit ), bl_emit_fn emit ) { size_t i; unsigned char expanded[BL_MAX_PATH_LEN * sizeof(unsigned short)]; if (!targets || !results || !expand_path || !report_path_if_exists || !emit) { return; } for (i = 0; i < target_count; i++) { if (!expand_path(targets[i].pattern, expanded, BL_MAX_PATH_LEN)) { continue; } report_path_if_exists(targets[i].tool, targets[i].family, expanded, results, filters, emit); } } # ===== file: blacklight-scout/native/ai_path_scout_core.h ===== /* * Blacklight Scout - shared AI path scout core * * Discovery-only path enumeration shared by Windows BOF, macOS dylib, * and Linux .so loaders. Does not read file contents. */ #ifndef BLACKLIGHT_AI_PATH_SCOUT_CORE_H #define BLACKLIGHT_AI_PATH_SCOUT_CORE_H #include <stddef.h> #define BL_MAX_PATH_LEN 1024 typedef struct { const char *tool; const char *family; #ifdef BLACKLIGHT_SCOUT_WINDOWS const wchar_t *pattern; #else const char *pattern; #endif } bl_path_target_t; typedef struct { int hit_count; int directory_count; int file_count; int filtered_count; int auto_select_count; } bl_scan_results_t; typedef struct { const char *tools; int tools_len; const char *exclude_tools; int exclude_tools_len; int max_depth; int triage; } bl_scan_filters_t; typedef void (*bl_emit_fn)(const char *tool, const char *family, const char *type, const void *path); int bl_target_allowed(const char *tool, const bl_scan_filters_t *filters); int bl_loader_filter_tool(const char *value, int value_len, const char **canonical); const char *bl_parser_hint_for_family(const char *family); const char *bl_triage_parser_hint(const char *family, int sqlite_path); int bl_triage_priority_tier(const char *family, int secret_like_path); void bl_scan_static_targets( const bl_path_target_t *targets, size_t target_count, bl_scan_results_t *results, const bl_scan_filters_t *filters, int (*expand_path)(const void *pattern, void *expanded, size_t expanded_size), int (*report_path_if_exists)( const char *tool, const char *family, const void *path, bl_scan_results_t *results, const bl_scan_filters_t *filters, bl_emit_fn emit ), bl_emit_fn emit ); char *bl_scout_run(int argc, char **argv); #endif # ===== file: blacklight-scout/native/ai_path_scout_posix.c ===== #define _POSIX_C_SOURCE 200809L /* * Blacklight Scout - POSIX loader for AI path scout * * No-argument filesystem triage for macOS and Linux shared libraries and * standalone executables. */ #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <errno.h> #include <sys/stat.h> #include <time.h> #include "../catalog/static_targets_generated.h" #include "ai_path_scout_core.h" static int g_auto_select_count = 0; #define BL_MAX_OPERATOR_TARGETS 256 #define BL_SESSION_SCAN_LIMIT 10000 #define BL_SESSION_SCAN_MAX_DEPTH 32 #define BL_SESSION_TOOL_COUNT 5 #define BL_TOP_SESSIONS_PER_TOOL 3 #define BL_TOP_SESSION_COUNT (BL_SESSION_TOOL_COUNT * BL_TOP_SESSIONS_PER_TOOL) #define BL_DYNAMIC_DISCOVERY_LIMIT 5000 #define BL_DYNAMIC_ROOT_LIMIT (BL_DYNAMIC_DISCOVERY_LIMIT / 4) #define BL_DYNAMIC_DISCOVERY_MAX_DEPTH 6 #define BL_CHILD_ENTRY_SCAN_LIMIT 10000 #define BL_PLAN_PREVIEW_COUNT 5 #define BL_OUTPUT_CAPACITY (64 * 1024) #define BL_OUTPUT_TRUNCATED_MESSAGE "\n[!] Output truncated.\n" static char g_output[BL_OUTPUT_CAPACITY]; static size_t g_output_length = 0; static void mark_output_truncated(void) { const size_t message_length = sizeof(BL_OUTPUT_TRUNCATED_MESSAGE) - 1; const size_t output_limit = sizeof(g_output) - 1; if (message_length < output_limit) { memcpy(g_output + output_limit - message_length, BL_OUTPUT_TRUNCATED_MESSAGE, message_length); } g_output[output_limit] = '\0'; g_output_length = output_limit; } static void reset_output(void) { g_output[0] = '\0'; g_output_length = 0; } static int output_printf(const char *format, ...) { va_list arguments; int written; if (g_output_length >= sizeof(g_output) - 1) { mark_output_truncated(); return 0; } va_start(arguments, format); written = vsnprintf(g_output + g_output_length, sizeof(g_output) - g_output_length, format, arguments); va_end(arguments); if (written < 0) { return written; } if ((size_t)written >= sizeof(g_output) - g_output_length) { mark_output_truncated(); return written; } g_output_length += (size_t)written; return written; } #define printf output_printf typedef struct { int priority_tier; int auto_select; long long size_bytes; long long child_count; long long modified_time; const char *tool; const char *family; const char *type; const char *parser_hint; char path[BL_MAX_PATH_LEN]; } bl_operator_target_t; static bl_operator_target_t g_operator_targets[BL_MAX_OPERATOR_TARGETS]; static int g_operator_target_count = 0; typedef struct { const char *tool; long long size_bytes; long long modified_time; char path[BL_MAX_PATH_LEN]; } bl_session_candidate_t; static bl_session_candidate_t g_top_sessions[BL_TOP_SESSION_COUNT]; static int g_top_session_count = 0; static int g_session_artifact_counts[BL_SESSION_TOOL_COUNT]; static int g_session_entries_scanned = 0; static int g_session_scan_partial = 0; static int g_session_scan_stop = 0; static int g_dynamic_entries_scanned = 0; static int g_dynamic_root_entries_scanned = 0; static int g_dynamic_scan_partial = 0; static int g_operator_target_overflow = 0; static int g_child_entries_scanned = 0; static int g_child_scan_partial = 0; static void inline_memset(void *dest, int value, size_t count) { unsigned char *d = (unsigned char *)dest; while (count--) { *d++ = (unsigned char)value; } } static size_t inline_strlen(const char *s) { size_t i = 0; if (!s) { return 0; } while (s[i] != '\0') { i++; } return i; } static int append_text(char *dst, size_t dst_size, const char *suffix) { size_t idx; size_t i = 0; if (!dst || !suffix || dst_size == 0) { return 0; } idx = inline_strlen(dst); while (suffix[i] && idx + 1 < dst_size) { dst[idx++] = suffix[i++]; } if (suffix[i]) { dst[0] = '\0'; return 0; } dst[idx] = '\0'; return 1; } static int posix_expand_path(const void *pattern, void *expanded, size_t expanded_size) { const char *text_pattern = (const char *)pattern; char *text_expanded = (char *)expanded; const char *home; const char *cursor; const char *end; size_t needed; if (!text_pattern || !text_expanded || expanded_size == 0) { return 0; } inline_memset(text_expanded, 0, expanded_size); /* Computer History alone follows the configured memory root. */ if (strcmp(text_pattern, "$HOME/.codex/memories/extensions/skysight") == 0) { const char *memory_root = getenv("CODEX_HOME"); if (memory_root && *memory_root) { return append_text(text_expanded, expanded_size, memory_root) && append_text(text_expanded, expanded_size, "/memories/extensions/skysight"); } } if (strncmp(text_pattern, "$HOME", 5) == 0 && (text_pattern[5] == '\0' || text_pattern[5] == '/')) { home = getenv("HOME"); if (!home) { return 0; } if (!append_text(text_expanded, expanded_size, home)) { return 0; } if (text_pattern[5] == '/') { if (!append_text(text_expanded, expanded_size, text_pattern + 5)) { return 0; } } return 1; } cursor = text_pattern; while (*cursor) { if (*cursor == '$') { end = cursor + 1; while (*end && ((*end >= 'A' && *end <= 'Z') || (*end >= '0' && *end <= '9') || *end == '_')) { end++; } if (end > cursor + 1) { char key[128]; const char *value; size_t key_len = (size_t)(end - cursor - 1); if (key_len >= sizeof(key)) { return 0; } inline_memset(key, 0, sizeof(key)); memcpy(key, cursor + 1, key_len); value = getenv(key); if (!value) { return 0; } if (!append_text(text_expanded, expanded_size, value)) { return 0; } cursor = end; continue; } } needed = inline_strlen(text_expanded); if (needed + 1 >= expanded_size) { text_expanded[0] = '\0'; return 0; } text_expanded[needed] = *cursor; text_expanded[needed + 1] = '\0'; cursor++; } return 1; } static int ascii_lower(int value) { if (value >= 'A' && value <= 'Z') { return value + ('a' - 'A'); } return value; } static int contains_ci_n(const char *text, const char *needle, int needle_len) { int i; int j; int text_len; if (!text || !needle || needle_len <= 0) { return 0; } text_len = (int)inline_strlen(text); if (needle_len > text_len) { return 0; } for (i = 0; i <= text_len - needle_len; i++) { for (j = 0; j < needle_len; j++) { if (ascii_lower((unsigned char)text[i + j]) != ascii_lower((unsigned char)needle[j])) { break; } } if (j == needle_len) { return 1; } } return 0; } static int path_token_is_boundary(char value) { return value == '\0' || value == '/' || value == '\\' || value == '.' || value == '_' || value == '-'; } static int contains_token_ci_n(const char *text, const char *needle, int needle_len) { int i; int j; int text_len; if (!text || !needle || needle_len <= 0) { return 0; } text_len = (int)inline_strlen(text); if (needle_len > text_len) { return 0; } for (i = 0; i <= text_len - needle_len; i++) { if (i > 0 && !path_token_is_boundary(text[i - 1])) { continue; } for (j = 0; j < needle_len; j++) { if (ascii_lower((unsigned char)text[i + j]) != ascii_lower((unsigned char)needle[j])) { break; } } if (j == needle_len && path_token_is_boundary(text[i + needle_len])) { return 1; } } return 0; } static int ends_with_ci(const char *text, const char *suffix) { size_t text_len; size_t suffix_len; size_t i; if (!text || !suffix) return 0; text_len = inline_strlen(text); suffix_len = inline_strlen(suffix); if (suffix_len > text_len) return 0; for (i = 0; i < suffix_len; i++) { if (ascii_lower((unsigned char)text[text_len - suffix_len + i]) != ascii_lower((unsigned char)suffix[i])) return 0; } return 1; } static int session_file_recognized(const char *tool, const char *path) { if (strcmp(tool, "codex") == 0) return contains_ci_n(path, "sessions", 8) && ends_with_ci(path, ".jsonl"); if (strcmp(tool, "claude_code") == 0) { return (contains_ci_n(path, "sessions", 8) || contains_ci_n(path, "projects", 8)) && ends_with_ci(path, ".jsonl"); } if (strcmp(tool, "cursor") == 0) { return ends_with_ci(path, "store.db") || (contains_ci_n(path, "agent-transcripts", 17) && ends_with_ci(path, ".jsonl")); } if (strcmp(tool, "antigravity_cli") == 0) { return ends_with_ci(path, "transcript_full.jsonl") || ends_with_ci(path, "transcript.jsonl") || ends_with_ci(path, "conversation_summaries.db") || (contains_ci_n(path, "conversations", 13) && ends_with_ci(path, ".db")); } if (strcmp(tool, "grok") == 0) { return ends_with_ci(path, "updates.jsonl"); } return 0; } static int session_candidate_better(const bl_session_candidate_t *left, const bl_session_candidate_t *right) { int comparison; if (left->modified_time != right->modified_time) return left->modified_time > right->modified_time; if (left->size_bytes != right->size_bytes) return left->size_bytes > right->size_bytes; comparison = strcmp(left->tool, right->tool); if (comparison != 0) return comparison < 0; return strcmp(left->path, right->path) < 0; } static int session_tool_slot(const char *tool) { if (strcmp(tool, "codex") == 0) return 0; if (strcmp(tool, "claude_code") == 0) return 1; if (strcmp(tool, "cursor") == 0) return 2; if (strcmp(tool, "antigravity_cli") == 0) return 3; if (strcmp(tool, "grok") == 0) return 4; return -1; } static void consider_session_candidate(const char *tool, const char *path, const struct stat *info) { bl_session_candidate_t candidate; int tool_slot; int base; int insert = -1; int i; if (!tool || !path || !info || !session_file_recognized(tool, path)) return; tool_slot = session_tool_slot(tool); if (tool_slot < 0) return; g_session_artifact_counts[tool_slot]++; base = tool_slot * BL_TOP_SESSIONS_PER_TOOL; inline_memset(&candidate, 0, sizeof(candidate)); candidate.tool = tool; candidate.size_bytes = (long long)info->st_size; candidate.modified_time = (long long)info->st_mtime; memcpy(candidate.path, path, inline_strlen(path) < BL_MAX_PATH_LEN - 1 ? inline_strlen(path) : BL_MAX_PATH_LEN - 1); for (i = 0; i < BL_TOP_SESSIONS_PER_TOOL; i++) { if (g_top_sessions[base + i].tool && strcmp(g_top_sessions[base + i].path, path) == 0) return; if (insert < 0 && (!g_top_sessions[base + i].tool || session_candidate_better(&candidate, &g_top_sessions[base + i]))) insert = i; } if (insert < 0) return; if (!g_top_sessions[base + BL_TOP_SESSIONS_PER_TOOL - 1].tool) g_top_session_count++; for (i = BL_TOP_SESSIONS_PER_TOOL - 1; i > insert; i--) g_top_sessions[base + i] = g_top_sessions[base + i - 1]; g_top_sessions[base + insert] = candidate; } static void walk_session_files(const char *tool, const char *directory, int depth) { DIR *dir; struct dirent *entry; char child[BL_MAX_PATH_LEN]; struct stat info; size_t directory_len; if (g_session_scan_stop) return; if (depth > BL_SESSION_SCAN_MAX_DEPTH) { g_session_scan_partial = 1; return; } dir = opendir(directory); if (!dir) { g_session_scan_partial = 1; return; } directory_len = inline_strlen(directory); errno = 0; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if (g_session_entries_scanned >= BL_SESSION_SCAN_LIMIT) { g_session_scan_partial = 1; g_session_scan_stop = 1; break; } g_session_entries_scanned++; if (directory_len + inline_strlen(entry->d_name) + 2 >= sizeof(child)) { g_session_scan_partial = 1; continue; } inline_memset(child, 0, sizeof(child)); memcpy(child, directory, directory_len); if (directory_len && child[directory_len - 1] != '/') child[directory_len] = '/'; if (!append_text(child, sizeof(child), entry->d_name)) { g_session_scan_partial = 1; continue; } if (lstat(child, &info) != 0) { g_session_scan_partial = 1; continue; } if (S_ISLNK(info.st_mode)) continue; if (S_ISDIR(info.st_mode)) walk_session_files(tool, child, depth + 1); else if (S_ISREG(info.st_mode)) consider_session_candidate(tool, child, &info); if (g_session_scan_stop) break; errno = 0; } if (!g_session_scan_stop && errno != 0) g_session_scan_partial = 1; closedir(dir); } static int target_can_contain_sessions(const bl_operator_target_t *target) { if (!target || strcmp(target->type, "directory") != 0) return 0; if (strcmp(target->family, "sessions") == 0 || strcmp(target->family, "history") == 0) return 1; return strcmp(target->family, "workspace") == 0 && (strcmp(target->tool, "claude_code") == 0 || strcmp(target->tool, "cursor") == 0); } static int session_root_seen_before(int index, const bl_operator_target_t *target) { int i; for (i = 0; i < index; i++) { if (target_can_contain_sessions(&g_operator_targets[i]) && strcmp(g_operator_targets[i].tool, target->tool) == 0 && strcmp(g_operator_targets[i].path, target->path) == 0) return 1; } return 0; } static void scan_session_candidates(void) { int i; inline_memset(g_top_sessions, 0, sizeof(g_top_sessions)); inline_memset(g_session_artifact_counts, 0, sizeof(g_session_artifact_counts)); g_top_session_count = 0; g_session_entries_scanned = 0; g_session_scan_partial = 0; g_session_scan_stop = 0; for (i = 0; i < g_operator_target_count && !g_session_scan_stop; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; if (target_can_contain_sessions(target)) { if (!session_root_seen_before(i, target)) walk_session_files(target->tool, target->path, 0); } else if (strcmp(target->type, "file") == 0 && session_file_recognized(target->tool, target->path)) { struct stat info; if (lstat(target->path, &info) == 0 && S_ISREG(info.st_mode)) consider_session_candidate(target->tool, target->path, &info); else g_session_scan_partial = 1; } } } static long long count_directory_children(const char *path, int depth) { DIR *dir; struct dirent *entry; char child[BL_MAX_PATH_LEN]; struct stat info; long long count = 0; size_t path_len; dir = opendir(path); if (!dir || depth <= 0) { return -1; } path_len = inline_strlen(path); while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } if (g_child_entries_scanned >= BL_CHILD_ENTRY_SCAN_LIMIT) { g_child_scan_partial = 1; break; } g_child_entries_scanned++; count++; if (depth <= 1) { continue; } inline_memset(child, 0, sizeof(child)); if (path_len + inline_strlen(entry->d_name) + 2 >= sizeof(child)) { continue; } memcpy(child, path, path_len); if (path_len > 0 && child[path_len - 1] != '/') { child[path_len] = '/'; child[path_len + 1] = '\0'; } if (!append_text(child, sizeof(child), entry->d_name)) { continue; } if (lstat(child, &info) == 0 && !S_ISLNK(info.st_mode) && S_ISDIR(info.st_mode)) { long long nested = count_directory_children(child, depth - 1); if (nested > 0) { count += nested; } } } closedir(dir); return count; } static void record_operator_target( const char *tool, const char *family, const char *type, const char *path, int priority_tier, const char *parser_hint, long long size_bytes, long long child_count, long long modified_time, int auto_select ) { bl_operator_target_t *target; if (g_operator_target_count >= BL_MAX_OPERATOR_TARGETS || !path) { g_operator_target_overflow = 1; return; } target = &g_operator_targets[g_operator_target_count++]; target->priority_tier = priority_tier; target->auto_select = auto_select; target->size_bytes = size_bytes; target->child_count = child_count; target->modified_time = modified_time; target->tool = tool; target->family = family; target->type = type; target->parser_hint = parser_hint; inline_memset(target->path, 0, sizeof(target->path)); strncpy(target->path, path, sizeof(target->path) - 1); } static int operator_target_compare(const void *left, const void *right) { const bl_operator_target_t *a = (const bl_operator_target_t *)left; const bl_operator_target_t *b = (const bl_operator_target_t *)right; int cmp; if (a->priority_tier != b->priority_tier) { return a->priority_tier - b->priority_tier; } if (a->auto_select != b->auto_select) { return b->auto_select - a->auto_select; } cmp = strcmp(a->tool, b->tool); if (cmp != 0) { return cmp; } cmp = strcmp(a->family, b->family); if (cmp != 0) { return cmp; } return strcmp(a->path, b->path); } static int target_family_is(const bl_operator_target_t *target, const char *family) { return target && target->family && strcmp(target->family, family) == 0; } static int target_tool_is(const bl_operator_target_t *target, const char *tool) { return target && target->tool && strcmp(target->tool, tool) == 0; } static int target_is_file(const bl_operator_target_t *target) { return target && target->type && strcmp(target->type, "file") == 0; } static int target_is_directory(const bl_operator_target_t *target) { return target && target->type && strcmp(target->type, "directory") == 0; } static int target_is_tier2_review(const bl_operator_target_t *target) { return target && target->priority_tier == 2; } static int target_is_session_context(const bl_operator_target_t *target) { return target && target->priority_tier == 3; } static void format_size(long long size_bytes, char *buffer, size_t buffer_size) { if (!buffer || buffer_size == 0) { return; } if (size_bytes < 1024) { snprintf(buffer, buffer_size, "%lld B", size_bytes); } else if (size_bytes < 1024LL * 1024LL) { snprintf(buffer, buffer_size, "%.1f KB", (double)size_bytes / 1024.0); } else { snprintf(buffer, buffer_size, "%.1f MB", (double)size_bytes / (1024.0 * 1024.0)); } } static void print_escaped_path(const char *path) { const unsigned char *cursor = (const unsigned char *)path; if (!cursor) return; while (*cursor) { if (*cursor == '\n') printf("\\n"); else if (*cursor == '\r') printf("\\r"); else if (*cursor == '\t') printf("\\t"); else if (*cursor < 0x20 || *cursor == 0x7f) printf("\\x%02x", *cursor); else printf("%c", *cursor); cursor++; } } static void print_collection_first(void) { int i; int printed = 0; char size_text[32]; printf("[i] Collect first: credential/session metadata and auth-adjacent configs\n"); for (i = 0; i < g_operator_target_count; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; if (!target->auto_select) { continue; } format_size(target->size_bytes, size_text, sizeof(size_text)); printf( "[i] [%d] %s %s %s %s", target->priority_tier, target->tool, target->family, target->type, size_text ); printf("\n[i] "); print_escaped_path(target->path); printf("\n"); printed++; } if (!printed) { printf("[i] none\n"); } printf("[i]\n"); } static void print_tier2_group(const char *tool) { int i; int count = 0; int files = 0; int dirs = 0; int has_config = 0; int has_rules = 0; int has_sandbox = 0; int has_plugins = 0; int has_other = 0; for (i = 0; i < g_operator_target_count; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; if (!target_tool_is(target, tool) || !target_is_tier2_review(target)) { continue; } count++; files += target_is_file(target); dirs += target_is_directory(target); has_config |= target_family_is(target, "config") || target_family_is(target, "mcp") || target_family_is(target, "mcp_config") || target_family_is(target, "project_config") || target_family_is(target, "project_mcp_config") || target_family_is(target, "permissions"); has_rules |= target_family_is(target, "rules"); has_sandbox |= target_family_is(target, "sandbox") || strstr(target->path, "/.sandbox-secrets/") != NULL; has_plugins |= target_family_is(target, "plugins") || target_family_is(target, "extensions") || target_family_is(target, "skills"); has_other |= !(target_family_is(target, "config") || target_family_is(target, "mcp") || target_family_is(target, "mcp_config") || target_family_is(target, "project_config") || target_family_is(target, "project_mcp_config") || target_family_is(target, "permissions") || target_family_is(target, "rules") || target_family_is(target, "sandbox") || target_family_is(target, "plugins") || target_family_is(target, "extensions") || target_family_is(target, "skills") || strstr(target->path, "/.sandbox-secrets/") != NULL); } if (count <= 0) { return; } printf("[i] [2] %s ", tool); { int labels = 0; if (has_config) { printf("config"); labels++; } if (has_rules) { printf("%srules", labels ? "/" : ""); labels++; } if (has_plugins) { printf("%splugins", labels ? "/" : ""); labels++; } if (has_sandbox) { printf("%ssandbox", labels ? "/" : ""); labels++; } if (has_other || labels == 0) { printf("%sreview", labels ? "/" : ""); } } if (files == count) { printf(" files x%d", count); } else if (dirs == count) { printf(" dirs x%d", count); } else { printf(" artifacts x%d", count); } printf("\n"); for (i = 0; i < g_operator_target_count; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; if (target_tool_is(target, tool) && target_is_tier2_review(target)) { printf("[i] "); print_escaped_path(target->path); printf("\n"); } } } static void print_review_next(void) { int i; printf("[i] Review next: config, rules, plugins, sandbox state\n"); for (i = 0; i < g_operator_target_count; i++) { int seen = 0; int j; if (!target_is_tier2_review(&g_operator_targets[i])) { continue; } for (j = 0; j < i; j++) { if (target_is_tier2_review(&g_operator_targets[j]) && target_tool_is(&g_operator_targets[j], g_operator_targets[i].tool)) { seen = 1; break; } } if (!seen) { print_tier2_group(g_operator_targets[i].tool); } } printf("[i]\n"); } static void print_session_context(void) { int i; int j; int printed = 0; int group_count = 0; typedef struct { const char *tool; int count; long long largest; int printed; } session_group_t; session_group_t groups[32]; printf("[i] Session activity (collect selectively)\n"); for (i = 0; i < g_operator_target_count; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; int index = -1; if (!target_is_session_context(target)) { continue; } for (j = 0; j < group_count; j++) { if (strcmp(groups[j].tool, target->tool) == 0) { index = j; break; } } if (index < 0) { if (group_count >= (int)(sizeof(groups) / sizeof(groups[0]))) { continue; } index = group_count++; groups[index].tool = target->tool; groups[index].count = 0; groups[index].largest = 0; groups[index].printed = 0; } groups[index].count++; if (target->size_bytes > groups[index].largest) { groups[index].largest = target->size_bytes; } } while (printed < group_count) { int best = -1; for (i = 0; i < group_count; i++) { if (groups[i].printed) { continue; } if (best < 0 || groups[i].count > groups[best].count || (groups[i].count == groups[best].count && groups[i].largest > groups[best].largest) || (groups[i].count == groups[best].count && groups[i].largest == groups[best].largest && strcmp(groups[i].tool, groups[best].tool) < 0)) { best = i; } } if (best < 0) { break; } { int tool_slot = session_tool_slot(groups[best].tool); int session_artifacts = tool_slot >= 0 ? g_session_artifact_counts[tool_slot] : 0; printf("[i] [3] %s history/session locations x%d | %d session artifact%s%s\n", groups[best].tool, groups[best].count, session_artifacts, session_artifacts == 1 ? "" : "s", g_session_scan_partial ? " (partial scan)" : ""); } for (i = 0; i < g_operator_target_count; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; if (!target_is_session_context(target) || strcmp(target->tool, groups[best].tool) != 0) { continue; } printf("[i] "); print_escaped_path(target->path); printf("\n"); } groups[best].printed = 1; printed++; } if (!printed) { printf("[i] none\n"); } printf("[i]\n"); } static void print_plan_previews(void) { int i; int plan_count = 0; int plan_printed = 0; int selected[BL_PLAN_PREVIEW_COUNT] = {-1, -1, -1, -1, -1}; for (i = 0; i < g_operator_target_count; i++) { const bl_operator_target_t *target = &g_operator_targets[i]; if (!target_family_is(target, "plans")) continue; plan_count++; if (target_is_directory(target)) { printf("[i] "); print_escaped_path(target->path); printf("\n"); plan_printed++; } } for (i = 0; i < BL_PLAN_PREVIEW_COUNT; i++) { int j; int best = -1; for (j = 0; j < g_operator_target_count; j++) { const bl_operator_target_t *candidate = &g_operator_targets[j]; int already_selected = 0; int k; if (!target_family_is(candidate, "plans") || !target_is_file(candidate)) continue; for (k = 0; k < i; k++) { if (selected[k] == j) { already_selected = 1; break; } } if (already_selected || (best >= 0 && (candidate->modified_time < g_operator_targets[best].modified_time || (candidate->modified_time == g_operator_targets[best].modified_time && strcmp(candidate->path, g_operator_targets[best].path) >= 0)))) continue; best = j; } if (best < 0) break; selected[i] = best; printf("[i] "); print_escaped_path(g_operator_targets[best].path); printf("\n"); plan_printed++; } if (plan_count > plan_printed) { printf("[i] ... %d additional plan artifact%s omitted\n", plan_count - plan_printed, plan_count - plan_printed == 1 ? "" : "s"); } } static void print_low_priority(void) { int tier; int printed = 0; printf("[i] Other inventory\n"); for (tier = 4; tier <= 5; tier++) { int i; for (i = 0; i < g_operator_target_count; i++) { int j; int seen = 0; int count = 0; int dirs = 0; int files = 0; const char *family = g_operator_targets[i].family; if (g_operator_targets[i].priority_tier != tier) { continue; } for (j = 0; j < i; j++) { if (g_operator_targets[j].priority_tier == tier && strcmp(g_operator_targets[j].family, family) == 0) { seen = 1; break; } } if (seen) { continue; } for (j = 0; j < g_operator_target_count; j++) { const bl_operator_target_t *target = &g_operator_targets[j]; if (target->priority_tier == tier && strcmp(target->family, family) == 0) { count++; dirs += target_is_directory(target); files += target_is_file(target); } } printf("[i] [%d] %s %s x%d\n", tier, family, dirs == count ? "dirs" : (files == count ? "files" : "artifacts"), count); if (strcmp(family, "plans") == 0) print_plan_previews(); else for (j = 0; j < g_operator_target_count; j++) { const bl_operator_target_t *target = &g_operator_targets[j]; if (target->priority_tier == tier && strcmp(target->family, family) == 0) { printf("[i] "); print_escaped_path(target->path); printf("\n"); } } printed++; } } if (!printed) { printf("[i] none\n"); } printf("[i]\n"); } static void print_top_session_files(void) { int i; int rank = 0; printf("[i] PRIORITIZED SESSION ARTIFACTS (newest first)%s\n", g_session_scan_partial ? " (partial scan)" : ""); if (g_top_session_count == 0) printf("[i] none\n"); for (i = 0; i < BL_TOP_SESSION_COUNT; i++) { char size_text[32]; char project[BL_MAX_PATH_LEN] = {0}; const char *project_start; struct tm *utc; time_t modified; const bl_session_candidate_t *target = &g_top_sessions[i]; if (!target->tool) continue; rank = (i % BL_TOP_SESSIONS_PER_TOOL) + 1; format_size(target->size_bytes, size_text, sizeof(size_text)); project_start = strstr(target->path, "/projects/"); if (project_start && (strcmp(target->tool, "claude_code") == 0 || strcmp(target->tool, "cursor") == 0)) { size_t length = 0, last_dash = 0; project_start += 10; while (project_start[length] && project_start[length] != '/' && length + 1 < sizeof(project)) { project[length] = project_start[length]; if (project[length] == '-') last_dash = length; length++; } project[length] = '\0'; if (last_dash && project[last_dash + 1]) memmove(project, project + last_dash + 1, strlen(project + last_dash + 1) + 1); } modified = (time_t)target->modified_time; utc = gmtime(&modified); printf("[+] [%d] %s | %s", rank, target->tool, size_text); if (utc) printf(" | modified %04d-%02d-%02d", utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday); if (project[0]) { printf(" | project="); print_escaped_path(project); } printf("\n "); print_escaped_path(target->path); printf("\n"); } printf("[i]\n"); } static void print_operator_triage_summary(void) { int i, tools = 0, auth = 0, sessions = 0, session_artifacts = 0; printf("[i] Blacklight endpoint assessment\n[i]\n"); if (g_operator_target_count <= 0) { printf("[i] ASSESSMENT SUMMARY\n[i] Tools detected: 0\n[i] Discovery status: COMPLETE\n[i]\n"); return; } printf("[i] ASSESSMENT SUMMARY\n"); for (i = 0; i < g_operator_target_count; i++) { int j, seen = 0; if (target_family_is(&g_operator_targets[i], "auth") && target_is_file(&g_operator_targets[i])) auth++; if (target_is_session_context(&g_operator_targets[i])) sessions++; for (j = 0; j < i; j++) if (strcmp(g_operator_targets[j].tool, g_operator_targets[i].tool) == 0) { seen = 1; break; } if (!seen) tools++; } printf("[i] Tools detected: %d\n", tools); if (auth) printf("[+] Credential stores: %d file%s\n", auth, auth == 1 ? "" : "s"); if (sessions) printf("[i] Session locations: %d\n", sessions); for (i = 0; i < BL_SESSION_TOOL_COUNT; i++) session_artifacts += g_session_artifact_counts[i]; if (sessions || g_session_scan_partial) printf("[i] Session artifacts: %d%s\n", session_artifacts, g_session_scan_partial ? " (partial scan)" : ""); printf("%s Discovery status: %s\n[i]\n", (g_dynamic_scan_partial || g_operator_target_overflow || g_session_scan_partial) ? "[!]" : "[i]", (g_dynamic_scan_partial || g_operator_target_overflow || g_session_scan_partial) ? "PARTIAL" : "COMPLETE"); qsort( g_operator_targets, (size_t)g_operator_target_count, sizeof(g_operator_targets[0]), operator_target_compare ); print_collection_first(); print_review_next(); print_top_session_files(); print_session_context(); print_low_priority(); if (g_operator_target_overflow) printf("[!] Result storage cap reached at %d artifacts. Results are incomplete.\n", BL_MAX_OPERATOR_TARGETS); if (g_dynamic_scan_partial) printf("[!] Discovery cap reached after %d candidate entries (per-root cap %d; run cap %d). Results are incomplete.\n", g_dynamic_entries_scanned, BL_DYNAMIC_ROOT_LIMIT, BL_DYNAMIC_DISCOVERY_LIMIT); } static void posix_emit_triage(const char *tool, const char *family, const char *type, const void *path) { const char *text_path = (const char *)path; struct stat info; long long size_bytes = 0; long long child_count = 0; long long modified_time = 0; int priority_tier; const char *parser_hint; int sandbox_secret_path; int auth_adjacent_config_path; if (stat(text_path, &info) == 0) { modified_time = (long long)info.st_mtime; if (S_ISDIR(info.st_mode)) { child_count = count_directory_children(text_path, 1); if (child_count < 0) { child_count = 0; } } else { size_bytes = (long long)info.st_size; } } sandbox_secret_path = contains_ci_n(text_path, ".sandbox-secrets", 16); auth_adjacent_config_path = contains_ci_n(text_path, ".claude.json", 12) && !contains_ci_n(text_path, ".claude/.claude.json", 20); priority_tier = bl_triage_priority_tier( family, (!sandbox_secret_path && !contains_ci_n(text_path, "mcp-needs-auth-cache", 21) && ( contains_token_ci_n(text_path, "auth", 4) || contains_token_ci_n(text_path, "credential", 10) || contains_token_ci_n(text_path, "token", 5) || contains_token_ci_n(text_path, "secret", 6) )) || auth_adjacent_config_path ); if (sandbox_secret_path && priority_tier == 1) { priority_tier = 2; } parser_hint = bl_triage_parser_hint(family, contains_ci_n(text_path, ".db", 3) || contains_ci_n(text_path, ".sqlite", 7)); record_operator_target( tool, family, type, text_path, priority_tier, parser_hint, size_bytes, child_count, modified_time, priority_tier == 1 && type && type[0] == 'f' ); if (priority_tier == 1 && type && type[0] == 'f') { g_auto_select_count++; } } static int posix_report_path_if_exists( const char *tool, const char *family, const void *path, bl_scan_results_t *results, const bl_scan_filters_t *filters, bl_emit_fn emit ) { const char *text_path = (const char *)path; struct stat info; const char *type; if (!tool || !family || !text_path || !results || !emit) { return 0; } if (!bl_target_allowed(tool, filters)) { results->filtered_count++; return 0; } if (lstat(text_path, &info) != 0 || S_ISLNK(info.st_mode)) { return 0; } if (S_ISDIR(info.st_mode)) { type = "directory"; results->directory_count++; } else { type = "file"; results->file_count++; } results->hit_count++; emit(tool, family, type, text_path); return 1; } static int operator_path_recorded(const char *path) { int i; for (i = 0; i < g_operator_target_count; i++) { if (strcmp(g_operator_targets[i].path, path) == 0) return 1; } return 0; } static void report_dynamic_file( const char *tool, const char *family, const char *path, bl_scan_results_t *results, const bl_scan_filters_t *filters, bl_emit_fn emit ) { if (!operator_path_recorded(path)) posix_report_path_if_exists(tool, family, path, results, filters, emit); } static void scan_dynamic_tree( const char *tool, const char *directory, int depth, int include_plan_metadata, bl_scan_results_t *results, const bl_scan_filters_t *filters, bl_emit_fn emit ) { DIR *dir; struct dirent *entry; char child[BL_MAX_PATH_LEN]; struct stat info; size_t directory_len; if (depth < 0 || g_dynamic_entries_scanned >= BL_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BL_DYNAMIC_ROOT_LIMIT) { if (g_dynamic_entries_scanned >= BL_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BL_DYNAMIC_ROOT_LIMIT) g_dynamic_scan_partial = 1; return; } dir = opendir(directory); if (!dir) return; directory_len = inline_strlen(directory); while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; if (g_dynamic_entries_scanned >= BL_DYNAMIC_DISCOVERY_LIMIT || g_dynamic_root_entries_scanned >= BL_DYNAMIC_ROOT_LIMIT) { g_dynamic_scan_partial = 1; break; } g_dynamic_entries_scanned++; g_dynamic_root_entries_scanned++; if (directory_len + inline_strlen(entry->d_name) + 2 >= sizeof(child)) continue; inline_memset(child, 0, sizeof(child)); memcpy(child, directory, directory_len); if (directory_len && child[directory_len - 1] != '/') child[directory_len] = '/'; if (!append_text(child, sizeof(child), entry->d_name) || lstat(child, &info) != 0 || S_ISLNK(info.st_mode)) continue; if (S_ISDIR(info.st_mode)) { if (depth > 0) scan_dynamic_tree(tool, child, depth - 1, include_plan_metadata, results, filters, emit); } else if (S_ISREG(info.st_mode) && strcmp(entry->d_
# ===== file: blacklight-scout/native/ai_path_scout_bof.c =====
/*
* Blacklight Scout - Windows BOF wrapper for AI path scout
*
* Snapshot BOF for Windows AI tooling artifacts. Reports bounded filesystem
* metadata without reading file contents.
*/
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define BLACKLIGHT_SCOUT_WINDOWS 1
#include <stddef.h>
#include <stdint.h>
#include "../catalog/static_targets_generated.h"
#include "ai_path_scout_core.h"
#include "beacon.h"
#define MAX_BOF_TRIAGE_TARGETS 256
#define BOF_SESSION_SCAN_LIMIT 10000
#define BOF_SESSION_SCAN_MAX_DEPTH 32
#define BOF_SESSION_TOOL_COUNT 5
#define BOF_TOP_SESSIONS_PER_TOOL 3
#define BOF_TOP_SESSION_COUNT (BOF_SESSION_TOOL_COUNT * BOF_TOP_SESSIONS_PER_TOOL)
#define BOF_DYNAMIC_DISCOVERY_LIMIT 5000
#define BOF_DYNAMIC_ROOT_LIMIT (BOF_DYNAMIC_DISCOVERY_LIMIT / 4)
#define BOF_DYNAMIC_DISCOVERY_MAX_DEPTH 6
#define BOF_CHILD_ENTRY_SCAN_LIMIT 10000
DECLSPEC_IMPORT DWORD WINAPI KERNEL32$ExpandEnvironmentStringsW(LPCWSTR, wchar_t *, DWORD);
DECLSPEC_IMPORT DWORD WINAPI KERNEL32$GetFileAttributesW(LPCWSTR);
DECLSPEC_IMPORT HANDLE WINAPI KERNEL32$FindFirstFileW(LPCWSTR, LPWIN32_FIND_DATAW);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$FindNextFileW(HANDLE, LPWIN32_FIND_DATAW);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$FindClose(HANDLE);
DECLSPEC_IMPORT DWORD WINAPI KERNEL32$GetLastError(void);
DECLSPEC_IMPORT BOOL WINAPI KERNEL32$FileTimeToSystemTime(const FILETIME *, SYSTEMTIME *);
static int g_auto_select_count = 0;
typedef struct {
int priority_tier;
int auto_select;
long long size_bytes;
long long child_count;
const char *tool;
const char *family;
const char *type;
const char *parser_hint;
wchar_t path[BL_MAX_PATH_LEN];
} bof_triage_target_t;
static bof_triage_target_t g_bof_triage_targets[MAX_BOF_TRIAGE_TARGETS];
static int g_bof_triage_target_count = 0;
static int g_bof_triage_target_overflow = 0;
typedef struct {
const char *tool;
long long size_bytes;
FILETIME last_write_time;
wchar_t path[BL_MAX_PATH_LEN];
} bof_session_candidate_t;
static bof_session_candidate_t g_top_sessions[BOF_TOP_SESSION_COUNT];
static int g_top_session_count = 0;
static int g_session_artifact_counts[BOF_SESSION_TOOL_COUNT];
static int g_session_entries_scanned = 0;
static int g_session_scan_partial = 0;
static int g_session_scan_stop = 0;
static int g_dynamic_entries_scanned = 0;
static int g_dynamic_root_entries_scanned = 0;
static int g_dynamic_scan_partial = 0;
static int g_child_entries_scanned = 0;
static int g_child_scan_partial = 0;
static void inline_memset(void *dest, int value, size_t count) {
unsigned char *d = (unsigned char *)dest;
while (count--) {
*d++ = (unsigned char)value;
}
}
static size_t inline_wcslen(const wchar_t *s) {
size_t i = 0;
if (!s) {
return 0;
}
while (s[i] != L'\0') {
i++;
}
return i;
}
static int build_path(const wchar_t *left, const wchar_t *right, wchar_t *out, size_t out_size) {
size_t idx = 0;
size_t left_idx = 0;
size_t right_idx = 0;
if (!left || !right || !out || out_size == 0) {