Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// 1. Skills — package finance know-how (valuation, risk-scoring) as discoverable SKILL.md
// files the agent loads on demand. Optionally fold in centrally-managed Foundry
// skills from a Foundry Toolbox MCP endpoint (opt-in via FOUNDRY_TOOLBOX_MCP_SERVER_URL).
// 2. Shell — a sandboxed shell, confined to the trade-confirmation vault, that the agent
// uses to reorganize the accumulated confirmation files (year/month, rename,
// archive). Guarded by a deny-list policy and a confined working directory.
// 2. Shell — a local shell that starts each command in the trade-confirmation vault, which
// the agent uses to reorganize the accumulated confirmation files (year/month,
// rename, archive). Every command needs approval; the shell is not sandboxed.
// 3. CodeAct — the agent writes and runs Python to crunch portfolio numbers, in a sandboxed
// Hyperlight micro-VM (needs hardware virtualization).
// 4. Background agents — fan out a per-ticker research sub-agent so several tickers are researched
Expand Down Expand Up @@ -130,13 +130,14 @@ research before making decisions.
// </background>

// <shell>
// A sandboxed shell, confined to the trade-confirmation vault. ConfineWorkingDirectory re-anchors
// every command to the vault, and the deny-list policy pre-filters obviously destructive commands.
// (Patterns are a UX guardrail, not a security boundary — for hard isolation use DockerShellExecutor.)
// A local shell that starts each command in the trade-confirmation vault. By default the executor
// moves every command back to the vault before it runs, and the deny-list policy pre-filters obviously
// destructive commands. Neither is a security boundary: a command can still reach files outside the
// vault (for example with `cd ..` or an absolute path). Each command needs approval, which is the
// actual check. For hard isolation use DockerShellExecutor.
await using var shellExecutor = new LocalShellExecutor(new LocalShellExecutorOptions
{
WorkingDirectory = vaultDir,
ConfineWorkingDirectory = true,
Policy = new ShellPolicy(denyList:
[
@"\brm\s+-rf\b",
Expand All @@ -158,7 +159,7 @@ research before making decisions.

// <create_agent>
// Turn the chat client into a HarnessAgent. On top of Post 2's file access and approvals we add the
// four "scaling" capabilities: skills (our own provider), background agents, a confined shell, and
// four "scaling" capabilities: skills (our own provider), background agents, an approval-gated shell, and
// CodeAct.
// The shell is wired up in two parts: the ShellEnvironmentProvider injects OS/shell/CWD info into the
// system prompt, and the shell tool is registered below in ChatOptions.
Expand Down Expand Up @@ -188,7 +189,7 @@ research before making decisions.
[
StockTools.CreateGetStockPriceTool(),
TradingTools.CreatePlaceTradeTool(),
// The confined shell, exposed as the approval-gated run_shell tool.
// The local shell, exposed as the approval-gated run_shell tool.
shellExecutor.AsAIFunction(requireApproval: true),
],
Reasoning = new() { Effort = ReasoningEffort.Medium },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ It builds on Post 2's personal finance assistant and makes it *more capable* alo
harness default. Optionally folds in centrally-managed **Foundry skills** discovered live from a
Foundry **Toolbox MCP** endpoint via `FoundrySkills.ConnectAsync(...)` + `UseMcpSkills(...)`
(opt-in; see below).
- **Shell** — a `LocalShellExecutor` confined to the trade-confirmation vault
- **Shell** — a `LocalShellExecutor` that starts each command in the trade-confirmation vault
(`working/confirmations/`) lets the agent tidy the accumulated confirmation files (reorganize into
`year/month`, rename to `YYYY-MM-DD_TICKER_BUY|SELL.txt`). `ConfineWorkingDirectory` re-anchors
every command to the vault and a `ShellPolicy` deny-list pre-filters obviously destructive
commands. Exposed as the `run_shell` tool, which prompts for approval before each command runs.
(The deny-list is a UX guardrail, not a security boundary — for hard isolation use a
`DockerShellExecutor`.)
`year/month`, rename to `YYYY-MM-DD_TICKER_BUY|SELL.txt`). By default the executor moves
every command back to the vault before it runs and a `ShellPolicy` deny-list pre-filters obviously
destructive commands. Exposed as the `run_shell` tool, which prompts for approval before each
command runs. (Neither the working directory nor the deny-list is a security boundary: a command
can still reach files outside the vault. For hard isolation use a `DockerShellExecutor`.)
- **CodeAct** — a `HyperlightCodeActProvider` gives the agent a sandboxed Python interpreter to
crunch portfolio numbers by writing and running code. It runs on Hyperlight (a micro-VM), so it
requires hardware virtualization. The guest module path is resolved automatically from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ public static async Task<ClawAgentBuild> CreateAsync(ClawAgentFactoryOptions? op
AgentSkillsProvider skillsProvider = skillsBuilder.Build();
AIAgent researchAgent = ResearchAgent.Create(chatClient);

// Shell access is a powerful capability. It is confined to the local vault directory with a
// deny-list here, but on shared/hosted deployments it is disabled entirely (see hosted host).
// Shell access is a powerful capability. Commands start in the local vault directory and a
// deny-list filters obvious mistakes, but neither stops a command from reaching other files;
// approval is the actual check. On shared/hosted deployments it is disabled entirely (see hosted host).
LocalShellExecutor? shell = null;
if (options.EnableShell)
{
shell = new LocalShellExecutor(new LocalShellExecutorOptions
{
WorkingDirectory = vaultDir,
ConfineWorkingDirectory = true,
Policy = new ShellPolicy(denyList:
[
@"\brm\s+-rf\b",
Expand All @@ -164,7 +164,7 @@ public static async Task<ClawAgentBuild> CreateAsync(ClawAgentFactoryOptions? op
]),
Timeout = TimeSpan.FromSeconds(15),
});
log("Shell enabled (confined to the confirmations vault). ");
log("Shell enabled (commands start in the confirmations vault and require approval). ");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Shared class library for the production-ready personal finance claw.

`ClawAgentFactory.CreateAsync(...)` returns a `ClawAgentBuild` containing the fully configured `AIAgent` plus disposable resources. It preserves the Step 03 capabilities: Foundry Responses `IChatClient`, local file skills, optional Foundry Toolbox MCP skills, background research agent, confined `LocalShellExecutor`, Hyperlight CodeAct, file access, approvals, agent modes, stock tools, and trading tools.
`ClawAgentFactory.CreateAsync(...)` returns a `ClawAgentBuild` containing the fully configured `AIAgent` plus disposable resources. It preserves the Step 03 capabilities: Foundry Responses `IChatClient`, local file skills, optional Foundry Toolbox MCP skills, background research agent, approval-gated `LocalShellExecutor`, Hyperlight CodeAct, file access, approvals, agent modes, stock tools, and trading tools.

Purview is opt-in via `PURVIEW_CLIENT_APP_ID`; when unset, the chat client is not wrapped. Telemetry is always enabled through `HarnessAgentOptions.OpenTelemetrySourceName`.
2 changes: 1 addition & 1 deletion dotnet/samples/02-agents/Harness/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Samples accompanying the [*Build your own agent harness or claw with Microsoft A
| --- | --- |
| [Claw_Step01_MeetYourClaw](./BuildYourOwnClaw/Claw_Step01_MeetYourClaw/README.md) | Post 1 — a minimal HarnessAgent with a custom `get_stock_price` tool, web search, and planning |
| [Claw_Step02_WorkingWithData](./BuildYourOwnClaw/Claw_Step02_WorkingWithData/README.md) | Post 2 — file access, approvals, and durable memory (file memory plus optional Foundry memory) |
| [Claw_Step03_ScalingCapabilities](./BuildYourOwnClaw/Claw_Step03_ScalingCapabilities/README.md) | Post 3 — scaling the claw with skills (plus optional Foundry skills), a confined shell, CodeAct, and background agents |
| [Claw_Step03_ScalingCapabilities](./BuildYourOwnClaw/Claw_Step03_ScalingCapabilities/README.md) | Post 3 — scaling the claw with skills (plus optional Foundry skills), an approval-gated local shell, CodeAct, and background agents |
| [Claw_Step04_ProductionReady](./BuildYourOwnClaw/Claw_Step04_ProductionReady/README.md) | Post 4 — production-ready shared claw library with observability, opt-in Purview governance, Foundry hosted deployment, and evals |

## Foundry hosted Harness sample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public override async Task InitializeAsync(CancellationToken cancellationToken =
this._session = new ShellSession(
inner,
workingDirectory: null, // workdir is set on the container itself
confineWorkingDirectory: false,
resetWorkingDirectoryEachCall: false,
environment: null,
cleanEnvironment: false,
maxOutputBytes: this._maxOutputBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public sealed class LocalShellExecutor : ShellExecutor
private readonly TimeSpan? _timeout;
private readonly int _maxOutputBytes;
private readonly string? _workingDirectory;
private readonly bool _confineWorkingDirectory;
private readonly bool _resetWorkingDirectoryEachCall;
private readonly IReadOnlyDictionary<string, string?>? _environment;
private readonly bool _cleanEnvironment;
private readonly bool _acknowledgeUnsafe;
Expand Down Expand Up @@ -109,7 +109,7 @@ public LocalShellExecutor(LocalShellExecutorOptions options)
this._timeout = options.Timeout;
this._maxOutputBytes = options.MaxOutputBytes;
this._workingDirectory = options.WorkingDirectory;
this._confineWorkingDirectory = options.ConfineWorkingDirectory;
this._resetWorkingDirectoryEachCall = !options.DisableWorkingDirectoryResetOnEachCall;
this._environment = options.Environment;
this._cleanEnvironment = options.CleanEnvironment;
this._acknowledgeUnsafe = options.AcknowledgeUnsafe;
Expand Down Expand Up @@ -162,7 +162,7 @@ private async Task<ShellResult> RunPersistentAsync(string command, CancellationT
this._session ??= new ShellSession(
this._shell,
this._workingDirectory,
this._confineWorkingDirectory,
this._resetWorkingDirectoryEachCall,
this._environment,
this._cleanEnvironment,
this._maxOutputBytes);
Expand All @@ -186,7 +186,7 @@ public override Task InitializeAsync(CancellationToken cancellationToken = defau
this._session ??= new ShellSession(
this._shell,
this._workingDirectory,
this._confineWorkingDirectory,
this._resetWorkingDirectoryEachCall,
this._environment,
this._cleanEnvironment,
this._maxOutputBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,34 @@ public sealed class LocalShellExecutorOptions

/// <summary>
/// Working directory for the spawned shell. Defaults to the current
/// process directory. Required when <see cref="ConfineWorkingDirectory"/>
/// is <see langword="true"/>.
/// process directory. This is only where commands start; it does not
/// limit which files a command can reach.
/// In persistent mode, each command starts here again unless
/// <see cref="DisableWorkingDirectoryResetOnEachCall"/> is set.
/// </summary>
public string? WorkingDirectory { get; set; }

/// <summary>
/// When <see langword="true"/> (the default), every command in
/// When <see langword="false"/> (the default), every command in
/// persistent mode is prefixed with a <c>cd</c> back into
/// <see cref="WorkingDirectory"/> so a wandering <c>cd</c> in one call
/// doesn't leak to the next.
/// <see cref="WorkingDirectory"/> so a <c>cd</c> in one call
/// doesn't carry over to the next. Set to <see langword="true"/> to let
/// a <c>cd</c> carry over to later calls. The reset only happens when
/// <see cref="WorkingDirectory"/> is set. This option has no effect in
/// stateless mode, where every command already starts in
/// <see cref="WorkingDirectory"/>.
/// </summary>
public bool ConfineWorkingDirectory { get; set; } = true;
/// <remarks>
/// The reset does not restrict file access. It only sets where a command
/// starts. A command can still change directory or use absolute paths
/// within the same call: <c>cd /tmp &amp;&amp; rm -rf .</c> touches
/// <c>/tmp</c>, and <c>cat /etc/passwd</c> reads a file outside
/// <see cref="WorkingDirectory"/>. <see cref="ShellPolicy"/> only filters
/// command text; it does not restrict file access either. To limit what
/// commands can reach, use an executor with separately enforced isolation
/// and restricted permissions, such as <see cref="DockerShellExecutor"/>.
/// </remarks>
public bool DisableWorkingDirectoryResetOnEachCall { get; set; }

/// <summary>
/// Extra environment variables. Pass a <see langword="null"/> value to
Expand Down
19 changes: 10 additions & 9 deletions dotnet/src/Microsoft.Agents.AI.Tools.Shell/ShellSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal sealed class ShellSession : IAsyncDisposable

private readonly ResolvedShell _shell;
private readonly string? _workingDirectory;
private readonly bool _confineWorkingDirectory;
private readonly bool _resetWorkingDirectoryEachCall;
private readonly IReadOnlyDictionary<string, string?>? _environment;
private readonly bool _cleanEnvironment;
private readonly int _maxOutputBytes;
Expand All @@ -95,14 +95,14 @@ internal sealed class ShellSession : IAsyncDisposable
public ShellSession(
ResolvedShell shell,
string? workingDirectory,
bool confineWorkingDirectory,
bool resetWorkingDirectoryEachCall,
IReadOnlyDictionary<string, string?>? environment,
bool cleanEnvironment,
int maxOutputBytes)
{
this._shell = shell;
this._workingDirectory = workingDirectory;
this._confineWorkingDirectory = confineWorkingDirectory;
this._resetWorkingDirectoryEachCall = resetWorkingDirectoryEachCall;
this._environment = environment;
this._cleanEnvironment = cleanEnvironment;
this._maxOutputBytes = maxOutputBytes;
Expand Down Expand Up @@ -563,10 +563,11 @@ private static int ParseRc(byte[] tail, int newlineIdx)

private string BuildScript(string command, string sentinel)
{
// Idempotent re-anchor: in confined mode every command is prefixed
// with a `cd` back to the configured workdir so a `cd` inside one
// command doesn't leak to the next.
var effective = this.MaybeReanchor(command);
// Idempotent reset: when enabled, every command is prefixed with a
// `cd` back to the configured workdir so a `cd` inside one command
// doesn't leak to the next. This only sets the starting directory;
// it does not restrict what the command can reach.
var effective = this.MaybeResetWorkingDirectory(command);

if (this._shell.Kind == ShellKind.PowerShell)
{
Expand Down Expand Up @@ -619,9 +620,9 @@ private string BuildScript(string command, string sentinel)
$"printf '\\n{sentinel}_%s\\n' \"$__af_rc\"\n";
}

private string MaybeReanchor(string command)
private string MaybeResetWorkingDirectory(string command)
{
if (!this._confineWorkingDirectory || string.IsNullOrEmpty(this._workingDirectory))
if (!this._resetWorkingDirectoryEachCall || string.IsNullOrEmpty(this._workingDirectory))
{
return command;
}
Expand Down
Loading