|
| 1 | +import ComposableArchitecture |
| 2 | +import Foundation |
| 3 | +import Testing |
| 4 | + |
| 5 | +@testable import GraphcodeKit |
| 6 | + |
| 7 | +/// The gaps the live five-backend run of #346 found on 0.1.70-beta2. |
| 8 | +@Suite |
| 9 | +struct GoalResolutionFollowUpTests { |
| 10 | + // MARK: - A follow-up question reaches a finished loop |
| 11 | + |
| 12 | + private func finishedTarget( |
| 13 | + alive: Bool, delivered: LockIsolated<[String]>, memory: LockIsolated<[String]> |
| 14 | + ) async -> (GraphStore, UUID) { |
| 15 | + let store = GraphStore( |
| 16 | + onDeliverMessage: { _, text, _ in |
| 17 | + delivered.withValue { $0.append(text) } |
| 18 | + return true |
| 19 | + }, |
| 20 | + onSessionAlive: { _, _ in alive }, |
| 21 | + onAppendMemory: { _, entry in memory.withValue { $0.append(entry) } }) |
| 22 | + await store.handle( |
| 23 | + .createNode( |
| 24 | + NodeDraft(title: "Docs", loopType: .goalBased, goal: GoalSpec(summary: "Write it")))) |
| 25 | + let id = await store.graph.nodes[0].id |
| 26 | + await store.handle(.completeNode(id, result: nil, from: id)) |
| 27 | + return (store, id) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + func aQuestionToAFinishedLoopWithALiveSessionIsTypedInAndChangesNothing() async { |
| 32 | + let delivered = LockIsolated<[String]>([]) |
| 33 | + let memory = LockIsolated<[String]>([]) |
| 34 | + let (store, id) = await finishedTarget(alive: true, delivered: delivered, memory: memory) |
| 35 | + let resolution = await store.graph.nodes[id: id]?.resolution |
| 36 | + |
| 37 | + await store.handle(.messageNode(id, text: "what did you change?", from: nil, followUp: false)) |
| 38 | + |
| 39 | + #expect(delivered.value.contains { $0.contains("what did you change?") }) |
| 40 | + #expect(!memory.value.contains { $0.hasPrefix("while you were away") }) |
| 41 | + #expect(await store.graph.nodes[id: id]?.state == .succeeded) |
| 42 | + #expect(await store.graph.nodes[id: id]?.resolution == resolution) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + func aQuestionToAFinishedLoopWhoseSessionEndedIsStaged() async { |
| 47 | + let delivered = LockIsolated<[String]>([]) |
| 48 | + let memory = LockIsolated<[String]>([]) |
| 49 | + let (store, id) = await finishedTarget(alive: false, delivered: delivered, memory: memory) |
| 50 | + |
| 51 | + await store.handle(.messageNode(id, text: "what did you change?", from: nil, followUp: false)) |
| 52 | + |
| 53 | + #expect(!delivered.value.contains { $0.contains("what did you change?") }) |
| 54 | + #expect(memory.value.contains { $0.hasPrefix("while you were away") }) |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: - /goal stays the command when the prompt moves to a file |
| 58 | + |
| 59 | + @Test |
| 60 | + func aPointerForADirectiveLedPromptStillOpensWithTheDirective() { |
| 61 | + let pointer = "Your complete instructions are in the file at /x/PROMPT.md - read it." |
| 62 | + let led = ZmxSessionLauncher.directiveLedPointer( |
| 63 | + pointer, prompt: "/goal Write the docs for the login flow", directive: "/goal") |
| 64 | + #expect(led == "/goal Write the docs for the login flow - \(pointer)") |
| 65 | + #expect(led.hasPrefix("/goal ")) |
| 66 | + |
| 67 | + let long = "/goal " + String(repeating: "word ", count: 80) |
| 68 | + let cut = ZmxSessionLauncher.directiveLedPointer(pointer, prompt: long, directive: "/goal") |
| 69 | + #expect(cut.hasPrefix("/goal word")) |
| 70 | + #expect(cut.contains("... - \(pointer)")) |
| 71 | + |
| 72 | + #expect( |
| 73 | + ZmxSessionLauncher.directiveLedPointer( |
| 74 | + pointer, prompt: "/goal Write the docs", directive: "/goal", headLength: 0) |
| 75 | + == "/goal \(pointer)") |
| 76 | + #expect( |
| 77 | + ZmxSessionLauncher.directiveLedPointer(pointer, prompt: "Work toward it", directive: nil) |
| 78 | + == pointer) |
| 79 | + #expect( |
| 80 | + ZmxSessionLauncher.directiveLedPointer(pointer, prompt: "Plain prose", directive: "/goal") |
| 81 | + == pointer) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + func aLongCodexGoalLaunchesWithGoalAsTheCommand() { |
| 86 | + let goal = String(repeating: "Write the single line into the file and verify it. ", count: 60) |
| 87 | + let node = LoopNode( |
| 88 | + title: "Long", loopType: .goalBased, goal: GoalSpec(summary: goal), backend: .codex) |
| 89 | + defer { NodeMemory.remove(projectPath: "/tmp", nodeID: node.id) } |
| 90 | + |
| 91 | + let arguments = |
| 92 | + ZmxSessionLauncher.arguments( |
| 93 | + forNode: node, projectPath: "/tmp", settings: GraphcodeSettings()) ?? [] |
| 94 | + |
| 95 | + let typedPrompt = arguments.first { $0.contains(NodeMemory.promptFileName) } |
| 96 | + #expect(typedPrompt?.hasPrefix("/goal Write the single line") == true) |
| 97 | + } |
| 98 | + |
| 99 | + // MARK: - A backend with no verdict of its own is told to report done |
| 100 | + |
| 101 | + @Test |
| 102 | + func openCodeAndPiGoalsAreToldToRunNodeDone() { |
| 103 | + for backend in [CLISessionBackendKind.openCode, .pi] { |
| 104 | + let node = LoopNode( |
| 105 | + title: "a", loopType: .goalBased, goal: GoalSpec(summary: "Ship it"), backend: backend) |
| 106 | + #expect(node.sessionPrompt?.hasSuffix(LoopNode.reportDoneSentence) == true) |
| 107 | + } |
| 108 | + for backend in [CLISessionBackendKind.claudeCode, .codex, .copilotCLI] { |
| 109 | + let node = LoopNode( |
| 110 | + title: "a", loopType: .goalBased, goal: GoalSpec(summary: "Ship it"), backend: backend) |
| 111 | + #expect(node.sessionPrompt?.contains("graphcode node done") == false) |
| 112 | + } |
| 113 | + let pi = LoopNode( |
| 114 | + title: "a", loopType: .goalBased, goal: GoalSpec(summary: "Ship it"), backend: .pi) |
| 115 | + let literal = pi.sessionPrompt(forProjectPath: "/Volumes/SCG/wd/graphcode") ?? "" |
| 116 | + #expect( |
| 117 | + literal.contains( |
| 118 | + "run: graphcode node done /Volumes/SCG/wd/graphcode \(pi.id.uuidString) <one-line result>")) |
| 119 | + #expect(!literal.contains(LoopNode.reportDoneSentence)) |
| 120 | + let predicated = LoopNode( |
| 121 | + title: "a", loopType: .goalBased, goal: GoalSpec(summary: "Ship it", predicate: "true"), |
| 122 | + backend: .pi) |
| 123 | + #expect(predicated.sessionPrompt?.contains("graphcode node done") == false) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + func aChildGoalLoopIsHandedTheDoneCommandAtBirth() async { |
| 128 | + let memory = LockIsolated<[(UUID, String)]>([]) |
| 129 | + let store = GraphStore(onAppendMemory: { id, entry in memory.withValue { $0.append((id, entry)) } }) |
| 130 | + await store.handle( |
| 131 | + .createNode(NodeDraft(title: "Lead", loopType: .goalBased, goal: GoalSpec(summary: "Lead")))) |
| 132 | + let leader = await store.graph.nodes[0].id |
| 133 | + await store.handle( |
| 134 | + .createNode( |
| 135 | + NodeDraft( |
| 136 | + title: "Child", loopType: .goalBased, goal: GoalSpec(summary: "Child work"), |
| 137 | + backend: .pi, createdBy: leader))) |
| 138 | + let child = await store.graph.nodes[1].id |
| 139 | + |
| 140 | + let birth = memory.value.first { $0.0 == child }?.1 ?? "" |
| 141 | + #expect(birth.contains("graphcode node send")) |
| 142 | + #expect(birth.contains("graphcode node done")) |
| 143 | + #expect(birth.contains(child.uuidString)) |
| 144 | + } |
| 145 | +} |
0 commit comments