Files
paperclip/packages/plugins/plugin-llm-wiki/migrations/003_spaces.sql
T
Dotta 508355b8fc [codex] Add LLM Wiki plugin package to master (#5716)
## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies.
> - The plugin system is the extension surface for optional product
capabilities without baking every workflow into core.
> - The LLM Wiki plugin package was reviewed in stacked PR #5592, which
targeted `pap-9173-llm-wiki-rest`.
> - The stack base PR #5597 merged to `master` before #5592 was merged
into that branch, so the plugin package never reached `master`.
> - A direct PR from `pap-9173-llm-wiki-rest` back to `master` would be
noisy because that branch has diverged from current `master`.
> - This pull request reapplies the reviewed
`packages/plugins/plugin-llm-wiki/` package onto current `master` and
updates Docker deps-stage manifest coverage.
> - The branch intentionally no longer changes `pnpm-workspace.yaml`
after maintainer feedback; because the new package is now a root
workspace importer, the remaining integration question is how
maintainers want the root lockfile handled under the current PR policy.

## What Changed

- Added the LLM Wiki plugin package under
`packages/plugins/plugin-llm-wiki/` from the merged PR #5592 head.
- Preserved the post-review cleanup from #5592: generated
design/screenshot artifacts are not committed, and `src/ui/index.tsx` /
`src/wiki.ts` are small public entrypoints.
- Added the new plugin package manifest to the Docker deps stage so
policy can validate package manifest coverage.
- Removed the earlier `pnpm-workspace.yaml` exclusion per maintainer
request, so the plugin is included by the existing `packages/plugins/*`
workspace glob.

## Verification

Current head:
- PGlite migration harness: ran migrations 001-003, verified old
non-space distillation unique constraints were removed, inserted
duplicate cursor and work-item keys in a second space, then reran
migration 003 successfully
- `node ./scripts/check-docker-deps-stage.mjs`
- `git diff --check`

Known current-head install result after removing the workspace
exclusion:
- `pnpm install --frozen-lockfile` fails because `pnpm-lock.yaml` has no
importer for `packages/plugins/plugin-llm-wiki/package.json`.

Previously verified on the same plugin source before the
workspace-exclusion removal:
- `pnpm --filter @paperclipai/plugin-sdk build`
- `cd packages/plugins/plugin-llm-wiki && pnpm install --lockfile=false
&& pnpm test`

## Risks

- The branch now includes `packages/plugins/plugin-llm-wiki` in the root
workspace but does not update `pnpm-lock.yaml`. Root frozen install will
fail until maintainers choose a lockfile path that fits repo policy.
- Committing `pnpm-lock.yaml` directly on this PR conflicts with the
current PR policy check, while excluding the package from
`pnpm-workspace.yaml` was rejected in maintainer feedback.
- The package includes UI code already reviewed in #5592; generated
screenshot/design artifacts were intentionally removed per maintainer
request, so visual review should regenerate screenshots locally if
needed.
- The package depends on plugin host support from #5597, which is
already merged to `master`.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI GPT-5 Codex via Codex CLI, tool use and local code execution
enabled; context window not exposed.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have run the targeted checks listed above
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge

Stack context: #5592 was merged into `pap-9173-llm-wiki-rest` after
#5597 had already merged that branch to `master`, so this follow-up PR
is needed to carry the plugin package itself into `master`.

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-11 20:45:41 -05:00

237 lines
13 KiB
SQL

CREATE TABLE IF NOT EXISTS plugin_llm_wiki_8f50da974f.wiki_spaces (
id uuid PRIMARY KEY,
company_id uuid NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
wiki_id text NOT NULL DEFAULT 'default',
slug text NOT NULL,
display_name text NOT NULL,
space_type text NOT NULL DEFAULT 'local_folder',
folder_mode text NOT NULL DEFAULT 'managed_subfolder',
root_folder_key text NOT NULL DEFAULT 'wiki-root',
path_prefix text,
configured_root_path text,
access_scope text NOT NULL DEFAULT 'shared',
owner_user_id text,
owner_agent_id uuid REFERENCES public.agents(id) ON DELETE SET NULL,
team_key text,
settings jsonb NOT NULL DEFAULT '{}'::jsonb,
status text NOT NULL DEFAULT 'active',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (company_id, wiki_id, slug)
);
CREATE INDEX IF NOT EXISTS wiki_spaces_company_status_idx
ON plugin_llm_wiki_8f50da974f.wiki_spaces (company_id, wiki_id, status);
WITH wiki_pairs AS (
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_instances
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_sources
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_pages
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_page_revisions
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_operations
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.wiki_query_sessions
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_distillation_runs
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_source_snapshots
UNION
SELECT company_id, wiki_id FROM plugin_llm_wiki_8f50da974f.paperclip_page_bindings
)
INSERT INTO plugin_llm_wiki_8f50da974f.wiki_spaces
(id, company_id, wiki_id, slug, display_name, space_type, folder_mode, root_folder_key, path_prefix, access_scope, status)
SELECT (
substr(md5(company_id::text || ':' || wiki_id || ':default'), 1, 8) || '-' ||
substr(md5(company_id::text || ':' || wiki_id || ':default'), 9, 4) || '-' ||
'4' || substr(md5(company_id::text || ':' || wiki_id || ':default'), 14, 3) || '-' ||
'8' || substr(md5(company_id::text || ':' || wiki_id || ':default'), 18, 3) || '-' ||
substr(md5(company_id::text || ':' || wiki_id || ':default'), 21, 12)
)::uuid,
company_id,
wiki_id,
'default',
'default',
'local_folder',
'managed_subfolder',
'wiki-root',
NULL,
'shared',
'active'
FROM wiki_pairs
ON CONFLICT (company_id, wiki_id, slug) DO NOTHING;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots ADD COLUMN IF NOT EXISTS space_id uuid;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings ADD COLUMN IF NOT EXISTS space_id uuid;
UPDATE plugin_llm_wiki_8f50da974f.wiki_sources t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_pages t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_page_revisions t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_operations t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.wiki_query_sessions t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
UPDATE plugin_llm_wiki_8f50da974f.paperclip_page_bindings t
SET space_id = s.id
FROM plugin_llm_wiki_8f50da974f.wiki_spaces s
WHERE t.company_id = s.company_id AND t.wiki_id = s.wiki_id AND s.slug = 'default' AND t.space_id IS NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots ALTER COLUMN space_id SET NOT NULL;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings ALTER COLUMN space_id SET NOT NULL;
DO $$
DECLARE
target record;
constraint_name text;
BEGIN
FOR target IN
SELECT * FROM (VALUES
('wiki_pages', ARRAY['company_id', 'wiki_id', 'path']::text[]),
('paperclip_distillation_cursors', ARRAY['company_id', 'wiki_id', 'source_scope', 'scope_key', 'source_kind']::text[]),
('paperclip_distillation_work_items', ARRAY['company_id', 'wiki_id', 'idempotency_key']::text[]),
('paperclip_page_bindings', ARRAY['company_id', 'wiki_id', 'page_path']::text[])
) AS targets(table_name, column_names)
LOOP
FOR constraint_name IN
SELECT c.conname
FROM pg_constraint c
JOIN pg_class t ON t.oid = c.conrelid
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE n.nspname = 'plugin_llm_wiki_8f50da974f'
AND t.relname = target.table_name
AND c.contype = 'u'
AND (
SELECT array_agg(a.attname ORDER BY constraint_columns.ordinality)::text[]
FROM unnest(c.conkey) WITH ORDINALITY AS constraint_columns(attnum, ordinality)
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = constraint_columns.attnum
) = target.column_names
LOOP
EXECUTE format('ALTER TABLE %I.%I DROP CONSTRAINT %I', 'plugin_llm_wiki_8f50da974f', target.table_name, constraint_name);
END LOOP;
END LOOP;
END $$;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
DROP CONSTRAINT IF EXISTS wiki_pages_company_wiki_space_path_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
ADD CONSTRAINT wiki_pages_company_wiki_space_path_key UNIQUE (company_id, wiki_id, space_id, path);
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
DROP CONSTRAINT IF EXISTS distillation_cursors_company_wiki_space_scope_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
ADD CONSTRAINT distillation_cursors_company_wiki_space_scope_key UNIQUE (company_id, wiki_id, space_id, source_scope, scope_key, source_kind);
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
DROP CONSTRAINT IF EXISTS distillation_work_items_company_wiki_space_idempotency_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
ADD CONSTRAINT distillation_work_items_company_wiki_space_idempotency_key UNIQUE (company_id, wiki_id, space_id, idempotency_key);
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
DROP CONSTRAINT IF EXISTS page_bindings_company_wiki_space_page_path_key;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
ADD CONSTRAINT page_bindings_company_wiki_space_page_path_key UNIQUE (company_id, wiki_id, space_id, page_path);
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources
DROP CONSTRAINT IF EXISTS wiki_sources_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_sources
ADD CONSTRAINT wiki_sources_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
DROP CONSTRAINT IF EXISTS wiki_pages_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_pages
ADD CONSTRAINT wiki_pages_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions
DROP CONSTRAINT IF EXISTS wiki_page_revisions_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_page_revisions
ADD CONSTRAINT wiki_page_revisions_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations
DROP CONSTRAINT IF EXISTS wiki_operations_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_operations
ADD CONSTRAINT wiki_operations_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions
DROP CONSTRAINT IF EXISTS wiki_query_sessions_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.wiki_query_sessions
ADD CONSTRAINT wiki_query_sessions_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
DROP CONSTRAINT IF EXISTS paperclip_distillation_cursors_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_cursors
ADD CONSTRAINT paperclip_distillation_cursors_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
DROP CONSTRAINT IF EXISTS paperclip_distillation_work_items_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_work_items
ADD CONSTRAINT paperclip_distillation_work_items_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs
DROP CONSTRAINT IF EXISTS paperclip_distillation_runs_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_distillation_runs
ADD CONSTRAINT paperclip_distillation_runs_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots
DROP CONSTRAINT IF EXISTS paperclip_source_snapshots_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_source_snapshots
ADD CONSTRAINT paperclip_source_snapshots_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
DROP CONSTRAINT IF EXISTS paperclip_page_bindings_space_id_fk;
ALTER TABLE plugin_llm_wiki_8f50da974f.paperclip_page_bindings
ADD CONSTRAINT paperclip_page_bindings_space_id_fk FOREIGN KEY (space_id) REFERENCES plugin_llm_wiki_8f50da974f.wiki_spaces(id) ON DELETE CASCADE;
CREATE INDEX IF NOT EXISTS wiki_sources_space_idx ON plugin_llm_wiki_8f50da974f.wiki_sources (company_id, wiki_id, space_id, created_at DESC);
CREATE INDEX IF NOT EXISTS wiki_operations_space_idx ON plugin_llm_wiki_8f50da974f.wiki_operations (company_id, wiki_id, space_id, created_at DESC);
CREATE INDEX IF NOT EXISTS wiki_query_sessions_space_idx ON plugin_llm_wiki_8f50da974f.wiki_query_sessions (company_id, wiki_id, space_id, updated_at DESC);
CREATE INDEX IF NOT EXISTS distillation_runs_space_idx ON plugin_llm_wiki_8f50da974f.paperclip_distillation_runs (company_id, wiki_id, space_id, created_at DESC);