源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
This commit is contained in:
commit
2ed3415fb7
8018 files changed
+1936476
No files matched your search
@@ -0,0 +1,113 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
type editRetryRuntimeBackend struct {
|
||||
RuntimeBackend
|
||||
execInput agentruntime.ExecInput
|
||||
historyInput agentruntime.EffectiveHistoryInput
|
||||
rollbackInput agentruntime.EffectiveHistoryInput
|
||||
}
|
||||
|
||||
func (backend *editRetryRuntimeBackend) Exec(
|
||||
_ context.Context,
|
||||
input agentruntime.ExecInput,
|
||||
) (agentruntime.ExecResult, error) {
|
||||
backend.execInput = input
|
||||
return agentruntime.ExecResult{
|
||||
ProviderDispatch: &agentruntime.ProviderDispatchResult{
|
||||
Disposition: agentruntime.DispatchDispositionApplied,
|
||||
Acceptance: &agentruntime.ProviderAcceptanceReceipt{
|
||||
Source: agentruntime.AcceptanceSourceTurnStartResponse,
|
||||
ProviderSessionID: "provider-session-1",
|
||||
ProviderTurnID: "provider-turn-1",
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (*editRetryRuntimeBackend) SupportsEffectiveHistory(
|
||||
context.Context,
|
||||
agentruntime.EffectiveHistoryInput,
|
||||
) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (backend *editRetryRuntimeBackend) ReadEffectiveHistory(
|
||||
_ context.Context,
|
||||
input agentruntime.EffectiveHistoryInput,
|
||||
) (agentruntime.EffectiveHistorySnapshot, error) {
|
||||
backend.historyInput = input
|
||||
return agentruntime.EffectiveHistorySnapshot{
|
||||
ProviderSessionID: "provider-session-1",
|
||||
Turns: []agentruntime.EffectiveHistoryTurn{{
|
||||
ID: "provider-turn-1", Status: "completed",
|
||||
ClientUserMessageID: "replacement-submit-1",
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (backend *editRetryRuntimeBackend) RollbackLatestTurn(
|
||||
_ context.Context,
|
||||
input agentruntime.EffectiveHistoryInput,
|
||||
) (agentruntime.HistoryMutationResult, error) {
|
||||
backend.rollbackInput = input
|
||||
snapshot := agentruntime.EffectiveHistorySnapshot{
|
||||
ProviderSessionID: "provider-session-1",
|
||||
}
|
||||
return agentruntime.HistoryMutationResult{
|
||||
Disposition: agentruntime.DispatchDispositionApplied,
|
||||
Snapshot: &snapshot,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestRuntimeControllerProjectsEditRetryProviderContracts(t *testing.T) {
|
||||
backend := &editRetryRuntimeBackend{}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
|
||||
exec, err := controller.Exec(t.Context(), host.RuntimeExecInput{
|
||||
WorkspaceID: "workspace-1", AgentSessionID: "session-1",
|
||||
TurnID: "replacement-turn-1", ClientSubmitID: "replacement-submit-1",
|
||||
HistoryReplacement: true, RequireProviderAcceptance: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !backend.execInput.HistoryReplacement ||
|
||||
exec.ProviderDispatch.Disposition != host.RuntimeDispatchDispositionApplied ||
|
||||
exec.ProviderDispatch.Acceptance == nil ||
|
||||
exec.ProviderDispatch.Acceptance.Source != host.RuntimeAcceptanceSourceTurnStartResponse ||
|
||||
exec.ProviderDispatch.Acceptance.ProviderTurnID != "provider-turn-1" {
|
||||
t.Fatalf("exec input=%#v result=%#v", backend.execInput, exec)
|
||||
}
|
||||
|
||||
historyInput := host.RuntimeHistoryInput{
|
||||
WorkspaceID: "workspace-1", AgentSessionID: "session-1", Provider: "codex",
|
||||
}
|
||||
history, err := controller.ReadEffectiveHistory(t.Context(), historyInput)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if backend.historyInput.RoomID != historyInput.WorkspaceID ||
|
||||
len(history.Turns) != 1 ||
|
||||
history.Turns[0].ClientUserMessageID != "replacement-submit-1" {
|
||||
t.Fatalf("history input=%#v result=%#v", backend.historyInput, history)
|
||||
}
|
||||
|
||||
rollback, err := controller.RollbackLatestTurn(t.Context(), historyInput)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if backend.rollbackInput.AgentSessionID != historyInput.AgentSessionID ||
|
||||
rollback.Disposition != host.RuntimeDispatchDispositionApplied ||
|
||||
rollback.Snapshot == nil ||
|
||||
rollback.Snapshot.ProviderSessionID != "provider-session-1" {
|
||||
t.Fatalf("rollback input=%#v result=%#v", backend.rollbackInput, rollback)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
type goalControlLifecycleBackend interface {
|
||||
SetGoalControlLifecycleObserver(agentruntime.GoalControlLifecycleObserver)
|
||||
}
|
||||
|
||||
type goalControlLifecycleObserver struct {
|
||||
sink host.RuntimeGoalControlAppliedSink
|
||||
}
|
||||
|
||||
func (o goalControlLifecycleObserver) ObserveGoalControlApplied(
|
||||
ctx context.Context,
|
||||
observation agentruntime.GoalControlAppliedObservation,
|
||||
) error {
|
||||
if o.sink == nil {
|
||||
return nil
|
||||
}
|
||||
return o.sink(ctx, host.RuntimeGoalControlAppliedInput{
|
||||
WorkspaceID: observation.WorkspaceID, AgentSessionID: observation.AgentSessionID,
|
||||
OperationID: observation.OperationID, GoalRevision: observation.Revision,
|
||||
RepairEpoch: observation.RepairEpoch, Action: observation.Action,
|
||||
ProviderTurnID: observation.ProviderTurnID, Observed: observation.Observed,
|
||||
OccurredAtUnixMS: observation.OccurredAtUnixMS,
|
||||
ExecutionPending: observation.ExecutionPending,
|
||||
})
|
||||
}
|
||||
|
||||
func (a *RuntimeController) SetGoalControlAppliedSink(sink host.RuntimeGoalControlAppliedSink) {
|
||||
if a == nil {
|
||||
return
|
||||
}
|
||||
backend, ok := a.Backend.(goalControlLifecycleBackend)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
backend.SetGoalControlLifecycleObserver(goalControlLifecycleObserver{sink: sink})
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
func runtimeMCPServerBindings(input []host.MCPServerBinding) []agentruntime.MCPServerBinding {
|
||||
if len(input) == 0 {
|
||||
return nil
|
||||
}
|
||||
result := make([]agentruntime.MCPServerBinding, 0, len(input))
|
||||
for _, binding := range input {
|
||||
headers := make(map[string]string, len(binding.Headers))
|
||||
for key, value := range binding.Headers {
|
||||
headers[key] = value
|
||||
}
|
||||
result = append(result, agentruntime.MCPServerBinding{Name: binding.Name, Type: binding.Type, URL: binding.URL, Headers: headers})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func hostMCPServerBindings(input []agentruntime.MCPServerBinding) []host.MCPServerBinding {
|
||||
if len(input) == 0 {
|
||||
return nil
|
||||
}
|
||||
result := make([]host.MCPServerBinding, 0, len(input))
|
||||
for _, binding := range input {
|
||||
headers := make(map[string]string, len(binding.Headers))
|
||||
for key, value := range binding.Headers {
|
||||
headers[key] = value
|
||||
}
|
||||
result = append(result, host.MCPServerBinding{Name: binding.Name, Type: binding.Type, URL: binding.URL, Headers: headers})
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,858 @@
|
||||
// Package hostadapter adapts the daemon runtime contract to Agent Host.
|
||||
//
|
||||
// Both sides of this boundary are owned by Tutti. Product services should
|
||||
// provide only the concrete runtime backend and current-user identity instead
|
||||
// of maintaining their own lifecycle and error mappings.
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
"github.com/tutti-os/tutti/packages/agent/store-sqlite/canonical"
|
||||
)
|
||||
|
||||
// RuntimeBackend is the daemon controller surface required by Agent Host.
|
||||
// *runtime.Controller implements this interface.
|
||||
type RuntimeBackend interface {
|
||||
Start(context.Context, agentruntime.StartInput) (agentruntime.StartResult, error)
|
||||
PublishSessionInitialization(context.Context, string, string) (agentruntime.Session, error)
|
||||
Resume(context.Context, agentruntime.ResumeInput) (agentruntime.Session, error)
|
||||
Session(string, string) (agentruntime.Session, bool)
|
||||
State(string, string) (agentruntime.SessionStateSnapshot, error)
|
||||
CanResume(agentruntime.ResumeInput) bool
|
||||
Exec(context.Context, agentruntime.ExecInput) (agentruntime.ExecResult, error)
|
||||
DurablyReportSubmitProvenance(context.Context, agentruntime.SubmitProvenanceInput) error
|
||||
ValidatePromptContent(context.Context, agentruntime.ExecInput) error
|
||||
Cancel(context.Context, agentruntime.CancelInput) (agentruntime.CancelResult, error)
|
||||
SubmitInteractive(context.Context, agentruntime.SubmitInteractiveInput) (agentruntime.SubmitInteractiveResult, error)
|
||||
InteractiveDisposition(string, string, string, string, string) agentruntime.InteractiveDisposition
|
||||
UpdateSettings(context.Context, agentruntime.UpdateSettingsInput) (agentruntime.UpdateSettingsResult, error)
|
||||
SetTitle(context.Context, string, string, string) (agentruntime.Session, error)
|
||||
SetVisible(context.Context, string, string, bool) (agentruntime.Session, error)
|
||||
Close(context.Context, agentruntime.CloseInput) (agentruntime.CloseResult, error)
|
||||
GoalControl(context.Context, agentruntime.GoalControlInput) (agentruntime.GoalControlResult, error)
|
||||
ReconcileGoal(context.Context, agentruntime.GoalReconcileInput) (agentruntime.GoalReconcileResult, error)
|
||||
GoalCapabilities(context.Context, agentruntime.GoalReconcileInput) (agentruntime.GoalAdapterCapabilities, error)
|
||||
}
|
||||
|
||||
type runtimeRetainedSettingsBackend interface {
|
||||
UpdateRetainedSettings(context.Context, agentruntime.UpdateSettingsInput) (agentruntime.UpdateSettingsResult, error)
|
||||
}
|
||||
|
||||
type runtimeHistoryBackend interface {
|
||||
SupportsEffectiveHistory(context.Context, agentruntime.EffectiveHistoryInput) (bool, error)
|
||||
ReadEffectiveHistory(context.Context, agentruntime.EffectiveHistoryInput) (agentruntime.EffectiveHistorySnapshot, error)
|
||||
RollbackLatestTurn(context.Context, agentruntime.EffectiveHistoryInput) (agentruntime.HistoryMutationResult, error)
|
||||
}
|
||||
|
||||
// RuntimeController implements Agent Host runtime ports with a daemon backend.
|
||||
type RuntimeController struct {
|
||||
Backend RuntimeBackend
|
||||
CurrentUserID func() string
|
||||
}
|
||||
|
||||
var (
|
||||
_ host.RuntimeController = (*RuntimeController)(nil)
|
||||
_ host.RuntimeSessionInitializationPublisher = (*RuntimeController)(nil)
|
||||
_ host.RuntimeSessionRepreparer = (*RuntimeController)(nil)
|
||||
_ host.RuntimeHistoryController = (*RuntimeController)(nil)
|
||||
_ host.RuntimeProviderTurnAcceptanceReconciler = (*RuntimeController)(nil)
|
||||
_ host.RuntimeSessionLiveness = (*RuntimeController)(nil)
|
||||
_ host.RuntimeWorkspaceDisconnector = (*RuntimeController)(nil)
|
||||
_ host.RuntimeWorkspaceDisconnectTargeter = (*RuntimeController)(nil)
|
||||
_ host.RuntimeRetainedSettingsUpdater = (*RuntimeController)(nil)
|
||||
_ host.RuntimeSubmitProvenanceReporter = (*RuntimeController)(nil)
|
||||
_ host.SessionForkRuntime = (*RuntimeController)(nil)
|
||||
_ host.SessionForkTurnBindingRecoveryRuntime = (*RuntimeController)(nil)
|
||||
_ host.SideConversationRuntime = (*RuntimeController)(nil)
|
||||
_ host.GoalRuntimeController = (*RuntimeController)(nil)
|
||||
_ host.GoalRuntimeControlLifecycleRegistrar = (*RuntimeController)(nil)
|
||||
_ host.GoalRuntimeReconciler = (*RuntimeController)(nil)
|
||||
_ host.GoalRuntimeRecoveryPolicyResolver = (*RuntimeController)(nil)
|
||||
_ host.GoalRuntimeGenerationFencer = (*RuntimeController)(nil)
|
||||
)
|
||||
|
||||
type runtimeSessionReprepareBackend interface {
|
||||
Reprepare(context.Context, agentruntime.ResumeInput) (agentruntime.Session, error)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) SupportsEffectiveHistory(
|
||||
ctx context.Context,
|
||||
input host.RuntimeHistoryInput,
|
||||
) (bool, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeHistoryBackend)
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
return backend.SupportsEffectiveHistory(ctx, runtimeHistoryInput(input))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ReadEffectiveHistory(
|
||||
ctx context.Context,
|
||||
input host.RuntimeHistoryInput,
|
||||
) (host.RuntimeHistorySnapshot, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeHistorySnapshot{}, err
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeHistoryBackend)
|
||||
if !ok {
|
||||
return host.RuntimeHistorySnapshot{}, host.ErrRuntimeHistoryUnsupported
|
||||
}
|
||||
snapshot, err := backend.ReadEffectiveHistory(ctx, runtimeHistoryInput(input))
|
||||
return hostHistorySnapshot(snapshot), mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) RollbackLatestTurn(
|
||||
ctx context.Context,
|
||||
input host.RuntimeHistoryInput,
|
||||
) (host.RuntimeHistoryMutationResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeHistoryMutationResult{
|
||||
Disposition: host.RuntimeDispatchDispositionNotDispatched,
|
||||
}, err
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeHistoryBackend)
|
||||
if !ok {
|
||||
return host.RuntimeHistoryMutationResult{
|
||||
Disposition: host.RuntimeDispatchDispositionNotDispatched,
|
||||
}, host.ErrRuntimeHistoryUnsupported
|
||||
}
|
||||
result, err := backend.RollbackLatestTurn(ctx, runtimeHistoryInput(input))
|
||||
projected := host.RuntimeHistoryMutationResult{
|
||||
Disposition: host.RuntimeDispatchDisposition(result.Disposition),
|
||||
}
|
||||
if result.Snapshot != nil {
|
||||
snapshot := hostHistorySnapshot(*result.Snapshot)
|
||||
projected.Snapshot = &snapshot
|
||||
}
|
||||
return projected, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Start(ctx context.Context, input host.RuntimeStartInput) (host.RuntimeStartResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeStartResult{}, err
|
||||
}
|
||||
result, err := a.Backend.Start(ctx, agentruntime.StartInput{
|
||||
RoomID: input.WorkspaceID,
|
||||
AgentSessionID: input.AgentSessionID,
|
||||
AgentTargetID: input.AgentTargetID,
|
||||
Provider: input.Provider,
|
||||
CWD: input.Cwd,
|
||||
Env: append([]string(nil), input.Env...),
|
||||
MCPServers: runtimeMCPServerBindings(input.MCPServers),
|
||||
Title: input.Title,
|
||||
InitialTitleEstablished: input.InitialTitleEstablished,
|
||||
Visible: input.Visible,
|
||||
RuntimeContext: cloneMap(input.RuntimeContext),
|
||||
ProviderTargetRef: cloneMap(input.ProviderTargetRef),
|
||||
PermissionModeID: input.PermissionModeID,
|
||||
Settings: runtimeSettings(host.ComposerSettings{
|
||||
CodexSaverMode: input.CodexSaverMode,
|
||||
RTKSaverMode: input.RTKSaverMode,
|
||||
Model: input.Model,
|
||||
PermissionModeID: input.PermissionModeID,
|
||||
PlanMode: input.PlanMode,
|
||||
BrowserUse: input.BrowserUse,
|
||||
ComputerUse: input.ComputerUse,
|
||||
ReasoningEffort: input.ReasoningEffort,
|
||||
Speed: input.Speed,
|
||||
ConversationDetailMode: input.ConversationDetailMode,
|
||||
}),
|
||||
Provisional: input.Provisional,
|
||||
CanonicalInitPending: input.CanonicalInitPending,
|
||||
})
|
||||
if err != nil {
|
||||
return host.RuntimeStartResult{}, mapRuntimeError(err)
|
||||
}
|
||||
session := a.sessionWithState(result.Session)
|
||||
session.Provisional = input.Provisional
|
||||
return host.RuntimeStartResult{Session: session, Created: result.Created}, nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) PublishSessionInitialization(
|
||||
ctx context.Context,
|
||||
input host.RuntimeSessionInitializationPublishInput,
|
||||
) (host.ProviderRuntimeSession, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.ProviderRuntimeSession{}, err
|
||||
}
|
||||
session, err := a.Backend.PublishSessionInitialization(
|
||||
ctx,
|
||||
input.WorkspaceID,
|
||||
input.AgentSessionID,
|
||||
)
|
||||
if err != nil {
|
||||
return host.ProviderRuntimeSession{}, mapRuntimeError(err)
|
||||
}
|
||||
return a.sessionWithState(session), nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Resume(ctx context.Context, input host.RuntimeResumeInput) (host.ProviderRuntimeSession, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.ProviderRuntimeSession{}, err
|
||||
}
|
||||
session, err := a.Backend.Resume(ctx, runtimeResumeInput(input))
|
||||
if err != nil {
|
||||
return host.ProviderRuntimeSession{}, mapRuntimeError(err)
|
||||
}
|
||||
return a.sessionWithState(session), nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Reprepare(ctx context.Context, input host.RuntimeResumeInput) (host.ProviderRuntimeSession, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.ProviderRuntimeSession{}, err
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeSessionReprepareBackend)
|
||||
if !ok {
|
||||
return host.ProviderRuntimeSession{}, host.ErrRuntimeSessionReprepareUnavailable
|
||||
}
|
||||
session, err := backend.Reprepare(ctx, runtimeResumeInput(input))
|
||||
if err != nil {
|
||||
return host.ProviderRuntimeSession{}, mapRuntimeError(err)
|
||||
}
|
||||
return a.sessionWithState(session), nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Session(workspaceID, sessionID string) (host.ProviderRuntimeSession, bool) {
|
||||
if a == nil || a.Backend == nil {
|
||||
return host.ProviderRuntimeSession{}, false
|
||||
}
|
||||
session, found := a.Backend.Session(workspaceID, sessionID)
|
||||
if !found {
|
||||
return host.ProviderRuntimeSession{}, false
|
||||
}
|
||||
return a.sessionWithState(session), true
|
||||
}
|
||||
|
||||
func (a *RuntimeController) RuntimeSessionLive(workspaceID, sessionID string) bool {
|
||||
if a == nil || a.Backend == nil {
|
||||
return false
|
||||
}
|
||||
liveness, ok := a.Backend.(interface {
|
||||
HasLiveSession(string, string) bool
|
||||
})
|
||||
return ok && liveness.HasLiveSession(strings.TrimSpace(workspaceID), strings.TrimSpace(sessionID))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) CanResume(input host.RuntimeResumeInput) bool {
|
||||
return a != nil && a.Backend != nil && a.Backend.CanResume(runtimeResumeInput(input))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Exec(ctx context.Context, input host.RuntimeExecInput) (host.RuntimeExecResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeExecResult{}, err
|
||||
}
|
||||
result, err := a.Backend.Exec(ctx, runtimeExecInput(input))
|
||||
projected := host.RuntimeExecResult{
|
||||
AgentSessionID: result.AgentSessionID,
|
||||
Status: result.Status,
|
||||
TurnID: result.TurnID,
|
||||
Accepted: result.Accepted,
|
||||
SessionStatus: result.SessionStatus,
|
||||
TurnLifecycle: hostTurnLifecycle(result.TurnLifecycle),
|
||||
SubmitAvailability: host.SubmitAvailability{
|
||||
State: result.SubmitAvailability.State, Reason: result.SubmitAvailability.Reason,
|
||||
},
|
||||
}
|
||||
if result.ProviderDispatch != nil {
|
||||
projected.ProviderDispatch.Disposition = host.RuntimeDispatchDisposition(
|
||||
result.ProviderDispatch.Disposition,
|
||||
)
|
||||
if result.ProviderDispatch.Acceptance != nil {
|
||||
projected.ProviderDispatch.Acceptance = &host.RuntimeProviderAcceptanceReceipt{
|
||||
ProviderSessionID: result.ProviderDispatch.Acceptance.ProviderSessionID,
|
||||
ProviderTurnID: result.ProviderDispatch.Acceptance.ProviderTurnID,
|
||||
Source: host.RuntimeAcceptanceSource(
|
||||
result.ProviderDispatch.Acceptance.Source,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
return projected, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) DurablyReportSubmitProvenance(ctx context.Context, input host.RuntimeSubmitProvenanceInput) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
return mapRuntimeError(a.Backend.DurablyReportSubmitProvenance(ctx, runtimeSubmitProvenanceInput(input)))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ReconcileProviderTurnAcceptance(
|
||||
ctx context.Context,
|
||||
input host.RuntimeProviderTurnAcceptanceInput,
|
||||
) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
reconciler, ok := a.Backend.(interface {
|
||||
ReconcileProviderTurnAcceptance(
|
||||
context.Context,
|
||||
agentruntime.ProviderTurnAcceptanceInput,
|
||||
) error
|
||||
})
|
||||
if !ok {
|
||||
return errors.New("agent runtime cannot reconcile provider turn acceptance")
|
||||
}
|
||||
return mapRuntimeError(reconciler.ReconcileProviderTurnAcceptance(
|
||||
ctx,
|
||||
agentruntime.ProviderTurnAcceptanceInput{
|
||||
RoomID: input.WorkspaceID,
|
||||
AgentSessionID: input.AgentSessionID,
|
||||
Provider: input.Provider,
|
||||
RootTurnID: input.RootTurnID,
|
||||
ExpectedProviderSessionID: input.ExpectedProviderSessionID,
|
||||
ExpectedProviderTurnID: input.ExpectedProviderTurnID,
|
||||
ClientUserMessageID: input.ClientUserMessageID,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ValidatePromptContent(ctx context.Context, input host.RuntimeExecInput) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
return mapRuntimeError(a.Backend.ValidatePromptContent(ctx, runtimeExecInput(input)))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Cancel(ctx context.Context, input host.RuntimeCancelInput) (host.RuntimeCancelResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeCancelResult{}, err
|
||||
}
|
||||
targets := make([]agentruntime.CancelTarget, 0, len(input.Targets))
|
||||
for _, target := range input.Targets {
|
||||
targets = append(targets, agentruntime.CancelTarget{AgentSessionID: target.AgentSessionID, TurnID: target.TurnID})
|
||||
}
|
||||
result, err := a.Backend.Cancel(ctx, agentruntime.CancelInput{
|
||||
RoomID: input.WorkspaceID, RootAgentSessionID: input.RootAgentSessionID, Targets: targets, Reason: input.Reason,
|
||||
})
|
||||
confirmed := make([]host.RuntimeCancelTarget, 0, len(result.ConfirmedTargets))
|
||||
for _, target := range result.ConfirmedTargets {
|
||||
confirmed = append(confirmed, host.RuntimeCancelTarget{AgentSessionID: target.AgentSessionID, TurnID: target.TurnID})
|
||||
}
|
||||
hostResult := host.RuntimeCancelResult{
|
||||
AgentSessionID: result.AgentSessionID,
|
||||
Canceled: result.Canceled,
|
||||
TargetAbsent: result.TargetAbsent,
|
||||
ProviderStateLost: result.ProviderStateLost,
|
||||
ConfirmedTargets: confirmed,
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrCancelTargetMismatch) {
|
||||
return hostResult, host.ErrRuntimeCancelDeliveryUnconfirmed
|
||||
}
|
||||
return hostResult, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) SubmitInteractive(ctx context.Context, input host.RuntimeSubmitInteractiveInput) (host.RuntimeSubmitInteractiveResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeSubmitInteractiveResult{}, err
|
||||
}
|
||||
result, err := a.Backend.SubmitInteractive(ctx, agentruntime.SubmitInteractiveInput{
|
||||
RoomID: input.WorkspaceID, RootAgentSessionID: input.RootAgentSessionID,
|
||||
AgentSessionID: input.AgentSessionID, TurnID: input.TurnID, RequestID: input.RequestID,
|
||||
Action: input.Action, OptionID: input.OptionID, Payload: cloneMap(input.Payload),
|
||||
})
|
||||
return host.RuntimeSubmitInteractiveResult{
|
||||
Disposition: host.RuntimeInteractiveDisposition(result.Disposition),
|
||||
FollowUpPrompt: result.FollowUpPrompt,
|
||||
}, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) InteractiveDisposition(workspaceID, rootSessionID, sessionID, turnID, requestID string) host.RuntimeInteractiveDisposition {
|
||||
if a == nil || a.Backend == nil {
|
||||
return host.RuntimeInteractiveDispositionUnknown
|
||||
}
|
||||
return host.RuntimeInteractiveDisposition(a.Backend.InteractiveDisposition(workspaceID, rootSessionID, sessionID, turnID, requestID))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) UpdateSettings(ctx context.Context, input host.RuntimeUpdateSettingsInput) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := a.Backend.UpdateSettings(ctx, agentruntime.UpdateSettingsInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
Settings: agentruntime.SessionSettingsPatch{
|
||||
Model: input.Settings.Model, ReasoningEffort: input.Settings.ReasoningEffort, Speed: input.Settings.Speed,
|
||||
PlanMode: input.Settings.PlanMode, BrowserUse: input.Settings.BrowserUse,
|
||||
ComputerUse: input.Settings.ComputerUse, PermissionModeID: input.Settings.PermissionModeID,
|
||||
},
|
||||
})
|
||||
return mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) UpdateRetainedSettings(ctx context.Context, input host.RuntimeUpdateSettingsInput) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeRetainedSettingsBackend)
|
||||
if !ok {
|
||||
return host.ErrWorkspaceDisconnectUnavailable
|
||||
}
|
||||
_, err := backend.UpdateRetainedSettings(ctx, agentruntime.UpdateSettingsInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
Settings: agentruntime.SessionSettingsPatch{
|
||||
Model: input.Settings.Model, ReasoningEffort: input.Settings.ReasoningEffort, Speed: input.Settings.Speed,
|
||||
PlanMode: input.Settings.PlanMode, BrowserUse: input.Settings.BrowserUse,
|
||||
ComputerUse: input.Settings.ComputerUse, PermissionModeID: input.Settings.PermissionModeID,
|
||||
},
|
||||
})
|
||||
return mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) SetTitle(ctx context.Context, input host.RuntimeSetTitleInput) (host.ProviderRuntimeSession, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.ProviderRuntimeSession{}, err
|
||||
}
|
||||
session, err := a.Backend.SetTitle(ctx, input.WorkspaceID, input.AgentSessionID, input.Title)
|
||||
if err != nil {
|
||||
return host.ProviderRuntimeSession{}, mapRuntimeError(err)
|
||||
}
|
||||
return a.sessionWithState(session), nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) SetVisible(ctx context.Context, input host.RuntimeSetVisibleInput) (host.ProviderRuntimeSession, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.ProviderRuntimeSession{}, err
|
||||
}
|
||||
session, err := a.Backend.SetVisible(ctx, input.WorkspaceID, input.AgentSessionID, input.Visible)
|
||||
if err != nil {
|
||||
return host.ProviderRuntimeSession{}, mapRuntimeError(err)
|
||||
}
|
||||
return a.sessionWithState(session), nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) Close(ctx context.Context, input host.RuntimeCloseInput) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := a.Backend.Close(ctx, agentruntime.CloseInput{
|
||||
RoomID: input.WorkspaceID,
|
||||
AgentSessionID: input.AgentSessionID,
|
||||
PreserveCanonicalState: input.PreserveCanonicalState,
|
||||
})
|
||||
return mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ResolveSessionFork(
|
||||
ctx context.Context,
|
||||
source host.ProviderRuntimeSession,
|
||||
) (host.SessionForkDriverDescriptor, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.SessionForkDriverDescriptor{}, err
|
||||
}
|
||||
backend, ok := a.Backend.(sessionForkRuntimeBackend)
|
||||
if !ok {
|
||||
return host.SessionForkDriverDescriptor{}, nil
|
||||
}
|
||||
capabilities, err := backend.ForkCapabilities(ctx, runtimeSession(source))
|
||||
if err != nil {
|
||||
return host.SessionForkDriverDescriptor{}, mapRuntimeError(err)
|
||||
}
|
||||
if !capabilities.FullSession && !capabilities.ThroughTurn {
|
||||
return host.SessionForkDriverDescriptor{}, nil
|
||||
}
|
||||
return host.SessionForkDriverDescriptor{
|
||||
Kind: firstNonEmptyString(capabilities.DriverKind, "daemon-runtime-native"),
|
||||
Version: firstNonEmptyString(capabilities.DriverVersion, "v1"),
|
||||
StateBindingMode: host.SessionForkStateBindingMode(firstNonEmptyString(capabilities.StateBindingMode, string(host.SessionForkStateBindingHostCopy))),
|
||||
FullSession: capabilities.FullSession,
|
||||
ThroughTurn: capabilities.ThroughTurn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ForkSession(
|
||||
ctx context.Context,
|
||||
input host.RuntimeSessionForkInput,
|
||||
) (host.RuntimeSessionForkResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeSessionForkResult{
|
||||
DeliveryDisposition: host.SessionForkDeliveryNotStarted,
|
||||
}, err
|
||||
}
|
||||
backend, ok := a.Backend.(sessionForkRuntimeBackend)
|
||||
if !ok {
|
||||
return host.RuntimeSessionForkResult{
|
||||
DeliveryDisposition: host.SessionForkDeliveryNotStarted,
|
||||
}, host.ErrSessionForkUnsupported
|
||||
}
|
||||
result, err := backend.Fork(ctx, agentruntime.SessionForkInput{
|
||||
Source: runtimeSession(input.Source),
|
||||
ProviderTurnID: input.SourceProviderTurnID,
|
||||
ProviderTurnBindingJSON: append([]byte(nil), input.SourceProviderTurnBindingJSON...),
|
||||
TargetTitle: input.TargetTitle,
|
||||
})
|
||||
mapped := host.RuntimeSessionForkResult{
|
||||
ProviderSessionID: strings.TrimSpace(result.ProviderSessionID),
|
||||
TargetProviderTurnBindings: make(
|
||||
[]host.SessionForkProviderTurnBinding,
|
||||
0,
|
||||
len(result.TargetProviderTurnBindings),
|
||||
),
|
||||
StateBindingMode: host.SessionForkStateBindingMode(strings.TrimSpace(result.StateBindingMode)),
|
||||
StateBindingReceipt: strings.TrimSpace(result.StateBindingReceipt),
|
||||
DeliveryDisposition: host.SessionForkDeliveryDisposition(
|
||||
result.DeliveryDisposition,
|
||||
),
|
||||
}
|
||||
for _, binding := range result.TargetProviderTurnBindings {
|
||||
mapped.TargetProviderTurnBindings = append(
|
||||
mapped.TargetProviderTurnBindings,
|
||||
host.SessionForkProviderTurnBinding{
|
||||
ProviderTurnID: strings.TrimSpace(binding.ProviderTurnID),
|
||||
ProviderTurnBindingJSON: append(
|
||||
[]byte(nil),
|
||||
binding.ProviderTurnBindingJSON...,
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
if mapped.StateBindingMode == "" {
|
||||
mapped.StateBindingMode = host.SessionForkStateBindingHostCopy
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, agentruntime.ErrSessionForkUnsupported) {
|
||||
return mapped, host.ErrSessionForkUnsupported
|
||||
}
|
||||
return mapped, mapRuntimeError(err)
|
||||
}
|
||||
return mapped, nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) CanForkProviderTurn(
|
||||
ctx context.Context,
|
||||
input host.RuntimeProviderTurnForkabilityInput,
|
||||
) (bool, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
backend, ok := a.Backend.(sessionForkRuntimeBackend)
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
return backend.CanForkProviderTurn(
|
||||
ctx,
|
||||
agentruntime.ProviderTurnForkabilityInput{
|
||||
Source: runtimeSession(input.Source),
|
||||
CanonicalTurnID: strings.TrimSpace(input.CanonicalTurnID),
|
||||
ProviderTurnID: strings.TrimSpace(input.ProviderTurnID),
|
||||
ProviderTurnBindingJSON: append([]byte(nil), input.ProviderTurnBindingJSON...),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func firstNonEmptyString(values ...string) string {
|
||||
for _, value := range values {
|
||||
if value = strings.TrimSpace(value); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *RuntimeController) GoalControl(ctx context.Context, input host.RuntimeGoalControlInput) (host.RuntimeGoalControlResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeGoalControlResult{}, err
|
||||
}
|
||||
result, err := a.Backend.GoalControl(ctx, agentruntime.GoalControlInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
Action: agentruntime.GoalControlAction(input.Action), Objective: input.Objective,
|
||||
OperationID: input.OperationID, GoalRevision: input.GoalRevision, RepairEpoch: input.RepairEpoch,
|
||||
SubmissionMetadata: cloneMap(input.SubmissionMetadata), RequireLive: input.RequireLive,
|
||||
})
|
||||
return host.RuntimeGoalControlResult{
|
||||
AgentSessionID: result.AgentSessionID, Goal: cloneMap(result.Goal), Evidence: cloneMap(result.Evidence),
|
||||
ProviderPhase: result.ProviderPhase, ExecutionPending: result.ExecutionPending,
|
||||
}, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ReconcileGoal(ctx context.Context, input host.RuntimeGoalControlInput) (host.RuntimeGoalReconcileResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeGoalReconcileResult{}, err
|
||||
}
|
||||
result, err := a.Backend.ReconcileGoal(ctx, agentruntime.GoalReconcileInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID, RequireLive: input.RequireLive,
|
||||
})
|
||||
return host.RuntimeGoalReconcileResult{
|
||||
AgentSessionID: result.AgentSessionID, Goal: cloneMap(result.Goal), Evidence: cloneMap(result.Evidence),
|
||||
}, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) GoalRecoveryPolicy(ctx context.Context, input host.RuntimeGoalControlInput) (host.RuntimeGoalRecoveryPolicy, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeGoalRecoveryPolicy{}, err
|
||||
}
|
||||
capabilities, err := a.Backend.GoalCapabilities(ctx, agentruntime.GoalReconcileInput{RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID})
|
||||
return host.RuntimeGoalRecoveryPolicy{
|
||||
QuerySupported: capabilities.QuerySupported, ReplaySetAfterRestart: capabilities.ReplaySetAfterRestart,
|
||||
}, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) FenceGoalGeneration(ctx context.Context, input host.RuntimeGoalGenerationFenceInput) error {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return err
|
||||
}
|
||||
fencer, ok := a.Backend.(interface {
|
||||
FenceGoalGeneration(context.Context, agentruntime.GoalGenerationFenceRequest) error
|
||||
})
|
||||
if !ok {
|
||||
return host.ErrGoalGenerationFenceUnavailable
|
||||
}
|
||||
return mapRuntimeError(fencer.FenceGoalGeneration(ctx, agentruntime.GoalGenerationFenceRequest{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
OperationID: input.TargetOperationID, Revision: input.TargetRevision,
|
||||
RepairEpoch: input.TargetRepairEpoch, Reason: input.Reason, RequireLive: input.RequireLive,
|
||||
}))
|
||||
}
|
||||
|
||||
func (a *RuntimeController) requireBackend() error {
|
||||
if a == nil || a.Backend == nil {
|
||||
return errors.New("agent runtime controller is unavailable")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapRuntimeError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrSessionDisconnected) {
|
||||
return errors.Join(host.ErrRuntimeSessionDisconnected, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrSessionNotFound) {
|
||||
return errors.Join(host.ErrSessionNotFound, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrActiveTurnTargetRequired) {
|
||||
return errors.Join(host.ErrActiveTurnTargetRequired, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrActiveTurnTargetMismatch) {
|
||||
return errors.Join(host.ErrActiveTurnTargetMismatch, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrSessionActiveTurn) {
|
||||
return errors.Join(host.ErrRuntimeSessionActive, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrEffectiveHistoryUnsupported) {
|
||||
return host.ErrRuntimeHistoryUnsupported
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrSideConversationUnsupported) {
|
||||
return errors.Join(host.ErrSideConversationUnsupported, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrSideConversationConflict) {
|
||||
return errors.Join(host.ErrSideConversationConflict, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrSideConversationExpired) {
|
||||
return errors.Join(host.ErrSideConversationExpired, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrInteractiveRequestNotLive) {
|
||||
return errors.Join(host.ErrInteractiveRequestNotLive, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrInteractiveAlreadyAnswered) {
|
||||
return errors.Join(host.ErrInteractiveAlreadyAnswered, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrInteractiveResponseInvalid) {
|
||||
return errors.Join(host.ErrInteractiveResponseInvalid, err)
|
||||
}
|
||||
if errors.Is(err, agentruntime.ErrProviderStartTimeout) {
|
||||
var appErr *agentruntime.AppError
|
||||
if errors.As(err, &appErr) && appErr != nil {
|
||||
return host.NewProviderStartTimeoutError(appErr.Message, appErr.DebugMessage, err)
|
||||
}
|
||||
return host.NewProviderStartTimeoutError("", "", err)
|
||||
}
|
||||
var appErr *agentruntime.AppError
|
||||
if errors.As(err, &appErr) && appErr != nil {
|
||||
if errors.Is(appErr, context.Canceled) || errors.Is(appErr, context.DeadlineExceeded) {
|
||||
return err
|
||||
}
|
||||
return host.NewProviderError(appErr.Code, appErr.Message, appErr.DebugMessage, appErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *RuntimeController) fromSession(session agentruntime.Session) host.ProviderRuntimeSession {
|
||||
var settings *host.ComposerSettings
|
||||
if session.Settings != nil {
|
||||
value := hostSettings(*session.Settings)
|
||||
settings = &value
|
||||
}
|
||||
return host.ProviderRuntimeSession{
|
||||
ID: session.AgentSessionID, WorkspaceID: session.RoomID, UserID: a.currentUserID(),
|
||||
Scope: host.RuntimeSessionScope(session.Scope),
|
||||
SourceAgentSessionID: session.SourceAgentSessionID, SideRequestID: session.SideRequestID,
|
||||
AgentTargetID: session.AgentTargetID, Provider: session.Provider, ProviderSessionID: session.ProviderSessionID,
|
||||
Resumable: session.Resumable,
|
||||
Cwd: session.CWD, Env: append([]string(nil), session.Env...), MCPServers: hostMCPServerBindings(session.MCPServers), Settings: settings,
|
||||
ProviderTargetRef: cloneMap(session.ProviderTargetRef),
|
||||
RuntimeContext: cloneMap(session.RuntimeContext), Status: session.Status,
|
||||
TurnLifecycle: hostTurnLifecyclePointer(session.TurnLifecycle), SubmitAvailability: hostSubmitAvailability(session.SubmitAvailability),
|
||||
Visible: session.Visible, Title: session.Title, InitialTitleEstablished: session.InitialTitleEstablished,
|
||||
LastError: session.LastError, CreatedAtUnixMS: session.CreatedAtUnixMS, UpdatedAtUnixMS: session.UpdatedAtUnixMS,
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeSession(session host.ProviderRuntimeSession) agentruntime.Session {
|
||||
var settings *agentruntime.SessionSettings
|
||||
if session.Settings != nil {
|
||||
settings = runtimeSettings(*session.Settings)
|
||||
}
|
||||
return agentruntime.Session{
|
||||
RoomID: session.WorkspaceID, AgentSessionID: session.ID,
|
||||
Scope: agentruntime.RuntimeSessionScope(session.Scope),
|
||||
SourceAgentSessionID: session.SourceAgentSessionID, SideRequestID: session.SideRequestID,
|
||||
AgentTargetID: session.AgentTargetID, Provider: session.Provider,
|
||||
ProviderSessionID: session.ProviderSessionID, Resumable: session.Resumable,
|
||||
CWD: session.Cwd, Env: append([]string(nil), session.Env...), MCPServers: runtimeMCPServerBindings(session.MCPServers),
|
||||
Status: session.Status, TurnLifecycle: runtimeTurnLifecyclePointer(session.TurnLifecycle),
|
||||
SubmitAvailability: runtimeSubmitAvailability(session.SubmitAvailability),
|
||||
Title: session.Title, LastError: session.LastError, Visible: session.Visible,
|
||||
RuntimeContext: cloneMap(session.RuntimeContext), ProviderTargetRef: cloneMap(session.ProviderTargetRef),
|
||||
Settings: settings,
|
||||
CreatedAtUnixMS: session.CreatedAtUnixMS, UpdatedAtUnixMS: session.UpdatedAtUnixMS,
|
||||
InitialTitleEstablished: session.InitialTitleEstablished,
|
||||
}
|
||||
}
|
||||
|
||||
// sessionWithState preserves the daemon runtime's provider-enriched live
|
||||
// observation. The base Session owns process identity and lifecycle fields;
|
||||
// State overlays provider-computed settings and runtime context such as model
|
||||
// catalogs, usage, rate limits, account details, and commands.
|
||||
func (a *RuntimeController) sessionWithState(session agentruntime.Session) host.ProviderRuntimeSession {
|
||||
result := a.fromSession(session)
|
||||
if a == nil || a.Backend == nil {
|
||||
return result
|
||||
}
|
||||
state, err := a.Backend.State(session.RoomID, session.AgentSessionID)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
if state.ProviderSessionID != "" {
|
||||
result.ProviderSessionID = state.ProviderSessionID
|
||||
}
|
||||
result.Resumable = result.Resumable || state.Resumable
|
||||
if state.Status != "" {
|
||||
result.Status = state.Status
|
||||
}
|
||||
if state.TurnLifecycle != nil {
|
||||
result.TurnLifecycle = hostTurnLifecyclePointer(state.TurnLifecycle)
|
||||
}
|
||||
if state.SubmitAvailability != nil {
|
||||
result.SubmitAvailability = hostSubmitAvailability(state.SubmitAvailability)
|
||||
}
|
||||
if state.Settings != nil {
|
||||
settings := hostSettings(*state.Settings)
|
||||
result.Settings = &settings
|
||||
}
|
||||
result.Capabilities = canonical.CloneCapabilitySnapshot(state.Capabilities)
|
||||
result.RuntimeContext = cloneMap(state.RuntimeContext)
|
||||
if state.UpdatedAtUnixMS > 0 {
|
||||
result.UpdatedAtUnixMS = state.UpdatedAtUnixMS
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (a *RuntimeController) currentUserID() string {
|
||||
if a != nil && a.CurrentUserID != nil {
|
||||
return strings.TrimSpace(a.CurrentUserID())
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func runtimeResumeInput(input host.RuntimeResumeInput) agentruntime.ResumeInput {
|
||||
return agentruntime.ResumeInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID, AgentTargetID: input.AgentTargetID,
|
||||
Provider: input.Provider, ProviderSessionID: input.ProviderSessionID, CWD: input.Cwd,
|
||||
Resumable: input.Resumable,
|
||||
Env: append([]string(nil), input.Env...), MCPServers: runtimeMCPServerBindings(input.MCPServers),
|
||||
Title: input.Title, Status: input.Status, Visible: input.Visible,
|
||||
RuntimeContext: cloneMap(input.RuntimeContext),
|
||||
ProviderLaunchRuntimeContext: cloneMap(input.ProviderLaunchRuntimeContext),
|
||||
ProviderTargetRef: cloneMap(input.ProviderTargetRef),
|
||||
PermissionModeID: input.Settings.PermissionModeID, Settings: runtimeSettings(input.Settings),
|
||||
CreatedAtUnixMS: input.CreatedAtUnixMS, UpdatedAtUnixMS: input.UpdatedAtUnixMS,
|
||||
GoalGenerationFences: runtimeGoalGenerationFences(input.GoalGenerationFences),
|
||||
RecreateIfMissing: input.RecreateIfMissing,
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeExecInput(input host.RuntimeExecInput) agentruntime.ExecInput {
|
||||
return agentruntime.ExecInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
TurnID: input.TurnID, ClientSubmitID: input.ClientSubmitID,
|
||||
CanonicalSubmitOccurredAtUnixMS: input.CanonicalSubmitOccurredAtUnixMS,
|
||||
CapabilityRefs: runtimeCapabilityReferences(input.CapabilityRefs),
|
||||
TuttiModeSnapshot: runtimeTuttiModeSnapshot(input.TuttiModeSnapshot),
|
||||
Content: runtimePromptContent(input.Content),
|
||||
DisplayPrompt: input.DisplayPrompt, InitialTitle: input.InitialTitle, InitialTitleBase: input.InitialTitleBase,
|
||||
Metadata: cloneMap(input.Metadata), Guidance: input.Guidance,
|
||||
HistoryReplacement: input.HistoryReplacement,
|
||||
RequireProviderAcceptance: input.RequireProviderAcceptance,
|
||||
ConnectorRoutingUpdate: cloneStringPointer(input.ConnectorRoutingUpdate),
|
||||
}
|
||||
}
|
||||
|
||||
func cloneStringPointer(value *string) *string {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
cloned := *value
|
||||
return &cloned
|
||||
}
|
||||
|
||||
func runtimeHistoryInput(input host.RuntimeHistoryInput) agentruntime.EffectiveHistoryInput {
|
||||
return agentruntime.EffectiveHistoryInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
Provider: input.Provider,
|
||||
}
|
||||
}
|
||||
|
||||
func hostHistorySnapshot(input agentruntime.EffectiveHistorySnapshot) host.RuntimeHistorySnapshot {
|
||||
turns := make([]host.RuntimeHistoryTurn, 0, len(input.Turns))
|
||||
for _, turn := range input.Turns {
|
||||
turns = append(turns, host.RuntimeHistoryTurn{
|
||||
ID: turn.ID, Status: turn.Status,
|
||||
ClientUserMessageID: turn.ClientUserMessageID,
|
||||
})
|
||||
}
|
||||
return host.RuntimeHistorySnapshot{
|
||||
ProviderSessionID: input.ProviderSessionID,
|
||||
Turns: turns,
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeSubmitProvenanceInput(input host.RuntimeSubmitProvenanceInput) agentruntime.SubmitProvenanceInput {
|
||||
return agentruntime.SubmitProvenanceInput{
|
||||
RoomID: input.WorkspaceID, AgentSessionID: input.AgentSessionID,
|
||||
TurnID: input.TurnID, ClientSubmitID: input.ClientSubmitID,
|
||||
CanonicalSubmitOccurredAtUnixMS: input.CanonicalSubmitOccurredAtUnixMS,
|
||||
Content: runtimePromptContent(input.Content), DisplayPrompt: input.DisplayPrompt,
|
||||
Guidance: input.Guidance,
|
||||
}
|
||||
}
|
||||
|
||||
func runtimePromptContent(input []host.PromptContentBlock) []agentruntime.PromptContentBlock {
|
||||
content := make([]agentruntime.PromptContentBlock, 0, len(input))
|
||||
for _, block := range input {
|
||||
content = append(content, agentruntime.PromptContentBlock{
|
||||
Type: block.Type, Text: block.Text, MimeType: block.MimeType, Data: block.Data, URL: block.URL,
|
||||
AttachmentID: block.AttachmentID, Name: block.Name, Path: block.Path, ConnectorKey: block.ConnectorKey,
|
||||
})
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
func runtimeCapabilityReferences(input []host.CapabilityReference) []agentruntime.CapabilityReference {
|
||||
references := make([]agentruntime.CapabilityReference, 0, len(input))
|
||||
for _, reference := range input {
|
||||
references = append(references, agentruntime.CapabilityReference{
|
||||
Capability: reference.Capability,
|
||||
Source: reference.Source,
|
||||
})
|
||||
}
|
||||
return references
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
func runtimeGoalGenerationFences(input []host.RuntimeGoalGenerationFenceInput) []agentruntime.GoalGenerationFenceInput {
|
||||
result := make([]agentruntime.GoalGenerationFenceInput, 0, len(input))
|
||||
for _, fence := range input {
|
||||
result = append(result, agentruntime.GoalGenerationFenceInput{
|
||||
OperationID: fence.TargetOperationID, Revision: fence.TargetRevision,
|
||||
RepairEpoch: fence.TargetRepairEpoch, Reason: fence.Reason,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
func runtimeTuttiModeSnapshot(input *host.TuttiModeTurnSnapshot) *agentruntime.TuttiModeTurnSnapshot {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
legacyOrchestrationIntensity := input.OrchestrationIntensity //nolint:staticcheck // Compatibility bridge preserves version-zero snapshots.
|
||||
return &agentruntime.TuttiModeTurnSnapshot{
|
||||
ActivationID: input.ActivationID, RevisionID: input.RevisionID, Revision: input.Revision,
|
||||
State: input.State, Source: input.Source,
|
||||
PreferenceVersion: input.PreferenceVersion,
|
||||
Effect: input.Effect,
|
||||
Speed: input.Speed,
|
||||
OrchestrationIntensity: legacyOrchestrationIntensity,
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeSettings(settings host.ComposerSettings) *agentruntime.SessionSettings {
|
||||
return &agentruntime.SessionSettings{
|
||||
CodexSaverMode: settings.CodexSaverMode,
|
||||
RTKSaverMode: settings.RTKSaverMode,
|
||||
Model: settings.Model, ReasoningEffort: settings.ReasoningEffort, Speed: settings.Speed,
|
||||
PlanMode: settings.PlanMode, BrowserUse: settings.BrowserUse, ComputerUse: settings.ComputerUse,
|
||||
PermissionModeID: settings.PermissionModeID, ConversationDetailMode: settings.ConversationDetailMode,
|
||||
}
|
||||
}
|
||||
|
||||
func hostSettings(settings agentruntime.SessionSettings) host.ComposerSettings {
|
||||
return host.ComposerSettings{
|
||||
CodexSaverMode: settings.CodexSaverMode,
|
||||
RTKSaverMode: settings.RTKSaverMode,
|
||||
Model: settings.Model, ReasoningEffort: settings.ReasoningEffort, Speed: settings.Speed,
|
||||
PlanMode: settings.PlanMode, BrowserUse: settings.BrowserUse, ComputerUse: settings.ComputerUse,
|
||||
PermissionModeID: settings.PermissionModeID, ConversationDetailMode: settings.ConversationDetailMode,
|
||||
}
|
||||
}
|
||||
|
||||
func hostTurnLifecyclePointer(input *agentruntime.TurnLifecycle) *host.TurnLifecycle {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
value := hostTurnLifecycle(*input)
|
||||
return &value
|
||||
}
|
||||
|
||||
func hostTurnLifecycle(input agentruntime.TurnLifecycle) host.TurnLifecycle {
|
||||
var completed *host.CompletedCommand
|
||||
if input.CompletedCommand != nil {
|
||||
completed = &host.CompletedCommand{Kind: input.CompletedCommand.Kind, Status: input.CompletedCommand.Status}
|
||||
}
|
||||
return host.TurnLifecycle{
|
||||
ActiveTurnID: input.ActiveTurnID, Phase: input.Phase, Settling: input.Settling,
|
||||
Outcome: input.Outcome, CompletedCommand: completed,
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeTurnLifecyclePointer(input *host.TurnLifecycle) *agentruntime.TurnLifecycle {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
var completed *agentruntime.CompletedCommand
|
||||
if input.CompletedCommand != nil {
|
||||
completed = &agentruntime.CompletedCommand{
|
||||
Kind: input.CompletedCommand.Kind, Status: input.CompletedCommand.Status,
|
||||
}
|
||||
}
|
||||
return &agentruntime.TurnLifecycle{
|
||||
ActiveTurnID: input.ActiveTurnID, Phase: input.Phase, Settling: input.Settling,
|
||||
Outcome: input.Outcome, CompletedCommand: completed,
|
||||
}
|
||||
}
|
||||
|
||||
func hostSubmitAvailability(input *agentruntime.SubmitAvailability) *host.SubmitAvailability {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
return &host.SubmitAvailability{State: input.State, Reason: input.Reason}
|
||||
}
|
||||
|
||||
func runtimeSubmitAvailability(input *host.SubmitAvailability) *agentruntime.SubmitAvailability {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
return &agentruntime.SubmitAvailability{State: input.State, Reason: input.Reason}
|
||||
}
|
||||
|
||||
func cloneMap(input map[string]any) map[string]any {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]any, len(input))
|
||||
for key, value := range input {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
)
|
||||
|
||||
type sessionForkRuntimeBackend interface {
|
||||
ForkCapabilities(context.Context, agentruntime.Session) (agentruntime.SessionForkCapabilities, error)
|
||||
CanForkProviderTurn(context.Context, agentruntime.ProviderTurnForkabilityInput) (bool, error)
|
||||
Fork(context.Context, agentruntime.SessionForkInput) (agentruntime.SessionForkResult, error)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
type sideConversationRuntimeBackend interface {
|
||||
SideCapabilitiesForSource(context.Context, agentruntime.Session) (agentruntime.SideConversationCapabilities, error)
|
||||
OpenSide(context.Context, agentruntime.SideConversationOpenInput) (agentruntime.SideConversationOpenResult, error)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) ResolveSideConversation(
|
||||
ctx context.Context,
|
||||
source host.ProviderRuntimeSession,
|
||||
) (host.SideConversationCapabilities, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.SideConversationCapabilities{}, err
|
||||
}
|
||||
backend, ok := a.Backend.(sideConversationRuntimeBackend)
|
||||
if !ok {
|
||||
return host.SideConversationCapabilities{}, nil
|
||||
}
|
||||
capabilities, err := backend.SideCapabilitiesForSource(ctx, runtimeSession(source))
|
||||
return hostSideCapabilities(capabilities), mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) OpenSideConversation(
|
||||
ctx context.Context,
|
||||
input host.RuntimeOpenSideConversationInput,
|
||||
) (host.OpenSideConversationResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.OpenSideConversationResult{}, err
|
||||
}
|
||||
backend, ok := a.Backend.(sideConversationRuntimeBackend)
|
||||
if !ok {
|
||||
return host.OpenSideConversationResult{}, host.ErrSideConversationUnsupported
|
||||
}
|
||||
result, err := backend.OpenSide(
|
||||
ctx,
|
||||
agentruntime.SideConversationOpenInput{
|
||||
RoomID: input.Source.WorkspaceID,
|
||||
SourceAgentSessionID: input.Source.ID,
|
||||
SideAgentSessionID: input.SideAgentSessionID,
|
||||
RequestID: input.RequestID,
|
||||
Source: runtimeSession(input.Source),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return host.OpenSideConversationResult{}, mapRuntimeError(err)
|
||||
}
|
||||
return host.OpenSideConversationResult{
|
||||
Session: a.sessionWithState(result.Session),
|
||||
Capabilities: hostSideCapabilities(result.Capabilities),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func hostSideCapabilities(
|
||||
capabilities agentruntime.SideConversationCapabilities,
|
||||
) host.SideConversationCapabilities {
|
||||
return host.SideConversationCapabilities{
|
||||
Supported: capabilities.Supported,
|
||||
ActiveSourceTurn: capabilities.ActiveSourceTurn,
|
||||
Ephemeral: capabilities.Ephemeral,
|
||||
HideInheritedTurns: capabilities.HideInheritedTurns,
|
||||
ModelBoundaryInjected: capabilities.ModelBoundaryInjected,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,536 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
"github.com/tutti-os/tutti/packages/agent/store-sqlite/canonical"
|
||||
)
|
||||
|
||||
type stateRuntimeBackend struct {
|
||||
RuntimeBackend
|
||||
session agentruntime.Session
|
||||
state agentruntime.SessionStateSnapshot
|
||||
stateErr error
|
||||
}
|
||||
|
||||
type provenanceRuntimeBackend struct {
|
||||
RuntimeBackend
|
||||
input agentruntime.SubmitProvenanceInput
|
||||
err error
|
||||
}
|
||||
|
||||
type goalLifecycleRuntimeBackend struct {
|
||||
RuntimeBackend
|
||||
observer agentruntime.GoalControlLifecycleObserver
|
||||
}
|
||||
|
||||
type closeRuntimeBackend struct {
|
||||
RuntimeBackend
|
||||
input agentruntime.CloseInput
|
||||
}
|
||||
|
||||
type mismatchCancelRuntimeBackend struct {
|
||||
RuntimeBackend
|
||||
input agentruntime.CancelInput
|
||||
}
|
||||
|
||||
func (b *mismatchCancelRuntimeBackend) Cancel(
|
||||
_ context.Context,
|
||||
input agentruntime.CancelInput,
|
||||
) (agentruntime.CancelResult, error) {
|
||||
b.input = input
|
||||
return agentruntime.CancelResult{AgentSessionID: input.RootAgentSessionID}, agentruntime.ErrCancelTargetMismatch
|
||||
}
|
||||
|
||||
type workspaceDisconnectBackend struct {
|
||||
RuntimeBackend
|
||||
sessions []agentruntime.Session
|
||||
roomID string
|
||||
sessionID string
|
||||
target agentruntime.RuntimeDisconnectTarget
|
||||
}
|
||||
|
||||
func (*workspaceDisconnectBackend) SnapshotRuntimeDisconnectTargets(string) []agentruntime.RuntimeDisconnectTarget {
|
||||
return []agentruntime.RuntimeDisconnectTarget{{
|
||||
RoomID: "workspace-1", AgentSessionID: "session-1", ConnectionGeneration: 7,
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *workspaceDisconnectBackend) DisconnectRuntimeSessionTarget(
|
||||
_ context.Context,
|
||||
target agentruntime.RuntimeDisconnectTarget,
|
||||
) (agentruntime.DisconnectRuntimeSessionResult, error) {
|
||||
b.target = target
|
||||
return agentruntime.DisconnectRuntimeSessionResult{Disconnected: true}, nil
|
||||
}
|
||||
|
||||
func (b *workspaceDisconnectBackend) RuntimeSessions(context.Context, string) ([]agentruntime.Session, error) {
|
||||
return append([]agentruntime.Session(nil), b.sessions...), nil
|
||||
}
|
||||
|
||||
func (*workspaceDisconnectBackend) State(_, _ string) (agentruntime.SessionStateSnapshot, error) {
|
||||
return agentruntime.SessionStateSnapshot{}, nil
|
||||
}
|
||||
|
||||
func (b *workspaceDisconnectBackend) DisconnectRuntimeSession(
|
||||
_ context.Context,
|
||||
roomID string,
|
||||
sessionID string,
|
||||
) (agentruntime.DisconnectRuntimeSessionResult, error) {
|
||||
b.roomID = roomID
|
||||
b.sessionID = sessionID
|
||||
return agentruntime.DisconnectRuntimeSessionResult{Disconnected: true}, nil
|
||||
}
|
||||
|
||||
func TestRuntimeControllerBridgesWorkspaceRuntimeDisconnect(t *testing.T) {
|
||||
t.Parallel()
|
||||
backend := &workspaceDisconnectBackend{sessions: []agentruntime.Session{{
|
||||
RoomID: "workspace-1", AgentSessionID: "session-1", ProviderSessionID: "provider-1",
|
||||
}}}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
sessions, err := controller.WorkspaceRuntimeSessions(t.Context(), " workspace-1 ")
|
||||
if err != nil {
|
||||
t.Fatalf("WorkspaceRuntimeSessions: %v", err)
|
||||
}
|
||||
if len(sessions) != 1 || sessions[0].ID != "session-1" || sessions[0].ProviderSessionID != "provider-1" {
|
||||
t.Fatalf("sessions=%#v", sessions)
|
||||
}
|
||||
disconnected, err := controller.DisconnectRuntimeSession(t.Context(), host.SessionRef{
|
||||
WorkspaceID: " workspace-1 ", AgentSessionID: " session-1 ",
|
||||
})
|
||||
if err != nil || !disconnected {
|
||||
t.Fatalf("disconnected=%v err=%v", disconnected, err)
|
||||
}
|
||||
if backend.roomID != "workspace-1" || backend.sessionID != "session-1" {
|
||||
t.Fatalf("backend ref=%q/%q", backend.roomID, backend.sessionID)
|
||||
}
|
||||
targets := controller.SnapshotWorkspaceRuntimeDisconnectTargets("workspace-1")
|
||||
if len(targets) != 1 || targets[0].ConnectionGeneration != 7 {
|
||||
t.Fatalf("targets=%#v", targets)
|
||||
}
|
||||
disconnected, err = controller.DisconnectRuntimeSessionTarget(t.Context(), targets[0])
|
||||
if err != nil || !disconnected || backend.target.ConnectionGeneration != 7 {
|
||||
t.Fatalf("target disconnect=%v err=%v backend=%#v", disconnected, err, backend.target)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerMapsExactCancelMismatchToDeliveryUnconfirmed(t *testing.T) {
|
||||
t.Parallel()
|
||||
backend := &mismatchCancelRuntimeBackend{}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
|
||||
result, err := controller.Cancel(t.Context(), host.RuntimeCancelInput{
|
||||
WorkspaceID: "workspace-1", RootAgentSessionID: "root-session", Reason: "user_requested",
|
||||
Targets: []host.RuntimeCancelTarget{{AgentSessionID: "child-session", TurnID: "child-turn"}},
|
||||
})
|
||||
if !errors.Is(err, host.ErrRuntimeCancelDeliveryUnconfirmed) {
|
||||
t.Fatalf("Cancel() error = %v, want delivery-unconfirmed", err)
|
||||
}
|
||||
if result.TargetAbsent || result.Canceled || backend.input.RootAgentSessionID != "root-session" || len(backend.input.Targets) != 1 ||
|
||||
backend.input.Targets[0].AgentSessionID != "child-session" || backend.input.Targets[0].TurnID != "child-turn" {
|
||||
t.Fatalf("Cancel() result=%#v backend input=%#v", result, backend.input)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *closeRuntimeBackend) Close(_ context.Context, input agentruntime.CloseInput) (agentruntime.CloseResult, error) {
|
||||
b.input = input
|
||||
return agentruntime.CloseResult{AgentSessionID: input.AgentSessionID, Disconnected: true}, nil
|
||||
}
|
||||
|
||||
func TestRuntimeControllerPreservesCanonicalStateWhenClosingRuntime(t *testing.T) {
|
||||
t.Parallel()
|
||||
backend := &closeRuntimeBackend{}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
|
||||
if err := controller.Close(t.Context(), host.RuntimeCloseInput{
|
||||
WorkspaceID: "workspace-1", AgentSessionID: "session-rejected", PreserveCanonicalState: true,
|
||||
}); err != nil {
|
||||
t.Fatalf("Close() error = %v", err)
|
||||
}
|
||||
if backend.input.RoomID != "workspace-1" ||
|
||||
backend.input.AgentSessionID != "session-rejected" ||
|
||||
!backend.input.PreserveCanonicalState {
|
||||
t.Fatalf("backend close input = %#v", backend.input)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeResumeInputMapsDurableGoalGenerationFences(t *testing.T) {
|
||||
t.Parallel()
|
||||
mapped := runtimeResumeInput(host.RuntimeResumeInput{
|
||||
WorkspaceID: "workspace-1", AgentSessionID: "session-1", Provider: "codex",
|
||||
GoalGenerationFences: []host.RuntimeGoalGenerationFenceInput{{
|
||||
TargetOperationID: "old-goal", TargetRevision: 3, TargetRepairEpoch: 2,
|
||||
Reason: "binding_revoked", RequireLive: false,
|
||||
}},
|
||||
})
|
||||
if len(mapped.GoalGenerationFences) != 1 {
|
||||
t.Fatalf("mapped fences=%#v", mapped.GoalGenerationFences)
|
||||
}
|
||||
fence := mapped.GoalGenerationFences[0]
|
||||
if fence.OperationID != "old-goal" || fence.Revision != 3 || fence.RepairEpoch != 2 ||
|
||||
fence.Reason != "binding_revoked" {
|
||||
t.Fatalf("mapped fence=%#v", fence)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *goalLifecycleRuntimeBackend) SetGoalControlLifecycleObserver(observer agentruntime.GoalControlLifecycleObserver) {
|
||||
b.observer = observer
|
||||
}
|
||||
|
||||
func TestRuntimeControllerBridgesGoalLifecycleToHostSink(t *testing.T) {
|
||||
t.Parallel()
|
||||
backend := &goalLifecycleRuntimeBackend{}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
var received host.RuntimeGoalControlAppliedInput
|
||||
controller.SetGoalControlAppliedSink(func(_ context.Context, input host.RuntimeGoalControlAppliedInput) error {
|
||||
received = input
|
||||
return nil
|
||||
})
|
||||
if backend.observer == nil {
|
||||
t.Fatal("goal lifecycle observer was not registered")
|
||||
}
|
||||
err := backend.observer.ObserveGoalControlApplied(t.Context(), agentruntime.GoalControlAppliedObservation{
|
||||
WorkspaceID: "workspace", AgentSessionID: "session", OperationID: "goal-op-1",
|
||||
Revision: 3, RepairEpoch: 1, Action: "set", ProviderTurnID: "provider-turn-1",
|
||||
Observed: map[string]any{"objective": "ship it", "status": "active"}, OccurredAtUnixMS: 42,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if received.WorkspaceID != "workspace" || received.AgentSessionID != "session" ||
|
||||
received.OperationID != "goal-op-1" || received.GoalRevision != 3 || received.RepairEpoch != 1 ||
|
||||
received.Action != "set" || received.ProviderTurnID != "provider-turn-1" ||
|
||||
received.Observed["objective"] != "ship it" || received.OccurredAtUnixMS != 42 {
|
||||
t.Fatalf("host goal lifecycle input=%#v", received)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *provenanceRuntimeBackend) DurablyReportSubmitProvenance(_ context.Context, input agentruntime.SubmitProvenanceInput) error {
|
||||
b.input = input
|
||||
return b.err
|
||||
}
|
||||
|
||||
func (b *stateRuntimeBackend) Session(_, _ string) (agentruntime.Session, bool) {
|
||||
return b.session, true
|
||||
}
|
||||
|
||||
func (b *stateRuntimeBackend) State(_, _ string) (agentruntime.SessionStateSnapshot, error) {
|
||||
return b.state, b.stateErr
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorPreservesProviderDiagnostics(t *testing.T) {
|
||||
cause := errors.New("provider process rejected request")
|
||||
runtimeErr := &agentruntime.AppError{
|
||||
Code: "provider_auth_required",
|
||||
Message: "Agent provider needs authentication",
|
||||
DebugMessage: "provider exited with status 1",
|
||||
Cause: cause,
|
||||
}
|
||||
|
||||
mapped := mapRuntimeError(fmt.Errorf("daemon runtime: %w", runtimeErr))
|
||||
var providerErr *host.ProviderError
|
||||
if !errors.As(mapped, &providerErr) {
|
||||
t.Fatalf("mapped error = %v, want ProviderError", mapped)
|
||||
}
|
||||
if providerErr.Code != runtimeErr.Code || providerErr.Message != runtimeErr.Message || providerErr.DebugMessage != runtimeErr.DebugMessage {
|
||||
t.Fatalf("ProviderError = %#v, want diagnostics from %#v", providerErr, runtimeErr)
|
||||
}
|
||||
if !errors.Is(mapped, runtimeErr) || !errors.Is(mapped, cause) {
|
||||
t.Fatalf("mapped error did not preserve source chain: %v", mapped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorKeepsTransportOutcomeUnknown(t *testing.T) {
|
||||
for _, target := range []error{context.Canceled, context.DeadlineExceeded} {
|
||||
t.Run(target.Error(), func(t *testing.T) {
|
||||
code := "request_failed"
|
||||
if errors.Is(target, context.DeadlineExceeded) {
|
||||
code = "request_timed_out"
|
||||
}
|
||||
runtimeErr := &agentruntime.AppError{
|
||||
Code: code,
|
||||
Cause: fmt.Errorf("provider response: %w", target),
|
||||
}
|
||||
mapped := mapRuntimeError(runtimeErr)
|
||||
var providerErr *host.ProviderError
|
||||
if errors.As(mapped, &providerErr) {
|
||||
t.Fatalf("mapped error = %#v, want transport outcome to remain unknown", providerErr)
|
||||
}
|
||||
if !errors.Is(mapped, target) {
|
||||
t.Fatalf("mapped error = %v, want %v in chain", mapped, target)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorPreservesProviderStartTimeoutVerdict(t *testing.T) {
|
||||
runtimeErr := &agentruntime.AppError{
|
||||
Code: "request_timed_out",
|
||||
Message: "Agent could not start before the request timed out.",
|
||||
DebugMessage: "provider startup exceeded its deadline",
|
||||
Cause: errors.Join(
|
||||
agentruntime.ErrProviderStartTimeout,
|
||||
fmt.Errorf("provider start: %w", context.DeadlineExceeded),
|
||||
),
|
||||
}
|
||||
mapped := mapRuntimeError(fmt.Errorf("daemon runtime: %w", runtimeErr))
|
||||
var providerErr *host.ProviderError
|
||||
if !errors.As(mapped, &providerErr) {
|
||||
t.Fatalf("mapped error = %v, want ProviderError", mapped)
|
||||
}
|
||||
if providerErr.Code != host.ProviderErrorCodeStartTimeout {
|
||||
t.Fatalf("ProviderError code = %q, want %q", providerErr.Code, host.ProviderErrorCodeStartTimeout)
|
||||
}
|
||||
if providerErr.Message != runtimeErr.Message || providerErr.DebugMessage != runtimeErr.DebugMessage {
|
||||
t.Fatalf("ProviderError = %#v, want presentation diagnostics from %#v", providerErr, runtimeErr)
|
||||
}
|
||||
if !errors.Is(mapped, context.DeadlineExceeded) || !errors.Is(mapped, runtimeErr) {
|
||||
t.Fatalf("mapped error did not preserve runtime deadline chain: %v", mapped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorMapsDisconnectedSessionAcrossHostBoundary(t *testing.T) {
|
||||
runtimeErr := fmt.Errorf("fence requires live provider: %w", agentruntime.ErrSessionDisconnected)
|
||||
mapped := mapRuntimeError(runtimeErr)
|
||||
if !errors.Is(mapped, host.ErrRuntimeSessionDisconnected) {
|
||||
t.Fatalf("mapped error = %v, want Host disconnected sentinel", mapped)
|
||||
}
|
||||
if !errors.Is(mapped, agentruntime.ErrSessionDisconnected) {
|
||||
t.Fatalf("mapped error = %v, want source runtime sentinel preserved", mapped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorMapsInteractiveContractAcrossHostBoundary(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
runtime error
|
||||
host error
|
||||
}{
|
||||
{agentruntime.ErrInteractiveRequestNotLive, host.ErrInteractiveRequestNotLive},
|
||||
{agentruntime.ErrInteractiveAlreadyAnswered, host.ErrInteractiveAlreadyAnswered},
|
||||
{agentruntime.ErrInteractiveResponseInvalid, host.ErrInteractiveResponseInvalid},
|
||||
} {
|
||||
mapped := mapRuntimeError(test.runtime)
|
||||
if !errors.Is(mapped, test.host) || !errors.Is(mapped, test.runtime) {
|
||||
t.Fatalf(
|
||||
"mapped error = %v, want host %v and runtime %v",
|
||||
mapped,
|
||||
test.host,
|
||||
test.runtime,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorMapsMissingSessionAcrossHostBoundary(t *testing.T) {
|
||||
runtimeErr := fmt.Errorf("fence session disappeared: %w", agentruntime.ErrSessionNotFound)
|
||||
mapped := mapRuntimeError(runtimeErr)
|
||||
if !errors.Is(mapped, host.ErrSessionNotFound) {
|
||||
t.Fatalf("mapped error = %v, want Host missing-session sentinel", mapped)
|
||||
}
|
||||
if !errors.Is(mapped, agentruntime.ErrSessionNotFound) {
|
||||
t.Fatalf("mapped error = %v, want source runtime sentinel preserved", mapped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapRuntimeErrorMapsGuidanceTargetVerdictsAcrossHostBoundary(t *testing.T) {
|
||||
for _, testCase := range []struct {
|
||||
runtimeErr error
|
||||
hostErr error
|
||||
}{
|
||||
{agentruntime.ErrActiveTurnTargetRequired, host.ErrActiveTurnTargetRequired},
|
||||
{agentruntime.ErrActiveTurnTargetMismatch, host.ErrActiveTurnTargetMismatch},
|
||||
} {
|
||||
t.Run(testCase.runtimeErr.Error(), func(t *testing.T) {
|
||||
mapped := mapRuntimeError(fmt.Errorf("guidance dispatch: %w", testCase.runtimeErr))
|
||||
if !errors.Is(mapped, testCase.hostErr) {
|
||||
t.Fatalf("mapped error = %v, want Host sentinel %v", mapped, testCase.hostErr)
|
||||
}
|
||||
if !errors.Is(mapped, testCase.runtimeErr) {
|
||||
t.Fatalf("mapped error = %v, want source runtime sentinel preserved", mapped)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerProjectsSessionWithoutAliasingMutableInputs(t *testing.T) {
|
||||
runtimeContext := map[string]any{"mode": "plan"}
|
||||
providerTargetRef := map[string]any{"agent": "codex"}
|
||||
env := []string{"A=1"}
|
||||
controller := &RuntimeController{CurrentUserID: func() string { return " user-1 " }}
|
||||
|
||||
projected := controller.fromSession(agentruntime.Session{
|
||||
RoomID: "workspace-1", AgentSessionID: "session-1", AgentTargetID: "target-1",
|
||||
Provider: "codex", Env: env, RuntimeContext: runtimeContext,
|
||||
ProviderTargetRef: providerTargetRef,
|
||||
Settings: &agentruntime.SessionSettings{Model: "gpt-5.6", ReasoningEffort: "max", Speed: "standard"},
|
||||
})
|
||||
env[0] = "A=2"
|
||||
runtimeContext["mode"] = "changed"
|
||||
providerTargetRef["agent"] = "changed"
|
||||
|
||||
if projected.UserID != "user-1" || projected.Env[0] != "A=1" ||
|
||||
projected.RuntimeContext["mode"] != "plan" ||
|
||||
projected.ProviderTargetRef["agent"] != "codex" {
|
||||
t.Fatalf("projected session retained mutable input or identity whitespace: %#v", projected)
|
||||
}
|
||||
if projected.Settings == nil || projected.Settings.Model != "gpt-5.6" || projected.Settings.ReasoningEffort != "max" || projected.Settings.Speed != "standard" {
|
||||
t.Fatalf("projected settings = %#v", projected.Settings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeSessionProjectsPreparedIdentityWithoutAliasing(t *testing.T) {
|
||||
env := []string{"FORK_ENV=prepared"}
|
||||
runtimeContext := map[string]any{"origin": "prepared"}
|
||||
providerTargetRef := map[string]any{"agent": "codex"}
|
||||
projected := runtimeSession(host.ProviderRuntimeSession{
|
||||
ID: "session-1", WorkspaceID: "workspace-1", Provider: "codex",
|
||||
Cwd: "/prepared", Env: env, RuntimeContext: runtimeContext,
|
||||
ProviderTargetRef: providerTargetRef,
|
||||
Settings: &host.ComposerSettings{Model: "gpt-5.6"},
|
||||
})
|
||||
env[0] = "FORK_ENV=changed"
|
||||
runtimeContext["origin"] = "changed"
|
||||
providerTargetRef["agent"] = "changed"
|
||||
|
||||
if projected.CWD != "/prepared" || projected.Env[0] != "FORK_ENV=prepared" ||
|
||||
projected.RuntimeContext["origin"] != "prepared" ||
|
||||
projected.ProviderTargetRef["agent"] != "codex" ||
|
||||
projected.Settings == nil || projected.Settings.Model != "gpt-5.6" {
|
||||
t.Fatalf("projected prepared identity=%#v", projected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerProjectsProviderEnrichedLiveState(t *testing.T) {
|
||||
backend := &stateRuntimeBackend{
|
||||
session: agentruntime.Session{
|
||||
RoomID: "workspace-1", AgentSessionID: "session-1", Provider: "codex",
|
||||
ProviderSessionID: "base-provider-session", Status: "starting",
|
||||
RuntimeContext: map[string]any{"base": true},
|
||||
Settings: &agentruntime.SessionSettings{Model: "base-model"},
|
||||
UpdatedAtUnixMS: 10,
|
||||
},
|
||||
state: agentruntime.SessionStateSnapshot{
|
||||
ProviderSessionID: "live-provider-session",
|
||||
Status: "ready",
|
||||
Settings: &agentruntime.SessionSettings{
|
||||
Model: "gpt-5.6", ReasoningEffort: "max", Speed: "fast",
|
||||
},
|
||||
Capabilities: canonical.NewCapabilitySnapshot([]string{canonical.CapabilityGoalPause}),
|
||||
RuntimeContext: map[string]any{
|
||||
"account": map[string]any{"email": "agent@example.com"},
|
||||
"rateLimits": map[string]any{"primary": 42},
|
||||
"usage": map[string]any{"usedTokens": 1200},
|
||||
"commands": []string{"compact", "status"},
|
||||
},
|
||||
UpdatedAtUnixMS: 20,
|
||||
},
|
||||
}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
|
||||
projected, found := controller.Session("workspace-1", "session-1")
|
||||
if !found {
|
||||
t.Fatal("Session() found = false")
|
||||
}
|
||||
if projected.ProviderSessionID != "live-provider-session" || projected.Status != "ready" || projected.UpdatedAtUnixMS != 20 {
|
||||
t.Fatalf("projected live identity/status = %#v", projected)
|
||||
}
|
||||
if projected.Settings == nil || projected.Settings.Model != "gpt-5.6" || projected.Settings.ReasoningEffort != "max" || projected.Settings.Speed != "fast" {
|
||||
t.Fatalf("projected live settings = %#v", projected.Settings)
|
||||
}
|
||||
if projected.RuntimeContext["account"] == nil || projected.RuntimeContext["rateLimits"] == nil || projected.RuntimeContext["usage"] == nil || projected.RuntimeContext["commands"] == nil {
|
||||
t.Fatalf("projected live runtime context = %#v", projected.RuntimeContext)
|
||||
}
|
||||
if projected.Capabilities == nil || len(projected.Capabilities.Values) != 1 || projected.Capabilities.Values[0] != canonical.CapabilityGoalPause {
|
||||
t.Fatalf("projected live capabilities = %#v", projected.Capabilities)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerFallsBackToBaseSessionWhenLiveStateFails(t *testing.T) {
|
||||
backend := &stateRuntimeBackend{
|
||||
session: agentruntime.Session{
|
||||
RoomID: "workspace-1", AgentSessionID: "session-1", Provider: "codex",
|
||||
Status: "starting", RuntimeContext: map[string]any{"base": true},
|
||||
},
|
||||
stateErr: errors.New("state unavailable"),
|
||||
}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
|
||||
projected, found := controller.Session("workspace-1", "session-1")
|
||||
if !found || projected.Status != "starting" || projected.RuntimeContext["base"] != true {
|
||||
t.Fatalf("Session() = %#v found=%v, want base observation", projected, found)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerFailsClosedWithoutBackend(t *testing.T) {
|
||||
controller := &RuntimeController{}
|
||||
if _, err := controller.Start(t.Context(), host.RuntimeStartInput{}); err == nil {
|
||||
t.Fatal("Start succeeded without a runtime backend")
|
||||
}
|
||||
if controller.CanResume(host.RuntimeResumeInput{}) {
|
||||
t.Fatal("CanResume reported support without a runtime backend")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerDelegatesDurableSubmitProvenance(t *testing.T) {
|
||||
backend := &provenanceRuntimeBackend{}
|
||||
controller := &RuntimeController{Backend: backend}
|
||||
input := host.RuntimeSubmitProvenanceInput{
|
||||
WorkspaceID: " workspace-1 ", AgentSessionID: "session-1", TurnID: "turn-1",
|
||||
ClientSubmitID: "submit-1", DisplayPrompt: "display", Guidance: true,
|
||||
CanonicalSubmitOccurredAtUnixMS: 1_234,
|
||||
Content: []host.PromptContentBlock{{Type: "text", Text: "hello"}},
|
||||
}
|
||||
|
||||
if err := controller.DurablyReportSubmitProvenance(t.Context(), input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if backend.input.RoomID != input.WorkspaceID || backend.input.AgentSessionID != input.AgentSessionID ||
|
||||
backend.input.TurnID != input.TurnID || backend.input.ClientSubmitID != input.ClientSubmitID ||
|
||||
backend.input.CanonicalSubmitOccurredAtUnixMS != input.CanonicalSubmitOccurredAtUnixMS ||
|
||||
backend.input.DisplayPrompt != input.DisplayPrompt || !backend.input.Guidance {
|
||||
t.Fatalf("delegated provenance = %#v", backend.input)
|
||||
}
|
||||
if len(backend.input.Content) != 1 || backend.input.Content[0].Text != "hello" {
|
||||
t.Fatalf("delegated content = %#v", backend.input.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeControllerPreservesTypedExecIdentity(t *testing.T) {
|
||||
input := host.RuntimeExecInput{
|
||||
WorkspaceID: "workspace-1", AgentSessionID: "session-1", TurnID: "turn-1", ClientSubmitID: "submit-1",
|
||||
CanonicalSubmitOccurredAtUnixMS: 1_234,
|
||||
CapabilityRefs: []host.CapabilityReference{{Capability: "browser-use", Source: "composer"}},
|
||||
TuttiModeSnapshot: &host.TuttiModeTurnSnapshot{
|
||||
ActivationID: "activation-1", RevisionID: "revision-1", Revision: 2,
|
||||
State: "active", Source: "workspace",
|
||||
PreferenceVersion: host.TuttiModePreferenceVersionEffectSpeed,
|
||||
Effect: 75, Speed: 60, OrchestrationIntensity: 75,
|
||||
},
|
||||
}
|
||||
|
||||
projected := runtimeExecInput(input)
|
||||
if projected.TurnID != input.TurnID || projected.ClientSubmitID != input.ClientSubmitID ||
|
||||
projected.CanonicalSubmitOccurredAtUnixMS != input.CanonicalSubmitOccurredAtUnixMS {
|
||||
t.Fatalf("projected exec identity = %#v", projected)
|
||||
}
|
||||
if len(projected.CapabilityRefs) != 1 || projected.CapabilityRefs[0].Capability != "browser-use" || projected.CapabilityRefs[0].Source != "composer" {
|
||||
t.Fatalf("projected capability refs = %#v", projected.CapabilityRefs)
|
||||
}
|
||||
if projected.TuttiModeSnapshot == nil {
|
||||
t.Fatal("projected Tutti Mode snapshot is nil")
|
||||
}
|
||||
legacyOrchestrationIntensity := projected.TuttiModeSnapshot.OrchestrationIntensity //nolint:staticcheck // Compatibility assertion covers the deprecated alias.
|
||||
if projected.TuttiModeSnapshot.ActivationID != "activation-1" ||
|
||||
projected.TuttiModeSnapshot.RevisionID != "revision-1" || projected.TuttiModeSnapshot.Revision != 2 ||
|
||||
projected.TuttiModeSnapshot.State != "active" || projected.TuttiModeSnapshot.Source != "workspace" ||
|
||||
projected.TuttiModeSnapshot.PreferenceVersion != agentruntime.TuttiModePreferenceVersionEffectSpeed ||
|
||||
projected.TuttiModeSnapshot.Effect != 75 || projected.TuttiModeSnapshot.Speed != 60 ||
|
||||
legacyOrchestrationIntensity != 75 {
|
||||
t.Fatalf("projected Tutti Mode snapshot = %#v", projected.TuttiModeSnapshot)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
type providerTurnBindingRecoveryBackend interface {
|
||||
RecoverProviderTurnBinding(
|
||||
context.Context,
|
||||
agentruntime.ProviderTurnBindingRecoveryInput,
|
||||
) (agentruntime.ProviderTurnBindingRecoveryResult, error)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) RecoverProviderTurnBinding(
|
||||
ctx context.Context,
|
||||
input host.RuntimeProviderTurnBindingRecoveryInput,
|
||||
) (host.RuntimeProviderTurnBindingRecoveryResult, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return host.RuntimeProviderTurnBindingRecoveryResult{}, err
|
||||
}
|
||||
backend, ok := a.Backend.(providerTurnBindingRecoveryBackend)
|
||||
if !ok {
|
||||
return host.RuntimeProviderTurnBindingRecoveryResult{},
|
||||
host.ErrSessionForkUnsupported
|
||||
}
|
||||
result, err := backend.RecoverProviderTurnBinding(
|
||||
ctx,
|
||||
agentruntime.ProviderTurnBindingRecoveryInput{
|
||||
Source: runtimeSession(input.Source),
|
||||
CanonicalTurnID: input.CanonicalTurnID,
|
||||
RecoveryToken: input.RecoveryToken,
|
||||
LegacyTextHMACKey: input.LegacyTextHMACKey,
|
||||
LegacyTextHMACDigest: input.LegacyTextHMACDigest,
|
||||
},
|
||||
)
|
||||
return host.RuntimeProviderTurnBindingRecoveryResult{
|
||||
ProviderSessionID: result.ProviderSessionID,
|
||||
ProviderTurnID: result.ProviderTurnID,
|
||||
ProviderTurnBindingJSON: append([]byte(nil), result.ProviderTurnBindingJSON...),
|
||||
}, mapRuntimeError(err)
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package hostadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
agentruntime "github.com/tutti-os/tutti/packages/agent/daemon/runtime"
|
||||
host "github.com/tutti-os/tutti/packages/agent/host"
|
||||
)
|
||||
|
||||
type runtimeWorkspaceDisconnectBackend interface {
|
||||
RuntimeSessions(context.Context, string) ([]agentruntime.Session, error)
|
||||
DisconnectRuntimeSession(context.Context, string, string) (agentruntime.DisconnectRuntimeSessionResult, error)
|
||||
}
|
||||
|
||||
type runtimeWorkspaceDisconnectTargetBackend interface {
|
||||
SnapshotRuntimeDisconnectTargets(string) []agentruntime.RuntimeDisconnectTarget
|
||||
DisconnectRuntimeSessionTarget(context.Context, agentruntime.RuntimeDisconnectTarget) (agentruntime.DisconnectRuntimeSessionResult, error)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) WorkspaceRuntimeSessions(ctx context.Context, workspaceID string) ([]host.ProviderRuntimeSession, error) {
|
||||
if a == nil || a.Backend == nil {
|
||||
return nil, host.ErrWorkspaceDisconnectUnavailable
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeWorkspaceDisconnectBackend)
|
||||
if !ok {
|
||||
return nil, host.ErrWorkspaceDisconnectUnavailable
|
||||
}
|
||||
sessions, err := backend.RuntimeSessions(ctx, strings.TrimSpace(workspaceID))
|
||||
if err != nil {
|
||||
return nil, mapRuntimeError(err)
|
||||
}
|
||||
result := make([]host.ProviderRuntimeSession, 0, len(sessions))
|
||||
for _, session := range sessions {
|
||||
result = append(result, a.sessionWithState(session))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *RuntimeController) DisconnectRuntimeSession(
|
||||
ctx context.Context,
|
||||
ref host.SessionRef,
|
||||
) (bool, error) {
|
||||
if err := a.requireBackend(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeWorkspaceDisconnectBackend)
|
||||
if !ok {
|
||||
return false, host.ErrWorkspaceDisconnectUnavailable
|
||||
}
|
||||
result, err := backend.DisconnectRuntimeSession(
|
||||
ctx,
|
||||
strings.TrimSpace(ref.WorkspaceID),
|
||||
strings.TrimSpace(ref.AgentSessionID),
|
||||
)
|
||||
return result.Disconnected, mapRuntimeError(err)
|
||||
}
|
||||
|
||||
func (a *RuntimeController) SnapshotWorkspaceRuntimeDisconnectTargets(workspaceID string) []host.RuntimeDisconnectTarget {
|
||||
if a == nil || a.Backend == nil {
|
||||
return nil
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeWorkspaceDisconnectTargetBackend)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
targets := backend.SnapshotRuntimeDisconnectTargets(strings.TrimSpace(workspaceID))
|
||||
result := make([]host.RuntimeDisconnectTarget, 0, len(targets))
|
||||
for _, target := range targets {
|
||||
result = append(result, host.RuntimeDisconnectTarget{
|
||||
WorkspaceID: target.RoomID, AgentSessionID: target.AgentSessionID,
|
||||
ConnectionGeneration: target.ConnectionGeneration,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (a *RuntimeController) DisconnectRuntimeSessionTarget(
|
||||
ctx context.Context,
|
||||
target host.RuntimeDisconnectTarget,
|
||||
) (bool, error) {
|
||||
if a == nil || a.Backend == nil {
|
||||
return false, host.ErrWorkspaceDisconnectUnavailable
|
||||
}
|
||||
backend, ok := a.Backend.(runtimeWorkspaceDisconnectTargetBackend)
|
||||
if !ok {
|
||||
return false, host.ErrWorkspaceDisconnectUnavailable
|
||||
}
|
||||
result, err := backend.DisconnectRuntimeSessionTarget(ctx, agentruntime.RuntimeDisconnectTarget{
|
||||
RoomID: target.WorkspaceID, AgentSessionID: target.AgentSessionID,
|
||||
ConnectionGeneration: target.ConnectionGeneration,
|
||||
})
|
||||
return result.Disconnected, mapRuntimeError(err)
|
||||
}
|
||||
Reference in new issue
Block a user