@anyswarm/core 0.5.0 → 0.5.1
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 +11 -10
- package/deploy/worker/Dockerfile +12 -4
- package/dist/deploy/worker/Dockerfile +12 -4
- package/dist/examples/README.md +24 -12
- package/dist/examples/authoring/README.md +24 -0
- package/dist/examples/authoring/business.d.ts +2 -0
- package/dist/examples/authoring/business.js +4 -0
- package/dist/examples/authoring/config.d.ts +5 -0
- package/dist/examples/authoring/config.js +2 -0
- package/dist/examples/authoring/eject.json +5 -0
- package/dist/examples/authoring/harness.d.ts +4 -0
- package/dist/examples/authoring/harness.js +19 -0
- package/dist/examples/autoresearch/README.md +33 -0
- package/dist/examples/autoresearch/REQUIREMENTS.zh.md +18 -0
- package/dist/examples/autoresearch/config.d.ts +51 -0
- package/dist/examples/autoresearch/config.js +57 -0
- package/dist/examples/autoresearch/demo.d.ts +11 -0
- package/dist/examples/autoresearch/demo.js +52 -0
- package/dist/examples/autoresearch/eject.json +15 -0
- package/dist/examples/autoresearch/frontiercs2.d.ts +87 -0
- package/dist/examples/autoresearch/frontiercs2.js +211 -0
- package/dist/examples/autoresearch/prepare.d.ts +95 -0
- package/dist/examples/autoresearch/prepare.js +257 -0
- package/dist/examples/autoresearch/process.d.ts +13 -0
- package/dist/examples/autoresearch/process.js +48 -0
- package/dist/examples/autoresearch/project.d.ts +115 -0
- package/dist/examples/autoresearch/project.js +410 -0
- package/dist/examples/autoresearch/research-service.d.ts +106 -0
- package/dist/examples/autoresearch/research-service.js +255 -0
- package/dist/examples/code/README.md +25 -0
- package/dist/examples/code/REQUIREMENTS.zh.md +11 -0
- package/dist/examples/code/candidate.d.ts +19 -0
- package/dist/examples/code/candidate.js +80 -0
- package/dist/examples/code/config.d.ts +32 -0
- package/dist/examples/code/config.js +18 -0
- package/dist/examples/code/eject.json +5 -0
- package/dist/examples/code/project.d.ts +3 -0
- package/dist/examples/code/project.js +605 -0
- package/dist/examples/code/prompts.d.ts +5 -0
- package/dist/examples/code/prompts.js +12 -0
- package/dist/examples/code/verification.d.ts +19 -0
- package/dist/examples/code/verification.js +268 -0
- package/dist/examples/code/worktree.d.ts +17 -0
- package/dist/examples/code/worktree.js +233 -0
- package/dist/examples/harness-session/README.md +3 -6
- package/dist/examples/lead-worker/harness/dynamic-scheduler.d.ts +2 -1
- package/dist/examples/lead-worker/harness/dynamic-scheduler.js +5 -2
- package/dist/examples/lead-worker/harness/index.d.ts +60 -0
- package/dist/examples/lead-worker/harness/index.js +77 -6
- package/dist/examples/lead-worker/project.js +2 -2
- package/dist/examples/lead-worker/services/board.d.ts +5 -0
- package/dist/examples/lead-worker/services/board.js +19 -1
- package/dist/examples/messages/README.md +5 -5
- package/dist/examples/messages/handoff.d.ts +2 -0
- package/dist/examples/messages/handoff.js +5 -1
- package/dist/examples/messages/project.js +52 -2
- package/dist/examples/notes/README.md +13 -18
- package/dist/examples/notes/project.js +67 -6
- package/dist/examples/peermesh/harness/router.js +3 -1
- package/dist/examples/peermesh/harness/team.d.ts +8 -0
- package/dist/examples/peermesh/harness/team.js +13 -0
- package/dist/examples/peermesh/project.js +22 -4
- package/dist/examples/services/agent-control/README.md +3 -3
- package/dist/examples/services/mathlib-search/Dockerfile +4 -2
- package/dist/examples/services/mathlib-search/README.md +1 -1
- package/dist/examples/services/mcp-managed/README.md +2 -2
- package/dist/examples/services/persistent-board/README.md +13 -3
- package/dist/examples/services/persistent-board/business.d.ts +7 -0
- package/dist/examples/services/persistent-board/business.js +7 -0
- package/dist/examples/services/persistent-board/index.js +10 -0
- package/dist/examples/services/persistent-board/live.d.ts +3 -0
- package/dist/examples/services/persistent-board/live.js +58 -0
- package/dist/examples/subagents/README.md +5 -5
- package/dist/examples/subagents/business.js +3 -1
- package/dist/examples/subagents/harness/controller.d.ts +15 -0
- package/dist/examples/subagents/harness/controller.js +181 -32
- package/dist/examples/subagents/project.d.ts +2 -0
- package/dist/examples/subagents/project.js +43 -34
- package/dist/examples/subagents/prompts/prompts.d.ts +1 -0
- package/dist/examples/subagents/prompts/prompts.js +3 -0
- package/dist/examples/subagents/services/collaboration-service.d.ts +3 -2
- package/dist/examples/subagents/services/collaboration-service.js +14 -2
- package/dist/examples/substrate/README.md +2 -2
- package/dist/examples/substrate/harness/index.d.ts +1 -1
- package/dist/examples/substrate/harness/index.js +77 -12
- package/dist/examples/substrate/project.js +4 -1
- package/dist/examples/super/README.md +40 -0
- package/dist/examples/super/actions.d.ts +95 -0
- package/dist/examples/super/actions.js +230 -0
- package/dist/examples/super/artifact.d.ts +4 -0
- package/dist/examples/super/artifact.js +51 -0
- package/dist/examples/super/config.d.ts +80 -0
- package/dist/examples/super/config.js +83 -0
- package/dist/examples/super/coordinator.d.ts +33 -0
- package/dist/examples/super/coordinator.js +151 -0
- package/dist/examples/super/daemon.d.ts +11 -0
- package/dist/examples/super/daemon.js +109 -0
- package/dist/examples/super/decisions.d.ts +8 -0
- package/dist/examples/super/decisions.js +52 -0
- package/dist/examples/super/eject.json +5 -0
- package/dist/examples/super/execution.d.ts +11 -0
- package/dist/examples/super/execution.js +136 -0
- package/dist/examples/super/github.d.ts +62 -0
- package/dist/examples/super/github.js +269 -0
- package/dist/examples/super/inbox.d.ts +162 -0
- package/dist/examples/super/inbox.js +284 -0
- package/dist/examples/super/location.d.ts +5 -0
- package/dist/examples/super/location.js +64 -0
- package/dist/examples/super/outbox.d.ts +5 -0
- package/dist/examples/super/outbox.js +103 -0
- package/dist/examples/super/project.d.ts +111 -0
- package/dist/examples/super/project.js +253 -0
- package/dist/examples/super/prompts.d.ts +3 -0
- package/dist/examples/super/prompts.js +25 -0
- package/dist/examples/taskpool/deploy/service-hosts/Dockerfile +4 -2
- package/dist/examples/taskpool/harness/controller.d.ts +35 -6
- package/dist/examples/taskpool/harness/controller.js +73 -7
- package/dist/examples/taskpool/project.js +18 -2
- package/dist/scripts/anyswarm-applications.d.ts +19 -0
- package/dist/scripts/anyswarm-applications.js +19 -0
- package/dist/scripts/anyswarm-arguments.d.ts +35 -5
- package/dist/scripts/anyswarm-arguments.js +87 -23
- package/dist/scripts/anyswarm-author.d.ts +5 -0
- package/dist/scripts/anyswarm-author.js +24 -0
- package/dist/scripts/anyswarm-eject.d.ts +11 -0
- package/dist/scripts/anyswarm-eject.js +164 -0
- package/dist/scripts/anyswarm-harnesses.d.ts +2 -0
- package/dist/scripts/anyswarm-harnesses.js +31 -0
- package/dist/scripts/anyswarm-installation.js +19 -13
- package/dist/scripts/anyswarm-launch.d.ts +37 -0
- package/dist/scripts/anyswarm-launch.js +86 -0
- package/dist/scripts/anyswarm-project.d.ts +12 -2
- package/dist/scripts/anyswarm-project.js +121 -20
- package/dist/scripts/anyswarm-source-runner.d.ts +1 -0
- package/dist/scripts/anyswarm-source-runner.js +232 -0
- package/dist/scripts/anyswarm-tui-config.d.ts +36 -0
- package/dist/scripts/anyswarm-tui-config.js +189 -0
- package/dist/scripts/anyswarm-tui-session.d.ts +66 -0
- package/dist/scripts/anyswarm-tui-session.js +304 -0
- package/dist/scripts/anyswarm-tui-terminal.d.ts +41 -0
- package/dist/scripts/anyswarm-tui-terminal.js +154 -0
- package/dist/scripts/anyswarm-tui.d.ts +2 -0
- package/dist/scripts/anyswarm-tui.js +1012 -0
- package/dist/scripts/anyswarm.js +47 -2
- package/dist/scripts/build-provenance.json +1 -0
- package/dist/scripts/build.js +7 -1
- package/dist/scripts/generate-architecture-api.js +2 -0
- package/dist/scripts/generate-architecture-prompts.js +24 -2
- package/dist/scripts/local-environment.d.ts +6 -1
- package/dist/scripts/local-environment.js +52 -16
- package/dist/scripts/local-setup-smoke.js +3 -2
- package/dist/scripts/local-setup-worker.d.ts +1 -1
- package/dist/scripts/local-setup-worker.js +15 -6
- package/dist/scripts/local-setup.d.ts +2 -2
- package/dist/scripts/local-setup.js +72 -12
- package/dist/scripts/opensandbox-agent-worker.js +2 -2
- package/dist/scripts/pack-release.d.ts +1 -0
- package/dist/scripts/pack-release.js +63 -0
- package/dist/scripts/pi-session-bridge.js +11 -3
- package/dist/scripts/run-harness.js +3 -1
- package/dist/scripts/setup-bootstrap.js +43 -6
- package/dist/src/core/allocation-ledger.js +4 -3
- package/dist/src/core/host-identity.d.ts +2 -0
- package/dist/src/core/host-identity.js +11 -0
- package/dist/src/core/index.d.ts +4 -2
- package/dist/src/core/index.js +12 -4
- package/dist/src/core/mcp-service.js +2 -2
- package/dist/src/core/runtime-management.d.ts +6 -0
- package/dist/src/core/runtime-management.js +24 -0
- package/dist/src/core/sandbox/executor-driver.d.ts +4 -2
- package/dist/src/core/sandbox/executor-driver.js +12 -14
- package/dist/src/core/sandbox/local-executor.d.ts +13 -1
- package/dist/src/core/sandbox/local-executor.js +69 -19
- package/dist/src/core/sandbox/local-worker.js +32 -21
- package/dist/src/core/sandbox/opensandbox-backend.d.ts +2 -0
- package/dist/src/core/sandbox/opensandbox-backend.js +36 -11
- package/dist/src/core/sandbox/sandbox-connection.d.ts +2 -0
- package/dist/src/core/sandbox/sandbox-connection.js +14 -6
- package/dist/src/core/service-live-state.d.ts +14 -0
- package/dist/src/core/service-live-state.js +161 -0
- package/dist/src/core/service-observation-types.d.ts +21 -1
- package/dist/src/core/service-process.d.ts +1 -1
- package/dist/src/core/service-process.js +45 -4
- package/dist/src/core/service-types.d.ts +5 -1
- package/dist/src/core/services.d.ts +1 -1
- package/dist/src/core/services.js +8 -3
- package/dist/src/core/types.d.ts +3 -0
- package/dist/src/core/worker-image.js +3 -3
- package/dist/src/harness/harness-session-types.d.ts +10 -0
- package/dist/src/harness/harness-session.js +42 -5
- package/dist/src/harness/project-harness.d.ts +13 -1
- package/dist/src/harness/project-harness.js +36 -10
- package/dist/src/harness/project-input.js +81 -15
- package/dist/src/harness/project-rpc-client.d.ts +1 -0
- package/dist/src/harness/project-rpc-client.js +5 -0
- package/dist/src/harness/project-rpc.d.ts +8 -2
- package/dist/src/harness/project-rpc.js +31 -7
- package/dist/src/harness/project-runtime.js +3 -1
- package/dist/src/harness/typescript-loader.d.ts +2 -0
- package/dist/src/harness/typescript-loader.js +41 -0
- package/dist/src/trace-viewer/public/app.js +6 -5
- package/dist/src/trace-viewer/reader.mjs +42 -2
- package/docs/INDEX.md +3 -1
- package/docs/architecture/00-overview.md +1 -1
- package/docs/architecture/01-agent-core.md +1 -1
- package/docs/architecture/06-observability-experiments.md +1 -1
- package/docs/architecture/07-run-configuration.md +18 -12
- package/docs/architecture/08-prompts.md +22 -0
- package/docs/architecture/10-service-api-reference.md +53 -18
- package/docs/architecture/11-storage-reference.md +18 -1
- package/docs/architecture/12-code-coverage.md +148 -84
- package/docs/architecture/15-core-runtime.md +8 -0
- package/docs/architecture/16-pi-opensandbox-backend.md +5 -5
- package/docs/architecture/17-harness-reporting.md +1 -1
- package/docs/architecture/19-substrate-harness.md +28 -1
- package/docs/architecture/20-lead-worker-harness.md +34 -1
- package/docs/architecture/22-website.md +5 -3
- package/docs/architecture/23-project-harness.md +124 -5
- package/docs/architecture/24-runtime-recovery.md +10 -3
- package/docs/architecture/25-foreground-tui.md +51 -0
- package/docs/architecture/26-code.md +39 -0
- package/docs/architecture/27-personal.md +43 -0
- package/docs/architecture/28-autoresearch.md +31 -0
- package/docs/architecture/INDEX.md +4 -0
- package/docs/architecture/prompts/08-lead-worker-harness.md +4 -2
- package/docs/architecture/prompts/08-subagents-harness.md +21 -0
- package/docs/architecture/prompts/21-personal.md +44 -0
- package/docs/architecture/prompts/22-autoresearch.md +63 -0
- package/docs/architecture/prompts/26-code.md +55 -0
- package/docs/architecture/prompts/source-manifest.json +152 -33
- package/docs/architecture/source-map.json +489 -21
- package/docs/designs/core-harness/06-project-harness.md +13 -2
- package/docs/designs/core-harness/README.md +5 -0
- package/docs/en/agents.md +1 -1
- package/docs/en/build-your-harness.md +3 -5
- package/docs/en/changelog.md +13 -4
- package/docs/en/first-harness.md +8 -11
- package/docs/en/harnesses/lead-worker.md +1 -1
- package/docs/en/harnesses/peermesh.md +2 -2
- package/docs/en/harnesses/subagents.md +2 -2
- package/docs/en/harnesses/substrate.md +4 -4
- package/docs/en/harnesses/taskpool.md +2 -2
- package/docs/en/latest.md +12 -123
- package/docs/en/overview.md +1 -1
- package/docs/en/quickstart.md +40 -16
- package/docs/en/reference/api.md +14 -4
- package/docs/en/reference/cli.md +58 -14
- package/docs/en/runtime.md +2 -2
- package/docs/en/services.md +4 -0
- package/docs/third-party-notices.md +1 -0
- package/docs/workflows/development.md +17 -12
- package/docs/workflows/release-and-docs-versioning.md +76 -0
- package/docs/zh/agents.md +1 -1
- package/docs/zh/build-your-harness.md +3 -5
- package/docs/zh/changelog.md +13 -4
- package/docs/zh/first-harness.md +8 -11
- package/docs/zh/harnesses/lead-worker.md +1 -1
- package/docs/zh/harnesses/peermesh.md +2 -2
- package/docs/zh/harnesses/subagents.md +2 -2
- package/docs/zh/harnesses/substrate.md +4 -4
- package/docs/zh/harnesses/taskpool.md +2 -2
- package/docs/zh/latest.md +13 -124
- package/docs/zh/overview.md +1 -1
- package/docs/zh/quickstart.md +40 -16
- package/docs/zh/reference/api.md +14 -4
- package/docs/zh/reference/cli.md +58 -14
- package/docs/zh/runtime.md +2 -2
- package/docs/zh/services.md +4 -0
- package/examples/README.md +24 -12
- package/examples/authoring/README.md +24 -0
- package/examples/authoring/business.ts +4 -0
- package/examples/authoring/config.ts +2 -0
- package/examples/authoring/eject.json +5 -0
- package/examples/authoring/harness.ts +21 -0
- package/examples/autoresearch/README.md +33 -0
- package/examples/autoresearch/REQUIREMENTS.zh.md +18 -0
- package/examples/autoresearch/config.ts +90 -0
- package/examples/autoresearch/demo.ts +55 -0
- package/examples/autoresearch/eject.json +15 -0
- package/examples/autoresearch/frontiercs2.ts +237 -0
- package/examples/autoresearch/prepare.ts +267 -0
- package/examples/autoresearch/process.ts +50 -0
- package/examples/autoresearch/project.ts +397 -0
- package/examples/autoresearch/research-service.ts +302 -0
- package/examples/code/README.md +25 -0
- package/examples/code/REQUIREMENTS.zh.md +11 -0
- package/examples/code/candidate.ts +89 -0
- package/examples/code/config.ts +19 -0
- package/examples/code/eject.json +5 -0
- package/examples/code/project.ts +581 -0
- package/examples/code/prompts.ts +16 -0
- package/examples/code/verification.ts +239 -0
- package/examples/code/worktree.ts +188 -0
- package/examples/harness-session/README.md +3 -6
- package/examples/lead-worker/harness/dynamic-scheduler.ts +6 -3
- package/examples/lead-worker/harness/index.ts +61 -6
- package/examples/lead-worker/project.ts +2 -2
- package/examples/lead-worker/services/board.ts +14 -1
- package/examples/messages/README.md +5 -5
- package/examples/messages/handoff.ts +6 -2
- package/examples/messages/project.ts +47 -2
- package/examples/notes/README.md +13 -18
- package/examples/notes/project.ts +57 -9
- package/examples/peermesh/harness/router.ts +2 -2
- package/examples/peermesh/harness/team.ts +11 -0
- package/examples/peermesh/project.ts +19 -4
- package/examples/services/agent-control/README.md +3 -3
- package/examples/services/mathlib-search/Dockerfile +4 -2
- package/examples/services/mathlib-search/README.md +1 -1
- package/examples/services/mcp-managed/README.md +2 -2
- package/examples/services/persistent-board/README.md +13 -3
- package/examples/services/persistent-board/business.ts +10 -0
- package/examples/services/persistent-board/index.ts +10 -0
- package/examples/services/persistent-board/live.ts +54 -0
- package/examples/subagents/README.md +5 -5
- package/examples/subagents/business.ts +3 -1
- package/examples/subagents/harness/controller.ts +161 -29
- package/examples/subagents/project.ts +47 -33
- package/examples/subagents/prompts/prompts.ts +4 -0
- package/examples/subagents/services/collaboration-service.ts +14 -2
- package/examples/substrate/README.md +2 -2
- package/examples/substrate/harness/index.ts +67 -11
- package/examples/substrate/project.ts +5 -1
- package/examples/super/README.md +40 -0
- package/examples/super/actions.ts +196 -0
- package/examples/super/artifact.ts +56 -0
- package/examples/super/config.ts +95 -0
- package/examples/super/coordinator.ts +158 -0
- package/examples/super/daemon.ts +84 -0
- package/examples/super/decisions.ts +46 -0
- package/examples/super/eject.json +5 -0
- package/examples/super/execution.ts +131 -0
- package/examples/super/github.ts +242 -0
- package/examples/super/inbox.ts +306 -0
- package/examples/super/location.ts +54 -0
- package/examples/super/outbox.ts +97 -0
- package/examples/super/project.ts +227 -0
- package/examples/super/prompts.ts +27 -0
- package/examples/taskpool/deploy/service-hosts/Dockerfile +4 -2
- package/examples/taskpool/harness/controller.ts +59 -7
- package/examples/taskpool/project.ts +18 -2
- package/npm-shrinkwrap.development.json +2199 -0
- package/npm-shrinkwrap.json +86 -1920
- package/package.development.json +92 -0
- package/package.json +13 -7
- package/scripts/anyswarm-applications.ts +39 -0
- package/scripts/anyswarm-arguments.ts +94 -28
- package/scripts/anyswarm-author.ts +25 -0
- package/scripts/anyswarm-eject.ts +145 -0
- package/scripts/anyswarm-harnesses.ts +27 -0
- package/scripts/anyswarm-installation.ts +19 -13
- package/scripts/anyswarm-launch.ts +108 -0
- package/scripts/anyswarm-project.ts +114 -21
- package/scripts/anyswarm-source-runner.ts +203 -0
- package/scripts/anyswarm-tui-config.ts +174 -0
- package/scripts/anyswarm-tui-session.ts +295 -0
- package/scripts/anyswarm-tui-terminal.ts +147 -0
- package/scripts/anyswarm-tui.ts +912 -0
- package/scripts/anyswarm.ts +43 -2
- package/scripts/build.ts +8 -1
- package/scripts/generate-architecture-api.ts +2 -0
- package/scripts/generate-architecture-prompts.ts +35 -2
- package/scripts/local-environment.ts +55 -17
- package/scripts/local-setup-smoke.ts +3 -2
- package/scripts/local-setup-worker.ts +17 -7
- package/scripts/local-setup.ts +66 -13
- package/scripts/opensandbox-agent-worker.ts +2 -2
- package/scripts/pack-release.ts +67 -0
- package/scripts/pi-session-bridge.ts +10 -2
- package/scripts/run-harness.ts +3 -1
- package/scripts/setup-bootstrap.ts +41 -6
- package/src/core/allocation-ledger.ts +5 -3
- package/src/core/host-identity.ts +11 -0
- package/src/core/index.ts +12 -6
- package/src/core/mcp-service.ts +2 -2
- package/src/core/runtime-management.ts +22 -0
- package/src/core/sandbox/executor-driver.ts +15 -18
- package/src/core/sandbox/local-executor.ts +65 -19
- package/src/core/sandbox/local-worker.ts +29 -21
- package/src/core/sandbox/opensandbox-backend.ts +36 -11
- package/src/core/sandbox/sandbox-connection.ts +14 -5
- package/src/core/service-live-state.ts +156 -0
- package/src/core/service-observation-types.ts +23 -1
- package/src/core/service-process.ts +36 -3
- package/src/core/service-types.ts +3 -1
- package/src/core/services.ts +8 -3
- package/src/core/types.ts +3 -0
- package/src/core/worker-image.ts +3 -3
- package/src/harness/harness-session-types.ts +13 -1
- package/src/harness/harness-session.ts +40 -7
- package/src/harness/project-harness.ts +36 -10
- package/src/harness/project-input.ts +73 -15
- package/src/harness/project-rpc-client.ts +5 -0
- package/src/harness/project-rpc.ts +29 -8
- package/src/harness/project-runtime.ts +2 -1
- package/src/harness/typescript-loader.ts +37 -0
- package/src/trace-viewer/public/app.js +6 -5
- package/src/trace-viewer/reader.mjs +42 -2
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { appendFileSync } from 'node:fs';
|
|
3
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { inProcessService, type JsonValue, type Runtime, type ServiceInterface } from '@anyswarm/core';
|
|
6
|
+
|
|
7
|
+
export const candidatePath = '.anyswarm-candidate.patch';
|
|
8
|
+
const maxPatch = 1024 * 1024 - 1536;
|
|
9
|
+
const patchChunk = 24 * 1024;
|
|
10
|
+
const maxNotes = 512;
|
|
11
|
+
|
|
12
|
+
export interface Evaluation {
|
|
13
|
+
valid: boolean;
|
|
14
|
+
score: number | null;
|
|
15
|
+
feedback: JsonValue;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Evaluator {
|
|
19
|
+
start?(directory: string, signal: AbortSignal): Promise<void>;
|
|
20
|
+
close?(): Promise<'complete' | 'pending'>;
|
|
21
|
+
evidence?(): JsonValue;
|
|
22
|
+
train(candidate: Buffer, signal: AbortSignal, admitted: (reference: string | number) => void): Promise<Evaluation>;
|
|
23
|
+
final(candidate: Buffer, signal: AbortSignal): Promise<JsonValue>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface Candidate {
|
|
27
|
+
id: string;
|
|
28
|
+
agentId: string;
|
|
29
|
+
sha256: string;
|
|
30
|
+
bytes: number;
|
|
31
|
+
summary: string;
|
|
32
|
+
basedOn: string | null;
|
|
33
|
+
path: string;
|
|
34
|
+
state: 'queued' | 'evaluating' | 'scored' | 'invalid' | 'unknown' | 'cancelled';
|
|
35
|
+
officialJob?: string | number;
|
|
36
|
+
evaluation?: Evaluation;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ResearchEvent {
|
|
40
|
+
seq: number;
|
|
41
|
+
at: string;
|
|
42
|
+
type: 'research';
|
|
43
|
+
operation: string;
|
|
44
|
+
agentId?: string;
|
|
45
|
+
to?: string;
|
|
46
|
+
detail?: string;
|
|
47
|
+
candidate?: string;
|
|
48
|
+
basedOn?: string;
|
|
49
|
+
sha256?: string;
|
|
50
|
+
basedOnSha256?: string;
|
|
51
|
+
score?: number | null;
|
|
52
|
+
officialJob?: string | number;
|
|
53
|
+
label?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const text = (maxLength: number) => ({ type: 'string', minLength: 1, maxLength });
|
|
57
|
+
const object = (properties: Record<string, unknown>, required: string[]) =>
|
|
58
|
+
({ type: 'object', properties, required, additionalProperties: false });
|
|
59
|
+
const identifier = text(200);
|
|
60
|
+
const summary = text(400);
|
|
61
|
+
const candidateId = { type: 'string', pattern: '^candidate-[0-9]+$' };
|
|
62
|
+
|
|
63
|
+
const researchInterface: ServiceInterface = {
|
|
64
|
+
description: 'One team’s shared research directions, peer messages, candidate patches and trusted train results.',
|
|
65
|
+
operations: {
|
|
66
|
+
board: { description: 'See the team’s current directions, messages, candidates and train scores.', sideEffect: 'read' },
|
|
67
|
+
claim: { description: 'Announce the research direction you are taking.', inputSchema: object({ direction: summary }, ['direction']), idempotent: true },
|
|
68
|
+
publish: { description: 'Share a finding or a peer candidate review.', inputSchema: object({
|
|
69
|
+
kind: { enum: ['finding', 'review'] }, summary, candidate: candidateId,
|
|
70
|
+
}, ['kind', 'summary']), idempotent: true },
|
|
71
|
+
message: { description: 'Send a short research message to another peer.', inputSchema: object({ peer: identifier, summary }, ['peer', 'summary']), idempotent: true },
|
|
72
|
+
submit: { description: `Queue the exact patch saved at ${candidatePath} for trusted evaluation. Optionally reference a peer candidate you built on.`,
|
|
73
|
+
inputSchema: object({ summary, basedOn: candidateId }, ['summary']), idempotent: true },
|
|
74
|
+
read_patch: { description: 'Read a peer candidate patch segment by byte offset. Decode base64 and use nextOffset until it is null.', sideEffect: 'read',
|
|
75
|
+
inputSchema: object({ candidate: candidateId, offset: { type: 'integer', minimum: 0 } }, ['candidate', 'offset']) },
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
export const researchOperations = Object.keys(researchInterface.operations);
|
|
79
|
+
|
|
80
|
+
function validScore(result: Evaluation) {
|
|
81
|
+
return result.valid && typeof result.score === 'number' && Number.isFinite(result.score);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createResearch(options: {
|
|
85
|
+
outputDir: string;
|
|
86
|
+
direction: 'max' | 'min';
|
|
87
|
+
maxEvaluations: number;
|
|
88
|
+
targetScore?: number;
|
|
89
|
+
deadlineAt: number;
|
|
90
|
+
evaluationTimeoutMs: number;
|
|
91
|
+
signal: AbortSignal;
|
|
92
|
+
trainSignal?: AbortSignal;
|
|
93
|
+
evaluator: Evaluator;
|
|
94
|
+
traceEvent: (event: ResearchEvent) => void;
|
|
95
|
+
statusChanged: () => void;
|
|
96
|
+
stopWork?: (reason: string) => void;
|
|
97
|
+
readPatch: (agentId: string, requestId: string) => Promise<Buffer>;
|
|
98
|
+
}) {
|
|
99
|
+
const events: ResearchEvent[] = [];
|
|
100
|
+
const peers = new Map<string, string>();
|
|
101
|
+
const candidates = new Map<string, Candidate>();
|
|
102
|
+
const known = new Map<string, Candidate>();
|
|
103
|
+
const patches = new Map<string, Buffer>();
|
|
104
|
+
const reads = new Map<string, Map<string, number>>();
|
|
105
|
+
const claims = new Map<string, string>();
|
|
106
|
+
let best: Candidate | undefined;
|
|
107
|
+
let reserved = 0;
|
|
108
|
+
let closed = false;
|
|
109
|
+
let stopReason: string | undefined;
|
|
110
|
+
let queue = Promise.resolve();
|
|
111
|
+
const project = (record: unknown) => record as JsonValue;
|
|
112
|
+
const reached = (score: number, target: number) => options.direction === 'max' ? score >= target : score <= target;
|
|
113
|
+
function event(operation: string, data: Omit<ResearchEvent, 'operation' | 'seq' | 'at' | 'type'> = {}) {
|
|
114
|
+
const entry = { ...data, seq: events.length + 1, at: new Date().toISOString(), type: 'research' as const, operation };
|
|
115
|
+
events.push(entry);
|
|
116
|
+
options.traceEvent(entry);
|
|
117
|
+
options.statusChanged();
|
|
118
|
+
}
|
|
119
|
+
function admit() {
|
|
120
|
+
if (closed || stopReason || options.signal.aborted || events.length >= maxNotes) throw new Error('Research admissions are closed');
|
|
121
|
+
}
|
|
122
|
+
function state() {
|
|
123
|
+
return {
|
|
124
|
+
peers: [...peers].map(([agentId, label]) => ({ agentId, label })),
|
|
125
|
+
best: best ? { id: best.id, score: best.evaluation?.score, author: peers.get(best.agentId) } : null,
|
|
126
|
+
budget: { evaluations: reserved, maxEvaluations: options.maxEvaluations, deadlineAt: options.deadlineAt },
|
|
127
|
+
recent: events.slice(-12).map(entry => ({ ...entry, peer: entry.agentId ? peers.get(entry.agentId) : 'host' })),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function readEntirePatch(agentId: string, id: string) {
|
|
131
|
+
const bytes = patches.get(id);
|
|
132
|
+
return bytes !== undefined && reads.get(agentId)?.get(id) === bytes.length;
|
|
133
|
+
}
|
|
134
|
+
async function evaluate(candidate: Candidate, bytes: Buffer) {
|
|
135
|
+
if (closed || stopReason || options.signal.aborted || Date.now() >= options.deadlineAt) {
|
|
136
|
+
candidate.state = 'cancelled';
|
|
137
|
+
event('candidate.cancelled', { agentId: candidate.agentId, candidate: candidate.id });
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
candidate.state = 'evaluating';
|
|
141
|
+
event('train.started', { agentId: candidate.agentId, candidate: candidate.id });
|
|
142
|
+
try {
|
|
143
|
+
const signal = AbortSignal.any([options.trainSignal ?? options.signal, AbortSignal.timeout(options.evaluationTimeoutMs)]);
|
|
144
|
+
const evaluation = await withDeadline(signal, () => options.evaluator.train(Buffer.from(bytes), signal, reference => {
|
|
145
|
+
if (signal.aborted || candidate.state !== 'evaluating') return;
|
|
146
|
+
candidate.officialJob = reference;
|
|
147
|
+
event('train.admitted', { agentId: candidate.agentId, candidate: candidate.id, officialJob: reference });
|
|
148
|
+
}));
|
|
149
|
+
candidate.evaluation = evaluation;
|
|
150
|
+
candidate.state = validScore(evaluation) ? 'scored' : 'invalid';
|
|
151
|
+
event('train.returned', { agentId: candidate.agentId, candidate: candidate.id, score: evaluation.score });
|
|
152
|
+
if (validScore(evaluation) && (!best || (evaluation.score !== best.evaluation!.score && reached(evaluation.score!, best.evaluation!.score!)))) {
|
|
153
|
+
best = candidate;
|
|
154
|
+
event('candidate.best', { agentId: candidate.agentId, candidate: candidate.id, score: evaluation.score });
|
|
155
|
+
}
|
|
156
|
+
if (best && options.targetScore !== undefined && reached(best.evaluation!.score!, options.targetScore)) {
|
|
157
|
+
stopReason = 'target-reached';
|
|
158
|
+
options.stopWork?.(stopReason);
|
|
159
|
+
}
|
|
160
|
+
} catch (error) {
|
|
161
|
+
candidate.state = 'unknown';
|
|
162
|
+
stopReason = 'train-outcome-unknown';
|
|
163
|
+
event('train.unknown', { agentId: candidate.agentId, candidate: candidate.id, detail: error instanceof Error ? error.message.slice(0, 320) : 'Evaluation failed' });
|
|
164
|
+
options.stopWork?.(stopReason);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const service = inProcessService(researchInterface, {
|
|
168
|
+
board: (_, { caller, assertAuthorized }) => {
|
|
169
|
+
assertAuthorized();
|
|
170
|
+
return project({
|
|
171
|
+
...state(), recent: events.filter(entry => !entry.to || entry.to === caller.agentId || entry.agentId === caller.agentId).slice(-12),
|
|
172
|
+
claims: [...claims].slice(-32).map(([direction, agentId]) => ({ direction, agentId })),
|
|
173
|
+
candidates: [...candidates.values()].slice(-8).map(({ path: _, ...candidate }) => candidate),
|
|
174
|
+
});
|
|
175
|
+
},
|
|
176
|
+
claim: (input, { caller, assertAuthorized }) => {
|
|
177
|
+
assertAuthorized();
|
|
178
|
+
admit();
|
|
179
|
+
const direction = (input as { direction: string }).direction.trim();
|
|
180
|
+
if (!direction) throw new Error('Provide a research direction');
|
|
181
|
+
const key = direction.toLowerCase();
|
|
182
|
+
const current = claims.get(key);
|
|
183
|
+
if (current) return { claimed: current === caller.agentId, agentId: current };
|
|
184
|
+
claims.set(key, caller.agentId);
|
|
185
|
+
event('peer.claim', { agentId: caller.agentId, detail: direction });
|
|
186
|
+
return { claimed: true, agentId: caller.agentId };
|
|
187
|
+
},
|
|
188
|
+
publish: (input, { caller, assertAuthorized }) => {
|
|
189
|
+
assertAuthorized();
|
|
190
|
+
admit();
|
|
191
|
+
const { kind, summary, candidate } = input as { kind: 'finding' | 'review'; summary: string; candidate?: string };
|
|
192
|
+
const reviewed = candidate === undefined ? undefined : candidates.get(candidate);
|
|
193
|
+
if (candidate !== undefined && !reviewed) throw new Error('Candidate does not exist');
|
|
194
|
+
if (kind === 'review' && (!reviewed || reviewed.agentId === caller.agentId || !readEntirePatch(caller.agentId, reviewed.id))) {
|
|
195
|
+
throw new Error('Read the other peer’s candidate before reviewing it');
|
|
196
|
+
}
|
|
197
|
+
event(`peer.${kind}`, { agentId: caller.agentId, detail: summary,
|
|
198
|
+
...(reviewed ? { candidate: reviewed.id, sha256: reviewed.sha256 } : {}) });
|
|
199
|
+
return { published: true };
|
|
200
|
+
},
|
|
201
|
+
message: (input, { caller, assertAuthorized }) => {
|
|
202
|
+
assertAuthorized();
|
|
203
|
+
admit();
|
|
204
|
+
const { peer, summary } = input as { peer: string; summary: string };
|
|
205
|
+
if (peer === caller.agentId || !peers.has(peer)) throw new Error('Peer does not belong to this Run');
|
|
206
|
+
event('peer.message', { agentId: caller.agentId, to: peer, detail: summary });
|
|
207
|
+
return { sent: true };
|
|
208
|
+
},
|
|
209
|
+
read_patch: (input, { caller, assertAuthorized }) => {
|
|
210
|
+
assertAuthorized();
|
|
211
|
+
admit();
|
|
212
|
+
const { candidate, offset } = input as { candidate: string; offset: number };
|
|
213
|
+
const bytes = patches.get(candidate);
|
|
214
|
+
if (!bytes || offset >= bytes.length) throw new Error('Candidate or patch offset does not exist');
|
|
215
|
+
let progress = reads.get(caller.agentId);
|
|
216
|
+
if (!progress) reads.set(caller.agentId, progress = new Map());
|
|
217
|
+
const received = progress.get(candidate) ?? 0;
|
|
218
|
+
if (offset > received) throw new Error('Read the patch in order, starting at offset 0');
|
|
219
|
+
const start = offset;
|
|
220
|
+
const end = Math.min(bytes.length, start + patchChunk);
|
|
221
|
+
const sha256 = candidates.get(candidate)!.sha256;
|
|
222
|
+
if (end > received) {
|
|
223
|
+
progress.set(candidate, end);
|
|
224
|
+
if (end === bytes.length) event('peer.read-patch', { agentId: caller.agentId, candidate, sha256, to: caller.agentId });
|
|
225
|
+
}
|
|
226
|
+
return { candidate, sha256, data: bytes.subarray(start, end).toString('base64'), nextOffset: end < bytes.length ? end : null };
|
|
227
|
+
},
|
|
228
|
+
submit: async (input, { caller, envelope, assertAuthorized }) => {
|
|
229
|
+
assertAuthorized();
|
|
230
|
+
admit();
|
|
231
|
+
const { summary, basedOn } = input as { summary: string; basedOn?: string };
|
|
232
|
+
if (basedOn && !readEntirePatch(caller.agentId, basedOn)) throw new Error('Read the referenced candidate before building on it');
|
|
233
|
+
const bytes = await options.readPatch(caller.agentId, envelope.operation_id);
|
|
234
|
+
assertAuthorized();
|
|
235
|
+
admit();
|
|
236
|
+
if (!bytes.length || bytes.length > maxPatch) throw new Error('Candidate patch is empty or exceeds the size limit');
|
|
237
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
238
|
+
const previous = known.get(sha256);
|
|
239
|
+
if (previous) return { candidate: previous.id, status: previous.state, duplicate: true, sha256 };
|
|
240
|
+
if (reserved >= options.maxEvaluations) throw new Error('Shared evaluation budget is exhausted');
|
|
241
|
+
reserved++;
|
|
242
|
+
const id = `candidate-${reserved}`;
|
|
243
|
+
const path = join(options.outputDir, 'candidates', `${id}.patch`);
|
|
244
|
+
const candidate: Candidate = { id, agentId: caller.agentId, summary, basedOn: basedOn ?? null, path,
|
|
245
|
+
sha256, bytes: bytes.length, state: 'queued' };
|
|
246
|
+
known.set(sha256, candidate);
|
|
247
|
+
try {
|
|
248
|
+
await mkdir(join(options.outputDir, 'candidates'), { recursive: true, mode: 0o700 });
|
|
249
|
+
await writeFile(path, bytes, { mode: 0o600, flag: 'wx' });
|
|
250
|
+
assertAuthorized();
|
|
251
|
+
admit();
|
|
252
|
+
} catch (error) {
|
|
253
|
+
candidate.state = 'cancelled';
|
|
254
|
+
candidates.set(id, candidate);
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
patches.set(id, bytes);
|
|
258
|
+
candidates.set(id, candidate);
|
|
259
|
+
event(basedOn ? 'candidate.built-on' : 'candidate.queued', { agentId: caller.agentId, candidate: id, sha256, detail: summary,
|
|
260
|
+
...(basedOn ? { basedOn, basedOnSha256: candidates.get(basedOn)!.sha256 } : {}) });
|
|
261
|
+
queue = queue.then(() => evaluate(candidate, bytes));
|
|
262
|
+
return { candidate: id, status: candidate.state, duplicate: false, sha256 };
|
|
263
|
+
},
|
|
264
|
+
}, {
|
|
265
|
+
readState: () => ({
|
|
266
|
+
state: project(state()), capturedAt: new Date().toISOString(), revision: String(events.length),
|
|
267
|
+
presentation: {
|
|
268
|
+
summary: [{ label: 'Best', path: '/best/id' }, { label: 'Score', path: '/best/score' },
|
|
269
|
+
{ label: 'Evaluations queued', path: '/budget/evaluations' }, { label: 'Evaluation limit', path: '/budget/maxEvaluations' }],
|
|
270
|
+
tables: [{ title: 'Research activity', path: '/recent', columns: [
|
|
271
|
+
{ label: 'Peer', path: '/peer' }, { label: 'Activity', path: '/operation' }, { label: 'Candidate', path: '/candidate' }, { label: 'Detail', path: '/detail' },
|
|
272
|
+
] }],
|
|
273
|
+
},
|
|
274
|
+
}),
|
|
275
|
+
});
|
|
276
|
+
return { service, state, event,
|
|
277
|
+
addPeer: (id: string, label: string) => { peers.set(id, label); event('peer.ready', { agentId: id, label }); },
|
|
278
|
+
close: () => { closed = true; },
|
|
279
|
+
stopReason: () => stopReason,
|
|
280
|
+
drained: () => queue,
|
|
281
|
+
best: () => best,
|
|
282
|
+
peerLabel: (agentId: string) => peers.get(agentId),
|
|
283
|
+
selection: () => best ?? [...candidates.values()].find(candidate => candidate.state === 'invalid'),
|
|
284
|
+
patch: (candidate: Candidate) => Buffer.from(patches.get(candidate.id)!),
|
|
285
|
+
candidates: () => [...candidates.values()],
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export async function withDeadline<T>(signal: AbortSignal, work: () => Promise<T>): Promise<T> {
|
|
290
|
+
signal.throwIfAborted();
|
|
291
|
+
let rejectAborted: () => void;
|
|
292
|
+
const aborted = new Promise<never>((_, reject) => {
|
|
293
|
+
rejectAborted = () => reject(signal.reason instanceof Error ? signal.reason : new Error(String(signal.reason)));
|
|
294
|
+
signal.addEventListener('abort', rejectAborted, { once: true });
|
|
295
|
+
});
|
|
296
|
+
try { return await Promise.race([aborted, work()]); }
|
|
297
|
+
finally { signal.removeEventListener('abort', rejectAborted!); }
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function appendResearchEvent(runtime: Runtime, output: string, entry: ResearchEvent) {
|
|
301
|
+
appendFileSync(join(output, runtime.id, 'research-events.jsonl'), JSON.stringify({ ...entry, runtimeId: runtime.id }) + '\n', { mode: 0o600 });
|
|
302
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Code quickstart
|
|
2
|
+
|
|
3
|
+
Prepare an OpenSandbox connection, a Pi or Codex Worker, and a model route using AnySwarm's CLI quickstart. On the Linux host that holds your project, open its directory and run:
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
anys -u code
|
|
7
|
+
anys -u code "Add a test for the empty-input case"
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
You can also start `anys` and enter `/code`. Enter sends, Alt+Enter adds a line, Ctrl+C interrupts the current task, and `/quit` closes the team. `/diff` prints the latest completed turn's paths, patch preview and full patch location into the conversation. Use `/agents` for the member roster, `@<editor> <message>` to steer an active editor, and `/stop <editor>` to stop that editor. Answer inline verification confirmations with `/y` or `/n`, and editor questions with `/reply <id> <answer>`. Use `/config` and `/doctor` for setup information and `/trace` for Trace Viewer.
|
|
11
|
+
|
|
12
|
+
The main Agent edits and tests a copy of your current project in one writable Sandbox. Follow-up requests reuse its conversation. AnySwarm reconciles your files before each turn, including edits you made between turns. To delegate, write a request such as “Ask a subagent to write the regression test while you fix the implementation.” The main Agent can continue while the child works in a separate Sandbox, then collect and review the result. The host checks the integrated changes before writeback. If an edit conflicts with yours, resolve the reported path before retrying.
|
|
13
|
+
|
|
14
|
+
The default configuration reads `ANYSWARM_MODEL_URL`, optional `ANYSWARM_MODEL_TOKEN`, `ANYSWARM_MODEL_ID` and `ANYSWARM_MODEL_PROVIDER`. Code-specific `ANYSWARM_CODE_*` equivalents take precedence. Use `anys -u code --config ./trusted.ts` for an explicit model/Sandbox configuration. The application can ask before running a detected `npm test` or `make test`. Set `check` to an argv array in a trusted config to authorize it for each candidate, or set `check: false` to skip verification. Checks run locally with your permissions in a temporary copy. Code copies a supported existing Node dependency installation into that copy. Install the project's dependencies first if the checker reports an unavailable environment. A failed verification leaves the original project files in place. After a successful task, `/quit` returns 0 when cleanup completes and keeps the report location visible.
|
|
15
|
+
|
|
16
|
+
To edit the application itself:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
anys eject code --dir ./my-code
|
|
20
|
+
cd ./my-code
|
|
21
|
+
npm install
|
|
22
|
+
anys run ./project.ts --mode tui --lifetime interactive --cwd /path/to/project
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The exported package uses the SDK bytes from the installed AnySwarm package. Edit `prompts.ts` or `project.ts` and run the same command again to use your version. Keep the exported source outside the project directory if you do not want its files in the project scope.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Code 用户需求
|
|
2
|
+
|
|
3
|
+
适用范围见 [#336](https://github.com/AnySwarm/anyswarm/issues/336)。用户在自己的项目目录运行 `anys -u code [任务]`,也可以打开 `anys` 后输入 `/code`。共享界面显示项目、执行器和模型。用户可以回答内联问题、用 `/diff` 查看最近一轮的改动,或继续输入。退出时提供清理结果。
|
|
4
|
+
|
|
5
|
+
- 一个会话由一个 Runtime 管理。主 Agent 默认在一个可写的隔离工作区读项目、改代码并运行检查。用户可以在聊天中要求委派,主 Agent 可以和独立子 Agent 同时工作,收取结果并审查。用户可针对任意活跃成员发消息、中断或停止。
|
|
6
|
+
- 每轮以用户当前目录的内容为起点,包含已跟踪文件的未提交修改和未忽略的新文件。宿主比较主 Agent、已收取的子 Agent 结果和实时工作树,合并后按任务授权写回。用户改动冲突时显示路径并保留原文件。
|
|
7
|
+
- 用户可见改动文件和实际 diff。宿主监督配置或用户确认的检查,并展示命令、退出码和状态。Node 检查可复用受支持的现有依赖安装,缺失时显示校验环境问题。无命令时显示未验证。校验失败时保留原项目文件。
|
|
8
|
+
- `Ctrl+C` 请求中断当前轮,之后可以继续输入。`/quit` 等待资源清理。空闲时在成功任务后退出,清理确认后返回 0。会话收尾保留报告、检查结果、已写入的路径和 Trace。
|
|
9
|
+
- `anys eject code` 导出完整 TS 应用。作者能在仓库外直接运行、修改并重新执行其副本。
|
|
10
|
+
|
|
11
|
+
验收使用同一个仓库外安装的候选包。用真实项目覆盖默认单 Agent 两轮编辑、用户明确要求并行委派、中断后继续、验证失败、并行成员控制、退出清理和导出源码。记录每轮文件上传下载字节数、实际容器数、峰值内存和最终检查结果。ProgramBench 使用同一 Code 应用,运行状态和官方评分单独记录。
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { completeFileTreeDigest, decodeFileTree, isGitTreePath, snapshotFileTree,
|
|
3
|
+
type CompleteFileDigest, type FileBudget, type FileEntry, type FileTree } from '@anyswarm/core';
|
|
4
|
+
|
|
5
|
+
export interface CodeChange {
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly before?: FileEntry;
|
|
8
|
+
readonly after?: FileEntry;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type Candidate = { status: 'conflict'; paths: readonly string[] } | {
|
|
12
|
+
status: 'ready'; files: FileTree; digest: CompleteFileDigest; changes: readonly CodeChange[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function canonical(tree: FileTree, budget?: FileBudget) {
|
|
16
|
+
return new Map(decodeFileTree(tree, budget).entries.map(({ path, bytes, mode }) => [path, Object.freeze({
|
|
17
|
+
encoding: 'base64' as const, data: Buffer.from(bytes).toString('base64'), mode: mode as 0o644 | 0o755,
|
|
18
|
+
})]));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function same(left: FileEntry | undefined, right: FileEntry | undefined) {
|
|
22
|
+
return left?.mode === right?.mode && left?.data === right?.data;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function selectEditorFiles(cwd: string, baseline: FileTree, files: FileTree, git: boolean, signal: AbortSignal) {
|
|
26
|
+
const added = Object.keys(files).filter(path => !Object.hasOwn(baseline, path));
|
|
27
|
+
const excluded = new Set(added.filter(path => /^(?:node_modules|\.anyswarm-sdk|\.anyswarm-code-recovery-[A-Za-z0-9]{6})(?:\/|$)/.test(path)));
|
|
28
|
+
const remaining = added.filter(path => !excluded.has(path));
|
|
29
|
+
if (git && remaining.length) {
|
|
30
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')));
|
|
31
|
+
const ignored = await new Promise<string>((resolve, reject) => {
|
|
32
|
+
const process = execFile('git', ['check-ignore', '-z', '--stdin'], { cwd, env,
|
|
33
|
+
signal, timeout: 10_000, maxBuffer: 64 * 1024 ** 2 }, (error, stdout) => {
|
|
34
|
+
if (!error || error.code === 1) resolve(stdout);
|
|
35
|
+
else reject(error);
|
|
36
|
+
});
|
|
37
|
+
process.stdin?.on('error', reject);
|
|
38
|
+
process.stdin?.end(remaining.join('\0') + '\0');
|
|
39
|
+
});
|
|
40
|
+
for (const path of ignored.split('\0')) if (path) excluded.add(path);
|
|
41
|
+
}
|
|
42
|
+
return excluded.size ? Object.fromEntries(Object.entries(files).filter(([path]) => !excluded.has(path))) : files;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Integrate independent Worker proposals against the live host tree before verification and apply. */
|
|
46
|
+
export function prepareCodeCandidate(baseline: FileTree, current: FileTree, proposals: readonly FileTree[], budget?: FileBudget, candidateBudget = budget): Candidate {
|
|
47
|
+
const base = canonical(baseline, budget);
|
|
48
|
+
const host = canonical(current, budget);
|
|
49
|
+
const edits = new Map<string, FileEntry | undefined>();
|
|
50
|
+
const conflicts = new Set<string>();
|
|
51
|
+
|
|
52
|
+
for (const proposal of proposals) {
|
|
53
|
+
const worker = canonical(proposal, budget);
|
|
54
|
+
for (const path of new Set([...base.keys(), ...worker.keys()])) {
|
|
55
|
+
const previous = base.get(path);
|
|
56
|
+
const after = worker.get(path);
|
|
57
|
+
if (same(previous, after)) continue;
|
|
58
|
+
const local = host.get(path);
|
|
59
|
+
if (!isGitTreePath(path) || (!same(local, previous) && !same(local, after)) ||
|
|
60
|
+
(edits.has(path) && !same(edits.get(path), after))) {
|
|
61
|
+
conflicts.add(path);
|
|
62
|
+
}
|
|
63
|
+
edits.set(path, after);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const files = new Map<string, FileEntry>(host);
|
|
68
|
+
const changes: CodeChange[] = [];
|
|
69
|
+
for (const path of [...edits.keys()].sort()) {
|
|
70
|
+
const before = host.get(path);
|
|
71
|
+
const after = edits.get(path);
|
|
72
|
+
if (same(before, after)) continue;
|
|
73
|
+
if (after) files.set(path, after);
|
|
74
|
+
else files.delete(path);
|
|
75
|
+
changes.push(Object.freeze({ path, before, after }));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (const path of files.keys()) {
|
|
79
|
+
for (let slash = path.indexOf('/'); slash !== -1; slash = path.indexOf('/', slash + 1)) {
|
|
80
|
+
const parent = path.slice(0, slash);
|
|
81
|
+
if (files.has(parent)) conflicts.add(parent);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (conflicts.size) return { status: 'conflict', paths: [...conflicts].sort() };
|
|
85
|
+
|
|
86
|
+
const candidate = Object.freeze(Object.fromEntries(files));
|
|
87
|
+
const { digest } = snapshotFileTree(candidate, candidateBudget);
|
|
88
|
+
return { status: 'ready', digest: completeFileTreeDigest(digest), files: candidate, changes: Object.freeze(changes) };
|
|
89
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { resolveSandboxConnection, workerImageFor } from '@anyswarm/core';
|
|
2
|
+
|
|
3
|
+
const { image, advertiseHost, publicUrl, endpointSource: _source, executor: installedExecutor, ...sandbox } = resolveSandboxConnection({}, 'CODE');
|
|
4
|
+
const upstream = process.env.ANYSWARM_CODE_MODEL_URL ?? process.env.ANYSWARM_MODEL_URL;
|
|
5
|
+
if (!upstream) throw new Error('Set ANYSWARM_MODEL_URL or ANYSWARM_CODE_MODEL_URL before running Code');
|
|
6
|
+
const token = process.env.ANYSWARM_CODE_MODEL_TOKEN ?? process.env.ANYSWARM_MODEL_TOKEN;
|
|
7
|
+
const executor = process.env.ANYSWARM_CODE_EXECUTOR ?? process.env.ANYSWARM_EXECUTOR ?? installedExecutor ?? 'pi';
|
|
8
|
+
const model = process.env.ANYSWARM_CODE_MODEL_ID ?? process.env.ANYSWARM_MODEL_ID ?? 'gpt-5.4';
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
executor,
|
|
12
|
+
image: image ?? workerImageFor(executor),
|
|
13
|
+
sandbox: { ...sandbox, resourceLimits: { cpu: '1', memory: '1Gi' } },
|
|
14
|
+
connection: { publicUrl, advertiseHost },
|
|
15
|
+
models: { default: {
|
|
16
|
+
provider: process.env.ANYSWARM_CODE_MODEL_PROVIDER ?? process.env.ANYSWARM_MODEL_PROVIDER ?? 'openai', model,
|
|
17
|
+
upstream: { url: upstream, headers: token ? { authorization: `Bearer ${token}` } : {} },
|
|
18
|
+
} },
|
|
19
|
+
};
|