@wjarka/cezarion 0.17.0-dev.5515 → 0.17.0-dev.5527

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { runCheckout, NO_WORKTREE } from './run-checkout.js';
1
+ import { runCheckout, preparePublishCheckout, reclaimCheckoutError, NO_WORKTREE } from './run-checkout.js';
2
2
  import { adoptFallbackControl, protectFallbackRun, runArtifactDirectory } from './project-run-files.js';
3
3
  import { deleteSavedWorkflow } from './workflow-files.js';
4
4
  import { projectPaths, fallbackProjectWriteError } from '../project-paths.js';
@@ -20,7 +20,6 @@ import { mergeNumberParams, prChangesParams, prChangesQuery, mergeBodySchema } f
20
20
  import { gitCommitSchema, reclaimBodySchema, repoBranchSchema } from '../contract/index.js';
21
21
  import { setConfigSchema, setAgentConfigSchema } from '../contract/index.js';
22
22
  import { saveWorkflowInputSchema as saveWorkflowSchema } from '../contract/index.js';
23
- import { isUsageLimitHold } from '../contract/index.js';
24
23
  import { patchRunInputSchema as patchRunSchema, messageInputSchema as messageSchema } from '../contract/index.js';
25
24
  import { checkoutProjectInputSchema as checkoutSchema, registerProjectInputSchema as registerProjectSchema } from '../contract/index.js';
26
25
  import { setWorkspaceConfigInputSchema as workspaceConfigUpdateSchema } from '../contract/index.js';
@@ -118,7 +117,7 @@ import { artifactPreview, loadFileLink, rasterMime } from '../artifacts/resolve.
118
117
  import { isUntouchedCancelledRun, toPastedContent } from '../workflows/run.js';
119
118
  import { worktreeDiff, worktreeSizeBytes } from '../git-worktree.js';
120
119
  import { releaseThenRemoveWorktree } from '../git-worktree-release.js';
121
- import { isReclaimable, reclaimWorktree, reclaimWorktrees, rematerializeReclaimedWorktree, selectReclaimableWorktrees } from '../runs/retention.js';
120
+ import { isReclaimable, reclaimWorktree, reclaimWorktrees, selectReclaimableWorktrees } from '../runs/retention.js';
122
121
  import { getBranches, getCommit, getDiff, getLogWithParents, getBranchHead, getRepoInfo, getStatus, getTracking } from './git.js';
123
122
  import { createGithubBranchForge } from './repo-branch-forge.js';
124
123
  import { attributeLog, classifyInputFor, deleteBranches, forgetRepoBranches, readRepoBranchCounts, readRepoBranches } from './repo-branches.js';
@@ -3522,32 +3521,17 @@ export function createApp(deps) {
3522
3521
  // badge. Failures come back as 409 with a `manual` merge command the GUI
3523
3522
  // shows next to the toast. CEZ_DRY_RUN=1 fakes the URL (no push, no gh).
3524
3523
  .post('/runs/:id/pr', async (c) => {
3525
- const { root: repoRoot, store, manager } = c.get('project');
3524
+ const { root: repoRoot, store } = c.get('project');
3526
3525
  const id = c.req.param('id');
3527
- const held = store.getRun(id);
3528
- if (!held)
3529
- return c.json({ error: 'not found' }, 404);
3530
- const refusal = held.worktreePath && fallbackProjectWriteError(held.worktreePath);
3531
- if (refusal)
3532
- return c.json({ error: refusal }, 409);
3533
- if (manager.isActive(id))
3534
- return c.json({ error: 'run is still active — wait for the review gate' }, 409);
3535
- // A hold is live: done would drop its resume (S7 B13). Don’t resume (DELETE …/auto-resume) settles it failed.
3536
- if (isUsageLimitHold(held))
3537
- return c.json({ error: 'run is waiting on a usage-limit hold — choose Don’t resume in the task’s usage-limit hint, then retry Draft PR' }, 409);
3538
- const release = manager.claimForPublish(id);
3539
- if (!release)
3540
- return c.json({ error: 'its worktree or branch is being cleaned up — retry in a moment' }, 409);
3526
+ const checkout = runCheckout(c.get('project'), id, 'publish');
3527
+ if ('status' in checkout)
3528
+ return c.json(checkout.body, checkout.status);
3529
+ const { release } = checkout;
3541
3530
  try {
3542
- // Reclaim (#483, issue 08 §B4) removed the directory and kept the branch, and Not landed
3543
- // offers this on exactly those rows: restore the checkout first, as Continue does.
3544
- await rematerializeReclaimedWorktree(repoRoot, store, id);
3545
- const run = store.getRun(id);
3546
- if (!run)
3547
- return c.json({ error: 'not found' }, 404);
3548
- if (!run.worktreePath || !existsSync(run.worktreePath) || !run.branch) {
3549
- return c.json({ error: 'no worktree/branch to publish — this task ran in the repo working tree' }, 400);
3550
- }
3531
+ const prepared = await preparePublishCheckout(c.get('project'), id);
3532
+ if ('status' in prepared)
3533
+ return c.json(prepared.body, prepared.status);
3534
+ const { run } = prepared;
3551
3535
  const outcome = await resolveForgeRoot(repoRoot, deps.forgeFactory).createPR({
3552
3536
  repoRoot,
3553
3537
  run,
@@ -3570,18 +3554,15 @@ export function createApp(deps) {
3570
3554
  // Archived tasks keep their worktree for inspection; this is the explicit
3571
3555
  // "🧹 Remove worktree" cleanup (spec 006).
3572
3556
  .post('/runs/:id/remove-worktree', protectFallbackRun('worktree'), async (c) => {
3573
- const { root: repoRoot, store, manager } = c.get('project');
3557
+ const { root: repoRoot, store } = c.get('project');
3574
3558
  const id = c.req.param('id');
3575
- const run = store.getRun(id);
3576
- if (!run)
3577
- return c.json({ error: 'not found' }, 404);
3578
- if (manager.isActive(id))
3579
- return c.json({ error: 'run is active — cancel it first' }, 409);
3580
- if (!store.canDeleteRun(id, 'worktree') || run.delegation?.role === 'worker')
3581
- return c.json({ error: 'owned resources require verified worker cleanup' }, 409);
3582
- const release = manager.claimForPublish(id);
3583
- if (!release)
3584
- return c.json({ error: 'worktree cleanup pending — retry after cleanup is confirmed' }, 409);
3559
+ const checkout = runCheckout(c.get('project'), id, 'remove', {
3560
+ claim: 'publish', activeBeforeLookup: false, checksActive: true, deletion: 'worktree', ignoresHold: true,
3561
+ nullClaimError: 'worktree cleanup pending — retry after cleanup is confirmed',
3562
+ });
3563
+ if ('status' in checkout)
3564
+ return c.json(checkout.body, checkout.status);
3565
+ const { run, release } = checkout;
3585
3566
  try {
3586
3567
  if (run.worktreePath)
3587
3568
  await releaseThenRemoveWorktree({ previewHost: deps.previewHost }, id, repoRoot, run.worktreePath, run.branch);
@@ -3593,19 +3574,15 @@ export function createApp(deps) {
3593
3574
  }
3594
3575
  })
3595
3576
  .delete('/runs/:id', protectFallbackRun('record'), async (c) => {
3596
- const { root: repoRoot, store, manager } = c.get('project');
3577
+ const { root: repoRoot, store } = c.get('project');
3597
3578
  const id = c.req.param('id');
3598
- if (manager.isActive(id))
3599
- return c.json({ error: 'run is active — cancel it first' }, 409);
3600
- const run = store.getRun(id);
3601
- if (!run)
3602
- return c.json({ error: 'not found' }, 404);
3603
- // Delete cleans up after itself: worktree + branch go with the run (spec 006).
3604
- if (!store.canDeleteRun(id))
3605
- return c.json({ error: 'owned resources require verified worker cleanup and explicit child history deletion first' }, 409);
3606
- const release = manager.claimForPublish(id);
3607
- if (!release)
3608
- return c.json({ error: 'worktree cleanup pending — retry after cleanup is confirmed' }, 409);
3579
+ const checkout = runCheckout(c.get('project'), id, 'remove', {
3580
+ claim: 'publish', activeBeforeLookup: true, checksActive: true, deletion: 'record', ignoresHold: true,
3581
+ nullClaimError: 'worktree cleanup pending — retry after cleanup is confirmed',
3582
+ });
3583
+ if ('status' in checkout)
3584
+ return c.json(checkout.body, checkout.status);
3585
+ const { run, release } = checkout;
3609
3586
  try {
3610
3587
  if (run.delegation?.role !== 'worker' && run.worktreePath)
3611
3588
  await releaseThenRemoveWorktree({ previewHost: deps.previewHost }, id, repoRoot, run.worktreePath, run.branch);
@@ -3711,27 +3688,17 @@ export function createApp(deps) {
3711
3688
  // the `cez/<id8>` branch is kept, which is what makes this safe to offer on every finished
3712
3689
  // row. `/runs/:id/remove-worktree` (directory AND branch) stays the task screen's action.
3713
3690
  .post('/worktrees/:runId/reclaim', paramZodValidator(reclaimRunParamsSchema), async (c) => {
3714
- const { root: repoRoot, store, manager } = c.get('project');
3715
- const run = store.getRun(c.req.valid('param').runId);
3716
- if (!run)
3717
- return c.json({ error: 'not found' }, 404);
3718
- const refused = store.fallbackDeleteError(run.id, 'worktree') ?? store.writeRefusal(run.id);
3719
- if (refused)
3720
- return c.json({ error: refused }, 409);
3721
- // Claimed, not merely checked: the claim holds Continue off until the directory is gone.
3722
- const release = manager.claimWorktreeReclaim(run.id);
3723
- if (!release) {
3724
- return c.json({ error: 'this worktree is in use or already reclaimed — only a finished task\'s worktree can be reclaimed' }, 409);
3725
- }
3691
+ const { root: repoRoot, store } = c.get('project');
3692
+ const checkout = runCheckout(c.get('project'), c.req.valid('param').runId, 'remove', {
3693
+ claim: 'reclaim', activeBeforeLookup: false, checksActive: false, deletion: 'inline', ignoresHold: true,
3694
+ nullClaimError: 'this worktree is in use or already reclaimed — only a finished task\'s worktree can be reclaimed',
3695
+ });
3696
+ if ('status' in checkout)
3697
+ return c.json(checkout.body, checkout.status);
3698
+ const { run, release } = checkout;
3726
3699
  const worktreeReclaimedAt = await reclaimWorktree(repoRoot, store, run, { requireClean: true, previewHost: deps.previewHost }).finally(release);
3727
3700
  if (!worktreeReclaimedAt) {
3728
- // Reclaim keeps only what is committed, so it leaves a dirty checkout alone; say which case this was.
3729
- const dirty = run.worktreePath && existsSync(run.worktreePath) ? (await getStatus(run.worktreePath).catch(() => [])).length : 0;
3730
- return c.json({
3731
- error: dirty > 0
3732
- ? `the worktree has ${dirty} uncommitted file${dirty === 1 ? '' : 's'} that exist nowhere else — commit or discard them first`
3733
- : 'the worktree directory could not be removed',
3734
- }, 409);
3701
+ return c.json({ error: await reclaimCheckoutError(run) }, 409);
3735
3702
  }
3736
3703
  forgetRepoBranches(repoRoot);
3737
3704
  return c.json({ runId: run.id, worktreeReclaimedAt });