feat(judgement,approvals,ui): action classes are records with a level, history and reset; the precedent guard reads the class level (#2003)
This commit is contained in:
15 files changed
+900
-16
No files matched your search
@@ -15,6 +15,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
@@ -85,12 +86,12 @@ func Run(ctx context.Context, configPath string) error {
|
||||
engine := runs.New(db, log.With("module", "runs"), runs.Options{Owner: owner()})
|
||||
// A workflow takes its context from the run handle, as a handler takes it
|
||||
// from the request.
|
||||
judge := judgement.NewModule(db, log.With("module", "judgement"), orgID, conns, nil,
|
||||
[]judgement.Guard{judgement.Reversibility{}, judgement.Precedent{}})
|
||||
judge, classes := newJudgement(db, log, conns)
|
||||
// An approval request is carried by deliveries, and its answer wakes the run
|
||||
// that is waiting on it.
|
||||
allow := approvals.NewModule(db, log.With("module", "approvals"), orgID, tell, engine)
|
||||
allow.Routes(mux, guard)
|
||||
classes.Routes(mux, guard, allow)
|
||||
engine.Register("investigate", runs.Investigate(settings, conns, judge, allow, tell)) //nolint:contextcheck // see above
|
||||
|
||||
// An answer a channel delivers is an event, and settles its request.
|
||||
@@ -101,17 +102,32 @@ func Run(ctx context.Context, configPath string) error {
|
||||
|
||||
ctx, stop := context.WithCancel(ctx)
|
||||
defer stop()
|
||||
var workers sync.WaitGroup
|
||||
workers.Go(func() { engine.Work(ctx) })
|
||||
workers.Go(func() { hear.Work(ctx) })
|
||||
workers.Go(func() { tell.Work(ctx) })
|
||||
wait := work(ctx, engine.Work, hear.Work, tell.Work)
|
||||
|
||||
err = serve(ctx, log, cfg.Listen, mux)
|
||||
stop()
|
||||
workers.Wait()
|
||||
wait()
|
||||
return err
|
||||
}
|
||||
|
||||
// work runs each loop until ctx ends and returns a function that waits for
|
||||
// them all.
|
||||
func work(ctx context.Context, loops ...func(context.Context)) func() {
|
||||
var workers sync.WaitGroup
|
||||
for _, loop := range loops {
|
||||
workers.Go(func() { loop(ctx) })
|
||||
}
|
||||
return workers.Wait
|
||||
}
|
||||
|
||||
// newJudgement returns the judgement module and the action classes its
|
||||
// precedent guard reads.
|
||||
func newJudgement(db *sqlx.DB, log *slog.Logger, conns judgement.Tools) (*judgement.Module, *judgement.Classes) {
|
||||
classes := judgement.NewClasses(db, log.With("module", "judgement"), orgID)
|
||||
return judgement.NewModule(db, log.With("module", "judgement"), orgID, conns, nil,
|
||||
[]judgement.Guard{judgement.Reversibility{}, judgement.Precedent{Classes: classes}}), classes
|
||||
}
|
||||
|
||||
// orgID is the one organisation until hosting is multi-tenant.
|
||||
const orgID = "default"
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
-- +goose Up
|
||||
-- An action class is what earned autonomy belongs to: one connector's action on
|
||||
-- one kind of target in one environment. A class is made the first time an
|
||||
-- action of it is judged, at the level its kind of action starts at, and its
|
||||
-- decisions stay in judgement.decisions under the same four names. A target
|
||||
-- that moves environment is a different class, which starts as any new one.
|
||||
CREATE TABLE judgement.classes (
|
||||
id uuid PRIMARY KEY,
|
||||
org_id text NOT NULL,
|
||||
connector text NOT NULL,
|
||||
action text NOT NULL,
|
||||
target_kind text NOT NULL,
|
||||
environment text NOT NULL,
|
||||
read_only boolean NOT NULL,
|
||||
level text NOT NULL CHECK (level IN ('act_quietly', 'act_and_tell', 'propose_and_wait', 'escalate', 'never')),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
reset_at timestamptz,
|
||||
UNIQUE (org_id, connector, action, target_kind, environment)
|
||||
);
|
||||
|
||||
-- A reset returns a class to its default and keeps what led there.
|
||||
CREATE TABLE judgement.class_resets (
|
||||
id uuid PRIMARY KEY,
|
||||
org_id text NOT NULL,
|
||||
class_id uuid NOT NULL REFERENCES judgement.classes (id),
|
||||
from_level text NOT NULL,
|
||||
to_level text NOT NULL,
|
||||
by text NOT NULL,
|
||||
at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX class_resets_by_class ON judgement.class_resets (class_id, at);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE judgement.class_resets;
|
||||
DROP TABLE judgement.classes;
|
||||
Reference in new issue
Block a user