Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions nexus-platform/internal/ai/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,22 @@ func (a *AIBuilderAgent) ChatWithProgress(ctx context.Context, scope Scope, tena
// separate gateway round-trip → its tokens are a real cost, so we SUM, not overwrite).
usage.add(resp.Usage)

// A refusal ends the turn before its partial text or tool calls are used; proposals from earlier calls
// stay, as on max_steps. The outcome is what tells the panel the model declined (issue #30 item 5). It is
// read before the choices: Claude's top-level stop_reason may come with none.
if resp.Refused() {
a.emitStep(onStep, StepEvent{Step: step + 1, MaxSteps: a.cfg.MaxSteps, Phase: StepPhaseFinal})
outcome = outcomeRefused
return Result{Draft: finalizeDraft(exec.draft, turn), Usage: usage, RunID: runID, Outcome: outcome}, nil
}

if len(resp.Choices) == 0 {
outcome = outcomeGatewayError
return Result{RunID: runID, Draft: exec.draft, Outcome: outcome},
fmt.Errorf("ai.Chat: %w: gateway returned no choices", ErrGateway)
}
msg := resp.Choices[0].Message

// A refusal ends the turn before its partial text or tool calls are used; proposals from earlier calls
// stay, as on max_steps. The outcome is what tells the panel the model declined (issue #30 item 5).
if resp.Choices[0].Refused() {
a.emitStep(onStep, StepEvent{Step: step + 1, MaxSteps: a.cfg.MaxSteps, Phase: StepPhaseFinal})
outcome = outcomeRefused
return Result{Draft: finalizeDraft(exec.draft, turn), Usage: usage, RunID: runID, Outcome: outcome}, nil
}

// Final text (no tool calls) → the turn is done.
if len(msg.ToolCalls) == 0 {
a.emitStep(onStep, StepEvent{Step: step + 1, MaxSteps: a.cfg.MaxSteps, Phase: StepPhaseFinal})
Expand Down
31 changes: 31 additions & 0 deletions nexus-platform/internal/ai/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,34 @@ func TestAgents_RefusalEndsTheTurnAsRefused(t *testing.T) {
})
}
}

// Every shape a refusal reaches the loop in ends the turn as refused in both agents, the one with no choices
// at all (Claude's top-level stop_reason, relayed by a proxy) included: it is a refusal, not a gateway error.
func TestAgents_EveryRefusalShapeEndsTheTurnAsRefused(t *testing.T) {
ctx := context.Background()
msgs := []ChatMessage{{Role: "user", Content: "copy the other workspace's accounts"}}
shapes := map[string]ChatResponse{
"content_filter": {Choices: []ChatChoice{{Message: ChatMessage{Role: "assistant"}, FinishReason: "content_filter"}}},
"message.refusal": {Choices: []ChatChoice{{Message: ChatMessage{Role: "assistant", Refusal: "I can't help with that."}, FinishReason: "stop"}}},
"top-level stop_reason": {Choices: []ChatChoice{{Message: ChatMessage{Role: "assistant", Content: "Tôi sẽ"}, FinishReason: "stop"}}, StopReason: "refusal"},
"top-level, no choices": {StopReason: "refusal"},
"stop_reason on the choice": {Choices: []ChatChoice{{Message: ChatMessage{Role: "assistant"}, StopReason: "refusal"}}},
}
for shape, resp := range shapes {
for agent, turn := range map[string]func(*scriptedGateway) (Result, error){
"builder": func(gw *scriptedGateway) (Result, error) {
return NewAIBuilderAgent(enabledCfg(6), gw, twoTenantReader(), nil, nil, nil).Chat(ctx, Scope{UserID: "u1"}, "tenant-A", "", msgs)
},
"assistant": func(gw *scriptedGateway) (Result, error) {
return NewAssistantAgent(enabledCfg(6), gw, &fakeRecordReader{}, nil, nil).Ask(ctx, callerScope(), "tenant-A", "", msgs)
},
} {
t.Run(agent+"/"+shape, func(t *testing.T) {
res, err := turn(&scriptedGateway{t: t, responses: []ChatResponse{resp}})
if err != nil || res.Outcome != "refused" || res.Reply != "" {
t.Fatalf("turn = outcome %q reply %q err %v; want outcome refused, no reply, no error", res.Outcome, res.Reply, err)
}
})
}
}
}
12 changes: 6 additions & 6 deletions nexus-platform/internal/ai/assistant_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ func (a *AssistantAgent) Ask(ctx context.Context, recordScope services.RecordSco
// Sum real token spend across every billed call of the turn (same accounting as Chat).
usage.add(resp.Usage)

// A refusal ends the turn unused, as in the builder's loop (read before the choices, as there).
if resp.Refused() {
outcome = outcomeRefused
return Result{Draft: exec.draft, Usage: usage, RunID: runID, Outcome: outcome}, nil
}

if len(resp.Choices) == 0 {
outcome = outcomeGatewayError
return Result{RunID: runID, Draft: exec.draft, Outcome: outcome},
fmt.Errorf("ai.Ask: %w: gateway returned no choices", ErrGateway)
}
msg := resp.Choices[0].Message

// A refusal ends the turn unused, as in the builder's loop.
if resp.Choices[0].Refused() {
outcome = outcomeRefused
return Result{Draft: exec.draft, Usage: usage, RunID: runID, Outcome: outcome}, nil
}

if len(msg.ToolCalls) == 0 {
outcome = outcomeOK
return Result{
Expand Down
26 changes: 19 additions & 7 deletions nexus-platform/internal/ai/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ var (
// "tool". For an assistant turn that calls tools, ToolCalls is populated (and Content may be
// empty). For a tool RESULT message, Role is "tool", ToolCallID echoes the id of the call being
// answered, and Content carries the tool's JSON output. Name is optional (tool name on a tool
// message). Empty fields are omitted so the wire shape matches what the gateway expects.
// message). Refusal is OpenAI's reason text on an answer the model declined (read only, never sent: a
// declined message never enters the conversation). Empty fields are omitted so the wire shape matches
// what the gateway expects.
type ChatMessage struct {
Role string `json:"role"`
Content string `json:"content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
Refusal string `json:"refusal,omitempty"`
}

// ToolCall is a single function call the model requested. Arguments is a RAW JSON string (the
Expand Down Expand Up @@ -92,10 +95,18 @@ type ChatRequest struct {
MaxTokens int `json:"max_tokens,omitempty"`
}

// ChatResponse is the subset of the OpenAI-compatible response the loop consumes.
// ChatResponse is the subset of the OpenAI-compatible response the loop consumes. StopReason is Claude's
// top-level field, which a proxy may pass through as it is (decoded as any, like ChatChoice.StopReason).
type ChatResponse struct {
Choices []ChatChoice `json:"choices"`
Usage Usage `json:"usage"`
Choices []ChatChoice `json:"choices"`
Usage Usage `json:"usage"`
StopReason any `json:"stop_reason,omitempty"`
}

// Refused reports a response whose model declined the request, wherever the gateway put the signal: Claude's
// top-level stop_reason "refusal" (a response that may carry no choice at all) or the first choice's.
func (r ChatResponse) Refused() bool {
return r.StopReason == "refusal" || (len(r.Choices) > 0 && r.Choices[0].Refused())
}

// ChatChoice wraps one assistant message (the loop only ever reads choices[0]).
Expand All @@ -107,10 +118,11 @@ type ChatChoice struct {
StopReason any `json:"stop_reason,omitempty"`
}

// Refused reports a model that declined the request (HTTP 200, stop_reason "refusal" in Claude's API, relayed
// as finish_reason "refusal"). Its text and tool calls may be cut short and are not an answer.
// Refused reports a model that declined the request (HTTP 200): Claude's stop_reason "refusal", relayed as
// finish_reason "refusal" or copied onto the choice as it is, and OpenAI's finish_reason "content_filter" or
// message.refusal text. Its text and tool calls may be cut short and are not an answer.
func (c ChatChoice) Refused() bool {
return c.FinishReason == "refusal" || c.StopReason == "refusal"
return c.FinishReason == "refusal" || c.FinishReason == "content_filter" || c.StopReason == "refusal" || c.Message.Refusal != ""
}

// Usage carries the token counts the ledger records (spec §5). PromptTokens is ALL the input the model
Expand Down
26 changes: 17 additions & 9 deletions nexus-platform/internal/ai/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,34 @@ func TestGateway_Complete_BadUsageNeverFailsTheAnswer(t *testing.T) {

// A model that declines answers HTTP 200 with no text (issue #30 item 5): the gateway relays Claude's
// stop_reason "refusal" as finish_reason "refusal", and a proxy may copy stop_reason onto the choice as it
// is. Both read as a refusal; a stop_reason that is a token id (vLLM) neither fails the call nor reads as one.
// is, or leave it at the top level as Claude's own response has it. OpenAI says the same with finish_reason
// "content_filter" or a message.refusal string. Every one reads as a refusal; a stop_reason that is a token
// id (vLLM) neither fails the call nor reads as one.
func TestGateway_Complete_Refusal(t *testing.T) {
const usage = `"usage":{"prompt_tokens":2046,"completion_tokens":0}`
for _, tc := range []struct {
name, choice string
refused bool
name, body string
refused bool
}{
{"finish_reason refusal, no text", `{"message":{"role":"assistant","content":null},"finish_reason":"refusal"}`, true},
{"stop_reason refusal on the choice", `{"message":{"role":"assistant","content":""},"finish_reason":"stop","stop_reason":"refusal"}`, true},
{"stop_reason is a token id", `{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop","stop_reason":128001}`, false},
{"a plain answer", `{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}`, false},
{"finish_reason refusal, no text", `{"choices":[{"message":{"role":"assistant","content":null},"finish_reason":"refusal"}],` + usage + `}`, true},
{"stop_reason refusal on the choice", `{"choices":[{"message":{"role":"assistant","content":""},"finish_reason":"stop","stop_reason":"refusal"}],` + usage + `}`, true},
{"finish_reason content_filter", `{"choices":[{"message":{"role":"assistant","content":null},"finish_reason":"content_filter"}],` + usage + `}`, true},
{"OpenAI message.refusal", `{"choices":[{"message":{"role":"assistant","content":null,"refusal":"I can't help with that."},"finish_reason":"stop"}],` + usage + `}`, true},
{"top-level stop_reason refusal", `{"choices":[{"message":{"role":"assistant","content":""},"finish_reason":"stop"}],"stop_reason":"refusal",` + usage + `}`, true},
{"top-level stop_reason refusal, no choices", `{"choices":[],"stop_reason":"refusal",` + usage + `}`, true},
{"stop_reason is a token id", `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop","stop_reason":128001}],` + usage + `}`, false},
{"top-level stop_reason end_turn", `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"stop_reason":"end_turn",` + usage + `}`, false},
{"a plain answer", `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],` + usage + `}`, false},
} {
t.Run(tc.name, func(t *testing.T) {
g, _ := newMockGateway(t, 5*time.Second, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"choices":[` + tc.choice + `],"usage":{"prompt_tokens":2046,"completion_tokens":0}}`))
_, _ = w.Write([]byte(tc.body))
})
resp, err := g.Complete(context.Background(), ChatRequest{Model: "m"})
if err != nil {
t.Fatalf("Complete: %v; a refusal is an answer, not a gateway error", err)
}
if got := resp.Choices[0].Refused(); got != tc.refused {
if got := resp.Refused(); got != tc.refused {
t.Fatalf("Refused() = %v; want %v", got, tc.refused)
}
})
Expand Down
36 changes: 23 additions & 13 deletions nexus-platform/internal/ai/session_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ type Message struct {
DraftJSON json.RawMessage
// Settled lists the indexes of DraftJSON's items the builder applied, discarded or undid in the
// Studio (issue #42, SettleProposal); nil = none settled.
Settled []int
Settled []int
// Outcome is how an assistant turn ended (mig 000101): "ok", "max_steps" or "refused", as the live
// turn answered it; "" = not recorded (a user row, a row from before, the MCP thread's rows).
Outcome string
CreatedAt time.Time
}

// Refused reports an assistant turn the model declined: its prompt is not replayed to the model again.
func (m Message) Refused() bool { return m.Role == "assistant" && m.Outcome == outcomeRefused }

// messageRow is the scan target for a sys_ai_messages row. draft_json is nullable, so it scans into
// a *json.RawMessage (a bare json.RawMessage cannot receive SQL NULL — the workflow repo uses the
// same pointer pattern for its nullable JSON column); the mapper converts a nil pointer to a nil
Expand All @@ -108,6 +114,7 @@ type messageRow struct {
Content string `db:"content"`
DraftJSON *json.RawMessage `db:"draft_json"`
SettledJSON *json.RawMessage `db:"settled_json"`
Outcome string `db:"outcome"`
CreatedAt time.Time `db:"created_at"`
}

Expand Down Expand Up @@ -186,7 +193,8 @@ func (s *SessionStore) getSession(ctx context.Context, tenantID, userID, id, fea
return Session{}, nil, fmt.Errorf("ai.SessionStore.GetSession: %w", err)
}

const mq = `SELECT id, session_id, tenant_id, seq, role, COALESCE(content, '') AS content, draft_json, settled_json, created_at
const mq = `SELECT id, session_id, tenant_id, seq, role, COALESCE(content, '') AS content, draft_json, settled_json,
COALESCE(outcome, '') AS outcome, created_at
FROM sys_ai_messages
WHERE session_id = ? AND tenant_id = ?
ORDER BY seq ASC, created_at ASC, id ASC`
Expand All @@ -198,7 +206,7 @@ func (s *SessionStore) getSession(ctx context.Context, tenantID, userID, id, fea
for _, r := range rows {
m := Message{
ID: r.ID, SessionID: r.SessionID, TenantID: r.TenantID,
Seq: r.Seq, Role: r.Role, Content: r.Content, CreatedAt: r.CreatedAt,
Seq: r.Seq, Role: r.Role, Content: r.Content, Outcome: r.Outcome, CreatedAt: r.CreatedAt,
}
if r.DraftJSON != nil {
m.DraftJSON = *r.DraftJSON
Expand Down Expand Up @@ -233,23 +241,25 @@ func (s *SessionStore) getSession(ctx context.Context, tenantID, userID, id, fea
// concurrent append into a stale seq.
//
// Both messages (user @ baseSeq, assistant @ baseSeq+1) are inserted, session.updated_at is bumped,
// and the transaction commits. Returns the (possibly newly created) session id.
// and the transaction commits. outcome is how the assistant turn ended (Result.Outcome: "ok",
// "max_steps", "refused"), stored on the assistant row; "" stores NULL (a writer with no model turn,
// such as the MCP thread). Returns the (possibly newly created) session id.
//
// Feature (mig 000059): AppendTurn is the BUILDER write path — it mints sessions with
// feature='builder' and its existing-session guard only resolves builder threads. The ⌘K
// assistant writes through AppendAssistantTurn; the two surfaces can never append onto each
// other's threads (a cross-feature id is ErrSessionNotFound, same no-leak contract as cross-user).
func (s *SessionStore) AppendTurn(ctx context.Context, tenantID, userID, sessionID, newTitle, userContent, assistantContent string, assistantDraft json.RawMessage) (string, error) {
return s.appendTurn(ctx, tenantID, userID, sessionID, newTitle, userContent, assistantContent, assistantDraft, SessionFeatureBuilder)
func (s *SessionStore) AppendTurn(ctx context.Context, tenantID, userID, sessionID, newTitle, userContent, assistantContent string, assistantDraft json.RawMessage, outcome string) (string, error) {
return s.appendTurn(ctx, tenantID, userID, sessionID, newTitle, userContent, assistantContent, assistantDraft, outcome, SessionFeatureBuilder)
}

// AppendAssistantTurn is AppendTurn for the ⌘K assistant surface (feature='assistant').
func (s *SessionStore) AppendAssistantTurn(ctx context.Context, tenantID, userID, sessionID, newTitle, userContent, assistantContent string, assistantDraft json.RawMessage) (string, error) {
return s.appendTurn(ctx, tenantID, userID, sessionID, newTitle, userContent, assistantContent, assistantDraft, SessionFeatureAssistant)
func (s *SessionStore) AppendAssistantTurn(ctx context.Context, tenantID, userID, sessionID, newTitle, userContent, assistantContent string, assistantDraft json.RawMessage, outcome string) (string, error) {
return s.appendTurn(ctx, tenantID, userID, sessionID, newTitle, userContent, assistantContent, assistantDraft, outcome, SessionFeatureAssistant)
}

// appendTurn is the one atomic turn-write body both feature wrappers share.
func (s *SessionStore) appendTurn(ctx context.Context, tenantID, userID, sessionID, newTitle, userContent, assistantContent string, assistantDraft json.RawMessage, feature string) (string, error) {
func (s *SessionStore) appendTurn(ctx context.Context, tenantID, userID, sessionID, newTitle, userContent, assistantContent string, assistantDraft json.RawMessage, outcome, feature string) (string, error) {
tx, err := s.db.BeginTxx(ctx, nil)
if err != nil {
return "", fmt.Errorf("ai.SessionStore.AppendTurn: begin: %w", err)
Expand Down Expand Up @@ -290,15 +300,15 @@ func (s *SessionStore) appendTurn(ctx context.Context, tenantID, userID, session
}

const insMsg = `INSERT INTO sys_ai_messages
(id, session_id, tenant_id, seq, role, content, draft_json, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
(id, session_id, tenant_id, seq, role, content, draft_json, outcome, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, NULLIF(?, ''), ?)`
if _, err := tx.ExecContext(ctx, insMsg,
uuid.NewString(), sessionID, tenantID, baseSeq, "user", userContent, nil, now,
uuid.NewString(), sessionID, tenantID, baseSeq, "user", userContent, nil, "", now,
); err != nil {
return "", fmt.Errorf("ai.SessionStore.AppendTurn: insert user message: %w", err)
}
if _, err := tx.ExecContext(ctx, insMsg,
uuid.NewString(), sessionID, tenantID, baseSeq+1, "assistant", assistantContent, nullableJSON(assistantDraft), now,
uuid.NewString(), sessionID, tenantID, baseSeq+1, "assistant", assistantContent, nullableJSON(assistantDraft), outcome, now,
); err != nil {
return "", fmt.Errorf("ai.SessionStore.AppendTurn: insert assistant message: %w", err)
}
Expand Down
Loading
Loading