Skip to content

feat(agent/agentcontainers): add file watcher and dirty status #17573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 29, 2025

Conversation

mafredri
Copy link
Member

server := &http.Server{
Handler: a.apiHandler(),
BaseContext: func(net.Listener) context.Context { return ctx },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: This change allows all r.Contexts to inherit the graceful context from the agent, ensuring cancellation on agent shutdown.

return r
return r, func() error {
return containerAPI.Close()
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: We don't want to just rely on agent context, we will also explicitly clean up running services in the containerAPI.

@mafredri mafredri requested a review from Copilot April 28, 2025 11:21
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces file watcher functionality and a new “dirty” flag to track modifications in devcontainer configuration files. Key changes include:

  • Adding a “dirty” field to both the TypeScript and Go devcontainer types.
  • Implementing file watcher functionality using fsnotify and integrating it into the API to mark devcontainers as dirty on configuration changes.
  • Updating tests and API logic to clear the dirty flag appropriately after container recreation.

Reviewed Changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
site/src/api/typesGenerated.ts Added a new readonly “dirty” field to the WorkspaceAgentDevcontainer interface.
codersdk/workspaceagents.go Added the Dirty boolean field for tracking runtime dirtiness.
agent/api.go Modified apiHandler return signature and integrated cleanup; updated API logic for file watching.
agent/agentcontainers/watcher/* Added implementations and tests for file watchers (FSNotify and Noop).
agent/agentcontainers/api_test.go & api_internal_test.go Updated tests to exercise file watcher behavior and dirty flag handling.
agent/agentcontainers/api.go Integrated watcher logic within API methods to mark and clear the dirty flag.
agent/agent.go Updated tailnet creation to use the new apiHandler cleanup functionality.
Files not reviewed (1)
  • go.mod: Language not supported
Comments suppressed due to low confidence (2)

agent/agentcontainers/api.go:476

  • [nitpick] Consider renaming 'configModifiedTimes' to 'configFileModifiedTimes' to improve clarity on its purpose.
api.configModifiedTimes[configPath] = modifiedAt

agent/agent.go:1484

  • Ensure that the cleanup function 'closeAPIHAndler' is invoked in all execution paths to avoid resource leaks, even if an error occurs later on.
apiHandler, closeAPIHAndler := a.apiHandler()

@mafredri mafredri marked this pull request as ready for review April 28, 2025 11:29
@mafredri mafredri force-pushed the mafredri/feat-agent-agentcontainers-watch branch from cfbd699 to 51fb44e Compare April 28, 2025 11:38
@mafredri mafredri force-pushed the mafredri/feat-agent-agentcontainers-watch branch from 51fb44e to bfeb1da Compare April 28, 2025 11:39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this package could be moved outside of agentcontainers for later re-use, but that doesn't have to be done in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a valid suggestion, but I'd like to defer until the need arises to keep devcontainer related stuff contained (heh).

@mafredri mafredri force-pushed the mafredri/feat-agent-agentcontainers-watch branch from 9f483ed to 9cf5415 Compare April 28, 2025 12:38
@mafredri mafredri force-pushed the mafredri/feat-agent-agentcontainers-watch branch from 0b16448 to eaf5922 Compare April 28, 2025 13:05
@mafredri mafredri requested a review from Copilot April 28, 2025 13:40
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a file watcher and introduces a dirty flag to track when devcontainer configuration files change. Key changes include adding a "dirty" property to devcontainer types, integrating a file system watcher into the API logic, and supplementing the feature with comprehensive tests.

Reviewed Changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
site/src/api/typesGenerated.ts Added a "dirty" flag to WorkspaceAgentDevcontainer interface.
codersdk/workspaceagents.go Added a "Dirty" field to the Go struct representing the devcontainer.
agent/api.go Modified apiHandler signature and integrated file watcher functionality.
agent/agentcontainers/watcher/* Added and tested file watcher implementations (fsnotify and noop).
agent/agentcontainers/api*.go Updated API logic to track dirty state and integrate file watcher events.
agent/agent.go Updated tailnet creation logic to obtain and close the API handler.
Files not reviewed (1)
  • go.mod: Language not supported

@mafredri mafredri requested a review from johnstcn April 28, 2025 13:54
lockCh: make(chan struct{}, 1),
devcontainerNames: make(map[string]struct{}),
knownDevcontainers: []codersdk.WorkspaceAgentDevcontainer{},
configFileModifiedTimes: make(map[string]time.Time),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now, but if we ever decide to expose this in the API we may want to refactor this to a map[string]codersdk.NullTime

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need to store a null time in the map, that can already be represented by missing map entry or zero time. But if we expose it in codersdk types then we can totally use that type there 👍🏻

@mafredri mafredri merged commit 268a50c into main Apr 29, 2025
35 of 37 checks passed
@mafredri mafredri deleted the mafredri/feat-agent-agentcontainers-watch branch April 29, 2025 08:54
@github-actions github-actions bot locked and limited conversation to collaborators Apr 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants