Skip to content

Commit 556ffcb

Browse files
RusDynclaude
andcommitted
Rename default_project_id/default_department_id to project_id/department_id
Agent keys have a home project and department, not just defaults. Rename columns, update CHECK constraint, recreate workspace consistency trigger, and update all code references across Edge Functions, Next.js services, actions, API routes, and seed script. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 88a84e8 commit 556ffcb

File tree

14 files changed

+128
-81
lines changed

14 files changed

+128
-81
lines changed

scripts/seed-radiancefleet.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async function getOrCreateAgentKey(
164164
workspaceId: string,
165165
name: string,
166166
createdBy: string,
167-
defaults?: { defaultProjectId: string; defaultDepartmentId: string },
167+
defaults?: { projectId: string; departmentId: string },
168168
): Promise<{ row: AgentKeyRow; fullKey: string | null; isNew: boolean }> {
169169
const existing = await rest('agent_keys', {
170170
query: `workspace_id=eq.${workspaceId}&name=eq.${encodeURIComponent(name)}&select=id,name`,
@@ -179,8 +179,8 @@ async function getOrCreateAgentKey(
179179
method: 'PATCH',
180180
query: `id=eq.${existing[0].id}`,
181181
body: {
182-
default_project_id: defaults.defaultProjectId,
183-
default_department_id: defaults.defaultDepartmentId,
182+
project_id: defaults.projectId,
183+
department_id: defaults.departmentId,
184184
},
185185
})
186186
console.log(` Updated defaults on existing key`)
@@ -202,8 +202,8 @@ async function getOrCreateAgentKey(
202202
workspace_id: workspaceId,
203203
created_by: createdBy,
204204
...(defaults && {
205-
default_project_id: defaults.defaultProjectId,
206-
default_department_id: defaults.defaultDepartmentId,
205+
project_id: defaults.projectId,
206+
department_id: defaults.departmentId,
207207
}),
208208
},
209209
prefer: 'return=representation',
@@ -271,12 +271,12 @@ async function main() {
271271
// 2. Agent keys
272272
console.log('\nCreating agent keys...')
273273
const opsAgent = await getOrCreateAgentKey(workspaceId, 'radiancefleet-ops-agent', ownerId, {
274-
defaultProjectId: project.id,
275-
defaultDepartmentId: opsDept.id,
274+
projectId: project.id,
275+
departmentId: opsDept.id,
276276
})
277277
const coreAgent = await getOrCreateAgentKey(workspaceId, 'radiancefleet-core-agent', ownerId, {
278-
defaultProjectId: project.id,
279-
defaultDepartmentId: coreDept.id,
278+
projectId: project.id,
279+
departmentId: coreDept.id,
280280
})
281281

282282
// 3. Permissions

src/app/(dashboard)/actions/agent-key-actions.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ export async function createAgentKeyAction(formData: FormData) {
3737
}
3838

3939
const workspace = await getWorkspaceForUser(supabase);
40-
const defaultProjectId = formData.get('defaultProjectId') as string | null;
41-
const defaultDepartmentId = formData.get('defaultDepartmentId') as string | null;
40+
const projectId = formData.get('projectId') as string | null;
41+
const departmentId = formData.get('departmentId') as string | null;
4242
const result = await createAgentKey(supabase, {
4343
name: parsed.data.name,
4444
role: parsed.data.role,
4545
specialPrompt: parsed.data.specialPrompt,
4646
createdBy: user.id,
4747
workspaceId: workspace.id,
48-
defaultProjectId: defaultProjectId ?? null,
49-
defaultDepartmentId: defaultDepartmentId ?? null,
48+
projectId: projectId ?? null,
49+
departmentId: departmentId ?? null,
5050
});
5151

5252
revalidatePath('/agent-keys');
@@ -78,10 +78,10 @@ export async function updateAgentKeyAction(formData: FormData) {
7878
const isActive = formData.get('isActive');
7979
if (isActive !== null) raw.isActive = isActive === 'true';
8080

81-
const defaultProjectId = formData.get('defaultProjectId');
82-
if (defaultProjectId !== null) raw.defaultProjectId = defaultProjectId || null;
83-
const defaultDepartmentId = formData.get('defaultDepartmentId');
84-
if (defaultDepartmentId !== null) raw.defaultDepartmentId = defaultDepartmentId || null;
81+
const projectId = formData.get('projectId');
82+
if (projectId !== null) raw.projectId = projectId || null;
83+
const departmentId = formData.get('departmentId');
84+
if (departmentId !== null) raw.departmentId = departmentId || null;
8585

8686
const parsed = agentKeyUpdateSchema.safeParse(raw);
8787
if (!parsed.success) {
@@ -94,11 +94,8 @@ export async function updateAgentKeyAction(formData: FormData) {
9494
name: parsed.data.name,
9595
specialPrompt: parsed.data.specialPrompt,
9696
isActive: parsed.data.isActive,
97-
defaultProjectId: (parsed.data as Record<string, unknown>).defaultProjectId as
98-
| string
99-
| null
100-
| undefined,
101-
defaultDepartmentId: (parsed.data as Record<string, unknown>).defaultDepartmentId as
97+
projectId: (parsed.data as Record<string, unknown>).projectId as string | null | undefined,
98+
departmentId: (parsed.data as Record<string, unknown>).departmentId as
10299
| string
103100
| null
104101
| undefined,

src/app/api/agent-keys/[id]/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export async function PATCH(req: Request, { params }: { params: Promise<{ id: st
5757
name?: string;
5858
specialPrompt?: string | null;
5959
isActive?: boolean;
60-
defaultProjectId?: string | null;
61-
defaultDepartmentId?: string | null;
60+
projectId?: string | null;
61+
departmentId?: string | null;
6262
};
6363

6464
const workspace = await getWorkspaceForUser(supabase);
@@ -67,8 +67,8 @@ export async function PATCH(req: Request, { params }: { params: Promise<{ id: st
6767
name: body.name,
6868
specialPrompt: body.specialPrompt,
6969
isActive: body.isActive,
70-
defaultProjectId: body.defaultProjectId,
71-
defaultDepartmentId: body.defaultDepartmentId,
70+
projectId: body.projectId,
71+
departmentId: body.departmentId,
7272
actorId: user.id,
7373
workspaceId: workspace.id,
7474
});

src/lib/services/agent-keys.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function listAgentKeys(
3131
const { data, error } = await supabase
3232
.from('agent_keys')
3333
.select(
34-
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, default_project_id, default_department_id',
34+
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, project_id, department_id',
3535
)
3636
.order('created_at', { ascending: false });
3737

@@ -47,8 +47,8 @@ export async function createAgentKey(
4747
specialPrompt?: string | null;
4848
createdBy: string;
4949
workspaceId: string;
50-
defaultProjectId?: string | null;
51-
defaultDepartmentId?: string | null;
50+
projectId?: string | null;
51+
departmentId?: string | null;
5252
},
5353
): Promise<{ key: Omit<AgentKey, 'key_hash'>; fullKey: string }> {
5454
const keyId = crypto.randomUUID();
@@ -65,11 +65,11 @@ export async function createAgentKey(
6565
special_prompt: params.specialPrompt ?? null,
6666
created_by: params.createdBy,
6767
workspace_id: params.workspaceId,
68-
default_project_id: params.defaultProjectId ?? null,
69-
default_department_id: params.defaultDepartmentId ?? null,
68+
project_id: params.projectId ?? null,
69+
department_id: params.departmentId ?? null,
7070
})
7171
.select(
72-
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, default_project_id, default_department_id',
72+
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, project_id, department_id',
7373
)
7474
.single();
7575

@@ -98,8 +98,8 @@ export async function updateAgentKey(
9898
name?: string;
9999
specialPrompt?: string | null;
100100
isActive?: boolean;
101-
defaultProjectId?: string | null;
102-
defaultDepartmentId?: string | null;
101+
projectId?: string | null;
102+
departmentId?: string | null;
103103
actorId: string;
104104
workspaceId: string;
105105
},
@@ -108,20 +108,19 @@ export async function updateAgentKey(
108108
if (params.name !== undefined) updates.name = params.name;
109109
if (params.specialPrompt !== undefined) updates.special_prompt = params.specialPrompt;
110110
if (params.isActive !== undefined) updates.is_active = params.isActive;
111-
if (params.defaultProjectId !== undefined) {
112-
updates.default_project_id = params.defaultProjectId;
111+
if (params.projectId !== undefined) {
112+
updates.project_id = params.projectId;
113113
// Clearing project must also clear department (DB CHECK constraint)
114-
if (params.defaultProjectId === null) updates.default_department_id = null;
114+
if (params.projectId === null) updates.department_id = null;
115115
}
116-
if (params.defaultDepartmentId !== undefined)
117-
updates.default_department_id = params.defaultDepartmentId;
116+
if (params.departmentId !== undefined) updates.department_id = params.departmentId;
118117

119118
const { data, error } = await supabase
120119
.from('agent_keys')
121120
.update(updates)
122121
.eq('id', params.id)
123122
.select(
124-
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, default_project_id, default_department_id',
123+
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, project_id, department_id',
125124
)
126125
.single();
127126

@@ -154,7 +153,7 @@ export async function rotateAgentKey(
154153
.update({ key_hash: hash, key_prefix: prefix })
155154
.eq('id', params.id)
156155
.select(
157-
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, default_project_id, default_department_id',
156+
'id, name, role, key_prefix, is_active, special_prompt, created_at, last_used_at, created_by, project_id, department_id',
158157
)
159158
.single();
160159

src/lib/types/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export interface AgentKey {
100100
last_used_at: string | null;
101101
created_by: string;
102102
workspace_id: string;
103-
default_project_id: string | null;
104-
default_department_id: string | null;
103+
project_id: string | null;
104+
department_id: string | null;
105105
}
106106

107107
export interface AgentPermission {

src/lib/utils/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export const agentKeyUpdateSchema = z.object({
6464
name: z.string().min(1).max(100).optional(),
6565
specialPrompt: z.string().max(5000).optional().nullable(),
6666
isActive: z.boolean().optional(),
67-
defaultProjectId: z.uuid().optional().nullable(),
68-
defaultDepartmentId: z.uuid().optional().nullable(),
67+
projectId: z.uuid().optional().nullable(),
68+
departmentId: z.uuid().optional().nullable(),
6969
});
7070

7171
export const permissionSchema = z.object({

supabase/functions/_shared/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ export async function authenticateAgent(
131131
specialPrompt: keyRecord.special_prompt,
132132
permissions,
133133
workspaceId: keyRecord.workspace_id,
134-
defaultProjectId: keyRecord.default_project_id,
135-
defaultDepartmentId: keyRecord.default_department_id,
134+
projectId: keyRecord.project_id,
135+
departmentId: keyRecord.department_id,
136136
}
137137
}
138138

supabase/functions/_shared/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export interface AgentContext {
3232
specialPrompt: string | null
3333
permissions: AgentPermission[]
3434
workspaceId: string
35-
defaultProjectId: string | null
36-
defaultDepartmentId: string | null
35+
projectId: string | null
36+
departmentId: string | null
3737
}
3838

3939
export interface AgentKeyRecord {
@@ -48,6 +48,6 @@ export interface AgentKeyRecord {
4848
last_used_at: string | null
4949
created_by: string
5050
workspace_id: string
51-
default_project_id: string | null
52-
default_department_id: string | null
51+
project_id: string | null
52+
department_id: string | null
5353
}

supabase/functions/mcp-server/schema/dynamic-schema.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ export async function createMcpServerForAgent(
5757
.map(([slug, depts]) => `Departments for ${slug}: ${depts.join(', ')}`)
5858
.join('; ')
5959

60-
// Default project: stored default overrides computed (F5b → F5a fallback)
61-
const defaultProject = ctx.defaultProjectId
62-
? projectSlugs.find(s => activePerms.find(p => p.projectSlug === s)?.projectId === ctx.defaultProjectId)
60+
// Agent's home project: stored value overrides computed (single-project fallback)
61+
const defaultProject = ctx.projectId
62+
? projectSlugs.find(s => activePerms.find(p => p.projectSlug === s)?.projectId === ctx.projectId)
6363
: (projectSlugs.length === 1 ? projectSlugs[0] : undefined)
6464

65-
// Default department: stored default overrides computed (F5b → F5a fallback)
66-
const defaultDept = ctx.defaultDepartmentId
65+
// Agent's home department: stored value overrides computed (single-dept fallback)
66+
const defaultDept = ctx.departmentId
6767
? (() => {
68-
const dp = activePerms.find(p => p.departmentId === ctx.defaultDepartmentId && p.projectId === ctx.defaultProjectId && !p.isDepartmentArchived)
68+
const dp = activePerms.find(p => p.departmentId === ctx.departmentId && p.projectId === ctx.projectId && !p.isDepartmentArchived)
6969
return dp?.departmentSlug ?? undefined
7070
})()
7171
: (defaultProject && deptsByProject[defaultProject]?.length === 1
@@ -198,8 +198,8 @@ export async function createMcpServerForAgent(
198198
role: z.enum(['worker', 'manager']).optional().describe('Agent role (for create/update)'),
199199
special_prompt: z.string().optional().describe('Special system prompt (for create/update)'),
200200
is_active: z.boolean().optional().describe('Active status (for update)'),
201-
default_project_id: z.string().optional().describe('Default project UUID (for create/update)'),
202-
default_department_id: z.string().optional().describe('Default department UUID (for create/update, requires default_project_id)'),
201+
project_id: z.string().optional().describe('Project UUID (for create/update)'),
202+
department_id: z.string().optional().describe('Department UUID (for create/update, requires project_id)'),
203203
limit: z.number().max(50).optional().describe('Max results for list action (default 20, max 50)'),
204204
cursor: z.string().optional().describe('Pagination cursor for list action'),
205205
},

supabase/functions/mcp-server/tools/get-tasks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export async function handleGetTasks(
5757

5858
// 3. Resolve department: explicit param → agent default → null (all)
5959
const effectiveDept = params.department
60-
?? (ctx.defaultDepartmentId && projectPerms.some((p) => p.departmentId === ctx.defaultDepartmentId)
61-
? ctx.defaultDepartmentId
60+
?? (ctx.departmentId && projectPerms.some((p) => p.departmentId === ctx.departmentId)
61+
? ctx.departmentId
6262
: undefined)
6363
let departmentId: string | null = null
6464
if (effectiveDept) {

0 commit comments

Comments
 (0)