Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-11 09:44:50 +03:00
commit ebb1cb7950
101 changed files with 4971 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
-- Зеркалирование: технические таблицы и зеркала JobTread (docs/mirroring.md)
create table sync_source (
prefix text primary key,
name text not null,
last_snapshot_ts timestamptz,
last_success_ts timestamptz,
last_error_count int not null default 0,
retention_policy jsonb
);
create table sync_snapshot (
started_at timestamptz primary key,
source_prefix text not null references sync_source(prefix),
finished_at timestamptz,
requested_tables text[] not null,
had_errors boolean not null default false,
error_count int not null default 0,
error_summary text
);
create table sync_table_status (
snapshot_ts timestamptz not null references sync_snapshot(started_at) on delete cascade,
table_name text not null,
status text not null check (status in ('ok','error')),
row_count int,
finished_at timestamptz,
error_message text,
primary key (snapshot_ts, table_name)
);
create index sync_table_status_table_idx on sync_table_status (table_name, snapshot_ts desc);
create table jt_job (
id text primary key,
number text,
name text not null,
status text,
closed_on date,
snapshot_ts timestamptz not null references sync_snapshot(started_at)
);
create table jt_membership (
id text primary key,
user_name text not null,
role text,
snapshot_ts timestamptz not null references sync_snapshot(started_at)
);
create table jt_task_type (
id text primary key,
name text not null,
snapshot_ts timestamptz not null references sync_snapshot(started_at)
);
create table jt_task (
id text primary key,
job_id text,
parent_task_id text,
task_type_id text,
name text not null,
description text,
start_date date,
end_date date,
progress numeric,
is_todo boolean not null default false,
is_group boolean not null default false,
is_deleted boolean not null default false,
snapshot_ts timestamptz not null references sync_snapshot(started_at)
);
create index jt_task_job_end_idx on jt_task (job_id, end_date);
create index jt_task_todo_end_idx on jt_task (is_todo, end_date);
create table jt_task_dependency (
task_id text not null,
depends_on_task_id text not null,
snapshot_ts timestamptz not null references sync_snapshot(started_at),
primary key (task_id, depends_on_task_id)
);
create table jt_task_assignment (
task_id text not null,
membership_id text not null,
snapshot_ts timestamptz not null references sync_snapshot(started_at),
primary key (task_id, membership_id)
);
-- Двойники _hist: те же колонки, PK дополнительно включает snapshot_ts, без внешних ключей
create table jt_job_hist (like jt_job);
create table jt_membership_hist (like jt_membership);
create table jt_task_type_hist (like jt_task_type);
create table jt_task_hist (like jt_task);
create table jt_task_dependency_hist (like jt_task_dependency);
create table jt_task_assignment_hist (like jt_task_assignment);
alter table jt_job_hist add primary key (id, snapshot_ts);
alter table jt_membership_hist add primary key (id, snapshot_ts);
alter table jt_task_type_hist add primary key (id, snapshot_ts);
alter table jt_task_hist add primary key (id, snapshot_ts);
alter table jt_task_dependency_hist add primary key (task_id, depends_on_task_id, snapshot_ts);
alter table jt_task_assignment_hist add primary key (task_id, membership_id, snapshot_ts);
create index jt_task_hist_snap_idx on jt_task_hist (snapshot_ts);
insert into sync_source (prefix, name) values ('jt', 'JobTread');
-- Supabase: таблицы в public доступны через REST; без политик RLS закрывает доступ anon/authenticated
alter table sync_source enable row level security;
alter table sync_snapshot enable row level security;
alter table sync_table_status enable row level security;
alter table jt_job enable row level security;
alter table jt_membership enable row level security;
alter table jt_task_type enable row level security;
alter table jt_task enable row level security;
alter table jt_task_dependency enable row level security;
alter table jt_task_assignment enable row level security;
alter table jt_job_hist enable row level security;
alter table jt_membership_hist enable row level security;
alter table jt_task_type_hist enable row level security;
alter table jt_task_hist enable row level security;
alter table jt_task_dependency_hist enable row level security;
alter table jt_task_assignment_hist enable row level security;

View File

@@ -0,0 +1,163 @@
-- Native-таблицы: люди, проекты ассистента, справочники, планировщик, пакеты входных данных, результаты агента
create table person (
id uuid primary key default gen_random_uuid(),
display_name text not null,
email text,
jt_membership_id text,
slack_user_id text,
is_active boolean not null default true,
note text
);
create table construction_manager (
person_id uuid primary key references person(id),
timezone text not null default 'America/New_York',
is_active boolean not null default true,
paused_until date
);
create table assistant_project (
id uuid primary key default gen_random_uuid(),
jt_job_id text not null unique,
manager_person_id uuid not null references construction_manager(person_id),
slack_channel_id text,
mode text not null default 'test' check (mode in ('test','prod')),
is_enabled boolean not null default true,
created_at timestamptz not null default now()
);
create table purchase_task_ref (
id uuid primary key default gen_random_uuid(),
task_name text not null,
note text,
is_verified boolean not null default false,
verified_by text,
verified_at timestamptz
);
create unique index purchase_task_ref_name_uq on purchase_task_ref (lower(task_name));
insert into purchase_task_ref (task_name, note) values
('Purchase 0', 'реальное название из JobTread 2026-10-11'),
('Purchase 0: Contract for Pilings Installation', 'реальное название из JobTread 2026-10-11'),
('Purchase 1 order start', 'реальное название из JobTread 2026-10-11'),
('Purchase 2 Start', 'реальное название из JobTread 2026-10-11'),
('Purchase 3 Start', 'реальное название из JobTread 2026-10-11'),
('Purchase 4 Start', 'реальное название из JobTread 2026-10-11'),
('Purchase 4 Delivered', 'реальное название из JobTread 2026-10-11'),
('Purchase 5 delivered', 'реальное название из JobTread 2026-10-11'),
('Start Purchase 5', 'реальное название из JobTread 2026-10-11');
create table calendar_day (
day date primary key,
is_workday boolean not null,
holiday_name text,
source text not null check (source in ('nager','manual')),
is_verified boolean not null default false,
verified_by text,
verified_at timestamptz,
loaded_at timestamptz not null default now()
);
create table reminder_history (
id uuid primary key default gen_random_uuid(),
assistant_project_id uuid not null references assistant_project(id),
jt_task_id text,
reminded_at timestamptz not null,
reminder_text text,
reply_at timestamptz,
reply_summary text,
reply_outcome text check (reply_outcome in ('done','rescheduled','no_answer','blocked','other'))
);
create table sched_run (
id uuid primary key default gen_random_uuid(),
job_name text not null,
scheduled_minute timestamptz not null,
status text not null check (status in ('started','ok','error','missed','skipped')),
noticed_at timestamptz not null default now(),
finished_at timestamptz,
http_status int,
response_ref text,
message text,
unique (job_name, scheduled_minute)
);
create table ap_input_package (
id uuid primary key default gen_random_uuid(),
assistant_project_id uuid not null references assistant_project(id),
package_date date not null,
kind text not null check (kind in ('morning','noon')),
source_snapshot_ts timestamptz,
content_hash text not null,
item_count int not null,
changed_since_morning boolean,
created_at timestamptz not null default now(),
unique (assistant_project_id, package_date, kind)
);
create table ap_input_package_item (
id bigserial primary key,
package_id uuid not null references ap_input_package(id) on delete cascade,
input_no smallint not null check (input_no in (1,2,3,4,5,7)),
jt_task_id text,
name text not null,
start_date date,
end_date date,
is_overdue boolean not null default false,
is_purchase boolean not null default false,
assignees text[] not null default '{}',
related_task_id text,
days_until_start int,
payload jsonb
);
create index ap_input_package_item_idx on ap_input_package_item (package_id, input_no);
create table ap_agent_invocation (
id uuid primary key default gen_random_uuid(),
package_id uuid not null references ap_input_package(id),
reason text not null check (reason in ('morning','noon_changed')),
requested_at timestamptz not null default now(),
status text not null check (status in ('requested','accepted','failed')),
http_status int,
session_ref text,
error text,
attempt int not null default 1
);
create table active_task_list (
id uuid primary key default gen_random_uuid(),
assistant_project_id uuid not null references assistant_project(id),
package_id uuid not null references ap_input_package(id),
list_date date not null,
summary_text text,
is_current boolean not null default true,
created_at timestamptz not null default now()
);
create table active_task (
id uuid primary key default gen_random_uuid(),
list_id uuid not null references active_task_list(id) on delete cascade,
rank int not null,
jt_task_id text,
title text not null,
rationale text,
importance text check (importance in ('high','medium','low')),
remind_next boolean not null default false
);
create view v_agent_input as
select p.id as package_id, ap.id as assistant_project_id, j.name as project_name, ap.jt_job_id,
per.display_name as manager_name, p.package_date, p.kind,
i.input_no, i.jt_task_id, i.name, i.start_date, i.end_date, i.is_overdue, i.is_purchase,
i.assignees, i.related_task_id, i.days_until_start, i.payload
from ap_input_package p
join assistant_project ap on ap.id = p.assistant_project_id
join person per on per.id = ap.manager_person_id
left join jt_job j on j.id = ap.jt_job_id
join ap_input_package_item i on i.package_id = p.id;
alter table person enable row level security;
alter table construction_manager enable row level security;
alter table assistant_project enable row level security;
alter table purchase_task_ref enable row level security;
alter table calendar_day enable row level security;
alter table reminder_history enable row level security;
alter table sched_run enable row level security;
alter table ap_input_package enable row level security;
alter table ap_input_package_item enable row level security;
alter table ap_agent_invocation enable row level security;
alter table active_task_list enable row level security;
alter table active_task enable row level security;