progmune-runtime 3.3.8 → 3.4.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/CHANGELOG.md +34 -0
- package/dist/agent-cli.js +209 -0
- package/dist/agent-loop.js +324 -0
- package/dist/agent-loop.test.js +256 -0
- package/dist/agent-patrol.js +200 -0
- package/dist/agent-patrol.test.js +141 -0
- package/dist/agent-perception.js +197 -0
- package/dist/agent-perception.test.js +125 -0
- package/dist/agent-supervision.js +145 -0
- package/dist/agent-supervision.test.js +60 -0
- package/dist/execute.js +6 -2
- package/dist/extract-ir.js +3 -1
- package/dist/patrol-cli.js +130 -0
- package/dist/planner-prompts.js +3 -0
- package/dist/planner.js +2 -0
- package/dist/trust/engine.js +81 -0
- package/package.json +4 -1
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Phase 12: Progmune 免疫巡逻 CLI — `progmune patrol`(形态 B 第一版)
|
|
4
|
+
*
|
|
5
|
+
* 扫描项目 → trust_check → 违规报告 + 建议补丁(绝不自动合并)。
|
|
6
|
+
* 支持 --watch 持续监听(文件变更 → 防抖 → 重新巡逻 → 刷新报告)。
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* npx ts-node src/patrol-cli.ts --project <dir> [options]
|
|
10
|
+
* npm run patrol -- --project <dir> [options]
|
|
11
|
+
*
|
|
12
|
+
* Options:
|
|
13
|
+
* --project <dir> 目标项目目录(默认 CWD)
|
|
14
|
+
* --watch 持续监听模式(文件变更后自动重扫)
|
|
15
|
+
* --json JSON 输出(单次扫描)
|
|
16
|
+
* --help, -h
|
|
17
|
+
*/
|
|
18
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
21
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
22
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
23
|
+
}
|
|
24
|
+
Object.defineProperty(o, k2, desc);
|
|
25
|
+
}) : (function(o, m, k, k2) {
|
|
26
|
+
if (k2 === undefined) k2 = k;
|
|
27
|
+
o[k2] = m[k];
|
|
28
|
+
}));
|
|
29
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
30
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
31
|
+
}) : function(o, v) {
|
|
32
|
+
o["default"] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
35
|
+
var ownKeys = function(o) {
|
|
36
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
37
|
+
var ar = [];
|
|
38
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
39
|
+
return ar;
|
|
40
|
+
};
|
|
41
|
+
return ownKeys(o);
|
|
42
|
+
};
|
|
43
|
+
return function (mod) {
|
|
44
|
+
if (mod && mod.__esModule) return mod;
|
|
45
|
+
var result = {};
|
|
46
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
47
|
+
__setModuleDefault(result, mod);
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
})();
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
const path = __importStar(require("path"));
|
|
53
|
+
const agent_patrol_1 = require("./agent-patrol");
|
|
54
|
+
const agent_perception_1 = require("./agent-perception");
|
|
55
|
+
// 在 chdir 到项目目录之前按启动 CWD 加载 .env——
|
|
56
|
+
// trust 引擎内部的 lazy require(语义映射 LLM 回退)发生在 chdir 之后,
|
|
57
|
+
// 若不预载,LLM_API_KEY 不可用,映射降级会导致漏报。
|
|
58
|
+
try {
|
|
59
|
+
require("dotenv/config");
|
|
60
|
+
}
|
|
61
|
+
catch { /* dotenv 可选 */ }
|
|
62
|
+
const args = process.argv.slice(2);
|
|
63
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
64
|
+
console.log(`
|
|
65
|
+
Progmune 免疫巡逻 (P4) — 监听/扫描 → trust_check → 报告 + 建议补丁(永不自动合并)
|
|
66
|
+
|
|
67
|
+
Usage:
|
|
68
|
+
npx ts-node src/patrol-cli.ts --project <dir> [options]
|
|
69
|
+
npm run patrol -- --project <dir> [options]
|
|
70
|
+
|
|
71
|
+
Options:
|
|
72
|
+
--project <dir> 目标项目目录(默认当前目录)
|
|
73
|
+
--watch 持续监听模式(源文件变更后自动重扫并刷新报告)
|
|
74
|
+
--json JSON 输出(单次扫描)
|
|
75
|
+
--help, -h 显示帮助
|
|
76
|
+
|
|
77
|
+
Example:
|
|
78
|
+
npm run patrol -- --project demo-patrol
|
|
79
|
+
npm run patrol -- --project demo-patrol --watch
|
|
80
|
+
`);
|
|
81
|
+
process.exit(0);
|
|
82
|
+
}
|
|
83
|
+
const getFlag = (name) => {
|
|
84
|
+
const idx = args.indexOf(`--${name}`);
|
|
85
|
+
return idx >= 0 && idx + 1 < args.length ? args[idx + 1] : undefined;
|
|
86
|
+
};
|
|
87
|
+
const projectPath = path.resolve(getFlag("project") || process.cwd());
|
|
88
|
+
const watch = args.includes("--watch");
|
|
89
|
+
const json = args.includes("--json");
|
|
90
|
+
async function scanOnce(label) {
|
|
91
|
+
try {
|
|
92
|
+
const report = await (0, agent_patrol_1.runPatrol)(projectPath);
|
|
93
|
+
const reportFile = (0, agent_patrol_1.writePatrolReport)(report, projectPath);
|
|
94
|
+
if (json && !watch) {
|
|
95
|
+
console.log(JSON.stringify(report, null, 2));
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
console.log(`[${label}] ` + (0, agent_patrol_1.formatPatrolTerminal)(report).replace(/\n/g, "\n "));
|
|
99
|
+
console.log(` 报告: ${reportFile}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
console.error(`❌ 巡逻失败: ${e?.message || e}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function main() {
|
|
107
|
+
process.chdir(projectPath);
|
|
108
|
+
console.log(`🛡️ Progmune 免疫巡逻 (P4) — 项目: ${projectPath}${watch ? "(持续监听)" : ""}`);
|
|
109
|
+
if (!watch) {
|
|
110
|
+
await scanOnce("扫描");
|
|
111
|
+
process.exit(0);
|
|
112
|
+
}
|
|
113
|
+
// 持续监听:RepoWatcher 防抖触发重扫
|
|
114
|
+
await scanOnce("首次");
|
|
115
|
+
let scanning = false;
|
|
116
|
+
const watcher = new agent_perception_1.RepoWatcher(projectPath, async (file) => {
|
|
117
|
+
if (scanning)
|
|
118
|
+
return; // 扫描期间的新变更合并进下一轮
|
|
119
|
+
scanning = true;
|
|
120
|
+
console.log(`🔍 检测到变更: ${file}`);
|
|
121
|
+
await scanOnce("重扫");
|
|
122
|
+
scanning = false;
|
|
123
|
+
}, 1500);
|
|
124
|
+
watcher.start();
|
|
125
|
+
console.log("👂 监听中(Ctrl+C 退出)…");
|
|
126
|
+
}
|
|
127
|
+
main().catch((e) => {
|
|
128
|
+
console.error(`❌ 巡逻 CLI 异常: ${e?.message || e}`);
|
|
129
|
+
process.exit(1);
|
|
130
|
+
});
|
package/dist/planner-prompts.js
CHANGED
|
@@ -40,6 +40,9 @@ exports.RETRY_HINT = `输出格式:紧凑 JSON 数组 [{"f":"函数名","to":"
|
|
|
40
40
|
// ── Formatters ──
|
|
41
41
|
/** Build a compact function list with parameter examples for LLM precision. */
|
|
42
42
|
function buildCompactFuncList(funcs, allFuncs) {
|
|
43
|
+
// 语义 marker(__progmune_*,提取器注入供规则消费)不是真实可调用函数——
|
|
44
|
+
// 不出现在 LLM 可见函数列表中,防止被生成成真实调用导致编译失败
|
|
45
|
+
funcs = funcs.filter((f) => !String(f.name || "").startsWith("__progmune_"));
|
|
43
46
|
// Example values for each type — helps LLM fill meaningful args
|
|
44
47
|
function exampleValue(type, paramName) {
|
|
45
48
|
const t = (type || "any").replace(/\[\]$/, "").toLowerCase();
|
package/dist/planner.js
CHANGED
|
@@ -900,6 +900,8 @@ ${planner_prompts_1.RETRY_HINT}
|
|
|
900
900
|
: await (0, llm_1.generate)(`你是程序合成助手。\n\n${currentPrompt}`);
|
|
901
901
|
}
|
|
902
902
|
catch (e) {
|
|
903
|
+
// 铁律:失败原因必须可见(不许静默绕过)——LLM 异常记录后继续重试/降级
|
|
904
|
+
console.error(`⚠️ LLM 调用失败 (attempt ${r + 1}/${maxRetries}): ${e?.message || e}`);
|
|
903
905
|
continue;
|
|
904
906
|
}
|
|
905
907
|
if (!text)
|
package/dist/trust/engine.js
CHANGED
|
@@ -704,12 +704,53 @@ async function collectProtocolViolations(ctx, callGraph) {
|
|
|
704
704
|
let ssgMatchedCalls = 0;
|
|
705
705
|
let ssgViolationCount = 0;
|
|
706
706
|
try {
|
|
707
|
+
// ── P4.5 校准:TS/JS 项目在 ir.json 缺失时先提取 IR ──
|
|
708
|
+
// 序列必须来自「函数体内真实调用」而非「文件内函数声明名单」——
|
|
709
|
+
// 声明顺序 ≠ 执行顺序:正则扫描会把声明当调用(auth.ts 误报),
|
|
710
|
+
// 也会因调用数不足阈值漏掉单调用违规文件(bad_flow 漏报)。
|
|
711
|
+
if (!ctx.language || ctx.language === "typescript" || ctx.language === "javascript") {
|
|
712
|
+
try {
|
|
713
|
+
const fs = require("fs");
|
|
714
|
+
if (!fs.existsSync(path.join(ctx.projectPath, "ir.json"))) {
|
|
715
|
+
const { extractIR } = require("../extract-ir");
|
|
716
|
+
const ir = extractIR(ctx.projectPath);
|
|
717
|
+
fs.writeFileSync(path.join(ctx.projectPath, "ir.json"), JSON.stringify(ir, null, 2));
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
catch { /* best-effort — 回退正则扫描 */ }
|
|
721
|
+
}
|
|
707
722
|
// ── Phase 1-5 Semantic Pipeline ──
|
|
708
723
|
const callSequences = extractCallSequencesFromProject(ctx.projectPath, ctx.language);
|
|
709
724
|
const flaggedCount = { value: 0 };
|
|
710
725
|
const cleanCount = { value: 0 };
|
|
711
726
|
// ── SSG State Machine: load protocol rules once ──
|
|
712
727
|
protocolRulesData = (0, ssg_bridge_1.loadProtocolRules)(ctx.projectPath);
|
|
728
|
+
// ── P4.5: 合并项目 IR 注解协议(IR 优先,缺 namespace 继承内置 JSON) ──
|
|
729
|
+
// 内置 protocols.json 的规则是通用弱约束(如 generate_jwt pre=[]),
|
|
730
|
+
// 项目文件里的 @protocol 注解才是项目真实协议(如 pre=[PASSWORD_VERIFIED])。
|
|
731
|
+
// planner 已用此合并语义,trust 引擎需对齐,否则项目级前置约束不生效。
|
|
732
|
+
if (protocolRulesData) {
|
|
733
|
+
try {
|
|
734
|
+
const fs = require("fs");
|
|
735
|
+
const irPath = path.join(ctx.projectPath, "ir.json");
|
|
736
|
+
if (fs.existsSync(irPath)) {
|
|
737
|
+
const ir = JSON.parse(fs.readFileSync(irPath, "utf-8"));
|
|
738
|
+
if (Array.isArray(ir)) {
|
|
739
|
+
for (const f of ir) {
|
|
740
|
+
if (!f.protocol)
|
|
741
|
+
continue;
|
|
742
|
+
const protocol = { ...f.protocol };
|
|
743
|
+
const existing = protocolRulesData.rules.get(String(f.name));
|
|
744
|
+
if (existing?.namespace && !protocol.namespace) {
|
|
745
|
+
protocol.namespace = existing.namespace;
|
|
746
|
+
}
|
|
747
|
+
protocolRulesData.rules.set(String(f.name), protocol);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
catch { /* best-effort */ }
|
|
753
|
+
}
|
|
713
754
|
for (const seq of callSequences) {
|
|
714
755
|
try {
|
|
715
756
|
const semantic = await (0, api_semantic_mapper_1.mapSequenceToSemanticWithLLM)(seq.calls);
|
|
@@ -864,6 +905,46 @@ async function collectProtocolViolations(ctx, callGraph) {
|
|
|
864
905
|
};
|
|
865
906
|
}
|
|
866
907
|
function extractCallSequencesFromProject(projectPath, language) {
|
|
908
|
+
// P4.5: 优先 IR 精确序列(每个函数体内的真实调用,从各自入口验证)
|
|
909
|
+
const irSequences = extractCallSequencesFromIR(projectPath);
|
|
910
|
+
if (irSequences.length > 0)
|
|
911
|
+
return irSequences;
|
|
912
|
+
// 回退:正则扫描(C 等无 IR 语言保持原行为)
|
|
913
|
+
return extractCallSequencesRegex(projectPath, language);
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* P4.5: 从 ir.json 构建 per-function 调用序列。
|
|
917
|
+
* 每个函数体内的 calls[] 是一条独立序列(从协议初始状态起步验证)——
|
|
918
|
+
* 函数声明名单不再是验证对象;语义 marker(__progmune_*)供规则消费,不作真实调用。
|
|
919
|
+
*/
|
|
920
|
+
function extractCallSequencesFromIR(projectPath) {
|
|
921
|
+
try {
|
|
922
|
+
const fs = require("fs");
|
|
923
|
+
const irPath = path.join(projectPath, "ir.json");
|
|
924
|
+
if (!fs.existsSync(irPath))
|
|
925
|
+
return [];
|
|
926
|
+
const ir = JSON.parse(fs.readFileSync(irPath, "utf-8"));
|
|
927
|
+
if (!Array.isArray(ir))
|
|
928
|
+
return [];
|
|
929
|
+
const sequences = [];
|
|
930
|
+
for (const f of ir) {
|
|
931
|
+
const calls = (f.calls || []).filter((c) => typeof c === "string" && !c.startsWith("__progmune_"));
|
|
932
|
+
if (calls.length === 0)
|
|
933
|
+
continue;
|
|
934
|
+
sequences.push({
|
|
935
|
+
calls,
|
|
936
|
+
file: String(f.file || ""),
|
|
937
|
+
function: String(f.name || "unknown"),
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
return sequences;
|
|
941
|
+
}
|
|
942
|
+
catch {
|
|
943
|
+
return [];
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/** 正则扫描回退路径(原实现):按文件扫调用样 token,≥4 才成序列。 */
|
|
947
|
+
function extractCallSequencesRegex(projectPath, language) {
|
|
867
948
|
const sequences = [];
|
|
868
949
|
try {
|
|
869
950
|
const fs = require("fs");
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "progmune-runtime",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Progmune — AI Trust Decision Engine. Verify AI-generated code before it reaches production. Outputs APPROVED / NEEDS_REVIEW / BLOCKED with evidence.",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist/",
|
|
7
7
|
"protocols.json",
|
|
8
|
+
"CHANGELOG.md",
|
|
8
9
|
"docs/Progmune_项目全解.html",
|
|
9
10
|
"docs/Progmune_投资人白皮书_v2.0.html"
|
|
10
11
|
],
|
|
@@ -30,6 +31,8 @@
|
|
|
30
31
|
"governance:json": "node dist/audit/cli.js --all --json",
|
|
31
32
|
"governance:md": "node dist/audit/cli.js --all --markdown",
|
|
32
33
|
"certify": "node dist/certify.js",
|
|
34
|
+
"agent": "node dist/agent-cli.js",
|
|
35
|
+
"patrol": "node dist/patrol-cli.js",
|
|
33
36
|
"certify:html": "node dist/certify-html.js",
|
|
34
37
|
"policy": "node dist/policy/cli.js check",
|
|
35
38
|
"precision": "node dist/multi-repo-precision.js",
|