Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(sandbox): rebind when attachment staging loses its container (#4220)
AsanoReo1c0abaabb0da
diff --git a/internal/sandbox/remote_fake_test.go b/internal/sandbox/remote_fake_test.goindex bf7989b9a..9e02520c0 100644--- a/internal/sandbox/remote_fake_test.go+++ b/internal/sandbox/remote_fake_test.go@@ -96,10 +96,12 @@ type fakeRemoteClient struct {deleteHook func(context.Context, string) errorexecHook func(RemoteExecRequest) (*RemoteExecResult, error)- makeDirPaths []string- failMakeDirIfExists bool- execRequests []RemoteExecRequest- writeFiles []fakeRemoteWriteFile+ makeDirPaths []string+ failMakeDirIfExists bool+ vanishOnFirstMakeDir bool+ missingPathMakeDir bool+ execRequests []RemoteExecRequest+ writeFiles []fakeRemoteWriteFile// snapshots maps snapshotID -> source sandboxID.snapshots map[string]string@@ -377,9 +379,19 @@ func (c *fakeRemoteClient) ListDir(return nil, nil}-func (c *fakeRemoteClient) MakeDir(_ context.Context, _ RemoteSandboxHandle, path string) error {+func (c *fakeRemoteClient) MakeDir(_ context.Context, handle RemoteSandboxHandle, path string) error {c.mu.Lock()defer c.mu.Unlock()+ if c.missingPathMakeDir {+ return NewRemoteError(c.provider, "MakeDir", RemoteErrorKindNotFound, path+" does not exist", nil)+ }+ if c.vanishOnFirstMakeDir {+ c.vanishOnFirstMakeDir = false+ if handle != nil {+ delete(c.sandboxes, handle.ID())+ }+ return NewRemoteError(c.provider, "MakeDir", RemoteErrorKindNotFound, "container vanished mid-op", nil)+ }if c.failMakeDirIfExists {for _, existing := range c.makeDirPaths {if existing == path {diff --git a/internal/sandbox/session_input_rebind_test.go b/internal/sandbox/session_input_rebind_test.gonew file mode 100644index 000000000..fbaf099e1--- /dev/null+++ b/internal/sandbox/session_input_rebind_test.go@@ -0,0 +1,59 @@+package sandbox++import (+ "context"+ "testing"++ "github.com/Tencent/WeKnora/internal/types"+ "github.com/stretchr/testify/require"+)++func TestWriteSessionInputFileRebindsAfterTheContainerVanishes(t *testing.T) {+ client := newFakeRemoteClient(SandboxTypeCube)+ client.vanishOnFirstMakeDir = true+ cfg := DefaultConfig()+ cfg.CubeTemplate = "tpl-test"+ mgr, err := NewSessionBoundManager(SessionBoundManagerConfig{+ Config: cfg,+ Client: client,+ Store: NewMemorySessionSandboxBindingStore(),+ Checker: &fakeSessionExistenceChecker{exists: true},+ SkipHealthProbe: true,+ })+ require.NoError(t, err)++ ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(10000))+ err = mgr.WriteSessionInputFile(ctx, "session-a", SessionInputRoot+"/note.txt", []byte("hi"))+ require.NoError(t, err)++ client.mu.Lock()+ defer client.mu.Unlock()+ require.Equal(t, 2, client.createCount)+ require.Len(t, client.writeFiles, 1)+ require.Equal(t, SessionInputRoot+"/note.txt", client.writeFiles[0].path)+ require.Equal(t, []byte("hi"), client.writeFiles[0].content)+}++func TestWriteSessionInputFileKeepsTheBindingWhenThePathIsMissing(t *testing.T) {+ client := newFakeRemoteClient(SandboxTypeCube)+ client.missingPathMakeDir = true+ cfg := DefaultConfig()+ cfg.CubeTemplate = "tpl-test"+ mgr, err := NewSessionBoundManager(SessionBoundManagerConfig{+ Config: cfg,+ Client: client,+ Store: NewMemorySessionSandboxBindingStore(),+ Checker: &fakeSessionExistenceChecker{exists: true},+ SkipHealthProbe: true,+ })+ require.NoError(t, err)++ ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(10000))+ err = mgr.WriteSessionInputFile(ctx, "session-a", SessionInputRoot+"/note.txt", []byte("hi"))+ require.Error(t, err)++ client.mu.Lock()+ defer client.mu.Unlock()+ require.Equal(t, 1, client.createCount)+ require.Empty(t, client.writeFiles)+}diff --git a/internal/sandbox/session_manager.go b/internal/sandbox/session_manager.goindex 1fb308c89..423e5aab1 100644--- a/internal/sandbox/session_manager.go+++ b/internal/sandbox/session_manager.go@@ -674,10 +674,30 @@ func (m *SessionBoundManager) WriteSessionInputFile(return err}defer release()- handle, err := m.resolveScope(ctx, sc)- if err != nil {- return err+ var handle RemoteSandboxHandle+ for attempt := 0; attempt < 2; attempt++ {+ handle, err = m.resolveScope(ctx, sc)+ if err != nil {+ return err+ }+ err = m.writeSessionInputOnHandle(ctx, handle, clean, content)+ if err == nil || attempt == 1 || !sessionBindingLost(err) {+ return err+ }+ // The idle sweep can delete the container after resolve and before+ // mkdir. The binding still points at that container, so drop it and+ // provision once more. A missing path inside a live container does+ // not take this branch.+ if dropErr := m.dropBoundSandbox(ctx, sc.machine(), handle.ID()); dropErr != nil {+ return err+ }}+ return err+}++func (m *SessionBoundManager) writeSessionInputOnHandle(+ ctx context.Context, handle RemoteSandboxHandle, clean string, content []byte,+) error {if err := ignoreExistingDir(m.client.MakeDir(ctx, handle, path.Dir(clean))); err != nil {return fmt.Errorf("sandbox: create input directory: %w", err)}@@ -687,6 +707,29 @@ func (m *SessionBoundManager) WriteSessionInputFile(return nil}+// sessionBindingLost reports a sandbox that is gone. A missing path inside a+// live container is also NotFound ("does not exist"); rebinding on that+// would discard the session.+func sessionBindingLost(err error) bool {+ if !CanReplaceRemoteBinding(err) {+ return false+ }+ msg := strings.ToLower(err.Error())+ if strings.Contains(msg, "does not exist") && !strings.Contains(msg, "vanished") {+ return false+ }+ return true+}++func (m *SessionBoundManager) dropBoundSandbox(ctx context.Context, key SessionSandboxKey, sandboxID string) error {+ binding, err := m.bindings.Get(ctx, key)+ if err != nil || binding == nil || binding.SandboxID != sandboxID || binding.Provider != m.client.Provider() {+ return err+ }+ _, err = m.bindings.DeleteIfMatch(ctx, key, binding.Provider, binding.SandboxID)+ return err+}+// WriteSessionWorkspaceFile writes a model-authored file into the session's// remote sandbox, provisioning the sandbox on first call. Paths must sit// inside the session sandbox and must not land in /workspace/input.