/**
* M6 Witness Test: Multi-Dimensional Orchestration
*
* Verifies:
* 1. Tool spawning works
* 2. Permission inheritance works
* 3. Multi-step workflow works
* 4. Recursion depth limit works
* 5. Circular dependency detection works
* 6. Error propagation works
*/
import { ConversationManager } from './dist/core/conversation-manager.js';
import { ConversationStore } from './dist/core/conversation-store.js';
const store = new ConversationStore({ dbPath: ':memory:' });
const manager = new ConversationManager(store);
await manager.waitForToolsLoaded();
console.log('=== M6 Witness Test ===\n');
let passCount = 0;
let failCount = 0;
function assert(condition, message) {
if (condition) {
console.log(`✅ PASS: ${message}`);
passCount++;
} else {
console.error(`❌ FAIL: ${message}`);
failCount++;
}
}
// ========================================
// Act 1: Tool Spawning
// ========================================
console.log('\n=== Act 1: Tool Spawning ===');
console.log('Orchestrator spawns data-tool → data-tool creates resource → orchestrator receives success\n');
// First, upgrade orchestrator-tool to level 2 so it can spawn tools that need level 2
console.log('Upgrading orchestrator-tool to level 2...');
await manager.negotiate('m6-test', 'upgrade:orchestrator-tool:level-2', {});
const workflow1 = await manager.negotiate('m6-test', 'workflow', {});
console.log('Workflow result:', JSON.stringify(workflow1, null, 2));
assert(workflow1.success, 'Workflow should succeed');
assert(workflow1.output?.steps === 3, 'Workflow should execute 3 steps');
assert(workflow1.output?.workflow === 'create-validate-read', 'Workflow should be create-validate-read');
// ========================================
// Act 2: Permission Inheritance
// ========================================
console.log('\n=== Act 2: Permission Inheritance ===');
console.log('Orchestrator at level 2 → spawns data-tool (requires level 2) → data-tool inherits level 2\n');
// Reset conversation
const permTest = 'm6-perm-test';
// Verify orchestrator starts at level 1
const status1 = await manager.negotiate(permTest, 'conversation:status');
console.log('Initial permission levels:', status1.output?.toolPermissions || {});
// Upgrade orchestrator to level 2
console.log('Upgrading orchestrator-tool to level 2...');
await manager.negotiate(permTest, 'upgrade:orchestrator-tool:level-2', {});
// Run workflow - orchestrator will spawn data-tool which inherits level 2
const workflow2 = await manager.negotiate(permTest, 'workflow', {});
console.log('Workflow with permission inheritance:', JSON.stringify(workflow2, null, 2));
assert(workflow2.success, 'Workflow should succeed with permission inheritance');
// Check if data-tool inherited permission
const status2 = await manager.negotiate(permTest, 'conversation:status');
console.log('After workflow permission levels:', status2.output?.toolPermissions || {});
const dataToolPerm = status2.output?.toolPermissions?.['data-tool'];
assert(dataToolPerm?.level === 2, 'data-tool should have inherited level 2');
assert(dataToolPerm?.inheritedFrom === 'orchestrator-tool', 'data-tool should show inheritance from orchestrator-tool');
// ========================================
// Act 3: Multi-Step Workflow
// ========================================
console.log('\n=== Act 3: Multi-Step Workflow ===');
console.log('Orchestrator.workflow() → create → validate → read → aggregated result\n');
await manager.negotiate('m6-multi-step', 'upgrade:orchestrator-tool:level-2', {});
const workflow3 = await manager.negotiate('m6-multi-step', 'workflow', {});
assert(workflow3.success, 'Multi-step workflow should succeed');
assert(workflow3.output?.results?.length === 3, 'Should have 3 step results');
assert(workflow3.output?.results?.[0]?.step === 'create', 'Step 1 should be create');
assert(workflow3.output?.results?.[1]?.step === 'validate', 'Step 2 should be validate');
assert(workflow3.output?.results?.[2]?.step === 'read', 'Step 3 should be read');
// ========================================
// Act 4: Recursion Depth Limit
// ========================================
console.log('\n=== Act 4: Recursion Depth Limit ===');
console.log('Tool spawning beyond depth 5 → should fail with depth error\n');
// Upgrade orchestrator-tool first so permission doesn't block the depth test
await manager.negotiate('m6-depth-test', 'upgrade:orchestrator-tool:level-2', {});
// Try to execute workflow at depth 5 (children will spawn at depth 6, which exceeds maxDepth=5)
const depthTest = await manager.negotiate(
'm6-depth-test',
'workflow',
{},
undefined,
{ parentTool: 'test', depth: 5, callChain: ['test'] }
);
console.log('Depth 5 workflow result:', JSON.stringify(depthTest, null, 2));
assert(!depthTest.success, 'Depth 6 spawn should fail');
assert(
depthTest.error?.includes('Maximum tool spawning depth exceeded') ||
depthTest.output?.completedSteps?.[0]?.result?.error?.includes('Maximum tool spawning depth exceeded'),
'Should have depth error message'
);
// ========================================
// Act 5: Circular Dependency Detection
// ========================================
console.log('\n=== Act 5: Circular Dependency Detection ===');
console.log('Tool-A in callChain tries to spawn Tool-A again → should fail with circular error\n');
const circularTest = await manager.negotiate(
'm6-circular-test',
'workflow',
{},
undefined,
{ parentTool: 'orchestrator-tool', depth: 1, callChain: ['orchestrator-tool'] }
);
console.log('Circular dependency test:', JSON.stringify(circularTest, null, 2));
assert(!circularTest.success, 'Circular spawn should fail');
assert(circularTest.error?.includes('Circular dependency detected'), 'Should have circular error message');
// ========================================
// Act 6: Error Propagation
// ========================================
console.log('\n=== Act 6: Error Propagation ===');
console.log('Workflow: create (✅) → delete (requires level 3, has level 2, ❌) → workflow stops\n');
// Upgrade orchestrator to level 2 (not 3) so delete will fail
await manager.negotiate('m6-error-test', 'upgrade:orchestrator-tool:level-2', {});
// Create a custom pipeline that will fail at step 2
const errorTest = await manager.negotiate('m6-error-test', 'pipeline', {
steps: [
{ action: 'create-resource', args: { name: 'test-resource', data: { value: 1 } } },
{ action: 'delete-resource', args: { name: 'test-resource' } }, // Requires level 3
],
});
console.log('Error propagation test:', JSON.stringify(errorTest, null, 2));
assert(!errorTest.success, 'Pipeline should fail when child action fails');
assert(errorTest.output?.completedSteps?.length === 2, 'Should show both attempted steps');
assert(errorTest.output?.completedSteps?.[0]?.result?.success === true, 'Step 1 should succeed');
assert(errorTest.output?.completedSteps?.[1]?.result?.success === false, 'Step 2 should fail');
// ========================================
// Summary
// ========================================
console.log('\n=== M6 Witness Test Summary ===');
console.log(`Total Tests: ${passCount + failCount}`);
console.log(`Passed: ${passCount}`);
console.log(`Failed: ${failCount}`);
if (failCount === 0) {
console.log('\n✅ ALL M6 WITNESS TESTS PASSED\n');
console.log('M6 Multi-Dimensional Orchestration is COMPLETE ✅');
process.exit(0);
} else {
console.error('\n❌ SOME M6 WITNESS TESTS FAILED\n');
process.exit(1);
}