Compare commits

...
Author SHA1 Message Date
Ryder Sun 1ddf3fbdd7 release: prepare 1.0.9 preview and clarify agent readiness
Bundle knowledge-base logos and report unverified or failed native authentication accurately. Update release metadata and document the six-agent discussion validation and known configuration limits.

Checks: frontend tests (379), desktop tests (1700), landing tests (13), frontend builds, desktop pack and dist, DMG and ZIP integrity.
2026-11-02 15:03:02 +08:00
Ryder Sun ef405bb721 fix: finish native collaboration turns on explicit completion
Use an invocation-bound assistant signal to return completed contributions to the scheduler without waiting for repeated native closing replies. Ignore late output, preserve settled native sessions, and retain independent version-bound group approvals.

Verified frontend 376 tests, desktop 1698 tests, both frontend builds and desktop pack. Real Claude, Hermes, Pi, OpenCode, Kimi and MiMo discussion completed after 9 rounds with all six approving candidate b91dddfe and matching immutable file hashes.
2026-10-30 15:59:51 +08:00
Ryder Sun 525901dd9a fix: preserve native tools and allow running conversation rename
Allow running conversations to rename and inspect locked settings. Honor writable group authorization across native discussion and concurrent replies, preserve configured plugins, and invalidate approvals when candidate files change.

Verify frontend 376 tests, desktop 1692 tests, web/desktop builds, and desktop pack. Electron confirms live rename and locked settings; native networking works, but repeated-output completion remains unresolved in OpenCode/MiMo.
2026-10-30 15:32:42 +08:00
Ryder Sun c6b08f4ce1 test: wait for observed tool start before cancelling native CLI
Replace child readiness-file timing with the actual mapped tool_start event so cancellation consistently verifies the matching stopped event. Ensure the fixture process is aborted during cleanup.

Checks: full desktop discovery suite 1685/1685 at concurrency 4; frontend 375/375; web and desktop builds; desktop pack; Electron first-run/returning-user loading, theme colors and narrow-window checks.
2026-10-30 11:09:34 +08:00
Ryder Sun d2f6171ddd fix: show one compact theme-aware discovery loader
Remove the duplicate progress bar and pulsing status dot. Reduce wave geometry to 190 by 72 and inherit existing light/dark accent colors. Record the provider-agnostic native CLI boundary in AGENTS.md.

Checks: frontend 375 tests; web and desktop builds; desktop pack; Electron first-run and returning-user checks with animated loader, light/dark colors and a 560px window.
2026-10-30 11:01:49 +08:00
Ryder Sun 05ec2bfbf0 merge: retain main updates without automatic Agent hard limits
Integrate origin/main while replacing tool-budget termination with a sequential OpenCode regression that verifies twenty native tools can finish normally. Preserve localized diagnostics for historical failures.

Checks: invocation watchdog and sequential OpenCode focused tests passed; frontend loading suite 375 tests passed before integration.
2026-10-30 10:54:32 +08:00
Ryder Sun 59a546e071 fix: require native Agent consensus and show discovery loading first
Preserve native CLI completion and recovery, collect explicit approvals for an immutable candidate, and honor invitation order. Display the existing loading animation before first-run onboarding. Keep provider configuration owned by native Agents.

Checks: desktop 1684 tests, frontend 375 tests, both frontend builds and desktop pack; real six-Agent Electron consensus completed, including a 381-second Hermes invocation.
2026-10-30 10:53:15 +08:00
Ryder Sun 9ed1552cec fix: bound stalled Auto discussion agent turns
Stop Auto V4 proposal and discussion invocations after prolonged answer silence, excessive tool calls, or an absolute turn deadline. Preserve streamed output when OpenCode or another native Agent never returns a terminal result.

Checks: watchdog and sequential OpenCode focused tests passed; frontend 370 tests and both builds passed; isolated Electron pack and code signature verification passed. Full desktop suite was attempted but blocked by unrelated concurrent test load and timeout failures.
2026-10-28 17:52:39 +08:00
Ryder Sun 4f10fd14d9 site: simplify landing messaging and refresh collaboration stepper
- Drop the 'V4' suffix from Auto Discussion branding across copy and structured data
- Remove the Capabilities section; fold its boundary/shared-context messaging into existing sections
- Rework the scenario stepper with round-by-round agent activity states (Waiting/Thinking/Working/Done) and bilingual runtime labels
- Bump asset cache versions; update landing tests (13 pass via node --test)
2026-10-28 13:43:40 +08:00
Ryder Sun d11deef490 release: prepare 1.0.8 with verified native CLI fixes and explicit limits
Align app, README and website downloads on 1.0.8. Document the separate Magpie
finish-reason defect and native configuration failures instead of treating CLI
detection as successful execution. Keep Apache-2.0 commercial-use terms
consistent and retain the ad-hoc signing/notarization disclosure.

Validation: desktop 1655 tests, frontend 370 tests, website 12 tests and packaging
8 tests; web/desktop builds; DMG/ZIP integrity and packaged signatures/licenses.
Electron five-Agent unlimited acceptance completed 19 native calls without
failed attempts or retries. Auto mode survives sending, completion and group
navigation. Native acceptance used an isolated Magpie hotfix; existing user
gateways were not replaced or restarted.
2026-10-27 23:01:40 +08:00
Ryder Sun bd395b7a2f fix: finish native Agent queries and preserve discussion state
Keep Kimi read-only queries in manual approval mode rather than forcing a
planning cycle, and prefer its terminal rejection over feedback-free revision.
Preserve MiMo's same-message tail after the terminal RPC, forward supported
native configuration variables, cancel repetitive native output while retaining
partial content, and preserve each group's selected composer mode.

Validation: frontend 370 tests; desktop 1655 tests; web and desktop builds;
desktop pack and packaged signature verification. Audited all 13 installed
CLIs. A real five-Agent unlimited discussion completed 14 native calls through
an isolated Magpie null-tool-call fix. Five CLIs still have native configuration
failures; production Magpie remains unchanged. Electron UI acceptance awaits
macOS unlock; this commit does not certify production acceptance or publish a
release.
2026-10-27 21:09:46 +08:00
Ryder Sun 4c522c0997 release: unify 1.0.7 downloads and Apache-2.0 commercial-use terms
Update bilingual documentation and website, include the official Zcode logo and deployable license notices, and replace the unverified display font with the bundled OFL font. Guard version, download and licensing consistency in CI. Give the native ACP multi-phase integration test a realistic completion budget.

Validation: frontend 369/369, landing 12/12, deterministic eval 6 cases/18 results, web/desktop builds, pack/dist, DMG/ZIP integrity and extracted-app signature/license checks passed. Packaged Electron startup passed. The ACP restart group passed 10/10 after one full-suite timing failure; the full desktop suite is rerunning before publication.
2026-10-27 15:28:09 +08:00
Ryder Sun 9971659e8a Merge remote-tracking branch 'origin/main' into 1.0.7
# Conflicts:
#	README.md
#	README.zh-CN.md
#	meldwork-landing/index.html
2026-10-27 15:17:12 +08:00
Ryder Sun 83709a13e8 fix: preserve interrupted discussion output and support native Zcode and Hermes formats
Prevent terminal-only plan dialogs from blocking headless Agents, preserve buffered replies on stop, and keep Chinese slash-separated text intact. Add Zcode discovery, execution, credentials diagnostics, and its official logo; use Hermes native streaming when supported.

Validation: 1637 desktop tests and 369 frontend tests passed; 91 follow-up CLI tests, web/desktop builds, packaging, Electron cancellation/reload checks, and live WorkBuddy invocation passed. ZGCI finish_reason and missing Zcode default credentials remain external limitations.
2026-10-27 15:14:40 +08:00
Ryder Sun 5a938f60d2 feat(landing): refresh landing page with ember background and bilingual i18n
- Add glyph-embers.js canvas background and i18n.js language switching
- Refresh index.html, main.js, and styles.css layout and content
- Bundle Fusion Pixel fonts for CJK display type
- Add landing test suite (node --test meldwork-landing/test/landing.test.cjs)
- Update landing docs and switch README license notice to Apache 2.0

Checks: node --test meldwork-landing/test/landing.test.cjs (11 passed)
2026-10-21 12:46:35 +08:00
Ryder Sun a97fd91975 feat: use 0-100 scoring for delivery preferences and refresh settings layout
- Replace three-step work preference dials with 0-100 score scale
  (desktop schema, prompts, frontend form, presets, styles, locales)
- Restructure general settings panel layout with WorkPreferences section
- Update landing page copy and README download details
- Update related frontend and desktop tests

Checks: npm --prefix frontend test (369 passed),
node --test desktop/test/workspace/work-preferences.test.cjs (3 passed)
2026-10-21 10:39:54 +08:00
Ryder Sun fbc6e08e80 chore: untrack local docs folder from version control
docs/ is already in .gitignore but two files remained tracked.
Removed them from the index so future docs stay local-only.
2026-10-21 10:39:50 +08:00
Ryder Sun 0591472bc4 docs: refresh V1.0.6 downloads and release details
Commit the previously retained documentation changes separately from V1.0.7
runtime work. Update English/Chinese README and landing download links, release
metadata and verification notes to the published V1.0.6 Apple silicon preview,
including the Ryder-Sun repository and website URLs.

Keep the existing product copy and document preview signing and acceptance limits.
Checks: reviewed the three-file diff and git diff --cached --check passed.
This is documentation only; no additional runtime tests were needed.
2026-10-20 14:32:58 +08:00
Ryder Sun 35b8075b87 perf: compact shared discussion context without acknowledging excerpts as full
Bound shared transcripts to 16000 characters with fair per-peer budgets. Retain
source IDs, opening/closing context, constraints, evidence and unresolved issues
using deterministic source excerpts; deduplicate repeated sentences and retain
early user anchors in frozen history. Partial text never proves full delivery.

Document native Agent findings, common-context guarantees and limitations.
The fixed 32-peer repetition sample drops 338018 to 6512 characters (98.07%);
this is not a universal compression rate or a lossless semantic guarantee.

Checks on the combined working tree: desktop full suite 1629/1630, with the sole
failure being an obsolete requirement for prompts longer than 24000 characters.
After replacing that assertion with compaction and fidelity checks, the complete
V4 natural-discussion file passed 40/40. Focused context/preferences/MiMo/preload
passed 47/47; frontend 365/365, builds, pack and Electron smoke passed.
git diff --cached --check passed.
2026-10-20 14:32:58 +08:00
Ryder Sun c92b30c2c6 feat: add optional delivery preferences to onboarding and settings
Let users start from a preset and tune delivery detail, initiative, evidence,
preferred collaborators and a short custom note. Make first-run setup skippable
and editable later; refresh collaborator choices after startup detection.

Validate and persist preferences through narrow local IPC with rollback and
active-run protection. Defaults add no prompt, custom notes are bounded, and
preferences never grant write access or add group participants automatically.

Checks on the combined working tree: frontend 365/365; focused context,
preferences, MiMo and preload 47/47; web/desktop builds and pack passed.
Isolated Electron verified presets, custom save, persistence, narrow-window
layout and Escape dismissal. git diff --cached --check passed.
2026-10-20 14:32:35 +08:00
Ryder Sun 67d4084de6 fix: isolate WorkBuddy CLI credentials and native model compatibility
Distinguish the bundled lite CLI's protected desktop login from independent
CLI installs. Do not infer authentication from unrelated provider keys or choose
the first local model. Offer explicit native tool-model selection as a collapsed
advanced option, pass its model through resume, and keep provider auth failures
run-scoped. Renderer APIs expose only model names and IDs; native secrets stay
in the main-process invocation environment.

Desktop-account/current-model reuse remains unresolved. The official assistant
API requires registered application credentials, OAuth and cloud forwarding;
this change does not bypass authentication or claim that integration is complete.

Checks on the combined working tree: readiness/WorkBuddy/catalog 71/71 and
frontend 365/365 passed; builds, pack and isolated Electron checks passed.
Earlier explicit-provider live direct/resume/group checks passed; these do not
prove automatic desktop-account reuse. git diff --cached --check passed.
2026-10-20 14:32:14 +08:00
Ryder Sun 7f9e95d86c fix: honor MiMo native provider configuration when checking readiness
Recognize the selected custom Provider instead of treating official-account
whoami output as proof that a configured native model cannot run. Keep native
model and credential selection inside MiMo and isolate auth probes in tests.

Checks on the combined working tree: focused context/preferences/MiMo/preload
47/47 passed; readiness/WorkBuddy/catalog 71/71 passed. Real MiMo ACP first call,
native Session continuation, and production Electron group invocation passed
without forcing a model. git diff --cached --check passed.
2026-10-20 14:31:41 +08:00
Ryder Sun f77fd8b0c0 fix: remove chat delivery controls and reclaim conversation space
Remove Tasks & delivery from direct and group conversations, including composer
binding and the obsolete UI smoke. Retain stored delivery records and backend APIs.
Hide the sidebar scrollbar while preserving scrolling, and remove the reported
conversation and execution-detail borders without redesigning the workbench.

Checks on the combined working tree: frontend 365/365; web and desktop builds,
desktop pack, and isolated Electron UI/native MiMo smoke passed. The smoke checked
three-row chat layout, no delivery panel, and a scrollable sidebar with no scrollbar.
git diff --cached --check passed.
2026-10-20 14:31:01 +08:00
Ryder Sun 6983e58791 fix: restore local Agent discovery startup animations
What changed
- Restore PixelBlast and WavePhysicsLoader with the three dependency.
- Show the discovery overlay and block workspace interaction while local Agent
  CLI detection runs; release the overlay on both success and failure.
- Restore theme styles and Chinese/English startup copy, with App coverage for
  pending detection, completion, errors and animation wiring.

Why
Restore the intended local Agent discovery startup experience while retaining
the existing workbench once detection settles. Keep this separate from P0 delivery.

Checks
- Final vitest run of DeliveryPanel.spec.js and App.spec.js: 54/54 passed.
- Previously completed combined-tree validation: frontend 362/362, web build,
  desktop build, desktop pack and Electron smoke passed. Pack lacks a Developer
  ID signing identity; deterministic delivery smoke does not test live providers.
- git diff --check and git diff --cached --check passed.

README.md, README.zh-CN.md and meldwork-landing/index.html remain uncommitted.
2026-10-19 22:28:22 +08:00
Ryder Sun c01f96b4c7 feat: add V1.0.7 P0 task delivery and human acceptance
What changed
- Document P0 scope, interactions, task/run/delivery states and acceptance criteria.
- Add optional task cards to existing conversations and bind execution through
  the existing composer; freeze inputs and retain independent delivery versions.
- Support version-specific revision, human review/acceptance, native Markdown
  and file exports, and reuse in a fresh read-only conversation.
- Persist delivery state and retained attachments through validated main-process
  IPC and narrow preload APIs; add component, workspace and Electron smoke tests.

Why
Turn conversations into reviewable work deliveries without replacing the existing
chat, Agent selection or permission flow. Run completion never implies acceptance.

Checks
- Final node --test desktop/test/workspace/delivery-tasks.test.cjs: 9/9 passed.
- Final frontend vitest DeliveryPanel.spec.js and App.spec.js: 54/54 passed
  on the combined working tree (startup restoration is committed separately).
- Earlier combined-tree frontend test: 362/362; web and desktop builds, desktop
  pack and deterministic Electron delivery smoke passed.
- Desktop full suite: 1607/1609. Both failures were 10-second V4 restart waits;
  each passed two isolated reruns. Unmodified main 7deb8a3 also passes in similar
  time (Gate 8.9s, ACP 5.9s), supporting load-sensitive timeout flakiness rather
  than an observed delivery regression. Full suite is not claimed all-green.
- git diff --check and git diff --cached --check passed.

Frontend three dependency and startup animation changes are intentionally left
for the next commit. Existing README and landing-page edits remain uncommitted.
2026-10-19 22:28:04 +08:00
Ryder Sun a501718400 docs: update README and website release information for V1.0.6
Point downloads to the published 0.1.6 Apple silicon asset, use the renamed repository, and describe the release improvements and verification limits. Checked README versions, website metadata, release links, local assets, and git diff --check.
2026-10-19 14:18:39 +08:00
Ryder Sun 7deb8a3d12 Merge feature/1.0.6 into main: Meldwork V1.0.6, and the V1.0.5 line that never reached main
What this brings into main (53 commits)
- The V1.0.5 line, published as a prerelease on 2026-09-17 (tag Meldwork-V1.0.5
  at bb55fad). It was released from feature/1.0.5 and never merged: local Agent
  readiness and recovery, isolated per-conversation Agent sessions, workspace
  preference and titlebar changes, the Pi capability probe, and the General
  Agent workspace positioning.
- Meldwork V1.0.6, the 38 commits after that tag plus the release-prep commit:
  - Agent reliability: tiered authentication failures with transient retries,
    long-lived ACP Agents that survive stops, restarts and hangs, a watchdog that
    no longer kills Agents that are still working, unsafe ACP permission requests
    refused in read-only turns, and WorkBuddy's bundled CLI launching again.
  - Group conversations: replies shown in dispatch order, natural multi-round
    discussions that no longer end on one slip, attachments and knowledge bases
    delivered to group Agents, a group no longer blocked by one unready member,
    and conversations that no longer wait for each other.
  - Interface: startup no longer blocked by Agent detection, marks for
    conversations waiting on a decision, a General settings tab with
    System/Light/Dark appearance, notes explaining why an installed Agent is
    unavailable, and a shared type scale and status colors. The chat page and the
    dialogs keep their previous look.
  - Internal: the 6000-line auto-runner split into modules grouped by purpose.
- Package version 0.1.6 and the CHANGELOG entry.
- docs/ is no longer tracked. That was done by 6aef211 on the V1.0.5 line; the
  files stay in history.

Why
main stopped at eac015d while the V1.0.5 release line, and then V1.0.6, moved
on. This joins them so main is the source for the next release. The V1.0.6 work
had been developed under the name feature/1.0.5; it lives on feature/1.0.6 now
and feature/1.0.5 is back at the published V1.0.5.

Checks
- main had no commit the branch lacked, so there are no conflicts and the merged
  tree is identical to feature/1.0.6 at 11a25df.
- On the code of ce995cb: npm --prefix frontend test 357/357, npm --prefix
  desktop test 1603/1603, deterministic evaluation 6 cases / 18 results, and the
  frontend build and build:desktop passed.
- The release build is verified again on this merge commit before it is tagged.
2026-10-17 19:14:26 +08:00
Ryder Sun 11a25df584 release: prepare Meldwork V1.0.6
What changed
- Package version 0.1.5 -> 0.1.6 in desktop and frontend (package.json and the
  lock files).
- CHANGELOG entry for 0.1.6 (Meldwork-V1.0.6 prerelease).

Why
V1.0.5 was published on 2026-09-17 from the tag Meldwork-V1.0.5 (bb55fad).
Everything after it, 38 commits of Agent reliability, group conversation and
interface work, is version 1.0.6. This line was developed under the name
feature/1.0.5 by mistake. It is feature/1.0.6 now, and feature/1.0.5 is back at
the published V1.0.5.

Checks
- Only version fields and the changelog change, and no test pins the version.
- The code under test is that of ce995cb: frontend 357/357, desktop 1603/1603,
  deterministic evaluation 6 cases / 18 results.
2026-10-17 19:09:45 +08:00
Ryder Sun ce995cb93d feat: refresh the app UI and surface what needs the user
The chat page (header, messages, composer, run status and the animated empty
state) and the modal dialogs are deliberately left as they were before this
refresh: those files are byte-identical to 17c57f5.

What changed
- Design tokens: an 8-step type scale (readable text no smaller than 12px
  outside badges), semantic success/warning/danger/info colors for both themes,
  AA-contrast muted and accent text, a focus-ring token, elevation, layer and
  motion tokens, and a dark backdrop that is darker than the workspace card.
- Startup is no longer blocked by Agent detection. The full-screen WebGL overlay
  and the physics loader are removed (three is dropped from the dependencies);
  the sidebar, home page and composer show "Detecting local Agents" instead, and
  an empty Agent list is not reported as a setup problem while detection runs.
- Needs your decision: conversations waiting on a permission or decision are
  marked in the sidebar (rows, Agent icons, collapsed switcher) and listed on the
  home page. Group runs that finish while you are elsewhere get the same finished
  mark direct chats already had. Collapsed sidebar sections show a count.
- Settings: a new General tab (language, appearance: System/Light/Dark). The
  theme now follows the OS until one is chosen; it used to freeze on first
  launch. Agent cards explain why an installed Agent is unavailable (installed
  version, verified range, failed check) with a Re-detect action. Cloud
  connections ask before deleting.
- Gemini: a detected Gemini CLI gets its own logo. It stays out of the built-in
  list so nobody sees a "Not installed" card for an Agent Meldwork cannot install.
- Toasts: the duration grows with the message length and pauses on hover; errors
  use the alert role.
- Onboarding: a Skip button and arrow-key navigation.
- Unsent text is kept per conversation while the app is open.
- Copy: engineering terms (circuit breaker, hard budget, delivery owner) are
  gone from user-facing status text.
- Status colors, focus ring and the run-details waiting state use the new tokens.

Why
Small text and low-contrast grey carried the most important state (Agent
availability, time, hints); the brand color meant ready, done, running and
warning at once; and the app could not be used until local Agent detection
finished although history is local. This makes states legible and separable and
shows the ones that need the user.

Checks
- npm --prefix frontend test: 43 files, 357 tests passed
- npm --prefix frontend run build and build:desktop: passed
- Not verified in a running Electron window; the changed screens need a visual
  check in light and dark, Chinese and English.
2026-10-16 23:25:46 +08:00
Ryder Sun ef9933d571 fix: launch the WorkBuddy app's bundled CLI, which now ships only the lite bundle
What changed
- WorkBuddy is launched with CODEBUDDY_FORCE_LITE_WB_BUNDLE=1 when the install
  carries only dist/codebuddy-lite-wb.mjs, both for the capability probe
  (--help) and for real runs (the child environment). Installs that carry a
  regular bundle launch exactly as before, and other Agents are unaffected.
- Tests cover the lite-only layout, an install with a regular bundle, another
  Agent kind, an unreadable layout, a symlinked launcher, and the run
  environment.

Why
WorkBuddy 5.8.x updated its bundled CLI (2.161.0) to the lite bundle, which its
launcher loads only when that variable is set. Without it every headless call,
--help included, exits with MODULE_NOT_FOUND ("Cannot find module
'../dist/codebuddy'"). The capability probe therefore failed, WorkBuddy was
marked incompatible, and it disappeared from the usable Agent lists (new group,
@ menu). A CLI version outside the verified range does not block an Agent on its
own; only a failed capability probe does.

Checks
- npm --prefix desktop test: 1603 passed, 0 failed
- detectAgents() on this machine now reports WorkBuddy 2.161.0 as compatible; it
  was incompatible with a failed workbuddy-print probe before
2026-10-16 23:24:37 +08:00
Ryder Sun 17c57f57f8 test: a conversation stuck on an Agent must not stop another from using it
End-to-end version of the blocking fix, on the real LocalWorkspace: one direct
chat's turn never finishes, and a second direct chat with the same Agent has to
answer anyway while the first is still marked running. On the commit before the
fix this test times out, which is the symptom that was reported (the second
conversation sat on 'waiting for another Agent that is writing in the same
folder' until the first finished).
2026-10-16 16:10:03 +08:00
Ryder Sun 5054553a16 fix: the agent avatars no longer draw over the composer's @ menu
In a group chat the strip of member avatars above the composer (the run
status for the latest topic) was painted on top of the @ menu, covering its
'Agents' heading and the first rows.

The avatars overlap each other and order themselves with z-index 100, 99, 98
and so on. Nothing around them created a stacking context, so those numbers
competed with the page directly and beat the menu (z-index 4). The strip is
now its own stacking context, so the ordering only applies between the
avatars. The composer box gets the same treatment for the member chips (z-index
1-6): they were only contained while the box was focused, because the box's
focus transform happened to create a context.

Checked in a real renderer (headless Electron, the built frontend with a stub
bridge, a six-member group with a finished run and with a running run, composer
focused and not): the points of the menu covered by something else go from 36
to 0 in both states. jsdom has no layout, so composerStacking.spec.js pins the
CSS that keeps the numbers contained; two of its three cases fail without the
fix.

Checked with: npm --prefix frontend test (336 pass), build:desktop.
2026-10-16 16:02:48 +08:00
Ryder Sun e1500a73ef fix: conversations no longer wait for each other, even in the same folder
Sending to an Agent while the same Agent (or any Agent) was busy in another
conversation made the new conversation sit on 'Running' until the first one
finished. The cause was the writer lease, which was keyed by the real folder:
every writable conversation that used the same folder (and most keep the
default one) took turns, one at a time, across groups and direct chats.

9198bf6 had deliberately scoped the lease of direct chats to the chat so that
one writable chat could not block another Agent. 17cbd32 reverted that as a
regression and added a 'waiting for another Agent writing in the same folder'
notice. That was the wrong call: it made the product behave like a single
queue. This commit reverts 17cbd32 and goes one step further than 9198bf6:

- the writer lease is scoped to the conversation for groups as well as direct
  chats, so conversations never wait for each other; writers inside one
  conversation still take turns
- the waiting notice, the waitingForWorkspaceKinds snapshot field and the
  scheduler queue callbacks that only existed for the folder lock are removed
  with the revert
- the old rule 'group chats serialize writes across symlink aliases' is
  replaced by its opposite; new tests cover the same Agent in two direct chats
  and a group at once, group chats sharing a folder through a symlink alias,
  and that turns of one conversation share a lease scope while another
  conversation in the same folder does not
- architecture.md states the new rule and the trade-off

Trade-off: two Agents in different conversations can now edit the same files
at the same time, like two terminals in one directory. Output capture compares
.meldwork-output/ before and after a run, so when two conversations produce
deliverables in the same folder at the same time one may attach a file the
other wrote. No guard was added for that.

The new write-scheduling tests fail on the previous commit (two fail, one
never gets its second Agent started).

Checked with: local-workspace-write-scheduling (6 pass), run-scheduler,
run-coordinator, state, harness, npm --prefix frontend test (333 pass).
2026-10-16 15:10:35 +08:00
Ryder Sun 4a0915594f test: pin that unsupported ACP client requests get an error, never a success
fs/* and terminal/* requests from an Agent that never was offered those
capabilities, and unknown methods, must each receive an error response. The
behaviour held partly through parameter validation; the test now states the
contract (no result field, numeric error code, unknown methods get -32601).
2026-10-16 11:55:16 +08:00
Ryder Sun 5641969445 fix: switching between read-only and write no longer restarts the ACP process
The ACP runtime was identified by a signature that included the session mode
(plan/build for MiMo and OpenCode, default/accept_edits for Hermes). Group
discussions alternate between a read-only proposal and a write-enabled turn, so
the signature changed, the process was closed and the stored native session was
reported invalid, losing the conversation's context on every switch.

The mode is applied on every turn with session/set_mode, so it does not
identify a process. What genuinely needs another process (Hermes' and
OpenCode's permission policy is passed through the environment) is already part
of the signature through the environment, so those still start separately.
MiMo, whose arguments and environment are identical in both modes, now keeps
one process and one session across a read-only turn, a write turn and another
read-only turn.

The new test fails on the previous commit with LOCAL_AGENT_SESSION_INVALID.

Checked with: cli-native-acp-lifecycle (16 pass).
2026-10-16 11:51:00 +08:00
Ryder Sun 4b8ddd917f docs: describe the module layout and reliability rules, and fix dead links
architecture.md pointed at docs/flows.md, permissions.md, variables.md and
automation.md, which exist on no branch, and at three more that exist only on
main (this branch keeps /docs/ untracked). The related-documents list now
links only files tracked in the repository.

Adds a Code Map (what each invocation, prompt and auto-runner module owns and
where new behaviour should go) and the Reliability Rules the harness now
enforces, so the next change starts from the layout instead of the 6000-line
class.
2026-10-16 11:43:34 +08:00
Ryder Sun 0e4f1620e6 refactor: split the 6000-line auto-runner into files grouped by what they do
LocalWorkspaceAutoRunner was one class of 6000 lines. Its 99 largest methods now
live in eleven files named for their topic (recovery, collaboration,
natural-discussion, v4-context, v4-delivery, v4-synthesis, v4-results,
v4-phase, v4-discussion, task-graph, rounds) and are assembled onto the same
prototype the way the existing natural-decisions and v4-outcomes parts already
were. local-workspace-auto-runner.cjs keeps the constructor, start/resume and
run-level helpers (285 lines); the largest remaining file is 965 lines.

This is a move, not a rewrite. The split was generated by a script that copies
method source verbatim, and checked three ways before the tests ran:
- all 135 prototype methods exist with the same name and an identical source
  hash before and after (only the class constructor text differs, because the
  mixins are listed after it)
- a scope analysis finds no identifier that is read but not declared or
  imported in any of the 15 auto-runner files
- node --check on every file

The V2 round and V3 task-graph runs, which only exist to resume older runs,
are in their own files so they can be deleted together when that support ends.

Checked with: npm --prefix desktop run test:workspace (627 pass).
2026-10-16 11:43:34 +08:00
Ryder Sun 958d18152b fix: MiMo/OpenCode turns with tool calls are no longer cut short, and a CLI's own error is no longer masked by EPIPE
Two ways a CLI run ended early or with the wrong reason:

- MiMo and OpenCode print a step_finish after every model step. A step that
  ends with reason tool-calls is followed by more steps, but the transport
  terminated the process 250 ms after the first step_finish, so every turn
  that used tools came back as a partial answer. Only a final step now ends
  the run.
- Codex reads its prompt from stdin. A CLI that exits at once (expired
  sign-in, rejected flag) while a prompt larger than the pipe buffer is still
  being written produces EPIPE, which was reported as LOCAL_AGENT_SEND_FAILED
  instead of the CLI's own message. A closed pipe now waits for the process to
  finish and reports its stderr (so a sign-in failure is recognised as one);
  a process that stays alive without reading is still a send failure after
  5 seconds.

Both regressions fail on the previous commit.

Checked with: cli-adapters (74 pass incl. cli-process-support).
2026-10-16 11:29:28 +08:00
Ryder Sun bf49afcd92 feat: a member that is not ready no longer blocks messages to the whole group
Sending to everyone in a group failed with 'this local Agent is unavailable'
as soon as one member was not ready, so one broken CLI made the whole group
unusable. When the recipients are implicit (everyone, or auto discussion),
members that are not ready are now left out and the user is told which ones
were skipped. Agents the user picked or @mentioned explicitly are kept so the
send can still report them, and when nobody is ready the full list is kept so
the problem is not hidden.

Checked with: composerContext spec (3 new cases), npm --prefix frontend test
(333 pass).
2026-10-16 11:21:36 +08:00
Ryder Sun 49ed8aee0f fix: an Agent stays available through an inconclusive probe, and keeps its shell environment
Two ways a healthy Agent used to drop out of use without anything being wrong
with it:

- readiness: OpenClaw's 7 second status check, or any probe that crashes,
  marked the Agent unavailable at once, so it vanished from the picker until
  the next successful probe. A timeout is not a 'not signed in' answer. An
  Agent that was available now stays available through inconclusive results
  for up to 10 minutes; a definitive answer (signed out, incompatible version,
  a recorded auth failure) still applies immediately, and an Agent that was
  never available is not made available by it.
- login shell: when the shell used to capture PATH and API keys did not answer
  (slow rc file, cold boot), the bare app environment was used and cached for
  30 seconds, so Agents started without the user's PATH or keys and failed
  with sign-in errors. The last environment the shell produced is kept and
  reused; the timeout is 10 seconds; with no earlier success the failure is
  cached for 5 seconds instead of 30, measured from when the capture ends.

Both new tests fail on the previous commit.

Checked with: local-workspace-agent-catalog (22 pass), local-agent-readiness
(39), cli-network-environment, main-security (51).
2026-10-16 11:21:36 +08:00
Ryder Sun 462c1bd783 fix: an ACP turn no longer dies on traffic this client does not know
The ACP transport failed the whole turn on anything it did not recognise: a
banner line on stdout, a notification with an unknown method, a session update
kind added by a newer Agent, or an unknown request. Every Agent upgrade that
adds a message therefore broke Meldwork until it was updated, and a single
large tool result (over 1 MiB on one line) failed the turn.

- stdout lines that are not JSON objects are ignored
- unknown notifications and session updates that fail validation are dropped
  before they reach the SDK, which logs rejected messages with their params
- unknown requests are forwarded with their params stripped, so the Agent gets
  a proper 'method not found' answer instead of waiting, and nothing
  secret-bearing is logged
- the per-line limit is 4 MiB (still bounded; total input and reply limits are
  unchanged)
- truncated JSON that starts like a message, an invalid response payload and
  invalid permission requests still fail closed

The Kimi 'unsafe protocol input' test described the old behaviour; it now
checks that malformed JSON still fails closed and that unknown traffic carrying
a secret neither fails the turn nor reaches a log. The new tolerance test
fails on the previous commit (LOCAL_AGENT_PROCESS_FAILED).

Checked with: cli-adapters-agent-protocols (69 pass), cli-native-acp-lifecycle
(15), cli-acp-tolerance, cli-adapters (64).
2026-10-16 11:21:36 +08:00
Ryder Sun a97afa9b5b feat: deliver attachments and knowledge bases to group Agents; one prompt module per mode
Group chats with several Agents run on the V4 path, which never forwarded the
user's attached files to the Agents (the send was validated against what each
Agent can read, then the files were dropped), and its prompts omitted the
knowledge-base hints, the answer language and the deliverable folder contract.

- attachments are now staged for manual group replies and for every turn of
  an automatic discussion; frozen prompts only gain the staged-file section
- manual group replies no longer use the phased 'proposal' template (asking
  for capabilities, work items and a receipt). prompts/manual-reply-prompt
  tells each Agent that everyone answers on their own, whether it may change
  files, and to answer in the user's language
- natural discussion prompts move to prompts/natural-discussion-prompt with
  named sections (brief, writer, owner, routing, receipt), now including the
  group name and topic, knowledge bases, the answer language and, for the
  writer, the .meldwork-output contract
- shared language and delivery contracts live in prompts/prompt-contracts

Also made the hanging-ACP-initialize test independent of spawn counting: under
load the 300 ms timeout killed the first process before it recorded itself, so
the next spawn counted as the first one and hung (16 of 32 stressed runs).

Checked with: new tests for each prompt module and for attachment delivery
(both fail on the previous commit); the V4, context-pack and delivery test
files pass.
2026-10-16 10:49:44 +08:00
Ryder Sun 3ce566f152 refactor: split the invocation's session, outputs, budget, prompt and failure handling into modules
The 1500-line invokeLeased still mixed session bookkeeping, workspace output
capture, budget accounting, prompt assembly and failure classification with
the run lifecycle. Each is now a small module with unit tests:

- InvocationSession: one place that resumes, adopts, discards (rebuilds) and
  completes the native session; the three copies of the 'start a fresh
  session' dance, one of them dead code, are gone
- InvocationOutputs: workspace baseline, media import and outcome descriptors
- invocation-budget: usage recording; a payload without a recorded wire size
  no longer fails the turn
- invocation-prompt: the prompt one Agent receives (frozen, direct, group)
- invocation-failure: why an invocation ended (stopped, timed out,
  interrupted, cancelled, failed) and the hidden error metadata
- permission helpers for resumed decisions moved next to the permission policy

invokeLeased is about 1000 lines shorter and reads as the run lifecycle.
Behaviour is unchanged; focused invocation tests and the new module tests
pass.
2026-10-16 10:21:25 +08:00
Ryder Sun 68145eb319 refactor: extract receipt parsing, watchdog, stream marker filter and permission policy from the invocation
local-workspace-agent-invocation.cjs held four independent pieces of logic
inside one 1500-line method. They now live in small modules with their own
tests, and the invocation only wires them together:

- collaboration/collaboration-receipt.cjs: parse and normalise the receipt
  (unchanged behaviour; the existing receipt tests now import it directly)
- runs/invocation-watchdog.cjs: startup, inactivity and stall windows
- collaboration/answer-marker-filter.cjs: hides control markers while the
  answer streams
- agents/agent-permission-policy.cjs: automatic answers to permission
  requests, read-only turns only approve side-effect-free tools

Two latent defects fixed on the way:
- the watchdog cleared its timer on the first event without re-arming it, so
  an Agent that went silent right after its first event was never stopped
- the marker filter upper-cased the buffer to find markers, which shifts the
  cut position for characters whose upper-case form is longer (e.g. ss)

Checked with: node --test on the new module tests; npm --prefix desktop run
test:workspace (595 pass).
2026-10-16 10:09:38 +08:00
Ryder Sun c3c090f83b fix: write state compactly and durably, and record main-process faults
- The workspace state is rewritten synchronously on the main process for
  every save. It is now compact JSON written through the private atomic
  writer, which fsyncs before renaming (the old path could leave a
  truncated file after a crash). The Run Ledger snapshot is compact too.
- Unhandled rejections and uncaught exceptions in the main process are
  written to a size-capped, secret-redacted meldwork-diagnostics.log in
  userData instead of disappearing, and a renderer that crashes is
  reloaded instead of leaving a blank window.

Checks: run-ledger, local-workspace-state, local-workspace and
main-security tests (185 pass). Full desktop suite: 1540 pass; frontend
web and desktop builds pass (existing large-chunk warning only).
2026-10-15 19:38:33 +08:00
Ryder Sun 26d4be2404 fix: compare installed Agent CLI versions as releases
The installer decided "up to date" and "already installed" with a
substring test, so 11.2.3 counted as 1.2.3 and a newer CLI was offered a
downgrade. It now extracts both versions and compares them as releases
(installed is current when it is the recipe's version or newer), falling
back to the old string match only when a version cannot be parsed.

Checks: agent-installer and the installer facade API test (37 pass,
including a new version comparison test). Full desktop suite: 1540 pass.
2026-10-15 19:38:33 +08:00
Ryder Sun 00ba2d3b38 fix: import only files an Agent wrote, including paths with spaces
28127b3 imports files whose absolute paths appear in a reply, to catch
outputs written outside .meldwork-output. Those candidates skipped the
baseline and start-time checks, so a file the Agent merely mentioned
(the README it read, the user's staged attachment) was imported as an
output, and paths with spaces were cut at the first space.

Reported paths are now imported only when their content changed during
this run and they are outside the .meldwork-input staging folder.
Backticked, quoted and Markdown-linked paths keep their spaces.

Checks: agent-output-importer (11 pass, including a new reported-path
test). Full desktop suite: 1540 pass.
2026-10-15 19:38:33 +08:00
Ryder Sun 17cbd32387 fix: keep one writer per folder and show when an Agent waits for it
9198bf6 gave every direct chat its own scheduler key so that one writable
chat would not block another. That broke the one-writer-per-folder rule:
two Agents could edit the same files at once, and a direct chat and a
group could write the same folder concurrently. The underlying problem
was that a writer waiting for the folder lease showed nothing and looked
like a dead Agent (no queued event, no watchdog, no output).

- Scheduler requests report onQueued/onGranted when they actually wait.
- Direct chats use the real-folder key again; while an Agent waits for
  the folder, the run lists it in waitingForWorkspaceKinds and the run
  status card says it is waiting for another Agent writing in the same
  folder (zh/en). A stopped wait clears the mark.

Checks: run-scheduler (new queue notice test), write-scheduling (the
concurrent-direct-chat test now asserts the second writer waits, is shown
as waiting, then runs), run-coordinator public run keys. Full desktop
suite: 1537 of 1538 before adding the new public key to that list, which
now passes; npm --prefix frontend test (330).
2026-10-15 19:17:03 +08:00
Ryder Sun 8d8f9e65f6 fix: let discussion peers see workspace file paths instead of [path]
Peer transcripts in natural discussions replaced every token that starts
with "/" by [path]. Agents working on the same repository could not tell
each other which file they changed, and API routes such as /api/users or
commands such as /login were erased too.

Natural discussion transcripts now show files inside the shared
workspace as workspace-relative paths, keep non-filesystem tokens, and
still redact other absolute local paths (/Users, /tmp, ...) and secrets.
Phased-V4 delivery text keeps the previous redaction because its hashes
take part in resume checks.

Checks: collaboration-records (new peer path test), natural discussion
(new end-to-end relative path test), natural transcript and delivery
suites. Full desktop suite: 1537 pass.
2026-10-15 19:04:42 +08:00
Ryder Sun b2249f0859 fix: stop skipping every WorkBuddy permission check in write mode
28127b3 added --dangerously-skip-permissions next to acceptEdits when a
conversation allows workspace writes. That flag lets the CLI run any
command and write anywhere on the machine without confirmation, which
goes beyond Meldwork's opt-in workspace-write boundary and differs from
every other Agent. Write mode now relies on acceptEdits, which accepts
file edits in the working directory.

The local machine has no CodeBuddy CLI, so an edit-only tool allowlist
was not added; an unverified flag could break every WorkBuddy turn.

Checks: new cli-adapters test for WorkBuddy write/read-only arguments;
WorkBuddy adapter and protocol tests (7 pass). Full desktop suite: 1537.
2026-10-15 19:04:42 +08:00
Ryder Sun 6e0becfb70 fix: refuse unsafe ACP permission requests during read-only turns
Local invocations answer ACP permission prompts automatically. The
handler picked the "allow" option for every request, ignoring whether the
turn was read-only, so a read-only discussion participant (for example
Hermes running with command approval prompts) had edit, delete, move,
command execution and mode-switch requests approved.

Read-only turns now approve only side-effect-free tool kinds (read,
search, fetch, think) and refuse everything else, including requests
without a recognised kind. Writable turns and the explicit Human Gate
mode are unchanged.

Checks: new local-workspace-permission-policy tests (read-only refusal
matrix, writable approval). Full desktop suite: 1537 pass.
2026-10-15 19:04:42 +08:00
Ryder Sun 6c0f773d3f fix: ask discussion Agents to engage with each other and stop nudging them to stop
The natural discussion prompt told the owner "another reviewer is
optional", told every Agent it "may finish with nextKinds: []", and showed
a receipt example with empty nextKinds and deliverables. Proposals were
told to "engage with peers" although they run independently; sequential
discussions advertised routing that the code ignores; and the response
scope pointed at an "authoritative" label that natural prompts never
show. Agents therefore rarely requested each other and discussions
collapsed into one owner turn.

- Proposals: say peers answer independently and will be discussed next
  round. Discussion turns: respond to peers' specific points, dispute
  with reasons, build on their work; end every turn with the receipt.
- Owner: before completing, make sure the relevant peers reviewed the
  actual deliverables, otherwise request them with a concrete question.
- Peers: answer the points raised; blocked or needs-human goes to the
  owner, who decides.
- Routing: list every peer whose input is needed and ask them a concrete
  question; the receipt example names a real peer and shows the completed
  form with deliverables.
- Sequential discussions no longer advertise routing and instead say the
  next participant runs after this invocation ends.
- The response scope refers to "the current user task stated above".

Checks: local-workspace natural discussion, natural writer, delivery,
transcript, context-pack and harness suites (152 pass). Full desktop
suite: 1532 pass.
2026-10-15 18:50:46 +08:00
Ryder Sun d22c8eb1f0 fix: keep natural discussions going instead of ending on one slip
Automatic group discussions (the production auto path) ended as
"partial" for reasons that had nothing to do with the task:
- any receipt whose nextKinds named an absent or paused Agent stopped the
  whole discussion with "no valid task completion decision";
- a peer's blocked or needs-human status ended the run on the spot,
  although only the delivery owner judges the task;
- an owner who failed, was paused, or handed off to an absent Agent ended
  the run silently;
- one missing owner receipt stopped the run without asking again;
- strict receipt and taskDecision grammar failed the Agent's whole turn
  for "Done", an @handle, a single string instead of a list, a trailing
  comma or code fence, an extra key, or a long summary, and the failed
  Agent was then excluded from the discussion.

Now absent recipients are dropped with a notice and valid requests still
run; a peer blocker is forwarded to the owner, who decides; the next
active selected Agent takes over ownership with a notice; an ignored
handoff keeps the current owner; an owner without a readable decision is
asked once more and the run stops only after two undecided owner turns;
completing without stopped participants explains why the run is partial.
Discussion receipts are normalised (status synonyms, @handles, case,
lists, smart quotes, trailing commas, closing fences, extra keys, missing
summary) before the strict parser runs, and an unreadable decision is
treated as no decision instead of failing the turn. Phased-V4 receipts
keep their strict contract. New zh/en notices explain each case.

Checks: natural discussion, natural writer, receipt, task-decision and
human-gate recovery suites (99 pass on this tree, including a new fenced
loose-receipt test and updated tests for the new routing, blocker and
handoff behaviour); npm --prefix frontend test (330). Full desktop suite
with the following prompt commit: 1532 pass.
2026-10-15 18:50:46 +08:00
Ryder Sun f994225990 test: give the hanging ACP initialize test a realistic second-spawn timeout
The test used a 100 ms initialize timeout for both turns. Under a loaded
full-suite run the healthy second fake-Agent process also needed more than
100 ms to start, so it fell back too. Only the first process hangs; the
second turn now uses the default timeout.

Checks: the lifecycle test passes three runs in a row.
2026-10-15 18:28:52 +08:00
Ryder Sun 06e1e8772c fix: render group replies in the order Agents were dispatched
No message carried a turn order. The timeline rebuilt one from round,
phase, the Agent's current position in the group and the save time, and
those disagreed:
- live bubbles had numeric startedAt, which Date.parse turns into NaN, so
  every running Agent was pinned under the user message and the list
  reshuffled each time a reply was saved;
- manual concurrent mode wrote failure notices before the barrier
  committed successful replies, so failures always rendered first;
- round-less notices sorted after every round, the 'discussion' phase
  had no rank, and editing the group reordered history;
- natural proposals were saved in completion order and peers read the
  transcript in that order, unlike what the user saw.

Now every group topic message gets a per-topic turnSeq. Batch
dispatchers (manual concurrent replies, natural proposal and discussion
batches) reserve numbers in slot order before dispatching; other turns
reserve at invocation start; failure and stop notices take their turn's
number; other topic messages take the next number when written. The
harness exposes the number on live Agent runs, so a live bubble and the
reply that replaces it share one position. Numbers are derived from saved
messages after a restart (no ledger or journal field changes). The
renderer sorts group topics by turnSeq with one shared rule for the
timeline and the trace panel; older history keeps its saved chronological
order. A failed attempt whose retry was already saved no longer lingers
as a ghost bubble, and peers read turns in dispatch order.

Checks: new local-workspace-turn-order tests (slot order with a failing
middle Agent and reversed completion; numbering continues after restart;
none on direct chats), natural discussion, manual durability and write
scheduling tests (63 pass on this tree); npm --prefix frontend test
(330 pass, including a new conversationOrdering spec and a
production-shaped live round test). Full desktop suite on the combined
tree: 1529 of 1530, the remaining failure being the ACP timing test fixed
in the next commit.
2026-10-15 18:28:52 +08:00
Ryder Sun 6522d357e0 fix: keep long-lived ACP Agents usable across stops, restarts and hangs
Persistent ACP runtimes (Hermes, Kimi, MiMo, OpenCode) were fragile:
- any turn error, including a user stop or watchdog timeout, destroyed
  the process and its native session; Hermes rejects cross-process
  resume, so the conversation then fell back to the silent legacy CLI;
- the process identity hashed the whole child environment, so a fresh
  login-shell PATH (fnm uses a per-shell directory) restarted the process
  and invalidated the stored session;
- initialize had no timeout and ignored stop while holding the
  conversation lock, leaving that Agent stuck until the app restarted;
- stderr kept the first megabyte of the process lifetime, so new
  failures were classified from stale lines.

Now a stopped or timed-out turn waits up to 3 s for the Agent to answer
the cancelled prompt and keeps the process and session when it does;
volatile shell values (PATH, TERM, TMPDIR, fnm variables, ...) no longer
change the process identity; initialize times out after 60 s (settable
per call) and honours stop without falling back; stderr keeps only the
current turn's last 64 KB. Disposable runtimes still close after every
turn.

Checks: cli-native-acp-lifecycle with three new fake-ACP tests (hanging
initialize releases the lock, a cancelled turn reuses one process and
session, a changed PATH keeps the process), plus the existing lifecycle,
adapter and protocol suites (185 pass). Full desktop suite: 1528 pass.
2026-10-15 18:04:17 +08:00
Ryder Sun 28fb01eff5 fix: stop the watchdog from killing Agents that are still working
Group runs gave an Agent three minutes to produce a mapped runtime event,
and only mapped events lifted that deadline. ACP agent_thought_chunk was
mapped to nothing, so Hermes, Kimi, MiMo, OpenCode and OpenClaw were
killed while thinking, and auto mode never retries timeouts. The 3/5
minute inactivity windows also cut off real builds and slow providers.

- Any sign of life (stdout/stderr bytes, ACP traffic) now lifts the
  startup deadline. In automatic discussions a separate no-progress cap
  (three inactivity windows) still stops CLIs that only emit heartbeats,
  so a stalled participant cannot block the round; manual and direct
  turns keep running while the process is active.
- Defaults are 10 minutes of inactivity and 20 minutes while a tool runs.
- ACP thinking is reported as a text-free reasoning status event
  (running, then completed when the answer starts); the private thought
  text never leaves the main process.

Checks: local-workspace-harness (74, including a new slow-starting Agent
test that failed with the old hard deadline, and the existing
heartbeat-only stop test), cli-adapters and cli-adapters-agent-protocols
(event order now includes the reasoning status). Full desktop suite:
1528 pass.
2026-10-15 18:04:17 +08:00
Ryder Sun 5fc70afde9 fix: explain run failures in Chinese and English instead of raw codes
Several codes that end an Agent turn or a whole run had no translation,
so the timeline showed a generic error or the raw code (for example
LOCAL_RUN_PERSIST_FAILED), and users could not tell why a discussion
stopped. The round-limit notice also claimed "no consensus" although
natural discussions have no consensus step.

- Map access-denied, session-invalid, output-limit, send-failed,
  refused, permission race, version/capability/protocol mismatch,
  task-decision and receipt errors, context overflow, persistence
  failures and the circuit breaker to zh/en messages.
- The round-limit notice now says the latest replies are the current
  result and how to continue (backend fallback text updated too).

Checks: npm --prefix frontend test (325 pass); updated assertions that
expected the raw code and the old round-limit wording.
2026-10-15 17:40:04 +08:00
Ryder Sun 35ed0d4e4a fix: send Hermes image attachments over ACP instead of dropping them
eb2234b stopped switching Hermes to its legacy transport for image
attachments to preserve the ACP session, but the ACP runner only sends a
text block, so images reached Hermes as nothing at all (the adapter test
for this failed on bb55fad).

Hermes 0.21.5 advertises promptCapabilities.image during initialize. The
runner now keeps the initialize capabilities, and when an ACP spec
carries imageAttachments it sends them as ACP image content blocks
(png/jpeg/gif/webp, up to 10 MB each) on the same session. An Agent that
does not advertise image prompts fails before prompting, so the existing
setup fallback uses the legacy transport with its --image argument.

Checks: cli-adapters (Hermes invocation spec) and a new
cli-native-acp-lifecycle test with fake Hermes ACP agents covering the
image-capable path and the legacy fallback. The capability response was
verified against the locally installed Hermes 0.21.5 with an
initialize-only handshake (no session or model call).
2026-10-15 17:39:43 +08:00
Ryder Sun 624a099bdd fix: give direct chats the current request, delivery contract, and full task
Commit 9198bf6 made direct chats send only the latest message text and
rely on the native session. After 1c9aabb a rebuilt session received the
continuation text instead, and that text deliberately excludes the
latest user message, so the Agent never saw the request it was answering.
Direct prompts also lost the deliverable contract (.meldwork-output/),
the read-only media warning, staged attachment paths, and regeneration
and recovery instructions, which is why generated files were not
captured and three media tests failed on bb55fad.

- New directPromptFor() leads with the user's own words. A new or rebuilt
  native session gets the earlier conversation plus "Current request:";
  a resumed session gets the request alone. The full delivery and
  language contract is sent once per native session, a one-line reminder
  afterwards. Staged inputs, recovery and regeneration instructions are
  appended again.
- The shared deliveryContract() is used by group and direct prompts.
- Isolated (task-graph) invocations again neither resume nor persist the
  conversation's native session (3ce8ee4 made them resume it, breaking
  the "approved prompt only" contract).
- The current task is no longer cut to 6,000 characters, and longer
  messages no longer fail every turn with
  LOCAL_RUN_REQUIRED_CONTEXT_OVERFLOW: submitted messages are already
  capped at 20,000 characters, so the full request is delivered.

Checks: local-workspace-harness (73) and local-workspace-context-packs
(15) pass on this tree, plus the direct, media and custom-label tests in
local-workspace and native-streaming (18), five of which failed on
bb55fad.
2026-10-15 17:39:18 +08:00
Ryder Sun 2da40db63d fix: tier authentication failures and retry transient provider errors
The merged branches disagreed on authentication failures: feature/1.0.5
marked every runtime auth failure 'unknown' so readiness never flipped,
while main's 22f811c classified any auth-looking diagnostic as
LOCAL_AGENT_AUTH_REQUIRED. Together with broad regexes (a bare
"forbidden" anywhere, a credential regex matching "login" or "403"),
proxy refusals stopped Agents while real sign-outs left them "ready".

- New pure module runs/failure-signals.cjs classifies CLI text:
  explicit sign-in or API-key statements are definitive; line-leading
  403/forbidden/unauthorized and permission_error are ambiguous; tool
  output that merely mentions 403 is neither. It also recognises rate
  limits (with Retry-After hints), network failures, provider 5xx and
  context overflow.
- Definitive failures stay LOCAL_AGENT_AUTH_REQUIRED and now mark the
  credential 'missing' (the catalog clears it after the retry window or
  when native credentials verify). Ambiguous refusals become
  LOCAL_AGENT_ACCESS_DENIED, are retried once, and never touch readiness.
- Process failures whose diagnostics show throttling, network loss or a
  provider outage are retried in that category. Rate limits honour
  Retry-After up to 60 s and wait at least 2 s without a hint.
- A resumed session that no longer fits the model context is classified
  as LOCAL_AGENT_SESSION_INVALID so the existing fresh-session retry
  rebuilds it; Rust panics and JSON parse errors no longer discard a
  healthy session.
- Automatic retries no longer re-run the whole Agent catalog refresh
  before each attempt, which flipped busy Agents to unavailable.

Checks: failure-signals (new), failure-policy, cli-process-support,
cli-adapters-agent-protocols, agent-runtime-contract and the auth/retry
tests in local-workspace and local-workspace-auto pass on this tree,
including three readiness tests that failed on bb55fad.
2026-10-15 17:37:57 +08:00
Ryder Sun 1033d086d3 fix: keep group members after Agent failures and recover emptied groups
An authentication or compatibility failure in an automatic discussion
called removeAgent, which rewrote and persisted group.agentKinds. One
proxy 403 or expired OAuth refresh made the Agent disappear from every
later turn of that group, and when the last member was removed the group
could no longer load: normalizeLoadedGroup rejects an empty member list,
so the next start silently dropped the group together with all of its
messages.

- The auto runner now pauses a failed Agent only for the current run.
  Group membership is never edited; the run-scoped exclusion already
  persisted in orchestration activeKinds keeps resume behaviour intact.
- The notice says the Agent is paused for this discussion, with the
  failure reason, and stays in the group (system.agentPausedAfterFailure,
  zh/en). The removal notice text is kept so history still renders.
- removeAgent refuses to remove a group's last member.
- Loading rebuilds an emptied group's members from the Agents that
  replied in it instead of dropping the group and its history.

Checks: node --test local-workspace-state.test.cjs (3 pass, including a
new recovery test) and the membership tests in
local-workspace-auto.test.cjs. Two tests there that also expect a
'missing' credential state stay failing until the next commit (they
already failed on bb55fad).
2026-10-15 17:36:07 +08:00
Ryder Sun 4f31b77503 chore: remove scratch files committed at the repository root
Commit 6aef211 added temporary icon exports, two large PNG screenshots
and a confirmation.json backup to the repository root. AGENTS.md forbids
placing screenshots and temporary files there and nothing references
them.

Checks: rg found no references to the removed paths.
2026-10-15 16:33:10 +08:00
Ryder Sun 6b6be1403e Merge main into feature/1.0.5
Bring main's 22f811c (auth classification from errors[], OpenClaw runtime
scope per CLI version, full participant round and cross-review gate for
natural discussions) and eac015d (removal of the managed shared Provider)
into the V1.0.5 branch before the stability work.

Conflict resolution:
- local-workspace-agent-catalog.cjs: keep feature's guard that an
  'unknown' runtime credential does not downgrade an authenticated Agent;
  drop the provider-credential-unavailable clause removed on main. The
  tiered auth policy is reconciled in a follow-up commit.
- README.md / README.zh-CN.md: keep feature's V1.0.5 summary and apply
  main's wording that Agents use their own CLI's model provider.
- docs/*: keep feature's untracking of local documentation drafts.
- cli-adapters-discovery.test.cjs: both sides were equivalent.

Checks: npm --prefix frontend test (325 pass). npm --prefix desktop test:
1503/1515 pass. The 12 failures are pre-existing on feature/1.0.5 at
bb55fad (verified by running them on a detached worktree of bb55fad) and
are addressed by the follow-up fixes.
2026-10-15 16:32:53 +08:00
Ryder Sun eac015dbfa refactor: remove the managed shared Provider capability
Each Agent CLI now runs only with its own native credentials. The central
path of configuring one provider inside Meldwork for every Agent added
instability without being realistic to maintain, and it depended on
machine-local configuration details.

- Delete the provider store/options modules, the local-agent-provider IPC
  handlers and preload API, per-CLI env injection (CODEBUDDY_*/ANTHROPIC_*/
  KIMI_* ...), the managed OpenClaw provider runtime, and shared-provider
  catalog gating; startup removes the legacy encrypted
  meldwork-provider.json.
- OpenClaw always resolves its native runtime now; native credential
  passthrough, the isolated gateway/guard runtime, and the
  runtime-auth-failure state machine stay unchanged.
- Remove the Provider settings UI, onboarding slide, providerMode display,
  ~100 i18n keys per language, provider CSS, and update the architecture,
  security, and discoverability docs to the native-credentials boundary.

Checks: npm --prefix desktop test (1512 pass), npm --prefix frontend test
(325 pass), frontend build and build:desktop, desktop pack.
2026-10-14 16:59:21 +08:00
Ryder Sun 22f811cde2 fix: harden local agent runs and broaden natural discussion rounds
- Extract WorkBuddy/Claude/Qwen error-result lists (errors[]) into failure
  diagnostics and classify authentication failures as
  LOCAL_AGENT_AUTH_REQUIRED instead of a bare LOCAL_AGENT_PROCESS_FAILED,
  so the catalog marks the Agent as needing a fresh login and retries stop.
- Scope the isolated OpenClaw runtime directory by session, workspace, and
  CLI version, so an upgraded OpenClaw gets a fresh managed scope instead
  of failing on legacy workspace state written by an older CLI.
- Natural agent-led discussions now open with a full participant round and
  the delivery owner cannot declare completion before peers had a
  discussion round; sequential runs get the same cross-review gate
  (system.autoCrossReview).

Checks: npm --prefix desktop test (1574 pass), npm --prefix frontend test
(343 pass), frontend build and build:desktop, desktop pack.
2026-10-14 16:52:25 +08:00
285 changed files with 19883 additions and 14030 deletions

No files matched your search

+3
View File
@@ -31,6 +31,9 @@ jobs:
- name: Run unit tests
run: npm test
- name: Check website and release licensing consistency
run: node --test ../meldwork-landing/test/landing.test.cjs
- name: Build web application
run: npm run build
-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM575.34 477.84l-61.22 102.3L452.3 477.8a12 12 0 0 0-10.27-5.79h-38.44a12 12 0 0 0-6.4 1.85 12 12 0 0 0-3.75 16.56l82.34 130.42-83.45 132.78a12 12 0 0 0-1.84 6.39 12 12 0 0 0 12 12h34.46a12 12 0 0 0 10.21-5.7l62.7-101.47 62.3 101.45a12 12 0 0 0 10.23 5.72h37.48a12 12 0 0 0 6.48-1.9 12 12 0 0 0 3.62-16.58l-83.83-130.55 85.3-132.47a12 12 0 0 0 1.9-6.5 12 12 0 0 0-12-12h-35.7a12 12 0 0 0-10.29 5.84z"/>
</svg>

Before

Width:  |  Height:  |  Size: 719 B

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7L639.4 73.4c-6-6-14.2-9.4-22.7-9.4H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V311.3c0-8.5-3.4-16.6-9.4-22.6zM400 402c22.1 0 40 17.9 40 40s-17.9 40-40 40-40-17.9-40-40 17.9-40 40-40zm296 294H328c-6.7 0-10.4-7.7-6.3-12.9l99.8-127.2a8 8 0 0 1 12.6 0l41.1 52.4 77.8-99.2a8 8 0 0 1 12.6 0l136.5 174c4.3 5.2.5 12.9-6.1 12.9zm-94-370V137.8L790.2 326H602z"/>
</svg>

Before

Width:  |  Height:  |  Size: 530 B

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM426.13 600.93l59.11 132.97a16 16 0 0 0 14.62 9.5h24.06a16 16 0 0 0 14.63-9.51l59.1-133.35V758a16 16 0 0 0 16.01 16H641a16 16 0 0 0 16-16V486a16 16 0 0 0-16-16h-34.75a16 16 0 0 0-14.67 9.62L512.1 662.2l-79.48-182.59a16 16 0 0 0-14.67-9.61H383a16 16 0 0 0-16 16v272a16 16 0 0 0 16 16h27.13a16 16 0 0 0 16-16V600.93z"/>
</svg>

Before

Width:  |  Height:  |  Size: 635 B

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM633.22 637.26c-15.18-.5-31.32.67-49.65 2.96-24.3-14.99-40.66-35.58-52.28-65.83l1.07-4.38 1.24-5.18c4.3-18.13 6.61-31.36 7.3-44.7.52-10.07-.04-19.36-1.83-27.97-3.3-18.59-16.45-29.46-33.02-30.13-15.45-.63-29.65 8-33.28 21.37-5.91 21.62-2.45 50.07 10.08 98.59-15.96 38.05-37.05 82.66-51.2 107.54-18.89 9.74-33.6 18.6-45.96 28.42-16.3 12.97-26.48 26.3-29.28 40.3-1.36 6.49.69 14.97 5.36 21.92 5.3 7.88 13.28 13 22.85 13.74 24.15 1.87 53.83-23.03 86.6-79.26 3.29-1.1 6.77-2.26 11.02-3.7l11.9-4.02c7.53-2.54 12.99-4.36 18.39-6.11 23.4-7.62 41.1-12.43 57.2-15.17 27.98 14.98 60.32 24.8 82.1 24.8 17.98 0 30.13-9.32 34.52-23.99 3.85-12.88.8-27.82-7.48-36.08-8.56-8.41-24.3-12.43-45.65-13.12zM385.23 765.68v-.36l.13-.34a54.86 54.86 0 0 1 5.6-10.76c4.28-6.58 10.17-13.5 17.47-20.87 3.92-3.95 8-7.8 12.79-12.12 1.07-.96 7.91-7.05 9.19-8.25l11.17-10.4-8.12 12.93c-12.32 19.64-23.46 33.78-33 43-3.51 3.4-6.6 5.9-9.1 7.51a16.43 16.43 0 0 1-2.61 1.42c-.41.17-.77.27-1.13.3a2.2 2.2 0 0 1-1.12-.15 2.07 2.07 0 0 1-1.27-1.91zM511.17 547.4l-2.26 4-1.4-4.38c-3.1-9.83-5.38-24.64-6.01-38-.72-15.2.49-24.32 5.29-24.32 6.74 0 9.83 10.8 10.07 27.05.22 14.28-2.03 29.14-5.7 35.65zm-5.81 58.46l1.53-4.05 2.09 3.8c11.69 21.24 26.86 38.96 43.54 51.31l3.6 2.66-4.39.9c-16.33 3.38-31.54 8.46-52.34 16.85 2.17-.88-21.62 8.86-27.64 11.17l-5.25 2.01 2.8-4.88c12.35-21.5 23.76-47.32 36.05-79.77zm157.62 76.26c-7.86 3.1-24.78.33-54.57-12.39l-7.56-3.22 8.2-.6c23.3-1.73 39.8-.45 49.42 3.07 4.1 1.5 6.83 3.39 8.04 5.55a4.64 4.64 0 0 1-1.36 6.31 6.7 6.7 0 0 1-2.17 1.28z"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.8 KiB

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM468.53 760v-91.54h59.27c60.57 0 100.2-39.65 100.2-98.12 0-58.22-39.58-98.34-99.98-98.34H424a12 12 0 0 0-12 12v276a12 12 0 0 0 12 12h32.53a12 12 0 0 0 12-12zm0-139.33h34.9c47.82 0 67.19-12.93 67.19-50.33 0-32.05-18.12-50.12-49.87-50.12h-52.22v100.45z"/>
</svg>

Before

Width:  |  Height:  |  Size: 571 B

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM320 482a8 8 0 0 0-8 8v48a8 8 0 0 0 8 8h384a8 8 0 0 0 8-8v-48a8 8 0 0 0-8-8H320zm0 136a8 8 0 0 0-8 8v48a8 8 0 0 0 8 8h184a8 8 0 0 0 8-8v-48a8 8 0 0 0-8-8H320z"/>
</svg>

Before

Width:  |  Height:  |  Size: 479 B

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM512 566.1l52.81 197a12 12 0 0 0 11.6 8.9h31.77a12 12 0 0 0 11.6-8.88l74.37-276a12 12 0 0 0 .4-3.12 12 12 0 0 0-12-12h-35.57a12 12 0 0 0-11.7 9.31l-45.78 199.1-49.76-199.32A12 12 0 0 0 528.1 472h-32.2a12 12 0 0 0-11.64 9.1L434.6 680.01 388.5 481.3a12 12 0 0 0-11.68-9.29h-35.39a12 12 0 0 0-3.11.41 12 12 0 0 0-8.47 14.7l74.17 276A12 12 0 0 0 415.6 772h31.99a12 12 0 0 0 11.59-8.9l52.81-197z"/>
</svg>

Before

Width:  |  Height:  |  Size: 711 B

-4
View File
@@ -1,4 +0,0 @@
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 1024 1024">
<path d="M854.6 288.7c6 6 9.4 14.1 9.4 22.6V928c0 17.7-14.3 32-32 32H192c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h424.7c8.5 0 16.7 3.4 22.7 9.4l215.2 215.3zM790.2 326L602 137.8V326h188.2zM296 136v64h64v-64h-64zm64 64v64h64v-64h-64zm-64 64v64h64v-64h-64zm64 64v64h64v-64h-64zm-64 64v64h64v-64h-64zm64 64v64h64v-64h-64zm-64 64v64h64v-64h-64zm0 64v160h128V584H296zm48 48h32v64h-32v-64z"/>
</svg>

Before

Width:  |  Height:  |  Size: 516 B

-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" fill="currentColor" fill-rule="evenodd"><g><path d="M353 16H192.545v425.615c0 105.313-36.166 134.718-99.104 134.718-29.494 0-56.055-5.05-76.718-12.144L0 688.784C29.484 698.924 74.73 705 110.126 705 254.631 705 353 637.16 353 442.658zM702.49 0C547.26 0 449 88.126 449 204.609c0 100.313 75.67 163.12 185.696 203.629 79.577 28.358 111.03 53.695 111.03 95.218 0 45.579-36.358 74.96-105.13 74.96-63.868 0-121.849-21.311-161.146-42.573v-.042L449 662.427C486.361 683.735 556.12 705 631.741 705 813.52 705 898 607.753 898 493.293c0-97.243-54.036-160.035-170.937-204.627-86.47-34.432-122.813-53.669-122.813-97.227 0-34.45 31.446-65.834 96.3-65.834 63.834 0 107.728 21.445 133.307 34.632L872.193 32.05C832.103 14.461 778.109 0 702.49 0" transform="translate(63 160)"/></g></svg>

Before

Width:  |  Height:  |  Size: 833 B

-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" fill="currentColor" fill-rule="evenodd"><path d="M212.68 692.99s-34.325 19.95 24.343 26.6c71.1 8.05 107.351 7 185.632-7.874 0 0 20.665 12.949 49.385 24.149-175.475 75.074-397.184-4.375-259.36-42.875m-21.366-98.173s-38.352 28.35 20.315 34.475c75.83 7.875 135.897 8.4 239.571-11.55 0 0 14.36 14.525 36.952 22.4-212.427 62.123-448.846 5.075-296.838-45.325m180.73-166.422c43.256 49.699-11.384 94.498-11.384 94.498s109.804-56.7 59.368-127.573c-47.11-66.15-83.185-99.049 112.255-212.273.175 0-306.819 76.65-160.24 245.348M604.26 765.439s25.393 20.825-27.846 37.1c-101.397 30.625-421.7 39.9-510.664 1.225-32.048-13.825 28.02-33.25 46.934-37.275 19.613-4.2 30.997-3.5 30.997-3.5-35.551-25.025-229.94 49.175-98.771 70.35 357.605 58.1 652.165-26.075 559.35-67.9M229.142 493.144S66.1 531.818 171.35 545.818c44.482 5.95 133.095 4.55 215.58-2.275 67.423-5.6 135.196-17.85 135.196-17.85s-23.818 10.15-40.98 21.875C315.653 591.143-3.95 570.843 87.99 526.393c77.93-37.45 141.151-33.25 141.151-33.25M521.6 656.416c168.296-87.324 90.365-171.322 36.077-159.948-13.31 2.8-19.264 5.25-19.264 5.25s4.903-7.7 14.36-11.025c107.351-37.8 190.01 111.299-34.675 170.273 0-.175 2.627-2.45 3.502-4.55M420.028 0s93.166 93.1-88.438 236.246c-145.53 114.8-33.274 180.424 0 255.148-84.936-76.65-147.28-144.024-105.425-206.848C287.634 192.672 457.68 147.873 420.028 0m-174.25 893.188c161.466 10.325 409.443-5.775 415.222-82.075 0 0-11.208 28.875-133.445 51.975-137.824 25.9-307.87 22.925-408.567 6.3 0-.175 20.665 16.975 126.79 23.8" transform="translate(182 64)"/></svg>

Before

Width:  |  Height:  |  Size: 1.6 KiB

-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" fill="currentColor" fill-rule="evenodd"><g><path d="M443 678.5c0 15.74 12.76 28.5 28.5 28.5s28.5-12.76 28.5-28.5-12.76-28.5-28.5-28.5-28.5 12.76-28.5 28.5M300 121.5c0 15.74 12.76 28.5 28.5 28.5s28.5-12.76 28.5-28.5S344.24 93 328.5 93 300 105.76 300 121.5" transform="translate(112 112)"/><path d="M709.524 185.714h-95.238V90.476C614.286 40.571 573.714 0 523.81 0H276.19c-49.904 0-90.476 40.571-90.476 90.476v95.238H90.476C40.571 185.714 0 226.286 0 276.19v247.62c0 49.904 40.571 90.476 90.476 90.476h95.238v95.238c0 49.905 40.572 90.476 90.476 90.476h247.62c49.904 0 90.476-40.571 90.476-90.476v-95.238h95.238c49.905 0 90.476-40.572 90.476-90.476V276.19c0-49.904-40.571-90.476-90.476-90.476M90.476 557.143c-18.38 0-33.333-14.953-33.333-33.333V276.19c0-18.38 14.952-33.333 33.333-33.333h278.572c15.81 0 28.571-12.762 28.571-28.571 0-15.81-12.762-28.572-28.571-28.572h-126.19V90.476c0-18.38 14.952-33.333 33.332-33.333h247.62c18.38 0 33.333 14.952 33.333 33.333v256.476c0 13.524-10.953 24.477-24.476 24.477H267.333c-45.047 0-81.619 36.666-81.619 81.619v104.095zm652.381-33.333c0 18.38-14.952 33.333-33.333 33.333H430.952c-15.81 0-28.571 12.762-28.571 28.571 0 15.81 12.762 28.572 28.571 28.572h126.19v95.238c0 18.38-14.952 33.333-33.332 33.333H276.19c-18.38 0-33.333-14.952-33.333-33.333V453.048c0-13.524 10.953-24.477 24.476-24.477h265.334c45.047 0 81.619-36.666 81.619-81.619V242.857h95.238c18.38 0 33.333 14.953 33.333 33.333z" transform="translate(112 112)"/></g></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

+2 -1
View File
@@ -11,7 +11,8 @@
- Meldwork is a local-first desktop application. Do not introduce a required server, container, or cluster dependency unless the task explicitly calls for it.
- Keep Agent execution in the Electron main process. Renderer access must go through narrow, validated preload APIs.
- Never expose executable paths, tokens, provider credentials, or unrestricted shell execution to renderer code.
- Respect native Agent configuration and keep CLI integration provider-agnostic. Do not add gateway-specific adapters, provider overrides, or bundled patches for external tools such as Magpie.
- Never expose executable paths, tokens, credentials, or unrestricted shell execution to renderer code.
- Store secrets only through operating-system-backed secure storage. Do not add plaintext fallbacks.
- Keep workspace write access opt-in. Conversations and groups stay local; do not add remote participants, cloud channels, or remote conversation storage.
+55 -1
View File
@@ -1,5 +1,59 @@
# Changelog
## 1.0.7 - 2026-10-27 (Meldwork-V1.0.7 prerelease)
- Preserve streamed discussion replies across cancellation and reload; scope ACP notifications to the active session.
- Support Hermes native stream JSON and avoid MiMo interactive plan prompts in headless runs.
- Add Zcode with its official logo before Pi Agent, native default configuration, and explicit missing-key errors.
- Let WorkBuddy use an explicitly selected model from its desktop configuration, without exposing credentials to the renderer.
- Keep Chinese slash-separated text intact while retaining credential and local-path redaction.
- Unify application, artifact, README, and website versions at 1.0.7.
- License Meldwork's original source, documentation, and website under Apache-2.0: commercial use is permitted without separate authorization or license fees. Third-party notices remain applicable. Older entries below describe their historical release terms.
- Native acceptance includes a real WorkBuddy response; six-Agent unlimited discussion cancellation and reload were checked with simulated adapters. Zcode's default provider still needs a valid API key; the observed OpenCode router's invalid `tool_calls` finish reason remains an upstream issue.
- Apple silicon DMG and ZIP remain ad-hoc signed and unnotarized.
## 0.1.6 - 2026-10-17 (Meldwork-V1.0.6 prerelease)
### Agent Reliability
- Tier authentication failures instead of treating any authentication-looking diagnostic as a sign-in problem, and retry transient provider errors.
- Keep long-lived ACP Agents (Hermes, Kimi, MiMo, OpenCode) usable across stops, restarts, and hangs, including when a group alternates between read-only and write turns.
- Stop the watchdog from killing Agents that are still thinking or building, and keep an ACP turn alive through traffic this client does not recognise.
- Keep an Agent available through an inconclusive readiness probe and preserve its shell environment.
- Let MiMo and OpenCode turns that call tools finish instead of returning a partial answer, and report a CLI's own error instead of EPIPE.
- Launch WorkBuddy's app-bundled CLI again (it now ships only the lite bundle) and stop skipping every WorkBuddy permission check in write mode.
- Refuse unsafe ACP permission requests during read-only turns.
- Give direct chats the current request and the delivery contract again after a native session is rebuilt.
- Compare installed Agent CLI versions as releases, so a newer installed CLI is not offered a downgrade.
- Write workspace state compactly and durably, and record unhandled main-process faults in the diagnostics log.
- Internal: split the auto-runner and the Agent invocation into modules grouped by purpose.
### Group Conversations
- Show group replies in the order Agents were dispatched.
- Keep natural discussions going instead of ending on one slip, ask Agents to engage with each other, and let peers see workspace file paths.
- Keep group members after an Agent failure, so one proxy 403 or an expired sign-in no longer removes an Agent from later turns, and recover groups that were emptied.
- A member that is not ready no longer blocks a message to the whole group; skipped members are named.
- Deliver attachments and knowledge bases to group Agents, and send Hermes image attachments over ACP.
- Conversations no longer wait for each other, even in the same folder; writers inside one conversation still take turns.
- Import only files an Agent wrote, including paths with spaces.
- Explain run failures in Chinese and English instead of showing raw codes.
### Interface
- Start without waiting for Agent detection: the sidebar, home page, and composer show that detection is running.
- Mark conversations that wait for your decision in the sidebar and list them on the home page; mark group runs that finished while you were in another conversation.
- Add a General settings tab with language and a System, Light, or Dark appearance; System follows the operating system until you choose.
- Explain on each Agent card why an installed Agent is unavailable: its installed version, the verified range, and the failed check.
- Use a shared type scale and semantic status colors, keep secondary text within contrast guidelines, and keep long notices on screen long enough to read.
- Keep unsent text per conversation, add Skip and arrow-key navigation to first-run onboarding, ask before deleting a cloud server connection, and show Gemini CLI's own logo.
### Verification And Distribution
- Frontend 357/357, desktop 1603/1603, and deterministic evaluation 6 cases / 18 results passed on a clean checkout.
- DMG and ZIP are ad-hoc signed, without Apple Developer ID signing or notarization. No installed user application is replaced automatically.
- Live coverage is limited to observed CLI versions and configurations; this release did not repeat the live Agent acceptance run.
## 0.1.5 - 2026-09-09 (Meldwork-V1.0.5 prerelease)
### Local Agent Reliability
@@ -109,7 +163,7 @@
- Sets the desktop package version to `0.1.2` with permanent Bundle ID `com.rydersun.meldwork`.
- Produces Apple silicon DMG and ZIP prerelease artifacts using ad-hoc signing. This is not Apple Developer ID signing or notarization and may require Open Anyway on first launch.
- Keeps `dist:public` as the fail-closed future formal-distribution path requiring a Developer ID identity, Hardened Runtime, complete Apple notarization credentials, and post-sign validation.
- Uses the Meldwork Non-Commercial Source License; commercial use requires prior written permission.
- This historical release used the Meldwork Non-Commercial Source License and required prior written permission for commercial use. Current releases use Apache-2.0 as stated above.
### Verification
+20 -11
View File
@@ -1,14 +1,23 @@
# Commercial Use And Licensing
# Commercial use / 商业使用
Meldwork is licensed under the [Apache License 2.0](LICENSE). Commercial use,
private use, modification, redistribution, and use inside a paid service are
permitted under the license terms.
Meldwork's original source code, documentation, README files, and official website
are licensed under [Apache License 2.0](LICENSE). Commercial use, private use,
modification, redistribution, and inclusion in paid products or services are
permitted under its terms. No separate commercial authorization or license fee
is required.
This page is a practical summary and does not replace the license text. Apache
License 2.0 does not grant trademark rights to the Meldwork name, logo,
screenshots, or other brand assets. Third-party software, Agent products,
models, names, logos, and other materials remain subject to their own licenses
and terms.
When redistributing, preserve the license, applicable copyright and attribution
notices, and NOTICE information, and identify modified files as required by the
license. Apache-2.0 does not grant trademark rights (Section 6).
Before distributing a product that includes third-party materials, review the
relevant notices and preserve the required attribution and license information.
Third-party dependencies, fonts, Agent names, and logos retain their own licenses
and terms; their inclusion does not imply endorsement. In particular, the
website's bundled Fusion Pixel font retains its OFL license and notices.
Meldwork 原创源代码、文档、README 和官网均采用 Apache-2.0。依照许可证条款,
允许商业使用、私人使用、修改、再分发,以及用于收费产品或服务;无需另行取得商业
授权或支付许可费。再分发时应保留许可证、适用的版权与归属声明及 NOTICE 信息,并
按许可证要求标明修改。Apache-2.0 第 6 条不授予商标权。第三方依赖、字体、Agent
名称和标志仍遵循各自条款;官网附带的 Fusion Pixel 字体保留 OFL 许可证与声明。
This summary does not replace LICENSE. 本说明不替代 LICENSE 正文。
+1 -1
View File
@@ -9,7 +9,7 @@ Unless a separate written agreement applies, a contribution accepted into this r
- Keep changes small and tied to a clear product or engineering need.
- Do not submit secrets, tokens, private logs, private prompts, or user data.
- Do not add remote services, telemetry, credential collection, or server dependencies without explicit project approval.
- Preserve the local-first boundary: renderer code must not gain unrestricted filesystem, shell, Provider credential, or executable-path access.
- Preserve the local-first boundary: renderer code must not gain unrestricted filesystem, shell, credential, or executable-path access.
- Include focused tests for behavior, security boundary, storage, or IPC changes.
- Confirm that all submitted code and assets are yours to contribute and do not introduce incompatible license obligations.
+4
View File
@@ -10,3 +10,7 @@ do not imply sponsorship or endorsement.
The OpenCodeReview logo is sourced from Alibaba's OpenCodeReview project:
https://github.com/alibaba/open-code-review/blob/main/imgs/logo-core.svg
The upstream project is licensed under Apache License 2.0.
The Zcode logo is sourced unchanged from the official Zcode website:
https://zcode.z.ai/favicon-192x192.png?v=20260707-transparent
The logo identifies the integration; its trademark rights remain with its owner.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 MiB

+43 -40
View File
@@ -6,37 +6,37 @@
<strong>English</strong> · <a href="README.zh-CN.md">简体中文</a>
</p>
# Meldwork — A local-first workspace for General Agents
# Meldwork — Let Agents collaborate to move tasks forward
**Turn several General Agents into a reviewable working group. Meldwork gives every task a shared context, lets selected Agents investigate independently or discuss across rounds, and keeps the evidence and human decision visible before anything is adopted.**
**Set the goal. Let Agents work together. Meldwork brings your existing Agents into one working group to share context, discuss approaches, carry out work, and check each other's results — with less relaying, prompting, and stitching outputs together.**
Meldwork is a local-first Electron workspace for using the Agent tools you already have on your Mac. It supports focused conversations, independent concurrent responses, and Auto Discussion V4 for proposal, challenge, negotiated work, synthesis, and verification. Agents can work with text, files, images, media, Skills, and selected knowledge sources, so the same workspace can support research, analysis, writing, planning, review, and implementation.
Meldwork is a local-first multi-Agent collaboration workspace for the Agent tools you already use on your Mac. Once you choose the participants, goal, context, and permissions, Agents in Auto Discussion can build on each other's work, request contributions or reviews, and advance the task within the authorized scope. Direct conversations and independent concurrent responses also support research, analysis, writing, planning, review, and implementation.
The product is organized around a durable review record: **Case → Finding → Evidence → Decision → Disposition**. Meldwork keeps the task snapshot, Agent contributions, run state, artifacts, Human Gates, and adoption decision together in one local work cell. Workspace writes are opt-in and remain under the user's control.
You do not need to direct every round, but you still set the goal, grant permissions, and make key decisions. Missing information, permission requests, or tradeoffs may require your input. Workspace writes are opt-in; collaboration history, run state, and deliverables stay local so you can revisit the work and continue it.
<p align="center">
<a href="https://github.com/Ryder-MHumble/Meldwork/releases/download/Meldwork-V1.0.5/Meldwork-0.1.5-arm64.dmg"><strong>Download Meldwork V1.0.5 for Apple silicon macOS</strong></a>
<a href="https://github.com/Ryder-Sun/Meldwork/releases/download/Meldwork-V1.0.9/Meldwork-1.0.9-arm64.dmg"><strong>Download Meldwork V1.0.9 for Apple silicon macOS</strong></a>
· <a href="architecture.md">Architecture</a>
· <a href="LICENSE">License</a>
</p>
## What Meldwork is for
Meldwork is useful when a decision benefits from more than one perspective and a clear record of why a result was adopted. Typical work includes:
Meldwork is useful when several Agents need to advance the same task and you want to spend less time passing context and coordinating next steps. Typical work includes:
- research and information synthesis across local Agent tools and selected knowledge sources;
- product, market, operational, and technical analysis;
- writing, planning, review, and structured document work;
- multimodal work with local documents, images, audio, video, PDFs, and code or configuration files;
- implementation and review tasks where findings, evidence, permissions, and the final adoption decision should remain inspectable.
- implementation in an authorized workspace, with other Agents checking the deliverables and requesting revisions.
## How a task moves through Meldwork
1. **Select** the Agents and participants for this Case.
1. **Select** the Agents that will work on the task together.
2. **Scope** the goal, working directory, context, attachments, Skills, knowledge sources, and permissions.
3. **Run** a direct conversation, Concurrent Responses, or Auto Discussion V4.
4. **Review** the findings, evidence, artifacts, trace, and any Human Gate.
5. **Adopt** the approved result or leave the Case unresolved for further work.
3. **Collaborate** in Auto Discussion: let Agents discuss approaches, carry out authorized work, and check results. Direct conversations and Concurrent Responses are also available.
4. **Decide** when the work needs information, permission, or a tradeoff resolved.
5. **Check the result** and request changes when more work is needed.
## Collaboration modes
@@ -44,21 +44,15 @@ Meldwork is useful when a decision benefits from more than one perspective and a
| --- | --- | --- |
| **Direct** | One selected Agent keeps its conversation and native session when supported. | Focused research, writing, analysis, or execution. |
| **Concurrent Responses** | Selected Agents receive the same frozen task snapshot and return independent responses in stable order. | Comparing perspectives before choosing a path. |
| **Auto Discussion V4** | Agents propose, challenge, negotiate responsibilities, execute agreed work, synthesize a candidate result, and verify it across bounded rounds. Native Agent sessions continue when supported. | Multi-step work that needs discussion, division of responsibility, and review. |
| **Auto Discussion V4** | Agents propose independently, discuss, request peer contributions, carry out authorized work, and check deliverables. A delivery owner judges progress and requests user decisions when needed. Native sessions continue when supported. | Multi-step collaboration with less manual coordination. |
Participants are selected by the user. Automatic selection from a larger roster is outside the current preview.
## The review record
## Revisit the process and continue the work
Meldwork treats collaboration as a decision process rather than a stream of chat messages:
Meldwork keeps task context, Agent replies, run state, and deliverables locally so you can see how work progressed, where it stalled, and what needs attention next. The Run Ledger preserves phase, participant, attempt, artifact, recovery, and Human Gate state. Diagnostic tool output and sensitive runtime details stay behind the Electron main-process boundary.
- **Case** defines the question, scope, context, and permissions.
- **Finding** records an Agent's claim, observation, or proposed action.
- **Evidence** links the finding to a response, artifact, file, knowledge result, or other bounded reference.
- **Decision** records what the evidence supports, what remains uncertain, and which path is selected.
- **Disposition** records whether the result was accepted, revised, rejected, superseded, or left unresolved.
The Run Ledger preserves phase, participant, attempt, receipt, artifact, recovery, and Human Gate state. Diagnostic tool output and sensitive runtime details stay behind the Electron main-process boundary.
These records help you inspect collaboration; they do not establish that every claim has been fact-checked. Peer review does not guarantee correctness, and important deliverables still need acceptance checks appropriate to their use.
## General Agent catalog
@@ -66,20 +60,21 @@ Meldwork detects and invokes compatible locally installed Agent commands. The ca
<table>
<tr>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/codex.svg" alt="Codex logo" width="32" height="32"><br><strong>Codex</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/hermes.png" alt="Hermes logo" width="32" height="32"><br><strong>Hermes</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/openclaw-transparent.png" alt="OpenClaw logo" width="32" height="32"><br><strong>OpenClaw</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/workbuddy.png" alt="WorkBuddy logo" width="32" height="32"><br><strong>WorkBuddy</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/pi.svg" alt="Pi Agent logo" width="32" height="32"><br><strong>Pi Agent</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/kimi.png" alt="Kimi Code logo" width="32" height="32"><br><strong>Kimi Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/codex.svg" alt="Codex logo" width="32" height="32"><br><strong>Codex</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/hermes.png" alt="Hermes logo" width="32" height="32"><br><strong>Hermes</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/openclaw-transparent.png" alt="OpenClaw logo" width="32" height="32"><br><strong>OpenClaw</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/workbuddy.png" alt="WorkBuddy logo" width="32" height="32"><br><strong>WorkBuddy</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/zcode.png" alt="Zcode logo" width="32" height="32"><br><strong>Zcode</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/pi.svg" alt="Pi Agent logo" width="32" height="32"><br><strong>Pi Agent</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/kimi.png" alt="Kimi Code logo" width="32" height="32"><br><strong>Kimi Code</strong></td>
</tr>
<tr>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/mimo.svg" alt="MiMo Code logo" width="32" height="32"><br><strong>MiMo Code</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/claude.png" alt="Claude Code logo" width="32" height="32"><br><strong>Claude Code</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/gemini.svg" alt="Gemini CLI logo" width="32" height="32"><br><strong>Gemini CLI</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/opencode.svg" alt="OpenCode logo" width="32" height="32"><br><strong>OpenCode</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/qwen.svg" alt="Qwen Code logo" width="32" height="32"><br><strong>Qwen Code</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/opencodereview.svg" alt="OpenCodeReview logo" width="32" height="32"><br><strong>OpenCodeReview</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/mimo.svg" alt="MiMo Code logo" width="32" height="32"><br><strong>MiMo Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/claude.png" alt="Claude Code logo" width="32" height="32"><br><strong>Claude Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/gemini.svg" alt="Gemini CLI logo" width="32" height="32"><br><strong>Gemini CLI</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/opencode.svg" alt="OpenCode logo" width="32" height="32"><br><strong>OpenCode</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/qwen.svg" alt="Qwen Code logo" width="32" height="32"><br><strong>Qwen Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/opencodereview.svg" alt="OpenCodeReview logo" width="32" height="32"><br><strong>OpenCodeReview</strong></td>
</tr>
</table>
@@ -89,11 +84,11 @@ Approved Agent Connectors and custom executable Agents use the desktop connector
Meldwork runs as a local Electron application. Conversations, group configuration, run records, and app-owned attachments stay in the local user data directory. The renderer receives only validated snapshots through a narrow preload API; executable paths, credentials, native session references, Skill paths, and unrestricted shell access remain in the main process.
Selected Agents may send prompts, attachments, or Skills to their configured model Provider. Knowledge access is explicit and bounded: the current implementation supports local Obsidian retrieval and CLI-owned Feishu or DingTalk access modes. Local-first describes where Meldwork stores and coordinates work; it does not promise that a configured Agent Provider is offline.
Selected Agents may send prompts, attachments, or Skills to the model provider configured in their own CLI. Knowledge access is explicit and bounded: the current implementation supports local Obsidian retrieval and CLI-owned Feishu or DingTalk access modes. Local-first describes where Meldwork stores and coordinates work; it does not promise that an Agent's model provider is offline.
## Download V1.0.5
## Download V1.0.9
For Apple silicon macOS, download the [Meldwork V1.0.5 prerelease](https://github.com/Ryder-MHumble/Meldwork/releases/tag/Meldwork-V1.0.5) and choose [`Meldwork-0.1.5-arm64.dmg`](https://github.com/Ryder-MHumble/Meldwork/releases/download/Meldwork-V1.0.5/Meldwork-0.1.5-arm64.dmg). The release also includes a ZIP archive and SHA-256 manifest. Install at least one supported local Agent CLI before opening the app.
For Apple silicon macOS, download the [Meldwork V1.0.9 prerelease](https://github.com/Ryder-Sun/Meldwork/releases/tag/Meldwork-V1.0.9) and choose [`Meldwork-1.0.9-arm64.dmg`](https://github.com/Ryder-Sun/Meldwork/releases/download/Meldwork-V1.0.9/Meldwork-1.0.9-arm64.dmg). The release also includes a ZIP archive and SHA-256 manifest. Install at least one supported local Agent CLI before opening the app.
The preview artifacts are ad-hoc signed and not notarized with an Apple Developer ID. macOS may require **Open Anyway** under **System Settings → Privacy & Security** on first launch.
@@ -116,11 +111,19 @@ npm --prefix frontend run build:desktop
npm --prefix desktop test
```
## V1.0.5
## V1.0.9
V1.0.5 improves local Agent readiness and recovery, preserves healthy participants when another Agent fails, keeps completed and stopped work visible after restart, and tightens group execution and output handling. It also updates the workspace preference flow, sidebar and titlebar details, and the Pi capability probe contract.
Auto Discussion can complete native Agent turns on explicit completion and requires each participant to approve the same delivery version. A real six-Agent unlimited discussion completed in nine rounds with matching delivery hashes. This preview also improves Agent authentication status, handles CLI authentication errors reported on stderr, and bundles knowledge-base logos.
Verification for this prerelease includes 343/343 frontend tests, 1,562/1,562 desktop tests, six deterministic evaluation cases with 18 results, web and desktop builds, packaging, ZIP integrity, deep code-signature verification, and packaged macOS acceptance for group execution, cancellation, and restart recovery. The artifacts remain ad-hoc signed and unnotarized; live behavior still depends on the installed Agent CLI version, authentication, Provider, and capabilities.
Agent installation does not guarantee a working model connection: OpenClaw's isolated runtime may not support the native environment configuration, WorkBuddy's CLI may require its own login, and Zcode needs valid native provider credentials. Desktop and frontend regression checks passed 1,700 and 379 tests respectively.
## V1.0.8
V1.0.8 keeps Kimi read-only queries in manual approval mode instead of forcing a planning cycle, preserves MiMo's final text chunks, forwards supported native CLI configuration, and retains the selected Auto Discussion mode after sending. Repetitive native output is cancelled with partial replies preserved. Zcode remains before Pi Agent with its official logo and native default configuration; WorkBuddy can reuse a configured desktop model selected in Meldwork.
Real CLI acceptance covered all 13 installed Agents. A five-Agent unlimited discussion completed all 14 native calls through an isolated Magpie hotfix. The installed Magpie version can rewrite `stop` into `tool_calls` when `tool_calls` is null, causing OpenCode, MiMo, and Hermes to continue indefinitely; this Meldwork release does not install or restart Magpie. OpenClaw, Zcode, Gemini, Qwen, and OpenCodeReview also had native configuration or authentication failures in this audit. Detecting an installed CLI does not certify a working model connection. Regression validation includes 1,655 desktop tests, 370 frontend tests, web and desktop builds, and package validation. Unlimited discussion retains stalled-process detection and write permissions remain opt-in.
Electron acceptance with the same isolated gateway also completed a separate five-Agent unlimited discussion: 19 native calls, no failed attempts or retries, about 12 minutes. Auto Discussion and No round limit remained selected after sending, completion, and leaving and reopening the group. This verifies the workflow, not a response-time guarantee or a repair to the user's installed gateway.
## Documentation
@@ -131,4 +134,4 @@ Verification for this prerelease includes 343/343 frontend tests, 1,562/1,562 de
## License
Meldwork is source-available under the [Meldwork Non-Commercial Source License 1.0](LICENSE). You may inspect, run, modify, and share it for non-commercial purposes. Commercial use requires prior written permission; see [COMMERCIAL_USE.md](COMMERCIAL_USE.md).
Meldwork is open source under the [Apache License 2.0](LICENSE). Commercial use, private use, modification, and redistribution are permitted under the license terms. No separate commercial authorization or license fee is required. Apache-2.0 does not grant trademark rights; see [COMMERCIAL_USE.md](COMMERCIAL_USE.md).
+43 -40
View File
@@ -6,37 +6,37 @@
<a href="README.md">English</a> · <strong>简体中文</strong>
</p>
# Meldwork:本地优先的 General Agent 工作空间
# Meldwork:让 Agent 自主协作推进任务
**把多个 General Agent 组织成一个可复核的工作小组。Meldwork 为每个任务建立共享上下文,让选定的 Agent 独立调查或分轮讨论,并在采用结果前保留完整的证据和人工决策。**
**提出目标,让 Agent 们协作完成。Meldwork 把你已有的 Agent 组织成一个工作小组,共享上下文、讨论方案、协作执行并相互检查,减少你在它们之间转述、催办和整合结果的工作。**
Meldwork 是一个本地优先的 Electron 工作空间,用来连接你已经在电脑上使用的 Agent 工具。它支持直接会话、独立并发回复,以及包含提案、质询、职责协商、分工执行、整合和复核的 Auto Discussion V4。Agent 可以处理文字、文件、图片、音视频、Skill 和选定的知识源,因此同一个工作空间可以覆盖研究、分析、写作、规划、评审和实施。
Meldwork 是一个本地优先的多 Agent 协作工作空间,用来连接你已经在电脑上使用的 Agent 工具。你选定参与者、目标、上下文和权限后,自动讨论中的 Agent 可以接续彼此的工作,请求同伴补充或检查,并在授权范围内推进任务。它也支持单个 Agent 的直接会话和多个 Agent 的独立并发回复,覆盖研究、分析、写作、规划、评审和实施。
产品围绕一条可持续复核的记录组织工作:**Case → Finding → Evidence → Decision → Disposition**。Meldwork 将任务快照、Agent 贡献、运行状态、产物、人工审批门和采用决定保存在同一个本地工作单元中。工作区写入默认受控,并由用户明确授权。
你无需安排每一轮发言,但仍负责目标、权限和关键决策。遇到信息缺失、权限请求或需要取舍的问题时,任务可能等待你处理。工作区写入需要明确授权;协作过程、运行状态和产物保存在本地,方便回看和继续工作。
<p align="center">
<a href="https://github.com/Ryder-MHumble/Meldwork/releases/download/Meldwork-V1.0.5/Meldwork-0.1.5-arm64.dmg"><strong>下载 Meldwork V1.0.5 Apple 芯片 macOS 版</strong></a>
<a href="https://github.com/Ryder-Sun/Meldwork/releases/download/Meldwork-V1.0.9/Meldwork-1.0.9-arm64.dmg"><strong>下载 Meldwork V1.0.9 Apple 芯片 macOS 版</strong></a>
· <a href="architecture.md">架构</a>
· <a href="LICENSE">许可证</a>
</p>
## Meldwork 适合什么工作
当一个决定需要多个视角,并且需要留下清晰的采用依据时,Meldwork 更适合:
当一个任务需要多个 Agent 共同推进,而你不想反复传递上下文、协调下一步时,Meldwork 更适合:
- 研究与信息综合:结合本地 Agent 工具和明确选择的知识源;
- 产品、市场、运营和技术分析;
- 写作、规划、评审和结构化文档工作;
- 处理本地文档、图片、音频、视频、PDF、代码和配置文件的多模态工作;
- 需要保留发现、证据、权限、运行轨迹和最终采用决定的实施工作。
- 在授权工作目录内执行、由其他 Agent 检查成果并继续修改的实施工作。
## 一个任务如何在 Meldwork 中推进
1. **选择**本 Case 的 Agent 和参与者。
1. **选择**一起处理任务的 Agent。
2. **限定**目标、工作目录、上下文、附件、Skill、知识源和权限。
3. **运行**直接会话、并发回复或 Auto Discussion V4。
4. **复核**发现、证据、产物、运行轨迹和人工审批门。
5. **采用**批准的结果,或保留为待解决状态继续处理。
3. **协作**:启动 Auto Discussion,让 Agent 讨论方案、接续执行并检查成果;也可选择直接会话或并发回复。
4. **决策**:在需要补充信息、授权或取舍时介入。
5. **查看结果**:检查交付物,必要时提出修改,让工作继续推进。
## 协作模式
@@ -44,21 +44,15 @@ Meldwork 是一个本地优先的 Electron 工作空间,用来连接你已经
| --- | --- | --- |
| **直接会话** | 一个已选 Agent 在适配器支持时保持对话和原生会话。 | 聚焦研究、写作、分析或执行。 |
| **并发回复** | 已选 Agent 收到同一份冻结任务快照,并按稳定顺序返回独立回复。 | 在选择方案前比较不同视角。 |
| **Auto Discussion V4** | Agent 先提出方案,再进行质询、职责协商、分工执行、候选结果整合和独立复核;支持时会在多轮中保持原生会话。 | 需要讨论、分工和复核的多步骤工作。 |
| **Auto Discussion V4** | Agent 独立提出方案后,接续讨论、请求同伴贡献、在授权范围内执行并检查成果。交付负责人判断任务进度,必要时请求用户决策;支持时保持原生会话。 | 希望减少人工协调的多步骤协作任务。 |
参与者始终由用户选择。从更大候选池自动选人组队不属于当前预览版。
## 复核记录
## 过程可回看,工作可继续
Meldwork 将协作组织成一套决策过程,而不是一串聊天消息:
Meldwork 在本地保留任务上下文、Agent 回复、运行状态和产物,方便了解任务如何推进、哪里遇到阻塞,以及接下来需要处理什么。Run Ledger 保留阶段、参与者、尝试、产物、恢复状态和人工审批门;诊断工具输出与敏感运行信息留在 Electron 主进程边界内。
- **Case**:定义问题、范围、上下文和权限;
- **Finding**:记录 Agent 的判断、观察或行动建议;
- **Evidence**:将 Finding 连接到回复、产物、文件、知识结果或其他受约束的依据;
- **Decision**:记录证据支持的结论、未决问题和选定路径;
- **Disposition**:记录结果已接受、修订、拒绝、被替代或仍未解决。
Run Ledger 会保留阶段、参与者、尝试、回执、产物、恢复状态和人工审批门。诊断工具输出与敏感运行信息留在 Electron 主进程边界内。
过程记录帮助检查协作情况,不代表每条结论都经过事实核验。Agent 的相互检查也不能保证结果正确,重要交付物仍需按实际用途验收。
## General Agent 目录
@@ -66,20 +60,21 @@ Run Ledger 会保留阶段、参与者、尝试、回执、产物、恢复状态
<table>
<tr>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/codex.svg" alt="Codex logo" width="32" height="32"><br><strong>Codex</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/hermes.png" alt="Hermes logo" width="32" height="32"><br><strong>Hermes</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/openclaw-transparent.png" alt="OpenClaw logo" width="32" height="32"><br><strong>OpenClaw</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/workbuddy.png" alt="WorkBuddy logo" width="32" height="32"><br><strong>WorkBuddy</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/pi.svg" alt="Pi Agent logo" width="32" height="32"><br><strong>Pi Agent</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/kimi.png" alt="Kimi Code logo" width="32" height="32"><br><strong>Kimi Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/codex.svg" alt="Codex logo" width="32" height="32"><br><strong>Codex</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/hermes.png" alt="Hermes logo" width="32" height="32"><br><strong>Hermes</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/openclaw-transparent.png" alt="OpenClaw logo" width="32" height="32"><br><strong>OpenClaw</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/workbuddy.png" alt="WorkBuddy logo" width="32" height="32"><br><strong>WorkBuddy</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/zcode.png" alt="Zcode logo" width="32" height="32"><br><strong>Zcode</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/pi.svg" alt="Pi Agent logo" width="32" height="32"><br><strong>Pi Agent</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/kimi.png" alt="Kimi Code logo" width="32" height="32"><br><strong>Kimi Code</strong></td>
</tr>
<tr>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/mimo.svg" alt="MiMo Code logo" width="32" height="32"><br><strong>MiMo Code</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/claude.png" alt="Claude Code logo" width="32" height="32"><br><strong>Claude Code</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/gemini.svg" alt="Gemini CLI logo" width="32" height="32"><br><strong>Gemini CLI</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/opencode.svg" alt="OpenCode logo" width="32" height="32"><br><strong>OpenCode</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/qwen.svg" alt="Qwen Code logo" width="32" height="32"><br><strong>Qwen Code</strong></td>
<td align="center" valign="top" width="16.66%"><img src="frontend/public/agent-logos/opencodereview.svg" alt="OpenCodeReview logo" width="32" height="32"><br><strong>OpenCodeReview</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/mimo.svg" alt="MiMo Code logo" width="32" height="32"><br><strong>MiMo Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/claude.png" alt="Claude Code logo" width="32" height="32"><br><strong>Claude Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/gemini.svg" alt="Gemini CLI logo" width="32" height="32"><br><strong>Gemini CLI</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/opencode.svg" alt="OpenCode logo" width="32" height="32"><br><strong>OpenCode</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/qwen.svg" alt="Qwen Code logo" width="32" height="32"><br><strong>Qwen Code</strong></td>
<td align="center" valign="top"><img src="frontend/public/agent-logos/opencodereview.svg" alt="OpenCodeReview logo" width="32" height="32"><br><strong>OpenCodeReview</strong></td>
</tr>
</table>
@@ -89,11 +84,11 @@ Run Ledger 会保留阶段、参与者、尝试、回执、产物、恢复状态
Meldwork 作为本地 Electron 应用运行。对话、群组配置、运行记录和应用管理的附件保存在本地用户数据目录。渲染进程只通过受约束的 preload API 获取校验后的状态;可执行路径、凭据、原生会话引用、Skill 路径和任意 Shell 权限都留在主进程中。
选定的 Agent 仍可能把 Prompt、附件或 Skill 发送给其配置的模型 Provider。知识访问必须明确选择并受边界约束:当前实现支持本地 Obsidian 检索,以及由本地 CLI 管理的飞书或钉钉访问模式。Local-first 描述的是 Meldwork 的数据和协作位置,不代表已配置的 Agent Provider 一定离线。
选定的 Agent 仍可能把 Prompt、附件或 Skill 发送到其自身 CLI 凭据对应的模型服务商。知识访问必须明确选择并受边界约束:当前实现支持本地 Obsidian 检索,以及由本地 CLI 管理的飞书或钉钉访问模式。Local-first 描述的是 Meldwork 的数据和协作位置,不代表 Agent 使用的模型服务商一定离线。
## 下载 V1.0.5
## 下载 V1.0.9
Apple 芯片 macOS 用户请前往 [Meldwork V1.0.5 预发布页](https://github.com/Ryder-MHumble/Meldwork/releases/tag/Meldwork-V1.0.5),下载 [`Meldwork-0.1.5-arm64.dmg`](https://github.com/Ryder-MHumble/Meldwork/releases/download/Meldwork-V1.0.5/Meldwork-0.1.5-arm64.dmg)。发布页同时提供 ZIP 压缩包和 SHA-256 校验文件。启动应用前,请至少安装一个受支持的本地 Agent CLI。
Apple 芯片 macOS 用户请前往 [Meldwork V1.0.9 预发布页](https://github.com/Ryder-Sun/Meldwork/releases/tag/Meldwork-V1.0.9),下载 [`Meldwork-1.0.9-arm64.dmg`](https://github.com/Ryder-Sun/Meldwork/releases/download/Meldwork-V1.0.9/Meldwork-1.0.9-arm64.dmg)。发布页同时提供 ZIP 压缩包和 SHA-256 校验文件。启动应用前,请至少安装一个受支持的本地 Agent CLI。
该预览版安装包使用 ad-hoc 临时签名,未使用 Apple Developer ID 签名,也未进行公证。macOS 首次启动时可能需要在“系统设置 → 隐私与安全性”中选择“仍要打开 / Open Anyway”。
@@ -116,11 +111,19 @@ npm --prefix frontend run build:desktop
npm --prefix desktop test
```
## V1.0.5
## V1.0.9
V1.0.5 改进了本地 Agent 的就绪检测与恢复,在单个 Agent 失败时保留健康参与者,重启后继续显示已完成和已停止的工作,并加强了群组执行与结果导入。同时更新了工作区偏好、侧栏和标题栏细节,以及 Pi 能力探测契约。
自动讨论可根据原生 Agent 的明确完成信号结束发言,并要求所有参与者认可同一版交付物。一轮真实六 Agent 无限轮讨论在九轮后完成,交付物哈希一致。此预览版还改进了 Agent 认证状态、识别 CLI 在标准错误中报告的认证失败,并将知识库图标打包进应用。
本预览版已完成 343/343 前端测试、1,562/1,562 桌面端测试、6 个确定性评估场景共 18 条结果验证,并通过 Web 与桌面构建、打包、ZIP 完整性、深度代码签名校验,以及 Apple 芯片 macOS 上的群组执行、取消和重启恢复验收。安装包仍为 ad-hoc 临时签名且未公证;实际运行结果仍取决于本机 Agent CLI 版本、鉴权、Provider 和能力。
已安装 Agent 不代表模型连接可用:OpenClaw 的隔离环境可能无法使用现有原生配置,WorkBuddy CLI 可能需要单独登录,Zcode 需要有效的原生提供方凭据。桌面端和前端回归测试分别通过 1,700 项和 379 项。
## V1.0.8
V1.0.8 将 Kimi 只读问答改为手动权限模式,避免强制进入计划流程;修复 MiMo 末尾文本丢失、原生 CLI 配置变量遗漏,以及发送后 Auto Discussion 模式重置。原生输出持续重复时会取消调用并保留已输出内容。Zcode 保持官方标志和 Pi Agent 前的位置,沿用原生默认配置;WorkBuddy 可以复用在 Meldwork 中选定的桌面已配置模型。
本轮对全部 13 个已安装 CLI 进行了真实调用。使用独立 Magpie 热修网关的五 Agent 无限轮讨论中,14 次原生调用全部完成。当前安装的 Magpie 可能把 `tool_calls:null` 误判为真实工具调用,将 `stop` 改写为 `tool_calls`,导致 OpenCode、MiMo 和 Hermes 持续续写;本次 Meldwork 发布不会安装或重启 Magpie。OpenClaw、Zcode、Gemini、Qwen 和 OpenCodeReview 在本机另有原生配置或认证故障,检测到安装不代表模型连接已可用。回归验证包括桌面测试 1,655 项、前端测试 370 项、Web 与桌面构建及打包验证。无限轮讨论保留进程停滞检测,工作区写入仍需明确授权。
Electron 实机界面另完成一轮五 Agent 无限轮讨论:19 次原生调用全部成功,无失败或自动重试,用时约 12 分钟。发送、完成、切回首页再返回群聊后,自动讨论与不限轮数均保持选中。该验收同样使用独立热修网关,证明流程可完成,不构成响应时长保证,也不代表用户现有网关已修复。
## 文档
@@ -131,4 +134,4 @@ V1.0.5 改进了本地 Agent 的就绪检测与恢复,在单个 Agent 失败
## 许可证
Meldwork 采用 [Meldwork 非商用源码许可 1.0](LICENSE)。你可以出于非商业目的查看、运行、修改和分享。商业使用需要事先书面许可,详见 [COMMERCIAL_USE.md](COMMERCIAL_USE.md)。
Meldwork 以 [Apache License 2.0](LICENSE) 开源。在许可证条款范围内,可以商用、私用、修改和再分发。商用无需另行授权或支付许可费。Apache-2.0 不授予商标权;详见 [COMMERCIAL_USE.md](COMMERCIAL_USE.md)。
+3 -3
View File
@@ -1,6 +1,6 @@
# Security Policy
Meldwork is a local-first desktop application that invokes local Agent CLIs, stores local conversations, imports validated image, media, document, code, and archive attachments, and manages Provider credentials through the Electron main process.
Meldwork is a local-first desktop application that invokes local Agent CLIs, stores local conversations, imports validated image, media, document, code, and archive attachments, and runs each Agent with its own native CLI credentials through the Electron main process.
## Reporting
@@ -11,10 +11,10 @@ Do not open a public issue with exploit details, secrets, local file paths, or p
## In Scope
- Renderer-to-main IPC authorization bypasses.
- Exposure of executable paths, attachment paths, native session references, Provider API keys, or Skill file paths to renderer code.
- Exposure of executable paths, attachment paths, native session references, CLI API keys, or Skill file paths to renderer code.
- Arbitrary shell execution, arbitrary file read/write, unsafe navigation, or unsafe installer behavior.
- Attachment validation, private storage, checksum, or preview isolation failures.
- Credential storage or Provider configuration failures that expose secrets.
- Credential storage failures that expose secrets.
## Out of Scope
+57 -17
View File
@@ -11,15 +11,15 @@ The supported product surface is the packaged Electron application. The frontend
| Component | Responsibility | Primary evidence |
| --- | --- | --- |
| Vue renderer | Agent-first multi-session UI, direct-session rename/delete, live direct-chat conclusions and inline execution summaries, group-chat result streaming with an expandable per-Agent/right-side trace panel, per-round navigation, background completion markers, first-run carousel, conversation/Skill/image/knowledge composer UI, bounded attachment-preview cache, theme/i18n, user confirmation and input collection | `frontend/src/App.vue`, `frontend/src/components/RunTracePanel.vue`, `frontend/src/composables/useAttachmentPreviews.js`, `frontend/src/desktop.js`, `frontend/src/knowledgeBaseCatalog.js` |
| Electron preload | Exposes narrow workspace, Human Gate, Cloud control, installer/Skill, attachment, Provider, and knowledge-source methods through `window.meldworkDesktop` only to a local `file:` document; Channel ingress has no renderer API | `desktop/src/preload.cjs` |
| Electron main | Trust boundary for IPC authorization and shutdown gating, persistence, attachment import/preview, Connector registration, Skill and knowledge-source validation, local/Cloud execution, Channel receivers, navigation, sanitized run events, operating-system notifications, Provider storage, and installer control | `desktop/src/main.cjs` |
| Electron preload | Exposes narrow workspace, Human Gate, Cloud control, installer/Skill, attachment, and knowledge-source methods through `window.meldworkDesktop` only to a local `file:` document; Channel ingress has no renderer API | `desktop/src/preload.cjs` |
| Electron main | Trust boundary for IPC authorization and shutdown gating, persistence, attachment import/preview, Connector registration, Skill and knowledge-source validation, local/Cloud execution, Channel receivers, navigation, sanitized run events, operating-system notifications, and installer control | `desktop/src/main.cjs` |
| Local workspace | Persists Meldwork conversations, final replies, compact evidence capsules, safe message metadata, task-scoped group Sessions, conversation-scoped direct Sessions, legacy Session migration, Agent-specific transcript deltas, Agent preferences/runtime evidence, and active run coordination | `desktop/src/workspace/local-workspace.cjs` |
| V4 orchestration | Validates frozen snapshots, phase-aware slots, typed collaboration receipts, negotiated responsibility graphs, work receipts, synthesis recovery, verification, and idempotent commits | `desktop/src/collaboration/orchestration-v4-records.cjs`, `desktop/src/workspace/local-workspace-message-submission.cjs`, `desktop/src/workspace/local-workspace-auto-runner.cjs` |
| V4 orchestration | Validates frozen snapshots, phase-aware slots, typed collaboration receipts, negotiated responsibility graphs, work receipts, synthesis recovery, verification, and idempotent commits | `desktop/src/collaboration/orchestration-v4-records.cjs`, `desktop/src/workspace/local-workspace-message-submission.cjs`, `desktop/src/workspace/local-workspace-auto-runner.cjs` and its `local-workspace-auto-runner-*.cjs` parts (see Code Map) |
| Run Ledger | Atomically checkpoints bounded, sanitized per-run and per-Agent state for stop/crash recovery, marks nonterminal runs and Agent attempts interrupted after restart, supports idempotent reconciliation of missing terminal Agent messages, and purges records with their conversation | `desktop/src/runs/run-ledger.cjs` |
| Context Pack and delivery stores | Capture immutable, content-addressed Task inputs, attempt-specific approved prompts, exact outbound fingerprints, source hashes, target scope, and native Session provenance | `desktop/src/collaboration/context-pack-store.cjs`, `desktop/src/workspace/local-workspace-context-packs.cjs` |
| Outcome store | Persists typed Artifacts, Evidence, Findings, and Adoption records separately from diagnostic reasoning and tool summaries | `desktop/src/collaboration/outcome-store.cjs`, `desktop/src/collaboration/outcome-records.cjs` |
| Human Gate store | Persists permission, budget, and decision requests and their terminal user/system decisions without exposing raw request payloads to the renderer | `desktop/src/gates/human-gate-store.cjs`, `desktop/src/gates/human-gate-coordinator.cjs` |
| Scheduling and failure policy | Enforces run budgets, retry policy, per-Agent controls, and global/workspace scheduling | `desktop/src/runs/run-scheduler.cjs`, `desktop/src/runs/failure-policy.cjs` |
| Scheduling and failure policy | Enforces run budgets, per-Agent controls, and one writer at a time within a conversation (conversations never wait for each other); classifies failures (sign-in, rate limit, network, provider, context overflow) and decides retries with `Retry-After`; stops a silent Agent through the invocation watchdog | `desktop/src/runs/run-scheduler.cjs`, `desktop/src/runs/failure-policy.cjs`, `desktop/src/runs/failure-signals.cjs`, `desktop/src/runs/invocation-watchdog.cjs` |
| Agent Connector registry/runtime | Validates approved manifests and instances, separates CredentialRefs, records Connector/upstream versions, and reduces versioned idempotent Run Events | `desktop/src/agents/connectors/agent-connector-manifest.cjs`, `desktop/src/agents/connectors/agent-connector-registry.cjs`, `desktop/src/agents/connectors/agent-connector-runtime.cjs` |
| Attachment store | Imports validated local images, media, documents, code/configuration files, Office files, and archives into private app-owned storage, verifies metadata/checksums on read, and removes unreferenced entries | `desktop/src/attachments/attachment-store.cjs`, `desktop/src/attachments/image-dimensions.cjs` |
| Local Skill catalog | Scans bounded known roots per Agent and returns sanitized Skill coordinates without exposing paths or contents | `desktop/src/skills/local-skill-catalog.cjs` |
@@ -30,9 +30,53 @@ The supported product surface is the packaged Electron application. The frontend
| Knowledge-source store | Persists only the user-selected Obsidian Vault path; Feishu and DingTalk credentials remain owned by their local CLIs | `desktop/src/knowledge/knowledge-base-store.cjs` |
| CLI adapters | Detect and invoke supported local Agent executables with per-Agent arguments and constrained child environments | `desktop/src/agents/cli/cli-adapters.cjs` |
| Agent installer | Detects Agents and runs allowlisted npm packages or official installer scripts after user confirmation | `desktop/src/agents/installer/agent-installer.cjs` |
| Provider store | Validates Provider metadata and encrypts the API key with Electron `safeStorage` | `desktop/src/providers/provider-store.cjs` |
| Managed OpenClaw runtime | Creates isolated local configuration and a restricted tool policy for OpenClaw | `desktop/src/agents/cli/openclaw-runtime.cjs` |
## Code Map
The workspace is composed of small modules around a thin coordinator. A module either owns one piece of state or is a pure function of its inputs; each has a test file with the same name under `desktop/test/`.
**One Agent invocation** (`desktop/src/workspace/local-workspace-agent-invocation.cjs`) is the run lifecycle: lease, run, race against stop and timeout, record the result. It delegates to:
| Module | Owns |
| --- | --- |
| `invocation-session.cjs` | The native Agent session of one invocation: resume, adopt a new id, discard and rebuild, record the turn |
| `invocation-outputs.cjs` | Workspace snapshot before a writable turn, media import and artifact descriptors after it |
| `invocation-prompt.cjs` | The prompt one Agent receives (frozen, direct chat, group) |
| `invocation-budget.cjs` | Usage recorded against the run budget |
| `invocation-failure.cjs` | Why an invocation ended (stopped, timed out, interrupted, cancelled, failed) and the hidden error metadata |
| `desktop/src/runs/invocation-watchdog.cjs` | Startup, inactivity and stall windows; liveness versus substantive progress |
| `desktop/src/agents/agent-permission-policy.cjs` | Automatic answers to permission requests; a read-only turn only approves side-effect-free tools |
| `desktop/src/collaboration/collaboration-receipt.cjs` | Parsing and normalising the control receipt at the end of a turn (lenient for natural discussion, strict for phased V4) |
| `desktop/src/collaboration/answer-marker-filter.cjs` | Hides control markers while an answer streams |
**Prompts** live in `desktop/src/workspace/prompts/`: `natural-discussion-prompt.cjs` (named sections: brief, writer, owner, routing, receipt), `manual-reply-prompt.cjs` (concurrent replies in a group), and `prompt-contracts.cjs` (the language and deliverable contracts shared by all of them). Direct chats are built by `directPromptFor` in `local-workspace-context.cjs`.
**The automatic run** is `LocalWorkspaceAutoRunner`; its methods are grouped into files by what they do and assembled with `Object.assign` onto the class prototype:
| File | Responsibility |
| --- | --- |
| `local-workspace-auto-runner.cjs` | Constructor, `start` and `resume`, run-level helpers |
| `…-natural-discussion.cjs`, `…-natural-decisions.cjs` | The production path: transcript, prompts, the sequential and agent-led loops, and what happens after each round (owner takeover, ignored routes, peer blockers, missing decisions) |
| `…-v4-discussion.cjs`, `…-v4-phase.cjs`, `…-v4-results.cjs` | The phased V4 run, one phase, and turning results into receipts and commits |
| `…-v4-context.cjs`, `…-v4-delivery.cjs`, `…-v4-synthesis.cjs`, `…-v4-outcomes.cjs` | What a run freezes and shares, how context reaches each Agent, synthesis recovery, and the receipts, review issues, convergence and acceptance checks |
| `…-recovery.cjs` | Retries, pausing a failed Agent for the run, unauthorized-session recovery |
| `…-collaboration.cjs` | Collaboration records written as a run progresses |
| `…-task-graph.cjs`, `…-rounds.cjs` | V3 task-graph and V2 round runs, kept only to resume older runs |
Splitting by file did not change any method; the class still shares one `this`. New behaviour should go into a small module with its own tests and be called from the group that owns the topic, not appended to a large method.
## Reliability Rules
- Conversations are independent. A running chat, direct or group, never makes an Agent in another conversation wait, even when both use the same folder and the same Agent. The writer lease is scoped to the conversation; inside one conversation writers still take turns.
- A failure pauses an Agent for the current run only. Group membership is never rewritten because an Agent failed; a group whose members were all removed by an older build is rebuilt from the Agents that replied in it.
- Only a definitive sign-in failure marks credentials missing. A refusal that could be a proxy or a tool's own output is retried once and reported as a provider failure.
- An Agent that was available stays available through an inconclusive readiness probe for ten minutes. A definitive answer applies immediately.
- A stopped or failed turn keeps a healthy long-lived ACP process and its native session. The process is replaced only when it ignores cancellation or the protocol breaks.
- Unknown ACP traffic (banners, new update kinds, unknown notifications) is ignored; unknown requests are answered with "method not found". Malformed JSON, invalid responses and invalid permission requests still fail closed.
- Messages in a group topic carry a dispatch number (`turnSeq`) assigned before the Agent starts, so live and saved replies share one order. Failure and stop notices take the turn's number.
- A discussion does not end on a malformed receipt, an absent recipient, a peer reporting a blocker or an owner who forgot the decision: those are discarded, handed to the owner, or asked for once.
## Concurrent Collaboration V4
- A group with multiple selected Agents in `manual` mode runs as one concurrent batch. Harness freezes every selected Agent's task snapshot and delivery plan before any Scheduler lease, preserves all selected members, and publishes the batch in stable member order after the barrier. Direct conversations, one-Agent targets, and regeneration keep their existing execution paths.
@@ -45,11 +89,11 @@ Automatic participant selection from a larger roster, production remote Agents,
## Trust Boundaries
1. The renderer is not trusted with Node.js, Agent executable paths, native Agent session IDs or storage paths, attachment paths, Skill paths/contents, raw Provider credentials after saving, arbitrary filesystem reads, or arbitrary process execution. User-selected conversation workdirs and the user-selected Obsidian Vault path are intentional UI-visible paths; pasted image bytes use one exact import payload.
1. The renderer is not trusted with Node.js, Agent executable paths, native Agent session IDs or storage paths, attachment paths, Skill paths/contents, raw CLI credentials, arbitrary filesystem reads, or arbitrary process execution. User-selected conversation workdirs and the user-selected Obsidian Vault path are intentional UI-visible paths; pasted image bytes use one exact import payload.
2. Preload exposes only named IPC methods. Main validates that every request comes from the current main frame and exact bundled frontend file.
3. Main owns local files, OS dialogs, attachment validation/storage, Skill and knowledge-source discovery/revalidation, Connector approval/registration, Agent discovery, child processes, Cloud observation, Channel authentication, installer downloads, and credential encryption.
4. Agent CLIs are external processes. They receive a selected working directory, a constrained environment, permission flags, main-resolved image paths, target-scoped immutable Skill snapshots, and validated knowledge snapshots or read-only hints after capability checks.
5. Provider, Agent Connector, Cloud Connector, Channel Connector, and installer destinations are external trust boundaries. Provider URLs are user supplied and validated; manifests and Connector instances fail closed unless explicitly approved; Channel signatures and Workspace scope are verified before Task delivery.
5. Agent Connector, Cloud Connector, Channel Connector, and installer destinations are external trust boundaries. Manifests and Connector instances fail closed unless explicitly approved; Channel signatures and Workspace scope are verified before Task delivery.
6. Harness events expose only allowlisted status, conclusion deltas, reasoning summaries, plans, tool lifecycle summaries, bounded result metadata, and source message IDs. Raw chain-of-thought, commands, executable paths, native session references, credentials, unrestricted stdout/stderr, and arbitrary tool payloads remain outside the renderer and shared Agent context.
## Local Data
@@ -61,7 +105,6 @@ Automatic participant selection from a larger roster, production remote Agents,
| Immutable Context Packs, delivery records, content blobs, typed outcomes, and Human Gates | Electron user data under `meldwork-private/` | Content-addressed or atomically written main-only records; renderer snapshots expose only bounded IDs, provenance summaries, budgets, and pending Gate choices |
| Cloud operations and Channel Inbox | Electron user data under `meldwork-private/` | Private `0600` stores keep opaque remote references, idempotency state, signed-event-derived Task data, delivery cursors, Task mappings, and outbound audit records; raw signatures, headers, credentials, and complete IM history are not exposed to the renderer |
| Imported attachments | Electron user data under `attachments/` | App-owned copy; `0700` directories, `0600` files, type/MIME/size validation, image-dimension checks where applicable, metadata checksum, ID-only renderer access |
| Provider metadata and encrypted key | Electron user data: `meldwork-provider.json` | API key encrypted with `safeStorage`; file written atomically with mode `0600` |
| Knowledge-source preference | Electron user data: `meldwork-knowledge-base.json` | Stores the selected absolute Obsidian Vault path only; written atomically as a private file |
| Managed OpenClaw state/config | Electron user data under `openclaw-managed/` | Per-scope directories, mode `0700`; config mode `0600`; API key passed by child environment |
| Theme, locale, and onboarding completion | Renderer `localStorage` | Non-secret UI preferences |
@@ -74,7 +117,6 @@ There is no Meldwork-hosted remote data store or application account namespace.
- Agent CLIs may call their own configured model providers.
- Selected attachments and Skill-influenced prompts may be sent by an Agent CLI to its configured model provider after local validation and capability checks.
- Compatible Agents may receive the user-configured Provider URL/model/key from main.
- Feishu and DingTalk readiness probes may invoke their configured local CLIs; a selected Agent may then use those CLI connections with read-only instructions.
- An explicit Obsidian selection is searched and fetched through the main-owned read-only filesystem Connector, then captured as an immutable snapshot or explicit live reference before Agent invocation.
- Approved Agent Connectors may contact only their declared outbound destinations and receive main-owned CredentialRefs rather than renderer-provided secrets.
@@ -86,6 +128,7 @@ There is no Meldwork-hosted remote data store or application account namespace.
## Known Risks And Assumptions
- Writable conversations that share a folder are not coordinated. Two Agents in different conversations can edit the same files at the same time, as two terminals in one directory can. Output capture compares `.meldwork-output/` before and after a run, so when two conversations produce deliverables in the same folder at the same time, one may attach or record a file the other wrote.
- Installer recipes use mutable upstream packages or scripts and currently do not verify a pinned digest or signature (`desktop/src/agents/installer/agent-installer.cjs`).
- Conversation history is local but not application-encrypted (`desktop/src/workspace/local-workspace.cjs`).
- Sanitized Run Ledger checkpoints are local and private but not application-encrypted. The store assumes one Electron main-process writer and recovers only the last persisted bounded checkpoint, not every in-memory event (`desktop/src/runs/run-ledger.cjs`).
@@ -95,7 +138,7 @@ There is no Meldwork-hosted remote data store or application account namespace.
- Obsidian selections use a main-owned read-only Connector with bounded retrieval, immutable snapshots, and citation hash verification. Generic Feishu and DingTalk access remains CLI-owned and prompt-mediated; their authentication and document permissions are not controlled by Meldwork. Notion, Confluence, Google Drive, and SharePoint are reference-only planned sources and are not active Connectors.
- Native image support is currently limited to Codex, Hermes, and OpenCode, with different per-run limits; mixed groups fail before execution when they cannot receive equal image context (`desktop/src/agents/cli/cli-adapters.cjs`).
- Read-only enforcement is owned by the Obsidian Connector for captured selections. Agent workspace and CLI-mediated remote access still depends partly on each upstream CLI honoring its permission flags; Meldwork adds per-CLI hardening where available (`desktop/src/agents/cli/cli-adapters.cjs`).
- Provider requests and native Agent authentication are external dependencies; Meldwork cannot guarantee their availability or data handling.
- Native Agent authentication and model requests are external dependencies; Meldwork cannot guarantee their availability or data handling.
- Agent/Cloud/Channel Connector contracts validate provenance, scope, and idempotency, but the external implementation must honor its CredentialRef, outbound destination, cancellation, and signature-verification obligations. The production build registers no Cloud or Channel Connector by default.
- macOS packaging uses ad-hoc signing for local validation, not a notarized public release (`desktop/scripts/after-pack.cjs`).
@@ -107,10 +150,7 @@ There is no Meldwork-hosted remote data store or application account namespace.
## Related Documents
- [Flows](docs/flows.md)
- [Permissions](docs/permissions.md)
- [Variables And Secrets](docs/variables.md)
- [Test Coverage](docs/tests.md)
- [Automation](docs/automation.md)
- [Public MVP Release Checklist](docs/public-mvp-release.md)
- [macOS Signing And Notarization](docs/macos-signing.md)
- [Contributing](CONTRIBUTING.md)
- [Security Policy](SECURITY.md)
- [Repository Guidelines For Agents](AGENTS.md)
- [Changelog](CHANGELOG.md)
-1
View File
@@ -1 +0,0 @@
{"mode": "chat"}
+18 -20
View File
@@ -13,12 +13,15 @@ API Key 或任意 Shell 执行能力。
- Hermes
- OpenClaw
- WorkBuddy
- Zcode
- Pi Agent
- Kimi Code
- MiMo Code
- Claude Code
- Gemini CLI
- OpenCode
- Qwen Code
- OpenCodeReview
Agent 管理面板提供经过白名单约束的安装入口。安装命令和下载地址由主进程固定,不会根据页面
输入执行任意命令。内置 Agent 通过各自支持的原生权限模式或 Meldwork 托管工具策略执行;无法
@@ -28,7 +31,7 @@ Agent 管理面板提供经过白名单约束的安装入口。安装命令和
## 首次启动与本地 Agent 检测
V1.0.3 GitHub 预发布使用 ad-hoc 临时签名,不使用 Apple Developer ID,也不提交 Apple 公证。
V1.0.7 GitHub 预发布使用 ad-hoc 临时签名,不使用 Apple Developer ID,也不提交 Apple 公证。
如果 macOS 首次启动时拦截候选应用,请打开
“系统设置 → 隐私与安全性”,找到 Meldwork 的拦截提示,点击“仍要打开 / Open Anyway”,
再确认启动。
@@ -66,34 +69,29 @@ Agent 原生支持的图片会按其数量限制直接传递。其他文件,
## Provider 配置
用户可以在客户端内填写一个 OpenAI-compatible Provider:
```text
provider: Provider 显示名称
baseUrl: HTTPS 地址,或本机 loopback HTTP 地址
model: 模型名称
apiKey: API Key
```
API Key 仅通过 Electron `safeStorage` 加密保存到本机;操作系统安全存储不可用时拒绝落盘。
Provider 不设品牌、服务地址或模型默认值,也不会从远端自动获取配置。配置后可供 Hermes、
OpenClaw、WorkBuddy、Qwen 和 OpenCodeReview 使用;其他 Agent 沿用各自的本机鉴权和 Provider 配置。
Agent 使用各自本机的原生鉴权、Provider 和默认模型配置。Meldwork 不要求统一 Provider,
也不会将一个 Agent 的密钥注入其他 Agent。WorkBuddy 桌面账号的加密登录不能直接供独立
CLI 解锁;可在 Meldwork 中选择桌面配置里已有且已配置鉴权的模型。
Meldwork 仅保存模型标识,运行时由主进程读取原生配置,不向渲染层返回密钥。
Zcode 沿用默认配置;若其默认 Provider 缺少 API Key,需要先修复本机原生配置。
## 本地 CLI 支持矩阵
| Agent | 检测命令 | macOS 安装 | Windows 安装 | 用户 Provider |
| --- | --- | --- | --- | --- |
| Codex | `codex` | npm:`@openai/codex@latest` | npm:`@openai/codex@latest` | 不注入;沿用 Codex 本机配置 |
| Hermes | `hermes` | 官方 `install.sh` | 官方 `install.ps1` | 支持 |
| OpenClaw | `openclaw` | npm:`openclaw@latest` | npm:`openclaw@latest` | 支持,使用客户端隔离的托管配置 |
| WorkBuddy | `codebuddy` | npm:`@tencent-ai/[email protected]` | npm:`@tencent-ai/[email protected]` | 实验性支持 |
| Hermes | `hermes` | 官方 `install.sh` | 官方 `install.ps1` | 沿用本机配置 |
| OpenClaw | `openclaw` | npm:`openclaw@latest` | npm:`openclaw@latest` | 沿用本机配置 |
| WorkBuddy | `codebuddy` | npm:`@tencent-ai/[email protected]` | npm:`@tencent-ai/[email protected]` | 可选择桌面已配置模型 |
| Zcode | `zcode` | 官方 CLI 安装 | 官方 CLI 安装 | 沿用默认配置 |
| Pi Agent | `pi` | npm:`@mariozechner/pi-coding-agent` | npm:`@mariozechner/pi-coding-agent` | 沿用本机配置 |
| Kimi Code | `kimi` | 官方 `install.sh` | 官方 `install.ps1` | 不注入;沿用 Kimi 本机配置 |
| MiMo Code | `mimo` | npm:`@mimo-ai/cli@latest` | npm:`@mimo-ai/cli@latest` | 不注入;沿用 MiMo 本机配置,Meldwork 固定使用只读 `plan` Agent |
| MiMo Code | `mimo` | npm:`@mimo-ai/cli@latest` | npm:`@mimo-ai/cli@latest` | 不注入;沿用 MiMo 本机配置,按授权使用 `plan` / `build` 模式 |
| Claude Code | `claude` | npm:`@anthropic-ai/claude-code@latest` | npm:`@anthropic-ai/claude-code@latest` | 不注入;沿用 Claude 本机配置 |
| Gemini CLI | `gemini` | npm:`@google/gemini-cli@latest` | npm:`@google/gemini-cli@latest` | 不注入;沿用 Gemini 本机配置 |
| OpenCode | `opencode` | npm:`opencode-ai@latest` | npm:`opencode-ai@latest` | 不注入;沿用 OpenCode 本机配置 |
| Qwen Code | `qwen` | npm:`@qwen-code/qwen-code@latest` | npm:`@qwen-code/qwen-code@latest` | 支持 |
| OpenCodeReview | `ocr` | npm:`@alibaba-group/open-code-review@latest` | npm:`@alibaba-group/open-code-review@latest` | 支持 |
| Qwen Code | `qwen` | npm:`@qwen-code/qwen-code@latest` | npm:`@qwen-code/qwen-code@latest` | 沿用本机配置 |
| OpenCodeReview | `ocr` | npm:`@alibaba-group/open-code-review@latest` | npm:`@alibaba-group/open-code-review@latest` | 沿用本机配置 |
macOS 会在当前 `PATH` 之外搜索 Volta、pnpm、fnm、asdf、mise、bun、
`~/.local/bin`、`~/.kimi-code/bin`、`~/.mimocode/bin`、Homebrew 等常见目录。Windows 会额外搜索用户 npm、
@@ -137,7 +135,7 @@ npm run dist
npm run dist:public
```
`pack` 用于生成未打包应用并验证 Electron 产物;`dist` 生成 V1.0.3 GitHub 预发布候选使用的 ad-hoc 签名 DMG 和 ZIP。这些产物不具备 Developer ID 信任链且未公证,不能声称通过 Gatekeeper。
`pack` 用于生成未打包应用并验证 Electron 产物;`dist` 生成 V1.0.7 GitHub 预发布候选使用的 ad-hoc 签名 DMG 和 ZIP。这些产物不具备 Developer ID 信任链且未公证,不能声称通过 Gatekeeper。
`dist:public` 是未来正式签名公开发行的专用命令:它要求
Developer ID 证书和完整公证凭据,强制代码签名,在签名后校验永久 Bundle ID、Team ID 与
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "meldwork-desktop",
"version": "0.1.5",
"version": "1.0.9",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "meldwork-desktop",
"version": "0.1.5",
"version": "1.0.9",
"license": "Apache-2.0",
"dependencies": {
"@agentclientprotocol/sdk": "0.23.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "meldwork-desktop",
"version": "0.1.5",
"version": "1.0.9",
"private": true,
"description": "A local-first desktop workspace for multi-Agent collaboration",
"author": "Ryder-MHumble",
+1 -1
View File
@@ -5,7 +5,7 @@ const TEST_SUITES = Object.freeze({
agents: ['agents'],
attachments: ['attachments'],
collaboration: ['collaboration'],
core: ['channels', 'knowledge', 'media', 'providers', 'skills'],
core: ['channels', 'knowledge', 'media', 'skills'],
runs: ['runs'],
security: ['security', 'shell'],
workspace: ['workspace', 'gates'],
@@ -5,6 +5,7 @@ const AGENT_VERSION_IDENTITIES = Object.freeze({
hermes: [/\bhermes(?:\s+agent)?\b/i],
openclaw: [/\bopenclaw\b/i],
workbuddy: [/\b(?:workbuddy|codebuddy)\b/i],
zcode: [/\bzcode\b/i],
pi: [/\bpi(?:\s+agent)?\b/i],
kimi: [/\bkimi(?:\s+code)?\b/i],
mimo: [/\bmimo(?:\s+code)?\b/i, /\bmimocode\b/i],
@@ -19,6 +20,7 @@ const AGENT_EXECUTABLE_NAMES = Object.freeze({
hermes: ['hermes'],
openclaw: ['openclaw'],
workbuddy: ['codebuddy'],
zcode: ['zcode'],
pi: ['pi', 'pi-agent', 'piagent'],
kimi: ['kimi'],
mimo: ['mimo'],
@@ -56,6 +58,9 @@ const AGENT_COMPATIBILITY = Object.freeze({
'json', 'plan', 'acceptEdits',
]),
]),
zcode: exactProfile('0.16.1', [
probe('zcode-prompt', ['--help'], ['--prompt', '--json', '--mode', '--resume', 'plan', 'edit']),
]),
pi: exactProfile('0.84.2', [
probe('pi-cli', ['--help'], [
'--mode', '--print', '--session', '--tools', '--approve', '--no-approve', 'json',
@@ -265,6 +270,7 @@ module.exports = {
AGENT_COMPATIBILITY,
assessAgentVersion,
capabilityProbes,
compareReleaseVersions,
extractAgentVersion,
identifyAgentVersion,
}
+6 -1
View File
@@ -442,8 +442,13 @@ function importAgentOutputs(input, attachmentStore) {
const realPath = fs.realpathSync(reportedPath)
const stat = fs.statSync(realPath)
const extension = path.extname(realPath).toLowerCase()
// A path the Agent merely mentioned (a file it read, the user's input,
// a staged attachment) is not an output. Only files written during this
// run outside the input staging folder are imported.
if (!stat.isFile() || !SUPPORTED_EXTENSIONS.has(extension)
|| stat.size > 64 * 1024 * 1024 || !isInside(workdirRealPath, realPath)) continue
|| stat.size > 64 * 1024 * 1024 || !isInside(workdirRealPath, realPath)
|| realPath.split(path.sep).includes('.meldwork-input')
|| stat.mtimeMs < startedAt - 2000) continue
reportedCandidates.push({
name: path.basename(realPath), path: realPath, size: stat.size,
mtimeMs: stat.mtimeMs, ctimeMs: stat.ctimeMs,
@@ -0,0 +1,85 @@
const { createHash } = require('node:crypto')
const { canonicalJson } = require('../collaboration/context-pack-records.cjs')
function sha256(value) {
return createHash('sha256').update(String(value || '')).digest('hex')
}
// Tool kinds an Agent may use without approval during a read-only turn.
const READ_ONLY_SAFE_TOOL_KINDS = new Set(['read', 'search', 'fetch', 'think'])
function terminalPlanRejection(options = []) {
// Kimi's first reject option is "Revise", which loops without feedback.
return options.find(option => option?.optionId === 'plan_reject_and_exit'
&& ['reject_once', 'reject_always'].includes(option.kind))
}
// Automatic answer to an Agent's permission request when the run is not
// waiting for a person. Returns the response to send, `null` when the request
// is refused but the Agent offered nothing to refuse with, or `undefined`
// when no automatic answer exists and the caller must ask the user.
//
// A read-only turn stays read-only even when the Agent asks: only
// side-effect-free tools are approved, while edits, deletes, moves, commands
// and mode switches are refused.
function automaticPermissionResponse(request, { allowWrite }) {
const options = Array.isArray(request?.options) ? request.options : []
if (!allowWrite && !READ_ONLY_SAFE_TOOL_KINDS.has(request?.toolCall?.kind)) {
const rejection = terminalPlanRejection(options)
|| options.find(candidate => candidate?.kind === 'reject_once')
|| options.find(candidate => candidate?.kind === 'reject_always')
return rejection?.optionId ? { status: 'rejected', optionId: rejection.optionId } : null
}
const option = options.find(candidate => candidate?.kind === 'allow_once')
|| options.find(candidate => candidate?.kind === 'allow_always')
|| options[0]
return option?.optionId ? { status: 'approved', optionId: option.optionId } : undefined
}
// A person's decision applies only to the exact request it was made for.
function matchesResumedPermission(request, resumedGate) {
if (!request || !resumedGate?.request) return false
try {
if (canonicalJson(request) !== canonicalJson(resumedGate.request)) return false
} catch {
return false
}
const selected = request.options?.find(option => option.optionId === resumedGate.optionId)
if (!selected) return false
return resumedGate.status === 'approved'
? selected.kind.startsWith('allow_')
: selected.kind.startsWith('reject_')
}
function permissionContinuationRequestId(requestHash, binding) {
return sha256(canonicalJson({
requestHash,
sessionRefHash: binding.sessionRefHash,
sessionProvenanceHash: binding.sessionProvenanceHash,
}))
}
// A resumed permission decision must belong to the native session the
// request came from; anything else is refused before the Agent runs.
function assertResumedPermissionBinding(resumed, currentBinding, sessionRef) {
const requestHash = sha256(canonicalJson(resumed.request))
const persistedBinding = {
sessionRefHash: resumed.sessionRefHash,
sessionProvenanceHash: resumed.sessionProvenanceHash,
}
if (resumed.requestId !== permissionContinuationRequestId(requestHash, persistedBinding)
|| resumed.requestHash !== requestHash
|| resumed.sessionProvenanceHash !== currentBinding.sessionProvenanceHash
|| (sessionRef && resumed.sessionRefHash !== currentBinding.sessionRefHash)) {
throw new Error('LOCAL_RUN_PERMISSION_REQUEST_MISMATCH')
}
}
module.exports = {
terminalPlanRejection,
READ_ONLY_SAFE_TOOL_KINDS,
assertResumedPermissionBinding,
automaticPermissionResponse,
matchesResumedPermission,
permissionContinuationRequestId,
}
+38 -3
View File
@@ -1,3 +1,4 @@
const { authFailureCertainty } = require('../runs/failure-signals.cjs')
const AGENT_OUTCOMES = Object.freeze([
'accepted',
'running',
@@ -79,6 +80,10 @@ const AGENT_RUNTIME_CAPABILITIES = Object.freeze({
domains: ['general', 'software-development', 'document-production'],
toolClasses: ['filesystem', 'shell'],
}),
zcode: generalProfile({
domains: ['general', 'software-development', 'research'],
toolClasses: ['filesystem', 'shell'],
}),
pi: generalProfile({
domains: ['general', 'software-development'],
toolClasses: ['filesystem', 'shell'],
@@ -219,10 +224,25 @@ function terminalAuthenticationDiagnostic(value) {
: ''
}
// True only for explicit sign-in or API-key statements. Ambiguous refusals
// (HTTP 403, proxy or region blocks, "provider rejected") are classified as
// LOCAL_AGENT_ACCESS_DENIED so they are retried instead of signing the Agent out.
function authConfigurationFailure(detail) {
return authFailureCertainty(detail) === 'definitive'
}
function authFailureCode(detail) {
const certainty = authFailureCertainty(detail)
if (certainty === 'definitive') return 'LOCAL_AGENT_AUTH_REQUIRED'
if (certainty === 'ambiguous') return 'LOCAL_AGENT_ACCESS_DENIED'
return ''
}
function failureCategory(code) {
const value = String(code || '')
if (value === 'LOCAL_AGENT_SESSION_INVALID') return 'session'
if (value === 'LOCAL_AGENT_AUTH_REQUIRED') return 'authentication'
if (value === 'LOCAL_AGENT_AUTH_REQUIRED' || value === 'LOCAL_WORKBUDDY_PROVIDER_AUTH') return 'authentication'
if (value === 'LOCAL_AGENT_ACCESS_DENIED') return 'provider'
if (value === 'LOCAL_AGENT_TIMEOUT') return 'timeout'
if (value === 'LOCAL_AGENT_EXECUTION_STOPPED') return 'cancellation'
if (value === 'LOCAL_AGENT_REFUSED') return 'refusal'
@@ -299,9 +319,19 @@ function requireTerminalAgentResult(result) {
throw agentRuntimeError('LOCAL_AGENT_EXECUTION_STOPPED')
}
if (normalized.outcome === 'failed') {
const failure = normalized.failure?.code
let failure = normalized.failure?.code
? normalized.failure
: agentRuntimeFailure('LOCAL_AGENT_PROCESS_FAILED')
const diagnostic = String(normalized.diagnostic || normalized.text || '')
// A structured failure whose diagnostic names an authentication problem is
// an authentication failure even when the CLI reported a generic process
// error, so the catalog can mark the Agent as needing a fresh login.
if (failure.category !== 'authentication' && authConfigurationFailure(diagnostic)) {
failure = agentRuntimeFailure('LOCAL_AGENT_AUTH_REQUIRED')
} else if (failure.code === 'LOCAL_AGENT_PROCESS_FAILED'
&& authFailureCertainty(normalized.diagnostic) === 'ambiguous') {
failure = agentRuntimeFailure('LOCAL_AGENT_ACCESS_DENIED')
}
throw agentRuntimeError(
failure.code,
normalized.diagnostic || normalized.text,
@@ -310,7 +340,10 @@ function requireTerminalAgentResult(result) {
}
const authenticationDiagnostic = terminalAuthenticationDiagnostic(normalized.text)
if (authenticationDiagnostic) {
throw agentRuntimeError('LOCAL_AGENT_AUTH_REQUIRED', authenticationDiagnostic)
throw agentRuntimeError(
authFailureCode(authenticationDiagnostic) || 'LOCAL_AGENT_ACCESS_DENIED',
authenticationDiagnostic,
)
}
return normalized
}
@@ -338,6 +371,8 @@ module.exports = {
agentRuntimeCapabilities,
agentRuntimeError,
agentRuntimeFailure,
authConfigurationFailure,
authFailureCode,
isCodeReviewAgentKind,
isReviewOnlyAgentKind,
normalizeAgentResult,
+222 -18
View File
@@ -1,5 +1,7 @@
const { spawn } = require('node:child_process')
const { createHash } = require('node:crypto')
const fs = require('node:fs')
const path = require('node:path')
const { PassThrough, Readable, Writable } = require('node:stream')
const { AGENT_PROFILES, prepareCommand } = require('./cli-discovery.cjs')
const {
@@ -21,10 +23,31 @@ const {
} = require('./cli-process-support.cjs')
const { createAcpOutboundPayload } = require('../../collaboration/outbound-payload.cjs')
const { redactSecrets } = require('../../security/secret-redaction.cjs')
const { createOutputRepetitionGuard } = require('./cli-output-repetition.cjs')
const { createTurnCompletion } = require('./cli-turn-completion.cjs')
const { terminalPlanRejection } = require('../agent-permission-policy.cjs')
const ACP_CANCEL_GRACE_MS = 250
const ACP_MAX_LINE_BYTES = 1024 * 1024
// After a stop or timeout the Agent gets this long to answer the cancelled
// prompt. A clean cancellation keeps the long-lived process and its native
// session; only an Agent that ignores cancel is torn down.
const ACP_CANCEL_SETTLE_MS = 3000
// MiMo can flush the last delta of a message just after its prompt response.
const MIMO_TERMINAL_DRAIN_MS = 50
const ACP_INITIALIZE_TIMEOUT_MS = 60 * 1000
const ACP_STDERR_TAIL_BYTES = 64 * 1024
// Process identity must not change when only volatile shell values change.
// Version managers such as fnm put a per-shell directory on PATH, so a fresh
// login-shell capture used to restart the ACP process and drop its session.
const ACP_VOLATILE_ENV_KEYS = new Set([
'PATH', 'Path', 'TERM', 'TERM_PROGRAM', 'TERM_PROGRAM_VERSION', 'TERM_SESSION_ID',
'SHLVL', 'PWD', 'OLDPWD', '_', 'TMPDIR', 'COLUMNS', 'LINES', 'SHELL',
'FNM_MULTISHELL_PATH', 'FNM_DIR', 'FNM_VERSION_FILE_STRATEGY', 'FNM_LOGLEVEL',
'FNM_NODE_DIST_MIRROR', 'FNM_ARCH', 'FNM_COREPACK_ENABLED', 'FNM_RESOLVE_ENGINES',
])
const ACP_MAX_LINE_BYTES = 4 * 1024 * 1024
const ACP_MAX_INPUT_BYTES = 16 * 1024 * 1024
const ACP_UNKNOWN_METHOD_NAME_LIMIT = 120
const ACP_MAX_REPLY_BYTES = 10 * 1024 * 1024
const ACP_PERSISTENT_RUNTIME_LIMIT = 16
const ACP_PERMISSION_OPTION_LIMIT = 16
@@ -88,6 +111,10 @@ function acpTransportError(diagnostic) {
return agentExecutionError('LOCAL_AGENT_PROCESS_FAILED', diagnostic)
}
// Returns the message to deliver, or null for traffic that is safe to ignore.
// A newer Agent can send updates and notifications this client does not know;
// failing the whole turn over them would break Meldwork on every Agent upgrade,
// so only messages that decide what is executed or approved stay strict.
function validateAcpInboundMessage(message, validators, replyState, protocolLabel) {
if (!message || typeof message !== 'object' || Array.isArray(message)
|| message.jsonrpc !== '2.0') {
@@ -99,7 +126,8 @@ function validateAcpInboundMessage(message, validators, replyState, protocolLabe
if (message.method === 'session/update' && !hasId) {
const parsed = validators.zSessionNotification.safeParse(message.params)
if (!parsed.success) {
throw acpTransportError(`${protocolLabel} returned invalid session update parameters.`)
replyState.ignoredMessages = (replyState.ignoredMessages || 0) + 1
return null
}
const update = parsed.data.update
if (replyState.collecting !== false
@@ -119,7 +147,23 @@ function validateAcpInboundMessage(message, validators, replyState, protocolLabe
}
return message
}
throw acpTransportError(`${protocolLabel} requested an unsupported client method.`)
// An unknown request still gets the connection's "method not found"
// answer, so the Agent is not left waiting. The SDK logs every request it
// rejects together with its parameters, so only the method name is
// forwarded. An unknown notification needs no answer and is dropped
// before it can reach the SDK's logging.
const validId = typeof message.id === 'string'
|| (Number.isInteger(message.id) && message.id >= 0)
if (hasId && validId) {
return {
jsonrpc: '2.0',
id: message.id,
method: message.method.slice(0, ACP_UNKNOWN_METHOD_NAME_LIMIT),
params: {},
}
}
replyState.ignoredMessages = (replyState.ignoredMessages || 0) + 1
return null
}
if (!Number.isInteger(message.id) || message.id < 0) {
@@ -215,6 +259,12 @@ function boundedAcpStream(
const parseLine = (line) => {
const text = line.toString('utf8').trim()
if (!text) return
// Some CLIs print banners or warnings on stdout next to the protocol.
// Anything that is not an object is not protocol traffic.
if (!text.startsWith('{')) {
replyState.ignoredMessages = (replyState.ignoredMessages || 0) + 1
return
}
let message
try {
message = JSON.parse(text)
@@ -224,6 +274,7 @@ function boundedAcpStream(
const validated = validateAcpInboundMessage(
message, validators, replyState, protocolLabel,
)
if (!validated) return
if (message.method === 'session/request_permission') {
replyState.pendingPermissionRequestIds.add(message.id)
} else if (message.method === 'session/update' && message.params?.update) {
@@ -309,7 +360,7 @@ function boundedAcpStream(
}
function permissionRejection(options) {
return (options || []).find(option => (
return terminalPlanRejection(options || []) || (options || []).find(option => (
['reject_once', 'reject_always'].includes(option.kind)
|| /reject|deny/i.test(`${option.optionId || ''} ${option.name || ''}`)
))
@@ -529,6 +580,32 @@ function persistenceKey(agent, options) {
return `${agent.kind}:${value}`
}
const ACP_IMAGE_MIME_TYPES = new Map([
['.png', 'image/png'], ['.jpg', 'image/jpeg'], ['.jpeg', 'image/jpeg'],
['.gif', 'image/gif'], ['.webp', 'image/webp'],
])
const ACP_MAX_IMAGE_BYTES = 10 * 1024 * 1024
// Images travel as ACP content blocks only when the Agent advertises image
// prompts during initialize. Otherwise the turn fails before prompting so the
// adapter can fall back to a transport that accepts image arguments.
function acpImageBlocks(runtime, imageAttachments) {
const images = Array.isArray(imageAttachments) ? imageAttachments : []
if (!images.length) return []
if (runtime.agentCapabilities?.promptCapabilities?.image !== true) {
throw allowAcpSetupFallback(agentExecutionError('LOCAL_AGENT_IMAGE_UNSUPPORTED'))
}
return images.map((filename) => {
const mimeType = ACP_IMAGE_MIME_TYPES.get(path.extname(filename).toLowerCase())
if (!mimeType) throw agentExecutionError('LOCAL_AGENT_IMAGE_UNSUPPORTED')
const stat = fs.statSync(filename)
if (!stat.isFile() || stat.size > ACP_MAX_IMAGE_BYTES) {
throw agentExecutionError('LOCAL_AGENT_IMAGE_LIMIT')
}
return { type: 'image', mimeType, data: fs.readFileSync(filename).toString('base64') }
})
}
function withAcpRuntimeLock(key, callback) {
const previous = acpRuntimeLocks.get(key) || Promise.resolve()
const current = previous.catch(() => {}).then(callback)
@@ -540,14 +617,19 @@ function withAcpRuntimeLock(key, callback) {
}
function runtimeSignature(agent, workdir, spec, profile, childEnv) {
const environment = Object.keys(childEnv).sort().map(key => [key, childEnv[key]])
const environment = Object.keys(childEnv).sort()
.filter(key => !ACP_VOLATILE_ENV_KEYS.has(key))
.map(key => [key, childEnv[key]])
return createHash('sha256').update(JSON.stringify({
kind: agent.kind,
executable: agent.executable,
workdir,
command: spec.command,
args: spec.args,
acpMode: spec.acpMode || '',
// The native session mode is applied on every turn
// with session/set_mode, so it does not identify a process. Anything that
// really needs a separate process (a permission policy in the environment)
// is already part of the environment below.
profileId: profile.profileId,
environment,
})).digest('hex')
@@ -653,8 +735,14 @@ async function createAcpRuntime(input) {
})
child.stderr.on('data', (chunk) => {
try { runtime.activeTurn?.noteActivity?.() } catch { /* activity is best-effort */ }
// Keep only recent stderr. A long-lived process used to keep the first
// megabyte of its whole lifetime, so a new failure was classified from
// stale early lines (for example an old auth warning).
runtime.stderr.push(chunk)
runtime.stderrBytes += chunk.length
if (runtime.stderrBytes <= 1024 * 1024) runtime.stderr.push(chunk)
while (runtime.stderrBytes > ACP_STDERR_TAIL_BYTES && runtime.stderr.length > 1) {
runtime.stderrBytes -= runtime.stderr.shift().length
}
})
runtime.observedStdout = createObservedAcpInput(
child.stdout, runtime.replyState, protocolLabel,
@@ -663,6 +751,11 @@ async function createAcpRuntime(input) {
const client = {
async requestPermission(params) {
const turn = runtime.activeTurn
if (turn?.completion?.completed) return { outcome: { outcome: 'cancelled' } }
if (runtime.agent.kind === 'mimo' && runtime.replyState.promptTerminalSequence != null) {
runtime.replyState.permissionPendingAtPromptTerminal = true
return { outcome: { outcome: 'cancelled' } }
}
const pending = permissionOutcome(params, turn?.options || {}, childEnv)
turn?.pendingPermissionCallbacks.add(pending)
pending.then(
@@ -676,11 +769,35 @@ async function createAcpRuntime(input) {
try { turn?.noteActivity?.() } catch { /* activity is best-effort */ }
const sequence = runtime.replyState.pendingUpdateSequences.shift()
if (!turn || !runtime.replyState.collecting) return
if (params.sessionId !== runtime.protocolSessionRef) return
if (runtime.replyState.promptTerminalSequence != null
&& Number.isInteger(sequence)
&& sequence > runtime.replyState.promptTerminalSequence) return
turn.structuredOutput.ingest?.(params)
for (const event of turn.profile.mapEvent(params.update, turn.runtimeState)) {
&& sequence > runtime.replyState.promptTerminalSequence
&& !(runtime.agent.kind === 'mimo'
&& params.update?.sessionUpdate === 'agent_message_chunk'
&& turn.lastMessageId
&& params.update.messageId === turn.lastMessageId)) return
if (params.update?.sessionUpdate === 'agent_message_chunk' && params.update.messageId) {
turn.lastMessageId = params.update.messageId
}
let update = params.update
if (['tool_call', 'tool_call_update'].includes(update?.sessionUpdate)) {
if (['completed', 'failed'].includes(update.status)) turn.openTools.delete(update.toolCallId)
else if (update.status || update.sessionUpdate === 'tool_call') turn.openTools.add(update.toolCallId)
}
if (turn.completion && update?.sessionUpdate === 'agent_message_chunk'
&& update.content?.type === 'text') {
const next = turn.completion.push(update.content.text,
turn.openTools.size === 0 && turn.pendingPermissionCallbacks.size === 0)
update = { ...update, content: { ...update.content, text: next.delta } }
if (next.completed) turn.finishContribution()
}
if (turn.repetitionGuard?.(update)) {
turn.stopRepeatedOutput()
return
}
turn.structuredOutput.ingest?.({ ...params, update })
for (const event of turn.profile.mapEvent(update, turn.runtimeState)) {
turn.runtimeEvents.emit(event)
}
},
@@ -713,15 +830,42 @@ async function createAcpRuntime(input) {
)
runtime.connection = new ClientSideConnection(() => client, stream)
acpInitializingRuntimes.add(runtime)
const abortSignal = input.options?.signal
const initializeTimeoutMs = Number.isFinite(input.options?.acpInitializeTimeoutMs)
&& input.options.acpInitializeTimeoutMs > 0
? input.options.acpInitializeTimeoutMs
: ACP_INITIALIZE_TIMEOUT_MS
let initializeTimer = null
let abortListener = null
try {
await Promise.race([
// initialize used to wait forever while holding the per-conversation
// lock, leaving the Agent unusable in that conversation until restart.
const initialized = await Promise.race([
runtime.connection.initialize({ protocolVersion: PROTOCOL_VERSION, clientCapabilities: {} }),
runtime.failurePromise,
new Promise((_, reject) => {
initializeTimer = setTimeout(() => reject(agentExecutionError(
'LOCAL_AGENT_PROTOCOL_UNAVAILABLE',
`${protocolLabel} did not answer the ACP initialize request.`,
)), initializeTimeoutMs)
}),
new Promise((_, reject) => {
abortListener = () => reject(agentExecutionError('LOCAL_AGENT_EXECUTION_STOPPED'))
if (abortSignal?.aborted) abortListener()
else abortSignal?.addEventListener('abort', abortListener, { once: true })
}),
])
runtime.agentCapabilities = initialized?.agentCapabilities
&& typeof initialized.agentCapabilities === 'object'
? initialized.agentCapabilities
: {}
} catch (error) {
await closeAcpRuntime(runtime)
if (abortSignal?.aborted) throw agentExecutionError('LOCAL_AGENT_EXECUTION_STOPPED')
throw allowAcpSetupFallback(acpProtocolError(error, childEnv))
} finally {
clearTimeout(initializeTimer)
if (abortListener) abortSignal?.removeEventListener('abort', abortListener)
acpInitializingRuntimes.delete(runtime)
}
return runtime
@@ -753,6 +897,8 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
try { options.onActivity?.() } catch { /* activity is best-effort */ }
}
const runtimeEvents = createRuntimeEventEmitter({ ...options, onActivity: noteActivity }, runtime.childEnv)
const completion = options.turnCompletion === true ? createTurnCompletion() : null
if (completion) prompt = `${prompt}\n\n${completion.instruction}`
const capabilityWarning = connectorLimitedRuntimeEvent(runtime.agent.kind, profile)
if (capabilityWarning) runtimeEvents.emit(capabilityWarning)
const turn = {
@@ -767,8 +913,13 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
noteActivity,
outboundPayloadFailed: false,
pendingPermissionCallbacks: new Set(),
completion,
openTools: new Set(),
repetitionGuard: ['opencode', 'mimo', 'hermes'].includes(runtime.agent.kind) ? createOutputRepetitionGuard() : null,
}
runtime.activeTurn = turn
runtime.stderr = []
runtime.stderrBytes = 0
runtime.replyState.bytes = 0
runtime.replyState.inputBytes = 0
runtime.replyState.observedInputBytes = 0
@@ -785,12 +936,16 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
runtime.publicSessionRef = visibleSessionRef
}
let promptStarted = false
let promptRequest = null
let caughtError = null
let abortRequested = false
let timeoutRequested = false
let cancelPromise = Promise.resolve()
let rejectAbort
let resolveContribution
const contribution = new Promise(resolve => { resolveContribution = resolve })
const abortPromise = new Promise((_, reject) => { rejectAbort = reject })
const abort = () => {
const abort = (error = agentExecutionError('LOCAL_AGENT_EXECUTION_STOPPED')) => {
if (abortRequested) return
abortRequested = true
if (protocolSessionRef) {
@@ -800,7 +955,16 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
})).catch(() => {})
} catch { /* the ACP stream has already closed */ }
}
rejectAbort(agentExecutionError('LOCAL_AGENT_EXECUTION_STOPPED'))
rejectAbort(error instanceof Error ? error : agentExecutionError('LOCAL_AGENT_EXECUTION_STOPPED'))
}
turn.stopRepeatedOutput = () => {
runtime.replyState.collecting = false
abort(agentExecutionError('LOCAL_AGENT_REPEATED_OUTPUT',
'The Agent repeated the same response without tool progress; the native turn was cancelled.'))
}
turn.finishContribution = () => {
runtime.replyState.collecting = false
resolveContribution({ stopReason: 'end_turn' })
}
const operation = promise => Promise.race([promise, abortPromise, runtime.failurePromise])
const timeout = setTimeout(() => {
@@ -844,14 +1008,28 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
if (visibleSessionRef && typeof options.onSessionRef === 'function') {
await operation(options.onSessionRef(visibleSessionRef, { transport: 'acp' }))
}
const imageBlocks = acpImageBlocks(runtime, spec.imageAttachments)
runtime.replyState.collecting = true
let promptResult
try {
promptStarted = true
promptResult = await operation(runtime.connection.prompt({
promptRequest = Promise.resolve(runtime.connection.prompt({
sessionId: protocolSessionRef,
prompt: [{ type: 'text', text: prompt }],
prompt: [{ type: 'text', text: prompt }, ...imageBlocks],
}))
promptRequest.catch(() => {})
promptResult = await operation(Promise.race([promptRequest, contribution]))
if (completion?.completed) {
await operation(settleWithin(Promise.resolve(runtime.connection.cancel({ sessionId: protocolSessionRef }))
.catch(() => {}), ACP_CANCEL_GRACE_MS))
let settled = false
await operation(settleWithin(promptRequest.then(() => { settled = true }, () => { settled = true }),
ACP_CANCEL_SETTLE_MS))
if (!settled) runtime.contributionNeedsClose = true
}
if (runtime.agent.kind === 'mimo' && turn.lastMessageId) {
await operation(new Promise(resolve => setTimeout(resolve, MIMO_TERMINAL_DRAIN_MS)))
}
} finally {
runtime.replyState.collecting = false
}
@@ -894,6 +1072,7 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
if (!promptStarted && normalized.message !== 'LOCAL_AGENT_SESSION_INVALID') {
allowAcpSetupFallback(normalized)
}
caughtError = normalized
throw normalized
} finally {
clearTimeout(timeout)
@@ -906,7 +1085,26 @@ async function runAcpTurn(runtime, prompt, options, spec, profile) {
turn.pendingPermissionCallbacks.clear()
runtime.activeTurn = null
runtime.lastUsedAt = Date.now()
if (abortRequested) await settleWithin(cancelPromise, ACP_CANCEL_GRACE_MS)
if (abortRequested) {
await settleWithin(cancelPromise, ACP_CANCEL_GRACE_MS)
// A user stop or watchdog timeout used to destroy the process and its
// native session. Keep both when the Agent honours the cancellation.
let promptSettled = false
// Disposable runtimes are always closed after the turn; only a
// persistent runtime is worth waiting for.
if (promptRequest && !acpDisposableRuntimes.has(runtime)) {
await settleWithin(
promptRequest.then(() => { promptSettled = true }, () => { promptSettled = true }),
ACP_CANCEL_SETTLE_MS,
)
}
if (caughtError && caughtError.code !== 'LOCAL_AGENT_REPEATED_OUTPUT'
&& promptSettled && !runtime.closed && !runtime.failureError) {
Object.defineProperty(caughtError, 'acpRuntimeReusable', {
value: true, enumerable: false, configurable: true,
})
}
}
}
}
@@ -970,11 +1168,17 @@ async function runPersistentAcpAgent(agent, prompt, workdir, options, spec, prof
try {
return await runAcpTurn(runtime, prompt, options, spec, profile)
} catch (error) {
acpSessionRuntimes.delete(key)
await closeAcpRuntime(runtime)
if (error?.acpRuntimeReusable !== true) {
acpSessionRuntimes.delete(key)
await closeAcpRuntime(runtime)
}
throw error
} finally {
runtime.active = false
if (runtime.contributionNeedsClose) {
acpSessionRuntimes.delete(key)
await closeAcpRuntime(runtime)
}
runtime.lastUsedAt = Date.now()
await evictIdleAcpRuntimes()
}
+56 -7
View File
@@ -30,6 +30,7 @@ const {
shutdownAcpSessionRuntime,
} = require('./cli-acp-runner.cjs')
const { withOpenClawGateway } = require('./cli-openclaw-gateway.cjs')
const { createOutputRepetitionGuard } = require('./cli-output-repetition.cjs')
const { imageAttachmentLimit, invocation } = require('./cli-invocations.cjs')
const {
connectorLimitedRuntimeEvent,
@@ -47,12 +48,24 @@ const {
failedAgentProcessError,
} = require('./cli-process-support.cjs')
const { createLegacyOutboundPayload } = require('../../collaboration/outbound-payload.cjs')
const { authFailureCertainty } = require('../../runs/failure-signals.cjs')
const MAX_PROGRESS_STEPS = 8
const MAX_STDOUT_CAPTURE_BYTES = 10 * 1024 * 1024
const MAX_STDERR_CAPTURE_BYTES = 1024 * 1024
const OUTPUT_TRUNCATION_MARKER = Buffer.from('\n[output truncated]\n')
const BROKEN_INPUT_GRACE_MS = 5000
// A step that ends because the model asked for tools is followed by more
// steps (the tools run, then the model continues); only the last step's finish
// ends the run. Ending the process after the first one cut every tool-using
// MiMo or OpenCode turn short.
function stepFinishContinues(event) {
const reason = String(event?.part?.reason || event?.reason || '').toLowerCase()
return /^tool[-_ ]?(calls?|use)$/.test(reason)
}
function createBoundedOutputCapture(maxBytes) {
const headLimit = Math.floor(maxBytes / 2)
const tailLimit = maxBytes - headLimit
@@ -125,12 +138,13 @@ async function runAgent(agent, prompt, workdir, options = {}) {
const spec = invocation(agent.kind, agent.executable, workdir, sessionRef, {
platform,
sandbox: options.sandbox,
provider: options.provider,
attachments: options.attachments,
hermesAcpAvailable,
hermesStreamJsonAvailable: agent.streamJsonAvailable,
invocationTransport: options.invocationTransport,
sessionTransport: options.sessionTransport,
maxTurns: options.maxTurns,
model: agent.kind === 'workbuddy' ? options.env?.CODEBUDDY_MODEL : undefined,
})
const profile = resolveConnectorEventProfile(agent.kind, {
transport: spec.eventTransport,
@@ -214,6 +228,8 @@ async function runAgent(agent, prompt, workdir, options = {}) {
}))
}
const childEnv = childEnvironment(agent, workdir, options, platform)
const repetitionGuard = ['opencode', 'mimo', 'hermes'].includes(agent.kind)
? createOutputRepetitionGuard() : null
const runtimeEvents = createRuntimeEventEmitter({ ...options, onActivity: noteActivity }, childEnv)
const capabilityWarning = connectorLimitedRuntimeEvent(agent.kind, profile)
if (capabilityWarning) runtimeEvents.emit(capabilityWarning)
@@ -246,6 +262,7 @@ async function runAgent(agent, prompt, workdir, options = {}) {
let stopRequested = false
let timeoutRequested = false
let inputError = null
let brokenInputTimer = null
let timeout
let forceKillTimeout
let forceSettleTimeout
@@ -283,6 +300,12 @@ async function runAgent(agent, prompt, workdir, options = {}) {
const progressEvent = profile.mapProgress(event)
if (progressEvent) emitProgress(progressEvent)
for (const runtimeEvent of profile.mapEvent(event, runtimeState)) {
if (stopRequested || settled) return
if (repetitionGuard?.(runtimeEvent)) {
inputError = agentExecutionError('LOCAL_AGENT_REPEATED_OUTPUT')
abort()
return
}
runtimeEvents.emit(runtimeEvent)
}
})
@@ -295,6 +318,7 @@ async function runAgent(agent, prompt, workdir, options = {}) {
clearTimeout(forceSettleTimeout)
clearTimeout(terminalKillTimer)
clearTimeout(terminalForceKillTimer)
clearTimeout(brokenInputTimer)
options.signal?.removeEventListener('abort', abort)
callback()
}
@@ -314,9 +338,13 @@ async function runAgent(agent, prompt, workdir, options = {}) {
if (terminalObserved || !event || typeof event !== 'object') return
const terminal = (
(['workbuddy', 'claude', 'qwen'].includes(agent.kind) && event.type === 'result')
|| (agent.kind === 'pi' && event.type === 'agent_end')
|| (agent.kind === 'hermes' && spec.eventTransport === 'stream-json' && event.type === 'result')
|| (agent.kind === 'pi' && (event.type === 'agent_settled'
|| (event.type === 'agent_end'
&& !Object.hasOwn(event, 'willRetry') && !Object.hasOwn(event, 'willContinue'))))
|| (agent.kind === 'codex' && event.type === 'turn.completed')
|| (['mimo', 'opencode'].includes(agent.kind) && event.type === 'step_finish')
|| (['mimo', 'opencode'].includes(agent.kind) && event.type === 'step_finish'
&& !stepFinishContinues(event))
|| (agent.kind === 'gemini' && event.type === 'result')
|| (agent.kind === 'kimi' && event.type === 'session.resume_hint')
)
@@ -385,10 +413,23 @@ async function runAgent(agent, prompt, workdir, options = {}) {
})
child.stdin.on('error', (error) => {
if (settled || inputError) return
inputError = agentExecutionError(
const sendFailure = () => agentExecutionError(
'LOCAL_AGENT_SEND_FAILED',
redactChildSecrets(error?.message || error, childEnv),
)
// A closed pipe usually means the CLI exited before it read the whole
// prompt (expired sign-in, rejected flag). Its own output says why, so
// let the process finish and report that. Only a process that stays
// alive without reading its input is a send failure.
if (['EPIPE', 'ERR_STREAM_DESTROYED', 'ECONNRESET'].includes(error?.code)) {
brokenInputTimer ||= setTimeout(() => {
if (settled || inputError) return
inputError = sendFailure()
abort()
}, BROKEN_INPUT_GRACE_MS)
return
}
inputError = sendFailure()
abort()
})
child.on('error', error => finish(() => rejectTerminal(
@@ -422,6 +463,12 @@ async function runAgent(agent, prompt, workdir, options = {}) {
})
const rawStdout = stdout.text()
const structuredDetail = structuredCliError(rawStdout)
if (!String(result?.text || '').trim() && authFailureCertainty(rawStderr) === 'definitive') {
rejectTerminal(failedAgentProcessError(redactChildSecrets(rawStderr, childEnv), {
sessionRef: failureSessionRef, kind: agent.kind, model: options.env?.CODEBUDDY_MODEL,
}))
return
}
const usableNonZeroResult = code !== 0
&& result
&& typeof result.text === 'string'
@@ -429,16 +476,18 @@ async function runAgent(agent, prompt, workdir, options = {}) {
&& ['completed', 'partial'].includes(result.outcome)
if (code !== 0 && !usableNonZeroResult) {
const detail = redactChildSecrets(
[rawStderr, structuredDetail].filter(Boolean).join('\n'),
[result?.error, rawStderr, structuredDetail].filter(Boolean).join('\n'),
childEnv,
)
rejectTerminal(failedAgentProcessError(detail, { sessionRef: failureSessionRef }))
rejectTerminal(failedAgentProcessError(detail, {
sessionRef: failureSessionRef, kind: agent.kind, model: options.env?.CODEBUDDY_MODEL,
}))
return
}
if (result.error) {
rejectTerminal(failedAgentProcessError(
redactChildSecrets(result.error, childEnv),
{ sessionRef: failureSessionRef },
{ sessionRef: failureSessionRef, kind: agent.kind, model: options.env?.CODEBUDDY_MODEL },
))
return
}
+31 -4
View File
@@ -16,6 +16,7 @@ const AGENT_PROFILES = {
hermes: { label: 'Hermes', commands: ['hermes'] },
openclaw: { label: 'OpenClaw', commands: ['openclaw'] },
workbuddy: { label: 'WorkBuddy', commands: ['codebuddy'] },
zcode: { label: 'Zcode', commands: ['zcode'] },
pi: { label: 'Pi Agent', commands: ['pi', 'pi-agent', 'piagent'] },
kimi: { label: 'Kimi', commands: ['kimi'] },
mimo: { label: 'MiMo', commands: ['mimo'] },
@@ -209,6 +210,23 @@ function agentCommands(kind, options = {}) {
return commands
}
// The WorkBuddy app ships its CLI with only the lite bundle, which the
// launcher loads solely when this variable is set. Without it every headless
// call, `--help` included, exits with MODULE_NOT_FOUND. Installs that carry a
// regular bundle launch exactly as before.
function launchEnvironment(kind, executable, options = {}) {
if (kind !== 'workbuddy' || !executable) return {}
const { pathApi } = runtimeOptions(options)
try {
const dist = pathApi.join(pathApi.dirname(fs.realpathSync(executable)), '..', 'dist')
const shipped = name => fs.existsSync(pathApi.join(dist, name))
if (shipped('codebuddy-lite-wb.mjs') && !shipped('codebuddy-headless.js') && !shipped('codebuddy.js')) {
return { CODEBUDDY_FORCE_LITE_WB_BUNDLE: '1' }
}
} catch { /* an unreadable layout keeps the default launch */ }
return {}
}
function executableExtensions(options = {}) {
const { platform, env } = runtimeOptions(options)
if (platform !== 'win32') return ['']
@@ -333,9 +351,12 @@ async function probeAgentCapabilities(kind, executable, options = {}) {
const runtime = runtimeOptions(options)
const { env, execFileFn, platform } = runtime
const prepareCommandFn = options.prepareCommandFn || prepareCommand
const childEnv = options.childEnv || {
...systemChildEnvironment(env, platform),
PATH: searchPath(options),
const childEnv = {
...(options.childEnv || {
...systemChildEnvironment(env, platform),
PATH: searchPath(options),
}),
...launchEnvironment(kind, executable, options),
}
let identity = ''
try {
@@ -405,6 +426,8 @@ async function probeAgentCapabilities(kind, executable, options = {}) {
compatibilityState: 'compatible',
incompatibilityReason: '',
incompatibilityProbe: '',
...(kind === 'hermes' && results.some(result => result.output?.includes('--format')
&& result.output.includes('stream-json')) ? { streamJsonAvailable: true } : {}),
}
}
@@ -475,7 +498,10 @@ async function inspectAgentCandidate(kind, executable, options, childEnv) {
executable,
version,
...compatibility,
...(kind === 'hermes' ? { acpAvailable } : {}),
...(kind === 'hermes' ? {
acpAvailable,
...(capabilityCompatibility.streamJsonAvailable ? { streamJsonAvailable: true } : {}),
} : {}),
}
}
@@ -513,6 +539,7 @@ module.exports = {
AGENT_PROFILES,
ALLOWED_KINDS,
detectAgents,
launchEnvironment,
prepareCommand,
probeAgentCapabilities,
resolveExecutable,
+89 -1
View File
@@ -450,11 +450,99 @@ const HERMES_FINAL_TEXT_PROFILE = profile({
createFinalOutputAccumulator: createHermesLegacyFinalOutputAccumulator,
})
const HERMES_STREAM_JSON_PROFILE = profile({
profileId: 'hermes-stream-json-v1',
protocol: 'hermes-stream-json',
framing: 'jsonl',
source: 'stdout',
capabilities: {
answerMode: 'delta', tools: START_RESULT_TOOL_LIFECYCLE,
plan: false, reasoning: false, session: true, terminal: true,
},
createDecoder: createJsonLineParser,
createState: createStatelessRuntimeState,
mapEvent(event) {
if (event.type === 'text' && typeof event.text === 'string') {
return [{ type: 'answer_delta', status: 'running', delta: event.text }]
}
if (['tool_use', 'tool_result'].includes(event.type)) {
return [{
id: event.tool_call_id || event.name,
type: event.type === 'tool_use' ? 'tool_start' : 'tool_result_summary',
title: event.name || 'tool',
status: event.type === 'tool_use' ? 'running' : event.is_error ? 'failed' : 'completed',
}]
}
return []
},
async finalizeResult({ result, options }) {
// Both native chat formats use the same resumable conversation IDs.
if (result.sessionRef) await options.onSessionRef?.(result.sessionRef, { transport: 'legacy' })
return result
},
createFinalOutputAccumulator(sessionRef = '') {
const state = { text: '', sessionRef, outcome: 'partial' }
const ingest = event => {
if (typeof event.session_id === 'string' && event.session_id) state.sessionRef = event.session_id
if (event.type === 'text' && typeof event.text === 'string') appendFinalText(state, event.text)
if (event.type === 'result') {
if (typeof event.text === 'string') state.text = event.text.slice(0, MAX_FINAL_TEXT_CHARS)
state.outcome = event.exit_code === 0 && !event.error ? 'completed' : 'failed'
if (event.error) state.error = String(event.error)
}
}
const parser = createJsonLineParser(ingest)
return {
ingest,
write: chunk => parser.write(chunk),
end() {
parser.end()
return { ...state }
},
}
},
})
const ZCODE_JSON_PROFILE = profile({
profileId: 'zcode-prompt-json-v1',
protocol: 'zcode-prompt-json',
framing: 'document',
source: 'stdout',
capabilities: {
answerMode: 'final', tools: NO_TOOL_LIFECYCLE,
plan: false, reasoning: false, session: true, terminal: true,
},
createState: createStatelessRuntimeState,
createDecoder: null,
mapEvent: finalOnlyRuntimeEvents,
createFinalOutputAccumulator() {
const decoder = new StringDecoder('utf8')
const raw = { text: '' }
return {
capture(chunk) { appendFinalText(raw, decoder.write(chunk)) },
end() {
appendFinalText(raw, decoder.end())
let value
try { value = JSON.parse(raw.text) } catch { return { text: '', outcome: 'failed' } }
if (typeof value.response !== 'string' || typeof value.sessionId !== 'string') {
return { text: '', outcome: 'failed' }
}
return {
text: value.response,
sessionRef: value.sessionId,
outcome: ['idle', 'completed'].includes(value.projection?.status) ? 'completed' : 'partial',
}
},
}
},
})
const CONNECTOR_EVENT_PROFILES = Object.freeze({
codex: CODEX_APP_SERVER_PROFILE,
hermes: ACP_JSONRPC_PROFILE,
openclaw: ACP_JSONRPC_PROFILE,
workbuddy: WORKBUDDY_TERMINAL_RESULT_PROFILE,
zcode: ZCODE_JSON_PROFILE,
pi: PI_JSON_EVENTS_PROFILE,
kimi: ACP_JSONRPC_PROFILE,
mimo: MIMO_JSON_EVENTS_PROFILE,
@@ -466,7 +554,7 @@ const CONNECTOR_EVENT_PROFILES = Object.freeze({
})
const TRANSPORT_EVENT_PROFILES = Object.freeze({
hermes: Object.freeze({ legacy: HERMES_FINAL_TEXT_PROFILE }),
hermes: Object.freeze({ legacy: HERMES_FINAL_TEXT_PROFILE, 'stream-json': HERMES_STREAM_JSON_PROFILE }),
openclaw: Object.freeze({ legacy: OPENCLAW_TERMINAL_DOCUMENT_PROFILE }),
kimi: Object.freeze({ 'stream-json': KIMI_STREAM_JSON_PROFILE }),
mimo: Object.freeze({ acp: ACP_JSONRPC_PROFILE }),
+41 -44
View File
@@ -7,12 +7,13 @@ const IMAGE_ATTACHMENT_LIMITS = Object.freeze({
opencode: 4,
})
const DEFAULT_INTERNAL_TURN_LIMITS = Object.freeze({
// Meldwork already owns the outer discussion rounds. Keep WorkBuddy's
// nested tool loop bounded so one participant cannot dominate a round.
// Preserve the default for independent replies. Automatic discussions pass
// an explicit zero to let the native tool loop finish its work.
workbuddy: 8,
})
function internalTurnLimit(kind, requested) {
if (requested === 0) return 0
const value = Number(requested)
if (Number.isInteger(value) && value >= 1 && value <= 50) return value
return DEFAULT_INTERNAL_TURN_LIMITS[kind]
@@ -73,6 +74,20 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
}
}
if (kind === 'hermes') {
if (options.hermesStreamJsonAvailable === true
&& (options.hermesAcpAvailable === false || options.invocationTransport === 'stream-json'
|| (sessionRef && options.sessionTransport !== 'acp'))
&& (!sessionRef || options.sessionTransport !== 'acp')) {
return {
command: executable,
args: ['chat', '--format', 'stream-json',
...(options.sandbox === 'workspace-write' ? ['--yolo'] : []),
...(sessionRef ? ['--resume', sessionRef] : []),
...(attachments[0] ? ['--image', attachments[0]] : []), '--query'],
promptArg: true,
eventTransport: 'stream-json',
}
}
// Hermes uses the same native conversation identifier across its ACP and
// legacy transports. Keep it when an attachment requires the legacy path.
const legacySessionRef = sessionRef
@@ -87,6 +102,9 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
eventTransport: 'acp',
fallbackTransport: 'legacy',
fallbackSessionPolicy: 'invalidate',
// Hermes ACP advertises image prompts; send images as content blocks
// instead of silently dropping them from a text-only prompt.
...(attachments.length ? { imageAttachments: attachments } : {}),
}
}
return {
@@ -95,8 +113,6 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
'chat',
'--quiet',
...(options.sandbox === 'workspace-write' ? ['--yolo'] : []),
...(options.provider?.id ? ['--provider', options.provider.id] : []),
...(options.provider?.model ? ['--model', options.provider.model] : []),
...(legacySessionRef ? ['--resume', legacySessionRef] : []),
...(attachments[0] ? ['--image', attachments[0]] : []),
'--query',
@@ -144,15 +160,27 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
'--print',
'--output-format', 'stream-json',
'--include-partial-messages',
// Workspace write access means accepting file edits in the working
// directory. Skipping every permission check would also let the CLI
// run any command anywhere on the machine without confirmation.
'--permission-mode', options.sandbox === 'workspace-write' ? 'acceptEdits' : 'plan',
...(options.sandbox === 'workspace-write' ? ['--dangerously-skip-permissions'] : []),
...(maxTurns ? ['--max-turns', String(maxTurns)] : []),
...(options.model ? ['--model', options.model] : []),
...(sessionRef ? ['--resume', sessionRef] : []),
],
promptArg: true,
eventTransport: 'stream-json',
}
}
if (kind === 'zcode') {
return {
command: executable,
args: ['--json', '--no-color', '--verbose', '--cwd', workdir,
'--mode', options.sandbox === 'workspace-write' ? 'edit' : 'plan',
...(sessionRef ? ['--resume', sessionRef] : []), '--prompt'],
promptArg: true,
}
}
if (kind === 'pi') {
return {
command: executable,
@@ -168,31 +196,20 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
}
}
if (kind === 'kimi') {
if (options.sandbox !== 'workspace-write') {
return {
command: executable,
args: ['acp'],
acpMode: 'plan',
eventTransport: 'acp',
}
}
// Kimi rejects --auto together with --prompt. Use the permission-aware
// ACP transport for both modes instead of bypassing approval with --yolo.
return {
command: executable,
args: [
'--output-format', 'stream-json',
'--auto',
...(sessionRef ? ['--session', sessionRef] : []),
'--prompt',
],
promptArg: true,
eventTransport: 'stream-json',
args: ['acp'],
acpMode: 'default',
eventTransport: 'acp',
}
}
if (kind === 'mimo') {
if (!attachments.length && options.invocationTransport !== 'json') {
return {
command: executable,
args: ['acp', '--pure', '--cwd', workdir],
args: ['acp', '--cwd', workdir],
acpMode: options.sandbox === 'workspace-write' ? 'build' : 'plan',
eventTransport: 'acp',
fallbackTransport: 'json',
@@ -202,7 +219,7 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
return {
command: executable,
args: [
'run', '--pure', '--agent', options.sandbox === 'workspace-write' ? 'build' : 'plan',
'run', '--agent', options.sandbox === 'workspace-write' ? 'build' : 'plan',
'--format', 'json', '--dir', workdir,
...(sessionRef ? ['--session', sessionRef] : []),
],
@@ -218,24 +235,6 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
'--include-partial-messages',
'--verbose',
'--permission-mode', options.sandbox === 'workspace-write' ? 'acceptEdits' : 'plan',
...(options.provider?.id === 'anthropic' ? [
'--settings', JSON.stringify({
// Native settings.env overrides process env. The helper keeps the key out of argv.
apiKeyHelper: (options.platform || process.platform) === 'win32'
? 'powershell.exe -NoLogo -NoProfile -NonInteractive -Command "[Console]::Write([Environment]::GetEnvironmentVariable(\'MELDWORK_PROVIDER_API_KEY\'))"'
: 'printf \'%s\' "$MELDWORK_PROVIDER_API_KEY"',
env: {
CLAUDE_CODE_USE_BEDROCK: '0', CLAUDE_CODE_USE_VERTEX: '0',
CLAUDE_CODE_USE_FOUNDRY: '0', CLAUDE_CODE_USE_MANTLE: '0',
ANTHROPIC_BASE_URL: options.provider.baseUrl,
ANTHROPIC_API_KEY: '', ANTHROPIC_AUTH_TOKEN: '', CLAUDE_CODE_OAUTH_TOKEN: '',
ANTHROPIC_CUSTOM_HEADERS: '',
ANTHROPIC_DEFAULT_OPUS_MODEL: '', ANTHROPIC_DEFAULT_SONNET_MODEL: '',
ANTHROPIC_DEFAULT_HAIKU_MODEL: '',
},
}),
] : []),
...(options.provider?.model ? ['--model', options.provider.model] : []),
...(sessionRef ? ['--resume', sessionRef] : []),
],
promptArg: true,
@@ -248,8 +247,6 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
'--output-format', 'stream-json',
'--include-partial-messages',
'--approval-mode', options.sandbox === 'workspace-write' ? 'auto-edit' : 'plan',
...(options.provider?.id === 'openai' ? ['--auth-type', 'openai'] : []),
...(options.provider?.model ? ['--model', options.provider.model] : []),
...(sessionRef ? ['--resume', sessionRef] : []),
],
promptArg: true,
@@ -271,7 +268,7 @@ function invocation(kind, executable, workdir, sessionRef = '', options = {}) {
if (!attachments.length && options.invocationTransport !== 'json') {
return {
command: executable,
args: ['acp', '--pure', '--cwd', workdir],
args: ['acp', '--cwd', workdir],
acpMode: options.sandbox === 'workspace-write' ? 'build' : 'plan',
eventTransport: 'acp',
fallbackTransport: 'json',
+36 -9
View File
@@ -172,17 +172,38 @@ function stepFinishOutcome(reason) {
return 'partial'
}
function structuredResultDiagnostic(event) {
const parts = []
if (typeof event?.error === 'string' && event.error.trim()) parts.push(event.error.trim())
else if (event?.error?.message) parts.push(String(event.error.message))
if (typeof event?.result === 'string' && event.result.trim()) parts.push(event.result.trim())
if (Array.isArray(event?.errors)) {
parts.push(...event.errors.map(value => String(value || '').trim()).filter(Boolean))
}
return parts.join('\n')
}
function ingestStructuredEvent(state, event) {
if (!event || typeof event !== 'object' || Array.isArray(event)) return
state.seen = true
if (['workbuddy', 'claude', 'qwen'].includes(state.kind)
&& event.type === 'result' && typeof event.result === 'string') {
&& event.type === 'result') {
if (typeof event.session_id === 'string') state.sessionRef = event.session_id
state.failure = null
state.diagnostic = ''
state.text = ''
appendStructuredText(state, event.result, '')
state.outcome = event.is_error === true ? 'partial' : 'completed'
if (typeof event.result === 'string') {
state.failure = null
state.diagnostic = ''
state.text = ''
appendStructuredText(state, event.result, '')
state.outcome = event.is_error === true ? 'partial' : 'completed'
return
}
// An error result without a usable result string still carries the CLI's
// error list; keep it as the failure diagnostic so authentication and
// session problems are classified instead of surfacing as a bare
// LOCAL_AGENT_PROCESS_FAILED.
if (event.is_error === true || String(event.subtype || '').startsWith('error')) {
markStructuredFailure(state, 'LOCAL_AGENT_PROCESS_FAILED', structuredResultDiagnostic(event))
}
return
}
if (structuredEventFailed(event)) {
@@ -236,14 +257,16 @@ function ingestStructuredEvent(state, event) {
state.outcome = stepFinishOutcome(event.message?.stopReason || event.stopReason || 'stop')
}
if (event.type === 'agent_end') {
state.piContinuing = event.willRetry === true || event.willContinue === true
const lastAssistant = event.messages?.findLast(message => message?.role === 'assistant')
if (lastAssistant?.stopReason) state.outcome = stepFinishOutcome(lastAssistant.stopReason)
else if (!state.outcome) state.outcome = 'completed'
if (state.outcome === 'completed' && event.willRetry !== true) {
if (state.outcome === 'completed' && !state.piContinuing) {
state.failure = null
state.diagnostic = ''
}
}
if (event.type === 'agent_settled') state.piContinuing = false
if (state.outcome === 'failed') markStructuredFailure(state)
return
}
@@ -407,7 +430,8 @@ function finalizeStructuredState(state, context = {}) {
}
if (!state.seen && !state.text && !state.outcome) return null
const outcome = state.outcome || (state.text ? 'partial' : 'failed')
const outcome = state.kind === 'pi' && state.piContinuing && state.outcome === 'completed'
? 'partial' : state.outcome || (state.text ? 'partial' : 'failed')
const result = {
text: state.kind === 'pi'
? stripPiPromptEcho(state.text, context.prompt)
@@ -843,6 +867,8 @@ function classifyCliOutcome(kind, stdout) {
}
if (kind === 'pi') {
const terminal = events.findLast(event => event?.type === 'turn_end' || event?.type === 'agent_end')
if ((terminal?.willRetry === true || terminal?.willContinue === true)
&& events.at(-1)?.type !== 'agent_settled') return { outcome: 'partial' }
const lastAssistant = terminal?.messages?.findLast(message => message?.role === 'assistant')
const previousTurn = events.findLast(event => event?.type === 'turn_end')
const reason = terminal?.message?.stopReason || terminal?.stopReason || lastAssistant?.stopReason
@@ -885,7 +911,8 @@ function classifyCliOutcome(kind, stdout) {
function structuredCliError(stdout) {
const failed = parseJsonOutputEvents(stdout)
.findLast(event => event?.is_error || event?.subtype?.startsWith('error'))
return String(failed?.error?.message || failed?.result || '').trim()
if (!failed) return ''
return structuredResultDiagnostic(failed)
}
function hermesSessionRef(stderr) {
@@ -0,0 +1,43 @@
// Detect a runaway native turn, rather than deduplicating its visible answer.
// Exact repetitions and sustained paraphrased closing acknowledgements count;
// real tool activity resets the window.
const CLOSING_ACKNOWLEDGEMENT = /^(?:(?:确认(?:收到)?|收到|本轮).{0,100}(?:收尾|结束|完成|定稿|后续轮次|按.{0,16}分工).{0,30}|确认|收到|确认收到|(?:confirmed|acknowledged|done)(?:[,:;].{0,100})?)$/iu
function createOutputRepetitionGuard() {
let text = ''
let unchecked = 0
return update => {
const type = update?.sessionUpdate || update?.type
if (['tool_call', 'tool_call_update', 'tool_start', 'tool_result_summary'].includes(type)) {
text = ''
unchecked = 0
return false
}
const value = type === 'answer_delta' ? update.delta
: type === 'agent_message_chunk' && update.content?.type === 'text' ? update.content.text : ''
const chunk = String(value || '').replace(/\s/g, '')
if (!chunk) return false
text = (text + chunk).slice(-8192)
unchecked += chunk.length
if (unchecked < 32 || text.length < 64) return false
unchecked = 0
const clauses = text.split(/[。!?.!?]+/u).filter(Boolean).slice(-12)
if (clauses.filter(clause => CLOSING_ACKNOWLEDGEMENT.test(clause)).length >= 8) return true
if (new Set(text).size < 6) return false
const anchor = text.slice(-8)
let previous = text.lastIndexOf(anchor, text.length - 16)
while (previous >= 0) {
const period = text.length - 8 - previous
if (period > 2048) break
const copies = period < 80 ? 8 : 4
if (period * copies <= text.length) {
const block = text.slice(-period)
if (new Set(block).size >= 6 && text.endsWith(block.repeat(copies))) return true
}
previous = previous > 0 ? text.lastIndexOf(anchor, previous - 1) : -1
}
return false
}
}
module.exports = { createOutputRepetitionGuard }
+42 -20
View File
@@ -1,8 +1,12 @@
const path = require('node:path')
const { searchPath, systemChildEnvironment } = require('./cli-discovery.cjs')
const { launchEnvironment, searchPath, systemChildEnvironment } = require('./cli-discovery.cjs')
const { NETWORK_ENV_KEYS, networkEnvironment } = require('./cli-network-environment.cjs')
const { redactChildSecrets } = require('./cli-runtime-events.cjs')
const { agentRuntimeError, terminalAuthenticationDiagnostic } = require('../agent-runtime-contract.cjs')
const {
agentRuntimeError,
authFailureCode,
} = require('../agent-runtime-contract.cjs')
const { authFailureCertainty, isContextOverflow } = require('../../runs/failure-signals.cjs')
const { validateOpenClawRuntimeGuard } = require('./openclaw-runtime.cjs')
const TERMINATE_GRACE_MS = 500
@@ -22,38 +26,44 @@ const OPENCLAW_RUNTIME_PATH_KEYS = Object.freeze([
'OPENCLAW_CONFIG_PATH',
'OPENCLAW_WORKSPACE_DIR',
])
const CONTROLLED_RUNTIME_ENV = /^(?:AWS_|AZURE_|GOOGLE_|GEMINI_|ANTHROPIC_|OPENAI_|OPENROUTER_|CODEBUDDY_|HERMES_|PI_|KIMI_|MIMO_|DASHSCOPE_|CLAUDE_|OCR_|CLOUDSDK_|VERTEX_|CLOUD_ML_|MOONSHOT_|CONNECTOR_TEST_|MELDWORK_TEST_)/
const CONTROLLED_RUNTIME_ENV = /^(?:AWS_|AZURE_|GOOGLE_|GEMINI_|ANTHROPIC_|OPENAI_|OPENROUTER_|CODEX_|CODEBUDDY_|HERMES_|PI_|KIMI_|MIMO_|MIMOCODE_|OPENCODE_|QWEN_|DASHSCOPE_|CLAUDE_|OCR_|CLOUDSDK_|VERTEX_|CLOUD_ML_|MOONSHOT_|CONNECTOR_TEST_|MELDWORK_TEST_)/
const BLOCKED_RUNTIME_ENV = /^(?:NODE_OPTIONS|NODE_PATH|LD_PRELOAD|LD_LIBRARY_PATH|DYLD_|ELECTRON_RUN_AS_NODE)$/
function agentExecutionError(code, diagnostic = '') {
return agentRuntimeError(code, diagnostic)
}
function authConfigurationFailure(detail) {
return String(detail || '').split(/\r?\n/).some((line) => {
const text = line.trim().replace(/^(?:error|fatal)\s*:\s*/i, '')
return Boolean(terminalAuthenticationDiagnostic(text))
|| /^(?:(?:invalid|expired|missing|incorrect)\s+(?:api[ _-]?key|(?:access|refresh|auth)[ _-]?token|credentials?)\b|(?:authentication|authorization)\s+(?:failed|required)\b|(?:please\s+)?(?:log|sign)\s+in\b|not\s+logged\s+in\b|select an auth type\b|provider rejected\b)/i.test(text)
|| /^(?:认证|鉴权|登录)(?:失败|过期)|^(?:令牌|凭据|密钥)(?:无效|缺失|已过期)|^请(?:先|重新)登录/.test(text)
})
}
function invalidSessionFailure(detail) {
const text = String(detail || '').trim()
if (!text || authConfigurationFailure(text)
if (!text || authFailureCertainty(text)
|| /\b(?:api|access|refresh|auth|session)[ _-]?token\b/i.test(text)) return false
// A resumed native session that no longer fits the model context cannot be
// continued; rebuilding it with compact continuation context is the fix.
if (isContextOverflow(text)) return true
if (/^(?:Error: )?Resume retains gateway session policy; creation overrides are unsupported on resume\./m.test(text)
&& /--yolo differs from this session's launch/.test(text)) return true
// Runtime crashes ("thread 'main' panicked ... invalid ...") and parser
// errors mention threads or invalid input without saying anything about the
// stored session. Discarding a healthy session for them loses context.
if (/\bpanicked\b|\bSyntaxError\b|\binvalid JSON\b|\bUnexpected token\b|\bstack backtrace\b/i.test(text)) {
return false
}
return /\b(?:no|unknown|invalid|expired|missing|stale)\s+(?:saved\s+)?(?:session|conversation|thread)\b|\b(?:session|conversation|thread)(?:\s+(?:id|key|reference))?\b[^\n]{0,100}\b(?:not found|does not exist|no longer exists|invalid|unknown|expired|has expired|was deleted|cannot be resumed)\b/i
.test(text)
}
function failedAgentProcessError(detail, options = {}) {
if (!detail) return agentExecutionError('LOCAL_AGENT_EXITED')
if (options.kind === 'zcode' && /\bModel provider is missing an API key:/i.test(detail)) {
return agentExecutionError('LOCAL_AGENT_AUTH_REQUIRED', 'Missing API key for the configured Zcode provider.')
}
if (options.kind === 'workbuddy' && options.model && authFailureCode(detail)) {
return agentExecutionError('LOCAL_WORKBUDDY_PROVIDER_AUTH', detail)
}
return agentExecutionError(
options.sessionRef && invalidSessionFailure(detail)
? 'LOCAL_AGENT_SESSION_INVALID'
: authConfigurationFailure(detail)
? 'LOCAL_AGENT_AUTH_REQUIRED'
: 'LOCAL_AGENT_PROCESS_FAILED',
: authFailureCode(detail) || 'LOCAL_AGENT_PROCESS_FAILED',
detail,
)
}
@@ -108,10 +118,21 @@ function childEnvironment(agent, workdir, options, platform) {
? { HERMES_EXEC_ASK: '', HERMES_YOLO_MODE: '1' }
: { HERMES_EXEC_ASK: '1', HERMES_YOLO_MODE: '' }
: {}
const openCodeSafetyEnv = agent.kind === 'opencode'
&& options.sandbox !== 'workspace-write'
? { OPENCODE_PERMISSION: OPENCODE_READ_ONLY_PERMISSION }
: {}
const openCodeSafetyEnv = {}
// ACP has no terminal UI for question/plan transitions. Match the native
// headless runner: return the answer to Meldwork instead of waiting forever.
const headlessPermissions = { question: 'deny', plan_enter: 'deny', plan_exit: 'deny' }
if (agent.kind === 'opencode') {
openCodeSafetyEnv.OPENCODE_PERMISSION = JSON.stringify({
...(options.sandbox !== 'workspace-write' ? JSON.parse(OPENCODE_READ_ONLY_PERMISSION) : {}),
...headlessPermissions,
})
}
if (agent.kind === 'mimo') {
// MiMo applies read/write permissions with ACP session/set_mode. Keep
// process-wide permissions stable when the discussion changes phase.
openCodeSafetyEnv.MIMOCODE_PERMISSION = JSON.stringify(headlessPermissions)
}
const source = { ...process.env, ...options.env }
const controlled = Object.fromEntries(Object.entries(options.env || {}).filter(([key, value]) => (
typeof value === 'string'
@@ -123,6 +144,7 @@ function childEnvironment(agent, workdir, options, platform) {
...controlled,
...hermesSafetyEnv,
...openCodeSafetyEnv,
...launchEnvironment(agent.kind, agent.executable, { platform }),
PATH: searchPath({ platform, env: source }),
}
}
@@ -425,7 +425,7 @@ function acpPlanSummary(update) {
}
function createAcpRuntimeState() {
return { tools: new Map() }
return { tools: new Map(), reasoning: new Map() }
}
const ACP_TOOL_KINDS = new Set([
@@ -447,10 +447,22 @@ function acpToolOperation(update) {
function acpRuntimeEvents(update, state) {
if (!update || typeof update !== 'object') return []
if (update.sessionUpdate === 'agent_message_chunk' && update.content?.type === 'text') {
return [{ type: 'answer_delta', status: 'running', delta: update.content.text }]
// Close an open reasoning phase once the visible answer starts.
const closed = state?.reasoning?.get('reasoning:acp')?.startsWith('running:')
? reasoningLifecycleEvent(state, 'reasoning:acp', 'completed')
: null
return [
...(closed ? [closed] : []),
{ type: 'answer_delta', status: 'running', delta: update.content.text },
]
}
if (update.sessionUpdate === 'agent_thought_chunk' && update.content?.type === 'text') {
return []
// Thinking is real progress: report a text-free reasoning status so the
// watchdog and the trace know the Agent is working. The private thought
// text itself never leaves the main process.
if (!state?.reasoning) return []
const event = reasoningLifecycleEvent(state, 'reasoning:acp', 'running')
return event ? [event] : []
}
if (update.sessionUpdate === 'tool_call' || update.sessionUpdate === 'tool_call_update') {
const status = runtimeEventStatus(update.status)
@@ -99,7 +99,7 @@ function pathSuffixLength(value) {
const matches = [
value.match(/file:\/\/\/[^\s"'`<>|,;)}\]]+$/i),
value.match(/(?<![A-Za-z0-9:])(?:[A-Za-z]:[\\/]|\\\\)[^\s"'`<>|,;)}\]]+$/),
value.match(/(?<![A-Za-z0-9:/])\/+[^\s"'`<>|,;)}\]]+$/),
value.match(/(?<![\p{L}\p{N}:/])\/+[^\s"'`<>|,;)}\]]+$/u),
]
return Math.max(...matches.map(match => match?.[0]?.length || 0))
}
@@ -107,6 +107,9 @@ function pathSuffixLength(value) {
function boundarySensitiveSuffixLength(value, secrets) {
let retained = 0
const retain = (length) => { retained = Math.max(retained, length || 0) }
// Keep a word boundary across chunks: a slash in the next chunk may be
// prose (云/端), not the start of an absolute filesystem path.
retain(value.match(/(?=[^\x00-\x7f])[\p{L}\p{N}]$/u)?.[0]?.length)
for (const secret of secrets) {
const limit = Math.min(value.length, secret.length - 1)
for (let length = limit; length > retained; length -= 1) {
@@ -123,7 +126,7 @@ function boundarySensitiveSuffixLength(value, secrets) {
}
const pathLength = pathSuffixLength(value)
if (pathLength) retain(pathLength)
retain(value.match(/(?<![A-Za-z0-9:/])\/+$/)?.[0]?.length)
retain(value.match(/(?<![\p{L}\p{N}:/])\/+$/u)?.[0]?.length)
const matches = [
value.match(/\bAKIA[0-9A-Z]{0,15}$/),
@@ -206,7 +209,7 @@ function sanitizeRuntimeEventText(value, childEnv, limit, singleLine = false, tr
.replace(/-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/gi, '[redacted private key]')
.replace(/file:\/\/\/[^\s"'`<>|,;)}\]]+/gi, '[path]')
.replace(/(?<![A-Za-z0-9:])(?:[A-Za-z]:[\\/]|\\\\)[^\s"'`<>|,;)}\]]+/g, '[path]')
.replace(/(?<![A-Za-z0-9:/])\/+[^\s"'`<>|,;)}\]]+/g, '[path]')
.replace(/(?<![\p{L}\p{N}:/])\/+[^\s"'`<>|,;)}\]]+/gu, '[path]')
.replace(/(["']?)(?:[A-Za-z0-9_.-]*(?:api[_-]?key|access[_-]?key(?:[_-]?id)?|secret[_-]?access[_-]?key|access[_-]?token|refresh[_-]?token|auth[_-]?token|database[_-]?url|connection[_-]?string|dsn|token|secret|password|credential|authorization|private[_-]?key)[A-Za-z0-9_.-]*)\1\s*[:=]\s*(?:"[^"\r\n]*"|'[^'\r\n]*'|\[[^\]\r\n]*\]|[^\s,;}\]]+)/gi, 'credential=[redacted]')
.replace(/\bAKIA[0-9A-Z]{16}\b/g, '[redacted]')
.replace(/\bbearer\s+[A-Za-z0-9._~+\/-]+=*/gi, 'Bearer [redacted]')
@@ -0,0 +1,41 @@
const { randomUUID } = require('node:crypto')
// A contribution ends only on an explicit, invocation-specific signal from
// the assistant. Tool output and acknowledgements cannot finish a turn.
function createTurnCompletion() {
const marker = `[[MELDWORK_TURN_DONE:${randomUUID()}]]`
let text = ''
let delivered = 0
let completed = false
return {
instruction: `This is one scheduled contribution. Other participants cannot run until you return. Complete only the contribution requested for this invocation; do not poll or wait for their approvals. After your answer and any required collaboration receipt, output this exact signal on its own final line: ${marker}\nThe signal ends only your invocation, never the group task. Do not quote or explain it, and do not output it before all your tools have finished.`,
push(delta, toolsFinished = true) {
if (completed) return { delta: '', completed: true }
text += delta
const index = text.lastIndexOf(marker)
if (toolsFinished && index > 0 && text[index - 1] === '\n'
&& /^(?:[ \t]*\r?\n|[ \t]*$)/.test(text.slice(index + marker.length))) {
const before = text.slice(0, index)
const fences = before.match(/^\s*(?:```|~~~)/gm) || []
if (fences.length % 2 === 0 && before.trim()) {
completed = true
const visible = before.slice(delivered)
text = before
delivered = text.length
return { delta: visible, completed: true }
}
}
let held = 0
for (let length = Math.min(marker.length, text.length); length > 0; length--) {
if (text.endsWith(marker.slice(0, length))) { held = length; break }
}
const end = text.length - held
const visible = text.slice(delivered, end)
delivered = end
return { delta: visible, completed: false }
},
get completed() { return completed },
}
}
module.exports = { createTurnCompletion }
@@ -0,0 +1,23 @@
const fs = require('node:fs')
const path = require('node:path')
const os = require('node:os')
function mimoConfiguredProvider(options = {}) {
const env = options.env || process.env
const root = env.XDG_CONFIG_HOME || path.join(options.home || os.homedir(), '.config')
try {
const file = path.join(root, 'mimocode', 'mimocode.json')
if (fs.statSync(file).size > 2 * 1024 * 1024) return false
const config = JSON.parse(fs.readFileSync(file, 'utf8'))
if (typeof config.model !== 'string' || !config.model.includes('/')) return false
const provider = config.provider?.[config.model.split('/')[0]]
let key = provider?.options?.apiKey
if (typeof key !== 'string' || !key.trim()) return false
const reference = key.match(/^\{env:([A-Za-z_][A-Za-z0-9_]*)\}$/)
if (reference) key = env[reference[1]]
else if (/^\{.*\}$/.test(key)) return false
return typeof key === 'string' && Boolean(key.trim())
} catch { return false }
}
module.exports = { mimoConfiguredProvider }
+11 -92
View File
@@ -5,7 +5,6 @@ const { isDeepStrictEqual } = require('node:util')
const { atomicWritePrivateFile } = require('../../security/private-file.cjs')
const MANAGED_OPENCLAW_PROVIDER_ID = 'meldwork-desktop'
const READ_ONLY_TOOLS = [
'read', 'web_search', 'web_fetch', 'memory_search', 'memory_get', 'session_status',
]
@@ -37,7 +36,6 @@ const OPENCLAW_RUNTIME_PATH_ENV_KEYS = Object.freeze([
'OPENCLAW_WORKSPACE_DIR',
])
const OPENCLAW_RUNTIME_CREDENTIAL_KEYS = new Set([
'MELDWORK_OPENCLAW_API_KEY',
'MELDWORK_OPENCLAW_NATIVE_API_KEY',
'OPENCLAW_GATEWAY_TOKEN',
])
@@ -173,26 +171,7 @@ function validateFileIdentity(expected) {
}
}
function normalizeProvider(input) {
const apiKey = typeof input?.OPENAI_API_KEY === 'string' ? input.OPENAI_API_KEY.trim() : ''
const baseUrl = String(input?.OPENAI_BASE_URL || '')
.trim()
.replace(/\/+$/, '')
.replace(/\/chat\/completions$/i, '')
const model = String(input?.OPENAI_MODEL || '').trim()
let parsed
try { parsed = new URL(baseUrl) } catch { throw new Error('OPENCLAW_PROVIDER_INVALID') }
const loopback = ['localhost', '127.0.0.1', '[::1]'].includes(parsed.hostname)
if (!apiKey || apiKey.length > 8192 || isOpenClawSecretReference(apiKey)
|| !model || model.length > 120
|| (parsed.protocol !== 'https:' && !(parsed.protocol === 'http:' && loopback))
|| parsed.username || parsed.password || parsed.search || parsed.hash) {
throw new Error('OPENCLAW_PROVIDER_INVALID')
}
return { apiKey, baseUrl, model }
}
function runtimePaths({ storageRoot, workdir, sessionRef, configName }) {
function runtimePaths({ storageRoot, workdir, sessionRef, configName, runtimeVersion }) {
if (!path.isAbsolute(storageRoot) || !path.isAbsolute(workdir)) {
throw new Error('OPENCLAW_RUNTIME_INVALID_SCOPE')
}
@@ -204,8 +183,15 @@ function runtimePaths({ storageRoot, workdir, sessionRef, configName }) {
throw new Error('OPENCLAW_RUNTIME_INVALID_SCOPE')
}
const resolvedWorkdir = path.resolve(workdir)
// The managed scope is keyed by session, workspace, and CLI version. OpenClaw
// refuses to start on state written by an older CLI ("Legacy workspace setup
// state requires migration"), so a version-scoped directory gives an upgraded
// CLI a fresh isolated scope instead of a guaranteed startup failure.
const versionSegment = String(runtimeVersion || '').trim()
const scope = crypto.createHash('sha256')
.update(`${sessionRef || 'configure'}\0${resolvedWorkdir}`)
.update(versionSegment
? `${sessionRef || 'configure'}\0${resolvedWorkdir}\0${versionSegment}`
: `${sessionRef || 'configure'}\0${resolvedWorkdir}`)
.digest('hex')
.slice(0, 24)
const runtimeRoot = path.join(resolvedStorageRoot, 'openclaw-managed', scope)
@@ -458,74 +444,8 @@ function normalizedNativeModel(input) {
return model
}
function managedOpenClawOptions({
storageRoot, workdir, sessionRef = '', allowWrite = false, provider,
}) {
const normalized = normalizeProvider(provider)
const configName = allowWrite
? 'openclaw.provider.workspace-write.json'
: 'openclaw.provider.read-only.json'
const runtime = runtimePaths({
storageRoot, workdir, sessionRef, configName,
})
const { resolvedWorkdir, home, state, configPath } = runtime
const modelRef = `${MANAGED_OPENCLAW_PROVIDER_ID}/${normalized.model}`
const gatewayCredentialKey = 'OPENCLAW_GATEWAY_TOKEN'
const gatewayToken = crypto.randomBytes(32).toString('base64url')
const config = {
agents: {
defaults: {
workspace: resolvedWorkdir,
model: { primary: modelRef },
skipBootstrap: true,
},
},
models: {
mode: 'replace',
providers: {
[MANAGED_OPENCLAW_PROVIDER_ID]: {
baseUrl: normalized.baseUrl,
apiKey: {
source: 'env', provider: 'default', id: 'MELDWORK_OPENCLAW_API_KEY',
},
api: 'openai-completions',
models: [{ id: normalized.model, name: normalized.model, input: ['text'] }],
},
},
},
tools: toolPolicy(allowWrite),
gateway: {
mode: 'local',
bind: 'loopback',
auth: {
mode: 'token',
token: { source: 'env', provider: 'default', id: gatewayCredentialKey },
},
controlUi: { enabled: false },
},
}
const credentialKey = 'MELDWORK_OPENCLAW_API_KEY'
const openClawRuntimeGuard = writeRuntimeConfig(runtime, config, {
[credentialKey]: normalized.apiKey,
[gatewayCredentialKey]: gatewayToken,
}, credentialKey)
return {
openClawRuntimeGuard,
env: {
OPENCLAW_HOME: home,
OPENCLAW_STATE_DIR: state,
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_WORKSPACE_DIR: resolvedWorkdir,
[credentialKey]: normalized.apiKey,
[gatewayCredentialKey]: gatewayToken,
},
}
}
function nativeOpenClawOptions({
storageRoot, workdir, sessionRef = '', allowWrite = false, runtime,
storageRoot, workdir, sessionRef = '', allowWrite = false, runtime, openclawVersion,
}) {
const modelRef = String(runtime?.model || '').trim()
const provider = runtime?.provider
@@ -557,7 +477,7 @@ function nativeOpenClawOptions({
? 'openclaw.native.workspace-write.json'
: 'openclaw.native.read-only.json'
const runtimePathsResult = runtimePaths({
storageRoot, workdir, sessionRef, configName,
storageRoot, workdir, sessionRef, configName, runtimeVersion: openclawVersion,
})
const { resolvedWorkdir, home, state, configPath } = runtimePathsResult
const modelConfig = { primary: modelRef }
@@ -618,7 +538,6 @@ function nativeOpenClawOptions({
module.exports = {
configureOpenClawGatewayRuntime,
isOpenClawSecretReference,
managedOpenClawOptions,
nativeOpenClawOptions,
refreshOpenClawRuntimeAfterStartup,
validateOpenClawRuntimeGuard,
@@ -0,0 +1,89 @@
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const { launchEnvironment } = require('./cli-discovery.cjs')
const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,119}$/
function workBuddyModels(options = {}) {
const env = options.env || process.env
const root = env.CODEBUDDY_CONFIG_DIR || path.join(options.home || os.homedir(), '.workbuddy')
try {
const filename = path.join(root, 'models.json')
const stat = fs.statSync(filename)
if (!stat.isFile() || stat.size > 2 * 1024 * 1024) return []
const value = JSON.parse(fs.readFileSync(filename, 'utf8'))
const models = Array.isArray(value) ? value : value.models
return (Array.isArray(models) ? models : []).filter(model => (
typeof model?.id === 'string' && MODEL_ID.test(model.id)
&& typeof model.apiKey === 'string' && model.apiKey.trim()
&& model.supportsToolCall === true && validUrl(model.url)
)).slice(0, 64)
} catch { return [] }
}
function validUrl(value) {
try {
const url = new URL(value)
return ['http:', 'https:'].includes(url.protocol) && !url.username && !url.password
} catch { return false }
}
function workBuddySettings(options = {}) {
return {
modelId: options.modelId || '',
models: workBuddyModels(options).map(model => ({
id: model.id, name: String(model.name || model.id).slice(0, 120),
})),
}
}
function workBuddyEnvironment(options = {}) {
if (!options.modelId) return {}
const model = workBuddyModels(options).find(model => model.id === options.modelId)
if (!model) throw new Error('LOCAL_WORKBUDDY_MODEL_UNAVAILABLE')
// Use the CLI's supported provider settings, without copying desktop account
// tokens or attempting to unlock its host-managed encrypted credential store.
return {
CODEBUDDY_CONFIG_DIR: (options.env || process.env).CODEBUDDY_CONFIG_DIR
|| path.join(options.home || os.homedir(), '.workbuddy'),
CODEBUDDY_MODEL: `custom-local:${model.id}`,
CODEBUDDY_API_KEY: model.apiKey,
CODEBUDDY_BASE_URL: model.url,
}
}
function workBuddyCredentialState(options = {}) {
if (options.modelId) {
try {
workBuddyEnvironment(options)
return { state: 'ready', source: 'native-credential' }
} catch { return { state: 'unknown', source: 'workbuddy-model-unavailable' } }
}
const env = options.env || process.env
if (env.CODEBUDDY_API_KEY?.trim() || env.CODEBUDDY_AUTH_TOKEN?.trim()) {
return { state: 'ready', source: 'native-credential' }
}
// A models.json entry is not a selected provider. The desktop app's shared
// encrypted login also cannot be decoded by its standalone lite CLI.
if (launchEnvironment('workbuddy', options.executable, options).CODEBUDDY_FORCE_LITE_WB_BUNDLE !== '1') {
return { state: 'unknown', source: 'unverified' }
}
const home = options.home || os.homedir()
const platform = options.platform || process.platform
const root = platform === 'darwin' ? path.join(home, 'Library', 'Application Support')
: platform === 'win32' ? path.join(home, 'AppData', 'Local')
: path.join(home, '.local', 'share')
try {
const filename = path.join(root, 'CodeBuddyExtension', 'Data', 'Public', 'auth', 'workbuddy-desktop.info')
if (fs.statSync(filename).size <= 2 * 1024 * 1024) {
const auth = JSON.parse(fs.readFileSync(filename, 'utf8')).auth
if (auth?.accessToken && typeof auth.accessToken === 'object') {
return { state: 'unknown', source: 'workbuddy-desktop-locked' }
}
}
} catch { /* native CLI may still have an independent login */ }
return { state: 'unknown', source: 'unverified' }
}
module.exports = { workBuddyCredentialState, workBuddyEnvironment, workBuddySettings }
@@ -632,8 +632,6 @@ class CloudAgentBridgeStore {
installed: true,
installSupported: false,
installErrorCode: '',
providerCompatible: false,
providerMode: 'connector',
imageAttachmentLimit: 0,
custom: false,
connector: true,
@@ -603,8 +603,6 @@ class LocalAgentConnectors {
installed: true,
installSupported: false,
installErrorCode: '',
providerCompatible: false,
providerMode: 'connector',
imageAttachmentLimit: manifest.inputTypes.includes('image') ? 32 : 0,
custom: !cloud,
connector: true,
@@ -127,9 +127,6 @@ function publicProfile(definition, version = '') {
promptMode: definition.promptMode,
custom: true,
recommended: false,
providerCompatible: false,
providerSupport: 'custom-cli',
providerMode: 'custom',
installed: true,
installSupported: false,
installErrorCode: '',
@@ -4,6 +4,17 @@ const fs = require('node:fs')
const path = require('node:path')
const { promisify } = require('node:util')
const { searchPath } = require('../cli/cli-adapters.cjs')
const { compareReleaseVersions, extractAgentVersion } = require('../agent-compatibility.cjs')
// True when the installed CLI is the recipe's version or newer. A substring
// test treated "11.2.3" as "1.2.3" and offered downgrades for newer CLIs.
function installedVersionCurrent(installedVersion, targetVersion) {
const installed = extractAgentVersion(installedVersion).split('-')[0]
const target = extractAgentVersion(targetVersion).split('-')[0]
const comparison = installed && target ? compareReleaseVersions(installed, target) : null
if (comparison != null) return comparison >= 0
return Boolean(targetVersion) && String(installedVersion || '').includes(targetVersion)
}
const execFileAsync = promisify(execFile)
const DETECTION_CACHE_TTL_MS = 30000
@@ -99,50 +110,17 @@ const NPM_RELEASES = Object.freeze({
const ALLOWED_SCRIPT_URLS = new Set(Object.values(HERMES_RECIPES).map(recipe => recipe.url))
const AGENT_CATALOG = Object.freeze([
{
kind: 'codex', label: 'Codex', recommended: false, providerCompatible: false,
providerSupport: 'responses-required',
},
{
kind: 'claude', label: 'Claude Code', recommended: false, providerCompatible: false,
providerSupport: 'anthropic-required',
},
{
kind: 'hermes', label: 'Hermes', recommended: true, providerCompatible: true,
providerSupport: 'supported',
},
{
kind: 'workbuddy', label: 'WorkBuddy', recommended: true, providerCompatible: true,
providerSupport: 'experimental',
},
{
kind: 'openclaw', label: 'OpenClaw', recommended: true, providerCompatible: true,
providerSupport: 'supported',
},
{
kind: 'pi', label: 'Pi Agent', recommended: false, providerCompatible: false,
providerSupport: 'native-config',
},
{
kind: 'opencode', label: 'OpenCode', recommended: false, providerCompatible: false,
providerSupport: 'native-config',
},
{
kind: 'opencodereview', label: 'OpenCodeReview', recommended: false, providerCompatible: true,
providerSupport: 'supported',
},
{
kind: 'kimi', label: 'Kimi Code', recommended: false, providerCompatible: false,
providerSupport: 'native-config',
},
{
kind: 'mimo', label: 'MiMo Code', recommended: false, providerCompatible: false,
providerSupport: 'native-config',
},
{
kind: 'qwen', label: 'Qwen Code', recommended: false, providerCompatible: true,
providerSupport: 'supported',
},
{ kind: 'codex', label: 'Codex', recommended: false },
{ kind: 'claude', label: 'Claude Code', recommended: false },
{ kind: 'hermes', label: 'Hermes', recommended: true },
{ kind: 'workbuddy', label: 'WorkBuddy', recommended: true },
{ kind: 'openclaw', label: 'OpenClaw', recommended: true },
{ kind: 'pi', label: 'Pi Agent', recommended: false },
{ kind: 'opencode', label: 'OpenCode', recommended: false },
{ kind: 'opencodereview', label: 'OpenCodeReview', recommended: false },
{ kind: 'kimi', label: 'Kimi Code', recommended: false },
{ kind: 'mimo', label: 'MiMo Code', recommended: false },
{ kind: 'qwen', label: 'Qwen Code', recommended: false },
])
function installRecipe(kind, platform) {
@@ -372,6 +350,7 @@ function verifiedRecipeAgent(agent, recipe) {
}
module.exports = {
installedVersionCurrent,
AGENT_CATALOG,
DETECTION_CACHE_TTL_MS,
abortError,
@@ -3,6 +3,7 @@ const fs = require('node:fs')
const { randomUUID } = require('node:crypto')
const { DISPLAY_LIMIT, listLocalAgentSkills } = require('../../skills/local-skill-catalog.cjs')
const {
installedVersionCurrent,
AGENT_CATALOG,
DETECTION_CACHE_TTL_MS,
abortError,
@@ -169,7 +170,8 @@ class AgentInstaller extends EventEmitter {
const recipe = installRecipe(profile.kind, this.platform)
const targetVersion = recipe?.detectedVersion || recipe?.version || ''
const currentVersion = String(agent?.version || '')
const upToDate = Boolean(agent && targetVersion && currentVersion.includes(targetVersion))
const upToDate = Boolean(agent && targetVersion
&& installedVersionCurrent(currentVersion, targetVersion))
let installSupported = Boolean(recipe)
let installErrorCode = ''
if (!recipe) {
@@ -241,7 +243,7 @@ class AgentInstaller extends EventEmitter {
const current = installed.find(agent => agent.kind === profile.kind)
const targetVersion = recipe.detectedVersion || recipe.version
if (current && (current.compatibilityState !== 'compatible'
|| String(current.version || '').includes(targetVersion))) {
|| installedVersionCurrent(current.version, targetVersion))) {
throw installerError('INSTALL_AGENT_ALREADY_INSTALLED')
}
let command = recipe.interpreter || ''
+58 -17
View File
@@ -7,6 +7,8 @@ const { promisify } = require('node:util')
const { prepareCommand, searchPath } = require('./cli/cli-adapters.cjs')
const { isOpenClawSecretReference } = require('./cli/openclaw-runtime.cjs')
const { NETWORK_ENV_KEYS, networkEnvironment } = require('./cli/cli-network-environment.cjs')
const { workBuddyCredentialState } = require('./cli/workbuddy-runtime.cjs')
const { mimoConfiguredProvider } = require('./cli/mimo-native-config.cjs')
const execFileAsync = promisify(execFile)
@@ -29,13 +31,20 @@ const OPENCLAW_MODEL_INPUTS = new Set(['text', 'image', 'audio', 'video'])
const MAX_CREDENTIAL_FILE_BYTES = 2 * 1024 * 1024
const MAX_SHELL_ENV_BYTES = 256 * 1024
const SHELL_ENV_CACHE_TTL_MS = 30000
// A login shell that loads nvm/conda/compinit can take several seconds,
// especially right after boot or under load.
const SHELL_ENV_TIMEOUT_MS = 10000
// When the shell does not answer and nothing better is known, retry soon
// instead of pinning the bare app environment for the whole cache window.
const SHELL_ENV_FAILURE_TTL_MS = 5000
const SHELL_ENV_MARKER = '__MELDWORK_NATIVE_ENV_V1__'
const VERTEX_MODEL_REGION_KEY = /^VERTEX_REGION_[A-Z0-9_]{1,120}$/
const CREDENTIAL_ENV_KEYS = Object.freeze({
codex: ['OPENAI_API_KEY'],
hermes: ['OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'ANTHROPIC_API_KEY'],
openclaw: ['OPENAI_API_KEY', 'OPENROUTER_API_KEY'],
workbuddy: ['CODEBUDDY_API_KEY', 'OPENAI_API_KEY', 'ANTHROPIC_API_KEY'],
workbuddy: ['CODEBUDDY_API_KEY', 'CODEBUDDY_AUTH_TOKEN'],
zcode: ['ANTHROPIC_API_KEY'],
pi: ['PI_API_KEY', 'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'OPENROUTER_API_KEY', 'GEMINI_API_KEY'],
kimi: ['MOONSHOT_API_KEY', 'KIMI_API_KEY', 'KIMI_MODEL_API_KEY', 'OPENAI_API_KEY'],
mimo: ['MIMO_API_KEY'],
@@ -49,18 +58,19 @@ const GOOGLE_CLOUD_ENV_KEYS = [
'GOOGLE_APPLICATION_CREDENTIALS', 'GOOGLE_CLOUD_PROJECT', 'GCLOUD_PROJECT',
'GOOGLE_CLOUD_LOCATION', 'GOOGLE_CLOUD_QUOTA_PROJECT', 'CLOUDSDK_CONFIG',
]
const NATIVE_CONFIG_ROOT_KEYS = new Set(['CODEX_HOME', 'CLAUDE_CONFIG_DIR', 'PI_CODING_AGENT_DIR'])
const RUNTIME_ENV_KEYS = Object.freeze({
codex: ['OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL', 'CODEX_HOME'],
hermes: [
'OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'ANTHROPIC_API_KEY',
'OPENAI_BASE_URL', 'OPENAI_MODEL', 'HERMES_INFERENCE_PROVIDER', 'HERMES_INFERENCE_MODEL',
'HERMES_HOME',
],
openclaw: ['OPENAI_API_KEY', 'OPENROUTER_API_KEY'],
workbuddy: [
'CODEBUDDY_API_KEY', 'CODEBUDDY_BASE_URL', 'CODEBUDDY_MODEL',
'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL', 'ANTHROPIC_API_KEY',
'CODEBUDDY_API_KEY', 'CODEBUDDY_AUTH_TOKEN', 'CODEBUDDY_BASE_URL', 'CODEBUDDY_MODEL',
'CODEBUDDY_CONFIG_DIR',
],
zcode: ['ANTHROPIC_API_KEY'],
pi: [
'PI_API_KEY', 'PI_BASE_URL', 'PI_MODEL',
'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL', 'ANTHROPIC_API_KEY',
@@ -69,9 +79,12 @@ const RUNTIME_ENV_KEYS = Object.freeze({
kimi: [
'MOONSHOT_API_KEY', 'KIMI_API_KEY', 'KIMI_MODEL_API_KEY',
'KIMI_MODEL_BASE_URL', 'KIMI_MODEL_NAME',
'KIMI_MODEL_PROVIDER_TYPE', 'KIMI_MODEL_CAPABILITIES',
'KIMI_MODEL_MAX_CONTEXT_SIZE', 'KIMI_MODEL_MAX_OUTPUT_SIZE',
'KIMI_CODE_HOME',
'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL',
],
mimo: ['MIMO_API_KEY', 'MIMO_BASE_URL', 'MIMO_MODEL'],
mimo: ['MIMO_API_KEY', 'MIMO_BASE_URL', 'MIMO_MODEL', 'MIMOCODE_CONFIG_DIR', 'MIMOCODE_CONFIG_CONTENT'],
claude: [
'ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN', 'CLAUDE_CODE_OAUTH_TOKEN',
'ANTHROPIC_BASE_URL', 'ANTHROPIC_MODEL', 'CLAUDE_CONFIG_DIR',
@@ -89,11 +102,12 @@ const RUNTIME_ENV_KEYS = Object.freeze({
'AZURE_CLIENT_CERTIFICATE_PATH', 'AZURE_CLIENT_CERTIFICATE_PASSWORD', 'AZURE_FEDERATED_TOKEN_FILE',
'AZURE_CONFIG_DIR',
],
qwen: ['DASHSCOPE_API_KEY', 'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL'],
gemini: ['GEMINI_API_KEY', 'GOOGLE_API_KEY', 'GEMINI_MODEL', ...GOOGLE_CLOUD_ENV_KEYS],
qwen: ['DASHSCOPE_API_KEY', 'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL', 'QWEN_HOME'],
gemini: ['GEMINI_API_KEY', 'GOOGLE_API_KEY', 'GEMINI_MODEL', 'GOOGLE_GEMINI_BASE_URL', ...GOOGLE_CLOUD_ENV_KEYS],
opencode: [
'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'OPENROUTER_API_KEY',
'OPENAI_BASE_URL', 'OPENAI_MODEL',
'OPENCODE_CONFIG', 'OPENCODE_CONFIG_DIR', 'OPENCODE_CONFIG_CONTENT',
],
opencodereview: [
'OCR_LLM_TOKEN', 'OCR_LLM_URL', 'OCR_LLM_MODEL', 'OCR_USE_ANTHROPIC',
@@ -159,12 +173,11 @@ function textContainsCredential(filename) {
}
}
function nativeCredentialEnvironment(kind, env = process.env, options = {}) {
function nativeCredentialEnvironment(kind, env = process.env) {
const result = {}
const keys = [...(RUNTIME_ENV_KEYS[kind] || []), ...(kind === 'claude'
? Object.keys(env).filter(key => VERTEX_MODEL_REGION_KEY.test(key)) : [])]
for (const key of keys) {
if (options.providerConfigured && !NATIVE_CONFIG_ROOT_KEYS.has(key)) continue
if (typeof env[key] === 'string') result[key] = env[key]
}
return result
@@ -259,7 +272,7 @@ async function queryNativeShellEnvironment(options = {}) {
shell,
[shellName === 'sh' ? '-lc' : '-lic', nativeShellCommand(shellName)],
{
timeout: 5000,
timeout: SHELL_ENV_TIMEOUT_MS,
maxBuffer: MAX_SHELL_ENV_BYTES,
windowsHide: true,
env: childEnv,
@@ -271,13 +284,19 @@ async function queryNativeShellEnvironment(options = {}) {
source: Object.keys(loaded).some(key => key !== 'PATH') ? 'native-shell' : 'process',
}
} catch {
return { env: fallback, source: 'process' }
return { env: fallback, source: 'process', failed: true }
}
}
function publicShellEnvironment({ env, source }) {
return { env, source }
}
let lastGoodShellEnvironment = null
function resolveNativeShellEnvironment(options = {}) {
const useCache = options.cache !== false && !options.execFileFn
if (!useCache) return queryNativeShellEnvironment(options)
if (!useCache) return queryNativeShellEnvironment(options).then(publicShellEnvironment)
const source = options.env || process.env
const platform = options.platform || process.platform
const home = options.home || os.homedir()
@@ -290,12 +309,31 @@ function resolveNativeShellEnvironment(options = {}) {
&& shellEnvironmentCache.expiresAt > now) {
return shellEnvironmentCache.promise
}
const promise = queryNativeShellEnvironment(options)
shellEnvironmentCache = { key, expiresAt: now + SHELL_ENV_CACHE_TTL_MS, promise }
return promise
// The window is measured from when the capture finishes, so a capture that
// used up its whole timeout is not already stale when it returns.
const entry = { key, promise: null, expiresAt: now + SHELL_ENV_CACHE_TTL_MS }
entry.promise = queryNativeShellEnvironment(options).then((result) => {
entry.expiresAt = Date.now() + SHELL_ENV_CACHE_TTL_MS
if (!result.failed) {
if (result.source === 'native-shell') lastGoodShellEnvironment = { key, result }
return publicShellEnvironment(result)
}
// The login shell did not answer (slow rc file, timeout, startup error).
// The bare app environment has none of the user's PATH entries or API
// keys, so an Agent started with it fails with misleading sign-in errors.
// Keep using what the shell last produced for this account instead.
if (lastGoodShellEnvironment?.key === key) {
return publicShellEnvironment(lastGoodShellEnvironment.result)
}
entry.expiresAt = Date.now() + SHELL_ENV_FAILURE_TTL_MS
return publicShellEnvironment(result)
})
shellEnvironmentCache = entry
return entry.promise
}
function nativeCredentialState(kind, options = {}) {
if (kind === 'workbuddy') return workBuddyCredentialState(options)
const home = options.home || os.homedir()
const env = options.env || process.env
const codexHome = env.CODEX_HOME || path.join(home, '.codex')
@@ -309,10 +347,10 @@ function nativeCredentialState(kind, options = {}) {
}
const jsonFiles = {
zcode: [path.join(home, '.zcode', 'cli', 'config.json')],
codex: [path.join(codexHome, 'auth.json'), path.join(codexHome, 'config.json')],
hermes: [path.join(home, '.hermes', 'auth.json'), path.join(home, '.hermes', 'config.json')],
openclaw: [path.join(home, '.openclaw', 'openclaw.json')],
workbuddy: [path.join(home, '.workbuddy', 'models.json')],
pi: [
path.join(piHome, 'auth.json'),
path.join(piHome, 'models.json'),
@@ -347,7 +385,6 @@ function nativeCredentialState(kind, options = {}) {
path.join(home, '.hermes', 'config.yml'),
],
openclaw: [path.join(home, '.openclaw', '.env')],
workbuddy: [path.join(home, '.workbuddy', '.env')],
pi: [
path.join(home, '.pi', '.env'),
path.join(home, '.pi', 'config.toml'),
@@ -716,8 +753,12 @@ async function queryMimoAuthState(options = {}) {
}
async function resolveNativeCredentialState(kind, options = {}) {
if (kind === 'workbuddy') return workBuddyCredentialState(options)
const current = nativeCredentialState(kind, options)
if (kind === 'mimo') {
if (current.state === 'ready' || mimoConfiguredProvider(options)) {
return { state: 'ready', source: 'native-credential' }
}
return await queryMimoAuthState(options) || current
}
if (kind === 'openclaw') {
@@ -12,6 +12,15 @@ const MAX_ATTACHMENT_DISCARD_REQUEST = 32
function attachmentIdsFromSnapshot(snapshot, groupId = '') {
const ids = []
for (const task of snapshot?.deliveryTasks || []) {
if (groupId && task.groupId !== groupId) continue
for (const version of task.versions) {
for (const attachment of version.attachments) ids.push(attachment.id)
}
for (const attempt of task.attempts) {
for (const source of attempt.sources) if (source.attachmentId) ids.push(source.attachmentId)
}
}
for (const message of Array.isArray(snapshot?.messages) ? snapshot.messages : []) {
if (groupId && message?.groupId !== groupId) continue
for (const attachment of Array.isArray(message?.attachments) ? message.attachments : []) {
@@ -0,0 +1,81 @@
const { V4_RECEIPT_MARKER_PREFIX } = require('./collaboration-receipt.cjs')
// Control markers an Agent appends to its answer. They steer the harness and
// must never reach the user's screen, even while the answer is still streaming.
const CONSENSUS_MARKERS = Object.freeze([
'[[MELDWORK_CONSENSUS:agree]]',
'[[MELDWORK_CONSENSUS:continue]]',
])
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
// Length of the longest buffer suffix that could still grow into a marker.
function heldSuffixLength(buffer, markers) {
let hold = 0
for (const marker of markers) {
for (let size = Math.min(marker.length - 1, buffer.length); size > hold; size -= 1) {
if (buffer.slice(-size).toLowerCase() === marker.slice(0, size).toLowerCase()) {
hold = size
break
}
}
}
return hold
}
// Streams answer deltas through a marker filter.
// plain nothing to hide: deltas pass through untouched.
// auto hide the legacy consensus markers.
// receipt hide the consensus markers and everything from the start of the
// collaboration receipt on (the receipt is parsed from the final text).
// `push` returns the text that is safe to show now; text that might be the
// start of a marker is held back until the next delta settles it, and
// `flush` releases what is left once the answer has finished.
function createAnswerMarkerFilter({ mode = 'plain' } = {}) {
if (mode === 'plain') {
return { push: delta => String(delta || ''), flush: () => '' }
}
const hideReceipt = mode === 'receipt'
const markers = hideReceipt
? [...CONSENSUS_MARKERS, V4_RECEIPT_MARKER_PREFIX]
: [...CONSENSUS_MARKERS]
const pattern = new RegExp(markers.map(escapeRegExp).join('|'), 'i')
let buffer = ''
let receiptStarted = false
return {
push(delta) {
if (receiptStarted) return ''
buffer += String(delta || '')
let visible = ''
while (buffer && !receiptStarted) {
const match = pattern.exec(buffer)
if (match) {
visible += buffer.slice(0, match.index)
buffer = buffer.slice(match.index + match[0].length)
if (hideReceipt
&& match[0].toLowerCase() === V4_RECEIPT_MARKER_PREFIX.toLowerCase()) {
receiptStarted = true
}
continue
}
const hold = heldSuffixLength(buffer, markers)
visible += hold ? buffer.slice(0, -hold) : buffer
buffer = hold ? buffer.slice(-hold) : ''
break
}
return visible
},
flush(finalText = '') {
const rest = buffer
buffer = ''
if (!hideReceipt) return rest
// A held fragment is real answer text only when the final answer ends
// with it; otherwise it was the start of the (hidden) receipt.
return !receiptStarted && rest && String(finalText).endsWith(rest) ? rest : ''
},
}
}
module.exports = { CONSENSUS_MARKERS, createAnswerMarkerFilter }
@@ -0,0 +1,295 @@
const { canonicalJson } = require('./context-pack-records.cjs')
const { parseLenientTaskDecision } = require('./task-decision.cjs')
const SHA256 = /^[a-f0-9]{64}$/
const V4_AGENT_PHASES = new Set([
'proposal', 'discussion', 'challenge', 'work', 'synthesis', 'verification',
])
const V4_RECEIPT_MARKER_PREFIX = '[[MELDWORK_COLLABORATION'
const V4_RECEIPT_MARKER = /\s*\[\[MELDWORK_COLLABORATION(?:_JSON)?:([\s\S]*?)\]\]/i
const V4_RECEIPT_BLOCK = /\s*\[\[MELDWORK_COLLABORATION(?:_JSON)?\]\]([\s\S]*?)\[\[\/MELDWORK_COLLABORATION(?:_JSON)?\]\]/i
const LEGACY_CONSENSUS_MARKER = /^\s*\[\[MELDWORK_CONSENSUS:(?:agree|continue)\]\]\s*$/gim
const V4_RECEIPT_MAX_TEXT = 800
const V4_RECEIPT_MAX_ITEMS = 16
const V4_RECEIPT_MAX_BYTES = 16 * 1024
function isV4InternalProtocolCommentary(value) {
const text = String(value || '').replace(/\s+/g, ' ').trim()
if (!text || text.length > 4000 || /\[\[MELDWORK_COLLABORATION/i.test(text)) return false
const mentionsControl = /(?:\breceipt\b|structured marker|MELDWORK framework|ExitPlanMode|\u7ed3\u6784\u5316\s*receipt|receipt\s*\u6807\u8bb0)/i.test(text)
const describesInternalProcess = /(?:coordination\/self-introduction|not a code implementation task|no plan file|implementation planning|plan mode|waiting for (?:the )?(?:next|subsequent).*(?:phase|instruction)|\u534f\u4f5c\u534f\u8c03\u7c7b\u6587\u672c\u4ea7\u51fa|\u975e\u4ee3\u7801\u5b9e\u73b0\u4efb\u52a1|\u6267\u884c\u8ba1\u5212\u6d41\u7a0b|\u4e0d\u8c03\u7528\s*ExitPlanMode|\u672c\u8f6e\u4ea4\u4ed8|\u7b49\u5f85\u540e\u7eed.*(?:\u9636\u6bb5|\u6307\u4ee4))/i.test(text)
return mentionsControl && describesInternalProcess
}
function stripV4InternalProtocolTail(value) {
const paragraphs = String(value || '').trim().split(/\n{2,}/)
while (paragraphs.length && isV4InternalProtocolCommentary(paragraphs.at(-1))) {
paragraphs.pop()
}
return paragraphs.join('\n\n').trim()
}
function boundedReceiptText(value, limit = V4_RECEIPT_MAX_TEXT) {
if (typeof value !== 'string' || value.trim().length > limit) return ''
const text = cleanReceiptText(value, limit)
return text && !text.includes('\u0000') ? text : ''
}
function boundedReceiptList(value) {
if (value == null) return []
if (!Array.isArray(value) || value.length > V4_RECEIPT_MAX_ITEMS) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
const result = value.map(item => boundedReceiptText(item))
if (result.some(item => !item)) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
return result
}
// A decision that is still unreadable after normalisation is treated as no
// decision: the orchestrator then asks the owner again instead of failing
// the Agent and removing it from the discussion.
function leniencyTaskDecision(value, summary) {
try {
return { taskDecision: parseLenientTaskDecision(value, { fallbackReason: summary }) }
} catch {
return {}
}
}
function parseV4CollaborationReceiptStrict(result, required = false, expectedPhase = '', options = {}) {
const rawText = typeof result?.text === 'string' ? result.text : ''
if (!required) return { text: rawText, collaboration: null }
let text = rawText
let receipt = result?.collaboration
// Natural discussion turns are free-form answers; a slightly malformed
// receipt there must not fail the turn and end the discussion. Strict
// phased-V4 receipts keep failing closed.
const lenient = expectedPhase === 'discussion'
let trailingVisibleText = ''
let unreadableReceipt = false
const block = rawText.match(V4_RECEIPT_BLOCK) || rawText.match(V4_RECEIPT_MARKER)
if (block) {
const markerStart = Number(block.index) || 0
const suffix = rawText.slice(markerStart + block[0].length).trim()
// A closing code fence after the receipt is formatting, not content.
if (suffix && !isV4InternalProtocolCommentary(suffix) && !/^[\s`~]*$/.test(suffix)) {
if (!lenient) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
trailingVisibleText = suffix
}
const encoded = String(block[1] || '').trim()
if (!encoded || Buffer.byteLength(encoded) > V4_RECEIPT_MAX_BYTES) {
if (!lenient) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
unreadableReceipt = true
}
if (!unreadableReceipt) {
try {
let parsed
try {
parsed = JSON.parse(encoded)
} catch (error) {
if (!lenient) throw error
// Common hand-written slips: smart quotes and trailing commas.
parsed = JSON.parse(encoded
.replace(/[\u201c\u201d]/g, '"')
.replace(/[\u2018\u2019]/g, "'")
.replace(/,\s*([}\]])/g, '$1'))
}
if (receipt != null && canonicalJson(receipt) !== canonicalJson(parsed)) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_MISMATCH')
}
receipt = parsed
} catch (error) {
if (error?.message === 'LOCAL_RUN_COLLABORATION_RECEIPT_MISMATCH') throw error
if (!lenient) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
unreadableReceipt = true
}
}
text = rawText.slice(0, markerStart) + (trailingVisibleText ? `\n\n${trailingVisibleText}` : '')
} else if (/\[\[MELDWORK_COLLABORATION/i.test(rawText)) {
if (!lenient) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
unreadableReceipt = true
text = rawText.slice(0, rawText.search(/\[\[MELDWORK_COLLABORATION/i))
}
if (unreadableReceipt) receipt = null
if (lenient && receipt && typeof receipt === 'object' && !Array.isArray(receipt)) {
// Keep only discussion fields and fill a missing summary, so an extra key
// or a long summary does not discard the Agent's whole contribution.
const decisionReason = typeof receipt.taskDecision?.reason === 'string'
? receipt.taskDecision.reason
: ''
const summary = typeof receipt.summary === 'string' && receipt.summary.trim()
? receipt.summary.trim().slice(0, V4_RECEIPT_MAX_TEXT)
: (decisionReason.trim().slice(0, V4_RECEIPT_MAX_TEXT) || 'Discussion contribution')
receipt = {
version: 1,
phase: 'discussion',
summary,
...(receipt.taskDecision != null ? { taskDecision: receipt.taskDecision } : {}),
}
}
text = stripV4InternalProtocolTail(
text.replace(LEGACY_CONSENSUS_MARKER, '').replace(/\n{3,}/g, '\n\n'),
)
if (receipt == null && options.allowMissingV4Receipt === true && text) {
return { text, collaboration: null }
}
if (receipt == null && expectedPhase === 'proposal'
&& options.allowMissingProposalReceipt === true && text) {
receipt = {
version: 1,
phase: 'proposal',
summary: 'The Agent returned a visible proposal without a structured receipt.',
capabilities: ['Delivered a user-facing proposal'],
intendedWork: ['Addressed the current user task'],
deliverables: ['Visible Agent response'],
dependencies: [],
}
}
if (receipt == null) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_REQUIRED')
if (!receipt || typeof receipt !== 'object' || Array.isArray(receipt)) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
try {
if (Buffer.byteLength(canonicalJson(receipt)) > V4_RECEIPT_MAX_BYTES) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
} catch (error) {
if (error?.message === 'LOCAL_RUN_COLLABORATION_RECEIPT_INVALID') throw error
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
const phase = String(expectedPhase || '')
const summary = boundedReceiptText(receipt.summary)
const allowed = new Set([
'version', 'phase', 'summary',
...(phase === 'discussion' ? ['taskDecision'] : []),
...(['challenge', 'verification'].includes(phase) ? ['verdict'] : []),
...(phase === 'proposal'
? ['capabilities', 'intendedWork', 'deliverables', 'dependencies'] : []),
...(phase === 'challenge'
? [
'proposedAssignments', 'finalizerKind', 'verifierKinds',
'supportedPlanHash', 'agreeToPlan',
] : []),
...(phase === 'work' ? ['workItemId', 'deliverables'] : []),
...(phase === 'synthesis' ? ['resolvedIssueIds'] : []),
])
if (!V4_AGENT_PHASES.has(phase) || !summary
|| Object.keys(receipt).some(key => !allowed.has(key))
|| (receipt.version != null && receipt.version !== 1)
|| (receipt.phase != null && receipt.phase !== phase)) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
if (['challenge', 'verification'].includes(phase)
&& !['support', 'contradict'].includes(receipt.verdict)) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
const resolvedIssueIds = phase === 'synthesis'
? boundedReceiptList(receipt.resolvedIssueIds)
: null
if (phase === 'synthesis' && !Array.isArray(receipt.resolvedIssueIds)) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
if (phase === 'challenge' && receipt.supportedPlanHash != null
&& !SHA256.test(String(receipt.supportedPlanHash))) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
const proposalList = (field, requireItem = false) => {
if (!Array.isArray(receipt[field])) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
const list = boundedReceiptList(receipt[field])
if (requireItem && list.length === 0) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
return list
}
const boundedList = value => {
if (value == null) return []
if (!Array.isArray(value) || value.length > V4_RECEIPT_MAX_ITEMS) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
}
const result = value.map(item => boundedReceiptText(item))
if (result.some(item => !item)) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
return result
}
const normalized = {
version: 1,
phase,
summary,
...(phase === 'discussion' && receipt.taskDecision != null
? leniencyTaskDecision(receipt.taskDecision, summary) : {}),
...(['challenge', 'verification'].includes(phase) ? { verdict: receipt.verdict } : {}),
...(phase === 'proposal' ? {
capabilities: proposalList('capabilities', true),
intendedWork: proposalList('intendedWork', true),
deliverables: proposalList('deliverables', true),
dependencies: proposalList('dependencies'),
} : {}),
...(phase === 'challenge' ? {
...(Array.isArray(receipt.proposedAssignments)
? {
proposedAssignments: receipt.proposedAssignments.map(assignment => (
assignment && typeof assignment === 'object' && !Array.isArray(assignment)
&& assignment.role === 'finalizer'
? { ...assignment, role: 'integrator' }
: assignment
)),
}
: {}),
...(receipt.finalizerKind ? { finalizerKind: boundedReceiptText(receipt.finalizerKind, 120) } : {}),
...(Array.isArray(receipt.verifierKinds) ? { verifierKinds: boundedList(receipt.verifierKinds) } : {}),
...(receipt.supportedPlanHash ? { supportedPlanHash: receipt.supportedPlanHash } : {}),
...(typeof receipt.agreeToPlan === 'boolean' ? { agreeToPlan: receipt.agreeToPlan } : {}),
} : {}),
...(phase === 'work' ? {
...(receipt.workItemId ? { workItemId: boundedReceiptText(receipt.workItemId, 120) } : {}),
deliverables: boundedList(receipt.deliverables),
} : {}),
...(phase === 'synthesis' ? { resolvedIssueIds } : {}),
}
return { text, collaboration: Object.freeze(JSON.parse(canonicalJson(normalized))) }
}
function parseV4CollaborationReceipt(result, required = false, expectedPhase = '', options = {}) {
try {
return parseV4CollaborationReceiptStrict(result, required, expectedPhase, options)
} catch (error) {
const code = String(error?.message || '')
if (expectedPhase !== 'proposal'
|| options.allowMissingProposalReceipt !== true
|| !['LOCAL_RUN_COLLABORATION_RECEIPT_INVALID', 'LOCAL_RUN_COLLABORATION_RECEIPT_REQUIRED']
.includes(code)) {
throw error
}
const rawText = typeof result?.text === 'string' ? result.text : ''
const markerIndex = rawText.search(/\[\[MELDWORK_COLLABORATION/i)
const visibleText = (markerIndex >= 0 ? rawText.slice(0, markerIndex) : rawText)
.replace(LEGACY_CONSENSUS_MARKER, '')
.replace(/\n{3,}/g, '\n\n')
.trim()
if (!visibleText) throw error
return {
text: visibleText,
collaboration: Object.freeze({
version: 1,
phase: 'proposal',
summary: 'The Agent returned a visible proposal without a valid structured receipt.',
capabilities: ['Delivered a user-facing proposal'],
intendedWork: ['Addressed the current user task'],
deliverables: ['Visible Agent response'],
dependencies: [],
}),
}
}
}
function cleanReceiptText(value, limit) {
return String(value || '').trim().slice(0, limit)
}
module.exports = {
V4_AGENT_PHASES,
V4_RECEIPT_MARKER_PREFIX,
parseV4CollaborationReceipt,
}
@@ -533,6 +533,37 @@ function publicCollaborationText(value, limit = MAX_TEXT_CHARS) {
.slice(0, limit)
}
const PRIVATE_FILESYSTEM_ROOT = /^\/(?:Users|home|private|var|tmp|etc|opt|Volumes|root|usr|System|Library|Applications|mnt|srv)(?:\/|$)/
function peerPathText(candidate, workspaceRoot) {
const trailing = candidate.match(/[.,;:!?]+$/)?.[0] || ''
const clean = trailing ? candidate.slice(0, -trailing.length) : candidate
if (workspaceRoot && (clean === workspaceRoot || clean.startsWith(`${workspaceRoot}/`))) {
return `${clean === workspaceRoot ? '.' : clean.slice(workspaceRoot.length + 1)}${trailing}`
}
if (PRIVATE_FILESYSTEM_ROOT.test(clean)) return `[path]${trailing}`
// Routes and commands such as /api/users or /login are not local files.
return candidate
}
// Peer-visible text for natural discussion transcripts. Unlike
// publicCollaborationText, files inside the shared workspace stay readable as
// workspace-relative paths and non-filesystem tokens (API routes, slash
// commands) are kept; other absolute local paths and secrets stay redacted.
function peerCollaborationText(value, options = {}) {
const workspaceRoot = typeof options.workspaceRoot === 'string'
? options.workspaceRoot.replace(/\/+$/, '')
: ''
return redactSecrets(String(value || ''))
.replace(/\bfile:\/\/(\/[^\s"'`<>]+)/giu, (_match, filePath) => peerPathText(filePath, workspaceRoot))
.replace(/(^|[\s("'`])(\/(?!\/)[^\s"'`<>)]*)/gmu, (_match, lead, candidate) => (
`${lead}${peerPathText(candidate, workspaceRoot)}`
))
.replace(/\b[A-Za-z]:\\(?:[^\s"'`<>]+\\)*[^\s"'`<>]*/gu, '[path]')
.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, '')
.trim()
}
function collaborationPackageText(handoff, entries) {
const lines = [
'MELDWORK_COLLABORATION_PACKAGE_V1',
@@ -557,6 +588,7 @@ function collaborationPackageText(handoff, entries) {
}
module.exports = {
peerCollaborationText,
BLACKBOARD_ENTRY_VERSION,
COLLABORATION_STATE_VERSION,
HANDOFF_VERSION,
@@ -1601,11 +1601,12 @@ function validateV4Relations(orchestration, targetKinds) {
assignment.readOnly === false
))
const validWriterBinding = writerKind
? writableSlots.length === 1
&& writableAssignments.length === 1
&& writableSlots[0].agentKind === writerKind
&& writableAssignments[0].agentKind === writerKind
&& writableAssignments[0].slotId === writableSlots[0].slotId
? writableSlots.some(slot => slot.agentKind === writerKind)
&& writableSlots.length === writableAssignments.length
&& writableSlots.every(slot => writableAssignments.some(assignment => (
assignment.slotId === slot.slotId && assignment.agentKind === slot.agentKind
&& (writableSlots.length === 1 || assignment.role === 'writer')
)))
: writableSlots.length === 0 && writableAssignments.length === 0
if (!validWriterBinding) fail('ORCHESTRATION_V4_PERMISSION_INVALID')
}
@@ -1618,12 +1619,15 @@ function validateV4Relations(orchestration, targetKinds) {
|| orchestration.coordinationPlan?.finalizerKind
|| orchestration.commitState.writerKind
|| orchestration.discussionWriterKind
if (writableSlots.length > 1 || writableSlots.some(slot => (
const nativeWrites = Boolean(orchestration.discussionWriterKind
&& !orchestration.coordinationPlan && !orchestration.synthesisBinding
&& ['proposal', 'discussion'].includes(orchestration.phase))
if (!nativeWrites && (writableSlots.length > 1 || writableSlots.some(slot => (
slot.agentKind !== effectiveWriterKind
|| !['discussion', 'synthesis', 'human-gate'].includes(orchestration.phase)
|| (orchestration.phase === 'discussion' && !orchestration.discussionWriterKind)
))) fail('ORCHESTRATION_V4_PERMISSION_INVALID')
for (const assignment of orchestration.phase === 'discussion' ? orchestration.plan.assignments : []) {
)))) fail('ORCHESTRATION_V4_PERMISSION_INVALID')
for (const assignment of ['proposal', 'discussion'].includes(orchestration.phase) ? orchestration.plan.assignments : []) {
const slot = orchestration.slots.find(item => item.slotId === assignment.slotId)
if (slot && assignment.readOnly !== (slot.permission !== 'workspace-write')) {
fail('ORCHESTRATION_V4_PERMISSION_INVALID')
+60 -2
View File
@@ -3,7 +3,7 @@ const { redactSecrets } = require('../security/secret-redaction.cjs')
function parseTaskDecision(input) {
const invalid = () => { throw new Error('LOCAL_RUN_TASK_DECISION_INVALID') }
if (!input || typeof input !== 'object' || Array.isArray(input)
|| Object.keys(input).some(key => !['status', 'reason', 'deliverables', 'handoffTo', 'nextKinds'].includes(key))
|| Object.keys(input).some(key => !['status', 'reason', 'deliverables', 'handoffTo', 'nextKinds', 'reviewedVersion'].includes(key))
|| !['completed', 'continue', 'blocked', 'needs-human'].includes(input.status)) invalid()
const text = (value, limit) => {
if (typeof value !== 'string' || !value.trim() || value.length > limit
@@ -24,11 +24,69 @@ function parseTaskDecision(input) {
|| (input.nextKinds.length > 0 && input.status !== 'continue'))) invalid()
const nextKinds = input.nextKinds?.map(kind => kind.toLowerCase())
if (nextKinds && new Set(nextKinds).size !== nextKinds.length) invalid()
if (input.reviewedVersion != null && (typeof input.reviewedVersion !== 'string'
|| !/^[a-f0-9]{64}$/.test(input.reviewedVersion))) invalid()
return {
status: input.status, reason, deliverables,
...(input.handoffTo != null ? { handoffTo: input.handoffTo } : {}),
...(nextKinds ? { nextKinds } : {}),
...(input.reviewedVersion != null ? { reviewedVersion: input.reviewedVersion } : {}),
}
}
module.exports = { parseTaskDecision }
const STATUS_SYNONYMS = new Map([
['completed', 'completed'], ['complete', 'completed'], ['done', 'completed'],
['finished', 'completed'], ['success', 'completed'], ['succeeded', 'completed'],
['continue', 'continue'], ['continuing', 'continue'], ['in-progress', 'continue'],
['in_progress', 'continue'], ['ongoing', 'continue'], ['pending', 'continue'],
['blocked', 'blocked'], ['blocker', 'blocked'],
['needs-human', 'needs-human'], ['needs_human', 'needs-human'],
['needs human', 'needs-human'], ['needshuman', 'needs-human'], ['human', 'needs-human'],
])
function agentKindToken(value) {
return typeof value === 'string'
? value.trim().replace(/^@+/, '').toLowerCase()
: ''
}
// Agents write task decisions by hand at the end of free text. Small slips
// (capitalised status, "done", an @handle, a single string instead of a list,
// a completed answer without a separate file) used to fail the whole turn
// and silently end the discussion. Normalise them first; the strict parser
// still validates the result.
function normalizeTaskDecision(input, options = {}) {
if (!input || typeof input !== 'object' || Array.isArray(input)) return input
const status = STATUS_SYNONYMS.get(String(input.status || '').trim().toLowerCase())
|| String(input.status || '')
const textList = value => (Array.isArray(value) ? value : (value == null ? [] : [value]))
.filter(item => typeof item === 'string' && item.trim())
.map(item => item.trim().slice(0, 800))
.slice(0, 16)
const reason = typeof input.reason === 'string' && input.reason.trim()
? input.reason.trim().slice(0, 1600)
: String(options.fallbackReason || '').trim().slice(0, 1600) || 'No reason given.'
let deliverables = textList(input.deliverables)
// A completed answer is itself the deliverable when nothing else was named.
if (status === 'completed' && !deliverables.length) deliverables = ['The answer in this message']
const nextKinds = [...new Set(textList(input.nextKinds).map(agentKindToken).filter(Boolean))]
const handoffTo = agentKindToken(input.handoffTo)
return {
status,
reason,
deliverables,
...(status === 'continue' && handoffTo ? { handoffTo } : {}),
// Keep an explicit list (even an empty one) so the stored decision says
// what the Agent wrote; peers can only be requested while continuing.
...(input.nextKinds != null
? { nextKinds: status === 'continue' ? nextKinds.slice(0, 32) : [] }
: {}),
...(input.reviewedVersion != null ? { reviewedVersion: input.reviewedVersion } : {}),
}
}
function parseLenientTaskDecision(input, options = {}) {
return parseTaskDecision(normalizeTaskDecision(input, options))
}
module.exports = { normalizeTaskDecision, parseLenientTaskDecision, parseTaskDecision }
+56 -45
View File
@@ -1,6 +1,8 @@
const electron = require('electron')
const { app, BrowserWindow, dialog, ipcMain, Menu, Notification, protocol, shell } = electron
const fs = require('node:fs')
const path = require('node:path')
const { redactSecrets } = require('./security/secret-redaction.cjs')
const { AttachmentStore } = require('./attachments/attachment-store.cjs')
const { ContentBlobStore } = require('./attachments/content-blob-store.cjs')
const { ContextPackStore } = require('./collaboration/context-pack-store.cjs')
@@ -49,6 +51,7 @@ const {
} = require('./agents/local-agent-readiness.cjs')
const { LocalWorkspace } = require('./workspace/local-workspace.cjs')
const { registerDesktopIpc } = require('./shell/main-ipc.cjs')
const { workBuddyEnvironment, workBuddySettings } = require('./agents/cli/workbuddy-runtime.cjs')
const {
isAllowedExternalUrl,
isAllowedLocalNavigation: isAllowedLocalNavigationForPaths,
@@ -69,26 +72,37 @@ const {
resolveKnowledgeBaseSources,
} = require('./knowledge/local-knowledge-base.cjs')
const { LocalKnowledgeConnectors } = require('./knowledge/local-knowledge-connectors.cjs')
const { ProviderStore } = require('./providers/provider-store.cjs')
const { nativeOpenClawOptions } = require('./agents/cli/openclaw-runtime.cjs')
const { migrateLegacyProductData } = require('./product-data-migration.cjs')
const {
EXTERNAL_PROVIDER_KINDS,
providerAgentKind,
providerOptionsFor,
} = require('./providers/provider-options.cjs')
const LOCAL_AGENT_KINDS = new Set([
'codex', 'hermes', 'openclaw', 'workbuddy', 'pi', 'kimi', 'mimo', 'claude', 'gemini', 'opencode', 'qwen',
'codex', 'hermes', 'openclaw', 'workbuddy', 'zcode', 'pi', 'kimi', 'mimo', 'claude', 'gemini', 'opencode', 'qwen',
'opencodereview',
])
const QUIT_CLEANUP_TIMEOUT_MS = 2500
let mainWindow = null
// Faults outside a request handler are written to a small private log in
// userData (secrets redacted, size-capped) instead of disappearing or
// taking the app down, so instability reports have evidence.
function recordMainProcessFault(kind, detail) {
const text = String(detail?.stack || detail?.message || detail || '').slice(0, 4000)
console.error(`[meldwork] ${kind}`, text)
try {
const file = path.join(app.getPath('userData'), 'meldwork-diagnostics.log')
try {
if (fs.statSync(file).size > 1024 * 1024) fs.renameSync(file, `${file}.1`)
} catch { /* first entry */ }
fs.appendFileSync(file, `${new Date().toISOString()} ${kind} ${redactSecrets(text)}\n`, { mode: 0o600 })
} catch { /* diagnostics must never throw */ }
}
process.on('unhandledRejection', reason => recordMainProcessFault('unhandledRejection', reason))
process.on('uncaughtException', error => recordMainProcessFault('uncaughtException', error))
let workspace = null
let workspaceChangedListener = null
let workspaceRunFinishedListener = null
let workspaceRunEventListener = null
let installer = null
let localAgentRefreshQueue = Promise.resolve()
let providerStore = null
let customAgentStore = null
let agentConnectors = null
let cloudAgentRuntime = null
@@ -225,18 +239,6 @@ function openExternalUrl(value) {
return true
}
function providerOptions(kind, context = {}) {
if (!EXTERNAL_PROVIDER_KINDS.has(kind)) return {}
const status = providerStore?.status(kind) || {}
if (!status.configured && !(kind === 'openclaw' && context.nativeRuntime)) return {}
return providerOptionsFor(
kind,
status.configured ? providerStore.envForAgent(kind) : {},
context,
status,
)
}
async function nativeShellEnvironment(options = {}) {
const home = app.getPath('home')
const resolved = await resolveNativeShellEnvironment({ home, refresh: options.refresh === true })
@@ -341,33 +343,34 @@ async function runCoreAgent(agent, prompt, workdir, options = {}) {
const connectorCredentialIsolation = options.connectorCredentialIsolation === true
const runOptions = { ...options }
delete runOptions.connectorCredentialIsolation
const status = providerStore.status(agent.kind)
if (!connectorCredentialIsolation && status.error) {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
const shellEnvironment = connectorCredentialIsolation ? null : await nativeShellEnvironment()
const nativeRuntime = !connectorCredentialIsolation
&& agent.kind === 'openclaw' && !status.configured
// OpenClaw runs through its isolated native runtime scope; every other Agent
// CLI uses its own native credentials.
const nativeRuntime = !connectorCredentialIsolation && agent.kind === 'openclaw'
? await resolveNativeOpenClawRuntime({
executable: agent.executable,
env: shellEnvironment.env,
home: shellEnvironment.home,
})
: null
const injected = connectorCredentialIsolation
const injected = connectorCredentialIsolation || agent.kind !== 'openclaw' || !nativeRuntime
? {}
: providerOptions(agent.kind, {
...runOptions,
workdir,
: nativeOpenClawOptions({
storageRoot: app.getPath('userData'),
nativeRuntime,
workdir,
sessionRef: runOptions.sessionRef,
allowWrite: runOptions.sandbox === 'workspace-write',
runtime: nativeRuntime,
openclawVersion: agent.version,
})
const nativeEnv = connectorCredentialIsolation || agent.kind === 'openclaw'
? {}
: {
...nativeCredentialEnvironment(agent.kind, shellEnvironment.env, {
providerConfigured: status.configured,
}),
...nativeCredentialEnvironment(agent.kind, shellEnvironment.env),
...(agent.kind === 'workbuddy' ? workBuddyEnvironment({
env: shellEnvironment.env, home: shellEnvironment.home,
modelId: workspace?.state.agentPreferences.workbuddy?.modelId,
}) : {}),
...(shellEnvironment.env.PATH ? { PATH: shellEnvironment.env.PATH } : {}),
}
const callerEnv = connectorCredentialIsolation || agent.kind !== 'openclaw'
@@ -542,19 +545,16 @@ function createWorkspace() {
credentialState: async (kind, agent) => {
if (customAgentStore.has(kind)) return { state: 'ready', source: 'custom-agent' }
if (agentConnectors.has(kind)) return { state: 'ready', source: 'agent-connector' }
if (providerStore.status(kind).error) {
return { state: 'unknown', source: 'provider-credential-unavailable' }
}
const shellEnvironment = await nativeShellEnvironment()
return resolveNativeCredentialState(kind, {
executable: agent?.executable,
env: shellEnvironment.env,
home: shellEnvironment.home,
credentialSource: shellEnvironment.source,
...(kind === 'workbuddy' ? { modelId: workspace?.state.agentPreferences.workbuddy?.modelId } : {}),
})
},
agentLabel: kind => customAgentStore.label(kind) || agentConnectors.label(kind),
sharedProviderReady: kind => EXTERNAL_PROVIDER_KINDS.has(kind) && providerStore.status(kind).configured,
connectorRuntime: agentConnectors,
naturalAgentResponses: true,
runAgent: async (agent, prompt, workdir, options = {}) => {
@@ -702,6 +702,14 @@ function createWindow() {
showOfflinePage(errorCode)
})
mainWindow.webContents.on('did-finish-load', flushPendingOpenGroup)
// A crashed renderer used to leave a blank window; reload it so the
// workspace (whose state lives in the main process) comes back.
mainWindow.webContents.on('render-process-gone', (_event, details) => {
recordMainProcessFault('render-process-gone', details?.reason || 'unknown')
if (details?.reason !== 'clean-exit' && mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.reload()
}
})
mainWindow.on('closed', () => {
mainWindow = null
})
@@ -741,6 +749,10 @@ function registerIpc() {
getCloudAgentBridges: () => cloudAgentBridges,
getMainWindow: () => mainWindow,
getWorkspace: () => workspace,
getWorkBuddySettings: async () => {
const shell = await nativeShellEnvironment()
return workBuddySettings({ ...shell, modelId: workspace?.state.agentPreferences.workbuddy?.modelId })
},
installer,
isShutdownStarted: () => shutdownStarted,
knowledgeBaseStore,
@@ -751,8 +763,6 @@ function registerIpc() {
openExternalUrl,
getOutcomeStore: () => workspace?.outcomeStore,
getSkillTrustStore: () => skillTrustStore,
providerStore,
providerAgentKind,
refreshLocalAgentState,
registerTrustedHandle,
skillCatalog,
@@ -785,11 +795,12 @@ if (!hasSingleInstanceLock) {
storagePath: path.join(app.getPath('userData'), 'meldwork-cloud-agent-bridges.json'),
fetch: globalThis.fetch,
})
providerStore = new ProviderStore({
storagePath: path.join(app.getPath('userData'), 'meldwork-provider.json'),
safeStorage: lazySafeStorage,
allowedKinds: [...LOCAL_AGENT_KINDS],
})
// The shared Provider store was removed; drop any legacy encrypted
// credentials so they do not linger on disk unread.
try {
const { rmSync } = require('node:fs')
rmSync(path.join(app.getPath('userData'), 'meldwork-provider.json'), { force: true })
} catch { /* best-effort cleanup */ }
knowledgeBaseStore = new KnowledgeBaseStore({
storagePath: path.join(app.getPath('userData'), 'meldwork-knowledge-base.json'),
})
+15 -7
View File
@@ -126,6 +126,21 @@ if (isLocalDocument) Object.assign(desktopApi, {
localOutcome: Object.freeze({
recordAdoption: input => ipcRenderer.invoke('local-outcome:record-adoption', input),
}),
localDelivery: Object.freeze({
save: input => ipcRenderer.invoke('local-delivery:save', input),
review: input => ipcRenderer.invoke('local-delivery:review', input),
revise: input => ipcRenderer.invoke('local-delivery:revise', input),
reuse: input => ipcRenderer.invoke('local-delivery:reuse', input),
export: input => ipcRenderer.invoke('local-delivery:export', input),
}),
workBuddy: Object.freeze({
settings: () => ipcRenderer.invoke('local-workbuddy:settings'),
setModel: modelId => ipcRenderer.invoke('local-workbuddy:set-model', modelId),
}),
workPreferences: Object.freeze({
get: () => ipcRenderer.invoke('local-work-preferences:get'),
save: input => ipcRenderer.invoke('local-work-preferences:save', input),
}),
agentInstaller: Object.freeze({
catalog: () => ipcRenderer.invoke('local-agent-installer:catalog'),
skills: kind => ipcRenderer.invoke('local-agent-installer:skills', kind),
@@ -176,13 +191,6 @@ if (isLocalDocument) Object.assign(desktopApi, {
open: id => ipcRenderer.invoke('local-attachments:open', id),
discard: ids => ipcRenderer.invoke('local-attachments:discard', ids),
}),
localAgentProvider: Object.freeze({
status: kind => ipcRenderer.invoke('local-agent-provider:status', kind),
probe: kind => ipcRenderer.invoke('local-agent-provider:probe', kind),
save: (kind, input) => ipcRenderer.invoke('local-agent-provider:save', kind, input),
activate: (kind, preset) => ipcRenderer.invoke('local-agent-provider:activate', kind, preset),
delete: (kind, preset) => ipcRenderer.invoke('local-agent-provider:delete', kind, preset),
}),
localKnowledgeBase: Object.freeze({
status: kind => (kind ? ipcRenderer.invoke('local-knowledge-base:status', kind) : ipcRenderer.invoke('local-knowledge-base:status')),
openGuide: (kind, action) => ipcRenderer.invoke('local-knowledge-base:open-guide', kind, action),
-1
View File
@@ -7,7 +7,6 @@ const MIGRATIONS = Object.freeze([
['workspace.json', 'workspace.json'],
['run-ledger.json', 'run-ledger.json'],
['custom-agents.json', 'custom-agents.json'],
['provider.json', 'provider.json'],
['knowledge-base.json', 'knowledge-base.json'],
['private', 'private'],
])
-184
View File
@@ -1,184 +0,0 @@
const { managedOpenClawOptions, nativeOpenClawOptions } = require('../agents/cli/openclaw-runtime.cjs')
const EXTERNAL_PROVIDER_KINDS = new Set([
'codex', 'hermes', 'openclaw', 'workbuddy', 'pi', 'kimi', 'mimo', 'claude', 'gemini', 'opencode', 'qwen',
'opencodereview',
])
const OFFICIAL_PROVIDER_BASE_URLS = Object.freeze({
codex: 'https://api.openai.com/v1',
hermes: 'https://api.openai.com/v1',
openclaw: 'https://api.openai.com/v1',
workbuddy: '',
pi: '',
kimi: 'https://api.moonshot.cn/v1',
mimo: '',
claude: 'https://api.anthropic.com',
gemini: 'https://generativelanguage.googleapis.com/v1beta',
opencode: '',
qwen: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
opencodereview: 'https://api.openai.com/v1',
})
function chatCompletionsUrl(baseUrl) {
const normalized = String(baseUrl || '').trim().replace(/\/+$/, '')
if (!normalized) return ''
return /\/chat\/completions$/i.test(normalized)
? normalized
: `${normalized}/chat/completions`
}
function providerPresetFromStatus(kind, status = {}) {
if (['official', 'openrouter', 'custom'].includes(status.activePreset)) {
return status.activePreset
}
const provider = String(status.provider || '').trim().toLowerCase()
const baseUrl = String(status.baseUrl || '').trim().replace(/\/+$/, '')
if (/openrouter/i.test(provider) || /openrouter\.ai$/i.test(new URL(baseUrl || 'https://example.com').hostname)) {
return 'openrouter'
}
if (OFFICIAL_PROVIDER_BASE_URLS[kind]
&& baseUrl
&& baseUrl === OFFICIAL_PROVIDER_BASE_URLS[kind]) {
return 'official'
}
return 'custom'
}
function providerOptionsFor(kind, generic, context = {}, status = {}) {
const preset = providerPresetFromStatus(kind, status)
if (kind === 'codex') return { env: generic }
if (kind === 'hermes') {
return {
provider: { id: 'openai-api', model: generic.OPENAI_MODEL },
env: {
...generic,
HERMES_INFERENCE_PROVIDER: 'openai-api',
HERMES_INFERENCE_MODEL: generic.OPENAI_MODEL,
},
}
}
if (kind === 'workbuddy') {
return {
env: {
...generic,
CODEBUDDY_MODEL: generic.OPENAI_MODEL,
CODEBUDDY_API_KEY: generic.OPENAI_API_KEY,
CODEBUDDY_BASE_URL: generic.OPENAI_BASE_URL,
},
}
}
if (kind === 'pi') {
return {
env: {
...generic,
PI_MODEL: generic.OPENAI_MODEL,
PI_API_KEY: generic.OPENAI_API_KEY,
PI_BASE_URL: generic.OPENAI_BASE_URL,
},
}
}
if (kind === 'kimi') {
return {
env: {
...generic,
MOONSHOT_API_KEY: generic.OPENAI_API_KEY,
KIMI_API_KEY: generic.OPENAI_API_KEY,
KIMI_MODEL_API_KEY: generic.OPENAI_API_KEY,
KIMI_MODEL_BASE_URL: generic.OPENAI_BASE_URL,
KIMI_MODEL_NAME: generic.OPENAI_MODEL,
},
}
}
if (kind === 'mimo') {
return {
env: {
...generic,
MIMO_API_KEY: generic.OPENAI_API_KEY,
MIMO_BASE_URL: generic.OPENAI_BASE_URL,
MIMO_MODEL: generic.OPENAI_MODEL,
},
}
}
if (kind === 'claude') {
return {
provider: { id: 'anthropic', model: generic.OPENAI_MODEL, baseUrl: generic.OPENAI_BASE_URL },
env: {
...generic,
ANTHROPIC_API_KEY: generic.OPENAI_API_KEY,
MELDWORK_PROVIDER_API_KEY: generic.OPENAI_API_KEY,
ANTHROPIC_BASE_URL: generic.OPENAI_BASE_URL,
ANTHROPIC_MODEL: generic.OPENAI_MODEL,
},
}
}
if (kind === 'gemini') {
return {
env: {
...generic,
GEMINI_API_KEY: generic.OPENAI_API_KEY,
GOOGLE_API_KEY: generic.OPENAI_API_KEY,
GEMINI_MODEL: generic.OPENAI_MODEL,
},
}
}
if (kind === 'qwen') {
return {
provider: { id: 'openai', model: generic.OPENAI_MODEL },
env: {
...generic,
DASHSCOPE_API_KEY: generic.OPENAI_API_KEY,
},
}
}
if (kind === 'opencode') {
return {
env: {
...generic,
...(preset === 'openrouter' ? { OPENROUTER_API_KEY: generic.OPENAI_API_KEY } : {}),
},
}
}
if (kind === 'opencodereview') {
return {
env: {
...generic,
OCR_LLM_URL: chatCompletionsUrl(generic.OPENAI_BASE_URL),
OCR_LLM_TOKEN: generic.OPENAI_API_KEY,
OCR_LLM_MODEL: generic.OPENAI_MODEL,
OCR_USE_ANTHROPIC: 'false',
},
}
}
if (kind === 'openclaw' && context.storageRoot && context.workdir) {
if (!generic?.OPENAI_API_KEY) {
return nativeOpenClawOptions({
storageRoot: context.storageRoot,
workdir: context.workdir,
sessionRef: context.sessionRef,
allowWrite: context.sandbox === 'workspace-write',
runtime: context.nativeRuntime,
})
}
return managedOpenClawOptions({
storageRoot: context.storageRoot,
workdir: context.workdir,
sessionRef: context.sessionRef,
allowWrite: context.sandbox === 'workspace-write',
provider: generic,
})
}
return {}
}
function providerAgentKind(value) {
const kind = String(value || '').trim()
if (!EXTERNAL_PROVIDER_KINDS.has(kind)) throw new Error('PROVIDER_AGENT_UNSUPPORTED')
return kind
}
module.exports = {
EXTERNAL_PROVIDER_KINDS,
providerAgentKind,
providerOptionsFor,
}
-382
View File
@@ -1,382 +0,0 @@
const fs = require('node:fs')
const { atomicWritePrivateFile } = require('../security/private-file.cjs')
const STORE_VERSION = 3
const SINGLE_PROFILE_STORE_VERSION = 2
const LEGACY_STORE_VERSION = 1
const PROVIDER_KIND = /^[a-z0-9][a-z0-9-]{0,31}$/
const PROVIDER_PRESETS = new Set(['official', 'openrouter', 'custom'])
const EMPTY_METADATA = Object.freeze({ provider: '', baseUrl: '', model: '' })
const OPENROUTER_PROVIDER = Object.freeze({
provider: 'OpenRouter',
baseUrl: 'https://openrouter.ai/api/v1',
})
const OFFICIAL_PROVIDERS = Object.freeze({
codex: { provider: 'OpenAI', baseUrl: 'https://api.openai.com/v1' },
hermes: { provider: 'OpenAI API', baseUrl: 'https://api.openai.com/v1' },
openclaw: { provider: 'OpenAI API', baseUrl: 'https://api.openai.com/v1' },
workbuddy: { provider: 'WorkBuddy Official', baseUrl: '' },
pi: { provider: 'Pi Native', baseUrl: '' },
kimi: { provider: 'Moonshot AI', baseUrl: 'https://api.moonshot.cn/v1' },
mimo: { provider: 'MiMo Native', baseUrl: '' },
claude: { provider: 'Anthropic', baseUrl: 'https://api.anthropic.com' },
gemini: { provider: 'Google AI Studio', baseUrl: 'https://generativelanguage.googleapis.com/v1beta' },
opencode: { provider: 'OpenCode Provider', baseUrl: '' },
qwen: { provider: 'DashScope', baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1' },
opencodereview: { provider: 'OpenAI API', baseUrl: 'https://api.openai.com/v1' },
})
function normalizeMetadata(input) {
const provider = String(input?.provider || '').trim()
let baseUrl = String(input?.baseUrl || '').trim().replace(/\/+$/, '')
baseUrl = baseUrl.replace(/\/chat\/completions$/i, '')
const model = String(input?.model || '').trim()
if (!provider || provider.length > 80 || !baseUrl || baseUrl.length > 300
|| !model || model.length > 160) {
throw new Error('PROVIDER_INVALID_METADATA')
}
let parsed
try { parsed = new URL(baseUrl) } catch { throw new Error('PROVIDER_INVALID_METADATA') }
const loopback = ['localhost', '127.0.0.1', '[::1]'].includes(parsed.hostname)
if (parsed.protocol !== 'https:' && !(parsed.protocol === 'http:' && loopback)) {
throw new Error('PROVIDER_INSECURE_BASE_URL')
}
if (parsed.username || parsed.password || parsed.search || parsed.hash) {
throw new Error('PROVIDER_INVALID_METADATA')
}
return { provider, baseUrl, model }
}
function sortedKeys(value) {
return value && typeof value === 'object' && !Array.isArray(value)
? Reflect.ownKeys(value).sort().join(',')
: ''
}
function inferStoredPreset(entry) {
const provider = String(entry?.provider || '').toLowerCase()
const baseUrl = String(entry?.baseUrl || '')
return /openrouter/i.test(provider) || /(^|\.)openrouter\.ai$/i.test(hostname(baseUrl))
? 'openrouter'
: 'custom'
}
function hostname(value) {
try { return new URL(value).hostname } catch { return '' }
}
function validatePresetMetadata(kind, preset, metadata) {
if (preset === 'custom') return metadata
const expected = preset === 'openrouter' ? OPENROUTER_PROVIDER : OFFICIAL_PROVIDERS[kind]
if (!expected?.baseUrl
|| metadata.provider !== expected.provider
|| metadata.baseUrl !== expected.baseUrl) {
throw new Error('PROVIDER_INVALID_METADATA')
}
return metadata
}
class ProviderStore {
constructor({ storagePath, safeStorage, allowedKinds = [] }) {
if (typeof storagePath !== 'string' || !storagePath) {
throw new Error('PROVIDER_STORAGE_PATH_REQUIRED')
}
const normalizedKinds = [...new Set(allowedKinds.map(kind => String(kind || '').trim()))]
if (!normalizedKinds.length || normalizedKinds.some(kind => !PROVIDER_KIND.test(kind))) {
throw new Error('PROVIDER_AGENT_KINDS_REQUIRED')
}
this.storagePath = storagePath
this.safeStorage = safeStorage
this.allowedKinds = new Set(normalizedKinds)
}
encryptionAvailable() {
try {
return Boolean(this.safeStorage?.isEncryptionAvailable?.())
} catch {
return false
}
}
status(kind, { probeEncryption = false } = {}) {
const targetKind = this.normalizeKind(kind)
const credentialStored = fs.existsSync(this.storagePath)
if (!credentialStored && !probeEncryption) {
return {
provider: '',
baseUrl: '',
model: '',
activePreset: 'official',
profiles: {},
encryptionAvailable: null,
configured: false,
}
}
const encryptionAvailable = this.encryptionAvailable()
let metadata = EMPTY_METADATA
let configured = false
let credentialUnavailable = false
let activePreset = 'official'
const profiles = {}
if (credentialStored) {
try {
const agent = this.readDocument().agents[targetKind]
if (agent) {
activePreset = agent.activePreset
for (const [preset, entry] of Object.entries(agent.profiles)) {
profiles[preset] = {
provider: entry.provider,
baseUrl: entry.baseUrl,
model: entry.model,
configured: false,
}
try {
this.decryptEntry(entry)
profiles[preset].configured = true
} catch { /* retain saved metadata while the credential is unavailable */ }
}
const activeEntry = profiles[activePreset]
if (activeEntry) {
metadata = activeEntry
configured = activeEntry.configured
credentialUnavailable = !configured
}
}
} catch { credentialUnavailable = true }
}
return {
provider: metadata.provider,
baseUrl: metadata.baseUrl,
model: metadata.model,
activePreset,
profiles,
encryptionAvailable,
configured,
...(credentialUnavailable ? { error: true } : {}),
}
}
save(kind, input) {
const targetKind = this.normalizeKind(kind)
if (sortedKeys(input) !== 'apiKey,baseUrl,model,preset,provider'
|| typeof input.apiKey !== 'string' || !input.apiKey.trim()
|| input.apiKey.trim().length > 8192) {
throw new Error('PROVIDER_INVALID_CREDENTIAL')
}
this.requireEncryption()
const apiKey = input.apiKey.trim()
const preset = this.normalizePreset(input.preset)
const metadata = validatePresetMetadata(targetKind, preset, normalizeMetadata(input))
let encrypted
try {
encrypted = this.safeStorage.encryptString(apiKey)
if (!Buffer.isBuffer(encrypted) || encrypted.length === 0
|| this.safeStorage.decryptString(encrypted) !== apiKey) {
throw new Error('invalid encryption result')
}
} catch {
throw new Error('PROVIDER_ENCRYPTION_FAILED')
}
const document = fs.existsSync(this.storagePath)
? this.readDocument()
: { version: STORE_VERSION, agents: {} }
const agent = document.agents[targetKind] || { activePreset: 'official', profiles: {} }
agent.profiles[preset] = {
...metadata,
encrypted: encrypted.toString('base64'),
}
agent.activePreset = preset
document.agents[targetKind] = agent
atomicWritePrivateFile(this.storagePath, JSON.stringify(document))
return this.status(targetKind)
}
activate(kind, preset) {
const targetKind = this.normalizeKind(kind)
const targetPreset = this.normalizePreset(preset)
if (!fs.existsSync(this.storagePath)) {
if (targetPreset !== 'official') throw new Error('PROVIDER_PROFILE_UNAVAILABLE')
return this.status(targetKind, { probeEncryption: true })
}
this.requireEncryption()
const document = this.readDocument()
const agent = document.agents[targetKind] || { activePreset: 'official', profiles: {} }
if (targetPreset !== 'official' && !agent.profiles[targetPreset]) {
throw new Error('PROVIDER_PROFILE_UNAVAILABLE')
}
if (agent.profiles[targetPreset]) this.decryptEntry(agent.profiles[targetPreset])
agent.activePreset = targetPreset
document.agents[targetKind] = agent
atomicWritePrivateFile(this.storagePath, JSON.stringify(document))
return this.status(targetKind)
}
delete(kind, preset = '') {
const targetKind = this.normalizeKind(kind)
if (fs.existsSync(this.storagePath)) {
const document = this.readDocument()
if (preset) {
const targetPreset = this.normalizePreset(preset)
const agent = document.agents[targetKind]
if (agent) {
delete agent.profiles[targetPreset]
if (agent.activePreset === targetPreset) agent.activePreset = 'official'
if (Object.keys(agent.profiles).length) document.agents[targetKind] = agent
else delete document.agents[targetKind]
}
} else {
delete document.agents[targetKind]
}
if (Object.keys(document.agents).length) {
atomicWritePrivateFile(this.storagePath, JSON.stringify(document))
} else {
fs.unlinkSync(this.storagePath)
}
}
return this.status(targetKind, { probeEncryption: true })
}
envForAgent(kind) {
const targetKind = this.normalizeKind(kind)
this.requireEncryption()
const agent = this.readDocument().agents[targetKind]
const entry = agent?.profiles?.[agent.activePreset]
if (!entry) throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
return {
OPENAI_API_KEY: this.decryptEntry(entry),
OPENAI_BASE_URL: entry.baseUrl,
OPENAI_MODEL: entry.model,
}
}
requireEncryption() {
if (!this.encryptionAvailable()) throw new Error('PROVIDER_ENCRYPTION_UNAVAILABLE')
}
normalizeKind(kind) {
const normalized = String(kind || '').trim()
if (!PROVIDER_KIND.test(normalized) || !this.allowedKinds.has(normalized)) {
throw new Error('PROVIDER_AGENT_UNSUPPORTED')
}
return normalized
}
normalizePreset(preset) {
const normalized = String(preset || '').trim().toLowerCase()
if (!PROVIDER_PRESETS.has(normalized)) throw new Error('PROVIDER_PRESET_UNSUPPORTED')
return normalized
}
decryptEntry(entry) {
this.requireEncryption()
try {
const encrypted = Buffer.from(entry.encrypted, 'base64')
if (!encrypted.length || encrypted.toString('base64') !== entry.encrypted) {
throw new Error('invalid ciphertext')
}
const apiKey = this.safeStorage.decryptString(encrypted)
if (typeof apiKey !== 'string' || !apiKey.trim()) throw new Error('invalid credential')
return apiKey
} catch {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
}
readDocument() {
let payload
try {
payload = JSON.parse(fs.readFileSync(this.storagePath, 'utf8'))
} catch {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
if (payload?.version === LEGACY_STORE_VERSION) {
if (sortedKeys(payload) !== 'baseUrl,encrypted,model,provider,version'
|| typeof payload.encrypted !== 'string') {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
const rawEntry = {
provider: payload.provider,
baseUrl: payload.baseUrl,
model: payload.model,
encrypted: payload.encrypted,
}
return {
version: STORE_VERSION,
agents: Object.fromEntries([...this.allowedKinds].map((kind) => {
const entry = this.normalizeEntry(rawEntry)
const preset = inferStoredPreset(entry)
return [kind, {
activePreset: preset,
profiles: { [preset]: { ...entry } },
}]
})),
}
}
if (payload?.version === SINGLE_PROFILE_STORE_VERSION) {
if (sortedKeys(payload) !== 'agents,version'
|| !payload.agents || typeof payload.agents !== 'object'
|| Array.isArray(payload.agents)) {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
const agents = {}
for (const [kind, rawEntry] of Object.entries(payload.agents)) {
if (!this.allowedKinds.has(kind)) throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
const entry = this.normalizeEntry(rawEntry)
const preset = inferStoredPreset(entry)
agents[kind] = { activePreset: preset, profiles: { [preset]: entry } }
}
return { version: STORE_VERSION, agents }
}
if (sortedKeys(payload) !== 'agents,version'
|| payload.version !== STORE_VERSION
|| !payload.agents || typeof payload.agents !== 'object'
|| Array.isArray(payload.agents)) {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
const agents = {}
for (const [kind, entry] of Object.entries(payload.agents)) {
if (!this.allowedKinds.has(kind)) throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
agents[kind] = this.normalizeAgentEntry(entry)
}
return { version: STORE_VERSION, agents }
}
normalizeAgentEntry(entry) {
try {
if (sortedKeys(entry) !== 'activePreset,profiles'
|| !entry.profiles || typeof entry.profiles !== 'object'
|| Array.isArray(entry.profiles)) {
throw new Error('invalid agent entry')
}
const activePreset = this.normalizePreset(entry.activePreset)
const profiles = {}
for (const [preset, profile] of Object.entries(entry.profiles)) {
const normalizedPreset = this.normalizePreset(preset)
profiles[normalizedPreset] = this.normalizeEntry(profile)
}
if (activePreset !== 'official' && !profiles[activePreset]) {
throw new Error('missing active profile')
}
return { activePreset, profiles }
} catch {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
}
normalizeEntry(entry) {
if (sortedKeys(entry) !== 'baseUrl,encrypted,model,provider'
|| typeof entry.encrypted !== 'string') {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
const encrypted = Buffer.from(entry.encrypted, 'base64')
if (!encrypted.length || encrypted.toString('base64') !== entry.encrypted) {
throw new Error('PROVIDER_CREDENTIAL_UNAVAILABLE')
}
return { ...normalizeMetadata(entry), encrypted: entry.encrypted }
}
}
module.exports = { ProviderStore }
+51 -7
View File
@@ -1,3 +1,11 @@
const {
authFailureCertainty,
isNetworkFailure,
isProviderUnavailable,
isRateLimited,
retryAfterFromText,
} = require('./failure-signals.cjs')
const FAILURE_CATEGORIES = Object.freeze([
'authentication',
'compatibility',
@@ -121,23 +129,47 @@ function retryAfterMs(error, now) {
const parsed = parseRetryAfter(headerValue(headers, 'retry-after'), now)
if (parsed != null) return parsed
}
return null
// Local CLIs surface provider throttling as text ("retry after 20s").
return retryAfterFromText(error?.diagnostic || error?.message)
}
// A local CLI process failure carries a code-derived category (usually
// protocol). Its diagnostic often proves a transient provider condition, so
// refine it before deciding whether to retry and how long to wait.
function transientCategoryFromText(detail) {
if (!detail) return ''
if (isRateLimited(detail)) return 'rate_limit'
if (isNetworkFailure(detail)) return 'network'
if (isProviderUnavailable(detail)) return 'provider'
return ''
}
function classifiedCategory(error, code, status) {
const explicit = String(error?.failure?.category || '')
if (FAILURE_CATEGORY_SET.has(explicit)) return explicit
const detail = String(error?.diagnostic || error?.message || '')
if (FAILURE_CATEGORY_SET.has(explicit)) {
if (['protocol', 'execution'].includes(explicit)) {
const refined = transientCategoryFromText(error?.diagnostic || '')
if (refined) return refined
}
return explicit
}
if (code === 'LOCAL_BUDGET_EXHAUSTED') return 'budget'
if (/PERMISSION/.test(code)) return 'permission'
if (code === 'LOCAL_AGENT_EXECUTION_STOPPED' || /CANCEL|ABORT/.test(code)) {
return 'cancellation'
}
if (code === 'LOCAL_AGENT_SESSION_INVALID') return 'session'
if (status === 401 || status === 403 || code === 'LOCAL_AGENT_AUTH_REQUIRED'
|| /unauthori[sz]ed|forbidden|invalid token/i.test(detail)) return 'authentication'
// Only explicit sign-in statements prove that credentials are gone. A 403,
// a proxy refusal, or "unauthorized" inside unrelated output is retried as
// a provider-side refusal instead of stopping the Agent for the run.
const authCertainty = authFailureCertainty(detail)
if (status === 401 || code === 'LOCAL_AGENT_AUTH_REQUIRED'
|| authCertainty === 'definitive') return 'authentication'
if (status === 403 || code === 'LOCAL_AGENT_ACCESS_DENIED'
|| authCertainty === 'ambiguous') return 'provider'
if (status === 429 || /RATE_LIMIT|TOO_MANY_REQUESTS/.test(code)
|| /rate limit|too many requests/i.test(detail)) return 'rate_limit'
|| isRateLimited(detail)) return 'rate_limit'
if (NETWORK_CODES.has(code) || /NETWORK|CONNECTION/.test(code)
|| /socket hang up|temporary failure|network error/i.test(detail)) return 'network'
if (status != null && status >= 500) return 'provider'
@@ -244,18 +276,30 @@ function retryDecision(input, options = {}) {
if (safety?.sideEffectsPossible === true && safety.idempotencyMode !== 'durable') {
return Object.freeze({ ...base, action: 'human_gate', exhausted: false })
}
const maxDelayMs = Math.max(1, boundedInteger(
const configuredMaxDelayMs = Math.max(1, boundedInteger(
options.maxDelayMs, 10 * 1000, 24 * 60 * 60 * 1000,
))
// Provider throttling needs a longer and server-directed wait than a crashed
// process. Honor Retry-After up to the rate-limit cap instead of hammering
// the provider with the generic sub-second backoff.
const rateLimited = failure.category === 'rate_limit'
const maxDelayMs = rateLimited
? Math.max(configuredMaxDelayMs, boundedInteger(
options.rateLimitMaxDelayMs, 60 * 1000, 24 * 60 * 60 * 1000,
))
: configuredMaxDelayMs
const backoff = boundedBackoffDelay(attempt, { ...options, maxDelayMs })
const requestedDelay = failure.retryAfterMs == null
? 0
: Math.min(maxDelayMs, boundedInteger(failure.retryAfterMs, 0, maxDelayMs))
const minimumDelay = rateLimited && failure.retryAfterMs == null
? Math.min(maxDelayMs, boundedInteger(options.rateLimitMinDelayMs, 0, maxDelayMs))
: 0
return Object.freeze({
...base,
action: 'retry',
exhausted: false,
delayMs: Math.max(backoff, requestedDelay),
delayMs: Math.max(backoff, requestedDelay, minimumDelay),
})
}
+129
View File
@@ -0,0 +1,129 @@
// Pure text classifiers for local Agent failure diagnostics.
//
// Agent CLIs report failures as free text on stderr or inside structured error
// results. Classification decides whether Meldwork retries, asks the user to
// sign in again, or rebuilds a native session, so every rule here errs on the
// side of "ambiguous": only explicit login and API-key statements count as a
// definitive authentication failure. A bare 403, a proxy refusal, or a word
// such as "unauthorized" inside unrelated output must never mark an installed
// Agent as signed out.
const MAX_SIGNAL_TEXT_CHARS = 64 * 1024
const LOG_PREFIX = /^(?:\[[^\]]{1,40}\]\s*|(?:error|fatal|err|warning)\s*[:!-]\s*|[✗✘×!]\s*)+/i
const DEFINITIVE_AUTH_LINE = [
/^(?:invalid|expired|missing|incorrect|revoked)\s+(?:api[ _-]?key|(?:access|refresh|auth|oauth|bearer)[ _-]?token|credentials?)\b/i,
/^(?:authentication|authorization)\s+(?:failed|required)\b/i,
/^(?:please\s+)?(?:log|sign)\s+in\b/i,
/^(?:please\s+)?run\s+\S*\s*(?:\/)?login\b/i,
/^not\s+(?:logged|signed)\s+in\b/i,
/^you\s+are\s+not\s+(?:logged|signed)\s+in\b/i,
/^select an auth type\b/i,
/^oauth\s+token\s+(?:has\s+)?(?:expired|is\s+invalid|was\s+revoked)\b/i,
/^HTTP\s+401\s*:/i,
/^provider rejected\b/i,
/^(?:认证|鉴权|登录)(?:失败|过期)/,
/^(?:令牌|凭据|密钥)(?:无效|缺失|已过期)/,
/^请(?:先|重新)登录/,
/^未登录/,
]
const DEFINITIVE_AUTH_ANYWHERE = /"type"\s*:\s*"authentication_error"|\binvalid_api_key\b|\bincorrect api key provided\b|\binvalid x-api-key\b/i
// Ambiguous refusals must lead a diagnostic line. Tool output such as
// "Failed to download docs: HTTP 403 Forbidden" mentions a refusal of some
// unrelated resource and stays a plain process failure.
const AMBIGUOUS_AUTH_LINE = [
/^HTTP\s+403\s*:/i,
/^(?:403\s+)?forbidden\b/i,
/^unauthori[sz]ed\b/i,
/^access denied\b/i,
/^invalid token\b/i,
]
const AMBIGUOUS_AUTH_ANYWHERE = /"type"\s*:\s*"permission_error"/i
const RATE_LIMIT = /\b429\b|too many requests|rate[ _-]?limit(?:ed|ing)?\b|\boverloaded(?:_error)?\b|\bquota (?:exceeded|exhausted)\b|\bresource[ _-]?exhausted\b|\binsufficient_quota\b|\b529\b/i
const NETWORK = /\b(?:ECONNRESET|ECONNREFUSED|ECONNABORTED|ENOTFOUND|ETIMEDOUT|EAI_AGAIN|ENETUNREACH|ENETDOWN|EHOSTUNREACH)\b|socket hang up|fetch failed|network (?:error|is unreachable)|connection (?:reset|refused|closed|timed out)|getaddrinfo|tls handshake|ssl handshake|proxy (?:error|connection)/i
const PROVIDER_UNAVAILABLE = /\b(?:HTTP\s*)?50[0234]\b\s*(?::|-)?\s*(?:internal server error|bad gateway|service unavailable|gateway timeout)|\bservice unavailable\b|\bbad gateway\b|\bgateway timeout\b|\binternal server error\b|"type"\s*:\s*"api_error"|\bupstream connect error\b/i
const CONTEXT_OVERFLOW = /prompt is too long|context[_ ]length[_ ]exceeded|maximum context length|context window (?:exceeded|is full|limit)|input is too long|too many (?:input )?tokens|exceeds? the (?:maximum|model'?s?) context|context_window_exceeded|request too large for model/i
function signalText(value) {
return String(value || '').slice(0, MAX_SIGNAL_TEXT_CHARS)
}
function diagnosticLines(value) {
return signalText(value)
.split(/\r?\n/)
.map(line => line.trim().replace(LOG_PREFIX, '').trim())
.filter(Boolean)
}
// Returns 'definitive', 'ambiguous', or ''.
function authFailureCertainty(value) {
const text = signalText(value)
if (!text.trim()) return ''
const lines = diagnosticLines(text)
if (DEFINITIVE_AUTH_ANYWHERE.test(text)
|| lines.some(line => DEFINITIVE_AUTH_LINE.some(pattern => pattern.test(line)))) {
return 'definitive'
}
if (lines.some(line => AMBIGUOUS_AUTH_LINE.some(pattern => pattern.test(line)))
|| AMBIGUOUS_AUTH_ANYWHERE.test(text)) {
return 'ambiguous'
}
return ''
}
function isRateLimited(value) {
return RATE_LIMIT.test(signalText(value))
}
function isNetworkFailure(value) {
return NETWORK.test(signalText(value))
}
function isProviderUnavailable(value) {
return PROVIDER_UNAVAILABLE.test(signalText(value))
}
function isContextOverflow(value) {
return CONTEXT_OVERFLOW.test(signalText(value))
}
const RETRY_AFTER_PATTERNS = [
/retry[- ]after["']?\s*[:=]?\s*(\d+(?:\.\d+)?)\s*(ms|milliseconds?|s|secs?|seconds?|m|mins?|minutes?)?/i,
/(?:try|retry) again in\s+(\d+(?:\.\d+)?)\s*(ms|milliseconds?|s|secs?|seconds?|m|mins?|minutes?)?/i,
/(?:reset|resets) in\s+(\d+(?:\.\d+)?)\s*(ms|milliseconds?|s|secs?|seconds?|m|mins?|minutes?)/i,
]
const MAX_RETRY_AFTER_MS = 5 * 60 * 1000
function retryAfterFromText(value) {
const text = signalText(value)
for (const pattern of RETRY_AFTER_PATTERNS) {
const match = text.match(pattern)
if (!match) continue
const amount = Number(match[1])
if (!Number.isFinite(amount) || amount < 0) continue
const unit = String(match[2] || 's').toLowerCase()
const multiplier = unit.startsWith('ms') || unit.startsWith('milli')
? 1
: unit.startsWith('m') ? 60 * 1000 : 1000
return Math.min(MAX_RETRY_AFTER_MS, Math.round(amount * multiplier))
}
return null
}
module.exports = {
authFailureCertainty,
isContextOverflow,
isNetworkFailure,
isProviderUnavailable,
isRateLimited,
retryAfterFromText,
}
+144
View File
@@ -0,0 +1,144 @@
const REAL_CLOCK = Object.freeze({
now: () => Date.now(),
setTimeout: (callback, ms) => setTimeout(callback, ms),
clearTimeout: handle => clearTimeout(handle),
})
function positiveFinite(value) {
return Number.isFinite(value) && value > 0
}
// Stops an Agent invocation that has gone quiet. Three windows apply:
// - startup: strict turns (autonomous discussion) must show any sign of life
// within `timeoutMs`; once they do, the deadline is lifted.
// - inactivity: any activity refreshes `timeoutMs`, or `toolTimeoutMs` while a
// tool call is running.
// - stall: strict turns also stop after three inactivity windows without
// substantive progress, so a CLI that emits empty heartbeats forever still
// ends and the remaining participants can continue. Other turns keep
// running while the process is active; the user can stop them.
// Raw process output only proves liveness (`liveness`); mapped events, answer
// text, tool lifecycle and structured progress count as progress (`progress`).
function createInvocationWatchdog({
timeoutMs,
toolTimeoutMs,
strict = false,
isStopped = () => false,
onTimeout = () => {},
clock = REAL_CLOCK,
} = {}) {
const enabled = positiveFinite(timeoutMs)
const startedAt = clock.now()
const startupDeadlineAt = strict && enabled ? startedAt + timeoutMs : Number.POSITIVE_INFINITY
let lastProgressAt = startedAt
let lastSubstantiveAt = startedAt
let paused = false
let established = false
let toolActive = false
const activeToolIds = new Set()
let closed = false
let timedOut = false
let timeoutError = null
let timer = null
let rejectTimeout = () => {}
const promise = new Promise((_, reject) => { rejectTimeout = reject })
promise.catch(() => {})
function disarm() {
clock.clearTimeout(timer)
timer = null
}
function arm() {
if (timer || paused || isStopped() || !enabled) return
const now = clock.now()
const inactivityWindowMs = toolActive ? toolTimeoutMs : timeoutMs
const inactivityRemainingMs = Math.max(0, inactivityWindowMs - (now - lastProgressAt))
const startupRemainingMs = Math.max(
0, (established ? Number.POSITIVE_INFINITY : startupDeadlineAt) - now,
)
const stallWindowMs = !strict
? Number.POSITIVE_INFINITY
: (toolActive ? toolTimeoutMs : timeoutMs * 3)
const stallRemainingMs = Math.max(0, stallWindowMs - (now - lastSubstantiveAt))
const remainingMs = Math.min(inactivityRemainingMs, startupRemainingMs, stallRemainingMs)
timer = clock.setTimeout(() => {
timer = null
if (paused || isStopped()) return
timedOut = true
timeoutError = new Error('LOCAL_AGENT_TIMEOUT')
onTimeout(timeoutError)
rejectTimeout(timeoutError)
}, remainingMs)
}
function rearm() {
if (paused) return
disarm()
arm()
}
function inactive() {
return closed || isStopped()
}
// The process produced something meaningful: refresh every window.
function progress() {
if (inactive()) return
lastProgressAt = clock.now()
lastSubstantiveAt = lastProgressAt
rearm()
}
arm()
return {
promise,
get timedOut() { return timedOut },
get error() { return timeoutError },
progress,
// The process is alive but has not said anything yet (stdout/stderr bytes,
// ACP traffic): lift the startup deadline and refresh the inactivity
// window outside tool calls.
liveness() {
if (inactive()) return
established = true
if (!toolActive) lastProgressAt = clock.now()
rearm()
},
// First proof that the run started. Lifts the startup deadline only.
establish() {
if (established || isStopped()) return
established = true
rearm()
},
toolStarted(toolCallId = '') {
if (toolCallId) activeToolIds.add(String(toolCallId))
toolActive = true
progress()
},
toolFinished(toolCallId = '') {
if (toolCallId) activeToolIds.delete(String(toolCallId))
else activeToolIds.clear()
toolActive = activeToolIds.size > 0
progress()
},
// Waiting for a person (Human Gate) is not the Agent being quiet.
pause() {
paused = true
disarm()
},
resume() {
paused = false
lastProgressAt = clock.now()
lastSubstantiveAt = lastProgressAt
arm()
},
close() {
closed = true
disarm()
},
}
}
module.exports = { createInvocationWatchdog }
+6 -1
View File
@@ -149,7 +149,7 @@ class RunHarness {
return next
}
beginAgent(kind, round = 0, sourceMessageIds = []) {
beginAgent(kind, round = 0, sourceMessageIds = [], options = {}) {
const safeKind = cleanId(kind)
if (!safeKind) throw new Error('RUN_HARNESS_AGENT_REQUIRED')
if (!this.targetKinds.includes(safeKind)) {
@@ -184,6 +184,10 @@ class RunHarness {
seenSeqs: [],
eventCursor: 0,
context: {},
// Per-topic dispatch order; the live bubble and the saved reply share it.
...(Number.isSafeInteger(options?.turnSeq) && options.turnSeq > 0
? { turnSeq: options.turnSeq }
: {}),
}
this.agentRuns.push(run)
while (this.agentRuns.length > this.maxAgentRuns) {
@@ -328,6 +332,7 @@ class RunHarness {
seenSeqs: [...run.seenSeqs],
eventCursor: run.eventCursor,
context: { ...run.context },
...(run.turnSeq ? { turnSeq: run.turnSeq } : {}),
}))
}
}
+1 -1
View File
@@ -311,7 +311,7 @@ class RunLedger {
persist(runs = this.runs) {
atomicWritePrivateFile(
this.storagePath,
`${JSON.stringify({ version: STORE_VERSION, runs }, null, 2)}\n`,
`${JSON.stringify({ version: STORE_VERSION, runs })}\n`,
)
}
+86
View File
@@ -0,0 +1,86 @@
const fs = require('node:fs')
const path = require('node:path')
const { z } = require('zod')
const id = z.string().min(1).max(120).regex(/^[A-Za-z0-9._:-]+$/)
function request(schema, input) {
const parsed = schema.safeParse(input)
if (!parsed.success) throw new Error('LOCAL_DELIVERY_INVALID')
return parsed.data
}
function registerDeliveryIpc({ registerTrustedHandle, getWorkspace, getAttachmentStore, dialog, getMainWindow }) {
const workspace = () => {
const value = getWorkspace()
if (!value) throw new Error('LOCAL_WORKSPACE_UNAVAILABLE')
return value
}
for (const method of ['save', 'review', 'revise']) {
registerTrustedHandle(`local-delivery:${method}`, input => {
const current = workspace()
if (method === 'review' && input?.accept === true) {
const task = current.delivery.task(input.taskId)
const version = current.delivery.version(task, input.versionId)
for (const attachment of version.attachments) {
const store = getAttachmentStore()
if (!store) throw new Error('LOCAL_ATTACHMENT_STORAGE_UNAVAILABLE')
store.read(attachment.id)
}
}
const result = current.delivery[method](input)
current.emitChanged()
return { taskId: method === 'save' ? result : input.taskId }
})
}
registerTrustedHandle('local-delivery:reuse', input => {
const { taskId } = request(z.strictObject({ taskId: id }), input)
const current = workspace()
const task = current.delivery.task(taskId)
const original = current.getGroup(task.groupId)
const root = path.join(path.dirname(current.storagePath), 'delivery-workspaces')
fs.mkdirSync(root, { recursive: true, mode: 0o700 })
const workdir = fs.mkdtempSync(path.join(root, 'task-'))
let group
try {
group = current.createGroup({
name: task.brief.title, agentKinds: original.agentKinds,
conversationType: original.conversationType,
directAgentKind: original.directAgentKind,
workdir, allowWrite: false,
})
const newTaskId = current.delivery.save({ groupId: group.id, brief: task.brief })
current.emitChanged()
return { taskId: newTaskId, groupId: group.id }
} catch (error) {
if (group) current.deleteGroup(group.id)
fs.rmdirSync(workdir)
throw error
}
})
registerTrustedHandle('local-delivery:export', async input => {
const { taskId, versionId, attachmentId } = request(z.strictObject({
taskId: id, versionId: id, attachmentId: id.optional(),
}), input)
const current = workspace()
const task = current.delivery.task(taskId)
const version = current.delivery.version(task, versionId)
const attachment = attachmentId ? version.attachments.find(item => item.id === attachmentId) : null
if (attachmentId && !attachment) throw new Error('LOCAL_DELIVERY_NOT_FOUND')
let bytes
if (attachment) {
const store = getAttachmentStore()
if (!store) throw new Error('LOCAL_ATTACHMENT_STORAGE_UNAVAILABLE')
bytes = store.read(attachment.id)
} else bytes = Buffer.from(current.delivery.markdown(taskId, versionId), 'utf8')
const filename = (attachment?.name || `${task.brief.title}-V${task.versions.indexOf(version) + 1}.md`)
.replace(/[\\/\x00-\x1f:*?"<>|]/g, '_')
const result = await dialog.showSaveDialog(getMainWindow(), { defaultPath: filename })
if (result.canceled || !result.filePath) return { saved: false }
try {
fs.writeFileSync(result.filePath, bytes, { mode: 0o600, flag: fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_TRUNC | fs.constants.O_NOFOLLOW })
} catch { throw new Error('LOCAL_DELIVERY_EXPORT_FAILED') }
return { saved: true }
})
}
module.exports = { registerDeliveryIpc }
+49 -35
View File
@@ -1,6 +1,8 @@
const { ADOPTION_ACTION_STATUSES } = require('../collaboration/outcome-records.cjs')
const { statusForHumanGateOption } = require('../gates/human-gate-coordinator.cjs')
const { ATTACHMENT_FILE_EXTENSIONS } = require('../attachments/attachment-records.cjs')
const { registerDeliveryIpc } = require('./delivery-ipc.cjs')
const { loadedWorkPreferences, parseWorkPreferences } = require('../workspace/work-preferences.cjs')
const LOCAL_GROUP_IDENTIFIER = /^[^\u0000-\u001f\u007f]{1,100}$/u
const LOCAL_RUN_IDENTIFIER = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,119}$/
@@ -163,6 +165,7 @@ function sanitizeCloudAgentSnapshot(snapshot) {
}
function registerDesktopIpc(options) {
registerDeliveryIpc(options)
const {
app,
attachmentIdsFromSnapshot,
@@ -178,6 +181,7 @@ function registerDesktopIpc(options) {
getOutcomeStore,
getSkillTrustStore,
getWorkspace,
getWorkBuddySettings,
installer,
isShutdownStarted,
knowledgeBaseStore,
@@ -185,8 +189,6 @@ function registerDesktopIpc(options) {
loadKnowledgeBaseSources,
localAgentCatalog,
openExternalUrl,
providerStore,
providerAgentKind,
refreshLocalAgentState,
registerTrustedHandle,
skillCatalog,
@@ -419,6 +421,51 @@ function registerDesktopIpc(options) {
})
registerTrustedHandle('local-workspace:default-directory', () => app.getPath('documents'))
registerTrustedHandle('local-agent-installer:catalog', async () => localAgentCatalog())
registerTrustedHandle('local-workbuddy:settings', () => getWorkBuddySettings())
registerTrustedHandle('local-work-preferences:get', () => loadedWorkPreferences(getWorkspace()?.state.workPreferences))
registerTrustedHandle('local-work-preferences:save', (...args) => {
if (args.length !== 1) throw new Error('LOCAL_WORK_PREFERENCES_INVALID')
const value = parseWorkPreferences(args[0])
const workspace = getWorkspace()
if (!workspace) throw new Error('LOCAL_WORKSPACE_UNAVAILABLE')
if (workspace.activeRuns.size || workspace.preparingRuns?.size) throw new Error('LOCAL_WORK_PREFERENCES_BUSY')
const previous = workspace.state.workPreferences
workspace.state.workPreferences = value
try { workspace.save() } catch (error) {
if (previous) workspace.state.workPreferences = previous
else delete workspace.state.workPreferences
throw error
}
workspace.emitChanged()
return value
})
registerTrustedHandle('local-workbuddy:set-model', async (...args) => {
if (args.length !== 1 || typeof args[0] !== 'string' || args[0].length > 120) {
throw new Error('LOCAL_WORKBUDDY_MODEL_UNAVAILABLE')
}
const workspace = getWorkspace()
if (!workspace) throw new Error('LOCAL_WORKSPACE_UNAVAILABLE')
const modelId = args[0]
const settings = await getWorkBuddySettings()
if (modelId && !settings.models.some(model => model.id === modelId)) {
throw new Error('LOCAL_WORKBUDDY_MODEL_UNAVAILABLE')
}
if (workspace.activeRuns.size || workspace.preparingRuns?.size) {
throw new Error('LOCAL_WORKBUDDY_BUSY')
}
const before = workspace.state.agentPreferences.workbuddy
const runtime = workspace.state.agentRuntime.workbuddy
workspace.state.agentPreferences.workbuddy = { ...before, modelId }
delete workspace.state.agentRuntime.workbuddy
try { workspace.save() } catch (error) {
if (before) workspace.state.agentPreferences.workbuddy = before
else delete workspace.state.agentPreferences.workbuddy
if (runtime) workspace.state.agentRuntime.workbuddy = runtime
throw error
}
await refreshLocalAgentState()
return { ...settings, modelId }
})
registerTrustedHandle('local-agent-installer:skills', async (kind) => {
const selectedKind = String(kind || '')
if (customAgentStore.has(selectedKind)) {
@@ -660,39 +707,6 @@ function registerDesktopIpc(options) {
registerTrustedHandle('local-attachments:preview', id => attachments.preview(String(id || '')))
registerTrustedHandle('local-attachments:open', id => attachments.open(String(id || '')))
registerTrustedHandle('local-attachments:discard', ids => attachments.discardUnreferenced(ids))
registerTrustedHandle('local-agent-provider:status', kind => (
providerStore.status(providerAgentKind(kind))
))
registerTrustedHandle('local-agent-provider:probe', kind => (
providerStore.status(providerAgentKind(kind), { probeEncryption: true })
))
registerTrustedHandle('local-agent-provider:save', async (kind, input) => {
const selectedKind = providerAgentKind(kind)
const result = providerStore.save(selectedKind, {
apiKey: input?.apiKey,
provider: input?.provider,
baseUrl: input?.baseUrl,
model: input?.model,
preset: input?.preset,
})
getWorkspace()?.markRuntimeCredential(selectedKind, 'unknown')
await refreshLocalAgentState()
return result
})
registerTrustedHandle('local-agent-provider:activate', async (kind, preset) => {
const selectedKind = providerAgentKind(kind)
const result = providerStore.activate(selectedKind, preset)
getWorkspace()?.markRuntimeCredential(selectedKind, 'unknown')
await refreshLocalAgentState()
return result
})
registerTrustedHandle('local-agent-provider:delete', async (kind, preset) => {
const selectedKind = providerAgentKind(kind)
const result = providerStore.delete(selectedKind, preset)
getWorkspace()?.markRuntimeCredential(selectedKind, 'unknown')
await refreshLocalAgentState()
return result
})
registerTrustedHandle('local-knowledge-connector:list', (...args) => {
if (args.length) throw new Error('LOCAL_KNOWLEDGE_CONNECTOR_REQUEST_INVALID')
const connectors = getKnowledgeConnectors?.()
+257
View File
@@ -0,0 +1,257 @@
const { randomUUID } = require('node:crypto')
const { z } = require('zod')
const { normalizeAttachmentMetadata } = require('./local-workspace-message-records.cjs')
const id = z.string().min(1).max(120).regex(/^[A-Za-z0-9._:-]+$/)
const text = (max) => z.string().trim().min(1).max(max)
const briefSchema = z.strictObject({
title: text(120), goal: text(4000), output: text(2000),
outputKind: z.enum(['text', 'files']),
criteria: z.array(text(300)).min(1).max(20),
language: z.enum(['zh', 'en']),
})
const attachmentSchema = z.strictObject({
id, name: text(160), mimeType: text(160), size: z.number().int().positive(),
})
const sourceSchema = z.strictObject({ name: text(200), kind: text(40), attachmentId: id.optional() })
const revisionSchema = z.strictObject({ baseVersionId: id, note: text(4000) })
const attemptSchema = z.strictObject({
runId: id, messageId: id, status: text(40), createdAt: text(80),
input: z.string().max(200000), sources: z.array(sourceSchema).max(41),
revision: revisionSchema.nullable(),
})
const versionSchema = z.strictObject({
id, runId: id, createdAt: text(80),
responses: z.array(z.strictObject({ messageId: id, agentKind: text(40), content: z.string().max(1000000) })).max(32),
attachments: z.array(attachmentSchema).max(128),
checks: z.array(z.enum(['pending', 'met', 'unmet', 'unknown'])).max(20),
notes: z.string().max(4000), acceptedAt: z.string().max(80),
})
const taskSchema = z.strictObject({
id, groupId: text(100), brief: briefSchema, createdAt: text(80),
attempts: z.array(attemptSchema).max(200), versions: z.array(versionSchema).max(200),
revision: revisionSchema.nullable(),
})
function parse(schema, input) {
const result = schema.safeParse(input)
if (!result.success) throw new Error('LOCAL_DELIVERY_INVALID')
return result.data
}
function parseDeliveryTasks(value = []) {
return parse(z.array(taskSchema).max(1000), value)
}
class DeliveryTasks {
constructor(workspace) {
this.workspace = workspace
this.pending = new Map()
}
get tasks() { return this.workspace.state.deliveryTasks || [] }
task(taskId) {
parse(id, taskId)
const task = this.tasks.find(item => item.id === taskId)
if (!task || !this.workspace.state.groups.some(group => group.id === task.groupId)) {
throw new Error('LOCAL_DELIVERY_NOT_FOUND')
}
return task
}
version(task, versionId) {
parse(id, versionId)
const version = task.versions.find(item => item.id === versionId)
if (!version) throw new Error('LOCAL_DELIVERY_NOT_FOUND')
return version
}
commit(change) {
const previous = structuredClone(this.tasks)
try {
this.workspace.state.deliveryTasks ||= []
const result = change()
parseDeliveryTasks(this.tasks)
this.workspace.save()
return result
} catch (error) {
this.workspace.state.deliveryTasks = previous
throw error
}
}
idle(task) {
if (this.workspace.isGroupBusy(task.groupId) || this.pending.has(task.groupId)) {
throw new Error('LOCAL_GROUP_RUNNING')
}
}
save(input) {
const request = parse(z.strictObject({ groupId: text(100), taskId: id.optional(), brief: briefSchema }), input)
this.workspace.getGroup(request.groupId)
if (this.workspace.isGroupBusy(request.groupId)) throw new Error('LOCAL_GROUP_RUNNING')
const existing = request.taskId ? this.task(request.taskId) : null
if (existing && (existing.groupId !== request.groupId || existing.attempts.length)) {
throw new Error('LOCAL_DELIVERY_LOCKED')
}
return this.commit(() => {
if (existing) { existing.brief = request.brief; return existing.id }
const task = {
id: `delivery-${randomUUID()}`, groupId: request.groupId, brief: request.brief,
createdAt: this.workspace.now(), attempts: [], versions: [], revision: null,
}
this.tasks.push(task)
return task.id
})
}
prepare(input) {
const task = this.task(input.deliveryTaskId)
if (task.groupId !== input.groupId || input.regenerateMessageId || input.threadRootId) {
throw new Error('LOCAL_DELIVERY_INVALID')
}
this.idle(task)
if (task.versions.length && !task.revision) throw new Error('LOCAL_DELIVERY_REVISION_REQUIRED')
const brief = task.brief
const base = task.revision ? this.version(task, task.revision.baseVersionId) : null
if (task.attempts.length >= 200) throw new Error('LOCAL_DELIVERY_INPUT_LIMIT')
if (input.attachments != null && !Array.isArray(input.attachments)) throw new Error('LOCAL_DELIVERY_INVALID')
const attachments = [...new Map([...(base?.attachments || []), ...(input.attachments || [])]
.map(item => [item.id, item])).values()]
if (attachments.length > 32) throw new Error('LOCAL_ATTACHMENT_COUNT_LIMIT')
const prompt = [
brief.language === 'zh' ? '工作交付要求(请用中文回答)' : 'Work delivery requirements (respond in English)',
JSON.stringify(brief),
'Produce the requested deliverable. Identify source materials for key claims, conflicting or missing information, and unresolved issues. Never invent missing data or claim that a file exists without creating it. Human acceptance is separate from your self-review.',
...(base ? ['Previous version:', ...base.responses.map(item => item.content), `Revision request: ${task.revision.note}`] : []),
`Additional instructions: ${String(input.text || '')}`,
].join('\n\n')
if (prompt.length > 200000) throw new Error('LOCAL_DELIVERY_INPUT_LIMIT')
this.pending.set(task.groupId, { taskId: task.id, input: String(input.text || '') })
const { deliveryTaskId: _id, ...rest } = input
return { ...rest, text: prompt, attachments }
}
bind(groupId, controller, messageId) {
const pending = this.pending.get(groupId)
if (!pending) return
const task = this.task(pending.taskId)
const message = this.workspace.state.messages.find(item => item.id === messageId)
if (!message || message.groupId !== groupId) throw new Error('LOCAL_DELIVERY_INVALID')
this.commit(() => task.attempts.push({
runId: controller.runId, messageId, status: 'running', createdAt: this.workspace.now(),
input: pending.input, revision: task.revision ? { ...task.revision } : null,
sources: [
{ name: this.workspace.getGroup(groupId).name || 'Conversation', kind: 'conversation' },
...(message.attachments || []).map(item => ({ name: item.name, kind: 'attachment', attachmentId: item.id })),
...(message.knowledgeBaseHints || []).map(item => ({ name: item.name, kind: item.kind })),
...(message.skillHints || []).map(item => ({ name: item.name, kind: 'skill' })),
],
}))
}
complete(runId, status, committedMessageIds = []) {
const task = this.tasks.find(item => item.attempts.some(attempt => attempt.runId === runId))
if (!task) return
const attempt = task.attempts.find(item => item.runId === runId)
if (!['running', 'waiting', 'preparing'].includes(attempt.status)) return
const messages = this.workspace.state.messages
const rootIndex = messages.findIndex(item => item.id === attempt.messageId)
const nextUserIndex = messages.findIndex((item, index) => index > rootIndex && item.groupId === task.groupId && item.role === 'user')
const related = messages.filter((message, index) => {
if (message.role !== 'agent' || message.groupId !== task.groupId) return false
if (committedMessageIds.includes(message.id)) return true
if (message.trace?.runId && message.trace.runId !== runId) return false
return message.trace?.runId === runId
|| message.threadRootId === attempt.messageId
|| (!message.threadRootId && rootIndex >= 0 && index > rootIndex && (nextUserIndex < 0 || index < nextUserIndex))
})
const outputs = related.filter(message => {
const phase = message.trace?.phase || message.trace?.context?.phase
return committedMessageIds.includes(message.id)
|| !['proposal', 'discussion', 'challenge', 'coordination', 'work', 'verification'].includes(phase)
}).filter(message => message.content?.trim())
const attachments = [...new Map(related.flatMap(item => item.attachments || [])
.map(normalizeAttachmentMetadata).filter(Boolean).map(item => [item.id, item])).values()]
this.commit(() => {
attempt.status = status
if (!outputs.length && !attachments.length) return
task.versions.push({
id: `version-${randomUUID()}`, runId, createdAt: this.workspace.now(),
responses: outputs.map(item => ({ messageId: item.id, agentKind: item.agentKind, content: item.content || '' })),
attachments, checks: task.brief.criteria.map(() => 'pending'), notes: '', acceptedAt: '',
})
task.revision = null
})
}
recover() {
for (const task of this.tasks) {
for (const attempt of task.attempts) {
if (!['running', 'waiting', 'preparing'].includes(attempt.status)) continue
const record = this.workspace.runLedger?.list?.().find(item => item.runId === attempt.runId)
if (this.workspace.activeRuns.has(task.groupId)
|| (record?.status === 'waiting' && this.workspace.canResumeHumanGateRecord(record))) continue
const status = record && !['running', 'waiting', 'preparing', 'queued'].includes(record.status)
? record.status : 'interrupted'
this.complete(attempt.runId, status, [
...(record?.orchestration?.commitState?.messageIds || []),
record?.orchestration?.candidateCommit?.messageId,
].filter(Boolean))
}
}
}
review(input) {
const request = parse(z.strictObject({
taskId: id, versionId: id, checks: z.array(z.enum(['pending', 'met', 'unmet', 'unknown'])).max(20),
notes: z.string().max(4000), accept: z.boolean(),
}), input)
const task = this.task(request.taskId)
this.idle(task)
const version = this.version(task, request.versionId)
if (version.acceptedAt || request.checks.length !== task.brief.criteria.length) throw new Error('LOCAL_DELIVERY_LOCKED')
if (request.accept && (request.checks.some(check => check !== 'met')
|| (task.brief.outputKind === 'files' && !version.attachments.length))) {
throw new Error('LOCAL_DELIVERY_NOT_READY')
}
this.commit(() => {
version.checks = request.checks
version.notes = request.notes.trim()
if (request.accept) version.acceptedAt = this.workspace.now()
})
}
revise(input) {
const request = parse(z.strictObject({ taskId: id, baseVersionId: id, note: text(4000) }), input)
const task = this.task(request.taskId)
this.idle(task)
this.version(task, request.baseVersionId)
this.commit(() => { task.revision = { baseVersionId: request.baseVersionId, note: request.note } })
}
markdown(taskId, versionId) {
const task = this.task(taskId)
const version = this.version(task, versionId)
const attempt = task.attempts.find(item => item.runId === version.runId)
const zh = task.brief.language === 'zh'
const checks = zh
? { pending: '待检查', met: '符合', unmet: '不符合', unknown: '无法判断' }
: { pending: 'Not checked', met: 'Met', unmet: 'Not met', unknown: 'Unknown' }
return [
`# ${task.brief.title}`, `V${task.versions.indexOf(version) + 1} · ${version.createdAt}`,
`${zh ? '采用时间' : 'Accepted at'}: ${version.acceptedAt || (zh ? '未采用' : 'Not accepted')}`,
...version.responses.map(item => `## ${item.agentKind}\n\n${item.content}`),
`## ${zh ? '输入材料(不代表逐条引用已核实)' : 'Input materials (not verified claim-level citations)'}`,
...attempt.sources.map(item => `- ${item.name} (${item.kind})`),
`## ${zh ? '人工验收' : 'Human review'}`,
...task.brief.criteria.map((criterion, index) => `- [${checks[version.checks[index]]}] ${criterion}`),
`## ${zh ? '备注与未决事项' : 'Notes and unresolved issues'}`, version.notes || '—',
`## ${zh ? '附件(需单独导出)' : 'Attachments (export separately)'}`,
...version.attachments.map(item => `- ${item.name}`),
].join('\n\n') + '\n'
}
}
module.exports = { DeliveryTasks, parseDeliveryTasks }
@@ -0,0 +1,37 @@
const { outboundWirePayloadBytes } = require('../collaboration/outbound-payload.cjs')
// What the Agent was sent: wire bytes are reported, tokens are an estimate.
function recordOutboundUsage(budget, outbound) {
if (!budget) return
const bytes = outboundWirePayloadBytes(outbound)
// A payload that was not built by the transport has no recorded wire size;
// skipping it is better than failing the Agent's turn over bookkeeping.
if (!bytes) return
budget.addUsageBatch([
{ dimension: 'outboundBytes', amount: bytes.length, source: 'reported' },
{ dimension: 'inputTokens', amount: Math.ceil(bytes.length / 4), source: 'estimated' },
])
}
// What the Agent produced: prefer the usage the CLI reported over an estimate.
function recordReplyUsage(budget, { usage, replyText, toolCallCount }) {
if (!budget) return
const reportedOutputTokens = Number(usage?.outputTokens)
const hasReportedOutputTokens = Number.isSafeInteger(reportedOutputTokens)
&& reportedOutputTokens >= 0
const reportedCostMicros = Number(usage?.costMicros)
budget.addUsageBatch([
{
dimension: 'outputTokens',
amount: hasReportedOutputTokens ? reportedOutputTokens : Math.ceil(replyText.length / 4),
source: hasReportedOutputTokens ? 'reported' : 'estimated',
},
{ dimension: 'toolCalls', amount: toolCallCount, source: 'estimated' },
...(Number.isSafeInteger(reportedCostMicros) && reportedCostMicros >= 0
? [{ dimension: 'costMicros', amount: reportedCostMicros, source: 'reported' }]
: []),
{ dimension: 'elapsedMs', value: budget.elapsedValue(), source: 'reported' },
])
}
module.exports = { recordOutboundUsage, recordReplyUsage }
@@ -0,0 +1,72 @@
const { agentStoppedError } = require('./local-workspace-inputs.cjs')
// Marks how far an invocation got before it failed, so the run can decide
// whether the operation may be retried safely.
function attachInvocationFailure(error, input) {
if (!error || (typeof error !== 'object' && typeof error !== 'function')) return error
const existing = error.invocationFailure && typeof error.invocationFailure === 'object'
? error.invocationFailure
: {}
Object.defineProperty(error, 'invocationFailure', {
value: Object.freeze({
outcomeCertainty: existing.outcomeCertainty || input.outcomeCertainty,
sideEffectsPossible: existing.sideEffectsPossible === true || input.sideEffectsPossible === true,
operationId: existing.operationId || input.operationId,
idempotencyMode: existing.idempotencyMode || input.idempotencyMode,
}),
enumerable: false,
configurable: true,
})
return error
}
// Hidden properties on an error that downstream code reads but that must not
// leak into serialised error messages.
function attachErrorMetadata(error, metadata) {
if (!error || (typeof error !== 'object' && typeof error !== 'function')) return error
for (const [name, value] of Object.entries(metadata)) {
if (!value) continue
Object.defineProperty(error, name, { value, enumerable: false, configurable: true })
}
return error
}
// Why an invocation ended and how to report it:
// - the parent run was stopped, timed out or is shutting down;
// - the watchdog cut a silent Agent off;
// - the Agent was cancelled on its own (e.g. a superseded attempt);
// - or it simply failed with `caughtError`.
// `settleOutstanding` tells the caller to wait briefly for capture and run
// promises that may still be winding down.
function resolveInvocationStop({
caughtError, signal, parentAbortObserved, activeRun, agentController, watchdog,
}) {
const parentTimedOut = Boolean(signal?.aborted && activeRun?.stopReason === 'timeout')
const parentStopped = Boolean(signal?.aborted || parentAbortObserved)
const parentInterrupted = parentStopped && activeRun?.stopReason === 'shutdown'
const agentCancelled = agentController.signal.aborted && !parentStopped && !watchdog.timedOut
let error = caughtError
let status = 'failed'
if (parentTimedOut) {
error = new Error('LOCAL_AGENT_TIMEOUT')
status = 'timeout'
} else if (parentStopped) {
error = caughtError?.message === 'LOCAL_AGENT_EXECUTION_STOPPED'
? caughtError
: agentStoppedError()
status = parentInterrupted ? 'interrupted' : 'stopped'
} else if (watchdog.timedOut) {
error = watchdog.error || new Error('LOCAL_AGENT_TIMEOUT')
status = 'timeout'
} else if (agentCancelled) {
error = agentStoppedError()
status = 'stopped'
}
return {
error,
status,
settleOutstanding: parentStopped || watchdog.timedOut || agentCancelled,
}
}
module.exports = { attachErrorMetadata, attachInvocationFailure, resolveInvocationStop }
@@ -0,0 +1,113 @@
const {
MAX_MESSAGE_ATTACHMENTS,
abortableOperation,
normalizeAttachmentMetadata,
} = require('./local-workspace-inputs.cjs')
// Absolute paths an Agent mentions in its reply. Quoted, backticked or
// Markdown-linked paths may contain spaces; bare paths end at whitespace.
function extractReportedOutputPaths(text) {
if (typeof text !== 'string') return []
const root = '\\/(?:Users|home|tmp|private\\/tmp|Volumes)\\/'
const delimited = [...text.matchAll(new RegExp(`[\`"'(<](${root}[^\`"'<>\\n)]+)[\`"')>]`, 'g'))]
.map(match => match[1])
const bare = (text.match(new RegExp(`${root}[^\\s"'<>\`\\]\\[)]+`, 'g')) || [])
.filter(value => !delimited.some(full => full !== value && full.startsWith(value)))
return [...new Set([...delimited, ...bare].map(value => value.trim().replace(/[.,;:!?]+$/, '')))]
}
// Files a writable Agent turn leaves behind: a snapshot of the workspace is
// taken before the turn and compared afterwards to import generated media and
// to describe produced artifacts. Capturing is best effort; only a stop
// request is allowed to interrupt it.
class InvocationOutputs {
constructor({ enabled, workdir, signal, agentKind, startedAt, deps }) {
this.enabled = enabled === true
this.workdir = workdir
this.signal = signal
this.agentKind = agentKind
this.startedAt = startedAt
this.deps = deps
this.baseline = null
this.artifactBaseline = null
this.capturePromise = null
this.artifactCapturePromise = null
this.importPromise = null
this.outcomePromise = null
}
// Promises that may still be running when the invocation is cut short.
pending() {
return [
this.capturePromise,
this.artifactCapturePromise,
this.importPromise,
this.outcomePromise,
].filter(Boolean)
}
async tolerate(operation) {
try {
return await abortableOperation(operation, this.signal)
} catch (error) {
if (this.signal.aborted) throw error
return undefined
}
}
async begin() {
if (!this.enabled) return
this.artifactCapturePromise = Promise.resolve().then(
() => this.deps.captureArtifactOutputs(this.workdir, { signal: this.signal }),
)
this.artifactCapturePromise.catch(() => {})
try {
this.capturePromise = Promise.resolve().then(
() => this.deps.captureAgentOutputs(this.workdir, { signal: this.signal }),
)
this.capturePromise.catch(() => {})
this.baseline = await abortableOperation(() => this.capturePromise, this.signal)
} catch (error) {
if (this.signal.aborted) throw error
}
this.artifactBaseline = (await this.tolerate(() => this.artifactCapturePromise)) ?? null
}
// Media the Agent produced during this turn, as message attachments.
async importMedia(replyText) {
if (!this.enabled) return []
this.importPromise = Promise.resolve().then(() => this.deps.importAgentOutputs({
workdir: this.workdir,
baseline: this.baseline,
startedAt: this.startedAt,
agentKind: this.agentKind,
reportedPaths: extractReportedOutputPaths(replyText),
signal: this.signal,
}))
this.importPromise.catch(() => {})
const imported = await this.tolerate(() => this.importPromise)
return (Array.isArray(imported) ? imported : [])
.slice(0, MAX_MESSAGE_ATTACHMENTS)
.map(normalizeAttachmentMetadata)
.filter(Boolean)
}
// Descriptors of the artifacts the Agent wrote, for the evidence record.
async describeOutcomes({ runId, agentRunId }) {
if (!this.enabled || !this.artifactBaseline) return []
this.outcomePromise = Promise.resolve().then(() => this.deps.captureAgentOutcomeDescriptors({
workdir: this.workdir,
baseline: this.artifactBaseline,
startedAt: this.startedAt,
agentKind: this.agentKind,
runId,
agentRunId,
signal: this.signal,
}))
this.outcomePromise.catch(() => {})
const captured = await this.tolerate(() => this.outcomePromise)
return Array.isArray(captured) ? captured : []
}
}
module.exports = { InvocationOutputs, extractReportedOutputPaths }
@@ -0,0 +1,63 @@
const { stagedAgentInputPrompt } = require('../agents/agent-input-staging.cjs')
const { directPromptFor } = require('./local-workspace-context.cjs')
const { cleanText } = require('./local-workspace-inputs.cjs')
const RESPONSE_SCOPE_INSTRUCTION = [
'Final response scope:',
'Answer only the current user task stated above.',
'Use earlier messages only as supporting evidence when they help answer that current task.',
'Do not separately mention, summarize, continue, or complete any previous user task.',
'Do not append an answer to an older task after the current-task conclusion.',
].join('\n')
// Builds the prompt one Agent receives for one turn.
// - frozen / isolated invocations send exactly the approved prompt.
// - direct chats lead with the user's request; a resumed native session
// already holds the conversation, a new or rebuilt one gets the history.
// - group turns use the V4 prompt when the orchestrator supplied one, else
// the workspace prompt for the current mode.
// Staged attachments and harness recovery instructions are appended to both.
function assembleInvocationPrompt({
group, kind, mode, threadRootId, context, activeRun, isolated, frozen,
v4Prompt, promptMode, afterKind, packed, session, stagedInputs, promptFor, agentLabel,
}) {
// Frozen and isolated prompts are fixed text. Staged attachments are the
// only thing added to a frozen one, so the Agent can open the user's files.
if (isolated || frozen) {
return [v4Prompt, stagedAgentInputPrompt(stagedInputs)].filter(Boolean).join('\n\n')
}
const runtimeInstruction = cleanText(context.runtimeInstruction, 3000)
const regenerationInstruction = cleanText(context.regenerationInstruction, 3000)
const additions = [
stagedAgentInputPrompt(stagedInputs),
runtimeInstruction ? `Harness recovery task:\n${runtimeInstruction}` : '',
regenerationInstruction ? `Fresh response instruction:\n${regenerationInstruction}` : '',
]
if (group.conversationType === 'direct') {
return [
directPromptFor({
group,
label: agentLabel(kind),
packed,
skillHints: context.skillHints || [],
knowledgeBaseHints: context.knowledgeBaseHints || [],
includeHistory: session.rotated || !session.ref,
}),
...additions,
].filter(Boolean).join('\n\n')
}
return [
context.v4 === true && v4Prompt
? v4Prompt
: promptFor(
group, kind, context.completionPolicy === 'typed' ? 'manual' : mode,
threadRootId, context.skillHints || [],
context.knowledgeBaseHints || [], afterKind, packed, promptMode,
activeRun?.unlimitedRounds === true,
),
...additions,
RESPONSE_SCOPE_INSTRUCTION,
].filter(Boolean).join('\n')
}
module.exports = { assembleInvocationPrompt }
@@ -0,0 +1,156 @@
const { createHash, randomUUID } = require('node:crypto')
const { canonicalJson } = require('../collaboration/context-pack-records.cjs')
const { nextSessionMeta, normalizeSessionMeta } = require('../runs/run-harness.cjs')
const MAX_INHERITED_TASK_IDS = 64
function sha256(value) {
return createHash('sha256').update(String(value || '')).digest('hex')
}
function createdSessionProvenance(taskId, stateless = false) {
if (stateless) {
return {
scope: 'none', reuse: false, origin: 'none', originTaskId: null,
inheritedTaskIds: [], completeness: 'complete',
}
}
return {
scope: 'conversation',
reuse: false,
origin: 'created',
originTaskId: taskId || null,
inheritedTaskIds: [],
completeness: 'complete',
}
}
function provenanceMeta(provenance) {
if (provenance.scope === 'unknown-legacy') {
return {
sessionScope: 'unknown-legacy',
originTaskId: '',
inheritedTaskIds: [],
provenanceCompleteness: 'unknown-legacy',
}
}
if (provenance.scope === 'none') return {}
return {
sessionScope: provenance.scope,
originTaskId: provenance.originTaskId || '',
inheritedTaskIds: provenance.inheritedTaskIds || [],
provenanceCompleteness: provenance.completeness,
}
}
function completedSessionMeta(meta, provenance, taskId, usage) {
let inheritedTaskIds = [...(provenance.inheritedTaskIds || [])]
let completeness = provenance.completeness
if (provenance.scope === 'conversation' && taskId
&& taskId !== provenance.originTaskId && !inheritedTaskIds.includes(taskId)) {
inheritedTaskIds.push(taskId)
if (inheritedTaskIds.length > MAX_INHERITED_TASK_IDS) {
inheritedTaskIds = inheritedTaskIds.slice(-MAX_INHERITED_TASK_IDS)
completeness = 'partial'
}
}
return nextSessionMeta(meta, {
...usage,
...provenanceMeta({ ...provenance, inheritedTaskIds, completeness }),
})
}
// Binds a pending permission or connector request to the exact native session
// it was raised in, so a decision cannot be replayed against another session.
function connectorSessionBinding(sessionRef, sessionProvenance) {
const stableProvenance = {
scope: String(sessionProvenance?.scope || 'none'),
originTaskId: String(sessionProvenance?.originTaskId || ''),
inheritedTaskIds: Array.isArray(sessionProvenance?.inheritedTaskIds)
? [...sessionProvenance.inheritedTaskIds]
: [],
completeness: String(sessionProvenance?.completeness || 'complete'),
}
return {
sessionRefHash: sha256(sessionRef),
sessionProvenanceHash: sha256(canonicalJson(stableProvenance)),
}
}
// The native Agent session one invocation runs in. A conversation owns one
// native session for its lifetime: it is resumed on every turn and rebuilt
// only when the Agent reports that the stored session is no longer valid.
class InvocationSession {
// `resolved` is what the workspace stored for this conversation; `deps`
// carries the state hooks so this class never touches workspace state itself.
constructor({ resolved, group, kind, taskId, deps }) {
this.group = group
this.kind = kind
this.taskId = taskId
this.deps = deps
this.key = resolved.key
this.storedRef = String(resolved.sessionRef || '')
this.ref = this.storedRef
this.meta = normalizeSessionMeta(resolved.sessionMeta)
this.provenance = resolved.provenance
this.rotated = resolved.sessionReset === true
this.transport = this.ref ? String(this.meta.transport || '') : ''
}
binding() {
return connectorSessionBinding(this.ref, this.provenance)
}
// Forget the native session so the next turn starts a fresh one. OpenClaw
// addresses its sessions by name, so it gets a new name straight away.
discard() {
this.meta = {}
this.provenance = createdSessionProvenance(this.taskId)
this.transport = ''
this.rotated = true
this.deps.commitSessionState((nextState) => {
delete nextState.sessions[this.key]
delete nextState.sessionMeta[this.key]
if (this.kind === 'openclaw') {
const generation = randomUUID().replace(/[^a-zA-Z0-9]/g, '').slice(0, 12) || 'session'
this.ref = this.deps.openClawSessionRef(
this.group, generation, this.group.conversationType === 'direct' ? '' : this.taskId,
)
nextState.sessions[this.key] = this.ref
nextState.sessionMeta[this.key] = normalizeSessionMeta(provenanceMeta(this.provenance))
} else {
this.ref = ''
}
})
}
// The Agent reported (or created) its native session id.
adopt(nextRef, metadata = {}) {
const transport = ['legacy', 'acp'].includes(metadata?.transport) ? metadata.transport : ''
if (transport) this.transport = transport
this.ref = String(nextRef || '')
this.deps.persistSessionState(this.key, this.ref, normalizeSessionMeta({
...this.meta,
...provenanceMeta(this.provenance),
...(this.transport ? { transport: this.transport } : {}),
}))
}
// A turn finished: record what it cost and which message it had seen.
complete(resultRef, usage) {
this.deps.persistSessionState(
this.key,
resultRef || this.ref,
completedSessionMeta(this.meta, this.provenance, this.taskId, usage),
)
}
}
module.exports = {
InvocationSession,
completedSessionMeta,
connectorSessionBinding,
createdSessionProvenance,
provenanceMeta,
sha256,
}
@@ -4,6 +4,12 @@ const {
const RECENT_VERIFICATION_MS = 24 * 60 * 60 * 1000
const AUTH_FAILURE_RETRY_MS = 30 * 1000
// A readiness probe that times out or crashes says nothing about whether the
// Agent works: it is not a "not signed in" answer. An Agent that was usable
// stays usable through such results for this long; a definitive answer
// (signed out, incompatible, a recorded auth failure) applies immediately.
const INDETERMINATE_PROBE_GRACE_MS = 10 * 60 * 1000
const INDETERMINATE_PROBE_SOURCES = new Set(['native-runtime-unavailable', 'native-probe-failed'])
function expiredAuthFailure(runtime, now) {
if (runtime?.credentialState !== 'missing') return false
@@ -43,7 +49,6 @@ class LocalWorkspaceAgentCatalog {
this.setDetectedAgents = options.setDetectedAgents
this.detectAgents = options.detectAgents
this.credentialState = options.credentialState
this.sharedProviderReady = options.sharedProviderReady
this.attachmentSupport = options.attachmentSupport || (() => ({}))
this.save = options.save
this.emitChanged = options.emitChanged
@@ -51,6 +56,7 @@ class LocalWorkspaceAgentCatalog {
this.now = options.now
this.refreshTask = null
this.refreshAgain = false
this.indeterminateSince = new Map()
}
async refresh() {
@@ -75,17 +81,9 @@ class LocalWorkspaceAgentCatalog {
async refreshOnce() {
const runtimeAtRefreshStart = new Map(Object.entries(this.state().agentRuntime))
const previousAgents = new Map(this.detectedAgents().map(agent => [agent.kind, agent]))
const detected = await this.detectAgents()
const nativeStates = await Promise.all(detected.map(async (agent) => {
const runtime = runtimeAtRefreshStart.get(agent.kind)
const sharedProviderReady = Boolean(this.sharedProviderReady(agent.kind))
if (sharedProviderReady && runtime?.credentialState === 'missing'
&& !expiredAuthFailure(runtime, this.now())) {
return { state: 'missing', source: 'runtime-auth-failure' }
}
if (sharedProviderReady) {
return { state: 'ready', source: 'shared-provider' }
}
try {
return await this.credentialState(agent.kind, agent)
} catch {
@@ -97,11 +95,9 @@ class LocalWorkspaceAgentCatalog {
const agents = detected.map((agent, index) => {
const native = nativeStates[index]
let runtime = state.agentRuntime[agent.kind]
const sharedProviderReady = Boolean(this.sharedProviderReady(agent.kind))
const nativeState = ['ready', 'missing'].includes(native?.state) ? native.state : 'unknown'
const workBuddyUnavailable = ['workbuddy-desktop-locked', 'workbuddy-model-unavailable'].includes(native?.source)
const authoritativeNativeState = native?.source === 'native-auth-status'
const sharedProviderRequired = native?.source === 'shared-provider-required'
const providerCredentialUnavailable = native?.source === 'provider-credential-unavailable'
// Give corrected native credentials a retry after the cooldown, never erase a newer failure.
if (expiredAuthFailure(runtime, this.now())
&& !(authoritativeNativeState && nativeState === 'ready')
@@ -129,39 +125,41 @@ class LocalWorkspaceAgentCatalog {
const nativeReadySource = nativeState === 'ready'
? (native.source || 'native-credential')
: ''
const retained = this.retainedAvailability({
agent, previous: previousAgents.get(agent.kind), native, runtimeMissing,
})
let credentialState = 'unknown'
if (runtimeMissing || sharedProviderRequired || nativeState === 'missing') {
if (runtimeMissing || nativeState === 'missing') {
credentialState = 'missing'
}
if (!runtimeMissing && sharedProviderReady) credentialState = 'ready'
else if (!runtimeMissing && nativeState === 'ready') credentialState = 'ready'
if (!runtimeMissing && nativeState === 'ready') credentialState = 'ready'
else if (!runtimeMissing && verifiedReady && nativeState !== 'missing') credentialState = 'ready'
if (providerCredentialUnavailable) credentialState = 'unknown'
if (retained && retained.credentialState === 'ready') credentialState = 'ready'
const installed = true
const versionIdentified = agentVersionIdentified(agent)
const compatible = agentCompatible(agent)
const nativeConfigurationUnknown = !runtimeMissing
&& !providerCredentialUnavailable
&& !workBuddyUnavailable
&& nativeState === 'unknown'
&& native?.source !== 'native-runtime-unavailable'
&& native?.source !== 'native-probe-failed'
const configured = providerCredentialUnavailable || sharedProviderReady
const configured = retained !== null
|| nativeState === 'ready'
|| native?.source === 'native-auth-status'
|| native?.source === 'native-runtime-unavailable'
|| verifiedReady
|| runtimeMissing
|| nativeConfigurationUnknown
const authenticated = !providerCredentialUnavailable && !runtimeMissing && nativeState !== 'missing' && (
sharedProviderReady || nativeState === 'ready' || verifiedReady || nativeConfigurationUnknown
)
const runtimePrerequisitesReady = native?.source !== 'native-runtime-unavailable'
&& (native?.source !== 'native-probe-failed' || recentlyVerified(runtime, this.now()))
const authenticated = retained !== null || (!runtimeMissing && nativeState !== 'missing' && (
nativeState === 'ready' || verifiedReady || nativeConfigurationUnknown
))
const runtimePrerequisitesReady = retained !== null
|| (native?.source !== 'native-runtime-unavailable'
&& (native?.source !== 'native-probe-failed' || recentlyVerified(runtime, this.now())))
const invocable = installed && versionIdentified && compatible && configured
&& authenticated && runtimePrerequisitesReady
const verifiedRecently = !providerCredentialUnavailable && (
(authoritativeNativeState && nativeState === 'ready') || recentlyVerified(runtime, this.now())
)
&& authenticated && runtimePrerequisitesReady && !workBuddyUnavailable
const verifiedRecently = (authoritativeNativeState && nativeState === 'ready')
|| recentlyVerified(runtime, this.now())
const available = invocable
const preferred = state.agentPreferences[agent.kind]?.showInSidebar
const capabilities = agentRuntimeCapabilities(agent.kind, {
@@ -170,14 +168,15 @@ class LocalWorkspaceAgentCatalog {
})
let availabilitySource = 'unverified'
if (!compatible) availabilitySource = 'incompatible'
else if (providerCredentialUnavailable) availabilitySource = 'provider-credential-unavailable'
else if (runtimeMissing) availabilitySource = 'runtime-auth-failure'
else if (sharedProviderReady) availabilitySource = nativeReadySource || 'shared-provider'
else if (nativeState === 'missing') availabilitySource = native.source || 'none'
else if (native?.source === 'native-probe-failed') availabilitySource = 'native-probe-failed'
else if (native?.source === 'native-runtime-unavailable') availabilitySource = 'native-runtime-unavailable'
else if (nativeReadySource) availabilitySource = nativeReadySource
else if (verifiedReady) availabilitySource = 'verified-run'
else if (nativeConfigurationUnknown) availabilitySource = 'local-cli'
if (retained) availabilitySource = retained.availabilitySource
if (workBuddyUnavailable) availabilitySource = native.source
return {
...agent,
installed,
@@ -202,11 +201,32 @@ class LocalWorkspaceAgentCatalog {
return this.snapshot()
}
// The state an Agent keeps while a readiness probe is inconclusive, or null
// when the probe result must be taken at face value.
retainedAvailability({ agent, previous, native, runtimeMissing }) {
if (!INDETERMINATE_PROBE_SOURCES.has(native?.source)) {
this.indeterminateSince.delete(agent.kind)
return null
}
const nowMs = Date.parse(String(this.now()))
if (!this.indeterminateSince.has(agent.kind) && Number.isFinite(nowMs)) {
this.indeterminateSince.set(agent.kind, nowMs)
}
const since = this.indeterminateSince.get(agent.kind)
if (previous?.available !== true || runtimeMissing || !Number.isFinite(nowMs)
|| !Number.isFinite(since) || nowMs - since >= INDETERMINATE_PROBE_GRACE_MS
|| !agentVersionIdentified(agent) || !agentCompatible(agent)) return null
return {
credentialState: previous.credentialState,
availabilitySource: previous.availabilitySource || 'native-probe-failed',
}
}
setSidebarVisibility(kind, visible) {
const agent = this.detectedAgents().find(item => item.kind === kind)
if (!agent) throw new Error('LOCAL_AGENT_NOT_INSTALLED')
const state = this.state()
state.agentPreferences[kind] = { showInSidebar: Boolean(visible) }
state.agentPreferences[kind] = { ...state.agentPreferences[kind], showInSidebar: Boolean(visible) }
agent.showInSidebar = Boolean(visible)
this.save()
this.emitChanged()
@@ -219,9 +239,9 @@ class LocalWorkspaceAgentCatalog {
credentialState,
checkedAt: this.now(),
}
if (credentialState === 'ready') this.indeterminateSince.delete(kind)
const agent = this.detectedAgents().find(item => item.kind === kind)
if (agent && agent.availabilitySource !== 'provider-credential-unavailable'
&& !(credentialState === 'unknown' && agent.authenticated === true)) {
if (agent && !(credentialState === 'unknown' && agent.authenticated === true)) {
agent.credentialState = credentialState
agent.versionIdentified = agentVersionIdentified(agent)
agent.compatible = agentCompatible(agent)
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,261 @@
// Collaboration records written while an automatic run progresses (roles, objectives, results).
const { createHash } = require('node:crypto')
const {
appendBlackboardEntry,
appendHandoff,
collaborationPackageText,
emptyCollaborationState,
publicCollaborationText,
roleForIndex,
visibleBlackboardEntries,
} = require('../collaboration/collaboration-records.cjs')
const { createTaskGraphCursor } = require('../collaboration/task-graph-records.cjs')
const collaborationMethods = {
orchestrationCursor(targetKinds, taskGraph = null) {
if (taskGraph) {
return {
version: 3,
workflow: 'auto',
template: 'task-graph',
currentKind: '',
pendingKinds: [...targetKinds],
activeKinds: [...targetKinds],
successfulKinds: [],
agreementKinds: [],
attachmentRecipients: [],
totalSuccesses: 0,
terminalFailureOccurred: false,
collaboration: emptyCollaborationState(),
taskGraph: createTaskGraphCursor(taskGraph),
}
}
return {
version: 2,
workflow: 'auto',
currentKind: '',
pendingKinds: [...targetKinds],
activeKinds: [...targetKinds],
successfulKinds: [],
agreementKinds: [],
attachmentRecipients: [],
totalSuccesses: 0,
terminalFailureOccurred: false,
collaboration: emptyCollaborationState(),
}
},
collaborationRole(activeKinds, kind) {
return roleForIndex(Math.max(0, activeKinds.indexOf(kind)))
},
collaborationAudience(role) {
return {
roles: role === 'primary'
? ['reviewer', 'arbiter']
: (role === 'reviewer' ? ['primary', 'arbiter'] : ['primary', 'reviewer']),
agentKinds: [],
}
},
collaborationAcceptance(role) {
if (role === 'reviewer') {
return [
'Check the selected claims and outcome references.',
'Identify unsupported assumptions, conflicts, or missing evidence.',
]
}
if (role === 'arbiter') {
return [
'Resolve visible conflicts using the selected evidence.',
'Return one bounded conclusion and state whether consensus is reached.',
]
}
return [
'Produce one concrete conclusion for the current task.',
'Publish only claims and durable outcome references needed downstream.',
]
},
collaborationExpectedOutput(role) {
if (role === 'reviewer') return 'A concise review with corrections or explicit acceptance.'
if (role === 'arbiter') return 'A final adjudication of the selected claims and conflicts.'
return 'A concise primary conclusion with any durable Artifact or Evidence references.'
},
collaborationObjective(group, controller, threadRootId) {
const root = this.state().messages.find(message => (
message.groupId === group.id && message.id === threadRootId && message.role === 'user'
))
return publicCollaborationText(root?.content || 'Complete the current user task.', 3000)
|| 'Complete the current user task.'
},
prepareCollaborationPackage(
group, controller, activeKinds, kind, threadRootId, options = {},
) {
const collaboration = controller.orchestration?.collaboration || emptyCollaborationState()
const destination = {
agentKind: kind,
role: options.role || this.collaborationRole(activeKinds, kind),
}
const selectedEntryIds = Array.isArray(options.selectedEntryIds)
? new Set(options.selectedEntryIds)
: null
const selectedEntries = selectedEntryIds
? collaboration.entries.filter(entry => selectedEntryIds.has(entry.entryId))
: visibleBlackboardEntries(collaboration, destination)
const objective = this.collaborationObjective(group, controller, threadRootId)
const expectedOutput = options.expectedOutput
|| this.collaborationExpectedOutput(destination.role)
const acceptanceCriteria = options.acceptanceCriteria
|| this.collaborationAcceptance(destination.role)
const selectedIds = selectedEntries.map(entry => entry.entryId)
const reusable = [...collaboration.handoffs].reverse().find(handoff => (
handoff.destination.agentKind === destination.agentKind
&& handoff.destination.role === destination.role
&& handoff.objective === objective
&& handoff.expectedOutput === expectedOutput
&& handoff.selectedEntryIds.length === selectedIds.length
&& handoff.selectedEntryIds.every((id, index) => id === selectedIds[index])
&& handoff.acceptanceCriteria.length === acceptanceCriteria.length
&& handoff.acceptanceCriteria.every((value, index) => value === acceptanceCriteria[index])
))
if (reusable) {
return {
handoff: reusable,
entries: selectedEntries,
text: collaborationPackageText(reusable, selectedEntries),
}
}
const previousOwner = [...collaboration.entries].reverse().find(entry => (
entry.owner.type === 'agent'
))?.owner
const createdAt = Date.now()
const next = appendHandoff(collaboration, {
source: previousOwner || { type: 'harness' },
destination,
objective,
selectedEntryIds: selectedIds,
expectedOutput,
acceptanceCriteria,
provenance: {
runId: controller.runId,
taskId: controller.taskId,
round: controller.currentRound,
agentRunId: null,
artifactIds: [],
evidenceIds: [],
},
createdAt,
})
const handoff = next.handoffs.at(-1)
this.checkpointOrchestration(group, controller, { collaboration: next })
return {
handoff,
entries: selectedEntries,
text: collaborationPackageText(handoff, selectedEntries),
}
},
recordCollaborationResult(group, controller, activeKinds, kind, result, options = {}) {
const role = options.role || this.collaborationRole(activeKinds, kind)
const conclusion = publicCollaborationText(result?.message?.content || '')
const trace = result?.message?.trace
const provenance = {
runId: controller.runId,
taskId: controller.taskId,
round: controller.currentRound,
agentRunId: trace?.agentRunId || null,
artifactIds: result?.outcomeRefs?.artifactIds || [],
evidenceIds: result?.outcomeRefs?.evidenceIds || [],
}
let collaboration = controller.orchestration?.collaboration || emptyCollaborationState()
const previousCollaboration = collaboration
const previousEntryIds = new Set(collaboration.entries.map(entry => entry.entryId))
let sequence = collaboration.entries.length + 1
const recordedAt = Date.now()
const conclusionValue = conclusion
? createHash('sha256').update(conclusion).digest('hex')
: ''
const entryType = options.entryType || 'claim'
const subject = options.subject || `task:${controller.taskId}:conclusion`
const entryRefs = Array.isArray(options.refs) ? options.refs : []
const duplicateConclusion = conclusion && collaboration.entries.some(entry => (
entry.entryType === entryType
&& entry.lifecycle.state === 'active'
&& entry.subject === subject
&& entry.value === conclusionValue
&& entry.owner.type === 'agent'
&& entry.owner.agentKind === kind
))
if (conclusion && !duplicateConclusion) {
collaboration = appendBlackboardEntry(collaboration, {
entryType,
subject,
statement: conclusion,
value: entryType === 'claim' ? conclusionValue : '',
owner: { type: 'agent', agentKind: kind, role },
audience: this.collaborationAudience(role),
lifecycle: {
state: 'active', sequence, recordedAt, supersedesEntryId: null,
},
provenance,
refs: entryRefs,
})
sequence = collaboration.entries.length + 1
}
for (const artifactId of duplicateConclusion ? [] : provenance.artifactIds.slice(0, 1)) {
collaboration = appendBlackboardEntry(collaboration, {
entryType: 'artifact-ref',
subject: `task:${controller.taskId}:artifact`,
statement: 'Durable Artifact produced by the Agent.',
value: '',
owner: { type: 'agent', agentKind: kind, role },
audience: this.collaborationAudience(role),
lifecycle: {
state: 'active', sequence, recordedAt, supersedesEntryId: null,
},
provenance,
refs: [artifactId],
})
sequence = collaboration.entries.length + 1
}
for (const evidenceId of duplicateConclusion ? [] : provenance.evidenceIds.slice(0, 1)) {
collaboration = appendBlackboardEntry(collaboration, {
entryType: 'evidence-ref',
subject: `task:${controller.taskId}:evidence`,
statement: 'Durable Evidence recorded for the Agent conclusion.',
value: '',
owner: { type: 'agent', agentKind: kind, role },
audience: this.collaborationAudience(role),
lifecycle: {
state: 'active', sequence, recordedAt, supersedesEntryId: null,
},
provenance,
refs: [evidenceId],
})
sequence = collaboration.entries.length + 1
}
if (collaboration !== previousCollaboration) {
this.checkpointOrchestration(group, controller, { collaboration })
}
return collaboration.entries
.filter(entry => !previousEntryIds.has(entry.entryId))
.map(entry => entry.entryId)
},
checkpointOrchestration(group, controller, updates = {}) {
const previous = controller.orchestration
controller.orchestration = { ...previous, ...updates }
// A thrown callback has an unknown write outcome; only explicit failure is safe to roll back.
const persisted = this.checkpointRun?.(group.id, controller)
if (this.hasRunLedger() && persisted !== true) {
controller.orchestration = previous
if (previous?.version === 4) controller.currentRound = previous.round
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
},
}
module.exports = { collaborationMethods }
@@ -0,0 +1,135 @@
const { hashValue } = require('../collaboration/orchestration-v4-records.cjs')
const { createHash } = require('node:crypto')
const fs = require('node:fs')
const path = require('node:path')
// Rebuild consensus from committed, operation-bound messages. No in-memory vote
// can survive a crash without its corresponding durable Agent response.
const naturalConsensusMethods = {
v4NaturalConsensus(group, controller, threadRootId) {
const messages = this.state().messages.filter(message => (
message.groupId === group.id && message.threadRootId === threadRootId
&& message.role === 'agent'
&& this.v4NaturalMessageMatchesBinding(message, controller)
)).sort((a, b) => (a.turnSeq || 0) - (b.turnSeq || 0))
let candidate = null
const files = new Map()
for (const message of messages) {
const artifactIds = message.trace?.context?.outcomeRefs?.artifactIds || []
for (const artifactId of artifactIds) {
const { artifact, contentHash } = this.v4ArtifactIdentity(artifactId)
if (artifact.locationRef?.kind === 'workspace-relative') {
if (candidate?.files.some(file => file.path === artifact.locationRef.path
&& file.contentHash !== contentHash)) candidate = null
files.set(artifact.locationRef.path, { path: artifact.locationRef.path,
artifactId, contentHash, size: artifact.contentRef.size })
}
}
if (message.trace?.phase !== 'discussion') continue
const decision = message.trace?.context?.taskDecision
if (!decision) continue
if (decision.status !== 'completed') {
candidate = null
continue
}
if (decision.reviewedVersion) {
if (candidate && decision.reviewedVersion === candidate.version) {
candidate.approvedKinds.add(message.agentKind)
}
continue
}
// An unbound confirmation cannot replace the version being reviewed.
// Revisions must first withdraw it with a non-completed decision.
if (candidate) continue
const contribution = this.v4NaturalMessageContent(message)
if (contribution.partial || !contribution.text.trim()) {
candidate = null
continue
}
const candidateFiles = [...files.values()]
candidate = {
version: hashValue({ messageId: message.id, text: contribution.text, artifactIds, files: candidateFiles }),
message,
text: contribution.text,
artifactIds,
files: candidateFiles,
approvedKinds: new Set([message.agentKind]),
}
}
// Votes refer to captured bytes, not just a filename. Workspace edits made
// outside the discussion must not inherit approval of the previous bytes.
if (candidate?.files.length) {
try {
const root = fs.realpathSync(group.workdir)
for (const file of candidate.files) {
const filename = fs.realpathSync(path.resolve(root, file.path))
const relative = path.relative(root, filename)
if (!relative || relative === '..' || relative.startsWith(`..${path.sep}`)
|| path.isAbsolute(relative) || fs.statSync(filename).size !== file.size
|| createHash('sha256').update(fs.readFileSync(filename)).digest('hex') !== file.contentHash) return null
}
} catch { return null }
}
return candidate
},
v4NaturalConsensusPrompt(group, controller, threadRootId) {
const candidate = this.v4NaturalConsensus(group, controller, threadRootId)
if (!candidate) return ''
return [
`Candidate version: ${candidate.version}`,
`Proposed by @${candidate.message.agentKind}. Review this exact complete candidate, including its deliverable files, before accepting.`,
`Candidate deliverables: ${JSON.stringify(candidate.message.trace.context.taskDecision.deliverables)}`,
`Immutable artifact references: ${JSON.stringify(candidate.artifactIds)}`,
`Deliverable file versions (read each file and verify its contents): ${JSON.stringify(candidate.files)}`,
'Complete candidate answer (not a transcript excerpt):',
candidate.text,
`To accept this version, use status completed and reviewedVersion "${candidate.version}" in your receipt. Do not rewrite or replace the candidate while accepting it.`,
'If anything needs changing, use status continue, explain the defect and request the peer who should fix it. This withdraws the candidate and all its approvals; the revised result needs fresh approval from everyone.',
'Do not modify this candidate while accepting it. Missing, stale or implicit approval never counts as acceptance.',
].join('\n\n')
},
async v4NaturalAgentLedOutcome(group, controller, threadRootId, round, activeKinds) {
const messages = this.v4NaturalDiscussionRoundMessages(group, controller, threadRootId, round)
const candidate = this.v4NaturalConsensus(group, controller, threadRootId)
if (candidate) {
const missing = controller.targetKinds.filter(kind => !candidate.approvedKinds.has(kind))
if (!missing.length) return { status: 'completed' }
const available = missing.filter(kind => activeKinds.includes(kind))
if (available.length) return { nextKinds: available }
return { status: 'partial' }
}
const decisionMessage = messages.at(-1)
const decision = decisionMessage?.trace?.context?.taskDecision
const route = this.v4NaturalRouteDecision(decision, activeKinds)
if (route.dropped.length) {
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoRouteIgnored',
'Requests for unavailable participants were skipped.',
{ agents: route.dropped.map(kind => this.agentLabel(kind)).join(', ') })
}
if (decision?.status === 'needs-human' && this.requestTaskDecision) {
const response = await this.requestTaskDecision(group, controller, decisionMessage)
if (response?.status === 'approved') return { nextKinds: [decisionMessage.agentKind] }
if (response?.status === 'rejected') {
controller.stopReason = 'human_gate_rejected'
return { status: 'stopped' }
}
}
if (['blocked', 'needs-human'].includes(decision?.status)) {
this.addMessage(group.id, 'system', decision.reason, '', threadRootId, {
key: decision.status === 'needs-human' ? 'system.autoTaskNeedsHuman' : 'system.autoTaskBlocked',
params: { reason: decision.reason },
})
return { status: 'partial' }
}
const nextKinds = this.v4NaturalNextKinds(group, controller, threadRootId, round, activeKinds)
if (nextKinds.length) return { nextKinds }
// A missing receipt is not approval. Ask the current speaker to finish
// their handoff or propose a concrete candidate instead of inventing votes.
return { nextKinds: [], fallbackKind: activeKinds.includes(decisionMessage?.agentKind)
? decisionMessage.agentKind : activeKinds[0] }
},
}
module.exports = { naturalConsensusMethods }
@@ -1,61 +1,117 @@
const { parseTaskDecision } = require('../collaboration/task-decision.cjs')
const { parseLenientTaskDecision } = require('../collaboration/task-decision.cjs')
const v4NaturalDecisionMethods = {
v4NaturalRouteDecision(decision, activeKinds) {
const participants = Array.isArray(activeKinds) ? activeKinds : []
if (decision == null) return { status: 'none', kinds: [] }
if (decision == null) return { status: 'none', kinds: [], dropped: [] }
let parsed
try { parsed = parseTaskDecision(decision) }
catch { return { status: 'invalid', kinds: [] } }
try { parsed = parseLenientTaskDecision(decision) }
catch { return { status: 'none', kinds: [], dropped: [] } }
const requested = parsed.nextKinds || []
if (requested.some(kind => !participants.includes(kind))) return { status: 'invalid', kinds: [] }
if (!requested.length) return { status: 'none', kinds: [] }
return { status: 'valid', kinds: participants.filter(kind => requested.includes(kind)) }
// A request for an absent or paused participant is dropped on its own;
// it used to stop the whole discussion with a misleading message.
const dropped = requested.filter(kind => !participants.includes(kind))
const kinds = requested.filter(kind => participants.includes(kind))
return { status: kinds.length ? 'valid' : 'none', kinds, dropped }
},
v4NaturalNotice(group, controller, threadRootId, key, text, params = {}) {
const runParams = { ...params, runId: controller.runId }
const duplicate = this.state().messages.some(message => (
message.threadRootId === threadRootId
&& message.system?.key === key
&& JSON.stringify(message.system?.params || {}) === JSON.stringify(runParams)
))
if (!duplicate) {
this.addMessage(group.id, 'system', text, '', threadRootId, { key, params: runParams })
}
},
async v4NaturalRoundOutcome(group, controller, threadRootId, round, activeKinds) {
const messages = this.v4NaturalDiscussionRoundMessages(group, controller, threadRootId, round)
const owner = this.v4NaturalOwner(group, controller, threadRootId)
if (!activeKinds.length) return { status: 'partial' }
const nominalOwner = this.v4NaturalOwner(group, controller, threadRootId)
const owner = this.v4NaturalOwner(group, controller, threadRootId, activeKinds)
if (owner !== nominalOwner) {
// The owner failed or was paused: the next selected participant takes
// over instead of the discussion ending silently as partial.
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoOwnerChanged',
`${this.agentLabel(owner)} took over as delivery owner because ${this.agentLabel(nominalOwner)} is no longer available.`,
{ agent: this.agentLabel(owner), previous: this.agentLabel(nominalOwner) })
}
const routes = messages.map(message => this.v4NaturalRouteDecision(
message.trace?.context?.taskDecision, activeKinds,
))
if (routes.some(route => route.status === 'invalid')) {
this.addMessage(group.id, 'system', 'Discussion stopped without a valid task completion decision.',
'', threadRootId, { key: 'system.autoTaskDecisionMissing' })
return { status: 'partial' }
const dropped = [...new Set(routes.flatMap(route => route.dropped || []))]
if (dropped.length) {
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoRouteIgnored',
`Requests for ${dropped.map(kind => this.agentLabel(kind)).join(', ')} were skipped because they are not active in this discussion.`,
{ agents: dropped.map(kind => this.agentLabel(kind)).join(', ') })
}
// A peer's explicit blocker is a task outcome, not an empty routing choice.
// Without this guard the delivery owner could complete the run while a
// participant had already reported that the work was blocked.
const ownerMessage = messages.find(message => message.agentKind === owner)
// A peer's blocker is input for the delivery owner, who judges the whole
// task. It used to end the discussion as partial on the spot.
const peerBlocker = messages.find(message => {
if (message.agentKind === owner) return false
const status = message.trace?.context?.taskDecision?.status
return status === 'blocked' || status === 'needs-human'
})
if (peerBlocker) {
const decision = peerBlocker.trace?.context?.taskDecision
this.addMessage(group.id, 'system', decision.reason, '', threadRootId, {
key: decision.status === 'needs-human' ? 'system.autoTaskNeedsHuman' : 'system.autoTaskBlocked',
params: { reason: decision.reason },
})
return { status: 'partial' }
const ownerSawBlocker = peerBlocker && ownerMessage
&& controller.discussionStyle === 'sequential'
&& messages.indexOf(ownerMessage) > messages.indexOf(peerBlocker)
if (peerBlocker && !ownerSawBlocker) {
const peerDecision = peerBlocker.trace?.context?.taskDecision
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoPeerBlockerForwarded',
`${this.agentLabel(peerBlocker.agentKind)} reported a blocker; the delivery owner will decide how to proceed.`,
{ agent: this.agentLabel(peerBlocker.agentKind), reason: peerDecision.reason })
return { nextKinds: [owner], ownerReview: true }
}
const nextKinds = this.v4NaturalNextKinds(group, controller, threadRootId, round, activeKinds)
if (nextKinds.length) return { nextKinds, ownerReview: false }
if (!activeKinds.includes(owner)) return { status: 'partial' }
const ownerMessage = messages.find(message => message.agentKind === owner)
const decision = ownerMessage?.trace?.context?.taskDecision
if (decision?.handoffTo && !activeKinds.includes(decision.handoffTo)) return { status: 'partial' }
if (decision?.handoffTo && !activeKinds.includes(decision.handoffTo)) {
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoHandoffIgnored',
`The handoff to ${this.agentLabel(decision.handoffTo)} was ignored because that Agent is not active; ${this.agentLabel(owner)} remains the delivery owner.`,
{ agent: this.agentLabel(decision.handoffTo), owner: this.agentLabel(owner) })
}
if (decision?.status === 'continue') return { nextKinds: [owner], ownerReview: false }
const sawAllContributions = messages.length === 1
|| (controller.discussionStyle === 'sequential' && messages.at(-1)?.agentKind === owner)
if (!ownerMessage || !sawAllContributions) return { nextKinds: [owner], ownerReview: true }
if (!decision) {
// Ask the owner once more for a judgment before giving up: a missing
// or unreadable receipt is usually a formatting slip, not a verdict.
if (!this.v4NaturalOwnerMissedDecisionBefore(group, controller, threadRootId, round, owner)) {
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoTaskDecisionRequested',
`${this.agentLabel(owner)} did not include a readable task decision; asking for one before continuing.`,
{ agent: this.agentLabel(owner), round })
return { nextKinds: [owner], ownerReview: true }
}
this.addMessage(group.id, 'system', 'Discussion stopped without a valid task completion decision.',
'', threadRootId, { key: 'system.autoTaskDecisionMissing' })
return { status: 'partial' }
}
if (decision.status === 'completed') return { status: controller.failedKinds.length ? 'partial' : 'completed' }
if (decision.status === 'completed') {
// The owner may not close the task before selected peers had one discussion
// round of their own; otherwise a single full round collapses into an
// owner-only review and peers never get to challenge the result.
if (activeKinds.some(kind => kind !== owner)
&& !this.v4NaturalPriorPeerDiscussion(group, controller, threadRootId, round, owner)) {
this.addMessage(
group.id, 'system',
'The delivery owner signaled completion before peer discussion; running one cross-review round first.',
'', threadRootId, { key: 'system.autoCrossReview' },
)
return { nextKinds: [...activeKinds], ownerReview: false }
}
if (controller.failedKinds.length) {
this.v4NaturalNotice(group, controller, threadRootId, 'system.autoCompletedWithout',
`The delivery owner completed the task without ${controller.failedKinds.map(kind => this.agentLabel(kind)).join(', ')}, which stopped earlier in this discussion.`,
{ agents: controller.failedKinds.map(kind => this.agentLabel(kind)).join(', ') })
return { status: 'partial' }
}
return { status: 'completed' }
}
if (decision.status === 'needs-human' && this.requestTaskDecision) {
const response = await this.requestTaskDecision(group, controller, ownerMessage)
if (response?.status === 'approved') return { nextKinds: [owner], ownerReview: true }
@@ -78,10 +134,40 @@ const v4NaturalDecisionMethods = {
.filter(kind => kind !== message.agentKind)
)))
if (!selected.size) return []
return activeKinds.filter(kind => selected.has(kind))
return [...selected]
},
v4NaturalOwner(group, controller, threadRootId) {
v4NaturalPriorPeerDiscussion(group, controller, threadRootId, round, owner) {
return this.state().messages.some(message => (
message.groupId === group.id
&& message.threadRootId === threadRootId
&& message.role === 'agent'
&& message.agentKind !== owner
&& message.trace?.runId === controller.runId
&& message.trace?.phase === 'discussion'
&& Number(message.trace?.round) < round
&& this.v4NaturalMessageMatchesBinding(message, controller)
))
},
v4NaturalOwnerMissedDecisionBefore(group, controller, threadRootId, round, owner) {
const earlier = this.state().messages.filter(message => (
message.groupId === group.id
&& message.threadRootId === threadRootId
&& message.role === 'agent'
&& message.agentKind === owner
&& message.trace?.runId === controller.runId
&& message.trace?.phase === 'discussion'
&& Number(message.trace?.round) < round
&& this.v4NaturalMessageMatchesBinding(message, controller)
))
const previous = earlier.at(-1)
return Boolean(previous && !previous.trace?.context?.taskDecision)
},
// With activeKinds, an owner who failed or was paused hands ownership to
// the first still-active selected participant.
v4NaturalOwner(group, controller, threadRootId, activeKinds = null) {
let owner = controller.targetKinds[0] || ''
for (const message of this.state().messages) {
if (message.groupId !== group.id || message.threadRootId !== threadRootId
@@ -92,6 +178,9 @@ const v4NaturalDecisionMethods = {
if (decision?.status === 'continue' && decision.handoffTo
&& controller.targetKinds.includes(decision.handoffTo)) owner = decision.handoffTo
}
if (Array.isArray(activeKinds) && activeKinds.length && !activeKinds.includes(owner)) {
return controller.targetKinds.find(kind => activeKinds.includes(kind)) || owner
}
return owner
},
@@ -111,9 +200,19 @@ const v4NaturalDecisionMethods = {
|| messageRound > round || !this.v4NaturalMessageMatchesBinding(message, controller)) continue
const contribution = this.v4NaturalMessageContent(message)
if (contribution.partial) return false
const content = contribution.text
const text = contribution.text
.replace(/@[A-Za-z0-9][A-Za-z0-9_-]*/gu, '')
.replace(/\s+/gu, ' ').trim()
const files = []
for (const artifactId of message.trace?.context?.outcomeRefs?.artifactIds || []) {
try {
const { artifact, contentHash } = this.v4ArtifactIdentity(artifactId)
if (artifact.locationRef?.kind === 'workspace-relative') {
files.push([artifact.locationRef.path, contentHash])
}
} catch { return false }
}
const content = JSON.stringify([text, message.trace?.context?.taskDecision?.reviewedVersion || '', files.sort()])
if (previous.has(message.agentKind) && previous.get(message.agentKind) !== content) return false
if (messageRound >= firstRound) {
observedRounds.add(messageRound)
@@ -0,0 +1,557 @@
// Natural discussion, the production path for automatic group runs: transcript, prompts and the sequential and agent-led loops.
const path = require('node:path')
const { MAX_MESSAGE_TEXT_CHARS } = require('./local-workspace-contracts.cjs')
const { hashValue } = require('../collaboration/orchestration-v4-records.cjs')
const { packNaturalDiscussionTranscript, v4Prompt } = require('./local-workspace-context.cjs')
const { naturalDiscussionPrompt } = require('./prompts/natural-discussion-prompt.cjs')
const { terminalRunStatusForReason } = require('./local-workspace-auto-runner-support.cjs')
const { traceCapsuleFromAgentRun } = require('../runs/run-harness-normalization.cjs')
const naturalDiscussionMethods = {
v4NaturalMessageMatchesBinding(message, controller) {
const phase = String(message?.trace?.phase || '')
const kind = String(message?.agentKind || '')
const round = Number(message?.trace?.round) || 0
if (!['proposal', 'discussion'].includes(phase) || !kind || round < 1) return false
if (message.trace?.runId !== controller.runId) return false
const slot = controller.orchestration?.slots?.find(candidate => candidate.agentKind === kind)
const snapshotHash = String(controller.orchestration?.snapshotHash || '')
if (!slot?.slotId || !snapshotHash) return false
const operationPhase = phase === 'discussion' ? `discussion:${round}` : phase
const expectedOperationId = this.v4OperationId(
controller, kind, operationPhase, slot.slotId, round,
)
return message.trace?.context?.operationId === expectedOperationId
&& message.trace?.context?.snapshotHash === snapshotHash
},
v4NaturalMessageContent(message) {
const text = String(message.content || '')
if (text.length < MAX_MESSAGE_TEXT_CHARS && message.trace?.truncated !== true) {
return { text, partial: false }
}
for (const artifactId of message.trace?.context?.outcomeRefs?.artifactIds || []) {
try {
const { artifact, content } = this.v4ArtifactIdentity(artifactId)
if (artifact.name !== `${message.agentKind}-conclusion.txt`
|| artifact.producedBy?.runId !== message.trace?.runId
|| artifact.producedBy?.agentRunId !== message.trace?.agentRunId
|| artifact.producedBy?.agentKind !== message.agentKind
|| !content.startsWith(text)) continue
return { text: content, partial: false }
} catch { /* Historical blobs may be unavailable; identify the excerpt below. */ }
}
return { text, partial: true }
},
v4NaturalThreadEntries(group, threadRootId, options = {}) {
const rounds = Array.isArray(options.rounds) ? new Set(options.rounds) : null
const entries = this.state().messages.filter(message => (
message.groupId === group.id
&& message.threadRootId === threadRootId
&& message.role === 'agent'
&& message.agentKind
&& (!rounds || rounds.has(Number(message.trace?.round) || 0))
&& (!options.controller || this.v4NaturalMessageMatchesBinding(
message, options.controller,
))
)).map((message, index) => ({ message, index }))
// Peers read turns in dispatch order, the same order the user sees,
// instead of the order replies happened to be saved in.
.sort((left, right) => {
const leftTurn = Number.isSafeInteger(left.message.turnSeq) ? left.message.turnSeq : 0
const rightTurn = Number.isSafeInteger(right.message.turnSeq) ? right.message.turnSeq : 0
return (leftTurn ? 1 : 0) - (rightTurn ? 1 : 0)
|| leftTurn - rightTurn
|| left.index - right.index
})
.map(({ message }) => message)
.map(message => ({
...this.v4NaturalMessageContent(message),
id: message.id,
agentKind: message.agentKind,
round: Number(message.trace?.round) || 0,
phase: message.trace?.phase || 'proposal',
operationId: message.trace?.context?.operationId || '',
})).map(entry => ({
// Peers collaborating on the same workspace must be able to name its
// files; keep workspace paths readable as relative paths.
...entry, text: this.v4SanitizeDeliveryText(entry.text, Number.MAX_SAFE_INTEGER, {
workspaceRoot: group.workdir ? path.resolve(group.workdir) : '',
}),
}))
return entries.map(entry => ({ ...entry, hash: hashValue(entry) }))
},
v4NaturalThreadTranscript(group, threadRootId, options = {}) {
return packNaturalDiscussionTranscript(this.v4NaturalThreadEntries(group, threadRootId, options))
},
v4NaturalDeliveryPrompt(group, controller, threadRootId, kind, slot, binding, promptBuilder) {
const entries = this.v4NaturalThreadEntries(group, threadRootId, { controller })
const previousEntries = binding.hasSession && !binding.sessionRotated
? (controller.orchestration?.deliveryState || []).filter(entry => (
entry.recipientKind === kind && entry.status === 'acknowledged'
&& entry.sessionRefHash === binding.sessionRefHash
&& entry.sessionProvenanceHash === binding.sessionProvenanceHash
&& entry.snapshotHash === controller.orchestration.snapshotHash
&& entry.sourceMessages
))
: []
const seen = new Set(previousEntries.flatMap(entry => entry.sourceMessages.map(message => (
[entry.sourceAgentKind, entry.sourcePhase, message.id, message.hash].join('\u0000')
))))
const unseen = entries.filter(entry => !seen.has([
entry.agentKind, entry.phase, entry.id, entry.hash,
].join('\u0000')))
const fullOpening = controller.discussionStyle === 'agent-led' && controller.currentRound === 2
? entries.filter(entry => entry.phase === 'proposal') : null
const packed = fullOpening ? {
text: fullOpening.map(entry => `Round 1 - @${entry.agentKind} [${entry.id}]\n${entry.text}`).join('\n\n'),
completeMessageIds: fullOpening.filter(entry => !entry.partial).map(entry => entry.id),
} : packNaturalDiscussionTranscript(unseen, {
withEntries: true,
prefix: unseen.length < entries.length
? 'Previously delivered turns are not repeated here. They were supplied to this native session; ask peers to restate missing evidence if your current context is incomplete. Their omission does not imply agreement or task completion.'
: '',
})
const included = new Set(packed.completeMessageIds)
const sources = new Map()
for (const entry of unseen) {
if (!included.has(entry.id)) continue
const key = `${entry.agentKind}\u0000${entry.phase}`
const source = sources.get(key) || {
sourceAgentKind: entry.agentKind, sourcePhase: entry.phase,
watermark: 0, operationId: entry.operationId, sourceMessages: [],
}
source.watermark = Math.max(source.watermark, entry.round)
source.sourceMessages.push({ id: entry.id, hash: entry.hash })
sources.set(key, source)
}
const packageHash = hashValue(packed.text)
const deliveryId = `delivery-${hashValue({
runId: controller.runId, kind, operationId: slot.operationId,
packageHash, binding, now: Date.now(),
})}`
const delivery = sources.size ? {
natural: true, previousEntries, packageHash, deliveryId,
entries: [...sources.values()].map(source => ({
...source, recipientKind: kind,
sessionRefHash: binding.sessionRefHash,
sessionProvenanceHash: binding.sessionProvenanceHash,
snapshotHash: controller.orchestration.snapshotHash,
packageHash, deliveryId, status: 'prepared', updatedAt: Date.now(),
})),
} : null
this.v4PrepareDelivery(group, controller, delivery)
return { prompt: promptBuilder(packed.text), delivery }
},
v4NaturalRecoveredPhaseResult(group, controller, threadRootId, phase, kind, slot) {
if (!this.naturalAgentResponses || !slot || controller.currentRound < 1) return null
if (!['proposal', 'discussion'].includes(phase)) return null
const operationId = String(slot.operationId || '')
const snapshotHash = String(slot.snapshotHash || '')
if (!operationId || !snapshotHash) return null
const matchesBinding = context => (
context?.operationId === operationId && context?.snapshotHash === snapshotHash
)
const scopedMessages = [...this.state().messages].reverse().filter(message => (
message.groupId === group.id
&& message.threadRootId === threadRootId
&& message.role === 'agent'
&& message.agentKind === kind
&& message.trace?.runId === controller.runId
&& message.trace?.phase === phase
&& Number(message.trace?.round) === controller.currentRound
))
if (scopedMessages.some(message => !matchesBinding(message.trace?.context))) {
throw new Error('LOCAL_RUN_COLLABORATION_SCOPE_INVALID')
}
const existingMessage = scopedMessages.find(message => matchesBinding(message.trace?.context))
let content = String(existingMessage?.content || '')
let trace = existingMessage?.trace || null
let metadata = existingMessage ? {
...(existingMessage.elapsedMs != null ? { elapsedMs: existingMessage.elapsedMs } : {}),
...(Array.isArray(existingMessage.toolCalls) ? { toolCalls: existingMessage.toolCalls } : {}),
...(Array.isArray(existingMessage.attachments)
? { attachments: existingMessage.attachments } : {}),
...(existingMessage.responseVersionRootId
? { responseVersionRootId: existingMessage.responseVersionRootId } : {}),
trace,
} : null
if (!content || !trace?.agentRunId) {
const scopedRuns = [...(controller.harness?.snapshot?.() || [])].reverse().filter(run => (
run.kind === kind
&& run.round === controller.currentRound
&& run.status === 'completed'
&& String(run.output || '')
))
if (scopedRuns.some(run => !matchesBinding(run.context))) {
throw new Error('LOCAL_RUN_COLLABORATION_SCOPE_INVALID')
}
const recoveredRun = scopedRuns.find(run => matchesBinding(run.context))
if (!recoveredRun) return null
content = String(recoveredRun.output)
trace = traceCapsuleFromAgentRun(recoveredRun, {
runId: controller.runId,
status: recoveredRun.status,
phase,
})
if (!trace?.agentRunId) return null
metadata = { trace }
}
return {
text: content,
pendingMessage: {
groupId: group.id,
role: 'agent',
content,
agentKind: kind,
threadRootId,
system: null,
metadata,
},
collaboration: trace.context?.taskDecision
? { summary: trace.context.taskDecision.reason, taskDecision: trace.context.taskDecision }
: null,
outcomeRefs: trace.context?.outcomeRefs || {},
operationId: slot.operationId,
consensus: false,
}
},
v4NaturalRecoveredDiscussionResult(group, controller, threadRootId, kind, slot) {
return this.v4NaturalRecoveredPhaseResult(
group, controller, threadRootId, 'discussion', kind, slot,
)
},
v4NaturalPhasePrompt(group, kind, phase, snapshot, receiptRecords, activeKinds, options = {}) {
const humanResponses = options.runId ? this.taskDecisionResponses(options.runId) : []
return naturalDiscussionPrompt({
brief: v4Prompt({
group, kind, phase, snapshot, role: 'participant',
skillHints: options.skillHints,
knowledgeBaseHints: options.knowledgeBaseHints,
naturalResponse: true,
}),
group,
kind,
phase,
activeKinds,
transcript: String(options.transcript || ''),
humanResponsesText: humanResponses.length ? packNaturalDiscussionTranscript(humanResponses) : '',
writerKind: group.allowWrite === true ? kind : '',
ownerKind: options.ownerKind,
allowRouting: options.allowRouting === true,
consensusText: options.consensusText || '',
})
},
v4NaturalDiscussionOperationIds(controller, slots, kinds, round) {
return new Map(kinds.map((kind) => {
const slot = slots.find(candidate => candidate.agentKind === kind)
return [kind, this.v4OperationId(
controller, kind, `discussion:${round}`, slot?.slotId || '', round,
)]
}))
},
v4NaturalDiscussionRoundMessages(group, controller, threadRootId, round) {
return this.state().messages.filter(message => (
message.groupId === group.id
&& message.threadRootId === threadRootId
&& message.role === 'agent'
&& message.trace?.runId === controller.runId
&& message.trace?.phase === 'discussion'
&& Number(message.trace?.round) === round
&& this.v4NaturalMessageMatchesBinding(message, controller)
))
},
async runV4SequentialDiscussion(group, controller, threadRootId, context, input) {
const {
targetKinds, activeKinds: initialActiveKinds, writerKind, batchId,
snapshot, snapshotRecord, snapshotHash, slots: initialSlots, receiptRecords,
checkpointPhase, removePhaseFailures, addRoundLimitNotice,
} = input
let activeKinds = [...initialActiveKinds]
let slots = initialSlots
let round = Math.max(1, Number(controller.currentRound) || 1)
let pendingKinds = controller.orchestration?.discussionStyle === 'sequential'
&& controller.orchestration?.phase === 'discussion'
&& controller.orchestration?.round === round
? (controller.orchestration.pendingKinds || []).filter(kind => activeKinds.includes(kind))
: [...activeKinds]
if (!pendingKinds.length && controller.orchestration?.phase === 'discussion') {
const outcome = await this.v4NaturalRoundOutcome(group, controller, threadRootId, round, activeKinds)
if (outcome.status) return outcome.status
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
round += 1
pendingKinds = outcome.ownerReview ? outcome.nextKinds : [...activeKinds]
}
while (!controller.signal.aborted) {
controller.currentRound = round
while (pendingKinds.length && !controller.signal.aborted) {
const kind = pendingKinds[0]
const remainingKinds = pendingKinds.slice(1)
const dispatch = await this.runV4Phase(group, controller, threadRootId, context, {
phase: 'discussion',
activeKinds: [kind],
targetKinds,
participantKinds: activeKinds,
writerKind,
batchId,
snapshot,
snapshotRecord,
snapshotHash,
slots,
receiptRecords,
challengeBindings: null,
coordinationPlan: null,
workReceipts: [],
operationIds: this.v4NaturalDiscussionOperationIds(
controller, slots, [kind], round,
),
pendingKinds,
remainingKinds,
sessionPolicy: null,
resumedGate: input.resumedGate,
disableDeliveryPrompt: true,
promptBuilder: ({ kind: promptKind, transcript }) => this.v4NaturalPhasePrompt(
group, promptKind, 'discussion', snapshot, receiptRecords, activeKinds, {
runId: controller.runId,
transcript: transcript ?? this.v4NaturalThreadTranscript(
group, threadRootId, { controller },
),
skillHints: context.rootSkillsByKind.get(promptKind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(promptKind) || [],
ownerKind: this.v4NaturalOwner(group, controller, threadRootId, activeKinds),
writerKind,
allowRouting: false,
},
),
})
slots = dispatch.slots || slots
if (!dispatch.ok) {
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
const failedKinds = removePhaseFailures(dispatch)
activeKinds = activeKinds.filter(agentKind => !failedKinds.includes(agentKind))
if (!activeKinds.length) {
return dispatch.phasePendingMessages?.length ? 'partial' : 'failed'
}
}
pendingKinds = remainingKinds.filter(agentKind => activeKinds.includes(agentKind))
}
const outcome = await this.v4NaturalRoundOutcome(group, controller, threadRootId, round, activeKinds)
if (outcome.status) return outcome.status
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
round += 1
controller.currentRound = round
pendingKinds = outcome.ownerReview ? outcome.nextKinds : [...activeKinds]
}
return terminalRunStatusForReason(controller.stopReason)
},
async runV4AgentLedDiscussion(group, controller, threadRootId, context, input) {
const {
targetKinds, activeKinds: initialActiveKinds, batchId,
snapshot, snapshotRecord, snapshotHash, slots: initialSlots, receiptRecords,
latestFor, runPhase, checkpointPhase, removePhaseFailures, addRoundLimitNotice,
} = input
let activeKinds = [...initialActiveKinds]
let slots = initialSlots
let round = Math.max(1, Number(controller.currentRound) || 1)
const proposalCompleteBeforeRun = activeKinds.every(kind => Boolean(latestFor('proposal', kind)))
if (!proposalCompleteBeforeRun) {
controller.currentRound = 1
const proposal = await runPhase('proposal', activeKinds)
slots = proposal.slots || slots
if (!proposal.ok) {
const failedKinds = removePhaseFailures(proposal)
activeKinds = activeKinds.filter(kind => !failedKinds.includes(kind))
checkpointPhase('proposal', [], slots, null)
if (!activeKinds.length) return proposal.phasePendingMessages?.length ? 'partial' : 'failed'
}
}
const proposalComplete = activeKinds.every(kind => Boolean(latestFor('proposal', kind)))
if (!proposalComplete) {
return activeKinds.some(kind => Boolean(latestFor('proposal', kind))) ? 'partial' : 'failed'
}
if (!controller.unlimitedRounds && (controller.maxRounds || 6) <= 1) {
addRoundLimitNotice()
return 'round-limit'
}
let nextKinds = []
if (controller.orchestration?.phase === 'discussion') {
round = Math.max(2, Number(controller.orchestration.round) || 2)
nextKinds = (controller.orchestration.pendingKinds || [])
.filter(kind => activeKinds.includes(kind))
if (!nextKinds.length) {
if (this.v4NaturalStopRepeatingDiscussion(
group, controller, threadRootId, round, activeKinds,
)) return 'partial'
const outcome = await this.v4NaturalAgentLedOutcome(group, controller, threadRootId, round, activeKinds)
if (outcome.status) return outcome.status
nextKinds = outcome.nextKinds.length ? outcome.nextKinds : [outcome.fallbackKind]
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
round += 1
}
} else {
round = 2
nextKinds = [activeKinds[0]]
}
while (!controller.signal.aborted) {
controller.currentRound = round
const kind = nextKinds[0]
const remainingKinds = nextKinds.slice(1)
const candidate = this.v4NaturalConsensus(group, controller, threadRootId)
const turnWriterKind = !candidate && group.allowWrite === true && this.canWriteWorkspace(kind)
? kind : ''
const dispatch = await this.runV4Phase(group, controller, threadRootId, context, {
phase: 'discussion',
activeKinds: [kind],
targetKinds,
participantKinds: activeKinds,
writerKind: turnWriterKind,
batchId,
snapshot,
snapshotRecord,
snapshotHash,
slots,
receiptRecords,
challengeBindings: null,
coordinationPlan: null,
workReceipts: [],
operationIds: this.v4NaturalDiscussionOperationIds(
controller, slots, [kind], round,
),
pendingKinds: nextKinds,
remainingKinds,
sessionPolicy: null,
resumedGate: input.resumedGate,
disableDeliveryPrompt: true,
promptBuilder: ({ kind, transcript }) => this.v4NaturalPhasePrompt(
group, kind, 'discussion', snapshot, receiptRecords, activeKinds, {
runId: controller.runId,
transcript: transcript ?? this.v4NaturalThreadTranscript(group, threadRootId, { controller }),
skillHints: context.rootSkillsByKind.get(kind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(kind) || [],
allowRouting: true,
writerKind: turnWriterKind,
consensusText: this.v4NaturalConsensusPrompt(group, controller, threadRootId),
},
),
})
slots = dispatch.slots || slots
if (!dispatch.ok) {
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
const failedKinds = removePhaseFailures(dispatch)
activeKinds = activeKinds.filter(kind => !failedKinds.includes(kind))
if (!activeKinds.length) return dispatch.phasePendingMessages?.length ? 'partial' : 'failed'
}
if (this.v4NaturalStopRepeatingDiscussion(
group, controller, threadRootId, round, activeKinds,
)) return 'partial'
const outcome = await this.v4NaturalAgentLedOutcome(group, controller, threadRootId, round, activeKinds)
if (outcome.status) return outcome.status
// A new candidate starts its own confirmation queue. An objection
// cancels pending approvals; otherwise preserve outstanding invitations.
nextKinds = this.v4NaturalConsensus(group, controller, threadRootId) || candidate
? outcome.nextKinds
: [...new Set([...outcome.nextKinds, ...remainingKinds])]
nextKinds = nextKinds.filter(agentKind => activeKinds.includes(agentKind))
if (!nextKinds.length) nextKinds = [outcome.fallbackKind || activeKinds[0]]
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
round += 1
controller.currentRound = round
}
return terminalRunStatusForReason(controller.stopReason)
},
async runV4NaturalDiscussion(group, controller, threadRootId, context, input) {
if (input.resume && ['proposal', 'discussion'].includes(controller.orchestration?.phase)) {
for (const slot of input.slots) {
if (slot.permission !== 'workspace-write' || !slot.agentRunId
|| slot.commitStatus === 'committed'
|| !['running', 'waiting', 'stopped', 'failed'].includes(slot.status)) continue
if (this.v4NaturalRecoveredPhaseResult(group, controller, threadRootId,
controller.orchestration.phase, slot.agentKind, slot)) continue
if (input.resumedGate?.operationId === slot.operationId
&& input.resumedGate.agentKind === slot.agentKind
&& input.resumedGate.status === 'approved') continue
if (!this.requestHumanGate) throw new Error('LOCAL_RUN_RETRY_GATE_INVALID')
const binding = {
agentRunId: slot.agentRunId, agentKind: slot.agentKind,
round: controller.currentRound, phase: controller.orchestration.phase, slotId: slot.slotId,
operationId: slot.operationId, snapshotHash: input.snapshotHash,
}
const request = {
phase: 'discussion-writer-recovery', batchId: input.batchId,
slotId: slot.slotId, operationId: slot.operationId, attempt: slot.attempt,
outcomeCertainty: 'unknown_outcome', sideEffectsPossible: true,
}
const gate = await this.requestHumanGate({
type: 'retry', runId: controller.runId, agentRunId: slot.agentRunId,
agentKind: slot.agentKind,
summary: 'The interrupted Agent may already have changed the workspace.',
options: [
{ optionId: 'retry-once', name: 'Retry once', kind: 'allow_once' },
{ optionId: 'cancel-retry', name: 'Do not retry', kind: 'reject_once' },
],
request,
}, {
signal: controller.signal,
preserveOnAbort: () => controller.stopReason === 'shutdown',
continuation: { resumeKind: 'agent_slot', ...binding },
})
if (gate.status !== 'approved') {
if (this.completeHumanGateContinuation?.(controller.runId, gate.gateId, 'cancelled') !== true
&& this.hasRunLedger()) throw new Error('LOCAL_RUN_PERSIST_FAILED')
controller.stopReason = 'human_gate_rejected'
return 'stopped'
}
if (gate.optionId !== 'retry-once') throw new Error('LOCAL_RUN_CONTINUATION_INVALID')
if (this.completeHumanGateContinuation?.(controller.runId, gate.gateId, 'completed') !== true
&& this.hasRunLedger()) throw new Error('LOCAL_RUN_PERSIST_FAILED')
input.resumedGate = { ...binding, gateId: gate.gateId, type: 'retry',
status: gate.status, optionId: gate.optionId, request, used: true }
}
}
if (input.resumedGate?.type === 'retry'
&& input.resumedGate.request?.phase === 'discussion-writer-recovery') {
const gate = input.resumedGate
const slot = input.slots.find(item => item.slotId === gate.slotId)
if (gate.status !== 'approved' || gate.optionId !== 'retry-once'
|| !slot || slot.agentKind !== gate.agentKind
|| slot.operationId !== gate.operationId
|| slot.snapshotHash !== gate.snapshotHash
|| slot.attempt !== gate.request.attempt) throw new Error('LOCAL_RUN_CONTINUATION_INVALID')
if (!gate.used && this.completeHumanGateContinuation?.(controller.runId, gate.gateId, 'completed') !== true
&& this.hasRunLedger()) throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
const style = controller.orchestration?.discussionStyle
|| controller.discussionStyle
|| 'sequential'
return style === 'agent-led'
? this.runV4AgentLedDiscussion(group, controller, threadRootId, context, input)
: this.runV4SequentialDiscussion(group, controller, threadRootId, context, input)
},
}
module.exports = { naturalDiscussionMethods }
@@ -0,0 +1,443 @@
// Failed, interrupted and unauthorized invocations: retry policy, pausing an Agent for the run, and recovery prompts.
const { createHash } = require('node:crypto')
const {
MAX_RUN_AGENT_ATTEMPTS,
boundedBackoffDelay,
normalizeAttemptHistoryEntry,
normalizeFailure,
normalizeFailureOutcome,
retryDecision,
} = require('../runs/failure-policy.cjs')
const {
circuitBreakerError,
sanitizedAuthenticationError,
unauthorizedFailure,
} = require('./local-workspace-auto-runner-support.cjs')
const recoveryMethods = {
setCurrentAgent(controller, kind) {
controller.currentKind = kind
controller.progress = []
this.emitChanged()
},
pauseFailedAgentForRun(group, controller, kind, threadRootId, error = null) {
// Failures are scoped to the current run. Group membership is user
// configuration: editing it after a provider or login hiccup made the
// Agent disappear from every later conversation turn, and removing the
// last member made the whole group (and its history) unloadable.
if (group.conversationType === 'direct' || !group.agentKinds.includes(kind)) return false
if (!controller.failedKinds.includes(kind)) controller.failedKinds.push(kind)
const alreadyReported = this.state().messages.some(message => (
message.threadRootId === threadRootId
&& message.agentKind === kind
&& message.system?.key === 'system.agentPausedAfterFailure'
&& message.system?.params?.runId === controller.runId
))
if (!alreadyReported) {
const agent = this.agentLabel(kind)
const reason = normalizeFailure(error).code || 'LOCAL_AGENT_PROCESS_FAILED'
this.addMessage(
group.id,
'system',
`${agent} is paused for the rest of this discussion (${reason}). It stays in the group and will be tried again on the next message.`,
kind,
threadRootId,
{
key: 'system.agentPausedAfterFailure',
params: { agent, reason, runId: controller.runId },
},
)
}
return true
},
shouldPauseFailedAgent(error) {
const category = normalizeFailure(error).category
// Runtime, provider, rate-limit, protocol, and timeout failures stay
// silent run-scoped exclusions; their own failure message explains them.
// Authentication and compatibility failures get an explicit notice
// because the user usually has to act (sign in or update the CLI).
return ['authentication', 'compatibility'].includes(category)
},
replacementInstruction(failedKind) {
return [
`You are replacing ${this.agentLabel(failedKind)} for this turn.`,
'Complete the interrupted Agent slot using the shared task context, then return your own conclusion.',
'Do not claim that the interrupted Agent completed this work.',
].join('\n')
},
retryDelay(failedAttempt) {
return boundedBackoffDelay(failedAttempt, {
baseDelayMs: this.retryBaseDelayMs,
maxDelayMs: this.retryMaxDelayMs,
})
},
operationIdFor(controller, kind, mode) {
const digest = createHash('sha256').update(JSON.stringify({
runId: controller.runId,
kind,
mode,
round: controller.currentRound || 0,
})).digest('hex')
return `agent-operation-${digest}`
},
recordAttempt(group, controller, input) {
const history = Array.isArray(controller.attemptHistory) ? controller.attemptHistory : []
const previousSequence = history.at(-1)?.sequence || 0
const entry = normalizeAttemptHistoryEntry({
sequence: previousSequence + 1,
agentKind: input.agentKind,
phase: input.phase,
attempt: input.attempt,
failureCategory: input.failureCategory ?? null,
policyAction: input.policyAction,
backoffMs: input.backoffMs || 0,
recoveryAgentKind: input.recoveryAgentKind || '',
finalOutcome: input.finalOutcome,
timestamp: Date.now(),
...(input.outcomeCertainty ? { outcomeCertainty: input.outcomeCertainty } : {}),
...(Object.hasOwn(input, 'sideEffectsPossible')
? { sideEffectsPossible: input.sideEffectsPossible }
: {}),
...(input.operationId ? { operationId: input.operationId } : {}),
...(input.idempotencyMode ? { idempotencyMode: input.idempotencyMode } : {}),
})
if (!entry) throw new Error('LOCAL_RUN_ATTEMPT_INVALID')
controller.attemptHistory = [...history, entry].slice(-256)
const persisted = this.checkpointRun?.(group.id, controller)
if (this.hasRunLedger() && persisted !== true) {
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
return entry
},
async prepareRetry(group, kind, controller, failedAttempt, options = {}) {
if (options.resetSession === true) {
this.resetAgentSession(group, kind, true, controller.taskId)
}
this.markRuntimeCredential(kind, 'unknown')
try { await this.refreshAgents() } catch {
if (controller.signal.aborted) throw new Error('LOCAL_AGENT_EXECUTION_STOPPED')
}
const delayMs = Number.isFinite(options.delayMs)
? Math.max(0, Math.floor(options.delayMs))
: this.retryDelay(failedAttempt)
await this.sleep(delayMs, controller.signal)
return delayMs
},
async invokeTransiently(invokeSource, group, kind, controller, options = {}) {
const operationId = options.operationId
const idempotencyMode = options.idempotencyMode === 'durable' ? 'durable' : 'none'
const sideEffectsPossible = options.sideEffectsPossible === true
for (let attempt = 1; ; attempt += 1) {
if (controller.attemptHistory.length >= MAX_RUN_AGENT_ATTEMPTS) {
throw circuitBreakerError()
}
const phase = attempt === 1 ? (options.phase || 'initial') : 'transient_retry'
const phaseAttempt = attempt === 1 ? (options.attempt || 1) : attempt - 1
try {
const result = await invokeSource()
this.recordAttempt(group, controller, {
agentKind: options.agentKind || kind,
phase,
attempt: phaseAttempt,
failureCategory: null,
policyAction: options.successAction || 'complete',
backoffMs: options.successBackoffMs || 0,
recoveryAgentKind: options.recoveryAgentKind || '',
finalOutcome: 'succeeded',
sideEffectsPossible,
operationId,
idempotencyMode,
})
return result
} catch (error) {
if (controller.signal.aborted) {
this.recordAttempt(group, controller, {
agentKind: options.agentKind || kind,
phase,
attempt: phaseAttempt,
failureCategory: 'cancellation',
policyAction: 'cancel',
recoveryAgentKind: options.recoveryAgentKind || '',
finalOutcome: 'cancelled',
})
throw error
}
if (unauthorizedFailure(error)) throw error
const failure = normalizeFailure(error)
const safety = normalizeFailureOutcome(error, {
category: failure.category,
sideEffectsPossible,
operationId,
idempotencyMode,
})
if (failure.category === 'cancellation') throw error
if (options.deferFailurePolicy === true) {
this.recordAttempt(group, controller, {
agentKind: options.agentKind || kind,
phase,
attempt: phaseAttempt,
failureCategory: failure.category,
policyAction: 'fail',
backoffMs: 0,
recoveryAgentKind: '',
finalOutcome: 'failed',
...safety,
})
throw error
}
// Session recovery is owned by LocalWorkspaceAgentInvocation so a stale
// native Session gets exactly one fresh attempt across every run mode.
// An ambiguous refusal (HTTP 403, proxy or region block) gets exactly
// one more attempt: a transient gateway refusal recovers, while a real
// permission problem fails fast without signing the Agent out.
const maxAttempts = failure.category === 'timeout' && options.retryTimeouts !== true
? 1
: failure.code === 'LOCAL_AGENT_ACCESS_DENIED' ? 2 : 4
const decision = retryDecision(failure, {
attempt,
maxAttempts,
baseDelayMs: this.retryBaseDelayMs,
maxDelayMs: this.retryMaxDelayMs,
rateLimitMinDelayMs: this.retryRateLimitMinDelayMs,
safety,
})
if (decision.action === 'human_gate') {
this.recordAttempt(group, controller, {
agentKind: options.agentKind || kind,
phase,
attempt: phaseAttempt,
failureCategory: failure.category,
policyAction: 'human_gate',
backoffMs: decision.delayMs,
recoveryAgentKind: options.recoveryAgentKind || '',
finalOutcome: 'failed',
...safety,
})
if (!this.requestHumanGate || !error?.runTrace?.agentRunId) {
throw new Error('LOCAL_RUN_RETRY_GATE_INVALID')
}
const gate = await this.requestHumanGate({
type: 'retry',
runId: controller.runId,
agentRunId: error.runTrace.agentRunId,
agentKind: options.agentKind || kind,
summary: 'The previous write-capable Agent attempt may already have changed the workspace.',
options: [
{ optionId: 'retry-once', name: 'Retry once', kind: 'allow_once' },
{ optionId: 'cancel-retry', name: 'Do not retry', kind: 'reject_once' },
],
request: {
failureCategory: failure.category,
outcomeCertainty: safety.outcomeCertainty,
sideEffectsPossible: safety.sideEffectsPossible,
operationId: safety.operationId,
idempotencyMode: safety.idempotencyMode,
},
}, {
signal: controller.signal,
preserveOnAbort: () => controller.stopReason === 'shutdown',
continuation: {
resumeKind: 'agent_slot',
agentRunId: error.runTrace.agentRunId,
agentKind: options.agentKind || kind,
round: controller.currentRound || 0,
...(options.v4AgentSlotBinding || {}),
},
})
if (gate.status !== 'approved') {
this.completeHumanGateContinuation?.(controller.runId, gate.gateId, 'cancelled')
controller.stopReason = 'human_gate_rejected'
controller.abort()
throw new Error('LOCAL_AGENT_EXECUTION_STOPPED')
}
if (this.completeHumanGateContinuation?.(
controller.runId, gate.gateId, 'completed',
) !== true && this.hasRunLedger()) {
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
this.markRuntimeCredential(kind, 'unknown')
try { await this.refreshAgents() } catch {
if (controller.signal.aborted) throw new Error('LOCAL_AGENT_EXECUTION_STOPPED')
}
await this.sleep(decision.delayMs, controller.signal)
continue
}
if (decision.action !== 'retry') {
this.recordAttempt(group, controller, {
agentKind: options.agentKind || kind,
phase,
attempt: phaseAttempt,
failureCategory: failure.category,
policyAction: options.failureAction
|| (decision.action === 'refresh_session' ? 'refresh_session' : 'fail'),
backoffMs: options.failureBackoffMs || 0,
recoveryAgentKind: options.recoveryAgentKind || '',
finalOutcome: 'failed',
...safety,
})
throw error
}
this.recordAttempt(group, controller, {
agentKind: options.agentKind || kind,
phase,
attempt: phaseAttempt,
failureCategory: failure.category,
policyAction: 'retry',
backoffMs: decision.delayMs,
recoveryAgentKind: options.recoveryAgentKind || '',
finalOutcome: 'failed',
...safety,
})
// Transient retries keep the Agent's readiness untouched. Refreshing
// the whole catalog here re-ran discovery and credential probes while
// the Agent was busy and could flip a healthy Agent to unavailable.
await this.sleep(decision.delayMs, controller.signal)
}
}
},
async controlAfterFailure(group, kind, controller, error) {
const request = this.consumeAgentControl(controller, kind)
if (!request) return null
const failure = normalizeFailure(error)
const manualAttempt = (controller.manualRetryCounts.get(kind) || 0) + 1
if (request.action === 'retry') {
controller.manualRetryCounts.set(kind, manualAttempt)
const delayMs = this.retryDelay(manualAttempt)
this.recordAttempt(group, controller, {
agentKind: kind,
phase: 'manual_retry',
attempt: manualAttempt,
failureCategory: failure.category,
policyAction: 'retry',
backoffMs: delayMs,
finalOutcome: 'failed',
})
await this.prepareRetry(group, kind, controller, manualAttempt, {
resetSession: true,
delayMs,
})
return { action: 'retry' }
}
this.recordAttempt(group, controller, {
agentKind: kind,
phase: 'manual_retry',
attempt: manualAttempt,
failureCategory: failure.category,
policyAction: request.action === 'replace' ? 'replace_agent' : 'cancel',
recoveryAgentKind: request.replacementKind || '',
finalOutcome: request.action === 'replace' ? 'replaced' : 'cancelled',
})
return request
},
async invokeWithUnauthorizedRecovery(input) {
const { controller, kind } = input
if (controller.attemptHistory.length >= MAX_RUN_AGENT_ATTEMPTS) {
throw circuitBreakerError()
}
if (controller.agentSlotKinds.has(kind)) {
throw new Error('LOCAL_AGENT_ATTEMPT_RUNNING')
}
controller.agentSlotKinds.add(kind)
try {
return await this.invokeWithUnauthorizedRecoveryInSlot(input)
} finally {
controller.agentSlotKinds.delete(kind)
}
},
async invokeWithUnauthorizedRecoveryInSlot({
group,
kind,
controller,
threadRootId,
context,
mode = 'auto',
}) {
const requestedOperationId = String(context?.operationId || '')
const operationId = /^[A-Za-z0-9._:-]{1,120}$/.test(requestedOperationId)
? requestedOperationId
: this.operationIdFor(controller, kind, mode)
const contract = this.retryContract(kind) || {}
const idempotencyMode = contract.idempotencyMode === 'durable' ? 'durable' : 'none'
const sideEffectsPossible = group.allowWrite === true
&& context.permissionMode !== 'read-only'
const invokeSource = async () => {
if (context.parallelGraph !== true) this.setCurrentAgent(controller, kind)
return this.invokeAgent(group, kind, mode, controller.signal, threadRootId, {
...context,
yolo: context.yolo ?? this.defaultYolo,
deferCredentialFailure: true,
operationId,
})
}
let phase = 'initial'
let phaseAttempt = 1
while (true) {
try {
return {
result: await this.invokeTransiently(invokeSource, group, kind, controller, {
phase,
attempt: phaseAttempt,
operationId,
idempotencyMode,
sideEffectsPossible,
deferFailurePolicy: context.v4SynthesisRecovery === true
|| (context.v4 === true && context.resumedGate?.type === 'permission'),
// A timed-out automatic slot is a failed participant for this
// round. Retrying it serially delays every healthy peer and
// defeats failure isolation; manual/direct invocations retain
// their existing timeout retry policy.
retryTimeouts: mode !== 'auto',
v4AgentSlotBinding: mode === 'auto' && context.v4 === true ? {
phase: context.phase,
slotId: context.slotId,
operationId,
snapshotHash: context.snapshotHash,
} : null,
}),
removed: false,
}
} catch (error) {
if (controller.signal.aborted) throw error
const control = await this.controlAfterFailure(group, kind, controller, error)
if (control?.action === 'retry') {
phase = 'manual_retry'
phaseAttempt = controller.manualRetryCounts.get(kind) || 1
continue
}
if (control) return { result: null, removed: false, control, error }
if (!unauthorizedFailure(error)) throw error
// Only explicit sign-in failures reach this point. Mark the Agent as
// needing a fresh login; the catalog clears it on the next refresh
// after the retry window or as soon as native credentials verify.
this.markRuntimeCredential(kind, 'missing')
this.recordAttempt(group, controller, {
agentKind: kind,
phase,
attempt: phaseAttempt,
failureCategory: 'authentication',
policyAction: 'fail',
finalOutcome: 'failed',
...normalizeFailureOutcome(error, {
sideEffectsPossible: group.allowWrite === true,
operationId,
idempotencyMode,
}),
})
throw sanitizedAuthenticationError(error)
}
}
},
}
module.exports = { recoveryMethods }
@@ -0,0 +1,313 @@
// V2 round-based runs. Kept so older runs can be resumed; new runs do not use it.
const { MAX_RUN_AGENT_ATTEMPTS } = require('../runs/failure-policy.cjs')
const {
circuitBreakerError,
circuitBreakerFailure,
hardBudgetFailure,
terminalRunStatusForReason,
} = require('./local-workspace-auto-runner-support.cjs')
const roundsMethods = {
async runRounds(group, controller, threadRootId, maxRounds, context, resume = false) {
const {
rootAttachments,
rootSkillsByKind,
rootKnowledgeBasesByKind,
} = context
const cursor = resume ? controller.orchestration : null
const attachmentRecipients = new Set(cursor?.attachmentRecipients || [])
let terminalFailureOccurred = cursor?.terminalFailureOccurred === true
let activeKinds = cursor ? [...cursor.activeKinds] : [...controller.targetKinds]
let executionSequence = Number(cursor?.executionSequence) || 0
let totalSuccesses = cursor?.totalSuccesses || 0
let consensusReached = false
const reportedFailures = new Set()
const firstRound = resume ? Math.max(0, controller.currentRound - 1) : 0
for (
let round = firstRound;
(controller.unlimitedRounds || round < maxRounds) && !controller.signal.aborted;
round += 1
) {
const resumedRound = resume && round === firstRound
const successfulKinds = new Set(resumedRound ? cursor.successfulKinds : [])
const agreementKinds = new Set(resumedRound ? cursor.agreementKinds : [])
const replacementInstructions = new Map()
const roundQueue = resumedRound ? [...cursor.pendingKinds] : [...activeKinds]
controller.currentRound = round + 1
controller.completedKinds = resumedRound
? activeKinds.filter(kind => !roundQueue.includes(kind))
: []
controller.failedKinds = resumedRound
? controller.completedKinds.filter(kind => !successfulKinds.has(kind))
: []
if (!resumedRound) {
this.checkpointOrchestration(group, controller, {
currentKind: '',
pendingKinds: [...roundQueue],
activeKinds: [...activeKinds],
successfulKinds: [],
agreementKinds: [],
attachmentRecipients: [...attachmentRecipients],
totalSuccesses,
terminalFailureOccurred,
executionSequence,
})
}
this.emitChanged()
while (roundQueue.length) {
const kind = roundQueue.shift()
if (!activeKinds.includes(kind)) {
this.checkpointOrchestration(group, controller, { pendingKinds: [...roundQueue] })
continue
}
if (controller.signal.aborted) break
const executionKind = kind
executionSequence += 1
this.setCurrentAgent(controller, executionKind)
this.checkpointOrchestration(group, controller, {
currentKind: executionKind,
pendingKinds: [...roundQueue],
activeKinds: [...activeKinds],
successfulKinds: [...successfulKinds],
agreementKinds: [...agreementKinds],
attachmentRecipients: [...attachmentRecipients],
totalSuccesses,
terminalFailureOccurred,
executionSequence,
})
try {
if (controller.attemptHistory.length >= MAX_RUN_AGENT_ATTEMPTS) {
throw circuitBreakerError()
}
const collaborationPackage = this.prepareCollaborationPackage(
group, controller, activeKinds, executionKind, threadRootId,
)
const attachments = attachmentRecipients.has(executionKind)
? []
: rootAttachments.map(attachment => attachment.path)
const invocation = await this.invokeWithUnauthorizedRecovery({
group,
kind: executionKind,
controller,
activeKinds,
threadRootId,
context: {
attachments,
attachmentSnapshots: attachments.length ? rootAttachments : [],
skillHints: rootSkillsByKind.get(executionKind) || [],
knowledgeBaseHints: rootKnowledgeBasesByKind.get(executionKind) || [],
runtimeInstruction: replacementInstructions.get(executionKind) || '',
collaborationPackage,
contextOptions: {
focusUserMessageId: threadRootId,
omitAgentThreadRootId: threadRootId,
},
executionSequence,
},
reportedFailures,
})
replacementInstructions.delete(executionKind)
if (invocation.control?.action === 'replace') {
this.recordAgentInterruption(
group.id, executionKind, invocation.error, threadRootId, 'stopped', reportedFailures,
)
if (!controller.failedKinds.includes(executionKind)) {
controller.failedKinds.push(executionKind)
}
if (!controller.completedKinds.includes(executionKind)) {
controller.completedKinds.push(executionKind)
}
successfulKinds.delete(executionKind)
agreementKinds.delete(executionKind)
const replacementKind = invocation.control.replacementKind
activeKinds = activeKinds.filter(activeKind => activeKind !== executionKind)
replacementInstructions.set(
replacementKind,
this.replacementInstruction(executionKind),
)
if (!roundQueue.includes(replacementKind)) roundQueue.unshift(replacementKind)
controller.currentKind = ''
controller.progress = []
this.checkpointOrchestration(group, controller, {
currentKind: '',
pendingKinds: [...roundQueue],
activeKinds: [...activeKinds],
successfulKinds: [...successfulKinds],
agreementKinds: [...agreementKinds],
})
this.emitChanged()
continue
}
if (invocation.control?.action === 'cancel') {
this.recordAgentInterruption(
group.id, executionKind, invocation.error, threadRootId, 'stopped', reportedFailures,
)
if (!controller.failedKinds.includes(executionKind)) {
controller.failedKinds.push(executionKind)
}
if (!controller.completedKinds.includes(executionKind)) {
controller.completedKinds.push(executionKind)
}
successfulKinds.delete(executionKind)
agreementKinds.delete(executionKind)
controller.currentKind = ''
controller.progress = []
this.checkpointOrchestration(group, controller, {
currentKind: '',
pendingKinds: [...roundQueue],
successfulKinds: [...successfulKinds],
agreementKinds: [...agreementKinds],
})
this.emitChanged()
continue
}
const result = invocation.result
if (attachments.length) attachmentRecipients.add(executionKind)
successfulKinds.add(executionKind)
if (result.consensus) agreementKinds.add(executionKind)
else agreementKinds.delete(executionKind)
this.recordCollaborationResult(
group, controller, activeKinds, executionKind, result,
)
} catch (error) {
if (circuitBreakerFailure(error)) {
controller.stopReason = 'circuit_breaker'
controller.abort()
break
}
const hardBudget = hardBudgetFailure(error)
if (hardBudget) {
this.recordAgentFailure(
group.id, executionKind, error, threadRootId, reportedFailures,
)
successfulKinds.delete(executionKind)
agreementKinds.delete(executionKind)
if (!controller.failedKinds.includes(executionKind)) {
controller.failedKinds.push(executionKind)
}
if (!controller.completedKinds.includes(executionKind)) {
controller.completedKinds.push(executionKind)
}
terminalFailureOccurred = true
controller.stopReason = 'hard_budget'
controller.abort()
break
}
if (controller.signal.aborted) {
const interruptedKind = controller.currentKind || executionKind
if (error?.runTrace) {
this.recordAgentInterruption(
group.id,
interruptedKind,
error,
threadRootId,
controller.stopReason === 'shutdown' ? 'interrupted' : 'stopped',
reportedFailures,
)
if (!controller.completedKinds.includes(interruptedKind)) {
controller.completedKinds.push(interruptedKind)
}
controller.currentKind = ''
controller.progress = []
this.emitChanged()
}
break
}
this.recordAgentFailure(
group.id, executionKind, error, threadRootId, reportedFailures,
)
successfulKinds.delete(executionKind)
agreementKinds.delete(executionKind)
if (!controller.failedKinds.includes(executionKind)) {
controller.failedKinds.push(executionKind)
}
terminalFailureOccurred = true
// Failed CLIs leave only this run's active queue; the configured
// group keeps them so the next turn can retry.
if (this.shouldPauseFailedAgent(error)) {
this.pauseFailedAgentForRun(group, controller, executionKind, threadRootId, error)
}
activeKinds = activeKinds.filter(activeKind => activeKind !== executionKind)
}
if (controller.signal.aborted) break
if (!controller.completedKinds.includes(executionKind)) {
controller.completedKinds.push(executionKind)
}
controller.currentKind = ''
controller.progress = []
this.checkpointOrchestration(group, controller, {
currentKind: '',
pendingKinds: [...roundQueue],
activeKinds: [...activeKinds],
successfulKinds: [...successfulKinds],
agreementKinds: [...agreementKinds],
attachmentRecipients: [...attachmentRecipients],
totalSuccesses,
terminalFailureOccurred,
executionSequence,
})
this.emitChanged()
}
if (controller.signal.aborted) break
const successes = activeKinds.filter(kind => successfulKinds.has(kind)).length
const agreements = activeKinds.filter(kind => agreementKinds.has(kind)).length
totalSuccesses += successes
this.checkpointOrchestration(group, controller, {
currentKind: '',
pendingKinds: [],
activeKinds: [...activeKinds],
successfulKinds: [...successfulKinds],
agreementKinds: [...agreementKinds],
attachmentRecipients: [...attachmentRecipients],
totalSuccesses,
terminalFailureOccurred,
executionSequence,
})
if (activeKinds.length < 2) break
const crossReviewComplete = !controller.unlimitedRounds || controller.currentRound >= 2
if (crossReviewComplete
&& successes === activeKinds.length
&& agreements === activeKinds.length) {
consensusReached = true
break
}
}
let runStatus
if (controller.signal.aborted) {
runStatus = terminalRunStatusForReason(controller.stopReason)
} else if (terminalFailureOccurred) runStatus = totalSuccesses > 0 ? 'partial' : 'failed'
else if (consensusReached) runStatus = 'completed'
else runStatus = totalSuccesses > 0 ? 'round-limit' : 'failed'
if (!terminalFailureOccurred && !controller.unlimitedRounds
&& (runStatus === 'round-limit' || runStatus === 'failed')) {
this.addMessage(
group.id,
'system',
`Automatic discussion reached its ${maxRounds}-round limit. The latest replies above are the current result.`,
'',
threadRootId,
{ key: 'system.autoRoundLimit', params: { rounds: maxRounds } },
)
}
if (controller.stopReason === 'circuit_breaker') {
this.addMessage(
group.id,
'system',
`Automatic discussion stopped after ${MAX_RUN_AGENT_ATTEMPTS} Agent attempts.`,
'',
threadRootId,
{
key: 'system.runCircuitBreaker',
params: { maxAttempts: MAX_RUN_AGENT_ATTEMPTS },
},
)
}
return runStatus
},
}
module.exports = { roundsMethods }
@@ -0,0 +1,419 @@
// V3 task-graph runs. Kept so older runs can be resumed; new runs do not use it.
const { createHash } = require('node:crypto')
const {
appendBlackboardEntry,
emptyCollaborationState,
publicCollaborationText,
} = require('../collaboration/collaboration-records.cjs')
const { unlimitedReviewContract } = require('./local-workspace-context.cjs')
const {
parseTaskGraphCursor,
readyTaskGraphNodes,
terminalTaskGraphState,
updateTaskGraphCursor,
} = require('../collaboration/task-graph-records.cjs')
const {
circuitBreakerFailure,
hardBudgetFailure,
terminalRunStatusForReason,
} = require('./local-workspace-auto-runner-support.cjs')
const taskGraphMethods = {
unresolvedTaskGraphConflicts(collaborationInput = null) {
const collaboration = collaborationInput || emptyCollaborationState()
const resolved = new Set(collaboration.entries
.filter(entry => entry.entryType === 'decision' && entry.lifecycle.state === 'active')
.flatMap(entry => entry.refs))
return collaboration.entries.filter(entry => (
entry.entryType === 'conflict'
&& entry.lifecycle.state === 'active'
&& !resolved.has(entry.entryId)
))
},
taskGraphAcceptanceCriteria(node) {
const criteria = []
if (node.acceptance.requireConclusion) criteria.push('Return one concrete conclusion.')
if (node.acceptance.minArtifactRefs) {
criteria.push(`Produce at least ${node.acceptance.minArtifactRefs} durable Artifact reference(s).`)
}
if (node.acceptance.minEvidenceRefs) {
criteria.push(`Produce at least ${node.acceptance.minEvidenceRefs} durable Evidence reference(s).`)
}
return criteria.length ? criteria : ['Complete the explicit Human decision.']
},
taskGraphSelectedEntryIds(controller, node) {
const cursor = parseTaskGraphCursor(controller.orchestration.taskGraph)
const states = new Map(cursor.nodeStates.map(state => [state.nodeId, state]))
const selected = node.inputNodeIds.flatMap(nodeId => states.get(nodeId)?.entryIds || [])
if (['reviewer', 'arbiter'].includes(node.role)) {
selected.push(...this.unresolvedTaskGraphConflicts(
controller.orchestration.collaboration,
).map(entry => entry.entryId))
}
return [...new Set(selected)]
},
taskGraphPrompt(node, objective, collaborationPackage, unlimitedRounds = false) {
return [
'MELDWORK_TASK_GRAPH_V1',
`Node: ${node.nodeId}`,
`Role: ${node.role}`,
`Objective: ${objective}`,
`Expected output: ${node.expectedOutput}`,
`Acceptance criteria: ${this.taskGraphAcceptanceCriteria(node).join(' | ')}`,
unlimitedReviewContract(unlimitedRounds),
collaborationPackage.text,
'Return the requested result directly. Completion is evaluated from durable typed records; do not emit a consensus marker.',
].join('\n')
},
checkpointTaskGraph(group, controller, cursorInput, options = {}) {
let taskGraph = parseTaskGraphCursor(cursorInput)
taskGraph = parseTaskGraphCursor({
...taskGraph,
terminalState: terminalTaskGraphState(taskGraph),
})
const states = new Map(taskGraph.nodeStates.map(state => [state.nodeId, state]))
const pendingKinds = taskGraph.graph.nodes
.filter(node => states.get(node.nodeId)?.status === 'pending' && node.agentKind)
.map(node => node.agentKind)
const successfulKinds = taskGraph.graph.nodes
.filter(node => states.get(node.nodeId)?.status === 'accepted' && node.agentKind)
.map(node => node.agentKind)
const uniquePendingKinds = [...new Set(pendingKinds)]
const uniqueSuccessfulKinds = [...new Set(successfulKinds)]
this.checkpointOrchestration(group, controller, {
taskGraph,
currentKind: options.currentKind || '',
pendingKinds: uniquePendingKinds,
successfulKinds: uniqueSuccessfulKinds,
agreementKinds: [],
totalSuccesses: taskGraph.nodeStates.filter(state => state.status === 'accepted').length,
terminalFailureOccurred: taskGraph.terminalState === 'failed',
})
return taskGraph
},
taskGraphResultAccepted(node, result) {
const conclusion = publicCollaborationText(result?.message?.content || '')
const artifactIds = result?.outcomeRefs?.artifactIds || []
const evidenceIds = result?.outcomeRefs?.evidenceIds || []
return (!node.acceptance.requireConclusion || Boolean(conclusion))
&& artifactIds.length >= node.acceptance.minArtifactRefs
&& evidenceIds.length >= node.acceptance.minEvidenceRefs
},
applyTaskGraphAgentResult(group, controller, node, result) {
const conflicts = this.unresolvedTaskGraphConflicts(controller.orchestration.collaboration)
const entryIds = this.recordCollaborationResult(
group,
controller,
controller.orchestration.activeKinds,
node.agentKind,
result,
{
role: node.role,
entryType: node.role === 'arbiter' ? 'decision' : 'claim',
subject: node.role === 'primary'
? `task:${controller.taskId}:conclusion`
: `task:${controller.taskId}:node:${node.nodeId}`,
refs: node.role === 'arbiter' ? conflicts.map(entry => entry.entryId) : [],
},
)
const accepted = this.taskGraphResultAccepted(node, result)
const conclusion = publicCollaborationText(result?.message?.content || '')
const artifactIds = result?.outcomeRefs?.artifactIds || []
const evidenceIds = result?.outcomeRefs?.evidenceIds || []
let cursor = updateTaskGraphCursor(
controller.orchestration.taskGraph,
node.nodeId,
{
status: accepted ? 'accepted' : 'failed',
entryIds,
artifactIds,
evidenceIds,
conclusionHash: conclusion
? createHash('sha256').update(conclusion).digest('hex')
: '',
},
)
if (accepted) {
if (!controller.completedKinds.includes(node.agentKind)) {
controller.completedKinds.push(node.agentKind)
}
} else if (!controller.failedKinds.includes(node.agentKind)) {
controller.failedKinds.push(node.agentKind)
}
cursor = this.checkpointTaskGraph(group, controller, cursor)
return accepted
},
taskGraphDecisionAgentRunId(graphId, nodeId) {
const digest = createHash('sha256').update(JSON.stringify([graphId, nodeId])).digest('hex')
return `graph-decision-${digest}`
},
taskGraphDecisionAnchor(controller, node) {
const graph = parseTaskGraphCursor(controller.orchestration.taskGraph)
const byId = new Map(graph.graph.nodes.map(candidate => [candidate.nodeId, candidate]))
return [...node.dependsOn].reverse()
.map(nodeId => byId.get(nodeId)?.agentKind)
.find(Boolean) || controller.targetKinds[0]
},
applyTaskGraphHumanDecision(group, controller, node, decision) {
const option = node.decisionOptions.find(candidate => candidate.optionId === decision.optionId)
if (!option) throw new Error('LOCAL_WORKFLOW_DECISION_INVALID')
const accepted = decision.status === 'approved'
const conflicts = this.unresolvedTaskGraphConflicts(controller.orchestration.collaboration)
let collaboration = controller.orchestration.collaboration
collaboration = appendBlackboardEntry(collaboration, {
entryType: 'decision',
subject: `task:${controller.taskId}:node:${node.nodeId}`,
statement: `Human selected: ${option.name}`,
value: '',
owner: { type: 'harness' },
audience: { roles: ['primary', 'reviewer', 'arbiter'], agentKinds: [] },
lifecycle: {
state: 'active',
sequence: collaboration.entries.length + 1,
recordedAt: Date.now(),
supersedesEntryId: null,
},
provenance: {
runId: controller.runId,
taskId: controller.taskId,
round: controller.currentRound,
agentRunId: this.taskGraphDecisionAgentRunId(
controller.orchestration.taskGraph.graph.graphId,
node.nodeId,
),
artifactIds: [],
evidenceIds: [],
},
refs: conflicts.map(entry => entry.entryId),
})
const decisionEntryId = collaboration.entries.at(-1).entryId
this.checkpointOrchestration(group, controller, { collaboration })
let cursor = updateTaskGraphCursor(
controller.orchestration.taskGraph,
node.nodeId,
{
status: accepted ? 'accepted' : 'rejected',
entryIds: [decisionEntryId],
decisionOptionId: option.optionId,
},
)
cursor = this.checkpointTaskGraph(group, controller, cursor)
return accepted
},
async executeTaskGraphAgentNode(
group, controller, threadRootId, context, node, parallelGraph = false,
) {
const selectedEntryIds = this.taskGraphSelectedEntryIds(controller, node)
const collaborationPackage = this.prepareCollaborationPackage(
group,
controller,
controller.orchestration.activeKinds,
node.agentKind,
threadRootId,
{
role: node.role,
selectedEntryIds,
expectedOutput: node.expectedOutput,
acceptanceCriteria: this.taskGraphAcceptanceCriteria(node),
},
)
const isolated = ['reviewer', 'arbiter'].includes(node.role)
const attachments = node.role === 'primary'
&& !controller.orchestration.attachmentRecipients.includes(node.agentKind)
? context.rootAttachments.map(attachment => attachment.path)
: []
const objective = this.collaborationObjective(group, controller, threadRootId)
return this.invokeWithUnauthorizedRecovery({
group,
kind: node.agentKind,
controller,
threadRootId,
context: {
attachments,
attachmentSnapshots: attachments.length ? context.rootAttachments : [],
skillHints: context.rootSkillsByKind.get(node.agentKind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(node.agentKind) || [],
collaborationPackage,
completionPolicy: 'typed',
parallelGraph,
runtimeInstruction: isolated ? '' : [
`Execute task-graph node ${node.nodeId} as ${node.role}.`,
`Expected output: ${node.expectedOutput}`,
'Do not emit a consensus marker.',
].join('\n'),
...(isolated ? {
sessionPolicy: 'isolated',
promptOverride: this.taskGraphPrompt(
node, objective, collaborationPackage, controller.unlimitedRounds === true,
),
contextPackId: controller.contextPackId,
} : {}),
contextOptions: {
focusUserMessageId: threadRootId,
omitAgentThreadRootId: threadRootId,
},
},
mode: 'auto',
})
},
async executeTaskGraphHumanNode(group, controller, node) {
if (!this.requestHumanGate) throw new Error('LOCAL_WORKFLOW_DECISION_UNAVAILABLE')
const anchorKind = this.taskGraphDecisionAnchor(controller, node)
const agentRunId = this.taskGraphDecisionAgentRunId(
controller.orchestration.taskGraph.graph.graphId,
node.nodeId,
)
let cursor = updateTaskGraphCursor(
controller.orchestration.taskGraph,
node.nodeId,
{ status: 'waiting', attention: 'decision', attempts: 1 },
)
cursor = this.checkpointTaskGraph(group, controller, cursor, { currentKind: anchorKind })
const gate = await this.requestHumanGate({
type: 'decision',
runId: controller.runId,
agentRunId,
agentKind: anchorKind,
summary: node.expectedOutput,
options: node.decisionOptions,
request: {
graphId: cursor.graph.graphId,
nodeId: node.nodeId,
inputNodeIds: node.inputNodeIds,
},
}, {
signal: controller.signal,
preserveOnAbort: () => controller.stopReason === 'shutdown',
continuation: {
resumeKind: 'role_review_decision',
agentRunId,
agentKind: anchorKind,
round: controller.currentRound,
},
})
const accepted = this.applyTaskGraphHumanDecision(group, controller, node, gate)
if (this.completeHumanGateContinuation?.(
controller.runId,
gate.gateId,
accepted ? 'completed' : 'cancelled',
) !== true && this.hasRunLedger()) {
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
return accepted
},
taskGraphBatch(group, readyNodes) {
const parallel = []
const kinds = new Set()
if (group.allowWrite !== true) {
for (const node of readyNodes) {
if (!node.parallel || node.role === 'human' || kinds.has(node.agentKind)) continue
parallel.push(node)
kinds.add(node.agentKind)
}
}
return parallel.length > 1 ? parallel : readyNodes.slice(0, 1)
},
async runTaskGraph(group, controller, threadRootId, context) {
while (!controller.signal.aborted) {
let cursor = parseTaskGraphCursor(controller.orchestration.taskGraph)
const terminalState = terminalTaskGraphState(cursor)
if (terminalState !== 'running') {
cursor = this.checkpointTaskGraph(group, controller, cursor)
break
}
if (!controller.unlimitedRounds && controller.currentRound >= controller.maxRounds) break
const readyNodes = readyTaskGraphNodes(cursor)
if (!readyNodes.length) {
cursor = parseTaskGraphCursor({ ...cursor, terminalState: 'failed' })
this.checkpointTaskGraph(group, controller, cursor)
break
}
const batch = this.taskGraphBatch(group, readyNodes)
controller.currentRound += 1
if (batch.length === 1 && batch[0].role === 'human') {
await this.executeTaskGraphHumanNode(group, controller, batch[0])
continue
}
const conflicts = this.unresolvedTaskGraphConflicts(controller.orchestration.collaboration)
for (const node of batch) {
cursor = updateTaskGraphCursor(cursor, node.nodeId, {
status: 'running',
attention: node.role === 'reviewer' && conflicts.length
? 'review'
: (node.role === 'arbiter' && conflicts.length ? 'decision' : 'none'),
attempts: cursor.nodeStates.find(state => state.nodeId === node.nodeId).attempts + 1,
})
}
const currentKind = batch.length === 1 ? batch[0].agentKind : ''
cursor = this.checkpointTaskGraph(group, controller, cursor, { currentKind })
this.emitChanged()
const settled = await Promise.allSettled(batch.map(node => (
this.executeTaskGraphAgentNode(
group, controller, threadRootId, context, node, batch.length > 1,
)
)))
for (let index = 0; index < batch.length; index += 1) {
const node = batch[index]
const outcome = settled[index]
if (outcome.status === 'fulfilled' && outcome.value?.result) {
if (outcome.value.result.outcomeRefs?.artifactIds?.length
&& !controller.orchestration.attachmentRecipients.includes(node.agentKind)) {
this.checkpointOrchestration(group, controller, {
attachmentRecipients: [...controller.orchestration.attachmentRecipients, node.agentKind],
})
}
this.applyTaskGraphAgentResult(group, controller, node, outcome.value.result)
continue
}
const error = outcome.status === 'rejected'
? outcome.reason
: (outcome.value?.error || new Error('LOCAL_AGENT_UNKNOWN_FAILURE'))
if (controller.signal.aborted) continue
this.recordAgentFailure(group.id, node.agentKind, error, threadRootId, new Set())
cursor = updateTaskGraphCursor(
controller.orchestration.taskGraph,
node.nodeId,
{ status: 'failed' },
)
this.checkpointTaskGraph(group, controller, cursor)
if (!controller.failedKinds.includes(node.agentKind)) {
controller.failedKinds.push(node.agentKind)
}
if (circuitBreakerFailure(error)) {
controller.stopReason = 'circuit_breaker'
controller.abort()
} else if (hardBudgetFailure(error)) {
controller.stopReason = 'hard_budget'
controller.abort()
}
}
controller.currentKind = ''
controller.progress = []
this.emitChanged()
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
const cursor = parseTaskGraphCursor(controller.orchestration.taskGraph)
if (cursor.terminalState === 'accepted') return 'completed'
if (cursor.terminalState === 'rejected') return 'stopped'
if (cursor.terminalState === 'failed') {
return cursor.nodeStates.some(state => state.status === 'accepted') ? 'partial' : 'failed'
}
return cursor.nodeStates.some(state => state.status === 'accepted') ? 'round-limit' : 'failed'
},
}
module.exports = { taskGraphMethods }
@@ -0,0 +1,403 @@
// What a V4 run freezes and shares: the task snapshot, stored receipts and the collaboration package.
const { createHash } = require('node:crypto')
const { peerCollaborationText, publicCollaborationText } = require('../collaboration/collaboration-records.cjs')
const {
buildCollaborationPackage,
hashValue,
parseCollaborationControlBlock,
resolveCoordinationConsensus,
} = require('../collaboration/orchestration-v4-records.cjs')
const { canonicalJson } = require('../collaboration/context-pack-records.cjs')
const {
restoreV4SnapshotSkills,
v4SnapshotBodyHash,
v4SnapshotSkillHints,
validateV4SnapshotBody,
} = require('./local-workspace-context.cjs')
const v4ContextMethods = {
v4OperationId(
controller, kind, phase, slotId = '', round = controller.currentRound || 0,
) {
const digest = createHash('sha256').update(JSON.stringify([
controller.runId,
controller.taskId,
kind,
slotId,
phase,
round,
])).digest('hex')
return `operation-${digest}`
},
v4SnapshotRecord(controller, snapshot, targetKinds) {
if (!this.contentBlobStore) throw new Error('LOCAL_RUN_SNAPSHOT_STORE_UNAVAILABLE')
const serialized = canonicalJson(snapshot)
const contentRef = this.contentBlobStore.put(serialized, { mediaType: 'application/json' })
const sourceIds = [snapshot?.messageId, ...(snapshot?.history || []).map(item => item.id)]
.filter(Boolean).slice(-64)
const taskId = snapshot?.taskId || controller.taskId || null
const record = {
contextPackId: controller.contextPackId || null,
taskId,
messageId: snapshot?.messageId || controller.threadRootId || null,
groupId: snapshot?.group?.id || controller.groupId || null,
round: Math.max(0, Number(controller.currentRound) || 0),
targetKinds: [...targetKinds],
sourceIds,
capturedAt: Date.now(),
charCount: Buffer.byteLength(serialized),
contentHash: contentRef.hash,
bodyHash: v4SnapshotBodyHash(snapshot),
contentRef,
}
return { record, snapshotHash: hashValue(record) }
},
v4SnapshotSkillHints(snapshot, targetKinds) {
return v4SnapshotSkillHints(snapshot, targetKinds)
},
async v4RestoreSnapshotSkills(snapshot, targetKinds, persisted = null) {
return restoreV4SnapshotSkills({
snapshot,
targetKinds,
validateSkillSelections: this.validateSkillSelections,
persisted,
})
},
v4LoadSnapshot(orchestration, { taskId, messageId, groupId, targetKinds }) {
const record = orchestration?.snapshot
if (!record?.contentRef || !this.contentBlobStore) {
throw new Error('LOCAL_RUN_SNAPSHOT_INVALID')
}
let bytes
try { bytes = this.contentBlobStore.read(record.contentRef) } catch {
throw new Error('LOCAL_RUN_SNAPSHOT_INVALID')
}
const serialized = bytes.toString('utf8')
let body
try { body = JSON.parse(serialized) } catch {
throw new Error('LOCAL_RUN_SNAPSHOT_INVALID')
}
return validateV4SnapshotBody({
body,
serialized,
byteLength: bytes.length,
record,
orchestrationSnapshotHash: orchestration.snapshotHash,
taskId,
messageId,
groupId,
targetKinds,
})
},
v4RestoreReceipts(controller) {
if (Array.isArray(controller.v4ReceiptRecords)) {
return controller.v4ReceiptRecords.map(item => ({
receipt: item.receipt,
verdict: item.verdict || '',
resolvedIssueIds: Array.isArray(item.resolvedIssueIds)
? [...item.resolvedIssueIds]
: [],
}))
}
const restored = []
const slots = Array.isArray(controller.orchestration?.slots)
? controller.orchestration.slots
: []
for (const slot of slots) {
const records = slot?.resultRefs?.workflowOutcomeRefs
if (!Array.isArray(records)) continue
for (const record of records) {
if (!record?.receipt) continue
try {
const receipt = parseCollaborationControlBlock(record.receipt)
restored.push({
receipt,
verdict: typeof record.verdict === 'string' ? record.verdict : '',
resolvedIssueIds: Array.isArray(record.resolvedIssueIds)
? record.resolvedIssueIds.filter(value => typeof value === 'string')
: [],
})
} catch { /* malformed historical receipts are ignored and re-run */ }
}
}
controller.v4ReceiptRecords = restored
return restored
},
v4Package(receiptRecords, options = {}) {
const recipientKind = String(options.recipientKind || '')
const binding = options.sessionBinding || {}
const deliveryState = Array.isArray(options.deliveryState) ? options.deliveryState : []
const latest = new Map()
for (const item of receiptRecords) {
const receipt = item?.receipt
if (!receipt) continue
const key = receipt.agentKind || `${receipt.phase}\u0000${receipt.summary}`
const previous = latest.get(key)
if (!previous || (Number(receipt.deliveryWatermark) || 0)
> (Number(previous.receipt.deliveryWatermark) || 0)
|| ((Number(receipt.deliveryWatermark) || 0)
=== (Number(previous.receipt.deliveryWatermark) || 0)
&& receipt.phase.localeCompare(previous.receipt.phase) > 0)) {
latest.set(key, item)
}
}
let selectedItems = [...latest.values()]
const snapshotHash = typeof options.snapshotHash === 'string' ? options.snapshotHash : ''
const targetKinds = new Set(Array.isArray(options.targetKinds) ? options.targetKinds : [])
const resolvedIssueIds = []
const resolved = new Set()
for (const item of receiptRecords) {
const receipt = item?.receipt
if (!snapshotHash || receipt?.snapshotHash !== snapshotHash
|| (targetKinds.size && !targetKinds.has(receipt.agentKind))) continue
for (const value of Array.isArray(item.resolvedIssueIds) ? item.resolvedIssueIds : []) {
if (typeof value !== 'string' || !value || value.length > 120 || resolved.has(value)) continue
resolved.add(value)
resolvedIssueIds.push(value)
if (resolvedIssueIds.length >= 256) break
}
if (resolvedIssueIds.length >= 256) break
}
if (recipientKind && binding.sessionRefHash && !options.forceFull) {
const acknowledged = new Map(deliveryState
.filter(item => item.recipientKind === recipientKind
&& item.sessionRefHash === binding.sessionRefHash
&& item.sessionProvenanceHash === binding.sessionProvenanceHash
&& item.status === 'acknowledged')
.map(item => [`${item.sourceAgentKind}\u0000${item.sourcePhase}`, item.watermark]))
selectedItems = selectedItems.filter(item => {
const receipt = item.receipt
const previous = acknowledged.get(`${receipt.agentKind}\u0000${receipt.phase}`) || 0
return (Number(receipt.deliveryWatermark) || 0) > previous
})
}
const selectedReceipts = selectedItems.map(item => item.receipt)
const totalLimit = Number.isSafeInteger(options.totalLimit) ? options.totalLimit : 6000
const packageRecord = buildCollaborationPackage(selectedReceipts, {
targetKinds: options.targetKinds || [],
totalLimit,
resolvedIssueIds,
})
const text = this.v4SanitizeDeliveryText(packageRecord.text, packageRecord.text.length)
return text === packageRecord.text
? packageRecord
: { ...packageRecord, text, totalChars: text.length, packageHash: hashValue(text) }
},
v4CoordinationResult({
receiptRecords, targetKinds, snapshotHash, snapshot = null,
challengeBindings = [], requireSupport = true,
}) {
const candidateReceipts = receiptRecords
.filter(record => record?.receipt?.phase === 'challenge')
const supportReceipts = requireSupport
? targetKinds.map((kind) => {
const operationId = challengeBindings.find(binding => (
binding.reviewerKind === kind
))?.reviewerOperationId || ''
return this.v4ReceiptForOperation(
receiptRecords, 'challenge', kind, operationId,
) || null
})
: []
if (supportReceipts.some(receipt => !receipt)) {
const result = resolveCoordinationConsensus({
targetKinds, snapshotHash, candidateReceipts, supportReceipts: [],
})
return this.v4SemanticCoordinationResult(result, snapshot)
}
const result = resolveCoordinationConsensus({
targetKinds, snapshotHash, candidateReceipts, supportReceipts,
})
return this.v4SemanticCoordinationResult(result, snapshot)
},
v4SemanticCoordinationResult(result, snapshot) {
const candidates = result.candidates.filter((candidate) => {
try {
for (const assignment of candidate.assignments) {
this.v4DirectAssignmentReferences(assignment, snapshot)
}
return true
} catch (error) {
if (error?.message === 'LOCAL_RUN_V4_ASSIGNMENT_REFERENCE_INVALID') return false
throw error
}
})
const candidateHashes = new Set(candidates.map(candidate => candidate.planHash))
return {
...result,
plan: result.plan && candidateHashes.has(result.plan.planHash) ? result.plan : null,
candidates,
}
},
v4SanitizeDeliveryText(value, limit = 6000, options = {}) {
const privateWrapper = '(?:private[ _-]?(?:reasoning|thoughts?)|analysis|chain[ _-]?of[ _-]?thought|cot)'
const privateHeading = '(?:private[ _-]?(?:reasoning|thoughts?)|chain[ _-]?of[ _-]?thought|cot)'
const stripped = this.v4StripMeldworkControls(
options.workspaceRoot
? peerCollaborationText(value, { workspaceRoot: options.workspaceRoot })
: publicCollaborationText(value, Number.MAX_SAFE_INTEGER),
)
const maximum = Number.isSafeInteger(limit) && limit >= 0 ? limit : 6000
let privateSectionLevel = null
const withoutPrivateSections = stripped.split(/\r?\n/).filter((line) => {
const heading = line.match(/^\s*(#{1,6})\s+(.+)$/u)
const level = heading?.[1].length
if (heading && new RegExp(`^${privateHeading}\\b`, 'iu').test(heading[2].trim())) {
privateSectionLevel = Math.min(privateSectionLevel ?? level, level)
return false
}
if (privateSectionLevel !== null && heading && level <= privateSectionLevel) {
privateSectionLevel = null
}
return privateSectionLevel === null
}).join('\n')
return withoutPrivateSections
.replace(new RegExp(`<\\s*${privateWrapper}\\b[^>]*>[\\s\\S]*?<\\/\\s*${privateWrapper}\\s*>`, 'giu'), '')
.replace(/(?:^|\n)\s*(?:my\s+)?private[ _-]?reasoning\s*:\s*[^\n]*/giu, '\n')
.replace(/\b(?:session(?:[ _-]?(?:id|ref))?)\b\s*(?::|=)\s*[A-Za-z0-9][A-Za-z0-9._:-]*/giu, '[redacted]')
.replace(/\b(?:session(?:[ _-]?(?:id|ref))?)\b\s+[A-Za-z0-9][A-Za-z0-9._:-]*/giu, '[redacted]')
.replace(/\b(?:session[-_][A-Za-z0-9._:-]+|[A-Za-z0-9][A-Za-z0-9._:-]*-(?:native-)?session)\b/giu, '[redacted]')
.replace(/\n{3,}/g, '\n\n')
.trim()
.slice(0, maximum)
},
v4StripMeldworkControls(text) {
const marker = '[[MELDWORK_'
const source = String(text || '')
const upper = source.toUpperCase()
let cursor = 0
let output = ''
while (true) {
const start = upper.indexOf(marker, cursor)
if (start < 0) return output + source.slice(cursor)
output += source.slice(cursor, start)
let quote = ''
let escaped = false
let braces = 0
let brackets = 0
let end = -1
for (let index = start + 2; index < source.length - 1; index += 1) {
const character = source[index]
if (quote) {
if (escaped) escaped = false
else if (character === '\\') escaped = true
else if (character === quote) quote = ''
continue
}
if (character === '"' || character === "'") {
quote = character
} else if (character === '{') {
braces += 1
} else if (character === '}') {
braces = Math.max(0, braces - 1)
} else if (character === '[') {
brackets += 1
} else if (character === ']') {
if (braces === 0 && brackets === 0 && source[index + 1] === ']') {
end = index + 2
break
}
brackets = Math.max(0, brackets - 1)
}
}
if (end < 0) return output
const header = source.slice(start, end)
if (!header.includes(':')) {
const closing = `[[/${header.slice(2, -2)}]]`.toUpperCase()
const closingStart = upper.indexOf(closing, end)
cursor = closingStart < 0 ? end : closingStart + closing.length
} else {
cursor = end
}
}
},
v4CoordinationText(result, limit = 4800) {
const candidates = Array.isArray(result?.candidates) ? result.candidates : []
if (!candidates.length) return ''
const supportCounts = new Map()
for (const planHash of result.supportPlanHashes || []) {
if (planHash) supportCounts.set(planHash, (supportCounts.get(planHash) || 0) + 1)
}
const lines = [
'MELDWORK_V4_RESPONSIBILITY_CANDIDATES_V1',
'Fields: h=planHash;s=support;f=finalizer;v=verifiers;a=[t=taskId,o=owner,r=role,x=objective,y=expectedOutput,i=inputRefs,a=artifactRefs,d=dependencies]',
]
const append = (line) => {
const next = [...lines, line].join('\n')
if (next.length > limit) return false
lines.push(line)
return true
}
for (const plan of candidates) {
let safe = true
const text = value => {
const raw = String(value || '')
const sanitized = this.v4SanitizeDeliveryText(raw, 2000)
if (/\[\[MELDWORK_/iu.test(raw) || sanitized !== raw) safe = false
return sanitized
}
const values = value => Array.isArray(value) ? value.map(text) : []
const graph = {
h: plan.planHash,
s: supportCounts.get(plan.planHash) || 0,
f: text(plan.finalizerKind),
v: values(plan.verifierKinds),
a: plan.assignments.map(assignment => ({
t: text(assignment.taskId),
o: text(assignment.ownerKind),
r: text(assignment.role),
x: text(assignment.objective),
y: text(assignment.expectedOutput),
i: values(assignment.inputRefs),
a: values(assignment.artifactIds),
d: values(assignment.dependsOn),
})),
}
if (!safe) continue
append(JSON.stringify(graph))
}
return lines.join('\n')
},
v4ChallengeProposalText(binding, reviewerKind, limit = 1600) {
const proposalKind = String(binding?.proposalKind || '')
if (!proposalKind || proposalKind === reviewerKind) {
throw new Error('LOCAL_RUN_V4_CHALLENGE_BINDING_INVALID')
}
for (const artifactId of binding.artifactIds || []) {
try {
const { artifact, content } = this.v4ArtifactIdentity(artifactId)
const body = this.v4SanitizeDeliveryText(content, limit)
if (artifact.producedBy?.agentKind !== proposalKind || !body) continue
return [
'MELDWORK_V4_ASSIGNED_PROPOSAL_V1',
`Proposal Agent: ${proposalKind}`,
`Proposal Artifact: ${artifactId}`,
'Proposal body excerpt:',
body,
].join('\n')
} catch { /* skip non-text and unavailable outcome refs */ }
}
throw new Error('LOCAL_RUN_V4_CHALLENGE_BINDING_INVALID')
},
v4CoordinationAssignment(coordinationPlan, kind, options = {}) {
const owned = coordinationPlan?.assignments?.filter(item => item.ownerKind === kind) || []
if (options.taskId) return owned.find(item => item.taskId === options.taskId) || null
if (options.role) return owned.find(item => item.role === options.role) || null
return owned[0] || null
},
}
module.exports = { v4ContextMethods }
@@ -0,0 +1,604 @@
// How a V4 run hands context to each Agent: assignments, the blackboard owner, delivery prompts and delivery state.
const { createHash } = require('node:crypto')
const { collaborationPackageIndexText } = require('../collaboration/collaboration-records.cjs')
const { hashValue } = require('../collaboration/orchestration-v4-records.cjs')
const { v4Prompt } = require('./local-workspace-context.cjs')
const v4DeliveryMethods = {
v4DirectAssignmentReferences(assignment, snapshot) {
const snapshotSources = new Map([
[snapshot?.messageId, snapshot?.taskText],
...(Array.isArray(snapshot?.history) ? snapshot.history : []).map(item => [item?.id, item?.text]),
].filter(([sourceId]) => typeof sourceId === 'string' && sourceId))
const safeReferenceText = (value) => {
const raw = String(value || '').trim()
const sanitized = this.v4SanitizeDeliveryText(raw, Number.MAX_SAFE_INTEGER)
if (!raw || sanitized !== raw || /\[\[MELDWORK_/iu.test(raw)) {
throw new Error('LOCAL_RUN_V4_ASSIGNMENT_REFERENCE_INVALID')
}
return sanitized
}
const inputReferences = assignment.inputRefs.map((sourceId) => {
if (!snapshotSources.has(sourceId)) {
throw new Error('LOCAL_RUN_V4_ASSIGNMENT_REFERENCE_INVALID')
}
return {
kind: 'input', id: sourceId, header: `Input ${sourceId}:`,
body: safeReferenceText(snapshotSources.get(sourceId)),
}
})
const directArtifacts = assignment.artifactIds.map((artifactId) => {
let identity
try { identity = this.v4ArtifactIdentity(artifactId) } catch {
throw new Error('LOCAL_RUN_V4_ASSIGNMENT_REFERENCE_INVALID')
}
if (identity.artifact.contentRef?.mediaType !== 'text/plain') {
throw new Error('LOCAL_RUN_V4_ASSIGNMENT_REFERENCE_INVALID')
}
return {
kind: 'artifact', id: artifactId, header: `Artifact ${artifactId}:`,
body: safeReferenceText(identity.content),
}
})
return { inputReferences, directArtifacts }
},
v4WorkAssignmentText(
coordinationPlan, kind, taskId, snapshot = null, limit = 6000, receiptRecords = [],
) {
const assignment = this.v4CoordinationAssignment(coordinationPlan, kind, { taskId })
if (!assignment) throw new Error('LOCAL_RUN_V4_COORDINATION_PLAN_INVALID')
const { inputReferences, directArtifacts } = this.v4DirectAssignmentReferences(
assignment, snapshot,
)
const directReferences = [...inputReferences, ...directArtifacts]
const dependencies = new Set(assignment.dependsOn)
const latestDependencies = new Map()
for (const record of receiptRecords) {
const receipt = record?.receipt
if (receipt?.phase !== 'work' || !dependencies.has(receipt.workItemId)) continue
latestDependencies.set(receipt.workItemId, record)
}
const dependencyArtifacts = [...latestDependencies.values()]
.sort((left, right) => String(left.receipt.workItemId)
.localeCompare(String(right.receipt.workItemId)))
.flatMap(record => (record.receipt.artifactIds || []).map((artifactId, index) => ({
artifactId,
index,
receipt: record.receipt,
})))
.map(({ artifactId, index, receipt }) => {
let identity
try { identity = this.v4ArtifactIdentity(artifactId) } catch {
throw new Error('LOCAL_RUN_V4_REFERENCE_INVALID')
}
const mediaType = String(identity.artifact.contentRef?.mediaType || '')
const raw = String(identity.content || '').trim()
const body = mediaType === 'text/plain'
? this.v4SanitizeDeliveryText(raw, Number.MAX_SAFE_INTEGER)
: ''
let omission = ''
if (mediaType !== 'text/plain') {
omission = `${artifactId} reason=non-text mediaType=${mediaType || 'unknown'}`
} else if (!raw) {
omission = `${artifactId} reason=empty`
} else if (body !== raw) {
omission = `${artifactId} reason=safety-filter`
}
return {
key: `${receipt.workItemId}:${index}`,
artifactId,
body,
omission,
header: `Artifact ${artifactId} from ${receipt.agentKind} work=${receipt.workItemId}`,
}
})
const render = (included) => {
const lines = [
'MELDWORK_V4_AGREED_WORK_PACKAGE_V1',
`Responsibility plan: ${coordinationPlan.planHash}`,
`Work item: ${assignment.taskId}`,
`Owner: ${assignment.ownerKind}`,
`Role: ${assignment.role}`,
`Input refs: ${assignment.inputRefs.length ? assignment.inputRefs.join(', ') : '(none)'}`,
`Artifact refs: ${assignment.artifactIds.length ? assignment.artifactIds.join(', ') : '(none)'}`,
`Dependencies: ${assignment.dependsOn.length ? assignment.dependsOn.join(', ') : '(none)'}`,
]
if (inputReferences.length) {
lines.push('MELDWORK_V4_FROZEN_INPUT_REFS_V1')
for (const reference of inputReferences) {
lines.push(reference.header)
const body = included.get(`direct:${reference.kind}:${reference.id}`) || ''
if (body) lines.push(body)
}
}
if (directArtifacts.length) {
lines.push('MELDWORK_V4_DIRECT_ARTIFACT_REFS_V1')
for (const reference of directArtifacts) {
lines.push(reference.header)
const body = included.get(`direct:${reference.kind}:${reference.id}`) || ''
if (body) lines.push(body)
}
}
if (dependencyArtifacts.length) {
lines.push('MELDWORK_V4_REFERENCED_ARTIFACTS_V1')
for (const artifact of dependencyArtifacts) {
lines.push(artifact.header)
const body = included.get(`dependency:${artifact.key}`) || ''
if (body) lines.push(body)
}
}
const objective = included.get('objective') || ''
const expectedOutput = included.get('expectedOutput') || ''
lines.push(`Objective: ${objective}`, `Expected output: ${expectedOutput}`)
const freeTextOmissions = [
...(objective.length < assignment.objective.length ? ['objective reason=budget'] : []),
...(expectedOutput.length < assignment.expectedOutput.length
? ['expectedOutput reason=budget'] : []),
]
if (freeTextOmissions.length) {
lines.push('MELDWORK_V4_TRUNCATED_FREE_TEXT_V1', ...freeTextOmissions)
}
const directOmissions = directReferences.filter(reference => (
(included.get(`direct:${reference.kind}:${reference.id}`) || '').length
< reference.body.length
)).map(reference => `${reference.kind} ${reference.id} reason=budget`)
if (directOmissions.length) {
lines.push('MELDWORK_V4_OMITTED_DIRECT_REFERENCE_BODIES_V1', ...directOmissions)
}
const dependencyOmissions = dependencyArtifacts.flatMap((artifact) => {
if (artifact.omission) return [artifact.omission]
return (included.get(`dependency:${artifact.key}`) || '').length < artifact.body.length
? [`${artifact.artifactId} reason=budget`]
: []
})
if (dependencyOmissions.length) {
lines.push('MELDWORK_V4_OMITTED_ARTIFACT_BODIES_V1', ...dependencyOmissions)
}
return lines.join('\n')
}
const maximum = Number.isSafeInteger(limit) && limit >= 0 ? limit : 6000
const fullIncluded = new Map([
['objective', assignment.objective],
['expectedOutput', assignment.expectedOutput],
...directReferences.map(reference => [
`direct:${reference.kind}:${reference.id}`, reference.body,
]),
...dependencyArtifacts.filter(artifact => artifact.body).map(artifact => [
`dependency:${artifact.key}`, artifact.body,
]),
])
const full = render(fullIncluded)
if (full.length <= maximum) return full
const bodyFields = [...fullIncluded.entries()]
const bodyLineCount = directReferences.length
+ dependencyArtifacts.filter(artifact => artifact.body).length
let remaining = maximum - render(new Map()).length - bodyLineCount
if (remaining < 0) throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
const included = new Map()
let pendingFields = bodyFields.length
for (const [key, body] of bodyFields) {
const share = pendingFields > 0 ? Math.floor(remaining / pendingFields) : 0
const value = body.slice(0, Math.max(0, share))
included.set(key, value)
remaining -= value.length
pendingFields -= 1
}
const result = render(included)
if (result.length > maximum) throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
return result
},
v4CheckpointRole(phase, slot, coordinationPlan, workAssignment = null) {
if (['proposal', 'discussion', 'challenge'].includes(phase)) return 'participant'
const assignment = phase === 'work'
? (workAssignment || this.v4CoordinationAssignment(coordinationPlan, slot.agentKind))
: (phase === 'synthesis'
? this.v4CoordinationAssignment(coordinationPlan, slot.agentKind, { role: 'integrator' })
|| this.v4CoordinationAssignment(coordinationPlan, slot.agentKind)
: this.v4CoordinationAssignment(coordinationPlan, slot.agentKind))
if (phase === 'verification') {
if (coordinationPlan?.verifierKinds?.includes(slot.agentKind)) return 'verifier'
return assignment?.role || 'writer'
}
return assignment?.role || 'writer'
},
v4PromptRole(phase, kind, coordinationPlan, workAssignment = null) {
if (['proposal', 'discussion', 'challenge'].includes(phase)) return 'participant'
if (phase === 'verification') {
if (!coordinationPlan?.verifierKinds?.includes(kind)) {
throw new Error('LOCAL_RUN_V4_COORDINATION_PLAN_INVALID')
}
return 'verifier'
}
const assignment = phase === 'work'
? workAssignment
: (phase === 'synthesis'
? this.v4CoordinationAssignment(coordinationPlan, kind, { role: 'integrator' })
|| this.v4CoordinationAssignment(coordinationPlan, kind)
: this.v4CoordinationAssignment(coordinationPlan, kind))
if (!assignment) throw new Error('LOCAL_RUN_V4_COORDINATION_PLAN_INVALID')
return assignment.role
},
v4BlackboardOwner(coordinationPlan, kind) {
const assignment = this.v4CoordinationAssignment(coordinationPlan, kind, { role: 'integrator' })
|| this.v4CoordinationAssignment(coordinationPlan, kind)
if (!assignment) {
throw new Error('LOCAL_RUN_V4_COORDINATION_PLAN_INVALID')
}
return {
type: 'agent',
agentKind: kind,
role: assignment.role,
}
},
v4ArtifactContext(
receiptRecords, phase, coordinationPlan, kind, limit = 3600, workTaskId = '',
) {
let selected = []
if (phase === 'work') {
const assignment = this.v4CoordinationAssignment(
coordinationPlan, kind, { taskId: workTaskId },
)
const dependencies = new Set(assignment?.dependsOn || [])
selected = receiptRecords.filter(record => (
record?.receipt?.phase === 'work' && dependencies.has(record.receipt.workItemId)
))
} else if (phase === 'synthesis') {
const workItems = new Set(coordinationPlan?.assignments?.map(item => item.taskId) || [])
selected = receiptRecords.filter(record => (
record?.receipt?.phase === 'work' && workItems.has(record.receipt.workItemId)
))
} else if (phase === 'verification') {
const latest = this.v4LatestReceipt(receiptRecords, 'synthesis')
selected = latest ? [latest] : []
}
const latestByKey = new Map()
for (const record of selected) {
const receipt = record.receipt
const key = receipt.workItemId || `${receipt.phase}:${receipt.agentKind}`
latestByKey.set(key, record)
}
const records = [...latestByKey.values()].sort((left, right) => (
String(left.receipt.workItemId || left.receipt.agentKind)
.localeCompare(String(right.receipt.workItemId || right.receipt.agentKind))
))
if (!records.length) return ''
const artifacts = records.flatMap((record, recordIndex) => {
const receipt = record.receipt
return (receipt.artifactIds || []).map((artifactId, artifactIndex) => ({
key: `${recordIndex}:${artifactIndex}`,
artifactId,
receipt,
}))
}).map(({ key, artifactId, receipt }) => {
let identity
try { identity = this.v4ArtifactIdentity(artifactId) } catch {
throw new Error('LOCAL_RUN_V4_REFERENCE_INVALID')
}
const mediaType = String(identity.artifact.contentRef?.mediaType || '')
const raw = String(identity.content || '').trim()
const body = mediaType === 'text/plain'
? this.v4SanitizeDeliveryText(raw, Number.MAX_SAFE_INTEGER)
: ''
return {
key,
artifactId,
receipt,
mediaType,
raw,
body,
header: `Artifact ${artifactId} from ${receipt.agentKind} work=${receipt.workItemId || receipt.phase}`,
}
})
if (!artifacts.length) return ''
const maximum = Number.isSafeInteger(limit) && limit >= 0 ? limit : 3600
const textArtifacts = artifacts.filter(artifact => artifact.mediaType === 'text/plain'
&& artifact.body)
const fixedOmissions = artifacts.flatMap((artifact) => {
if (artifact.mediaType !== 'text/plain') {
return [`${artifact.artifactId} reason=non-text mediaType=${artifact.mediaType || 'unknown'}`]
}
if (!artifact.raw) return [`${artifact.artifactId} reason=empty`]
return artifact.body === artifact.raw
? []
: [`${artifact.artifactId} reason=safety-filter`]
})
const potentialBudgetOmissions = textArtifacts.map(artifact => (
`${artifact.artifactId} reason=budget`
))
const structuralLines = [
'MELDWORK_V4_REFERENCED_ARTIFACTS_V1',
...artifacts.map(artifact => artifact.header),
]
const reservedManifest = [...fixedOmissions, ...potentialBudgetOmissions]
const reservedLines = reservedManifest.length
? [...structuralLines, 'MELDWORK_V4_OMITTED_ARTIFACT_BODIES_V1', ...reservedManifest]
: structuralLines
const bodySeparators = textArtifacts.length
const bodyBudget = maximum - reservedLines.join('\n').length - bodySeparators
if (bodyBudget < 0) throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
let remaining = bodyBudget
let remainingArtifacts = textArtifacts.length
const includedByKey = new Map()
for (const artifact of textArtifacts) {
const share = remainingArtifacts > 0 ? Math.floor(remaining / remainingArtifacts) : 0
const included = artifact.body.slice(0, Math.max(0, share))
includedByKey.set(artifact.key, included)
remaining -= included.length
remainingArtifacts -= 1
}
const budgetOmissions = textArtifacts.filter(artifact => (
includedByKey.get(artifact.key).length < artifact.body.length
)).map(artifact => `${artifact.artifactId} reason=budget`)
const lines = ['MELDWORK_V4_REFERENCED_ARTIFACTS_V1']
for (const artifact of artifacts) {
lines.push(artifact.header)
const included = includedByKey.get(artifact.key) || ''
if (included) lines.push(included)
}
const omissions = [...fixedOmissions, ...budgetOmissions]
if (omissions.length) {
lines.push('MELDWORK_V4_OMITTED_ARTIFACT_BODIES_V1', ...omissions)
}
const result = lines.join('\n')
if (result.length > maximum) throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
return result
},
v4DeliveryPrompt(group, controller, input) {
const {
kind, phase, snapshot, receiptRecords, role, targetKinds, slot, options,
sessionBinding, skillHints = [],
} = input
const full = sessionBinding.sessionRotated === true || sessionBinding.hasSession !== true
const packageFloor = phase === 'proposal' ? 0 : collaborationPackageIndexText(targetKinds).length
let remaining = 6000 - packageFloor
let assignedProposalText = phase === 'challenge'
? this.v4SanitizeDeliveryText(options.assignedProposalText || '', 1600)
: ''
if (assignedProposalText) {
remaining = Math.max(0, remaining - 2)
assignedProposalText = assignedProposalText.slice(0, remaining)
remaining -= assignedProposalText.length
}
const rawExtraContext = String(options.extraContext || '').trim()
const requestedExtraContext = this.v4SanitizeDeliveryText(rawExtraContext, 6000)
const artifactContextPlan = options.artifactContext?.coordinationPlan || null
const structuredArtifactContext = ['synthesis', 'verification'].includes(phase)
&& options.artifactContext && typeof options.artifactContext === 'object'
&& !Array.isArray(options.artifactContext)
if ((phase === 'work' || structuredArtifactContext) && rawExtraContext.length > 6000) {
throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
}
if ((phase === 'work' || structuredArtifactContext)
&& requestedExtraContext !== rawExtraContext) {
throw new Error('LOCAL_RUN_V4_ASSIGNMENT_REFERENCE_INVALID')
}
const extraLimit = Math.max(0, remaining - 2)
let extraContext = requestedExtraContext
if (structuredArtifactContext) {
const artifactLimit = extraLimit - (extraContext ? extraContext.length + 2 : 0)
const artifactContext = this.v4ArtifactContext(
receiptRecords,
phase,
artifactContextPlan,
kind,
Math.max(0, artifactLimit),
)
extraContext = [extraContext, artifactContext].filter(Boolean).join('\n\n')
if (extraContext.length > extraLimit) {
throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
}
} else if (extraContext.length > extraLimit) {
if (phase === 'work') throw new Error('LOCAL_RUN_V4_DELIVERY_BUDGET_EXCEEDED')
const marker = 'MELDWORK_V4_CONTEXT_TRUNCATED_V1'
if (extraLimit <= marker.length + 1) {
extraContext = ''
} else {
let prefix = extraContext.slice(0, extraLimit - marker.length - 1)
const boundary = prefix.lastIndexOf('\n')
if (boundary > 0) prefix = prefix.slice(0, boundary)
extraContext = `${prefix.trimEnd()}\n${marker}`
}
}
if (extraContext) remaining -= extraContext.length + 2
const packageRecord = phase === 'proposal'
? null
: this.v4Package(receiptRecords, {
recipientKind: kind,
sessionBinding,
deliveryState: controller.orchestration?.deliveryState || [],
targetKinds,
snapshotHash: controller.orchestration?.snapshotHash,
forceFull: full,
totalLimit: packageFloor + remaining,
})
const deliveryId = `delivery-${hashValue({
runId: controller.runId, recipientKind: kind, slotId: slot.slotId,
operationId: slot.operationId, packageHash: packageRecord?.packageHash || '', now: Date.now(),
}).slice(0, 64)}`
const delivery = packageRecord?.receipts?.length ? {
deliveryId,
packageHash: packageRecord.packageHash,
entries: packageRecord.receipts.map(receipt => ({
recipientKind: kind,
sessionRefHash: sessionBinding.sessionRefHash,
sessionProvenanceHash: sessionBinding.sessionProvenanceHash,
sourceAgentKind: receipt.agentKind,
sourcePhase: receipt.phase,
watermark: receipt.deliveryWatermark,
snapshotHash: receipt.snapshotHash,
operationId: receipt.operationId,
packageHash: packageRecord.packageHash,
deliveryId,
status: 'prepared',
updatedAt: Date.now(),
})),
} : null
this.v4PrepareDelivery(group, controller, delivery)
return {
prompt: this.v4PhasePrompt(group, kind, phase, snapshot, receiptRecords, role, {
reviewTarget: options.reviewTarget,
assignedProposalText,
packageText: packageRecord?.text || '',
extraContext,
snapshotHash: controller.orchestration?.snapshotHash,
skillHints,
}),
delivery,
}
},
v4PrepareDelivery(group, controller, delivery) {
if (delivery) {
const replaced = new Set(delivery.entries.map(entry => [
entry.recipientKind, entry.sessionRefHash, entry.sessionProvenanceHash,
entry.sourceAgentKind, entry.sourcePhase,
].join('\u0000')))
const deliveryState = [
...(controller.orchestration?.deliveryState || []).filter(entry => (
(!delivery.natural || entry.recipientKind !== delivery.entries[0].recipientKind
|| (entry.sessionRefHash === delivery.entries[0].sessionRefHash
&& entry.sessionProvenanceHash === delivery.entries[0].sessionProvenanceHash))
&& !replaced.has([
entry.recipientKind, entry.sessionRefHash, entry.sessionProvenanceHash,
entry.sourceAgentKind, entry.sourcePhase,
].join('\u0000')))),
...delivery.entries,
].sort((left, right) => [
left.recipientKind, left.sessionRefHash, left.sessionProvenanceHash,
left.sourceAgentKind, left.sourcePhase,
].join('\u0000').localeCompare([
right.recipientKind, right.sessionRefHash, right.sessionProvenanceHash,
right.sourceAgentKind, right.sourcePhase,
].join('\u0000')))
this.checkpointOrchestration(group, controller, { deliveryState })
}
},
v4SetDeliveryStatus(group, controller, delivery, status, binding = null) {
if (!delivery?.entries?.length) return
const deliveryState = (controller.orchestration?.deliveryState || []).map(entry => {
const matched = delivery.entries.some(candidate => candidate.deliveryId === entry.deliveryId
&& candidate.sourceAgentKind === entry.sourceAgentKind
&& candidate.sourcePhase === entry.sourcePhase
&& entry.status === 'prepared')
let sourceMessages = entry.sourceMessages
if (matched && sourceMessages && status === 'acknowledged'
&& binding?.sessionRefHash === entry.sessionRefHash
&& binding?.sessionProvenanceHash === entry.sessionProvenanceHash) {
const previous = delivery.previousEntries?.find(candidate => (
candidate.sourceAgentKind === entry.sourceAgentKind
&& candidate.sourcePhase === entry.sourcePhase
))
sourceMessages = [...new Map([
...(previous?.sourceMessages || []), ...sourceMessages,
].map(message => [message.id, message])).values()].slice(-100)
}
return matched ? {
...entry,
...(sourceMessages ? { sourceMessages } : {}),
...(binding ? {
sessionRefHash: binding.sessionRefHash,
sessionProvenanceHash: binding.sessionProvenanceHash,
} : {}),
status,
updatedAt: Date.now(),
} : entry
}).sort((left, right) => [
left.recipientKind, left.sessionRefHash, left.sessionProvenanceHash,
left.sourceAgentKind, left.sourcePhase,
].join('\u0000').localeCompare([
right.recipientKind, right.sessionRefHash, right.sessionProvenanceHash,
right.sourceAgentKind, right.sourcePhase,
].join('\u0000')))
this.checkpointOrchestration(group, controller, { deliveryState })
},
v4ChallengeTarget(kind, targetKinds, round = 1, seed = '') {
const kinds = Array.isArray(targetKinds) ? targetKinds : []
const index = kinds.indexOf(kind)
if (index < 0 || kinds.length < 2) return ''
const digest = createHash('sha256').update(JSON.stringify([seed, round, kinds])).digest('hex')
const offset = 1 + (Number.parseInt(digest.slice(0, 8), 16) % (kinds.length - 1))
return kinds[(index + offset) % kinds.length] || ''
},
v4ChallengeBindings({ controller, targetKinds, round, snapshotRecord, slots, receiptRecords }) {
const kinds = [...new Set(Array.isArray(targetKinds) ? targetKinds : [])].sort()
if (kinds.length < 2 || !Number.isSafeInteger(round) || round < 2) {
throw new Error('LOCAL_RUN_V4_CHALLENGE_BINDING_INVALID')
}
const digest = createHash('sha256').update(JSON.stringify([
snapshotRecord?.bodyHash || '', kinds,
])).digest('hex')
const initialOffset = 1 + (Number.parseInt(digest.slice(0, 8), 16) % (kinds.length - 1))
const offset = 1 + ((initialOffset - 1 + (round - 2)) % (kinds.length - 1))
const slotsByKind = new Map(slots.map(slot => [slot.agentKind, slot]))
const proposalsByKind = new Map()
for (const record of receiptRecords) {
const receipt = record?.receipt
if (receipt?.phase === 'proposal' && kinds.includes(receipt.agentKind)) {
proposalsByKind.set(receipt.agentKind, receipt)
}
}
return kinds.map((reviewerKind, index) => {
const proposalKind = kinds[(index + offset) % kinds.length]
const reviewerSlot = slotsByKind.get(reviewerKind)
const proposalSlot = slotsByKind.get(proposalKind)
const proposalReceipt = proposalsByKind.get(proposalKind)
if (!reviewerSlot || !proposalSlot || !proposalReceipt) {
throw new Error('LOCAL_RUN_V4_CHALLENGE_BINDING_INVALID')
}
return {
round,
reviewerKind,
reviewerSlotId: reviewerSlot.slotId,
reviewerOperationId: this.v4OperationId(
controller, reviewerKind, 'challenge', reviewerSlot.slotId,
),
proposalKind,
proposalSlotId: proposalSlot.slotId,
proposalOperationId: proposalReceipt.operationId,
proposalReceiptId: proposalReceipt.receiptId,
artifactIds: [...(proposalReceipt.artifactIds || [])],
evidenceIds: [...(proposalReceipt.evidenceIds || [])],
}
})
},
v4PhasePrompt(group, kind, phase, snapshot, receiptRecords, role, options = {}) {
const collaborationPackage = phase === 'proposal'
? ''
: String(options.packageText || this.v4Package(receiptRecords, {
recipientKind: kind,
forceFull: options.forceFullPackage === true,
snapshotHash: options.snapshotHash,
}).text)
const reviewTarget = phase === 'challenge' ? String(options.reviewTarget || '') : ''
const assignedProposalText = phase === 'challenge'
? this.v4SanitizeDeliveryText(options.assignedProposalText || '', 1600) : ''
const extraContext = this.v4SanitizeDeliveryText(options.extraContext || '', 6000)
return [
v4Prompt({
group, kind, phase, snapshot, role, skillHints: options.skillHints,
knowledgeBaseHints: options.knowledgeBaseHints,
naturalResponse: this.naturalAgentResponses,
}),
reviewTarget
? `Coverage responsibility: explicitly incorporate or challenge ${reviewTarget}'s proposal while negotiating with every peer. This coverage duty does not assign responsibilities or authorize you to allocate work for others.`
: '',
assignedProposalText,
collaborationPackage
? `Selected records from completed phases:\n${collaborationPackage}`
: '',
extraContext,
].filter(Boolean).join('\n\n')
},
}
module.exports = { v4DeliveryMethods }
@@ -0,0 +1,965 @@
// The V4 discussion run: snapshot, phases, resume and commit.
const { normalizeFailure } = require('../runs/failure-policy.cjs')
const { commitDeliveryObservable } = require('../collaboration/orchestration-v4-records.cjs')
const { v4Snapshot } = require('./local-workspace-context.cjs')
const { terminalRunStatusForReason } = require('./local-workspace-auto-runner-support.cjs')
const v4DiscussionMethods = {
async runV4Discussion(group, controller, threadRootId, context, resume = false, resumedGate = null) {
const snapshotTargetKinds = [...controller.targetKinds]
const existing = controller.orchestration?.version === 4
? controller.orchestration
: null
const targetKinds = [...snapshotTargetKinds]
controller.discussionStyle = existing?.discussionStyle
|| controller.discussionStyle
|| 'sequential'
let activeKinds = Array.isArray(existing?.activeKinds) && existing.activeKinds.length
? existing.activeKinds.filter(kind => targetKinds.includes(kind))
: [...targetKinds]
const removedKinds = new Set(targetKinds.filter(kind => !activeKinds.includes(kind)))
if (existing?.synthesisBinding) {
throw new Error('LOCAL_RUN_V4_LEGACY_SYNTHESIS_BINDING_UNSUPPORTED')
}
let synthesisBinding = existing?.synthesisBinding
? {
...existing.synthesisBinding,
candidates: existing.synthesisBinding.candidates.map(candidate => ({
...candidate,
...(candidate.evidence ? { evidence: { ...candidate.evidence } } : {}),
})),
verificationKinds: [...existing.synthesisBinding.verificationKinds],
}
: null
let synthesisRecovery = existing?.synthesisRecovery
? {
...existing.synthesisRecovery,
verificationKinds: [...existing.synthesisRecovery.verificationKinds],
rankedKinds: [...existing.synthesisRecovery.rankedKinds],
triedWriters: [...existing.synthesisRecovery.triedWriters],
attempts: existing.synthesisRecovery.attempts.map(attempt => ({ ...attempt })),
...(existing.synthesisRecovery.pendingGate
? { pendingGate: { ...existing.synthesisRecovery.pendingGate } }
: {}),
}
: null
let coordinationPlan = existing?.coordinationPlan
? {
...existing.coordinationPlan,
assignments: existing.coordinationPlan.assignments.map(assignment => ({
...assignment,
inputRefs: [...assignment.inputRefs],
artifactIds: [...assignment.artifactIds],
dependsOn: [...assignment.dependsOn],
})),
verifierKinds: [...existing.coordinationPlan.verifierKinds],
agreedBy: [...existing.coordinationPlan.agreedBy],
...(existing.coordinationPlan.supportReceiptIds
? { supportReceiptIds: [...existing.coordinationPlan.supportReceiptIds] }
: {}),
}
: null
const postSynthesisPhases = new Set([
'synthesis', 'verification', 'human-gate', 'commit', 'committed', 'completed',
])
const synthesisObservable = existing && (
postSynthesisPhases.has(existing.phase)
|| commitDeliveryObservable(existing.commitState)
|| existing.slots?.some(slot => (
postSynthesisPhases.has(slot.phase)
|| (slot.resultRefs?.workflowOutcomeRefs || []).some(item => (
['synthesis', 'verification'].includes(item?.receipt?.phase)
))
))
)
if (!synthesisBinding && !coordinationPlan && synthesisObservable) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_BINDING_REQUIRED')
}
let writerKind = synthesisRecovery?.activeWriterKind
|| coordinationPlan?.finalizerKind || synthesisBinding?.writerKind
|| existing?.discussionWriterKind
|| (!existing && this.naturalAgentResponses && group.allowWrite === true
? targetKinds.find(kind => this.canWriteWorkspace(kind)) : '') || ''
controller.currentRound = Math.max(1, controller.currentRound || existing?.round || 0)
const rootMessage = existing ? null : this.state().messages.find(message => (
message.id === threadRootId && message.groupId === group.id && message.role === 'user'
))
const snapshot = existing
? this.v4LoadSnapshot(existing, {
taskId: controller.taskId,
messageId: threadRootId,
groupId: controller.groupId,
targetKinds: snapshotTargetKinds,
})
: v4Snapshot({
state: this.state(),
group,
taskId: threadRootId,
targetKinds: snapshotTargetKinds,
message: rootMessage,
skillHintsByKind: context.rootSkillsByKind,
phase: 'proposal',
writerKind,
})
if (existing && this.naturalAgentResponses && !coordinationPlan && !synthesisBinding
&& controller.discussionStyle !== 'agent-led'
&& (snapshot.writerKind || '') !== writerKind) {
throw new Error('LOCAL_RUN_COLLABORATION_SCOPE_INVALID')
}
if (existing) {
const persistedSkills = this.v4SnapshotSkillHints(snapshot, snapshotTargetKinds)
const hasPersistedSkills = snapshotTargetKinds.some(kind => (
(persistedSkills.get(kind) || []).length > 0
))
context = {
rootAttachments: [],
rootSkillsByKind: hasPersistedSkills
? await this.v4RestoreSnapshotSkills(snapshot, snapshotTargetKinds, persistedSkills)
: persistedSkills,
rootKnowledgeBasesByKind: new Map(snapshotTargetKinds.map(kind => [kind, []])),
}
}
const builtSnapshot = existing
? { record: existing.snapshot, snapshotHash: existing.snapshotHash }
: this.v4SnapshotRecord(controller, snapshot, snapshotTargetKinds)
const snapshotRecord = builtSnapshot.record
const snapshotHash = builtSnapshot.snapshotHash
const batchId = existing?.batchId || this.v4BatchId(controller, 'discussion')
const receiptRecords = this.v4RestoreReceipts(controller)
let workReceipts = Array.isArray(existing?.workReceipts)
? existing.workReceipts.map(receipt => ({
...receipt,
snapshotContentRef: { ...receipt.snapshotContentRef },
collaborationReceipt: { ...receipt.collaborationReceipt },
artifacts: receipt.artifacts.map(artifact => ({
...artifact,
contentRef: { ...artifact.contentRef },
})),
}))
: []
controller.v4WorkReceipts = workReceipts
let challengeBindings = Array.isArray(existing?.challengeBindings)
? existing.challengeBindings.map(binding => ({
...binding,
artifactIds: [...binding.artifactIds],
evidenceIds: [...binding.evidenceIds],
}))
: null
controller.v4Watermarks = existing?.deliveryWatermarks
? existing.deliveryWatermarks.map(item => ({ ...item }))
: []
const initialSlots = existing?.slots?.length
? existing.slots.map(slot => ({ ...slot }))
: targetKinds.map((kind, index) => ({
slotId: `slot-${index + 1}-${kind}`,
agentKind: kind,
phase: 'proposal',
status: 'planned',
operationId: this.v4OperationId(
controller, kind, 'proposal', `slot-${index + 1}-${kind}`,
),
queuePosition: index,
snapshotHash,
deliveryWatermark: 0,
receiptId: '',
resultHash: '',
assignedAt: Date.now(),
startedAt: Date.now(),
finishedAt: null,
commitStatus: 'pending',
attempt: 0,
permission: 'read-only',
resultRefs: { artifactIds: [], evidenceIds: [], workflowOutcomeRefs: [] },
}))
let slots = initialSlots
if (!existing) {
this.v4CheckpointPhase(group, controller, {
targetKinds,
participantKinds: activeKinds,
phase: 'proposal',
batchId,
snapshotRecord,
snapshotHash,
slots,
writerKind,
currentKinds: targetKinds,
pendingKinds: targetKinds,
receipts: receiptRecords,
})
this.emitChanged()
}
const latestFor = (phase, kind) => this.v4LatestReceipt(receiptRecords, phase, kind)
const pendingKindsFor = (phase, activeKinds, workAssignments = []) => activeKinds.filter(kind => {
if (phase === 'work') {
const assignment = workAssignments.find(candidate => candidate.ownerKind === kind)
return !assignment || !workReceipts.some(receipt => (
receipt.planHash === coordinationPlan.planHash
&& receipt.taskId === assignment.taskId
&& receipt.ownerKind === kind
))
}
const latest = latestFor(phase, kind)
if (!latest) return true
if (phase === 'proposal') return false
const slot = slots.find(candidate => candidate.agentKind === kind)
const synthesisAttempt = phase === 'synthesis'
? [...(synthesisRecovery?.attempts || [])].reverse().find(attempt => (
attempt.writerKind === kind
))
: null
const expected = synthesisAttempt?.operationId || (slot
? this.v4OperationId(controller, kind, phase, slot.slotId)
: '')
return latest.receipt.operationId !== expected
})
const checkpointPhase = (phase, activeKinds, phaseSlots = slots, bindings = challengeBindings) => {
this.v4CheckpointPhase(group, controller, {
targetKinds,
participantKinds: activeKindsForRun(),
phase,
batchId,
snapshotRecord,
snapshotHash,
slots: phaseSlots,
writerKind,
currentKinds: [...activeKinds],
pendingKinds: [...activeKinds],
receipts: receiptRecords,
challengeBindings: bindings,
synthesisBinding,
synthesisRecovery,
coordinationPlan,
workReceipts,
})
}
const activeKindsForRun = () => [...activeKinds]
const runPhase = async (phase, requestedKinds, bindings = challengeBindings, options = {}) => {
const workAssignments = Array.isArray(options.workAssignments)
? options.workAssignments
: []
const pending = pendingKindsFor(phase, requestedKinds, workAssignments)
if (!pending.length) {
checkpointPhase(phase, [], slots)
return { ok: true, phaseReceipts: [], synthesisPendingMessage: null }
}
const runPending = async (phaseKinds) => {
const result = await this.runV4Phase(group, controller, threadRootId, context, {
phase,
activeKinds: phaseKinds,
targetKinds,
participantKinds: activeKindsForRun(),
writerKind,
batchId,
snapshot,
snapshotRecord,
snapshotHash,
slots,
receiptRecords,
challengeBindings: bindings,
synthesisBinding,
synthesisRecovery,
coordinationPlan,
workReceipts,
deliveryReceiptRecords: options.deliveryReceiptRecords,
coordinationText: options.coordinationText || '',
workAssignments,
resumedGate,
})
slots = result.slots
if (result.synthesisRecovery) synthesisRecovery = result.synthesisRecovery
if (result.workReceipts) {
workReceipts = result.workReceipts
controller.v4WorkReceipts = workReceipts
}
return result
}
if (!['challenge', 'verification'].includes(phase) || pending.length <= 1) {
return runPending(pending)
}
const aggregate = {
slots,
receiptRecords,
phaseReceipts: [],
phasePendingMessages: [],
failures: [],
synthesisPendingMessage: null,
ok: true,
}
for (const kind of pending) {
const result = await runPending([kind])
aggregate.slots = result.slots
aggregate.phaseReceipts.push(...(result.phaseReceipts || []))
aggregate.phasePendingMessages.push(...(result.phasePendingMessages || []))
aggregate.failures.push(...(result.failures || []))
if (!result.ok) aggregate.ok = false
}
return aggregate
}
const commitPhaseMessages = (phase, result) => {
// Proposal/work runners commit each pending Agent message at the same
// time they persist its receipt and slot. Replaying those messages here
// creates a second commit attempt with live metadata and can reject a
// valid first-round result as LOCAL_RUN_COMMIT_INVALID.
return (result?.phasePendingMessages || []).filter(item => (
item?.pendingMessage?.content && item.kind
)).length
}
const removePhaseFailures = (result) => {
const failedKinds = [...new Set((result?.failures || [])
.filter((item) => {
const failure = normalizeFailure(item.error)
return !['budget', 'cancellation', 'permission'].includes(failure.category)
&& !failure.code.startsWith('COLLABORATION_CONTROL_')
})
.map(item => item.kind)
.filter(Boolean))]
const removed = []
for (const kind of failedKinds) {
const failure = (result?.failures || []).find(item => item.kind === kind)?.error
// Every failure is a run-scoped exclusion. Group membership is never
// edited, so a later turn can use the Agent again.
const didRemove = this.shouldPauseFailedAgent(failure)
? this.pauseFailedAgentForRun(group, controller, kind, threadRootId, failure)
: false
// The failed slot is still isolated from this run's active queue even
// when its configured group membership is retained.
removed.push(kind)
if (didRemove) {
removedKinds.add(kind)
}
}
return removed
}
const finalStatus = (status) => {
if (status !== 'completed') return status
if (!removedKinds.size && !controller.failedKinds.length) return status
controller.failedKinds = [...new Set([...controller.failedKinds, ...removedKinds])]
return 'partial'
}
const addRoundLimitNotice = () => {
if (this.state().messages.some(message => (
message.threadRootId === threadRootId
&& message.system?.key === 'system.autoRoundLimit'
))) return
this.addMessage(
group.id,
'system',
`Automatic discussion reached its ${controller.maxRounds || 6}-round limit. The latest replies above are the current result.`,
'',
threadRootId,
{ key: 'system.autoRoundLimit', params: { rounds: controller.maxRounds || 6 } },
)
}
const requestStableGate = async (convergence) => {
if (!this.requestHumanGate) return 'round-limit'
const gateAgentRunId = this.v4OperationId(controller, writerKind, 'human-gate',
slots.find(slot => slot.agentKind === writerKind)?.slotId || '')
const publicAgentRunId = slots.find(slot => slot.agentKind === writerKind)?.agentRunId || ''
checkpointPhase('human-gate', [], slots)
const gate = await this.requestHumanGate({
type: 'decision',
runId: controller.runId,
agentRunId: gateAgentRunId,
agentKind: writerKind,
summary: 'The candidate and unresolved issues have remained unchanged for two rounds.',
options: [
{ optionId: 'continue-discussion', name: 'Continue', kind: 'accept' },
{ optionId: 'stop-discussion', name: 'Stop', kind: 'reject' },
],
request: {
phase: 'discussion',
round: convergence.lastCompletedRound,
candidateHash: convergence.candidateContentHash,
unresolvedIssueIds: [...convergence.openIssueIds],
stateEpoch: convergence.stateEpoch,
},
}, {
signal: controller.signal,
preserveOnAbort: () => controller.stopReason === 'shutdown',
continuation: {
resumeKind: 'v4_human_gate',
agentRunId: gateAgentRunId,
...(publicAgentRunId ? { publicAgentRunId } : {}),
agentKind: writerKind,
round: convergence.lastCompletedRound,
stateEpoch: convergence.stateEpoch,
},
})
if (gate.status !== 'approved') {
if (gate.gateId) this.completeHumanGateContinuation?.(
controller.runId, gate.gateId, 'cancelled',
)
return 'stopped'
}
const current = controller.orchestration?.convergence
if (!current || current.stateEpoch !== convergence.stateEpoch
|| current.stateKey !== convergence.stateKey) {
throw new Error('LOCAL_RUN_CONTINUATION_INVALID')
}
controller.orchestration = {
...controller.orchestration,
convergence: { ...current, acknowledgedGateEpoch: current.stateEpoch },
}
if (gate.gateId && this.completeHumanGateContinuation?.(
controller.runId, gate.gateId, 'completed',
) !== true && this.hasRunLedger()) throw new Error('LOCAL_RUN_PERSIST_FAILED')
return 'continue'
}
const requestSynthesisRecoveryGate = async () => {
const binding = synthesisRecovery?.pendingGate
if (!binding || !this.requestHumanGate) return 'failed'
const publicAgentRunId = slots.find(slot => (
slot.slotId === binding.slotId && slot.agentKind === binding.writerKind
))?.agentRunId || ''
const permissionContinuation = controller.continuation
if (permissionContinuation?.gateType === 'permission'
&& permissionContinuation.resumeKind === 'agent_slot'
&& permissionContinuation.phase === 'synthesis'
&& permissionContinuation.agentKind === binding.writerKind
&& permissionContinuation.slotId === binding.slotId
&& permissionContinuation.operationId === binding.operationId) {
if (this.completeHumanGateContinuation?.(
controller.runId, permissionContinuation.gateId, 'completed',
) !== true && this.hasRunLedger()) throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
slots = slots.map(slot => slot.status === 'running'
? { ...slot, status: 'waiting' }
: slot)
checkpointPhase('human-gate', [], slots)
const gate = await this.requestHumanGate({
type: 'decision',
runId: controller.runId,
agentRunId: binding.operationId,
agentKind: binding.writerKind,
summary: 'The workspace-write synthesis attempt may have produced side effects, but its result is unknown.',
options: [
{ optionId: 'retry-original-writer', name: 'Retry original writer', kind: 'accept' },
...(binding.proposedReplacementKind ? [{
optionId: 'replace-next-writer', name: 'Replace with next writer', kind: 'accept',
}] : []),
{ optionId: 'stop-discussion', name: 'Stop', kind: 'reject' },
],
request: {
phase: 'synthesis-recovery',
bindingHash: binding.bindingHash,
writerKind: binding.writerKind,
slotId: binding.slotId,
operationId: binding.operationId,
attempt: binding.attempt,
proposedReplacementKind: binding.proposedReplacementKind,
round: binding.round,
stateEpoch: binding.stateEpoch,
outcomeCertainty: 'unknown_outcome',
},
}, {
signal: controller.signal,
preserveOnAbort: () => controller.stopReason === 'shutdown',
continuation: {
resumeKind: 'v4_synthesis_recovery',
agentRunId: binding.operationId,
...(publicAgentRunId ? { publicAgentRunId } : {}),
agentKind: binding.writerKind,
round: binding.round,
stateEpoch: binding.stateEpoch,
},
})
if (gate.status !== 'approved' || gate.optionId === 'stop-discussion') {
synthesisRecovery = this.v4CancelSynthesisRecovery(synthesisRecovery)
checkpointPhase('human-gate', [], slots)
if (gate.gateId) this.completeHumanGateContinuation?.(
controller.runId, gate.gateId, 'cancelled',
)
return 'stopped'
}
if (!['retry-original-writer', 'replace-next-writer'].includes(gate.optionId)) {
throw new Error('LOCAL_RUN_CONTINUATION_INVALID')
}
const nextWriterKind = gate.optionId === 'replace-next-writer'
? binding.proposedReplacementKind
: binding.writerKind
if (!nextWriterKind) throw new Error('LOCAL_RUN_CONTINUATION_INVALID')
if (gate.gateId && this.completeHumanGateContinuation?.(
controller.runId, gate.gateId, 'completed',
) !== true && this.hasRunLedger()) throw new Error('LOCAL_RUN_PERSIST_FAILED')
synthesisRecovery = this.v4AppendSynthesisAttempt(
controller, coordinationPlan || synthesisBinding, synthesisRecovery, slots, nextWriterKind,
)
writerKind = synthesisRecovery.activeWriterKind
const nextAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
slots = slots.map((slot) => {
if (slot.agentKind !== writerKind) return { ...slot, permission: 'read-only' }
const { agentRunId: _agentRunId, ...retainedSlot } = slot
return {
...retainedSlot,
phase: 'synthesis',
status: 'planned',
operationId: nextAttempt.operationId,
receiptId: '',
resultHash: '',
finishedAt: null,
commitStatus: 'pending',
permission: group.allowWrite === true ? 'workspace-write' : 'read-only',
}
})
checkpointPhase('synthesis', [writerKind], slots)
return 'continue'
}
let round = Math.max(1, Number(existing?.round) || controller.currentRound || 1)
const acknowledgedGateResume = resume === true
&& existing?.phase === 'challenge'
&& existing.convergence?.acknowledgedGateEpoch === existing.convergence?.stateEpoch
&& existing.convergence?.lastCompletedRound === round
&& controller.continuation?.resumeKind === 'v4_human_gate'
&& controller.continuation?.state === 'completed'
if (acknowledgedGateResume) {
this.assertV4PhaseCanAdvance(controller)
round += 1
controller.currentRound = round
challengeBindings = null
}
let proposalComplete = activeKinds.every(kind => Boolean(latestFor('proposal', kind)))
const legacyBoundRun = Boolean(synthesisBinding && !coordinationPlan)
const postChallengePhases = new Set([
'coordination', 'work', 'synthesis', 'verification',
'human-gate', 'commit', 'committed', 'completed',
])
const challengeObservable = existing && (
postChallengePhases.has(existing.phase)
|| existing.slots?.some(slot => (
(slot.phase === 'challenge' && slot.status !== 'planned')
|| postChallengePhases.has(slot.phase)
|| (slot.resultRefs?.workflowOutcomeRefs || []).some(item => (
item?.receipt?.phase === 'challenge'
))
))
)
if (!coordinationPlan && !legacyBoundRun
&& !challengeBindings && challengeObservable && !acknowledgedGateResume) {
throw new Error('LOCAL_RUN_V4_CHALLENGE_BINDING_REQUIRED')
}
if (!coordinationPlan && !legacyBoundRun
&& challengeBindings?.some(binding => binding.round !== round)) {
throw new Error('LOCAL_RUN_V4_CHALLENGE_BINDING_INVALID')
}
if (existing?.candidateCommit) {
const verificationKinds = synthesisRecovery?.verificationKinds
|| coordinationPlan?.verifierKinds
|| synthesisBinding?.verificationKinds
|| []
const validationSlots = slots.map(slot => (
slot.agentKind === writerKind && slot.phase === 'completed'
? { ...slot, phase: 'synthesis' }
: slot
))
const acceptance = this.v4Acceptance(
receiptRecords, activeKinds, writerKind, verificationKinds,
{
runId: controller.runId,
round,
slots: validationSlots,
challengeBindings,
previousConvergence: null,
coordinationPlan,
workReceipts,
},
)
if (!acceptance.accepted
|| acceptance.candidateArtifactId !== existing.candidateCommit.candidateArtifactId
|| acceptance.candidateHash !== existing.candidateCommit.candidateContentHash) {
throw new Error('LOCAL_RUN_V4_CANDIDATE_INVALID')
}
return this.v4CommitAcceptedCandidate({
group, controller, threadRootId, targetKinds, batchId, snapshotRecord, snapshotHash,
slots, receiptRecords, challengeBindings, synthesisBinding, synthesisRecovery,
coordinationPlan, workReceipts, convergence: existing.convergence,
writerKind, verificationKinds, acceptance, participantKinds: activeKinds,
})
}
if (this.naturalAgentResponses && !coordinationPlan && !synthesisBinding && !synthesisRecovery) {
return this.runV4NaturalDiscussion(group, controller, threadRootId, context, {
resume,
resumedGate,
targetKinds,
activeKinds,
writerKind,
batchId,
snapshot,
snapshotRecord,
snapshotHash,
slots,
receiptRecords,
latestFor,
runPhase,
checkpointPhase,
removePhaseFailures,
addRoundLimitNotice,
})
}
while (!controller.signal.aborted) {
controller.currentRound = round
if (!proposalComplete) {
const proposal = await runPhase('proposal', activeKinds)
commitPhaseMessages('proposal', proposal)
if (!proposal.ok) {
const failedKinds = removePhaseFailures(proposal)
activeKinds = activeKinds.filter(kind => !failedKinds.includes(kind))
checkpointPhase('proposal', [], slots)
if (!failedKinds.length || activeKinds.length < 2) {
return proposal.phasePendingMessages?.length ? 'partial' : 'failed'
}
}
proposalComplete = activeKinds.every(kind => Boolean(latestFor('proposal', kind)))
if (!proposalComplete) {
return activeKinds.some(kind => Boolean(latestFor('proposal', kind))) ? 'partial' : 'failed'
}
}
if (!coordinationPlan && !legacyBoundRun) {
if (round === 1) {
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
this.assertV4PhaseCanAdvance(controller)
round = 2
controller.currentRound = round
}
if (!challengeBindings || challengeBindings[0]?.round !== round) {
const activeSet = new Set(activeKinds)
slots = slots.map((slot) => {
if (!activeSet.has(slot.agentKind)) return slot
const { agentRunId: _agentRunId, ...retainedSlot } = slot
return {
...retainedSlot,
phase: 'challenge',
status: 'planned',
operationId: this.v4OperationId(
controller, slot.agentKind, 'challenge', slot.slotId,
),
receiptId: '',
resultHash: '',
startedAt: Date.now(),
finishedAt: null,
commitStatus: 'pending',
permission: 'read-only',
}
})
challengeBindings = this.v4ChallengeBindings({
controller,
targetKinds: activeKinds,
round,
snapshotRecord,
slots,
receiptRecords,
})
checkpointPhase('challenge', activeKinds, slots, challengeBindings)
this.emitChanged()
}
const knownCoordination = this.v4CoordinationResult({
receiptRecords,
targetKinds: activeKinds,
snapshotHash,
snapshot,
requireSupport: false,
})
const challenge = await runPhase('challenge', activeKinds, challengeBindings, {
coordinationText: this.v4CoordinationText(knownCoordination),
})
commitPhaseMessages('challenge', challenge)
if (!challenge.ok) {
if (controller.signal.aborted) return 'stopped'
const failedKinds = removePhaseFailures(challenge)
activeKinds = activeKinds.filter(kind => !failedKinds.includes(kind))
if (!failedKinds.length || activeKinds.length < 2
|| (!controller.unlimitedRounds && round >= (controller.maxRounds || 6))) {
return challenge.phasePendingMessages?.length ? 'partial' : 'failed'
}
this.assertV4PhaseCanAdvance(controller)
round += 1
controller.currentRound = round
challengeBindings = null
this.emitChanged()
continue
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
const coordination = this.v4CoordinationResult({
receiptRecords,
targetKinds: activeKinds,
snapshotHash,
snapshot,
challengeBindings,
})
if (!coordination.plan) {
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
this.assertV4PhaseCanAdvance(controller)
round += 1
controller.currentRound = round
challengeBindings = null
this.emitChanged()
continue
}
coordinationPlan = coordination.plan
writerKind = coordinationPlan.finalizerKind
this.v4CheckpointPhase(group, controller, {
targetKinds,
participantKinds: activeKinds,
phase: 'coordination',
batchId,
snapshotRecord,
snapshotHash,
slots,
writerKind,
currentKinds: [],
pendingKinds: [],
receipts: receiptRecords,
challengeBindings,
coordinationPlan,
})
this.emitChanged()
}
if (coordinationPlan) {
while (!controller.signal.aborted) {
const workState = this.v4WorkState(workReceipts, coordinationPlan)
if (!workState.pending.length) break
if (!workState.ready.length) return 'failed'
const readyAssignments = []
const readyOwners = new Set()
for (const assignment of workState.ready) {
if (readyOwners.has(assignment.ownerKind)) continue
readyOwners.add(assignment.ownerKind)
readyAssignments.push(assignment)
}
const readyKinds = activeKinds.filter(kind => readyOwners.has(kind))
const completedBefore = workState.completed.size
const work = await runPhase('work', readyKinds, challengeBindings, {
deliveryReceiptRecords: [...receiptRecords],
workAssignments: readyAssignments,
})
if (!work.ok) return controller.signal.aborted ? 'stopped' : 'failed'
const completedAfter = this.v4WorkState(workReceipts, coordinationPlan).completed.size
if (completedAfter <= completedBefore) return 'failed'
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
writerKind = coordinationPlan.finalizerKind
} else {
writerKind = synthesisRecovery?.activeWriterKind || synthesisBinding.writerKind
}
const roleBinding = coordinationPlan || synthesisBinding
if (!roleBinding) return 'failed'
if (!synthesisRecovery) {
synthesisRecovery = this.v4CreateSynthesisRecovery(
controller,
roleBinding,
slots,
{ stateEpoch: controller.orchestration?.convergence?.stateEpoch || 0 },
)
writerKind = synthesisRecovery.activeWriterKind
} else if (!this.v4ActiveSynthesisAttempt(synthesisRecovery)) {
const latestAttempt = synthesisRecovery.attempts.at(-1)
const currentRoundOperationId = latestAttempt
? this.v4SynthesisAttemptOperationId(
controller,
latestAttempt.writerKind,
latestAttempt.slotId,
latestAttempt.attempt,
)
: ''
if (latestAttempt?.status === 'completed'
&& latestAttempt.operationId !== currentRoundOperationId) {
synthesisRecovery = this.v4AppendSynthesisAttempt(
controller,
roleBinding,
synthesisRecovery,
slots,
synthesisRecovery.activeWriterKind,
)
}
}
let activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
if (activeAttempt?.status === 'leased'
&& activeAttempt.permission === 'workspace-write'
&& activeAttempt.leaseAcquired === true) {
const acceptedReceipt = this.v4TrustedSynthesisResult({
controller,
slots,
activeAttempt,
synthesisRecovery,
coordinationPlan,
snapshotHash,
})
if (!acceptedReceipt) {
synthesisRecovery = this.v4UpdateSynthesisAttempt(
synthesisRecovery,
activeAttempt.operationId,
{
status: 'unknown_outcome',
sideEffectsPossible: true,
outcomeCertainty: 'unknown_outcome',
},
)
synthesisRecovery = {
...synthesisRecovery,
pendingGate: this.v4SynthesisRecoveryGate(controller, synthesisRecovery),
}
activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
}
}
if (activeAttempt?.status === 'unknown_outcome') {
if (!synthesisRecovery.pendingGate) {
synthesisRecovery = {
...synthesisRecovery,
pendingGate: this.v4SynthesisRecoveryGate(controller, synthesisRecovery),
}
}
const recoveryResult = await requestSynthesisRecoveryGate()
if (recoveryResult !== 'continue') return recoveryResult
}
let verificationKinds = [...synthesisRecovery.verificationKinds]
let synthesis = null
while (!controller.signal.aborted) {
writerKind = synthesisRecovery.activeWriterKind
verificationKinds = [...synthesisRecovery.verificationKinds]
synthesis = await runPhase('synthesis', [writerKind])
if (synthesis.ok) break
if (controller.signal.aborted) return 'stopped'
const failedAttempt = [...synthesisRecovery.attempts].reverse()[0]
if (failedAttempt?.status === 'unknown_outcome') {
const recoveryResult = await requestSynthesisRecoveryGate()
if (recoveryResult !== 'continue') return recoveryResult
continue
}
const replacement = coordinationPlan
? null
: this.v4ReplaceSynthesisWriter(
controller, synthesisBinding, synthesisRecovery, slots,
)
if (!replacement) return 'failed'
synthesisRecovery = replacement
writerKind = replacement.activeWriterKind
verificationKinds = [...replacement.verificationKinds]
}
if (!synthesis?.ok) return controller.signal.aborted ? 'stopped' : 'failed'
let synthesisPendingMessage = synthesis.synthesisPendingMessage
const synthesisReceipt = latestFor('synthesis', writerKind)
if (!synthesisPendingMessage && synthesisReceipt) {
const candidateArtifactId = synthesisReceipt.receipt.artifactIds?.[0]
let candidateContent = ''
if (candidateArtifactId) {
try { candidateContent = this.v4ArtifactIdentity(candidateArtifactId).content } catch {}
}
synthesisPendingMessage = {
groupId: group.id,
role: 'agent',
content: candidateContent
|| synthesisReceipt.receipt.conclusion
|| synthesisReceipt.receipt.summary,
agentKind: writerKind,
threadRootId,
system: null,
metadata: {},
}
}
if (!synthesisPendingMessage) return 'failed'
const previousConvergence = controller.orchestration?.convergence || null
this.v4RoundState(
receiptRecords, activeKinds, writerKind, verificationKinds,
{
runId: controller.runId,
round,
slots,
challengeBindings,
previousConvergence,
coordinationPlan,
includeVerification: false,
},
)
const verification = await runPhase('verification', verificationKinds)
if (!verification.ok) return controller.signal.aborted ? 'stopped' : 'partial'
const acceptance = this.v4Acceptance(
receiptRecords, activeKinds, writerKind, verificationKinds,
{
runId: controller.runId,
round,
slots,
challengeBindings,
previousConvergence,
coordinationPlan,
workReceipts,
},
)
const convergence = this.v4NextConvergence(
previousConvergence,
{
artifactId: acceptance.candidateArtifactId,
contentHash: acceptance.candidateHash,
},
acceptance.openIssueIds,
round,
writerKind,
)
this.v4CheckpointPhase(group, controller, {
targetKinds,
participantKinds: activeKinds,
phase: 'verification',
batchId,
snapshotRecord,
snapshotHash,
slots,
writerKind,
currentKinds: [],
pendingKinds: [],
receipts: receiptRecords,
challengeBindings,
synthesisBinding,
coordinationPlan,
convergence,
})
const acceptedStable = controller.unlimitedRounds
? convergence.consecutiveStableRounds >= 2
: round >= (controller.maxRounds || 6)
if (acceptance.accepted && acceptedStable) {
return finalStatus(await this.v4CommitAcceptedCandidate({
group, controller, threadRootId, targetKinds, batchId, snapshotRecord, snapshotHash,
slots, receiptRecords, challengeBindings, synthesisBinding, synthesisRecovery,
coordinationPlan, workReceipts, convergence,
writerKind, verificationKinds, acceptance, participantKinds: activeKinds,
}))
}
if (this.v4ShouldOpenStableGate(convergence, controller.unlimitedRounds)) {
const gateResult = await requestStableGate(convergence)
if (gateResult !== 'continue') return gateResult
}
if (!controller.unlimitedRounds && round >= (controller.maxRounds || 6)) {
addRoundLimitNotice()
return 'round-limit'
}
if (controller.signal.aborted) break
this.assertV4PhaseCanAdvance(controller)
round += 1
controller.currentRound = round
if (coordinationPlan) {
challengeBindings = null
if (Object.hasOwn(controller.orchestration || {}, 'challengeBindings')) {
const { challengeBindings: _challengeBindings, ...orchestration } = controller.orchestration
controller.orchestration = orchestration
}
}
this.emitChanged()
}
return terminalRunStatusForReason(controller.stopReason)
},
}
module.exports = { v4DiscussionMethods }
@@ -0,0 +1,875 @@
// Running one V4 phase for a set of Agents.
const { collaborationPackageIndexText, emptyCollaborationState } = require('../collaboration/collaboration-records.cjs')
const { hashValue } = require('../collaboration/orchestration-v4-records.cjs')
const { stalePermissionResumeFailure } = require('./local-workspace-auto-runner-support.cjs')
const v4PhaseMethods = {
v4PhaseAdvanceBlocked(controller) {
const orchestration = controller.orchestration
const continuation = controller.continuation
return orchestration?.version === 4
&& continuation?.resumeKind === 'agent_slot'
&& continuation.phase === orchestration.phase
&& continuation.round === orchestration.round
// `resuming` means the gate decision has already been accepted and the
// continuation is actively replaying the gated slot. Blocking that
// state makes the resume path fail with its own phase-gate error.
&& ['pending', 'ready'].includes(continuation.state)
},
assertV4PhaseCanAdvance(controller) {
if (this.v4PhaseAdvanceBlocked(controller)) {
throw new Error('LOCAL_RUN_V4_PHASE_GATE_PENDING')
}
},
v4PhaseSlots({ controller, targetKinds, phase, activeKinds, slots, snapshotHash,
writerKind, operationIds = null, allowWrite = false, now = Date.now() }) {
const active = new Set(activeKinds)
return slots.map((slot, index) => {
const running = active.has(slot.agentKind)
const previousHistory = Array.isArray(slot.resultRefs?.workflowOutcomeRefs)
? slot.resultRefs.workflowOutcomeRefs
: []
const operationId = running
? (operationIds?.get(slot.agentKind)
|| this.v4OperationId(controller, slot.agentKind, phase, slot.slotId))
: slot.operationId
const { agentRunId: previousAgentRunId, ...retainedSlot } = slot
return {
...retainedSlot,
...(!running && operationId === slot.operationId && previousAgentRunId
? { agentRunId: previousAgentRunId }
: {}),
slotId: slot.slotId || `slot-${index + 1}-${slot.agentKind}`,
phase: running ? phase : (slot.phase || phase),
status: running ? 'planned' : (slot.status || 'completed'),
operationId,
queuePosition: index,
snapshotHash,
deliveryWatermark: Number(slot.deliveryWatermark) || 0,
receiptId: running ? '' : (slot.receiptId || ''),
resultHash: running ? '' : (slot.resultHash || ''),
assignedAt: slot.assignedAt || now,
startedAt: running ? now : (slot.startedAt || now),
finishedAt: running ? null : (slot.finishedAt || now),
commitStatus: running ? 'pending' : (slot.commitStatus || 'committed'),
attempt: (Number.isSafeInteger(slot.attempt) ? slot.attempt : 0) + (running ? 1 : 0),
permission: allowWrite && ((this.naturalAgentResponses && this.canWriteWorkspace(slot.agentKind))
|| (slot.agentKind === writerKind && phase === 'synthesis'))
? 'workspace-write'
: 'read-only',
resultRefs: {
artifactIds: Array.isArray(slot.resultRefs?.artifactIds)
? [...slot.resultRefs.artifactIds]
: [],
evidenceIds: Array.isArray(slot.resultRefs?.evidenceIds)
? [...slot.resultRefs.evidenceIds]
: [],
workflowOutcomeRefs: [...previousHistory],
},
}
})
},
v4CheckpointPhase(group, controller, {
targetKinds, phase, batchId, snapshotRecord, snapshotHash, slots,
writerKind, currentKinds = [], pendingKinds = [], receipts = [], commitState = null,
participantKinds = targetKinds,
challengeBindings = controller.orchestration?.round === controller.currentRound
? controller.orchestration?.challengeBindings
: null,
synthesisBinding = controller.orchestration?.synthesisBinding || null,
synthesisRecovery = controller.orchestration?.synthesisRecovery || null,
convergence = controller.orchestration?.convergence || null,
coordinationPlan = controller.orchestration?.coordinationPlan || null,
workReceipts = controller.orchestration?.workReceipts || [],
candidateCommit = controller.orchestration?.candidateCommit || null,
workAssignments = [],
}) {
const current = controller.orchestration
const naturalWriterKind = this.naturalAgentResponses && ['proposal', 'discussion'].includes(phase)
&& !coordinationPlan && !synthesisBinding
? writerKind || current?.discussionWriterKind || '' : ''
if (current?.version === 4
&& (current.phase !== phase || current.round !== controller.currentRound)) {
this.assertV4PhaseCanAdvance(controller)
}
const workAssignmentsByKind = new Map(workAssignments.map(assignment => (
[assignment.ownerKind, assignment]
)))
const assignments = slots.map((slot, index) => ({
agentKind: slot.agentKind,
slotId: slot.slotId,
role: this.v4CheckpointRole(
phase, slot, coordinationPlan, workAssignmentsByKind.get(slot.agentKind) || null,
),
operationId: slot.operationId,
objective: phase === 'proposal'
? 'Produce an independent bounded proposal.'
: (phase === 'work' && workAssignmentsByKind.has(slot.agentKind)
? workAssignmentsByKind.get(slot.agentKind).objective
: `Complete the ${phase} collaboration phase using the peer-agreed responsibility plan.`),
expectedOutput: phase === 'work' && workAssignmentsByKind.has(slot.agentKind)
? workAssignmentsByKind.get(slot.agentKind).expectedOutput
: 'Return a concise structured collaboration result.',
inputRefs: receipts.map(item => item.receipt?.receiptId).filter(Boolean).slice(-64),
readOnly: slot.permission !== 'workspace-write',
index,
}))
const plan = {
version: 1,
snapshotHash,
assignments: assignments.map(({ index: _index, ...assignment }) => assignment),
createdAt: snapshotRecord.capturedAt,
barrier: 'phase',
}
const watermarks = Array.isArray(controller.v4Watermarks)
? controller.v4Watermarks
: []
const commit = commitState || {
status: 'pending',
writerKind: naturalWriterKind ? null : writerKind || null,
committedKinds: [],
pendingKinds: [...participantKinds],
operationId: '',
attempt: 0,
updatedAt: Date.now(),
committedSlotIds: [],
messageIds: [],
blackboardEntryIds: [],
}
const record = {
version: 4,
workflow: 'auto',
template: 'discussion',
phase,
discussionStyle: controller.discussionStyle
|| controller.orchestration?.discussionStyle
|| 'sequential',
...(naturalWriterKind ? { discussionWriterKind: naturalWriterKind } : {}),
batchId,
round: Math.max(0, Number(controller.currentRound) || 0),
currentKind: '',
currentKinds: [...currentKinds],
pendingKinds: [...pendingKinds],
activeKinds: [...participantKinds],
successfulKinds: slots
.filter(slot => ['completed', 'partial'].includes(slot.status))
.map(slot => slot.agentKind),
agreementKinds: [],
attachmentRecipients: [],
totalSuccesses: slots.filter(slot => slot.status === 'completed').length,
terminalFailureOccurred: slots.some(slot => slot.status === 'failed'),
snapshotHash,
snapshot: snapshotRecord,
plan,
slots,
deliveryWatermarks: watermarks,
...(Array.isArray(controller.orchestration?.deliveryState)
? { deliveryState: controller.orchestration.deliveryState.map(entry => ({ ...entry })) }
: {}),
commitState: commit,
...(candidateCommit ? { candidateCommit: { ...candidateCommit } } : {}),
collaboration: controller.orchestration?.collaboration || emptyCollaborationState(),
...(Array.isArray(challengeBindings)
? { challengeBindings: challengeBindings.map(binding => ({
...binding,
artifactIds: [...binding.artifactIds],
evidenceIds: [...binding.evidenceIds],
})) }
: {}),
...(synthesisBinding ? {
synthesisBinding: {
...synthesisBinding,
candidates: synthesisBinding.candidates.map(candidate => ({
...candidate,
...(candidate.evidence ? { evidence: { ...candidate.evidence } } : {}),
})),
verificationKinds: [...synthesisBinding.verificationKinds],
},
} : {}),
...(synthesisRecovery ? {
synthesisRecovery: {
...synthesisRecovery,
verificationKinds: [...synthesisRecovery.verificationKinds],
rankedKinds: [...synthesisRecovery.rankedKinds],
triedWriters: [...synthesisRecovery.triedWriters],
attempts: synthesisRecovery.attempts.map(attempt => ({ ...attempt })),
...(synthesisRecovery.pendingGate
? { pendingGate: { ...synthesisRecovery.pendingGate } }
: {}),
},
} : {}),
...(convergence ? { convergence: { ...convergence, openIssueIds: [...convergence.openIssueIds] } } : {}),
...(coordinationPlan ? {
coordinationPlan: {
...coordinationPlan,
assignments: coordinationPlan.assignments.map(assignment => ({ ...assignment })),
verifierKinds: [...coordinationPlan.verifierKinds],
agreedBy: [...coordinationPlan.agreedBy],
...(coordinationPlan.supportReceiptIds
? { supportReceiptIds: [...coordinationPlan.supportReceiptIds] }
: {}),
},
} : {}),
...(coordinationPlan ? {
workReceipts: workReceipts.map(receipt => ({
...receipt,
snapshotContentRef: { ...receipt.snapshotContentRef },
collaborationReceipt: { ...receipt.collaborationReceipt },
artifacts: receipt.artifacts.map(artifact => ({
...artifact,
contentRef: { ...artifact.contentRef },
})),
})),
} : {}),
}
this.checkpointOrchestration(group, controller, record)
return record
},
async runV4Phase(group, controller, threadRootId, context, phaseInput) {
const {
phase, activeKinds, targetKinds, writerKind, batchId,
snapshot, snapshotRecord, snapshotHash, slots, receiptRecords, challengeBindings,
} = phaseInput
const participantKinds = Array.isArray(phaseInput.participantKinds)
? phaseInput.participantKinds
: targetKinds
const coordinationPlan = phaseInput.coordinationPlan || null
const workAssignments = phase === 'work' && Array.isArray(phaseInput.workAssignments)
? phaseInput.workAssignments
: []
const workAssignmentsByKind = new Map(workAssignments.map(assignment => (
[assignment.ownerKind, assignment]
)))
if (phase === 'work' && (workAssignments.length !== activeKinds.length
|| workAssignmentsByKind.size !== activeKinds.length
|| activeKinds.some(kind => {
const assignment = workAssignmentsByKind.get(kind)
return !assignment || !coordinationPlan?.assignments?.some(candidate => (
candidate.taskId === assignment.taskId && candidate.ownerKind === kind
))
}))) {
throw new Error('LOCAL_RUN_V4_COORDINATION_PLAN_INVALID')
}
let workReceipts = Array.isArray(phaseInput.workReceipts)
? [...phaseInput.workReceipts]
: []
const deliveryReceiptRecords = Array.isArray(phaseInput.deliveryReceiptRecords)
? phaseInput.deliveryReceiptRecords
: [...receiptRecords]
const workContextLimit = Math.max(
0, 6000 - collaborationPackageIndexText(participantKinds).length - 2,
)
const extraContextFor = kind => phase === 'work'
? this.v4WorkAssignmentText(
coordinationPlan,
kind,
workAssignmentsByKind.get(kind)?.taskId,
snapshot,
workContextLimit,
deliveryReceiptRecords,
)
: (phase === 'challenge' ? phaseInput.coordinationText || '' : '')
let synthesisRecovery = phaseInput.synthesisRecovery || null
const synthesisAttempt = phase === 'synthesis'
? this.v4ActiveSynthesisAttempt(synthesisRecovery)
: null
const operationIds = phaseInput.operationIds instanceof Map
? phaseInput.operationIds
: synthesisAttempt
? new Map([[synthesisAttempt.writerKind, synthesisAttempt.operationId]])
: (phase === 'work'
? new Map(activeKinds.map((kind) => {
const slot = slots.find(candidate => candidate.agentKind === kind)
const assignment = workAssignmentsByKind.get(kind)
return [kind, this.v4OperationId(
controller, kind, `work:${assignment.taskId}`, slot?.slotId || '',
)]
}))
: null)
const phaseSlots = this.v4PhaseSlots({
controller,
targetKinds,
phase,
activeKinds,
slots,
snapshotHash,
writerKind,
operationIds,
allowWrite: group.allowWrite === true,
})
const resumedGate = phaseInput.resumedGate
const resumedGateFor = slot => (
resumedGate?.phase === phase
&& resumedGate.agentKind === slot.agentKind
&& resumedGate.slotId === slot.slotId
&& resumedGate.operationId === slot.operationId
&& resumedGate.snapshotHash === snapshotHash
&& resumedGate.round === controller.currentRound
? resumedGate
: null
)
const activeSet = new Set(activeKinds)
const phaseReceipts = []
const snapshotSourceMessageIds = [snapshot.messageId, ...snapshot.history.map(item => item.id)]
for (const slot of phaseSlots) {
if (activeSet.has(slot.agentKind)) {
slot.status = phase === 'synthesis' ? 'queued' : 'running'
}
}
const cursorPendingKinds = Array.isArray(phaseInput.pendingKinds)
? phaseInput.pendingKinds
: activeKinds
this.v4CheckpointPhase(group, controller, {
targetKinds,
phase,
batchId,
snapshotRecord,
snapshotHash,
slots: phaseSlots,
writerKind,
currentKinds: activeKinds,
pendingKinds: cursorPendingKinds,
participantKinds,
receipts: receiptRecords,
challengeBindings,
synthesisRecovery,
coordinationPlan,
workReceipts,
workAssignments,
})
this.emitChanged()
if (phase === 'proposal') {
const proposalTurns = this.reserveTurnSeqs(group.id, threadRootId, activeKinds.length)
const dispatches = activeKinds.map((kind, kindIndex) => {
const slot = phaseSlots.find(candidate => candidate.agentKind === kind)
const prompt = this.v4PhasePrompt(
group, kind, phase, snapshot, receiptRecords, 'participant', {
reviewTarget: '',
packageText: '',
skillHints: context.rootSkillsByKind.get(kind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(kind) || [],
},
)
const sessionPolicy = this.naturalAgentResponses ? null : 'frozen'
return {
kind,
slot,
context: {
v4: true,
phase,
batchId,
slotId: slot.slotId,
snapshotHash,
...(proposalTurns[kindIndex] ? { turnSeq: proposalTurns[kindIndex] } : {}),
...(sessionPolicy ? { sessionPolicy } : {}),
promptOverride: prompt,
contextPackId: controller.contextPackId,
snapshotSourceMessageIds,
snapshotSourceEntries: snapshot.history,
permissionMode: slot.permission,
singleWriterKind: this.naturalAgentResponses ? '' : writerKind,
independentProposal: this.naturalAgentResponses,
parallelGraph: true,
deferMessage: true,
allowMissingProposalReceipt: true,
allowMissingV4Receipt: this.naturalAgentResponses,
operationId: slot.operationId,
...(resumedGateFor(slot) ? { resumedGate: resumedGateFor(slot) } : {}),
skillHints: context.rootSkillsByKind.get(kind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(kind) || [],
...(this.naturalAgentResponses && context.rootAttachments?.length
? { attachmentSnapshots: context.rootAttachments }
: {}),
},
}
})
return this.runV4ProposalBatch(group, controller, threadRootId, {
targetKinds,
participantKinds,
writerKind,
batchId,
snapshotRecord,
snapshotHash,
phaseSlots,
receiptRecords,
dispatches,
resumedGate,
})
}
const failures = []
const phasePendingMessages = []
let synthesisPendingMessage = null
let workAdmissionClosed = false
let workAdmissionTail = Promise.resolve()
const admitWork = (item) => {
const admission = workAdmissionTail.then(() => {
if (workAdmissionClosed) return { ...item, workSettled: true }
const admitted = this.v4AdmitWorkSettlement(
group, controller, threadRootId, item, {
targetKinds,
participantKinds,
writerKind,
batchId,
snapshotRecord,
snapshotHash,
phaseSlots,
receiptRecords,
challengeBindings,
coordinationPlan,
workReceipts,
phaseReceipts,
workAssignments,
resumedGate: resumedGateFor(phaseSlots.find(candidate => (
candidate.agentKind === item.kind
))),
},
)
workReceipts = admitted.workReceipts
if (admitted.failure) failures.push(admitted.failure)
return { ...item, workSettled: true }
})
workAdmissionTail = admission.catch(() => { workAdmissionClosed = true })
return admission
}
// Batched peers keep dispatch order in the timeline whatever order they
// finish in.
const phaseTurns = this.reserveTurnSeqs(group.id, threadRootId, activeKinds.length)
const turnSeqByKind = new Map(activeKinds.map((kind, index) => [kind, phaseTurns[index]]))
const settled = await Promise.all(activeKinds.map(async kind => {
const slot = phaseSlots.find(candidate => candidate.agentKind === kind)
const challengeBinding = phase === 'challenge'
? challengeBindings?.find(binding => binding.reviewerKind === kind) || null
: null
const assignedProposalText = challengeBinding
? this.v4ChallengeProposalText(challengeBinding, kind)
: ''
const role = this.v4PromptRole(
phase, kind, coordinationPlan, workAssignmentsByKind.get(kind) || null,
)
const extraContext = extraContextFor(kind)
const promptOptions = {
reviewTarget: phase === 'challenge'
? challengeBinding?.proposalKind || ''
: '',
assignedProposalText,
packageText: '',
extraContext,
snapshotHash,
skillHints: context.rootSkillsByKind.get(kind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(kind) || [],
}
const prompt = typeof phaseInput.promptBuilder === 'function'
? phaseInput.promptBuilder({
kind, slot, role, options: promptOptions,
...(phaseInput.disableDeliveryPrompt === true ? { transcript: '' } : {}),
})
: this.v4PhasePrompt(
group,
kind,
phase,
snapshot,
deliveryReceiptRecords,
role,
promptOptions,
)
const sessionPolicy = phaseInput.sessionPolicy === undefined
? 'frozen'
: phaseInput.sessionPolicy
let preparedDelivery = null
let item
const resumedSlotGate = resumedGateFor(slot)
const recoveredResult = phase === 'discussion'
? this.v4NaturalRecoveredDiscussionResult(
group, controller, threadRootId, kind, slot,
)
: null
if (recoveredResult) return { kind, invocation: { result: recoveredResult } }
try {
const invocation = await this.invokeWithUnauthorizedRecovery({
group,
kind,
controller,
activeKinds: participantKinds,
threadRootId,
context: {
v4: true,
phase,
batchId,
slotId: slot.slotId,
snapshotHash,
...(turnSeqByKind.get(kind) ? { turnSeq: turnSeqByKind.get(kind) } : {}),
...(sessionPolicy ? { sessionPolicy } : {}),
promptOverride: prompt,
contextPackId: controller.contextPackId,
snapshotSourceMessageIds,
snapshotSourceEntries: snapshot.history,
permissionMode: slot.permission,
singleWriterKind: this.naturalAgentResponses ? '' : writerKind,
parallelGraph: true,
deferMessage: true,
allowMissingV4Receipt: this.naturalAgentResponses,
operationId: slot.operationId,
...(resumedSlotGate ? { resumedGate: resumedSlotGate } : {}),
v4SynthesisRecovery: phase === 'synthesis',
onLeaseAcquired: (leaseState) => {
if (!leaseState?.agentRunId) throw new Error('LOCAL_RUN_PERSIST_FAILED')
slot.agentRunId = leaseState.agentRunId
if (phase === 'synthesis') {
const activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
if (!activeAttempt || activeAttempt.operationId !== slot.operationId) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
synthesisRecovery = this.v4UpdateSynthesisAttempt(
synthesisRecovery,
activeAttempt.operationId,
{
status: 'leased',
permission: leaseState?.permissionMode === 'workspace-write'
? 'workspace-write' : 'read-only',
leaseAcquired: true,
sideEffectsPossible: leaseState?.sideEffectsPossible === true,
outcomeCertainty: leaseState?.sideEffectsPossible === true
? 'unknown_outcome' : 'not_started',
},
)
}
slot.status = 'running'
this.v4CheckpointPhase(group, controller, {
targetKinds,
phase,
batchId,
snapshotRecord,
snapshotHash,
slots: phaseSlots,
writerKind,
currentKinds: activeKinds,
pendingKinds: cursorPendingKinds,
participantKinds,
receipts: receiptRecords,
challengeBindings,
synthesisRecovery,
coordinationPlan,
workReceipts,
workAssignments,
})
},
...(phaseInput.disableDeliveryPrompt === true
? {
v4PromptBuilder: sessionBinding => {
const built = this.v4NaturalDeliveryPrompt(
group, controller, threadRootId, kind, slot, sessionBinding,
transcript => phaseInput.promptBuilder({ kind, slot, role, options: promptOptions, transcript }),
)
preparedDelivery = built.delivery
return built
},
}
: {
v4PromptBuilder: (sessionBinding) => {
const built = this.v4DeliveryPrompt(group, controller, {
kind, phase, snapshot, receiptRecords: deliveryReceiptRecords,
role, targetKinds: participantKinds, slot,
options: {
reviewTarget: phase === 'challenge'
? challengeBinding?.proposalKind || ''
: '',
assignedProposalText,
extraContext,
...(['synthesis', 'verification'].includes(phase)
? { artifactContext: { coordinationPlan } }
: {}),
},
sessionBinding,
skillHints: context.rootSkillsByKind.get(kind) || [],
})
preparedDelivery = built.delivery
return built
},
}),
skillHints: context.rootSkillsByKind.get(kind) || [],
knowledgeBaseHints: context.rootKnowledgeBasesByKind.get(kind) || [],
...(this.naturalAgentResponses && context.rootAttachments?.length
? { attachmentSnapshots: context.rootAttachments }
: {}),
},
})
if (invocation.result?.v4Delivery) {
this.v4SetDeliveryStatus(
group, controller, invocation.result.v4Delivery, 'acknowledged',
invocation.result.v4SessionBinding,
)
} else if (preparedDelivery) {
this.v4SetDeliveryStatus(group, controller, preparedDelivery, 'uncertain')
}
item = { kind, invocation }
} catch (error) {
if (preparedDelivery) this.v4SetDeliveryStatus(group, controller, preparedDelivery, 'uncertain')
if (stalePermissionResumeFailure(error, resumedSlotGate)) {
this.resetAgentSession(group, kind, true, controller.taskId)
item = { kind, error, permissionRestartRecovery: true }
} else {
item = { kind, error }
}
}
return phase === 'work' ? admitWork(item) : item
}))
for (const item of phase === 'work' ? [] : settled) {
const slot = phaseSlots.find(candidate => candidate.agentKind === item.kind)
const result = item.invocation?.result
if (controller.signal.aborted) {
slot.status = 'stopped'
slot.commitStatus = 'partial'
slot.finishedAt = Date.now()
const error = item.error || item.invocation?.error
if (error?.runTrace) {
this.recordAgentInterruption(group.id, item.kind, error, threadRootId,
controller.stopReason === 'shutdown' ? 'interrupted' : 'stopped')
}
continue
}
if (!result) {
const error = item.error || item.invocation?.error
|| new Error('LOCAL_AGENT_UNKNOWN_FAILURE')
slot.status = 'failed'
slot.commitStatus = 'failed'
slot.finishedAt = Date.now()
failures.push({ ...item, error })
if (!controller.signal.aborted) {
this.recordAgentFailure(group.id, item.kind, error, threadRootId, new Set())
}
if (phase === 'synthesis' && synthesisRecovery) {
const activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
if (activeAttempt?.operationId === slot.operationId) {
const failure = error?.invocationFailure || {}
const unknownWrite = activeAttempt.permission === 'workspace-write'
&& activeAttempt.leaseAcquired
&& (item.permissionRestartRecovery === true
|| (failure.outcomeCertainty !== 'not_started'
&& failure.outcomeCertainty !== 'known_failed'))
synthesisRecovery = this.v4UpdateSynthesisAttempt(
synthesisRecovery,
activeAttempt.operationId,
unknownWrite ? {
status: 'unknown_outcome',
sideEffectsPossible: true,
outcomeCertainty: 'unknown_outcome',
} : {
status: 'failed',
sideEffectsPossible: false,
outcomeCertainty: failure.outcomeCertainty === 'not_started'
? 'not_started' : 'known_failed',
},
)
}
}
continue
}
try {
const reportedOperationId = result?.message?.trace?.context?.operationId
|| result?.pendingMessage?.metadata?.trace?.context?.operationId
if (reportedOperationId && reportedOperationId !== slot.operationId) {
throw new Error('LOCAL_RUN_COLLABORATION_SCOPE_INVALID')
}
const receiptRecord = this.v4ReceiptForResult(
result,
phase,
item.kind,
slot,
snapshotHash,
{
controller,
...(phase === 'work'
? { workItemId: workAssignmentsByKind.get(item.kind)?.taskId || '' }
: {}),
reviewedArtifactId: phase === 'challenge'
? challengeBindings?.find(binding => (
binding.reviewerKind === item.kind
))?.artifactIds?.[0] || ''
: (phase === 'verification'
? [...receiptRecords].reverse().find(record => (
record.receipt?.phase === 'synthesis'
))?.receipt?.artifactIds?.[0] || ''
: ''),
},
)
if (phase === 'work') {
const assignment = workAssignmentsByKind.get(item.kind)
if (!assignment || receiptRecord.receipt.workItemId !== assignment.taskId) {
throw new Error('LOCAL_RUN_V4_WORK_RECEIPT_INVALID')
}
if (!this.v4ReceiptReferencesComplete(receiptRecord)) {
throw new Error('LOCAL_RUN_V4_WORK_RECEIPT_INVALID')
}
}
const duplicate = receiptRecords.find(existing => (
existing?.receipt?.receiptId === receiptRecord.receipt.receiptId
))
if (!duplicate) {
phaseReceipts.push(receiptRecord)
receiptRecords.push(receiptRecord)
}
const effectiveReceiptRecord = duplicate || receiptRecord
controller.v4ReceiptRecords = receiptRecords
slot.status = 'completed'
slot.commitStatus = 'pending'
slot.finishedAt = Date.now()
slot.receiptId = effectiveReceiptRecord.receipt.receiptId
slot.deliveryWatermark = effectiveReceiptRecord.receipt.deliveryWatermark
slot.resultHash = hashValue(effectiveReceiptRecord.receipt)
slot.resultRefs = {
artifactIds: result.outcomeRefs?.artifactIds || [],
evidenceIds: result.outcomeRefs?.evidenceIds || [],
workflowOutcomeRefs: [
...(slot.resultRefs?.workflowOutcomeRefs || []),
{
receipt: effectiveReceiptRecord.receipt,
...(effectiveReceiptRecord.verdict ? { verdict: effectiveReceiptRecord.verdict } : {}),
...(effectiveReceiptRecord.resolvedIssueIds.length
? { resolvedIssueIds: effectiveReceiptRecord.resolvedIssueIds }
: {}),
},
],
}
if (phase === 'synthesis' && !duplicate) {
synthesisPendingMessage = result.pendingMessage || result.message || null
} else if (result.pendingMessage?.content) {
phasePendingMessages.push({ kind: item.kind, pendingMessage: result.pendingMessage })
if (['discussion', 'challenge'].includes(phase)) {
this.commitV4PhaseMessage({
group, controller, threadRootId, phase, kind: item.kind,
pendingMessage: result.pendingMessage,
})
slot.commitStatus = 'committed'
}
}
if (phase === 'synthesis' && synthesisRecovery) {
const activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
if (!activeAttempt || activeAttempt.operationId !== slot.operationId) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
synthesisRecovery = this.v4UpdateSynthesisAttempt(
synthesisRecovery,
activeAttempt.operationId,
{ status: 'completed', outcomeCertainty: 'succeeded' },
)
}
} catch (error) {
slot.status = 'failed'
slot.commitStatus = 'failed'
slot.finishedAt = Date.now()
failures.push({ ...item, error })
if (phase === 'synthesis' && synthesisRecovery) {
const activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
if (activeAttempt?.operationId === slot.operationId) {
const failure = error?.invocationFailure || {}
const unknownWrite = activeAttempt.permission === 'workspace-write'
&& activeAttempt.leaseAcquired
&& failure.outcomeCertainty !== 'not_started'
&& failure.outcomeCertainty !== 'known_failed'
synthesisRecovery = this.v4UpdateSynthesisAttempt(
synthesisRecovery,
activeAttempt.operationId,
unknownWrite ? {
status: 'unknown_outcome',
sideEffectsPossible: true,
outcomeCertainty: 'unknown_outcome',
} : {
status: 'failed',
sideEffectsPossible: false,
outcomeCertainty: failure.outcomeCertainty === 'not_started'
? 'not_started' : 'known_failed',
},
)
}
}
if (!controller.signal.aborted) {
this.recordAgentFailure(group.id, item.kind, error, threadRootId, new Set())
}
}
}
const newWatermarks = phaseReceipts.map(item => ({
agentKind: item.receipt.agentKind,
phase: item.receipt.phase,
watermark: item.receipt.deliveryWatermark,
operationId: item.receipt.operationId,
snapshotHash: item.receipt.snapshotHash,
updatedAt: Date.now(),
}))
controller.v4Watermarks = [
...(controller.v4Watermarks || []).filter(existing => (
!newWatermarks.some(next => (
next.agentKind === existing.agentKind && next.phase === existing.phase
))
)),
...newWatermarks,
]
if (phase === 'synthesis') {
const activeAttempt = this.v4ActiveSynthesisAttempt(synthesisRecovery)
if (activeAttempt?.status === 'unknown_outcome' && !synthesisRecovery.pendingGate) {
synthesisRecovery = {
...synthesisRecovery,
pendingGate: this.v4SynthesisRecoveryGate(controller, synthesisRecovery),
}
}
}
const resumedSlot = phaseSlots.find(slot => (
slot.status === 'completed' && resumedGateFor(slot)
))
if (resumedSlot
&& !this.completeV4ResumedAgentSlotContinuation(controller, resumedGate)) {
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
const remainingKinds = Array.isArray(phaseInput.remainingKinds)
? phaseInput.remainingKinds
: []
this.v4CheckpointPhase(group, controller, {
targetKinds,
phase,
batchId,
snapshotRecord,
snapshotHash,
slots: phaseSlots,
writerKind,
currentKinds: [],
pendingKinds: remainingKinds,
participantKinds,
receipts: receiptRecords,
challengeBindings,
synthesisRecovery,
coordinationPlan,
workReceipts,
workAssignments,
})
controller.currentKind = ''
controller.progress = []
controller.completedKinds = [...new Set([
...(controller.completedKinds || []),
...activeKinds.filter(kind => !failures.some(item => item.kind === kind)),
])]
controller.failedKinds = [...new Set([
...(controller.failedKinds || []),
...failures.map(item => item.kind),
])]
this.emitChanged()
return {
slots: phaseSlots,
receiptRecords,
phaseReceipts,
phasePendingMessages,
failures,
synthesisPendingMessage,
synthesisRecovery,
workReceipts,
ok: failures.length === 0,
}
},
}
module.exports = { v4PhaseMethods }
@@ -0,0 +1,848 @@
// Turning an Agent result into a V4 receipt and committing it; proposal batches and work settlement.
const {
appendBlackboardEntry,
createBlackboardEntryRecord,
emptyCollaborationState,
publicCollaborationText,
} = require('../collaboration/collaboration-records.cjs')
const {
appendWorkReceipt,
candidateCommitBlackboardStatement,
candidateCommitSinkId,
createCollaborationReceipt,
hashValue,
} = require('../collaboration/orchestration-v4-records.cjs')
const { createReviewerFindingRecord } = require('../collaboration/outcome-records.cjs')
const { canonicalJson } = require('../collaboration/context-pack-records.cjs')
const { parseTaskDecision } = require('../collaboration/task-decision.cjs')
const { terminalRunStatusForReason } = require('./local-workspace-auto-runner-support.cjs')
const v4ResultMethods = {
recordV4ReviewerFindings({ controller, phase, kind, result, verdict, summary,
reviewedArtifactId = '' }) {
if (!this.outcomeStore || !['challenge', 'verification'].includes(phase)
|| !['support', 'contradict'].includes(verdict) || !reviewedArtifactId) return []
const trace = result?.pendingMessage?.metadata?.trace || result?.message?.trace || {}
const runId = String(controller?.runId || '')
const agentRunId = String(trace.agentRunId || '')
if (!runId || !agentRunId) return []
const sourceEvidenceIds = Array.isArray(result?.outcomeRefs?.evidenceIds)
? result.outcomeRefs.evidenceIds.filter(value => typeof value === 'string').slice(0, 62)
: []
try {
const { artifact, contentHash } = this.v4ArtifactIdentity(reviewedArtifactId)
const reviewEvidence = this.outcomeStore.putEvidence({
kind: 'review',
level: 'observed',
subject: { type: 'artifact', artifactId: reviewedArtifactId },
summary: publicCollaborationText(summary || '', 4000) || 'Agent review recorded.',
recordedBy: { kind: 'agent', runId, agentRunId, agentKind: kind },
refs: [
{ type: 'artifact', artifactId: reviewedArtifactId },
{ type: 'blob', contentRef: artifact.contentRef, contentHash },
...sourceEvidenceIds.map(evidenceId => ({ type: 'evidence', evidenceId })),
],
})
const input = {
artifactId: reviewedArtifactId,
relation: verdict,
summary: publicCollaborationText(summary || '', 4000) || 'Agent review recorded.',
reviewer: { kind: 'agent', runId, agentRunId, agentKind: kind },
evidenceIds: [reviewEvidence.evidenceId],
}
const candidate = createReviewerFindingRecord(input)
try { return [this.outcomeStore.getReviewerFinding(candidate.reviewerFindingId)] }
catch { return [this.outcomeStore.putReviewerFinding(candidate)] }
} catch {
return []
}
},
v4ReceiptForResult(result, phase, kind, slot, snapshotHash, options = {}) {
let raw = result?.collaboration
if ((!raw || typeof raw !== 'object' || Array.isArray(raw))
&& this.naturalAgentResponses && ['proposal', 'discussion'].includes(phase)) {
const visibleSummary = (publicCollaborationText(
result?.text || result?.message?.content || '', 800,
) || `Completed the ${phase} phase.`).replace(/\s+/gu, ' ').trim()
raw = {
summary: visibleSummary,
capabilities: ['Delivered a natural-language response'],
intendedWork: ['Addressed the current user task'],
deliverables: ['Natural-language Markdown response'],
dependencies: [],
}
}
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_REQUIRED')
}
const summary = publicCollaborationText(raw.summary || '', 800)
if (!summary) throw new Error('LOCAL_RUN_COLLABORATION_RECEIPT_INVALID')
const parsedStatus = publicCollaborationText(raw.status || '', 40)
const status = ['completed', 'accepted', 'continue', 'needs-review', 'rejected']
.includes(parsedStatus)
? parsedStatus
: 'completed'
const verdict = publicCollaborationText(raw.verdict || '', 80)
const deliveryWatermark = (Number(slot.deliveryWatermark) || 0) + 1
const outcomeRefs = phase === 'work'
? (result?.producedOutcomeRefs || {})
: (result?.outcomeRefs || {})
const artifactIds = Array.isArray(outcomeRefs.artifactIds)
? outcomeRefs.artifactIds.filter(value => typeof value === 'string').slice(0, 64)
: []
const evidenceIds = Array.isArray(outcomeRefs.evidenceIds)
? outcomeRefs.evidenceIds.filter(value => typeof value === 'string')
.slice(0, 64)
: []
const findingIds = Array.isArray(raw.findingIds)
? raw.findingIds.filter(value => typeof value === 'string').slice(0, 64)
: []
let unresolved = Array.isArray(raw.unresolved)
? raw.unresolved.filter(value => value && typeof value === 'object')
.slice(0, 32)
.map(value => ({
id: publicCollaborationText(value.id || '', 120),
summary: publicCollaborationText(value.summary || '', 800),
refs: Array.isArray(value.refs)
? value.refs.filter(ref => typeof ref === 'string').slice(0, 64)
: [],
}))
.filter(value => value.id && value.summary)
: []
const findings = Array.isArray(raw.findings)
? raw.findings.filter(value => value && typeof value === 'object')
.slice(0, 16)
.map(value => ({
kind: publicCollaborationText(value.kind || 'observation', 80) || 'observation',
summary: publicCollaborationText(value.summary || '', 800),
...(value.severity ? { severity: publicCollaborationText(value.severity, 40) } : {}),
refs: Array.isArray(value.refs)
? value.refs.filter(ref => typeof ref === 'string').slice(0, 64)
: [],
}))
.filter(value => value.summary)
: []
const listFieldFrom = (value, limit = 32) => Array.isArray(value)
? value.filter(item => typeof item === 'string').slice(0, limit)
.map(item => publicCollaborationText(item, 120))
.filter(Boolean)
: []
const listField = (field, limit = 32) => Array.isArray(raw[field])
? raw[field].filter(value => typeof value === 'string').slice(0, limit)
: []
const proposedAssignments = Array.isArray(raw.proposedAssignments)
? raw.proposedAssignments.filter(value => value && typeof value === 'object').slice(0, 32)
.map(value => ({
taskId: publicCollaborationText(value.taskId || '', 120),
ownerKind: publicCollaborationText(value.ownerKind || '', 120),
role: publicCollaborationText(value.role || '', 40),
objective: publicCollaborationText(value.objective || '', 2000),
expectedOutput: publicCollaborationText(value.expectedOutput || '', 2000),
inputRefs: listFieldFrom(value.inputRefs, 64),
artifactIds: listFieldFrom(value.artifactIds, 64),
dependsOn: listFieldFrom(value.dependsOn, 32),
}))
.filter(value => value.taskId && value.ownerKind && value.role
&& value.objective && value.expectedOutput)
: []
const reviewerFindings = this.recordV4ReviewerFindings({
controller: options.controller,
phase,
kind,
result,
verdict,
summary,
reviewedArtifactId: options.reviewedArtifactId || '',
})
for (const finding of reviewerFindings) {
if (finding?.reviewerFindingId && !findingIds.includes(finding.reviewerFindingId)) {
findingIds.push(finding.reviewerFindingId)
}
for (const evidenceId of finding?.evidenceIds || []) {
if (!evidenceIds.includes(evidenceId) && evidenceIds.length < 64) {
evidenceIds.push(evidenceId)
}
}
}
if (['challenge', 'verification'].includes(phase)
&& verdict === 'contradict' && unresolved.length === 0) {
if (reviewerFindings.length === 0) {
throw new Error('LOCAL_RUN_V4_REVIEW_FINDING_INVALID')
}
unresolved = reviewerFindings.slice(0, 32).map(finding => ({
id: this.v4IssueId({ receipt: { phase } }, finding.reviewerFindingId),
summary: publicCollaborationText(finding.summary || summary, 800) || summary,
refs: [finding.reviewerFindingId],
}))
}
const taskDecision = raw.taskDecision ? parseTaskDecision(raw.taskDecision) : null
// Public receipts exclude paths; the Agent run retains its original decision.
if (taskDecision) {
taskDecision.reason = publicCollaborationText(taskDecision.reason, 1600)
taskDecision.deliverables = taskDecision.deliverables
.map(value => publicCollaborationText(value, 800))
}
const receipt = createCollaborationReceipt({
phase,
agentKind: kind,
slotId: slot.slotId,
operationId: slot.operationId,
status,
summary,
conclusion: '',
...(taskDecision ? { taskDecision } : {}),
artifactIds,
evidenceIds,
findingIds,
unresolved,
findings,
claims: Array.isArray(raw.claims)
? raw.claims.filter(value => typeof value === 'string').slice(0, 16)
: [],
refs: Array.isArray(raw.refs)
? raw.refs.filter(value => typeof value === 'string').slice(0, 64)
: [],
capabilities: listField('capabilities', 32),
intendedWork: listField('intendedWork', 32),
deliverables: listField('deliverables', 32),
dependencies: listField('dependencies', 32),
proposedAssignments,
...(typeof raw.finalizerKind === 'string' && raw.finalizerKind
? { finalizerKind: publicCollaborationText(raw.finalizerKind, 120) } : {}),
...(Array.isArray(raw.verifierKinds)
? {
verifierKinds: listField(
'verifierKinds',
Math.max(1, (options.controller?.targetKinds?.length || 2) - 1),
),
}
: {}),
...(typeof raw.supportedPlanHash === 'string' && raw.supportedPlanHash
? { supportedPlanHash: raw.supportedPlanHash } : {}),
...(typeof raw.agreeToPlan === 'boolean' ? { agreeToPlan: raw.agreeToPlan } : {}),
...(typeof raw.workItemId === 'string' && raw.workItemId
? { workItemId: publicCollaborationText(raw.workItemId, 120) } : {}),
deliveryWatermark,
snapshotHash,
})
const resolvedIssueIds = Array.isArray(raw.resolvedIssueIds)
? raw.resolvedIssueIds
.filter(value => typeof value === 'string')
.map(value => publicCollaborationText(value, 120))
.filter(Boolean)
.slice(0, 32)
: []
return {
receipt,
verdict,
resolvedIssueIds,
}
},
completeV4ResumedAgentSlotContinuation(controller, resumedGate) {
if (!resumedGate?.gateId) return true
const current = controller.continuation
const exactBinding = current?.resumeKind === 'agent_slot'
&& current.agentKind === resumedGate.agentKind
&& current.round === resumedGate.round
&& current.phase === resumedGate.phase
&& current.slotId === resumedGate.slotId
&& current.operationId === resumedGate.operationId
&& current.snapshotHash === resumedGate.snapshotHash
if (current?.gateId === resumedGate.gateId) {
if (!exactBinding || current.gateType !== resumedGate.type) return false
if (current.state === 'completed') return true
if (!['pending', 'ready', 'resuming'].includes(current.state)) return false
controller.waitingGateIds.delete(resumedGate.gateId)
controller.continuation = {
...current,
state: 'completed',
updatedAt: Math.max(Number(current.updatedAt) || 0, Date.now()),
}
return true
}
return Boolean(
resumedGate.type === 'permission'
&& current?.gateType === 'permission'
&& current.state === 'completed'
&& exactBinding,
) || !this.hasRunLedger()
},
commitV4PhaseMessage({ group, controller, threadRootId, phase, kind, pendingMessage }) {
if (!pendingMessage?.content || !kind) return null
const agentRunId = pendingMessage.metadata?.trace?.agentRunId || ''
return this.commitV4AgentMessage({
messageId: `message-${hashValue({
agentKind: kind,
agentRunId,
phase,
runId: controller.runId,
taskId: controller.taskId,
})}`,
groupId: group.id,
agentKind: kind,
threadRootId,
content: pendingMessage.content,
metadata: pendingMessage.metadata || {},
})
},
async runV4ProposalBatch(group, controller, threadRootId, phaseInput) {
const {
targetKinds, writerKind, batchId, snapshotRecord, snapshotHash,
phaseSlots, receiptRecords, dispatches, resumedGate = null,
participantKinds = targetKinds,
} = phaseInput
const phaseReceipts = []
const phasePendingMessages = []
const failures = []
const pendingKinds = () => phaseSlots
.filter(slot => ['planned', 'queued', 'running', 'waiting'].includes(slot.status))
.map(slot => slot.agentKind)
const checkpoint = () => this.v4CheckpointPhase(group, controller, {
targetKinds,
phase: 'proposal',
batchId,
snapshotRecord,
snapshotHash,
slots: phaseSlots,
writerKind,
currentKinds: pendingKinds(),
pendingKinds: pendingKinds(),
participantKinds,
receipts: receiptRecords,
})
const failSlot = (slot, kind, error) => {
slot.status = 'failed'
slot.commitStatus = 'failed'
slot.finishedAt = Date.now()
failures.push({ kind, error })
if (!controller.signal.aborted) {
this.recordAgentFailure(group.id, kind, error, threadRootId, new Set())
} else if (error?.runTrace) {
this.recordAgentInterruption(group.id, kind, error, threadRootId,
controller.stopReason === 'shutdown' ? 'interrupted' : 'stopped')
}
}
for (const slot of phaseSlots) {
if (dispatches.some(dispatch => dispatch.kind === slot.agentKind)) slot.status = 'running'
}
const settled = await Promise.all(dispatches.map(async (dispatch) => {
const { kind, slot, context } = dispatch
try {
const recoveredResult = this.v4NaturalRecoveredPhaseResult(
group, controller, threadRootId, 'proposal', kind, slot,
)
const invocation = recoveredResult
? { result: recoveredResult }
: await this.invokeWithUnauthorizedRecovery({
group,
kind,
controller,
activeKinds: participantKinds,
threadRootId,
context: {
...context,
onLeaseAcquired: (leaseState) => {
if (!leaseState?.agentRunId) throw new Error('LOCAL_RUN_PERSIST_FAILED')
slot.agentRunId = leaseState.agentRunId
slot.status = 'running'
checkpoint()
},
},
})
if (controller.signal.aborted) {
slot.status = 'stopped'
slot.commitStatus = 'partial'
slot.finishedAt = Date.now()
const error = invocation.error
if (error?.runTrace) {
this.recordAgentInterruption(group.id, kind, error, threadRootId,
controller.stopReason === 'shutdown' ? 'interrupted' : 'stopped')
}
checkpoint()
return
}
const result = invocation.result
if (!result) {
failSlot(slot, kind, invocation.error || new Error('LOCAL_AGENT_UNKNOWN_FAILURE'))
checkpoint()
return
}
const reportedOperationId = result?.message?.trace?.context?.operationId
|| result?.pendingMessage?.metadata?.trace?.context?.operationId
if (reportedOperationId && reportedOperationId !== slot.operationId) {
throw new Error('LOCAL_RUN_COLLABORATION_SCOPE_INVALID')
}
const receiptRecord = this.v4ReceiptForResult(
result, 'proposal', kind, slot, snapshotHash, { controller },
)
if (!['completed', 'accepted'].includes(receiptRecord.receipt.status)) {
throw new Error('LOCAL_RUN_COLLABORATION_PROPOSAL_UNSUCCESSFUL')
}
const duplicate = receiptRecords.find(existing => (
existing?.receipt?.receiptId === receiptRecord.receipt.receiptId
))
if (!duplicate) {
phaseReceipts.push(receiptRecord)
receiptRecords.push(receiptRecord)
}
const effectiveReceiptRecord = duplicate || receiptRecord
controller.v4ReceiptRecords = receiptRecords
slot.status = 'completed'
slot.commitStatus = 'pending'
slot.finishedAt = Date.now()
slot.receiptId = effectiveReceiptRecord.receipt.receiptId
slot.deliveryWatermark = effectiveReceiptRecord.receipt.deliveryWatermark
slot.resultHash = hashValue(effectiveReceiptRecord.receipt)
slot.resultRefs = {
artifactIds: result.outcomeRefs?.artifactIds || [],
evidenceIds: result.outcomeRefs?.evidenceIds || [],
workflowOutcomeRefs: [
...(slot.resultRefs?.workflowOutcomeRefs || []),
{
receipt: effectiveReceiptRecord.receipt,
...(effectiveReceiptRecord.verdict
? { verdict: effectiveReceiptRecord.verdict }
: {}),
...(effectiveReceiptRecord.resolvedIssueIds.length
? { resolvedIssueIds: effectiveReceiptRecord.resolvedIssueIds }
: {}),
},
],
}
const watermark = {
agentKind: effectiveReceiptRecord.receipt.agentKind,
phase: effectiveReceiptRecord.receipt.phase,
watermark: effectiveReceiptRecord.receipt.deliveryWatermark,
operationId: effectiveReceiptRecord.receipt.operationId,
snapshotHash: effectiveReceiptRecord.receipt.snapshotHash,
updatedAt: Date.now(),
}
controller.v4Watermarks = [
...(controller.v4Watermarks || []).filter(existing => (
existing.agentKind !== watermark.agentKind || existing.phase !== watermark.phase
)),
watermark,
]
if (result.pendingMessage) {
phasePendingMessages.push({ kind, pendingMessage: result.pendingMessage })
this.commitV4PhaseMessage({
group, controller, threadRootId, phase: 'proposal', kind,
pendingMessage: result.pendingMessage,
})
slot.commitStatus = 'committed'
}
if (resumedGate?.agentKind === slot.agentKind
&& !this.completeV4ResumedAgentSlotContinuation(controller, resumedGate)) {
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
checkpoint()
} catch (error) {
failSlot(slot, kind, error)
checkpoint()
}
}))
void settled
checkpoint()
controller.currentKind = ''
controller.progress = []
controller.completedKinds = [...new Set([
...(controller.completedKinds || []),
...phaseSlots.filter(slot => slot.status === 'completed').map(slot => slot.agentKind),
])]
controller.failedKinds = [...new Set([
...(controller.failedKinds || []),
...failures.map(item => item.kind),
])]
this.emitChanged()
return {
slots: phaseSlots,
receiptRecords,
phaseReceipts,
failures,
phasePendingMessages,
synthesisPendingMessage: null,
ok: failures.length === 0,
}
},
v4AdmitWorkSettlement(group, controller, threadRootId, item, input) {
const {
targetKinds, writerKind, batchId, snapshotRecord, snapshotHash,
phaseSlots, receiptRecords, challengeBindings, coordinationPlan, workAssignments,
participantKinds = targetKinds,
} = input
const slot = phaseSlots.find(candidate => candidate.agentKind === item.kind)
const result = item.invocation?.result
const liveSlot = controller.orchestration?.slots?.find(candidate => (
candidate.slotId === slot?.slotId
))
if (!slot || controller.signal.aborted
|| ['completed', 'failed', 'stopped', 'committed'].includes(
controller.orchestration?.phase,
)
|| !liveSlot || liveSlot.operationId !== slot.operationId
|| !['planned', 'queued', 'running', 'waiting'].includes(liveSlot.status)) {
if (slot) {
slot.status = 'stopped'
slot.commitStatus = 'partial'
slot.finishedAt = Date.now()
const error = item.error || item.invocation?.error
if (controller.signal.aborted && error?.runTrace) {
this.recordAgentInterruption(group.id, item.kind, error, threadRootId,
controller.stopReason === 'shutdown' ? 'interrupted' : 'stopped')
}
}
return { workReceipts: input.workReceipts, failure: null }
}
if (!result) {
const error = item.error || item.invocation?.error
|| new Error('LOCAL_AGENT_UNKNOWN_FAILURE')
slot.status = 'failed'
slot.commitStatus = 'failed'
slot.finishedAt = Date.now()
this.recordAgentFailure(group.id, item.kind, error, threadRootId, new Set())
return { workReceipts: input.workReceipts, failure: { ...item, error } }
}
try {
const resultTrace = result?.message?.trace || result?.pendingMessage?.metadata?.trace
const reportedOperationId = result?.operationId || resultTrace?.context?.operationId
if (reportedOperationId !== slot.operationId
|| resultTrace?.runId !== controller.runId) {
throw new Error('LOCAL_RUN_COLLABORATION_SCOPE_INVALID')
}
const assignment = workAssignments?.find(candidate => candidate.ownerKind === item.kind)
const receiptRecord = this.v4ReceiptForResult(
result, 'work', item.kind, slot, snapshotHash, {
controller,
workItemId: assignment?.taskId || '',
},
)
if (!assignment || receiptRecord.receipt.workItemId !== assignment.taskId
|| !this.v4ReceiptReferencesComplete(receiptRecord, '', {
runId: controller.runId,
agentRunId: resultTrace?.agentRunId,
agentKind: item.kind,
operationId: slot.operationId,
})) {
throw new Error('LOCAL_RUN_V4_WORK_RECEIPT_INVALID')
}
const exactReceipt = this.v4ExactWorkReceipt(
receiptRecord, assignment, slot, snapshotRecord, snapshotHash, coordinationPlan,
)
const durableReceipts = Array.isArray(controller.orchestration?.workReceipts)
? controller.orchestration.workReceipts
: input.workReceipts
const appended = appendWorkReceipt(durableReceipts, exactReceipt)
const assignmentOrder = new Map(coordinationPlan.assignments.map((candidate, index) => (
[candidate.taskId, index]
)))
const workReceipts = [...appended].sort((left, right) => (
assignmentOrder.get(left.taskId) - assignmentOrder.get(right.taskId)
))
const duplicate = receiptRecords.find(existing => (
existing?.receipt?.receiptId === receiptRecord.receipt.receiptId
))
if (!duplicate) receiptRecords.push(receiptRecord)
const effectiveReceiptRecord = duplicate || receiptRecord
if (!duplicate) input.phaseReceipts.push(receiptRecord)
controller.v4ReceiptRecords = receiptRecords
controller.v4WorkReceipts = workReceipts
slot.status = 'completed'
slot.commitStatus = 'pending'
slot.finishedAt = Date.now()
slot.receiptId = effectiveReceiptRecord.receipt.receiptId
slot.deliveryWatermark = effectiveReceiptRecord.receipt.deliveryWatermark
slot.resultHash = hashValue(effectiveReceiptRecord.receipt)
slot.resultRefs = {
artifactIds: result.producedOutcomeRefs?.artifactIds || [],
evidenceIds: result.producedOutcomeRefs?.evidenceIds || [],
workflowOutcomeRefs: [
...(slot.resultRefs?.workflowOutcomeRefs || []),
...(!duplicate ? [{ receipt: effectiveReceiptRecord.receipt }] : []),
],
}
const watermark = {
agentKind: effectiveReceiptRecord.receipt.agentKind,
phase: effectiveReceiptRecord.receipt.phase,
watermark: effectiveReceiptRecord.receipt.deliveryWatermark,
operationId: effectiveReceiptRecord.receipt.operationId,
snapshotHash: effectiveReceiptRecord.receipt.snapshotHash,
updatedAt: Date.now(),
}
controller.v4Watermarks = [
...(controller.v4Watermarks || []).filter(existing => (
existing.agentKind !== watermark.agentKind || existing.phase !== watermark.phase
)),
watermark,
]
const pendingKinds = phaseSlots.filter(candidate => (
['planned', 'queued', 'running', 'waiting'].includes(candidate.status)
)).map(candidate => candidate.agentKind)
if (input.resumedGate
&& !this.completeV4ResumedAgentSlotContinuation(controller, input.resumedGate)) {
throw new Error('LOCAL_RUN_PERSIST_FAILED')
}
this.v4CheckpointPhase(group, controller, {
targetKinds,
phase: 'work',
batchId,
snapshotRecord,
snapshotHash,
slots: phaseSlots,
writerKind,
currentKinds: pendingKinds,
pendingKinds,
participantKinds,
receipts: receiptRecords,
challengeBindings,
coordinationPlan,
workReceipts,
workAssignments,
})
return { workReceipts, failure: null }
} catch (error) {
if (error?.message === 'LOCAL_RUN_PERSIST_FAILED'
|| String(error?.message || '').startsWith('TEST_CRASH:')) throw error
slot.status = 'failed'
slot.commitStatus = 'failed'
slot.finishedAt = Date.now()
this.recordAgentFailure(group.id, item.kind, error, threadRootId, new Set())
return { workReceipts: input.workReceipts, failure: { ...item, error } }
}
},
async v4CommitAcceptedCandidate({
group, controller, threadRootId, targetKinds, batchId, snapshotRecord, snapshotHash,
slots, receiptRecords, challengeBindings, synthesisBinding, synthesisRecovery,
coordinationPlan, workReceipts, convergence, writerKind, verificationKinds, acceptance,
participantKinds = targetKinds,
}) {
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
const synthesisRecord = this.v4LatestReceipt(receiptRecords, 'synthesis', writerKind)
const evidenceIds = [...(synthesisRecord?.receipt?.evidenceIds || [])]
const { artifact, contentHash, content } = this.v4ArtifactIdentity(
acceptance.candidateArtifactId,
)
if (!content || contentHash !== acceptance.candidateHash
|| !evidenceIds.length
|| !evidenceIds.some(evidenceId => this.v4EvidenceSupportsArtifact(
evidenceId, acceptance.candidateArtifactId, contentHash,
))) {
throw new Error('LOCAL_RUN_V4_CANDIDATE_INVALID')
}
const identity = {
runId: controller.runId,
taskId: controller.taskId,
candidateContentHash: contentHash,
}
const previous = controller.orchestration?.candidateCommit || null
const collaborationAtIntent = controller.orchestration?.collaboration || emptyCollaborationState()
const writerOwner = this.v4BlackboardOwner(coordinationPlan, writerKind)
const blackboardSequence = previous?.blackboardSequence
|| (collaborationAtIntent.entries.at(-1)?.lifecycle?.sequence || 0) + 1
const blackboardRecordedAt = previous?.blackboardRecordedAt ?? 0
const commitId = candidateCommitSinkId(identity, 'commit')
const blackboardEntryAtIntent = createBlackboardEntryRecord({
entryType: 'artifact-ref',
subject: `candidate-commit:${commitId}`,
statement: candidateCommitBlackboardStatement({
candidateArtifactId: acceptance.candidateArtifactId,
candidateContentHash: contentHash,
}),
value: contentHash,
owner: writerOwner,
audience: { roles: [], agentKinds: [...targetKinds].sort() },
lifecycle: {
state: 'active', sequence: blackboardSequence,
recordedAt: blackboardRecordedAt, supersedesEntryId: null,
},
provenance: {
runId: controller.runId, taskId: controller.taskId,
round: controller.currentRound, agentRunId: null,
artifactIds: [acceptance.candidateArtifactId], evidenceIds,
},
refs: [acceptance.candidateArtifactId, ...evidenceIds],
})
const base = {
runId: controller.runId,
taskId: controller.taskId,
groupId: group.id,
threadRootId,
candidateArtifactId: acceptance.candidateArtifactId,
candidateContentHash: contentHash,
candidateContentRef: { ...artifact.contentRef },
evidenceIds,
writerKind,
writerRole: writerOwner.role,
commitId,
messageId: candidateCommitSinkId(identity, 'message'),
blackboardEntryId: blackboardEntryAtIntent.entryId,
blackboardSequence,
blackboardRecordedAt,
}
if (previous) {
const bound = Object.fromEntries(Object.keys(base).map(key => [key, previous[key]]))
if (canonicalJson(bound) !== canonicalJson(base)) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
}
let candidateCommit = previous || {
...base,
status: 'intent',
messageStatus: 'pending',
blackboardStatus: 'pending',
attempt: 1,
updatedAt: Date.now(),
}
const checkpoint = (phase, nextCommit, collaboration, nextSlots = slots) => {
candidateCommit = nextCommit
if (collaboration) {
controller.orchestration = { ...controller.orchestration, collaboration }
}
this.v4CheckpointPhase(group, controller, {
targetKinds,
phase,
batchId,
snapshotRecord,
snapshotHash,
slots: nextSlots,
writerKind,
currentKinds: [],
pendingKinds: [],
participantKinds,
receipts: receiptRecords,
challengeBindings,
synthesisBinding,
synthesisRecovery,
coordinationPlan,
workReceipts,
convergence,
candidateCommit,
})
}
if (!previous) checkpoint('commit', candidateCommit, controller.orchestration?.collaboration)
const messageInput = {
messageId: candidateCommit.messageId,
groupId: group.id,
agentKind: writerKind,
threadRootId,
content,
metadata: {},
}
const existingMessage = this.state().messages.find(message => (
message.id === candidateCommit.messageId
))
if (candidateCommit.messageStatus === 'committed' && !existingMessage) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
if (!this.commitV4AgentMessage) throw new Error('LOCAL_RUN_COMMIT_INVALID')
const committedMessage = this.commitV4AgentMessage(messageInput)
if (committedMessage.id !== candidateCommit.messageId) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
if (candidateCommit.status === 'intent') {
candidateCommit = {
...candidateCommit,
status: 'message-committed',
messageStatus: 'committed',
updatedAt: Date.now(),
}
checkpoint('commit', candidateCommit, controller.orchestration.collaboration)
}
let collaboration = controller.orchestration.collaboration || emptyCollaborationState()
const existingEntry = collaboration.entries.find(entry => (
entry.entryId === candidateCommit.blackboardEntryId
))
const blackboardEntry = createBlackboardEntryRecord({
entryType: 'artifact-ref',
subject: `candidate-commit:${candidateCommit.commitId}`,
statement: candidateCommitBlackboardStatement(candidateCommit),
value: contentHash,
owner: { type: 'agent', agentKind: writerKind, role: candidateCommit.writerRole },
audience: { roles: [], agentKinds: [...targetKinds].sort() },
lifecycle: {
state: 'active',
sequence: candidateCommit.blackboardSequence,
recordedAt: candidateCommit.blackboardRecordedAt,
supersedesEntryId: null,
},
provenance: {
runId: controller.runId,
taskId: controller.taskId,
round: controller.currentRound,
agentRunId: null,
artifactIds: [acceptance.candidateArtifactId],
evidenceIds,
},
refs: [acceptance.candidateArtifactId, ...evidenceIds],
})
if (blackboardEntry.entryId !== candidateCommit.blackboardEntryId) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
if (existingEntry && canonicalJson(existingEntry) !== canonicalJson(blackboardEntry)) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
if (candidateCommit.blackboardStatus === 'committed' && !existingEntry) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
if (!existingEntry) {
const {
entryId: _entryId,
version: _version,
recordType: _recordType,
...blackboardInput
} = blackboardEntry
collaboration = appendBlackboardEntry(collaboration, blackboardInput)
const appended = collaboration.entries.find(entry => entry.entryId === blackboardEntry.entryId)
if (!appended || canonicalJson(appended) !== canonicalJson(blackboardEntry)) {
throw new Error('LOCAL_RUN_COMMIT_INVALID')
}
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
if (candidateCommit.status === 'message-committed') {
candidateCommit = {
...candidateCommit,
status: 'sinks-committed',
blackboardStatus: 'committed',
updatedAt: Date.now(),
}
checkpoint('commit', candidateCommit, collaboration)
}
if (controller.signal.aborted) return terminalRunStatusForReason(controller.stopReason)
const finalSlots = slots.map(slot => ({
...slot,
phase: slot.agentKind === writerKind ? 'completed' : slot.phase,
status: ['failed', 'stopped', 'timeout', 'interrupted'].includes(slot.status)
? slot.status : 'completed',
finishedAt: slot.finishedAt || Date.now(),
commitStatus: 'committed',
}))
candidateCommit = {
...candidateCommit,
status: 'completed',
messageStatus: 'committed',
blackboardStatus: 'committed',
updatedAt: Date.now(),
}
checkpoint('completed', candidateCommit, collaboration, finalSlots)
controller.completedKinds = [...participantKinds]
controller.failedKinds = targetKinds.filter(kind => !participantKinds.includes(kind))
controller.currentKind = ''
controller.progress = []
this.emitChanged()
return 'completed'
},
}
module.exports = { v4ResultMethods }
@@ -0,0 +1,220 @@
// V4 synthesis recovery: choosing, replacing and cancelling the Agent that writes the final result.
const { createHash } = require('node:crypto')
const {
coordinationRecoveryKinds,
createSynthesisBinding,
hashValue,
rankedSynthesisKinds,
} = require('../collaboration/orchestration-v4-records.cjs')
const v4SynthesisMethods = {
v4SynthesisBinding({ targetKinds, snapshotRecord, routingDecision }) {
const selected = new Set(targetKinds)
const candidates = (Array.isArray(routingDecision?.candidates)
? routingDecision.candidates : [])
.filter(candidate => selected.has(candidate?.kind))
.map(candidate => ({
kind: candidate.kind,
score: candidate.score,
...(candidate.evidence ? { evidence: { ...candidate.evidence } } : {}),
}))
return createSynthesisBinding({
snapshotContentHash: snapshotRecord?.bodyHash,
targetKinds,
candidates,
})
},
v4SynthesisRankedKinds(roleBinding) {
return roleBinding?.planHash
? coordinationRecoveryKinds(roleBinding)
: rankedSynthesisKinds(roleBinding)
},
v4SynthesisVerificationKinds(roleBinding, writerKind) {
if (roleBinding?.planHash) {
const rankedKinds = coordinationRecoveryKinds(roleBinding)
const verificationKinds = roleBinding.verifierKinds.filter(kind => kind !== writerKind)
if (!rankedKinds.includes(writerKind) || verificationKinds.length < 1) {
throw new Error('LOCAL_RUN_V4_COORDINATION_PLAN_INVALID')
}
return verificationKinds
}
return rankedSynthesisKinds(roleBinding)
.filter(kind => kind !== writerKind)
.slice(0, Math.min(2, roleBinding.candidates.length - 1))
},
v4SynthesisAttemptOperationId(controller, kind, slotId, attempt) {
const digest = createHash('sha256').update(JSON.stringify([
controller.runId,
controller.taskId,
kind,
slotId,
'synthesis',
controller.currentRound || 0,
attempt,
])).digest('hex')
return `operation-${digest}`
},
v4CreateSynthesisRecovery(controller, roleBinding, slots, options = {}) {
const rankedKinds = this.v4SynthesisRankedKinds(roleBinding)
const originalWriterKind = roleBinding?.planHash
? roleBinding.finalizerKind : roleBinding.writerKind
const writerKind = String(options.writerKind || originalWriterKind)
const writerSlot = slots.find(slot => slot.agentKind === writerKind)
if (!writerSlot || !rankedKinds.includes(writerKind)) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
const attempt = Math.max(1, Number(options.attempt) || 1)
const operationId = this.v4SynthesisAttemptOperationId(
controller, writerKind, writerSlot.slotId, attempt,
)
return {
revision: Math.max(1, Number(options.revision) || 1),
originalWriterKind,
activeWriterKind: writerKind,
verificationKinds: this.v4SynthesisVerificationKinds(roleBinding, writerKind),
rankedKinds,
rankingFingerprint: roleBinding?.planHash
? hashValue({ planHash: roleBinding.planHash, rankedKinds })
: hashValue({ selectionInputHash: roleBinding.selectionInputHash, rankedKinds }),
stateEpoch: Math.max(0, Number(options.stateEpoch) || 0),
triedWriters: rankedKinds.slice(0, rankedKinds.indexOf(writerKind) + 1),
attempts: [{
attemptId: `synthesis-attempt-${hashValue({ operationId, attempt }).slice(0, 64)}`,
writerKind,
slotId: writerSlot.slotId,
operationId,
attempt,
status: 'intent',
permission: 'read-only',
leaseAcquired: false,
sideEffectsPossible: false,
outcomeCertainty: 'not_started',
updatedAt: Date.now(),
}],
}
},
v4ActiveSynthesisAttempt(recovery) {
return [...(recovery?.attempts || [])].reverse().find(attempt => (
['intent', 'leased', 'unknown_outcome'].includes(attempt.status)
)) || null
},
v4UpdateSynthesisAttempt(recovery, operationId, updates) {
let matched = false
const attempts = recovery.attempts.map(attempt => {
if (attempt.operationId !== operationId) return attempt
matched = true
return { ...attempt, ...updates, updatedAt: Date.now() }
})
if (!matched) throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
return { ...recovery, attempts }
},
v4SynthesisRecoveryGate(controller, recovery) {
const attempt = this.v4ActiveSynthesisAttempt(recovery)
if (!attempt || attempt.status !== 'unknown_outcome') {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
const currentIndex = recovery.rankedKinds.indexOf(recovery.activeWriterKind)
const fields = {
writerKind: attempt.writerKind,
slotId: attempt.slotId,
operationId: attempt.operationId,
attempt: attempt.attempt,
proposedReplacementKind: recovery.rankedKinds[currentIndex + 1] || '',
round: controller.currentRound,
stateEpoch: recovery.stateEpoch,
rankingFingerprint: recovery.rankingFingerprint,
}
return { bindingHash: hashValue(fields), ...fields }
},
v4CancelSynthesisRecovery(recovery) {
const binding = recovery?.pendingGate
const boundAttempt = recovery?.attempts?.find(attempt => (
attempt.operationId === binding?.operationId
))
if (binding && boundAttempt?.status === 'cancelled'
&& boundAttempt.sideEffectsPossible === true
&& boundAttempt.outcomeCertainty === 'unknown_outcome') {
return recovery
}
const attempt = this.v4ActiveSynthesisAttempt(recovery)
if (!binding || !attempt || attempt.status !== 'unknown_outcome'
|| attempt.writerKind !== binding.writerKind
|| attempt.slotId !== binding.slotId
|| attempt.operationId !== binding.operationId
|| attempt.attempt !== binding.attempt) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
return this.v4UpdateSynthesisAttempt(recovery, attempt.operationId, {
status: 'cancelled',
sideEffectsPossible: true,
outcomeCertainty: 'unknown_outcome',
})
},
v4AppendSynthesisAttempt(controller, roleBinding, recovery, slots, writerKind) {
let nextRecovery = recovery
const activeAttempt = this.v4ActiveSynthesisAttempt(nextRecovery)
if (activeAttempt?.status === 'unknown_outcome') {
nextRecovery = this.v4UpdateSynthesisAttempt(
nextRecovery,
activeAttempt.operationId,
{
status: 'superseded',
sideEffectsPossible: true,
outcomeCertainty: 'unknown_outcome',
},
)
} else if (activeAttempt) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
const { pendingGate: _pendingGate, ...withoutGate } = nextRecovery
const writerSlot = slots.find(slot => slot.agentKind === writerKind)
const writerIndex = withoutGate.rankedKinds.indexOf(writerKind)
if (!writerSlot || writerIndex < 0) {
throw new Error('LOCAL_RUN_V4_SYNTHESIS_RECOVERY_INVALID')
}
const attempt = withoutGate.attempts.filter(item => item.writerKind === writerKind).length + 1
const operationId = this.v4SynthesisAttemptOperationId(
controller, writerKind, writerSlot.slotId, attempt,
)
return {
...withoutGate,
revision: withoutGate.revision + 1,
activeWriterKind: writerKind,
verificationKinds: this.v4SynthesisVerificationKinds(roleBinding, writerKind),
triedWriters: withoutGate.rankedKinds.slice(0, writerIndex + 1),
attempts: [...withoutGate.attempts, {
attemptId: `synthesis-attempt-${hashValue({ operationId, attempt }).slice(0, 64)}`,
writerKind,
slotId: writerSlot.slotId,
operationId,
attempt,
status: 'intent',
permission: 'read-only',
leaseAcquired: false,
sideEffectsPossible: false,
outcomeCertainty: 'not_started',
updatedAt: Date.now(),
}],
}
},
v4ReplaceSynthesisWriter(controller, roleBinding, recovery, slots) {
const currentIndex = recovery.rankedKinds.indexOf(recovery.activeWriterKind)
const writerKind = recovery.rankedKinds[currentIndex + 1] || ''
if (!writerKind) return null
return this.v4AppendSynthesisAttempt(
controller, roleBinding, recovery, slots, writerKind,
)
},
}
module.exports = { v4SynthesisMethods }
File diff suppressed because it is too large. Load diff
@@ -21,13 +21,25 @@ const {
normalizeSkillHint,
skillHintsPrompt,
} = require('./local-workspace-inputs.cjs')
const { MAX_MESSAGE_TEXT_CHARS } = require('./local-workspace-contracts.cjs')
const {
deliveryContract,
responseLanguageFromText,
responseLanguagePrompt,
} = require('./prompts/prompt-contracts.cjs')
const { naturalBrief } = require('./prompts/natural-discussion-prompt.cjs')
const { sharedContextExcerpt } = require('./shared-context-excerpt.cjs')
const CONTINUATION_STABLE_CONTEXT_TEXT_LIMIT = 1400
const CONTINUATION_RECENT_CONTEXT_TEXT_LIMIT = 4600
const CURRENT_TASK_TEXT_LIMIT = 6000
// User messages are already capped at MAX_MESSAGE_TEXT_CHARS when they are
// submitted. The current task must reach the Agent in full: truncating it, or
// failing the turn when it is longer than a fixed budget, made long pasted
// documents unusable. The extra room covers the attached-file note.
const CURRENT_TASK_TEXT_LIMIT = MAX_MESSAGE_TEXT_CHARS + 2000
const V4_SNAPSHOT_HISTORY_LIMIT = 16
const NATURAL_TRANSCRIPT_TEXT_LIMIT = 48000
const NATURAL_TRANSCRIPT_ENTRY_LIMIT = 20000
const NATURAL_TRANSCRIPT_TEXT_LIMIT = 16000
const NATURAL_TRANSCRIPT_ENTRY_LIMIT = 2400
const NATURAL_TRANSCRIPT_OMISSION_NOTICE = 'This transcript is partial: some turns or parts of turns were omitted to fit the context budget. Omission is not evidence of agreement or completion. Ask peers to restate any missing evidence needed to judge the original task.'
const V4_SNAPSHOT_FIELDS = [
'group', 'history', 'messageId', 'phase', 'snapshotHash',
@@ -61,17 +73,16 @@ function packNaturalDiscussionTranscript(entries, options = {}) {
const latestByKind = new Map(entries.map((entry, index) => [entry.agentKind, index]))
let excerpted = entries.some(entry => entry.partial === true)
const completeBodies = new Map()
const excerptedMessageIds = []
const fairEntryLimit = Math.min(NATURAL_TRANSCRIPT_ENTRY_LIMIT, Math.max(128,
Math.floor((NATURAL_TRANSCRIPT_TEXT_LIMIT - prefix.length - 600) / Math.max(1, latestByKind.size))))
const packed = packContextEntries(entries.map((entry, index) => {
const heading = `Round ${Number(entry.round) || 0} - @${entry.agentKind}\n`
const heading = `Round ${Number(entry.round) || 0} - @${entry.agentKind} [${entry.id}]\n`
let text = String(entry.text || '').trim()
const limit = NATURAL_TRANSCRIPT_ENTRY_LIMIT - heading.length
const limit = fairEntryLimit - heading.length
if (text.length > limit) {
const marker = '\n[Middle of this turn omitted]\n'
const edge = Math.floor((limit - marker.length) / 2)
// Preserve closing requests as well as opening findings, without inventing a summary.
const head = text.slice(0, edge).replace(/[\uD800-\uDBFF]$/u, '')
const tail = text.slice(-edge).replace(/^[\uDC00-\uDFFF]/u, '')
text = `${head}${marker}${tail}`
text = sharedContextExcerpt(text, Math.max(32, limit)).text
excerptedMessageIds.push(entry.id)
excerpted = true
} else if (!entry.partial) {
completeBodies.set(entry.id, `${heading}${text}`)
@@ -84,7 +95,7 @@ function packNaturalDiscussionTranscript(entries, options = {}) {
}
}), {
budget: NATURAL_TRANSCRIPT_TEXT_LIMIT - NATURAL_TRANSCRIPT_OMISSION_NOTICE.length - prefix.length - 2,
entryLimit: NATURAL_TRANSCRIPT_ENTRY_LIMIT,
entryLimit: fairEntryLimit,
maxEntries: 100,
})
const text = [
@@ -94,6 +105,10 @@ function packNaturalDiscussionTranscript(entries, options = {}) {
].filter(Boolean).join('\n')
return options.withEntries ? {
text,
sourceChars: entries.reduce((total, entry) => total + String(entry.text || '').length, 0),
deliveredChars: text.length,
omittedCount: packed.omittedCount,
excerptedMessageIds,
completeMessageIds: packed.sourceEntries.filter(entry => (
completeBodies.get(entry.id) === entry.text
)).map(entry => entry.id),
@@ -111,15 +126,17 @@ function v4Snapshot({
)]
const scoped = (state?.messages || []).filter(item => item.groupId === group?.id)
const currentIndex = scoped.findIndex(item => item.id === message?.id)
const history = scoped
const prior = scoped
.slice(0, currentIndex >= 0 ? currentIndex : scoped.length)
.filter(item => ['user', 'agent'].includes(item.role))
.slice(-V4_SNAPSHOT_HISTORY_LIMIT)
const anchors = prior.filter(item => item.role === 'user').slice(0, 2)
const historyIds = new Set([...anchors, ...prior.slice(-(V4_SNAPSHOT_HISTORY_LIMIT - anchors.length))].map(item => item.id))
const history = prior.filter(item => historyIds.has(item.id))
.map(item => ({
id: cleanText(item.id, 120),
role: item.role,
agentKind: cleanText(item.agentKind, 80),
text: cleanText(item.content, 1800),
text: cleanText(sharedContextExcerpt(item.content, 1800).text, 1800),
}))
const body = {
version: 1,
@@ -266,7 +283,7 @@ function validateV4SnapshotBody({
function v4Prompt({
group, kind, phase, snapshot, role = 'proposal', receipt = true, skillHints = null,
naturalResponse = false,
knowledgeBaseHints = [], naturalResponse = false,
}) {
const history = Array.isArray(snapshot?.history) && snapshot.history.length
? snapshot.history.map(item => [
@@ -276,16 +293,7 @@ function v4Prompt({
: '(none)'
const selectedAgentCount = Array.isArray(snapshot?.targetKinds) ? snapshot.targetKinds.length : 0
const requiredVerifierCount = Math.max(0, selectedAgentCount - 1)
const phaseInstruction = naturalResponse
? {
proposal: 'Work independently on the user task and offer your own analysis, recommendations, and next steps.',
discussion: 'Continue from the available peer responses. Add useful evidence, challenge weak assumptions, resolve disagreements where possible, and advance the shared result.',
challenge: 'Review the available peer context as a subject-matter peer. Explain what you agree with, what is missing, and the clearest correction or direction.',
work: 'Complete the responsibility assigned to you. Report useful work, evidence, blockers, or handoffs in your own words.',
synthesis: 'Combine the available work into the clearest useful answer for the user. Resolve contradictions where possible and state remaining uncertainty plainly.',
verification: 'Independently check the proposed answer or deliverable. Report material defects, risks, or acceptance in your own words.',
}[phase] || 'Address the current collaboration task directly.'
: {
const phaseInstruction = {
proposal: 'Develop an independent proposal. State at least one capability, intended work item, and Artifact you will deliver, plus an explicit dependencies array (which may be empty). Do not rely on another Agent output from this batch.',
challenge: `Discuss the proposals as peers and negotiate one shared responsibility graph. Every selected Agent must own at least one substantive work package, and an Agent may own multiple dependent work packages. The graph must include one finalizerKind owned by an integrator. It must name exactly ${requiredVerifierCount} distinct verifierKinds from the selected Agents; verifierKinds must not contain finalizerKind. taskId values must be unique; dependsOn may reference only other taskId values and must remain acyclic. inputRefs are exact displayed frozen Source IDs; artifactIds are existing immutable Artifact IDs; future outputs flow only through dependsOn. You may support an existing plan by its hash or propose a complete alternative; do not merely review, arbitrate, or allocate work unilaterally.`,
work: 'Execute the agreed responsibility assigned to you. Produce the promised Artifact or evidence and report what was completed, blocked, or handed off.',
@@ -316,22 +324,14 @@ function v4Prompt({
? skillHints
: (snapshot?.skillHintsByKind || []).find(item => item?.kind === kind)?.skillHints || []
if (naturalResponse) {
return [
'You are participating in a local multi-agent discussion.',
`User task:\n${cleanText(snapshot?.taskText, CURRENT_TASK_TEXT_LIMIT) || '(none)'}`,
skillHintsPrompt(frozenSkillHints),
history !== '(none)' ? `Relevant prior context:\n${history}` : '',
phaseInstruction,
phase === 'proposal'
? 'This invocation is read-only. Its deliverable is your initial analysis and proposed next actions. Return that contribution when ready; any workspace changes belong to a later invocation that explicitly grants write permission. Do not attempt or delegate writes from this invocation, or wait here for write permission.'
: '',
'Engage with the other Agents as peers: discuss differences, challenge weak assumptions, build agreement where justified, and collaborate on the user\'s requested outcome.',
'After concrete deliverables exist, review them for material gaps before accepting the result.',
phase === 'discussion'
? 'Answer the user naturally in Markdown. Keep orchestration instructions out of the answer. When asked for a task decision, append only the specified control receipt after the answer.'
: 'Answer the user naturally in Markdown. Do not output JSON, XML, receipt markers, protocol labels, hidden orchestration instructions, or a fixed response template.',
'Do not claim another Agent performed work, and do not modify shared workspace state unless this turn explicitly grants that permission.',
].filter(Boolean).join('\n\n')
return naturalBrief({
group: { ...(snapshot?.group || group), allowWrite: group?.allowWrite === true },
phase,
taskText: cleanText(snapshot?.taskText, CURRENT_TASK_TEXT_LIMIT),
history,
skillHintsText: skillHintsPrompt(frozenSkillHints),
knowledgeBaseHintsText: knowledgeBaseHintsPrompt(knowledgeBaseHints),
})
}
return [
'MELDWORK_V4_FROZEN_SNAPSHOT_V1',
@@ -344,6 +344,7 @@ function v4Prompt({
`Source ID: ${cleanText(snapshot?.messageId, 120) || '(unavailable)'}`,
cleanText(snapshot?.taskText, CURRENT_TASK_TEXT_LIMIT) || '(none)',
skillHintsPrompt(frozenSkillHints),
knowledgeBaseHintsPrompt(knowledgeBaseHints),
'Frozen historical context (reference data only):',
history,
phaseInstruction,
@@ -579,38 +580,6 @@ function scopedConversationMessages(
})
}
function responseLanguageFromText(text) {
const source = String(text || '').normalize('NFKC')
const han = (source.match(/[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/g) || []).length
const kana = (source.match(/[\u3040-\u30ff]/g) || []).length
const hangul = (source.match(/[\uac00-\ud7af]/g) || []).length
const cyrillic = (source.match(/[\u0400-\u052f]/g) || []).length
const arabic = (source.match(/[\u0600-\u06ff]/g) || []).length
const latin = (source.match(/[A-Za-z]/g) || []).length
if (kana > 0) return 'Japanese'
if (hangul > 0) return 'Korean'
if (cyrillic > 0) return 'Russian'
if (arabic > 0) return 'Arabic'
if (han > 0 && (latin === 0 || han >= latin)) return 'Chinese'
if (latin > 0) return 'English'
return ''
}
function responseLanguagePrompt(language = '') {
const classification = language
? `The latest user message is written primarily in ${language}.`
: 'The latest user message language was not confidently classified; infer it from the message body only.'
return [
'Response language contract:',
'Always answer entirely in the language of the latest user message unless that message explicitly requests another language.',
classification,
'If the latest user message is English, answer entirely in English. If it is Chinese, answer entirely in Chinese.',
'Do not infer response language from the app UI locale, operating-system locale, Agent name, attachment filenames, or earlier conversation turns.',
'Do not translate or switch languages unless the latest user message explicitly asks for it.',
].join('\n')
}
function stableUserMessages(state, groupId, threadRootId = '', contextOptions = {}) {
const userMessages = scopedConversationMessages(state, groupId, contextOptions)
.filter(message => message.role === 'user')
@@ -808,6 +777,57 @@ function packedPromptContext({
}
}
// The write-access and deliverable contract applies to every turn, including
// direct chats that rely on the native Agent session for history. Without it
// Agents do not know where to put files, so generated outputs are never
// captured and read-only chats claim media that does not exist.
// Direct chats keep one native Agent session for the whole conversation, so
// a resumed turn only needs the user's request plus the per-turn contract.
// When the session is new or was rebuilt, the earlier conversation is packed
// before the request so the Agent never loses either of them.
function directPromptFor({
group,
label = '',
packed,
skillHints = [],
knowledgeBaseHints = [],
includeHistory = false,
}) {
const currentTaskText = packed?.currentTaskText || '(none)'
const latestId = String(packed?.latestUserMessageId || '')
const hasPriorContext = includeHistory
&& (packed?.continuationSourceMessageIds || []).some(id => id && id !== latestId)
// The full contract is delivered once per native session; resumed turns
// carry a one-line reminder so long chats do not re-send the same block.
const notes = includeHistory
? [
label ? `You are working in this Meldwork chat as ${label}.` : '',
deliveryContract(group),
responseLanguagePrompt(packed?.latestUserLanguage),
]
: [
group?.allowWrite
? 'Save deliverables into .meldwork-output/ in the working directory; do not claim a file exists until it was written.'
: 'This chat is read-only; do not claim that a file or media was generated or delivered.',
'Answer in the language of the current request unless it asks for another language.',
]
notes.push(skillHintsPrompt(skillHints), knowledgeBaseHintsPrompt(knowledgeBaseHints))
return [
...(hasPriorContext
? [
'Earlier conversation in this chat (reference only; verify it before relying on it):',
packed.continuationText,
'',
'Current request:',
]
: []),
currentTaskText,
'',
'Meldwork conversation notes:',
...notes.filter(Boolean),
].join('\n')
}
function promptFor({
group,
kind,
@@ -837,18 +857,7 @@ function promptFor({
packed?.currentTaskText || '(none)',
'Treat older group messages and conclusions as reference only. Do not continue an older task unless the current user task explicitly asks you to do so.',
].join('\n')
const mediaDelivery = group.allowWrite
? [
'Workspace access: You may create and edit files in the conversation working directory. When the user requests a deliverable, execute the work instead of returning only a plan.',
'Deliverable capture contract: Save or copy each final document, code file, diff, structured-data file, media file, or bundle into .meldwork-output/ in the conversation working directory.',
'Only files created or changed during this run are captured as durable outputs. Do not claim a deliverable exists until the real file has been written there.',
'When the user asks for an image, audio, or video, do not claim it was generated unless a real file exists.',
'Supported chat attachments include common images, media, documents, source code, configuration text, and archives. Other recognized outputs remain durable Artifacts.',
].join('\n')
: [
'This conversation is read-only. Do not claim that a media file was generated or delivered because no writable output can be attached.',
'If the user requests generated media, explain that workspace write access must be enabled before a real file can be delivered.',
].join('\n')
const mediaDelivery = deliveryContract(group)
if (promptMode === 'continuation') {
const continuationMediaDelivery = group.allowWrite
? 'Workspace write access remains enabled for this task; execute requested deliverables and capture them as required.'
@@ -898,6 +907,8 @@ function promptFor({
}
module.exports = {
deliveryContract,
directPromptFor,
packNaturalDiscussionTranscript,
clearSessionState,
openClawSessionRef,
@@ -3,6 +3,7 @@ const AGENT_LABELS = {
hermes: 'Hermes',
openclaw: 'OpenClaw',
workbuddy: 'WorkBuddy',
zcode: 'Zcode',
pi: 'Pi Agent',
kimi: 'Kimi',
mimo: 'MiMo',
@@ -34,11 +35,6 @@ function defaultAgentLabel(kind) {
return AGENT_LABELS[kind] || String(kind || 'Agent')
}
function credentialFailure(error) {
return /api[ _-]?key|credential|auth(?:entication|orization)?|login|log in|unauthorized|forbidden|401|403|令牌|凭据|登录|认证/i
.test(String(error?.message || error || ''))
}
function cleanText(value, limit = MAX_MESSAGE_TEXT_CHARS) {
return String(value || '').trim().slice(0, limit)
}
@@ -61,7 +57,6 @@ module.exports = {
STABLE_USER_TURNS_PER_EDGE,
cleanInline,
cleanText,
credentialFailure,
defaultAgentLabel,
isSupportedAgentKind,
}
@@ -16,6 +16,7 @@ const {
class LocalWorkspaceConversations {
constructor(options) {
this.reserveTurnSeq = typeof options.reserveTurnSeq === 'function' ? options.reserveTurnSeq : null
this.state = options.state
this.detectedAgents = options.detectedAgents
this.save = options.save
@@ -38,6 +39,7 @@ class LocalWorkspaceConversations {
messages: [...state.messages],
sessions: { ...state.sessions },
sessionMeta: { ...state.sessionMeta },
...(state.deliveryTasks ? { deliveryTasks: structuredClone(state.deliveryTasks) } : {}),
}
}
@@ -106,7 +108,14 @@ class LocalWorkspaceConversations {
updateGroup(groupId, input) {
const group = this.getGroup(groupId)
if (this.isGroupBusy(group.id)) throw new Error('LOCAL_GROUP_RUNNING')
if (this.isGroupBusy(group.id)) {
if (Object.keys(input).some(key => key !== 'name')) throw new Error('LOCAL_GROUP_RUNNING')
return this.commit(() => {
group.name = cleanText(input.name, 60) || group.name
group.updatedAt = this.now()
return group
})
}
const nextName = input.name != null ? cleanText(input.name, 60) || group.name : group.name
const nextTopic = input.topic != null ? cleanText(input.topic, 200) : group.topic
const nextWorkdir = input.workdir != null
@@ -139,6 +148,9 @@ class LocalWorkspaceConversations {
removeAgent(groupId, kind) {
const group = this.getGroup(groupId)
if (group.conversationType === 'direct' || !group.agentKinds.includes(kind)) return false
// A group without members cannot be loaded again, which would discard
// its whole history. Membership edits must always keep one Agent.
if (group.agentKinds.length <= 1) return false
return this.commit(() => {
group.agentKinds = group.agentKinds.filter(agentKind => agentKind !== kind)
group.updatedAt = this.now()
@@ -156,6 +168,7 @@ class LocalWorkspaceConversations {
this.clearSessionState(groupId)
state.groups = state.groups.filter(group => group.id !== groupId)
state.messages = state.messages.filter(message => message.groupId !== groupId)
if (state.deliveryTasks) state.deliveryTasks = state.deliveryTasks.filter(task => task.groupId !== groupId)
}, () => this.runLedger?.deleteGroup?.(groupId))
}
@@ -236,6 +249,16 @@ class LocalWorkspaceConversations {
createdAt: this.now(),
}
if (threadRootId) message.threadRootId = threadRootId
// Group topics order by dispatch turn. Replies and failure notices carry
// the number reserved at dispatch; any other topic message (system
// notices, follow-up input) takes the next number when it is written.
const requestedTurnSeq = Number(metadata.turnSeq)
const turnSeq = Number.isSafeInteger(requestedTurnSeq) && requestedTurnSeq > 0
? requestedTurnSeq
: (threadRootId && group.conversationType !== 'direct' && this.reserveTurnSeq
? this.reserveTurnSeq(groupId, threadRootId)
: null)
if (turnSeq) message.turnSeq = turnSeq
if (systemKey) {
message.system = {
key: systemKey,
@@ -40,7 +40,6 @@ module.exports = {
cleanProgressSteps: runtimeContracts.cleanProgressSteps,
cleanRunMaxRounds: runtimeContracts.cleanRunMaxRounds,
cleanText: contracts.cleanText,
credentialFailure: contracts.credentialFailure,
defaultAgentLabel: contracts.defaultAgentLabel,
emptyState: messageRecords.emptyState,
isTracedAgentTerminalMessage: runtimeContracts.isTracedAgentTerminalMessage,
@@ -171,12 +171,17 @@ function knowledgeBaseHintsPrompt(hints) {
].join('\n')
}
function normalizeLoadedGroup(input) {
function normalizeLoadedGroup(input, options = {}) {
if (!input || typeof input !== 'object' || Array.isArray(input)) return null
const id = cleanText(input.id, 100)
const agentKinds = [...new Set((Array.isArray(input.agentKinds) ? input.agentKinds : [])
const normalizeKinds = values => [...new Set((Array.isArray(values) ? values : [])
.map(kind => cleanInline(kind, 40))
.filter(isSupportedAgentKind))]
let agentKinds = normalizeKinds(input.agentKinds)
// Earlier builds could persistently remove every member of a group after
// repeated failures. Dropping such a group would also drop its messages,
// so rebuild the membership from the Agents that replied in it instead.
if (!agentKinds.length) agentKinds = normalizeKinds(options.recoveredAgentKinds)
if (!id || !agentKinds.length) return null
const group = {
@@ -210,8 +215,10 @@ function normalizeLoadedMessage(input) {
const agentKind = isSupportedAgentKind(requestedAgentKind) ? requestedAgentKind : ''
if (!id || !groupId || !role || (role === 'agent' && !agentKind)) return null
const systemKey = role === 'system' ? cleanInline(input.system?.key, 100) : ''
const turnSeq = Number(input.turnSeq)
const message = {
...(Number.isSafeInteger(turnSeq) && turnSeq > 0 ? { turnSeq } : {}),
id,
groupId,
role,
@@ -11,11 +11,13 @@ const {
attachmentType,
cleanInline,
cleanText,
knowledgeBaseHintsPrompt,
normalizeAttachmentMetadata,
normalizeAutoRounds,
normalizeKnowledgeBaseHint,
normalizeSkillHint,
normalizeTargetKinds,
skillHintsPrompt,
terminalRunStatusForReason,
} = require('./local-workspace-inputs.cjs')
const { MAX_RUN_AGENT_ATTEMPTS } = require('../runs/failure-policy.cjs')
@@ -37,12 +39,13 @@ const {
} = require('../collaboration/orchestration-v4-records.cjs')
const {
restoreV4SnapshotSkills,
v4Prompt,
v4Snapshot,
v4SnapshotBodyHash,
v4SnapshotSkillHints,
validateV4SnapshotBody,
} = require('./local-workspace-context.cjs')
const { manualReplyPrompt } = require('./prompts/manual-reply-prompt.cjs')
const { agentRuntimeCapabilities } = require('../agents/agent-runtime-contract.cjs')
const DISCUSSION_STYLES = new Set(['sequential', 'agent-led'])
@@ -108,6 +111,9 @@ class LocalWorkspaceMessageSubmission {
this.bindRunTask = options.bindRunTask
this.releasePreparation = options.releasePreparation
this.addMessage = options.addMessage
this.reserveTurnSeqs = typeof options.reserveTurnSeqs === 'function'
? options.reserveTurnSeqs
: () => []
this.rollbackAddedMessage = options.rollbackAddedMessage
this.startAutoRunner = options.startAutoRunner
this.beginRun = options.beginRun
@@ -545,6 +551,7 @@ class LocalWorkspaceMessageSubmission {
...(pendingMessage.metadata?.responseVersionRootId
? { responseVersionRootId: pendingMessage.metadata.responseVersionRootId } : {}),
...(trace ? { trace } : {}),
...(pendingMessage.metadata?.turnSeq ? { turnSeq: pendingMessage.metadata.turnSeq } : {}),
},
}))
const contentRef = this.contentBlobStore.put(canonicalJson(body), {
@@ -634,7 +641,7 @@ class LocalWorkspaceMessageSubmission {
const assignments = slotList.map((slot, index) => ({
agentKind: slot.agentKind,
slotId: slot.slotId,
role: slot.agentKind === writerKind ? 'writer' : 'primary',
role: slot.permission === 'workspace-write' ? 'writer' : 'primary',
operationId: slot.operationId,
objective: normalizedPhase === 'proposal'
? 'Produce an independent bounded response.'
@@ -879,14 +886,22 @@ class LocalWorkspaceMessageSubmission {
skillHintsByKind = new Map(), resumedGate = null }) {
const snapshotSourceMessageIds = [snapshot.messageId, ...snapshot.history.map(item => item.id)]
const frozenSnapshotHash = controller.orchestration.snapshotHash
const prompts = new Map(activeKinds.map(kind => [kind, v4Prompt({
const prompts = new Map(activeKinds.map(kind => [kind, manualReplyPrompt({
group: { ...group, ...snapshot.group },
kind,
phase: 'proposal',
role: kind === writerKind ? 'writer-proposal' : 'proposal',
snapshot,
skillHints: skillHintsByKind.get(kind) || [],
targetKinds,
taskText: snapshot.taskText,
history: snapshot.history,
skillHintsText: skillHintsPrompt(skillHintsByKind.get(kind) || []),
knowledgeBaseHintsText: knowledgeBaseHintsPrompt(
prepared?.knowledgeBaseHintsByKind?.get(kind) || [],
),
writer: slots.find(slot => slot.agentKind === kind)?.permission === 'workspace-write',
})]))
// Concurrent replies keep slot order in the timeline whatever order they
// finish in; failure notices take their slot's number too.
const reservedTurns = this.reserveTurnSeqs(group.id, threadRootId, activeKinds.length)
const turnSeqByKind = new Map(activeKinds.map((kind, index) => [kind, reservedTurns[index]]))
const pending = await Promise.all(activeKinds.map(async (kind) => {
const index = targetKinds.indexOf(kind)
const slot = slots[index]
@@ -923,12 +938,14 @@ class LocalWorkspaceMessageSubmission {
slotId: slot.slotId,
snapshotHash: frozenSnapshotHash,
sessionPolicy: 'frozen',
...(turnSeqByKind.get(kind) ? { turnSeq: turnSeqByKind.get(kind) } : {}),
promptOverride: prompts.get(kind),
contextPackId: controller.contextPackId,
snapshotSourceMessageIds,
snapshotSourceEntries: snapshot.history,
permissionMode: slot.permission,
singleWriterKind: writerKind || '',
singleWriterKind: '',
independentProposal: true,
parallelGraph: true,
deferMessage: true,
allowMissingProposalReceipt: true,
@@ -965,6 +982,7 @@ class LocalWorkspaceMessageSubmission {
...(resumedGate?.agentKind === kind ? { resumedGate } : {}),
skillHints: skillHintsByKind.get(kind) || [],
knowledgeBaseHints: prepared?.knowledgeBaseHintsByKind?.get(kind) || [],
attachmentSnapshots: prepared?.attachments || [],
},
})
if (controller.signal.aborted) return { kind, index, stopped: true }
@@ -1146,9 +1164,12 @@ class LocalWorkspaceMessageSubmission {
}
async runV4Manual({ group, targetKinds, userMessage, prepared, controller, threadRootId }) {
const writerKind = group.allowWrite === true
? this.v4WriterKind(controller, targetKinds, 'manual')
: ''
const writableKinds = group.allowWrite === true ? targetKinds.filter(kind => (
agentRuntimeCapabilities(kind, {
agent: this.detectedAgents().find(agent => agent.kind === kind),
}).permissionModes.includes('workspace-write')
)) : []
const writerKind = this.v4WriterKind(controller, writableKinds, 'manual')
const snapshot = v4Snapshot({
state: this.snapshot(),
group,
@@ -1168,7 +1189,7 @@ class LocalWorkspaceMessageSubmission {
status: 'pending',
attempt: 0,
operationId: `agent-operation-${createHash('sha256').update(`${controller.runId}:${batchId}:${kind}`).digest('hex')}`,
permission: writerKind && kind === writerKind ? 'workspace-write' : 'read-only',
permission: writableKinds.includes(kind) ? 'workspace-write' : 'read-only',
resultRefs: { artifactIds: [], evidenceIds: [] },
deliveryWatermark: { version: 1, entryIds: [] },
resultBodyArtifactId: '',
@@ -1269,7 +1290,6 @@ class LocalWorkspaceMessageSubmission {
let effectiveResumedGate = resumedGate
if (!effectiveOnlyKind) {
const unknownWriters = slots.filter(isV4ManualUnknownWriter)
if (unknownWriters.length > 1) throw new Error('LOCAL_RUN_CONTINUATION_INVALID')
const unknownWriter = unknownWriters[0]
if (unknownWriter) {
if (!this.requestHumanGate) throw new Error('LOCAL_RUN_RETRY_GATE_INVALID')
@@ -9,6 +9,11 @@ const {
terminalStatusPrefix,
} = require('./local-workspace-inputs.cjs')
function turnSeqMetadata(error) {
const turnSeq = Number(error?.turnSeq)
return Number.isSafeInteger(turnSeq) && turnSeq > 0 ? { turnSeq } : {}
}
class LocalWorkspaceRunMessages {
constructor(options) {
this.state = options.state
@@ -73,7 +78,7 @@ class LocalWorkspaceRunMessages {
kind,
threadRootId,
system,
{ trace, responseVersionRootId },
{ trace, responseVersionRootId, ...turnSeqMetadata(error) },
)
}
return { label, reason }
@@ -114,7 +119,7 @@ class LocalWorkspaceRunMessages {
key: interrupted ? 'system.agentInterrupted' : 'system.agentStopped',
params: { agent: label },
},
{ trace, responseVersionRootId },
{ trace, responseVersionRootId, ...turnSeqMetadata(error) },
)
return trace
}
@@ -10,8 +10,12 @@ const {
const AUTO_CONSENSUS_MARKER = /\[\[MELDWORK_CONSENSUS:(agree|continue)\]\]/gi
const AUTO_FINAL_CONSENSUS_MARKER = /(?:^|\r?\n)[ \t]*\[\[MELDWORK_CONSENSUS:(agree|continue)\]\][ \t]*$/i
const DEFAULT_RUN_SILENCE_WARNING_MS = 20 * 1000
const DEFAULT_RUN_AGENT_TIMEOUT_MS = 3 * 60 * 1000
const DEFAULT_RUN_AGENT_TOOL_TIMEOUT_MS = 5 * 60 * 1000
// Inactivity windows for one Agent turn. Real CLIs regularly think, run builds
// or wait on slow providers for several minutes; the former 3/5 minute limits
// killed Agents that were still working. A separate no-progress cap (three
// times the inactivity window) still stops CLIs that only emit heartbeats.
const DEFAULT_RUN_AGENT_TIMEOUT_MS = 10 * 60 * 1000
const DEFAULT_RUN_AGENT_TOOL_TIMEOUT_MS = 20 * 60 * 1000
const DEFAULT_RUN_ABORT_GRACE_MS = 2500
const DEFAULT_AUTO_ROUNDS = 6
const MAX_AUTO_ROUNDS = 10
@@ -1,5 +1,8 @@
const fs = require('node:fs')
const { atomicWritePrivateFile } = require('../security/private-file.cjs')
const path = require('node:path')
const { parseDeliveryTasks } = require('./delivery-tasks.cjs')
const { loadedWorkPreferences } = require('./work-preferences.cjs')
const { RUN_STATUSES } = require('../runs/run-ledger-records.cjs')
const { normalizeSessionMeta } = require('../runs/run-harness.cjs')
const {
@@ -184,8 +187,17 @@ function loadWorkspaceState(storagePath) {
}
try {
const repliedAgentKinds = new Map()
for (const message of Array.isArray(parsed.messages) ? parsed.messages : []) {
if (message?.role !== 'agent' || typeof message.groupId !== 'string') continue
const kinds = repliedAgentKinds.get(message.groupId) || []
if (!kinds.includes(message.agentKind)) kinds.push(message.agentKind)
repliedAgentKinds.set(message.groupId, kinds)
}
const groups = parsed.groups
.map(normalizeLoadedGroup)
.map(group => normalizeLoadedGroup(group, {
recoveredAgentKinds: repliedAgentKinds.get(group?.id) || [],
}))
.filter(Boolean)
const groupIds = new Set(groups.map(group => group.id))
const messages = parsed.messages
@@ -211,6 +223,10 @@ function loadWorkspaceState(storagePath) {
return {
state: {
version: 3,
...(parsed.workPreferences ? { workPreferences: loadedWorkPreferences(parsed.workPreferences) } : {}),
...(parsed.deliveryTasks ? {
deliveryTasks: parseDeliveryTasks(parsed.deliveryTasks).filter(task => groupIds.has(task.groupId)),
} : {}),
groups,
messages,
sessions,
@@ -238,11 +254,12 @@ function loadWorkspaceState(storagePath) {
}
}
// The whole workspace is rewritten on every save, synchronously on the main
// process. Compact JSON cuts the bytes written (and the time the UI is
// blocked) for long histories, and the private atomic writer fsyncs before
// the rename so a crash cannot leave a truncated file behind.
function saveWorkspaceState(storagePath, state) {
fs.mkdirSync(path.dirname(storagePath), { recursive: true })
const tempPath = `${storagePath}.tmp`
fs.writeFileSync(tempPath, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 })
fs.renameSync(tempPath, storagePath)
atomicWritePrivateFile(storagePath, `${JSON.stringify(state)}\n`)
}
function durableWaitingRunSnapshots({ state, records, pendingGates, liveRunIds, liveGroupIds }) {
@@ -403,10 +420,23 @@ function workspaceSnapshot({
return gate?.runId === snapshot.runId
}),
}))
const deliveryMessages = new Map((state.deliveryTasks || []).flatMap(task => (
task.attempts.map(attempt => [attempt.messageId, { task, attempt }])
)))
return {
agents: detectedAgents.map(({ executable, ...agent }) => agent),
groups: state.groups,
messages: state.messages,
messages: state.messages.map(message => {
const delivery = deliveryMessages.get(message.id)
if (!delivery || message.role !== 'user' || message.groupId !== delivery.task.groupId) return message
const { task, attempt } = delivery
return { ...message, content: [
task.brief.title, task.brief.goal, task.brief.output,
...task.brief.criteria.map(item => `- ${item}`),
attempt.revision?.note, attempt.input,
].filter(Boolean).join('\n\n') }
}),
...(state.deliveryTasks?.length ? { deliveryTasks: structuredClone(state.deliveryTasks) } : {}),
...(workspaceRecovery?.trusted === false ? {
recovery: {
state: 'read-only',
Loaded 100 of 285 files, more files were not shown because too many files have changed in this diff. Show more