fix(state): serialise source writes and keep binding workspaces audited

This commit is contained in:
sam committed 2026-10-25 21:33:08 +11:00
1 parent 7f5a73d97d
commit 5602534ba9
4 files changed
+91 -22

No files matched your search

+34 -6
View File
@@ -97,17 +97,30 @@ func mappingRowAuditDoc(r postgresdb.GetMappingRow) map[string]any {
}
// bindingAuditDoc is the record an audit row shows for a binding.
func bindingAuditDoc(id, name, source, mappingID, filter, workflow, owner, repo, repoParam, inputs, status string, version int64) map[string]any {
func bindingAuditDoc(id, name, source, mappingID, filter, workflow, owner, repo, repoParam, inputs, status, workspaceID string, version int64) map[string]any {
return map[string]any{
"id": id, "name": name, "source": source, "mapping": mappingID,
"filter": filter, "workflow": workflow, "owner": owner, "repo": repo,
"repo_param": repoParam, "inputs": decodedJSON(inputs), "status": status,
"version": version,
"workspace_id": workspaceID, "version": version,
}
}
func bindingRowAuditDoc(r postgresdb.GetBindingRow) map[string]any {
return bindingAuditDoc(r.ID, r.Name, r.Source, r.Mapping, r.Filter, r.Workflow, r.Owner, r.Repo, r.RepoParam, r.Inputs, r.Status, r.Version)
return bindingAuditDoc(r.ID, r.Name, r.Source, r.Mapping, r.Filter, r.Workflow, r.Owner, r.Repo, r.RepoParam, r.Inputs, r.Status, r.WorkspaceID, r.Version)
}
// bindingWorkspace is the workspace the binding SQL derives: its source's, or
// "default" when the source no longer exists.
func bindingWorkspace(ctx context.Context, q *postgresdb.Queries, source, orgID string) (string, error) {
workspace, err := q.SourceWorkspace(ctx, postgresdb.SourceWorkspaceParams{Path: source, OrgID: orgID})
if errors.Is(err, pgx.ErrNoRows) {
return "default", nil
}
if err != nil {
return "", err
}
return workspace, nil
}
// bindingEditableEqual reports whether b would change any field UpdateBinding
@@ -465,6 +478,10 @@ func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, err
orgID := string(org.OrgFromContext(ctx))
if err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
workspace, err := bindingWorkspace(ctx, q, b.Matcher.Source, orgID)
if err != nil {
return err
}
if err := q.InsertBinding(ctx, postgresdb.InsertBindingParams{
OrgID: orgID,
ID: id,
@@ -483,7 +500,7 @@ func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, err
}
return insertAuditValues(ctx, q, "bindings", id, "binding", nil,
bindingAuditDoc(id, b.Name, b.Matcher.Source, b.MappingID, b.Filter, b.Workflow,
b.Owner, b.Repo, b.RepoParam, inputs, string(binding.StatusPendingApproval), 1), 1, orgID)
b.Owner, b.Repo, b.RepoParam, inputs, string(binding.StatusPendingApproval), workspace, 1), 1, orgID)
}); err != nil {
return "", err
}
@@ -560,7 +577,18 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error {
if err != nil {
return err
}
if bindingEditableEqual(old, b, inputs) && old.Status == string(binding.StatusPendingApproval) {
// The target workspace comes from the source, so hold the source's lock
// until the UPDATE has recomputed it too. Binding then source is the
// only order taken, so it cannot cycle.
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: b.Matcher.Source}); err != nil {
return err
}
targetWorkspace, err := bindingWorkspace(ctx, q, b.Matcher.Source, old.OrgID)
if err != nil {
return err
}
if bindingEditableEqual(old, b, inputs) && old.Status == string(binding.StatusPendingApproval) &&
old.WorkspaceID == targetWorkspace {
return nil
}
n, err := q.UpdateBinding(ctx, postgresdb.UpdateBindingParams{
@@ -587,7 +615,7 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error {
return insertAuditValues(ctx, q, "bindings", b.ID, "binding",
bindingRowAuditDoc(old),
bindingAuditDoc(b.ID, b.Name, b.Matcher.Source, b.MappingID, b.Filter, b.Workflow,
b.Owner, b.Repo, b.RepoParam, inputs, string(binding.StatusPendingApproval), next),
b.Owner, b.Repo, b.RepoParam, inputs, string(binding.StatusPendingApproval), targetWorkspace, next),
next, old.OrgID)
})
if err != nil {
@@ -1205,6 +1205,24 @@ func (q *Queries) SourceExists(ctx context.Context, path string) (bool, error) {
return exists, err
}
const sourceWorkspace = `-- name: SourceWorkspace :one
SELECT workspace_id FROM sources WHERE path = $1 AND org_id = $2
`
type SourceWorkspaceParams struct {
Path string
OrgID string
}
// The workspace the binding/mapping SQL derives from a source path, so an
// audit snapshots the same value the write stores.
func (q *Queries) SourceWorkspace(ctx context.Context, arg SourceWorkspaceParams) (string, error) {
row := q.db.QueryRow(ctx, sourceWorkspace, arg.Path, arg.OrgID)
var workspace_id string
err := row.Scan(&workspace_id)
return workspace_id, err
}
const taskToolCalls = `-- name: TaskToolCalls :many
SELECT id, task_id, attempt, tool, result, error, called_at
FROM tool_calls WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) ORDER BY called_at ASC
@@ -208,6 +208,11 @@ FROM workspaces w WHERE w.org_id = sqlc.arg(org_id) AND w.id = sqlc.arg(workspac
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SourceWorkspace :one
-- The workspace the binding/mapping SQL derives from a source path, so an
-- audit snapshots the same value the write stores.
SELECT workspace_id FROM sources WHERE path = sqlc.arg(path) AND org_id = sqlc.arg(org_id);
-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC, path;
+34 -16
View File
@@ -82,6 +82,9 @@ func (s *EDA) SetSourceSigning(ctx context.Context, path string, from, to source
orgID := string(org.OrgFromContext(ctx))
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
return err
}
if from == to {
// No transition to write; still report a missing source or a
// caller whose idea of the current state is stale.
@@ -124,6 +127,9 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
orgID := string(org.OrgFromContext(ctx))
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
return err
}
n, err := q.SetSourceSecret(ctx, postgresdb.SetSourceSecretParams{Path: path, Secret: sealed, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source secret: %w", err)
@@ -139,26 +145,38 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
// SetSourceName relabels a source.
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source name: %w", err)
}
if n == 0 {
return storecontract.ErrSourceNotFound
}
return nil
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
return err
}
n, err := q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source name: %w", err)
}
if n == 0 {
return storecontract.ErrSourceNotFound
}
return nil
})
}
// SetSourceDeliveryHeader sets the header that names a source's deliveries.
func (s *EDA) SetSourceDeliveryHeader(ctx context.Context, path, header string) error {
n, err := s.q.SetSourceDeliveryHeader(ctx, postgresdb.SetSourceDeliveryHeaderParams{Path: path, DeliveryHeader: header, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source delivery header: %w", err)
}
if n == 0 {
return storecontract.ErrSourceNotFound
}
return nil
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
return err
}
n, err := q.SetSourceDeliveryHeader(ctx, postgresdb.SetSourceDeliveryHeaderParams{Path: path, DeliveryHeader: header, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source delivery header: %w", err)
}
if n == 0 {
return storecontract.ErrSourceNotFound
}
return nil
})
}
// DeleteSource deletes a source no armed binding fires on.