fix: isolate bytecode bare-name resolve to module scope (G02)
Imported functions shared the flat call table, so bare same-named calls could silently bind across imports. Scope tags keep path calls working while rejecting ambiguous bare names on both backends. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ecbdd18303
commit
e9ae7ff1ae
4 files changed
+72
-56
No files matched your search
@@ -189,7 +189,7 @@ P0/P1/P2 分类改为缺口领域,不再按编号整组串行:G06 的未知
|
||||
|---|---|---|---|
|
||||
| S0 作者基线与最小语料 | 无 | 六类实际写作/复用任务、当前语法入口、无网络最小门禁(G10/G11) | **已落地**:[tests/executable_knowledge/](../../tests/executable_knowledge/README.md) + `cargo test --test executable_knowledge`;Bytecode 裸名同名误连记为基线缺陷待 S2/G02 |
|
||||
| S1 可读步骤与唯一目标 | S0 | 一份文档的源码/步骤/view 联动;统一 resolver、图与上下文节点(G01/G03) | **已落地子集**:G01 双图分离;G03 步骤 kind + `steps.json` + view 导航;同名 import resolve;正文 NarrativeRef 仍待 |
|
||||
| S2 确定执行与早期 AI 复用 | S1 | BoundCall 双后端、最小上下文卡、本地 handle 与现有 Agent 试点(G02/G06/G07/G09 的安全子集) | **部分落地**:G06 效应传递/未知不纯;G07 MLSP symbol 绑定执行;**G02 BoundCall 双后端仍待** |
|
||||
| S2 确定执行与早期 AI 复用 | S1 | BoundCall 双后端、最小上下文卡、本地 handle 与现有 Agent 试点(G02/G06/G07/G09 的安全子集) | **已落地子集**:G06 效应;G07 MLSP symbol 绑定;G02 Bytecode 裸名不跨 import scope(`06b` 双后端拒绝);完整 BoundCall IR / Agent 强制 bind 仍待 |
|
||||
| S3 局部编辑与库可读性 | S1–S2 | 摘要保护的局部补丁、语义 diff、受限纯表达式提取;lib/ext 示例优化(G03/G09/G10/G11) | 无无关改写;提取等价;声明变更可见;不为旧库增加兼容语法 |
|
||||
| S4 业务知识与适用性 | S2;与 S3 可交错 | 书店契约、来源/验证事件、传递效应及政策、负知识(G05/G06/G07/G09) | 分清输入不适用、来源过期和测试失败;纯工具免虚构来源 |
|
||||
| S5 变化传播与严格复用 | S1–S2/S4 | 分类摘要、SCC 闭包、整代快照、impact、严格 handle 复核(G04/G08) | 依赖/证据变化影响正确;文案不混同实现;故障注入无混代 |
|
||||
|
||||
+49
-11
@@ -28,11 +28,14 @@ struct FlatFun {
|
||||
path_index: HashMap<Vec<String>, usize>,
|
||||
/// Short name from frontmatter `use` → path segments.
|
||||
uses: HashMap<String, Vec<String>>,
|
||||
/// Module collection scope (G02): bare-name resolve must not cross imports.
|
||||
scope: u32,
|
||||
}
|
||||
|
||||
pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
|
||||
let mut flat = Vec::new();
|
||||
let _exports = collect_module_tree(module, &mut flat);
|
||||
let mut next_scope = 1u32;
|
||||
let _exports = collect_module_tree(module, &mut flat, 0, &mut next_scope);
|
||||
if flat.is_empty() {
|
||||
bail!("bytecode: no functions to compile");
|
||||
}
|
||||
@@ -106,10 +109,21 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
|
||||
|
||||
/// Collect `module` and its imports into `flat`.
|
||||
/// Returns relative export paths within this module (e.g. `["parse"]`, `["agent","step"]`).
|
||||
fn collect_module_tree(module: &Module, flat: &mut Vec<FlatFun>) -> HashMap<Vec<String>, usize> {
|
||||
///
|
||||
/// Each module gets a distinct `scope` id. Bare-name `resolve_call` only searches
|
||||
/// within the caller's scope so imported same-named functions cannot be silently
|
||||
/// selected (BoundCall / G02). Cross-module calls must use `bind.member` paths.
|
||||
fn collect_module_tree(
|
||||
module: &Module,
|
||||
flat: &mut Vec<FlatFun>,
|
||||
scope: u32,
|
||||
next_scope: &mut u32,
|
||||
) -> HashMap<Vec<String>, usize> {
|
||||
let mut path_index = HashMap::new();
|
||||
for (bind, dep) in &module.import_modules {
|
||||
let dep_exports = collect_module_tree(dep, flat);
|
||||
let child_scope = *next_scope;
|
||||
*next_scope += 1;
|
||||
let dep_exports = collect_module_tree(dep, flat, child_scope, next_scope);
|
||||
for (rel, idx) in dep_exports {
|
||||
let mut full = Vec::with_capacity(rel.len() + 1);
|
||||
full.push(bind.clone());
|
||||
@@ -126,7 +140,7 @@ fn collect_module_tree(module: &Module, flat: &mut Vec<FlatFun>) -> HashMap<Vec<
|
||||
|
||||
let mut exports = HashMap::new();
|
||||
for fun in &module.functions {
|
||||
let id = collect_fun(fun, None, flat, &path_index, &uses);
|
||||
let id = collect_fun(fun, None, flat, &path_index, &uses, scope);
|
||||
export_tree(&mut exports, &[], id, flat);
|
||||
}
|
||||
exports
|
||||
@@ -152,6 +166,7 @@ fn collect_fun(
|
||||
flat: &mut Vec<FlatFun>,
|
||||
path_index: &HashMap<Vec<String>, usize>,
|
||||
uses: &HashMap<String, Vec<String>>,
|
||||
scope: u32,
|
||||
) -> usize {
|
||||
let id = flat.len();
|
||||
flat.push(FlatFun {
|
||||
@@ -166,10 +181,11 @@ fn collect_fun(
|
||||
span: fun.span,
|
||||
path_index: path_index.clone(),
|
||||
uses: uses.clone(),
|
||||
scope,
|
||||
});
|
||||
let mut child_ids = Vec::new();
|
||||
for child in &fun.children {
|
||||
child_ids.push(collect_fun(child, Some(id), flat, path_index, uses));
|
||||
child_ids.push(collect_fun(child, Some(id), flat, path_index, uses, scope));
|
||||
}
|
||||
flat[id].children = child_ids;
|
||||
id
|
||||
@@ -323,28 +339,42 @@ impl<'a> FnCompiler<'a> {
|
||||
s
|
||||
}
|
||||
|
||||
/// BoundCall-equivalent bare-name resolve (G02): only the caller's module
|
||||
/// scope. Imported functions are reachable solely via `path_index` paths.
|
||||
fn resolve_call(&self, name: &str) -> Result<usize> {
|
||||
let fun = &self.flat[self.fn_id];
|
||||
let scope = fun.scope;
|
||||
for &cid in &fun.children {
|
||||
if self.flat[cid].name == name {
|
||||
if self.flat[cid].name == name && self.flat[cid].scope == scope {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
let mut parent = fun.parent;
|
||||
while let Some(pid) = parent {
|
||||
if self.flat[pid].scope != scope {
|
||||
break;
|
||||
}
|
||||
for &cid in &self.flat[pid].children {
|
||||
if self.flat[cid].name == name {
|
||||
if self.flat[cid].name == name && self.flat[cid].scope == scope {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
parent = self.flat[pid].parent;
|
||||
}
|
||||
let mut matches = Vec::new();
|
||||
for (i, f) in self.flat.iter().enumerate() {
|
||||
if f.parent.is_none() && f.name == name {
|
||||
return Ok(i);
|
||||
if f.parent.is_none() && f.scope == scope && f.name == name {
|
||||
matches.push(i);
|
||||
}
|
||||
}
|
||||
Err(self.err(format!("unknown function `{name}`")))
|
||||
match matches.as_slice() {
|
||||
[only] => Ok(*only),
|
||||
[] => Err(self.err(format!("unknown function `{name}`"))),
|
||||
_ => Err(self.err(format!(
|
||||
"ambiguous function `{name}`: {} candidates in this module; use an explicit path",
|
||||
matches.len()
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_stmt(&mut self, stmt: &Stmt) -> Result<()> {
|
||||
@@ -650,7 +680,15 @@ impl<'a> FnCompiler<'a> {
|
||||
|
||||
match self.resolve_call(&call.callee) {
|
||||
Ok(fid) => self.compile_user_call(fid, &call, as_stmt),
|
||||
Err(_) => self.compile_plugin_call(&call, as_stmt),
|
||||
Err(err) => {
|
||||
// Ambiguous bare names must not fall through to PluginCall (G02).
|
||||
let msg = format!("{err:#}");
|
||||
if msg.contains("ambiguous function") {
|
||||
return Err(err);
|
||||
}
|
||||
// Unresolved bare names may still be runtime plugins.
|
||||
self.compile_plugin_call(&call, as_stmt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,24 +99,6 @@ fn s0_06a_missing_import_rejected() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn s0_06b_ambiguous_bare_name_tree_rejects() {
|
||||
let (code, stdout, stderr) = run_backend(
|
||||
"tests/executable_knowledge/baseline/06-reject/ambiguous.mq.md",
|
||||
"tree",
|
||||
);
|
||||
assert_ne!(code, 0, "tree should reject bare ambiguous name; stdout={stdout}");
|
||||
assert!(
|
||||
stderr.contains("商品小计"),
|
||||
"tree stderr should name the unresolved function: {stderr}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Baseline defect fingerprint (S2 BoundCall must flip this to a hard failure).
|
||||
///
|
||||
/// Today bytecode silently picks one of the same-named imports (`60` from A or
|
||||
/// `120` from B — order is not stable). When fixed, change this test to
|
||||
/// `assert_ne!(code, 0)` on both backends.
|
||||
#[test]
|
||||
fn s2_g07_mlsp_bind_execute_reuses_existing_capability() {
|
||||
// Stub/no-LLM REUSE path (G07/G09 subset): find → bind → execute, no new source.
|
||||
@@ -418,19 +400,20 @@ fn s1_g01_semantic_edges_not_overwritten_by_ekc() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn s0_06b_bytecode_ambiguous_wrongly_resolves_baseline_defect() {
|
||||
let (code, stdout, stderr) = run_backend(
|
||||
"tests/executable_knowledge/baseline/06-reject/ambiguous.mq.md",
|
||||
"bytecode",
|
||||
);
|
||||
assert_eq!(
|
||||
code, 0,
|
||||
"baseline defect: bytecode still wrongly resolves; if this fails with \
|
||||
a rejection, BoundCall may have landed — flip test to expect failure. stderr={stderr}"
|
||||
);
|
||||
let out = stdout.trim_end();
|
||||
assert!(
|
||||
out == "60" || out == "120",
|
||||
"baseline defect fingerprint: silent pick of A(60) or B(120), got {out:?}"
|
||||
);
|
||||
fn s0_06b_ambiguous_bare_name_rejected_both_backends() {
|
||||
// G02: bare same-named imports must not silently bind (Tree + Bytecode).
|
||||
for backend in ["tree", "bytecode"] {
|
||||
let (code, stdout, stderr) = run_backend(
|
||||
"tests/executable_knowledge/baseline/06-reject/ambiguous.mq.md",
|
||||
backend,
|
||||
);
|
||||
assert_ne!(
|
||||
code, 0,
|
||||
"{backend} must reject bare ambiguous name; stdout={stdout}"
|
||||
);
|
||||
assert!(
|
||||
stderr.contains("商品小计"),
|
||||
"{backend} stderr should name the unresolved function: {stderr}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -16,16 +16,11 @@
|
||||
| 04 | `baseline/04-access-loop/` | implemented | 取元+遍历输出 | `import json` + `access`/`遍历` 写法(与现网一致) |
|
||||
| 05 | `baseline/05-policy-draft/` | **design** | 可试跑 → `113`;**不**宣称税率事实已证实 | 叙述中的来源意图(尚未有强制 schema) |
|
||||
| 06a | `baseline/06-reject/missing-import.mq.md` | expected_failure | 双后端均非 0 | 0(正确拒绝) |
|
||||
| 06b | `baseline/06-reject/ambiguous.mq.md` | expected_failure / **基线缺陷** | Tree 拒绝;Bytecode **误返回 `120`**(见下) | — |
|
||||
| 06b | `baseline/06-reject/ambiguous.mq.md` | expected_failure | **双后端均拒绝**裸名同名(G02) | — |
|
||||
|
||||
### 基线缺陷(须在 S1/S2 消除)
|
||||
### 同名裸调用(G02)
|
||||
|
||||
`06-reject/ambiguous.mq.md` 同时 `import` 了 `a` 与 `b`(同名 `商品小计`),正文用**裸名**调用。
|
||||
|
||||
- Tree:`unknown function \`商品小计\``(正确拒绝)
|
||||
- Bytecode:静默选中 A 或 B,输出 `60` 或 `120`(**顺序不稳定**)
|
||||
|
||||
这违反「同名不得误连 / 不得静默选目标」。S2 BoundCall 落地后,双后端均应失败或要求消歧路径;届时翻转对应测试。
|
||||
`06-reject/ambiguous.mq.md` 同时 `import` 了 `a` 与 `b`(同名 `商品小计`),正文用**裸名**调用。双后端均须拒绝;正确写法是 `a.商品小计` / `b.商品小计` 等显式路径。
|
||||
|
||||
## 不计为成功的项
|
||||
|
||||
@@ -37,15 +32,15 @@
|
||||
|
||||
| 切片 | 状态 | 证据 |
|
||||
|---|---|---|
|
||||
| S0 | **done** | `s0_*` 八项(含 bytecode 同名误连缺陷指纹) |
|
||||
| S0 | **done** | `s0_*` 基线正负例 |
|
||||
| S1/G01 | **done** | `graph/` 权威 + `ekc/graph/` 检索分离 |
|
||||
| S1/G03 | **done** | 步骤 kind ≠ 全 call;`steps.json`;view `#stmt-` 导航 |
|
||||
| S1 同名 | **done** | `08-same-name` resolve→A + 运行 `60` |
|
||||
| S2/G06 | **done** | 包装继承 `fs.write`;未知不标 pure |
|
||||
| S2/G07 子集 | **done** | MLSP bind 含 `symbol_id`;execute 按源行绑定;pure 拒非纯 |
|
||||
| S2/G02 BoundCall | **待办** | Tree/Bytecode 仍按名查找;`06b` bytecode 缺陷待翻转 |
|
||||
| S2/G02 BoundCall | **done(作用域子集)** | Bytecode 裸名解析不跨 import scope;`06b` 双后端拒绝 |
|
||||
|
||||
## 与后续切片的关系
|
||||
|
||||
- S2 BoundCall:双后端只消费绑定目标 → 翻转 `s0_06b_bytecode_ambiguous_*`
|
||||
- S2 后续:sema 路径 BoundCall 记录、Agent eng_reuse 强制 bind
|
||||
- S3+:局部编辑、来源适用性、整代快照 — 见缺口路线图
|
||||
Reference in new issue
Block a user