magi-ai 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ja.md +377 -0
- package/README.md +377 -0
- package/dist/bin/magi-benchmark.d.ts +14 -0
- package/dist/bin/magi-benchmark.js +93 -0
- package/dist/bin/magi-mcp.d.ts +8 -0
- package/dist/bin/magi-mcp.js +28 -0
- package/dist/bin/magi.d.ts +2 -0
- package/dist/bin/magi.js +634 -0
- package/dist/src/adapters/base.d.ts +34 -0
- package/dist/src/adapters/base.js +149 -0
- package/dist/src/adapters/claude.d.ts +29 -0
- package/dist/src/adapters/claude.js +65 -0
- package/dist/src/adapters/codex.d.ts +21 -0
- package/dist/src/adapters/codex.js +41 -0
- package/dist/src/adapters/gemini.d.ts +18 -0
- package/dist/src/adapters/gemini.js +31 -0
- package/dist/src/adapters/registry.d.ts +19 -0
- package/dist/src/adapters/registry.js +59 -0
- package/dist/src/audit/hash-chain.d.ts +21 -0
- package/dist/src/audit/hash-chain.js +70 -0
- package/dist/src/audit/types.d.ts +25 -0
- package/dist/src/audit/types.js +1 -0
- package/dist/src/audit/writer.d.ts +18 -0
- package/dist/src/audit/writer.js +100 -0
- package/dist/src/benchmark/golden-tasks.d.ts +9 -0
- package/dist/src/benchmark/golden-tasks.js +476 -0
- package/dist/src/benchmark/reporter.d.ts +5 -0
- package/dist/src/benchmark/reporter.js +107 -0
- package/dist/src/benchmark/runner.d.ts +30 -0
- package/dist/src/benchmark/runner.js +224 -0
- package/dist/src/benchmark/scorer.d.ts +12 -0
- package/dist/src/benchmark/scorer.js +124 -0
- package/dist/src/benchmark/types.d.ts +54 -0
- package/dist/src/benchmark/types.js +1 -0
- package/dist/src/cache/deliberation-cache.d.ts +49 -0
- package/dist/src/cache/deliberation-cache.js +127 -0
- package/dist/src/cli/commands/config-cmd.d.ts +11 -0
- package/dist/src/cli/commands/config-cmd.js +190 -0
- package/dist/src/cli/commands/demo.d.ts +12 -0
- package/dist/src/cli/commands/demo.js +66 -0
- package/dist/src/cli/commands/setup.d.ts +7 -0
- package/dist/src/cli/commands/setup.js +182 -0
- package/dist/src/cli/i18n.d.ts +89 -0
- package/dist/src/cli/i18n.js +176 -0
- package/dist/src/cli/interactive-select.d.ts +27 -0
- package/dist/src/cli/interactive-select.js +130 -0
- package/dist/src/cli/tui-setup.d.ts +24 -0
- package/dist/src/cli/tui-setup.js +42 -0
- package/dist/src/config/cli-detector.d.ts +37 -0
- package/dist/src/config/cli-detector.js +99 -0
- package/dist/src/config/user-config.d.ts +81 -0
- package/dist/src/config/user-config.js +134 -0
- package/dist/src/context/auto-collector.d.ts +43 -0
- package/dist/src/context/auto-collector.js +337 -0
- package/dist/src/context/manager.d.ts +35 -0
- package/dist/src/context/manager.js +162 -0
- package/dist/src/context/serializer.d.ts +20 -0
- package/dist/src/context/serializer.js +52 -0
- package/dist/src/demo/recorded-deliberation.d.ts +13 -0
- package/dist/src/demo/recorded-deliberation.js +277 -0
- package/dist/src/engine/angel-detector.d.ts +83 -0
- package/dist/src/engine/angel-detector.js +334 -0
- package/dist/src/engine/at-field.d.ts +40 -0
- package/dist/src/engine/at-field.js +195 -0
- package/dist/src/engine/berserk-orchestrator.d.ts +66 -0
- package/dist/src/engine/berserk-orchestrator.js +378 -0
- package/dist/src/engine/change-metrics.d.ts +56 -0
- package/dist/src/engine/change-metrics.js +214 -0
- package/dist/src/engine/consensus.d.ts +20 -0
- package/dist/src/engine/consensus.js +146 -0
- package/dist/src/engine/dead-sea-scrolls.d.ts +132 -0
- package/dist/src/engine/dead-sea-scrolls.js +610 -0
- package/dist/src/engine/drift-detector.d.ts +39 -0
- package/dist/src/engine/drift-detector.js +225 -0
- package/dist/src/engine/dummy-plug.d.ts +44 -0
- package/dist/src/engine/dummy-plug.js +190 -0
- package/dist/src/engine/engram-manager.d.ts +55 -0
- package/dist/src/engine/engram-manager.js +306 -0
- package/dist/src/engine/events.d.ts +130 -0
- package/dist/src/engine/events.js +44 -0
- package/dist/src/engine/gospel.d.ts +30 -0
- package/dist/src/engine/gospel.js +129 -0
- package/dist/src/engine/hallucination-detector.d.ts +33 -0
- package/dist/src/engine/hallucination-detector.js +215 -0
- package/dist/src/engine/human-resolver.d.ts +19 -0
- package/dist/src/engine/human-resolver.js +89 -0
- package/dist/src/engine/instrumentality.d.ts +64 -0
- package/dist/src/engine/instrumentality.js +297 -0
- package/dist/src/engine/iruel-battle.d.ts +79 -0
- package/dist/src/engine/iruel-battle.js +319 -0
- package/dist/src/engine/kernel/deliberation-kernel.d.ts +12 -0
- package/dist/src/engine/kernel/deliberation-kernel.js +303 -0
- package/dist/src/engine/kernel/index.d.ts +8 -0
- package/dist/src/engine/kernel/index.js +7 -0
- package/dist/src/engine/kernel/phase-runner.d.ts +10 -0
- package/dist/src/engine/kernel/phase-runner.js +155 -0
- package/dist/src/engine/kernel/post-processor.d.ts +17 -0
- package/dist/src/engine/kernel/post-processor.js +131 -0
- package/dist/src/engine/kernel/types.d.ts +107 -0
- package/dist/src/engine/kernel/types.js +1 -0
- package/dist/src/engine/kernel/unit-executor.d.ts +6 -0
- package/dist/src/engine/kernel/unit-executor.js +132 -0
- package/dist/src/engine/lcl-manager.d.ts +44 -0
- package/dist/src/engine/lcl-manager.js +143 -0
- package/dist/src/engine/middleware/cache.d.ts +7 -0
- package/dist/src/engine/middleware/cache.js +29 -0
- package/dist/src/engine/middleware/chain.d.ts +18 -0
- package/dist/src/engine/middleware/chain.js +45 -0
- package/dist/src/engine/middleware/firewall.d.ts +8 -0
- package/dist/src/engine/middleware/firewall.js +24 -0
- package/dist/src/engine/middleware/index.d.ts +4 -0
- package/dist/src/engine/middleware/index.js +3 -0
- package/dist/src/engine/middleware/types.d.ts +43 -0
- package/dist/src/engine/middleware/types.js +1 -0
- package/dist/src/engine/nebuchadnezzar-key.d.ts +61 -0
- package/dist/src/engine/nebuchadnezzar-key.js +203 -0
- package/dist/src/engine/neon-genesis.d.ts +52 -0
- package/dist/src/engine/neon-genesis.js +203 -0
- package/dist/src/engine/objective-judge.d.ts +53 -0
- package/dist/src/engine/objective-judge.js +214 -0
- package/dist/src/engine/offline-mode.d.ts +18 -0
- package/dist/src/engine/offline-mode.js +46 -0
- package/dist/src/engine/orchestrator.d.ts +79 -0
- package/dist/src/engine/orchestrator.js +58 -0
- package/dist/src/engine/secret-cipher.d.ts +26 -0
- package/dist/src/engine/secret-cipher.js +114 -0
- package/dist/src/engine/seele-council.d.ts +90 -0
- package/dist/src/engine/seele-council.js +482 -0
- package/dist/src/engine/self-destruct.d.ts +61 -0
- package/dist/src/engine/self-destruct.js +231 -0
- package/dist/src/engine/self-evolution.d.ts +64 -0
- package/dist/src/engine/self-evolution.js +368 -0
- package/dist/src/engine/sync-rate.d.ts +45 -0
- package/dist/src/engine/sync-rate.js +151 -0
- package/dist/src/engine/type666-firewall.d.ts +76 -0
- package/dist/src/engine/type666-firewall.js +343 -0
- package/dist/src/engine/umbilical-cable.d.ts +41 -0
- package/dist/src/engine/umbilical-cable.js +192 -0
- package/dist/src/index.d.ts +106 -0
- package/dist/src/index.js +426 -0
- package/dist/src/mcp/server.d.ts +38 -0
- package/dist/src/mcp/server.js +196 -0
- package/dist/src/metrics/token-tracker.d.ts +38 -0
- package/dist/src/metrics/token-tracker.js +112 -0
- package/dist/src/parsers/json-extractor.d.ts +9 -0
- package/dist/src/parsers/json-extractor.js +239 -0
- package/dist/src/parsers/opinion-schema.d.ts +81 -0
- package/dist/src/parsers/opinion-schema.js +147 -0
- package/dist/src/parsers/unstructured-parser.d.ts +20 -0
- package/dist/src/parsers/unstructured-parser.js +122 -0
- package/dist/src/pipelines/architecture.d.ts +10 -0
- package/dist/src/pipelines/architecture.js +9 -0
- package/dist/src/pipelines/bug-analysis.d.ts +9 -0
- package/dist/src/pipelines/bug-analysis.js +8 -0
- package/dist/src/pipelines/code-review.d.ts +10 -0
- package/dist/src/pipelines/code-review.js +30 -0
- package/dist/src/pipelines/custom.d.ts +14 -0
- package/dist/src/pipelines/custom.js +29 -0
- package/dist/src/pipelines/registry.d.ts +9 -0
- package/dist/src/pipelines/registry.js +20 -0
- package/dist/src/prompts/personas.d.ts +6 -0
- package/dist/src/prompts/personas.js +44 -0
- package/dist/src/prompts/schemas.d.ts +4 -0
- package/dist/src/prompts/schemas.js +24 -0
- package/dist/src/prompts/templates.d.ts +6 -0
- package/dist/src/prompts/templates.js +91 -0
- package/dist/src/repl/accessibility.d.ts +23 -0
- package/dist/src/repl/accessibility.js +46 -0
- package/dist/src/repl/banner.d.ts +4 -0
- package/dist/src/repl/banner.js +28 -0
- package/dist/src/repl/boot-animation.d.ts +13 -0
- package/dist/src/repl/boot-animation.js +143 -0
- package/dist/src/repl/completer.d.ts +21 -0
- package/dist/src/repl/completer.js +168 -0
- package/dist/src/repl/context.d.ts +24 -0
- package/dist/src/repl/context.js +42 -0
- package/dist/src/repl/display-utils.d.ts +13 -0
- package/dist/src/repl/display-utils.js +65 -0
- package/dist/src/repl/event-listener.d.ts +18 -0
- package/dist/src/repl/event-listener.js +112 -0
- package/dist/src/repl/export-formatter.d.ts +8 -0
- package/dist/src/repl/export-formatter.js +73 -0
- package/dist/src/repl/ghost-text.d.ts +31 -0
- package/dist/src/repl/ghost-text.js +119 -0
- package/dist/src/repl/handoff-animation.d.ts +15 -0
- package/dist/src/repl/handoff-animation.js +65 -0
- package/dist/src/repl/history.d.ts +16 -0
- package/dist/src/repl/history.js +130 -0
- package/dist/src/repl/job-registry.d.ts +26 -0
- package/dist/src/repl/job-registry.js +80 -0
- package/dist/src/repl/magi-repl.d.ts +72 -0
- package/dist/src/repl/magi-repl.js +1008 -0
- package/dist/src/repl/multiline-input.d.ts +45 -0
- package/dist/src/repl/multiline-input.js +78 -0
- package/dist/src/repl/prompt-builder.d.ts +19 -0
- package/dist/src/repl/prompt-builder.js +36 -0
- package/dist/src/repl/repl-state.d.ts +5 -0
- package/dist/src/repl/repl-state.js +19 -0
- package/dist/src/repl/result-display.d.ts +8 -0
- package/dist/src/repl/result-display.js +195 -0
- package/dist/src/repl/session-stats.d.ts +26 -0
- package/dist/src/repl/session-stats.js +119 -0
- package/dist/src/repl/slash-commands.d.ts +60 -0
- package/dist/src/repl/slash-commands.js +725 -0
- package/dist/src/repl/terminal-sanitize.d.ts +14 -0
- package/dist/src/repl/terminal-sanitize.js +19 -0
- package/dist/src/reporters/console.d.ts +7 -0
- package/dist/src/reporters/console.js +78 -0
- package/dist/src/reporters/json.d.ts +2 -0
- package/dist/src/reporters/json.js +3 -0
- package/dist/src/reporters/markdown.d.ts +2 -0
- package/dist/src/reporters/markdown.js +65 -0
- package/dist/src/reporters/streaming.d.ts +20 -0
- package/dist/src/reporters/streaming.js +178 -0
- package/dist/src/tui/activity-log.d.ts +23 -0
- package/dist/src/tui/activity-log.js +67 -0
- package/dist/src/tui/animations.d.ts +39 -0
- package/dist/src/tui/animations.js +167 -0
- package/dist/src/tui/ansi.d.ts +28 -0
- package/dist/src/tui/ansi.js +51 -0
- package/dist/src/tui/boot-sequence.d.ts +11 -0
- package/dist/src/tui/boot-sequence.js +98 -0
- package/dist/src/tui/colors.d.ts +101 -0
- package/dist/src/tui/colors.js +71 -0
- package/dist/src/tui/header.d.ts +24 -0
- package/dist/src/tui/header.js +122 -0
- package/dist/src/tui/index.d.ts +3 -0
- package/dist/src/tui/index.js +3 -0
- package/dist/src/tui/keypress.d.ts +25 -0
- package/dist/src/tui/keypress.js +95 -0
- package/dist/src/tui/layout.d.ts +74 -0
- package/dist/src/tui/layout.js +171 -0
- package/dist/src/tui/magi-tui.d.ts +101 -0
- package/dist/src/tui/magi-tui.js +754 -0
- package/dist/src/tui/panel.d.ts +45 -0
- package/dist/src/tui/panel.js +292 -0
- package/dist/src/tui/screen-buffer.d.ts +54 -0
- package/dist/src/tui/screen-buffer.js +262 -0
- package/dist/src/tui/status-bar.d.ts +25 -0
- package/dist/src/tui/status-bar.js +124 -0
- package/dist/src/tui/terminal-detect.d.ts +26 -0
- package/dist/src/tui/terminal-detect.js +44 -0
- package/dist/src/tui/tui-helpers.d.ts +12 -0
- package/dist/src/tui/tui-helpers.js +37 -0
- package/dist/src/types/adapter.d.ts +75 -0
- package/dist/src/types/adapter.js +36 -0
- package/dist/src/types/config.d.ts +108 -0
- package/dist/src/types/config.js +85 -0
- package/dist/src/types/consensus.d.ts +55 -0
- package/dist/src/types/consensus.js +17 -0
- package/dist/src/types/core.d.ts +178 -0
- package/dist/src/types/core.js +85 -0
- package/dist/src/types/magi-api.d.ts +62 -0
- package/dist/src/types/magi-api.js +7 -0
- package/dist/src/types/phase-h.d.ts +142 -0
- package/dist/src/types/phase-h.js +7 -0
- package/dist/src/types/phase-i.d.ts +186 -0
- package/dist/src/types/phase-i.js +6 -0
- package/dist/src/types/phase-k.d.ts +259 -0
- package/dist/src/types/phase-k.js +6 -0
- package/dist/src/types/phase-l.d.ts +199 -0
- package/dist/src/types/phase-l.js +6 -0
- package/dist/src/types/pipeline.d.ts +37 -0
- package/dist/src/types/pipeline.js +2 -0
- package/dist/src/utils/abstain-factory.d.ts +2 -0
- package/dist/src/utils/abstain-factory.js +18 -0
- package/dist/src/utils/errors.d.ts +34 -0
- package/dist/src/utils/errors.js +59 -0
- package/dist/src/utils/file-validator.d.ts +50 -0
- package/dist/src/utils/file-validator.js +124 -0
- package/dist/src/utils/fire-and-forget.d.ts +5 -0
- package/dist/src/utils/fire-and-forget.js +10 -0
- package/dist/src/utils/flag-validator.d.ts +21 -0
- package/dist/src/utils/flag-validator.js +79 -0
- package/dist/src/utils/freeze.d.ts +8 -0
- package/dist/src/utils/freeze.js +16 -0
- package/dist/src/utils/language-detector.d.ts +16 -0
- package/dist/src/utils/language-detector.js +159 -0
- package/dist/src/utils/latency-tracker.d.ts +45 -0
- package/dist/src/utils/latency-tracker.js +100 -0
- package/dist/src/utils/logger.d.ts +33 -0
- package/dist/src/utils/logger.js +112 -0
- package/dist/src/utils/process.d.ts +40 -0
- package/dist/src/utils/process.js +253 -0
- package/dist/src/utils/retry.d.ts +12 -0
- package/dist/src/utils/retry.js +30 -0
- package/dist/src/utils/safe-fs.d.ts +38 -0
- package/dist/src/utils/safe-fs.js +56 -0
- package/dist/src/utils/safe-json-parse.d.ts +15 -0
- package/dist/src/utils/safe-json-parse.js +49 -0
- package/dist/src/utils/sanitize.d.ts +14 -0
- package/dist/src/utils/sanitize.js +186 -0
- package/dist/src/utils/semaphore.d.ts +22 -0
- package/dist/src/utils/semaphore.js +57 -0
- package/dist/src/utils/shutdown.d.ts +6 -0
- package/dist/src/utils/shutdown.js +51 -0
- package/dist/src/utils/tty.d.ts +5 -0
- package/dist/src/utils/tty.js +7 -0
- package/package.json +82 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { getMajorityVote } from '../types/consensus.js';
|
|
2
|
+
/**
|
|
3
|
+
* MAGI Consensus Engine
|
|
4
|
+
*
|
|
5
|
+
* Calculates consensus from N MAGI units' opinions (2-7) using:
|
|
6
|
+
* - Unanimous: all agree
|
|
7
|
+
* - Majority: more than half of non-abstain voters
|
|
8
|
+
* - Deadlock resolution: configurable strategy
|
|
9
|
+
*/
|
|
10
|
+
export class ConsensusEngine {
|
|
11
|
+
config;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
if (!config) {
|
|
15
|
+
throw new Error('ConsensusEngine requires a ConsensusConfig');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
calculate(opinions) {
|
|
19
|
+
const votes = this.tallyVotes(opinions);
|
|
20
|
+
const nonAbstainCount = votes.approve.length + votes.reject.length;
|
|
21
|
+
// Check quorum
|
|
22
|
+
if (nonAbstainCount < this.config.quorum) {
|
|
23
|
+
return this.buildResult('INSUFFICIENT', 'escalated', votes, opinions);
|
|
24
|
+
}
|
|
25
|
+
// Unanimous check (excluding dummy plug units if configured)
|
|
26
|
+
const excluded = new Set(this.config.excludeFromUnanimous ?? []);
|
|
27
|
+
const eligibleOpinions = opinions.filter(o => !excluded.has(o.unit));
|
|
28
|
+
const eligibleCount = eligibleOpinions.length;
|
|
29
|
+
if (eligibleCount > 0) {
|
|
30
|
+
const eligibleApprove = eligibleOpinions.filter(o => o.vote === 'APPROVE').length;
|
|
31
|
+
const eligibleReject = eligibleOpinions.filter(o => o.vote === 'REJECT').length;
|
|
32
|
+
if (eligibleApprove === eligibleCount) {
|
|
33
|
+
return this.buildResult('UNANIMOUS_APPROVE', 'unanimous', votes, opinions);
|
|
34
|
+
}
|
|
35
|
+
if (eligibleReject === eligibleCount) {
|
|
36
|
+
return this.buildResult('UNANIMOUS_REJECT', 'unanimous', votes, opinions);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Majority check: proper majority threshold (floor(nonAbstain / 2) + 1)
|
|
40
|
+
const majorityThreshold = Math.floor(nonAbstainCount / 2) + 1;
|
|
41
|
+
if (votes.approve.length >= majorityThreshold) {
|
|
42
|
+
return this.buildResult('MAJORITY_APPROVE', 'majority', votes, opinions);
|
|
43
|
+
}
|
|
44
|
+
if (votes.reject.length >= majorityThreshold) {
|
|
45
|
+
return this.buildResult('MAJORITY_REJECT', 'majority', votes, opinions);
|
|
46
|
+
}
|
|
47
|
+
// Deadlock: neither side reaches majority
|
|
48
|
+
return this.resolveDeadlock(votes, opinions);
|
|
49
|
+
}
|
|
50
|
+
tallyVotes(opinions) {
|
|
51
|
+
const approve = [];
|
|
52
|
+
const reject = [];
|
|
53
|
+
const abstain = [];
|
|
54
|
+
let weightedApprove = 0;
|
|
55
|
+
let weightedReject = 0;
|
|
56
|
+
for (const opinion of opinions) {
|
|
57
|
+
const weight = this.getUnitWeight(opinion.unit) * opinion.confidence;
|
|
58
|
+
switch (opinion.vote) {
|
|
59
|
+
case 'APPROVE':
|
|
60
|
+
approve.push(opinion.unit);
|
|
61
|
+
weightedApprove += weight;
|
|
62
|
+
break;
|
|
63
|
+
case 'REJECT':
|
|
64
|
+
reject.push(opinion.unit);
|
|
65
|
+
weightedReject += weight;
|
|
66
|
+
break;
|
|
67
|
+
case 'ABSTAIN':
|
|
68
|
+
abstain.push(opinion.unit);
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { approve, reject, abstain, weightedApprove, weightedReject };
|
|
73
|
+
}
|
|
74
|
+
resolveDeadlock(votes, opinions) {
|
|
75
|
+
switch (this.config.deadlockStrategy) {
|
|
76
|
+
case 'melchior-tiebreak': {
|
|
77
|
+
const melchior = opinions.find((o) => o.unit === 'MELCHIOR');
|
|
78
|
+
if (melchior && melchior.vote !== 'ABSTAIN') {
|
|
79
|
+
const decision = melchior.vote === 'APPROVE' ? 'MAJORITY_APPROVE' : 'MAJORITY_REJECT';
|
|
80
|
+
return this.buildResult(decision, 'tiebreak', votes, opinions);
|
|
81
|
+
}
|
|
82
|
+
// Fallback: highest-confidence when MELCHIOR not present
|
|
83
|
+
const nonAbstain = opinions.filter((o) => o.vote !== 'ABSTAIN');
|
|
84
|
+
const sorted = [...nonAbstain].sort((a, b) => b.confidence - a.confidence);
|
|
85
|
+
const highest = sorted[0];
|
|
86
|
+
if (highest) {
|
|
87
|
+
const decision = highest.vote === 'APPROVE' ? 'MAJORITY_APPROVE' : 'MAJORITY_REJECT';
|
|
88
|
+
return this.buildResult(decision, 'weighted', votes, opinions);
|
|
89
|
+
}
|
|
90
|
+
return this.buildResult('DEADLOCK', 'escalated', votes, opinions);
|
|
91
|
+
}
|
|
92
|
+
case 'highest-confidence': {
|
|
93
|
+
const nonAbstain = opinions.filter((o) => o.vote !== 'ABSTAIN');
|
|
94
|
+
const sorted = [...nonAbstain].sort((a, b) => b.confidence - a.confidence);
|
|
95
|
+
const highest = sorted[0];
|
|
96
|
+
if (highest) {
|
|
97
|
+
const decision = highest.vote === 'APPROVE' ? 'MAJORITY_APPROVE' : 'MAJORITY_REJECT';
|
|
98
|
+
return this.buildResult(decision, 'weighted', votes, opinions);
|
|
99
|
+
}
|
|
100
|
+
return this.buildResult('DEADLOCK', 'escalated', votes, opinions);
|
|
101
|
+
}
|
|
102
|
+
case 'human-in-the-loop':
|
|
103
|
+
case 'escalate':
|
|
104
|
+
default:
|
|
105
|
+
return this.buildResult('DEADLOCK', 'escalated', votes, opinions);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
getUnitWeight(unit) {
|
|
109
|
+
return this.config.unitWeights?.[unit] ?? 1.0;
|
|
110
|
+
}
|
|
111
|
+
buildResult(decision, method, votes, opinions) {
|
|
112
|
+
const majorityVote = getMajorityVote(decision);
|
|
113
|
+
const dissent = opinions
|
|
114
|
+
.filter((o) => o.vote !== majorityVote && o.vote !== 'ABSTAIN')
|
|
115
|
+
.map((o) => ({
|
|
116
|
+
unit: o.unit,
|
|
117
|
+
vote: o.vote,
|
|
118
|
+
reasoning: o.reasoning,
|
|
119
|
+
}));
|
|
120
|
+
const confidences = opinions.map((o) => o.confidence);
|
|
121
|
+
const avgConfidence = confidences.length > 0
|
|
122
|
+
? confidences.reduce((sum, c) => sum + c, 0) / confidences.length
|
|
123
|
+
: 0;
|
|
124
|
+
return {
|
|
125
|
+
decision,
|
|
126
|
+
method,
|
|
127
|
+
votes,
|
|
128
|
+
confidence: avgConfidence,
|
|
129
|
+
summary: this.buildSummary(decision, method, votes),
|
|
130
|
+
dissent: dissent.length > 0 ? dissent : undefined,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
buildSummary(decision, method, votes) {
|
|
134
|
+
const decisionLabel = decision.replace(/_/g, ' ');
|
|
135
|
+
const methodLabel = method.toUpperCase();
|
|
136
|
+
const approveNames = votes.approve.join(', ') || 'none';
|
|
137
|
+
const rejectNames = votes.reject.join(', ') || 'none';
|
|
138
|
+
const abstainNames = votes.abstain.join(', ') || 'none';
|
|
139
|
+
return [
|
|
140
|
+
`MAGI DECISION: ${decisionLabel} (via ${methodLabel})`,
|
|
141
|
+
`APPROVE: ${approveNames}`,
|
|
142
|
+
`REJECT: ${rejectNames}`,
|
|
143
|
+
`ABSTAIN: ${abstainNames}`,
|
|
144
|
+
].join('\n');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeadSeaScrolls --- 死海文書 預言エンジン (A-08)
|
|
3
|
+
*
|
|
4
|
+
* ファイルの9次元特徴量ベクトルからランダムフォレストで欠陥確率を予測し、
|
|
5
|
+
* エヴァンゲリオン風の預言ナラティブを生成する。
|
|
6
|
+
* 外部ML依存なし --- ランダムフォレストをスクラッチ実装。
|
|
7
|
+
*/
|
|
8
|
+
import type { ProphecyLevel, Prophecy, ProphecyAccuracy, DecisionTreeNode, SATDResult } from '../types/phase-k.js';
|
|
9
|
+
import type { MagiEventBus } from './events.js';
|
|
10
|
+
/**
|
|
11
|
+
* Calculate Gini impurity for a set of labels.
|
|
12
|
+
* Gini = 1 - sum(p_i^2) where labels are binned into [0,0.5) and [0.5,1].
|
|
13
|
+
*/
|
|
14
|
+
export declare function giniImpurity(labels: number[]): number;
|
|
15
|
+
interface TrainingPoint {
|
|
16
|
+
features: number[];
|
|
17
|
+
label: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Build a decision tree recursively.
|
|
21
|
+
*/
|
|
22
|
+
export declare function buildTree(data: TrainingPoint[], depth: number, maxDepth: number, featureSubset: number[]): DecisionTreeNode;
|
|
23
|
+
/**
|
|
24
|
+
* Traverse a decision tree to get a prediction.
|
|
25
|
+
*/
|
|
26
|
+
export declare function predictTree(node: DecisionTreeNode, features: number[]): number;
|
|
27
|
+
/**
|
|
28
|
+
* Create a bootstrap sample (with replacement) of the same size as the input.
|
|
29
|
+
*/
|
|
30
|
+
export declare function bootstrapSample(data: TrainingPoint[], rng: () => number): TrainingPoint[];
|
|
31
|
+
/**
|
|
32
|
+
* Train a random forest: 50 trees, each on a bootstrap sample
|
|
33
|
+
* with sqrt(9)=3 randomly selected features per split.
|
|
34
|
+
*/
|
|
35
|
+
export declare function trainForest(data: TrainingPoint[], rng: () => number): DecisionTreeNode[];
|
|
36
|
+
/**
|
|
37
|
+
* Predict using the random forest: average prediction across all trees.
|
|
38
|
+
*/
|
|
39
|
+
export declare function predictForest(trees: DecisionTreeNode[], features: number[]): number;
|
|
40
|
+
/**
|
|
41
|
+
* Generate a pre-trained default forest using a seeded PRNG.
|
|
42
|
+
* Creates realistic trees that split on sensible thresholds for
|
|
43
|
+
* the 9-dimensional feature vector.
|
|
44
|
+
*
|
|
45
|
+
* Feature semantics and typical ranges:
|
|
46
|
+
* 0: churn (0-5000) - higher = more risky
|
|
47
|
+
* 1: commitFrequency (0-100) - moderate is best
|
|
48
|
+
* 2: uniqueAuthors (1-20) - too many authors = risky
|
|
49
|
+
* 3: bugFixRatio (0-1) - higher = more risky
|
|
50
|
+
* 4: age (0-3650 days) - very old = risky
|
|
51
|
+
* 5: cyclomaticComplexity (0-100) - higher = more risky
|
|
52
|
+
* 6: satdCount (0-50) - higher = more risky
|
|
53
|
+
* 7: testCoverage (0-1) - lower = more risky
|
|
54
|
+
* 8: duplicateRatio (0-1) - higher = more risky
|
|
55
|
+
*/
|
|
56
|
+
export declare function generateDefaultForest(): DecisionTreeNode[];
|
|
57
|
+
/**
|
|
58
|
+
* Scan file content line by line for Self-Admitted Technical Debt patterns.
|
|
59
|
+
*/
|
|
60
|
+
export declare function detectSATD(content: string): SATDResult;
|
|
61
|
+
/**
|
|
62
|
+
* Map defect probability to prophecy level.
|
|
63
|
+
*/
|
|
64
|
+
export declare function getProphecyLevel(probability: number): ProphecyLevel;
|
|
65
|
+
/**
|
|
66
|
+
* Generate Evangelion-themed prophecy narrative text.
|
|
67
|
+
*/
|
|
68
|
+
export declare function generateNarrative(prophecy: Prophecy): string;
|
|
69
|
+
/**
|
|
70
|
+
* Estimate cyclomatic complexity from source code by counting
|
|
71
|
+
* branching constructs: if, else, for, while, switch, case, &&, ||.
|
|
72
|
+
*/
|
|
73
|
+
export declare function estimateCyclomaticComplexity(content: string): number;
|
|
74
|
+
export declare class DeadSeaScrolls {
|
|
75
|
+
private readonly scrollsDir;
|
|
76
|
+
private readonly eventBus?;
|
|
77
|
+
private _forest;
|
|
78
|
+
private prophecies;
|
|
79
|
+
private accuracy;
|
|
80
|
+
/** Lazy-initialized random forest. Built on first access to avoid startup cost. */
|
|
81
|
+
private get forest();
|
|
82
|
+
constructor(workspaceDir: string, eventBus?: MagiEventBus);
|
|
83
|
+
/**
|
|
84
|
+
* Extract 9-dimensional feature vector for a file.
|
|
85
|
+
*/
|
|
86
|
+
private extractFeatures;
|
|
87
|
+
/**
|
|
88
|
+
* Convert FeatureVector to numeric array for the forest.
|
|
89
|
+
*/
|
|
90
|
+
private featuresToArray;
|
|
91
|
+
/**
|
|
92
|
+
* Analyze a single file and produce a prophecy.
|
|
93
|
+
*/
|
|
94
|
+
analyze(filePath: string, cwd?: string): Promise<Prophecy>;
|
|
95
|
+
/**
|
|
96
|
+
* Analyze all TypeScript files in a directory.
|
|
97
|
+
*/
|
|
98
|
+
analyzeAll(dirPath: string, cwd?: string): Promise<Prophecy[]>;
|
|
99
|
+
/**
|
|
100
|
+
* Recursively find all .ts files in a directory.
|
|
101
|
+
*/
|
|
102
|
+
private findTsFiles;
|
|
103
|
+
/**
|
|
104
|
+
* Detect SATD patterns in content.
|
|
105
|
+
*/
|
|
106
|
+
detectSATD(content: string): SATDResult;
|
|
107
|
+
/**
|
|
108
|
+
* Generate prophecy narrative.
|
|
109
|
+
*/
|
|
110
|
+
generateNarrative(prophecy: Prophecy): string;
|
|
111
|
+
/**
|
|
112
|
+
* Record that a prophecy was fulfilled (defect actually occurred).
|
|
113
|
+
*/
|
|
114
|
+
recordFulfillment(prophecyId: string): Promise<boolean>;
|
|
115
|
+
/**
|
|
116
|
+
* Get current accuracy statistics.
|
|
117
|
+
*/
|
|
118
|
+
getAccuracy(): ProphecyAccuracy;
|
|
119
|
+
/**
|
|
120
|
+
* Load prophecies and accuracy from persistence.
|
|
121
|
+
*/
|
|
122
|
+
load(): Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Persist prophecies and accuracy to disk.
|
|
125
|
+
*/
|
|
126
|
+
persist(): Promise<void>;
|
|
127
|
+
/** Get the current forest (for testing). */
|
|
128
|
+
getForest(): DecisionTreeNode[];
|
|
129
|
+
/** Get stored prophecies (for testing). */
|
|
130
|
+
getProphecies(): Map<string, Prophecy>;
|
|
131
|
+
}
|
|
132
|
+
export {};
|