Add /graph G6: plan-boundary topology optimizer

A restricted optimizer pass now reviews the graph at plan boundaries —
right after initial planning and piggybacked on each replan version
boundary, never mid-execution. An optimizer subagent may emit four ops
over Waiting/Ready nodes only: remove_dep (delete a false dependency,
restoring parallelism — the highest-value edit), reorder (pending
priority for the serial scheduler), merge (fold two tiny nodes; specs
concatenate, deps union, dependents re-point, absorbed self-deps drop),
and split (2-3 focused replacements inheriting the original's deps and
dependents). The optimizer changes graph DATA only; the executor stays
pure deterministic Rust. KIGI_GRAPH_OPTIMIZER=0 disables it entirely.

apply_optimization enforces the contract twice: per-op checks
(pending-only targets, known ids, terminal node untouchable, dead-node
deps rejected as DeadDep, merge/split targets with non-pending
dependents rejected with the true reason instead of tripping the
immutable invariant later), then FINAL invariants — every non-pending
node byte-identical in the result, the gn-final gate rebuilt over all
survivors, node cap, whole-graph acyclicity, and a BIDIRECTIONAL status
re-derivation for pending nodes (adversarial review caught the critical
hole: a merge grafting unsatisfied deps onto a Ready node would
otherwise dispatch it ahead of its new prerequisites, since
recompute_ready is promote-only). Applied passes bump plan_version,
freeze an immutable baseline, and consume a slot of the SHARED replan
cap; an explicit {"ops": []} is a respected free no-op; any failure
degrades to keeping the current plan. Plumbing reuses a new shared
artifact-pass runner (stale-artifact delete, size cap, missing-file
fail-closed) extracted from the replanner.

Tests: remove_dep parallelism restore + loud no-such-dep, immutable and
terminal-node rejections across all four ops, merge/split dependent
rewiring incl. final-gate rebuild and intra-split dep resolution,
result-cycle rejection, dead-dep splits, Ready-demote-on-merge, and
three e2e flows — false-dep removal proven ACTUALLY parallel by the
held-reply fan-out gate, OPTIMIZER=0 spawning zero passes, and the
shared-cap guard. kigi-shell 4959 lib tests green; clippy clean.
This commit is contained in:
ZacharyZhang-NY committed 2026-07-20 20:21:49 -04:00
1 parent db05ed0751
commit 02cf5deebd
19 files changed
+1123

No files matched your search

@@ -742,6 +742,27 @@ impl GraphTracker {
self.recompute_ready();
}
/// Install an optimizer-transformed node set: bump `plan_version`,
/// consume a shared replan-cap slot, recompute readiness. The
/// validator already guaranteed immutable nodes are untouched.
pub fn install_optimized_nodes(&mut self, nodes: Vec<GraphNode>) {
let Some(state) = self.state.as_mut() else {
return;
};
state.nodes = nodes;
state.plan_version += 1;
state.replan_runs += 1;
push_history(
state,
GraphHistoryEntry::now(
GraphEvent::PlanningCompleted,
None,
Some(format!("optimized to v{}", state.plan_version)),
),
);
self.recompute_ready();
}
/// Drain pending discoveries WITHOUT replanning (cap exhausted):
/// they stay in history (queued there at capture time) only.
pub fn drain_discoveries_to_history(&mut self) -> usize {