mirror of
https://github.com/PR0M3TH3AN/TORCH.git
synced 2026-10-27 07:51:52 +00:00
Refactor memory scripts and fix configuration for new installs
- Replaced fragile inline Node.js scripts in `torch-config.json` with dedicated modules: `scripts/memory/retrieve.mjs` and `scripts/memory/store.mjs`. - Updated `src/ops.mjs` initialization logic to dynamically configure `scheduler.memoryPolicyByCadence` with correct paths based on the installation directory. - Added default memory policy configuration to `torch-config.example.json` to guide users. - Injected `torch:memory:list` and `torch:memory:inspect` convenience scripts into the host `package.json` during `init`. - Removed legacy shell scripts (`run-memory-retrieval.sh`, `run-memory-store.sh`). - Verified memory scripts function correctly and pass linting. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
ea5229d5d7
commit
d23e6ff0b3
84 files changed
+263
-693
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { ingestEvents, getRelevantMemories } from '../../src/services/memory/index.js';
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
// Parse environment variables
|
||||
const fallbackCadence = 'daily';
|
||||
const cadence = process.env.SCHEDULER_CADENCE || fallbackCadence;
|
||||
const agentId = process.env.SCHEDULER_AGENT || `scheduler-memory-${cadence}`;
|
||||
const promptPath = process.env.SCHEDULER_PROMPT_PATH || '';
|
||||
const runId = process.env.SCHEDULER_RUN_ID ||
|
||||
process.env.SCHEDULER_SESSION_ID ||
|
||||
process.env.RUN_ID ||
|
||||
`session-${Date.now().toString(36)}`;
|
||||
|
||||
// Extract prompt intent
|
||||
let promptIntent = `scheduler memory retrieval`;
|
||||
if (promptPath) {
|
||||
try {
|
||||
const promptRaw = readFileSync(promptPath, 'utf8');
|
||||
const promptLines = promptRaw.split(/\r?\n/).map(line => line.trim()).filter(Boolean);
|
||||
const intentLine = promptLines.find(line => line.startsWith('#') || line.toLowerCase().startsWith('goal'));
|
||||
if (intentLine) {
|
||||
promptIntent = intentLine.replace(/^#+\s*/, '');
|
||||
} else if (promptLines.length > 0) {
|
||||
promptIntent = promptLines[0];
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(`Could not read prompt file: ${promptPath}`, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Construct query
|
||||
const query = ['scheduler', cadence, 'agent', agentId, 'intent', promptIntent].join(' | ');
|
||||
const timestamp = Date.now();
|
||||
|
||||
// Create retrieval seed event
|
||||
const events = [{
|
||||
agent_id: agentId,
|
||||
content: `Memory retrieval seed for ${cadence} :: ${promptIntent}`,
|
||||
timestamp,
|
||||
tags: ['scheduler', cadence, 'retrieve'],
|
||||
metadata: {
|
||||
session_id: runId,
|
||||
source: 'scheduler-retrieve',
|
||||
importance: 0.4,
|
||||
prompt_path: promptPath
|
||||
}
|
||||
}];
|
||||
|
||||
try {
|
||||
// 1. Ingest seed event
|
||||
const ingested = await ingestEvents(events, { agent_id: agentId });
|
||||
|
||||
// 2. Retrieve memories
|
||||
const retrieved = await getRelevantMemories({ agent_id: agentId, query, k: 5 });
|
||||
|
||||
// 3. Prepare artifacts
|
||||
const sessionDir = path.join('.scheduler-memory', runId);
|
||||
const latestDir = path.join('.scheduler-memory', 'latest', cadence);
|
||||
|
||||
mkdirSync(sessionDir, { recursive: true });
|
||||
mkdirSync(latestDir, { recursive: true });
|
||||
|
||||
const artifact = {
|
||||
cadence,
|
||||
operation: 'retrieve',
|
||||
runId,
|
||||
servicePath: 'src/services/memory/index.js#getRelevantMemories',
|
||||
inputs: {
|
||||
agentId,
|
||||
promptPath,
|
||||
promptIntent,
|
||||
query,
|
||||
events: events.length
|
||||
},
|
||||
outputs: {
|
||||
ingestedCount: ingested.length,
|
||||
retrievedCount: retrieved.length
|
||||
},
|
||||
status: 'ok'
|
||||
};
|
||||
|
||||
// 4. Write artifacts
|
||||
writeFileSync(path.join(sessionDir, 'retrieve.json'), JSON.stringify(artifact, null, 2));
|
||||
writeFileSync(path.join(sessionDir, 'retrieve.ok'), 'MEMORY_RETRIEVED\n');
|
||||
writeFileSync(path.join(latestDir, 'retrieve.ok'), 'MEMORY_RETRIEVED\n');
|
||||
writeFileSync(path.join(latestDir, 'memories.md'), retrieved.map(m => `- ${m.content}`).join('\n'));
|
||||
|
||||
// 5. Output success marker to stdout (for scheduler verification)
|
||||
console.log('MEMORY_RETRIEVED');
|
||||
} catch (error) {
|
||||
console.error('Memory retrieval failed:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { ingestEvents } from '../../src/services/memory/index.js';
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
// Parse environment variables
|
||||
const fallbackCadence = 'weekly';
|
||||
const cadence = process.env.SCHEDULER_CADENCE || fallbackCadence;
|
||||
const agentId = process.env.SCHEDULER_AGENT || `scheduler-memory-${cadence}`;
|
||||
const promptPath = process.env.SCHEDULER_PROMPT_PATH || '';
|
||||
const runId = process.env.SCHEDULER_RUN_ID ||
|
||||
process.env.SCHEDULER_SESSION_ID ||
|
||||
process.env.RUN_ID ||
|
||||
`session-${Date.now().toString(36)}`;
|
||||
|
||||
// Extract prompt intent
|
||||
let promptIntent = `scheduler memory store`;
|
||||
if (promptPath) {
|
||||
try {
|
||||
const promptRaw = readFileSync(promptPath, 'utf8');
|
||||
const promptLines = promptRaw.split(/\r?\n/).map(line => line.trim()).filter(Boolean);
|
||||
const intentLine = promptLines.find(line => line.startsWith('#') || line.toLowerCase().startsWith('goal'));
|
||||
if (intentLine) {
|
||||
promptIntent = intentLine.replace(/^#+\s*/, '');
|
||||
} else if (promptLines.length > 0) {
|
||||
promptIntent = promptLines[0];
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(`Could not read prompt file: ${promptPath}`, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
const baseTs = Date.now();
|
||||
const events = [
|
||||
{
|
||||
agent_id: agentId,
|
||||
content: `Store memory event A for ${cadence} :: ${promptIntent}`,
|
||||
timestamp: baseTs,
|
||||
tags: ['scheduler', cadence, 'store'],
|
||||
metadata: {
|
||||
session_id: runId,
|
||||
source: 'scheduler-store',
|
||||
importance: 0.55,
|
||||
prompt_path: promptPath
|
||||
}
|
||||
},
|
||||
{
|
||||
agent_id: agentId,
|
||||
content: `Store memory event B for ${cadence} :: ${promptIntent}`,
|
||||
timestamp: baseTs + 1,
|
||||
tags: ['scheduler', cadence, 'store'],
|
||||
metadata: {
|
||||
session_id: runId,
|
||||
source: 'scheduler-store',
|
||||
importance: 0.55,
|
||||
prompt_path: promptPath
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
try {
|
||||
// 1. Ingest events
|
||||
const stored = await ingestEvents(events, { agent_id: agentId });
|
||||
|
||||
// 2. Prepare artifacts
|
||||
const sessionDir = path.join('.scheduler-memory', runId);
|
||||
const latestDir = path.join('.scheduler-memory', 'latest', cadence);
|
||||
|
||||
mkdirSync(sessionDir, { recursive: true });
|
||||
mkdirSync(latestDir, { recursive: true });
|
||||
|
||||
const artifact = {
|
||||
cadence,
|
||||
operation: 'store',
|
||||
runId,
|
||||
servicePath: 'src/services/memory/index.js#ingestEvents',
|
||||
inputs: {
|
||||
agentId,
|
||||
promptPath,
|
||||
promptIntent,
|
||||
events: events.length
|
||||
},
|
||||
outputs: {
|
||||
storedCount: stored.length,
|
||||
summaries: stored.map(m => m.summary)
|
||||
},
|
||||
status: 'ok'
|
||||
};
|
||||
|
||||
// 3. Write artifacts
|
||||
writeFileSync(path.join(sessionDir, 'store.json'), JSON.stringify(artifact, null, 2));
|
||||
writeFileSync(path.join(sessionDir, 'store.ok'), 'MEMORY_STORED\n');
|
||||
writeFileSync(path.join(latestDir, 'store.ok'), 'MEMORY_STORED\n');
|
||||
|
||||
// 4. Output success marker to stdout (for scheduler verification)
|
||||
console.log('MEMORY_STORED');
|
||||
} catch (error) {
|
||||
console.error('Memory storage failed:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
set -euo pipefail
|
||||
node --input-type=module -e "import { ingestEvents, getRelevantMemories } from './src/services/memory/index.js'; import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; const fallbackCadence='weekly'; const cadence=process.env.SCHEDULER_CADENCE || fallbackCadence; const agentId=process.env.SCHEDULER_AGENT || ('scheduler-memory-' + cadence); const promptPath=process.env.SCHEDULER_PROMPT_PATH || ''; const runId=(process.env.SCHEDULER_RUN_ID || process.env.SCHEDULER_SESSION_ID || process.env.RUN_ID || ('session-' + Date.now().toString(36))); const promptRaw=promptPath ? readFileSync(promptPath, 'utf8') : ''; const promptLines=promptRaw.split(/\\r?\\n/).map((line)=>line.trim()).filter(Boolean); const promptIntent=promptLines.find((line)=>line.startsWith('#') || line.toLowerCase().startsWith('goal')) || promptLines[0] || 'scheduler memory retrieval'; const query=['scheduler', cadence, 'agent', agentId, 'intent', promptIntent.replace(/^#+\\s*/, '')].join(' | '); const timestamp=Date.now(); const events=[{ agent_id: agentId, content: 'Memory retrieval seed for ' + cadence + ' :: ' + promptIntent, timestamp, tags:['scheduler', cadence, 'retrieve'], metadata:{ session_id:runId, source:'scheduler-retrieve', importance:0.4, prompt_path:promptPath } }]; const ingested=await ingestEvents(events,{ agent_id: agentId }); const retrieved=await getRelevantMemories({ agent_id: agentId, query, k: 5 }); const sessionDir='.scheduler-memory/' + runId; const latestDir='.scheduler-memory/latest/' + cadence; mkdirSync(sessionDir,{ recursive:true }); mkdirSync(latestDir,{ recursive:true }); const artifact={ cadence, operation:'retrieve', runId, servicePath:'src/services/memory/index.js#getRelevantMemories', inputs:{ agentId, promptPath, promptIntent, query, events:events.length }, outputs:{ ingestedCount:ingested.length, retrievedCount:retrieved.length }, status:'ok' }; writeFileSync(sessionDir + '/retrieve.json', JSON.stringify(artifact, null, 2)); writeFileSync(sessionDir + '/retrieve.ok', 'MEMORY_RETRIEVED\\n'); writeFileSync(latestDir + '/retrieve.ok', 'MEMORY_RETRIEVED\\n');"
|
||||
echo MEMORY_RETRIEVED
|
||||
@@ -1,3 +0,0 @@
|
||||
set -euo pipefail
|
||||
node --input-type=module -e "import { ingestEvents } from './src/services/memory/index.js'; import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; const fallbackCadence='weekly'; const cadence=process.env.SCHEDULER_CADENCE || fallbackCadence; const agentId=process.env.SCHEDULER_AGENT || ('scheduler-memory-' + cadence); const promptPath=process.env.SCHEDULER_PROMPT_PATH || ''; const runId=(process.env.SCHEDULER_RUN_ID || process.env.SCHEDULER_SESSION_ID || process.env.RUN_ID || ('session-' + Date.now().toString(36))); const promptRaw=promptPath ? readFileSync(promptPath, 'utf8') : ''; const promptLines=promptRaw.split(/\\r?\\n/).map((line)=>line.trim()).filter(Boolean); const promptIntent=promptLines.find((line)=>line.startsWith('#') || line.toLowerCase().startsWith('goal')) || promptLines[0] || 'scheduler memory store'; const baseTs=Date.now(); const events=[{ agent_id: agentId, content: 'Store memory event A for ' + cadence + ' :: ' + promptIntent, timestamp: baseTs, tags:['scheduler', cadence, 'store'], metadata:{ session_id:runId, source:'scheduler-store', importance:0.55, prompt_path:promptPath } },{ agent_id: agentId, content: 'Store memory event B for ' + cadence + ' :: ' + promptIntent, timestamp: baseTs + 1, tags:['scheduler', cadence, 'store'], metadata:{ session_id:runId, source:'scheduler-store', importance:0.55, prompt_path:promptPath } }]; const stored=await ingestEvents(events,{ agent_id: agentId }); const sessionDir='.scheduler-memory/' + runId; const latestDir='.scheduler-memory/latest/' + cadence; mkdirSync(sessionDir,{ recursive:true }); mkdirSync(latestDir,{ recursive:true }); const artifact={ cadence, operation:'store', runId, servicePath:'src/services/memory/index.js#ingestEvents', inputs:{ agentId, promptPath, promptIntent, events:events.length }, outputs:{ storedCount:stored.length, summaries:stored.map((m)=>m.summary) }, status:'ok' }; writeFileSync(sessionDir + '/store.json', JSON.stringify(artifact, null, 2)); writeFileSync(sessionDir + '/store.ok', 'MEMORY_STORED\\n'); writeFileSync(latestDir + '/store.ok', 'MEMORY_STORED\\n');"
|
||||
echo MEMORY_STORED
|
||||
Reference in new issue
Block a user