Skip to content

Commit 7eea88c

Browse files
authored
Merge pull request #158 from LennartvdM/codex/implement-global-run-counter-and-fifo-history
Limit balance history to last 50 runs
2 parents a4d64b5 + 4b9b91a commit 7eea88c

3 files changed

Lines changed: 65 additions & 38 deletions

File tree

web/app.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
createRadialUrchin,
44
createBalanceHistoryEntry,
55
computeScheduleSignature,
6-
MAX_HISTORY_ENTRIES as BALANCE_HISTORY_LIMIT,
6+
MAX_HISTORY_ROWS as BALANCE_HISTORY_LIMIT,
77
} from './ui/visuals/RadialUrchin.js';
88

99
console.info('[app] app.js loaded');
@@ -104,6 +104,7 @@ const visualsState = {
104104
metaSlot: null,
105105
runLabel: null,
106106
balanceHistory: [],
107+
totalRunCount: 0,
107108
};
108109
let lastVisualPayload = null;
109110
let lastVisualSchedule = null;
@@ -325,21 +326,20 @@ function applyBalanceHistoryToUrchin() {
325326
if (!visualsState.urchin || typeof visualsState.urchin.setBalanceHistory !== 'function') {
326327
return;
327328
}
328-
visualsState.urchin.setBalanceHistory(visualsState.balanceHistory);
329+
visualsState.urchin.setBalanceHistory(
330+
visualsState.balanceHistory,
331+
visualsState.totalRunCount
332+
);
329333
}
330334

331335
function appendActivityBalanceSnapshot(schedule) {
332336
if (!schedule || typeof schedule !== 'object') {
333337
return;
334338
}
335-
const previous =
336-
visualsState.balanceHistory.length > 0
337-
? visualsState.balanceHistory[visualsState.balanceHistory.length - 1]
338-
: null;
339-
const nextRunNumber =
340-
(previous && Number.isFinite(previous.runNumber)
341-
? previous.runNumber
342-
: visualsState.balanceHistory.length) + 1;
339+
const previousTotal = Number.isFinite(visualsState.totalRunCount)
340+
? visualsState.totalRunCount
341+
: 0;
342+
const nextRunNumber = previousTotal + 1;
343343
const signature = computeScheduleSignature(schedule);
344344
const entry = createBalanceHistoryEntry(schedule, {
345345
runNumber: nextRunNumber,
@@ -349,17 +349,18 @@ function appendActivityBalanceSnapshot(schedule) {
349349
if (!entry) {
350350
return;
351351
}
352-
const next = [...visualsState.balanceHistory, entry];
353-
if (next.length > BALANCE_HISTORY_LIMIT) {
354-
next.splice(0, next.length - BALANCE_HISTORY_LIMIT);
355-
}
356-
visualsState.balanceHistory = next;
352+
visualsState.totalRunCount = nextRunNumber;
353+
const nextHistory =
354+
visualsState.balanceHistory.length >= BALANCE_HISTORY_LIMIT
355+
? [...visualsState.balanceHistory.slice(1), entry]
356+
: [...visualsState.balanceHistory, entry];
357+
visualsState.balanceHistory = nextHistory;
357358
if (
358359
visualsState.urchin &&
359360
typeof visualsState.urchin.appendBalanceHistoryEntry === 'function'
360361
) {
361362
try {
362-
visualsState.urchin.appendBalanceHistoryEntry(entry);
363+
visualsState.urchin.appendBalanceHistoryEntry(entry, nextRunNumber);
363364
} catch (error) {
364365
console.warn('[visuals] failed to append balance history entry:', error);
365366
}
@@ -737,7 +738,9 @@ function updateActiveRunLabel() {
737738
}
738739

739740
const entry = runHistory[index];
740-
const runNumber = runHistory.length - index;
741+
const runNumber = Number.isFinite(entry?.runNumber)
742+
? entry.runNumber
743+
: runHistory.length - index;
741744
const parts = [`Run #${runNumber}`];
742745
if (entry.timestamp) {
743746
const formatted = formatHistoryTimestamp(entry.timestamp);
@@ -907,7 +910,12 @@ function renderCalendarHistorySummary() {
907910
});
908911

909912
if (meta) {
910-
const runNumber = activeIndex !== -1 ? runHistory.length - activeIndex : null;
913+
const activeEntry = activeIndex !== -1 ? runHistory[activeIndex] : null;
914+
const runNumber = activeEntry && Number.isFinite(activeEntry.runNumber)
915+
? activeEntry.runNumber
916+
: activeIndex !== -1
917+
? runHistory.length - activeIndex
918+
: null;
911919
const metaParts = [];
912920
if (Number.isFinite(runNumber)) {
913921
metaParts.push(`Run #${runNumber}`);
@@ -1049,6 +1057,9 @@ function recordCalendarHistoryEntry(entry) {
10491057
const visualPayload = resolveVisualPayload(entry.calendarJson || entry.rawResult);
10501058
const normalized = {
10511059
id: entry.id || generateCalendarHistoryId(),
1060+
runNumber: Number.isFinite(visualsState.totalRunCount)
1061+
? visualsState.totalRunCount
1062+
: undefined,
10521063
timestamp: entry.timestamp || new Date().toISOString(),
10531064
archetype: entry.archetype || '',
10541065
seed:

web/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ body {
13461346
flex-direction: column;
13471347
gap: 4px;
13481348
min-height: 0;
1349+
overflow: hidden;
13491350
}
13501351

13511352
.activity-balance-history--dense .activity-balance-history__stack {

web/ui/visuals/RadialUrchin.js

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const SPEED_OPTIONS = [
2525

2626
const HOVER_DELAY = 180;
2727

28-
export const MAX_HISTORY_ENTRIES = 100;
28+
export const MAX_HISTORY_ROWS = 50;
29+
export const MAX_HISTORY_ENTRIES = MAX_HISTORY_ROWS;
2930

3031
export function extractScheduleTimestamp(schedule) {
3132
if (!schedule || typeof schedule !== 'object') {
@@ -122,8 +123,7 @@ export function createBalanceHistoryEntry(schedule, options = {}) {
122123
return null;
123124
}
124125

125-
const previousRunNumber = Number.isFinite(options.runNumber) ? options.runNumber : 1;
126-
const runNumber = previousRunNumber;
126+
const runNumber = Number.isFinite(options.runNumber) ? options.runNumber : 1;
127127
const timestamp = extractScheduleTimestamp(schedule);
128128
const entry = {
129129
id: options.id || `${runNumber}-${Date.now()}`,
@@ -267,8 +267,9 @@ export class RadialUrchin {
267267
this.shareContainer = null;
268268
this.historyView = null;
269269
this.balanceHistory = [];
270+
this.totalRunCount = 0;
270271
this.isHistoryOpen = false;
271-
this.maxHistoryEntries = MAX_HISTORY_ENTRIES;
272+
this.maxHistoryEntries = MAX_HISTORY_ROWS;
272273

273274
this.handleResize = this.handleResize.bind(this);
274275
this.handlePointerMove = this.handlePointerMove.bind(this);
@@ -569,7 +570,7 @@ export class RadialUrchin {
569570
this.updateHistoryUi();
570571
}
571572

572-
setBalanceHistory(entries) {
573+
setBalanceHistory(entries, totalRunCount) {
573574
const next = Array.isArray(entries) ? entries.slice(-this.maxHistoryEntries) : [];
574575
this.balanceHistory = next.map((entry) => ({
575576
...entry,
@@ -580,10 +581,22 @@ export class RadialUrchin {
580581
? entry.activities.map((activity) => ({ ...activity }))
581582
: [],
582583
}));
584+
if (Number.isFinite(totalRunCount)) {
585+
this.totalRunCount = totalRunCount;
586+
} else if (this.balanceHistory.length > 0) {
587+
const latest = this.balanceHistory[this.balanceHistory.length - 1];
588+
if (latest && Number.isFinite(latest.runNumber)) {
589+
this.totalRunCount = latest.runNumber;
590+
} else {
591+
this.totalRunCount = this.balanceHistory.length;
592+
}
593+
} else {
594+
this.totalRunCount = 0;
595+
}
583596
this.updateHistoryUi({ refreshEntries: true });
584597
}
585598

586-
appendBalanceHistoryEntry(entry) {
599+
appendBalanceHistoryEntry(entry, totalRunCount) {
587600
if (!entry || typeof entry !== 'object') {
588601
return;
589602
}
@@ -596,32 +609,34 @@ export class RadialUrchin {
596609
? entry.activities.map((activity) => ({ ...activity }))
597610
: [],
598611
};
599-
const next = [...this.balanceHistory, normalized];
600-
if (next.length > this.maxHistoryEntries) {
601-
next.splice(0, next.length - this.maxHistoryEntries);
602-
}
612+
const next =
613+
this.balanceHistory.length >= this.maxHistoryEntries
614+
? [...this.balanceHistory.slice(1), normalized]
615+
: [...this.balanceHistory, normalized];
603616
this.balanceHistory = next;
617+
if (Number.isFinite(totalRunCount)) {
618+
this.totalRunCount = totalRunCount;
619+
} else if (Number.isFinite(normalized.runNumber)) {
620+
const previousTotal = Number.isFinite(this.totalRunCount) ? this.totalRunCount : 0;
621+
this.totalRunCount = Math.max(previousTotal, normalized.runNumber);
622+
} else {
623+
const previousTotal = Number.isFinite(this.totalRunCount) ? this.totalRunCount : 0;
624+
this.totalRunCount = previousTotal + 1;
625+
}
604626
this.updateHistoryUi({ refreshEntries: true });
605627
}
606628

607629
captureBalanceSnapshot(schedule, signature) {
608-
const previous =
609-
this.balanceHistory.length > 0
610-
? this.balanceHistory[this.balanceHistory.length - 1]
611-
: null;
612-
const runNumber =
613-
(previous && Number.isFinite(previous.runNumber)
614-
? previous.runNumber
615-
: this.balanceHistory.length) + 1;
630+
const nextRunNumber = (Number.isFinite(this.totalRunCount) ? this.totalRunCount : 0) + 1;
616631
const entry = createBalanceHistoryEntry(schedule, {
617-
runNumber,
632+
runNumber: nextRunNumber,
618633
highContrast: this.state.highContrast,
619634
signature,
620635
});
621636
if (!entry) {
622637
return;
623638
}
624-
this.appendBalanceHistoryEntry(entry);
639+
this.appendBalanceHistoryEntry(entry, nextRunNumber);
625640
}
626641

627642
getRunMetaSlot() {

0 commit comments

Comments
 (0)