feat: Phase 9 gaps closed — ApprovalService.Decide convergence, execlog fold, execworker poller
Some checks are pending
ci / build-test (push) Waiting to run
ci / docker-build (push) Waiting to run

- ApprovalService (core/app/approval.go) + ApprovalRepo (postgres adapter) with
  full decide transaction: HMAC token verify, approval flip, execution un-gate,
  session-scoped window keys (+session suffix matching GovernanceStore gate),
  nomos session flip, audit+event on failure abort. httpapi DecideApproval now
  a thin presenter delegating to the service. ListPending payload format fixed
  (json.Unmarshal not raw-wrap).
- execlog folded into postgres adapter: internal/execlog deleted, NewExecutionLog
  / ReadExecutionLog live in the db package, callers updated (mcp, httpapi).
- execworker poller over ExecutionService.DispatchQueued: advisory lock leak
  fixed (defer/recover per execution), correlation_id preserved via Finalize
  event emission (ExecRunRepo.Finalize now emits execution.{status} with
  correlation_id from the row).
- Phase 8 session export-rename completed: Store, New, and all 53 methods
  exported; cmd/nomos/ agent.go fixed to use session.PendingContinuation etc.
- Coverage gates: ExecutionService.Submit 93.1%, PolicyService.Decide 100%.
- Plans index updated, VERSION bumped to 0.36.0.
This commit is contained in:
2026-08-16 12:29:59 +02:00
parent 986937799a
commit b98d7c24bf
27 changed files with 1161 additions and 600 deletions

View File

@@ -281,3 +281,135 @@ func TestExecutionSubmitHostHint(t *testing.T) {
t.Errorf("default hint missing: %s", d.Message)
}
}
func TestQueuedCommandExtraction(t *testing.T) {
cases := map[string]string{
`run:{"command":"df -h","purpose":"check"}`: "df -h",
`systemctl restart caddy`: "systemctl restart caddy",
`pct:{"command":"pct list"}`: "pct list",
`apt_upgrade:not-json`: "apt_upgrade:not-json",
`plain command`: "plain command",
}
for in, want := range cases {
if got := queuedCommand(in); got != want {
t.Errorf("queuedCommand(%q) = %q, want %q", in, got, want)
}
}
}
func TestExecutionDispatchQueued(t *testing.T) {
rec, exec, _ := newExecDeps(t)
exec.Results = []ports.ExecResult{{Output: "uptime output"}}
res, err := svcFor(t, rec, exec).DispatchQueued(context.Background(), targetID, "lxc:x", `run:{"command":"uptime"}`)
if err != nil {
t.Fatalf("DispatchQueued: %v", err)
}
if res != "uptime output" {
t.Errorf("result = %q", res)
}
if len(rec.Running) != 1 || len(rec.Finalized) != 1 {
t.Fatalf("running=%d finalized=%d", len(rec.Running), len(rec.Finalized))
}
if rec.Finalized[0].Status != "completed" {
t.Errorf("finalized = %+v", rec.Finalized[0])
}
}
func TestExecutionDispatchQueuedFailure(t *testing.T) {
rec, exec, _ := newExecDeps(t)
exec.Results = []ports.ExecResult{{Output: "boom", Err: errors.New("exit 2")}}
_, err := svcFor(t, rec, exec).DispatchQueued(context.Background(), targetID, "lxc:x", "uptime")
if err == nil {
t.Fatal("expected dispatch error")
}
if rec.Finalized[0].Status != "failed" {
t.Errorf("finalized = %+v, want failed", rec.Finalized[0])
}
}
func TestExecutionDispatchQueuedResolverFailure(t *testing.T) {
rec, exec, _ := newExecDeps(t)
failing := &portstest.FakeResolver{Err: errors.New("no route")}
svc := NewExecutionService(NewPolicyService(portstest.NewGovernanceStore()), exec, failing, rec)
_, err := svc.DispatchQueued(context.Background(), targetID, "lxc:x", "uptime")
if err == nil {
t.Fatal("expected resolver error")
}
if len(exec.Calls) != 0 {
t.Error("no execution on resolve failure")
}
if rec.Finalized[0].Status != "failed" {
t.Errorf("finalized = %+v, want failed", rec.Finalized[0])
}
}
func TestExecutionSubmitNoSessionGetsCorrelationID(t *testing.T) {
rec, exec, svc := newExecDeps(t)
exec.Results = []ports.ExecResult{{Output: "ok"}}
res := svc.Submit(context.Background(), ExecutionSubmitCmd{
AgentID: agentID, TargetID: targetID, TargetSlug: "lxc:x",
Command: "df -h", // auto-run, no session
})
if res.Err != nil {
t.Fatalf("submit: %v", res.Err)
}
if len(rec.Created) != 1 || rec.Created[0].CorrelationID == "" {
t.Errorf("created = %+v, want a correlation id", rec.Created)
}
}
func TestExecutionSubmitAsyncStarted(t *testing.T) {
rec, exec, _ := newExecDeps(t)
exec.Results = []ports.ExecResult{{Output: "sleeping"}}
store := portstest.NewGovernanceStore()
store.PlanSessions[sessionOK] = true
store.Assent[string(agentID)+"/"+sessionOK] = true
svc := NewExecutionService(NewPolicyService(store), exec, &portstest.FakeResolver{Addr: "10.0.0.1", User: "root"}, rec)
res := svc.Submit(context.Background(), ExecutionSubmitCmd{
AgentID: agentID, TargetID: targetID, TargetSlug: "lxc:x",
Command: "sleep 60", SessionID: sessionOK, Async: true,
})
if !res.AsyncStarted {
t.Fatalf("result = %+v, want async started", res)
}
if res.ExecutionID == "" {
t.Fatal("async submission must return the execution id synchronously")
}
}
func TestExecutionSubmitAutoRunQueueResume(t *testing.T) {
rec, exec, _ := newExecDeps(t)
exec.Results = []ports.ExecResult{{Output: "done"}}
// A gated command queues; the same command with a live assent window
// auto-runs instead — the window route switch in Submit.
store := portstest.NewGovernanceStore()
store.PlanSessions[sessionOK] = true
store.Assent[string(agentID)+"/"+sessionOK] = true
svc := NewExecutionService(NewPolicyService(store), exec, &portstest.FakeResolver{Addr: "10.0.0.1", User: "root"}, rec)
res := svc.Submit(context.Background(), ExecutionSubmitCmd{
AgentID: agentID, TargetID: targetID, TargetSlug: "lxc:x",
Command: "systemctl enable caddy", SessionID: sessionOK,
})
if res.Decision.Action != DecisionAuto {
t.Fatalf("decision = %+v, want auto (via assent window)", res.Decision)
}
if len(rec.Queued) != 0 {
t.Errorf("window route must not queue, got %+v", rec.Queued)
}
}
// svcFor builds a service over the given recorder/executor with a plan-backed
// governance store.
func svcFor(t *testing.T, rec *portstest.ExecutionRecorder, exec *portstest.RecordingExecutor) *ExecutionService {
t.Helper()
store := portstest.NewGovernanceStore()
store.PlanSessions[sessionOK] = true
return NewExecutionService(NewPolicyService(store), exec, &portstest.FakeResolver{Addr: "10.1.1.1", User: "root"}, rec)
}