# 結構缺陷計算：以程式語義圖進行非智能化 Bug 與異常偵測

**Structural Defect Computation: Non-Intelligent Bug and Anomaly Detection over Program Semantic Graphs**

**系列：** 程式底空間與本體反演系列（Program Base Space and Ontology Inversion, PBSOI）  
**篇次：** Paper 06  
**版本：** v0.1  
**作者：** Neo.K  
**機構：** EveMissLab  
**日期：** 2026-09-14  
**狀態：** 缺陷計算理論初稿／Deterministic Detector Framework

**公司：** 一言諾科技有限公司  

---

## 摘要

前述 PBSOI 系列已將程式由原始碼表面逐步提升為程式底空間、程式碼誘導語義圖（CISG）、可觀測程式本體與可執行語義圖（ESG）。一旦程式被表示為具有節點型別、關係型別、狀態、效果、邊界、來源與不確定性的可計算結構，另一個直接問題隨之出現：**Bug 是否仍必須主要依賴人類或 AI 閱讀程式碼後才被理解，還是其中一部分其實可以被重新表述為圖論、約束、狀態可達性與一致性檢查問題？**

本文提出「結構缺陷計算」（Structural Defect Computation, SDC），將部分程式缺陷定義為語義圖中的非法結構、不可滿足約束、異常可達性、非法狀態轉換、邊界穿越、效果洩漏或設計—實作不一致。其基本形式為：

$$
D
:
(G,K,\mathcal C)
\rightarrow
\Delta
$$

其中：

- $G$：CISG 或更完整的程式語義圖；
- $K$：型別、契約、政策與不變量；
- $\mathcal C$：分析語境；
- $\Delta$：偵測到的缺陷、異常、風險或未知集合。

本文首先區分「缺陷」（defect）、「異常」（anomaly）與「熱點」（hotspot），避免將高 fan-out、複雜度或 unresolved boundary 誤報為 Bug。其後提出結構缺陷分類：可達性缺陷、依賴缺陷、狀態缺陷、資料流缺陷、效果缺陷、邊界缺陷、契約缺陷、時間與並行缺陷，以及設計—實作偏差。本文進一步建立 detector 的 soundness、completeness、false-positive cost、evidence obligation 與 uncertainty preservation 原則。

本文主張，AI 可以協助命名、解釋、排序與生成修復候選，但一個 deterministic structural detector 應能在不依賴一般智能的情況下回答：

> 哪個形式規則被違反？  
> 哪些節點與邊構成證據？  
> 哪個 revision 出現此缺陷？  
> 該結論是已證明、強推論、弱推論，還是仍未知？

本系列先前的 RepoLumen CISG PoC 已執行 mutation test：人工注入 mutable default、unreachable statement、bare exception 與 import cycle 四類結構錯誤，第一版 detector 對四類均成功觸發；同時，原始基線中的 high-call-fanout 與 boundary-heavy 結構只被標示為 informational hotspot，而未被誤稱為 Bug。本文將此結果視為「部分缺陷可以直接在反演圖上機械檢測」的初步工程證據。

本文的核心命題是：

> **當 Bug 可以被表示為結構約束的違反時，偵測 Bug 的核心問題就不再是『模型有沒有看懂程式碼』，而是『我們是否已經恢復了足夠的程式結構，以及是否能在其上定義正確的不變量與判定器』。**

---

## 關鍵詞

結構缺陷計算、Structural Defect Computation、SDC、Bug Detection、CISG、程式語義圖、靜態分析、可達性、約束檢查、狀態不變量、效果分析、架構漂移、Deterministic Analysis、Program Verification

---

# 一、問題的提出

## 1.1 「找 Bug」不應被視為單一問題

一般語言中常說：

> 這段程式有 Bug。

但「Bug」可能指完全不同的問題：

- 語法錯誤；
- 型別錯誤；
- 未初始化值；
- dead code；
- import cycle；
- 資源洩漏；
- race condition；
- API contract mismatch；
- security boundary violation；
- 性能退化；
- 邏輯結果錯誤；
- 使用者意圖未被實現。

因此：

$$
\text{Bug}
$$

不是一種單一型別。

更合理地，可定義缺陷空間：

$$
\Delta
=
\Delta_{\mathrm{syntax}}
\cup
\Delta_{\mathrm{structure}}
\cup
\Delta_{\mathrm{data}}
\cup
\Delta_{\mathrm{state}}
\cup
\Delta_{\mathrm{effect}}
\cup
\Delta_{\mathrm{contract}}
\cup
\Delta_{\mathrm{temporal}}
\cup
\Delta_{\mathrm{intent}}
$$

不同缺陷需要不同證據與判定方法。

---

## 1.2 為什麼 CISG 改變了問題形式

若程式主要被當成文字：

$$
C
$$

那麼 detector 通常必須從文字重新解析出：

- 作用域；
- 呼叫；
- 狀態；
- 型別；
- 控制；
- 資料依賴。

但若已有：

$$
G_C
=
\rho(C)
$$

則大量前置工作已被集中於統一結構中。

此時 Bug detection 可以改寫為：

$$
D(G_C,K)
\rightarrow
\Delta
$$

這表示：

> 分析器不必每次重新從程式碼文字理解整個專案，而可以直接對恢復後的語義圖做結構計算。

---

# 二、缺陷、異常與熱點必須分開

## 2.1 缺陷

本文定義：

$$
\delta\in\Delta_{\mathrm{defect}}
$$

當且僅當存在一個明確規則：

$$
k\in K
$$

且圖：

$$
G
$$

違反：

$$
G\not\models k
$$

例如：

$$
\text{AcyclicImport}
$$

要求：

$$
\operatorname{ImportGraph}(G)
$$

為 DAG。

若存在 cycle：

$$
A\rightarrow B\rightarrow C\rightarrow A
$$

則：

$$
G\not\models
\operatorname{AcyclicImport}
$$

可形成 deterministic defect。

---

## 2.2 異常

異常不必等於錯誤。

定義：

$$
\delta\in\Delta_{\mathrm{anomaly}}
$$

表示：

$$
G[S]
$$

與已知正常結構分布顯著不同，但沒有足夠規則證明它非法。

例如：

- 某 module 的 outbound calls 遠高於其他 module；
- 某 function 的狀態寫入範圍突然擴大；
- 某 revision 出現全新跨層 dependency。

因此：

$$
\text{Anomaly}
\not\Rightarrow
\text{Bug}
$$

---

## 2.3 熱點

熱點表示：

$$
\operatorname{Cost}(G[S])
$$

或：

$$
\operatorname{RiskSurface}(G[S])
$$

較高。

例如：

- high fan-out；
- high fan-in；
- many external boundaries；
- high mutation count；
- high centrality。

熱點只表示：

> 值得優先看。

不表示：

> 已經錯了。

因此：

$$
\boxed{
\text{Defect}
\neq
\text{Anomaly}
\neq
\text{Hotspot}
}
$$

---

# 三、結構缺陷計算的基本模型

## 3.1 Detector

定義：

$$
D
=
\{
d_1,
d_2,
\ldots,
d_n
\}
$$

其中每個：

$$
d_i
:
(G,K_i,\mathcal C_i)
\rightarrow
R_i
$$

輸出：

$$
R_i
=
(
s,
\delta,
E,
q
)
$$

其中：

- $s$：status；
- $\delta$：缺陷型別；
- $E$：證據集合；
- $q$：可信度或判定層級。

---

## 3.2 最小判定狀態

本文建議：

$$
s
\in
\{
\text{violated},
\text{satisfied},
\text{unknown},
\text{not-applicable}
\}
$$

若 analysis coverage 不足，應輸出：

$$
\text{unknown}
$$

而不是：

$$
\text{satisfied}
$$

因此：

$$
\boxed{
\text{No evidence of violation}
\neq
\text{Proof of correctness}
}
$$

---

# 四、可達性缺陷

## 4.1 Unreachable Node

若入口集合：

$$
V_{\mathrm{entry}}
$$

與可達集：

$$
R^+
(
V_{\mathrm{entry}}
)
$$

已知，且：

$$
v
\notin
R^+
(
V_{\mathrm{entry}}
)
$$

則 $v$ 是候選 unreachable node。

但仍需考慮：

- reflection；
- plugin entry；
- external callback；
- test-only entry。

因此：

$$
\operatorname{Unreachable}_{\mathrm{static}}
$$

不必直接等於：

$$
\operatorname{DeadCode}
$$

---

## 4.2 Post-Return Unreachable

若控制流中：

$$
v_{\mathrm{return}}
\prec
v_x
$$

且不存在合法 edge 使 $v_x$ 可達，則：

$$
v_x
$$

可被直接標記為不可達語句。

這是一類局部、強可判定缺陷。

---

# 五、依賴缺陷

## 5.1 Cycle

對依賴圖：

$$
G_D
=
(V_D,E_D)
$$

若架構契約要求 DAG：

$$
G_D\in\operatorname{DAG}
$$

但：

$$
\exists
v_1,\ldots,v_n
:
v_1\rightarrow v_2\rightarrow\cdots\rightarrow v_n\rightarrow v_1
$$

則形成 cycle defect。

---

## 5.2 Layer Violation

若定義 layer function：

$$
\lambda
:
V
\rightarrow
\mathbb N
$$

並要求：

$$
(u,v)\in E_{\mathrm{dep}}
\Rightarrow
\lambda(u)\geq\lambda(v)
$$

但存在：

$$
\lambda(u)<\lambda(v)
$$

則形成：

$$
\Delta_{\mathrm{layer}}
$$

---

## 5.3 Forbidden Dependency

若政策：

$$
K_{\mathrm{dep}}
=
\{
(A,B)\mid A\not\rightarrow B
\}
$$

而 CISG 出現：

$$
A\rightarrow B
$$

則可直接產生 evidence-grounded violation。

---

# 六、資料流缺陷

## 6.1 Use Before Definition

對變數版本：

$$
x_i
$$

若：

$$
\operatorname{use}(x_i)
$$

在所有合法 path 上都不存在先行：

$$
\operatorname{define}(x_i)
$$

則形成候選缺陷。

---

## 6.2 Undefined Merge

若控制流合併：

$$
\phi(x_1,x_2)
$$

但某 branch 並未定義對應值，則：

$$
\operatorname{PHI}
$$

不完備。

---

## 6.3 Tainted Flow

定義來源：

$$
S_{\mathrm{taint}}
$$

與危險 sink：

$$
S_{\mathrm{sink}}
$$

若存在 path：

$$
S_{\mathrm{taint}}
\leadsto
S_{\mathrm{sink}}
$$

且路徑中沒有合法 sanitizer：

$$
S_{\mathrm{sanitize}}
$$

則形成候選資料流風險。

---

# 七、狀態缺陷

## 7.1 Illegal State Transition

定義合法狀態圖：

$$
K_S
=
(S,E_S)
$$

若實作圖產生：

$$
s_i
\rightarrow
s_j
$$

但：

$$
(s_i,s_j)
\notin
E_S
$$

則：

$$
G\not\models K_S
$$

形成狀態缺陷。

---

## 7.2 Missing Transition

若契約要求：

$$
\forall s\in S_{\mathrm{active}},
\quad
\exists s'
:
s\rightarrow s'
$$

但某狀態沒有合法 outgoing transition，可能形成 stuck state。

---

## 7.3 Mutable Default

對某些語言或 framework，若 function default 物件：

$$
d
$$

在多次呼叫間共享 mutable state，且規則禁止：

$$
\operatorname{MutableDefault}(d)
$$

則可直接產生 deterministic defect。

這類問題不需要 AI 理解「作者想做什麼」。

---

# 八、效果缺陷

## 8.1 Effect Leak

假設 pure region：

$$
R_{\mathrm{pure}}
$$

要求：

$$
\forall v\in R_{\mathrm{pure}},
\quad
\operatorname{Effect}(v)=\varnothing
$$

但存在：

$$
v
\xrightarrow{\mathrm{effect}}
B_{\mathrm{network}}
$$

則：

$$
\Delta_{\mathrm{effect-leak}}
$$

成立。

---

## 8.2 Resource Acquire / Release

若：

$$
\operatorname{Acquire}(r)
$$

在某 path 上發生，但該 path 沒有：

$$
\operatorname{Release}(r)
$$

則可能形成 resource leak。

更正式可寫為：

$$
\exists p
:
\operatorname{Acquire}(r)\in p
\land
\operatorname{Release}(r)\notin p
$$

---

## 8.3 Privilege Violation

若 node：

$$
v
$$

執行：

$$
e
$$

需要 capability：

$$
c
$$

但：

$$
c\notin\operatorname{Caps}(v)
$$

則：

$$
\operatorname{CapabilityViolation}(v,e)
$$

成立。

---

# 九、錯誤與例外缺陷

## 9.1 Bare Exception

某些語言模式下：

```text
except:
```

會吞掉過多錯誤類型。

若專案規則禁止：

$$
\operatorname{CatchAllWithoutPolicy}
$$

則 detector 可以直接標記。

---

## 9.2 Unhandled Error Edge

若：

$$
o
\xrightarrow{\mathrm{error}}
B_{\mathrm{uncaught}}
$$

且 contract 要求：

$$
\operatorname{Handled}(o)
$$

則形成：

$$
\Delta_{\mathrm{unhandled}}
$$

---

## 9.3 Error Semantic Collapse

若轉換系統把：

$$
\operatorname{throw}
$$

錯誤正規化成普通：

$$
\operatorname{return}
$$

則這不是原程式 Bug，而是：

$$
\Delta_{\mathrm{analysis}}
$$

即分析／反演器本身的缺陷。

---

# 十、邊界缺陷

## 10.1 Forbidden Boundary Crossing

若安全區域：

$$
Z_{\mathrm{secure}}
$$

禁止直接呼叫：

$$
B_{\mathrm{external}}
$$

但 CISG 中存在：

$$
v\in Z_{\mathrm{secure}}
\rightarrow
B_{\mathrm{external}}
$$

則形成 boundary violation。

---

## 10.2 Missing Validation Boundary

若所有 external input：

$$
x\in X_{\mathrm{external}}
$$

必須經：

$$
V_{\mathrm{validate}}
$$

而某 path：

$$
x
\leadsto
S_{\mathrm{critical}}
$$

沒有通過任何 validation node，則可能形成：

$$
\Delta_{\mathrm{validation}}
$$

---

# 十一、契約缺陷

## 11.1 Precondition Violation

若 function：

$$
f
$$

要求：

$$
P(x)
$$

但 caller path 可以提供：

$$
\neg P(x)
$$

則 contract 可能被破壞。

---

## 11.2 Postcondition Violation

若：

$$
f
$$

保證：

$$
Q(y)
$$

但 symbolic / exhaustive bounded analysis 找到：

$$
\exists x:
\neg Q(f(x))
$$

則形成反例。

---

## 11.3 Schema Mismatch

若 producer：

$$
A
$$

輸出 schema：

$$
S_A
$$

consumer：

$$
B
$$

要求：

$$
S_B
$$

而：

$$
S_A
\not\sqsubseteq
S_B
$$

則形成 interface mismatch。

---

# 十二、時間與並行缺陷

## 12.1 Happens-Before Violation

若要求：

$$
A
\prec_{\mathrm{hb}}
B
$$

但實作允許：

$$
B
\prec_{\mathrm{hb}}
A
$$

則可能形成 race 或 ordering defect。

---

## 12.2 Double Write Conflict

若兩個 concurrent operation：

$$
w_1,w_2
$$

都對同一 state region：

$$
S
$$

執行 write：

$$
w_1\parallel w_2
$$

且沒有 synchronization relation，則：

$$
\operatorname{WriteConflict}(w_1,w_2)
$$

為候選缺陷。

---

## 12.3 Deadlock Cycle

定義 wait-for graph：

$$
G_W
$$

若：

$$
T_1
\rightarrow
T_2
\rightarrow
\cdots
\rightarrow
T_n
\rightarrow
T_1
$$

則存在 deadlock candidate。

---

# 十三、設計—實作缺陷

## 13.1 三圖中的第一個差異

定義：

$$
G_I
$$

為 intent / design graph。

$$
G_C
$$

為 CISG。

若：

$$
G_I
\neq
G_C
$$

並不表示一定有 Bug。

因此需要政策集合：

$$
K_I
$$

判定哪些差異允許，哪些不允許。

---

## 13.2 Missing Implementation

若：

$$
e\in G_I
$$

但：

$$
e\notin G_C
$$

且該 edge 是 required：

$$
\operatorname{Required}(e)
$$

則可能是：

$$
\Delta_{\mathrm{missing-implementation}}
$$

---

## 13.3 Hidden Implementation

若：

$$
e\notin G_I
$$

但：

$$
e\in G_C
$$

且政策禁止未宣告跨層關係，則形成 architecture drift。

---

# 十四、Detector 必須輸出證據

## 14.1 最小 Evidence Contract

任何：

$$
\delta
$$

至少應具有：

$$
E(\delta)
=
(
V_\delta,
E_\delta,
K_\delta,
\Gamma_\delta
)
$$

其中：

- $V_\delta$：相關節點；
- $E_\delta$：相關邊；
- $K_\delta$：被違反規則；
- $\Gamma_\delta$：source provenance。

---

## 14.2 不接受「模型覺得」

如果結果只有：

> 這段看起來可能有問題。

但沒有：

$$
K_\delta
$$

與：

$$
E(\delta)
$$

則只能標為：

$$
\text{heuristic suspicion}
$$

而非 deterministic defect。

---

# 十五、Soundness 與 Completeness

## 15.1 Soundness

理想 detector：

$$
d
$$

若輸出 defect：

$$
d(G)=\delta
$$

希望：

$$
\Pr(
\delta
\text{ 真為缺陷}
)
$$

很高。

對可形式判定規則，甚至可要求：

$$
d(G)=\delta
\Rightarrow
G\not\models K
$$

---

## 15.2 Completeness

另一方面，希望：

$$
G\not\models K
\Rightarrow
d(G)
$$

能偵測。

但現實中因 analysis coverage、dynamic behavior、undecidability 等限制，不可能對所有程式取得完全完備。

因此 detector 應聲明：

$$
\operatorname{Scope}(d)
$$

---

## 15.3 Soundness 優先

對自動修復前置 detector，本文建議：

$$
\lambda_{\mathrm{false-positive}}
>
\lambda_{\mathrm{false-negative}}
$$

至少在高風險修復場景中如此。

因為錯誤宣告一個不存在的缺陷可能誘發不必要修改。

---

# 十六、未知性與保守判定

## 16.1 Unknown 必須是一級結果

若 dynamic dispatch 無法解析，則：

$$
d(G)
=
\text{unknown}
$$

可以是正確答案。

---

## 16.2 Unresolved Boundary 不是 Bug

若：

$$
A
\rightarrow
B_{\mathrm{unresolved}}
$$

本身只能表示：

> 目前不知道 target。

不能直接推出：

$$
\text{Bug}
$$

---

## 16.3 Coverage-Aware Verdict

可以定義：

$$
\operatorname{Verdict}
=
(
s,
c,
u
)
$$

其中：

- $s$：判定狀態；
- $c$：coverage；
- $u$：unresolved fraction。

---

# 十七、Mutation Testing 作為 Detector 驗證

## 17.1 原理

若已知 baseline：

$$
G_0
$$

無某 defect，人工注入：

$$
m_i
$$

得到：

$$
G_i
=
m_i(G_0)
$$

並知道：

$$
\delta_i
$$

應存在。

則可測：

$$
d_i(G_i)
=
\delta_i
$$

---

## 17.2 RepoLumen PoC

第一輪 mutation suite 注入：

1. mutable default；
2. unreachable statement；
3. bare exception；
4. import cycle。

結果：

$$
4/4
$$

全部觸發對應 detector。

同時 baseline 中的：

- high call fan-out；
- boundary heavy；

只標為 informational hotspot。

因此：

$$
\boxed{
\text{Hotspot 沒有被直接升格為 Bug}
}
$$

這是 detector 語義分層的重要驗證。

---

# 十八、從規則偵測到圖缺陷代數

## 18.1 缺陷作為 forbidden subgraph

某些缺陷可表示為：

$$
F_k
$$

即 forbidden subgraph pattern。

若：

$$
F_k
\hookrightarrow
G
$$

則：

$$
\delta_k
$$

成立。

---

## 18.2 缺陷作為缺失結構

某些缺陷反而是 required pattern 不存在。

若：

$$
R_k
\not\hookrightarrow
G
$$

則：

$$
\delta_k
$$

成立。

---

## 18.3 缺陷作為約束不可滿足

更一般：

$$
K(G)
=
\text{false}
$$

則產生：

$$
\delta_K
$$

因此可以統一寫為：

$$
\boxed{
\Delta
=
\operatorname{Violations}(G,K)
}
$$

---

# 十九、AI 在 SDC 中的位置

## 19.1 AI 不應成為唯一判定器

對 deterministic defect：

$$
\delta
$$

應優先由：

- graph algorithm；
- type checker；
- constraint solver；
- model checker；
- symbolic executor；

判定。

---

## 19.2 AI 適合做什麼

AI 可以協助：

- 將 defect 翻譯成人類可讀說明；
- 將低階 pattern 命名成較高階功能；
- 排序修復優先級；
- 生成 patch candidate；
- 產生新的 detector hypothesis；
- 推薦需要新增的 invariant。

---

## 19.3 AI 生成規則也必須被驗證

若 AI 提出：

$$
K_{\mathrm{new}}
$$

則不能直接進 authoritative detector。

應先：

$$
K_{\mathrm{new}}
\rightarrow
\text{test}
\rightarrow
\text{counterexample}
\rightarrow
\text{validation}
$$

之後才能升格。

---

# 二十、SDC 與傳統靜態分析的差異

## 20.1 不取代既有工具

本文不主張重新發明：

- lint；
- type checker；
- compiler warning；
- SAST；
- model checking；
- symbolic execution。

---

## 20.2 SDC 的定位

SDC 更接近：

$$
\boxed{
\text{將各種缺陷判定映射到同一個 evidence-grounded program graph 上}
}
$$

使：

- Bug；
- architecture drift；
- state inconsistency；
- boundary violation；
- effect leak；

可在同一 project-level graph 上互相組合。

---

# 二十一、缺陷歷史

## 21.1 缺陷不是只有當前狀態

對 revision：

$$
r_1,\ldots,r_n
$$

可建立：

$$
\Delta^{(r_1)}
\rightarrow
\Delta^{(r_2)}
\rightarrow
\cdots
\rightarrow
\Delta^{(r_n)}
$$

---

## 21.2 Defect Birth

定義：

$$
r_{\mathrm{birth}}(\delta)
=
\min
\{
r_i
\mid
\delta\in\Delta^{(r_i)}
\}
$$

---

## 21.3 Defect Death

$$
r_{\mathrm{death}}(\delta)
=
\min
\{
r_j>r_{\mathrm{birth}}
\mid
\delta\notin\Delta^{(r_j)}
\}
$$

這使 Bug 分析從：

> 現在有沒有？

擴展成：

> 何時出現、存在多久、何時被消除、是否復發？

---

# 二十二、缺陷影響域

## 22.1 Local Defect

對缺陷：

$$
\delta
$$

定義其 seed nodes：

$$
S_\delta
$$

---

## 22.2 Impact Closure

利用：

$$
\operatorname{Reach}^{+}(S_\delta)
$$

與：

$$
\operatorname{Reach}^{-}(S_\delta)
$$

估計：

- downstream risk；
- upstream caller；
- tests；
- state regions；
- external boundary。

---

## 22.3 Risk 不是 Bug Truth

影響域大：

$$
|\operatorname{Impact}(\delta)|
\gg 1
$$

只代表修復可能波及較廣。

不代表 defect 更「真」。

---

# 二十三、修復前的責任層定位

結構缺陷偵測之後，不能立刻修改所有受影響節點。

應先找：

$$
L^\ast
$$

即最小責任層。

可以定義：

$$
L^\ast
=
\arg\min_L
\{
\operatorname{RepairCost}(L)
+
\operatorname{GlobalDisturbance}(L)
\}
$$

subject to：

$$
\operatorname{Fix}(L)
\models
K
$$

這與「Maximum Local Precision + Minimum Global Disturbance」一致。

---

# 二十四、可證偽條件

## 24.1 無法區分 Bug 與 Hotspot

若系統大量把：

- high fan-out；
- high centrality；
- unresolved boundary；

直接報為 defect，則 SDC 的語義契約失敗。

---

## 24.2 無證據缺陷

若 detector 無法提供：

$$
E(\delta)
$$

則結果只能是 heuristic，而不能標為 verified defect。

---

## 24.3 Mutation Recall 過低

對已知 mutation corpus：

$$
M
$$

若：

$$
\operatorname{Recall}(D,M)
$$

長期過低，表示圖表示或 detector 規則不足。

---

## 24.4 False Positive 過高

若 baseline 大量出現：

$$
\Delta_{\mathrm{false}}
$$

則 deterministic detector 失去工程價值。

---

## 24.5 圖反演錯誤污染 Detector

若：

$$
\rho(C)
$$

產生錯誤 edge，導致：

$$
D(\rho(C))
$$

誤報，則問題可能在 POI，而非 detector。

因此應區分：

$$
\Delta_{\mathrm{program}}
$$

與：

$$
\Delta_{\mathrm{analysis}}
$$

---

# 二十五、核心命題

本文提出以下十個核心命題。

## 命題 1：缺陷結構化命題

部分程式 Bug 可以表示為：

$$
G\not\models K
$$

---

## 命題 2：非智能判定命題

對這類 defect，不需要一般智能即可完成：

$$
D(G,K)
$$

---

## 命題 3：缺陷—異常分離命題

$$
\text{Defect}
\neq
\text{Anomaly}
\neq
\text{Hotspot}
$$

---

## 命題 4：Evidence Obligation 命題

若：

$$
D(G)=\delta
$$

標為 verified，則：

$$
E(\delta)\neq\varnothing
$$

---

## 命題 5：Unknown First-Class 命題

$$
\text{unknown}
$$

是合法 detector 結果，不應被強迫轉成 true / false。

---

## 命題 6：Project-Level Composition 命題

不同 detector 可以在同一：

$$
G_C
$$

上組合，形成全域缺陷視圖。

---

## 命題 7：歷史缺陷命題

缺陷可以擁有：

$$
r_{\mathrm{birth}}
$$

與：

$$
r_{\mathrm{death}}
$$

因此 Bug 是時間結構，而不只是當前標記。

---

## 命題 8：影響域分離命題

$$
\operatorname{DefectTruth}
\neq
\operatorname{ImpactSize}
$$

---

## 命題 9：分析缺陷命題

錯誤的本體反演本身也可能形成：

$$
\Delta_{\mathrm{analysis}}
$$

不能將所有誤報歸咎於原程式。

---

## 命題 10：AI 後置命題

對可 deterministic 判定的缺陷：

$$
\text{Structure Detection}
\rightarrow
\text{AI Explanation / Repair}
$$

優先於：

$$
\text{AI Guess}
\rightarrow
\text{Defect Claim}
$$

---

# 二十六、後續工程路線

第一階段應建立 deterministic detector library：

$$
\mathcal D_0
$$

包含：

- unreachable；
- import cycle；
- mutable default；
- bare catch；
- layer violation；
- forbidden dependency；
- resource acquire/release；
- unresolved critical boundary。

第二階段：

$$
\mathcal D_1
$$

加入：

- SSA data-flow；
- taint；
- state invariant；
- effect policy；
- contract mismatch。

第三階段：

$$
\mathcal D_2
$$

加入：

- alias；
- concurrency；
- temporal property；
- race candidate；
- deadlock candidate。

所有 detector 都必須附：

- scope；
- evidence；
- limitations；
- mutation tests；
- baseline false-positive tests。

---

# 二十七、與下一篇的關係

Paper 07 將研究程式功能 motif。

Paper 06 的問題是：

> 哪些結構是不合法的？

Paper 07 的問題則是：

> 哪些合法結構反覆出現，並可以被辨識為某種功能形狀？

因此：

$$
\text{Defect Pattern}
$$

與：

$$
\text{Functional Motif}
$$

會成為同一語義圖上的兩種 pattern family。

一種表示：

$$
\text{不應存在的結構}
$$

另一種表示：

$$
\text{反覆存在且具有功能意義的結構}
$$

---

# 二十八、結論

本文提出「結構缺陷計算」SDC，主張當程式已被恢復為 evidence-grounded semantic graph 後，一部分 Bug 可以直接被重新表述為：

$$
\boxed{
\text{Graph Violation}
+
\text{Constraint Violation}
+
\text{State Reachability Violation}
}
$$

其基本形式為：

$$
D
:
(G,K,\mathcal C)
\rightarrow
\Delta
$$

這並不意味著所有 Bug 都可以被機械決定。

例如真正依賴模糊需求、人類審美、商業目的或未知外部世界狀態的問題，仍可能需要：

- 人類；
- AI；
- runtime evidence；
- domain knowledge。

但對具有明確結構規則的缺陷，真正重要的問題不再是：

> 哪一個模型最會讀程式碼？

而是：

> **我們是否已恢復足夠的程式底層結構，並將正確的不變量、契約與合法關係定義成可被機器判定的規則？**

因此，PBSOI 到此形成新的鏈：

$$
C
\xrightarrow{\rho}
G_C
\xrightarrow{D}
\Delta
$$

而：

$$
\Delta
$$

不是自然語言猜測，而是可以具有：

$$
\boxed{
\text{Rule}
+
\text{Evidence}
+
\text{Location}
+
\text{Revision}
+
\text{Confidence}
}
$$

的結構化缺陷物件。

在此架構下，AI 最有價值的位置不是取代 detector，而是站在 detector 之後：

$$
\boxed{
\text{Detect}
\rightarrow
\text{Explain}
\rightarrow
\text{Localize}
\rightarrow
\text{Repair}
\rightarrow
\text{Re-verify}
}
$$

這使「分析程式的程式」從程式理解工具進一步成為：

$$
\boxed{
\text{程式結構的機械化自檢層}
}
$$

---

# 參考與前置理論

1. Neo.K，《程式底空間：從原始碼表徵到計算關係的存在層》，PBSOI Paper 01。
2. Neo.K，《程式碼誘導語義圖：由實作生成而非由設計繪製的程式節點圖》，PBSOI Paper 02。
3. Neo.K，《程式本體反演：從可執行投影重建可觀測程式本體》，PBSOI Paper 03。
4. Neo.K，《共通程式底空間：跨語言語義正規化與表徵收斂》，PBSOI Paper 04。
5. Neo.K，《可執行語義圖：脫離原始碼後的程式重建與再執行》，PBSOI Paper 05。
6. Neo.K，《多重投影程式論：原始碼不再是程式本體》。
7. Neo.K，《從程式碼到意圖：程式概念的歷史轉換與後文本時代》。
8. EveMissLab，CISG RepoLumen PoC v0.1。
9. EveMissLab，Semantic Graph PoC v0.1。
