MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / detect_cursor_decode

Function detect_cursor_decode

src/mcp/mcp.c:18189–18243  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18187}
18188
18189static const char *detect_cursor_decode(const char *token, char expected_stream,
18190 const char current_snapshot[33], uint64_t expected_qhash,
18191 detect_cursor_t *out) {
18192 memset(out, 0, sizeof(*out));
18193 if (!token || strncmp(token, "d1.", 3) != 0 || token[3] != expected_stream || token[4] != '.') {
18194 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18195 }
18196 out->stream = token[3];
18197 const char *snapshot_start = token + 5;
18198 const char *snapshot_end = strchr(snapshot_start, '.');
18199 if (!snapshot_end || snapshot_end - snapshot_start != 32) {
18200 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18201 }
18202 for (const char *digit = snapshot_start; digit < snapshot_end; digit++) {
18203 if (!isxdigit((unsigned char)*digit)) {
18204 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18205 }
18206 }
18207 memcpy(out->snapshot, snapshot_start, 32);
18208 out->snapshot[32] = '\0';
18209
18210 const char *hash_start = snapshot_end + 1;
18211 const char *hash_end = strchr(hash_start, '.');
18212 if (!hash_end || hash_end - hash_start != 16) {
18213 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18214 }
18215 for (const char *digit = hash_start; digit < hash_end; digit++) {
18216 if (!isxdigit((unsigned char)*digit)) {
18217 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18218 }
18219 }
18220 errno = 0;
18221 char *parsed_end = NULL;
18222 unsigned long long parsed_hash = strtoull(hash_start, &parsed_end, 16);
18223 if (errno == ERANGE || parsed_end != hash_end) {
18224 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18225 }
18226 errno = 0;
18227 long parsed_offset = strtol(hash_end + 1, &parsed_end, 10);
18228 if (errno == ERANGE || parsed_end == hash_end + 1 || *parsed_end != '\0' || parsed_offset < 1 ||
18229 parsed_offset > INT_MAX) {
18230 return "invalid_cursor: unrecognized detect_changes cursor — rerun without the cursor";
18231 }
18232 out->qhash = (uint64_t)parsed_hash;
18233 out->offset = (int)parsed_offset;
18234 if (out->qhash != expected_qhash) {
18235 return "cursor_params_mismatch: detect_changes cursor belongs to different semantic "
18236 "arguments — rerun without the cursor";
18237 }
18238 if (strcmp(out->snapshot, current_snapshot) != 0) {
18239 return "snapshot_changed: commits, worktree, or graph changed since this cursor was issued "
18240 "— rerun detect_changes without the cursor";
18241 }
18242 return NULL;
18243}
18244
18245static void detect_snapshot_add_field(cbm_sha256_ctx *hash, const char *value) {
18246 size_t length = value ? strlen(value) : 0;

Callers 1

handle_detect_changesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected