AI Engineering Workspace
PartialChat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.
Project files
read-onlyapp/src/lib/rls.sql.mjs
1// PostgreSQL row-level security (v0.7.0). Idempotent; executed by the MIGRATION role only. The runtime role (studio_app) is a non-owner2// without BYPASSRLS, so these policies bind every request-handling query. Context is transaction-local: set_config(..., true).3// app.org_id : tenant identity. app.mode : tenant | platform | demo | system. Unset/empty => no policy matches => fail closed.4export const RLS_SQL = `5CREATE OR REPLACE FUNCTION app_org() RETURNS text LANGUAGE sql STABLE AS $f$ SELECT NULLIF(current_setting('app.org_id', true), '') $f$;6CREATE OR REPLACE FUNCTION app_mode() RETURNS text LANGUAGE sql STABLE AS $f$ SELECT COALESCE(NULLIF(current_setting('app.mode', true), ''), 'none') $f$;7ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS org_id TEXT;8UPDATE audit_log SET org_id = substring(detail from '"org":"([A-Za-z0-9_-]+)"') WHERE org_id IS NULL AND detail LIKE '%"org":"%';9DO $do$10DECLARE t text; p text;11BEGIN12 -- content tables: only the owning tenant (or an explicitly authorized system operation)13 FOREACH t IN ARRAY ARRAY['projects','ai_sessions','credentials','usage_ledger','node_jobs','app_deployments','app_releases','domains'] LOOP14 EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t);15 EXECUTE format('DROP POLICY IF EXISTS rls_all ON %I', t);16 EXECUTE format($p$CREATE POLICY rls_all ON %I USING (app_mode()='system' OR (app_mode() IN ('tenant','platform','demo') AND org_id = app_org())) WITH CHECK (app_mode()='system' OR (app_mode() IN ('tenant','platform') AND org_id = app_org()))$p$, t);17 END LOOP;18 -- tenant-visible metadata that platform administrators may also read/administer (never secrets/content)19 FOREACH t IN ARRAY ARRAY['instances','nodes','provisioning_requests','usage_records','memberships'] LOOP20 EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t);21 EXECUTE format('DROP POLICY IF EXISTS rls_all ON %I', t);22 EXECUTE format($p$CREATE POLICY rls_all ON %I USING (app_mode() IN ('system','platform') OR (app_mode() IN ('tenant','demo') AND org_id = app_org())) WITH CHECK (app_mode() IN ('system','platform') OR (app_mode()='tenant' AND org_id = app_org()))$p$, t);23 END LOOP;24 -- children of tenant-owned parents: visibility inherited from the (RLS-filtered) parent row25 FOREACH p IN ARRAY ARRAY['tasks:project_id:projects','milestones:project_id:projects','workspace_locks:project_id:projects','ai_messages:session_id:ai_sessions','ai_runs:session_id:ai_sessions','ai_checkpoints:run_id:ai_runs'] LOOP26 t := split_part(p, ':', 1);27 EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t);28 EXECUTE format('DROP POLICY IF EXISTS rls_all ON %I', t);29 EXECUTE format($p$CREATE POLICY rls_all ON %I USING (app_mode()='system' OR EXISTS (SELECT 1 FROM %I par WHERE par.id = %I.%I)) WITH CHECK (app_mode()='system' OR (app_mode()<>'demo' AND EXISTS (SELECT 1 FROM %I par WHERE par.id = %I.%I)))$p$,30 t, split_part(p, ':', 3), t, split_part(p, ':', 2), split_part(p, ':', 3), t, split_part(p, ':', 2));31 END LOOP;32END $do$;3334ALTER TABLE organizations ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON organizations;35CREATE POLICY rls_all ON organizations USING (app_mode() IN ('system','platform') OR (app_mode() IN ('tenant','demo') AND id = app_org())) WITH CHECK (app_mode() IN ('system','platform'));36ALTER TABLE users ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON users;37CREATE POLICY rls_all ON users USING (app_mode() IN ('system','platform') OR (app_mode()='tenant' AND EXISTS (SELECT 1 FROM memberships m WHERE m.user_id = users.id))) WITH CHECK (app_mode() IN ('system','platform'));38-- identity / federation / central bookkeeping: system-only (callback, back-channel logout, administrative linking)39DO $do2$ DECLARE t text; BEGIN40 FOREACH t IN ARRAY ARRAY['external_identities','oidc_requests','central_outbox','user_credentials'] LOOP41 EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t); EXECUTE format('DROP POLICY IF EXISTS rls_all ON %I', t);42 EXECUTE format($p$CREATE POLICY rls_all ON %I USING (app_mode()='system') WITH CHECK (app_mode()='system')$p$, t);43 END LOOP;44 FOREACH t IN ARRAY ARRAY['invitations','org_mappings','app_entitlements'] LOOP45 EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t); EXECUTE format('DROP POLICY IF EXISTS rls_all ON %I', t);46 EXECUTE format($p$CREATE POLICY rls_all ON %I USING (app_mode() IN ('system','platform') OR (app_mode()='tenant' AND org_id = app_org())) WITH CHECK (app_mode() IN ('system','platform') OR (app_mode()='tenant' AND org_id = app_org()))$p$, t);47 END LOOP;48 FOREACH t IN ARRAY ARRAY['acceptance','acceptance_bugs'] LOOP49 EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t); EXECUTE format('DROP POLICY IF EXISTS rls_all ON %I', t);50 EXECUTE format($p$CREATE POLICY rls_all ON %I USING (app_mode() IN ('system','platform')) WITH CHECK (app_mode() IN ('system','platform'))$p$, t);51 END LOOP;52END $do2$;53ALTER TABLE sessions ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON sessions;54CREATE POLICY rls_all ON sessions USING (app_mode()='system') WITH CHECK (app_mode()='system');55ALTER TABLE settings ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_read ON settings; DROP POLICY IF EXISTS rls_write ON settings; DROP POLICY IF EXISTS rls_all ON settings;56CREATE POLICY rls_read ON settings FOR SELECT USING (app_mode() <> 'none');57CREATE POLICY rls_write ON settings FOR ALL USING (app_mode() IN ('system','platform')) WITH CHECK (app_mode() IN ('system','platform'));58ALTER TABLE resource_plans ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_read ON resource_plans; DROP POLICY IF EXISTS rls_write ON resource_plans;59CREATE POLICY rls_read ON resource_plans FOR SELECT USING (app_mode() <> 'none');60CREATE POLICY rls_write ON resource_plans FOR ALL USING (app_mode() IN ('system','platform')) WITH CHECK (app_mode() IN ('system','platform'));61ALTER TABLE managed_inventory ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON managed_inventory; DROP POLICY IF EXISTS rls_read ON managed_inventory; DROP POLICY IF EXISTS rls_write ON managed_inventory;62CREATE POLICY rls_read ON managed_inventory FOR SELECT USING (app_mode() IN ('system','platform') OR (app_mode()='tenant' AND assigned_org_id = app_org()));63CREATE POLICY rls_write ON managed_inventory FOR ALL USING (app_mode() IN ('system','platform')) WITH CHECK (app_mode() IN ('system','platform'));64ALTER TABLE contact_requests ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON contact_requests;65CREATE POLICY rls_all ON contact_requests USING (app_mode() IN ('system','platform')) WITH CHECK (app_mode() IN ('system','platform'));66ALTER TABLE deployments ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON deployments;67CREATE POLICY rls_all ON deployments USING (app_mode() IN ('system','platform','demo')) WITH CHECK (app_mode() IN ('system','platform'));68ALTER TABLE design_approvals ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_all ON design_approvals;69CREATE POLICY rls_all ON design_approvals USING (app_mode() <> 'none') WITH CHECK (app_mode() IN ('system','platform'));70ALTER TABLE audit_log ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS rls_sel ON audit_log; DROP POLICY IF EXISTS rls_ins ON audit_log;71CREATE POLICY rls_sel ON audit_log FOR SELECT USING (app_mode() IN ('system','platform') OR (app_mode()='tenant' AND org_id = app_org()));72CREATE POLICY rls_ins ON audit_log FOR INSERT WITH CHECK (app_mode()='system');73`;74// Role bootstrap, run by the migration role: a login role that owns nothing, cannot bypass RLS and cannot change schema.75export const roleSql = (pw) => `76DO $do$ BEGIN77 IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname='studio_app') THEN CREATE ROLE studio_app LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOBYPASSRLS NOINHERIT PASSWORD '${pw.replace(/'/g, "''")}'; END IF;78END $do$;79ALTER ROLE studio_app NOSUPERUSER NOBYPASSRLS;80GRANT CONNECT ON DATABASE studio TO studio_app; GRANT USAGE ON SCHEMA public TO studio_app;81GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO studio_app;82GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO studio_app;83REVOKE UPDATE, DELETE, TRUNCATE ON audit_log FROM studio_app;84`;85
Conversation
PartialSign in to start AI sessions.
Task checklist
- Application shell deployed
- Persistent projects
- Agent runtime
- Sandboxed terminal
Execution activity
No agent sessions. Git HEAD 1c27633 on master.
Approval requests
None pending.