Skip to content

Commit 2a3a352

Browse files
authored
Merge pull request #1 from im4codes/dev
Dev
2 parents 8dd4cf1 + fd39689 commit 2a3a352

56 files changed

Lines changed: 3014 additions & 1929 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## Context
2+
3+
P2P discussions already support multi-round execution, but each hop is awaited serially. The current design uses a single shared context file that all hops append to, which is simple for a serial chain but becomes unsafe and hard to reason about once multiple hops run concurrently. The user wants the parallel version to preserve existing naming and prompt patterns, use an LLM-driven collection/synthesis step, and keep the main discussion file updated in place.
4+
5+
The key architectural constraint is that this is not a source-control merge system. The goal is to preserve multiple agents' viewpoints well enough that the summary step can synthesize them, not to perform perfect byte-for-byte file merging. The highest-value contract is clear hop/run state observability; minor formatting duplication is acceptable if the content remains attributable and summarizable.
6+
7+
## Goals / Non-Goals
8+
9+
**Goals:**
10+
- Parallelize non-summary hops within each round.
11+
- Keep the initiator kickoff and round summary as round barriers.
12+
- Give each hop a dedicated temp file and reserve main-file writes for orchestrator collection plus summary append.
13+
- Add explicit hop and round status contracts that can be relayed compatibly through shared types and existing consumers.
14+
- Preserve the legacy top-level P2P progress projection through a daemon-owned compatibility layer.
15+
- Preserve existing prompt naming and discussion heading conventions, with minimal prompt changes outside summary collection instructions.
16+
17+
**Non-Goals:**
18+
- Building a perfect diff/merge engine for hop files.
19+
- Rewriting existing P2P discussion UX beyond additive compatibility with richer run-update payloads.
20+
- Changing the meaning of existing P2P modes or round prompt naming.
21+
- Introducing a second long-lived persistence model for hop files beyond round-scoped temp artifacts.
22+
23+
## Decisions
24+
25+
### 1. Use per-hop temp files and keep the main discussion file single-writer during collection
26+
Each phase-2 hop gets its own temp file for the round. That keeps concurrent writers off the main discussion file entirely. After the round barrier, the orchestrator collects the newly-added content from each hop file and appends it to the main discussion file. The summary step then reads the updated main file and appends its round summary section.
27+
28+
**Alternatives considered:**
29+
- Direct concurrent writes to the main file: rejected because correctness depends on write interleaving behavior and makes attribution/debugging harder.
30+
- Let the summary LLM perform all main-file structural writes: rejected because it weakens append-only guarantees, complicates retries, and reduces testability.
31+
32+
### 2. Identify hop-added content with a bounded byte-offset strategy
33+
For each round, the orchestrator records the main discussion file size before creating hop temp files. Hop temp files are seeded from that main file snapshot. After each hop settles, the orchestrator treats content after the recorded byte offset as that hop's newly-added analysis.
34+
35+
The governing correctness rule is one-way:
36+
- **missing completed-hop evidence is never acceptable**
37+
- **minor duplication is acceptable**
38+
39+
If a hop file does not preserve the expected append-only structure well enough for exact byte-offset extraction, the implementation should prefer retaining attributable content over preserving perfect formatting or strict idempotency.
40+
41+
**Alternatives considered:**
42+
- Heading-based parsing: workable, but more fragile if prompt formatting drifts.
43+
- Whole-file concatenation: rejected because it reintroduces duplicated history into each round's summary input.
44+
45+
### 3. Treat summary as evidence collection + synthesis, not perfect reconstruction
46+
The summary prompt reads the round's collected evidence and appends the round summary section. The system is allowed to preserve minor duplication or formatting noise as long as each hop's viewpoint remains attributable and summarizable.
47+
48+
**Alternatives considered:**
49+
- Perfect diff/merge semantics with strict idempotency: useful but too heavy for the product goal.
50+
- Blind concatenation of whole hop files: too likely to drown the summary in duplicated history.
51+
52+
### 4. Add explicit hop and run state contracts before wiring broader consumers
53+
Parallel execution makes the old serial run status insufficient. The design therefore introduces explicit hop states and summary-phase run states first, then threads them through shared types, daemon orchestration, and additive downstream relay. This is the main guardrail against an implementation that “works” but is impossible to reason about in production.
54+
55+
The compatibility projection remains daemon-owned: the daemon serializer is responsible for emitting the legacy top-level `status`, phase, and progress fields expected by current consumers, while newer hop/run detail remains additive.
56+
57+
**Alternatives considered:**
58+
- Keep only existing run-level status fields: rejected because parallel hops would be opaque.
59+
- Emit only best-effort textual progress: rejected because it is not stable enough for tests or downstream compatibility.
60+
61+
### 5. Minimize prompt churn
62+
The kickoff and hop prompts should keep existing naming and structure. Only the summary prompt gets a new instruction block telling the summary step to consider the round's collected hop findings and append the integrated round-summary section.
63+
64+
**Alternatives considered:**
65+
- Rewriting every mode prompt around parallel execution: rejected because it creates unnecessary drift and retuning cost.
66+
67+
### 6. Keep server/web scope additive in this change
68+
Most execution changes belong in the daemon orchestrator and shared contracts. Server relay and existing web consumers should remain compatible with richer run-update payloads, but this change does not require a new dedicated UI capability or full browser-side hop timeline redesign.
69+
70+
**Alternatives considered:**
71+
- Expanding scope to fully redesign P2P UI progress handling: rejected as out of scope for this change.
72+
- Keeping all new fields daemon-local: rejected because server/web still need additive compatibility.
73+
74+
## Risks / Trade-offs
75+
76+
- **[A hop's new analysis is partially missed]** → Mitigate by using a deterministic byte-offset baseline per round, testing divergent multi-hop outputs, and prioritizing content retention over perfect formatting.
77+
- **[A hop rewrites or truncates its temp file instead of pure append]** → Mitigate by treating append-only structure as a best-effort expectation, preferring attributable content retention over strict exactness, and making missing completed-hop evidence a test failure.
78+
- **[Parallel state transitions become hard to debug]** → Mitigate by defining hop/run status contracts up front and testing event ordering explicitly.
79+
- **[Cross-project hops accidentally regain write access to the main file]** → Mitigate by making temp-file-only writes an explicit orchestration rule and copying cross-project hop artifacts back to the main project's hop-file location instead of the main discussion file.
80+
- **[Temp files accumulate after crashes or cleanup failures]** → Mitigate by best-effort post-summary deletion plus orphan cleanup on later orchestrator startup or run initialization.
81+
- **[Implementation drifts by copying dispatch logic]** → Mitigate by parameterizing `dispatchHop` instead of introducing a second near-duplicate control path.
82+
- **[Richer payloads break downstream consumers]** → Mitigate by making new run-update fields additive and testing compatibility at the relay layer.
83+
84+
## Migration Plan
85+
86+
1. Define shared hop/run status constants and additive run-update payload shape.
87+
2. Refactor daemon orchestration so `dispatchHop` accepts per-hop file/watch parameters without duplicating logic.
88+
3. Add per-hop temp-file lifecycle, phase-2 parallel dispatch, and cross-project hop copy-back into round hop artifacts.
89+
4. Add orchestrator-side evidence collection into the main discussion file and summary append flow.
90+
5. Thread expanded run payloads through existing server relay and verify existing consumers remain compatible.
91+
6. Land unit, integration, and event-order tests before enabling the new path by default.
92+
93+
Rollback is straightforward: switch orchestration back to the existing serial path and ignore hop temp files. The new shared status fields should remain additive so the serial path can still populate a compatible subset.
94+
95+
## Resolved Decisions
96+
97+
- Run updates SHALL expose both a compatibility-friendly top-level projection and a stable per-hop list for debugging/observers.
98+
- Summary prompts do not need to restate failed/timed-out hop details verbatim; only completed-hop evidence is guaranteed to be collected into the main discussion file, while failed terminal states remain observable via run updates.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Why
2+
3+
P2P multi-round discussion is currently fully serial, so total runtime grows with every hop in every round. That makes multi-agent audit/review discussions too slow, and it also forces all hops to write directly into one shared file, which makes round-level collection and status reporting hard to reason about.
4+
5+
## What Changes
6+
7+
- Run phase-2 hops in parallel within each round, while keeping the initiator kickoff and round summary sequential.
8+
- Give each hop its own temporary discussion file, let the orchestrator collect each hop's newly added analysis into the main discussion file in place, and let the summary step append the round summary section.
9+
- Standardize hop-level and run-level status updates so timeout, failure, cancel, and summary phases are observable.
10+
- Make the daemon serializer explicitly responsible for preserving the legacy top-level P2P progress projection so richer hop/run fields remain additive for existing consumers.
11+
- Preserve existing discussion naming and prompt structure, with only the summary prompt gaining explicit collection/synthesis instructions.
12+
- Keep server/web scope additive: richer run-update payloads are relayed compatibly, without requiring a new P2P UI redesign in this change.
13+
14+
## Capabilities
15+
16+
### New Capabilities
17+
- `p2p-parallel-orchestration`: Parallelize per-round hop execution with per-hop temp files, orchestrator-managed main-file collection, and summary-driven round synthesis.
18+
- `p2p-hop-status`: Expose explicit hop and round status transitions for daemon progress tracking and additive downstream relay.
19+
20+
### Modified Capabilities
21+
- `timeline-events`: Extend discussion-related run updates with additive hop-progress and summary-phase fields so downstream consumers can observe parallel execution without breaking existing payload handling.
22+
23+
## Impact
24+
25+
- **Daemon**: `src/daemon/p2p-orchestrator.ts`, prompt construction, temp-file management, timeout/cancel flow, and tests.
26+
- **Shared**: New shared P2P status/event contract for hop- and round-level states.
27+
- **Server**: Relay richer P2P run-update payloads compatibly.
28+
- **Web**: Existing consumers of P2P run updates remain compatible with additive fields; full new UI behavior is out of scope for this change.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## ADDED Requirements
2+
3+
### Requirement: Each hop has a defined lifecycle state set
4+
The system SHALL track each hop independently using the lifecycle states `queued`, `dispatched`, `running`, `completed`, `timed_out`, `failed`, and `cancelled`.
5+
6+
#### Scenario: Successful hop lifecycle
7+
- **WHEN** a hop is accepted, dispatched, runs, and finishes normally
8+
- **THEN** that hop transitions through defined lifecycle states ending in `completed`
9+
10+
#### Scenario: Timed-out hop lifecycle
11+
- **WHEN** a hop exceeds its timeout budget
12+
- **THEN** that hop transitions to `timed_out` and does not block other hops in the same round
13+
14+
#### Scenario: Failed hop lifecycle
15+
- **WHEN** dispatch or execution fails for one hop
16+
- **THEN** that hop transitions to `failed` while other hops in the same round continue toward the barrier
17+
18+
#### Scenario: Cancelled hop lifecycle
19+
- **WHEN** the overall run is cancelled before a hop has completed
20+
- **THEN** that hop transitions to `cancelled`
21+
22+
### Requirement: Run-level state distinguishes round execution from summary execution
23+
The system SHALL expose run-level states that distinguish round execution from summary execution. At minimum, the run SHALL represent preparing, round execution, summarizing, completed, failed, and cancelled outcomes.
24+
25+
`preparing` SHALL mean the run has been created and is performing run-start or round-start setup before the first dispatch of that execution window. It SHALL exit when the initiator kickoff begins for round 1, or when a later round begins dispatch preparation if the implementation chooses to expose per-round preparation.
26+
27+
#### Scenario: Entering summary phase
28+
- **WHEN** all hops in a round have reached terminal hop states
29+
- **THEN** the run enters a summary-specific state before the summary step starts appending the round-summary section
30+
31+
#### Scenario: Summary completion advances run
32+
- **WHEN** the summary step finishes for a non-final round
33+
- **THEN** the run transitions back into round execution for the next round instead of directly completing
34+
35+
#### Scenario: Run-level transitions stay within the defined state machine
36+
- **WHEN** a run changes top-level state
37+
- **THEN** it only transitions along legal paths: `preparing -> round execution`, `round execution -> summarizing`, `round execution -> failed`, `round execution -> cancelled`, `summarizing -> round execution`, `summarizing -> completed`, `summarizing -> failed`, or `summarizing -> cancelled`
38+
39+
### Requirement: Hop terminal states have defined summary semantics
40+
Only `completed` hops SHALL contribute collected evidence to the main discussion file. `timed_out`, `failed`, and `cancelled` hops SHALL remain observable in run updates but SHALL NOT be treated as successful evidence sources for that round.
41+
42+
#### Scenario: Partial failure still permits summary
43+
- **WHEN** one hop fails or times out but other hops in the round complete
44+
- **THEN** the run update reflects the non-completed hop terminal state and the summary phase may still start using only the completed hop evidence
45+
46+
#### Scenario: Zero completed hops still has defined behavior
47+
- **WHEN** every hop in a round reaches `timed_out`, `failed`, or `cancelled` and zero hops complete
48+
- **THEN** the run still enters the summary phase for that round, the main discussion file receives no completed-hop evidence for that round, and the summary step appends a summary section based on the empty-evidence outcome instead of silently skipping the round
49+
50+
### Requirement: Hop and run updates are relayed compatibly to observers
51+
The daemon SHALL emit run updates that include hop-level status progress and summary-phase transitions as additive fields, and downstream relay behavior SHALL preserve compatibility for consumers that do not understand the new fields. The daemon serializer SHALL own this compatibility projection.
52+
53+
#### Scenario: Browser receives hop progress
54+
- **WHEN** a hop transitions from running to completed
55+
- **THEN** connected observers receive a run update that reflects that hop's new terminal state
56+
57+
#### Scenario: Additive compatibility for older consumers
58+
- **WHEN** a downstream consumer reads a richer run-update payload but ignores hop-level fields
59+
- **THEN** the existing run-update handling still succeeds without requiring new mandatory fields
60+
61+
#### Scenario: Legacy skipped compatibility is preserved
62+
- **WHEN** a hop ends in any non-completed terminal state
63+
- **THEN** the richer hop-level payload records the specific terminal state, and any legacy skip-oriented compatibility field remains an aggregate backward-compatible projection rather than a replacement for the detailed hop state
64+
65+
### Requirement: Cancellation preserves completed hop outcomes
66+
The system SHALL preserve completed hop outcomes even when the overall run is cancelled.
67+
68+
#### Scenario: Cancel during phase-2 execution
69+
- **WHEN** the user cancels a run while some hops have already completed and others are still running
70+
- **THEN** completed hops remain marked completed, unfinished hops transition to cancelled, and no new summary phase starts

0 commit comments

Comments
 (0)