Skip to content

writeFileContent tool fails with "Cannot read properties of undefined (reading 'id')" #16899

@sgraband

Description

@sgraband

Bug Description:

The writeFileContent tool fails with the error Cannot read properties of undefined (reading 'id') when invoked by AI agents. This issue affects all writeFileContent calls across all tested agents.

Root Cause:

The issue is in packages/ai-ide/src/browser/file-changeset-functions.ts in the WriteFileContent class handler (around line 167-180):

handler: async (args: string, ctx: MutableChatRequestModel): Promise<string> => {
    // ...
    const fileElement = this.fileChangeFactory({
        uri: uri,
        type: type as 'modify' | 'add' | 'delete',
        state: 'pending',
        targetState: content,
        requestId: ctx.id,  // <-- ERROR: ctx is undefined
        chatSessionId: ctx.session.id  // <-- Also fails
    });
    // ...
}

The handler expects ctx to be a MutableChatRequestModel with valid id and session.id properties. However:

  1. The ToolRequest.handler signature in packages/ai-core/src/common/language-model.ts defines ctx as optional: handler: (arg_string: string, ctx?: unknown)
  2. When tools are invoked by language models directly (not through the chat service), the ctx parameter may be undefined, a ToolInvocationContext object (which only has toolCallId), or some other context type
  3. The code does not validate that ctx is actually a MutableChatRequestModel before accessing its properties

Affected Tools:

All file changeset tools in packages/ai-ide/src/browser/file-changeset-functions.ts are affected:

  • WriteFileContent
  • SuggestFileContent
  • SimpleSuggestFileReplacements
  • SimpleWriteFileReplacements
  • SuggestFileReplacements_Simple
  • WriteFileReplacements_Simple
  • ClearFileChanges
  • GetProposedFileState
  • SuggestFileReplacements
  • WriteFileReplacements

Steps to Reproduce:

  1. Start a Theia IDE instance with AI features enabled
  2. Interact with any AI agent that uses the writeFileContent tool (e.g., ask the agent to create a new file)
  3. Observe the error: Error executing tool 'writeFileContent': Cannot read properties of undefined (reading 'id')

Additional Information

  • Operating System: All (tested on multiple platforms)
  • Theia Version: Current master

Error Message:

Error executing tool 'writeFileContent': Cannot read properties of undefined (reading 'id')

Suggested Fix:
Add proper validation for the ctx parameter before accessing its properties:

handler: async (args: string, ctx: MutableChatRequestModel): Promise<string> => {
    if (ctx?.response?.cancellationToken?.isCancellationRequested) {
        return JSON.stringify({ error: 'Operation cancelled by user' });
    }
    
    // Add validation for ctx
    if (!ctx || !ctx.id || !ctx.session?.id) {
        return JSON.stringify({ 
            error: 'writeFileContent requires a valid chat context. This tool can only be used within a chat session.' 
        });
    }
    
    const { path, content } = JSON.parse(args);
    // ... rest of the handler
}

Related Files:

  • packages/ai-ide/src/browser/file-changeset-functions.ts - Tool implementations
  • packages/ai-core/src/common/language-model.ts - ToolRequest interface definition
  • packages/ai-chat/src/browser/change-set-file-element.ts - ChangeSetFileElement factory
  • packages/ai-chat/src/common/chat-model.ts - MutableChatRequestModel class

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions