diff --git a/backend/internal/service/basispoints/README.md b/backend/internal/service/basispoints/README.md index 16664e5acdc2..ae17e8f663e0 100644 --- a/backend/internal/service/basispoints/README.md +++ b/backend/internal/service/basispoints/README.md @@ -223,3 +223,17 @@ these ceilings are not throughput or memory-use guarantees. - Failure/cancellation retains usage and withholds unvalidated tools. Readers stop at cancellation without waiting for EOF, including correction and image compaction readers. This does not add a BPS WebSocket transport. + +# Context policy and current encrypted input + +The bridge forwards an explicitly supplied `context_management` array, including +an empty array, without injecting a fixed compaction threshold when it is absent. +Upstream defaults then apply. Clients that require the former 920000 threshold +can declare it explicitly. This does not extend a model's context window. + +The optional encrypted-history omission path never replaces the last meaningful +message or tool result's encrypted payload with a placeholder. Trailing reasoning, +additional tool declarations, tool-search output and compaction triggers do not +make that current input historical. Such requests fail with a safe input path and +require the original plaintext. Older encrypted history may still use the existing +explicit omission option; this does not recover or decrypt its missing content. diff --git a/backend/internal/service/basispoints/agent_assignment.go b/backend/internal/service/basispoints/agent_assignment.go index 6bdfc6544119..28fdacf2a6f2 100644 --- a/backend/internal/service/basispoints/agent_assignment.go +++ b/backend/internal/service/basispoints/agent_assignment.go @@ -20,17 +20,15 @@ func encryptedAgentPath(item object, index int) string { // substitute for its unreadable payload, even if a readable header is present. func validateNewAgentMessage(input any) error { items, _ := input.([]any) - for index := len(items) - 1; index >= 0; index-- { - item, _ := items[index].(object) - switch text(item["type"]) { - case "additional_tools", "tool_search_output", "compaction_trigger": - continue - case "agent_message": - if path := encryptedAgentPath(item, index); path != "" { - return &ContentValidationError{Path: path, ContentType: "encrypted_content", message: "basispoints cannot read a new encrypted agent assignment; resend the task using the original plaintext collaboration path"} - } + index := lastMeaningfulInput(items) + if index < 0 { + return nil + } + item, _ := items[index].(object) + if text(item["type"]) == "agent_message" { + if path := encryptedAgentPath(item, index); path != "" { + return &ContentValidationError{Path: path, ContentType: "encrypted_content", message: "basispoints cannot read a new encrypted agent assignment; resend the task using the original plaintext collaboration path"} } - break } return nil } @@ -74,3 +72,16 @@ func plaintextPromptParameters(name, namespace string, parameters object) object result["properties"] = props return result } + +// Metadata and reasoning tails do not turn the current assignment into history. +func lastMeaningfulInput(items []any) int { + for index := len(items) - 1; index >= 0; index-- { + item, _ := items[index].(object) + switch text(item["type"]) { + case "additional_tools", "tool_search_output", "compaction_trigger", "reasoning": + continue + } + return index + } + return -1 +} diff --git a/backend/internal/service/basispoints/agent_assignment_test.go b/backend/internal/service/basispoints/agent_assignment_test.go index 663c7d3ccd0a..9c0f7b87270d 100644 --- a/backend/internal/service/basispoints/agent_assignment_test.go +++ b/backend/internal/service/basispoints/agent_assignment_test.go @@ -8,7 +8,7 @@ import ( func TestNewEncryptedAssignmentsFailBeforeOmission(t *testing.T) { for _, top := range []bool{false, true} { - for _, tail := range []string{"", "additional_tools", "tool_search_output", "compaction_trigger"} { + for _, tail := range []string{"", "additional_tools", "tool_search_output", "compaction_trigger", "reasoning"} { item := object{"type": "agent_message", "content": []any{object{"type": "input_text", "text": "Readable header"}}} if top { item["encrypted_content"] = "SYNTHETIC_PRIVATE_PAYLOAD" diff --git a/backend/internal/service/basispoints/basispoints_test.go b/backend/internal/service/basispoints/basispoints_test.go index 8d4fba70f8da..1c1b6c6f0043 100644 --- a/backend/internal/service/basispoints/basispoints_test.go +++ b/backend/internal/service/basispoints/basispoints_test.go @@ -71,13 +71,8 @@ func TestPrepareWireShapeAndDeterminism(t *testing.T) { if bridge.RequestedEffort != "max" || bridge.Effort != "xhigh" { t.Fatal("requested and effective effort must remain distinguishable") } - management, ok := first["context_management"].([]any) - if !ok || len(management) != 1 { - t.Fatal("default context compaction contract missing") - } - entry, _ := management[0].(object) - if entry["type"] != "compaction" || fmt.Sprint(entry["compact_threshold"]) != "920000" { - t.Fatalf("unexpected default compaction threshold: %+v", entry) + if _, exists := first["context_management"]; exists { + t.Fatal("absent client compaction policy must not inject a fixed threshold") } for _, field := range []string{"tools", "reasoning", "include", "service_tier", "instructions"} { if _, exists := first[field]; exists { diff --git a/backend/internal/service/basispoints/context_boundaries_test.go b/backend/internal/service/basispoints/context_boundaries_test.go new file mode 100644 index 000000000000..5ffccf2809e2 --- /dev/null +++ b/backend/internal/service/basispoints/context_boundaries_test.go @@ -0,0 +1,56 @@ +package basispoints + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCurrentEncryptedInputCannotBeOmitted(t *testing.T) { + for _, kind := range []string{"message", "agent_message", "function_call_output", "custom_tool_call_output"} { + for _, tail := range []string{"", "reasoning", "additional_tools", "tool_search_output", "compaction_trigger"} { + t.Run(kind+"/"+tail, func(t *testing.T) { + field := "content" + if kind == "function_call_output" || kind == "custom_tool_call_output" { + field = "output" + } + item := object{"type": kind, "role": "user", field: []any{object{"type": "input_text", "text": "Header"}, object{"type": "encrypted_content", "encrypted_content": "PRIVATE_TASK"}}} + input := []any{message("user", "Previous task"), item} + if tail != "" { + input = append(input, object{"type": tail}) + } + raw, err := json.Marshal(object{"input": input}) + require.NoError(t, err) + _, err = StripEncryptedContent(raw) + require.Error(t, err) + require.Contains(t, err.Error(), "input[1]."+field+"[1]") + require.NotContains(t, err.Error(), "PRIVATE_TASK") + // A later plaintext instruction makes the same item historical. + input = append(input, message("user", "New plaintext task")) + raw, err = json.Marshal(object{"input": input}) + require.NoError(t, err) + stripped, err := StripEncryptedContent(raw) + require.NoError(t, err) + require.NotContains(t, string(stripped), "PRIVATE_TASK") + require.Contains(t, string(stripped), encryptedContentOmitted) + }) + } + } +} + +func TestCompactionPolicyOnlyUsesClientDeclaration(t *testing.T) { + for _, policy := range []any{[]any{}, []any{object{"type": "compaction", "compact_threshold": 200000}}, []any{object{"type": "compaction", "compact_threshold": 920000}}} { + source := testSource() + source["context_management"] = policy + wire, _ := mustPrepare(t, source, "context", nil) + want, err := json.Marshal(policy) + require.NoError(t, err) + got, err := json.Marshal(wire["context_management"]) + require.NoError(t, err) + require.JSONEq(t, string(want), string(got)) + } + source := testSource() + wire, _ := mustPrepare(t, source, "context", nil) + require.NotContains(t, wire, "context_management") +} diff --git a/backend/internal/service/basispoints/encrypted_content.go b/backend/internal/service/basispoints/encrypted_content.go index af32d2434979..c6fee0212c80 100644 --- a/backend/internal/service/basispoints/encrypted_content.go +++ b/backend/internal/service/basispoints/encrypted_content.go @@ -22,8 +22,9 @@ func StripEncryptedContent(raw []byte) ([]byte, error) { return nil, err } input, _ := source["input"].([]any) + current := lastMeaningfulInput(input) changed := false - for _, rawItem := range input { + for itemIndex, rawItem := range input { item, _ := rawItem.(object) field := "content" switch text(item["type"]) { @@ -35,6 +36,9 @@ func StripEncryptedContent(raw []byte) ([]byte, error) { } if text(item["type"]) == "agent_message" { if value, exists := item["encrypted_content"]; exists && value != nil && value != "" { + if itemIndex == current { + return nil, currentEncryptedInputError(fmt.Sprintf("input[%d].encrypted_content", itemIndex)) + } marker := object{"type": "input_text", "text": encryptedContentOmitted} switch content := item["content"].(type) { case []any: @@ -58,6 +62,9 @@ func StripEncryptedContent(raw []byte) ([]byte, error) { } for index, rawPart := range parts { if part, _ := rawPart.(object); text(part["type"]) == "encrypted_content" { + if itemIndex == current { + return nil, currentEncryptedInputError(fmt.Sprintf("input[%d].%s[%d]", itemIndex, field, index)) + } parts[index] = object{"type": kind, "text": encryptedContentOmitted} changed = true } @@ -68,3 +75,7 @@ func StripEncryptedContent(raw []byte) ([]byte, error) { } return json.Marshal(source) } + +func currentEncryptedInputError(path string) error { + return &ContentValidationError{Path: path, ContentType: "encrypted_content", message: "basispoints cannot omit encrypted content from the current task; resend the original plaintext"} +} diff --git a/backend/internal/service/basispoints/request.go b/backend/internal/service/basispoints/request.go index dd69b434664b..f6fc34785632 100644 --- a/backend/internal/service/basispoints/request.go +++ b/backend/internal/service/basispoints/request.go @@ -244,7 +244,6 @@ func prepareWithInheritedCatalog(raw []byte, scope string, replay *ReplayCache, output := object{ "model": model, "model_selection": "explicit", "stream": true, "store": false, "input": append(prologue, translated...), "reasoning_effort": effort, - "context_management": []any{object{"type": "compaction", "compact_threshold": 920000}}, "metadata": object{ "task_id": fingerprint([]any{scope, conversation}), "turn_id": fingerprint([]any{scope, input[:turnEnd]}), "agent_iteration": fmt.Sprint(iteration),