Fix variable links not persisted when variable is used multiple times in config

- Dedupe extracted variable names (duplicate INSERTs aborted the
  config_has_variable transaction on PK conflict)
- Check per-statement rqlite errors in ExecTransaction: a failed
  statement silently returned success before
- Make link upserts idempotent (insert or ignore) and add link
  backfill on variable create/upsert from existing configs
- Validate config variables before insert (no orphaned configs)
- Resync active config links on module reconnect and on no-op
  config update (self-healing)
This commit is contained in:
Hermes Agent committed 2026-10-28 10:34:45 +03:00
1 parent 5c8a554d4f
commit 7f4e3ef028
7 files changed
+227 -21

No files matched your search

+48 -3
View File
@@ -20,10 +20,12 @@ type Repo interface {
Upsert(ctx context.Context, variables []entity.Variable) error
Delete(ctx context.Context, name string) (bool, error)
UpsertLinks(ctx context.Context, configId string, links []entity.ConfigHasVariable) error
InsertLinks(ctx context.Context, links []entity.ConfigHasVariable) error
}
type ConfigRepo interface {
GetMetaByVariables(ctx context.Context, variables []string) (map[string][]entity.ConfigMetaWithVariable, error)
FindIdsByVariableReference(ctx context.Context, variableName string) ([]string, error)
}
type EventRepo interface {
@@ -106,6 +108,16 @@ func (s Service) Create(ctx context.Context, req domain.CreateVariableRequest) e
if err != nil {
return errors.WithMessage(err, "insert variable")
}
err = s.syncVariableLinks(ctx, []string{variable.Name})
if err != nil {
return errors.WithMessage(err, "sync variable links")
}
err = s.syncConfigsByVariables(ctx, []string{variable.Name})
if err != nil {
return errors.WithMessage(err, "sync configs by variables")
}
return nil
}
@@ -151,6 +163,11 @@ func (s Service) Upsert(ctx context.Context, req []domain.UpsertVariableRequest)
return errors.WithMessage(err, "upsert variables")
}
err = s.syncVariableLinks(ctx, variableNames)
if err != nil {
return errors.WithMessage(err, "sync variable links")
}
err = s.syncConfigsByVariables(ctx, variableNames)
if err != nil {
return errors.WithMessage(err, "sync configs by variables")
@@ -215,9 +232,15 @@ func (s Service) RenderConfig(ctx context.Context, input []byte) ([]byte, error)
func (s Service) ExtractVariables(ctx context.Context, configId string, input []byte) (*domain.VariableExtractionResult, error) {
matches := s.variableRegexp.FindAllStringSubmatch(string(input), -1)
varNames := make([]string, len(matches))
for i, match := range matches {
varNames[i] = match[1]
seen := make(map[string]bool, len(matches))
varNames := make([]string, 0, len(matches))
for _, match := range matches {
name := match[1]
if seen[name] {
continue
}
seen[name] = true
varNames = append(varNames, name)
}
if len(varNames) == 0 {
return &domain.VariableExtractionResult{
@@ -287,6 +310,28 @@ func (s Service) toDto(variable entity.Variable, metaByVariables map[string][]en
}
}
func (s Service) syncVariableLinks(ctx context.Context, variableNames []string) error {
for _, name := range variableNames {
configIds, err := s.configRepo.FindIdsByVariableReference(ctx, name)
if err != nil {
return errors.WithMessagef(err, "find configs referencing variable: %s", name)
}
links := make([]entity.ConfigHasVariable, 0, len(configIds))
for _, configId := range configIds {
links = append(links, entity.ConfigHasVariable{
ConfigId: configId,
VariableName: name,
})
}
err = s.repo.InsertLinks(ctx, links)
if err != nil {
return errors.WithMessagef(err, "insert links for variable: %s", name)
}
}
return nil
}
func (s Service) syncConfigsByVariables(ctx context.Context, variables []string) error {
metaByVariables, err := s.configRepo.GetMetaByVariables(ctx, variables)
if err != nil {