Public: anyone can read this; the crew can change it.
Autonomous migration mode: an app from the platform gets a crew whose standing job is parity, shown as a checklist #13221
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/644-autonomous-migration-mode-an-app-from-th → main 9 commits fork 3 threads · 3 unresolved
22 files
changed +839
−17
docs/MCP.md +1 −1
| @@ -152,7 +152,7 @@ bytes in `tools/list`. After changing a tool run `bun scripts/mcp-table.ts`; | ||
| 152 | 152 | | `make_it_yours` | — | Fork an app you can see: code and history, following the original | 337 | |
| 153 | 153 | | `map` | — | An app's plan: ready or waiting, critical path, crew, parkedTwice; `done` adds closed; `first: N` ranks N first | 521 | |
| 154 | 154 | | `merge` | `land` | Land a pull, as its page's Merge button does; a person's only | 362 | |
| 155 | | `migrate` | — | Bring a repo in by public `url` (`name`, `crew`), history and all | 269 | | |
| 155 | | `migrate` | — | Bring a repo in by public `url` (`name`, `crew`), history and all | 315 | | |
| 156 | 156 | | `milestones` | — | How far each ask is | 439 | |
| 157 | 157 | | `mint_invite` | — | Mint a one-use invite link, shown once, valid `days` (1–30, default 7) | 225 | |
| 158 | 158 | | `mirror_now` | — | Admin: bring the snapshot mirror level now | 129 | |
docs/features/README.md +1
| @@ -30,6 +30,7 @@ Read with the guide page [What a forge is](../guide-what-a-forge-is.md). | ||
| 30 | 30 | - [](v1-proven.md) |
| 31 | 31 | - [](home-looks.md) |
| 32 | 32 | - [](front-milestones.md) |
| 33 | - [](migration-mode.md) | |
| 33 | 34 | - [](now-pages.md) |
| 34 | 35 | - [](pixel.md) |
| 35 | 36 | - [](home-plans.md) |
docs/features/migration-mode.md added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | # Migration mode: a crew whose standing job is parity | |
| 2 | ||
| 3 | For the owner of an app brought over from another platform. The crew writes a parity checklist against the original, works it down, and the app's page shows how far it has got. | |
| 4 | ||
| 5 | ## Start it | |
| 6 | ||
| 7 | - Web: open `/migrate?migration=1`, pick or paste the source, pick an agent, press **Bring it over (migration mode)**. (The same box is under "Put a crew on it": "Keep parity".) | |
| 8 | - API: `POST /api/v1/migrate` with `"crew": {"agent": "<name>", "mode": "migration"}`. | |
| 9 | - MCP: `migrate {url, crew, mode: "migration"}`. `mode` needs a `crew`: the crew holds the direction. | |
| 10 | ||
| 11 | The app's crew direction becomes "run natively here with parity to <origin>". The first run writes `MIGRATION.md` at the root (headings routes, data, auth, jobs, domains; a `- [ ]` line for each thing the original does) and opens one issue per unchecked line. Every run after changes `- [ ]` to `- [x]` in the pull that makes the line true. | |
| 12 | ||
| 13 | ## Read it | |
| 14 | ||
| 15 | - Web: the app's page shows a **Migration** card: done of total, and each line as a row. | |
| 16 | - Terminal: with the app open, the palette's "Migration" command shows the same lines and follows the crew as it checks them off. | |
| 17 | - MCP: `apps` returns `mode: "migration"` and `migration {origin, done, total, state}` for such an app. | |
| 18 | ||
| 19 | When every line is checked it reads "at parity". The owner then sees one button, **Cut over**, which opens the app's settings where the domain moves. A reader sees "at parity" and no button; the crew never cuts over itself. | |
| 20 | ||
| 21 | ## Who sees the source's address | |
| 22 | ||
| 23 | The address is stored without credentials, query or fragment. If the source was public, everyone who can see the app sees it. If it was private, only people who can write to the app do (the page, the terminal and MCP `apps`, where `origin` is `null` for everyone else), and the crew's direction says "its source repository" instead of the address. | |
| 24 | ||
| 25 | ## Not here yet | |
| 26 | ||
| 27 | Choosing the mode when a hosted forge is spawned ("fork of mine", "autonomous" and this one) waits for the spawn config (#M6). | |
| 28 | ||
| 29 | Both doors: the web card above and the terminal screen `src/tui/screens/migration.ts`. |
scripts/unused-exports-allow.json +1
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | "src/apps/egress.ts resetEgressTotals", |
| 7 | 7 | "src/apps/egress_refused.ts flushRefused", |
| 8 | 8 | "src/apps/manifest.ts declaresApp", |
| 9 | "src/apps/migration_mode.ts migrationOrigin", | |
| 9 | 10 | "src/apps/relay_wait.ts clearRelayWaits", |
| 10 | 11 | "src/apps/toolbar.ts TOOLBAR_PATH", |
| 11 | 12 | "src/arcade/host.ts arcadeOn", |
src/actions/migrate.ts +28 −8
| @@ -25,7 +25,10 @@ | ||
| 25 | 25 | * The clone runs as a job (`origins/jobs.ts`, the mirror's registry), so a |
| 26 | 26 | * page can watch it. With a crew asked for, the same job then puts the |
| 27 | 27 | * agent on the crew, makes the repository an app, and tells the crew one |
| 28 | * thing: make it run here, adapting rather than rewriting. | |
| 28 | * thing: make it run here, adapting rather than rewriting. In migration | |
| 29 | * mode (#644) that tell is not one-off: the crew's direction becomes "run | |
| 30 | * natively here with parity to <origin>", and its first run writes the | |
| 31 | * parity checklist (`apps/migration_mode.ts`) the app's page shows. | |
| 29 | 32 | * |
| 30 | 33 | * The data is a second step (`importAppData`, below): a dump a person |
| 31 | 34 | * uploads is loaded into the app's store and reported per table, counts and |
| @@ -71,6 +74,7 @@ import { requireUser } from "./access.ts"; | ||
| 71 | 74 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; |
| 72 | 75 | import { enableApp } from "./apps.ts"; |
| 73 | 76 | import { addToCrew } from "./crew.ts"; |
| 77 | import { cleanOrigin, migrationBrief, migrationDirectionFor, setMigration } from "../apps/migration_mode.ts"; | |
| 74 | 78 | import { |
| 75 | 79 | createRepo, |
| 76 | 80 | deleteRepo, |
| @@ -115,7 +119,16 @@ export interface MigrateInput { | ||
| 115 | 119 | */ |
| 116 | 120 | readonly branch?: string; |
| 117 | 121 | /** Put an agent on it: the crew makes it run here. */ |
| 118 | readonly crew?: { readonly agent: string; readonly autonomous: boolean }; | |
| 122 | readonly crew?: { | |
| 123 | readonly agent: string; | |
| 124 | readonly autonomous: boolean; | |
| 125 | /** | |
| 126 | * `migration` (#644): the crew's standing job is parity with the | |
| 127 | * source, shown as a checklist on the app's page. Left out, the crew | |
| 128 | * is told once to make it run here. | |
| 129 | */ | |
| 130 | readonly mode?: "migration"; | |
| 131 | }; | |
| 119 | 132 | /** |
| 120 | 133 | * What happens to the source afterwards, when it is on a connected |
| 121 | 134 | * origin: left alone (the default), archived there, or kept as a |
| @@ -165,7 +178,10 @@ interface Resolved { | ||
| 165 | 178 | */ |
| 166 | 179 | readonly private: boolean; |
| 167 | 180 | readonly authEnv: () => Promise<Record<string, string>>; |
| 168 | /** The address `vetHost` passed, when the host was vetted: git must dial it, not look the name up again. */ | |
| 181 | /** | |
| 182 | * The address `vetHost` passed, when the host was vetted: git must dial it, not look the name up | |
| 183 | * again. | |
| 184 | */ | |
| 169 | 185 | readonly pin?: Pin; |
| 170 | 186 | /** A connected origin's adapter: what issues and pulls are read through. */ |
| 171 | 187 | readonly provider?: Provider; |
| @@ -859,14 +875,18 @@ async function putCrewOn( | ||
| 859 | 875 | ? "Another app already answers to its name, so it is not an app yet — rename it and turn it on under Settings." |
| 860 | 876 | : "It could not be made an app.", |
| 861 | 877 | ); |
| 862 | if (crew.autonomous) { | |
| 878 | const migrating = crew.mode === "migration"; | |
| 879 | const mode = { origin: cleanOrigin(source.webUrl), open: !source.private }; | |
| 880 | if (crew.autonomous || migrating) { | |
| 863 | 881 | const set = await updateRepoSettings(app, user, repo, { |
| 864 | autonomy: "autonomous", | |
| 865 | autonomyBy: user.id, | |
| 882 | ...(crew.autonomous ? { autonomy: "autonomous" as const, autonomyBy: user.id } : {}), | |
| 883 | ...(migrating ? { autonomyDirection: migrationDirectionFor(mode) } : {}), | |
| 866 | 884 | }); |
| 867 | if (!set.ok) said.push("Autonomy could not be turned on."); | |
| 885 | if (!set.ok) said.push(migrating ? "The direction could not be set." : "Autonomy could not be turned on."); | |
| 868 | 886 | } |
| 869 | const told = await tell(app, user, repo, importerBrief(repo, source.label, work)); | |
| 887 | if (migrating) setMigration(app.db, repo.id, mode.origin, !source.private); | |
| 888 | const brief = importerBrief(repo, source.label, work) + (migrating ? migrationBrief(mode.origin || source.label) : ""); | |
| 889 | const told = await tell(app, user, repo, brief); | |
| 870 | 890 | said.unshift(told.ok ? toldWords(told.value) : "The crew could not be told."); |
| 871 | 891 | return said.join(" "); |
| 872 | 892 | } |
src/actions/migration.ts added +27
| @@ -0,0 +1,27 @@ | ||
| 1 | /** | |
| 2 | * What a viewer is shown of an app's migration mode (#644), the same for the page, the MCP tool | |
| 3 | * and the terminal: the progress from `MIGRATION.md`, the origin only when this viewer may be | |
| 4 | * told it (`originFor`), and whether they may act on parity. Plain data out. | |
| 5 | */ | |
| 6 | import { migrationOf, originFor, readProgress, type Progress } from "../apps/migration_mode.ts"; | |
| 7 | import type { Repo } from "../core/repos.ts"; | |
| 8 | import type { User } from "../core/users.ts"; | |
| 9 | import { repoDir } from "../git/repos.ts"; | |
| 10 | import type { App } from "../app.ts"; | |
| 11 | import { requireWriter } from "./access.ts"; | |
| 12 | ||
| 13 | export interface MigrationView { | |
| 14 | readonly origin: string | undefined; | |
| 15 | readonly progress: Progress; | |
| 16 | /** The viewer may write here: at parity they are the one who cuts over. */ | |
| 17 | readonly mayWrite: boolean; | |
| 18 | } | |
| 19 | ||
| 20 | /** The view, or undefined when the app is not in migration mode. */ | |
| 21 | export async function migrationView(app: App, user: User | undefined, repo: Repo): Promise<MigrationView | undefined> { | |
| 22 | const mode = migrationOf(app.db, repo.id); | |
| 23 | if (mode === undefined) return undefined; | |
| 24 | const mayWrite = requireWriter(app, user, repo).ok; | |
| 25 | const progress = await readProgress(repoDir(app.reposRoot, repo.ownerName, repo.name), repo.defaultBranch); | |
| 26 | return { origin: originFor(mode, mayWrite), progress, mayWrite }; | |
| 27 | } |
src/apps/migration_mode.ts added +169
| @@ -0,0 +1,169 @@ | ||
| 1 | /** | |
| 2 | * Migration mode (#644): an app brought over from a platform whose crew's | |
| 3 | * standing job is parity with the original. The mode is one row (the origin | |
| 4 | * it keeps parity with); its direction is a sentence; its progress is the | |
| 5 | * checklist in `MIGRATION.md` the crew writes on its first run and checks | |
| 6 | * off as it goes. Plain data in, plain data out. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import { readerFor } from "../git/read.ts"; | |
| 11 | import { isSafeRef } from "../git/guard.ts"; | |
| 12 | ||
| 13 | /** The file the crew keeps at the root of the app. */ | |
| 14 | export const CHECKLIST_FILE = "MIGRATION.md"; | |
| 15 | ||
| 16 | /** The areas the checklist covers, in the order the crew is asked to write them. */ | |
| 17 | export const CHECKLIST_AREAS = ["routes", "data", "auth", "jobs", "domains"] as const; | |
| 18 | ||
| 19 | export interface ChecklistItem { | |
| 20 | readonly text: string; | |
| 21 | readonly done: boolean; | |
| 22 | /** The nearest heading above it, "" when there is none. */ | |
| 23 | readonly area: string; | |
| 24 | } | |
| 25 | ||
| 26 | export interface Progress { | |
| 27 | readonly items: readonly ChecklistItem[]; | |
| 28 | readonly done: number; | |
| 29 | readonly total: number; | |
| 30 | /** Every line checked, and at least one line: the owner may cut over. */ | |
| 31 | readonly atParity: boolean; | |
| 32 | } | |
| 33 | ||
| 34 | /** The crew's direction in this mode: what its runs are held to. */ | |
| 35 | export function migrationDirection(origin: string): string { | |
| 36 | return `run natively here with parity to ${origin}`; | |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 40 | * Task-list lines of a markdown file: `- [ ] text`, `- [x] text` (also `*`, | |
| 41 | * and `X`). Headings (`#`) name the area of the lines under them; anything | |
| 42 | * else is prose and counts for nothing. Lines inside code fences are skipped. | |
| 43 | */ | |
| 44 | export function parseChecklist(markdown: string): Progress { | |
| 45 | const items: ChecklistItem[] = []; | |
| 46 | let area = ""; | |
| 47 | let fenced = false; | |
| 48 | for (const line of markdown.split(/\r?\n/)) { | |
| 49 | if (/^\s*(```|~~~)/.test(line)) { | |
| 50 | fenced = !fenced; | |
| 51 | continue; | |
| 52 | } | |
| 53 | if (fenced) continue; | |
| 54 | const heading = /^#{1,6}\s+(.+?)\s*#*\s*$/.exec(line); | |
| 55 | if (heading) { | |
| 56 | area = heading[1]!; | |
| 57 | continue; | |
| 58 | } | |
| 59 | const task = /^\s*[-*]\s+\[([ xX])\]\s+(\S.*?)\s*$/.exec(line); | |
| 60 | if (task) items.push({ text: task[2]!, done: task[1] !== " ", area }); | |
| 61 | } | |
| 62 | const done = items.filter((i) => i.done).length; | |
| 63 | return { items, done, total: items.length, atParity: items.length > 0 && done === items.length }; | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * The address of a source as an app may keep and show it: http(s) only, with no userinfo (a | |
| 68 | * `https://user:token@host/…` clone URL carries a credential), no query and no fragment. | |
| 69 | * "" when it is not an http(s) address. | |
| 70 | */ | |
| 71 | export function cleanOrigin(raw: string): string { | |
| 72 | try { | |
| 73 | const url = new URL(raw); | |
| 74 | if (url.protocol !== "https:" && url.protocol !== "http:") return ""; | |
| 75 | url.username = ""; | |
| 76 | url.password = ""; | |
| 77 | url.search = ""; | |
| 78 | url.hash = ""; | |
| 79 | return url.toString().replace(/\/+$/, ""); | |
| 80 | } catch { | |
| 81 | return ""; | |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 85 | /** Where an app's mode came from: the cleaned address, and whether anyone could read the source. */ | |
| 86 | export interface Migration { | |
| 87 | readonly origin: string; | |
| 88 | /** The source was readable by anyone, so its address is no secret. */ | |
| 89 | readonly open: boolean; | |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * Put an app in migration mode, keeping the origin it answers to. The address is cleaned here, | |
| 94 | * at the one door in, so no credential is ever stored. | |
| 95 | */ | |
| 96 | export function setMigration(db: Database, repoId: string, origin: string, open: boolean): void { | |
| 97 | db.query( | |
| 98 | `INSERT INTO app_migration (repo_id, origin, origin_open) VALUES (?1, ?2, ?3) | |
| 99 | ON CONFLICT(repo_id) DO UPDATE SET origin = excluded.origin, origin_open = excluded.origin_open`, | |
| 100 | ).run(repoId, cleanOrigin(origin), open ? 1 : 0); | |
| 101 | } | |
| 102 | ||
| 103 | /** The mode of one app, or undefined when it is not in migration mode. */ | |
| 104 | export function migrationOf(db: Database, repoId: string): Migration | undefined { | |
| 105 | return migrationsOf(db, [repoId]).get(repoId); | |
| 106 | } | |
| 107 | ||
| 108 | /** The origin an app keeps parity with, or undefined when it is not in migration mode. */ | |
| 109 | export function migrationOrigin(db: Database, repoId: string): string | undefined { | |
| 110 | return migrationOf(db, repoId)?.origin; | |
| 111 | } | |
| 112 | ||
| 113 | /** The modes of these apps in one query; an app not in the mode is absent. */ | |
| 114 | export function migrationsOf(db: Database, repoIds: readonly string[]): Map<string, Migration> { | |
| 115 | if (repoIds.length === 0) return new Map(); | |
| 116 | const rows = db | |
| 117 | .query<{ repo_id: string; origin: string; origin_open: number }, string[]>( | |
| 118 | `SELECT repo_id, origin, origin_open FROM app_migration WHERE repo_id IN (${repoIds.map(() => "?").join(",")})`, | |
| 119 | ) | |
| 120 | .all(...repoIds); | |
| 121 | return new Map(rows.map((r) => [r.repo_id, { origin: r.origin, open: r.origin_open === 1 }])); | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * The origin this viewer may be told: everyone's when the source was open, a writer's when it was | |
| 126 | * not, so a private source's address does not leak through a public app. undefined otherwise. | |
| 127 | */ | |
| 128 | export function originFor(m: Migration, mayWrite: boolean): string | undefined { | |
| 129 | return m.origin !== "" && (m.open || mayWrite) ? m.origin : undefined; | |
| 130 | } | |
| 131 | ||
| 132 | /** What the crew's direction calls the source: its address when open, else just "the source". */ | |
| 133 | export function migrationDirectionFor(m: Migration): string { | |
| 134 | return migrationDirection(m.open && m.origin !== "" ? m.origin : "its source repository"); | |
| 135 | } | |
| 136 | ||
| 137 | /** The checklist on the app's default branch; empty until the crew has written it. */ | |
| 138 | export async function readProgress(dir: string, branch: string): Promise<Progress> { | |
| 139 | if (!isSafeRef(branch)) return parseChecklist(""); | |
| 140 | try { | |
| 141 | const object = await readerFor(dir).object(`refs/heads/${branch}:${CHECKLIST_FILE}`); | |
| 142 | if (!object.ok || object.value.type !== "blob") return parseChecklist(""); | |
| 143 | return parseChecklist(new TextDecoder().decode(object.value.body)); | |
| 144 | } catch { | |
| 145 | return parseChecklist(""); | |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 149 | /** One line for a card or a tool: where the mode stands. */ | |
| 150 | export function progressWords(p: Progress): string { | |
| 151 | if (p.total === 0) return "writing the parity checklist"; | |
| 152 | if (p.atParity) return "at parity"; | |
| 153 | return `${p.done} of ${p.total} done`; | |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * What the crew is told in this mode, after the importer's brief: the | |
| 158 | * checklist first, then the work down it. Standing, not one-off: every run | |
| 159 | * starts from `MIGRATION.md`. | |
| 160 | */ | |
| 161 | export function migrationBrief(origin: string): string { | |
| 162 | return ` | |
| 163 | ||
| 164 | Your standing job here is parity with ${origin}, and ${CHECKLIST_FILE} is how a person sees it. | |
| 165 | ||
| 166 | First run: before changing anything else, write \`${CHECKLIST_FILE}\` at the root of the app: one heading per area (${CHECKLIST_AREAS.join(", ")}), and under each a line \`- [ ] <what the original does>\` for every route, table or file of data, way of signing in, scheduled job and domain it has. Open one issue per unchecked line, titled by the line, and land the file in a pull. | |
| 167 | ||
| 168 | Every run after: read \`${CHECKLIST_FILE}\`, work the issues down, and change \`- [ ]\` to \`- [x]\` in the same pull that makes the line true here, verified against the original. Add a line when you find something the original does that is not listed. When every line is checked the app is at parity, and the owner is asked to cut over; do not cut over yourself.`; | |
| 169 | } |
src/core/migration-pins/20261115120000-app-migration.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 21a59c8b2a30c10076d3fb8d3dad7e884ac6865be3608fb7d615ef8925633e61 |
src/core/migrations/20261115120000-app-migration.ts added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | // An app brought over in migration mode (#644): the origin its crew keeps | |
| 2 | // parity with. One row per app; none means a plain app. Additive: the | |
| 3 | // release before never reads it. | |
| 4 | export default ` | |
| 5 | CREATE TABLE IF NOT EXISTS app_migration ( | |
| 6 | repo_id TEXT PRIMARY KEY REFERENCES repo(id) ON DELETE CASCADE, | |
| 7 | origin TEXT NOT NULL, | |
| 8 | origin_open INTEGER NOT NULL DEFAULT 0, | |
| 9 | created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')) | |
| 10 | ); | |
| 11 | `; |
src/http/api.ts +7 −1
| @@ -3918,7 +3918,13 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3918 | 3918 | ? { visibility: input.visibility } |
| 3919 | 3919 | : {}), |
| 3920 | 3920 | ...(crew !== undefined && typeof crew.agent === "string" |
| 3921 | ? { crew: { agent: crew.agent, autonomous: crew.autonomous === true } } | |
| 3921 | ? { | |
| 3922 | crew: { | |
| 3923 | agent: crew.agent, | |
| 3924 | autonomous: crew.autonomous === true, | |
| 3925 | ...(crew.mode === "migration" ? { mode: "migration" as const } : {}), | |
| 3926 | }, | |
| 3927 | } | |
| 3922 | 3928 | : {}), |
| 3923 | 3929 | after: afterChoice(typeof input.after === "string" ? input.after : ""), |
| 3924 | 3930 | }); |
src/http/mcp/tools/apps.ts +24 −1
| @@ -5,11 +5,14 @@ import { PULL_RUNS_DEFAULT, PULL_RUNS_MAX, pullRunLines } from "../../../apps/pu | ||
| 5 | 5 | import { envPull } from "../../../apps/data.ts"; |
| 6 | 6 | import { getUserById } from "../../../core/users.ts"; |
| 7 | 7 | import { getAppByRepo } from "../../../core/apps.ts"; |
| 8 | import { migrationsOf, originFor, readProgress, progressWords } from "../../../apps/migration_mode.ts"; | |
| 9 | import { repoDir } from "../../../git/repos.ts"; | |
| 10 | import { requireWriter } from "../../../actions/access.ts"; | |
| 8 | 11 | import { bad, good, isAnswer, repoArg, type Tool } from "../shared.ts"; |
| 9 | 12 | |
| 10 | 13 | export const tool: Tool = { |
| 11 | 14 | name: "apps", |
| 12 | doc: "`{ archived? }`: your apps (`ui://forge/app` card): live or not, what is working, running `previews`, `crew` with a run now. `{ repo, pulls: true, limit? }`: an adapter's pull runs, as `GET …/app/pulls` (`limit`); a reader's", | |
| 15 | doc: "`{ archived? }`: your apps (`ui://forge/app` card): live or not, what is working, running `previews`, `crew` with a run now, and in migration mode `mode` and `migration {origin, done, total, state}` (`origin` is null to someone who may not see a private source). `{ repo, pulls: true, limit? }`: an adapter's pull runs, as `GET …/app/pulls` (`limit`); a reader's", | |
| 13 | 16 | description: |
| 14 | 17 | "Apps you can see: live or not, where served, how each runs, what works.", |
| 15 | 18 | inputSchema: { |
| @@ -54,6 +57,14 @@ export const tool: Tool = { | ||
| 54 | 57 | const names = new Map<string, string>(); |
| 55 | 58 | for (const r of active) |
| 56 | 59 | if (r.actorId !== null && !names.has(r.actorId)) names.set(r.actorId, getUserById(ctx.app.db, r.actorId)?.username ?? ""); |
| 60 | const migrating = migrationsOf(ctx.app.db, repos.map((r) => r.id)); | |
| 61 | const progress = new Map( | |
| 62 | await Promise.all( | |
| 63 | repos | |
| 64 | .filter((r) => migrating.has(r.id)) | |
| 65 | .map(async (r) => [r.id, await readProgress(repoDir(ctx.app.reposRoot, r.ownerName, r.name), r.defaultBranch)] as const), | |
| 66 | ), | |
| 67 | ); | |
| 57 | 68 | return good( |
| 58 | 69 | repos.map((repo) => { |
| 59 | 70 | const record = records.get(repo.id)!; |
| @@ -71,6 +82,18 @@ export const tool: Tool = { | ||
| 71 | 82 | url: runtime?.urlFor(record, "production") ?? null, |
| 72 | 83 | autonomy: repo.autonomy, |
| 73 | 84 | working: mine.length, |
| 85 | ...(migrating.has(repo.id) | |
| 86 | ? { | |
| 87 | mode: "migration", | |
| 88 | migration: { | |
| 89 | origin: | |
| 90 | originFor(migrating.get(repo.id)!, requireWriter(ctx.app, user, repo).ok) ?? null, | |
| 91 | done: progress.get(repo.id)!.done, | |
| 92 | total: progress.get(repo.id)!.total, | |
| 93 | state: progressWords(progress.get(repo.id)!), | |
| 94 | }, | |
| 95 | } | |
| 96 | : {}), | |
| 74 | 97 | previews, |
| 75 | 98 | crew, |
| 76 | 99 | ...(archived ? { archivedAt: repo.archivedAt } : {}), |
src/http/mcp/tools/migrate.ts +6 −2
| @@ -6,7 +6,7 @@ import { good, bad, type Tool, str } from "../shared.ts"; | ||
| 6 | 6 | |
| 7 | 7 | export const tool: Tool = { |
| 8 | 8 | name: "migrate", |
| 9 | doc: "`{ url, name?, crew? }` brings a public repository in by URL (history only; nothing reaches back) and answers `{ job, state, url }`; `{ job }` alone reads that job. Same validation, scope (`rw`) and private-address refusal as `POST /api/v1/migrate`", | |
| 9 | doc: "`{ url, name?, crew?, mode? }` brings a public repository in by URL (history only; nothing reaches back; `mode:\"migration\"` with `crew` sets the crew's standing job to parity, shown as a checklist on the app) and answers `{ job, state, url }`; `{ job }` alone reads that job. Same validation, scope (`rw`) and private-address refusal as `POST /api/v1/migrate`", | |
| 10 | 10 | description: |
| 11 | 11 | "Bring a repo in by public `url` (`name`, `crew`), history and all. With `job`, its state.", |
| 12 | 12 | inputSchema: { |
| @@ -15,6 +15,7 @@ export const tool: Tool = { | ||
| 15 | 15 | url: { type: "string", maxLength: 2048 }, |
| 16 | 16 | name: { type: "string", maxLength: 100 }, |
| 17 | 17 | crew: { type: "string", maxLength: 39 }, |
| 18 | mode: { type: "string", enum: ["migration"] }, | |
| 18 | 19 | job: { type: "string", maxLength: 64 }, |
| 19 | 20 | }, |
| 20 | 21 | }, |
| @@ -43,10 +44,13 @@ export const tool: Tool = { | ||
| 43 | 44 | const crew = args["crew"]; |
| 44 | 45 | if (name !== undefined && typeof name !== "string") return bad("invalid", "`name` is a string"); |
| 45 | 46 | if (crew !== undefined && typeof crew !== "string") return bad("invalid", "`crew` is a string"); |
| 47 | const mode = args["mode"]; | |
| 48 | if (mode !== undefined && mode !== "migration") return bad("invalid", "`mode` is \"migration\""); | |
| 49 | if (mode !== undefined && crew === undefined) return bad("invalid", "`mode` needs a `crew` to hold the direction"); | |
| 46 | 50 | const started = await migrateRepo(ctx.app, user, { |
| 47 | 51 | source: { url }, |
| 48 | 52 | ...(name === undefined ? {} : { name }), |
| 49 | ...(crew === undefined ? {} : { crew: { agent: crew, autonomous: false } }), | |
| 53 | ...(crew === undefined ? {} : { crew: { agent: crew, autonomous: false, ...(mode === undefined ? {} : { mode }) } }), | |
| 50 | 54 | }); |
| 51 | 55 | if (!started.ok) |
| 52 | 56 | return started.error === "unauthorized" |
src/tui/screens/migration.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * An app's migration checklist, in the terminal (#644): the page's card (`web/studio/migration.ts`) | |
| 3 | * from the same read (`actions/migration.ts`), so the same gate holds. Anyone who can read the app | |
| 4 | * sees the lines; the origin is shown only to someone who may be told it (a private source's | |
| 5 | * address is a writer's), and at parity only a writer is pointed at the cut-over. The screen | |
| 6 | * follows the repository's events, so a line the crew checks off appears without a key. | |
| 7 | */ | |
| 8 | ||
| 9 | import { migrationView, type MigrationView } from "../../actions/migration.ts"; | |
| 10 | import { migrationOf, progressWords } from "../../apps/migration_mode.ts"; | |
| 11 | import type { Repo } from "../../core/repos.ts"; | |
| 12 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 13 | import type { Command } from "../app/palette.ts"; | |
| 14 | import { binding } from "../app/keys.ts"; | |
| 15 | import { style } from "../term/style.ts"; | |
| 16 | import { truncate } from "../term/width.ts"; | |
| 17 | import type { Session } from "../session.ts"; | |
| 18 | import { fill, len, split } from "../ui/layout.ts"; | |
| 19 | import { ListState, drawList, live, type ListRow } from "./common.ts"; | |
| 20 | ||
| 21 | /** The lines a view is drawn as: the area in front of each line, a mark for done. */ | |
| 22 | export function migrationRows(view: MigrationView): ListRow[] { | |
| 23 | return view.progress.items.map((i) => ({ | |
| 24 | title: i.text, | |
| 25 | state: i.done ? "closed" : "open", | |
| 26 | ...(i.area === "" ? {} : { meta: i.area }), | |
| 27 | })); | |
| 28 | } | |
| 29 | ||
| 30 | /** The line over the list: where the mode stands, and what the viewer may do about it. */ | |
| 31 | export function migrationHead(view: MigrationView | undefined): string { | |
| 32 | if (view === undefined) return "This app is not in migration mode."; | |
| 33 | const said = [progressWords(view.progress)]; | |
| 34 | if (view.origin !== undefined) said.push(`parity with ${view.origin}`); | |
| 35 | if (view.progress.atParity && view.mayWrite) said.push("cut over under the app's settings"); | |
| 36 | return said.join(" · "); | |
| 37 | } | |
| 38 | ||
| 39 | export function migrationScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | |
| 40 | const state = new ListState(); | |
| 41 | let view: MigrationView | undefined; | |
| 42 | const read = async (): Promise<void> => { | |
| 43 | view = await migrationView(f.app, f.user, repo); | |
| 44 | ctx.invalidate(); | |
| 45 | }; | |
| 46 | return { | |
| 47 | title: `${repo.ownerName}/${repo.name} — Migration`, | |
| 48 | keys: [binding(["j", "k"], "move cursor")], | |
| 49 | async load(signal) { | |
| 50 | live(f, signal, { repoId: repo.id }, read); | |
| 51 | await read(); | |
| 52 | }, | |
| 53 | render(area, s) { | |
| 54 | const c = ctx.theme.c; | |
| 55 | const [head, list] = split(area, "v", [len(1), fill(1)]); | |
| 56 | s.text(head!.x, head!.y, truncate(migrationHead(view), head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 57 | drawList(s, list!, c, state, view === undefined ? [] : migrationRows(view)); | |
| 58 | }, | |
| 59 | key(e) { | |
| 60 | return state.key(e, view?.progress.items.length ?? 0); | |
| 61 | }, | |
| 62 | }; | |
| 63 | } | |
| 64 | ||
| 65 | /** The palette's command while an app in migration mode is open. */ | |
| 66 | export function migrationCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { | |
| 67 | if (repo === undefined || migrationOf(f.app.db, repo.id) === undefined) return []; | |
| 68 | return [ | |
| 69 | { | |
| 70 | id: "migration-app", | |
| 71 | title: `${repo.ownerName}/${repo.name} — Migration`, | |
| 72 | hint: "this app's parity checklist and how far the crew has got", | |
| 73 | run: () => ctx.push(migrationScreen(f, ctx, repo)), | |
| 74 | }, | |
| 75 | ]; | |
| 76 | } |
src/tui/screens/workspace.ts +2
| @@ -185,6 +185,7 @@ import { settingHistoryCommands } from "./settinghistory.ts"; | ||
| 185 | 185 | import { templateCommands } from "./template.ts"; |
| 186 | 186 | import { buildCacheCommands } from "./buildcache.ts"; |
| 187 | 187 | import { schedulesCommands } from "./schedules.ts"; |
| 188 | import { migrationCommands } from "./migration.ts"; | |
| 188 | 189 | import { recheckWindowCommands } from "./recheckwindow.ts"; |
| 189 | 190 | import { deskCommands } from "./desk.ts"; |
| 190 | 191 | import { landCommands } from "./landasis.ts"; |
| @@ -2116,6 +2117,7 @@ export function workspaceScreen( | ||
| 2116 | 2117 | ...autonomyCommands(f, ctx, repo), |
| 2117 | 2118 | ...trafficCommands(f, ctx, repo), |
| 2118 | 2119 | ...schedulesCommands(f, ctx, repo), |
| 2120 | ...migrationCommands(f, ctx, repo), | |
| 2119 | 2121 | ...recheckWindowCommands(f, ctx, repo, () => screen), |
| 2120 | 2122 | ...deskCommands(f, ctx, repo), |
| 2121 | 2123 | ...outsideAgentsCommands(f, ctx, repo), |
src/web/pages/migrate.ts +24 −2
| @@ -83,6 +83,8 @@ interface MigrateValues { | ||
| 83 | 83 | visibility: string; |
| 84 | 84 | agent: string; |
| 85 | 85 | autonomous: boolean; |
| 86 | /** Migration mode (#644): the crew's standing job is parity with the original. */ | |
| 87 | migration: boolean; | |
| 86 | 88 | after: AfterMigration; |
| 87 | 89 | /** The branch asked for; "" lets the work-branch rule choose. */ |
| 88 | 90 | branch: string; |
| @@ -543,6 +545,18 @@ function migrateForm( | ||
| 543 | 545 | change waits for your look.</span |
| 544 | 546 | > |
| 545 | 547 | </label> |
| 548 | </div> | |
| 549 | <div class="field"> | |
| 550 | <label class="check"> | |
| 551 | <input | |
| 552 | type="checkbox" | |
| 553 | name="mode" | |
| 554 | value="migration" | |
| 555 | id="migrate-mode" | |
| 556 | ${values.migration ? raw("checked") : null} | |
| 557 | /> | |
| 558 | <span>Keep parity: the crew checklists the original and works it down.</span> | |
| 559 | </label> | |
| 546 | 560 | </div>`} |
| 547 | 561 | <h2 class="mb mt">What happens to the GitHub repository</h2> |
| 548 | 562 | ${selectField({ |
| @@ -568,7 +582,7 @@ function migrateForm( | ||
| 568 | 582 | ], |
| 569 | 583 | })} |
| 570 | 584 | <div class="form-actions"> |
| 571 | ${btn("Migrate", { variant: "primary", icon: "download" })} | |
| 585 | ${btn(values.migration ? "Bring it over (migration mode)" : "Migrate", { variant: "primary", icon: "download" })} | |
| 572 | 586 | ${btn("Cancel", { href: "/", variant: "ghost" })} |
| 573 | 587 | </div> |
| 574 | 588 | </form> |
| @@ -671,6 +685,7 @@ export async function migratePage(ctx: Ctx): Promise<Response> { | ||
| 671 | 685 | // unless the link named one; "Nobody" is one choice away. |
| 672 | 686 | agent: linked.agent ?? names[0] ?? "", |
| 673 | 687 | autonomous: false, |
| 688 | migration: q.get("migration") === "1", | |
| 674 | 689 | after: "leave", |
| 675 | 690 | branch: (q.get("branch") ?? "").slice(0, 300), |
| 676 | 691 | showBranches: q.get("branches") === "1" || (q.get("branch") ?? "") !== "", |
| @@ -698,6 +713,7 @@ export async function migrateSubmit(ctx: Ctx): Promise<Response> { | ||
| 698 | 713 | visibility: field(form, "visibility") === "public" ? "public" : "private", |
| 699 | 714 | agent: field(form, "agent"), |
| 700 | 715 | autonomous: field(form, "autonomous") !== "", |
| 716 | migration: field(form, "mode") === "migration", | |
| 701 | 717 | after: afterChoice(field(form, "after")), |
| 702 | 718 | // A branch belongs to the repository its list was drawn for: another |
| 703 | 719 | // row's button migrates that row by the rule, not by this choice. |
| @@ -736,7 +752,13 @@ export async function migrateSubmit(ctx: Ctx): Promise<Response> { | ||
| 736 | 752 | : { branch: values.branch }), |
| 737 | 753 | ...(values.agent === "" |
| 738 | 754 | ? {} |
| 739 | : { crew: { agent: values.agent, autonomous: values.autonomous } }), | |
| 755 | : { | |
| 756 | crew: { | |
| 757 | agent: values.agent, | |
| 758 | autonomous: values.autonomous, | |
| 759 | ...(values.migration ? { mode: "migration" as const } : {}), | |
| 760 | }, | |
| 761 | }), | |
| 740 | 762 | after: values.after, |
| 741 | 763 | }); |
| 742 | 764 | if (!started.ok) { |
src/web/studio/app.ts +8 −1
| @@ -57,6 +57,7 @@ import { appMilestones } from "./milestone_lines.ts"; | ||
| 57 | 57 | import { nowCard, nowRegion } from "./now.ts"; |
| 58 | 58 | import { repoAsks } from "./asks.ts"; |
| 59 | 59 | import { notesCard } from "./notes.ts"; |
| 60 | import { migrationRegion } from "./migration.ts"; | |
| 60 | 61 | import { adapterCard } from "./adapter.ts"; |
| 61 | 62 | import { jobsLink } from "./jobs.ts"; |
| 62 | 63 | import { schedulesCard } from "./schedules.ts"; |
| @@ -170,7 +171,10 @@ function previewLatencyWords( | ||
| 170 | 171 | <span class="muted">· ${split}${latency.n} deploys${failedSuffix}</span>`; |
| 171 | 172 | } |
| 172 | 173 | |
| 173 | /** The policy line's card: `setting-list` lets the title wrap and the trail drop under it on a phone. */ | |
| 174 | /** | |
| 175 | * The policy line's card: `setting-list` lets the title wrap and the trail drop under it on a | |
| 176 | * phone. | |
| 177 | */ | |
| 174 | 178 | function policyLineCard(line: Html): Html { |
| 175 | 179 | return card(html`<ul class="list setting-list">${line}</ul>`, { pad: false }); |
| 176 | 180 | } |
| @@ -498,6 +502,7 @@ export async function studioApp( | ||
| 498 | 502 | const adapter = ownerFacts |
| 499 | 503 | ? await timePhaseAsync(req, "adapter", () => adapterCard(ctx, record, repo)) |
| 500 | 504 | : null; |
| 505 | const migration = await timePhaseAsync(req, "migration", () => migrationRegion(ctx, repo, ownerFacts)); | |
| 501 | 506 | const schedules = ownerFacts ? html`${schedulesCard(ctx, repo)}${jobsLink(ctx, repo)}` : null; |
| 502 | 507 | const rendering = performance.now(); |
| 503 | 508 | const tellRegion = html`${tell} |
| @@ -519,6 +524,7 @@ export async function studioApp( | ||
| 519 | 524 | ${adapter} |
| 520 | 525 | ${schedules} |
| 521 | 526 | ${tellRegion} |
| 527 | ${migration} | |
| 522 | 528 | <div id="app-previews"></div> |
| 523 | 529 | ${nowRegion(repo, now)} |
| 524 | 530 | ${appMilestones(ctx, repo, picture.milestones)} |
| @@ -551,6 +557,7 @@ export async function studioApp( | ||
| 551 | 557 | <div class="split" data-rail="wide"> |
| 552 | 558 | <div class="stack"> |
| 553 | 559 | ${tellRegion} |
| 560 | ${migration} | |
| 554 | 561 | <div id="app-previews">${previews}</div> |
| 555 | 562 | ${nowRegion(repo, now)} |
| 556 | 563 | ${appMilestones(ctx, repo, picture.milestones)} |
src/web/studio/migration.ts added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | /** | |
| 2 | * The migration checklist on an app's page (#644): for an app in migration mode, done/total and | |
| 3 | * the crew's own lines from `MIGRATION.md`, grouped by area, each a list row. The origin is told | |
| 4 | * only to people who may see it (`originFor`). At parity the owner gets one primary action, the | |
| 5 | * app's settings where the domain moves; a reader is told nothing more. Every value is the crew's | |
| 6 | * text and goes through `html`, which escapes it. | |
| 7 | */ | |
| 8 | import { migrationView } from "../../actions/migration.ts"; | |
| 9 | import { progressWords, type Progress } from "../../apps/migration_mode.ts"; | |
| 10 | import type { Repo } from "../../core/repos.ts"; | |
| 11 | import type { Ctx } from "../../http/context.ts"; | |
| 12 | import { html, type Html } from "../html.ts"; | |
| 13 | import { icon } from "../icons.ts"; | |
| 14 | import { badge, btn, card, row } from "../ui.ts"; | |
| 15 | ||
| 16 | /** The card's markup for a progress; the page and the tests share it. */ | |
| 17 | export function migrationCard(origin: string | undefined, progress: Progress, cutOver: string | null): Html { | |
| 18 | const areas = new Map<string, Progress["items"][number][]>(); | |
| 19 | for (const item of progress.items) areas.set(item.area, [...(areas.get(item.area) ?? []), item]); | |
| 20 | const lists = [...areas].map( | |
| 21 | ([area, items]) => html`${area === "" ? null : html`<h3 class="small">${area}</h3>`} | |
| 22 | <ul class="list"> | |
| 23 | ${items.map((i) => | |
| 24 | row({ | |
outdated
Principle 7 / design system: hand-rolled ☑/☐ glyphs in a bare span. Use the named row/check component so it reads right with a screen reader. |
||
| 25 | ...(i.done ? { lead: icon("check") } : {}), | |
| 26 | title: html`${i.text}<span class="sr-only"> — ${i.done ? "done" : "to do"}</span>`, | |
| 27 | }), | |
| 28 | )} | |
| 29 | </ul>`, | |
| 30 | ); | |
| 31 | return card( | |
| 32 | html`<div class="bar"> | |
| 33 | <strong>Migration</strong> | |
| 34 | ${badge(progressWords(progress), { tone: progress.atParity ? "success" : "default" })} | |
outdated
Principle 1: this line and the empty-state paragraph below say the same thing twice. Keep one. Also the origin is shown to every viewer; strip credentials and consider who may see it. |
||
| 35 | </div> | |
| 36 | ${origin === undefined ? null : html`<p class="muted small">Parity with ${origin}</p>`} | |
| 37 | ${progress.total === 0 ? html`<p class="muted">Waiting for the crew's checklist.</p>` : lists} | |
| 38 | ${progress.atParity && cutOver !== null ? btn("Cut over", { href: cutOver, variant: "primary" }) : null}`, | |
| 39 | { id: "app-migration" }, | |
| 40 | ); | |
| 41 | } | |
| 42 | ||
| 43 | /** The card, or nothing for an app that is not in migration mode. */ | |
| 44 | export async function migrationRegion(ctx: Ctx, repo: Repo, owner: boolean): Promise<Html | null> { | |
| 45 | const view = await migrationView(ctx.app, ctx.auth.user, repo); | |
| 46 | if (view === undefined) return null; | |
| 47 | return migrationCard(view.origin, view.progress, owner ? `/${repo.ownerName}/${repo.name}/settings/app` : null); | |
| 48 | } |
test/mcp-apps-migration.test.ts added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | /** | |
| 2 | * MCP `apps` returns an app's mode and checklist progress (#644); the app page shows the checklist. | |
| 3 | */ | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 6 | import { tmpdir } from "node:os"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { newApp } from "../src/actions/apps.ts"; | |
| 10 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 11 | import { setMigration } from "../src/apps/migration_mode.ts"; | |
| 12 | import { getRepo } from "../src/core/repos.ts"; | |
| 13 | import { getUserByName } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 16 | import { tokenFor } from "./helpers/tokens.ts"; | |
| 17 | ||
| 18 | let dataDir = ""; | |
| 19 | let app: App; | |
| 20 | let servers: Servers; | |
| 21 | let token = ""; | |
| 22 | ||
| 23 | const apps = async (): Promise<any[]> => { | |
| 24 | const res = await fetch(`${servers.url}/mcp`, { | |
| 25 | method: "POST", | |
| 26 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 27 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "apps", arguments: {} } }), | |
| 28 | }); | |
| 29 | const body: any = await res.json(); | |
| 30 | return JSON.parse(body.result.content[0].text); | |
| 31 | }; | |
| 32 | ||
| 33 | beforeAll(async () => { | |
| 34 | dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-apps-migration-")); | |
| 35 | app = createApp({ dataDir, port: 0 }); | |
| 36 | servers = await serve(app); | |
| 37 | await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-good-passphrase" }); | |
| 38 | const alice = getUserByName(app.db, "alice"); | |
| 39 | if (!alice) throw new Error("alice was not seeded"); | |
| 40 | for (const name of ["moving", "plain"]) { | |
| 41 | const made = await newApp(app, alice, { name, visibility: "public" }); | |
| 42 | if (!made.ok) throw new Error(String(made.error)); | |
| 43 | } | |
| 44 | token = tokenFor(app, "alice", "rw"); | |
| 45 | }, 30_000); | |
| 46 | ||
| 47 | afterAll(async () => { | |
| 48 | servers.stop(); | |
| 49 | app.close(); | |
| 50 | await rm(dataDir, { recursive: true, force: true }); | |
| 51 | }); | |
| 52 | ||
| 53 | test("an app in migration mode reports its origin and done/total; a plain app does not", async () => { | |
| 54 | const alice = getUserByName(app.db, "alice")!; | |
| 55 | const repo = getRepo(app.db, "alice", "moving")!; | |
| 56 | setMigration(app.db, repo.id, "https://old.example/app", true); | |
| 57 | let list = await apps(); | |
| 58 | const empty = list.find((a) => a.app === "alice/moving"); | |
| 59 | expect(empty.mode).toBe("migration"); | |
| 60 | expect(empty.migration).toMatchObject({ origin: "https://old.example/app", done: 0, total: 0 }); | |
| 61 | expect(list.find((a) => a.app === "alice/plain").mode).toBeUndefined(); | |
| 62 | ||
| 63 | const wrote = await commitToBranch(app, alice, repo, { | |
| 64 | branch: repo.defaultBranch, | |
| 65 | message: "checklist", | |
| 66 | files: [{ path: "MIGRATION.md", content: "## Routes\n- [x] GET /\n- [ ] POST /login\n" }], | |
| 67 | }); | |
| 68 | if (!wrote.ok) throw new Error(JSON.stringify(wrote.error)); | |
| 69 | list = await apps(); | |
| 70 | expect(list.find((a) => a.app === "alice/moving").migration).toMatchObject({ done: 1, total: 2, state: "1 of 2 done" }); | |
| 71 | ||
| 72 | // The same checklist on the app's page. | |
| 73 | const page = await fetch(`${servers.url}/alice/moving`, { headers: { authorization: `Bearer ${token}` } }); | |
| 74 | const text = await page.text(); | |
| 75 | expect(text).toContain('id="app-migration"'); | |
| 76 | expect(text).toContain("1 of 2 done"); | |
| 77 | expect(text).toContain("POST /login"); | |
| 78 | }); |
test/mcp-migrate.test.ts +70 −1
| @@ -7,10 +7,14 @@ import { afterAll, beforeAll, expect, test } from "bun:test"; | ||
| 7 | 7 | import { mkdtemp, rm } from "node:fs/promises"; |
| 8 | 8 | import { tmpdir } from "node:os"; |
| 9 | 9 | import { join } from "node:path"; |
| 10 | import { createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { migrationOrigin } from "../src/apps/migration_mode.ts"; | |
| 12 | import { getRepo } from "../src/core/repos.ts"; | |
| 10 | 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 11 | 14 | import { createApp, type App } from "../src/app.ts"; |
| 15 | import { createSession } from "../src/core/sessions.ts"; | |
| 12 | 16 | import { createToken } from "../src/core/tokens.ts"; |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 17 | import { createUser, getUserById } from "../src/core/users.ts"; | |
| 14 | 18 | import { settled } from "../src/origins/jobs.ts"; |
| 15 | 19 | import { serve, type Servers } from "../src/http/server.ts"; |
| 16 | 20 | import { quietLogs } from "./helpers/globals.ts"; |
| @@ -154,3 +158,68 @@ test("an unknown crew agent is refused", async () => { | ||
| 154 | 158 | }); |
| 155 | 159 | expect(res.error).toBeDefined(); |
| 156 | 160 | }); |
| 161 | ||
| 162 | test("`mode` needs a `crew`; with one, the mode reaches migrateRepo and the app is in migration mode", async () => { | |
| 163 | const tok = tokens.get("open")!; | |
| 164 | const url = `http://127.0.0.1:${openServer.port}/alice/fixture.git`; | |
| 165 | const lone = await migrate(openServer, tok, { url, name: "nocrew", mode: "migration" }); | |
| 166 | expect(lone.error).toContain("`crew`"); | |
| 167 | expect((await migrate(openServer, tok, { url, name: "odd", crew: "x", mode: "other" })).error).toContain("migration"); | |
| 168 | ||
| 169 | const alice = getUserById(open.db, aliceId)!; | |
| 170 | const agent = await createAgent(open, alice, { username: "parity", model: "", brief: "Keeps parity." }); | |
| 171 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 172 | const started = await migrate(openServer, tok, { url, name: "moving", crew: "parity", mode: "migration" }); | |
| 173 | expect(started.error).toBeUndefined(); | |
| 174 | await settled(started.value.job); | |
| 175 | const repo = getRepo(open.db, "alice", "moving")!; | |
| 176 | expect(migrationOrigin(open.db, repo.id)).toBe(url); | |
| 177 | expect(repo.autonomyDirection).toContain("parity to"); | |
| 178 | }); | |
| 179 | ||
| 180 | test("POST /api/v1/migrate carries crew.mode to the migration", async () => { | |
| 181 | const tok = tokens.get("open")!; | |
| 182 | const res = await fetch(`${openServer.url}/api/v1/migrate`, { | |
| 183 | method: "POST", | |
| 184 | headers: { authorization: `Bearer ${tok}`, "content-type": "application/json" }, | |
| 185 | body: JSON.stringify({ | |
| 186 | source: { url: `http://127.0.0.1:${openServer.port}/alice/fixture.git` }, | |
| 187 | name: "viaapi", | |
| 188 | crew: { agent: "parity", mode: "migration" }, | |
| 189 | }), | |
| 190 | }); | |
| 191 | expect(res.status).toBeLessThan(300); | |
| 192 | const { job } = (await res.json()) as { job: { id: string } }; | |
| 193 | await settled(job.id); | |
| 194 | expect(migrationOrigin(open.db, getRepo(open.db, "alice", "viaapi")!.id)).toBeDefined(); | |
| 195 | }); | |
| 196 | ||
| 197 | test("the migrate form's mode checkbox reaches migrateRepo; unticked, the app is plain", async () => { | |
| 198 | const cookie = `forge_session=${createSession(open.db, aliceId).token}`; | |
| 199 | const post = (name: string, ticked: boolean) => | |
| 200 | fetch(`${openServer.url}/migrate`, { | |
| 201 | method: "POST", | |
| 202 | headers: { cookie, origin: openServer.url }, | |
| 203 | body: new URLSearchParams({ | |
| 204 | url: `http://127.0.0.1:${openServer.port}/alice/fixture.git`, | |
| 205 | name, | |
| 206 | agent: "parity", | |
| 207 | ...(ticked ? { mode: "migration" } : {}), | |
| 208 | }), | |
| 209 | redirect: "manual", | |
| 210 | }); | |
| 211 | const jobOf = async (answer: Response) => { | |
| 212 | expect(answer.status).toBe(303); | |
| 213 | return answer.headers.get("location")!.split("/").pop()!; | |
| 214 | }; | |
| 215 | const ticked = await jobOf(await post("formmode", true)); | |
| 216 | await settled(ticked); | |
| 217 | const plain = await jobOf(await post("formplain", false)); | |
| 218 | await settled(plain); | |
| 219 | expect((await (await fetch(`${openServer.url}/api/v1/migrate/${plain}`, { headers: { cookie } })).json() as any).job.error ?? null).toBeNull(); | |
| 220 | const page = await (await fetch(`${openServer.url}/migrate?migration=1`, { headers: { cookie } })).text(); | |
| 221 | expect(page).toContain("Bring it over (migration mode)"); | |
| 222 | const on = migrationOrigin(open.db, getRepo(open.db, "alice", "formmode")!.id); | |
| 223 | expect(on).toBeDefined(); | |
| 224 | expect(migrationOrigin(open.db, getRepo(open.db, "alice", "formplain")!.id)).toBeUndefined(); | |
| 225 | }); |
test/migrate.test.ts +24
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 15 | import { migrationOrigin } from "../src/apps/migration_mode.ts"; | |
| 15 | 16 | import { existsSync } from "node:fs"; |
| 16 | 17 | import { mkdtemp, rm } from "node:fs/promises"; |
| 17 | 18 | import { tmpdir } from "node:os"; |
| @@ -541,6 +542,29 @@ describe("migrate", () => { | ||
| 541 | 542 | } |
| 542 | 543 | }); |
| 543 | 544 | |
| 545 | test("migration mode: the direction is parity with the origin, the brief asks for the checklist, the mode is kept", async () => { | |
| 546 | const agent = await createAgent(app, alice, { username: "parity", model: "", brief: "Keeps parity." }); | |
| 547 | expect(agent.ok).toBe(true); | |
| 548 | const job = await migrate(bob, { | |
| 549 | source: { url: fixtureUrl }, | |
| 550 | name: "inmode", | |
| 551 | crew: { agent: "parity", autonomous: false, mode: "migration" }, | |
| 552 | }); | |
| 553 | expect(job?.stage).toBe("done"); | |
| 554 | const repo = getRepo(app.db, "bob", "inmode")!; | |
| 555 | expect(repo.autonomyDirection).toBe(`run natively here with parity to ${fixtureUrl}`); | |
| 556 | expect(migrationOrigin(app.db, repo.id)).toBe(fixtureUrl); | |
| 557 | const issue = listIssues(app.db, repo.id, { kind: "issue", state: "open" })[0]!; | |
| 558 | expect(issue.body).toContain("MIGRATION.md"); | |
| 559 | expect(issue.body).toContain("one issue per unchecked line"); | |
| 560 | // Without the mode, no direction and no row. | |
| 561 | const plain = await migrate(bob, { source: { url: fixtureUrl }, name: "notmode", crew: { agent: "parity", autonomous: false } }); | |
| 562 | expect(plain?.stage).toBe("done"); | |
| 563 | const other = getRepo(app.db, "bob", "notmode")!; | |
| 564 | expect(migrationOrigin(app.db, other.id)).toBeUndefined(); | |
| 565 | expect(other.autonomyDirection).not.toContain("parity to"); | |
| 566 | }); | |
| 567 | ||
| 544 | 568 | test("from an origin: the origin's credential opens a private repository", async () => { |
| 545 | 569 | // Anonymous, by URL: refused by the upstream, and nothing left here. |
| 546 | 570 | const anon = await migrate(alice, { |
test/migration-doors.test.ts added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | /** | |
| 2 | * Migration mode's doors (#644): the page's card, the MCP tool and the terminal screen read the | |
| 3 | * same view, so the same gate holds. A private source's address is a writer's; a guest and a | |
| 4 | * reader of a public app see the lines and no address, and only a writer is pointed at the | |
| 5 | * cut-over. The terminal follows a line the crew checks off. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { migrationView } from "../src/actions/migration.ts"; | |
| 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { setMigration } from "../src/apps/migration_mode.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { migrationCommands, migrationHead, migrationRows, migrationScreen } from "../src/tui/screens/migration.ts"; | |
| 18 | import type { Ctx } from "../src/tui/app/app.ts"; | |
| 19 | import type { Session } from "../src/tui/session.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | import { promote } from "./helpers/members.ts"; | |
| 22 | ||
| 23 | quietLogs(); | |
| 24 | ||
| 25 | let dataDir: string; | |
| 26 | let app: App; | |
| 27 | let cy: User; // writer | |
| 28 | let dee: User; // reader of a public app | |
| 29 | let closed: Repo; // public app, private source | |
| 30 | let open: Repo; // public app, public source | |
| 31 | let plain: Repo; | |
| 32 | ||
| 33 | beforeAll(async () => { | |
| 34 | dataDir = await mkdtemp(join(tmpdir(), "forge-migration-doors-")); | |
| 35 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 36 | const make = async (username: string, adminIfFirst = false) => { | |
| 37 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst }); | |
| 38 | if (!made.ok) throw new Error(made.error); | |
| 39 | return made.value; | |
| 40 | }; | |
| 41 | await make("ada", true); | |
| 42 | cy = await make("cy"); | |
| 43 | promote(app.db, cy); | |
| 44 | dee = await make("dee"); | |
| 45 | const repos: Repo[] = []; | |
| 46 | for (const name of ["closedsrc", "opensrc", "plain"]) { | |
| 47 | const made = await createRepo(app, cy, { name, visibility: "public", description: "" }); | |
| 48 | if (!made.ok) throw new Error(String(made.error)); | |
| 49 | repos.push(made.value); | |
| 50 | } | |
| 51 | [closed, open, plain] = repos as [Repo, Repo, Repo]; | |
| 52 | setMigration(app.db, closed.id, "https://bot:[email protected]/acme/closed", false); | |
| 53 | setMigration(app.db, open.id, "https://git.example/acme/open", true); | |
| 54 | const wrote = await commitToBranch(app, cy, closed, { | |
| 55 | branch: closed.defaultBranch, | |
| 56 | message: "checklist", | |
| 57 | files: [{ path: "MIGRATION.md", content: "## Routes\n- [x] GET /\n" }], | |
| 58 | }); | |
| 59 | if (!wrote.ok) throw new Error(JSON.stringify(wrote.error)); | |
| 60 | }, 60_000); | |
| 61 | ||
| 62 | afterAll(async () => { | |
| 63 | app.db.close(); | |
| 64 | await rm(dataDir, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | const session = (user: User | undefined) => ({ user, db: app.db, app }) as unknown as Session; | |
| 68 | const ctxWith = () => ({ toast() {}, invalidate() {}, push() {}, theme: undefined }) as unknown as Ctx; | |
| 69 | ||
| 70 | describe("who is told what", () => { | |
| 71 | test("a private source's address is a writer's, with no credential in it", async () => { | |
| 72 | const mine = await migrationView(app, cy, closed); | |
| 73 | expect(mine?.origin).toBe("https://git.example/acme/closed"); | |
| 74 | for (const who of [dee, undefined]) { | |
| 75 | const view = await migrationView(app, who, closed); | |
| 76 | expect(view?.origin).toBeUndefined(); | |
| 77 | expect(view?.mayWrite).toBe(false); | |
| 78 | expect(view?.progress.atParity).toBe(true); | |
| 79 | } | |
| 80 | expect(JSON.stringify(await migrationView(app, cy, closed))).not.toContain("ghp_secret"); | |
| 81 | }); | |
| 82 | ||
| 83 | test("an open source's address is everyone's; an app not in the mode has no view", async () => { | |
| 84 | expect((await migrationView(app, undefined, open))?.origin).toBe("https://git.example/acme/open"); | |
| 85 | expect(await migrationView(app, cy, plain)).toBeUndefined(); | |
| 86 | }); | |
| 87 | }); | |
| 88 | ||
| 89 | describe("the terminal screen", () => { | |
| 90 | test("the command is offered only for an app in the mode, to a guest too", () => { | |
| 91 | expect(migrationCommands(session(undefined), ctxWith(), closed)).toHaveLength(1); | |
| 92 | expect(migrationCommands(session(cy), ctxWith(), plain)).toEqual([]); | |
| 93 | expect(migrationCommands(session(cy), ctxWith(), undefined)).toEqual([]); | |
| 94 | }); | |
| 95 | ||
| 96 | test("the head points only a writer at the cut-over, and shows no hidden address to a reader", async () => { | |
| 97 | const writer = await migrationView(app, cy, closed); | |
| 98 | const reader = await migrationView(app, dee, closed); | |
| 99 | expect(migrationHead(writer)).toContain("cut over"); | |
| 100 | expect(migrationHead(writer)).toContain("git.example/acme/closed"); | |
| 101 | expect(migrationHead(reader)).toBe("at parity"); | |
| 102 | expect(migrationRows(reader!)).toEqual([{ title: "GET /", state: "closed", meta: "Routes" }]); | |
| 103 | }); | |
| 104 | ||
| 105 | test("it reads the view when loaded and again when the repository's events arrive", async () => { | |
| 106 | const drawn: number[] = []; | |
| 107 | const ctx = { ...ctxWith(), invalidate: () => drawn.push(1) } as unknown as Ctx; | |
| 108 | const abort = new AbortController(); | |
| 109 | const screen = migrationScreen(session(cy), ctx, closed); | |
| 110 | await screen.load?.(abort.signal); | |
| 111 | const first = drawn.length; | |
| 112 | expect(first).toBeGreaterThan(0); | |
| 113 | // The crew checks off another line: a commit is an event on the bus. | |
| 114 | const more = await commitToBranch(app, cy, closed, { | |
| 115 | branch: closed.defaultBranch, | |
| 116 | message: "another line", | |
| 117 | files: [{ path: "MIGRATION.md", content: "## Routes\n- [x] GET /\n- [x] GET /health\n" }], | |
| 118 | }); | |
| 119 | expect(more.ok).toBe(true); | |
| 120 | await new Promise((r) => setTimeout(r, 400)); // real-timer: the screen coalesces events behind a timer of its own | |
| 121 | abort.abort(); | |
| 122 | expect(drawn.length).toBeGreaterThan(first); | |
| 123 | }); | |
| 124 | }); |
test/migration-mode.test.ts added +80
| @@ -0,0 +1,80 @@ | ||
| 1 | /** Migration mode (#644): the checklist parses, progress counts, the card says what it shows. */ | |
| 2 | import { Database } from "bun:sqlite"; | |
| 3 | import { describe, expect, test } from "bun:test"; | |
| 4 | import { cleanOrigin, migrationDirection, migrationDirectionFor, migrationOf, originFor, parseChecklist, progressWords, setMigration } from "../src/apps/migration_mode.ts"; | |
| 5 | import { migrationCard } from "../src/web/studio/migration.ts"; | |
| 6 | ||
| 7 | const MD = `# Parity with https://old.example/app | |
| 8 | ||
| 9 | ## Routes | |
| 10 | - [x] GET / renders the home page | |
| 11 | - [ ] POST /login signs in | |
| 12 | * [X] GET /health | |
| 13 | ||
| 14 | ## Data | |
| 15 | - [ ] users table | |
| 16 | \`\`\` | |
| 17 | - [ ] not a line: it is in a fence | |
| 18 | \`\`\` | |
| 19 | ||
| 20 | Some prose - [ ] not a task either. | |
| 21 | `; | |
| 22 | ||
| 23 | describe("migration mode", () => { | |
| 24 | test("the direction names the origin", () => { | |
| 25 | expect(migrationDirection("https://old.example/app")).toBe("run natively here with parity to https://old.example/app"); | |
| 26 | }); | |
| 27 | ||
| 28 | test("the checklist parses by area, skipping fences and prose", () => { | |
| 29 | const p = parseChecklist(MD); | |
| 30 | expect(p.items.map((i) => [i.area, i.text, i.done])).toEqual([ | |
| 31 | ["Routes", "GET / renders the home page", true], | |
| 32 | ["Routes", "POST /login signs in", false], | |
| 33 | ["Routes", "GET /health", true], | |
| 34 | ["Data", "users table", false], | |
| 35 | ]); | |
| 36 | }); | |
| 37 | ||
| 38 | test("progress counts, and parity needs every line checked and at least one", () => { | |
| 39 | const p = parseChecklist(MD); | |
| 40 | expect([p.done, p.total, p.atParity]).toEqual([2, 4, false]); | |
| 41 | expect(progressWords(p)).toBe("2 of 4 done"); | |
| 42 | expect(progressWords(parseChecklist(""))).toBe("writing the parity checklist"); | |
| 43 | const all = parseChecklist("- [x] a\n- [x] b\n"); | |
| 44 | expect([all.done, all.total, all.atParity]).toEqual([2, 2, true]); | |
| 45 | expect(progressWords(all)).toBe("at parity"); | |
| 46 | expect(parseChecklist("no lines").atParity).toBe(false); | |
| 47 | }); | |
| 48 | ||
| 49 | test("the card shows done/total, escapes the crew's text, and asks for the cut-over at parity", () => { | |
| 50 | const mid = String(migrationCard("https://old.example/app", parseChecklist(MD + "- [ ] <script>x</script>\n"), "/o/n/settings/app")); | |
| 51 | expect(mid).toContain("2 of 5 done"); | |
| 52 | expect(mid).not.toContain("<script>x"); | |
| 53 | expect(mid).toContain("<script>"); | |
| 54 | expect(mid).not.toContain("Cut over"); | |
| 55 | const done = String(migrationCard("https://old.example/app", parseChecklist("- [x] a\n"), "/o/n/settings/app")); | |
| 56 | expect(done).toContain("at parity"); | |
| 57 | expect(done).toContain("Cut over"); | |
| 58 | expect(done).toContain('href="/o/n/settings/app"'); | |
| 59 | // A reader sees parity and no action. | |
| 60 | const reader = String(migrationCard("https://old.example/app", parseChecklist("- [x] a\n"), null)); | |
| 61 | expect(reader).toContain("at parity"); | |
| 62 | expect(reader).not.toContain("Cut over"); | |
| 63 | }); | |
| 64 | ||
| 65 | test("an origin with credentials is stored without them, and a private source's address is for writers", () => { | |
| 66 | expect(cleanOrigin("https://bot:[email protected]/acme/app.git?token=x#frag")).toBe("https://git.example/acme/app.git"); | |
| 67 | expect(cleanOrigin("ssh://git@host/acme/app")).toBe(""); | |
| 68 | expect(cleanOrigin("not a url")).toBe(""); | |
| 69 | const db = new Database(":memory:"); | |
| 70 | db.run("CREATE TABLE app_migration (repo_id TEXT PRIMARY KEY, origin TEXT NOT NULL, origin_open INTEGER NOT NULL DEFAULT 0)"); | |
| 71 | setMigration(db, "r1", "https://bot:[email protected]/acme/app", false); | |
| 72 | const kept = migrationOf(db, "r1")!; | |
| 73 | expect(JSON.stringify(kept)).not.toContain("ghp_secret"); | |
| 74 | expect(originFor(kept, false)).toBeUndefined(); | |
| 75 | expect(originFor(kept, true)).toBe("https://git.example/acme/app"); | |
| 76 | expect(migrationDirectionFor(kept)).not.toContain("git.example"); | |
| 77 | setMigration(db, "r2", "https://git.example/acme/open", true); | |
| 78 | expect(originFor(migrationOf(db, "r2")!, false)).toBe("https://git.example/acme/open"); | |
| 79 | }); | |
| 80 | }); |
new above
offline
Principle 1: this label explains the mode in a paragraph and again on the app page. One short sentence. Add the how-to to docs/features/migration-mode.md.