32 lines
2.1 KiB
JavaScript
32 lines
2.1 KiB
JavaScript
// The brain can issue both signed instructions for the same inference cycle.
|
|
// The context agent prepares the next model window while the action agent
|
|
// handles the current read. Their receipts return to the brain for judgment.
|
|
import { worldRuntimeUrl } from '../../WORLD-RUNTIME-BRIDGE.mjs';
|
|
const { verifyBrainIntegrity } = await import(worldRuntimeUrl('brain-core.mjs'));
|
|
import { validateAgentInstruction } from './shuangyan-agent-protocol.mjs';
|
|
import { prepareNextContext } from './shuangyan-context-agent.mjs';
|
|
import { executeReadOnlyAction } from './shuangyan-action-agent.mjs';
|
|
|
|
export async function runShuangyanAgentCycle({ brainState, brainLog, contextInstruction,
|
|
actionInstruction, key, notionRoot, model = null, priorReceipts = [] } = {}) {
|
|
if (verifyBrainIntegrity(brainState, brainLog).status !== 'BRAIN_VERIFIED')
|
|
throw new Error('SHUANGYAN_AGENT_BRAIN_UNVERIFIED');
|
|
validateAgentInstruction(contextInstruction, { key, brainState });
|
|
validateAgentInstruction(actionInstruction, { key, brainState });
|
|
if (contextInstruction.cycle_id !== actionInstruction.cycle_id ||
|
|
contextInstruction.instruction_id === actionInstruction.instruction_id ||
|
|
contextInstruction.agent_role !== 'CONTEXT_PREPARER' ||
|
|
actionInstruction.agent_role !== 'ACTION_EXECUTOR')
|
|
throw new Error('SHUANGYAN_AGENT_CYCLE_INVALID');
|
|
const results = await Promise.allSettled([
|
|
prepareNextContext(contextInstruction, { key, brainState, model, priorReceipts }),
|
|
executeReadOnlyAction(actionInstruction, { key, brainState, notionRoot, model }),
|
|
]);
|
|
return { schema: 'guanghu.shuangyan-dual-agent-cycle-receipt/v1',
|
|
cycle_id: contextInstruction.cycle_id, brain_id: brainState.brain_id,
|
|
brain_revision: brainState.revision, results: results.map((item, index) => item.status === 'fulfilled'
|
|
? item.value : { agent_role: index === 0 ? 'CONTEXT_PREPARER' : 'ACTION_EXECUTOR',
|
|
outcome: 'HELD', error: String(item.reason?.message || item.reason),
|
|
neural_state_written: false, reality_write_authority_granted: false }),
|
|
brain_review_required: true, neural_state_written: false };
|
|
}
|