@desplega.ai/agent-swarm 1.160.0 → 1.162.0
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 +4 -3
- package/dist/{acp-adapter-2mjwcsfh.js → acp-adapter-74m73ae9.js} +163 -49
- package/dist/{actions-e43rxskr.js → actions-4sr14gnt.js} +42 -38
- package/dist/{anthropic-messages-5qx80j9k.js → anthropic-messages-8xxrzky9.js} +22 -26
- package/dist/{app-mc7xfagn.js → app-pt68fbwp.js} +17 -14
- package/dist/{artifact-3v5t9jtg.js → artifact-fey7dvj9.js} +3 -2
- package/dist/{assistant-sedkzv3b.js → assistant-cv3jytks.js} +43 -39
- package/dist/{azure-openai-responses-nfb21z9a.js → azure-openai-responses-f5cqsyj8.js} +15 -10
- package/dist/{boot-reembed-m3656j2n.js → boot-reembed-4ht4gqth.js} +17 -14
- package/dist/{boot-reembed-cqz9swn5.js → boot-reembed-nxpcvpj9.js} +16 -13
- package/dist/{boot-scrub-logs-xbxf5d7w.js → boot-scrub-logs-k93kyw8k.js} +13 -10
- package/dist/boot-seeds-1skcrs0e.js +32 -0
- package/dist/{claude-adapter-dc6289mc.js → claude-adapter-r5g0tzge.js} +39 -30
- package/dist/{claude-managed-adapter-naaw51me.js → claude-managed-adapter-yxkfxg8j.js} +6 -5
- package/dist/{claude-managed-setup-eacvchx0.js → claude-managed-setup-pkyrgshb.js} +27 -27
- package/dist/{claude-sdk-session-bqp2xvym.js → claude-sdk-session-jxyxnrwd.js} +8041 -7910
- package/dist/{cli-4derpgyk.js → cli-2hhfhfq5.js} +6 -1
- package/dist/{cli-rykfmgwg.js → cli-2x0e561b.js} +1 -1
- package/dist/{cli-tzx2ke1v.js → cli-2zy95vhm.js} +137 -64
- package/dist/{cli-0x01kwrd.js → cli-4cwxn7gp.js} +47 -186
- package/dist/{cli-xjcywfsf.js → cli-4dcpaefp.js} +12 -11
- package/dist/{cli-v7fn5gms.js → cli-4x0csdjq.js} +164 -76
- package/dist/{cli-12tszpk9.js → cli-5bcd7sfy.js} +91 -29
- package/dist/{cli-jz4w45qt.js → cli-5k6k8ta3.js} +190 -50
- package/dist/{cli-tpq83z89.js → cli-5xhpb3kv.js} +20 -19
- package/dist/{cli-r1mxd1xh.js → cli-7af8yhav.js} +6 -179
- package/dist/{cli-jajnsm8q.js → cli-7tre70zp.js} +18160 -31345
- package/dist/{cli-447dt1h3.js → cli-8ev9xqnx.js} +218 -25
- package/dist/cli-bsky8g7w.js +449 -0
- package/dist/{cli-0z2v4nhw.js → cli-cat6h73s.js} +1 -1
- package/dist/{cli-25fcq4b4.js → cli-cebmgwsc.js} +15 -11
- package/dist/{cli-shkxm1vn.js → cli-cvatsam7.js} +189 -13
- package/dist/{cli-2658f6at.js → cli-cvk6yzw5.js} +6 -8
- package/dist/cli-dbmwby6a.js +9 -0
- package/dist/{cli-ptr8h7kp.js → cli-eg0b3p0k.js} +5 -218
- package/dist/cli-f9w1semm.js +413 -0
- package/dist/{cli-523679eh.js → cli-g739zqzt.js} +6 -5
- package/dist/{cli-dv6cdjs2.js → cli-gd4favx9.js} +28 -7
- package/dist/{cli-c8a36vcc.js → cli-gpqpmns7.js} +2 -2
- package/dist/{cli-5nbmpy8w.js → cli-hb5c2x5y.js} +1 -1
- package/dist/{cli-741yb0xx.js → cli-hmsw1gpy.js} +55 -169
- package/dist/{cli-qydf22qq.js → cli-jf7wmv1r.js} +2562 -735
- package/dist/{cli-mfe5btp9.js → cli-jxs2ygaw.js} +317 -121
- package/dist/{cli-kmcpfwn2.js → cli-jyy6pm2f.js} +19 -3
- package/dist/cli-kdkwd3yr.js +615 -0
- package/dist/{cli-6cs6wzxt.js → cli-kh04eqwe.js} +2 -2
- package/dist/{cli-9hsgwz4w.js → cli-kp42yek0.js} +1309 -391
- package/dist/cli-ma3zvh39.js +10404 -0
- package/dist/{cli-ybgv4kw2.js → cli-mbssg1xv.js} +9 -2
- package/dist/{cli-jsp14b6c.js → cli-mzfth0bp.js} +38 -1
- package/dist/{cli-r2ap2czm.js → cli-nexqx4ve.js} +6 -0
- package/dist/{cli-f8gy6r2r.js → cli-nq7yqw7b.js} +19 -8
- package/dist/{cli-10nhj6xq.js → cli-nt8hecg9.js} +34 -33
- package/dist/cli-p0r8sxj5.js +303 -0
- package/dist/{cli-58e8yr0d.js → cli-pkd6s2wy.js} +4 -2
- package/dist/{cli-a8dvhzrt.js → cli-q24re23n.js} +7 -5
- package/dist/cli-qjjvzwzd.js +222 -0
- package/dist/cli-qrgt5bqf.js +189 -0
- package/dist/{cli-et8m9xsv.js → cli-rfjw9jw2.js} +22 -10
- package/dist/{cli-7e398j1h.js → cli-rj0gqwwp.js} +3 -3
- package/dist/{cli-btfv8y88.js → cli-rnyzn44p.js} +2 -2
- package/dist/{cli-d5czme6m.js → cli-s3hwpt9v.js} +4754 -4945
- package/dist/{cli-0jwe9034.js → cli-sptrj5f5.js} +3 -3
- package/dist/{cli-0q3avq3s.js → cli-sqvfa090.js} +19 -11
- package/dist/{cli-azhgzetg.js → cli-swfmp1fx.js} +27 -27
- package/dist/{cli-jyk0ynw2.js → cli-t845n9xk.js} +23 -10
- package/dist/{cli-zpep9q5e.js → cli-tcen9p5w.js} +20 -15
- package/dist/{cli-74h9hscv.js → cli-tre4bdgq.js} +1 -1
- package/dist/{cli-pm1rst0h.js → cli-twrh04ap.js} +30 -15
- package/dist/{cli-phsaj4xq.js → cli-v0pvvhfq.js} +1 -1
- package/dist/{cli-ydmcgve1.js → cli-vc9bp061.js} +5 -5
- package/dist/{cli-g2whhdrf.js → cli-vxe6qa4h.js} +1 -1
- package/dist/{cli-vctdqs5h.js → cli-vy3ed88x.js} +15 -2
- package/dist/{cli-br0qdrnp.js → cli-vyqs9ya8.js} +18 -18
- package/dist/{cli-cpvsaqsw.js → cli-w0bwte75.js} +8 -5
- package/dist/cli-wbvnn6tk.js +861 -0
- package/dist/{cli-r94z9p1z.js → cli-wgbezhw2.js} +3 -0
- package/dist/{cli-evvtk35y.js → cli-wjgs5wxe.js} +7 -7
- package/dist/cli-wxkkd0e0.js +33 -0
- package/dist/{cli-0wgxmwf6.js → cli-xk79303e.js} +20 -7
- package/dist/{cli-ktmyrtpn.js → cli-xqpk3rp7.js} +33 -10
- package/dist/{cli-ygwm91nt.js → cli-yy6dew39.js} +1 -1
- package/dist/cli-zp38xvtm.js +9487 -0
- package/dist/{cli-tfky2838.js → cli-zvhd3f64.js} +4 -4
- package/dist/{cli-jayjpjmb.js → cli-zxqx25nz.js} +66 -23
- package/dist/cli.js +40 -32
- package/dist/{cloudflare-workers-ai-system-one-k6fbxw9d.js → cloudflare-workers-ai-system-one-k8ddqjkq.js} +8 -6
- package/dist/{codex-adapter-nmxcatww.js → codex-adapter-esvmnhn7.js} +24 -23
- package/dist/{codex-hook-20me6byw.js → codex-hook-m73jmqgx.js} +19 -3
- package/dist/{codex-login-w6fz4xbz.js → codex-login-cf7mzsgg.js} +2 -2
- package/dist/{codex-session-runner-90aa80ev.js → codex-session-runner-ban9excr.js} +24 -23
- package/dist/{commands-8wcq1kn7.js → commands-75413p2n.js} +13 -10
- package/dist/{db-j40kc4se.js → db-7f5pecwg.js} +17 -10
- package/dist/{dispatcher-4zqy768d.js → dispatcher-fh9at8nz.js} +41 -37
- package/dist/dsh-adapter-wpze65d4.js +24 -0
- package/dist/{e2b-ez07znam.js → e2b-gjgdjp91.js} +12 -9
- package/dist/easter-egg-3d-3w5c2n4c.js +1057 -0
- package/dist/{execute-6e7qeyk4.js → execute-f50wg9nh.js} +203 -49
- package/dist/{google-generative-ai-k6n5nbpp.js → google-generative-ai-jxxpnegy.js} +2 -2
- package/dist/{google-vertex-44xxtcvp.js → google-vertex-x7q1angm.js} +5 -5
- package/dist/{handlers-kjdce1w5.js → handlers-w13787bb.js} +44 -40
- package/dist/{hook-r2t4bqfg.js → hook-46g1jrvx.js} +34 -20
- package/dist/{hook-czx13n12.js → hook-4tpts2ry.js} +25 -21
- package/dist/{http-3bbb19be.js → http-19pzbfrt.js} +1440 -283
- package/dist/{index-668me3f0.js → index-8fd4dm4h.js} +42 -38
- package/dist/{index-fg0dsp6c.js → index-dqmp15bq.js} +41 -37
- package/dist/{index-kkv84jq9.js → index-qb4q1y3e.js} +41 -37
- package/dist/index-s75wc4wd.js +33 -0
- package/dist/{index-zg7vp46x.js → index-scptvdxg.js} +64 -38
- package/dist/{keepalive-yra8dy57.js → keepalive-xa5ts9cd.js} +15 -12
- package/dist/{lead-va0pad2c.js → lead-qp4xkvjq.js} +59 -54
- package/dist/{maintenance-hqewtmyg.js → maintenance-rm5885hm.js} +17 -14
- package/dist/{mistral-conversations-pc3ntnxt.js → mistral-conversations-rw7fbt22.js} +4 -4
- package/dist/{node-a5e00qhp.js → node-8zkdq42h.js} +834 -833
- package/dist/node-f0ykebbv.js +2107 -0
- package/dist/node-wyamey5r.js +19 -0
- package/dist/{oauth-refresh-sweep-7rdt1m9n.js → oauth-refresh-sweep-1csdfebf.js} +14 -11
- package/dist/{onboard-j6ms6d0x.js → onboard-r6b78cs4.js} +10 -10
- package/dist/{openai-codex-responses-bbancrr0.js → openai-codex-responses-efvrgrq3.js} +4 -4
- package/dist/{openai-completions-jpaj5g2b.js → openai-completions-bbbrrz4z.js} +12 -8
- package/dist/{openai-responses-200k1wx4.js → openai-responses-9jjyrcm1.js} +15 -10
- package/dist/{opencode-adapter-89rabkw2.js → opencode-adapter-kb3g6pkf.js} +12 -9
- package/dist/{otel-impl-a792rjz0.js → otel-impl-0y9knpky.js} +6 -6
- package/dist/{pi-mono-adapter-g7qzvr3v.js → pi-mono-adapter-z6s1bme2.js} +128 -37
- package/dist/{pricing-refresh-wx0tp7cb.js → pricing-refresh-mth26tgp.js} +15 -12
- package/dist/{rbac-roles-8tzawacv.js → rbac-roles-kwntb23q.js} +17 -12
- package/dist/rbac-roles-vpk4ebm8.js +45 -0
- package/dist/{registry-v5wytg0s.js → registry-km6t1jd2.js} +1 -1
- package/dist/{render-v2-mm7zswb8.js → render-v2-hcm7h00y.js} +17 -14
- package/dist/{runtime-zyssgse0.js → runtime-vfjsfsc5.js} +5 -4
- package/dist/{setup-8y70ys6t.js → setup-hbyzd65h.js} +2 -2
- package/dist/{virtual-modules-6nmk7h3n.js → virtual-modules-9bv7ry91.js} +9 -7
- package/dist/{work-objects-fg6xkb0n.js → work-objects-m09sxnhe.js} +44 -40
- package/dist/{worker-wmwhxsph.js → worker-0h35rvk4.js} +59 -54
- package/dist/{x-n3e0v3xa.js → x-f1gt1pz3.js} +2 -2
- package/openapi.json +699 -26
- package/package.json +19 -11
- package/src/azure-devops/api.ts +121 -0
- package/src/azure-devops/auth.ts +78 -0
- package/src/azure-devops/handlers.ts +413 -0
- package/src/azure-devops/index.ts +27 -0
- package/src/azure-devops/templates.ts +108 -0
- package/src/azure-devops/types.ts +90 -0
- package/src/be/api-drain.ts +184 -0
- package/src/be/db/tasks/read.ts +32 -7
- package/src/be/db/tasks/write.ts +21 -4
- package/src/be/db-client.ts +11 -2
- package/src/be/db-retention.ts +95 -32
- package/src/be/db.ts +44 -19
- package/src/be/memory/constants.ts +16 -0
- package/src/be/memory/graph-expansion.ts +8 -1
- package/src/be/memory/index-content.ts +20 -1
- package/src/be/memory/key-guard.ts +93 -0
- package/src/be/memory/key-paths.ts +103 -0
- package/src/be/memory/link-resolver.ts +29 -3
- package/src/be/memory/providers/sqlite-store.ts +152 -45
- package/src/be/memory/raters/llm-summarizer.ts +10 -4
- package/src/be/memory/raters/llm.ts +15 -5
- package/src/be/memory/raters/store.ts +6 -3
- package/src/be/memory/raters/types.ts +11 -0
- package/src/be/memory/reranker.ts +38 -7
- package/src/be/memory/types.ts +12 -1
- package/src/be/migrations/190_memory_rating_model.sql +7 -0
- package/src/be/migrations/191_context_versions_previous_index.sql +8 -0
- package/src/be/migrations/192_favorites_agent_fs_path.sql +36 -0
- package/src/be/onboarding.ts +7 -1
- package/src/be/pricing-normalize.ts +4 -2
- package/src/be/scripts/typecheck.ts +4 -3
- package/src/be/seed-pricing.ts +16 -0
- package/src/be/seed-prompt-templates.ts +1 -0
- package/src/be/swarm-config-guard.ts +46 -17
- package/src/be/task-citations.ts +11 -5
- package/src/be/users.ts +3 -0
- package/src/cli.tsx +3 -2
- package/src/comb/agent-fs.ts +39 -0
- package/src/comb/config.ts +37 -0
- package/src/comb/markers.ts +37 -0
- package/src/comb/review-batch.ts +486 -0
- package/src/comb/templates.ts +88 -0
- package/src/commands/claude-managed-setup.ts +33 -31
- package/src/commands/context-preamble.ts +4 -1
- package/src/commands/hook.ts +2 -1
- package/src/commands/provider-credentials.ts +28 -15
- package/src/commands/runner.ts +458 -110
- package/src/commands/templates.ts +10 -0
- package/src/e2b/env.ts +2 -0
- package/src/extensions/catalog.generated.json +17 -0
- package/src/extensions/contract-types.generated.ts +1 -1
- package/src/fs/agent-fs-provider.ts +106 -2
- package/src/github/handlers.ts +10 -74
- package/src/github/reactions.ts +119 -138
- package/src/github/task-reactions.ts +34 -11
- package/src/heartbeat/heartbeat.ts +4 -0
- package/src/hooks/codex-hook.ts +25 -1
- package/src/hooks/hook.ts +14 -1
- package/src/hooks/pr-body-guard.ts +983 -0
- package/src/http/agents.ts +1 -1
- package/src/http/all-routes.ts +1 -0
- package/src/http/assets.ts +5 -17
- package/src/http/comb.ts +131 -0
- package/src/http/core.ts +14 -1
- package/src/http/extensions.ts +51 -27
- package/src/http/fs.ts +9 -17
- package/src/http/index.ts +27 -9
- package/src/http/mcp-user.ts +8 -13
- package/src/http/mcp.ts +19 -12
- package/src/http/memory.ts +80 -14
- package/src/http/onboarding.ts +21 -2
- package/src/http/poll.ts +30 -3
- package/src/http/realtime.ts +62 -0
- package/src/http/request-principal.ts +22 -0
- package/src/http/session-data.ts +6 -4
- package/src/http/stats.ts +1 -0
- package/src/http/status.ts +34 -2
- package/src/http/tasks.ts +17 -3
- package/src/http/users.ts +1 -1
- package/src/http/webhooks.ts +120 -2
- package/src/kv-reserved-namespaces.ts +10 -5
- package/src/providers/claude-adapter.ts +84 -16
- package/src/providers/claude-managed-adapter.ts +29 -2
- package/src/providers/codex-adapter.ts +1 -1
- package/src/providers/codex-skill-resolver.ts +6 -0
- package/src/providers/dsh-adapter.ts +204 -7
- package/src/providers/opencode-adapter.ts +24 -3
- package/src/providers/pi-mono-adapter.ts +154 -5
- package/src/providers/pi-mono-extension.ts +1 -1
- package/src/providers/pricing-sources.md +5 -1
- package/src/providers/reasoning-effort.ts +10 -2
- package/src/providers/types.ts +1 -1
- package/src/rbac/legacy-policy.ts +3 -0
- package/src/rbac/permissions.ts +14 -0
- package/src/realtime/auth.ts +63 -1
- package/src/realtime/tickets.ts +52 -0
- package/src/realtime/transport.ts +51 -22
- package/src/scripts-runtime/types/stdlib.d.ts +13 -5
- package/src/scripts-runtime/types/swarm-extension.d.ts +7 -3
- package/src/scripts-runtime/types/swarm-sdk.d.ts +13 -5
- package/src/tasks/context-key.ts +38 -1
- package/src/telemetry-context.ts +175 -0
- package/src/telemetry-contract/EVENTS.md +81 -0
- package/src/telemetry-contract/SOURCE.json +9 -0
- package/src/telemetry-contract/catalog.json +1335 -0
- package/src/telemetry-contract/index.ts +60 -0
- package/src/telemetry-contract/types.gen.ts +1971 -0
- package/src/telemetry-identity.ts +129 -0
- package/src/telemetry-snapshot.ts +211 -0
- package/src/telemetry-trigger.ts +125 -0
- package/src/telemetry-types.typetest.ts +40 -0
- package/src/telemetry.ts +271 -67
- package/src/tests/api-drain-e2e.test.ts +249 -0
- package/src/tests/api-drain-worker-e2e.test.ts +216 -0
- package/src/tests/api-drain.test.ts +351 -0
- package/src/tests/azure-devops-auth.test.ts +100 -0
- package/src/tests/azure-devops-handlers.test.ts +324 -0
- package/src/tests/azure-devops-webhook-malformed.test.ts +96 -0
- package/src/tests/claude-default-route.test.ts +260 -0
- package/src/tests/claude-managed-adapter.test.ts +47 -0
- package/src/tests/claude-managed-setup.test.ts +48 -0
- package/src/tests/claude-prompt-body-once.test.ts +117 -0
- package/src/tests/claude-routes-e2e.test.ts +316 -0
- package/src/tests/comb-config-guard.test.ts +38 -0
- package/src/tests/comb-links.test.ts +162 -0
- package/src/tests/comb-markers.test.ts +68 -0
- package/src/tests/comb-review-batch.test.ts +770 -0
- package/src/tests/comb-status.test.ts +152 -0
- package/src/tests/context-versions-retention.test.ts +314 -0
- package/src/tests/core-auth.test.ts +7 -0
- package/src/tests/db-retention.test.ts +14 -14
- package/src/tests/db-task-read-contract.test.ts +3 -0
- package/src/tests/db-task-write-contract.test.ts +3 -0
- package/src/tests/docs-index-footprint.test.js +3 -3
- package/src/tests/dsh-adapter.test.ts +134 -2
- package/src/tests/dsh-usage.test.ts +153 -0
- package/src/tests/extensions-catalog.test.ts +1 -0
- package/src/tests/extensions-example-deploy-awareness.test.ts +301 -0
- package/src/tests/extensions-http.test.ts +137 -10
- package/src/tests/extensions-mcp-tools.test.ts +28 -0
- package/src/tests/favorites-agent-fs-path.test.ts +226 -0
- package/src/tests/fetch-resolved-env.test.ts +24 -0
- package/src/tests/fixtures/azure-devops/pull-request-commented.json +129 -0
- package/src/tests/fixtures/azure-devops/pull-request-created.json +88 -0
- package/src/tests/fixtures/fake-claude-routes.ts +229 -0
- package/src/tests/github-handlers-reactions.test.ts +486 -0
- package/src/tests/github-reactions.test.ts +280 -0
- package/src/tests/harness-cli-version.test.ts +62 -0
- package/src/tests/http-api-integration.test.ts +21 -0
- package/src/tests/http-task-write-ownership.test.ts +24 -1
- package/src/tests/internal-ai/complete-structured.test.ts +94 -0
- package/src/tests/internal-ai/credentials.test.ts +22 -0
- package/src/tests/internal-ai/models.test.ts +36 -2
- package/src/tests/internal-ai/schema-parity.test.ts +25 -1
- package/src/tests/internal-ai/summarize-session.test.ts +23 -7
- package/src/tests/mcp-user-route.test.ts +133 -0
- package/src/tests/memory-key-paths.test.ts +1055 -0
- package/src/tests/memory-link-resolver.test.ts +34 -0
- package/src/tests/memory-rate-endpoint.test.ts +66 -0
- package/src/tests/memory-rater-llm-summarizer.test.ts +2 -1
- package/src/tests/memory-rater-llm.test.ts +73 -0
- package/src/tests/memory-rater-store.test.ts +31 -0
- package/src/tests/memory-reranker.test.ts +40 -0
- package/src/tests/onboarding.test.ts +108 -0
- package/src/tests/opencode-adapter.test.ts +57 -0
- package/src/tests/opencode-plugin.test.ts +4 -0
- package/src/tests/pi-codemode.test.ts +82 -4
- package/src/tests/pi-mono-adapter.test.ts +265 -2
- package/src/tests/pr-body-leaks.test.ts +789 -0
- package/src/tests/preload.ts +14 -0
- package/src/tests/provider-registration.test.ts +0 -2
- package/src/tests/providers/pi-cost.test.ts +190 -4
- package/src/tests/rbac-engine.test.ts +25 -1
- package/src/tests/realtime-room-auth.test.ts +104 -1
- package/src/tests/realtime-tickets.test.ts +74 -0
- package/src/tests/realtime-transport.test.ts +131 -4
- package/src/tests/runner-api-drain.test.ts +301 -0
- package/src/tests/runner-fallback-output.test.ts +61 -0
- package/src/tests/runner-fenced-json-output.test.ts +178 -0
- package/src/tests/runner-slot-reconcile.test.ts +199 -0
- package/src/tests/script-connections.test.ts +6 -0
- package/src/tests/send-task-requested-by.test.ts +133 -15
- package/src/tests/session-costs-recompute-all-providers.test.ts +23 -0
- package/src/tests/task-lifecycle-telemetry.test.ts +8 -3
- package/src/tests/task-reactions.test.ts +29 -6
- package/src/tests/telemetry-context.test.ts +314 -0
- package/src/tests/telemetry-init.test.ts +51 -19
- package/src/tests/telemetry-snapshot.test.ts +229 -0
- package/src/tests/telemetry-trigger.test.ts +200 -0
- package/src/tests/template-registry-helpers.ts +1 -0
- package/src/tests/vcs-azure-devops.test.ts +73 -0
- package/src/tests/webhooks-task-blocked.test.ts +23 -0
- package/src/tests/workflow-agent-task-retry.test.ts +363 -0
- package/src/tests/workflow-definition-telemetry.test.ts +4 -4
- package/src/tests/workflow-llm.test.ts +14 -0
- package/src/tools/context-diff.ts +20 -0
- package/src/tools/extension-install.ts +27 -16
- package/src/tools/memory-edit.ts +47 -3
- package/src/tools/memory-search.ts +13 -1
- package/src/tools/memory-store.ts +55 -3
- package/src/tools/poll-task.ts +20 -1
- package/src/tools/send-task.ts +9 -1
- package/src/tools/task-action.ts +11 -0
- package/src/types.ts +45 -8
- package/src/utils/api-drain.ts +24 -0
- package/src/utils/constants.ts +57 -16
- package/src/utils/credentials.test.ts +31 -1
- package/src/utils/credentials.ts +20 -9
- package/src/utils/dsh-usage.ts +53 -0
- package/src/utils/harness-cli-version.ts +6 -4
- package/src/utils/internal-ai/complete-structured.ts +26 -3
- package/src/utils/internal-ai/credentials.ts +23 -9
- package/src/utils/internal-ai/index.ts +8 -1
- package/src/utils/internal-ai/models.ts +34 -8
- package/src/utils/internal-ai/summarize-session.ts +22 -8
- package/src/utils/pr-body-leaks.ts +172 -0
- package/src/utils/secret-scrubber.ts +8 -0
- package/src/utils/task-attachment-links.ts +14 -8
- package/src/vcs/azure-devops.ts +80 -0
- package/src/vcs/index.ts +5 -1
- package/src/vcs/types.ts +2 -2
- package/src/workflows/recovery.ts +26 -1
- package/src/workflows/resume.ts +31 -4
- package/src/workflows/task-step-routing.ts +75 -2
- package/templates/community/open-prompt-showreel/PLAYBOOK.md +336 -0
- package/templates/community/open-prompt-showreel/README.md +33 -0
- package/templates/community/open-prompt-showreel/example/tla-races/facts.ts +145 -0
- package/templates/community/open-prompt-showreel/example/tla-races/facts.v3.json +140 -0
- package/templates/community/open-prompt-showreel/example/tla-races/music.mist-city.json +8 -0
- package/templates/community/open-prompt-showreel/example/tla-races/reel.html +805 -0
- package/templates/community/open-prompt-showreel/example/tla-races/smoke.sh +48 -0
- package/templates/community/open-prompt-showreel/lead-prompt.md +89 -0
- package/templates/community/open-prompt-showreel/setup.sh +167 -0
- package/templates/community/open-prompt-showreel/skills/motion-design-replication/SKILL.md +85 -0
- package/templates/community/open-prompt-showreel/skills/motion-design-video-analysis/SKILL.md +115 -0
- package/templates/community/open-prompt-showreel/skills/open-prompt-showreel/SKILL.md +147 -0
- package/templates/community/open-prompt-showreel/skills/video-generation/SKILL.md +195 -0
- package/templates/community/open-prompt-showreel/tools/fetch-music.sh +65 -0
- package/templates/community/open-prompt-showreel/tools/music-analyze.py +69 -0
- package/templates/community/open-prompt-showreel/tools/music-current.sh +34 -0
- package/templates/community/open-prompt-showreel/tools/music-cut.sh +36 -0
- package/templates/community/open-prompt-showreel/tools/music-refine.py +39 -0
- package/templates/community/open-prompt-showreel/tools/render.mjs +157 -0
- package/templates/community/open-prompt-showreel/tools/sheet.sh +44 -0
- package/templates/community/open-prompt-showreel/tools/web-compress.sh +17 -0
- package/templates/community/open-prompt-showreel/tools/x-reencode.sh +21 -0
- package/templates/extensions/README.md +1 -0
- package/templates/extensions/deploy-awareness/README.md +28 -0
- package/templates/extensions/deploy-awareness/hooks.ts +152 -0
- package/templates/extensions/deploy-awareness/manifest.json +8 -0
- package/templates/skills/memory/config.json +2 -2
- package/templates/skills/memory/content.md +59 -5
- package/templates/skills/swarm-extensions/config.json +1 -1
- package/templates/skills/swarm-extensions/content.md +28 -5
- package/tsconfig.json +3 -0
- package/dist/boot-seeds-gsetbs5y.js +0 -28
- package/dist/cli-j490egb5.js +0 -5632
- package/dist/cli-tjf4cn8z.js +0 -9119
- package/dist/cli-tmqt7dpj.js +0 -572
- package/dist/dsh-adapter-mpnzgztj.js +0 -19
- package/dist/index-7aj368tw.js +0 -30
|
@@ -0,0 +1,983 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PreToolUse guard: block `gh pr create` / `gh pr edit` when the PR title or
|
|
3
|
+
* body for a PUBLIC repo carries internal identifiers (see
|
|
4
|
+
* `src/utils/pr-body-leaks.ts`).
|
|
5
|
+
*
|
|
6
|
+
* Shared by the Claude hook (`hook.ts`) and the codex hook (`codex-hook.ts`).
|
|
7
|
+
* The block reason names leak categories only, never the matched text.
|
|
8
|
+
*
|
|
9
|
+
* What gets scanned: the resolved title and body, not the whole command.
|
|
10
|
+
* - `--title` / `--body` values (any flag form: `-b"x"`, `--body=x`, ...).
|
|
11
|
+
* - `--body-file PATH`, read relative to the `cd` target.
|
|
12
|
+
* - `--body-file -` fed by a heredoc, here-string or `< PATH`.
|
|
13
|
+
* - `--body-file -` fed by a pipe, or a body the shell builds from an
|
|
14
|
+
* unquoted `$(...)`: the guard cannot isolate the text, so it scans the
|
|
15
|
+
* whole command instead.
|
|
16
|
+
*
|
|
17
|
+
* Which repo: `--repo` / `-R` in any position (also before `pr`), the PR URL
|
|
18
|
+
* given to `gh pr edit` (after expanding `$VAR` / `${VAR}`), `GH_REPO`, else
|
|
19
|
+
* the checkout at the `cd` target. Every explicit target is checked; any
|
|
20
|
+
* public one blocks. Variables come from the hook env and the command itself
|
|
21
|
+
* (assignments, `export`, `unset`, subshells, `bash -c`, `eval`); where the
|
|
22
|
+
* parser cannot follow the shell, the variable is unknown (see
|
|
23
|
+
* `parseGhPrCommands`).
|
|
24
|
+
*
|
|
25
|
+
* Failure modes:
|
|
26
|
+
* - Text scanned clean: allow without a visibility lookup.
|
|
27
|
+
* - Text has leaks, or `--body-file` is unreadable: look up the repo
|
|
28
|
+
* visibility. PUBLIC blocks; PRIVATE / INTERNAL allows.
|
|
29
|
+
* - Visibility lookup fails, a `cd` target cannot be resolved (`cd -`,
|
|
30
|
+
* unset or unknown variable, a `cd` that may not have run), a `gh pr edit` target is neither a PR
|
|
31
|
+
* number, a branch nor a PR URL (unset or unknown variable, `$(...)`), or
|
|
32
|
+
* `GH_REPO` is unknown: block (fail closed). We
|
|
33
|
+
* already know the body is unsafe or unreadable, and we cannot prove the
|
|
34
|
+
* repo is private. An unknown edit target never falls back to the checkout.
|
|
35
|
+
* - Body comes from an unexpanded shell variable (`--body "$BODY"`): the
|
|
36
|
+
* guard cannot see it and allows (fail open). CI still checks the body.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import { isAbsolute, resolve } from "node:path";
|
|
40
|
+
import { findPrBodyLeaks, type LeakCategory } from "../utils/pr-body-leaks";
|
|
41
|
+
|
|
42
|
+
export type GhPrInvocation = {
|
|
43
|
+
/** Working directory of the `gh` call after any `cd`; null when a `cd` target could not be resolved. */
|
|
44
|
+
cwd: string | null;
|
|
45
|
+
/** Explicit target repos: `--repo` / `-R`, a PR URL, or `GH_REPO`. Empty means the checkout at `cwd`. */
|
|
46
|
+
repos: string[];
|
|
47
|
+
/** Also check the checkout: `GH_REPO` was assigned without `export`, so gh may not see it. */
|
|
48
|
+
alsoCheckout: boolean;
|
|
49
|
+
/** The `gh pr edit` target could not be classified (unset variable, `$(...)`, non-PR URL), so the PR's repo is unknown. */
|
|
50
|
+
unknownTarget: boolean;
|
|
51
|
+
/** Literal public text: `--title` and `--body` values, heredoc or here-string stdin. */
|
|
52
|
+
texts: string[];
|
|
53
|
+
/** Files that become the body, expanded: `--body-file PATH`, or `--body-file -` with `< PATH`. */
|
|
54
|
+
bodyFiles: string[];
|
|
55
|
+
/** Body file paths whose variables could not be resolved, as written. They count as unreadable. */
|
|
56
|
+
unresolvedBodyFiles: string[];
|
|
57
|
+
/** The body text could not be isolated (piped stdin, unquoted `$(...)`); scan the whole command. */
|
|
58
|
+
opaqueBody: boolean;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type PrBodyGuardDeps = {
|
|
62
|
+
readFile: (path: string) => Promise<string>;
|
|
63
|
+
/** Returns GitHub's `visibility` (PUBLIC, PRIVATE, INTERNAL). Throws when the lookup fails. */
|
|
64
|
+
repoVisibility: (target: { cwd: string; repo?: string }) => Promise<string>;
|
|
65
|
+
env: Record<string, string | undefined>;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* One simple command: its words, plus any heredoc / here-string text fed to its
|
|
70
|
+
* stdin. `depth` counts the open `(` before it (subshells and `$(`); it goes
|
|
71
|
+
* negative on an unmatched `)` (a `case` pattern). `async` marks a pipeline
|
|
72
|
+
* member or a member of a `&` background list, which runs in a subshell.
|
|
73
|
+
* `ops` lists the separators between the previous command and this one.
|
|
74
|
+
*/
|
|
75
|
+
export type ShellSegment = {
|
|
76
|
+
words: string[];
|
|
77
|
+
stdin: string[];
|
|
78
|
+
depth: number;
|
|
79
|
+
async: boolean;
|
|
80
|
+
ops: string[];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const SEPARATORS = ["&&", "||", ";", "|", "&", "\n", "(", ")"];
|
|
84
|
+
const COMMAND_WRAPPERS = new Set(["env", "command", "exec", "time", "sudo", "nohup"]);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Split a shell command into simple commands. Handles quotes, backslashes,
|
|
88
|
+
* comments, heredocs and here-strings, not expansions.
|
|
89
|
+
*/
|
|
90
|
+
export function tokenizeShell(command: string): ShellSegment[] {
|
|
91
|
+
let depth = 0;
|
|
92
|
+
const segments: ShellSegment[] = [{ words: [], stdin: [], depth, async: false, ops: [] }];
|
|
93
|
+
const current = () => segments[segments.length - 1] as ShellSegment;
|
|
94
|
+
// First segment of the current list, per `(` level: a `&` backgrounds all of it.
|
|
95
|
+
const listStarts = [0];
|
|
96
|
+
// After `&&`, `||` or `|`, a newline continues the list.
|
|
97
|
+
let continued = false;
|
|
98
|
+
const heredocs: Array<{ delimiter: string; stripTabs: boolean; segment: ShellSegment }> = [];
|
|
99
|
+
let hereString = false;
|
|
100
|
+
let word = "";
|
|
101
|
+
let inWord = false;
|
|
102
|
+
let quote: "'" | '"' | null = null;
|
|
103
|
+
const push = () => {
|
|
104
|
+
if (inWord) {
|
|
105
|
+
if (hereString) current().stdin.push(word);
|
|
106
|
+
else current().words.push(word);
|
|
107
|
+
hereString = false;
|
|
108
|
+
continued = false;
|
|
109
|
+
}
|
|
110
|
+
word = "";
|
|
111
|
+
inWord = false;
|
|
112
|
+
};
|
|
113
|
+
for (let i = 0; i < command.length; i++) {
|
|
114
|
+
const ch = command[i] as string;
|
|
115
|
+
if (quote === "'") {
|
|
116
|
+
if (ch === "'") quote = null;
|
|
117
|
+
else word += ch;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (quote === '"') {
|
|
121
|
+
if (ch === '"') quote = null;
|
|
122
|
+
else if (
|
|
123
|
+
ch === "\\" &&
|
|
124
|
+
i + 1 < command.length &&
|
|
125
|
+
'"\\$`'.includes(command[i + 1] as string)
|
|
126
|
+
) {
|
|
127
|
+
word += command[++i];
|
|
128
|
+
} else word += ch;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (ch === "'" || ch === '"') {
|
|
132
|
+
quote = ch;
|
|
133
|
+
inWord = true;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
137
|
+
if (command[i + 1] !== "\n") {
|
|
138
|
+
word += command[i + 1];
|
|
139
|
+
inWord = true;
|
|
140
|
+
}
|
|
141
|
+
i++;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (ch === "#" && !inWord) {
|
|
145
|
+
// Comment: skip to the end of the line, keep the newline.
|
|
146
|
+
while (i + 1 < command.length && command[i + 1] !== "\n") i++;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (command.startsWith("<<<", i)) {
|
|
150
|
+
push();
|
|
151
|
+
hereString = true;
|
|
152
|
+
i += 2;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (command.startsWith("<<", i)) {
|
|
156
|
+
push();
|
|
157
|
+
let j = i + 2;
|
|
158
|
+
const stripTabs = command[j] === "-";
|
|
159
|
+
if (stripTabs) j++;
|
|
160
|
+
while (command[j] === " " || command[j] === "\t") j++;
|
|
161
|
+
let delimiter = "";
|
|
162
|
+
while (j < command.length && !/[\s;&|()<>]/.test(command[j] as string)) {
|
|
163
|
+
if (!"'\"\\".includes(command[j] as string)) delimiter += command[j];
|
|
164
|
+
j++;
|
|
165
|
+
}
|
|
166
|
+
if (delimiter) heredocs.push({ delimiter, stripTabs, segment: current() });
|
|
167
|
+
i = j - 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (ch === "\n" && heredocs.length > 0) {
|
|
171
|
+
// Heredoc bodies start on the next line, in the order they were opened.
|
|
172
|
+
push();
|
|
173
|
+
let j = i + 1;
|
|
174
|
+
for (const doc of heredocs) {
|
|
175
|
+
const lines: string[] = [];
|
|
176
|
+
while (j < command.length) {
|
|
177
|
+
const newline = command.indexOf("\n", j);
|
|
178
|
+
const lineEnd = newline === -1 ? command.length : newline;
|
|
179
|
+
const line = command.slice(j, lineEnd);
|
|
180
|
+
j = lineEnd + 1;
|
|
181
|
+
if ((doc.stripTabs ? line.replace(/^\t+/, "") : line) === doc.delimiter) break;
|
|
182
|
+
lines.push(line);
|
|
183
|
+
}
|
|
184
|
+
doc.segment.stdin.push(lines.join("\n"));
|
|
185
|
+
}
|
|
186
|
+
heredocs.length = 0;
|
|
187
|
+
segments.push({ words: [], stdin: [], depth, async: false, ops: ["\n"] });
|
|
188
|
+
if (!continued) listStarts[listStarts.length - 1] = segments.length - 1;
|
|
189
|
+
i = j - 1;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
const sep = SEPARATORS.find((s) => command.startsWith(s, i));
|
|
193
|
+
if (sep) {
|
|
194
|
+
push();
|
|
195
|
+
if (sep === "|") current().async = true;
|
|
196
|
+
if (sep === "&") {
|
|
197
|
+
// `a && b &` runs the whole and-or list in the background.
|
|
198
|
+
for (let k = listStarts[listStarts.length - 1] as number; k < segments.length; k++) {
|
|
199
|
+
(segments[k] as ShellSegment).async = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (sep === "(") depth++;
|
|
203
|
+
if (sep === ")") depth--;
|
|
204
|
+
segments.push({ words: [], stdin: [], depth, async: sep === "|", ops: [sep] });
|
|
205
|
+
if (sep === "(") listStarts.push(segments.length - 1);
|
|
206
|
+
else if (sep === ")") {
|
|
207
|
+
if (listStarts.length > 1) listStarts.pop();
|
|
208
|
+
} else if (sep === ";" || sep === "&" || (sep === "\n" && !continued)) {
|
|
209
|
+
listStarts[listStarts.length - 1] = segments.length - 1;
|
|
210
|
+
}
|
|
211
|
+
continued = sep === "&&" || sep === "||" || sep === "|" || (sep === "\n" && continued);
|
|
212
|
+
i += sep.length - 1;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (ch === " " || ch === "\t") {
|
|
216
|
+
push();
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
word += ch;
|
|
220
|
+
inWord = true;
|
|
221
|
+
}
|
|
222
|
+
push();
|
|
223
|
+
for (const doc of heredocs) doc.segment.stdin.push("");
|
|
224
|
+
// Drop empty segments; their separators belong to the next command.
|
|
225
|
+
let ops: string[] = [];
|
|
226
|
+
return segments.filter((s) => {
|
|
227
|
+
ops.push(...s.ops);
|
|
228
|
+
if (s.words.length === 0) return false;
|
|
229
|
+
s.ops = ops;
|
|
230
|
+
ops = [];
|
|
231
|
+
return true;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Expand `~`, `$VAR` and `${VAR}`. Returns null when a variable is unset. */
|
|
236
|
+
function expandPath(path: string, env: Record<string, string | undefined>): string | null {
|
|
237
|
+
let unresolved = false;
|
|
238
|
+
const expanded = path
|
|
239
|
+
.replace(/^~(?=\/|$)/, env.HOME ?? "~")
|
|
240
|
+
.replace(/\$\{(\w+)\}|\$(\w+)/g, (_, braced: string | undefined, bare: string | undefined) => {
|
|
241
|
+
const value = env[(braced ?? bare) as string];
|
|
242
|
+
if (value === undefined) unresolved = true;
|
|
243
|
+
return value ?? "";
|
|
244
|
+
});
|
|
245
|
+
return unresolved ? null : expanded;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Value-taking flags of `gh` and `gh pr create|edit`, keyed by long name. Others are boolean. */
|
|
249
|
+
const VALUE_FLAGS = new Set([
|
|
250
|
+
"repo",
|
|
251
|
+
"body",
|
|
252
|
+
"body-file",
|
|
253
|
+
"title",
|
|
254
|
+
"base",
|
|
255
|
+
"head",
|
|
256
|
+
"assignee",
|
|
257
|
+
"label",
|
|
258
|
+
"milestone",
|
|
259
|
+
"project",
|
|
260
|
+
"reviewer",
|
|
261
|
+
"template",
|
|
262
|
+
"recover",
|
|
263
|
+
"add-assignee",
|
|
264
|
+
"add-label",
|
|
265
|
+
"add-project",
|
|
266
|
+
"add-reviewer",
|
|
267
|
+
"remove-assignee",
|
|
268
|
+
"remove-label",
|
|
269
|
+
"remove-project",
|
|
270
|
+
"remove-reviewer",
|
|
271
|
+
]);
|
|
272
|
+
const SHORT_FLAGS: Record<string, string> = {
|
|
273
|
+
R: "repo",
|
|
274
|
+
b: "body",
|
|
275
|
+
F: "body-file",
|
|
276
|
+
t: "title",
|
|
277
|
+
B: "base",
|
|
278
|
+
H: "head",
|
|
279
|
+
a: "assignee",
|
|
280
|
+
l: "label",
|
|
281
|
+
m: "milestone",
|
|
282
|
+
p: "project",
|
|
283
|
+
r: "reviewer",
|
|
284
|
+
T: "template",
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
type ParsedArgs = {
|
|
288
|
+
positionals: string[];
|
|
289
|
+
flags: Array<[name: string, value: string]>;
|
|
290
|
+
/** `< PATH` stdin redirect. */
|
|
291
|
+
stdinFile?: string;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Parse `gh` arguments the way pflag does: `--name value`, `--name=value`,
|
|
296
|
+
* `-x value`, `-xvalue`, `-x=value`, and boolean clusters such as `-dF body.md`.
|
|
297
|
+
*/
|
|
298
|
+
function parseGhArgs(args: string[]): ParsedArgs {
|
|
299
|
+
const parsed: ParsedArgs = { positionals: [], flags: [] };
|
|
300
|
+
for (let i = 0; i < args.length; i++) {
|
|
301
|
+
const arg = args[i] as string;
|
|
302
|
+
const redirect = /^\d*(<|>>?|>\|)(&?)(.*)$/.exec(arg);
|
|
303
|
+
if (redirect) {
|
|
304
|
+
const target = redirect[3] || (args[++i] ?? "");
|
|
305
|
+
if (redirect[1] === "<" && !redirect[2]) parsed.stdinFile = target;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (arg === "--") {
|
|
309
|
+
parsed.positionals.push(...args.slice(i + 1));
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
if (arg.startsWith("--")) {
|
|
313
|
+
const eq = arg.indexOf("=");
|
|
314
|
+
const name = arg.slice(2, eq === -1 ? undefined : eq);
|
|
315
|
+
if (eq !== -1) parsed.flags.push([name, arg.slice(eq + 1)]);
|
|
316
|
+
else parsed.flags.push([name, VALUE_FLAGS.has(name) ? (args[++i] ?? "") : ""]);
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (arg.startsWith("-") && arg.length > 1) {
|
|
320
|
+
for (let k = 1; k < arg.length; k++) {
|
|
321
|
+
const name = SHORT_FLAGS[arg[k] as string] ?? (arg[k] as string);
|
|
322
|
+
if (!VALUE_FLAGS.has(name)) {
|
|
323
|
+
parsed.flags.push([name, ""]);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const attached = arg.slice(k + 1).replace(/^=/, "");
|
|
327
|
+
parsed.flags.push([name, k + 1 < arg.length ? attached : (args[++i] ?? "")]);
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
parsed.positionals.push(arg);
|
|
333
|
+
}
|
|
334
|
+
return parsed;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** `OWNER/REPO` (or `HOST/OWNER/REPO`) from a PR URL, else undefined. */
|
|
338
|
+
function repoFromPrUrl(target: string | undefined): string | undefined {
|
|
339
|
+
const match = /^https?:\/\/([^/]+)\/([^/]+)\/([^/]+)\/pull\/\d+/.exec(target ?? "");
|
|
340
|
+
if (!match) return undefined;
|
|
341
|
+
const [, host, owner, name] = match;
|
|
342
|
+
return host === "github.com" ? `${owner}/${name}` : `${host}/${owner}/${name}`;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
type EditTarget = { kind: "local" } | { kind: "url"; repo: string } | { kind: "unknown" };
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Classify the positional target of `gh pr edit` after expanding `$VAR` /
|
|
349
|
+
* `${VAR}`. No target, a PR number or a branch name resolve in the checkout
|
|
350
|
+
* (or `-R` repo); a PR URL names its own repo. Anything else (an unset
|
|
351
|
+
* variable, command substitution, a non-PR URL) is unknown.
|
|
352
|
+
*/
|
|
353
|
+
function classifyEditTarget(
|
|
354
|
+
target: string | undefined,
|
|
355
|
+
expand: (word: string) => string | null,
|
|
356
|
+
): EditTarget {
|
|
357
|
+
if (target === undefined) return { kind: "local" };
|
|
358
|
+
if (target.includes("`") || target.includes("$(")) return { kind: "unknown" };
|
|
359
|
+
const expanded = expand(target);
|
|
360
|
+
if (expanded === null) return { kind: "unknown" };
|
|
361
|
+
const repo = repoFromPrUrl(expanded);
|
|
362
|
+
if (repo) return { kind: "url", repo };
|
|
363
|
+
if (/^#?\d+$/.test(expanded)) return { kind: "local" };
|
|
364
|
+
if (/^[\w.+@:/-]+$/.test(expanded) && !expanded.startsWith("-") && !expanded.includes("://")) {
|
|
365
|
+
return { kind: "local" };
|
|
366
|
+
}
|
|
367
|
+
return { kind: "unknown" };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
type FrameKind = "top" | "paren" | "if" | "loop" | "case" | "brace";
|
|
371
|
+
/** A variable or the working directory, changed by a command the parser applied. */
|
|
372
|
+
type Write = { name: string } | { dir: true };
|
|
373
|
+
/**
|
|
374
|
+
* A list context: the whole command, a `( ... )`, or a compound command
|
|
375
|
+
* (`if`, a loop, `case`, `{ ... }`).
|
|
376
|
+
*/
|
|
377
|
+
type Frame = {
|
|
378
|
+
kind: FrameKind;
|
|
379
|
+
/** The operator before the current element of the and-or list. */
|
|
380
|
+
tail: "none" | "and" | "or";
|
|
381
|
+
/** Writes applied by `&&` elements of the current and-or list. */
|
|
382
|
+
list: Write[];
|
|
383
|
+
/** Writes applied inside this compound command (this branch, for `if` and `case`). */
|
|
384
|
+
block: Write[];
|
|
385
|
+
};
|
|
386
|
+
const newFrame = (kind: FrameKind): Frame => ({ kind, tail: "none", list: [], block: [] });
|
|
387
|
+
const isCompound = (frame: Frame) => frame.kind !== "top" && frame.kind !== "paren";
|
|
388
|
+
|
|
389
|
+
type ReservedWords = {
|
|
390
|
+
/** Index of the first word after the leading reserved words. */
|
|
391
|
+
start: number;
|
|
392
|
+
/** Compound commands opened (by kind), branched (`else`, `elif`) or closed, in order. */
|
|
393
|
+
actions: Array<FrameKind | "branch" | "close">;
|
|
394
|
+
/** `function NAME`. */
|
|
395
|
+
functionKeyword: boolean;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
/** Read the reserved words that lead a simple command (`if gh ...`, `then X=1`, `fi`). */
|
|
399
|
+
function scanReservedWords(words: string[]): ReservedWords {
|
|
400
|
+
const actions: ReservedWords["actions"] = [];
|
|
401
|
+
let functionKeyword = false;
|
|
402
|
+
let k = 0;
|
|
403
|
+
for (; k < words.length; k++) {
|
|
404
|
+
const word = words[k] as string;
|
|
405
|
+
if (word === "if") actions.push("if");
|
|
406
|
+
else if (word === "while" || word === "until") actions.push("loop");
|
|
407
|
+
else if (word === "{") actions.push("brace");
|
|
408
|
+
else if (word === "else" || word === "elif") actions.push("branch");
|
|
409
|
+
else if (word === "fi" || word === "done" || word === "esac" || word === "}") {
|
|
410
|
+
actions.push("close");
|
|
411
|
+
} else if (word === "function" && k + 1 < words.length) {
|
|
412
|
+
functionKeyword = true;
|
|
413
|
+
k++; // the name
|
|
414
|
+
} else if (word !== "then" && word !== "do" && word !== "!") break;
|
|
415
|
+
}
|
|
416
|
+
// `for`, `select` and `case` take a header: the rest of this segment.
|
|
417
|
+
if (words[k] === "for" || words[k] === "select") actions.push("loop");
|
|
418
|
+
if (words[k] === "case") actions.push("case");
|
|
419
|
+
return { start: k, actions, functionKeyword };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** For each segment that opens a loop, the index of the segment that closes it. */
|
|
423
|
+
function loopEnds(segments: ShellSegment[]): Array<number | undefined> {
|
|
424
|
+
const ends: Array<number | undefined> = [];
|
|
425
|
+
const open: Array<{ kind: FrameKind; index: number }> = [];
|
|
426
|
+
segments.forEach((segment, index) => {
|
|
427
|
+
for (const action of scanReservedWords(segment.words).actions) {
|
|
428
|
+
if (action === "close") {
|
|
429
|
+
const opened = open.pop();
|
|
430
|
+
if (opened?.kind === "loop") ends[opened.index] = index;
|
|
431
|
+
} else if (action !== "branch") open.push({ kind: action, index });
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
for (const opened of open) {
|
|
435
|
+
if (opened.kind === "loop") ends[opened.index] = segments.length - 1;
|
|
436
|
+
}
|
|
437
|
+
return ends;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Commands that change variables in ways the parser does not model. After one,
|
|
442
|
+
* every variable expansion in the command is unknown.
|
|
443
|
+
*/
|
|
444
|
+
const UNMODELED_VARIABLE_COMMANDS = new Set([
|
|
445
|
+
"read",
|
|
446
|
+
"source",
|
|
447
|
+
".",
|
|
448
|
+
"eval",
|
|
449
|
+
"declare",
|
|
450
|
+
"typeset",
|
|
451
|
+
"local",
|
|
452
|
+
"readonly",
|
|
453
|
+
"mapfile",
|
|
454
|
+
"readarray",
|
|
455
|
+
"getopts",
|
|
456
|
+
"let",
|
|
457
|
+
"case",
|
|
458
|
+
]);
|
|
459
|
+
/** POSIX special builtins. In POSIX mode, assignments that precede one persist. */
|
|
460
|
+
const SPECIAL_BUILTINS = new Set([
|
|
461
|
+
":",
|
|
462
|
+
".",
|
|
463
|
+
"break",
|
|
464
|
+
"continue",
|
|
465
|
+
"eval",
|
|
466
|
+
"exec",
|
|
467
|
+
"exit",
|
|
468
|
+
"export",
|
|
469
|
+
"readonly",
|
|
470
|
+
"return",
|
|
471
|
+
"set",
|
|
472
|
+
"shift",
|
|
473
|
+
"times",
|
|
474
|
+
"trap",
|
|
475
|
+
"unset",
|
|
476
|
+
]);
|
|
477
|
+
/** `NAME=value`, `NAME+=value` or `NAME[i]=value`. */
|
|
478
|
+
const ASSIGNMENT = /^([A-Za-z_]\w*)(\[[^\]]*\])?(\+?)=([\s\S]*)$/;
|
|
479
|
+
|
|
480
|
+
type ShellState = {
|
|
481
|
+
env: Record<string, string | undefined>;
|
|
482
|
+
exported: Set<string>;
|
|
483
|
+
unknownVars: Set<string>;
|
|
484
|
+
dir: string | null;
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Find every `gh pr create|edit` call in a shell command.
|
|
489
|
+
*
|
|
490
|
+
* The parser follows the shell variables and working directory that decide a
|
|
491
|
+
* call's target and body file. Where it cannot follow the shell, it marks a
|
|
492
|
+
* variable (or every variable) unknown instead of guessing, so a target or body
|
|
493
|
+
* file that uses one fails closed:
|
|
494
|
+
* - `NAME=value` alone or in `export` sets the variable. A value with `$`, a
|
|
495
|
+
* backtick or `~` makes it unknown, as does `NAME+=` or `NAME[i]=`.
|
|
496
|
+
* - The parser does not know which commands run. A write (assignment,
|
|
497
|
+
* `export`, `unset`, `cd`) counts only for the commands that run only if
|
|
498
|
+
* it ran: the later `&&` elements of its and-or list, and the rest of its
|
|
499
|
+
* compound command branch. After that, the variable (or directory) is
|
|
500
|
+
* unknown until a write that always runs. A write after `||` is unknown at
|
|
501
|
+
* once. A loop body is read twice: its writes are unknown from its start.
|
|
502
|
+
* - A write in a pipeline member or a `&` list makes the variable (or
|
|
503
|
+
* directory) unknown. Leaving a `( ... )` subshell restores the variables
|
|
504
|
+
* and directory from before it.
|
|
505
|
+
* - A function definition makes every later expansion and the directory
|
|
506
|
+
* unknown: a call can run its body anywhere after it.
|
|
507
|
+
* - `read`, `source`, `.`, `eval`, `declare`, `typeset`, `local`,
|
|
508
|
+
* `readonly`, `mapfile`, `readarray`, `getopts`, `let`, `case`,
|
|
509
|
+
* `printf -v`, `export` with a flag, or a backtick outside a `gh` call
|
|
510
|
+
* make every later expansion unknown. `for NAME` and `${NAME:=x}` make
|
|
511
|
+
* NAME unknown.
|
|
512
|
+
*/
|
|
513
|
+
export function parseGhPrCommands(
|
|
514
|
+
command: string,
|
|
515
|
+
cwd: string,
|
|
516
|
+
env: Record<string, string | undefined> = process.env,
|
|
517
|
+
): GhPrInvocation[] {
|
|
518
|
+
return parseWithState(
|
|
519
|
+
command,
|
|
520
|
+
{
|
|
521
|
+
env: { ...env },
|
|
522
|
+
exported: new Set(Object.keys(env).filter((name) => env[name] !== undefined)),
|
|
523
|
+
unknownVars: new Set(),
|
|
524
|
+
dir: cwd,
|
|
525
|
+
},
|
|
526
|
+
{ allUnknown: false, dirUnknown: false },
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** Set for the rest of a command: every expansion, or the working directory, is unknown. */
|
|
531
|
+
type UnknownFlags = { allUnknown: boolean; dirUnknown: boolean };
|
|
532
|
+
|
|
533
|
+
const copyState = (s: ShellState): ShellState => ({
|
|
534
|
+
env: { ...s.env },
|
|
535
|
+
exported: new Set(s.exported),
|
|
536
|
+
unknownVars: new Set(s.unknownVars),
|
|
537
|
+
dir: s.dir,
|
|
538
|
+
});
|
|
539
|
+
const copyFrame = (f: Frame): Frame => ({ ...f, list: [...f.list], block: [...f.block] });
|
|
540
|
+
const isGhWord = (word: string | undefined) => word === "gh" || word?.endsWith("/gh") === true;
|
|
541
|
+
const isShellWord = (word: string | undefined) => /(^|\/)(ba|da|k|z)?sh$/.test(word ?? "");
|
|
542
|
+
|
|
543
|
+
function parseWithState(
|
|
544
|
+
command: string,
|
|
545
|
+
initial: ShellState,
|
|
546
|
+
initialFlags: UnknownFlags,
|
|
547
|
+
): GhPrInvocation[] {
|
|
548
|
+
const found: GhPrInvocation[] = [];
|
|
549
|
+
let state = initial;
|
|
550
|
+
let { allUnknown, dirUnknown } = initialFlags;
|
|
551
|
+
const scopes: ShellState[] = [];
|
|
552
|
+
let frames: Frame[] = [newFrame("top")];
|
|
553
|
+
const top = () => frames[frames.length - 1] as Frame;
|
|
554
|
+
const copy = copyState;
|
|
555
|
+
|
|
556
|
+
/** Expand `~`, `$VAR` and `${VAR}`. Null when a variable is unset or unknown, or an expansion is left. */
|
|
557
|
+
const expand = (word: string): string | null => {
|
|
558
|
+
const names = [...word.matchAll(/\$\{(\w+)\}|\$(\w+)/g)].map((m) => (m[1] ?? m[2]) as string);
|
|
559
|
+
if (/^~(?=\/|$)/.test(word)) {
|
|
560
|
+
if (state.env.HOME === undefined) return null;
|
|
561
|
+
names.push("HOME");
|
|
562
|
+
}
|
|
563
|
+
if (names.length > 0 && (allUnknown || names.some((name) => state.unknownVars.has(name)))) {
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
const expanded = expandPath(word, state.env);
|
|
567
|
+
return expanded === null || /[$`]/.test(expanded) ? null : expanded;
|
|
568
|
+
};
|
|
569
|
+
/** The writes may not have run: make each variable or the directory unknown. */
|
|
570
|
+
const forget = (writes: Write[]) => {
|
|
571
|
+
for (const write of writes) {
|
|
572
|
+
if ("dir" in write) state.dir = null;
|
|
573
|
+
else state.unknownVars.add(write.name);
|
|
574
|
+
}
|
|
575
|
+
writes.length = 0;
|
|
576
|
+
};
|
|
577
|
+
/** Remember an applied write, to forget it where the parser cannot tell whether it ran. */
|
|
578
|
+
const applied = (write: Write) => {
|
|
579
|
+
const frame = top();
|
|
580
|
+
if (frame.tail === "and") frame.list.push(write);
|
|
581
|
+
if (isCompound(frame)) frame.block.push(write);
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/** Process one segment. `dry` reads it for its writes only, all of them unknown. */
|
|
585
|
+
const step = (segment: ShellSegment, dry: boolean) => {
|
|
586
|
+
const { words, stdin, depth, ops } = segment;
|
|
587
|
+
while (scopes.length < depth) scopes.push(copy(state));
|
|
588
|
+
while (scopes.length > Math.max(depth, 0)) {
|
|
589
|
+
// Leaving a subshell restores its state; unknown variables stay unknown.
|
|
590
|
+
const outer = scopes.pop() as ShellState;
|
|
591
|
+
outer.unknownVars = new Set([...outer.unknownVars, ...state.unknownVars]);
|
|
592
|
+
state = outer;
|
|
593
|
+
}
|
|
594
|
+
if (depth < 0) {
|
|
595
|
+
// An unmatched `)` (a `case` pattern): the subshell model no longer holds.
|
|
596
|
+
allUnknown = true;
|
|
597
|
+
state.dir = null;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
let continued = false;
|
|
601
|
+
for (const op of ops) {
|
|
602
|
+
const frame = top();
|
|
603
|
+
if (op === "&&" || op === "||") {
|
|
604
|
+
if (op === "||") forget(frame.list);
|
|
605
|
+
frame.tail = op === "&&" ? "and" : "or";
|
|
606
|
+
continued = true;
|
|
607
|
+
} else if (op === "|") {
|
|
608
|
+
continued = true;
|
|
609
|
+
} else if (op === "(") {
|
|
610
|
+
frames.push(newFrame("paren"));
|
|
611
|
+
continued = false;
|
|
612
|
+
} else if (op === ")") {
|
|
613
|
+
// A subshell's writes stay in it. In `case`, `)` starts another arm.
|
|
614
|
+
if (frame.kind === "paren") frames.pop();
|
|
615
|
+
else if (frame.kind === "case") {
|
|
616
|
+
forget(frame.block);
|
|
617
|
+
forget(frame.list);
|
|
618
|
+
frame.tail = "none";
|
|
619
|
+
}
|
|
620
|
+
continued = false;
|
|
621
|
+
} else if (!(op === "\n" && continued)) {
|
|
622
|
+
// `;`, `&` or a newline ends the and-or list.
|
|
623
|
+
forget(frame.list);
|
|
624
|
+
frame.tail = "none";
|
|
625
|
+
continued = false;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
const reserved = scanReservedWords(words);
|
|
629
|
+
if (reserved.functionKeyword || ops.some((op, k) => op === "(" && ops[k + 1] === ")")) {
|
|
630
|
+
allUnknown = true;
|
|
631
|
+
dirUnknown = true;
|
|
632
|
+
}
|
|
633
|
+
for (const action of reserved.actions) {
|
|
634
|
+
const frame = top();
|
|
635
|
+
if (action === "branch" || action === "close") {
|
|
636
|
+
forget(frame.block);
|
|
637
|
+
forget(frame.list);
|
|
638
|
+
frame.tail = "none";
|
|
639
|
+
if (action === "close" && isCompound(frame)) frames.pop();
|
|
640
|
+
} else frames.push(newFrame(action));
|
|
641
|
+
}
|
|
642
|
+
// A write here may not run, or may not persist: it makes its target unknown.
|
|
643
|
+
const uncertain = dry || segment.async || top().tail === "or";
|
|
644
|
+
|
|
645
|
+
/** Set a shell variable; `value` null means the parser cannot know it. */
|
|
646
|
+
const assign = (name: string, value: string | null) => {
|
|
647
|
+
if (uncertain || value === null || /[$`]|^~|:~/.test(value)) {
|
|
648
|
+
state.unknownVars.add(name);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
state.env[name] = value;
|
|
652
|
+
state.unknownVars.delete(name);
|
|
653
|
+
applied({ name });
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
for (const word of words) {
|
|
657
|
+
for (const match of word.matchAll(/\$\{(\w+):?=/g)) state.unknownVars.add(match[1] as string);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
const start = reserved.start;
|
|
661
|
+
let i = start;
|
|
662
|
+
while (i < words.length && ASSIGNMENT.test(words[i] as string)) i++;
|
|
663
|
+
const assignments = words.slice(start, i).map((w) => ASSIGNMENT.exec(w) as RegExpExecArray);
|
|
664
|
+
const rest = words.slice(i);
|
|
665
|
+
const cmd = rest[0];
|
|
666
|
+
if (cmd === undefined) {
|
|
667
|
+
// Only assignments: they set shell variables (exported only if already exported).
|
|
668
|
+
for (const [, name, index, append, value] of assignments) {
|
|
669
|
+
assign(name as string, index || append ? null : (value as string));
|
|
670
|
+
}
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
if (SPECIAL_BUILTINS.has(cmd)) {
|
|
674
|
+
for (const [, name] of assignments) state.unknownVars.add(name as string);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (cmd === "cd" || cmd === "pushd") {
|
|
678
|
+
const operands = rest.slice(1);
|
|
679
|
+
while (operands[0] !== undefined && /^-[LPe@]+$/.test(operands[0])) operands.shift();
|
|
680
|
+
if (operands[0] === "--") operands.shift();
|
|
681
|
+
if (operands.length > 1) return; // bash: too many arguments, cd fails
|
|
682
|
+
const operand = operands[0] ?? "~";
|
|
683
|
+
const target = operand === "-" ? null : expand(operand);
|
|
684
|
+
const dir = state.dir;
|
|
685
|
+
if (uncertain || target === null) state.dir = null;
|
|
686
|
+
else {
|
|
687
|
+
state.dir = isAbsolute(target) ? target : dir === null ? null : resolve(dir, target);
|
|
688
|
+
applied({ dir: true });
|
|
689
|
+
}
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
if (cmd === "popd") {
|
|
693
|
+
state.dir = null;
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
if (cmd === "export") {
|
|
697
|
+
for (const word of rest.slice(1)) {
|
|
698
|
+
if (word.startsWith("-")) {
|
|
699
|
+
allUnknown = true; // `export -n`, `-f`, `-p`
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
const match = ASSIGNMENT.exec(word);
|
|
703
|
+
const name = match?.[1] ?? word;
|
|
704
|
+
if (match) assign(name, match[2] || match[3] ? null : (match[4] as string));
|
|
705
|
+
if (uncertain) state.unknownVars.add(name);
|
|
706
|
+
else {
|
|
707
|
+
state.exported.add(name);
|
|
708
|
+
applied({ name });
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
if (cmd === "unset") {
|
|
714
|
+
const operands = rest.slice(1);
|
|
715
|
+
if (operands.some((w) => /^-\w*f/.test(w))) return; // functions, not variables
|
|
716
|
+
for (const name of operands.filter((w) => !w.startsWith("-"))) {
|
|
717
|
+
if (uncertain) {
|
|
718
|
+
state.unknownVars.add(name);
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
state.env[name] = undefined;
|
|
722
|
+
state.exported.delete(name);
|
|
723
|
+
state.unknownVars.delete(name);
|
|
724
|
+
applied({ name });
|
|
725
|
+
}
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
if (cmd === "for" || cmd === "select") {
|
|
729
|
+
const name = rest[1] ?? "";
|
|
730
|
+
if (/^[A-Za-z_]\w*$/.test(name)) state.unknownVars.add(name);
|
|
731
|
+
else allUnknown = true;
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
if (
|
|
735
|
+
UNMODELED_VARIABLE_COMMANDS.has(cmd) ||
|
|
736
|
+
(cmd === "printf" && rest.some((w) => /^-v/.test(w)))
|
|
737
|
+
) {
|
|
738
|
+
// `eval` runs its words as a command in this shell.
|
|
739
|
+
if (cmd === "eval" && !dry) {
|
|
740
|
+
found.push(
|
|
741
|
+
...parseWithState(rest.slice(1).join(" "), copy(state), { allUnknown, dirUnknown }),
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
allUnknown = true;
|
|
745
|
+
if (cmd === "source" || cmd === "." || cmd === "eval") state.dir = null; // may `cd`
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// `gh` (or a nested shell) must be the command word: only env assignments
|
|
750
|
+
// or wrappers may precede it. A wrapper may take flags (`env -u GH_REPO`,
|
|
751
|
+
// `env -C dir`): then the cwd and `GH_REPO` that gh sees are unknown.
|
|
752
|
+
const offset = rest.findIndex((w) => !ASSIGNMENT.test(w) && !COMMAND_WRAPPERS.has(w));
|
|
753
|
+
let gh = offset === -1 ? -1 : i + offset;
|
|
754
|
+
if (COMMAND_WRAPPERS.has(cmd) && !isGhWord(words[gh]) && !isShellWord(words[gh])) {
|
|
755
|
+
gh = words.findIndex((w, k) => k > i && (isGhWord(w) || isShellWord(w)));
|
|
756
|
+
}
|
|
757
|
+
const wrapperFlags = gh > i && words.slice(i, gh).some((w) => w.startsWith("-"));
|
|
758
|
+
if (isShellWord(words[gh])) {
|
|
759
|
+
// `bash -c SCRIPT`: parse SCRIPT in a child shell that sees exported variables.
|
|
760
|
+
const flag = words.findIndex((w, k) => k > gh && /^-\w*c\w*$/.test(w));
|
|
761
|
+
const script = flag === -1 ? undefined : words[flag + 1];
|
|
762
|
+
if (script !== undefined && !dry) {
|
|
763
|
+
const child = copy(state);
|
|
764
|
+
for (const name of Object.keys(child.env)) {
|
|
765
|
+
if (!child.exported.has(name)) child.env[name] = undefined;
|
|
766
|
+
}
|
|
767
|
+
for (const w of words.slice(start, gh)) {
|
|
768
|
+
const match = ASSIGNMENT.exec(w);
|
|
769
|
+
if (!match) continue;
|
|
770
|
+
const name = match[1] as string;
|
|
771
|
+
child.exported.add(name);
|
|
772
|
+
if (match[2] || match[3] || /[$`]|^~|:~/.test(match[4] as string))
|
|
773
|
+
child.unknownVars.add(name);
|
|
774
|
+
else child.env[name] = match[4];
|
|
775
|
+
}
|
|
776
|
+
if (wrapperFlags) child.dir = null;
|
|
777
|
+
found.push(
|
|
778
|
+
...parseWithState(script, child, {
|
|
779
|
+
allUnknown: allUnknown || wrapperFlags,
|
|
780
|
+
dirUnknown,
|
|
781
|
+
}),
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
if (!isGhWord(words[gh])) {
|
|
787
|
+
// A backtick splits `X=`a b`` into words the parser cannot assign.
|
|
788
|
+
if (words.some((w) => w.includes("`"))) allUnknown = true;
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
if (dry) return;
|
|
792
|
+
const args = parseGhArgs(words.slice(gh + 1));
|
|
793
|
+
const [group, action, target] = args.positionals;
|
|
794
|
+
if (group !== "pr" || (action !== "create" && action !== "edit")) return;
|
|
795
|
+
|
|
796
|
+
const values = (...names: string[]) =>
|
|
797
|
+
args.flags.filter(([name]) => names.includes(name)).map(([, value]) => value);
|
|
798
|
+
const prefixRepo = words
|
|
799
|
+
.slice(start, gh)
|
|
800
|
+
.find((w) => w.startsWith("GH_REPO="))
|
|
801
|
+
?.slice("GH_REPO=".length);
|
|
802
|
+
const editTarget: EditTarget =
|
|
803
|
+
action === "edit" ? classifyEditTarget(target, expand) : { kind: "local" };
|
|
804
|
+
let repos = [...values("repo"), ...(editTarget.kind === "url" ? [editTarget.repo] : [])];
|
|
805
|
+
let unknownTarget = editTarget.kind === "unknown";
|
|
806
|
+
let alsoCheckout = false;
|
|
807
|
+
if (repos.length === 0) {
|
|
808
|
+
if (prefixRepo) repos = [prefixRepo];
|
|
809
|
+
else if (allUnknown || wrapperFlags || state.unknownVars.has("GH_REPO")) unknownTarget = true;
|
|
810
|
+
else if (state.env.GH_REPO) {
|
|
811
|
+
repos = [state.env.GH_REPO];
|
|
812
|
+
// A plain `GH_REPO=x` reaches gh only when exported (`set -a`): check the checkout too.
|
|
813
|
+
alsoCheckout = !state.exported.has("GH_REPO");
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const inline = values("title", "body");
|
|
818
|
+
const files = values("body-file").filter((file) => file !== "-");
|
|
819
|
+
const fromStdin = values("body-file").includes("-");
|
|
820
|
+
if (fromStdin && args.stdinFile !== undefined) files.push(args.stdinFile);
|
|
821
|
+
const bodyFiles: string[] = [];
|
|
822
|
+
const unresolvedBodyFiles: string[] = [];
|
|
823
|
+
for (const file of files) {
|
|
824
|
+
const expanded = expand(file);
|
|
825
|
+
if (expanded === null) unresolvedBodyFiles.push(file);
|
|
826
|
+
else bodyFiles.push(expanded);
|
|
827
|
+
}
|
|
828
|
+
found.push({
|
|
829
|
+
cwd: wrapperFlags || dirUnknown ? null : state.dir,
|
|
830
|
+
repos: [...new Set(repos)],
|
|
831
|
+
alsoCheckout,
|
|
832
|
+
unknownTarget,
|
|
833
|
+
texts: [...inline, ...(fromStdin ? stdin : [])],
|
|
834
|
+
bodyFiles,
|
|
835
|
+
unresolvedBodyFiles,
|
|
836
|
+
// An unquoted `$(` splits at the paren, leaving a value that ends in `$`.
|
|
837
|
+
// In the edit target, it also cuts every later flag off this segment.
|
|
838
|
+
opaqueBody:
|
|
839
|
+
inline.some((value) => value.endsWith("$")) ||
|
|
840
|
+
(action === "edit" && target?.endsWith("$") === true) ||
|
|
841
|
+
(fromStdin && stdin.length === 0 && args.stdinFile === undefined),
|
|
842
|
+
});
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
const segments = tokenizeShell(command);
|
|
846
|
+
const ends = loopEnds(segments);
|
|
847
|
+
segments.forEach((segment, index) => {
|
|
848
|
+
const end = ends[index];
|
|
849
|
+
if (end !== undefined) {
|
|
850
|
+
// A loop body can run again after any of its writes: read it once for
|
|
851
|
+
// its writes, so they are unknown from the top of the body.
|
|
852
|
+
const saved = frames.map(copyFrame);
|
|
853
|
+
for (let k = index; k <= end; k++) step(segments[k] as ShellSegment, true);
|
|
854
|
+
frames = saved;
|
|
855
|
+
}
|
|
856
|
+
step(segment, false);
|
|
857
|
+
});
|
|
858
|
+
return found;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
const BLOCK_ADVICE =
|
|
862
|
+
"Paraphrase the motivation ('a maintainer asked for X') and link only public sources (Fixes #N, a public PR or issue). Internal provenance stays in the swarm task.";
|
|
863
|
+
|
|
864
|
+
/** Check a shell command. Returns a block reason, or null to allow. */
|
|
865
|
+
export async function checkGhPrCommand(
|
|
866
|
+
command: string,
|
|
867
|
+
cwd: string,
|
|
868
|
+
deps: PrBodyGuardDeps,
|
|
869
|
+
): Promise<string | null> {
|
|
870
|
+
if (!/\bgh\b[\s\S]*\bpr\b/.test(command)) return null;
|
|
871
|
+
for (const call of parseGhPrCommands(command, cwd, deps.env)) {
|
|
872
|
+
const leaks = new Set<LeakCategory>();
|
|
873
|
+
for (const text of call.opaqueBody ? [...call.texts, command] : call.texts) {
|
|
874
|
+
for (const leak of findPrBodyLeaks(text)) leaks.add(leak);
|
|
875
|
+
}
|
|
876
|
+
let unreadable: string | undefined = call.unresolvedBodyFiles[0];
|
|
877
|
+
for (const bodyFile of call.bodyFiles) {
|
|
878
|
+
try {
|
|
879
|
+
const base = isAbsolute(bodyFile) ? "/" : call.cwd;
|
|
880
|
+
if (base === null) throw new Error("unknown working directory");
|
|
881
|
+
for (const leak of findPrBodyLeaks(await deps.readFile(resolve(base, bodyFile)))) {
|
|
882
|
+
leaks.add(leak);
|
|
883
|
+
}
|
|
884
|
+
} catch {
|
|
885
|
+
unreadable ??= bodyFile;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
if (leaks.size === 0 && unreadable === undefined) continue;
|
|
889
|
+
|
|
890
|
+
// An unknown edit target never falls back to the checkout: its repo is unknown.
|
|
891
|
+
const targets: Array<string | undefined | null> = call.unknownTarget
|
|
892
|
+
? [null]
|
|
893
|
+
: call.repos.length > 0
|
|
894
|
+
? [...call.repos, ...(call.alsoCheckout ? [undefined] : [])]
|
|
895
|
+
: [undefined];
|
|
896
|
+
for (const repo of targets) {
|
|
897
|
+
let visibility = "UNKNOWN";
|
|
898
|
+
if (repo !== null && (repo !== undefined || call.cwd !== null)) {
|
|
899
|
+
try {
|
|
900
|
+
visibility = (await deps.repoVisibility({ cwd: call.cwd ?? cwd, repo })).trim();
|
|
901
|
+
} catch {
|
|
902
|
+
visibility = "UNKNOWN";
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
if (visibility === "PRIVATE" || visibility === "INTERNAL") continue;
|
|
906
|
+
|
|
907
|
+
const where =
|
|
908
|
+
repo === null
|
|
909
|
+
? "a gh pr edit target that could not be resolved"
|
|
910
|
+
: visibility === "PUBLIC"
|
|
911
|
+
? "this public repo"
|
|
912
|
+
: "a repo whose visibility could not be confirmed";
|
|
913
|
+
if (leaks.size > 0) {
|
|
914
|
+
return `PR body leak check: the PR body for ${where} contains internal identifiers (${[...leaks].join(", ")}). ${BLOCK_ADVICE}`;
|
|
915
|
+
}
|
|
916
|
+
return `PR body leak check: could not read --body-file ${unreadable} for ${where}, so the body was not checked. Pass a literal path to a file that exists.`;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/** Real dependencies: the filesystem and `gh repo view`. */
|
|
923
|
+
export const defaultPrBodyGuardDeps = (
|
|
924
|
+
env: Record<string, string | undefined> = process.env,
|
|
925
|
+
): PrBodyGuardDeps => ({
|
|
926
|
+
env,
|
|
927
|
+
readFile: (path) => Bun.file(path).text(),
|
|
928
|
+
repoVisibility: async ({ cwd, repo }) => {
|
|
929
|
+
const proc = Bun.spawn(
|
|
930
|
+
[
|
|
931
|
+
"gh",
|
|
932
|
+
"repo",
|
|
933
|
+
"view",
|
|
934
|
+
...(repo ? [repo] : []),
|
|
935
|
+
"--json",
|
|
936
|
+
"visibility",
|
|
937
|
+
"--jq",
|
|
938
|
+
".visibility",
|
|
939
|
+
],
|
|
940
|
+
{ cwd, stdout: "pipe", stderr: "ignore", stdin: "ignore" },
|
|
941
|
+
);
|
|
942
|
+
const timer = setTimeout(() => proc.kill(), 10_000);
|
|
943
|
+
try {
|
|
944
|
+
const [out, code] = await Promise.all([new Response(proc.stdout).text(), proc.exited]);
|
|
945
|
+
if (code !== 0) throw new Error(`gh repo view exited ${code}`);
|
|
946
|
+
return out.trim();
|
|
947
|
+
} finally {
|
|
948
|
+
clearTimeout(timer);
|
|
949
|
+
}
|
|
950
|
+
},
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
/** The shell command of a Bash-like tool call, or null. Accepts a string or an argv array. */
|
|
954
|
+
export function shellCommandOf(toolInput: unknown): string | null {
|
|
955
|
+
const command = (toolInput as { command?: unknown } | undefined)?.command;
|
|
956
|
+
if (typeof command === "string") return command;
|
|
957
|
+
if (Array.isArray(command) && command.every((part) => typeof part === "string")) {
|
|
958
|
+
// ["bash", "-lc", "<script>"] -> the script; any other argv -> joined words.
|
|
959
|
+
if (command.length >= 3 && /^-\w*c$/.test(command[1] as string)) {
|
|
960
|
+
return command[command.length - 1] as string;
|
|
961
|
+
}
|
|
962
|
+
return command.join(" ");
|
|
963
|
+
}
|
|
964
|
+
return null;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* Entry point for both hooks. Returns a block reason or null. Never throws:
|
|
969
|
+
* an internal error allows the call (fail open), so a guard bug cannot halt
|
|
970
|
+
* every Bash call. CI's PR Body check is the backstop.
|
|
971
|
+
*/
|
|
972
|
+
export async function guardGhPrBody(
|
|
973
|
+
toolInput: unknown,
|
|
974
|
+
cwd: string,
|
|
975
|
+
deps: PrBodyGuardDeps = defaultPrBodyGuardDeps(),
|
|
976
|
+
): Promise<string | null> {
|
|
977
|
+
try {
|
|
978
|
+
const command = shellCommandOf(toolInput);
|
|
979
|
+
return command === null ? null : await checkGhPrCommand(command, cwd, deps);
|
|
980
|
+
} catch {
|
|
981
|
+
return null;
|
|
982
|
+
}
|
|
983
|
+
}
|