AkurAI Build
Menu

popagent

public

Latest change 324ce6d82a1e3e0f842da61c5c6109199a026104 - Control internal agent memory lifecycle by Ólafur Búi Ólafsson

CREATE TABLE IF NOT EXISTS popagent_memory_settings (
  singleton BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (singleton),
  auto_compact BOOLEAN NOT NULL,
  observation_tokens INTEGER NOT NULL CHECK (observation_tokens BETWEEN 4000 AND 200000),
  reflection_tokens INTEGER NOT NULL CHECK (reflection_tokens BETWEEN 4000 AND 400000),
  recent_message_percent INTEGER NOT NULL CHECK (recent_message_percent BETWEEN 5 AND 75),
  buffer_on_idle BOOLEAN NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS popagent_data_migrations (
  id TEXT PRIMARY KEY,
  applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

DO $migration$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM popagent_data_migrations
    WHERE id = '2026-08-14-memory-settings-v1'
  ) THEN
    INSERT INTO popagent_memory_settings (
      singleton, auto_compact, observation_tokens, reflection_tokens,
      recent_message_percent, buffer_on_idle
    )
    VALUES (TRUE, TRUE, 30000, 40000, 20, TRUE)
    ON CONFLICT (singleton) DO NOTHING;
    INSERT INTO popagent_data_migrations (id)
    VALUES ('2026-08-14-memory-settings-v1')
    ON CONFLICT DO NOTHING;
  END IF;
END
$migration$;

ALTER TABLE popagent_memory_settings ALTER COLUMN auto_compact DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN observation_tokens DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_tokens DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN recent_message_percent DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN buffer_on_idle DROP DEFAULT;

ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS async_buffering BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS buffer_interval_percent INTEGER;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS observation_block_percent INTEGER;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS reflection_buffer_percent INTEGER;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS reflection_block_percent INTEGER;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS optimize_observer_context BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS previous_observer_tokens INTEGER;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS retrieval_enabled BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS retrieval_scope TEXT;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS temporal_markers BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS activate_after_idle TEXT;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS activate_on_provider_change BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS share_token_budget BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS observe_attachments TEXT;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS observation_instruction TEXT;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS reflection_instruction TEXT;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS internal_recall BOOLEAN;
ALTER TABLE popagent_memory_settings ADD COLUMN IF NOT EXISTS internal_retention BOOLEAN;

DO $migration$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM popagent_data_migrations
    WHERE id = '2026-08-14-memory-settings-v2'
  ) THEN
    UPDATE popagent_memory_settings SET
      async_buffering = COALESCE(async_buffering, TRUE),
      buffer_interval_percent = COALESCE(buffer_interval_percent, 20),
      observation_block_percent = COALESCE(observation_block_percent, 120),
      reflection_buffer_percent = COALESCE(reflection_buffer_percent, 50),
      reflection_block_percent = COALESCE(reflection_block_percent, 120),
      optimize_observer_context = COALESCE(optimize_observer_context, TRUE),
      previous_observer_tokens = COALESCE(previous_observer_tokens, 2000),
      retrieval_enabled = COALESCE(retrieval_enabled, TRUE),
      retrieval_scope = COALESCE(retrieval_scope, 'resource'),
      temporal_markers = COALESCE(temporal_markers, FALSE),
      activate_after_idle = COALESCE(activate_after_idle, 'off'),
      activate_on_provider_change = COALESCE(activate_on_provider_change, FALSE),
      share_token_budget = COALESCE(share_token_budget, FALSE),
      observe_attachments = COALESCE(observe_attachments, 'auto'),
      observation_instruction = COALESCE(observation_instruction, ''),
      reflection_instruction = COALESCE(reflection_instruction, '');
    INSERT INTO popagent_data_migrations (id)
    VALUES ('2026-08-14-memory-settings-v2');
  END IF;
END
$migration$;

DO $migration$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM popagent_data_migrations
    WHERE id = '2026-08-14-memory-settings-v3'
  ) THEN
    UPDATE popagent_memory_settings SET
      internal_recall = COALESCE(internal_recall, TRUE),
      internal_retention = COALESCE(internal_retention, TRUE);
    INSERT INTO popagent_data_migrations (id)
    VALUES ('2026-08-14-memory-settings-v3');
  END IF;
END
$migration$;

ALTER TABLE popagent_memory_settings ALTER COLUMN async_buffering SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN buffer_interval_percent SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN observation_block_percent SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_buffer_percent SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_block_percent SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN optimize_observer_context SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN previous_observer_tokens SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN retrieval_enabled SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN retrieval_scope SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN temporal_markers SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN activate_after_idle SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN activate_on_provider_change SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN share_token_budget SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN observe_attachments SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN observation_instruction SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_instruction SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN internal_recall SET NOT NULL;
ALTER TABLE popagent_memory_settings ALTER COLUMN internal_retention SET NOT NULL;

DO $constraints$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_buffer_interval_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_buffer_interval_check CHECK (buffer_interval_percent BETWEEN 5 AND 90);
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_observation_block_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_observation_block_check CHECK (observation_block_percent BETWEEN 101 AND 199);
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_reflection_buffer_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_reflection_buffer_check CHECK (reflection_buffer_percent BETWEEN 10 AND 90);
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_reflection_block_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_reflection_block_check CHECK (reflection_block_percent BETWEEN 101 AND 199);
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_previous_observer_tokens_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_previous_observer_tokens_check CHECK (previous_observer_tokens BETWEEN 0 AND 100000);
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_retrieval_scope_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_retrieval_scope_check CHECK (retrieval_scope IN ('thread', 'resource'));
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_activate_after_idle_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_activate_after_idle_check CHECK (activate_after_idle IN ('off', 'auto', '5m', '1hr', '24hr'));
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_observe_attachments_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_observe_attachments_check CHECK (observe_attachments IN ('auto', 'all', 'none'));
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_instruction_length_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_instruction_length_check CHECK (length(observation_instruction) <= 8000 AND length(reflection_instruction) <= 8000);
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'popagent_memory_shared_budget_buffering_check') THEN
    ALTER TABLE popagent_memory_settings ADD CONSTRAINT popagent_memory_shared_budget_buffering_check CHECK (NOT share_token_budget OR NOT async_buffering);
  END IF;
END
$constraints$;

ALTER TABLE popagent_memory_settings ALTER COLUMN async_buffering DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN buffer_interval_percent DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN observation_block_percent DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_buffer_percent DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_block_percent DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN optimize_observer_context DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN previous_observer_tokens DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN retrieval_enabled DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN retrieval_scope DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN temporal_markers DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN activate_after_idle DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN activate_on_provider_change DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN share_token_budget DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN observe_attachments DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN observation_instruction DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN reflection_instruction DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN internal_recall DROP DEFAULT;
ALTER TABLE popagent_memory_settings ALTER COLUMN internal_retention DROP DEFAULT;