From 3f4107435d5873ca3767f02eddc096fc70668487 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 11 Nov 2026 09:32:06 -0700 Subject: [PATCH 1/6] =?UTF-8?q?=E2=9C=A8=20workflowmodel:=20ingest=20Tekto?= =?UTF-8?q?n=20pipelines=20from=20a=20live=20cluster=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add pkg/workflowmodel/ingest/cluster: lists tekton.dev Pipelines, Tasks, PipelineRuns (newest N per pipeline) and Tekton Triggers objects from a kube context via kubectl, serialises them as cluster///.yaml and feeds the existing file-based Tekton ingester so there is one parser. Last run status per pipeline is recorded as an observed annotation and coverage finding, never as an inferred lifecycle element. Clear errors for missing CRDs, RBAC denials, unknown contexts and missing namespaces; caps on namespaces, runs, objects and bytes. Refs hivecommons/hive#17620 Signed-off-by: Andrew Anderson --- .../workflowmodel/ingest/cluster/cluster.go | 502 ++++++++++++++++++ .../ingest/cluster/cluster_test.go | 477 +++++++++++++++++ .../workflowmodel/ingest/cluster/kubectl.go | 106 ++++ 3 files changed, 1085 insertions(+) create mode 100644 src/pkg/workflowmodel/ingest/cluster/cluster.go create mode 100644 src/pkg/workflowmodel/ingest/cluster/cluster_test.go create mode 100644 src/pkg/workflowmodel/ingest/cluster/kubectl.go diff --git a/src/pkg/workflowmodel/ingest/cluster/cluster.go b/src/pkg/workflowmodel/ingest/cluster/cluster.go new file mode 100644 index 0000000000..9710f1d291 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/cluster/cluster.go @@ -0,0 +1,502 @@ +// Package cluster ingests Tekton resources from a live Kubernetes context +// (hivecommons/hive#17620, RFC phase 6). It lists Pipelines, Tasks, the +// latest PipelineRuns per Pipeline and Tekton Triggers resources through +// kubectl, serialises every object to YAML under a stable virtual path and +// hands those files to the existing file-based Tekton ingester, so there is +// exactly one Tekton parser. Run outcomes are observations, not inferences: +// they are reported alongside the model and recorded as annotations, never +// as inferred lifecycle elements. +package cluster + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "sort" + "strings" + + "gopkg.in/yaml.v3" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest" + "github.com/hivecommons/hive/pkg/workflowmodel/tekton" +) + +// Caps. A cluster is not a repository: without them an all-namespaces +// fetch against a busy CI cluster would pull thousands of PipelineRuns. +const ( + // DefaultRunsPerPipeline is how many of the newest PipelineRuns per + // Pipeline are kept when Source.RunsPerPipeline is zero. + DefaultRunsPerPipeline = 3 + // MaxRunsPerPipeline bounds Source.RunsPerPipeline. + MaxRunsPerPipeline = 20 + // MaxObjects is the most objects (after run trimming) one fetch keeps. + MaxObjects = 500 + // MaxNamespaces is the most namespaces an all-namespaces fetch may span. + MaxNamespaces = 25 + // MaxTotalBytes caps the YAML handed to the ingester. + MaxTotalBytes = 8 << 20 +) + +// Findings this package adds on top of the Tekton ingester's. +const ( + CodeTriggersCRDMissing = "triggers-crd-missing" + CodeNamespaceCapped = "namespaces-capped" + CodeRunsTrimmed = "runs-trimmed" + CodeObservedRun = "observed-run-status" +) + +// AnnotationObservedRun is the model annotation prefix under which the last +// observed PipelineRun status of each pipeline is recorded +// ( = ). It is an observation, not an +// inference, so it never carries Confidence. +const AnnotationObservedRun = "workflow.hivecommons.dev/observed-run/" + +// AnnotationSource records the cluster source label on the model. +const AnnotationSource = "workflow.hivecommons.dev/source" + +// Errors a caller can distinguish. The API maps them to clear responses. +var ( + // ErrNoCRD means the cluster has no tekton.dev Pipeline resource type. + ErrNoCRD = errors.New("tekton is not installed in this cluster (no pipelines.tekton.dev resource)") + // ErrForbidden means RBAC denied the list. + ErrForbidden = errors.New("kubectl was denied: the hive's credentials cannot list Tekton resources") + // ErrNoContext means the named context is not in the kubeconfig. + ErrNoContext = errors.New("kube context not found") + // ErrNamespaceRequired means neither Namespace nor AllNamespaces was set. + ErrNamespaceRequired = errors.New("a namespace is required (or set allNamespaces)") + // ErrTooLarge means a cap was exceeded. + ErrTooLarge = errors.New("cluster fetch exceeds the ingest size cap") + // ErrNoRunner means no kubectl runner was supplied. + ErrNoRunner = errors.New("no kubectl runner") +) + +// Source names what to fetch. +type Source struct { + // Context is the kubeconfig context; empty uses kubectl's current one. + Context string `json:"context,omitempty"` + // Namespace scopes the list. Required unless AllNamespaces is set. + Namespace string `json:"namespace,omitempty"` + // AllNamespaces lists every namespace, capped at MaxNamespaces. + AllNamespaces bool `json:"allNamespaces,omitempty"` + // Selector is a label selector applied to every list. + Selector string `json:"selector,omitempty"` + // RunsPerPipeline is how many newest runs per pipeline to keep + // (0 = DefaultRunsPerPipeline, capped at MaxRunsPerPipeline). + RunsPerPipeline int `json:"runsPerPipeline,omitempty"` +} + +// String is the source label recorded on the model: cluster:/. +func (s Source) String() string { + ctx := s.Context + if ctx == "" { + ctx = "current-context" + } + ns := s.Namespace + if s.AllNamespaces { + ns = "*" + } + return "cluster:" + ctx + "/" + ns +} + +// Runner executes kubectl with args and returns stdout. Implementations +// must include stderr in the error so the failure can be classified. +type Runner interface { + Run(ctx context.Context, args ...string) ([]byte, error) +} + +// RunnerFunc adapts a function to Runner. +type RunnerFunc func(ctx context.Context, args ...string) ([]byte, error) + +// Run calls f. +func (f RunnerFunc) Run(ctx context.Context, args ...string) ([]byte, error) { return f(ctx, args...) } + +// RunStatus is the last observed outcome of one pipeline. +type RunStatus struct { + Pipeline string `json:"pipeline"` + Namespace string `json:"namespace"` + Run string `json:"run"` + // Status is Succeeded, Running, Unknown, or the Tekton reason (Failed, + // Cancelled, …) when the Succeeded condition is false. + Status string `json:"status"` + Completed string `json:"completed,omitempty"` +} + +// Fetched is the raw harvest of one Fetch. +type Fetched struct { + Files []tekton.File + Runs []RunStatus + Namespaces []string + Findings []workflowmodel.Finding + Objects int +} + +const kindPipelineRun = "PipelineRun" + +// resources names the kubectl resource types fetched, in order. Pipelines +// are required (their absence means no Tekton); the rest are optional. +var resources = []struct { + plural string + kind string + required bool +}{ + {"pipelines.tekton.dev", "Pipeline", true}, + {"tasks.tekton.dev", "Task", false}, + {"pipelineruns.tekton.dev", kindPipelineRun, false}, + {"eventlisteners.triggers.tekton.dev", "EventListener", false}, + {"triggertemplates.triggers.tekton.dev", "TriggerTemplate", false}, + {"triggerbindings.triggers.tekton.dev", "TriggerBinding", false}, + {"triggers.triggers.tekton.dev", "Trigger", false}, +} + +// Fetch lists the Tekton resources src names through runner and returns +// them as virtual files cluster///.yaml plus the latest run +// status per pipeline. +func Fetch(ctx context.Context, runner Runner, src Source) (*Fetched, error) { + if runner == nil { + return nil, ErrNoRunner + } + if strings.TrimSpace(src.Namespace) == "" && !src.AllNamespaces { + return nil, ErrNamespaceRequired + } + keep := src.RunsPerPipeline + if keep <= 0 { + keep = DefaultRunsPerPipeline + } + if keep > MaxRunsPerPipeline { + keep = MaxRunsPerPipeline + } + out := &Fetched{} + var runs []map[string]any + nsSeen := map[string]bool{} + capped := false + for _, r := range resources { + items, err := list(ctx, runner, src, r.plural) + if err != nil { + if errors.Is(err, ErrNoCRD) && !r.required { + if strings.Contains(r.plural, "triggers.tekton.dev") { + out.Findings = append(out.Findings, workflowmodel.Finding{Path: "cluster", Code: CodeTriggersCRDMissing, Detail: r.plural + " not installed", Severity: workflowmodel.SeverityUnsupported}) + } + continue + } + return nil, err + } + for _, it := range items { + ns := stringAt(mapAt(it, "metadata"), "namespace") + if !nsSeen[ns] { + if src.AllNamespaces && len(nsSeen) >= MaxNamespaces { + capped = true + continue + } + nsSeen[ns] = true + } + if r.kind == kindPipelineRun { + runs = append(runs, it) + continue + } + if err := out.add(it, r.kind); err != nil { + return nil, err + } + } + } + if capped { + out.Findings = append(out.Findings, workflowmodel.Finding{Path: "cluster", Code: CodeNamespaceCapped, Detail: fmt.Sprintf("only the first %d namespaces were read", MaxNamespaces), Severity: workflowmodel.SeverityWarning}) + } + kept, statuses, trimmed := trimRuns(runs, keep, nsSeen) + if trimmed > 0 { + out.Findings = append(out.Findings, workflowmodel.Finding{Path: "cluster", Code: CodeRunsTrimmed, Detail: fmt.Sprintf("%d older PipelineRuns skipped; newest %d per pipeline kept", trimmed, keep), Severity: workflowmodel.SeverityWarning}) + } + for _, it := range kept { + if err := out.add(it, kindPipelineRun); err != nil { + return nil, err + } + } + out.Runs = statuses + total := 0 + for _, f := range out.Files { + total += len(f.Data) + } + if total > MaxTotalBytes { + return nil, fmt.Errorf("%w: %d bytes of YAML", ErrTooLarge, total) + } + for ns := range nsSeen { + out.Namespaces = append(out.Namespaces, ns) + } + sort.Strings(out.Namespaces) + sort.SliceStable(out.Files, func(i, j int) bool { return out.Files[i].Path < out.Files[j].Path }) + return out, nil +} + +// add serialises one object into the harvest, enforcing MaxObjects. +func (f *Fetched) add(obj map[string]any, kind string) error { + f.Objects++ + if f.Objects > MaxObjects { + return fmt.Errorf("%w: more than %d objects", ErrTooLarge, MaxObjects) + } + file, err := fileFor(obj, kind) + if err != nil { + return err + } + f.Files = append(f.Files, file) + return nil +} + +// list runs one kubectl get and returns the items. +func list(ctx context.Context, runner Runner, src Source, plural string) ([]map[string]any, error) { + var args []string + if src.Context != "" { + args = append(args, "--context", src.Context) + } + args = append(args, "get", plural, "-o", "json") + if src.AllNamespaces { + args = append(args, "--all-namespaces") + } else { + args = append(args, "-n", src.Namespace) + } + if src.Selector != "" { + args = append(args, "-l", src.Selector) + } + raw, err := runner.Run(ctx, args...) + if err != nil { + return nil, classify(err, plural, src.Context) + } + var doc map[string]any + if err := json.Unmarshal(raw, &doc); err != nil { + return nil, fmt.Errorf("kubectl get %s: decode: %w", plural, err) + } + var items []map[string]any + for _, it := range sliceAt(doc, "items") { + if m, ok := it.(map[string]any); ok { + items = append(items, m) + } + } + return items, nil +} + +// classify maps kubectl's stderr to the package errors. +func classify(err error, plural, kubeContext string) error { + msg := strings.ToLower(err.Error()) + switch { + case strings.Contains(msg, "doesn't have a resource type"), strings.Contains(msg, "could not find the requested resource"), strings.Contains(msg, "no matches for kind"): + return fmt.Errorf("%w (%s)", ErrNoCRD, plural) + case strings.Contains(msg, "forbidden"), strings.Contains(msg, "unauthorized"): + return fmt.Errorf("%w: %v", ErrForbidden, err) + case strings.Contains(msg, "context") && (strings.Contains(msg, "does not exist") || strings.Contains(msg, "not found")): + return fmt.Errorf("%w: %q", ErrNoContext, kubeContext) + } + return fmt.Errorf("kubectl get %s: %w", plural, err) +} + +// fileFor serialises one object to a virtual file. Server-side noise +// (managedFields, resourceVersion, uid) is dropped so re-ingesting an +// unchanged cluster hashes the same; status is kept on PipelineRuns only. +func fileFor(obj map[string]any, kind string) (tekton.File, error) { + md := mapAt(obj, "metadata") + ns := stringAt(md, "namespace") + if ns == "" { + ns = "_cluster" + } + name := stringAt(md, "name") + if name == "" { + name = strings.TrimSuffix(stringAt(md, "generateName"), "-") + } + clean := make(map[string]any, len(obj)) + for k, v := range obj { + clean[k] = v + } + cmd := make(map[string]any, len(md)) + for k, v := range md { + switch k { + case "managedFields", "resourceVersion", "uid", "selfLink": + continue + } + cmd[k] = v + } + clean["metadata"] = cmd + if kind != kindPipelineRun { + delete(clean, "status") + } + data, err := yaml.Marshal(clean) + if err != nil { + return tekton.File{}, fmt.Errorf("%s/%s %s: %w", ns, kind, name, err) + } + return tekton.File{Path: fmt.Sprintf("cluster/%s/%s/%s.yaml", ns, strings.ToLower(kind), workflowmodel.Slug(name)), Data: data}, nil +} + +// trimRuns keeps the newest keep runs per (namespace, pipeline) and +// reports the latest status of each. Runs in namespaces beyond the cap are +// dropped. +func trimRuns(runs []map[string]any, keep int, nsSeen map[string]bool) ([]map[string]any, []RunStatus, int) { + type keyed struct { + when string + run map[string]any + } + groups := map[string][]keyed{} + for _, r := range runs { + ns := stringAt(mapAt(r, "metadata"), "namespace") + if !nsSeen[ns] { + continue + } + key := ns + "/" + runPipelineName(r) + groups[key] = append(groups[key], keyed{when: runTime(r), run: r}) + } + order := make([]string, 0, len(groups)) + for k := range groups { + order = append(order, k) + } + sort.Strings(order) + var kept []map[string]any + var statuses []RunStatus + trimmed := 0 + for _, key := range order { + g := groups[key] + sort.SliceStable(g, func(i, j int) bool { return g[i].when > g[j].when }) + if len(g) > keep { + trimmed += len(g) - keep + g = g[:keep] + } + for _, k := range g { + kept = append(kept, k.run) + } + latest := g[0].run + md := mapAt(latest, "metadata") + statuses = append(statuses, RunStatus{ + Pipeline: runPipelineName(latest), + Namespace: stringAt(md, "namespace"), + Run: stringAt(md, "name"), + Status: runOutcome(latest), + Completed: stringAt(mapAt(latest, "status"), "completionTime"), + }) + } + return kept, statuses, trimmed +} + +// runPipelineName is the Pipeline a run executes: pipelineRef.name, or +// the run's own name for an embedded pipelineSpec. +func runPipelineName(run map[string]any) string { + if name := stringAt(mapAt(mapAt(run, "spec"), "pipelineRef"), "name"); name != "" { + return name + } + md := mapAt(run, "metadata") + if name := stringAt(md, "name"); name != "" { + return name + } + return strings.TrimSuffix(stringAt(md, "generateName"), "-") +} + +// runTime orders runs: completion, then start, then creation. +func runTime(run map[string]any) string { + st := mapAt(run, "status") + for _, k := range []string{"completionTime", "startTime"} { + if v := stringAt(st, k); v != "" { + return v + } + } + return stringAt(mapAt(run, "metadata"), "creationTimestamp") +} + +// runOutcome reads the Succeeded condition. +func runOutcome(run map[string]any) string { + for _, c := range mapsAt(mapAt(run, "status"), "conditions") { + if stringAt(c, "type") != "Succeeded" { + continue + } + switch stringAt(c, "status") { + case "True": + return "Succeeded" + case "False": + if reason := stringAt(c, "reason"); reason != "" { + return reason + } + return "Failed" + default: + return "Running" + } + } + return "Unknown" +} + +// Result is a cluster ingest: the model plus what was observed. +type Result struct { + Model *workflowmodel.Model `json:"model"` + Runs []RunStatus `json:"runs"` + // Files is how many virtual files were handed to the Tekton ingester. + Files int `json:"files"` + Namespaces []string `json:"namespaces"` + Source string `json:"source"` +} + +// Ingest fetches src and composes the lifecycle model for scope exactly as +// a repository ingest would (ingest.Lifecycle over the virtual files). The +// latest run outcome per pipeline is recorded as an observed-run annotation +// and a finding; it is never an inferred element. +func Ingest(ctx context.Context, runner Runner, src Source, scope workflowmodel.Scope) (*Result, error) { + fetched, err := Fetch(ctx, runner, src) + if err != nil { + return nil, err + } + files := make(map[string][]byte, len(fetched.Files)) + for _, f := range fetched.Files { + files[f.Path] = f.Data + } + m := ingest.Lifecycle(scope, files) + m.Spec.Meta.Source = workflowmodel.SourceTekton + m.Spec.Meta.Description = fmt.Sprintf("Lifecycle ingested from %s: hive-default stages with the cluster's Tekton pipelines bound", src.String()) + m.Spec.Coverage.Findings = append(m.Spec.Coverage.Findings, fetched.Findings...) + if m.Metadata.Annotations == nil { + m.Metadata.Annotations = map[string]string{} + } + m.Metadata.Annotations[AnnotationSource] = src.String() + byName := map[string]string{} + for _, p := range m.Spec.Pipelines { + byName[p.Name] = p.ID + } + for _, r := range fetched.Runs { + id, ok := byName[r.Pipeline] + if !ok { + continue + } + m.Metadata.Annotations[AnnotationObservedRun+id] = r.Status + m.Spec.Coverage.Findings = append(m.Spec.Coverage.Findings, workflowmodel.Finding{ + Path: fmt.Sprintf("cluster/%s/pipelinerun/%s.yaml", r.Namespace, workflowmodel.Slug(r.Run)), Node: id, + Code: CodeObservedRun, Detail: "last run " + r.Status, Severity: workflowmodel.SeverityWarning, + }) + } + return &Result{Model: m, Runs: fetched.Runs, Files: len(fetched.Files), Namespaces: fetched.Namespaces, Source: src.String()}, nil +} + +func stringAt(m map[string]any, key string) string { + if m == nil { + return "" + } + s, _ := m[key].(string) + return s +} + +func mapAt(m map[string]any, key string) map[string]any { + if m == nil { + return nil + } + sub, _ := m[key].(map[string]any) + return sub +} + +func sliceAt(m map[string]any, key string) []any { + if m == nil { + return nil + } + s, _ := m[key].([]any) + return s +} + +func mapsAt(m map[string]any, key string) []map[string]any { + items := sliceAt(m, key) + out := make([]map[string]any, 0, len(items)) + for _, it := range items { + if im, ok := it.(map[string]any); ok { + out = append(out, im) + } + } + return out +} diff --git a/src/pkg/workflowmodel/ingest/cluster/cluster_test.go b/src/pkg/workflowmodel/ingest/cluster/cluster_test.go new file mode 100644 index 0000000000..4505061195 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/cluster/cluster_test.go @@ -0,0 +1,477 @@ +package cluster + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + "os/exec" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/hivedefault" +) + +// fakeKubectl answers kubectl get from a table; unknown plurals +// fail like a missing CRD. It records every argv for assertions. +type fakeKubectl struct { + byPlural map[string]any + errs map[string]error + calls [][]string +} + +func (f *fakeKubectl) Run(_ context.Context, args ...string) ([]byte, error) { + f.calls = append(f.calls, args) + plural := "" + for i, a := range args { + if a == "get" && i+1 < len(args) { + plural = args[i+1] + } + } + if err, ok := f.errs[plural]; ok { + return nil, err + } + items, ok := f.byPlural[plural] + if !ok { + return nil, fmt.Errorf("kubectl get %s: exit status 1: error: the server doesn't have a resource type %q", plural, strings.Split(plural, ".")[0]) + } + return json.Marshal(map[string]any{"apiVersion": "v1", "kind": "List", "items": items}) +} + +func obj(kind, ns, name string, extra map[string]any) map[string]any { + o := map[string]any{"apiVersion": "tekton.dev/v1", "kind": kind, "metadata": map[string]any{"name": name, "namespace": ns, "uid": "u-" + name, "resourceVersion": "9", "managedFields": []any{map[string]any{"manager": "kubectl"}}}} + for k, v := range extra { + o[k] = v + } + return o +} + +func run(ns, name, pipeline, status, when string) map[string]any { + r := obj("PipelineRun", ns, name, map[string]any{ + "spec": map[string]any{"pipelineRef": map[string]any{"name": pipeline}}, + "status": map[string]any{"completionTime": when, "conditions": []any{map[string]any{"type": "Succeeded", "status": status, "reason": map[string]string{"True": "Succeeded", "False": "Failed", "Unknown": "Running"}[status]}}}, + }) + r["metadata"].(map[string]any)["annotations"] = map[string]any{"pipelinesascode.tekton.dev/on-event": "[pull_request]", "pipelinesascode.tekton.dev/on-target-branch": "[main]"} + return r +} + +func pipelineSpec() map[string]any { + return map[string]any{"tasks": []any{ + map[string]any{"name": "clone", "taskRef": map[string]any{"name": "git-clone"}}, + map[string]any{"name": "test", "runAfter": []any{"clone"}, "taskRef": map[string]any{"name": "golang-test"}}, + }} +} + +func fixture() *fakeKubectl { + return &fakeKubectl{byPlural: map[string]any{ + "pipelines.tekton.dev": []any{obj("Pipeline", "ci", "build", map[string]any{"spec": pipelineSpec(), "status": map[string]any{"x": 1}})}, + "tasks.tekton.dev": []any{obj("Task", "ci", "golang-test", map[string]any{"spec": map[string]any{"steps": []any{}}})}, + "pipelineruns.tekton.dev": []any{ + run("ci", "build-run-1", "build", "True", "2026-01-01T00:00:00Z"), + run("ci", "build-run-2", "build", "False", "2026-01-03T00:00:00Z"), + run("ci", "build-run-3", "build", "Unknown", "2026-01-02T00:00:00Z"), + run("ci", "build-run-4", "build", "True", "2026-01-04T00:00:00Z"), + run("ci", "orphan-run", "missing", "True", "2026-01-01T00:00:00Z"), + }, + "eventlisteners.triggers.tekton.dev": []any{obj("EventListener", "ci", "gh", map[string]any{"apiVersion": "triggers.tekton.dev/v1beta1", "spec": map[string]any{}})}, + "triggertemplates.triggers.tekton.dev": []any{}, + "triggerbindings.triggers.tekton.dev": []any{}, + "triggers.triggers.tekton.dev": []any{}, + }} +} + +func TestFetchFilesRunsAndCitations(t *testing.T) { + k := fixture() + got, err := Fetch(context.Background(), k, Source{Context: "kind-ci", Namespace: "ci", Selector: "team=a", RunsPerPipeline: 2}) + if err != nil { + t.Fatal(err) + } + if got.Objects != 6 || len(got.Files) != 6 { + t.Fatalf("objects %d files %d", got.Objects, len(got.Files)) + } + var paths []string + for _, f := range got.Files { + paths = append(paths, f.Path) + if strings.Contains(string(f.Data), "managedFields") || strings.Contains(string(f.Data), "resourceVersion") { + t.Errorf("%s keeps server noise:\n%s", f.Path, f.Data) + } + if strings.HasPrefix(f.Path, "cluster/ci/pipeline/") && strings.Contains(string(f.Data), "status:") { + t.Errorf("pipeline keeps status") + } + } + want := []string{"cluster/ci/eventlistener/gh.yaml", "cluster/ci/pipeline/build.yaml", "cluster/ci/pipelinerun/build-run-2.yaml", "cluster/ci/pipelinerun/build-run-4.yaml", "cluster/ci/pipelinerun/orphan-run.yaml", "cluster/ci/task/golang-test.yaml"} + for _, w := range want { + if !contains(paths, w) { + t.Errorf("missing %s in %v", w, paths) + } + } + if contains(paths, "cluster/ci/pipelinerun/build-run-1.yaml") || contains(paths, "cluster/ci/pipelinerun/build-run-3.yaml") { + t.Errorf("older runs kept: %v", paths) + } + if len(got.Runs) != 2 || got.Runs[0].Pipeline != "build" || got.Runs[0].Status != "Succeeded" || got.Runs[0].Run != "build-run-4" || got.Runs[1].Pipeline != "missing" { + t.Fatalf("runs %+v", got.Runs) + } + if len(got.Namespaces) != 1 || got.Namespaces[0] != "ci" { + t.Fatalf("namespaces %v", got.Namespaces) + } + var codes []string + for _, f := range got.Findings { + codes = append(codes, f.Code) + } + if !contains(codes, CodeRunsTrimmed) { + t.Fatalf("findings %v", codes) + } + first := strings.Join(k.calls[0], " ") + if first != "--context kind-ci get pipelines.tekton.dev -o json -n ci -l team=a" { + t.Fatalf("argv %q", first) + } +} + +func TestFetchDefaultsAndStatusReasons(t *testing.T) { + k := fixture() + k.byPlural["pipelineruns.tekton.dev"] = []any{ + run("ci", "r1", "build", "False", ""), + func() map[string]any { + r := run("ci", "r2", "build", "Unknown", "") + r["status"] = map[string]any{"startTime": "2026-02-01T00:00:00Z"} + return r + }(), + func() map[string]any { + r := run("ci", "r3", "", "False", "2026-03-01T00:00:00Z") + r["spec"] = map[string]any{"pipelineSpec": pipelineSpec()} + r["status"].(map[string]any)["conditions"] = []any{map[string]any{"type": "Succeeded", "status": "False"}} + return r + }(), + } + got, err := Fetch(context.Background(), k, Source{Namespace: "ci"}) + if err != nil { + t.Fatal(err) + } + byPipe := map[string]RunStatus{} + for _, r := range got.Runs { + byPipe[r.Pipeline] = r + } + // r2 has only a startTime and no conditions: newest by startTime, status unknown. + if byPipe["build"].Run != "r2" || byPipe["build"].Status != "Unknown" { + t.Errorf("startTime ordering / unknown: %+v", byPipe["build"]) + } + k2 := fixture() + k2.byPlural["pipelineruns.tekton.dev"] = []any{run("ci", "r", "build", "Unknown", "")} + got2, _ := Fetch(context.Background(), k2, Source{Namespace: "ci"}) + if got2.Runs[0].Status != "Running" { + t.Errorf("unknown condition status is running: %+v", got2.Runs) + } + if byPipe["r3"].Status != "Failed" { + t.Errorf("false without reason: %+v", byPipe["r3"]) + } + // No context flag, namespace flag, no selector. + if strings.Join(k.calls[0], " ") != "get pipelines.tekton.dev -o json -n ci" { + t.Fatalf("argv %v", k.calls[0]) + } +} + +func TestFetchErrors(t *testing.T) { + ctx := context.Background() + if _, err := Fetch(ctx, nil, Source{Namespace: "ci"}); !errors.Is(err, ErrNoRunner) { + t.Fatalf("nil runner: %v", err) + } + if _, err := Fetch(ctx, fixture(), Source{}); !errors.Is(err, ErrNamespaceRequired) { + t.Fatalf("no namespace: %v", err) + } + noCRD := &fakeKubectl{byPlural: map[string]any{}} + if _, err := Fetch(ctx, noCRD, Source{Namespace: "ci"}); !errors.Is(err, ErrNoCRD) { + t.Fatalf("no crd: %v", err) + } + denied := fixture() + denied.errs = map[string]error{"pipelines.tekton.dev": errors.New(`exit status 1: Error from server (Forbidden): pipelines.tekton.dev is forbidden: User "system:serviceaccount:hive:hive" cannot list resource`)} + if _, err := Fetch(ctx, denied, Source{Namespace: "ci"}); !errors.Is(err, ErrForbidden) { + t.Fatalf("forbidden: %v", err) + } + noCtx := fixture() + noCtx.errs = map[string]error{"pipelines.tekton.dev": errors.New(`exit status 1: error: context "nope" does not exist`)} + if _, err := Fetch(ctx, noCtx, Source{Context: "nope", Namespace: "ci"}); !errors.Is(err, ErrNoContext) { + t.Fatalf("no context: %v", err) + } + other := fixture() + other.errs = map[string]error{"tasks.tekton.dev": errors.New("exit status 1: dial tcp: connection refused")} + if _, err := Fetch(ctx, other, Source{Namespace: "ci"}); err == nil || !strings.Contains(err.Error(), "connection refused") { + t.Fatalf("other: %v", err) + } + // Optional kinds missing their CRD are findings, not failures. + noTriggers := fixture() + for _, p := range []string{"eventlisteners.triggers.tekton.dev", "triggertemplates.triggers.tekton.dev", "triggerbindings.triggers.tekton.dev", "triggers.triggers.tekton.dev", "tasks.tekton.dev"} { + delete(noTriggers.byPlural, p) + } + got, err := Fetch(ctx, noTriggers, Source{Namespace: "ci"}) + if err != nil { + t.Fatal(err) + } + n := 0 + for _, f := range got.Findings { + if f.Code == CodeTriggersCRDMissing { + n++ + } + } + if n != 4 { + t.Fatalf("triggers findings %d: %+v", n, got.Findings) + } + bad := &fakeKubectl{byPlural: map[string]any{}} + bad.byPlural["pipelines.tekton.dev"] = nil + garbage := RunnerFunc(func(context.Context, ...string) ([]byte, error) { return []byte("{not json"), nil }) + if _, err := Fetch(ctx, garbage, Source{Namespace: "ci"}); err == nil || !strings.Contains(err.Error(), "decode") { + t.Fatalf("garbage: %v", err) + } +} + +func TestFetchCaps(t *testing.T) { + ctx := context.Background() + many := &fakeKubectl{byPlural: map[string]any{}} + var items []any + for i := 0; i <= MaxObjects; i++ { + items = append(items, obj("Pipeline", "ci", fmt.Sprintf("p%d", i), map[string]any{"spec": pipelineSpec()})) + } + many.byPlural["pipelines.tekton.dev"] = items + if _, err := Fetch(ctx, many, Source{Namespace: "ci"}); !errors.Is(err, ErrTooLarge) { + t.Fatalf("objects cap: %v", err) + } + + big := &fakeKubectl{byPlural: map[string]any{}} + big.byPlural["pipelines.tekton.dev"] = []any{obj("Pipeline", "ci", "p", map[string]any{"spec": map[string]any{"description": strings.Repeat("x", MaxTotalBytes+1)}})} + if _, err := Fetch(ctx, big, Source{Namespace: "ci"}); !errors.Is(err, ErrTooLarge) { + t.Fatalf("bytes cap: %v", err) + } + + wide := &fakeKubectl{byPlural: map[string]any{}} + var pipes, runs []any + for i := 0; i < MaxNamespaces+2; i++ { + ns := fmt.Sprintf("ns%02d", i) + pipes = append(pipes, obj("Pipeline", ns, "build", map[string]any{"spec": pipelineSpec()})) + runs = append(runs, run(ns, "r", "build", "True", "2026-01-01T00:00:00Z")) + } + wide.byPlural["pipelines.tekton.dev"] = pipes + wide.byPlural["pipelineruns.tekton.dev"] = runs + got, err := Fetch(ctx, wide, Source{AllNamespaces: true, RunsPerPipeline: MaxRunsPerPipeline + 5}) + if err != nil { + t.Fatal(err) + } + if len(got.Namespaces) != MaxNamespaces || len(got.Runs) != MaxNamespaces { + t.Fatalf("namespaces %d runs %d", len(got.Namespaces), len(got.Runs)) + } + found := false + for _, f := range got.Findings { + found = found || f.Code == CodeNamespaceCapped + } + if !found { + t.Fatalf("no namespace-capped finding: %+v", got.Findings) + } + if !contains(wide.calls[0], "--all-namespaces") { + t.Fatalf("argv %v", wide.calls[0]) + } +} + +func TestIngestBindsPipelinesAndRecordsObservedRuns(t *testing.T) { + res, err := Ingest(context.Background(), fixture(), Source{Context: "kind-ci", Namespace: "ci"}, workflowmodel.Scope{Forge: "github.com", Repository: "acme/widgets"}) + if err != nil { + t.Fatal(err) + } + m := res.Model + if res.Source != "cluster:kind-ci/ci" || m.Metadata.Annotations[AnnotationSource] != res.Source { + t.Fatalf("source %q ann %v", res.Source, m.Metadata.Annotations) + } + if len(m.Spec.Pipelines) != 1 || m.Spec.Pipelines[0].Name != "build" || m.Spec.Pipelines[0].Path != "cluster/ci/pipeline/build.yaml" { + t.Fatalf("pipelines %+v", m.Spec.Pipelines) + } + if m.Spec.Pipelines[0].Role != workflowmodel.RolePRVerify { + t.Fatalf("role %q: PaC annotations on runs should attach", m.Spec.Pipelines[0].Role) + } + if m.Spec.Meta.Source != workflowmodel.SourceTekton || m.Spec.Meta.Provenance != workflowmodel.ProvenanceIngestedUnreviewed { + t.Fatalf("meta %+v", m.Spec.Meta) + } + id := m.Spec.Pipelines[0].ID + if m.Metadata.Annotations[AnnotationObservedRun+id] != "Succeeded" { + t.Fatalf("observed annotation missing: %v", m.Metadata.Annotations) + } + observed := 0 + for _, f := range m.Spec.Coverage.Findings { + if f.Code == CodeObservedRun { + observed++ + if f.Node != id || f.Detail != "last run Succeeded" { + t.Errorf("finding %+v", f) + } + } + } + if observed != 1 { + t.Fatalf("observed findings %d (orphan run must not be recorded)", observed) + } + // Observations never become inferred lifecycle elements: the CI state + // cites the cluster file, and the observed status is not a state/event. + bound := 0 + for _, s := range m.Spec.States { + if s.Lifecycle.Column == hivedefault.ColumnCIReview { + if len(s.Pipelines) != 1 || len(s.Lifecycle.Citations) != 1 || s.Lifecycle.Citations[0].Path != "cluster/ci/pipeline/build.yaml" { + t.Fatalf("ci state %q: %v %v", s.ID, s.Pipelines, s.Lifecycle.Citations) + } + bound++ + } + } + if bound == 0 { + t.Fatal("no CI / Review state bound to the cluster pipeline") + } + if _, _, err := workflowmodel.Compile(m); err != nil { + t.Fatalf("compile: %v", err) + } + for _, s := range m.Spec.States { + for _, c := range s.Lifecycle.Citations { + if !strings.HasPrefix(c.Path, "cluster/ci/") { + t.Errorf("citation %+v not a cluster path", c) + } + } + } + if res.Files != 7 || len(res.Runs) != 2 || len(res.Namespaces) != 1 { + t.Fatalf("files %d runs %d ns %v", res.Files, len(res.Runs), res.Namespaces) + } + if _, err := Ingest(context.Background(), fixture(), Source{}, workflowmodel.Scope{Repository: "x"}); !errors.Is(err, ErrNamespaceRequired) { + t.Fatalf("ingest error passthrough: %v", err) + } +} + +func TestSourceString(t *testing.T) { + if s := (Source{}).String(); s != "cluster:current-context/" { + t.Fatal(s) + } + if s := (Source{Context: "c", AllNamespaces: true}).String(); s != "cluster:c/*" { + t.Fatal(s) + } +} + +func TestFileForNamesClusterScopedAndGenerateName(t *testing.T) { + f, err := fileFor(map[string]any{"kind": "ClusterTask", "metadata": map[string]any{"generateName": "lint-"}}, "ClusterTask") + if err != nil { + t.Fatal(err) + } + if f.Path != "cluster/_cluster/clustertask/lint.yaml" { + t.Fatal(f.Path) + } +} + +// TestHelperProcess is the fake kubectl for TestKubectlRun. +func TestHelperProcess(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + args := os.Args + for i, a := range args { + if a == "--" { + args = args[i+1:] + break + } + } + if contains(args, "fail") { + fmt.Fprint(os.Stderr, "Error from server (Forbidden): nope") + os.Exit(1) + } + fmt.Fprint(os.Stdout, strings.Join(args, " ")) + os.Exit(0) +} + +func TestKubectlRun(t *testing.T) { + orig := execCommandContext + t.Cleanup(func() { execCommandContext = orig }) + execCommandContext = func(ctx context.Context, name string, args ...string) *exec.Cmd { + cmd := exec.CommandContext(ctx, os.Args[0], append([]string{"-test.run=TestHelperProcess", "--", name}, args...)...) + cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") + return cmd + } + out, err := Kubectl{Kubeconfig: "/kc"}.Run(context.Background(), "get", "pods") + if err != nil { + t.Fatal(err) + } + if string(out) != "kubectl --kubeconfig /kc get pods" { + t.Fatalf("%q", out) + } + out, err = Kubectl{}.Run(context.Background(), "get", "pods") + if err != nil || string(out) != "kubectl get pods" { + t.Fatalf("%q %v", out, err) + } + _, err = Kubectl{}.Run(context.Background(), "get", "fail") + if err == nil || !strings.Contains(err.Error(), "Forbidden") { + t.Fatalf("stderr must be folded into the error: %v", err) + } +} + +func TestAvailable(t *testing.T) { + origLook, origStat, origEnv, origHome := lookPath, statFile, getenv, userHomeDir + t.Cleanup(func() { lookPath, statFile, getenv, userHomeDir = origLook, origStat, origEnv, origHome }) + files := map[string]bool{} + statFile = func(p string) (os.FileInfo, error) { + if files[p] { + return nil, nil + } + return nil, os.ErrNotExist + } + env := map[string]string{} + getenv = func(k string) string { return env[k] } + userHomeDir = func() (string, error) { return "/home/hive", nil } + + lookPath = func(string) (string, error) { return "", exec.ErrNotFound } + if ok, why := Available(); ok || !strings.Contains(why, "kubectl") { + t.Fatalf("%v %q", ok, why) + } + lookPath = func(string) (string, error) { return "/usr/bin/kubectl", nil } + if ok, why := Available(); ok || !strings.Contains(why, "kubeconfig") { + t.Fatalf("%v %q", ok, why) + } + env["KUBECONFIG"] = "/a:/b" + if ok, _ := Available(); ok { + t.Fatal("KUBECONFIG pointing at missing files is not available") + } + files["/b"] = true + if ok, _ := Available(); !ok { + t.Fatal("KUBECONFIG second path") + } + delete(env, "KUBECONFIG") + files["/home/hive/.kube/config"] = true + if ok, _ := Available(); !ok { + t.Fatal("~/.kube/config") + } + delete(files, "/home/hive/.kube/config") + userHomeDir = func() (string, error) { return "", errors.New("no home") } + files[inClusterTokenPath] = true + if ok, _ := Available(); !ok { + t.Fatal("in-cluster token") + } +} + +func TestContexts(t *testing.T) { + if _, err := Contexts(context.Background(), nil); !errors.Is(err, ErrNoRunner) { + t.Fatal(err) + } + var lines []string + for i := 0; i < maxContexts+3; i++ { + lines = append(lines, fmt.Sprintf("ctx%d", i)) + } + r := RunnerFunc(func(_ context.Context, args ...string) ([]byte, error) { + if strings.Join(args, " ") != "config get-contexts -o name" { + return nil, fmt.Errorf("argv %v", args) + } + return []byte("\n" + strings.Join(lines, "\n") + "\n\n"), nil + }) + got, err := Contexts(context.Background(), r) + if err != nil || len(got) != maxContexts || got[0] != "ctx0" { + t.Fatalf("%d %v", len(got), err) + } + if _, err := Contexts(context.Background(), RunnerFunc(func(context.Context, ...string) ([]byte, error) { return nil, errors.New("boom") })); err == nil { + t.Fatal("error passthrough") + } +} + +func contains(xs []string, x string) bool { + for _, v := range xs { + if v == x { + return true + } + } + return false +} diff --git a/src/pkg/workflowmodel/ingest/cluster/kubectl.go b/src/pkg/workflowmodel/ingest/cluster/kubectl.go new file mode 100644 index 0000000000..dab6e71435 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/cluster/kubectl.go @@ -0,0 +1,106 @@ +package cluster + +import ( + "bytes" + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "time" +) + +// KubectlTimeout bounds one kubectl invocation. +const KubectlTimeout = 60 * time.Second + +// maxContexts caps the context list returned to the UI. +const maxContexts = 100 + +// Seams for tests. Production always uses the real exec, PATH and files. +var ( + execCommandContext = exec.CommandContext + lookPath = exec.LookPath + statFile = os.Stat + getenv = os.Getenv + userHomeDir = os.UserHomeDir +) + +// inClusterTokenPath is the ServiceAccount token a pod gets. +const inClusterTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" + +// Kubectl runs the kubectl binary on PATH. Kubeconfig, when set, is passed +// as --kubeconfig. +type Kubectl struct { + Kubeconfig string +} + +// Run executes kubectl with args and returns stdout; stderr is folded into +// the error so Fetch can classify it. +func (k Kubectl) Run(ctx context.Context, args ...string) ([]byte, error) { + ctx, cancel := context.WithTimeout(ctx, KubectlTimeout) + defer cancel() + full := args + if k.Kubeconfig != "" { + full = append([]string{"--kubeconfig", k.Kubeconfig}, args...) + } + cmd := execCommandContext(ctx, "kubectl", full...) + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return nil, fmt.Errorf("kubectl %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String())) + } + return stdout.Bytes(), nil +} + +// Available reports whether this process can reach a cluster at all: +// kubectl is on PATH and either a kubeconfig (KUBECONFIG or +// ~/.kube/config) or an in-cluster ServiceAccount token exists. The reason +// explains a false answer in one sentence for the UI. +func Available() (bool, string) { + if _, err := lookPath("kubectl"); err != nil { + return false, "kubectl is not installed on the hive" + } + if kc := getenv("KUBECONFIG"); kc != "" { + for _, p := range filepath.SplitList(kc) { + if _, err := statFile(p); err == nil { + return true, "" + } + } + } + if home, err := userHomeDir(); err == nil { + if _, err := statFile(filepath.Join(home, ".kube", "config")); err == nil { + return true, "" + } + } + if _, err := statFile(inClusterTokenPath); err == nil { + return true, "" + } + return false, "no kubeconfig (KUBECONFIG or ~/.kube/config) and no in-cluster ServiceAccount token" +} + +// Contexts lists the kubeconfig context names, capped at maxContexts. An +// empty list with no error means kubectl has a config with no contexts +// (in-cluster, typically). +func Contexts(ctx context.Context, runner Runner) ([]string, error) { + if runner == nil { + return nil, ErrNoRunner + } + raw, err := runner.Run(ctx, "config", "get-contexts", "-o", "name") + if err != nil { + return nil, err + } + var out []string + for _, line := range strings.Split(string(raw), "\n") { + line = strings.TrimSpace(line) + if line == "" { + continue + } + out = append(out, line) + if len(out) >= maxContexts { + break + } + } + return out, nil +} From 3e5ecbbd472ed69716d7d722f87611dcfefd9153 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 11 Nov 2026 09:40:31 -0700 Subject: [PATCH 2/6] =?UTF-8?q?=E2=9C=A8=20workflowmodel:=20ingest=20writt?= =?UTF-8?q?en=20process=20specs=20into=20inferred=20draft=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add pkg/workflowmodel/ingest/spec. Markdown/plain text is split into heading sections with line ranges; a deterministic stage turns step lists, conditional sentences and role nouns into a compiling skeleton (states, columns, chained transitions, manual guards, gates, roles). An optional LLM stage asks a Completer to refine the skeleton with the Model schema (reflected from the Go types), the fact vocabulary and the compiler's refusal rules; the answer is parsed with DisallowUnknownFields, normalised by Mark (inferred confidence on every state/event/gate, ingested origin, spec:#L-L citations validated against real sections, pipelines/sources/agents stripped, provenance ingested-unreviewed) and compiled, with one retry quoting the diagnostics before falling back to the skeleton. This is the only LLM seam in the workflow-model stack. Refs hivecommons/hive#17620 Signed-off-by: Andrew Anderson --- src/pkg/workflowmodel/ingest/spec/extract.go | 455 +++++++++++++++ src/pkg/workflowmodel/ingest/spec/ingest.go | 201 +++++++ src/pkg/workflowmodel/ingest/spec/mark.go | 109 ++++ src/pkg/workflowmodel/ingest/spec/prompt.go | 69 +++ src/pkg/workflowmodel/ingest/spec/schema.go | 119 ++++ src/pkg/workflowmodel/ingest/spec/sections.go | 92 +++ .../workflowmodel/ingest/spec/spec_test.go | 545 ++++++++++++++++++ 7 files changed, 1590 insertions(+) create mode 100644 src/pkg/workflowmodel/ingest/spec/extract.go create mode 100644 src/pkg/workflowmodel/ingest/spec/ingest.go create mode 100644 src/pkg/workflowmodel/ingest/spec/mark.go create mode 100644 src/pkg/workflowmodel/ingest/spec/prompt.go create mode 100644 src/pkg/workflowmodel/ingest/spec/schema.go create mode 100644 src/pkg/workflowmodel/ingest/spec/sections.go create mode 100644 src/pkg/workflowmodel/ingest/spec/spec_test.go diff --git a/src/pkg/workflowmodel/ingest/spec/extract.go b/src/pkg/workflowmodel/ingest/spec/extract.go new file mode 100644 index 0000000000..1912957526 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/extract.go @@ -0,0 +1,455 @@ +package spec + +import ( + "fmt" + "regexp" + "strings" + + "github.com/hivecommons/hive/pkg/workflowmodel" +) + +// Finding codes added by the deterministic stage. +const ( + // CodeNoSteps says no step list or headings were found, so a minimal + // request -> done skeleton was produced. + CodeNoSteps = "spec-no-steps" + // CodeDerivationUnspecified says the spec does not say how item state + // is derived from forge facts; builtin derivation is assumed. + CodeDerivationUnspecified = "spec-derivation-unspecified" + // CodeConditionUnplaced says a conditional sentence could not be tied + // to a step and was recorded on the section's first transition. + CodeConditionUnplaced = "spec-condition-unplaced" +) + +// Annotation keys set on ingested models. +const ( + AnnotationSource = "workflow.hivecommons.dev/source" + AnnotationLLMAssisted = "workflow.hivecommons.dev/llm-assisted" +) + +const stageStep = 10 + +// columnHint maps a keyword found in a step to a column hint and the +// owner that usually acts there. +type columnHint struct { + Column string + Owner workflowmodel.Owner +} + +// hintTable is scanned in order; the first keyword present wins. +var hintTable = []struct { + Words []string + Hint columnHint +}{ + {[]string{"triage", "prioriti"}, columnHint{"triage", workflowmodel.OwnerHuman}}, + {[]string{"intake", "request", "submit", "file", "open an issue", "propose", "proposal"}, columnHint{"intake", workflowmodel.OwnerHuman}}, + {[]string{"approve", "approval", "lgtm", "sign-off", "sign off", "vote"}, columnHint{"approval", workflowmodel.OwnerHuman}}, + {[]string{"review", "feedback", "comment"}, columnHint{"review", workflowmodel.OwnerHuman}}, + {[]string{"implement", "develop", "draft", "write", "fix", "design", "prototype"}, columnHint{"work", workflowmodel.OwnerHuman}}, + {[]string{"ci ", "ci.", "pipeline", "build", "test", "lint", "check"}, columnHint{"ci", workflowmodel.OwnerExternalService}}, + {[]string{"merge"}, columnHint{"merge", workflowmodel.OwnerExternalObservation}}, + {[]string{"publish", "release", "tag", "announce"}, columnHint{"release", workflowmodel.OwnerExternalObservation}}, + {[]string{"deploy", "roll out", "rollout", "ship"}, columnHint{"deploy", workflowmodel.OwnerExternalService}}, + {[]string{"close", "done", "complete", "archive", "finish", "resolved"}, columnHint{"done", workflowmodel.OwnerExternalObservation}}, +} + +// roleTable maps role nouns to role definitions. +var roleTable = []struct { + Words []string + Role workflowmodel.RoleDef +}{ + {[]string{"maintainer", "maintainers"}, workflowmodel.RoleDef{ID: "maintainer", Label: "Maintainer", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "maintain"}, Human: true}}, + {[]string{"reviewer", "reviewers"}, workflowmodel.RoleDef{ID: "reviewer", Label: "Reviewer", Kind: workflowmodel.RoleFrozenSet, Params: map[string]string{"frozenAt": "review-requested"}, Human: true}}, + {[]string{"approver", "approvers"}, workflowmodel.RoleDef{ID: "approver", Label: "Approver", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "write"}, Human: true}}, + {[]string{"toc", "technical oversight committee"}, workflowmodel.RoleDef{ID: "toc", Label: "TOC", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "admin"}, Human: true}}, + {[]string{"steering committee", "steering"}, workflowmodel.RoleDef{ID: "steering-committee", Label: "Steering committee", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "admin"}, Human: true}}, + {[]string{"chair", "chairs"}, workflowmodel.RoleDef{ID: "chair", Label: "Chair", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "admin"}, Human: true}}, + {[]string{"owner", "owners", "code owner", "codeowners"}, workflowmodel.RoleDef{ID: "owner", Label: "Owner", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "admin"}, Human: true}}, + {[]string{"committer", "committers"}, workflowmodel.RoleDef{ID: "committer", Label: "Committer", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "write"}, Human: true}}, + {[]string{"release manager", "release managers"}, workflowmodel.RoleDef{ID: "release-manager", Label: "Release manager", Kind: workflowmodel.RolePermission, Params: map[string]string{"permission": "maintain"}, Human: true}}, + {[]string{"author", "authors", "submitter", "requester"}, workflowmodel.RoleDef{ID: "author", Label: "Author", Kind: workflowmodel.RoleAuthorKind, Params: map[string]string{"kind": "human"}, Human: true}}, + {[]string{"contributor", "contributors"}, workflowmodel.RoleDef{ID: "contributor", Label: "Contributor", Kind: workflowmodel.RoleAuthorKind, Params: map[string]string{"kind": "human"}, Human: true}}, + {[]string{"bot", "bots", "automation"}, workflowmodel.RoleDef{ID: "bot", Label: "Bot", Kind: workflowmodel.RoleBot, Params: map[string]string{"login": ""}}}, +} + +// gateWords mark a step whose exit a person must release. +var gateWords = []string{"approve", "approval", "review", "lgtm", "sign-off", "sign off", "vote", "accept"} + +// gateReleaserPreference is the order roles are tried for a gate. +var gateReleaserPreference = []string{"reviewer", "approver", "maintainer", "toc", "steering-committee", "chair", "owner", "committer", "release-manager"} + +var ( + listItem = regexp.MustCompile(`^\s*(?:\d+[.)]|[-*+])\s+(.+?)\s*$`) + conditional = regexp.MustCompile(`(?i)\b(if|when|after|unless|once|until)\b`) + sentenceEnd = regexp.MustCompile(`[.!?]\s+|[.!?]$`) + backWords = regexp.MustCompile(`(?i)\b(back|return|returned|reject|rejected|request changes|changes requested|reopen)\b`) + labelCutoff = regexp.MustCompile(`[:.;](\s|$)|\s[(—–-]`) + emphasis = strings.NewReplacer("**", "", "__", "", "`", "", "*", "") + wordBoundary = regexp.MustCompile(`[^a-z0-9]+`) +) + +const maxLabelLen = 60 + +// candidate is one step pulled from the spec. +type candidate struct { + Label string + // Text is the whole list item, for role and condition detection. + Text string + Section Section + Line int +} + +// block is a paragraph or one list item with the line it starts on. +type block struct { + Text string + Line int + // Item indexes the candidate this block is, or -1 for prose. + Item int +} + +// Extract runs the deterministic stage: a compiled skeleton model built +// from the sections without any LLM. The skeleton always compiles. +func Extract(origin, name string, scope workflowmodel.Scope, secs []Section) *workflowmodel.Model { + cands := steps(secs) + var findings []workflowmodel.Finding + if len(cands) == 0 { + cands = headingSteps(secs) + } + if len(cands) == 0 { + first := firstSection(secs) + cands = []candidate{{Label: "Request", Section: first, Line: first.StartLine}, {Label: "Done", Section: first, Line: first.StartLine}} + findings = append(findings, workflowmodel.Finding{Path: first.Ref(), Code: CodeNoSteps, Severity: workflowmodel.SeverityWarning, Detail: "no step list or headings found; produced a request -> done skeleton"}) + } + if len(cands) == 1 { + c := cands[0] + cands = append(cands, candidate{Label: "Done", Section: c.Section, Line: c.Line}) + } + + m := &workflowmodel.Model{APIVersion: workflowmodel.APIVersion, Kind: workflowmodel.KindWorkflowModel} + m.Metadata.Name = name + if m.Metadata.Name == "" { + m.Metadata.Name = workflowmodel.Slug(firstSection(secs).Heading) + } + m.Spec.Scope = scope + m.Spec.Meta = workflowmodel.Meta{Version: "1", Description: "Draft ingested from written spec " + origin, Source: workflowmodel.SourceSpec, Provenance: workflowmodel.ProvenanceIngestedUnreviewed} + m.Spec.Derivation = workflowmodel.Derivation{Mode: workflowmodel.DerivationBuiltin} + findings = append(findings, workflowmodel.Finding{Path: "spec:" + origin, Code: CodeDerivationUnspecified, Severity: workflowmodel.SeverityWarning, Detail: "the spec does not say how item state is derived from forge facts; builtin derivation assumed"}) + + // Roles from nouns anywhere in the text. + all := strings.ToLower(joinText(secs)) + roles := map[string]bool{} + for _, r := range roleTable { + for _, w := range r.Words { + if containsWord(all, w) { + if !roles[r.Role.ID] { + roles[r.Role.ID] = true + m.Spec.Roles = append(m.Spec.Roles, cloneRole(r.Role)) + } + break + } + } + } + + // States and columns, one column per step, last step terminal. + ids := map[string]bool{} + var stateIDs []string + for i, c := range cands { + id := uniqueID(ids, workflowmodel.Slug(c.Label)) + stateIDs = append(stateIDs, id) + hint := hintFor(c.Label) + last := i == len(cands)-1 + st := workflowmodel.State{ID: id, Label: c.Label, Owner: hint.Owner, Origin: workflowmodel.OriginIngested} + if last { + st.Type = workflowmodel.StateFinal + st.Owner = workflowmodel.OwnerExternalObservation + } + st.Lifecycle = workflowmodel.StateLifecycle{Column: id, Confidence: workflowmodel.ConfidenceInferred, Citations: []workflowmodel.Citation{cite(c)}} + if !last { + st.Lifecycle.Number = (i + 1) * stageStep + } + m.Spec.States = append(m.Spec.States, st) + m.Spec.Columns = append(m.Spec.Columns, workflowmodel.Column{ID: id, Label: c.Label, Hint: hint.Column, States: []string{id}, Terminal: last}) + } + m.Spec.Initial = stateIDs[0] + + // Chain transitions; gates on steps a person must release. + for i := 0; i+1 < len(cands); i++ { + tr := workflowmodel.Transition{From: stateIDs[i], To: stateIDs[i+1], On: "advance-to-" + stateIDs[i+1], Note: "next step in " + cands[i].Section.Heading} + if hasAny(strings.ToLower(cands[i].Label), gateWords) { + g := workflowmodel.Gate{ID: "gate-" + stateIDs[i], ReleasedBy: releaser(m, roles, cands[i].Text), Confidence: workflowmodel.ConfidenceInferred, Citations: []workflowmodel.Citation{cite(cands[i])}} + m.Spec.Gates = append(m.Spec.Gates, g) + tr.GateRef = g.ID + } + m.Spec.Transitions = append(m.Spec.Transitions, tr) + } + + // Conditional sentences become manual guards on the transition out + // of the step they mention, and back edges when they speak of + // returning to an earlier step. + guardN := 0 + for _, sec := range secs { + for _, blk := range blocks(sec, cands) { + for _, sent := range sentences(blk.Text) { + if !conditional.MatchString(sent) { + continue + } + from := blk.Item + placed := from >= 0 + if !placed { + from = stateMentioned(sent, cands, true) + placed = from >= 0 + } + if !placed { + from = firstStepIn(cands, sec) + } + if from < 0 || from == len(stateIDs)-1 { + continue + } + guardN++ + g := workflowmodel.Guard{ID: fmt.Sprintf("condition-%d", guardN), Kind: workflowmodel.GuardManual, Params: map[string]string{"condition": strings.TrimSpace(sent)}, Source: sec.Ref()} + m.Spec.Guards = append(m.Spec.Guards, g) + if !placed { + findings = append(findings, workflowmodel.Finding{Path: sec.Ref(), Node: stateIDs[from], Code: CodeConditionUnplaced, Severity: workflowmodel.SeverityWarning, Detail: "condition names no step; attached to " + stateIDs[from] + ": " + truncate(sent, maxLabelLen)}) + } + if back := stateMentioned(sent, cands, false); back >= 0 && back < from && backWords.MatchString(sent) { + m.Spec.Transitions = append(m.Spec.Transitions, workflowmodel.Transition{From: stateIDs[from], To: stateIDs[back], On: "return-to-" + stateIDs[back], GuardRef: g.ID, Note: truncate(sent, maxLabelLen)}) + continue + } + for i := range m.Spec.Transitions { + t := &m.Spec.Transitions[i] + if t.From == stateIDs[from] && t.GuardRef == "" && t.To == stateIDs[from+1] { + t.GuardRef = g.ID + break + } + } + } + } + } + + m.Metadata.Annotations = map[string]string{AnnotationSource: "spec:" + origin} + m.Spec.Coverage = workflowmodel.Coverage{Files: 1, Parsed: 1, Findings: findings} + return m +} + +// steps collects list items across sections in order. +func steps(secs []Section) []candidate { + var out []candidate + for _, s := range secs { + for i, line := range s.Lines { + m := listItem.FindStringSubmatch(line) + if m == nil { + continue + } + label := cleanLabel(m[1]) + if label == "" { + continue + } + out = append(out, candidate{Label: label, Text: strings.TrimSpace(m[1]), Section: s, Line: s.StartLine + i}) + } + } + return out +} + +// headingSteps uses sub-headings as steps when there is no list. +func headingSteps(secs []Section) []candidate { + var out []candidate + for _, s := range secs { + if s.Level >= 2 { + out = append(out, candidate{Label: cleanLabel(s.Heading), Text: s.Heading + " " + strings.Join(s.Lines, " "), Section: s, Line: s.StartLine}) + } + } + return out +} + +func firstSection(secs []Section) Section { + if len(secs) == 0 { + return Section{Heading: "Document", StartLine: 1, EndLine: 1} + } + return secs[0] +} + +func firstStepIn(cands []candidate, sec Section) int { + for i, c := range cands { + if c.Section.Heading == sec.Heading && c.Section.StartLine == sec.StartLine { + return i + } + } + return -1 +} + +// stateMentioned finds the step whose label words appear in the sentence. +// With preferFirst the earliest mention wins; otherwise the latest one. +func stateMentioned(sent string, cands []candidate, preferFirst bool) int { + low := strings.ToLower(sent) + best, bestPos := -1, -1 + for i, c := range cands { + key := strings.ToLower(c.Label) + pos := strings.Index(low, key) + if pos < 0 { + // Fall back to the leading word of the label when it is distinctive. + words := strings.Fields(wordBoundary.ReplaceAllString(key, " ")) + if len(words) == 0 || len(words[0]) < 5 || !containsWord(low, words[0]) { + continue + } + pos = strings.Index(low, words[0]) + } + if best < 0 || (preferFirst && pos < bestPos) || (!preferFirst && pos > bestPos) { + best, bestPos = i, pos + } + } + return best +} + +// blocks splits a section into list items (tied to their candidate) and +// prose paragraphs, each with its starting line. +func blocks(sec Section, cands []candidate) []block { + var out []block + var para []string + paraLine := 0 + flush := func() { + if len(para) > 0 { + out = append(out, block{Text: strings.Join(para, " "), Line: paraLine, Item: -1}) + para = nil + } + } + for i, line := range sec.Lines { + n := sec.StartLine + i + if strings.TrimSpace(line) == "" { + flush() + continue + } + if listItem.MatchString(line) { + flush() + item := -1 + for ci, c := range cands { + if c.Line == n && c.Section.StartLine == sec.StartLine { + item = ci + } + } + out = append(out, block{Text: strings.TrimSpace(listItem.FindStringSubmatch(line)[1]), Line: n, Item: item}) + continue + } + if len(para) == 0 { + paraLine = n + } + para = append(para, strings.TrimSpace(line)) + } + flush() + return out +} + +func sentences(text string) []string { + var out []string + for _, s := range sentenceEnd.Split(text, -1) { + if s = strings.TrimSpace(s); s != "" { + out = append(out, s) + } + } + return out +} + +func cleanLabel(s string) string { + s = strings.TrimSpace(emphasis.Replace(s)) + if loc := labelCutoff.FindStringIndex(s); loc != nil && loc[0] > 0 { + s = s[:loc[0]] + } + return truncate(strings.TrimSpace(s), maxLabelLen) +} + +func truncate(s string, n int) string { + if len(s) <= n { + return s + } + cut := strings.LastIndex(s[:n], " ") + if cut < n/2 { + cut = n + } + return strings.TrimRight(s[:cut], " ,;:") + "…" +} + +func hintFor(label string) columnHint { + low := strings.ReplaceAll(strings.ToLower(label), "pull request", "pr") + for _, h := range hintTable { + if hasAny(low, h.Words) { + return h.Hint + } + } + return columnHint{"work", workflowmodel.OwnerHuman} +} + +func hasAny(low string, words []string) bool { + for _, w := range words { + if strings.Contains(low, w) { + return true + } + } + return false +} + +func containsWord(low, w string) bool { + padded := " " + wordBoundary.ReplaceAllString(low, " ") + " " + return strings.Contains(padded, " "+w+" ") +} + +func uniqueID(seen map[string]bool, id string) string { + if id == "" { + id = "step" + } + base, n := id, 2 + for seen[id] { + id = fmt.Sprintf("%s-%d", base, n) + n++ + } + seen[id] = true + return id +} + +func releaser(m *workflowmodel.Model, roles map[string]bool, text string) string { + low := strings.ToLower(text) + for _, r := range roleTable { + if !roles[r.Role.ID] || !r.Role.Human { + continue + } + for _, w := range r.Words { + if containsWord(low, w) { + return r.Role.ID + } + } + } + for _, id := range gateReleaserPreference { + if roles[id] { + return id + } + } + for _, r := range roleTable { + if r.Role.ID == "maintainer" { + roles["maintainer"] = true + m.Spec.Roles = append(m.Spec.Roles, cloneRole(r.Role)) + } + } + return "maintainer" +} + +func cloneRole(r workflowmodel.RoleDef) workflowmodel.RoleDef { + p := map[string]string{} + for k, v := range r.Params { + p[k] = v + } + r.Params = p + return r +} + +func cite(c candidate) workflowmodel.Citation { + return workflowmodel.Citation{Path: c.Section.Ref(), Line: c.Line, Text: truncate(c.Label, maxLabelLen)} +} + +func joinText(secs []Section) string { + var b strings.Builder + for _, s := range secs { + b.WriteString(s.Heading) + b.WriteByte('\n') + b.WriteString(strings.Join(s.Lines, "\n")) + b.WriteByte('\n') + } + return b.String() +} diff --git a/src/pkg/workflowmodel/ingest/spec/ingest.go b/src/pkg/workflowmodel/ingest/spec/ingest.go new file mode 100644 index 0000000000..6838d6d61f --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/ingest.go @@ -0,0 +1,201 @@ +package spec + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "strings" + + "github.com/hivecommons/hive/pkg/workflowmodel" +) + +// Completer is the one LLM seam of the workflow-model stack: a single +// system+user turn returning text. The dashboard adapts its configured +// model gateway to this; nothing in this package knows a provider. +type Completer interface { + Complete(ctx context.Context, system, user string) (string, error) +} + +// CompleterFunc adapts a function to Completer. +type CompleterFunc func(ctx context.Context, system, user string) (string, error) + +// Complete implements Completer. +func (f CompleterFunc) Complete(ctx context.Context, system, user string) (string, error) { + return f(ctx, system, user) +} + +// MaxAttempts is how many times the LLM is asked before falling back. +const MaxAttempts = 2 + +// Finding codes from the LLM stage. +const ( + // CodeLLMFellBack says the LLM answer never compiled and the + // deterministic skeleton is returned instead. + CodeLLMFellBack = "spec-llm-fell-back" + // CodeLLMCitationReplaced says the LLM cited a section that does not + // exist; the whole-document reference was substituted. + CodeLLMCitationReplaced = "spec-llm-citation-replaced" +) + +// Errors. +var ( + ErrEmpty = errors.New("spec is empty") + ErrTooLarge = fmt.Errorf("spec exceeds %d bytes", MaxBytes) +) + +// Options drives one ingest. +type Options struct { + // Origin names where the text came from (pasted, repo path, URL). + Origin string + // Name is the model name; defaults to the first heading's slug. + Name string + Scope workflowmodel.Scope + // Completer is the gateway; nil disables the LLM stage. + Completer Completer + // ModelID is recorded in the LLM report and annotation. + ModelID string +} + +// LLMReport says whether and how the LLM stage ran. +type LLMReport struct { + Used bool `json:"used"` + Model string `json:"model,omitempty"` + Attempts int `json:"attempts"` + // Accepted is true when the returned model is the LLM's answer. + Accepted bool `json:"accepted"` + Error string `json:"error,omitempty"` +} + +// Result is one spec ingest. +type Result struct { + Model *workflowmodel.Model `json:"model"` + Compiled bool `json:"compiled"` + Profile *workflowmodel.Profile `json:"profile,omitempty"` + Diagnostics []workflowmodel.Diagnostic `json:"diagnostics"` + Sections []Section `json:"sections"` + Source string `json:"source"` + LLM LLMReport `json:"llm"` +} + +// Ingest runs both stages. The deterministic skeleton is always built; +// when opts.Completer is set the LLM is asked to refine it and its answer +// is accepted only if it parses strictly and compiles (one retry with the +// diagnostics). The returned model always carries the spec-ingest marks. +func Ingest(ctx context.Context, text string, opts Options) (*Result, error) { + if strings.TrimSpace(text) == "" { + return nil, ErrEmpty + } + if len(text) > MaxBytes { + return nil, ErrTooLarge + } + if opts.Origin == "" { + opts.Origin = "pasted" + } + secs := Split(text) + skeleton := Extract(opts.Origin, opts.Name, opts.Scope, secs) + Mark(skeleton, opts.Origin, opts.Name, opts.Scope, secs) + res := &Result{Sections: secs, Source: "spec:" + opts.Origin} + if opts.Completer != nil { + res.LLM = LLMReport{Used: true, Model: opts.ModelID} + m, attempts, err := refine(ctx, text, skeleton, secs, opts) + res.LLM.Attempts = attempts + if err != nil { + res.LLM.Error = err.Error() + skeleton.Spec.Coverage.Findings = append(skeleton.Spec.Coverage.Findings, workflowmodel.Finding{Path: res.Source, Code: CodeLLMFellBack, Severity: workflowmodel.SeverityWarning, Detail: "LLM refinement rejected after " + fmt.Sprint(attempts) + " attempt(s): " + err.Error()}) + } else { + res.LLM.Accepted = true + skeleton = m + skeleton.Metadata.Annotations[AnnotationLLMAssisted] = "LLM-assisted (model " + opts.ModelID + ")" + } + } + res.Model = skeleton + profile, diags, err := workflowmodel.Compile(skeleton) + res.Diagnostics = diags + if err == nil { + res.Compiled = true + res.Profile = profile + } + return res, nil +} + +// refine asks the completer for a model, up to MaxAttempts times. +func refine(ctx context.Context, text string, skeleton *workflowmodel.Model, secs []Section, opts Options) (*workflowmodel.Model, int, error) { + skel, err := json.MarshalIndent(skeleton, "", " ") + if err != nil { + return nil, 0, err + } + data := promptData{Origin: opts.Origin, Spec: text, Skeleton: string(skel), Schema: Schema(), Vocabulary: Vocabulary(), Sections: secs, Rules: RefusalRules, Name: skeleton.Metadata.Name, Forge: opts.Scope.Forge, Repository: opts.Scope.Repository} + var lastErr error + for attempt := 1; attempt <= MaxAttempts; attempt++ { + user, err := UserPrompt(data) + if err != nil { + return nil, attempt - 1, err + } + raw, err := opts.Completer.Complete(ctx, SystemPrompt, user) + if err != nil { + return nil, attempt, fmt.Errorf("gateway: %w", err) + } + m, diags, err := Validate(raw, opts.Origin, skeleton.Metadata.Name, opts.Scope, secs) + if err == nil { + return m, attempt, nil + } + lastErr = err + data.Diagnostics = data.Diagnostics[:0] + for _, d := range diags { + if d.Severity == workflowmodel.DiagError { + data.Diagnostics = append(data.Diagnostics, d.Code+" "+d.Subject+": "+d.Detail) + } + } + if len(data.Diagnostics) == 0 { + data.Diagnostics = []string{err.Error()} + } + } + return nil, MaxAttempts, lastErr +} + +// Validate parses an LLM answer strictly, applies the spec-ingest marks +// and compiles it. The compile diagnostics are returned with the error so +// a retry can quote them. +func Validate(raw, origin, name string, scope workflowmodel.Scope, secs []Section) (*workflowmodel.Model, []workflowmodel.Diagnostic, error) { + body := extractJSON(raw) + if body == "" { + return nil, nil, errors.New("answer contains no JSON object") + } + dec := json.NewDecoder(bytes.NewReader([]byte(body))) + dec.DisallowUnknownFields() + var m workflowmodel.Model + if err := dec.Decode(&m); err != nil { + return nil, nil, fmt.Errorf("answer is not a Model: %w", err) + } + replaced := Mark(&m, origin, name, scope, secs) + if replaced > 0 { + m.Spec.Coverage.Findings = append(m.Spec.Coverage.Findings, workflowmodel.Finding{Path: "spec:" + origin, Code: CodeLLMCitationReplaced, Severity: workflowmodel.SeverityWarning, Detail: fmt.Sprintf("%d citation(s) named no section in the spec and were replaced with the whole-document reference", replaced)}) + } + if _, diags, err := workflowmodel.Compile(&m); err != nil { + return nil, diags, err + } + return &m, nil, nil +} + +// extractJSON strips code fences and surrounding prose, returning the +// outermost {...}. +func extractJSON(raw string) string { + s := strings.TrimSpace(raw) + if i := strings.Index(s, "```"); i >= 0 { + rest := s[i+3:] + if nl := strings.IndexByte(rest, '\n'); nl >= 0 { + rest = rest[nl+1:] + } + if j := strings.LastIndex(rest, "```"); j >= 0 { + rest = rest[:j] + } + s = strings.TrimSpace(rest) + } + start, end := strings.IndexByte(s, '{'), strings.LastIndexByte(s, '}') + if start < 0 || end <= start { + return "" + } + return s[start : end+1] +} diff --git a/src/pkg/workflowmodel/ingest/spec/mark.go b/src/pkg/workflowmodel/ingest/spec/mark.go new file mode 100644 index 0000000000..230288598e --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/mark.go @@ -0,0 +1,109 @@ +package spec + +import ( + "strconv" + "strings" + + "github.com/hivecommons/hive/pkg/workflowmodel" +) + +// Mark enforces the spec-ingest guarantees on a model regardless of which +// stage produced it: identity fields, provenance, inferred confidence on +// every state, event and gate, ingested origin, citations that point at a +// real section, and no elements a spec cannot vouch for (pipelines, +// sources, agent grants). It returns the number of citations it had to +// replace with the whole-document reference. +func Mark(m *workflowmodel.Model, origin, name string, scope workflowmodel.Scope, secs []Section) int { + m.APIVersion = workflowmodel.APIVersion + m.Kind = workflowmodel.KindWorkflowModel + if name != "" { + m.Metadata.Name = name + } + if m.Metadata.Name == "" { + m.Metadata.Name = workflowmodel.Slug(firstSection(secs).Heading) + } + if m.Metadata.Annotations == nil { + m.Metadata.Annotations = map[string]string{} + } + m.Metadata.Annotations[AnnotationSource] = "spec:" + origin + m.Spec.Scope = scope + if m.Spec.Meta.Version == "" { + m.Spec.Meta.Version = "1" + } + m.Spec.Meta.Source = workflowmodel.SourceSpec + m.Spec.Meta.Provenance = workflowmodel.ProvenanceIngestedUnreviewed + m.Spec.Pipelines = nil + m.Spec.Sources = nil + m.Spec.Agents = nil + for i := range m.Spec.States { + m.Spec.States[i].Pipelines = nil + } + if m.Spec.Derivation.Mode == "" { + m.Spec.Derivation.Mode = workflowmodel.DerivationBuiltin + } + + known := headings(secs) + whole := workflowmodel.Citation{Path: firstSection(secs).Ref(), Line: firstSection(secs).StartLine} + if len(secs) > 1 { + whole = workflowmodel.Citation{Path: "spec:" + origin + "#L1-L" + strconv.Itoa(secs[len(secs)-1].EndLine)} + } + replaced := 0 + fix := func(cs []workflowmodel.Citation) []workflowmodel.Citation { + var kept []workflowmodel.Citation + for _, c := range cs { + if sec, ok := citedSection(c.Path, known); ok { + if c.Line < sec.StartLine || c.Line > sec.EndLine { + c.Line = sec.StartLine + } + kept = append(kept, c) + } + } + if len(kept) == 0 { + replaced++ + kept = []workflowmodel.Citation{whole} + } + return kept + } + var walk func(states []workflowmodel.State) + walk = func(states []workflowmodel.State) { + for i := range states { + s := &states[i] + s.Origin = workflowmodel.OriginIngested + s.Lifecycle.Confidence = workflowmodel.ConfidenceInferred + s.Lifecycle.Citations = fix(s.Lifecycle.Citations) + walk(s.Regions) + } + } + walk(m.Spec.States) + for i := range m.Spec.Events { + m.Spec.Events[i].Confidence = workflowmodel.ConfidenceInferred + m.Spec.Events[i].Citations = fix(m.Spec.Events[i].Citations) + } + for i := range m.Spec.Gates { + m.Spec.Gates[i].Confidence = workflowmodel.ConfidenceInferred + m.Spec.Gates[i].Citations = fix(m.Spec.Gates[i].Citations) + } + for i := range m.Spec.Guards { + if _, ok := citedSection(m.Spec.Guards[i].Source, known); !ok { + m.Spec.Guards[i].Source = whole.Path + } + } + if m.Spec.Coverage.Files == 0 { + m.Spec.Coverage.Files = 1 + m.Spec.Coverage.Parsed = 1 + } + return replaced +} + +// citedSection resolves a spec:#L.. path to its section. +func citedSection(path string, known map[string]Section) (Section, bool) { + if !strings.HasPrefix(path, "spec:") { + return Section{}, false + } + heading := strings.TrimPrefix(path, "spec:") + if i := strings.LastIndex(heading, "#L"); i >= 0 { + heading = heading[:i] + } + sec, ok := known[heading] + return sec, ok +} diff --git a/src/pkg/workflowmodel/ingest/spec/prompt.go b/src/pkg/workflowmodel/ingest/spec/prompt.go new file mode 100644 index 0000000000..d5a0eefb44 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/prompt.go @@ -0,0 +1,69 @@ +package spec + +import ( + "strings" + "text/template" +) + +// SystemPrompt frames the LLM's only job: a Model JSON for the written +// spec, inside the compiler's rules. The gateway is never consulted for +// derivation; only here, and only to draft. +const SystemPrompt = `You turn a written software process specification into a Hive WorkflowModel JSON document. + +Rules: +- Output exactly one JSON object and nothing else: no prose, no markdown fences. +- Use only the fields listed in the schema. Unknown fields are rejected. +- Model the steps the spec describes as states, in the order the spec gives them. Do not invent steps the spec does not describe. +- Every state, event and gate must carry "confidence": "inferred" and at least one citation whose "path" is exactly one of the section references given (spec:#L-L) and whose "line" is inside that range. Cite the section the element came from. +- Columns: one or more; each state is in exactly one column; the last column is "terminal": true. +- Transitions chain the states; "on" is a short kebab-case event name. Only declare spec.events when you also bind every transition.on to one of them. +- Gates model steps a person must release (approvals, reviews, votes); each gate's "releasedBy" names a role you declare in spec.roles. +- Roles: declare the people and bots the spec names (maintainer, reviewer, author, ...). Set "human": true for people. +- Do not emit pipelines, sources, agents or coverage; the ingest owns them. +- Keep spec.derivation {"mode": "builtin"} unless the spec gives explicit label/fact rules. +- Never claim certainty: this is a draft a human will review.` + +// userPromptTemplate is filled with the spec, the deterministic skeleton, +// the schema, the vocabulary and the refusal rules. +var userPromptTemplate = template.Must(template.New("spec-user").Parse(`# Specification ({{.Origin}}) + +{{.Spec}} + +# Section references (cite exactly these paths) +{{range .Sections}}- {{.Ref}} "{{.Heading}}" +{{end}} +# Deterministic skeleton +A rule-based pass produced this draft. Refine it: fix labels, merge or split steps the spec actually describes, add gates/roles/guards the text supports, keep ids kebab-case. Return the whole model, not a diff. + +{{.Skeleton}} + +# Model JSON schema (path: type) +{{.Schema}} + +# Fact vocabulary for predicates +{{.Vocabulary}} + +# The compiler refuses a model that breaks any of these +{{range .Rules}}- {{.}} +{{end}} +{{if .Diagnostics}}# Your previous answer did not compile +Fix every diagnostic below and return the corrected full model. +{{range .Diagnostics}}- {{.}} +{{end}}{{end}}Target: name "{{.Name}}", scope.forge "{{.Forge}}", scope.repository "{{.Repository}}". Reply with the JSON object only.`)) + +type promptData struct { + Origin, Spec, Skeleton, Schema, Vocabulary string + Sections []Section + Rules []string + Diagnostics []string + Name, Forge, Repository string +} + +// UserPrompt renders the user turn. +func UserPrompt(d promptData) (string, error) { + var b strings.Builder + if err := userPromptTemplate.Execute(&b, d); err != nil { + return "", err + } + return b.String(), nil +} diff --git a/src/pkg/workflowmodel/ingest/spec/schema.go b/src/pkg/workflowmodel/ingest/spec/schema.go new file mode 100644 index 0000000000..a6eb378cf9 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/schema.go @@ -0,0 +1,119 @@ +package spec + +import ( + "fmt" + "reflect" + "sort" + "strings" + + "github.com/hivecommons/hive/pkg/workflowmodel" +) + +// Schema renders the Model's JSON shape from the Go types so the prompt +// never drifts from the struct definitions. Each line is +// "path: type" with enum values where the type is a named string. +func Schema() string { + var lines []string + seen := map[reflect.Type]bool{} + walk(reflect.TypeOf(workflowmodel.Model{}), "", seen, &lines) + return strings.Join(lines, "\n") +} + +var enums = map[string][]string{ + "Owner": {string(workflowmodel.OwnerHuman), string(workflowmodel.OwnerRegisteredAgent), string(workflowmodel.OwnerExternalService), string(workflowmodel.OwnerExternalObservation)}, + "StateType": {"\"\" (sequential)", string(workflowmodel.StateParallel), string(workflowmodel.StateFinal)}, + "Origin": {string(workflowmodel.OriginIngested)}, + "GuardKind": {string(workflowmodel.GuardRequiredChecks), string(workflowmodel.GuardReviewPolicy), string(workflowmodel.GuardEnvironmentApproval), string(workflowmodel.GuardManual), string(workflowmodel.GuardExternal)}, + "Confidence": {string(workflowmodel.ConfidenceInferred)}, + "ItemKind": {string(workflowmodel.KindIssue), string(workflowmodel.KindPR)}, + "DerivationMode": {string(workflowmodel.DerivationBuiltin), string(workflowmodel.DerivationRules)}, + "Op": {string(workflowmodel.OpIs), string(workflowmodel.OpNot), string(workflowmodel.OpHas), string(workflowmodel.OpLacks), string(workflowmodel.OpTrue), string(workflowmodel.OpFalse)}, + "RoleKind": {string(workflowmodel.RolePermission), string(workflowmodel.RoleFrozenSet), string(workflowmodel.RoleAuthorKind), string(workflowmodel.RoleBot)}, + "MetaSource": {string(workflowmodel.SourceSpec)}, + "Provenance": {string(workflowmodel.ProvenanceIngestedUnreviewed)}, + "Severity": {string(workflowmodel.SeverityUnsupported), string(workflowmodel.SeverityUnknown), string(workflowmodel.SeverityWarning)}, +} + +// omitted are Model fields the LLM must not emit: they are owned by the +// ingest (pipelines, file sources, agent grants) and stripped anyway. +var omitted = map[string]bool{"pipelines": true, "sources": true, "agents": true, "coverage": true} + +func walk(t reflect.Type, prefix string, seen map[reflect.Type]bool, lines *[]string) { + for i := 0; i < t.NumField(); i++ { + f := t.Field(i) + if !f.IsExported() { + continue + } + name := strings.Split(f.Tag.Get("json"), ",")[0] + if name == "-" || name == "" { + continue + } + if prefix == "spec" && omitted[name] { + continue + } + path := name + if prefix != "" { + path = prefix + "." + name + } + ft := f.Type + suffix := "" + for ft.Kind() == reflect.Slice || ft.Kind() == reflect.Ptr { + if ft.Kind() == reflect.Slice { + suffix += "[]" + } + ft = ft.Elem() + } + switch { + case ft.Kind() == reflect.Struct: + if seen[ft] { + *lines = append(*lines, fmt.Sprintf("%s: %s%s (same shape as above)", path, ft.Name(), suffix)) + continue + } + seen[ft] = true + *lines = append(*lines, fmt.Sprintf("%s: object%s", path, suffix)) + walk(ft, path, seen, lines) + delete(seen, ft) + case ft.Kind() == reflect.Map: + *lines = append(*lines, fmt.Sprintf("%s: map[string]string%s", path, suffix)) + default: + typ := ft.Kind().String() + if vals, ok := enums[ft.Name()]; ok { + typ = "one of " + strings.Join(vals, " | ") + } + *lines = append(*lines, fmt.Sprintf("%s: %s%s", path, typ, suffix)) + } + } +} + +// Vocabulary lists the fact names a predicate may use, with their types. +func Vocabulary() string { + keys := make([]string, 0, len(workflowmodel.Facts)) + for k := range workflowmodel.Facts { + keys = append(keys, k) + } + sort.Strings(keys) + var parts []string + for _, k := range keys { + parts = append(parts, fmt.Sprintf("%s (%s)", k, workflowmodel.Facts[k])) + } + return strings.Join(parts, ", ") +} + +// RefusalRules are the compiler's refusals, stated for the prompt. They +// mirror the error codes in workflowmodel/compile.go. +var RefusalRules = []string{ + workflowmodel.CodeNoVersion + ": spec.meta.version must be set", + workflowmodel.CodeNoColumn + ": every top-level state must be collected by exactly one column (spec.columns[].states)", + workflowmodel.CodeColumnEmpty + ": every column must collect at least one state", + workflowmodel.CodeNoTerminalColumn + ": at least one column must be terminal: true", + workflowmodel.CodeNoExit + ": every non-terminal state needs a transition out", + workflowmodel.CodeDuplicateNumber + ": lifecycle.number must be unique per item kind", + workflowmodel.CodeGateNoOwner + ": every gate needs releasedBy naming a role in spec.roles", + workflowmodel.CodeUnknownRole + ": event actors and gate releasers must be declared roles", + workflowmodel.CodeUnknownGate + ": transition gateRef must name a declared gate", + workflowmodel.CodeUnknownEvent + ": if any spec.events are declared, every transition.on must name one", + workflowmodel.CodeDuplicateEvent + ": two events may not bind the same fact predicate", + workflowmodel.CodeUnknownFact + "/" + workflowmodel.CodeBadOp + ": predicates use only vocabulary facts with the right operator (bool: true/false; scalar: is/not; set: has/lacks)", + workflowmodel.CodeNoRules + ": derivation.mode rules needs at least one rule; prefer mode builtin", + "invalid: transitions and guardRefs must reference declared states and guards; initial must be a state; every state has an owner", +} diff --git a/src/pkg/workflowmodel/ingest/spec/sections.go b/src/pkg/workflowmodel/ingest/spec/sections.go new file mode 100644 index 0000000000..1d070e30c4 --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/sections.go @@ -0,0 +1,92 @@ +// Package spec turns a written process description (markdown or plain +// text) into a draft workflow model. +// +// Two stages run. The deterministic stage (no LLM) reads step lists, +// conditional sentences and role nouns into a skeleton. The optional LLM +// stage asks a configured gateway to refine that skeleton; its answer is +// parsed strictly, normalised and compiled, and it is dropped in favour of +// the skeleton when it does not compile after one retry. +// +// This is the only place in the workflow-model stack where an LLM is +// allowed. Whatever the stage, every state, event, gate and role carries +// confidence "inferred", cites the spec section it came from +// (spec:#L-L), and the model is marked +// provenance ingested-unreviewed so the editor demands review. +package spec + +import ( + "fmt" + "regexp" + "strings" +) + +// MaxBytes is the largest spec accepted. +const MaxBytes = 256 * 1024 + +// Section is one heading-delimited slice of the spec with its line range. +type Section struct { + Heading string `json:"heading"` + Level int `json:"level"` + // StartLine and EndLine are 1-based and inclusive. + StartLine int `json:"startLine"` + EndLine int `json:"endLine"` + Lines []string `json:"-"` +} + +// Ref is the citation path for the section. +func (s Section) Ref() string { + return fmt.Sprintf("spec:%s#L%d-L%d", s.Heading, s.StartLine, s.EndLine) +} + +var ( + atxHeading = regexp.MustCompile(`^\s{0,3}(#{1,6})\s+(.+?)\s*#*\s*$`) + setextRule = regexp.MustCompile(`^\s{0,3}(=+|-+)\s*$`) +) + +// Split cuts text into sections at markdown headings. Text before the +// first heading is the "Preamble" section; a spec without headings is a +// single "Document" section. Line numbers are 1-based. +func Split(text string) []Section { + lines := strings.Split(strings.ReplaceAll(text, "\r\n", "\n"), "\n") + var out []Section + cur := Section{Heading: "Preamble", Level: 0, StartLine: 1} + flush := func(end int) { + cur.EndLine = end + if strings.TrimSpace(strings.Join(cur.Lines, "")) != "" || cur.Level > 0 { + out = append(out, cur) + } + } + for i := 0; i < len(lines); i++ { + line := lines[i] + if m := atxHeading.FindStringSubmatch(line); m != nil { + flush(i) + cur = Section{Heading: m[2], Level: len(m[1]), StartLine: i + 1} + continue + } + if i+1 < len(lines) && strings.TrimSpace(line) != "" && setextRule.MatchString(lines[i+1]) && !strings.HasPrefix(strings.TrimSpace(line), "-") { + flush(i) + level := 1 + if strings.HasPrefix(strings.TrimSpace(lines[i+1]), "-") { + level = 2 + } + cur = Section{Heading: strings.TrimSpace(line), Level: level, StartLine: i + 1} + i++ + continue + } + cur.Lines = append(cur.Lines, line) + } + flush(len(lines)) + if len(out) == 1 && out[0].Level == 0 { + out[0].Heading = "Document" + } + return out +} + +// headings returns the heading set for citation validation. +func headings(secs []Section) map[string]Section { + m := map[string]Section{} + for _, s := range secs { + m[s.Heading] = s + } + return m +} diff --git a/src/pkg/workflowmodel/ingest/spec/spec_test.go b/src/pkg/workflowmodel/ingest/spec/spec_test.go new file mode 100644 index 0000000000..572342080f --- /dev/null +++ b/src/pkg/workflowmodel/ingest/spec/spec_test.go @@ -0,0 +1,545 @@ +package spec + +import ( + "context" + "encoding/json" + "errors" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/workflowmodel" +) + +const sample = `# Contribution process + +Anyone may open an issue. The TOC decides on proposals. + +## Steps + +1. Request: a contributor files an issue describing the change. +2. Triage: maintainers label the issue within a week. +3. Implement the change in a pull request. +4. Review by two reviewers. If changes are requested, the PR goes back to implement. +5. Approval: a maintainer approves when CI is green. +6. Merge +7. Release: the release manager publishes a tag. + +## Notes + +A bot closes stale issues after 90 days. +` + +var scope = workflowmodel.Scope{Forge: "github.com", Repository: "acme/widgets"} + +func TestSplitSections(t *testing.T) { + secs := Split(sample) + var got []string + for _, s := range secs { + got = append(got, s.Ref()) + } + if want := "spec:Contribution process#L1-L4 spec:Steps#L5-L14 spec:Notes#L15-L18"; strings.Join(got, " ") != want { + t.Fatalf("%v", got) + } + if secs[1].Level != 2 || len(secs[1].Lines) != 9 { + t.Fatalf("%+v", secs[1]) + } + + setext := "Title\n=====\n\nbody\n\nSub\n---\n- a\r\n- b\n" + secs = Split(setext) + if len(secs) != 2 || secs[0].Heading != "Title" || secs[0].Level != 1 || secs[1].Heading != "Sub" || secs[1].Level != 2 || secs[1].StartLine != 6 { + t.Fatalf("%+v", secs) + } + plain := Split("just text\nmore") + if len(plain) != 1 || plain[0].Heading != "Document" || plain[0].EndLine != 2 { + t.Fatalf("%+v", plain) + } + pre := Split("intro\n# H\nx") + if len(pre) != 2 || pre[0].Heading != "Preamble" || pre[1].StartLine != 2 { + t.Fatalf("%+v", pre) + } + // An empty section before a heading is dropped; a heading-only section is kept. + empty := Split("\n\n# Only\n") + if len(empty) != 1 || empty[0].Heading != "Only" { + t.Fatalf("%+v", empty) + } +} + +func TestExtractSkeleton(t *testing.T) { + secs := Split(sample) + m := Extract("pasted", "", scope, secs) + if m.Metadata.Name != "contribution-process" || m.Spec.Meta.Source != workflowmodel.SourceSpec || m.Spec.Meta.Provenance != workflowmodel.ProvenanceIngestedUnreviewed || m.Spec.Meta.Version != "1" { + t.Fatalf("meta %+v name %q", m.Spec.Meta, m.Metadata.Name) + } + var ids, hints []string + for i, s := range m.Spec.States { + ids = append(ids, s.ID) + hints = append(hints, m.Spec.Columns[i].Hint) + if s.Lifecycle.Confidence != workflowmodel.ConfidenceInferred || s.Origin != workflowmodel.OriginIngested { + t.Errorf("%s not inferred/ingested", s.ID) + } + if len(s.Lifecycle.Citations) != 1 || s.Lifecycle.Citations[0].Path != "spec:Steps#L5-L14" || s.Lifecycle.Citations[0].Line != 6+i { + t.Errorf("%s citations %+v", s.ID, s.Lifecycle.Citations) + } + if i < len(m.Spec.States)-1 && s.Lifecycle.Number != (i+1)*stageStep { + t.Errorf("%s number %d", s.ID, s.Lifecycle.Number) + } + } + if want := "request triage implement-the-change-in-a-pull-request review-by-two-reviewers approval merge release"; strings.Join(ids, " ") != want { + t.Fatalf("ids %v", ids) + } + if want := "intake triage work review approval merge release"; strings.Join(hints, " ") != want { + t.Fatalf("hints %v", hints) + } + last := m.Spec.States[len(m.Spec.States)-1] + if last.Type != workflowmodel.StateFinal || !m.Spec.Columns[len(m.Spec.Columns)-1].Terminal || last.Owner != workflowmodel.OwnerExternalObservation { + t.Fatalf("last %+v", last) + } + if m.Spec.States[5].Owner != workflowmodel.OwnerExternalObservation || m.Spec.States[0].Owner != workflowmodel.OwnerHuman { + t.Fatalf("owners %+v", m.Spec.States) + } + var edges []string + for _, tr := range m.Spec.Transitions { + edges = append(edges, tr.From+">"+tr.To+"["+tr.GuardRef+"/"+tr.GateRef+"]") + } + want := []string{ + "review-by-two-reviewers>approval[/gate-review-by-two-reviewers]", + "approval>merge[condition-2/gate-approval]", + "review-by-two-reviewers>implement-the-change-in-a-pull-request[condition-1/]", + } + for _, w := range want { + found := false + for _, e := range edges { + found = found || e == w + } + if !found { + t.Errorf("missing edge %s in %v", w, edges) + } + } + if len(m.Spec.Guards) != 2 || m.Spec.Guards[0].Kind != workflowmodel.GuardManual || !strings.HasPrefix(m.Spec.Guards[0].Params["condition"], "If changes are requested") || m.Spec.Guards[0].Source != "spec:Steps#L5-L14" { + t.Fatalf("guards %+v", m.Spec.Guards) + } + gates := map[string]string{} + for _, g := range m.Spec.Gates { + gates[g.ID] = g.ReleasedBy + if g.Confidence != workflowmodel.ConfidenceInferred || len(g.Citations) != 1 { + t.Errorf("gate %+v", g) + } + } + if gates["gate-review-by-two-reviewers"] != "reviewer" || gates["gate-approval"] != "maintainer" { + t.Fatalf("gates %v", gates) + } + roles := map[string]workflowmodel.RoleDef{} + for _, r := range m.Spec.Roles { + roles[r.ID] = r + } + for _, id := range []string{"maintainer", "reviewer", "toc", "release-manager", "contributor", "bot"} { + if _, ok := roles[id]; !ok { + t.Errorf("role %s missing: %v", id, roles) + } + } + if roles["bot"].Human || roles["bot"].Kind != workflowmodel.RoleBot || roles["contributor"].Kind != workflowmodel.RoleAuthorKind { + t.Fatalf("roles %+v", roles) + } + if m.Metadata.Annotations[AnnotationSource] != "spec:pasted" { + t.Fatalf("annotations %v", m.Metadata.Annotations) + } + if len(m.Spec.Coverage.Findings) != 1 || m.Spec.Coverage.Findings[0].Code != CodeDerivationUnspecified { + t.Fatalf("findings %+v", m.Spec.Coverage.Findings) + } + if _, _, err := workflowmodel.Compile(m); err != nil { + t.Fatalf("skeleton must compile: %v", err) + } +} + +func TestExtractFallbacks(t *testing.T) { + // Headings as steps when there is no list; a condition tied by mention. + byHeading := "# Flow\n\n## Propose\nWrite it up.\n\n## Review it\nReviewers comment. When review is done it is accepted.\n\n## Ship\nDone.\n" + m := Extract("doc.md", "flow", scope, Split(byHeading)) + ids := stateIDs(m) + if strings.Join(ids, " ") != "propose review-it ship" || m.Metadata.Name != "flow" { + t.Fatalf("%v %q", ids, m.Metadata.Name) + } + if len(m.Spec.Guards) != 1 || m.Spec.Transitions[1].GuardRef != "condition-1" { + t.Fatalf("guards %+v transitions %+v", m.Spec.Guards, m.Spec.Transitions) + } + if _, _, err := workflowmodel.Compile(m); err != nil { + t.Fatal(err) + } + + // Nothing at all: request -> done with a finding. + m = Extract("pasted", "", scope, Split("We like to ship when ready.")) + if strings.Join(stateIDs(m), " ") != "request done" || m.Metadata.Name != "document" { + t.Fatalf("%v %q", stateIDs(m), m.Metadata.Name) + } + if !hasFinding(m, CodeNoSteps) || !hasFinding(m, CodeConditionUnplaced) { + t.Fatalf("findings %+v", m.Spec.Coverage.Findings) + } + if _, _, err := workflowmodel.Compile(m); err != nil { + t.Fatal(err) + } + + // One step gets a synthetic Done; duplicate labels get unique ids; a + // gate with no role named adds maintainer. + m = Extract("pasted", "", scope, Split("- Approve\n")) + if strings.Join(stateIDs(m), " ") != "approve done" || m.Spec.Gates[0].ReleasedBy != "maintainer" || len(m.Spec.Roles) != 1 { + t.Fatalf("%v %+v %+v", stateIDs(m), m.Spec.Gates, m.Spec.Roles) + } + m = Extract("pasted", "", scope, Split("- Review\n- Review\n- **Done**: finished.\n")) + if strings.Join(stateIDs(m), " ") != "review review-2 done" { + t.Fatalf("%v", stateIDs(m)) + } + // Conditions in a section with no steps and no mention are skipped; a + // condition on the terminal step is skipped. + m = Extract("pasted", "", scope, Split("# A\n\n1. Start\n2. Finish when happy\n\n# B\n\nIf it rains, wait.\n")) + if len(m.Spec.Guards) != 0 { + t.Fatalf("guards %+v", m.Spec.Guards) + } + if _, _, err := workflowmodel.Compile(m); err != nil { + t.Fatal(err) + } + // Empty sections slice still yields a model. + m = Extract("pasted", "", scope, nil) + if m.Metadata.Name != "document" || len(m.Spec.States) != 2 { + t.Fatalf("%+v", m) + } +} + +func TestHelpers(t *testing.T) { + if got := truncate("a very long label that goes on and on and on and on and on and on and on and on", 30); !strings.HasSuffix(got, "…") || len(got) > 34 { + t.Fatalf("%q", got) + } + if got := truncate(strings.Repeat("x", 40), 30); got != strings.Repeat("x", 30)+"…" { + t.Fatalf("%q", got) + } + if cleanLabel(" **Triage** (within a week): maintainers label it. ") != "Triage" { + t.Fatalf("%q", cleanLabel(" **Triage** (within a week): maintainers label it. ")) + } + if cleanLabel("") != "" || cleanLabel(": leading") != ": leading" { + t.Fatal("edge labels") + } + seen := map[string]bool{} + if uniqueID(seen, "") != "step" || uniqueID(seen, "") != "step-2" { + t.Fatal("uniqueID") + } + if hintFor("something else").Column != "work" || hintFor("Deploy to prod").Owner != workflowmodel.OwnerExternalService { + t.Fatal("hint defaults") + } + if !containsWord("the toc votes", "toc") || containsWord("protocol", "toc") { + t.Fatal("containsWord") + } + cands := []candidate{{Label: "Request"}, {Label: "Implement the fix"}, {Label: "Ship"}} + if stateMentioned("after implementing, ship it", cands, true) != 2 || stateMentioned("on request, go back to implement the fix", cands, false) != 1 { + t.Fatal("stateMentioned") + } + if stateMentioned("nothing here", cands, true) != -1 { + t.Fatal("no mention") + } + if firstStepIn(cands, Section{Heading: "x"}) != -1 { + t.Fatal("firstStepIn") + } +} + +func TestMarkEnforcesGuarantees(t *testing.T) { + secs := Split(sample) + m := &workflowmodel.Model{} + m.Metadata.Name = "llm-name" + m.Spec.Pipelines = []workflowmodel.Pipeline{{ID: "p"}} + m.Spec.Sources = []workflowmodel.Source{{Path: "x"}} + m.Spec.Agents = []workflowmodel.AgentGrant{{Profile: "a"}} + m.Spec.States = []workflowmodel.State{ + {ID: "a", Origin: workflowmodel.OriginHuman, Pipelines: []string{"p"}, Lifecycle: workflowmodel.StateLifecycle{Citations: []workflowmodel.Citation{{Path: "spec:Steps#L5-L14", Line: 99}, {Path: "spec:Bogus#L1-L2"}, {Path: ".github/workflows/ci.yml"}}}}, + {ID: "par", Type: workflowmodel.StateParallel, Regions: []workflowmodel.State{{ID: "r"}}}, + } + m.Spec.Events = []workflowmodel.Event{{ID: "e", Citations: []workflowmodel.Citation{{Path: "spec:Notes#L15-L18", Line: 16}}}} + m.Spec.Gates = []workflowmodel.Gate{{ID: "g"}} + m.Spec.Guards = []workflowmodel.Guard{{ID: "c", Source: "nowhere"}, {ID: "d", Source: "spec:Notes#L15-L18"}} + replaced := Mark(m, "https://x/spec.md", "", scope, secs) + if replaced != 3 { // par, its region, and the gate + t.Fatalf("replaced %d", replaced) + } + if m.APIVersion != workflowmodel.APIVersion || m.Kind != workflowmodel.KindWorkflowModel || m.Metadata.Name != "llm-name" || m.Spec.Scope.Repository != scope.Repository { + t.Fatalf("identity %+v", m) + } + if m.Spec.Meta.Version != "1" || m.Spec.Meta.Source != workflowmodel.SourceSpec || m.Spec.Meta.Provenance != workflowmodel.ProvenanceIngestedUnreviewed || m.Spec.Derivation.Mode != workflowmodel.DerivationBuiltin { + t.Fatalf("meta %+v", m.Spec.Meta) + } + if m.Spec.Pipelines != nil || m.Spec.Sources != nil || m.Spec.Agents != nil || m.Spec.States[0].Pipelines != nil { + t.Fatal("ingest-owned fields must be stripped") + } + a := m.Spec.States[0] + if a.Origin != workflowmodel.OriginIngested || a.Lifecycle.Confidence != workflowmodel.ConfidenceInferred { + t.Fatalf("state a %+v", a) + } + if len(a.Lifecycle.Citations) != 1 || a.Lifecycle.Citations[0].Path != "spec:Steps#L5-L14" || a.Lifecycle.Citations[0].Line != 5 { + t.Fatalf("citations %+v", a.Lifecycle.Citations) + } + whole := "spec:https://x/spec.md#L1-L18" + r := m.Spec.States[1].Regions[0] + if r.Lifecycle.Confidence != workflowmodel.ConfidenceInferred || r.Lifecycle.Citations[0].Path != whole { + t.Fatalf("region %+v", r) + } + if m.Spec.Events[0].Confidence != workflowmodel.ConfidenceInferred || m.Spec.Events[0].Citations[0].Line != 16 { + t.Fatalf("event %+v", m.Spec.Events[0]) + } + if m.Spec.Gates[0].Confidence != workflowmodel.ConfidenceInferred || m.Spec.Gates[0].Citations[0].Path != whole { + t.Fatalf("gate %+v", m.Spec.Gates[0]) + } + if m.Spec.Guards[0].Source != whole || m.Spec.Guards[1].Source != "spec:Notes#L15-L18" { + t.Fatalf("guards %+v", m.Spec.Guards) + } + if m.Metadata.Annotations[AnnotationSource] != "spec:https://x/spec.md" || m.Spec.Coverage.Files != 1 { + t.Fatalf("%+v", m.Metadata) + } + // Single-section specs cite that section; empty names fall back. + one := &workflowmodel.Model{} + Mark(one, "pasted", "", scope, Split("just text")) + if one.Metadata.Name != "document" { + t.Fatal(one.Metadata.Name) + } + Mark(one, "pasted", "", scope, nil) + if one.Metadata.Name != "document" { + t.Fatal(one.Metadata.Name) + } +} + +func TestSchemaAndPrompt(t *testing.T) { + schema := Schema() + for _, want := range []string{ + "spec.states: object[]", + "spec.states.lifecycle.confidence: one of inferred", + "spec.states.regions: State[] (same shape as above)", + "spec.states.owner: one of human | registered-agent | external-service | external-observation", + "spec.transitions.gateRef: string", + "spec.guards.params: map[string]string", + "spec.derivation.rules.when.op: one of is | not | has | lacks | true | false", + "spec.roles.human: bool", + "metadata.annotations: map[string]string", + } { + if !strings.Contains(schema, want) { + t.Errorf("schema lacks %q\n%s", want, schema) + } + } + for _, banned := range []string{"spec.pipelines", "spec.sources", "spec.agents", "spec.coverage"} { + if strings.Contains(schema, banned) { + t.Errorf("schema must omit %s", banned) + } + } + vocab := Vocabulary() + if !strings.Contains(vocab, "pr.review (scalar)") || !strings.Contains(vocab, "labels (set)") { + t.Fatalf("%s", vocab) + } + user, err := UserPrompt(promptData{Origin: "o", Spec: "SPEC", Skeleton: "{}", Schema: "S", Vocabulary: "V", Sections: Split(sample), Rules: RefusalRules, Diagnostics: []string{"state-no-exit x"}, Name: "n", Forge: "f", Repository: "r"}) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"- spec:Steps#L5-L14", "state-no-exit x", "did not compile", "name \"n\", scope.forge \"f\", scope.repository \"r\"", workflowmodel.CodeGateNoOwner} { + if !strings.Contains(user, want) { + t.Errorf("prompt lacks %q", want) + } + } + if !strings.Contains(SystemPrompt, `"confidence": "inferred"`) { + t.Fatal("system prompt must demand inferred confidence") + } +} + +// goodAnswer is an LLM answer that compiles after marking: it cites real +// sections and declares a gate released by a role. +func goodAnswer(t *testing.T) string { + t.Helper() + m := Extract("pasted", "", scope, Split(sample)) + m.Spec.States[0].Label = "Request a change" + m.Metadata.Name = "ignored-by-mark" + for i := range m.Spec.States { + m.Spec.States[i].Lifecycle.Confidence = "" // the LLM "forgot"; Mark restores + } + m.Spec.Coverage = workflowmodel.Coverage{} + b, err := json.Marshal(m) + if err != nil { + t.Fatal(err) + } + return string(b) +} + +func TestIngestDeterministicOnly(t *testing.T) { + res, err := Ingest(context.Background(), sample, Options{Scope: scope}) + if err != nil { + t.Fatal(err) + } + if res.LLM.Used || res.Source != "spec:pasted" || !res.Compiled || res.Profile == nil || len(res.Sections) != 3 { + t.Fatalf("%+v", res.LLM) + } + if _, ok := res.Model.Metadata.Annotations[AnnotationLLMAssisted]; ok { + t.Fatal("no LLM annotation without the LLM") + } + warnings := 0 + for _, d := range res.Diagnostics { + if d.Code == workflowmodel.CodeInferred { + warnings++ + } + } + if warnings == 0 { + t.Fatal("inferred elements must be reported") + } + if _, err := Ingest(context.Background(), " \n", Options{}); !errors.Is(err, ErrEmpty) { + t.Fatal(err) + } + if _, err := Ingest(context.Background(), strings.Repeat("x", MaxBytes+1), Options{}); !errors.Is(err, ErrTooLarge) { + t.Fatal(err) + } +} + +func TestIngestLLMAccepted(t *testing.T) { + var systems, users []string + c := CompleterFunc(func(_ context.Context, system, user string) (string, error) { + systems = append(systems, system) + users = append(users, user) + return "Here you go:\n```json\n" + goodAnswer(t) + "\n```\nThanks!", nil + }) + res, err := Ingest(context.Background(), sample, Options{Origin: "acme/widgets:docs/process.md", Name: "process", Scope: scope, Completer: c, ModelID: "gw/model-x"}) + if err != nil { + t.Fatal(err) + } + if !res.LLM.Used || !res.LLM.Accepted || res.LLM.Attempts != 1 || res.LLM.Model != "gw/model-x" || res.LLM.Error != "" { + t.Fatalf("%+v", res.LLM) + } + if !res.Compiled || res.Model.Metadata.Name != "process" || res.Model.Spec.States[0].Label != "Request a change" { + t.Fatalf("compiled %v name %q", res.Compiled, res.Model.Metadata.Name) + } + if res.Model.Metadata.Annotations[AnnotationLLMAssisted] != "LLM-assisted (model gw/model-x)" || res.Model.Metadata.Annotations[AnnotationSource] != "spec:acme/widgets:docs/process.md" { + t.Fatalf("%v", res.Model.Metadata.Annotations) + } + for _, s := range res.Model.Spec.States { + if s.Lifecycle.Confidence != workflowmodel.ConfidenceInferred { + t.Fatalf("%s not inferred after LLM", s.ID) + } + } + if len(systems) != 1 || systems[0] != SystemPrompt || !strings.Contains(users[0], "# Specification (acme/widgets:docs/process.md)") || !strings.Contains(users[0], `"name": "process"`) { + t.Fatalf("prompt wiring: %d calls", len(systems)) + } +} + +func TestIngestLLMRetryThenAccept(t *testing.T) { + calls := 0 + c := CompleterFunc(func(_ context.Context, _, user string) (string, error) { + calls++ + if calls == 1 { + // Compiles structurally but a gate names no role: a refusal. + m := Extract("pasted", "", scope, Split(sample)) + m.Spec.Roles = nil + b, _ := json.Marshal(m) + return string(b), nil + } + if !strings.Contains(user, "did not compile") || !strings.Contains(user, workflowmodel.CodeUnknownRole) && !strings.Contains(user, workflowmodel.CodeGateNoOwner) { + t.Errorf("retry prompt must quote diagnostics:\n%s", user[len(user)-800:]) + } + return goodAnswer(t), nil + }) + res, err := Ingest(context.Background(), sample, Options{Scope: scope, Completer: c, ModelID: "m"}) + if err != nil { + t.Fatal(err) + } + if !res.LLM.Accepted || res.LLM.Attempts != 2 || !res.Compiled { + t.Fatalf("%+v", res.LLM) + } +} + +func TestIngestLLMFallsBack(t *testing.T) { + cases := map[string]struct { + answers []string + err error + wantErr string + }{ + "bad json": {answers: []string{"{not json", "```\n{\"spec\": }\n```"}, wantErr: "not a Model"}, + "no json": {answers: []string{"I cannot do that.", ""}, wantErr: "no JSON object"}, + "unknown field": {answers: []string{`{"apiVersion":"x","spec":{"states":[],"magic":1}}`, `{"bogus":true}`}, wantErr: "not a Model"}, + "never compiles": {answers: []string{`{"spec":{"initial":"a","states":[{"id":"a","label":"A","owner":"human"}]}}`, `{"spec":{"initial":"a","states":[{"id":"a","label":"A","owner":"human"}]}}`}, wantErr: workflowmodel.CodeNoColumn}, + "gateway error": {err: errors.New("503 upstream"), wantErr: "gateway: 503 upstream"}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + calls := 0 + c := CompleterFunc(func(context.Context, string, string) (string, error) { + calls++ + if tc.err != nil { + return "", tc.err + } + return tc.answers[calls-1], nil + }) + res, err := Ingest(context.Background(), sample, Options{Scope: scope, Completer: c, ModelID: "m"}) + if err != nil { + t.Fatal(err) + } + if !res.LLM.Used || res.LLM.Accepted || !strings.Contains(res.LLM.Error, tc.wantErr) { + t.Fatalf("%+v", res.LLM) + } + wantAttempts := MaxAttempts + if tc.err != nil { + wantAttempts = 1 + } + if res.LLM.Attempts != wantAttempts || calls != wantAttempts { + t.Fatalf("attempts %d calls %d", res.LLM.Attempts, calls) + } + // The skeleton is returned, compiled, with the fallback finding + // and no LLM annotation. + if !res.Compiled || !hasFinding(res.Model, CodeLLMFellBack) { + t.Fatalf("compiled %v findings %+v", res.Compiled, res.Model.Spec.Coverage.Findings) + } + if _, ok := res.Model.Metadata.Annotations[AnnotationLLMAssisted]; ok { + t.Fatal("fallback must not claim LLM assistance") + } + }) + } +} + +func TestValidateCitationReplacement(t *testing.T) { + secs := Split(sample) + m := Extract("pasted", "", scope, secs) + m.Spec.States[0].Lifecycle.Citations = []workflowmodel.Citation{{Path: "spec:Made up#L1-L3"}} + b, _ := json.Marshal(m) + got, diags, err := Validate(string(b), "pasted", "", scope, secs) + if err != nil || diags != nil { + t.Fatal(err, diags) + } + if !hasFinding(got, CodeLLMCitationReplaced) || got.Spec.States[0].Lifecycle.Citations[0].Path != "spec:pasted#L1-L18" { + t.Fatalf("%+v %+v", got.Spec.Coverage.Findings, got.Spec.States[0].Lifecycle.Citations) + } + // A non-compiling answer returns its diagnostics for the retry prompt. + m.Spec.Columns = nil + b, _ = json.Marshal(m) + _, diags, err = Validate(string(b), "pasted", "", scope, secs) + if err == nil || len(diags) == 0 { + t.Fatal("expected diagnostics") + } +} + +func TestExtractJSON(t *testing.T) { + cases := map[string]string{ + "```json\n{\"a\":1}\n```": `{"a":1}`, + "text {\"a\":{\"b\":2}} tail": `{"a":{"b":2}}`, + "```\n{\"a\":1}": `{"a":1}`, + "no braces": "", + "}{": "", + } + for in, want := range cases { + if got := extractJSON(in); got != want { + t.Errorf("%q -> %q want %q", in, got, want) + } + } +} + +func stateIDs(m *workflowmodel.Model) []string { + var ids []string + for _, s := range m.Spec.States { + ids = append(ids, s.ID) + } + return ids +} + +func hasFinding(m *workflowmodel.Model, code string) bool { + for _, f := range m.Spec.Coverage.Findings { + if f.Code == code { + return true + } + } + return false +} From 8521326cce30852fc04b5737819e10d435052114 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 11 Nov 2026 09:56:30 -0700 Subject: [PATCH 3/6] =?UTF-8?q?=E2=9C=A8=20dashboard:=20cluster=20and=20wr?= =?UTF-8?q?itten-spec=20workflow=20ingest=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add POST /api/workflows/ingest/cluster, POST /api/workflows/ingest/spec and GET /api/workflows/ingest/capabilities in a new handler file, registered by one line from registerWorkflowRoutes. The cluster route runs the kubectl-backed fetcher and the existing Tekton ingester; the spec route runs the deterministic extractor and, when a model gateway is configured and useLLM is on, the gateway-backed refinement through the reviewer TurnRunner with a larger completion budget. Owner role, audit workflow_ingest, same response shape as repo ingest plus source, llm and sections. 422 when kube access or a gateway is missing. Wire WorkflowRepoFile, WorkflowClusterRunner and WorkflowSpecLLM deps from cmd/hive; document the three routes and their schemas additively in dashboard/openapi.json. Refs hivecommons/hive#17620 Signed-off-by: Andrew Anderson --- dashboard/openapi.json | 356 ++++++++++++++ src/cmd/hive/delegatedecider.go | 26 +- src/cmd/hive/delegatedecider_test.go | 24 + src/cmd/hive/main.go | 7 +- src/cmd/hive/workflow_ingest.go | 29 ++ src/pkg/dashboard/api_workflows.go | 1 + .../dashboard/api_workflows_ingest_sources.go | 366 ++++++++++++++ .../api_workflows_ingest_sources_test.go | 456 ++++++++++++++++++ src/pkg/dashboard/deps.go | 14 + 9 files changed, 1276 insertions(+), 3 deletions(-) create mode 100644 src/pkg/dashboard/api_workflows_ingest_sources.go create mode 100644 src/pkg/dashboard/api_workflows_ingest_sources_test.go diff --git a/dashboard/openapi.json b/dashboard/openapi.json index abb90f57ab..1cc70f84a0 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -36034,6 +36034,217 @@ } } } + }, + "/api/workflows/ingest/capabilities": { + "get": { + "tags": [ + "Workflows" + ], + "summary": "What the ingest sources can do on this spoke", + "description": "Whether a kube context is reachable for POST /api/workflows/ingest/cluster (kubectl on PATH with a kubeconfig or in-cluster token), the contexts kubectl knows, and whether a model gateway is configured for the optional LLM stage of POST /api/workflows/ingest/spec. RFC hivecommons/hive#17620 phase 6. Owner role required.", + "responses": { + "200": { + "description": "Capabilities", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowIngestCapabilities" + } + } + } + }, + "403": { + "description": "Owner role required" + } + } + } + }, + "/api/workflows/ingest/cluster": { + "post": { + "tags": [ + "Workflows" + ], + "summary": "Ingest Tekton pipelines from a live cluster context", + "description": "Lists tekton.dev Pipelines, Tasks, the newest PipelineRuns per pipeline and Tekton Triggers objects from a kube context through kubectl, serialises them as cluster///.yaml and runs the same Tekton ingester as POST /api/workflows/ingest, so citations point at those virtual paths. The last run status per pipeline is recorded as an observed annotation (workflow.hivecommons.dev/observed-run/) and a coverage finding, never as an inferred lifecycle element. Caps: 25 namespaces, 20 runs per pipeline, 500 objects, 8 MiB. Owner role required.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "context": { + "type": "string", + "description": "kube context name (default: the current context)" + }, + "namespace": { + "type": "string", + "description": "namespace to read; required unless allNamespaces" + }, + "allNamespaces": { + "type": "boolean", + "description": "read every namespace (capped at 25)" + }, + "selector": { + "type": "string", + "description": "label selector passed to kubectl -l" + }, + "runsPerPipeline": { + "type": "integer", + "description": "newest PipelineRuns kept per pipeline (default 3, max 20)" + }, + "repository": { + "type": "string", + "description": "owner/name to scope the model to (default: cluster/)" + }, + "save": { + "type": "boolean" + }, + "name": { + "type": "string", + "description": "store name (default: slug of the source)" + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Ingested (not saved)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowClusterIngestResult" + } + } + } + }, + "201": { + "description": "Ingested and saved", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowClusterIngestResult" + } + } + } + }, + "400": { + "description": "Bad request, or namespace required" + }, + "403": { + "description": "Owner role required, or the kube credential may not list Tekton resources" + }, + "404": { + "description": "Unknown kube context" + }, + "413": { + "description": "Object or byte cap exceeded" + }, + "422": { + "description": "No kube access configured on this spoke, or Tekton CRDs not installed" + }, + "502": { + "description": "kubectl failed" + }, + "503": { + "description": "No workflow store" + } + } + } + }, + "/api/workflows/ingest/spec": { + "post": { + "tags": [ + "Workflows" + ], + "summary": "Ingest a written process spec into a draft workflow model", + "description": "Turns markdown or plain text (pasted, a default-branch file through the forge, or an https URL on a public host; 256 KiB cap) into a draft model. A deterministic stage reads step lists, conditional sentences and role nouns into a compiling skeleton; when useLLM is on (default: when a model gateway is configured) the configured gateway refines it and its answer is accepted only if it parses strictly and compiles, with one retry quoting the diagnostics before falling back to the skeleton. Every state, event and gate is confidence inferred and cites spec:#L-L; provenance is ingested-unreviewed; the LLM-assisted annotation names the model used. This is the only LLM use in the workflow-model stack. Owner role required.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "text": { + "type": "string", + "description": "pasted spec; exactly one of text, repo+path or url" + }, + "repo": { + "type": "string", + "description": "owner/name of the repository holding the spec" + }, + "path": { + "type": "string", + "description": "repo-relative path of the spec file" + }, + "url": { + "type": "string", + "description": "https URL of the spec" + }, + "useLLM": { + "type": "boolean", + "description": "ask the configured model gateway to refine the draft (default: true when a gateway is configured); 422 when requested without one" + }, + "repository": { + "type": "string", + "description": "owner/name to scope the model to (default: repo, or spec/)" + }, + "save": { + "type": "boolean" + }, + "name": { + "type": "string", + "description": "store name (default: slug of the first heading)" + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Ingested (not saved)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowSpecIngestResult" + } + } + } + }, + "201": { + "description": "Ingested and saved", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowSpecIngestResult" + } + } + } + }, + "400": { + "description": "Bad request, empty spec, or URL not https / private host" + }, + "403": { + "description": "Owner role required" + }, + "413": { + "description": "Spec exceeds 256 KiB" + }, + "422": { + "description": "useLLM requested but no model gateway is configured" + }, + "502": { + "description": "Spec fetch failed" + }, + "503": { + "description": "No forge client or no workflow store" + } + } + } } }, "components": { @@ -36880,6 +37091,151 @@ "$ref": "#/components/schemas/WorkflowEntry" } } + }, + "WorkflowIngestCapabilities": { + "type": "object", + "properties": { + "cluster": { + "type": "boolean", + "description": "kube access is configured on this spoke" + }, + "clusterReason": { + "type": "string", + "description": "why cluster is false, or why contexts could not be listed" + }, + "contexts": { + "type": "array", + "items": { + "type": "string" + } + }, + "spec": { + "type": "object", + "properties": { + "llm": { + "type": "boolean", + "description": "a model gateway is configured for the optional LLM stage" + }, + "model": { + "type": "string", + "description": "gateway model id the LLM stage would use" + } + } + } + } + }, + "WorkflowClusterIngestResult": { + "allOf": [ + { + "$ref": "#/components/schemas/WorkflowIngestResult" + }, + { + "type": "object", + "properties": { + "source": { + "type": "string", + "description": "cluster:/" + }, + "runs": { + "type": "array", + "items": { + "type": "object", + "properties": { + "pipeline": { + "type": "string" + }, + "namespace": { + "type": "string" + }, + "run": { + "type": "string" + }, + "status": { + "type": "string" + }, + "completed": { + "type": "string" + } + } + } + }, + "namespaces": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + ] + }, + "WorkflowSpecIngestResult": { + "type": "object", + "properties": { + "model": { + "$ref": "#/components/schemas/WorkflowModel" + }, + "compiled": { + "type": "boolean" + }, + "diagnostics": { + "type": "array", + "items": { + "$ref": "#/components/schemas/WorkflowDiagnostic" + } + }, + "profile": { + "type": "object" + }, + "source": { + "type": "string", + "description": "spec:" + }, + "llm": { + "type": "object", + "properties": { + "used": { + "type": "boolean" + }, + "model": { + "type": "string" + }, + "attempts": { + "type": "integer" + }, + "accepted": { + "type": "boolean", + "description": "the returned model is the gateway's answer (false: deterministic skeleton)" + }, + "error": { + "type": "string" + } + } + }, + "sections": { + "type": "array", + "items": { + "type": "object", + "properties": { + "heading": { + "type": "string" + }, + "level": { + "type": "integer" + }, + "startLine": { + "type": "integer" + }, + "endLine": { + "type": "integer" + } + } + } + }, + "entry": { + "$ref": "#/components/schemas/WorkflowEntry" + } + } } } } diff --git a/src/cmd/hive/delegatedecider.go b/src/cmd/hive/delegatedecider.go index d2a5e45541..fcd45594a9 100644 --- a/src/cmd/hive/delegatedecider.go +++ b/src/cmd/hive/delegatedecider.go @@ -26,6 +26,23 @@ type delegateDeciderRunner struct { // model returns the per-feature model override ("" keeps the reviewer // model); nil means the "Let hive decide" override. model func(*config.Config) string + // maxTokens caps the completion; zero means delegateDeciderMaxTokens. + maxTokens int +} + +// workflowSpecMaxTokens fits a whole WorkflowModel JSON document. +const workflowSpecMaxTokens = 8000 + +// newWorkflowSpecRunner is the same reviewer-endpoint turn for the optional +// LLM refinement of written-spec ingest (POST /api/workflows/ingest/spec, +// RFC #17620 phase 6): the reviewer model, a larger completion budget. +func newWorkflowSpecRunner(cfg func() *config.Config) delegatedecision.TurnRunner { + return &delegateDeciderRunner{ + cfg: cfg, + http: &http.Client{Timeout: delegateDeciderTimeout}, + model: func(*config.Config) string { return "" }, + maxTokens: workflowSpecMaxTokens, + } } const ( @@ -92,7 +109,7 @@ func (r *delegateDeciderRunner) RunDeciderTurn(ctx context.Context, profile, sys Model: model, Messages: []delegateDeciderMessage{{Role: "system", Content: system}, {Role: "user", Content: user}}, Temperature: 0, - MaxTokens: delegateDeciderMaxTokens, + MaxTokens: r.completionBudget(), }) if err != nil { return "", fmt.Errorf("%s: marshal request: %w", profile, err) @@ -128,3 +145,10 @@ func (r *delegateDeciderRunner) RunDeciderTurn(ctx context.Context, profile, sys } return cr.Choices[0].Message.Content, nil } + +func (r *delegateDeciderRunner) completionBudget() int { + if r.maxTokens > 0 { + return r.maxTokens + } + return delegateDeciderMaxTokens +} diff --git a/src/cmd/hive/delegatedecider_test.go b/src/cmd/hive/delegatedecider_test.go index bc30cd1ebd..1df14d950b 100644 --- a/src/cmd/hive/delegatedecider_test.go +++ b/src/cmd/hive/delegatedecider_test.go @@ -61,3 +61,27 @@ func TestDelegateDeciderRunner(t *testing.T) { t.Fatalf("nil config: %v", err) } } + +func TestWorkflowSpecRunnerUsesLargerBudget(t *testing.T) { + t.Setenv(config.LiteLLMEndpointEnv, "") + var got delegateDeciderRequest + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewDecoder(r.Body).Decode(&got) + _, _ = w.Write([]byte(`{"choices":[{"message":{"content":"{}"}}]}`)) + })) + t.Cleanup(srv.Close) + + cfg := &config.Config{} + cfg.Governor.Trajectory.Endpoint = srv.URL + cfg.Governor.Trajectory.Model = "reviewer-model" + runner := newWorkflowSpecRunner(func() *config.Config { return cfg }) + if _, err := runner.RunDeciderTurn(context.Background(), delegatedecision.DeciderProfile, "sys", "usr"); err != nil { + t.Fatal(err) + } + if got.Model != "reviewer-model" || got.MaxTokens != workflowSpecMaxTokens { + t.Fatalf("request = %+v", got) + } + if b := newDelegateDeciderRunner(func() *config.Config { return cfg }).(*delegateDeciderRunner).completionBudget(); b != delegateDeciderMaxTokens { + t.Fatalf("default budget = %d", b) + } +} diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index f9cc3d6e82..0c660bc8cb 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -1094,8 +1094,11 @@ func (b *boot) wireBootClosures() { WorkflowItemSnapshot: func(ctx context.Context, repo string, number int) (github.WorkflowItemSnapshot, error) { return b.ghClient.WorkflowItemSnapshot(ctx, repo, number) }, - AgentMgr: b.agentMgr, - CruxRuntimeHealth: cruxRuntimeHealthFunc(b.cruxSupervision), + WorkflowRepoFile: workflowRepoFile(func() *github.Client { return b.ghClient }), + WorkflowClusterRunner: workflowClusterRunner(), + WorkflowSpecLLM: newWorkflowSpecRunner(func() *config.Config { return b.cfg }), + AgentMgr: b.agentMgr, + CruxRuntimeHealth: cruxRuntimeHealthFunc(b.cruxSupervision), // Provider gateways (#5565 slice 3): concrete openrouter/watsonx/ // linearagent adapters behind the dashboard's consumer-defined // interfaces — this composition root is the only non-test place that diff --git a/src/cmd/hive/workflow_ingest.go b/src/cmd/hive/workflow_ingest.go index 391e49e2d7..eee09f74de 100644 --- a/src/cmd/hive/workflow_ingest.go +++ b/src/cmd/hive/workflow_ingest.go @@ -5,9 +5,11 @@ import ( "encoding/json" "errors" "log" + "strings" "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/workflowmodel/ingest" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest/cluster" ) // errNoForgeClient is returned by the workflow ingest fetcher before a @@ -47,3 +49,30 @@ func workflowRepoFiles(client func() *github.Client) func(ctx context.Context, r return files, co.Ref, nil } } + +// workflowRepoFile is the dashboard's WorkflowRepoFile dependency +// (POST /api/workflows/ingest/spec {repo, path}, RFC #17620 phase 6): one +// default-branch file through the forge API. +func workflowRepoFile(client func() *github.Client) func(ctx context.Context, repo, path string) (string, error) { + return func(ctx context.Context, repo, path string) (string, error) { + c := client() + if c == nil { + return "", errNoForgeClient + } + owner, name, ok := strings.Cut(repo, "/") + if !ok || owner == "" || name == "" { + return "", errors.New("repo must be owner/name") + } + return c.GetFileContent(ctx, owner, name, strings.TrimPrefix(path, "/")) + } +} + +// workflowClusterRunner is the dashboard's WorkflowClusterRunner dependency: +// kubectl on PATH with a kubeconfig or in-cluster token, else nil so the +// cluster ingest route answers 422 instead of failing per call. +func workflowClusterRunner() cluster.Runner { + if ok, _ := cluster.Available(); !ok { + return nil + } + return cluster.Kubectl{} +} diff --git a/src/pkg/dashboard/api_workflows.go b/src/pkg/dashboard/api_workflows.go index 9d45dc69c4..0ab4374b2b 100644 --- a/src/pkg/dashboard/api_workflows.go +++ b/src/pkg/dashboard/api_workflows.go @@ -28,6 +28,7 @@ func (s *Server) registerWorkflowRoutes() { s.mux.HandleFunc("DELETE /api/workflows/{name}/{version}", s.handleWorkflowDelete) s.registerWorkflowVersionRoutes() s.mux.HandleFunc("POST /api/workflows/{name}/{version}/simulate", s.handleWorkflowSimulate) + s.registerWorkflowIngestSourceRoutes() } func (s *Server) workflowStore(w http.ResponseWriter) *wfstore.Store { diff --git a/src/pkg/dashboard/api_workflows_ingest_sources.go b/src/pkg/dashboard/api_workflows_ingest_sources.go new file mode 100644 index 0000000000..77fd312a99 --- /dev/null +++ b/src/pkg/dashboard/api_workflows_ingest_sources.go @@ -0,0 +1,366 @@ +package dashboard + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "strings" + "time" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest/cluster" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest/spec" +) + +// RFC hivecommons/hive#17620 phase 6: ingest Tekton from a live cluster +// context and written process specs (LLM-assisted, every element +// inferred). This file owns the three routes; api_workflows.go only calls +// registerWorkflowIngestSourceRoutes. + +const ( + // workflowSpecURLTimeout bounds fetching a spec by URL. + workflowSpecURLTimeout = 20 * time.Second + // workflowSpecLLMTimeout bounds the whole spec ingest including up to + // spec.MaxAttempts gateway turns. + workflowSpecLLMTimeout = 3 * time.Minute + // workflowSpecLLMProfile is the delegatedecision profile label for the + // spec-ingest turn (audit/logging only). + workflowSpecLLMProfile = "workflow-spec-ingest" + // workflowSpecMaxTextChars caps a pasted or fetched spec, mirroring + // spec.MaxBytes so the handler refuses before decoding a huge body. + workflowSpecMaxTextChars = spec.MaxBytes +) + +func (s *Server) registerWorkflowIngestSourceRoutes() { + s.mux.HandleFunc("GET /api/workflows/ingest/capabilities", s.handleWorkflowIngestCapabilities) + s.mux.HandleFunc("POST /api/workflows/ingest/cluster", s.handleWorkflowIngestCluster) + s.mux.HandleFunc("POST /api/workflows/ingest/spec", s.handleWorkflowIngestSpec) +} + +// workflowSpecLLMConfigured says whether the optional LLM stage can run: +// a runner is wired and the reviewer gateway resolves an endpoint and a +// model. The model id is reported so the UI and the model annotation can +// name it. +func (s *Server) workflowSpecLLMConfigured() (bool, string) { + if s.deps == nil || s.deps.WorkflowSpecLLM == nil || s.deps.Config == nil { + return false, "" + } + if !s.deps.Config.Governor.ReviewerReady() { + return false, "" + } + _, _, model := s.deps.Config.Governor.ResolveReviewer() + return true, model +} + +func (s *Server) workflowClusterRunner() cluster.Runner { + if s.deps == nil { + return nil + } + return s.deps.WorkflowClusterRunner +} + +// handleWorkflowIngestCapabilities answers GET /api/workflows/ingest/capabilities: +// {cluster, clusterReason, contexts[], spec: {llm, model}}. Owner-gated +// like the ingest routes it describes. +func (s *Server) handleWorkflowIngestCapabilities(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + resp := map[string]interface{}{"cluster": false, "contexts": []string{}} + if runner := s.workflowClusterRunner(); runner != nil { + resp["cluster"] = true + ctx, cancel := context.WithTimeout(r.Context(), cluster.KubectlTimeout) + defer cancel() + contexts, err := cluster.Contexts(ctx, runner) + if err != nil { + resp["clusterReason"] = "kube contexts unavailable: " + err.Error() + } else if contexts != nil { + resp["contexts"] = contexts + } + } else { + _, reason := cluster.Available() + resp["clusterReason"] = reason + } + llm, model := s.workflowSpecLLMConfigured() + resp["spec"] = map[string]interface{}{"llm": llm, "model": model} + jsonResponse(w, resp) +} + +// workflowIngestClusterRequest is the body of POST /api/workflows/ingest/cluster. +type workflowIngestClusterRequest struct { + Context string `json:"context"` + Namespace string `json:"namespace"` + AllNamespaces bool `json:"allNamespaces,omitempty"` + Selector string `json:"selector,omitempty"` + // RunsPerPipeline is how many recent PipelineRuns per pipeline to keep + // (default cluster.DefaultRunsPerPipeline, capped). + RunsPerPipeline int `json:"runsPerPipeline,omitempty"` + // Repository scopes the model (owner/name); defaults to the context + // and namespace so the draft is still addressable. + Repository string `json:"repository,omitempty"` + Save bool `json:"save,omitempty"` + Name string `json:"name,omitempty"` +} + +// handleWorkflowIngestCluster lists Tekton objects from a kube context and +// ingests them through the file-based Tekton parser. Same response shape as +// POST /api/workflows/ingest plus source, runs and namespaces. +func (s *Server) handleWorkflowIngestCluster(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + var req workflowIngestClusterRequest + if err := decodeBody(r, &req); err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + runner := s.workflowClusterRunner() + if runner == nil { + _, reason := cluster.Available() + jsonError(w, "cluster ingest not available on this spoke: "+reason, http.StatusUnprocessableEntity) + return + } + src := cluster.Source{ + Context: strings.TrimSpace(req.Context), + Namespace: strings.TrimSpace(req.Namespace), + AllNamespaces: req.AllNamespaces, + Selector: strings.TrimSpace(req.Selector), + RunsPerPipeline: req.RunsPerPipeline, + } + repo := strings.TrimSpace(req.Repository) + if repo == "" { + repo = "cluster/" + workflowmodel.Slug(src.String()) + } + ctx, cancel := context.WithTimeout(r.Context(), workflowIngestTimeout) + defer cancel() + res, err := cluster.Ingest(ctx, runner, src, workflowmodel.Scope{Forge: "github.com", Repository: repo}) + if err != nil { + jsonError(w, err.Error(), clusterErrorStatus(err)) + return + } + m := res.Model + if req.Name != "" { + m.Metadata.Name = req.Name + } + p, diags, cerr := workflowmodel.Compile(m) + if diags == nil { + diags = []workflowmodel.Diagnostic{} + } + resp := map[string]interface{}{ + "model": m, "compiled": cerr == nil, "diagnostics": diags, + "ref": res.Source, "source": res.Source, "files": res.Files, "pipelines": len(m.Spec.Pipelines), + "runs": res.Runs, "namespaces": res.Namespaces, + } + if p != nil { + resp["profile"] = p + } + s.finishWorkflowIngest(w, r, req.Save, m, resp, fmt.Sprintf("from %s files=%d", res.Source, res.Files)) +} + +// clusterErrorStatus maps cluster ingest errors to HTTP statuses with the +// package's clear messages. +func clusterErrorStatus(err error) int { + switch { + case errors.Is(err, cluster.ErrNamespaceRequired): + return http.StatusBadRequest + case errors.Is(err, cluster.ErrForbidden): + return http.StatusForbidden + case errors.Is(err, cluster.ErrNoContext): + return http.StatusNotFound + case errors.Is(err, cluster.ErrNoCRD), errors.Is(err, cluster.ErrNoRunner): + return http.StatusUnprocessableEntity + case errors.Is(err, cluster.ErrTooLarge): + return http.StatusRequestEntityTooLarge + } + return http.StatusBadGateway +} + +// workflowIngestSpecRequest is the body of POST /api/workflows/ingest/spec. +// Exactly one of Text, Repo+Path or URL is set. +type workflowIngestSpecRequest struct { + Text string `json:"text,omitempty"` + Repo string `json:"repo,omitempty"` + Path string `json:"path,omitempty"` + URL string `json:"url,omitempty"` + // UseLLM asks for the gateway refinement; nil means "if configured". + UseLLM *bool `json:"useLLM,omitempty"` + // Repository scopes the model (owner/name); defaults to Repo or + // spec/. + Repository string `json:"repository,omitempty"` + Save bool `json:"save,omitempty"` + Name string `json:"name,omitempty"` +} + +// handleWorkflowIngestSpec turns a written process spec into a draft model. +// Response: {model, compiled, diagnostics, profile?, source, llm:{used, +// model, attempts, accepted, error?}, sections[], entry?}. +func (s *Server) handleWorkflowIngestSpec(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + var req workflowIngestSpecRequest + if err := decodeBody(r, &req); err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + req.Repo, req.Path, req.URL = strings.TrimSpace(req.Repo), strings.TrimSpace(req.Path), strings.TrimSpace(req.URL) + inputs := 0 + for _, set := range []bool{strings.TrimSpace(req.Text) != "", req.Repo != "" || req.Path != "", req.URL != ""} { + if set { + inputs++ + } + } + if inputs != 1 { + jsonError(w, "set exactly one of text, repo+path or url", http.StatusBadRequest) + return + } + llmReady, llmModel := s.workflowSpecLLMConfigured() + useLLM := llmReady + if req.UseLLM != nil { + useLLM = *req.UseLLM + } + if useLLM && !llmReady { + jsonError(w, "AI assistance requested but no model gateway is configured (Settings → Model Gateways); retry with useLLM=false for the deterministic draft", http.StatusUnprocessableEntity) + return + } + + ctx, cancel := context.WithTimeout(r.Context(), workflowSpecLLMTimeout) + defer cancel() + text, origin, repo, status, err := s.loadWorkflowSpecText(ctx, req) + if err != nil { + jsonError(w, err.Error(), status) + return + } + if strings.TrimSpace(req.Repository) != "" { + repo = strings.TrimSpace(req.Repository) + } + opts := spec.Options{Origin: origin, Name: strings.TrimSpace(req.Name), Scope: workflowmodel.Scope{Forge: "github.com", Repository: repo}} + if useLLM { + runner := s.deps.WorkflowSpecLLM + opts.ModelID = llmModel + opts.Completer = spec.CompleterFunc(func(ctx context.Context, system, user string) (string, error) { + return runner.RunDeciderTurn(ctx, workflowSpecLLMProfile, system, user) + }) + } + res, err := spec.Ingest(ctx, text, opts) + if err != nil { + status := http.StatusBadRequest + if errors.Is(err, spec.ErrTooLarge) { + status = http.StatusRequestEntityTooLarge + } + jsonError(w, err.Error(), status) + return + } + diags := res.Diagnostics + if diags == nil { + diags = []workflowmodel.Diagnostic{} + } + resp := map[string]interface{}{ + "model": res.Model, "compiled": res.Compiled, "diagnostics": diags, + "source": res.Source, "llm": res.LLM, "sections": res.Sections, + } + if res.Profile != nil { + resp["profile"] = res.Profile + } + s.finishWorkflowIngest(w, r, req.Save, res.Model, resp, fmt.Sprintf("from %s llm=%v attempts=%d", res.Source, res.LLM.Accepted, res.LLM.Attempts)) +} + +// loadWorkflowSpecText resolves the spec body from the request: pasted +// text, a default-branch file through the forge, or an https URL (public +// hosts only, size-capped). It returns the text, its origin label, the +// repository to scope the model to and, on error, the HTTP status. +func (s *Server) loadWorkflowSpecText(ctx context.Context, req workflowIngestSpecRequest) (text, origin, repo string, status int, err error) { + switch { + case strings.TrimSpace(req.Text) != "": + if len(req.Text) > workflowSpecMaxTextChars { + return "", "", "", http.StatusRequestEntityTooLarge, spec.ErrTooLarge + } + return req.Text, "pasted", "spec/pasted", 0, nil + case req.URL != "": + u, perr := url.Parse(req.URL) + if perr != nil || u.Scheme != "https" || u.Host == "" { + return "", "", "", http.StatusBadRequest, errors.New("url must be https://host/path") + } + if isPrivateURL(ctx, req.URL) { + return "", "", "", http.StatusBadRequest, errors.New("url resolves to a private or internal host") + } + body, ferr := fetchWorkflowSpecURL(ctx, req.URL) + if ferr != nil { + return "", "", "", http.StatusBadGateway, fmt.Errorf("fetch %s: %w", req.URL, ferr) + } + return body, req.URL, "spec/" + workflowmodel.Slug(u.Host+u.Path), 0, nil + default: + if req.Repo == "" || req.Path == "" || !strings.Contains(req.Repo, "/") { + return "", "", "", http.StatusBadRequest, errors.New("repo (owner/name) and path are both required") + } + if s.deps == nil || s.deps.WorkflowRepoFile == nil { + return "", "", "", http.StatusServiceUnavailable, errors.New("repository spec ingest not available: no forge client") + } + body, ferr := s.deps.WorkflowRepoFile(ctx, req.Repo, req.Path) + if ferr != nil { + return "", "", "", http.StatusBadGateway, fmt.Errorf("fetch %s:%s: %w", req.Repo, req.Path, ferr) + } + if len(body) > workflowSpecMaxTextChars { + return "", "", "", http.StatusRequestEntityTooLarge, spec.ErrTooLarge + } + return body, req.Repo + ":" + strings.TrimPrefix(req.Path, "/"), req.Repo, 0, nil + } +} + +// workflowSpecHTTP is the client for URL specs; a seam for tests. +var workflowSpecHTTP = &http.Client{Timeout: workflowSpecURLTimeout, CheckRedirect: noRedirectToPrivate} + +func fetchWorkflowSpecURL(ctx context.Context, rawURL string) (string, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) + if err != nil { + return "", err + } + req.Header.Set("Accept", "text/markdown, text/plain;q=0.9, */*;q=0.1") + resp, err := workflowSpecHTTP.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("status %d", resp.StatusCode) + } + body, err := io.ReadAll(io.LimitReader(resp.Body, int64(workflowSpecMaxTextChars)+1)) + if err != nil { + return "", err + } + if len(body) > workflowSpecMaxTextChars { + return "", spec.ErrTooLarge + } + return string(body), nil +} + +// finishWorkflowIngest either answers the preview or saves the model as +// the next draft version, audits it and answers 201 with the entry; the +// same tail POST /api/workflows/ingest has. +func (s *Server) finishWorkflowIngest(w http.ResponseWriter, r *http.Request, save bool, m *workflowmodel.Model, resp map[string]interface{}, detail string) { + if !save { + jsonResponse(w, resp) + return + } + st := s.workflowStore(w) + if st == nil { + return + } + version, err := nextWorkflowVersion(st, m.Metadata.Name) + if err != nil { + workflowStoreError(w, err) + return + } + m.Spec.Meta.Version = version + e, err := st.Save(m, requestUser(r), time.Now().UTC()) + if err != nil { + workflowStoreError(w, err) + return + } + s.auditFromRequest(r, "workflow_ingest", fmt.Sprintf("%s %s compiled=%v", e.Key(), detail, e.Compiled), "") + resp["entry"] = e + jsonStatusResponse(w, http.StatusCreated, resp) +} diff --git a/src/pkg/dashboard/api_workflows_ingest_sources_test.go b/src/pkg/dashboard/api_workflows_ingest_sources_test.go new file mode 100644 index 0000000000..11a241d36a --- /dev/null +++ b/src/pkg/dashboard/api_workflows_ingest_sources_test.go @@ -0,0 +1,456 @@ +package dashboard + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest/cluster" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest/spec" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" +) + +// fakeClusterRunner answers kubectl get with a one-pipeline Tekton +// namespace and kubectl config get-contexts with two contexts. +type fakeClusterRunner struct { + err error + calls int +} + +func (f *fakeClusterRunner) Run(_ context.Context, args ...string) ([]byte, error) { + f.calls++ + if f.err != nil { + return nil, f.err + } + argv := strings.Join(args, " ") + if strings.HasPrefix(argv, "config get-contexts") { + return []byte("kind-ci\nprod\n"), nil + } + items := []any{} + meta := func(kind, name string) map[string]any { + return map[string]any{"apiVersion": "tekton.dev/v1", "kind": kind, "metadata": map[string]any{"name": name, "namespace": "ci"}} + } + switch { + case strings.Contains(argv, "get pipelines."): + p := meta("Pipeline", "build") + p["spec"] = map[string]any{"tasks": []any{map[string]any{"name": "test", "taskRef": map[string]any{"name": "go-test"}}}} + items = append(items, p) + case strings.Contains(argv, "get pipelineruns."): + r := meta("PipelineRun", "build-1") + r["metadata"].(map[string]any)["annotations"] = map[string]any{"pipelinesascode.tekton.dev/on-event": "[pull_request]"} + r["spec"] = map[string]any{"pipelineRef": map[string]any{"name": "build"}} + r["status"] = map[string]any{"completionTime": "2026-01-01T00:00:00Z", "conditions": []any{map[string]any{"type": "Succeeded", "status": "False", "reason": "Failed"}}} + items = append(items, r) + case strings.Contains(argv, "triggers.tekton.dev"): + return nil, errors.New(`error: the server doesn't have a resource type "eventlisteners"`) + } + return json.Marshal(map[string]any{"items": items}) +} + +type fakeTurnRunner struct { + answers []string + err error + calls int + system string + user string +} + +func (f *fakeTurnRunner) RunDeciderTurn(_ context.Context, _, system, user string) (string, error) { + f.calls++ + f.system, f.user = system, user + if f.err != nil { + return "", f.err + } + return f.answers[(f.calls-1)%len(f.answers)], nil +} + +func configureSpecLLM(s *Server, ready bool) { + g := &s.deps.Config.Governor + if ready { + g.Trajectory.Endpoint = "https://reviewer.local" + g.Trajectory.Model = "reviewer/model-1" + return + } + g.Trajectory.Endpoint, g.Trajectory.Model = "", "" + g.LiteLLM.Endpoint, g.LiteLLM.DefaultModel = "", "" +} + +const specSample = "# Release process\n\n1. Request: the author opens an issue.\n2. Review by a maintainer. If rejected it goes back to request.\n3. Approve\n4. Merge\n" + +func wfDecode(t *testing.T, rec *httptest.ResponseRecorder, v any) { + t.Helper() + if err := json.Unmarshal(rec.Body.Bytes(), v); err != nil { + t.Fatalf("%v: %s", err, rec.Body) + } +} + +func TestWorkflowIngestCapabilities(t *testing.T) { + s := workflowTestServer(t, false) + configureSpecLLM(s, false) + s.deps.WorkflowClusterRunner = nil + if rec := wfReq(s, http.MethodGet, "/api/workflows/ingest/capabilities", nil, false); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner %d", rec.Code) + } + rec := wfReq(s, http.MethodGet, "/api/workflows/ingest/capabilities", nil, true) + var caps struct { + Cluster bool `json:"cluster"` + ClusterReason string `json:"clusterReason"` + Contexts []string `json:"contexts"` + Spec struct { + LLM bool `json:"llm"` + Model string `json:"model"` + } `json:"spec"` + } + wfDecode(t, rec, &caps) + if caps.Cluster || caps.Contexts == nil || len(caps.Contexts) != 0 || caps.Spec.LLM || caps.Spec.Model != "" { + t.Fatalf("%+v", caps) + } + + s.deps.WorkflowClusterRunner = &fakeClusterRunner{} + s.deps.WorkflowSpecLLM = &fakeTurnRunner{} + configureSpecLLM(s, true) + rec = wfReq(s, http.MethodGet, "/api/workflows/ingest/capabilities", nil, true) + wfDecode(t, rec, &caps) + if !caps.Cluster || strings.Join(caps.Contexts, ",") != "kind-ci,prod" || !caps.Spec.LLM || caps.Spec.Model != "reviewer/model-1" { + t.Fatalf("%+v", caps) + } + // A runner that cannot list contexts still reports cluster=true with a reason. + s.deps.WorkflowClusterRunner = &fakeClusterRunner{err: errors.New("kubeconfig unreadable")} + rec = wfReq(s, http.MethodGet, "/api/workflows/ingest/capabilities", nil, true) + wfDecode(t, rec, &caps) + if !caps.Cluster || !strings.Contains(caps.ClusterReason, "kubeconfig unreadable") || len(caps.Contexts) != 0 { + t.Fatalf("%+v", caps) + } + // A gateway runner wired but unconfigured is not advertised. + configureSpecLLM(s, false) + rec = wfReq(s, http.MethodGet, "/api/workflows/ingest/capabilities", nil, true) + wfDecode(t, rec, &caps) + if caps.Spec.LLM { + t.Fatalf("%+v", caps) + } +} + +func TestWorkflowIngestClusterPreviewAndSave(t *testing.T) { + s := workflowTestServer(t, true) + runner := &fakeClusterRunner{} + s.deps.WorkflowClusterRunner = runner + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/cluster", map[string]any{"namespace": "ci"}, false); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner %d", rec.Code) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/cluster", "{bad", true); rec.Code != http.StatusBadRequest { + t.Fatalf("bad body %d", rec.Code) + } + rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/cluster", map[string]any{"context": "kind-ci", "namespace": "ci", "selector": "team=a"}, true) + if rec.Code != http.StatusOK { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + var resp struct { + Compiled bool `json:"compiled"` + Pipelines int `json:"pipelines"` + Files int `json:"files"` + Source string `json:"source"` + Ref string `json:"ref"` + Runs []cluster.RunStatus `json:"runs"` + Namespaces []string `json:"namespaces"` + Model workflowmodel.Model `json:"model"` + Diags []workflowmodel.Diagnostic `json:"diagnostics"` + Profile *workflowmodel.Profile `json:"profile"` + Entry *wfstore.Entry `json:"entry"` + } + wfDecode(t, rec, &resp) + if !resp.Compiled || resp.Pipelines != 1 || resp.Files != 2 || resp.Source != "cluster:kind-ci/ci" || resp.Ref != resp.Source || resp.Profile == nil || resp.Entry != nil { + t.Fatalf("%+v", resp) + } + if len(resp.Runs) != 1 || resp.Runs[0].Status != "Failed" || strings.Join(resp.Namespaces, ",") != "ci" { + t.Fatalf("runs %+v ns %v", resp.Runs, resp.Namespaces) + } + if resp.Model.Spec.Scope.Repository != "cluster/cluster-kind-ci-ci" || resp.Model.Spec.Meta.Provenance != workflowmodel.ProvenanceIngestedUnreviewed { + t.Fatalf("scope %+v meta %+v", resp.Model.Spec.Scope, resp.Model.Spec.Meta) + } + if resp.Model.Metadata.Annotations[cluster.AnnotationObservedRun+resp.Model.Spec.Pipelines[0].ID] != "Failed" { + t.Fatalf("observed run annotation %v", resp.Model.Metadata.Annotations) + } + + before := time.Now().Add(-time.Minute) + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/cluster", map[string]any{"namespace": "ci", "repository": "acme/widgets", "name": "ci-draft", "save": true}, true) + if rec.Code != http.StatusCreated { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + wfDecode(t, rec, &resp) + if resp.Entry == nil || resp.Entry.Key() != "ci-draft@1" || resp.Model.Spec.Scope.Repository != "acme/widgets" || resp.Source != "cluster:current-context/ci" { + t.Fatalf("%+v", resp) + } + audits := s.audit.RecentWithPrefixSince(before, "workflow_ingest") + if len(audits) != 1 || !strings.Contains(audits[0].Detail, "ci-draft@1 from cluster:current-context/ci files=2 compiled=true") { + t.Fatalf("audit %+v", audits) + } + // Saving needs the store. + s2 := workflowTestServer(t, false) + s2.deps.WorkflowClusterRunner = runner + if rec := wfReq(s2, http.MethodPost, "/api/workflows/ingest/cluster", map[string]any{"namespace": "ci", "save": true}, true); rec.Code != http.StatusServiceUnavailable { + t.Fatalf("no store %d %s", rec.Code, rec.Body) + } +} + +func TestWorkflowIngestClusterErrors(t *testing.T) { + s := workflowTestServer(t, false) + s.deps.WorkflowClusterRunner = nil + rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/cluster", map[string]any{"namespace": "ci"}, true) + if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "not available on this spoke") { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + cases := []struct { + name string + body map[string]any + err error + code int + }{ + {"namespace required", map[string]any{}, nil, http.StatusBadRequest}, + {"forbidden", map[string]any{"namespace": "ci"}, errors.New("Error from server (Forbidden): pipelines.tekton.dev is forbidden"), http.StatusForbidden}, + {"no context", map[string]any{"namespace": "ci", "context": "nope"}, errors.New(`error: context "nope" does not exist`), http.StatusNotFound}, + {"no crd", map[string]any{"namespace": "ci"}, errors.New(`error: the server doesn't have a resource type "pipelines"`), http.StatusUnprocessableEntity}, + {"other", map[string]any{"namespace": "ci"}, errors.New("dial tcp: connection refused"), http.StatusBadGateway}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + s.deps.WorkflowClusterRunner = &fakeClusterRunner{err: tc.err} + rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/cluster", tc.body, true) + if rec.Code != tc.code { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + }) + } + if clusterErrorStatus(cluster.ErrTooLarge) != http.StatusRequestEntityTooLarge || clusterErrorStatus(cluster.ErrNoRunner) != http.StatusUnprocessableEntity { + t.Fatal("status mapping") + } +} + +type specResponse struct { + Compiled bool `json:"compiled"` + Source string `json:"source"` + LLM spec.LLMReport `json:"llm"` + Sections []spec.Section `json:"sections"` + Model workflowmodel.Model `json:"model"` + Diags []workflowmodel.Diagnostic `json:"diagnostics"` + Profile *workflowmodel.Profile `json:"profile"` + Entry *wfstore.Entry `json:"entry"` +} + +func TestWorkflowIngestSpecTextDeterministic(t *testing.T) { + s := workflowTestServer(t, true) + configureSpecLLM(s, false) + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample}, false); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner %d", rec.Code) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", "{bad", true); rec.Code != http.StatusBadRequest { + t.Fatalf("bad body %d", rec.Code) + } + for _, body := range []map[string]any{{}, {"text": "x", "url": "https://a/b"}, {"text": "x", "repo": "a/b", "path": "p"}} { + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", body, true); rec.Code != http.StatusBadRequest { + t.Fatalf("%v -> %d", body, rec.Code) + } + } + // No gateway: default is deterministic; asking for the LLM is 422. + rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample, "useLLM": true}, true) + if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "no model gateway") { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample}, true) + if rec.Code != http.StatusOK { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + var resp specResponse + wfDecode(t, rec, &resp) + if !resp.Compiled || resp.LLM.Used || resp.Source != "spec:pasted" || len(resp.Sections) != 1 || resp.Profile == nil || resp.Entry != nil { + t.Fatalf("%+v", resp) + } + if resp.Model.Spec.Scope.Repository != "spec/pasted" || resp.Model.Metadata.Name != "release-process" || resp.Model.Spec.Meta.Source != workflowmodel.SourceSpec { + t.Fatalf("%+v %+v", resp.Model.Metadata, resp.Model.Spec.Meta) + } + for _, st := range resp.Model.Spec.States { + if st.Lifecycle.Confidence != workflowmodel.ConfidenceInferred || !strings.HasPrefix(st.Lifecycle.Citations[0].Path, "spec:Release process#L") { + t.Fatalf("state %+v", st) + } + } + if _, ok := resp.Model.Metadata.Annotations[spec.AnnotationLLMAssisted]; ok { + t.Fatal("deterministic result must not claim LLM assistance") + } + // Save + audit. + before := time.Now().Add(-time.Minute) + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample, "name": "proc", "repository": "acme/widgets", "save": true}, true) + if rec.Code != http.StatusCreated { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + wfDecode(t, rec, &resp) + if resp.Entry == nil || resp.Entry.Key() != "proc@1" || resp.Model.Spec.Scope.Repository != "acme/widgets" { + t.Fatalf("%+v", resp.Entry) + } + audits := s.audit.RecentWithPrefixSince(before, "workflow_ingest") + if len(audits) != 1 || !strings.Contains(audits[0].Detail, "proc@1 from spec:pasted llm=false attempts=0") { + t.Fatalf("audit %+v", audits) + } + // Size caps. + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": strings.Repeat("x", workflowSpecMaxTextChars+1)}, true) + if rec.Code != http.StatusRequestEntityTooLarge { + t.Fatalf("%d", rec.Code) + } +} + +func TestWorkflowIngestSpecWithLLM(t *testing.T) { + s := workflowTestServer(t, false) + configureSpecLLM(s, true) + // The gateway answers with the deterministic skeleton relabelled, which + // compiles; the handler must mark it inferred and name the model. + secs := spec.Split(specSample) + good := spec.Extract("pasted", "", workflowmodel.Scope{Forge: "github.com", Repository: "spec/pasted"}, secs) + good.Spec.States[0].Label = "Request a release" + raw, _ := json.Marshal(good) + runner := &fakeTurnRunner{answers: []string{"```json\n" + string(raw) + "\n```"}} + s.deps.WorkflowSpecLLM = runner + + rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample}, true) + if rec.Code != http.StatusOK { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + var resp specResponse + wfDecode(t, rec, &resp) + if !resp.LLM.Used || !resp.LLM.Accepted || resp.LLM.Attempts != 1 || resp.LLM.Model != "reviewer/model-1" || !resp.Compiled { + t.Fatalf("%+v", resp.LLM) + } + if resp.Model.Spec.States[0].Label != "Request a release" || resp.Model.Metadata.Annotations[spec.AnnotationLLMAssisted] != "LLM-assisted (model reviewer/model-1)" { + t.Fatalf("%+v %v", resp.Model.Spec.States[0], resp.Model.Metadata.Annotations) + } + if runner.system != spec.SystemPrompt || !strings.Contains(runner.user, "# Specification (pasted)") { + t.Fatal("gateway must receive the package prompt") + } + // useLLM=false opts out even when configured. + runner.calls = 0 + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample, "useLLM": false}, true) + wfDecode(t, rec, &resp) + if rec.Code != http.StatusOK || resp.LLM.Used || runner.calls != 0 { + t.Fatalf("%d %+v calls=%d", rec.Code, resp.LLM, runner.calls) + } + // A gateway failure falls back to the skeleton and is reported, not a 5xx. + s.deps.WorkflowSpecLLM = &fakeTurnRunner{err: errors.New("502 from gateway")} + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"text": specSample}, true) + wfDecode(t, rec, &resp) + if rec.Code != http.StatusOK || !resp.LLM.Used || resp.LLM.Accepted || !strings.Contains(resp.LLM.Error, "502 from gateway") || !resp.Compiled { + t.Fatalf("%d %+v", rec.Code, resp.LLM) + } + found := false + for _, f := range resp.Model.Spec.Coverage.Findings { + found = found || f.Code == spec.CodeLLMFellBack + } + if !found { + t.Fatalf("findings %+v", resp.Model.Spec.Coverage.Findings) + } +} + +func TestWorkflowIngestSpecFromRepoAndURL(t *testing.T) { + s := workflowTestServer(t, false) + configureSpecLLM(s, false) + // repo+path validation and missing forge client. + s.deps.WorkflowRepoFile = nil + for body, code := range map[string]int{ + `{"repo":"acme/widgets"}`: http.StatusBadRequest, + `{"path":"docs/p.md"}`: http.StatusBadRequest, + `{"repo":"nope","path":"docs/p.md"}`: http.StatusBadRequest, + `{"repo":"acme/widgets","path":"docs/p.md"}`: http.StatusServiceUnavailable, + } { + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", body, true); rec.Code != code { + t.Fatalf("%s -> %d %s", body, rec.Code, rec.Body) + } + } + var asked string + s.deps.WorkflowRepoFile = func(_ context.Context, repo, path string) (string, error) { + asked = repo + ":" + path + switch path { + case "missing.md": + return "", errors.New("404") + case "huge.md": + return strings.Repeat("x", workflowSpecMaxTextChars+1), nil + } + return specSample, nil + } + rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"repo": "acme/widgets", "path": "/docs/process.md"}, true) + if rec.Code != http.StatusOK || asked != "acme/widgets:/docs/process.md" { + t.Fatalf("%d %s asked=%q", rec.Code, rec.Body, asked) + } + var resp specResponse + wfDecode(t, rec, &resp) + if resp.Source != "spec:acme/widgets:docs/process.md" || resp.Model.Spec.Scope.Repository != "acme/widgets" { + t.Fatalf("%+v", resp) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"repo": "acme/widgets", "path": "missing.md"}, true); rec.Code != http.StatusBadGateway { + t.Fatalf("%d", rec.Code) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"repo": "acme/widgets", "path": "huge.md"}, true); rec.Code != http.StatusRequestEntityTooLarge { + t.Fatalf("%d", rec.Code) + } + + // URL: https only, no private hosts, size cap, upstream status. + for _, u := range []string{"http://example.com/spec.md", "ftp://x/y", "https://", "not a url", "https://127.0.0.1/spec.md", "https://localhost/spec.md"} { + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"url": u}, true); rec.Code != http.StatusBadRequest { + t.Fatalf("%s -> %d %s", u, rec.Code, rec.Body) + } + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/spec.md": + fmt.Fprint(w, specSample) + case "/huge.md": + fmt.Fprint(w, strings.Repeat("x", workflowSpecMaxTextChars+1)) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + orig := workflowSpecHTTP + origResolver := privateURLResolver + t.Cleanup(func() { workflowSpecHTTP, privateURLResolver = orig, origResolver }) + privateURLResolver = func(_ context.Context, host string) ([]string, error) { + if host == "specs.example.com" { + return []string{"93.184.216.34"}, nil + } + return nil, errors.New("no such host") + } + workflowSpecHTTP = srv.Client() + // The handler insists on https and a public host; route the public name + // to the test server by rewriting in the transport. + workflowSpecHTTP.Transport = rewriteTransport{base: srv.Client().Transport, to: srv.URL} + + rec = wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"url": "https://specs.example.com/spec.md"}, true) + if rec.Code != http.StatusOK { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + wfDecode(t, rec, &resp) + if resp.Source != "spec:https://specs.example.com/spec.md" || resp.Model.Spec.Scope.Repository != "spec/specs-example-com-spec-md" { + t.Fatalf("%+v", resp) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"url": "https://specs.example.com/huge.md"}, true); rec.Code != http.StatusBadGateway || !strings.Contains(rec.Body.String(), "exceeds") { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/ingest/spec", map[string]any{"url": "https://specs.example.com/nope.md"}, true); rec.Code != http.StatusBadGateway || !strings.Contains(rec.Body.String(), "status 404") { + t.Fatalf("%d %s", rec.Code, rec.Body) + } +} + +// rewriteTransport sends every request to a fixed test server URL. +type rewriteTransport struct { + base http.RoundTripper + to string +} + +func (rt rewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) { + clone := req.Clone(req.Context()) + clone.URL.Scheme = "http" + clone.URL.Host = strings.TrimPrefix(rt.to, "http://") + return rt.base.RoundTrip(clone) +} diff --git a/src/pkg/dashboard/deps.go b/src/pkg/dashboard/deps.go index b142e3df11..251c12bddb 100644 --- a/src/pkg/dashboard/deps.go +++ b/src/pkg/dashboard/deps.go @@ -33,6 +33,7 @@ import ( "github.com/hivecommons/hive/pkg/scheduler" "github.com/hivecommons/hive/pkg/stageengine" "github.com/hivecommons/hive/pkg/tokens" + "github.com/hivecommons/hive/pkg/workflowmodel/ingest/cluster" ) type Dependencies struct { @@ -49,6 +50,19 @@ type Dependencies struct { // so the operator can simulate a real item. Nil disables the fetch; the // handler then answers 422 and the form still accepts hand-set facts. WorkflowItemSnapshot func(ctx context.Context, repo string, number int) (ghpkg.WorkflowItemSnapshot, error) + // WorkflowRepoFile fetches one default-branch file (a written process + // spec) for POST /api/workflows/ingest/spec {repo, path}. Nil disables + // repo-path spec ingest. + WorkflowRepoFile func(ctx context.Context, repo, path string) (string, error) + // WorkflowClusterRunner runs kubectl for POST /api/workflows/ingest/cluster + // (RFC #17620 phase 6). Nil means no kube access is configured on this + // spoke and the route answers 422. + WorkflowClusterRunner cluster.Runner + // WorkflowSpecLLM is the reviewer-gateway turn behind the optional LLM + // refinement of POST /api/workflows/ingest/spec. It is the only LLM use + // in the workflow-model stack; nil or an unconfigured gateway disables + // it and the route reports spec.llm=false in capabilities. + WorkflowSpecLLM delegatedecision.TurnRunner // AgentMgr is the agent runtime (hivecommons/hive#13536). The dashboard // programs against the crux.Runtime surface, not *agent.Manager; tests that // need manager-only seams unwrap it with agentManager. From 69eae944b2df516b4c5335d8a614e628b6747afe Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 11 Nov 2026 10:02:53 -0700 Subject: [PATCH 4/6] =?UTF-8?q?=E2=9C=A8=20dashboard:=20cluster=20and=20wr?= =?UTF-8?q?itten-spec=20panels=20on=20Workflows=20=E2=86=92=20Ingest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two collapsible panels appended to the Ingest view by a single guarded hook in renderWorkflowIngest: From a cluster (context select from kubectl's contexts or a text field, namespace, selector, all-namespaces, observed-run list) and From a written spec (paste, URL or repository file; a Use AI assistance checkbox shown only when the capabilities route reports a gateway, LLM note and section list in the result). Both reuse the repository ingest's result block, diagnostics and diagram. Pure renderers over module state, token-only CSS in its own sheet, node test for gating, modes, request validation and result rendering. Refs hivecommons/hive#17620 Signed-off-by: Andrew Anderson --- .../static-src/css/workflows-ingest.css | 21 ++ .../js/config-workflows-ingest-sources.js | 207 ++++++++++++++++ .../static-src/js/config-workflows-library.js | 3 +- src/pkg/dashboard/static-src/manifest.txt | 2 + src/pkg/dashboard/static/index.html | 231 +++++++++++++++++- .../workflows_ingest_sources_ui_test.go | 185 ++++++++++++++ 6 files changed, 647 insertions(+), 2 deletions(-) create mode 100644 src/pkg/dashboard/static-src/css/workflows-ingest.css create mode 100644 src/pkg/dashboard/static-src/js/config-workflows-ingest-sources.js create mode 100644 src/pkg/dashboard/workflows_ingest_sources_ui_test.go diff --git a/src/pkg/dashboard/static-src/css/workflows-ingest.css b/src/pkg/dashboard/static-src/css/workflows-ingest.css new file mode 100644 index 0000000000..10747b93c7 --- /dev/null +++ b/src/pkg/dashboard/static-src/css/workflows-ingest.css @@ -0,0 +1,21 @@ + /* Settings → Workflows → Ingest, other sources (#17620 phase 6): cluster and written-spec panels */ + .wf-src-panel { border: var(--line-width) solid var(--border); border-radius: var(--r); padding: var(--sp-0) var(--sp-4); margin-top: var(--sp-4); } + .wf-src-panel > summary { cursor: pointer; display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--sp-3); padding: var(--sp-3) var(--sp-0); list-style: none; } + .wf-src-panel > summary::-webkit-details-marker { display: none; } + .wf-src-panel > summary::before { content: '▸'; color: var(--muted); font-size: var(--fs-sm); } + .wf-src-panel[open] > summary::before { content: '▾'; } + .wf-src-panel > summary h3 { margin: var(--sp-0); font-size: var(--fs-base); } + .wf-src-panel > p, .wf-src-panel > .wf-ingest-form, .wf-src-panel > .wf-ingest-result, .wf-src-panel > .wf-ingest-error { margin-bottom: var(--sp-4); } + .wf-src-wide { grid-column: 1 / -1; } + .wf-src-check { display: flex; align-items: center; gap: var(--sp-2); font-size: var(--fs-sm); } + .wf-src-check input[type="checkbox"] { width: auto; margin: var(--sp-0); } + .wf-src-unavailable { color: var(--status-warn); } + .wf-src-note { margin: var(--sp-3) var(--sp-0) var(--sp-0); } + .wf-src-llm { display: inline-block; border-radius: var(--r-pill); padding: var(--sp-1) var(--sp-3); font-size: var(--fs-xs); font-weight: 700; border: var(--line-width) solid var(--border); color: var(--muted); } + .wf-src-llm-on { color: var(--brand); } + .wf-src-llm-fallback { color: var(--status-warn); } + .wf-src-runs, .wf-src-sections { margin: var(--sp-0); padding-left: var(--sp-6); font-size: var(--fs-sm); } + .wf-src-run-status { font-weight: 700; color: var(--muted); } + .wf-src-run-succeeded { color: var(--status-ok); } + .wf-src-run-failed { color: var(--status-error); } + .wf-src-run-running { color: var(--status-warn); } diff --git a/src/pkg/dashboard/static-src/js/config-workflows-ingest-sources.js b/src/pkg/dashboard/static-src/js/config-workflows-ingest-sources.js new file mode 100644 index 0000000000..a582205e9c --- /dev/null +++ b/src/pkg/dashboard/static-src/js/config-workflows-ingest-sources.js @@ -0,0 +1,207 @@ + // Settings → Workflows → Ingest: the two extra sources of RFC phase 6 + // (hivecommons/hive#17620) — Tekton from a live cluster context and a + // written process spec, optionally refined by the configured model + // gateway. Rendered by renderWorkflowIngestSources(state, owner), which + // renderWorkflowIngest appends to its HTML. Renderers are pure over + // _wfIngestSrc; the wfIngestSrc* handlers mutate it and rerender. + const WF_INGEST_SPEC_MODES = [['text', 'Paste the spec'], ['url', 'Fetch from a URL'], ['repo', 'Read a repository file']]; + const WF_INGEST_SRC_CAPS_UNKNOWN = { cluster: false, clusterReason: '', contexts: [], spec: { llm: false, model: '' } }; + const _wfIngestSrc = { + caps: null, capsRequested: false, capsError: '', + cluster: { open: false, context: '', namespace: '', allNamespaces: false, selector: '', name: '', busy: false, result: null, error: '' }, + spec: { open: false, mode: 'text', text: '', url: '', repo: '', path: '', useLLM: true, name: '', busy: false, result: null, error: '' }, + }; + + function wfIngestSrcCaps() { + const c = _wfIngestSrc.caps || WF_INGEST_SRC_CAPS_UNKNOWN; + return { + cluster: !!c.cluster, + clusterReason: String(c.clusterReason || ''), + contexts: Array.isArray(c.contexts) ? c.contexts.filter(Boolean) : [], + spec: { llm: !!(c.spec && c.spec.llm), model: String((c.spec && c.spec.model) || '') }, + }; + } + + // Shared result block: the same summary / diagnostics / diagram the + // repository ingest shows, plus the per-source extras passed in. + function renderWorkflowIngestSourceResult(r, extras) { + const e = r.entry || null; + const cols = (r.profile && r.profile.columns) || (r.model && r.model.spec && r.model.spec.columns) || []; + const pipelines = Array.isArray(r.pipelines) ? r.pipelines : []; + return ''; + } + + function renderWorkflowIngestClusterRuns(runs) { + const list = Array.isArray(runs) ? runs : []; + if (!list.length) return '

No PipelineRuns observed: the model carries no run status.

'; + return '

Observed runs

Last run per pipeline, recorded as an observed annotation — not an inferred lifecycle element.

    ' + + list.map(r => `
  • ${esc(r.namespace || '')}/${esc(r.pipeline || '')} ${esc(r.status || 'Unknown')}${r.run ? ` ${esc(r.run)}` : ''}${r.completed ? ` ${esc(formatDashboardDateTime(r.completed))}` : ''}
  • `).join('') + + '
'; + } + + function renderWorkflowIngestSpecNotes(r) { + const llm = r.llm || {}; + const sections = Array.isArray(r.sections) ? r.sections : []; + let note; + if (!llm.used) note = 'Deterministic only — step lists, conditional sentences and role nouns read without a model.'; + else if (llm.accepted) note = `LLM-assisted — refined by ${esc(llm.model || 'the configured gateway')} in ${esc(String(llm.attempts || 1))} attempt${llm.attempts === 1 ? '' : 's'}; every element is still inferred and cites the spec.`; + else note = `LLM fell back — ${esc(llm.model || 'the gateway')} did not return a compiling model after ${esc(String(llm.attempts || 0))} attempt${llm.attempts === 1 ? '' : 's'}${llm.error ? ` (${esc(llm.error)})` : ''}; showing the deterministic skeleton.`; + return `

${note}

` + + (sections.length ? '

Spec sections

    ' + sections.map(s => `
  • L${esc(String(s.startLine || 1))}–L${esc(String(s.endLine || s.startLine || 1))} ${esc(s.heading || '')}
  • `).join('') + '
' : ''); + } + + function renderWorkflowIngestClusterPanel(caps, owner) { + const st = _wfIngestSrc.cluster; + const disabled = !owner || st.busy || !caps.cluster ? 'disabled' : ''; + const contexts = caps.contexts; + const ctxField = contexts.length + ? `` + : ``; + let result = ''; + if (st.error) result = `
${esc(st.error)}
`; + else if (st.result) result = renderWorkflowIngestSourceResult(st.result, renderWorkflowIngestClusterRuns(st.result.runs) + (Array.isArray(st.result.namespaces) && st.result.namespaces.length > 1 ? `

Namespaces: ${st.result.namespaces.map(n => `${esc(n)}`).join(', ')}

` : '')); + return `
` + + '

From a cluster

Tekton Pipelines, Tasks, PipelineRuns and Triggers read through kubectl
' + + '

Lists the Tekton resources of one namespace (or every namespace, capped) in a kube context, writes them as cluster/<namespace>/<kind>/<name>.yaml and runs the same Tekton ingester as a repository, so citations point at those paths. The last PipelineRun status per pipeline is recorded as observed, never inferred.

' + + (caps.cluster ? '' : `

No kube access on this spoke${caps.clusterReason ? ': ' + esc(caps.clusterReason) : ''}. Install kubectl and a kubeconfig (or run in-cluster) to enable.

`) + + (owner ? '' : '

Owner role is required to ingest.

') + + '
' + ctxField + + `` + + `` + + `` + + `` + + `
` + + `
` + + '
' + result + '
'; + } + + function renderWorkflowIngestSpecPanel(caps, owner) { + const st = _wfIngestSrc.spec; + const disabled = !owner || st.busy ? 'disabled' : ''; + const modes = WF_INGEST_SPEC_MODES.map(([id, label]) => ``).join(''); + let input; + if (st.mode === 'url') input = ``; + else if (st.mode === 'repo') input = + `` + + ``; + else input = ``; + const llmField = caps.spec.llm + ? `` + : '

No model gateway is configured, so the spec is read deterministically. Configure one under Settings → Model Gateways to offer AI assistance.

'; + let result = ''; + if (st.error) result = `
${esc(st.error)}
`; + else if (st.result) result = renderWorkflowIngestSourceResult(st.result, renderWorkflowIngestSpecNotes(st.result)); + return `
` + + '

From a written spec

A process document becomes a draft model, every element inferred
' + + '

Reads numbered steps, "if / when / after" sentences and role nouns into states, events and gates. With AI assistance the configured gateway refines that skeleton; its answer is accepted only when it parses strictly and compiles, otherwise the deterministic draft is kept. Either way everything is marked inferred, cites the spec section it came from and is saved as ingested · unreviewed.

' + + (owner ? '' : '

Owner role is required to ingest.

') + + '
' + + `` + + `` + + input + llmField + + `
` + + `
` + + '
' + result + '
'; + } + + function renderWorkflowIngestSources(state, owner) { + if (!_wfIngestSrc.capsRequested && typeof wfIngestSrcLoadCaps === 'function') wfIngestSrcLoadCaps(); + const caps = wfIngestSrcCaps(); + return '
' + + '

Other sources

' + + (_wfIngestSrc.capsError ? `

Could not read ingest capabilities: ${esc(_wfIngestSrc.capsError)}

` : '') + + renderWorkflowIngestClusterPanel(caps, owner) + + renderWorkflowIngestSpecPanel(caps, owner) + + '
'; + } + + function wfIngestSrcToggle(which, open) { + if (_wfIngestSrc[which]) _wfIngestSrc[which].open = !!open; + } + + function wfIngestSrcSet(which, field, value) { + const st = _wfIngestSrc[which]; + if (!st || !(field in st) || field === 'busy' || field === 'result' || field === 'error' || field === 'open') return; + if (typeof st[field] === 'boolean') st[field] = !!value; + else st[field] = String(value == null ? '' : value); + if (field === 'mode') { st.result = null; st.error = ''; wfLibRerender(); } + } + + async function wfIngestSrcLoadCaps() { + _wfIngestSrc.capsRequested = true; + if (typeof fetch !== 'function') return; + try { + const res = await fetch('/api/workflows/ingest/capabilities', { headers: _inceptionAuthHeaders() }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + _wfIngestSrc.caps = await res.json(); + _wfIngestSrc.capsError = ''; + } catch (err) { + _wfIngestSrc.caps = WF_INGEST_SRC_CAPS_UNKNOWN; + _wfIngestSrc.capsError = err.message; + } + wfLibRerender(); + } + + // Body for POST /api/workflows/ingest/{cluster,spec}, or a validation + // message when the panel's fields do not describe a request. + function wfIngestSrcRequest(which, st, save, caps) { + if (which === 'cluster') { + if (!st.namespace.trim() && !st.allNamespaces) return { error: 'Enter a namespace or tick “All namespaces”.' }; + const body = { save: !!save }; + if (st.context.trim()) body.context = st.context.trim(); + if (st.allNamespaces) body.allNamespaces = true; else body.namespace = st.namespace.trim(); + if (st.selector.trim()) body.selector = st.selector.trim(); + if (st.name.trim()) body.name = st.name.trim(); + return { body }; + } + const body = { save: !!save, useLLM: !!(caps && caps.spec && caps.spec.llm && st.useLLM) }; + if (st.mode === 'url') { + if (!/^https:\/\/\S+$/.test(st.url.trim())) return { error: 'Enter an https:// URL.' }; + body.url = st.url.trim(); + } else if (st.mode === 'repo') { + if (!/^[^/\s]+\/[^/\s]+$/.test(st.repo.trim())) return { error: 'Enter a repository as owner/repo.' }; + if (!st.path.trim()) return { error: 'Enter the path of the spec file.' }; + body.repo = st.repo.trim(); body.path = st.path.trim(); + } else { + if (!st.text.trim()) return { error: 'Paste the process spec first.' }; + body.text = st.text; + } + if (st.name.trim()) body.name = st.name.trim(); + return { body }; + } + + async function wfIngestSrcRun(which, save) { + if (!dashboardRoleAtLeast(window._hiveRole || 'read', 'owner')) { showToast('Owner role is required to ingest.', 'error'); return; } + const st = _wfIngestSrc[which]; + if (!st) return; + const req = wfIngestSrcRequest(which, st, save, wfIngestSrcCaps()); + if (req.error) { st.error = req.error; st.result = null; wfLibRerender(); return; } + st.busy = true; st.error = ''; st.result = null; st.open = true; wfLibRerender(); + try { + const res = await fetch('/api/workflows/ingest/' + which, { method: 'POST', headers: _inceptionAuthHeaders(), body: JSON.stringify(req.body) }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + st.result = await res.json(); + if (save && st.result.entry) { + showToast('Saved ' + st.result.entry.name + '@' + st.result.entry.version); + _workflowState.loaded = false; + await loadWorkflowLibrary(); + } + } catch (err) { + st.error = 'Ingest failed: ' + err.message; + } finally { + st.busy = false; + wfLibRerender(); + } + } diff --git a/src/pkg/dashboard/static-src/js/config-workflows-library.js b/src/pkg/dashboard/static-src/js/config-workflows-library.js index cc8ba8476e..7d7cddb1a7 100644 --- a/src/pkg/dashboard/static-src/js/config-workflows-library.js +++ b/src/pkg/dashboard/static-src/js/config-workflows-library.js @@ -516,7 +516,8 @@ `` + `
` + `
` + - '' + result + ''; + '' + result + '' + + (typeof renderWorkflowIngestSources === 'function' ? renderWorkflowIngestSources(state, owner) : ''); } function wfIngestDefaultName(repo) { diff --git a/src/pkg/dashboard/static-src/manifest.txt b/src/pkg/dashboard/static-src/manifest.txt index 3475de6ec9..691fefa7a4 100644 --- a/src/pkg/dashboard/static-src/manifest.txt +++ b/src/pkg/dashboard/static-src/manifest.txt @@ -23,6 +23,7 @@ css/alerts-toasts.css css/feedback-faq.css css/config-dialog.css css/workflows.css +css/workflows-ingest.css css/chat.css css/nous.css css/knowledge.css @@ -137,6 +138,7 @@ js/config-agent.js js/config-governor.js js/config-workflows.js js/config-workflows-library.js +js/config-workflows-ingest-sources.js js/config-watchdog-security.js js/config-features.js js/integration-execution-settings.js diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index d2eae70bbb..68a4810cc5 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -5123,6 +5123,27 @@ .wf-sim-unknown { color: var(--status-warn); font-weight: 700; } .wf-sim-fill { margin-top: var(--sp-3); color: var(--text); } .wf-dg-state.simulated .wf-dg-chip { stroke: var(--status-ok); stroke-width: 3; } + /* Settings → Workflows → Ingest, other sources (#17620 phase 6): cluster and written-spec panels */ + .wf-src-panel { border: var(--line-width) solid var(--border); border-radius: var(--r); padding: var(--sp-0) var(--sp-4); margin-top: var(--sp-4); } + .wf-src-panel > summary { cursor: pointer; display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--sp-3); padding: var(--sp-3) var(--sp-0); list-style: none; } + .wf-src-panel > summary::-webkit-details-marker { display: none; } + .wf-src-panel > summary::before { content: '▸'; color: var(--muted); font-size: var(--fs-sm); } + .wf-src-panel[open] > summary::before { content: '▾'; } + .wf-src-panel > summary h3 { margin: var(--sp-0); font-size: var(--fs-base); } + .wf-src-panel > p, .wf-src-panel > .wf-ingest-form, .wf-src-panel > .wf-ingest-result, .wf-src-panel > .wf-ingest-error { margin-bottom: var(--sp-4); } + .wf-src-wide { grid-column: 1 / -1; } + .wf-src-check { display: flex; align-items: center; gap: var(--sp-2); font-size: var(--fs-sm); } + .wf-src-check input[type="checkbox"] { width: auto; margin: var(--sp-0); } + .wf-src-unavailable { color: var(--status-warn); } + .wf-src-note { margin: var(--sp-3) var(--sp-0) var(--sp-0); } + .wf-src-llm { display: inline-block; border-radius: var(--r-pill); padding: var(--sp-1) var(--sp-3); font-size: var(--fs-xs); font-weight: 700; border: var(--line-width) solid var(--border); color: var(--muted); } + .wf-src-llm-on { color: var(--brand); } + .wf-src-llm-fallback { color: var(--status-warn); } + .wf-src-runs, .wf-src-sections { margin: var(--sp-0); padding-left: var(--sp-6); font-size: var(--fs-sm); } + .wf-src-run-status { font-weight: 700; color: var(--muted); } + .wf-src-run-succeeded { color: var(--status-ok); } + .wf-src-run-failed { color: var(--status-error); } + .wf-src-run-running { color: var(--status-warn); } /* Hive Chat Panel */ .hive-chat-fab { position: fixed; bottom: 24px; right: 24px; z-index: 9000; @@ -63167,7 +63188,8 @@

Agent Watchdog

`` + `
` + `
` + - '' + result + ''; + '' + result + '' + + (typeof renderWorkflowIngestSources === 'function' ? renderWorkflowIngestSources(state, owner) : ''); } function wfIngestDefaultName(repo) { @@ -63322,6 +63344,213 @@

Agent Watchdog

wfLibRerender(); } } + // Settings → Workflows → Ingest: the two extra sources of RFC phase 6 + // (hivecommons/hive#17620) — Tekton from a live cluster context and a + // written process spec, optionally refined by the configured model + // gateway. Rendered by renderWorkflowIngestSources(state, owner), which + // renderWorkflowIngest appends to its HTML. Renderers are pure over + // _wfIngestSrc; the wfIngestSrc* handlers mutate it and rerender. + const WF_INGEST_SPEC_MODES = [['text', 'Paste the spec'], ['url', 'Fetch from a URL'], ['repo', 'Read a repository file']]; + const WF_INGEST_SRC_CAPS_UNKNOWN = { cluster: false, clusterReason: '', contexts: [], spec: { llm: false, model: '' } }; + const _wfIngestSrc = { + caps: null, capsRequested: false, capsError: '', + cluster: { open: false, context: '', namespace: '', allNamespaces: false, selector: '', name: '', busy: false, result: null, error: '' }, + spec: { open: false, mode: 'text', text: '', url: '', repo: '', path: '', useLLM: true, name: '', busy: false, result: null, error: '' }, + }; + + function wfIngestSrcCaps() { + const c = _wfIngestSrc.caps || WF_INGEST_SRC_CAPS_UNKNOWN; + return { + cluster: !!c.cluster, + clusterReason: String(c.clusterReason || ''), + contexts: Array.isArray(c.contexts) ? c.contexts.filter(Boolean) : [], + spec: { llm: !!(c.spec && c.spec.llm), model: String((c.spec && c.spec.model) || '') }, + }; + } + + // Shared result block: the same summary / diagnostics / diagram the + // repository ingest shows, plus the per-source extras passed in. + function renderWorkflowIngestSourceResult(r, extras) { + const e = r.entry || null; + const cols = (r.profile && r.profile.columns) || (r.model && r.model.spec && r.model.spec.columns) || []; + const pipelines = Array.isArray(r.pipelines) ? r.pipelines : []; + return '
' + + `
${r.compiled ? 'compiles' : 'does not compile'}` + + (r.source ? ` · ${esc(r.source)}` : '') + + (typeof r.files === 'number' ? ` · ${esc(String(r.files))} file${r.files === 1 ? '' : 's'}` : '') + + (Array.isArray(r.pipelines) ? ` · ${pipelines.length} pipeline${pipelines.length === 1 ? '' : 's'}` : '') + + ` · ${cols.length} column${cols.length === 1 ? '' : 's'}` + + (e ? ` · saved as ${esc(e.name + '@' + e.version)}` : '') + + '
' + + (extras || '') + + (pipelines.length ? '
    ' + pipelines.map(p => `
  • ${esc(p.system || p.kind || '')} ${esc(p.name || p.id || '')}${p.path ? ` ${esc(p.path)}` : ''}
  • `).join('') + '
' : '') + + '

Diagnostics

' + renderWorkflowDiagnostics(r.diagnostics) + + '

Proposed columns

' + workflowDiagramSvg(r.model, r.profile) + '
' + + '
'; + } + + function renderWorkflowIngestClusterRuns(runs) { + const list = Array.isArray(runs) ? runs : []; + if (!list.length) return '

No PipelineRuns observed: the model carries no run status.

'; + return '

Observed runs

Last run per pipeline, recorded as an observed annotation — not an inferred lifecycle element.

    ' + + list.map(r => `
  • ${esc(r.namespace || '')}/${esc(r.pipeline || '')} ${esc(r.status || 'Unknown')}${r.run ? ` ${esc(r.run)}` : ''}${r.completed ? ` ${esc(formatDashboardDateTime(r.completed))}` : ''}
  • `).join('') + + '
'; + } + + function renderWorkflowIngestSpecNotes(r) { + const llm = r.llm || {}; + const sections = Array.isArray(r.sections) ? r.sections : []; + let note; + if (!llm.used) note = 'Deterministic only — step lists, conditional sentences and role nouns read without a model.'; + else if (llm.accepted) note = `LLM-assisted — refined by ${esc(llm.model || 'the configured gateway')} in ${esc(String(llm.attempts || 1))} attempt${llm.attempts === 1 ? '' : 's'}; every element is still inferred and cites the spec.`; + else note = `LLM fell back — ${esc(llm.model || 'the gateway')} did not return a compiling model after ${esc(String(llm.attempts || 0))} attempt${llm.attempts === 1 ? '' : 's'}${llm.error ? ` (${esc(llm.error)})` : ''}; showing the deterministic skeleton.`; + return `

${note}

` + + (sections.length ? '

Spec sections

    ' + sections.map(s => `
  • L${esc(String(s.startLine || 1))}–L${esc(String(s.endLine || s.startLine || 1))} ${esc(s.heading || '')}
  • `).join('') + '
' : ''); + } + + function renderWorkflowIngestClusterPanel(caps, owner) { + const st = _wfIngestSrc.cluster; + const disabled = !owner || st.busy || !caps.cluster ? 'disabled' : ''; + const contexts = caps.contexts; + const ctxField = contexts.length + ? `` + : ``; + let result = ''; + if (st.error) result = `
${esc(st.error)}
`; + else if (st.result) result = renderWorkflowIngestSourceResult(st.result, renderWorkflowIngestClusterRuns(st.result.runs) + (Array.isArray(st.result.namespaces) && st.result.namespaces.length > 1 ? `

Namespaces: ${st.result.namespaces.map(n => `${esc(n)}`).join(', ')}

` : '')); + return `
` + + '

From a cluster

Tekton Pipelines, Tasks, PipelineRuns and Triggers read through kubectl
' + + '

Lists the Tekton resources of one namespace (or every namespace, capped) in a kube context, writes them as cluster/<namespace>/<kind>/<name>.yaml and runs the same Tekton ingester as a repository, so citations point at those paths. The last PipelineRun status per pipeline is recorded as observed, never inferred.

' + + (caps.cluster ? '' : `

No kube access on this spoke${caps.clusterReason ? ': ' + esc(caps.clusterReason) : ''}. Install kubectl and a kubeconfig (or run in-cluster) to enable.

`) + + (owner ? '' : '

Owner role is required to ingest.

') + + '
' + ctxField + + `` + + `` + + `` + + `` + + `
` + + `
` + + '
' + result + '
'; + } + + function renderWorkflowIngestSpecPanel(caps, owner) { + const st = _wfIngestSrc.spec; + const disabled = !owner || st.busy ? 'disabled' : ''; + const modes = WF_INGEST_SPEC_MODES.map(([id, label]) => ``).join(''); + let input; + if (st.mode === 'url') input = ``; + else if (st.mode === 'repo') input = + `` + + ``; + else input = ``; + const llmField = caps.spec.llm + ? `` + : '

No model gateway is configured, so the spec is read deterministically. Configure one under Settings → Model Gateways to offer AI assistance.

'; + let result = ''; + if (st.error) result = `
${esc(st.error)}
`; + else if (st.result) result = renderWorkflowIngestSourceResult(st.result, renderWorkflowIngestSpecNotes(st.result)); + return `
` + + '

From a written spec

A process document becomes a draft model, every element inferred
' + + '

Reads numbered steps, "if / when / after" sentences and role nouns into states, events and gates. With AI assistance the configured gateway refines that skeleton; its answer is accepted only when it parses strictly and compiles, otherwise the deterministic draft is kept. Either way everything is marked inferred, cites the spec section it came from and is saved as ingested · unreviewed.

' + + (owner ? '' : '

Owner role is required to ingest.

') + + '
' + + `` + + `` + + input + llmField + + `
` + + `
` + + '
' + result + '
'; + } + + function renderWorkflowIngestSources(state, owner) { + if (!_wfIngestSrc.capsRequested && typeof wfIngestSrcLoadCaps === 'function') wfIngestSrcLoadCaps(); + const caps = wfIngestSrcCaps(); + return '
' + + '

Other sources

' + + (_wfIngestSrc.capsError ? `

Could not read ingest capabilities: ${esc(_wfIngestSrc.capsError)}

` : '') + + renderWorkflowIngestClusterPanel(caps, owner) + + renderWorkflowIngestSpecPanel(caps, owner) + + '
'; + } + + function wfIngestSrcToggle(which, open) { + if (_wfIngestSrc[which]) _wfIngestSrc[which].open = !!open; + } + + function wfIngestSrcSet(which, field, value) { + const st = _wfIngestSrc[which]; + if (!st || !(field in st) || field === 'busy' || field === 'result' || field === 'error' || field === 'open') return; + if (typeof st[field] === 'boolean') st[field] = !!value; + else st[field] = String(value == null ? '' : value); + if (field === 'mode') { st.result = null; st.error = ''; wfLibRerender(); } + } + + async function wfIngestSrcLoadCaps() { + _wfIngestSrc.capsRequested = true; + if (typeof fetch !== 'function') return; + try { + const res = await fetch('/api/workflows/ingest/capabilities', { headers: _inceptionAuthHeaders() }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + _wfIngestSrc.caps = await res.json(); + _wfIngestSrc.capsError = ''; + } catch (err) { + _wfIngestSrc.caps = WF_INGEST_SRC_CAPS_UNKNOWN; + _wfIngestSrc.capsError = err.message; + } + wfLibRerender(); + } + + // Body for POST /api/workflows/ingest/{cluster,spec}, or a validation + // message when the panel's fields do not describe a request. + function wfIngestSrcRequest(which, st, save, caps) { + if (which === 'cluster') { + if (!st.namespace.trim() && !st.allNamespaces) return { error: 'Enter a namespace or tick “All namespaces”.' }; + const body = { save: !!save }; + if (st.context.trim()) body.context = st.context.trim(); + if (st.allNamespaces) body.allNamespaces = true; else body.namespace = st.namespace.trim(); + if (st.selector.trim()) body.selector = st.selector.trim(); + if (st.name.trim()) body.name = st.name.trim(); + return { body }; + } + const body = { save: !!save, useLLM: !!(caps && caps.spec && caps.spec.llm && st.useLLM) }; + if (st.mode === 'url') { + if (!/^https:\/\/\S+$/.test(st.url.trim())) return { error: 'Enter an https:// URL.' }; + body.url = st.url.trim(); + } else if (st.mode === 'repo') { + if (!/^[^/\s]+\/[^/\s]+$/.test(st.repo.trim())) return { error: 'Enter a repository as owner/repo.' }; + if (!st.path.trim()) return { error: 'Enter the path of the spec file.' }; + body.repo = st.repo.trim(); body.path = st.path.trim(); + } else { + if (!st.text.trim()) return { error: 'Paste the process spec first.' }; + body.text = st.text; + } + if (st.name.trim()) body.name = st.name.trim(); + return { body }; + } + + async function wfIngestSrcRun(which, save) { + if (!dashboardRoleAtLeast(window._hiveRole || 'read', 'owner')) { showToast('Owner role is required to ingest.', 'error'); return; } + const st = _wfIngestSrc[which]; + if (!st) return; + const req = wfIngestSrcRequest(which, st, save, wfIngestSrcCaps()); + if (req.error) { st.error = req.error; st.result = null; wfLibRerender(); return; } + st.busy = true; st.error = ''; st.result = null; st.open = true; wfLibRerender(); + try { + const res = await fetch('/api/workflows/ingest/' + which, { method: 'POST', headers: _inceptionAuthHeaders(), body: JSON.stringify(req.body) }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + st.result = await res.json(); + if (save && st.result.entry) { + showToast('Saved ' + st.result.entry.name + '@' + st.result.entry.version); + _workflowState.loaded = false; + await loadWorkflowLibrary(); + } + } catch (err) { + st.error = 'Ingest failed: ' + err.message; + } finally { + st.busy = false; + wfLibRerender(); + } + } // ── Watchdog activity strip (#7254) ────────────────────────────────────── // "Review the watchdog-*-observed entries in the Audit Log, then switch to // Heal" is only a data-driven decision if the data is on the page. The diff --git a/src/pkg/dashboard/workflows_ingest_sources_ui_test.go b/src/pkg/dashboard/workflows_ingest_sources_ui_test.go new file mode 100644 index 0000000000..075bfa62c6 --- /dev/null +++ b/src/pkg/dashboard/workflows_ingest_sources_ui_test.go @@ -0,0 +1,185 @@ +package dashboard + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// TestWorkflowsIngestSourcesWiring pins the RFC phase-6 surface +// (hivecommons/hive#17620): the Ingest view appends the cluster and +// written-spec panels through one hook, the panels post to the two new +// routes, read capabilities from the one that gates them, and the +// stylesheet rides on the shared tokens. +func TestWorkflowsIngestSourcesWiring(t *testing.T) { + html := indexHTML(t) + for _, want := range []string{ + `(typeof renderWorkflowIngestSources === 'function' ? renderWorkflowIngestSources(state, owner) : '')`, + `function renderWorkflowIngestSources(state, owner)`, + `function renderWorkflowIngestClusterPanel(caps, owner)`, + `function renderWorkflowIngestSpecPanel(caps, owner)`, + `fetch('/api/workflows/ingest/capabilities', {`, + `fetch('/api/workflows/ingest/' + which, {`, + `.wf-src-panel {`, + } { + if !strings.Contains(html, want) { + t.Errorf("index.html missing ingest-sources wiring %q", want) + } + } + // The hook is the only touch on the sibling-owned Ingest renderer. + lib, err := os.ReadFile(filepath.Join("static-src", "js", "config-workflows-library.js")) + if err != nil { + t.Fatal(err) + } + if n := strings.Count(string(lib), "renderWorkflowIngestSources"); n != 2 { + t.Errorf("config-workflows-library.js references renderWorkflowIngestSources %d times, want the one guarded hook", n) + } +} + +// The renderers under node: capability gating (cluster disabled with its +// reason, the AI checkbox only when a gateway is configured), the three spec +// input modes, request-body validation, the result block with the observed +// runs / LLM note / section list, and the state setters. +func TestWorkflowsIngestSourcesNode(t *testing.T) { + node, err := exec.LookPath("node") + if err != nil { + t.Skip("node not installed") + } + html := indexHTML(t) + script := `const assert = require('assert'); +function esc(s) { return String(s).replace(/&/g, '&').replace(/' + (d || []).length + ''; } +function workflowDiagramSvg() { return ''; } +let rerenders = 0; function wfLibRerender() { rerenders++; } +const WF_INGEST_SPEC_MODES = [['text', 'Paste the spec'], ['url', 'Fetch from a URL'], ['repo', 'Read a repository file']]; +const WF_INGEST_SRC_CAPS_UNKNOWN = { cluster: false, clusterReason: '', contexts: [], spec: { llm: false, model: '' } }; +const _wfIngestSrc = { + caps: null, capsRequested: true, capsError: '', + cluster: { open: false, context: '', namespace: '', allNamespaces: false, selector: '', name: '', busy: false, result: null, error: '' }, + spec: { open: false, mode: 'text', text: '', url: '', repo: '', path: '', useLLM: true, name: '', busy: false, result: null, error: '' }, +}; +` + jsFunc(t, html, "wfIngestSrcCaps") + jsFunc(t, html, "renderWorkflowIngestSourceResult") + jsFunc(t, html, "renderWorkflowIngestClusterRuns") + + jsFunc(t, html, "renderWorkflowIngestSpecNotes") + jsFunc(t, html, "renderWorkflowIngestClusterPanel") + jsFunc(t, html, "renderWorkflowIngestSpecPanel") + + jsFunc(t, html, "renderWorkflowIngestSources") + jsFunc(t, html, "wfIngestSrcToggle") + jsFunc(t, html, "wfIngestSrcSet") + jsFunc(t, html, "wfIngestSrcRequest") + ` +// Unknown capabilities: cluster panel disabled with the install hint, no AI checkbox. +let out = renderWorkflowIngestSources(null, true); +assert.ok(out.includes('data-section="workflows-ingest-sources"')); +assert.ok(out.includes('data-section="workflows-ingest-cluster"') && out.includes('data-section="workflows-ingest-spec"')); +assert.ok(out.includes('No kube access on this spoke')); +assert.ok(out.includes('id="wf-src-cluster-namespace"') && /id="wf-src-cluster-namespace"[^>]*disabled/.test(out)); +assert.ok(!out.includes('id="wf-src-spec-llm"')); +assert.ok(out.includes('No model gateway is configured')); +assert.ok(out.includes('
' + + `' + + (extras || '') + + (pipelines.length ? '
    ' + pipelines.map(p => `
  • ${esc(p.system || p.kind || '')} ${esc(p.name || p.id || '')}${p.path ? ` ${esc(p.path)}` : ''}
  • `).join('') + '
' : '') + + '

Diagnostics

' + renderWorkflowDiagnostics(r.diagnostics) + + '

Proposed columns

' + workflowDiagramSvg(r.model, r.profile) + '
' + + '