LifeMate — Personal, Family & Learning Companion
feat(identity): add synthetic child consent and invitation schema
Quinn White committed
Oct 16, 2026 at 06:51 UTC
6050a422598c3a575bf5a1f8a49fbfe262de843d
6 files changed
+169
-5
backend/migrations/0024_child_device_safety.sql
new
+67
@@ -0,0 +1,67 @@
1
+begin;
2
+
3
+-- Synthetic-only child identity foundation. Runtime gate is mandatory and never
4
+-- permits production. This migration does not approve legal consent or data hosting.
5
+alter table app_user add column is_managed_child boolean not null default false;
6
+alter table app_user drop constraint app_user_contact_required;
7
+alter table app_user add constraint app_user_contact_required
8
+ check (email_normalized is not null or phone_normalized is not null or is_managed_child);
9
+alter table app_user add constraint managed_child_has_no_contact
10
+ check (not is_managed_child or (email_normalized is null and phone_normalized is null));
11
+
12
+alter table auth_session add column access_scope text not null default 'standard'
13
+ check (access_scope in ('standard','child_education'));
14
+alter table auth_session add column child_issuer_user_id uuid references app_user(id);
15
+
16
+create table managed_child (
17
+ user_id uuid primary key references app_user(id) on delete cascade,
18
+ family_id uuid not null references family_workspace(id) on delete cascade,
19
+ creator_user_id uuid not null references app_user(id),
20
+ created_at timestamptz not null default clock_timestamp(),
21
+ unique (family_id,user_id),
22
+ check (user_id<>creator_user_id)
23
+);
24
+
25
+create table child_consent (
26
+ child_user_id uuid primary key references managed_child(user_id) on delete cascade,
27
+ actor_user_id uuid not null references app_user(id),
28
+ text_version text not null check (text_version='sandbox-child-consent-v1-not-legal'),
29
+ text_digest text not null check (text_digest ~ '^[a-f0-9]{64}$'),
30
+ accepted_at timestamptz not null default clock_timestamp(),
31
+ revoked_at timestamptz
32
+);
33
+
34
+create table child_device_invitation (
35
+ id uuid primary key default gen_random_uuid(),
36
+ family_id uuid not null,
37
+ child_user_id uuid not null,
38
+ issuer_user_id uuid not null references app_user(id),
39
+ token_digest text not null unique check (token_digest ~ '^[a-f0-9]{64}$'),
40
+ created_at timestamptz not null default clock_timestamp(),
41
+ expires_at timestamptz not null,
42
+ used_at timestamptz,
43
+ revoked_at timestamptz,
44
+ foreign key (family_id,child_user_id) references managed_child(family_id,user_id) on delete cascade
45
+);
46
+create index child_device_invitation_child_idx on child_device_invitation(child_user_id)
47
+ where used_at is null and revoked_at is null;
48
+
49
+create table guardian_invitation (
50
+ id uuid primary key default gen_random_uuid(),
51
+ family_id uuid not null,
52
+ child_user_id uuid not null,
53
+ issuer_user_id uuid not null references app_user(id),
54
+ guardian_user_id uuid not null references app_user(id),
55
+ token_digest text not null unique check (token_digest ~ '^[a-f0-9]{64}$'),
56
+ created_at timestamptz not null default clock_timestamp(),
57
+ expires_at timestamptz not null,
58
+ used_at timestamptz,
59
+ revoked_at timestamptz,
60
+ foreign key (family_id,child_user_id) references managed_child(family_id,user_id) on delete cascade,
61
+ check (guardian_user_id<>issuer_user_id and guardian_user_id<>child_user_id)
62
+);
63
+create index guardian_invitation_child_idx on guardian_invitation(child_user_id)
64
+ where used_at is null and revoked_at is null;
65
+
66
+revoke all on managed_child,child_consent,child_device_invitation,guardian_invitation from public;
67
+commit;
backend/migrations/0025_managed_guardian_authority.sql
new
+37
@@ -0,0 +1,37 @@
1
+begin;
2
+
3
+-- Preserve legacy role/membership checks. Only the new sandbox model requires
4
+-- versioned consent and a creator-authorized, accepted second-guardian invite.
5
+create or replace function is_active_guardian(p_family uuid, p_guardian uuid, p_minor uuid) returns boolean
6
+language sql stable as $$
7
+ select exists(
8
+ select 1 from guardian_relationship g
9
+ join family_workspace f on f.id=g.family_id and f.archived_at is null
10
+ join family_membership gm on gm.family_id=g.family_id and gm.user_id=g.guardian_user_id
11
+ and gm.ended_at is null and gm.role='parent_guardian'
12
+ join family_membership mm on mm.family_id=g.family_id and mm.user_id=g.minor_user_id
13
+ and mm.ended_at is null and mm.role='teen_minor'
14
+ join app_user gu on gu.id=gm.user_id and gu.disabled_at is null
15
+ join app_user cu on cu.id=mm.user_id and cu.disabled_at is null
16
+ where g.family_id=p_family and g.guardian_user_id=p_guardian
17
+ and g.minor_user_id=p_minor and g.active
18
+ and (not cu.is_managed_child or exists(
19
+ select 1 from managed_child mc
20
+ join child_consent cc on cc.child_user_id=mc.user_id and cc.revoked_at is null
21
+ and cc.actor_user_id=mc.creator_user_id
22
+ and cc.text_version='sandbox-child-consent-v1-not-legal'
23
+ and cc.text_digest='5dbf640a23b538800c82791620c3a43947b7346d7eff179e6f7d5afe5a55b387'
24
+ join profile gp on gp.user_id=g.guardian_user_id
25
+ and gp.birth_date<=(current_date-interval '18 years')::date
26
+ join profile cp on cp.user_id=mc.user_id
27
+ and cp.birth_date<=current_date and cp.birth_date>(current_date-interval '18 years')::date
28
+ where mc.user_id=g.minor_user_id and mc.family_id=g.family_id and not mm.is_admin
29
+ and (g.guardian_user_id=mc.creator_user_id or exists(
30
+ select 1 from guardian_invitation gi where gi.family_id=mc.family_id
31
+ and gi.child_user_id=mc.user_id and gi.issuer_user_id=mc.creator_user_id
32
+ and gi.guardian_user_id=g.guardian_user_id and gi.used_at is not null and gi.revoked_at is null
33
+ ))
34
+ ))
35
+ )
36
+$$;
37
+commit;
backend/scripts/build-supabase-qa-sql.js
+8
-3
@@ -1,4 +1,4 @@
1
-// Build a one-time, QA-only copy of the 20 baseline migrations plus auth metadata.
1
+// Build a one-time, QA-only copy of the baseline plus reviewed additive DDL.
2
// Existing provider-applied 0001..0020 bundles remain historical and unchanged.
3
// This script outputs SQL, does NOT connect to a DB and cannot deploy by itself.
4
// Crucially, original migration files / Compose role-provisioning stay unchanged.
@@ -40,6 +40,10 @@ function normalizeMigration(filename, source) {
40
sql = sql.replace(/^revoke all on auth_ip_rate_limit from public;$/m,
41
'-- Auth metadata is included in the QA wrapper table-wide lockdown.');
42
}
43
+ if (filename === '0024_child_device_safety.sql') {
44
+ sql = sql.replace(/^revoke all on managed_child,child_consent,child_device_invitation,guardian_invitation from public;$/m,
45
+ '-- Child identity tables are included in the QA wrapper lockdown.');
46
+ }
47
48
// Fail closed if a future source migration changes its privilege/schema rules.
49
if (/\b(?:public|auth|storage|realtime)\s*\./i.test(sql) ||
@@ -53,10 +57,11 @@ function normalizeMigration(filename, source) {
57
58
export async function buildSupabaseQaSql({ migrationsDir = sourceRoot } = {}) {
59
const entries = (await readdir(migrationsDir)).filter(name => /^\d{4}_[a-z0-9_]+\.sql$/.test(name)).sort();
56
- const expected = [...Array.from({length:20}, (_,i) => String(i+1).padStart(4,'0')), '0023'];
60
+ const expected = [...Array.from({length:20}, (_,i) => String(i+1).padStart(4,'0')), '0023', '0024', '0025'];
61
if (entries.length !== expected.length ||
62
entries.some((name, i) => name.slice(0,4) !== expected[i]) ||
59
- entries.at(-1) !== '0023_auth_ip_rate_limit.sql') {
63
+ entries[20] !== '0023_auth_ip_rate_limit.sql' || entries[21] !== '0024_child_device_safety.sql' ||
64
+ entries[22] !== '0025_managed_guardian_authority.sql') {
65
throw new Error('expected_baseline_and_auth_migrations_in_order');
66
}
67
backend/scripts/db-role-policy.js
+1
@@ -8,6 +8,7 @@ export const applicationTables = [
8
'ai_guide_message', 'ai_guide_session', 'ai_plan_proposal', 'app_user',
9
'auth_credential', 'auth_delivery_attempt', 'auth_ip_rate_limit', 'auth_phone_challenge', 'auth_session',
10
'auth_token', 'class_session', 'curriculum_subject', 'education_profile',
11
+ 'managed_child', 'child_consent', 'child_device_invitation', 'guardian_invitation',
12
'family_invitation', 'family_membership', 'family_workspace', 'guardian_relationship',
13
'iran_calendar_event', 'learning_checkin', 'learning_goal', 'life_context',
14
'learning_goal_mutation', 'family_lifecycle_mutation', 'school_lifecycle_mutation',
backend/tests/supabase_qa_bundle.test.js
+6
-2
@@ -6,11 +6,13 @@ import { join } from 'node:path';
6
import { pathToFileURL } from 'node:url';
7
import { buildSupabaseQaSql } from '../scripts/build-supabase-qa-sql.js';
8
9
-test('QA bundle retains 20 original checksums plus additive auth metadata and keeps private lockdown', async () => {
9
+test('QA bundle retains original checksums plus additive identity migrations and keeps private lockdown', async () => {
10
const { sql, manifest } = await buildSupabaseQaSql();
11
- assert.equal(manifest.length, 21);
11
+ assert.equal(manifest.length, 23);
12
assert.ok(manifest.slice(0,20).every((x,i) => x.filename.startsWith(String(i+1).padStart(4,'0')+'_')));
13
assert.equal(manifest[20].filename, '0023_auth_ip_rate_limit.sql');
14
+ assert.equal(manifest[21].filename, '0024_child_device_safety.sql');
15
+ assert.equal(manifest[22].filename, '0025_managed_guardian_authority.sql');
16
assert.match(sql, /create table auth_ip_rate_limit/);
17
assert.ok(manifest.every(x => /^[a-f0-9]{64}$/.test(x.sha256)));
18
assert.match(sql, /set local search_path=lifeguide_qa,extensions,pg_catalog/);
@@ -46,6 +48,8 @@ test('QA bundle generation rejects drift adding production schema privileges', a
48
await writeFile(join(dir,`${id}_test.sql`),sql);
49
}
50
await writeFile(join(dir,'0023_auth_ip_rate_limit.sql'), 'select 1;');
51
+ await writeFile(join(dir,'0024_child_device_safety.sql'), 'select 1;');
52
+ await writeFile(join(dir,'0025_managed_guardian_authority.sql'), 'select 1;');
53
await writeFile(join(dir,'0004_test.sql'),'create table public.account(id int);');
54
await assert.rejects(buildSupabaseQaSql({ migrationsDir:pathToFileURL(dir+'/') }),/source_migration_outside_private_qa:0004_test.sql/);
55
} finally { await rm(dir,{recursive:true,force:true}); }
docs/decisions/2026-10-16-child-identity-sandbox.md
new
+50
@@ -0,0 +1,50 @@
1
+# هویت و سرپرستی کودک در محیط آزمایشی
2
+
3
+**وضعیت: EXPERIMENTAL — پیادهسازی و آزمون در جریان؛ نتایج این بسته UNVERIFIED تا ثبت خروجی اجرا.**
4
+
5
+دستور «ادامه بده» پس از پیشنهاد مشخص C-A/E-A/R-A، مجوز ادامهٔ نمونهٔ آزمایشی آن طرح تلقی میشود. این سند پذیرش متن حقوقی رضایت، تأیید هویت یا سن واقعی، مجوز استفاده از دادهٔ واقعی، انتخاب زیرساخت، merge یا deploy نیست. گزینههای AI، نگهداری داده و نشست وب همچنان تصمیمهای مستقل و تأییدنشدهاند.
6
+
7
+## دامنهٔ مجاز
8
+
9
+فقط حسابها و خانوادههای ساختگی روی PostgreSQL محلی برای توسعه و آزمون. فعالسازی مستلزم مقدار دقیق `ENABLE_CHILD_IDENTITY_SANDBOX=true` و یکی از `NODE_ENV=test` یا `NODE_ENV=development` است. سایر حالتها، از جمله `production` حتی با flag فعال، باید برای مسیرهای این قابلیت HTTP503 بدهند. این کنترل، رفتار API است؛ بهتنهایی تضمین نمیکند اپراتور دادهٔ واقعی وارد نکرده باشد. هیچ دادهٔ کودک واقعی، SMS، ایمیل یا AI خارجی در این نمونه مجاز نیست.
10
+
11
+پیشفرض غیرفعال است. migration0024 صرفاً مبنای schema محلی است؛ وجود فایل یا آزمایش محلی بهمعنای اعمال آن روی Supabase یا سامانهٔ زنده نیست. سامانهٔ فعلی تولید همچنان خارج از این بسته است.
12
+
13
+## طرح C-A / E-A / R-A
14
+
15
+- **C-A — ایجاد و سرپرست دوم:** والد بالغِ عضو و مدیر فعال خانواده پروفایل کودک را میسازد؛ سازنده در همان تراکنش سرپرست نخست میشود. سرپرست دوم به دعوت مشخص سازنده و پذیرش صریح همان گیرنده نیاز دارد. مدیر بودن بهتنهایی اجازهٔ افزودن خود یا شخص دیگر به سرپرستی کودک را نمیدهد. مسیر قدیمی `/guardians` نباید این جریان را برای کودک مدیریتشده دور بزند.
16
+- **E-A — سن:** تاریخ تولد کامل و معتبر برای کودک جدید اجباری است؛ سن در سرور محاسبه میشود و این نمونه فقط زیر۱۸سال را میپذیرد. والد ایجادکننده نیز باید تاریخ تولد ثبتشده و سن بالغ داشته باشد. این داده خوداظهاری است و احراز قانونی سن نیست. تاریخ تولد کودک مدیریتشده در API عمومی قابل تغییر نیست؛ مسیر اصلاح خطای واقعی و گذار به بزرگسالی بعداً با تصمیم جدا طراحی میشود.
17
+- **R-A — دستگاه و بازیابی:** دعوت/بازیابی دستگاه، نشستها و دعوتهای قبلی کودک را پیش از صدور جایگزین باطل میکند. هیچ نشست صادرشده از این مسیر، حتی نخستین ورود، دسترسی کامل به حساب خصوصی کودک ندارد. بازیابی مستقل اطلاعات خصوصی کودک خارج از دامنه است.
18
+
19
+نوع پروفایل (`girl_minor` یا `boy_minor`) مستقل از نقش عضویت `teen_minor` و رابطهٔ سرپرستی است؛ هیچکدام جای دیگری را نمیگیرد. کودک مدیریتشده بدون ایمیل، موبایل و رمز ساخته میشود؛ این استثنا فقط با marker سروری `is_managed_child` مجاز است و ورودی عمومی ثبتنام نمیتواند آن را انتخاب کند.
20
+
21
+## دعوت و ثبت رضایتِ ساختگی
22
+
23
+دعوت دستگاه و دعوت سرپرست توکن تصادفیِ تکبارمصرف دارند؛ پایگاه داده فقط digest را نگه میدارد. پاسخ حاوی توکن `no-store` است؛ توکن نباید به لاگ، analytics یا پارامتر URL منتقل شود. QR واقعی/اشتراکگذاری و UI در این بسته پیادهسازی نمیشود. شناسهٔ خانواده، کودک، صادرکننده و در دعوت سرپرست گیرنده به رکورد دعوت متصل است. پذیرش باید پس از قفل دوباره انقضا، عضویت، اختیار صادرکننده و وضعیت رضایت را بخواند؛ تغییر اختیار هنگام انتظار قفل نباید پذیرفته شود.
24
+
25
+اعتبار ثابت **۱۰دقیقه** صرفاً پارامتر این sandbox برای آزمون است؛ سیاست پذیرفتهشدهٔ دعوت در تولید نیست. نمایش یک QR بهتنهایی اثبات رابطهٔ والد/کودک نیست.
26
+
27
+نسخهٔ `sandbox-child-consent-v1-not-legal` و هش متن ساختگی همراه actor و زمان ثبت میشوند. `synthetic=true` و پذیرش صریح همین نسخه لازم است. این رکورد **رضایت حقوقی نیست**؛ نباید به کاربر واقعی بهعنوان رضایت معتبر ارائه شود. لغو بهوسیلهٔ سازنده، نشستها، دعوتهای باز و روابط سرپرستی همین کودک را در تراکنش قطع میکند؛ حذف/نگهداری دادهٔ موجود تصمیم جدا میخواهد.
28
+
29
+## جلوگیری از ورود سرپرست به فضای خصوصی کودک
30
+
31
+marker حساب در DB و `auth_session.access_scope=child_education` مرجع مجوز هستند؛ ادعای JWT نمیتواند scope را گسترش دهد. صادرکنندهٔ نشست نیز ثبت و اعتبار فعلی او در سرور بررسی میشود. نشست مدیریتشدهٔ فاقد scope صحیح، نشست لغوشده/منقضی یا رابطه/رضایت نامعتبر باید رد شود؛ refresh نباید scope را به `standard` تبدیل کند. غیرفعالسازی sandbox نباید مسیر جایگزین ورود کودک ایجاد کند.
32
+
33
+allowlist باریک فقط برای خواندن پروفایل خود، فهرست خانوادهٔ مجاز، صفحهٔ «والد چه چیزی میبیند»، کاتالوگ/واحد/ارزیابی منتشرشدهٔ آموزشی و تقویم عمومی است. refresh/logout با کنترل مجدد DB مسیرهای چرخهٔ نشستاند، نه دسترسی محتوایی. فهرست مجاز صریح است؛ مسیرهای دیگر بهصورت پیشفرض رد میشوند. عضویت/نقش/پروفایل، endpoint قدیمی سرپرست و سایر مسیرهای عمومی نباید marker یا scope را دور بزنند.
34
+
35
+**محدودیت عمدی این نمونه:** planner خصوصی، مطالعه/تایمر، هدف، پاسخ و سابقهٔ آزمون، یادداشت، چت AI، wellbeing و سلامت/قاعدگی از این نشستها قابل استفاده نیستند. ابتدا باید در فاز۲ هر مسیر آموزشی با مجوز محدود خودش طراحی و آزموده شود. این نمونه هنوز مسیر کامل تکلیف←مطالعه←آزمون←گزارش والد یا نرمافزار قابل استفادهٔ خانواده نیست. دامنهٔ آموزشی صرفاً نام scope است و مجوز تمام دادههای آموزشی نیست.
36
+
37
+صفحهٔ دیدپذیری باید سرپرستان فعال و مرزهای دسترسی را به کودک نشان دهد. پاسخ آن به والد اجازهٔ دیدن گفتوگو، یادداشت، سلامت یا جزئیات خصوصی جدیدی نمیدهد. audit ساختگی با شناسه و نوع عملیات ثبت میشود؛ توکن، تاریخ تولد، نام و متن خصوصی در metadata ثبت نمیشوند. ممیزی همهٔ مشاهدههای سایر APIها همچنان کار مستقل فاز۳ است.
38
+
39
+## معیار پذیرش و حدود شواهد
40
+
41
+اجرای موارد زیر برای همین head باید جداگانه با دستور و خروجی در گزارش تحویل ثبت شود؛ این سند خود گواه موفقیت آنها نیست:
42
+
43
+1. flag پیشفرض خاموش؛ production و محیط نامشخص حتی با flag فعال HTTP503.
44
+2. ایجاد اتمیِ کودک، سن و رضایت ساختگی؛ رد ورودی نامعتبر، والد نامجاز/سن نامعلوم و خانوادهٔ دیگر.
45
+3. دعوت دومرحلهای سرپرست، رد گیرندهٔ اشتباه، تکبارمصرفی و انقضا پس از قفل واقعی DB.
46
+4. بازیابی با ابطال نشست/دعوت قبلی؛ قطع دسترسی پس از لغو رضایت/عضویت/اختیار و حفظ scope در refresh.
47
+5. رد همهٔ مسیرهای خصوصی و تغییر marker/DOB/scope از APIهای قدیمی؛ JWT جعلی با claim گسترده مجوز نمیآورد.
48
+6. خواندن مستقل DB برای digest، scope و audit؛ عدم ذخیرهٔ خود توکن یا دادهٔ حساس در لاگ.
49
+
50
+**UNVERIFIED در این ADR:** نتایج آزمون این پیادهسازی، UI/QR، Android و Safari واقعی، SMS کاوهنگار، کارایی تولید، رضایت حقوقی، دسترسی ایران و اعمال migration زنده. هیچیک از این موارد با سبزشدن تستهای قبلی هویت تأیید نمیشود.