Skip to content

Commit f360ecf

Browse files
Update jellyfish.c
1 parent ce50e28 commit f360ecf

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

code/logic/jellyfish.c

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdio.h>
1818
#include <stdlib.h>
1919
#include <ctype.h>
20+
#include <float.h>
2021
#include <time.h>
2122
#include <math.h>
2223

@@ -180,6 +181,44 @@ int parse_hex_field(const char **ptr, const char *key, uint8_t *out, size_t len)
180181
return skip_symbol(ptr, '"');
181182
}
182183

184+
int skip_key_value(const char **ptr, const char *key, const char *expected_value) {
185+
const char *p = *ptr;
186+
187+
// Skip optional whitespace
188+
while (isspace((unsigned char)*p)) p++;
189+
190+
// Match key
191+
size_t key_len = strlen(key);
192+
if (strncmp(p, "\"", 1) != 0) return 0;
193+
p++;
194+
195+
if (strncmp(p, key, key_len) != 0) return 0;
196+
p += key_len;
197+
198+
if (strncmp(p, "\"", 1) != 0) return 0;
199+
p++;
200+
201+
// Skip colon and optional whitespace
202+
while (isspace((unsigned char)*p)) p++;
203+
if (*p != ':') return 0;
204+
p++;
205+
while (isspace((unsigned char)*p)) p++;
206+
207+
// Match expected value
208+
size_t val_len = strlen(expected_value);
209+
if (strncmp(p, "\"", 1) != 0) return 0;
210+
p++;
211+
212+
if (strncmp(p, expected_value, val_len) != 0) return 0;
213+
p += val_len;
214+
215+
if (strncmp(p, "\"", 1) != 0) return 0;
216+
p++;
217+
218+
*ptr = p; // Update pointer if match succeeded
219+
return 1;
220+
}
221+
183222
static bool parse_string_field(const char **ptr, const char *key, char *out, size_t max) {
184223
if (!skip_key(ptr, key)) return false;
185224
if (!skip_symbol(ptr, '"')) return false;
@@ -612,31 +651,31 @@ const char* fossil_jellyfish_reason(fossil_jellyfish_chain *chain, const char *i
612651
}
613652

614653
void fossil_jellyfish_decay_confidence(fossil_jellyfish_chain *chain, float decay_rate) {
615-
if (!chain || chain->count == 0) return;
654+
if (!chain || chain->count == 0 || decay_rate <= 0.0f) return;
616655

617656
const float MIN_CONFIDENCE = 0.05f;
618657
const float MAX_CONFIDENCE = 1.0f;
619658

620-
// Half-life of confidence in seconds (e.g. 24 hours = 86400s)
621-
const double HALF_LIFE_SECONDS = 86400.0;
659+
// decay_rate here represents the half-life in seconds
660+
// To avoid too small or too large half-life, clamp decay_rate reasonably
661+
double half_life_seconds = fmax(1.0, (double)decay_rate);
622662

623-
// Current time in seconds
624663
time_t now = time(NULL);
625664

626665
for (size_t i = 0; i < chain->count; ++i) {
627666
fossil_jellyfish_block *block = &chain->memory[i];
628667
if (!block->valid) continue;
629668

630-
// Convert timestamps to seconds
631-
time_t block_time = (time_t)(block->timestamp / 1000); // assuming ms
669+
time_t block_time = (time_t)(block->timestamp / 1000);
632670
time_t age_seconds = now - block_time;
633-
if (age_seconds <= 0) continue; // future or zero-age blocks aren't decayed
634671

635-
// Compute decay factor: confidence *= 0.5^(age / half-life)
636-
double decay_factor = pow(0.5, (double)age_seconds / HALF_LIFE_SECONDS);
672+
if (age_seconds <= 0) continue;
673+
674+
// Compute decay factor:
675+
// confidence *= 0.5 ^ (age / half_life)
676+
double decay_factor = pow(0.5, (double)age_seconds / half_life_seconds);
637677
block->confidence *= (float)decay_factor;
638678

639-
// Clamp and check
640679
block->confidence = fmaxf(0.0f, fminf(block->confidence, MAX_CONFIDENCE));
641680
if (block->confidence < MIN_CONFIDENCE) {
642681
block->valid = 0;

0 commit comments

Comments
 (0)