CREATE TABLE "sys"."event_consumption" ( "event_id" text NOT NULL, "consumer" text NOT NULL, "consumed_at" timestamp with time zone DEFAULT now() NOT NULL, "ok" boolean DEFAULT true NOT NULL, CONSTRAINT "event_consumption_event_id_consumer_pk" PRIMARY KEY("event_id","consumer") ); --> statement-breakpoint CREATE TABLE "sys"."event_delivery" ( "id" text PRIMARY KEY NOT NULL, "event_id" text NOT NULL, "target_kind" text NOT NULL, "target_id" text NOT NULL, "attempt" integer DEFAULT 1 NOT NULL, "state" text DEFAULT 'pending' NOT NULL, "last_error" text, "next_attempt_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."event" ( "id" text PRIMARY KEY NOT NULL, "type" text NOT NULL, "correlation" jsonb NOT NULL, "payload" jsonb NOT NULL, "occurred_at" timestamp with time zone NOT NULL, "source" jsonb NOT NULL, "trace" jsonb NOT NULL, "target_kind" text, "target_id" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."listener" ( "id" text PRIMARY KEY NOT NULL, "task_id" text NOT NULL, "origin" text NOT NULL, "event_type" text NOT NULL, "key_filter" jsonb DEFAULT '{}'::jsonb NOT NULL, "condition" text, "semantic" text NOT NULL, "deadline" text NOT NULL, "escalation" jsonb DEFAULT '[]'::jsonb NOT NULL, "registered_at" timestamp with time zone NOT NULL, "expires_at" timestamp with time zone NOT NULL, "state" text DEFAULT 'armed' NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."outbound_ref" ( "id" text PRIMARY KEY NOT NULL, "task_id" text NOT NULL, "thread_id" text, "address" text NOT NULL, "sent_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."llm_instance" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "provider" text NOT NULL, "base_url" text NOT NULL, "model" text NOT NULL, "api_key_cipher" text, "system_use" boolean DEFAULT false NOT NULL, "enabled" boolean DEFAULT true NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."replay_model" ( "key" text PRIMARY KEY NOT NULL, "call" jsonb NOT NULL, "result" jsonb NOT NULL, "recorded_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."position_grant" ( "position_id" text NOT NULL, "object_key" text NOT NULL, "action" text NOT NULL, "object" jsonb NOT NULL, "enabled" boolean DEFAULT true NOT NULL, CONSTRAINT "position_grant_position_id_object_key_action_pk" PRIMARY KEY("position_id","object_key","action") ); --> statement-breakpoint CREATE TABLE "sys"."position" ( "id" text PRIMARY KEY NOT NULL, "code" text NOT NULL, "name" text NOT NULL, "note" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sys"."staff" ( "id" text PRIMARY KEY NOT NULL, "account" text NOT NULL, "name" text NOT NULL, "position_id" text NOT NULL, "state" text DEFAULT 'active' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "sys"."event_delivery" ADD CONSTRAINT "event_delivery_event_id_event_id_fk" FOREIGN KEY ("event_id") REFERENCES "sys"."event"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sys"."position_grant" ADD CONSTRAINT "position_grant_position_id_position_id_fk" FOREIGN KEY ("position_id") REFERENCES "sys"."position"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sys"."staff" ADD CONSTRAINT "staff_position_id_position_id_fk" FOREIGN KEY ("position_id") REFERENCES "sys"."position"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "consumption_key" ON "sys"."event_consumption" USING btree ("event_id","consumer");--> statement-breakpoint CREATE INDEX "delivery_pending_idx" ON "sys"."event_delivery" USING btree ("state","next_attempt_at");--> statement-breakpoint CREATE INDEX "delivery_target_idx" ON "sys"."event_delivery" USING btree ("target_kind","target_id");--> statement-breakpoint CREATE INDEX "event_target_idx" ON "sys"."event" USING btree ("target_kind","target_id","occurred_at");--> statement-breakpoint CREATE INDEX "event_occurred_idx" ON "sys"."event" USING btree ("occurred_at");--> statement-breakpoint CREATE INDEX "listener_armed_idx" ON "sys"."listener" USING btree ("state","expires_at");--> statement-breakpoint CREATE INDEX "listener_task_idx" ON "sys"."listener" USING btree ("task_id");--> statement-breakpoint CREATE INDEX "outbound_thread_idx" ON "sys"."outbound_ref" USING btree ("thread_id");--> statement-breakpoint CREATE INDEX "outbound_addr_idx" ON "sys"."outbound_ref" USING btree ("address","sent_at");--> statement-breakpoint CREATE UNIQUE INDEX "llm_instance_name_key" ON "sys"."llm_instance" USING btree ("name");--> statement-breakpoint CREATE INDEX "replay_model_recorded_idx" ON "sys"."replay_model" USING btree ("recorded_at");--> statement-breakpoint CREATE UNIQUE INDEX "position_code_key" ON "sys"."position" USING btree ("code");--> statement-breakpoint CREATE UNIQUE INDEX "staff_account_key" ON "sys"."staff" USING btree ("account");--> statement-breakpoint CREATE INDEX "staff_position_idx" ON "sys"."staff" USING btree ("position_id");