Repository navigation
feat(agentcfg): append-only agent config revision store (7/15) #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gusfcarvalho
merged 2 commits into
lisa/agent-config/06-sdk-client
from
lisa/agent-config/07-revision-store
Oct 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| // AgentsConfig tunes agent remote configuration: instance freshness, retention, pruning and | ||
| // the per-agent instance cap (R14, R37). | ||
| type AgentsConfig struct { | ||
| // InstanceStaleAfter: an instance is fresh when seen (heartbeat or report) within this | ||
| // window. CCF_AGENT_INSTANCE_STALE_AFTER, default 10m. | ||
| InstanceStaleAfter time.Duration `json:"instanceStaleAfter"` | ||
| // InstanceRetention: daemon (or unknown) instances not seen for this long are pruned. | ||
| // CCF_AGENT_INSTANCE_RETENTION, default 720h. | ||
| InstanceRetention time.Duration `json:"instanceRetention"` | ||
| // OneShotInstanceRetention: daemon=false instances not seen for this long are pruned. | ||
| // CCF_AGENT_INSTANCE_ONESHOT_RETENTION, default 24h. | ||
| OneShotInstanceRetention time.Duration `json:"oneShotInstanceRetention"` | ||
| // InstancePruneEnabled schedules the prune job (needs the worker service). | ||
| // CCF_AGENT_INSTANCE_PRUNE_ENABLED, default true. | ||
| InstancePruneEnabled bool `json:"instancePruneEnabled"` | ||
| // InstancePruneSchedule is the River (6-field, seconds first) cron of the prune job. | ||
| // CCF_AGENT_INSTANCE_PRUNE_SCHEDULE, default "0 17 * * * *" (hourly). The job is | ||
| // deduplicated per hour, so it runs at most hourly: a more frequent schedule is not | ||
| // honored. | ||
| InstancePruneSchedule string `json:"instancePruneSchedule"` | ||
| // MaxInstancesPerAgent caps the non-prunable instances of one agent. When the cap is | ||
| // reached, a new instance replaces the oldest stale one (not seen within | ||
| // InstanceStaleAfter); only when every counted instance is fresh does a report from a new | ||
| // instance get 409 (and its heartbeats are not recorded). CCF_AGENT_MAX_INSTANCES, | ||
| // default 500. | ||
| MaxInstancesPerAgent int `json:"maxInstancesPerAgent"` | ||
| } | ||
|
|
||
| // DefaultAgentsConfig returns the defaults. | ||
| func DefaultAgentsConfig() *AgentsConfig { | ||
| return &AgentsConfig{ | ||
| InstanceStaleAfter: 10 * time.Minute, | ||
| InstanceRetention: 720 * time.Hour, | ||
| OneShotInstanceRetention: 24 * time.Hour, | ||
| InstancePruneEnabled: true, | ||
| InstancePruneSchedule: "0 17 * * * *", | ||
| MaxInstancesPerAgent: 500, | ||
| } | ||
| } | ||
|
|
||
| // LoadAgentsConfig reads the CCF_AGENT_* settings, falling back to the default for any value | ||
| // that is unset or not positive. | ||
| func LoadAgentsConfig() *AgentsConfig { | ||
| cfg := DefaultAgentsConfig() | ||
| if d := viper.GetDuration("agent_instance_stale_after"); d > 0 { | ||
| cfg.InstanceStaleAfter = d | ||
| } | ||
| if d := viper.GetDuration("agent_instance_retention"); d > 0 { | ||
| cfg.InstanceRetention = d | ||
| } | ||
| if d := viper.GetDuration("agent_instance_oneshot_retention"); d > 0 { | ||
| cfg.OneShotInstanceRetention = d | ||
| } | ||
| if viper.IsSet("agent_instance_prune_enabled") { | ||
| cfg.InstancePruneEnabled = viper.GetBool("agent_instance_prune_enabled") | ||
| } | ||
| if s := viper.GetString("agent_instance_prune_schedule"); s != "" { | ||
| cfg.InstancePruneSchedule = s | ||
| } | ||
| if n := viper.GetInt("agent_max_instances"); n > 0 { | ||
| cfg.MaxInstancesPerAgent = n | ||
| } | ||
| return cfg | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestLoadAgentsConfigDefaults(t *testing.T) { | ||
| viper.Reset() | ||
| t.Cleanup(viper.Reset) | ||
|
|
||
| cfg := LoadAgentsConfig() | ||
|
|
||
| assert.Equal(t, 10*time.Minute, cfg.InstanceStaleAfter) | ||
| assert.Equal(t, 720*time.Hour, cfg.InstanceRetention) | ||
| assert.Equal(t, 24*time.Hour, cfg.OneShotInstanceRetention) | ||
| assert.True(t, cfg.InstancePruneEnabled) | ||
| assert.Equal(t, "0 17 * * * *", cfg.InstancePruneSchedule) | ||
| assert.Equal(t, 500, cfg.MaxInstancesPerAgent) | ||
| assert.Equal(t, DefaultAgentsConfig(), cfg) | ||
| } | ||
|
|
||
| func TestLoadAgentsConfigOverrides(t *testing.T) { | ||
| viper.Reset() | ||
| t.Cleanup(viper.Reset) | ||
| viper.Set("agent_instance_stale_after", "5m") | ||
| viper.Set("agent_instance_retention", "48h") | ||
| viper.Set("agent_instance_oneshot_retention", "2h") | ||
| viper.Set("agent_instance_prune_enabled", false) | ||
| viper.Set("agent_instance_prune_schedule", "0 0 * * * *") | ||
| viper.Set("agent_max_instances", 20) | ||
|
|
||
| cfg := LoadAgentsConfig() | ||
|
|
||
| assert.Equal(t, 5*time.Minute, cfg.InstanceStaleAfter) | ||
| assert.Equal(t, 48*time.Hour, cfg.InstanceRetention) | ||
| assert.Equal(t, 2*time.Hour, cfg.OneShotInstanceRetention) | ||
| assert.False(t, cfg.InstancePruneEnabled) | ||
| assert.Equal(t, "0 0 * * * *", cfg.InstancePruneSchedule) | ||
| assert.Equal(t, 20, cfg.MaxInstancesPerAgent) | ||
| } | ||
|
|
||
| func TestLoadAgentsConfigFromEnv(t *testing.T) { | ||
| viper.Reset() | ||
| t.Cleanup(viper.Reset) | ||
| viper.SetEnvPrefix("ccf") | ||
| viper.AutomaticEnv() | ||
| t.Setenv("CCF_AGENT_INSTANCE_PRUNE_ENABLED", "false") | ||
| t.Setenv("CCF_AGENT_MAX_INSTANCES", "3") | ||
| t.Setenv("CCF_AGENT_INSTANCE_STALE_AFTER", "90s") | ||
|
|
||
| cfg := LoadAgentsConfig() | ||
|
|
||
| assert.False(t, cfg.InstancePruneEnabled) | ||
| assert.Equal(t, 3, cfg.MaxInstancesPerAgent) | ||
| assert.Equal(t, 90*time.Second, cfg.InstanceStaleAfter) | ||
| } | ||
|
|
||
| func TestLoadAgentsConfigIgnoresNonPositiveValues(t *testing.T) { | ||
| viper.Reset() | ||
| t.Cleanup(viper.Reset) | ||
| viper.Set("agent_instance_stale_after", "0s") | ||
| viper.Set("agent_instance_retention", "-1h") | ||
| viper.Set("agent_instance_oneshot_retention", "0") | ||
| viper.Set("agent_instance_prune_schedule", "") | ||
| viper.Set("agent_max_instances", 0) | ||
|
|
||
| cfg := LoadAgentsConfig() | ||
|
|
||
| assert.Equal(t, DefaultAgentsConfig(), cfg) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package relational | ||
|
|
||
| import ( | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "gorm.io/datatypes" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // ErrAgentConfigRevisionAppendOnly is returned when code tries to update or delete a | ||
| // configuration revision. | ||
| var ErrAgentConfigRevisionAppendOnly = errors.New("agent config revisions are append-only") | ||
|
|
||
| // AgentConfigRevision is one immutable revision of an agent's configuration overlay (D11). | ||
| // The desired revision of an agent is MAX(revision). The row id feeds the agent-facing | ||
| // opaque ETag (R7), so a DB reset never produces a false 304. | ||
| type AgentConfigRevision struct { | ||
| UUIDModel | ||
| CreatedAt time.Time `json:"createdAt"` | ||
| AgentID uuid.UUID `json:"agentId" gorm:"type:uuid;not null;uniqueIndex:idx_agent_config_rev,priority:1"` | ||
| Revision int64 `json:"revision" gorm:"not null;uniqueIndex:idx_agent_config_rev,priority:2"` | ||
| Overlay datatypes.JSON `json:"overlay" gorm:"type:jsonb;not null"` | ||
| Comment *string `json:"comment,omitempty" gorm:"type:text"` | ||
| CreatedBy string `json:"createdBy" gorm:"type:text;not null"` // user subject id (email) | ||
| CreatedByID *uuid.UUID `json:"createdById,omitempty" gorm:"type:uuid"` // user_uuid claim when present | ||
| RevertOf *int64 `json:"revertOf,omitempty"` | ||
| } | ||
|
|
||
| func (AgentConfigRevision) TableName() string { return "ccf_agent_config_revisions" } | ||
|
|
||
| // BeforeUpdate keeps revisions append-only. | ||
| func (*AgentConfigRevision) BeforeUpdate(*gorm.DB) error { return ErrAgentConfigRevisionAppendOnly } | ||
|
|
||
| // BeforeDelete keeps revisions append-only. | ||
| func (*AgentConfigRevision) BeforeDelete(*gorm.DB) error { return ErrAgentConfigRevisionAppendOnly } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.