@intx/db 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +37 -0
  2. package/drizzle.config.ts +23 -0
  3. package/migrations/.gitkeep +0 -0
  4. package/migrations/0000_brown_wither.sql +50 -0
  5. package/migrations/0001_white_aqueduct.sql +105 -0
  6. package/migrations/0002_clever_falcon.sql +56 -0
  7. package/migrations/0003_stiff_tyrannus.sql +44 -0
  8. package/migrations/0004_rename_capability_to_offering.sql +1 -0
  9. package/migrations/0005_gigantic_cardiac.sql +1 -0
  10. package/migrations/0006_sidecar.sql +8 -0
  11. package/migrations/0007_agent_running_session.sql +1 -0
  12. package/migrations/0008_session.sql +10 -0
  13. package/migrations/0009_session_messages.sql +18 -0
  14. package/migrations/0010_agent_sidecar_pubkey.sql +2 -0
  15. package/migrations/0011_session_message_from.sql +2 -0
  16. package/migrations/0012_agent_instance.sql +22 -0
  17. package/migrations/0013_instance_session_id.sql +2 -0
  18. package/migrations/0014_drop_agent_runtime_columns.sql +12 -0
  19. package/migrations/0015_add_instance_id_to_session_message.sql +2 -0
  20. package/migrations/0016_jazzy_gamma_corps.sql +3 -0
  21. package/migrations/0017_hesitant_marvex.sql +1 -0
  22. package/migrations/0018_natural_sinister_six.sql +5 -0
  23. package/migrations/0019_rename_grant_source_to_origin.sql +1 -0
  24. package/migrations/0020_add_agent_role.sql +9 -0
  25. package/migrations/0021_acoustic_ozymandias.sql +15 -0
  26. package/migrations/0022_material_sleepwalker.sql +27 -0
  27. package/migrations/0023_flawless_scarlet_witch.sql +2 -0
  28. package/migrations/0024_bumpy_sharon_ventura.sql +1 -0
  29. package/migrations/0025_curvy_firestar.sql +26 -0
  30. package/migrations/0026_keen_ultimo.sql +13 -0
  31. package/migrations/0027_git_tokens.sql +21 -0
  32. package/migrations/0028_wet_sugar_man.sql +1 -0
  33. package/migrations/meta/0000_snapshot.json +316 -0
  34. package/migrations/meta/0001_snapshot.json +968 -0
  35. package/migrations/meta/0002_snapshot.json +1315 -0
  36. package/migrations/meta/0003_snapshot.json +1594 -0
  37. package/migrations/meta/0004_snapshot.json +1594 -0
  38. package/migrations/meta/0005_snapshot.json +1600 -0
  39. package/migrations/meta/0011_snapshot.json +1921 -0
  40. package/migrations/meta/0012_snapshot.json +2067 -0
  41. package/migrations/meta/0013_snapshot.json +2082 -0
  42. package/migrations/meta/0014_snapshot.json +2049 -0
  43. package/migrations/meta/0015_snapshot.json +2064 -0
  44. package/migrations/meta/0016_snapshot.json +2085 -0
  45. package/migrations/meta/0017_snapshot.json +2085 -0
  46. package/migrations/meta/0018_snapshot.json +2070 -0
  47. package/migrations/meta/0019_snapshot.json +2070 -0
  48. package/migrations/meta/0020_snapshot.json +2126 -0
  49. package/migrations/meta/0021_snapshot.json +2239 -0
  50. package/migrations/meta/0022_snapshot.json +2425 -0
  51. package/migrations/meta/0023_snapshot.json +2260 -0
  52. package/migrations/meta/0024_snapshot.json +2254 -0
  53. package/migrations/meta/0025_snapshot.json +2418 -0
  54. package/migrations/meta/0026_snapshot.json +2508 -0
  55. package/migrations/meta/0027_snapshot.json +2657 -0
  56. package/migrations/meta/0028_snapshot.json +2657 -0
  57. package/migrations/meta/_journal.json +209 -0
  58. package/package.json +27 -0
  59. package/src/client.ts +24 -0
  60. package/src/config.ts +19 -0
  61. package/src/connection.ts +27 -0
  62. package/src/credential-resolution.ts +378 -0
  63. package/src/grant-store.ts +51 -0
  64. package/src/index.ts +32 -0
  65. package/src/migrate.test.ts +35 -0
  66. package/src/migrate.ts +168 -0
  67. package/src/parse-row.test.ts +113 -0
  68. package/src/parse-row.ts +185 -0
  69. package/src/schema/agent-assets.ts +21 -0
  70. package/src/schema/agents.ts +49 -0
  71. package/src/schema/assets.ts +24 -0
  72. package/src/schema/auth.ts +51 -0
  73. package/src/schema/credentials.ts +43 -0
  74. package/src/schema/git-tokens.ts +83 -0
  75. package/src/schema/grants.ts +26 -0
  76. package/src/schema/index.ts +19 -0
  77. package/src/schema/instances.ts +36 -0
  78. package/src/schema/messages.ts +108 -0
  79. package/src/schema/oauth-clients.ts +26 -0
  80. package/src/schema/offerings.ts +20 -0
  81. package/src/schema/principals.ts +28 -0
  82. package/src/schema/providers.ts +23 -0
  83. package/src/schema/roles.ts +51 -0
  84. package/src/schema/session-assets.ts +49 -0
  85. package/src/schema/sessions.ts +26 -0
  86. package/src/schema/sidecar.ts +14 -0
  87. package/src/schema/tenants.ts +41 -0
  88. package/src/schema/wallets.ts +44 -0
  89. package/src/tenant-hierarchy.ts +34 -0
  90. package/tsconfig.json +4 -0
  91. package/tsconfig.tsbuildinfo +1 -0
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @intx/db
2
+
3
+ Drizzle ORM client, schema, migrations, and row parsers for the
4
+ hub's PostgreSQL database. Owns the database-backed `GrantStore`,
5
+ the tenant hierarchy walk, the credential resolution layer, and
6
+ the `parseRow` family that validates `jsonb` columns at the
7
+ database boundary so internal code never re-checks them.
8
+
9
+ Consumed by `@intx/hub-api` (HTTP request handlers and grant
10
+ middleware) and `@intx/hub-sessions` (session orchestration and
11
+ agent repo metadata).
12
+
13
+ ## Surface
14
+
15
+ - `@intx/db` — the client (`createDB`), config validator, migration
16
+ runner, grant store, credential resolution, tenant hierarchy
17
+ helpers, and the `parseRow` functions.
18
+ - `@intx/db/schema` — the Drizzle table exports, used by callers
19
+ that compose queries against the schema directly.
20
+
21
+ ```ts
22
+ import { createDB } from "@intx/db";
23
+ import { agent } from "@intx/db/schema";
24
+ import { eq } from "drizzle-orm";
25
+
26
+ const { db } = createDB(process.env);
27
+
28
+ const row = await db.query.agent.findFirst({
29
+ where: eq(agent.id, agentId),
30
+ });
31
+ ```
32
+
33
+ The `parseRow` functions (`parseAgentRow`, `parseGrantRow`, and so
34
+ on) validate each table's `jsonb` columns once and return fully
35
+ typed values; downstream code uses those values without
36
+ re-casting. See `CONVENTIONS.md` for the project-wide rule against
37
+ cast-at-callsite in DB consumers.
@@ -0,0 +1,23 @@
1
+ import { defineConfig } from "drizzle-kit";
2
+
3
+ function env(name: string): string {
4
+ const value = process.env[name];
5
+ if (!value) {
6
+ throw new Error(`Missing required environment variable: ${name}`);
7
+ }
8
+ return value;
9
+ }
10
+
11
+ export default defineConfig({
12
+ dialect: "postgresql",
13
+ schema: "./src/schema/index.ts",
14
+ out: "./migrations",
15
+ dbCredentials: {
16
+ host: env("DB_HOST"),
17
+ port: Number(process.env["DB_PORT"] ?? 5432),
18
+ user: env("DB_USER"),
19
+ password: process.env["DB_PASSWORD"] ?? "",
20
+ database: env("DB_NAME"),
21
+ ssl: process.env["DB_SSL"] === "true",
22
+ },
23
+ });
File without changes
@@ -0,0 +1,50 @@
1
+ CREATE TABLE "account" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "user_id" text NOT NULL,
4
+ "account_id" text NOT NULL,
5
+ "provider_id" text NOT NULL,
6
+ "access_token" text,
7
+ "refresh_token" text,
8
+ "access_token_expires_at" timestamp,
9
+ "refresh_token_expires_at" timestamp,
10
+ "scope" text,
11
+ "id_token" text,
12
+ "password" text,
13
+ "created_at" timestamp DEFAULT now() NOT NULL,
14
+ "updated_at" timestamp DEFAULT now() NOT NULL
15
+ );
16
+ --> statement-breakpoint
17
+ CREATE TABLE "session" (
18
+ "id" text PRIMARY KEY NOT NULL,
19
+ "user_id" text NOT NULL,
20
+ "token" text NOT NULL,
21
+ "expires_at" timestamp NOT NULL,
22
+ "ip_address" text,
23
+ "user_agent" text,
24
+ "created_at" timestamp DEFAULT now() NOT NULL,
25
+ "updated_at" timestamp DEFAULT now() NOT NULL,
26
+ CONSTRAINT "session_token_unique" UNIQUE("token")
27
+ );
28
+ --> statement-breakpoint
29
+ CREATE TABLE "user" (
30
+ "id" text PRIMARY KEY NOT NULL,
31
+ "name" text NOT NULL,
32
+ "email" text NOT NULL,
33
+ "email_verified" boolean DEFAULT false NOT NULL,
34
+ "image" text,
35
+ "created_at" timestamp DEFAULT now() NOT NULL,
36
+ "updated_at" timestamp DEFAULT now() NOT NULL,
37
+ CONSTRAINT "user_email_unique" UNIQUE("email")
38
+ );
39
+ --> statement-breakpoint
40
+ CREATE TABLE "verification" (
41
+ "id" text PRIMARY KEY NOT NULL,
42
+ "identifier" text NOT NULL,
43
+ "value" text NOT NULL,
44
+ "expires_at" timestamp NOT NULL,
45
+ "created_at" timestamp DEFAULT now() NOT NULL,
46
+ "updated_at" timestamp DEFAULT now() NOT NULL
47
+ );
48
+ --> statement-breakpoint
49
+ ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
50
+ ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,105 @@
1
+ CREATE TABLE "federation_trust" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "tenant_id" text NOT NULL,
4
+ "target_tenant_id" text NOT NULL,
5
+ "direction" text NOT NULL,
6
+ "created_at" timestamp DEFAULT now() NOT NULL,
7
+ CONSTRAINT "federation_trust_tenant_id_target_tenant_id_unique" UNIQUE("tenant_id","target_tenant_id")
8
+ );
9
+ --> statement-breakpoint
10
+ CREATE TABLE "tenant" (
11
+ "id" text PRIMARY KEY NOT NULL,
12
+ "name" text NOT NULL,
13
+ "slug" text NOT NULL,
14
+ "domain" text NOT NULL,
15
+ "parent_id" text,
16
+ "config" jsonb,
17
+ "created_at" timestamp DEFAULT now() NOT NULL,
18
+ "updated_at" timestamp DEFAULT now() NOT NULL,
19
+ CONSTRAINT "tenant_slug_unique" UNIQUE("slug"),
20
+ CONSTRAINT "tenant_domain_unique" UNIQUE("domain")
21
+ );
22
+ --> statement-breakpoint
23
+ CREATE TABLE "principal" (
24
+ "id" text PRIMARY KEY NOT NULL,
25
+ "tenant_id" text NOT NULL,
26
+ "kind" text NOT NULL,
27
+ "ref_id" text NOT NULL,
28
+ "status" text NOT NULL,
29
+ "created_at" timestamp DEFAULT now() NOT NULL,
30
+ "updated_at" timestamp DEFAULT now() NOT NULL,
31
+ CONSTRAINT "principal_tenant_id_kind_ref_id_unique" UNIQUE("tenant_id","kind","ref_id")
32
+ );
33
+ --> statement-breakpoint
34
+ CREATE TABLE "principal_role" (
35
+ "principal_id" text NOT NULL,
36
+ "role_id" text NOT NULL,
37
+ "created_at" timestamp DEFAULT now() NOT NULL,
38
+ CONSTRAINT "principal_role_principal_id_role_id_pk" PRIMARY KEY("principal_id","role_id")
39
+ );
40
+ --> statement-breakpoint
41
+ CREATE TABLE "role" (
42
+ "id" text PRIMARY KEY NOT NULL,
43
+ "tenant_id" text NOT NULL,
44
+ "name" text NOT NULL,
45
+ "description" text,
46
+ "is_system" boolean DEFAULT false NOT NULL,
47
+ "created_at" timestamp DEFAULT now() NOT NULL,
48
+ "updated_at" timestamp DEFAULT now() NOT NULL
49
+ );
50
+ --> statement-breakpoint
51
+ CREATE TABLE "grant" (
52
+ "id" text PRIMARY KEY NOT NULL,
53
+ "tenant_id" text NOT NULL,
54
+ "role_id" text,
55
+ "principal_id" text,
56
+ "resource" text NOT NULL,
57
+ "action" text NOT NULL,
58
+ "effect" text NOT NULL,
59
+ "conditions" jsonb,
60
+ "source" text NOT NULL,
61
+ "expires_at" timestamp,
62
+ "created_at" timestamp DEFAULT now() NOT NULL,
63
+ "updated_at" timestamp DEFAULT now() NOT NULL
64
+ );
65
+ --> statement-breakpoint
66
+ CREATE TABLE "agent" (
67
+ "id" text PRIMARY KEY NOT NULL,
68
+ "tenant_id" text NOT NULL,
69
+ "principal_id" text NOT NULL,
70
+ "name" text NOT NULL,
71
+ "description" text,
72
+ "system_prompt" text,
73
+ "skills" jsonb,
74
+ "context_config" jsonb,
75
+ "initial_state" jsonb,
76
+ "model_config" jsonb,
77
+ "capabilities" jsonb,
78
+ "current_version" text DEFAULT '1' NOT NULL,
79
+ "status" text DEFAULT 'deployed' NOT NULL,
80
+ "kernel_id" text,
81
+ "created_at" timestamp DEFAULT now() NOT NULL,
82
+ "updated_at" timestamp DEFAULT now() NOT NULL
83
+ );
84
+ --> statement-breakpoint
85
+ CREATE TABLE "agent_version" (
86
+ "id" text PRIMARY KEY NOT NULL,
87
+ "agent_id" text NOT NULL,
88
+ "version" text NOT NULL,
89
+ "status" text DEFAULT 'active' NOT NULL,
90
+ "created_at" timestamp DEFAULT now() NOT NULL
91
+ );
92
+ --> statement-breakpoint
93
+ ALTER TABLE "federation_trust" ADD CONSTRAINT "federation_trust_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
94
+ ALTER TABLE "federation_trust" ADD CONSTRAINT "federation_trust_target_tenant_id_tenant_id_fk" FOREIGN KEY ("target_tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
95
+ ALTER TABLE "tenant" ADD CONSTRAINT "tenant_parent_id_tenant_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."tenant"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
96
+ ALTER TABLE "principal" ADD CONSTRAINT "principal_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
97
+ ALTER TABLE "principal_role" ADD CONSTRAINT "principal_role_principal_id_principal_id_fk" FOREIGN KEY ("principal_id") REFERENCES "public"."principal"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
98
+ ALTER TABLE "principal_role" ADD CONSTRAINT "principal_role_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
99
+ ALTER TABLE "role" ADD CONSTRAINT "role_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
100
+ ALTER TABLE "grant" ADD CONSTRAINT "grant_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
101
+ ALTER TABLE "grant" ADD CONSTRAINT "grant_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
102
+ ALTER TABLE "grant" ADD CONSTRAINT "grant_principal_id_principal_id_fk" FOREIGN KEY ("principal_id") REFERENCES "public"."principal"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
103
+ ALTER TABLE "agent" ADD CONSTRAINT "agent_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
104
+ ALTER TABLE "agent" ADD CONSTRAINT "agent_principal_id_principal_id_fk" FOREIGN KEY ("principal_id") REFERENCES "public"."principal"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
105
+ ALTER TABLE "agent_version" ADD CONSTRAINT "agent_version_agent_id_agent_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agent"("id") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,56 @@
1
+ CREATE TABLE "credential" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "tenant_id" text NOT NULL,
4
+ "name" text NOT NULL,
5
+ "type" text NOT NULL,
6
+ "description" text,
7
+ "secret" text NOT NULL,
8
+ "metadata" jsonb,
9
+ "created_at" timestamp DEFAULT now() NOT NULL,
10
+ "updated_at" timestamp DEFAULT now() NOT NULL
11
+ );
12
+ --> statement-breakpoint
13
+ CREATE TABLE "transaction" (
14
+ "id" text PRIMARY KEY NOT NULL,
15
+ "wallet_id" text NOT NULL,
16
+ "agent_id" text,
17
+ "direction" text NOT NULL,
18
+ "amount" text NOT NULL,
19
+ "currency" text NOT NULL,
20
+ "recipient_id" text,
21
+ "sender_id" text,
22
+ "request_id" text,
23
+ "status" text DEFAULT 'pending' NOT NULL,
24
+ "created_at" timestamp DEFAULT now() NOT NULL
25
+ );
26
+ --> statement-breakpoint
27
+ CREATE TABLE "wallet" (
28
+ "id" text PRIMARY KEY NOT NULL,
29
+ "tenant_id" text NOT NULL,
30
+ "name" text NOT NULL,
31
+ "backend_type" text NOT NULL,
32
+ "currency" text NOT NULL,
33
+ "balance" text DEFAULT '0' NOT NULL,
34
+ "config" jsonb,
35
+ "created_at" timestamp DEFAULT now() NOT NULL,
36
+ "updated_at" timestamp DEFAULT now() NOT NULL
37
+ );
38
+ --> statement-breakpoint
39
+ CREATE TABLE "capability" (
40
+ "id" text PRIMARY KEY NOT NULL,
41
+ "agent_id" text NOT NULL,
42
+ "tenant_id" text NOT NULL,
43
+ "name" text NOT NULL,
44
+ "description" text,
45
+ "pricing" jsonb,
46
+ "schema" jsonb,
47
+ "created_at" timestamp DEFAULT now() NOT NULL,
48
+ "updated_at" timestamp DEFAULT now() NOT NULL
49
+ );
50
+ --> statement-breakpoint
51
+ ALTER TABLE "credential" ADD CONSTRAINT "credential_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
52
+ ALTER TABLE "transaction" ADD CONSTRAINT "transaction_wallet_id_wallet_id_fk" FOREIGN KEY ("wallet_id") REFERENCES "public"."wallet"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
53
+ ALTER TABLE "transaction" ADD CONSTRAINT "transaction_agent_id_agent_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agent"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
54
+ ALTER TABLE "wallet" ADD CONSTRAINT "wallet_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
55
+ ALTER TABLE "capability" ADD CONSTRAINT "capability_agent_id_agent_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agent"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
56
+ ALTER TABLE "capability" ADD CONSTRAINT "capability_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,44 @@
1
+ CREATE TABLE "provider" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "tenant_id" text NOT NULL,
4
+ "name" text NOT NULL,
5
+ "plugin" text NOT NULL,
6
+ "authorization_url" text,
7
+ "token_url" text,
8
+ "user_info_url" text,
9
+ "scopes" text[],
10
+ "metadata" jsonb,
11
+ "created_at" timestamp DEFAULT now() NOT NULL,
12
+ "updated_at" timestamp DEFAULT now() NOT NULL,
13
+ CONSTRAINT "provider_tenant_name" UNIQUE("tenant_id","name")
14
+ );
15
+ --> statement-breakpoint
16
+ CREATE TABLE "oauth_client" (
17
+ "id" text PRIMARY KEY NOT NULL,
18
+ "tenant_id" text NOT NULL,
19
+ "provider_id" text NOT NULL,
20
+ "name" text NOT NULL,
21
+ "client_id" text NOT NULL,
22
+ "client_secret" text NOT NULL,
23
+ "redirect_uris" text[],
24
+ "default_scopes" text[],
25
+ "metadata" jsonb,
26
+ "created_at" timestamp DEFAULT now() NOT NULL,
27
+ "updated_at" timestamp DEFAULT now() NOT NULL,
28
+ CONSTRAINT "oauth_client_tenant_provider" UNIQUE("tenant_id","provider_id")
29
+ );
30
+ --> statement-breakpoint
31
+ ALTER TABLE "credential" ADD COLUMN "principal_id" text;--> statement-breakpoint
32
+ ALTER TABLE "credential" ADD COLUMN "provider_id" text NOT NULL;--> statement-breakpoint
33
+ ALTER TABLE "credential" ADD COLUMN "oauth_client_id" text;--> statement-breakpoint
34
+ ALTER TABLE "credential" ADD COLUMN "refresh_secret" text;--> statement-breakpoint
35
+ ALTER TABLE "credential" ADD COLUMN "scopes" text[];--> statement-breakpoint
36
+ ALTER TABLE "credential" ADD COLUMN "expires_at" timestamp;--> statement-breakpoint
37
+ ALTER TABLE "credential" ADD COLUMN "status" text DEFAULT 'active' NOT NULL;--> statement-breakpoint
38
+ ALTER TABLE "provider" ADD CONSTRAINT "provider_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
39
+ ALTER TABLE "oauth_client" ADD CONSTRAINT "oauth_client_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
40
+ ALTER TABLE "oauth_client" ADD CONSTRAINT "oauth_client_provider_id_provider_id_fk" FOREIGN KEY ("provider_id") REFERENCES "public"."provider"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
41
+ ALTER TABLE "credential" ADD CONSTRAINT "credential_principal_id_principal_id_fk" FOREIGN KEY ("principal_id") REFERENCES "public"."principal"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
42
+ ALTER TABLE "credential" ADD CONSTRAINT "credential_provider_id_provider_id_fk" FOREIGN KEY ("provider_id") REFERENCES "public"."provider"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
43
+ ALTER TABLE "credential" ADD CONSTRAINT "credential_oauth_client_id_oauth_client_id_fk" FOREIGN KEY ("oauth_client_id") REFERENCES "public"."oauth_client"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
44
+ ALTER TABLE "credential" ADD CONSTRAINT "credential_tenant_name" UNIQUE("tenant_id","name");
@@ -0,0 +1 @@
1
+ ALTER TABLE "capability" RENAME TO "offering";
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent" ADD COLUMN "credential_requirements" jsonb;
@@ -0,0 +1,8 @@
1
+ CREATE TABLE IF NOT EXISTS "sidecar" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "url" text NOT NULL,
4
+ "status" text DEFAULT 'online' NOT NULL,
5
+ "last_heartbeat" timestamp with time zone,
6
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
7
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
8
+ );
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent" ADD COLUMN IF NOT EXISTS "session_id" text;
@@ -0,0 +1,10 @@
1
+ CREATE TABLE IF NOT EXISTS "agent_session" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "tenant_id" text NOT NULL REFERENCES "tenant"("id") ON DELETE CASCADE,
4
+ "agent_id" text NOT NULL REFERENCES "agent"("id") ON DELETE CASCADE,
5
+ "principal_id" text NOT NULL REFERENCES "principal"("id"),
6
+ "status" text DEFAULT 'active' NOT NULL,
7
+ "created_at" timestamp DEFAULT now() NOT NULL,
8
+ "updated_at" timestamp DEFAULT now() NOT NULL,
9
+ "ended_at" timestamp
10
+ );
@@ -0,0 +1,18 @@
1
+ CREATE TABLE IF NOT EXISTS "session_message" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "session_id" text NOT NULL REFERENCES "agent_session"("id") ON DELETE CASCADE,
4
+ "tenant_id" text NOT NULL REFERENCES "tenant"("id") ON DELETE CASCADE,
5
+ "role" text NOT NULL,
6
+ "status" text DEFAULT 'pending' NOT NULL,
7
+ "created_at" timestamp DEFAULT now() NOT NULL
8
+ );
9
+
10
+ CREATE TABLE IF NOT EXISTS "message_part" (
11
+ "id" text PRIMARY KEY NOT NULL,
12
+ "message_id" text NOT NULL REFERENCES "session_message"("id") ON DELETE CASCADE,
13
+ "session_id" text NOT NULL REFERENCES "agent_session"("id") ON DELETE CASCADE,
14
+ "type" text NOT NULL,
15
+ "content" text,
16
+ "metadata" jsonb,
17
+ "ordinal" integer NOT NULL
18
+ );
@@ -0,0 +1,2 @@
1
+ ALTER TABLE "agent" ADD COLUMN "sidecar_id" text REFERENCES "sidecar"("id") ON DELETE SET NULL;
2
+ ALTER TABLE "agent" ADD COLUMN "public_key" text;
@@ -0,0 +1,2 @@
1
+ ALTER TABLE "session_message" ADD COLUMN "from" text NOT NULL DEFAULT 'unknown';
2
+ ALTER TABLE "session_message" ALTER COLUMN "from" DROP DEFAULT;
@@ -0,0 +1,22 @@
1
+ CREATE TABLE "agent_instance" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "agent_id" text NOT NULL,
4
+ "tenant_id" text NOT NULL,
5
+ "principal_id" text NOT NULL,
6
+ "address" text NOT NULL,
7
+ "version_id" text,
8
+ "status" text DEFAULT 'deployed' NOT NULL,
9
+ "sidecar_id" text,
10
+ "public_key" text,
11
+ "kernel_id" text,
12
+ "created_at" timestamp DEFAULT now() NOT NULL,
13
+ "updated_at" timestamp DEFAULT now() NOT NULL,
14
+ "ended_at" timestamp,
15
+ CONSTRAINT "agent_instance_address_unique" UNIQUE("address")
16
+ );
17
+ --> statement-breakpoint
18
+ ALTER TABLE "agent_instance" ADD CONSTRAINT "agent_instance_agent_id_agent_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agent"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
19
+ ALTER TABLE "agent_instance" ADD CONSTRAINT "agent_instance_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
20
+ ALTER TABLE "agent_instance" ADD CONSTRAINT "agent_instance_principal_id_principal_id_fk" FOREIGN KEY ("principal_id") REFERENCES "public"."principal"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
21
+ ALTER TABLE "agent_instance" ADD CONSTRAINT "agent_instance_version_id_agent_version_id_fk" FOREIGN KEY ("version_id") REFERENCES "public"."agent_version"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
22
+ ALTER TABLE "agent_instance" ADD CONSTRAINT "agent_instance_sidecar_id_sidecar_id_fk" FOREIGN KEY ("sidecar_id") REFERENCES "public"."sidecar"("id") ON DELETE set null ON UPDATE no action;
@@ -0,0 +1,2 @@
1
+ ALTER TABLE "agent_instance" ADD COLUMN "session_id" text;--> statement-breakpoint
2
+ ALTER TABLE "agent_instance" ADD CONSTRAINT "agent_instance_session_id_agent_session_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."agent_session"("id") ON DELETE no action ON UPDATE no action;
@@ -0,0 +1,12 @@
1
+ -- Normalize any stale runtime statuses before dropping the columns
2
+ -- that tracked them. The agent table now only uses deployed/stopped.
3
+ UPDATE "agent" SET "status" = 'deployed' WHERE "status" IN ('running', 'updating', 'error');
4
+ --> statement-breakpoint
5
+ ALTER TABLE "agent" DROP CONSTRAINT IF EXISTS "agent_sidecar_id_sidecar_id_fk";
6
+ --> statement-breakpoint
7
+ ALTER TABLE "agent" DROP CONSTRAINT IF EXISTS "agent_sidecar_id_fkey";
8
+ --> statement-breakpoint
9
+ ALTER TABLE "agent" DROP COLUMN "sidecar_id";--> statement-breakpoint
10
+ ALTER TABLE "agent" DROP COLUMN "public_key";--> statement-breakpoint
11
+ ALTER TABLE "agent" DROP COLUMN "kernel_id";--> statement-breakpoint
12
+ ALTER TABLE "agent" DROP COLUMN "session_id";
@@ -0,0 +1,2 @@
1
+ ALTER TABLE "session_message" ADD COLUMN "instance_id" text;--> statement-breakpoint
2
+ ALTER TABLE "session_message" ADD CONSTRAINT "session_message_instance_id_agent_instance_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."agent_instance"("id") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,3 @@
1
+ ALTER TABLE "agent" ADD COLUMN "creator_principal_id" text;--> statement-breakpoint
2
+ ALTER TABLE "agent" ADD COLUMN "grant_requirements" jsonb;--> statement-breakpoint
3
+ ALTER TABLE "agent" ADD CONSTRAINT "agent_creator_principal_id_principal_id_fk" FOREIGN KEY ("creator_principal_id") REFERENCES "public"."principal"("id") ON DELETE no action ON UPDATE no action;
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent" ALTER COLUMN "principal_id" DROP NOT NULL;
@@ -0,0 +1,5 @@
1
+ UPDATE "agent" SET "creator_principal_id" = "principal_id" WHERE "creator_principal_id" IS NULL AND "principal_id" IS NOT NULL;
2
+ --> statement-breakpoint
3
+ ALTER TABLE "agent" DROP CONSTRAINT "agent_principal_id_principal_id_fk";
4
+ --> statement-breakpoint
5
+ ALTER TABLE "agent" DROP COLUMN "principal_id";
@@ -0,0 +1 @@
1
+ ALTER TABLE "grant" RENAME COLUMN "source" TO "origin";
@@ -0,0 +1,9 @@
1
+ CREATE TABLE "agent_role" (
2
+ "agent_id" text NOT NULL,
3
+ "role_id" text NOT NULL,
4
+ "created_at" timestamp DEFAULT now() NOT NULL,
5
+ CONSTRAINT "agent_role_agent_id_role_id_pk" PRIMARY KEY("agent_id","role_id")
6
+ );
7
+ --> statement-breakpoint
8
+ ALTER TABLE "agent_role" ADD CONSTRAINT "agent_role_agent_id_agent_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agent"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
9
+ ALTER TABLE "agent_role" ADD CONSTRAINT "agent_role_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,15 @@
1
+ CREATE TABLE "session_mail" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "session_id" text NOT NULL,
4
+ "instance_id" text,
5
+ "tenant_id" text NOT NULL,
6
+ "direction" text NOT NULL,
7
+ "status" text DEFAULT 'pending' NOT NULL,
8
+ "raw" "bytea" NOT NULL,
9
+ "created_at" timestamp DEFAULT now() NOT NULL
10
+ );
11
+ --> statement-breakpoint
12
+ ALTER TABLE "session_mail" ADD CONSTRAINT "session_mail_session_id_agent_session_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."agent_session"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
13
+ ALTER TABLE "session_mail" ADD CONSTRAINT "session_mail_instance_id_agent_instance_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."agent_instance"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
14
+ ALTER TABLE "session_mail" ADD CONSTRAINT "session_mail_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
15
+ CREATE INDEX "session_mail_instance_id_created_at_idx" ON "session_mail" USING btree ("instance_id","created_at");
@@ -0,0 +1,27 @@
1
+ CREATE TABLE "inference_turn" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "session_id" text NOT NULL,
4
+ "instance_id" text NOT NULL,
5
+ "tenant_id" text NOT NULL,
6
+ "model" text NOT NULL,
7
+ "status" text DEFAULT 'running' NOT NULL,
8
+ "started_at" timestamp NOT NULL,
9
+ "ended_at" timestamp
10
+ );
11
+ --> statement-breakpoint
12
+ CREATE TABLE "turn_part" (
13
+ "id" text PRIMARY KEY NOT NULL,
14
+ "turn_id" text NOT NULL,
15
+ "session_id" text NOT NULL,
16
+ "type" text NOT NULL,
17
+ "content" text,
18
+ "metadata" jsonb,
19
+ "ordinal" integer NOT NULL
20
+ );
21
+ --> statement-breakpoint
22
+ ALTER TABLE "inference_turn" ADD CONSTRAINT "inference_turn_session_id_agent_session_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."agent_session"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
23
+ ALTER TABLE "inference_turn" ADD CONSTRAINT "inference_turn_instance_id_agent_instance_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."agent_instance"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
24
+ ALTER TABLE "inference_turn" ADD CONSTRAINT "inference_turn_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
25
+ ALTER TABLE "turn_part" ADD CONSTRAINT "turn_part_turn_id_inference_turn_id_fk" FOREIGN KEY ("turn_id") REFERENCES "public"."inference_turn"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
26
+ ALTER TABLE "turn_part" ADD CONSTRAINT "turn_part_session_id_agent_session_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."agent_session"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
27
+ CREATE INDEX "inference_turn_instance_id_started_at_idx" ON "inference_turn" USING btree ("instance_id","started_at");
@@ -0,0 +1,2 @@
1
+ DROP TABLE "message_part" CASCADE;--> statement-breakpoint
2
+ DROP TABLE "session_message" CASCADE;
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent" DROP COLUMN "skills";
@@ -0,0 +1,26 @@
1
+ CREATE TABLE "asset" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "tenant_id" text NOT NULL,
4
+ "kind" text NOT NULL,
5
+ "name" text NOT NULL,
6
+ "display_name" text,
7
+ "creator_principal_id" text,
8
+ "created_at" timestamp DEFAULT now() NOT NULL,
9
+ "updated_at" timestamp DEFAULT now() NOT NULL,
10
+ CONSTRAINT "asset_tenant_kind_name" UNIQUE("tenant_id","kind","name")
11
+ );
12
+ --> statement-breakpoint
13
+ CREATE TABLE "agent_asset" (
14
+ "id" text PRIMARY KEY NOT NULL,
15
+ "agent_id" text NOT NULL,
16
+ "asset_id" text NOT NULL,
17
+ "ref" text NOT NULL,
18
+ "access_mode" text DEFAULT 'read-only' NOT NULL,
19
+ "created_at" timestamp DEFAULT now() NOT NULL,
20
+ CONSTRAINT "agent_asset_agent_asset" UNIQUE("agent_id","asset_id")
21
+ );
22
+ --> statement-breakpoint
23
+ ALTER TABLE "asset" ADD CONSTRAINT "asset_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
24
+ ALTER TABLE "asset" ADD CONSTRAINT "asset_creator_principal_id_principal_id_fk" FOREIGN KEY ("creator_principal_id") REFERENCES "public"."principal"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
25
+ ALTER TABLE "agent_asset" ADD CONSTRAINT "agent_asset_agent_id_agent_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agent"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
26
+ ALTER TABLE "agent_asset" ADD CONSTRAINT "agent_asset_asset_id_asset_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."asset"("id") ON DELETE cascade ON UPDATE no action;
@@ -0,0 +1,13 @@
1
+ CREATE TABLE "session_asset" (
2
+ "instance_id" text NOT NULL,
3
+ "agent_asset_id" text NOT NULL,
4
+ "mount_path" text NOT NULL,
5
+ "asset_pack_sha" text NOT NULL,
6
+ "source_commit_sha" text NOT NULL,
7
+ "materialized_at" timestamp DEFAULT now() NOT NULL,
8
+ CONSTRAINT "session_asset_instance_id_agent_asset_id_pk" PRIMARY KEY("instance_id","agent_asset_id")
9
+ );
10
+ --> statement-breakpoint
11
+ ALTER TABLE "session_asset" ADD CONSTRAINT "session_asset_instance_id_agent_instance_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."agent_instance"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
12
+ ALTER TABLE "session_asset" ADD CONSTRAINT "session_asset_agent_asset_id_agent_asset_id_fk" FOREIGN KEY ("agent_asset_id") REFERENCES "public"."agent_asset"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
13
+ CREATE INDEX "session_asset_pack_sha_idx" ON "session_asset" USING btree ("asset_pack_sha");
@@ -0,0 +1,21 @@
1
+ CREATE TABLE "git_token" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "tenant_id" text,
4
+ "user_id" text NOT NULL,
5
+ "principal_id" text,
6
+ "name" text NOT NULL,
7
+ "kind" text NOT NULL,
8
+ "token_hash_sha256" "bytea" NOT NULL,
9
+ "resource" text NOT NULL,
10
+ "ref_pattern" text NOT NULL,
11
+ "actions" text[] NOT NULL,
12
+ "expires_at" timestamp with time zone NOT NULL,
13
+ "revoked_at" timestamp with time zone,
14
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
15
+ CONSTRAINT "git_token_token_hash_sha256_unique" UNIQUE("token_hash_sha256")
16
+ );
17
+ --> statement-breakpoint
18
+ ALTER TABLE "git_token" ADD CONSTRAINT "git_token_tenant_id_tenant_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenant"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
19
+ ALTER TABLE "git_token" ADD CONSTRAINT "git_token_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
20
+ ALTER TABLE "git_token" ADD CONSTRAINT "git_token_principal_id_principal_id_fk" FOREIGN KEY ("principal_id") REFERENCES "public"."principal"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
21
+ CREATE UNIQUE INDEX "git_token_user_id_name_active_idx" ON "git_token" USING btree ("user_id","name") WHERE "git_token"."revoked_at" is null;
@@ -0,0 +1 @@
1
+ ALTER TABLE "agent" ALTER COLUMN "creator_principal_id" SET NOT NULL;