feat(ai): add AI VM services (diagnosis agent, pgvector RAG, Discord HITL bot) and harness approval endpoints
실험 설계서 6장(WP-12)의 AI VM 2층 구현.
- cmd/agent: 하네스 /diagnose 수신 → OpenAI Responses API 함수 호출 루프(읽기 전용 도구 7종:
query_metrics, query_logs, get_probe_status, get_k8s, get_gcp_instances, get_aws_state,
search_runbooks) → strict 스키마 submit_diagnosis(6.3) → 하네스 /proposals 등록 + HITL 게시,
또는 신뢰도 0.5 미만·escalate 시 3층 큐 에스컬레이션. 세션·프롬프트 버전·러너북 벡터는
pgvector(ai 스키마)에 저장. 라벨·조치 enum은 하네스 /scenarios·/actions/catalog에서 갱신.
- cmd/hitl: Discord Gateway 봇(intents GUILDS만). 승인/거부 버튼(custom_id approve:/reject:<token>),
3초 내 DEFERRED_UPDATE_MESSAGE, 승인 시 하네스 승인 → /actions/execute → 원 메시지 편집,
15분 무응답 시 에스컬레이션 채널로 이동. 재기동 시 미결 제안 타이머 복원.
- harness: POST /proposals/{token}/approve, /reject 추가. 제안은 진단 시점에 등록하고 승인은
버튼 시점에 기록해 MTTA(승인-제안)가 실제 대기 시간이 되도록 함.
OpenAI·Discord 토큰은 Secret Manager 파일로만 읽고 코드·저장소에 두지 않는다.
This commit is contained in:
1 parent
de076a79e4
commit
f30af2a4c3
21 files changed
+2875
-1
No files matched your search
@@ -0,0 +1,122 @@
|
||||
// Package harness talks to the harness runner (design 5.2): proposals, approval, execution, catalogue, scenarios.
|
||||
package harness
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
BaseURL string // http://10.20.0.11:8080
|
||||
HTTP *http.Client
|
||||
}
|
||||
|
||||
func New(base string) *Client {
|
||||
return &Client{BaseURL: strings.TrimRight(base, "/"), HTTP: &http.Client{Timeout: 11 * time.Minute}}
|
||||
}
|
||||
|
||||
func (c *Client) call(ctx context.Context, method, path string, body any, out any) (int, error) {
|
||||
var rd io.Reader
|
||||
if body != nil {
|
||||
b, _ := json.Marshal(body)
|
||||
rd = bytes.NewReader(b)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, c.BaseURL+path, rd)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
resp, err := c.HTTP.Do(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
rb, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
if out != nil && len(rb) > 0 {
|
||||
if err := json.Unmarshal(rb, out); err != nil {
|
||||
return resp.StatusCode, fmt.Errorf("harness %s %s: status %d, body not JSON: %s", method, path, resp.StatusCode, truncate(string(rb), 300))
|
||||
}
|
||||
}
|
||||
if resp.StatusCode/100 != 2 {
|
||||
return resp.StatusCode, fmt.Errorf("harness %s %s: status %d: %s", method, path, resp.StatusCode, truncate(string(rb), 300))
|
||||
}
|
||||
return resp.StatusCode, nil
|
||||
}
|
||||
|
||||
type Proposal struct {
|
||||
Token string `json:"token"`
|
||||
ExpiresIn float64 `json:"expires_in_s"`
|
||||
Approved bool `json:"approved"`
|
||||
}
|
||||
|
||||
// Propose registers a layer-2 proposal (POST /proposals); the harness records the proposal event and returns the token.
|
||||
func (c *Client) Propose(ctx context.Context, runID, scenarioID *string, actionID string, target, diagnosis map[string]any, ttl time.Duration) (*Proposal, error) {
|
||||
body := map[string]any{"run_id": runID, "scenario_id": scenarioID, "action_id": actionID, "target": target, "diagnosis": diagnosis}
|
||||
if ttl > 0 {
|
||||
body["ttl_seconds"] = int(ttl.Seconds())
|
||||
}
|
||||
var p Proposal
|
||||
if _, err := c.call(ctx, "POST", "/proposals", body, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// Approve marks the proposal approved (POST /proposals/{token}/approve); the harness records the approval event.
|
||||
func (c *Client) Approve(ctx context.Context, token, by string) error {
|
||||
_, err := c.call(ctx, "POST", "/proposals/"+token+"/approve", map[string]any{"approved_by": by}, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reject records the rejection (POST /proposals/{token}/reject).
|
||||
func (c *Client) Reject(ctx context.Context, token, by, reason string) error {
|
||||
_, err := c.call(ctx, "POST", "/proposals/"+token+"/reject", map[string]any{"rejected_by": by, "reason": reason}, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// Execute runs the catalogue action with the approved token (POST /actions/execute). The body is returned as-is for the
|
||||
// Discord message; a non-2xx status is an error with the body text.
|
||||
func (c *Client) Execute(ctx context.Context, token, actionID string, target map[string]any, runID, scenarioID *string, note string) (map[string]any, error) {
|
||||
body := map[string]any{"action_id": actionID, "target": target, "token": token, "run_id": runID, "scenario_id": scenarioID, "note": note}
|
||||
var out map[string]any
|
||||
_, err := c.call(ctx, "POST", "/actions/execute", body, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
type CatalogEntry struct {
|
||||
ActionID string `json:"action_id"`
|
||||
Layer string `json:"layer"`
|
||||
Exec bool `json:"harness_executes"`
|
||||
}
|
||||
|
||||
func (c *Client) Catalog(ctx context.Context) ([]CatalogEntry, error) {
|
||||
var out []CatalogEntry
|
||||
_, err := c.call(ctx, "GET", "/actions/catalog", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
type Scenario struct {
|
||||
ID string `json:"scenario_id"`
|
||||
Label string `json:"ground_truth_label"`
|
||||
}
|
||||
|
||||
func (c *Client) Scenarios(ctx context.Context) ([]Scenario, error) {
|
||||
var out []Scenario
|
||||
_, err := c.call(ctx, "GET", "/scenarios", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "..."
|
||||
}
|
||||
Reference in new issue
Block a user