Optimizes the `getLatestFile` function in `scripts/agent/run-scheduler-cycle.mjs` by parallelizing the reading and parsing of log files using `Promise.all`. This replaces the sequential `for...of` loop which caused I/O bottlenecks when processing large numbers of log files.
Measured improvement: ~56% faster (530ms -> 207ms) for 1000 log files.
Also removed a tracked `test_output.log` file that shouldn't be in the repository.
Co-authored-by: PR0M3TH3AN <[email protected]>
Moved the large usage help text from `src/lib.mjs` to `src/constants.mjs` to improve readability and maintainability of the library file.
- Added `USAGE_TEXT` to `src/constants.mjs`.
- Updated `src/lib.mjs` to import and use `USAGE_TEXT`.
- Removed unused `DEFAULT_DASHBOARD_PORT` import from `src/lib.mjs`.
Co-authored-by: PR0M3TH3AN <[email protected]>
Add a test case to `test/memory-formatter.test.mjs` to ensure that `renderPromptWithMemoryContext` handles circular references in `agentContext` by falling back to `String(agentContext)` instead of throwing a `JSON.stringify` error.
This addresses a missing edge case in JSON serialization testing.
Co-authored-by: PR0M3TH3AN <[email protected]>
Adds focused unit tests for `unpinMemory` in `test/memory-pinning.test.mjs` to ensure it correctly updates the repository and clears the cache. Covers both happy path and non-existent ID scenarios.
Co-authored-by: PR0M3TH3AN <[email protected]>
Added comprehensive unit tests for `summarizeHistory` in `src/relay-health.mjs`.
- Covers success rate calculation, window filtering, and all-down duration logic.
- Includes edge cases for empty history and malformed entries.
- Improves test coverage for relay health monitoring logic.
Co-authored-by: PR0M3TH3AN <[email protected]>
Introduced a `resolveIntegerConfig` helper function to handle environment variable checking, parsing, validation, and configuration fallback for integer settings. Updated `getQueryTimeoutMs`, `getPublishTimeoutMs`, `getMinSuccessfulRelayPublishes`, and `getMinActiveRelayPool` to use this helper. This improves code maintainability and consistency.
Co-authored-by: PR0M3TH3AN <[email protected]>
Adds a test case to `test/memory-formatter.test.mjs` to verify that `renderPromptWithMemoryContext` gracefully handles circular references in `agentContext` by falling back to string representation instead of throwing a JSON serialization error. This ensures the edge case at `src/services/memory/formatter.js:125` is covered.
Co-authored-by: PR0M3TH3AN <[email protected]>
Optimize the `verifyMemoryStep` function in `scripts/agent/run-scheduler-cycle.mjs` to check for artifact existence in parallel using `Promise.all` instead of a sequential loop.
Benchmarks indicate a ~10x performance improvement (from ~500ms to ~50ms) when checking for 10 artifacts where none or the last one exists, with no regression in the best-case scenario.
Verified with `test/run-scheduler-cycle-memory-policy.test.mjs`.
Co-authored-by: PR0M3TH3AN <[email protected]>
- Introduced `parseStringList` helper to handle parsing of string arrays from config.
- Replaced `parseRelayList` and `parseRoster` with `parseStringList`.
- Replaced inline parsing of `relays` and `dashboard.relays` with `parseStringList`.
- Unified behavior: empty lists in config now consistently return `null` instead of `[]`.
- Added unit test verifying consistent `null` return for empty lists.
Co-authored-by: PR0M3TH3AN <[email protected]>
Added unit tests to `test/memory-pinning.test.mjs` to cover the `unpinMemory` function.
Tests verify:
- Successful unpinning and cache clearing.
- Handling of non-existent IDs (returns null) and cache clearing.
Co-authored-by: PR0M3TH3AN <[email protected]>
The dashboard previously served the raw `torch-config.json` file, potentially exposing sensitive information like authentication credentials.
This commit modifies the `/torch-config.json` endpoint to:
1. Parse the configuration file.
2. Sanitize the output by removing the `dashboard.auth` field and any unknown fields (using `parseTorchConfig`).
3. Support `TORCH_CONFIG_PATH` environment variable for configuration file location, improving testability and consistency with other components.
Verified with new test `test/dashboard-config-security.test.mjs` and existing auth tests.
Co-authored-by: PR0M3TH3AN <[email protected]>
This patch addresses a security vulnerability where the dashboard server could serve any file from the package root.
Changes:
- Implemented an allowlist in `src/dashboard.mjs` to restrict file access to:
- `dashboard/`
- `assets/`
- `src/docs/`
- `torch-config.json`
- Added `test/dashboard-security.test.mjs` to verify the fix and prevent regressions.
The fix ensures that sensitive files like `package.json` or source code cannot be accessed via the dashboard server.
Co-authored-by: PR0M3TH3AN <[email protected]>
- Update `markMemoryMerged` to accept `options.repository` for better testability.
- Add unit tests in `test/memory-admin.test.mjs` to verify:
- Delegation to repository.
- Cache clearing side effect.
Co-authored-by: PR0M3TH3AN <[email protected]>
Replaced `console.log` with `util.debuglog('torch-memory')` in `src/services/memory/index.js` to prevent stdout pollution. Telemetry and metrics are now logged to stderr only when `NODE_DEBUG=torch-memory` is set, improving library behavior and observability control.
Added regression test `test/memory-telemetry.test.mjs`.
Co-authored-by: PR0M3TH3AN <[email protected]>
- Add `test/memory-pinning.test.mjs` to test `pinMemory` happy path and edge cases.
- Update `getRelevantMemories` in `src/services/memory/index.js` to use `repository.listMemories` when available, enabling isolated unit testing.
- Verified all memory tests pass.
Co-authored-by: PR0M3TH3AN <[email protected]>
This commit addresses the missing authentication vulnerability in the
dashboard server.
Changes:
- Added Basic Authentication support via TORCH_DASHBOARD_AUTH env var
or dashboard.auth config field.
- Defaulted dashboard host binding to 127.0.0.1 for better security.
- Added --host CLI argument to allow overriding the bind address.
- Used crypto.timingSafeEqual for secure credential comparison.
- Fixed a potential crash when parsing malformed Authorization headers.
- Added comprehensive unit tests for dashboard authentication.
- Updated documentation in README.md and torch-config.example.json.
Co-authored-by: PR0M3TH3AN <[email protected]>
Replace inefficient `[...events].reverse().find()` with `events.findLast()`.
This avoids a shallow copy of the array and an in-place reverse,
improving performance from O(N) space/time to O(1) space and O(N) time
(where N is the number of events searched).
In benchmarks with 100,000 events:
- Element near the end: 99.9% improvement (~0.8ms vs ~950ms)
- Element at the beginning: ~42% improvement (~145ms vs ~250ms)
Node.js v18.6.0+ is required for `findLast`, which is supported in the
current environment (v22.22.0).
Co-authored-by: PR0M3TH3AN <[email protected]>
- Added `test/memory-formatter.test.mjs` with comprehensive unit tests for:
- `estimateTokenCount`
- `formatMemoryBlock`
- `formatMemoriesForPrompt`
- `renderPromptWithMemoryContext`
- Verified all tests pass and no regressions in related tests.
- Addressed testing gap in `src/services/memory/formatter.js`.
Co-authored-by: PR0M3TH3AN <[email protected]>
- Extracted `syncAppDirectories` and `syncAppFiles` helper functions.
- Updated `cmdInit` and `cmdUpdate` to use the new helpers.
- Improved `cmdInit` to throw an Error instead of warning when the target directory is not empty and not forced.
- Fixed hanging tests in `test/ops.test.mjs` by providing `MOCK_CONFIG`.
- Updated tests to verify strict directory check in `cmdInit`.
Co-authored-by: PR0M3TH3AN <[email protected]>