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
26 changes: 25 additions & 1 deletion packages/plugin-sdk/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40945,6 +40945,11 @@ export const contracts: {
maxLength: 71,
pattern: '^sha256:[a-f0-9]{64}$',
},
imageOnly: {
description:
'When true (e.g. inline Markdown image rendering), stop after sniffing the\nheader if the file is not a supported image instead of buffering and streaming text.',
type: 'boolean',
},
},
additionalProperties: false,
required: ['sessionId', 'path'],
Expand Down Expand Up @@ -43701,12 +43706,31 @@ export const contracts: {
type: 'string',
},
path: {
$ref: '#/$defs/WorkspacePath',
$ref: '#/$defs/FileReadPath',
},
},
additionalProperties: false,
required: ['sessionId', 'path'],
$defs: {
FileReadPath: {
description:
'A file selected for reading. Only workspace paths are accepted by editing APIs.',
anyOf: [
{
$ref: '#/$defs/WorkspacePath',
},
{
type: 'object',
properties: {
absolute: {
type: 'string',
},
},
additionalProperties: false,
required: ['absolute'],
},
],
},
WorkspacePath: {
type: 'object',
properties: {
Expand Down
18 changes: 17 additions & 1 deletion packages/plugin-sdk/src/generated/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,14 @@ export type AgentRequestSystemPermissionOutput_ClosureReason =
export type AgentRequestSystemPermissionOutput_SystemPermissionGrant =
| 'accessibility'
| 'screen_recording';
/**
* A file selected for reading. Only workspace paths are accepted by editing APIs.
*/
export type AgentResolveRevealTargetInput_FileReadPath =
| AgentResolveRevealTargetInput_WorkspacePath
| {
absolute: string;
};
/**
* Session sandbox policy. Full access disables isolation and permits Computer Use
* without additional application, browser or audio consent.
Expand Down Expand Up @@ -5404,6 +5412,11 @@ export interface ServiceMethods {
sessionId: string;
path: AgentReadFileInput_FileReadPath;
expectedVersion?: string | null;
/**
* When true (e.g. inline Markdown image rendering), stop after sniffing the
* header if the file is not a supported image instead of buffering and streaming text.
*/
imageOnly?: boolean;
};
output: AgentReadFileOutput_AgentFileReadPart | null;
};
Expand Down Expand Up @@ -5531,7 +5544,7 @@ export interface ServiceMethods {
mode: 'query';
input: {
sessionId: string;
path: Runtime.WorkspacePath;
path: AgentResolveRevealTargetInput_FileReadPath;
};
output: string;
};
Expand Down Expand Up @@ -8405,6 +8418,9 @@ export interface AgentRequestSystemPermissionOutput_AgentSystemPermissionState {
grant: AgentRequestSystemPermissionOutput_SystemPermissionGrant;
effective: boolean;
}
export interface AgentResolveRevealTargetInput_WorkspacePath {
bytesBase64: string;
}
export interface AgentRestoreSessionCheckpointInput_AgentHistoryCheckpointRead {
sessionId: string;
/**
Expand Down
6 changes: 5 additions & 1 deletion plugins/agent/api/src/contract/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct FileLinkRead {
#[schemars(rename = "AgentWorkspaceFileReveal")]
pub struct WorkspaceFileReveal {
pub session_id: String,
pub path: WorkspacePath,
pub path: FileReadPath,
}

/// One bounded current-file capture. A version is a precondition, never read authority.
Expand All @@ -93,6 +93,10 @@ pub struct FileRead {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(length(min = 71, max = 71), regex(pattern = "^sha256:[a-f0-9]{64}$"))]
pub expected_version: Option<String>,
/// When true (e.g. inline Markdown image rendering), stop after sniffing the
/// header if the file is not a supported image instead of buffering and streaming text.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub image_only: bool,
}

/// All parts are subscription items; only End admits a complete preview.
Expand Down
69 changes: 51 additions & 18 deletions plugins/agent/native/src/owner/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,22 @@ impl Owner {
input: WorkspaceFileReveal,
scope: &Scope,
) -> Result<String, String> {
require_local_reveal(&scope.identity)?;
require_user_file_client(&scope.identity)?;
let (original, directory) = self.file_directory(&input.session_id, None, scope).await?;
let sandbox = self
.capture_sandbox(&original, scope.sandbox_floor())
.await?;
let selected = worktrees::blocking(scope, &self.stop, move |cancel| {
directory
.reveal_target(&input.path, reveal_policy(&sandbox)?.as_ref(), &cancel)
let policy = reveal_policy(&sandbox)?;
let target = match &input.path {
FileReadPath::Workspace(path) => {
directory.reveal_target(path, policy.as_ref(), &cancel)
}
FileReadPath::External { absolute } => {
Directory::reveal_external_target(Path::new(absolute))
}
};
target
.map_err(|e| {
if e.kind() == io::ErrorKind::NotFound {
"agent_reveal_missing".to_owned()
Expand All @@ -218,16 +226,19 @@ impl Owner {
input: FileLinkRead,
scope: &Scope,
) -> Result<FileReadPath, String> {
require_user_file_client(&scope.identity)?;
let (original, directory) = self.file_directory(&input.session_id, None, scope).await?;
let sandbox = self
.capture_sandbox(&original, scope.sandbox_floor())
.await?;
let home = self.home.clone();
let selected = worktrees::blocking(scope, &self.stop, move |_| {
require_local_preview_workspace(&sandbox)?;
let bytes = STANDARD
.decode(&input.path_bytes)
.map_err(|_| "agent_files_invalid_path")?;
directory
.resolve_link(bytes, read_policy(&sandbox)?.as_ref())
.resolve_link(bytes, home.as_deref())
.map_err(file_failure)
})
.await?;
Expand All @@ -242,6 +253,7 @@ impl Owner {
context: Context,
) -> Result<Value, String> {
let scope = context.scope().clone();
require_user_file_client(&scope.identity)?;
self.resource_work(&scope, move |owner, scope| {
Box::pin(async move {
let (original, directory) = owner
Expand All @@ -251,18 +263,27 @@ impl Owner {
.capture_sandbox(&original, scope.sandbox_floor())
.await?;
let capture = worktrees::blocking(&scope, &owner.stop, move |cancel| {
let policy = read_policy(&sandbox)?;
let version = input.expected_version.as_deref();
match &input.path {
FileReadPath::Workspace(path) => {
directory.read_permitted_file(path, version, policy.as_ref(), &cancel)
let policy = read_policy(&sandbox)?;
directory.read_permitted_file(
path,
version,
policy.as_ref(),
input.image_only,
&cancel,
)
}
FileReadPath::External { absolute } => {
require_local_preview_workspace(&sandbox)?;
Directory::read_external_file(
Path::new(absolute),
version,
input.image_only,
&cancel,
)
}
FileReadPath::External { absolute } => directory.read_external_image(
Path::new(absolute),
policy.as_ref(),
version,
&cancel,
),
}
.map_err(file_failure)
})
Expand Down Expand Up @@ -839,15 +860,23 @@ fn next_file_scan(elapsed: std::time::Duration) -> tokio::time::Instant {
tokio::time::Instant::now() + pause
}

fn require_local_reveal(identity: &maka_capabilities::call::Identity) -> Result<(), String> {
fn require_user_file_client(identity: &maka_capabilities::call::Identity) -> Result<(), String> {
use maka_capabilities::call::Identity;
match identity {
// The local desktop Workbench reaches the Host via Application Transport.
// User-facing Workbench/Web clients reach the Host via Native or Remote transport.
// Agent/background runs must use their sandboxed tools, never UI preview RPCs.
Identity::Native | Identity::Remote { .. } => Ok(()),
_ => Err("Only the local workspace client may reveal a file.".into()),
_ => Err("Only a user client may access file previews or reveal targets.".into()),
}
}

fn require_local_preview_workspace(sandbox: &maka_sandbox::Sandbox) -> Result<(), String> {
if matches!(sandbox, maka_sandbox::Sandbox::External { .. }) {
return Err("File preview requires a local workspace.".into());
}
Ok(())
}

fn reveal_policy(
sandbox: &maka_sandbox::Sandbox,
) -> Result<Option<maka_sandbox::filesystem::Compiled>, String> {
Expand All @@ -860,9 +889,9 @@ fn reveal_policy(
#[test]
fn reveal_requires_workspace_client_and_local_workspace() {
use maka_capabilities::call::Identity;
assert!(require_local_reveal(&Identity::Native).is_ok());
assert!(require_user_file_client(&Identity::Native).is_ok());
assert!(
require_local_reveal(&Identity::Remote {
require_user_file_client(&Identity::Remote {
request_id: uuid::Uuid::nil()
})
.is_ok()
Expand All @@ -884,12 +913,16 @@ fn reveal_requires_workspace_client_and_local_workspace() {
turn_id: "turn".into(),
},
] {
assert!(require_local_reveal(&identity).is_err());
assert!(require_user_file_client(&identity).is_err());
}
assert!(reveal_policy(&maka_sandbox::Sandbox::Disabled).is_ok());
assert!(
matches!(reveal_policy(&maka_sandbox::Sandbox::External { network: maka_sandbox::Network::Allowed }), Err(e) if e.contains("file manager"))
);
assert!(require_local_preview_workspace(&maka_sandbox::Sandbox::Disabled).is_ok());
assert!(
matches!(require_local_preview_workspace(&maka_sandbox::Sandbox::External { network: maka_sandbox::Network::Allowed }), Err(e) if e.contains("local workspace"))
);
}

fn read_policy(
Expand Down
Loading