|
23 | 23 |
|
24 | 24 | WORKSPACE_SCHEMA = "persephone.robomp.workspace.v1" |
25 | 25 | ISSUE_SCHEMA = "robomp.issue.workspace.v1" |
| 26 | +ASSISTANT_SCHEMA = "persephone.robomp.assistant.v1" |
26 | 27 | MAX_OWNER_OUTPUT = 16_000_000 |
27 | 28 | MAX_MUTATION_BYTES = 1_000_000 |
| 29 | +MAX_ASSISTANT_BYTES = 1_000_000 |
28 | 30 | MUTATION_ACTIONS = { |
29 | 31 | "configuration.patch", |
30 | 32 | "trigger.triage", |
@@ -115,6 +117,68 @@ def inspect(self, *, issue: str, limit: int = 50) -> dict[str, Any]: |
115 | 117 | raise RoboOMPWorkspaceError("Persephone returned a mismatched issue workspace") |
116 | 118 | return result |
117 | 119 |
|
| 120 | + def assistant(self, request: dict[str, Any]) -> dict[str, Any]: |
| 121 | + if ( |
| 122 | + not isinstance(request, dict) |
| 123 | + or type(request.get("version")) is not int |
| 124 | + or request.get("version") != 1 |
| 125 | + ): |
| 126 | + raise RoboOMPWorkspaceError("RoboOMP assistant request version must be 1") |
| 127 | + operation = request.get("operation", "ask") |
| 128 | + if operation not in {"ask", "history"}: |
| 129 | + raise RoboOMPWorkspaceError("RoboOMP assistant operation must be ask or history") |
| 130 | + issue = request.get("issue") |
| 131 | + if not isinstance(issue, str) or not issue.strip() or len(issue) > 500: |
| 132 | + raise RoboOMPWorkspaceError("RoboOMP assistant issue reference is invalid") |
| 133 | + issue = issue.strip() |
| 134 | + request = {**request, "issue": issue} |
| 135 | + question = request.get("question", "") |
| 136 | + if operation == "ask" and ( |
| 137 | + not isinstance(question, str) |
| 138 | + or not question.strip() |
| 139 | + or len(question) > 32_000 |
| 140 | + ): |
| 141 | + raise RoboOMPWorkspaceError("RoboOMP assistant question is invalid") |
| 142 | + context = request.get("context", []) |
| 143 | + if not isinstance(context, list) or len(context) > 16: |
| 144 | + raise RoboOMPWorkspaceError("RoboOMP assistant context is invalid") |
| 145 | + if not all(isinstance(item, dict) for item in context): |
| 146 | + raise RoboOMPWorkspaceError("RoboOMP assistant context items must be objects") |
| 147 | + encoded = json.dumps(request, ensure_ascii=False, separators=(",", ":")).encode("utf-8") |
| 148 | + if len(encoded) > MAX_ASSISTANT_BYTES: |
| 149 | + raise RoboOMPWorkspaceError("RoboOMP assistant request exceeds the 1 MB owner limit") |
| 150 | + |
| 151 | + self.payload_root.mkdir(parents=True, exist_ok=True, mode=0o700) |
| 152 | + try: |
| 153 | + self.payload_root.chmod(0o700) |
| 154 | + except OSError: |
| 155 | + pass |
| 156 | + digest = hashlib.sha256(encoded).hexdigest() |
| 157 | + payload = self.payload_root / f"assistant-{time.time_ns()}-{digest[:16]}.json" |
| 158 | + atomic_write_json(str(payload), request, indent=2) |
| 159 | + try: |
| 160 | + payload.chmod(0o600) |
| 161 | + except OSError: |
| 162 | + pass |
| 163 | + try: |
| 164 | + result = self._owner_json( |
| 165 | + [ |
| 166 | + "git-agent", |
| 167 | + "workspace", |
| 168 | + "assistant", |
| 169 | + str(payload), |
| 170 | + "--consume", |
| 171 | + ], |
| 172 | + timeout=691_320, |
| 173 | + ) |
| 174 | + finally: |
| 175 | + payload.unlink(missing_ok=True) |
| 176 | + if result.get("schemaVersion") != ASSISTANT_SCHEMA: |
| 177 | + raise RoboOMPWorkspaceError("Persephone returned an unsupported RoboOMP assistant schema") |
| 178 | + if result.get("issue") != issue or result.get("operation") != operation: |
| 179 | + raise RoboOMPWorkspaceError("Persephone returned a mismatched RoboOMP assistant response") |
| 180 | + return result |
| 181 | + |
118 | 182 | def create_lifecycle_plan(self, *, action: str) -> tuple[dict[str, Any], str]: |
119 | 183 | if action not in LIFECYCLE_ACTIONS: |
120 | 184 | raise RoboOMPWorkspaceError("Unsupported RoboOMP lifecycle action") |
|
0 commit comments