174 lines
5.5 KiB
JavaScript
174 lines
5.5 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
|
// Agent Runtime v5 — Med Operation Canonicalization Layer
|
||
|
|
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
|
|
||
|
|
import { RuntimeTraceV2 } from './runtime-trace-v2.mjs';
|
||
|
|
import { canonicalizeOperation, checkOperationPolicy } from './operation-canonicalizer.mjs';
|
||
|
|
import { getPolicy } from './policy-registry.mjs';
|
||
|
|
|
||
|
|
class AgentRuntimeV5 {
|
||
|
|
constructor(task) {
|
||
|
|
this.task = task;
|
||
|
|
this.trace = new RuntimeTraceV2();
|
||
|
|
this.policies = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
async execute() {
|
||
|
|
// 1. PLANNER
|
||
|
|
await this.runPlanner();
|
||
|
|
|
||
|
|
// 2. CONTEXT
|
||
|
|
await this.runContext();
|
||
|
|
|
||
|
|
// 3. MEMORY
|
||
|
|
await this.runMemory();
|
||
|
|
|
||
|
|
// 4. KNOWLEDGE
|
||
|
|
await this.runKnowledge();
|
||
|
|
|
||
|
|
// 5. INTENT CLASSIFICATION — Operation Canonicalization
|
||
|
|
const canonicalResult = await this.runIntentClassification();
|
||
|
|
|
||
|
|
// 6. EOS — Policy Check baserat på kanonisk operation
|
||
|
|
const eosResult = await this.runEOSCheck(canonicalResult);
|
||
|
|
if (!eosResult.passed) {
|
||
|
|
return {
|
||
|
|
status: 'blocked',
|
||
|
|
reason: eosResult.reason,
|
||
|
|
policy: eosResult.blockedBy,
|
||
|
|
evidence: eosResult.evidence
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
// 7. DEVELOPER
|
||
|
|
await this.runDeveloper();
|
||
|
|
|
||
|
|
// 8. REVIEWER
|
||
|
|
await this.runReviewer();
|
||
|
|
|
||
|
|
// 9. COMMIT
|
||
|
|
await this.runCommit();
|
||
|
|
|
||
|
|
// 10. OPERATOR
|
||
|
|
await this.runOperator();
|
||
|
|
|
||
|
|
return { status: 'completed', trace: this.trace.toJSON() };
|
||
|
|
}
|
||
|
|
|
||
|
|
async runPlanner() {
|
||
|
|
const node = this.trace.addNode('PLANNER', { task: this.task.description });
|
||
|
|
this.trace.completeNode(node.id, { plan: { steps: [], estimatedTime: null, dependencies: [] } });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runContext() {
|
||
|
|
const node = this.trace.addNode('CONTEXT', { task: this.task.description });
|
||
|
|
this.trace.completeNode(node.id, { confidence: 0.8, git: { branch: null, uncommitted: null, remote: null } });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runMemory() {
|
||
|
|
const node = this.trace.addNode('MEMORY', { queries: [] });
|
||
|
|
this.trace.completeNode(node.id, { results: { relevantDecisions: [], similarTasks: [], lessonsLearned: [] } });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runKnowledge() {
|
||
|
|
const node = this.trace.addNode('KNOWLEDGE', { queries: [] });
|
||
|
|
this.trace.completeNode(node.id, { results: { relatedCapabilities: [], dependencies: [], impact: [] } });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runIntentClassification() {
|
||
|
|
const node = this.trace.addNode('INTENT', { task: this.task.description });
|
||
|
|
|
||
|
|
const result = canonicalizeOperation(this.task);
|
||
|
|
|
||
|
|
this.trace.completeNode(node.id, {
|
||
|
|
operation: result.operation.id,
|
||
|
|
confidence: result.confidence,
|
||
|
|
evidence: result.evidence
|
||
|
|
});
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
async runEOSCheck(canonicalResult) {
|
||
|
|
const node = this.trace.addNode('EOS', { operation: canonicalResult.operation.id });
|
||
|
|
|
||
|
|
// Kör operation-baserad policy
|
||
|
|
const policyResult = checkOperationPolicy(canonicalResult);
|
||
|
|
|
||
|
|
if (!policyResult.passed) {
|
||
|
|
const policyInfo = getPolicy(policyResult.policyId);
|
||
|
|
|
||
|
|
this.trace.blockNode(node.id, policyResult.reason, {
|
||
|
|
policyId: policyResult.policyId,
|
||
|
|
policyName: policyInfo?.name || 'Unknown Policy',
|
||
|
|
evidence: policyResult.evidence,
|
||
|
|
riskClass: policyResult.severity,
|
||
|
|
requiredAction: `Använd godkänd process: ${policyInfo?.description || 'Kontakta EOS Team'}`
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
passed: false,
|
||
|
|
blockedBy: policyResult.rule,
|
||
|
|
reason: policyResult.reason,
|
||
|
|
severity: policyResult.severity,
|
||
|
|
evidence: policyResult.evidence
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
// Kör traditionella policyer (om några finns)
|
||
|
|
for (const policy of this.policies) {
|
||
|
|
const result = policy(this.task);
|
||
|
|
|
||
|
|
if (!result.passed) {
|
||
|
|
const policyInfo = getPolicy(result.policyId);
|
||
|
|
|
||
|
|
this.trace.blockNode(node.id, result.reason, {
|
||
|
|
policyId: result.policyId,
|
||
|
|
policyName: policyInfo?.name || 'Unknown Policy',
|
||
|
|
evidence: result.evidence,
|
||
|
|
riskClass: result.severity,
|
||
|
|
requiredAction: `Använd godkänd process: ${policyInfo?.description || 'Kontakta EOS Team'}`
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
passed: false,
|
||
|
|
blockedBy: result.rule,
|
||
|
|
reason: result.reason,
|
||
|
|
severity: result.severity,
|
||
|
|
evidence: result.evidence
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
this.trace.completeNode(node.id, { passed: true, operation: canonicalResult.operation.id });
|
||
|
|
return { passed: true };
|
||
|
|
}
|
||
|
|
|
||
|
|
async runDeveloper() {
|
||
|
|
const node = this.trace.addNode('DEVELOPER', { task: this.task.description });
|
||
|
|
this.trace.completeNode(node.id, { status: 'completed' });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runReviewer() {
|
||
|
|
const node = this.trace.addNode('REVIEWER', { task: this.task.description });
|
||
|
|
this.trace.completeNode(node.id, { status: 'completed' });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runCommit() {
|
||
|
|
const node = this.trace.addNode('COMMIT', { task: this.task.description });
|
||
|
|
this.trace.completeNode(node.id, { status: 'completed' });
|
||
|
|
}
|
||
|
|
|
||
|
|
async runOperator() {
|
||
|
|
const node = this.trace.addNode('OPERATOR', { task: this.task.description });
|
||
|
|
this.trace.completeNode(node.id, { status: 'completed' });
|
||
|
|
}
|
||
|
|
|
||
|
|
getTrace() {
|
||
|
|
return this.trace;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export { AgentRuntimeV5 };
|