InMemoryWorkspace
NexusLabs.Needlr.AgentFramework¶
NexusLabs.Needlr.AgentFramework.Workspace¶
InMemoryWorkspace Class¶
Thread-safe in-memory IWorkspace backed by a System.Collections.Concurrent.ConcurrentDictionary<>. Suitable for testing, sandboxed agent runs, and scenarios where persistence is not needed.
Inheritance System.Object 🡒 InMemoryWorkspace
Implements IWorkspace
Remarks¶
Marked NexusLabs.Needlr.DoNotAutoRegisterAttribute to prevent Needlr from registering it as a singleton — workspaces have per-orchestration lifecycle and must be explicitly constructed.
Paths are normalized: backslashes are replaced with forward slashes, leading slashes are trimmed, and comparison is case-insensitive (matching Windows file system behavior by default).
Methods¶
InMemoryWorkspace.FileExists(string) Method¶
Returns whether a file exists at the given path.
Parameters¶
path System.String
Implements FileExists(string)
Returns¶
InMemoryWorkspace.GetFilePaths() Method¶
Returns all file paths in the workspace.
Implements GetFilePaths()
Returns¶
System.Collections.Generic.IEnumerable<System.String>
InMemoryWorkspace.ListDirectory(string, int) Method¶
Produces a tree-formatted directory listing starting at directory, descending up to maxDepth levels. The output format is implementation-defined but should be human-readable (tree characters, indentation, file/directory markers).
Parameters¶
directory System.String
The root directory to list. Use "" or "." for the workspace root.
maxDepth System.Int32
Maximum directory depth to descend. Defaults to 2.
Implements ListDirectory(string, int)
Returns¶
System.String
A formatted string representing the directory tree.
InMemoryWorkspace.ReadFileAsMemory(string) Method¶
Reads the content of a file as a System.ReadOnlyMemory<> of System.Char. Implementations that store content as strings can return a zero-copy slice over the internal string via System.MemoryExtensions, avoiding per-read allocation.
Parameters¶
path System.String
Implements ReadFileAsMemory(string)
Returns¶
System.ReadOnlyMemory<System.Char>
Remarks¶
Callers that enumerate lines (e.g., MemoryExtensions.EnumerateLines()) benefit
from the zero-copy path. Callers that need a System.String should use
TryReadFile(string) instead.
InMemoryWorkspace.SeedFile(string, string) Method¶
Seeds the workspace with a file before agent execution begins. Convenience method for test setup and scenario harnesses.
Parameters¶
path System.String
content System.String
InMemoryWorkspace.TryCompareExchange(string, string, string) Method¶
Atomically replaces file content only if the current content matches expectedContent. Returns a structured result indicating whether the exchange succeeded and why it failed if it didn't.
public NexusLabs.Needlr.AgentFramework.Workspace.WorkspaceResult<NexusLabs.Needlr.AgentFramework.Workspace.CompareExchangeResult> TryCompareExchange(string path, string expectedContent, string newContent);
Parameters¶
path System.String
expectedContent System.String
newContent System.String
Implements TryCompareExchange(string, string, string)
Returns¶
NexusLabs.Needlr.AgentFramework.Workspace.WorkspaceResult<CompareExchangeResult>
Remarks¶
Enables optimistic concurrency for multi-agent pipelines where multiple agents may edit the same file. Callers read, transform, then CAS — if another agent wrote in between, the CAS fails and the caller can re-read and retry.
InMemoryWorkspace.TryReadFile(string) Method¶
Reads the entire content of a file.
public NexusLabs.Needlr.AgentFramework.Workspace.WorkspaceResult<NexusLabs.Needlr.AgentFramework.Workspace.ReadFileResult> TryReadFile(string path);
Parameters¶
path System.String
Implements TryReadFile(string)
Returns¶
NexusLabs.Needlr.AgentFramework.Workspace.WorkspaceResult<ReadFileResult>
InMemoryWorkspace.TryWriteFile(string, string) Method¶
Writes content to a file, creating or overwriting it.
public NexusLabs.Needlr.AgentFramework.Workspace.WorkspaceResult<NexusLabs.Needlr.AgentFramework.Workspace.WriteFileResult> TryWriteFile(string path, string content);
Parameters¶
path System.String
content System.String
Implements TryWriteFile(string, string)
Returns¶
NexusLabs.Needlr.AgentFramework.Workspace.WorkspaceResult<WriteFileResult>