Skip to content

Commit 789c60c

Browse files
committed
feat(mcp): report truthful index freshness from checkout evidence
Signed-off-by: tmonestudio <tmonestudio@users.noreply.github.com>
1 parent 7d74337 commit 789c60c

11 files changed

Lines changed: 1311 additions & 17 deletions

File tree

‎src/git/git_context.c‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ enum {
1616
GIT_OUTPUT_MAX = 4096,
1717
};
1818

19+
/* Bounded worktree-status stream constants.
20+
* STATUS_FIELD_MAX is the largest single NUL-delimited record field we
21+
* buffer; a field at/over the bound is treated as a malformed stream and the
22+
* whole snapshot fails closed. PATH components of real repos are far below
23+
* this even with long paths. */
24+
enum {
25+
STATUS_HEADER_LEN = 3, /* porcelain-v1 record prefix "XY " */
26+
STATUS_CHUNK_MAX = CBM_SZ_4K,
27+
STATUS_FIELD_MAX = CBM_SZ_16K,
28+
};
29+
1930
static char *git_strdup(const char *s) {
2031
if (!s) {
2132
s = "";
@@ -409,3 +420,210 @@ int cbm_git_context_props_json(const cbm_git_context_t *ctx, char *buf, int buf_
409420
}
410421
return off;
411422
}
423+
424+
/* ── Bounded worktree status ──────────────────────────────────────────────
425+
* git status --porcelain=v1 -z --untracked-files=all is parsed record by
426+
* record, NUL-delimited, over a bounded chunk buffer. The full stream is
427+
* counted but only max_samples paths per class (tracked / untracked) are
428+
* ever retained, so a huge dirty tree cannot grow memory. Rename/copy
429+
* records carry a second NUL-terminated path (the source); it is consumed
430+
* without being counted so one rename counts once. */
431+
432+
typedef struct {
433+
cbm_worktree_status_t *out;
434+
int max_samples;
435+
bool expecting_source; /* next field is the source path of an R/C record */
436+
bool parse_ok;
437+
char partial[STATUS_FIELD_MAX];
438+
size_t partial_len;
439+
} status_parser_t;
440+
441+
/* porcelain-v1 status columns (X/Y): documented codes plus the lowercase
442+
* submodule variants and type-change 'T'. Anything else is not a record
443+
* header, so a malformed stream fails closed instead of being misread. */
444+
static bool status_code_ok(char c) {
445+
switch (c) {
446+
case ' ':
447+
case 'M':
448+
case 'A':
449+
case 'D':
450+
case 'R':
451+
case 'C':
452+
case 'U':
453+
case '?':
454+
case '!':
455+
case 'T':
456+
case 'm':
457+
case 'a':
458+
case 'd':
459+
case 'r':
460+
case 'c':
461+
case 'u':
462+
case 't':
463+
return true;
464+
default:
465+
return false;
466+
}
467+
}
468+
469+
/* Retain one bounded sample; the running total always advances so the caller
470+
* sees the true stream count even when samples are capped or OOM. */
471+
static void status_add_sample(int *total, char ***samples, int *sample_count,
472+
bool *truncated, int max_samples, const char *path,
473+
size_t path_len) {
474+
(*total)++;
475+
if (*sample_count >= max_samples) {
476+
*truncated = true;
477+
return;
478+
}
479+
char *copy = (char *)malloc(path_len + 1);
480+
if (!copy) {
481+
*truncated = true;
482+
return;
483+
}
484+
memcpy(copy, path, path_len);
485+
copy[path_len] = '\0';
486+
char **grown = (char **)realloc(*samples, (size_t)(*sample_count + 1) * sizeof(char *));
487+
if (!grown) {
488+
free(copy);
489+
*truncated = true;
490+
return;
491+
}
492+
*samples = grown;
493+
(*samples)[*sample_count] = copy;
494+
(*sample_count)++;
495+
}
496+
497+
/* Parse one complete NUL-delimited field. Returns false on malformed input,
498+
* which fails the whole snapshot closed. */
499+
static bool status_parse_record(cbm_worktree_status_t *out, status_parser_t *parser,
500+
const char *field, size_t field_len) {
501+
if (parser->expecting_source) {
502+
parser->expecting_source = false;
503+
return true; /* second path of a rename/copy record — not counted */
504+
}
505+
if (field_len < STATUS_HEADER_LEN || field[2] != ' ' ||
506+
!status_code_ok(field[0]) || !status_code_ok(field[1])) {
507+
return false;
508+
}
509+
const char *path = field + STATUS_HEADER_LEN;
510+
size_t path_len = field_len - STATUS_HEADER_LEN;
511+
if (field[0] == '?' && field[1] == '?') {
512+
status_add_sample(&out->untracked_count, &out->untracked_paths,
513+
&out->untracked_sample_count, &out->untracked_truncated,
514+
parser->max_samples, path, path_len);
515+
return true;
516+
}
517+
if (field[0] == 'R' || field[0] == 'C' || field[1] == 'R' || field[1] == 'C') {
518+
parser->expecting_source = true;
519+
}
520+
status_add_sample(&out->tracked_count, &out->tracked_paths,
521+
&out->tracked_sample_count, &out->tracked_truncated,
522+
parser->max_samples, path, path_len);
523+
return true;
524+
}
525+
526+
/* Feed a chunk of the NUL-delimited stream through the bounded record parser. */
527+
static void status_feed(status_parser_t *parser, const char *data, size_t data_len) {
528+
size_t i = 0;
529+
while (i < data_len) {
530+
if (parser->partial_len >= STATUS_FIELD_MAX) {
531+
parser->parse_ok = false; /* field exceeded the bound: malformed */
532+
return;
533+
}
534+
size_t j = i;
535+
while (j < data_len && data[j] != '\0' &&
536+
parser->partial_len + (j - i) < STATUS_FIELD_MAX) {
537+
j++;
538+
}
539+
size_t take = j - i;
540+
memcpy(parser->partial + parser->partial_len, data + i, take);
541+
parser->partial_len += take;
542+
i += take;
543+
if (i < data_len && data[i] == '\0') {
544+
if (!status_parse_record(parser->out, parser, parser->partial,
545+
parser->partial_len)) {
546+
parser->parse_ok = false;
547+
return;
548+
}
549+
parser->partial_len = 0;
550+
i++; /* consume the record terminator */
551+
} else if (i >= data_len) {
552+
return; /* chunk exhausted mid-record */
553+
}
554+
}
555+
}
556+
557+
int cbm_git_worktree_status(const char *validated_root, int max_samples,
558+
cbm_worktree_status_t *out) {
559+
if (!out) {
560+
return CBM_NOT_FOUND;
561+
}
562+
memset(out, 0, sizeof(*out));
563+
if (!validated_root || !validated_root[0] || max_samples < 0) {
564+
return CBM_NOT_FOUND;
565+
}
566+
if (!git_validate_repo_path(validated_root)) {
567+
return CBM_NOT_FOUND;
568+
}
569+
570+
char cmd[GIT_CMD_MAX];
571+
#ifdef _WIN32
572+
const char *null_dev = "NUL";
573+
#else
574+
const char *null_dev = "/dev/null";
575+
#endif
576+
int n = snprintf(cmd, sizeof(cmd),
577+
"git -C \"%s\" status --porcelain=v1 -z --untracked-files=all 2>%s",
578+
validated_root, null_dev);
579+
if (n < 0 || n >= (int)sizeof(cmd)) {
580+
return CBM_NOT_FOUND;
581+
}
582+
583+
FILE *fp = cbm_popen(cmd, "r");
584+
if (!fp) {
585+
return 0; /* out is zeroed: available=false */
586+
}
587+
588+
status_parser_t parser;
589+
memset(&parser, 0, sizeof(parser));
590+
parser.out = out;
591+
parser.max_samples = max_samples;
592+
parser.parse_ok = true;
593+
594+
char chunk[STATUS_CHUNK_MAX];
595+
size_t got;
596+
bool io_error = false;
597+
while (parser.parse_ok && (got = fread(chunk, 1, sizeof(chunk), fp)) > 0) {
598+
status_feed(&parser, chunk, got);
599+
}
600+
if (ferror(fp)) {
601+
io_error = true;
602+
}
603+
int rc = cbm_pclose(fp);
604+
605+
if (rc != 0 || !parser.parse_ok || io_error || parser.partial_len > 0) {
606+
/* Fail closed: any deviation from a clean, complete status stream is a
607+
* machine-readable "unavailable", never a clean worktree. */
608+
cbm_git_worktree_status_free(out);
609+
return 0;
610+
}
611+
612+
out->available = true;
613+
return 0;
614+
}
615+
616+
void cbm_git_worktree_status_free(cbm_worktree_status_t *st) {
617+
if (!st) {
618+
return;
619+
}
620+
for (int i = 0; i < st->tracked_sample_count; i++) {
621+
free(st->tracked_paths[i]);
622+
}
623+
free(st->tracked_paths);
624+
for (int i = 0; i < st->untracked_sample_count; i++) {
625+
free(st->untracked_paths[i]);
626+
}
627+
free(st->untracked_paths);
628+
memset(st, 0, sizeof(*st));
629+
}

‎src/git/git_context.h‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,40 @@ void cbm_git_context_free(cbm_git_context_t *ctx);
2424
char *cbm_git_context_branch_qn(const char *project_name, const cbm_git_context_t *ctx);
2525
int cbm_git_context_props_json(const cbm_git_context_t *ctx, char *buf, int buf_size);
2626

27+
/* Bounded snapshot of the git worktree status for one validated repo root.
28+
*
29+
* available is true ONLY when `git status --porcelain=v1 -z --untracked-files=all`
30+
* exited 0 AND every NUL-delimited record parsed cleanly. Any other outcome —
31+
* shell-unsafe path, spawn failure, nonzero exit, malformed/oversized record,
32+
* trailing partial record — leaves available=false with zero counts and no
33+
* samples. Callers MUST treat !available as "status unavailable", never as a
34+
* clean worktree.
35+
*
36+
* The entire stream is counted, but only max_samples paths per class (tracked
37+
* / untracked) are ever retained, in stream order; exceeding the cap sets the
38+
* matching *_truncated flag. Rename/copy records are counted once (their two
39+
* NUL-separated paths are consumed without counting the second), and the
40+
* sampled path is the record's NEW path. Ignored files never appear because
41+
* --ignored is not requested.
42+
*
43+
* cbm_git_worktree_status returns 0 when *out has been filled (available tells
44+
* whether the snapshot succeeded) and CBM_NOT_FOUND for invalid arguments
45+
* (NULL root/out or max_samples < 0). The caller owns the returned samples via
46+
* cbm_git_worktree_status_free. */
47+
typedef struct {
48+
bool available;
49+
int tracked_count;
50+
int untracked_count;
51+
char **tracked_paths; /* heap-owned samples, tracked_sample_count entries */
52+
int tracked_sample_count;
53+
bool tracked_truncated;
54+
char **untracked_paths;
55+
int untracked_sample_count;
56+
bool untracked_truncated;
57+
} cbm_worktree_status_t;
58+
59+
int cbm_git_worktree_status(const char *validated_root, int max_samples,
60+
cbm_worktree_status_t *out);
61+
void cbm_git_worktree_status_free(cbm_worktree_status_t *st);
62+
2763
#endif

0 commit comments

Comments
 (0)