Skip to content

The open-source app everyone uses to manage agents at work

4.7 KB
Raw Blame
-- Lock resource writers before the journal foreign key and index take locks.
LOCK TABLE "agents", "projects" IN SHARE ROW EXCLUSIVE MODE;--> statement-breakpoint
CREATE TABLE "plugin_lifecycle_acknowledgments" (
	"plugin_id" uuid NOT NULL,
	"event_id" bigint NOT NULL,
	"acknowledged_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "plugin_lifecycle_acknowledgments_plugin_id_event_id_pk" PRIMARY KEY("plugin_id","event_id")
);
--> statement-breakpoint
ALTER TABLE "plugin_lifecycle_acknowledgments" ADD CONSTRAINT "plugin_lifecycle_acknowledgments_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "plugin_lifecycle_acknowledgments" ADD CONSTRAINT "plugin_lifecycle_acknowledgments_event_id_resource_lifecycle_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."resource_lifecycle_events"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "plugin_lifecycle_acknowledgments_event_idx" ON "plugin_lifecycle_acknowledgments" USING btree ("event_id");--> statement-breakpoint
CREATE INDEX "resource_lifecycle_events_resource_order_idx" ON "resource_lifecycle_events" USING btree ("company_id","resource_type","resource_id","id");--> statement-breakpoint
-- Older JavaScript backups lost identity metadata. Repair before seeding records.
DO $$
DECLARE next_id bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public' AND table_name = 'resource_lifecycle_events'
      AND column_name = 'id' AND is_identity = 'NO' AND column_default IS NULL
  ) THEN
    SELECT COALESCE(MAX("id"), 0) + 1 INTO next_id FROM "resource_lifecycle_events";
    EXECUTE format('ALTER TABLE "resource_lifecycle_events" ALTER COLUMN "id" ADD GENERATED ALWAYS AS IDENTITY (START WITH %s)', next_id);
  END IF;
END $$;--> statement-breakpoint
-- Seed a one-time current-state baseline before plugins can consume the journal.
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
SELECT "company_id", 'agent', "id", 'create'
FROM "agents"
WHERE "status" NOT IN ('pending_approval', 'terminated')
UNION ALL
SELECT "company_id", 'project', "id", 'create'
FROM "projects"
ON CONFLICT ("company_id", "resource_type", "resource_id") WHERE "action" = 'create' DO NOTHING;--> statement-breakpoint
-- Restore current stop/delete intent without repeating an existing final transition.
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
SELECT a."company_id", 'agent', a."id", CASE a."status" WHEN 'paused' THEN 'pause' ELSE 'terminate' END
FROM "agents" a
WHERE a."status" IN ('paused', 'terminated')
  AND (SELECT e."action" FROM "resource_lifecycle_events" e
       WHERE e."company_id" = a."company_id" AND e."resource_type" = 'agent'
         AND e."resource_id" = a."id" AND e."action" <> 'create'
       ORDER BY e."id" DESC LIMIT 1)
      IS DISTINCT FROM CASE a."status" WHEN 'paused' THEN 'pause' ELSE 'terminate' END;--> statement-breakpoint
-- A partial journal may end at pause even though the current agent is running.
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
SELECT a."company_id", 'agent', a."id", 'resume'
FROM "agents" a
WHERE a."status" NOT IN ('pending_approval', 'paused', 'terminated')
  AND (SELECT e."action" FROM "resource_lifecycle_events" e
       WHERE e."company_id" = a."company_id" AND e."resource_type" = 'agent'
         AND e."resource_id" = a."id" AND e."action" <> 'create'
       ORDER BY e."id" DESC LIMIT 1) = 'pause';
--> statement-breakpoint
-- Preserve project archive state, including resources created before capture.
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
SELECT p."company_id", 'project', p."id", 'archive'
FROM "projects" p
WHERE p."archived_at" IS NOT NULL
  AND (SELECT e."action" FROM "resource_lifecycle_events" e
       WHERE e."company_id" = p."company_id" AND e."resource_type" = 'project'
         AND e."resource_id" = p."id" AND e."action" <> 'create'
       ORDER BY e."id" DESC LIMIT 1) IS DISTINCT FROM 'archive';--> statement-breakpoint
-- A partial journal may end at archive even though the project was restored.
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
SELECT p."company_id", 'project', p."id", 'update'
FROM "projects" p
WHERE p."archived_at" IS NULL
  AND (SELECT e."action" FROM "resource_lifecycle_events" e
       WHERE e."company_id" = p."company_id" AND e."resource_type" = 'project'
         AND e."resource_id" = p."id" AND e."action" <> 'create'
       ORDER BY e."id" DESC LIMIT 1) = 'archive';