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
14 changes: 14 additions & 0 deletions backend/internal/service/basispoints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 21 additions & 10 deletions backend/internal/service/basispoints/agent_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 2 additions & 7 deletions backend/internal/service/basispoints/basispoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
56 changes: 56 additions & 0 deletions backend/internal/service/basispoints/context_boundaries_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
13 changes: 12 additions & 1 deletion backend/internal/service/basispoints/encrypted_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) {
Expand All @@ -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:
Expand All @@ -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
}
Expand All @@ -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"}
}
1 change: 0 additions & 1 deletion backend/internal/service/basispoints/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading