@desplega.ai/agent-swarm 2.13.0 → 2.13.2
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.
- package/README.md +1 -1
- package/dist/acp-adapter-x1qqgzwx.js +17 -0
- package/dist/{acp-targets-86f0sh6g.js → acp-targets-mz8c5dnt.js} +10 -10
- package/dist/{actions-zv8vcxvr.js → actions-5ft3qp5a.js} +27 -27
- package/dist/{agent-fs-provision-jqshb33y.js → agent-fs-provision-6fgz7wvz.js} +4 -4
- package/dist/{amp-adapter-gt503ywc.js → amp-adapter-nthn8ezm.js} +3 -3
- package/dist/{app-6t4k5rh1.js → app-535m78qw.js} +10 -10
- package/dist/{approval-notifications-fcfw063h.js → approval-notifications-dewbk403.js} +11 -11
- package/dist/{assistant-9dzybh12.js → assistant-39zyabv2.js} +28 -28
- package/dist/{boot-args-schema-xzfp2feg.js → boot-args-schema-bqv5bf77.js} +1 -1
- package/dist/{boot-reembed-3jz0p2jk.js → boot-reembed-157c0gcz.js} +1 -1
- package/dist/{boot-reembed-r88wngfg.js → boot-reembed-fas7z3zn.js} +1 -1
- package/dist/{boot-scrub-sweep-scrqhw48.js → boot-scrub-sweep-tmec0be2.js} +2 -2
- package/dist/{boot-seeds-vb2dcrrb.js → boot-seeds-32zm50x2.js} +4 -4
- package/dist/{boxd-68n75y1s.js → boxd-hr8rsxzq.js} +1 -1
- package/dist/{claude-adapter-h3mp9hgs.js → claude-adapter-gwq7x2nj.js} +9 -9
- package/dist/{claude-managed-adapter-kpbyj2vb.js → claude-managed-adapter-rkvw6fvq.js} +4 -4
- package/dist/{claude-managed-setup-0efr3wkg.js → claude-managed-setup-0vgz4d1j.js} +6 -6
- package/dist/{claude-sdk-session-03dw669q.js → claude-sdk-session-5wbwxy8c.js} +20967 -19390
- package/dist/{cli-z64t9pe6.js → cli-0f7n3td3.js} +1 -1
- package/dist/{cli-j53hpbsm.js → cli-0xwb7zpt.js} +2 -2
- package/dist/{cli-wxwv3t11.js → cli-1f48qdnb.js} +55 -13
- package/dist/{cli-gfcv679k.js → cli-1gevjrc1.js} +1 -1
- package/dist/{cli-0jkenasr.js → cli-2v4xxnx2.js} +1 -1
- package/dist/{cli-hc6e4tac.js → cli-2wde56gv.js} +6 -6
- package/dist/{cli-mh7pdxe0.js → cli-31vt7baf.js} +17 -17
- package/dist/{cli-ercwc8xn.js → cli-3mf04tkh.js} +1 -1
- package/dist/{cli-s6zk31gr.js → cli-43dsqhcp.js} +4 -4
- package/dist/{cli-1dbv9mj9.js → cli-4djg31jh.js} +1 -1
- package/dist/{cli-tn91wc6e.js → cli-50whymxq.js} +1 -1
- package/dist/{cli-js10kdk7.js → cli-5basv0e7.js} +19 -5
- package/dist/{cli-b2jh3dyb.js → cli-5k1hvycs.js} +4 -4
- package/dist/{cli-dkejt425.js → cli-62fjschk.js} +7 -7
- package/dist/{cli-zqmmy073.js → cli-69eq6gdk.js} +18 -18
- package/dist/{cli-qm3ntjqs.js → cli-6rmryz03.js} +13 -13
- package/dist/{cli-mz34pxbk.js → cli-7m6bbtab.js} +11 -11
- package/dist/{cli-wv20kbr3.js → cli-8a4qg44m.js} +5 -5
- package/dist/{cli-2m6yaq6b.js → cli-8fj8yfmk.js} +1 -1
- package/dist/{cli-9m9kyw77.js → cli-8qvte9hn.js} +2 -2
- package/dist/{cli-x05p2d05.js → cli-9tevdxyf.js} +1 -1
- package/dist/{cli-qec9bynj.js → cli-a6rzw649.js} +3 -3
- package/dist/{cli-ex1y88j3.js → cli-at67kvnp.js} +1 -1
- package/dist/{cli-yn4kt85g.js → cli-b0kqw55g.js} +1 -1
- package/dist/{cli-n3qe7tyr.js → cli-b0m3bg0f.js} +134 -3
- package/dist/{cli-xfjn2kyq.js → cli-bp2rt28p.js} +9 -9
- package/dist/{cli-2jk4s7z1.js → cli-c9bwawf9.js} +3 -3
- package/dist/{cli-wndegmns.js → cli-cmgdptmx.js} +3 -3
- package/dist/{cli-3p288654.js → cli-cpkxh68d.js} +2 -2
- package/dist/{cli-117g15t4.js → cli-cs26ed89.js} +8 -8
- package/dist/{cli-prf7kz78.js → cli-d0jb2qnt.js} +603 -78
- package/dist/{cli-8hp56wat.js → cli-d2epqm0y.js} +1 -1
- package/dist/{cli-wrhbym01.js → cli-d528acsw.js} +6 -6
- package/dist/{cli-tstj6e8e.js → cli-dnvmwsvc.js} +310 -88
- package/dist/{cli-9fsagra4.js → cli-e87fg3en.js} +1 -1
- package/dist/{cli-jj3c5nyb.js → cli-erh1rgh4.js} +116 -56
- package/dist/{cli-f1952kjy.js → cli-ewb028rs.js} +4 -4
- package/dist/{cli-ct8sz3np.js → cli-f1sdh8ca.js} +6 -6
- package/dist/{cli-0b655smz.js → cli-fcwrd7bh.js} +5 -5
- package/dist/{cli-eb279wn3.js → cli-fgm2mad9.js} +2 -2
- package/dist/{cli-bpje8n65.js → cli-fp65jdp2.js} +31 -6
- package/dist/{cli-dnh64e8e.js → cli-g0b3ma7r.js} +7 -7
- package/dist/{cli-avxh26pd.js → cli-gc7jkf4a.js} +4 -4
- package/dist/{cli-2skf1mdf.js → cli-gedf48yw.js} +4 -4
- package/dist/{cli-ajjvqhf3.js → cli-getrvmtx.js} +27 -6
- package/dist/{cli-c8v2yvej.js → cli-gq6qhmrr.js} +2 -2
- package/dist/{cli-mbwmqb5w.js → cli-h0ewec3d.js} +1 -1
- package/dist/{cli-tdk7jtka.js → cli-hv3820ez.js} +1 -1
- package/dist/{cli-avqkr20b.js → cli-j2ybkdk2.js} +2 -2
- package/dist/{cli-pqfecppn.js → cli-je587f74.js} +1 -1
- package/dist/{cli-hgsxnjyg.js → cli-jkkcm72j.js} +13 -13
- package/dist/{cli-g6j6ac3h.js → cli-ka13xx6b.js} +2 -2
- package/dist/{cli-t35hx078.js → cli-m1wddksc.js} +2 -2
- package/dist/{cli-50w82h6m.js → cli-mwxez0pz.js} +1 -0
- package/dist/{cli-nc6a8b5b.js → cli-n14r8h4n.js} +2 -2
- package/dist/{cli-y4a4458j.js → cli-n46d87mj.js} +3 -3
- package/dist/{cli-0dg7ytxm.js → cli-nkfv398a.js} +1096 -75
- package/dist/{cli-fhvk2nvp.js → cli-nvqq3232.js} +9 -9
- package/dist/{cli-bhgrhedp.js → cli-q8aq48v1.js} +1 -1
- package/dist/{cli-6a40p4kv.js → cli-qn79645p.js} +1 -1
- package/dist/{cli-hbq0kwxz.js → cli-r4fphq38.js} +4 -4
- package/dist/{cli-47hz21se.js → cli-r8ddn0hf.js} +4 -5
- package/dist/{cli-hpb9x72z.js → cli-rvhaahfv.js} +9 -9
- package/dist/{cli-qhhb30e8.js → cli-s08zsqva.js} +9 -9
- package/dist/{cli-sdh61g52.js → cli-s6fhqbz1.js} +2 -2
- package/dist/{cli-y518qkjg.js → cli-sjk85d7v.js} +13 -12
- package/dist/{cli-ytc7fpza.js → cli-t25rq8mg.js} +4 -4
- package/dist/{cli-sq913bh1.js → cli-t9nxq30h.js} +8 -8
- package/dist/{cli-49f5jyhh.js → cli-tbdw3f92.js} +3 -3
- package/dist/{cli-1v82d2w6.js → cli-tceqvv6h.js} +4 -4
- package/dist/{cli-fz9pcjsw.js → cli-tgmate57.js} +4 -4
- package/dist/{cli-rtmqzat7.js → cli-vdfngx2g.js} +2 -2
- package/dist/{cli-9w8712tq.js → cli-vr4eyq6p.js} +43 -14
- package/dist/{cli-z4f346g5.js → cli-vx9acp12.js} +1 -1
- package/dist/{cli-7dbhnsj9.js → cli-wdtqkqnq.js} +4 -4
- package/dist/{cli-kde4ky90.js → cli-x5yb8zc8.js} +2 -2
- package/dist/{cli-w14j2z4k.js → cli-x7aqhxj5.js} +2 -2
- package/dist/{cli-kspxjftk.js → cli-xerdrnqs.js} +15 -6
- package/dist/{cli-48nvk6sb.js → cli-xhkek467.js} +2 -2
- package/dist/{cli-dka73bak.js → cli-xysy1g8x.js} +152 -12
- package/dist/{cli-7wy8gbwb.js → cli-ya1cj689.js} +1 -1
- package/dist/{cli-n85m2wkq.js → cli-zkbz8m2q.js} +3 -2
- package/dist/{cli-0pazkmd4.js → cli-zm6bkx3e.js} +4 -4
- package/dist/{cli-6h88mn9q.js → cli-zqy28rwx.js} +4 -4
- package/dist/cli.js +30 -29
- package/dist/{codex-adapter-f3yp0kaj.js → codex-adapter-x25xfbnh.js} +8 -8
- package/dist/{codex-hook-262cekfp.js → codex-hook-c0nvm30r.js} +2 -2
- package/dist/{codex-login-1ven7yje.js → codex-login-ven5cz5j.js} +1 -1
- package/dist/{codex-session-runner-ns4mv70p.js → codex-session-runner-kn5xnrvp.js} +9 -9
- package/dist/{commands-5bzf2tfw.js → commands-8n59gxmy.js} +3 -3
- package/dist/{controller-5xt7kvfm.js → controller-wn3vd217.js} +3 -3
- package/dist/{controllers-connect-0gg13mzp.js → controllers-connect-09gvkrhc.js} +28 -28
- package/dist/core-2b8bv6ny.js +42 -0
- package/dist/{cursor-adapter-p0c5zv6r.js → cursor-adapter-w1681kp1.js} +2 -2
- package/dist/{db-2s68ycq7.js → db-e71keqxk.js} +3 -3
- package/dist/{db-q7q639k5.js → db-spg81qtr.js} +2 -2
- package/dist/{devin-adapter-t8d86q9x.js → devin-adapter-fs90ab4v.js} +1 -1
- package/dist/{dispatcher-j2y1q7ae.js → dispatcher-fr4vawwb.js} +25 -25
- package/dist/{docker-1b41eysj.js → docker-1h33jvh9.js} +2 -2
- package/dist/{dsh-adapter-pgnkf4st.js → dsh-adapter-g9yqv09y.js} +2 -2
- package/dist/{e2b-xm60c8ad.js → e2b-jnk0yeva.js} +2 -2
- package/dist/{e2b-ew8c5j5v.js → e2b-y7e5kmzf.js} +2 -2
- package/dist/{easter-egg-3d-0kea97jf.js → easter-egg-3d-ac9tv9a6.js} +1 -1
- package/dist/{embedding-space-3n6mhhmy.js → embedding-space-an77f45k.js} +6 -6
- package/dist/{embeddings-jfmj6yme.js → embeddings-jkrehejr.js} +1 -1
- package/dist/{event-bus-v4ywhc2p.js → event-bus-08t2b4y2.js} +1 -1
- package/dist/{execute-tjrej979.js → execute-3mys4nta.js} +1 -1
- package/dist/{external-channel-jy8w2gg3.js → external-channel-s6zxpts9.js} +2 -2
- package/dist/fx-adapter-4yet1c4f.js +20 -0
- package/dist/grok-adapter-8ge6tw7m.js +20 -0
- package/dist/handlers-0r9evrdj.js +41 -0
- package/dist/heartbeat-jjpb44e5.js +50 -0
- package/dist/{hook-fv1jwrv7.js → hook-68gbs87k.js} +6 -6
- package/dist/{hook-bpnncsec.js → hook-y6n23zzj.js} +8 -8
- package/dist/{http-bpa26czn.js → http-8c6y008s.js} +404 -146
- package/dist/{index-w216g9y9.js → index-0vthtbc2.js} +38 -22
- package/dist/index-1v23363p.js +39 -0
- package/dist/index-30qw6vyw.js +44 -0
- package/dist/index-6bm3w6cn.js +40 -0
- package/dist/{index-s8re7mvq.js → index-6h3r0vpe.js} +1 -1
- package/dist/{index-620y7jzq.js → index-7dgxveck.js} +3 -3
- package/dist/{index-4k7m5dgj.js → index-maddg9ys.js} +143 -103
- package/dist/{index-0h6qd4p1.js → index-mbpxhc4w.js} +3 -4
- package/dist/index-rz8ve510.js +569 -0
- package/dist/{index-mpj0z9e0.js → index-we3nd2t0.js} +1 -1
- package/dist/{index-pe3d8jfy.js → index-whysn1wf.js} +1 -1
- package/dist/{index-52tgbhrw.js → index-xcw7hjt5.js} +28 -28
- package/dist/{index-8p0j4h4j.js → index-ym9fgxad.js} +94 -89
- package/dist/{k8s-fy8hbhkg.js → k8s-8326q0rq.js} +2 -2
- package/dist/{keepalive-d6z12s04.js → keepalive-7e9r5dvp.js} +5 -4
- package/dist/lead-3jpc7kcr.js +79 -0
- package/dist/{maintenance-7kac6g2g.js → maintenance-d3erjvcg.js} +1 -1
- package/dist/{oauth-refresh-sweep-t1xjetbh.js → oauth-refresh-sweep-0qh70988.js} +4 -3
- package/dist/{object-webhooks-6rdagg0r.js → object-webhooks-ac7v2q5p.js} +7 -7
- package/dist/{onboard-32y1cfxy.js → onboard-5yphtken.js} +4 -4
- package/dist/{opencode-adapter-enp3v31n.js → opencode-adapter-0xvyba4m.js} +8 -8
- package/dist/{otel-impl-k0tm5ptj.js → otel-impl-svqp691q.js} +2 -2
- package/dist/{pending-24zebgvs.js → pending-bn4xe5br.js} +26 -26
- package/dist/{pi-mono-adapter-yj72xxxe.js → pi-mono-adapter-763s8a3w.js} +20 -18
- package/dist/{pricing-refresh-cf25x38j.js → pricing-refresh-kfc215mf.js} +2 -2
- package/dist/{rbac-roles-0wydd22m.js → rbac-roles-3bznvkpt.js} +3 -3
- package/dist/{rbac-roles-at6crb3r.js → rbac-roles-6r4h2ghj.js} +2 -2
- package/dist/{render-v2-nvcw0jd7.js → render-v2-f3qedbpy.js} +10 -10
- package/dist/{resume-y4w0k5v3.js → resume-vcag4ann.js} +13 -13
- package/dist/{runtime-vpe43hx0.js → runtime-x0s55z2t.js} +1 -1
- package/dist/{session-access-gkyvnfye.js → session-access-nwzve8q2.js} +5 -5
- package/dist/{session-source-backfill-3w2st4m2.js → session-source-backfill-jpqfzmgz.js} +2 -2
- package/dist/{setup-axxvqtys.js → setup-d2smwaem.js} +1 -1
- package/dist/thread-buffer-8fc703jc.js +42 -0
- package/dist/{tree-visibility-y4fqvab5.js → tree-visibility-dwzc8en0.js} +2 -2
- package/dist/{user-merge-jobs-kcmwvrc6.js → user-merge-jobs-4ce7kma2.js} +3 -3
- package/dist/{virtual-modules-7d92dct1.js → virtual-modules-78a6yw71.js} +3 -3
- package/dist/{work-objects-f708e069.js → work-objects-m0w1e7q8.js} +28 -28
- package/dist/worker-3p2vq107.js +79 -0
- package/dist/{worker-follow-up-0by8wztc.js → worker-follow-up-e3t6f895.js} +25 -25
- package/dist/{x-3dtaf2r9.js → x-asbrnafg.js} +2 -2
- package/openapi.json +1795 -1623
- package/package.json +13 -12
- package/src/apps/catalog.generated.json +726 -0
- package/src/apps/definition.ts +24 -7
- package/src/apps/page-validator.ts +225 -3
- package/src/be/db.ts +38 -10
- package/src/be/mcp-proxy.ts +5 -4
- package/src/be/migrations/340_api_key_spend_cap.sql +5 -0
- package/src/be/migrations/341_user_token_expiry.sql +4 -0
- package/src/be/schedules/validate.ts +26 -0
- package/src/be/scripts/run-credential.ts +55 -1
- package/src/be/scripts/typecheck.ts +4 -4
- package/src/be/scripts/user-code-mode.ts +160 -0
- package/src/be/swarm-config-guard.ts +4 -0
- package/src/be/users.ts +48 -7
- package/src/commands/credential-outcome-report.ts +5 -2
- package/src/commands/onboard/agent-fs-version.ts +1 -1
- package/src/commands/provider-credentials.ts +19 -0
- package/src/commands/provider-error-class.ts +4 -1
- package/src/commands/rate-limit-outcome.ts +48 -3
- package/src/commands/runner.ts +113 -55
- package/src/commands/worktree-reaper.ts +600 -0
- package/src/extensions/contract-types.generated.ts +1 -1
- package/src/http/agents.ts +4 -0
- package/src/http/api-keys.ts +27 -3
- package/src/http/pages-public.ts +40 -4
- package/src/http/pool-keys.ts +21 -2
- package/src/http/schedules.ts +28 -3
- package/src/http/script-connection-proxy.ts +18 -6
- package/src/http/script-connections.ts +12 -4
- package/src/http/script-runs.ts +12 -3
- package/src/http/scripts.ts +2 -11
- package/src/http/tasks.ts +6 -10
- package/src/http/users.ts +1 -0
- package/src/integrations/chat-sdk/adapters.ts +29 -0
- package/src/integrations/chat-sdk/limits.ts +3 -0
- package/src/integrations/chat-sdk/rocketchat/README.md +8 -0
- package/src/integrations/chat-sdk/rocketchat/index.ts +790 -0
- package/src/integrations/chat-sdk/rocketchat/markdown.ts +210 -0
- package/src/integrations/chat-sdk/rocketchat/types.ts +249 -0
- package/src/integrations/chat-sdk/runtime.ts +11 -5
- package/src/integrations/chat-sdk/schema.ts +37 -3
- package/src/integrations/chat-sdk/status.ts +9 -0
- package/src/mcp-client/http-client.ts +3 -1
- package/src/oauth/identity-capture.ts +6 -1
- package/src/oauth/mcp-wrapper.ts +10 -3
- package/src/oauth/wrapper.ts +15 -3
- package/src/prompts/session-templates.ts +1 -0
- package/src/providers/acp-targets.ts +2 -2
- package/src/providers/amp-adapter.ts +1 -1
- package/src/providers/dsh-adapter.ts +1 -1
- package/src/providers/pi-mono-adapter.ts +4 -2
- package/src/providers/runtime-dependencies.ts +1 -1
- package/src/rbac/legacy-policy.ts +3 -0
- package/src/rbac/permissions.ts +9 -0
- package/src/scripts-runtime/executors/native.ts +225 -67
- package/src/scripts-runtime/mcp-client.ts +2 -1
- package/src/scripts-runtime/swarm-sdk.ts +2 -1
- package/src/scripts-runtime/types/stdlib.d.ts +4 -4
- package/src/scripts-runtime/types/swarm-extension.d.ts +7 -7
- package/src/scripts-runtime/types/swarm-sdk.d.ts +4 -4
- package/src/server-user.ts +98 -0
- package/src/slack/enrich.ts +14 -11
- package/src/tests/agents-list-model-display.test.ts +23 -0
- package/src/tests/apps-spike2.test.ts +35 -0
- package/src/tests/base-prompt.test.ts +1 -0
- package/src/tests/chat-sdk-rocketchat-vendored.test.ts +67 -0
- package/src/tests/chat-sdk-status-route.test.ts +13 -1
- package/src/tests/codex-app-server.test.ts +4 -6
- package/src/tests/deploy-guard.test.ts +24 -0
- package/src/tests/dsh-adapter.test.ts +1 -1
- package/src/tests/ensure-token.test.ts +2 -2
- package/src/tests/entrypoint-repo-autoclone-args.test.ts +202 -0
- package/src/tests/fake-dns.ts +47 -0
- package/src/tests/fixtures/base-prompt-baseline.json +12 -12
- package/src/tests/latest-model-report.test.ts +138 -0
- package/src/tests/mcp-oauth-dcr-reuse.test.ts +5 -2
- package/src/tests/mcp-oauth-ensure-token.test.ts +3 -2
- package/src/tests/mcp-oauth-manual-client.test.ts +6 -1
- package/src/tests/mcp-oauth-wrapper.test.ts +20 -4
- package/src/tests/mcp-outbound-fetch-guard.test.ts +169 -0
- package/src/tests/mcp-server-ssrf.test.ts +2 -0
- package/src/tests/mcp-user-route.test.ts +10 -1
- package/src/tests/memory-search-stall.test.ts +5 -0
- package/src/tests/oauth-connection-fetch-guard.test.ts +162 -0
- package/src/tests/oauth-token-endpoint-fetch-guard.test.ts +121 -0
- package/src/tests/oauth-wrapper.test.ts +1 -2
- package/src/tests/pages-head-v2.test.ts +2 -1
- package/src/tests/script-connections-http.test.ts +8 -3
- package/src/tests/script-connections-mcp.test.ts +14 -10
- package/src/tests/script-connections-sql.test.ts +6 -2
- package/src/tests/script-connections.test.ts +4 -0
- package/src/tests/slack-thread-participants.test.ts +14 -3
- package/src/tests/spend-cap-credentials.test.ts +34 -0
- package/src/tests/spend-cap-outcome.test.ts +67 -0
- package/src/tests/subscription-first.test.ts +119 -0
- package/src/tests/worktree-reaper.test.ts +424 -0
- package/src/tools/get-task-details.ts +247 -5
- package/src/tools/schedules/patch-schedule.ts +26 -1
- package/src/tools/schedules/update-schedule.ts +28 -2
- package/src/tools/script-common.ts +37 -22
- package/src/tools/script-query-types.ts +1 -1
- package/src/tools/script-run.ts +1 -1
- package/src/tools/script-search.ts +12 -10
- package/src/tools/utils.ts +7 -1
- package/src/utils/credentials.ts +36 -1
- package/src/utils/egress-guard.ts +40 -10
- package/src/utils/error-tracker.ts +17 -0
- package/templates/skills/apps/SKILL.md +191 -2
- package/templates/skills/apps/content.md +191 -2
- package/templates/skills/boards-and-sheets/content.md +11 -0
- package/templates/skills/chat-sdk/content.md +2 -2
- package/templates/skills/pages/content.md +11 -0
- package/templates/skills/swarm-docs/content.md +4 -0
- package/templates/skills/swarm-scripts/SKILL.md +1 -0
- package/templates/skills/swarm-scripts/content.md +1 -0
- package/templates/workflows/ralph-loop/content.md +6 -6
- package/dist/acp-adapter-cn53011z.js +0 -17
- package/dist/cli-pr0eze3x.js +0 -149
- package/dist/core-0fqtbrxa.js +0 -42
- package/dist/fx-adapter-k6w3jvz9.js +0 -20
- package/dist/grok-adapter-4q5tssw0.js +0 -20
- package/dist/handlers-qxgfvvsf.js +0 -41
- package/dist/heartbeat-6tr2w2da.js +0 -50
- package/dist/index-0mcxjdsa.js +0 -44
- package/dist/index-dz4q23ag.js +0 -40
- package/dist/index-s175jckx.js +0 -39
- package/dist/lead-p3hx7h35.js +0 -79
- package/dist/thread-buffer-rwa7d4dx.js +0 -42
- package/dist/worker-8rg9gz74.js +0 -79
- /package/dist/{cli-m4ejz4nj.js → cli-cah298e5.js} +0 -0
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worktree reaper: removes the linked git worktrees that agents create during
|
|
3
|
+
* tasks once every task that could own them has ended, and sweeps the rest on
|
|
4
|
+
* a TTL. Agents create worktrees themselves (skills tell them to), so the
|
|
5
|
+
* runner cannot record creation at the source; it diffs
|
|
6
|
+
* `git worktree list --porcelain` across the known repo roots instead.
|
|
7
|
+
*
|
|
8
|
+
* Never removed: the main worktree, a locked worktree (the agent's explicit
|
|
9
|
+
* keep), one in use by an active task or any process, one with tracked
|
|
10
|
+
* changes or commits on no remote ref. Held worktrees past the TTL lose their
|
|
11
|
+
* gitignored `node_modules` instead ("slim").
|
|
12
|
+
*
|
|
13
|
+
* Worker-side only: no DB imports. The runner is the ledger's only writer.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { mkdir, readdir, readlink, realpath, rename, rm, stat, statfs } from "node:fs/promises";
|
|
17
|
+
import { dirname, join } from "node:path";
|
|
18
|
+
import { isTerminalTaskStatus } from "../types";
|
|
19
|
+
|
|
20
|
+
export type WorktreeReaperMode = "off" | "dry-run" | "on";
|
|
21
|
+
|
|
22
|
+
export interface WorktreeReaperConfig {
|
|
23
|
+
mode: WorktreeReaperMode;
|
|
24
|
+
ttlHours: number;
|
|
25
|
+
sweepIntervalMinutes: number;
|
|
26
|
+
extraRoots: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const DEFAULT_WORKTREE_TTL_HOURS = 72;
|
|
30
|
+
export const DEFAULT_WORKTREE_SWEEP_INTERVAL_MINUTES = 60;
|
|
31
|
+
|
|
32
|
+
function clampedNumber(raw: string | undefined, fallback: number, min: number, max: number) {
|
|
33
|
+
const value = Number(raw);
|
|
34
|
+
if (raw === undefined || raw.trim() === "" || !Number.isFinite(value)) return fallback;
|
|
35
|
+
return Math.min(max, Math.max(min, value));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Reaper config from the `WORKTREE_*` env keys. A provider with no local
|
|
40
|
+
* filesystem (claude-managed, devin) forces `off`: its worktrees live remotely.
|
|
41
|
+
*/
|
|
42
|
+
export function resolveWorktreeReaperConfig(
|
|
43
|
+
env: Record<string, string | undefined> = process.env,
|
|
44
|
+
hasLocalEnvironment = true,
|
|
45
|
+
): WorktreeReaperConfig {
|
|
46
|
+
const rawMode = env.WORKTREE_REAPER_MODE?.trim().toLowerCase();
|
|
47
|
+
const mode: WorktreeReaperMode =
|
|
48
|
+
rawMode === "off" || rawMode === "on" || rawMode === "dry-run" ? rawMode : "dry-run";
|
|
49
|
+
return {
|
|
50
|
+
mode: hasLocalEnvironment ? mode : "off",
|
|
51
|
+
ttlHours: clampedNumber(env.WORKTREE_TTL_HOURS, DEFAULT_WORKTREE_TTL_HOURS, 1, 24 * 90),
|
|
52
|
+
sweepIntervalMinutes: clampedNumber(
|
|
53
|
+
env.WORKTREE_SWEEP_INTERVAL_MINUTES,
|
|
54
|
+
DEFAULT_WORKTREE_SWEEP_INTERVAL_MINUTES,
|
|
55
|
+
5,
|
|
56
|
+
24 * 60,
|
|
57
|
+
),
|
|
58
|
+
extraRoots: (env.WORKTREE_REAPER_EXTRA_ROOTS ?? "")
|
|
59
|
+
.split(",")
|
|
60
|
+
.map((root) => root.trim())
|
|
61
|
+
.filter(Boolean),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface LedgerEntry {
|
|
66
|
+
/** Worktree path as `git worktree list` prints it. */
|
|
67
|
+
path: string;
|
|
68
|
+
/** Main worktree of the repo it belongs to. */
|
|
69
|
+
repoRoot: string;
|
|
70
|
+
firstSeenAt: string;
|
|
71
|
+
/** Tasks active when it was first seen. Empty: pre-existing or unattributed. */
|
|
72
|
+
candidateTaskIds: string[];
|
|
73
|
+
lastTaskEndAt?: string;
|
|
74
|
+
/** Set before `worktree remove` runs, so a later sweep can finish an interrupted delete. */
|
|
75
|
+
removingAt?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface Ledger {
|
|
79
|
+
/** False until the first scan: worktrees found then predate the reaper. */
|
|
80
|
+
initialized: boolean;
|
|
81
|
+
roots: string[];
|
|
82
|
+
entries: Record<string, LedgerEntry>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface ActiveTaskRef {
|
|
86
|
+
taskId: string;
|
|
87
|
+
workingDir?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type ReapAction =
|
|
91
|
+
| "remove"
|
|
92
|
+
| "would-remove"
|
|
93
|
+
| "slim"
|
|
94
|
+
| "would-slim"
|
|
95
|
+
| "finish-remove"
|
|
96
|
+
| "would-finish-remove"
|
|
97
|
+
| "skip"
|
|
98
|
+
| "keep"
|
|
99
|
+
| "error";
|
|
100
|
+
|
|
101
|
+
export interface ReapDecision {
|
|
102
|
+
path: string;
|
|
103
|
+
repoRoot: string;
|
|
104
|
+
action: ReapAction;
|
|
105
|
+
reason: string;
|
|
106
|
+
taskIds: string[];
|
|
107
|
+
ageHours: number | null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface ReapResult {
|
|
111
|
+
decisions: ReapDecision[];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface WorktreeInfo {
|
|
115
|
+
path: string;
|
|
116
|
+
/** Short branch name; unset when detached. */
|
|
117
|
+
branch?: string;
|
|
118
|
+
detached: boolean;
|
|
119
|
+
bare: boolean;
|
|
120
|
+
locked: boolean;
|
|
121
|
+
prunable: boolean;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface WorktreeReaperOptions {
|
|
125
|
+
ledgerPath: string;
|
|
126
|
+
/** Directories scanned (depth 3) for `.git` directories, i.e. main worktrees. */
|
|
127
|
+
scanRoots: string[];
|
|
128
|
+
/** Read on every call so mode and TTL changes apply without a restart. */
|
|
129
|
+
getConfig: () => WorktreeReaperConfig;
|
|
130
|
+
/** Server-side task status, or null when unknown (treated as active). */
|
|
131
|
+
statusOf: (taskId: string) => Promise<string | null>;
|
|
132
|
+
log?: (line: string) => void;
|
|
133
|
+
now?: () => number;
|
|
134
|
+
/** Process cwds checked by rule 3; defaults to reading `/proc/<pid>/cwd`. */
|
|
135
|
+
processCwds?: () => Promise<string[]>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const HOUR_MS = 3_600_000;
|
|
139
|
+
|
|
140
|
+
async function git(
|
|
141
|
+
dir: string,
|
|
142
|
+
args: string[],
|
|
143
|
+
): Promise<{ ok: boolean; stdout: string; stderr: string }> {
|
|
144
|
+
const result =
|
|
145
|
+
await Bun.$`env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE -u GIT_PREFIX git -C ${dir} ${args}`
|
|
146
|
+
.quiet()
|
|
147
|
+
.nothrow();
|
|
148
|
+
return {
|
|
149
|
+
ok: result.exitCode === 0,
|
|
150
|
+
stdout: result.stdout.toString().trim(),
|
|
151
|
+
stderr: result.stderr.toString().trim(),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function parseWorktreeList(porcelain: string): WorktreeInfo[] {
|
|
156
|
+
const worktrees: WorktreeInfo[] = [];
|
|
157
|
+
for (const block of porcelain.split(/\n\s*\n/)) {
|
|
158
|
+
const lines = block.split("\n").filter(Boolean);
|
|
159
|
+
const head = lines[0];
|
|
160
|
+
if (!head?.startsWith("worktree ")) continue;
|
|
161
|
+
const info: WorktreeInfo = {
|
|
162
|
+
path: head.slice("worktree ".length),
|
|
163
|
+
detached: false,
|
|
164
|
+
bare: false,
|
|
165
|
+
locked: false,
|
|
166
|
+
prunable: false,
|
|
167
|
+
};
|
|
168
|
+
for (const line of lines.slice(1)) {
|
|
169
|
+
if (line.startsWith("branch ")) info.branch = line.slice("branch refs/heads/".length);
|
|
170
|
+
else if (line === "detached") info.detached = true;
|
|
171
|
+
else if (line === "bare") info.bare = true;
|
|
172
|
+
else if (line === "locked" || line.startsWith("locked ")) info.locked = true;
|
|
173
|
+
else if (line === "prunable" || line.startsWith("prunable ")) info.prunable = true;
|
|
174
|
+
}
|
|
175
|
+
worktrees.push(info);
|
|
176
|
+
}
|
|
177
|
+
return worktrees;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isInside(child: string, parent: string): boolean {
|
|
181
|
+
return child === parent || child.startsWith(`${parent}/`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function exists(path: string): Promise<boolean> {
|
|
185
|
+
return stat(path).then(
|
|
186
|
+
() => true,
|
|
187
|
+
() => false,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function procCwds(): Promise<string[]> {
|
|
192
|
+
const pids = await readdir("/proc").catch(() => [] as string[]);
|
|
193
|
+
const cwds = await Promise.all(
|
|
194
|
+
pids
|
|
195
|
+
.filter((pid) => /^\d+$/.test(pid))
|
|
196
|
+
.map((pid) => readlink(`/proc/${pid}/cwd`).catch(() => null)),
|
|
197
|
+
);
|
|
198
|
+
return cwds.filter((cwd): cwd is string => cwd !== null);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export class WorktreeReaper {
|
|
202
|
+
private readonly opts: WorktreeReaperOptions;
|
|
203
|
+
private ledger: Ledger | null = null;
|
|
204
|
+
private discoveredRoots: string[] = [];
|
|
205
|
+
/** Serializes every operation: one ledger writer, one removal at a time. */
|
|
206
|
+
private queue: Promise<unknown> = Promise.resolve();
|
|
207
|
+
private pending = 0;
|
|
208
|
+
private sweepQueued = false;
|
|
209
|
+
|
|
210
|
+
constructor(opts: WorktreeReaperOptions) {
|
|
211
|
+
this.opts = opts;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private now(): number {
|
|
215
|
+
return this.opts.now?.() ?? Date.now();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private log(fields: Record<string, unknown>): void {
|
|
219
|
+
(this.opts.log ?? console.log)(JSON.stringify({ event: "worktree-reaper", ...fields }));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private enqueue<T>(fn: () => Promise<T>): Promise<T> {
|
|
223
|
+
this.pending += 1;
|
|
224
|
+
const run = this.queue.then(fn).finally(() => {
|
|
225
|
+
this.pending -= 1;
|
|
226
|
+
});
|
|
227
|
+
this.queue = run.catch(() => undefined);
|
|
228
|
+
return run;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Record worktrees that appeared since the last scan. Skipped while other work is queued. */
|
|
232
|
+
observe(active: ActiveTaskRef[]): Promise<void> {
|
|
233
|
+
if (this.opts.getConfig().mode === "off" || this.pending > 0) return Promise.resolve();
|
|
234
|
+
return this.enqueue(async () => {
|
|
235
|
+
await this.scan(active, false);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Decide on every worktree attributed to `taskId`, which has just ended on this worker. */
|
|
240
|
+
onTaskEnded(taskId: string, active: ActiveTaskRef[]): Promise<ReapResult> {
|
|
241
|
+
if (this.opts.getConfig().mode === "off") return Promise.resolve({ decisions: [] });
|
|
242
|
+
return this.enqueue(async () => {
|
|
243
|
+
const worktrees = await this.scan([...active, { taskId }], false);
|
|
244
|
+
const ledger = await this.loadLedger();
|
|
245
|
+
const endedAt = new Date(this.now()).toISOString();
|
|
246
|
+
for (const entry of Object.values(ledger.entries)) {
|
|
247
|
+
if (entry.candidateTaskIds.includes(taskId)) entry.lastTaskEndAt = endedAt;
|
|
248
|
+
}
|
|
249
|
+
await this.saveLedger();
|
|
250
|
+
const decisions: ReapDecision[] = [];
|
|
251
|
+
for (const entry of Object.values(ledger.entries)) {
|
|
252
|
+
if (!entry.candidateTaskIds.includes(taskId)) continue;
|
|
253
|
+
const info = worktrees.get(entry.path);
|
|
254
|
+
if (info) decisions.push(await this.decideAndAct(entry, info, active, false));
|
|
255
|
+
}
|
|
256
|
+
return { decisions };
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** TTL and orphan pass over every known worktree. Skipped when a sweep is already queued. */
|
|
261
|
+
sweep(active: ActiveTaskRef[]): Promise<ReapResult> {
|
|
262
|
+
if (this.opts.getConfig().mode === "off" || this.sweepQueued) {
|
|
263
|
+
return Promise.resolve({ decisions: [] });
|
|
264
|
+
}
|
|
265
|
+
this.sweepQueued = true;
|
|
266
|
+
return this.enqueue(async () => {
|
|
267
|
+
this.sweepQueued = false;
|
|
268
|
+
const freeBefore = await this.freeBytes();
|
|
269
|
+
const worktrees = await this.scan(active, true);
|
|
270
|
+
const ledger = await this.loadLedger();
|
|
271
|
+
const decisions: ReapDecision[] = [];
|
|
272
|
+
for (const entry of Object.values(ledger.entries)) {
|
|
273
|
+
const info = worktrees.get(entry.path);
|
|
274
|
+
if (info) decisions.push(await this.decideAndAct(entry, info, active, true));
|
|
275
|
+
}
|
|
276
|
+
const count = (...actions: ReapAction[]) =>
|
|
277
|
+
decisions.filter((d) => actions.includes(d.action)).length;
|
|
278
|
+
this.log({
|
|
279
|
+
action: "sweep-summary",
|
|
280
|
+
mode: this.opts.getConfig().mode,
|
|
281
|
+
worktrees: decisions.length,
|
|
282
|
+
removed: count("remove", "would-remove", "finish-remove", "would-finish-remove"),
|
|
283
|
+
slimmed: count("slim", "would-slim"),
|
|
284
|
+
errors: count("error"),
|
|
285
|
+
held: decisions
|
|
286
|
+
.filter((d) => d.action === "skip" || d.action.endsWith("slim"))
|
|
287
|
+
.map((d) => `${d.path} (${d.reason})`),
|
|
288
|
+
freeBytesBefore: freeBefore,
|
|
289
|
+
freeBytesAfter: await this.freeBytes(),
|
|
290
|
+
});
|
|
291
|
+
return { decisions };
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private async freeBytes(): Promise<number | null> {
|
|
296
|
+
try {
|
|
297
|
+
const fs = await statfs(dirname(this.opts.ledgerPath));
|
|
298
|
+
return fs.bavail * fs.bsize;
|
|
299
|
+
} catch {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private async loadLedger(): Promise<Ledger> {
|
|
305
|
+
if (this.ledger) return this.ledger;
|
|
306
|
+
const file = Bun.file(this.opts.ledgerPath);
|
|
307
|
+
const empty: Ledger = { initialized: false, roots: [], entries: {} };
|
|
308
|
+
if (!(await file.exists())) {
|
|
309
|
+
this.ledger = empty;
|
|
310
|
+
return empty;
|
|
311
|
+
}
|
|
312
|
+
try {
|
|
313
|
+
const parsed = (await file.json()) as Partial<Ledger>;
|
|
314
|
+
if (typeof parsed !== "object" || parsed === null || typeof parsed.entries !== "object") {
|
|
315
|
+
throw new Error("ledger has no entries map");
|
|
316
|
+
}
|
|
317
|
+
this.ledger = {
|
|
318
|
+
initialized: parsed.initialized === true,
|
|
319
|
+
roots: Array.isArray(parsed.roots) ? parsed.roots : [],
|
|
320
|
+
entries: parsed.entries ?? {},
|
|
321
|
+
};
|
|
322
|
+
} catch (err) {
|
|
323
|
+
// A corrupt ledger is set aside, not lost. Starting uninitialized means
|
|
324
|
+
// every listed worktree counts as unattributed: TTL sweep only.
|
|
325
|
+
const aside = `${this.opts.ledgerPath}.corrupt-${this.now()}`;
|
|
326
|
+
await rename(this.opts.ledgerPath, aside).catch(() => undefined);
|
|
327
|
+
this.log({ action: "ledger-reset", reason: String(err), path: aside });
|
|
328
|
+
this.ledger = { initialized: false, roots: [], entries: {} };
|
|
329
|
+
}
|
|
330
|
+
return this.ledger;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private async saveLedger(): Promise<void> {
|
|
334
|
+
if (!this.ledger) return;
|
|
335
|
+
await mkdir(dirname(this.opts.ledgerPath), { recursive: true });
|
|
336
|
+
const tmp = `${this.opts.ledgerPath}.tmp`;
|
|
337
|
+
await Bun.write(tmp, JSON.stringify(this.ledger, null, 2));
|
|
338
|
+
await rename(tmp, this.opts.ledgerPath);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private async mainWorktreeOf(dir: string): Promise<string | null> {
|
|
342
|
+
const common = await git(dir, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
343
|
+
if (!common.ok || !common.stdout.endsWith("/.git")) return null;
|
|
344
|
+
return dirname(common.stdout);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private async discoverRoots(): Promise<string[]> {
|
|
348
|
+
const roots: string[] = [];
|
|
349
|
+
for (const scanRoot of this.opts.scanRoots) {
|
|
350
|
+
if (!(await exists(scanRoot))) continue;
|
|
351
|
+
const found =
|
|
352
|
+
await Bun.$`find ${scanRoot} -maxdepth 3 -name node_modules -prune -o -name .git -type d -print`
|
|
353
|
+
.quiet()
|
|
354
|
+
.nothrow();
|
|
355
|
+
for (const line of found.stdout.toString().split("\n")) {
|
|
356
|
+
if (line.trim()) roots.push(dirname(line.trim()));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return roots;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* List every linked worktree of every known repo, add the new ones to the
|
|
364
|
+
* ledger, and drop entries git no longer lists. Returns path -> info.
|
|
365
|
+
*/
|
|
366
|
+
private async scan(
|
|
367
|
+
active: ActiveTaskRef[],
|
|
368
|
+
refreshRoots: boolean,
|
|
369
|
+
): Promise<Map<string, WorktreeInfo>> {
|
|
370
|
+
const ledger = await this.loadLedger();
|
|
371
|
+
if (refreshRoots || this.discoveredRoots.length === 0) {
|
|
372
|
+
this.discoveredRoots = await this.discoverRoots();
|
|
373
|
+
}
|
|
374
|
+
const taskRoots = await Promise.all(
|
|
375
|
+
active.flatMap((t) => (t.workingDir ? [this.mainWorktreeOf(t.workingDir)] : [])),
|
|
376
|
+
);
|
|
377
|
+
const candidates = new Set([
|
|
378
|
+
...this.discoveredRoots,
|
|
379
|
+
...ledger.roots,
|
|
380
|
+
...this.opts.getConfig().extraRoots,
|
|
381
|
+
...taskRoots.filter((root): root is string => root !== null),
|
|
382
|
+
]);
|
|
383
|
+
const roots = new Set<string>();
|
|
384
|
+
for (const candidate of candidates) {
|
|
385
|
+
const root = await this.mainWorktreeOf(candidate);
|
|
386
|
+
if (root) roots.add(root);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const seen = new Map<string, WorktreeInfo>();
|
|
390
|
+
const firstScan = !ledger.initialized;
|
|
391
|
+
const nowIso = new Date(this.now()).toISOString();
|
|
392
|
+
const activeIds = [...new Set(active.map((t) => t.taskId))];
|
|
393
|
+
for (const root of roots) {
|
|
394
|
+
const listed = await git(root, ["worktree", "list", "--porcelain"]);
|
|
395
|
+
if (!listed.ok) continue;
|
|
396
|
+
const [main, ...linked] = parseWorktreeList(listed.stdout);
|
|
397
|
+
if (!main || main.bare) continue; // Rule 1: a bare repo's worktrees stay out of scope.
|
|
398
|
+
for (const info of linked) {
|
|
399
|
+
seen.set(info.path, info);
|
|
400
|
+
if (ledger.entries[info.path]) continue;
|
|
401
|
+
ledger.entries[info.path] = {
|
|
402
|
+
path: info.path,
|
|
403
|
+
repoRoot: root,
|
|
404
|
+
firstSeenAt: nowIso,
|
|
405
|
+
candidateTaskIds: firstScan ? [] : activeIds,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
for (const path of Object.keys(ledger.entries)) {
|
|
410
|
+
const entry = ledger.entries[path];
|
|
411
|
+
// Keep entries of a root that failed to list this time; drop the rest.
|
|
412
|
+
if (!seen.has(path) && entry && roots.has(entry.repoRoot)) delete ledger.entries[path];
|
|
413
|
+
}
|
|
414
|
+
ledger.roots = [...roots];
|
|
415
|
+
ledger.initialized = true;
|
|
416
|
+
await this.saveLedger();
|
|
417
|
+
return seen;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private async ageHours(entry: LedgerEntry): Promise<number | null> {
|
|
421
|
+
const stamps: number[] = [];
|
|
422
|
+
if (entry.lastTaskEndAt) stamps.push(Date.parse(entry.lastTaskEndAt));
|
|
423
|
+
const gitDir = await git(entry.path, ["rev-parse", "--absolute-git-dir"]);
|
|
424
|
+
if (gitDir.ok) {
|
|
425
|
+
for (const name of ["HEAD", "index", "logs/HEAD"]) {
|
|
426
|
+
const info = await stat(join(gitDir.stdout, name)).catch(() => null);
|
|
427
|
+
if (info) stamps.push(info.mtimeMs);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
// Directory mtime is deliberately ignored: a refreshed parent pins old worktrees.
|
|
431
|
+
if (stamps.length === 0) return null;
|
|
432
|
+
return (this.now() - Math.max(...stamps)) / HOUR_MS;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
private async inUse(path: string, active: ActiveTaskRef[]): Promise<boolean> {
|
|
436
|
+
for (const task of active) {
|
|
437
|
+
if (!task.workingDir) continue;
|
|
438
|
+
const dir = await realpath(task.workingDir).catch(() => task.workingDir as string);
|
|
439
|
+
if (isInside(dir, path) || isInside(path, dir)) return true;
|
|
440
|
+
}
|
|
441
|
+
const cwds = await (this.opts.processCwds ?? procCwds)();
|
|
442
|
+
return cwds.some((cwd) => isInside(cwd, path));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
private async isDefaultUpstream(repoRoot: string, upstream: string): Promise<boolean> {
|
|
446
|
+
const originHead = await git(repoRoot, [
|
|
447
|
+
"symbolic-ref",
|
|
448
|
+
"-q",
|
|
449
|
+
"--short",
|
|
450
|
+
"refs/remotes/origin/HEAD",
|
|
451
|
+
]);
|
|
452
|
+
const defaults = new Set(["origin/main", "origin/master"]);
|
|
453
|
+
if (originHead.ok && originHead.stdout) defaults.add(originHead.stdout);
|
|
454
|
+
return defaults.has(upstream);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
private async ownerActive(entry: LedgerEntry, active: ActiveTaskRef[]): Promise<boolean> {
|
|
458
|
+
const activeIds = new Set(active.map((t) => t.taskId));
|
|
459
|
+
for (const taskId of entry.candidateTaskIds) {
|
|
460
|
+
if (activeIds.has(taskId)) return true;
|
|
461
|
+
const status = await this.opts.statusOf(taskId);
|
|
462
|
+
// Unknown status (API down, not found) keeps the worktree.
|
|
463
|
+
if (!status || !isTerminalTaskStatus(status)) return true;
|
|
464
|
+
}
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
private async decide(
|
|
469
|
+
entry: LedgerEntry,
|
|
470
|
+
info: WorktreeInfo,
|
|
471
|
+
active: ActiveTaskRef[],
|
|
472
|
+
ttlHours: number,
|
|
473
|
+
ageHours: number | null,
|
|
474
|
+
): Promise<{ verdict: "remove" | "finish-remove" | "held" | "skip" | "keep"; reason: string }> {
|
|
475
|
+
if (info.locked) return { verdict: "skip", reason: "locked" };
|
|
476
|
+
const real = await realpath(entry.path).catch(() => null);
|
|
477
|
+
if (await this.inUse(real ?? entry.path, active)) return { verdict: "skip", reason: "in-use" };
|
|
478
|
+
// `.git` file gone: either our own interrupted delete, or a damaged worktree
|
|
479
|
+
// we cannot inspect (git would walk up to an enclosing repo).
|
|
480
|
+
const top = await git(entry.path, ["rev-parse", "--show-toplevel"]);
|
|
481
|
+
if (info.prunable || real === null || top.stdout !== real) {
|
|
482
|
+
if (real === null) return { verdict: "finish-remove", reason: "missing" };
|
|
483
|
+
if (entry.removingAt && !isInside(entry.repoRoot, real)) {
|
|
484
|
+
return { verdict: "finish-remove", reason: "interrupted-remove" };
|
|
485
|
+
}
|
|
486
|
+
return { verdict: "skip", reason: "broken" };
|
|
487
|
+
}
|
|
488
|
+
const dirty = await git(entry.path, ["status", "--porcelain", "--untracked-files=no"]);
|
|
489
|
+
if (!dirty.ok) return { verdict: "skip", reason: "status-failed" };
|
|
490
|
+
if (dirty.stdout) return { verdict: "held", reason: "dirty" };
|
|
491
|
+
const unpushed = await git(entry.path, ["rev-list", "-1", "HEAD", "--not", "--remotes"]);
|
|
492
|
+
if (!unpushed.ok) return { verdict: "skip", reason: "rev-list-failed" };
|
|
493
|
+
if (unpushed.stdout) return { verdict: "held", reason: "unpushed" };
|
|
494
|
+
if (await this.ownerActive(entry, active)) return { verdict: "skip", reason: "owner-active" };
|
|
495
|
+
const young = ageHours === null || ageHours < ttlHours;
|
|
496
|
+
if (info.branch && young) {
|
|
497
|
+
const upstream = await git(entry.path, [
|
|
498
|
+
"rev-parse",
|
|
499
|
+
"--abbrev-ref",
|
|
500
|
+
`${info.branch}@{upstream}`,
|
|
501
|
+
]);
|
|
502
|
+
if (upstream.ok && !(await this.isDefaultUpstream(entry.repoRoot, upstream.stdout))) {
|
|
503
|
+
return { verdict: "keep", reason: "pushed-branch" };
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (entry.candidateTaskIds.length === 0 && young) return { verdict: "keep", reason: "young" };
|
|
507
|
+
return { verdict: "remove", reason: entry.candidateTaskIds.length ? "owners-ended" : "ttl" };
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
private async decideAndAct(
|
|
511
|
+
entry: LedgerEntry,
|
|
512
|
+
info: WorktreeInfo,
|
|
513
|
+
active: ActiveTaskRef[],
|
|
514
|
+
isSweep: boolean,
|
|
515
|
+
): Promise<ReapDecision> {
|
|
516
|
+
const { mode, ttlHours } = this.opts.getConfig();
|
|
517
|
+
const ageHours = await this.ageHours(entry);
|
|
518
|
+
const { verdict, reason } = await this.decide(entry, info, active, ttlHours, ageHours);
|
|
519
|
+
const decision: ReapDecision = {
|
|
520
|
+
path: entry.path,
|
|
521
|
+
repoRoot: entry.repoRoot,
|
|
522
|
+
action: verdict === "held" ? "skip" : verdict === "keep" ? "keep" : "skip",
|
|
523
|
+
reason,
|
|
524
|
+
taskIds: entry.candidateTaskIds,
|
|
525
|
+
ageHours: ageHours === null ? null : Math.round(ageHours * 10) / 10,
|
|
526
|
+
};
|
|
527
|
+
const dryRun = mode !== "on";
|
|
528
|
+
|
|
529
|
+
if (verdict === "held" && isSweep && ageHours !== null && ageHours >= ttlHours) {
|
|
530
|
+
// Rule 6 guards slimming too: a held worktree's owner may still need its deps.
|
|
531
|
+
if (await this.ownerActive(entry, active)) return decision;
|
|
532
|
+
const slimmed = await this.slim(entry.path, dryRun);
|
|
533
|
+
if (slimmed.length > 0) {
|
|
534
|
+
decision.action = dryRun ? "would-slim" : "slim";
|
|
535
|
+
decision.reason = `held-${reason}`;
|
|
536
|
+
this.log({ ...decision, nodeModules: slimmed });
|
|
537
|
+
}
|
|
538
|
+
return decision;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (verdict === "remove" || verdict === "finish-remove") {
|
|
542
|
+
decision.action = verdict === "remove" ? "remove" : "finish-remove";
|
|
543
|
+
if (dryRun) {
|
|
544
|
+
decision.action = verdict === "remove" ? "would-remove" : "would-finish-remove";
|
|
545
|
+
this.log({ ...decision });
|
|
546
|
+
return decision;
|
|
547
|
+
}
|
|
548
|
+
// Rule 3 again, right before the destructive step.
|
|
549
|
+
if (await this.inUse(await realpath(entry.path).catch(() => entry.path), active)) {
|
|
550
|
+
decision.action = "skip";
|
|
551
|
+
decision.reason = "in-use";
|
|
552
|
+
return decision;
|
|
553
|
+
}
|
|
554
|
+
const error = await this.remove(entry, verdict === "finish-remove");
|
|
555
|
+
if (error) {
|
|
556
|
+
decision.action = "error";
|
|
557
|
+
decision.reason = error;
|
|
558
|
+
}
|
|
559
|
+
this.log({ ...decision });
|
|
560
|
+
return decision;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (!isSweep) this.log({ ...decision });
|
|
564
|
+
return decision;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** Remove (or list, in dry-run) the gitignored `node_modules` directories under a worktree. */
|
|
568
|
+
private async slim(path: string, dryRun: boolean): Promise<string[]> {
|
|
569
|
+
const found = await Bun.$`find ${path} -name node_modules -type d -prune -print`
|
|
570
|
+
.quiet()
|
|
571
|
+
.nothrow();
|
|
572
|
+
const slimmed: string[] = [];
|
|
573
|
+
for (const dir of found.stdout.toString().split("\n").filter(Boolean)) {
|
|
574
|
+
if (!(await git(path, ["check-ignore", "-q", dir])).ok) continue;
|
|
575
|
+
if (!dryRun) await rm(dir, { recursive: true, force: true });
|
|
576
|
+
slimmed.push(dir);
|
|
577
|
+
}
|
|
578
|
+
return slimmed;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Returns an error string, or null on success. */
|
|
582
|
+
private async remove(entry: LedgerEntry, finishInterrupted: boolean): Promise<string | null> {
|
|
583
|
+
const ledger = await this.loadLedger();
|
|
584
|
+
entry.removingAt = new Date(this.now()).toISOString();
|
|
585
|
+
await this.saveLedger();
|
|
586
|
+
if (finishInterrupted) {
|
|
587
|
+
// Git still lists this path as a worktree of repoRoot and we started its
|
|
588
|
+
// removal, but its `.git` file is gone, so `worktree remove` refuses it.
|
|
589
|
+
await rm(entry.path, { recursive: true, force: true }).catch(() => undefined);
|
|
590
|
+
} else {
|
|
591
|
+
const removed = await git(entry.repoRoot, ["worktree", "remove", "--force", entry.path]);
|
|
592
|
+
if (!removed.ok) return `error:${removed.stderr.split("\n")[0] || "worktree remove failed"}`;
|
|
593
|
+
}
|
|
594
|
+
await git(entry.repoRoot, ["worktree", "prune"]);
|
|
595
|
+
if (await exists(entry.path)) return "error:path still present after remove";
|
|
596
|
+
delete ledger.entries[entry.path];
|
|
597
|
+
await this.saveLedger();
|
|
598
|
+
return null;
|
|
599
|
+
}
|
|
600
|
+
}
|