mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-11-06 04:42:24 +00:00
Merge pull request #402 from PR0M3TH3AN/agents/daily/const-refactor-13160759941082166389
refactor(constants): centralize time and memory constants
This commit is contained in:
9 files changed
+68
-10
No files matched your search
@@ -22,6 +22,11 @@ export const DEFAULT_SNAPSHOT_INTERVAL_MS = 60_000;
|
||||
export const VALID_CADENCES = new Set(['daily', 'weekly']);
|
||||
export const KIND_APP_DATA = 30078;
|
||||
export const MS_PER_SECOND = 1000;
|
||||
export const MINUTE_MS = 60_000;
|
||||
export const HOUR_MS = 60 * MINUTE_MS;
|
||||
export const DAY_MS = 24 * HOUR_MS;
|
||||
export const WEEK_MS = 7 * DAY_MS;
|
||||
export const MEMORY_CACHE_MAX_EVENTS = 5000;
|
||||
|
||||
/**
|
||||
* Script keys injected into the host package.json during `torch-lock init`.
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# Context for const-refactor-agent run 2026-02-21T02-30-00Z
|
||||
|
||||
Goal: Refactor duplicated constants and magic numbers in src/*.mjs to src/constants.mjs.
|
||||
Scope: src/services/memory/cache.js, src/services/memory/scheduler.js, src/relay-health.mjs.
|
||||
@@ -0,0 +1,11 @@
|
||||
# Decisions for const-refactor-agent run 2026-02-21T02-30-00Z
|
||||
|
||||
## Centralized Time Constants
|
||||
- **Decision**: Added `MINUTE_MS`, `HOUR_MS`, `DAY_MS`, `WEEK_MS` to `src/constants.mjs`.
|
||||
- **Reason**: Multiple files (`src/services/memory/scheduler.js`, `src/relay-health.mjs`, `src/services/memory/cache.js`) were redefining these or using magic numbers like `60_000`.
|
||||
- **Location**: `src/constants.mjs`
|
||||
|
||||
## Centralized Memory Cache Config
|
||||
- **Decision**: Added `MEMORY_CACHE_MAX_EVENTS` (5000) to `src/constants.mjs`.
|
||||
- **Reason**: `src/services/memory/cache.js` was defining a local constant `DEFAULT_MAX_TOTAL_EVENTS`. Centralizing it allows easier configuration and visibility.
|
||||
- **Location**: `src/constants.mjs`
|
||||
@@ -3,7 +3,7 @@ import path from 'node:path';
|
||||
import WebSocket from 'ws';
|
||||
import { finalizeEvent, generateSecretKey, getPublicKey } from 'nostr-tools/pure';
|
||||
import { getNamespace, getRelays } from './torch-config.mjs';
|
||||
import { MS_PER_SECOND } from './constants.mjs';
|
||||
import { MS_PER_SECOND, MINUTE_MS } from './constants.mjs';
|
||||
import { withTimeout } from './utils.mjs';
|
||||
|
||||
const PROBE_KIND = 27235;
|
||||
@@ -13,7 +13,7 @@ function nowIso() {
|
||||
}
|
||||
|
||||
export function summarizeHistory(historyEntries, { nowMs = Date.now(), windowMinutes = 60 } = {}) {
|
||||
const windowStartMs = nowMs - (windowMinutes * 60_000);
|
||||
const windowStartMs = nowMs - (windowMinutes * MINUTE_MS);
|
||||
const recent = historyEntries.filter((entry) => Date.parse(entry.timestamp || '') >= windowStartMs);
|
||||
const relayProbeCount = recent.reduce((sum, entry) => sum + (entry.summary?.totalRelays || 0), 0);
|
||||
const successCount = recent.reduce((sum, entry) => sum + (entry.summary?.healthyRelays || 0), 0);
|
||||
@@ -29,7 +29,7 @@ export function summarizeHistory(historyEntries, { nowMs = Date.now(), windowMin
|
||||
|
||||
const allDownDurationMinutes = lastHealthyAtMs === null
|
||||
? null
|
||||
: Math.max(0, Math.floor((nowMs - lastHealthyAtMs) / 60_000));
|
||||
: Math.max(0, Math.floor((nowMs - lastHealthyAtMs) / MINUTE_MS));
|
||||
|
||||
return {
|
||||
windowMinutes,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { MEMORY_CACHE_MAX_EVENTS, MINUTE_MS } from '../../constants.mjs';
|
||||
|
||||
const DEFAULT_MAX_NAMESPACES = 128;
|
||||
const DEFAULT_MAX_EVENTS_PER_NAMESPACE = 200;
|
||||
const DEFAULT_MAX_TOTAL_EVENTS = 5000;
|
||||
const DEFAULT_MAX_TOTAL_EVENTS = MEMORY_CACHE_MAX_EVENTS;
|
||||
|
||||
function normalizeScope(scope = {}) {
|
||||
const signerId = typeof scope.signer_id === 'string' && scope.signer_id.trim()
|
||||
@@ -150,11 +152,11 @@ export function createMemoryCache(options = {}) {
|
||||
runtimeNamespaces.clear();
|
||||
totalRuntimeEvents = 0;
|
||||
},
|
||||
setRuntimeEvent(scope, event, ttlMs = 60_000) {
|
||||
setRuntimeEvent(scope, event, ttlMs = MINUTE_MS) {
|
||||
const now = Date.now();
|
||||
removeExpired(now);
|
||||
|
||||
const safeTtlMs = Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : 60_000;
|
||||
const safeTtlMs = Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : MINUTE_MS;
|
||||
const expiresAt = now + safeTtlMs;
|
||||
const namespaceKey = getNamespaceKey(scope);
|
||||
const bucket = runtimeNamespaces.get(namespaceKey) ?? { events: [] };
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { getMemoryPruneMode, isMemoryEnabled, isMemoryIngestEnabled } from './feature-flags.js';
|
||||
const MINUTE_MS = 60_000;
|
||||
const HOUR_MS = 60 * MINUTE_MS;
|
||||
const DAY_MS = 24 * HOUR_MS;
|
||||
const WEEK_MS = 7 * DAY_MS;
|
||||
import { MINUTE_MS, HOUR_MS, DAY_MS, WEEK_MS } from '../../constants.mjs';
|
||||
|
||||
const JOBS = {
|
||||
ingestRecentRuntimeEvents: {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# Test Log for const-refactor-agent run 2026-02-21T02-30-00Z
|
||||
|
||||
Commands run:
|
||||
- npm run lint
|
||||
- node --test test/memory-cache.test.mjs
|
||||
- node --test test/memory-scheduler.test.mjs
|
||||
- node --test test/relay-health.test.mjs
|
||||
|
||||
Results:
|
||||
- Lint: Passed (clean)
|
||||
- Tests: All passed (16/16 assertions)
|
||||
@@ -0,0 +1,6 @@
|
||||
# TODO for const-refactor-agent run 2026-02-21T02-30-00Z
|
||||
|
||||
- [ ] Add MINUTE_MS, HOUR_MS, DAY_MS, WEEK_MS, MEMORY_CACHE_MAX_EVENTS to src/constants.mjs
|
||||
- [ ] Refactor src/services/memory/cache.js to use new constants
|
||||
- [ ] Refactor src/services/memory/scheduler.js to use new constants
|
||||
- [ ] Refactor src/relay-health.mjs to use new constants
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
agent: const-refactor-agent
|
||||
cadence: daily
|
||||
platform: jules
|
||||
status: completed
|
||||
---
|
||||
|
||||
# Completed: const-refactor-agent (daily)
|
||||
|
||||
Refactored duplicate constants in:
|
||||
- `src/constants.mjs` (Added time constants and memory cache limit)
|
||||
- `src/services/memory/cache.js`
|
||||
- `src/services/memory/scheduler.js`
|
||||
- `src/relay-health.mjs`
|
||||
|
||||
Artifacts created:
|
||||
- `src/context/CONTEXT_2026-02-21T02-30-00Z.md`
|
||||
- `src/todo/TODO_2026-02-21T02-30-00Z.md`
|
||||
- `src/decisions/DECISIONS_2026-02-21T02-30-00Z.md`
|
||||
- `src/test_logs/TEST_LOG_2026-02-21T02-30-00Z.md`
|
||||
|
||||
Memory retrieved and stored successfully.
|
||||
Reference in new issue
Block a user