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
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/host-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"test:integration": "bun test --pass-with-no-tests test/integration",
"test:integration:daemon": "node --experimental-strip-types --test src/terminal/DaemonClient/DaemonClient.node-test.ts src/daemon/DaemonSupervisor.node-test.ts",
"test:e2e": "bun run scripts/test-e2e.ts",
"bench": "bun test test/integration/pull-requests-scaling.bench.ts",
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
Expand Down
17 changes: 14 additions & 3 deletions packages/host-service/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Hono } from "hono";
import { cors } from "hono/cors";
import { createApiClient } from "./api";
import { createDb, type HostDb } from "./db";
import { EventBus, registerEventBusRoute } from "./events";
import { EventBus, GitWatcher, registerEventBusRoute } from "./events";
import type { ApiAuthProvider } from "./providers/auth";
import type { HostAuthProvider } from "./providers/host-auth";
import type { ModelProviderRuntimeResolver } from "./providers/model-providers";
Expand Down Expand Up @@ -76,13 +76,19 @@ export function createApp(options: CreateAppOptions): CreateAppResult {
return new Octokit({ auth: token });
});

const filesystem = new WorkspaceFilesystemManager({ db });
// GitWatcher is the single source of truth for `.git/` and worktree fs
// activity per workspace. Both EventBus (broadcasts to clients) and the
// pull-requests runtime (event-driven branch sync) subscribe to it.
const gitWatcher = new GitWatcher(db, filesystem);
gitWatcher.start();
const pullRequestRuntime = new PullRequestRuntimeManager({
db,
git,
github,
gitWatcher,
});
pullRequestRuntime.start();
const filesystem = new WorkspaceFilesystemManager({ db });
const chatRuntime =
options.chatRuntime ??
new ChatRuntimeManager({
Expand Down Expand Up @@ -111,7 +117,7 @@ export function createApp(options: CreateAppOptions): CreateAppResult {
}),
);

const eventBus = new EventBus({ db, filesystem });
const eventBus = new EventBus({ db, filesystem, gitWatcher });
eventBus.start();

// Backfill `kind='main'` v2 workspaces for projects already set up before
Expand Down Expand Up @@ -180,6 +186,11 @@ export function createApp(options: CreateAppOptions): CreateAppResult {
} catch (err) {
console.warn("[host-service] eventBus.close failed:", err);
}
try {
gitWatcher.close();
} catch (err) {
console.warn("[host-service] gitWatcher.close failed:", err);
}
if (ownsDb) {
try {
(db as unknown as { $client?: { close: () => void } }).$client?.close();
Expand Down
4 changes: 4 additions & 0 deletions packages/host-service/src/events/event-bus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import type { HostDb } from "../db";
import { portManager } from "../ports/port-manager";
import type { WorkspaceFilesystemManager } from "../runtime/filesystem";
import { EventBus } from "./event-bus";
import type { GitWatcher } from "./git-watcher";

function createEventBus(): EventBus {
return new EventBus({
db: {} as unknown as HostDb,
filesystem: {
resolveWorkspaceRoot: () => "/tmp/missing-workspace",
} as unknown as WorkspaceFilesystemManager,
gitWatcher: {
onChanged: () => () => {},
} as unknown as GitWatcher,
});
}

Expand Down
7 changes: 3 additions & 4 deletions packages/host-service/src/events/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { HostDb } from "../db/index.ts";
import { portManager } from "../ports/port-manager.ts";
import { getLabelsForWorkspace } from "../ports/static-ports.ts";
import type { WorkspaceFilesystemManager } from "../runtime/filesystem/index.ts";
import { GitWatcher } from "./git-watcher.ts";
import type { GitWatcher } from "./git-watcher.ts";
import type { ClientMessage, ServerMessage } from "./types.ts";

type WsSocket = {
Expand Down Expand Up @@ -52,6 +52,7 @@ function parseClientMessage(data: unknown): ClientMessage | null {
export interface EventBusOptions {
db: HostDb;
filesystem: WorkspaceFilesystemManager;
gitWatcher: GitWatcher;
}

/**
Expand All @@ -71,13 +72,12 @@ export class EventBus {

constructor(options: EventBusOptions) {
this.filesystem = options.filesystem;
this.gitWatcher = new GitWatcher(options.db, options.filesystem);
this.gitWatcher = options.gitWatcher;
}

start(): void {
if (this.removeGitListener || this.removePortListeners) return;

this.gitWatcher.start();
this.removeGitListener = this.gitWatcher.onChanged((event) => {
this.broadcast({
type: "git:changed",
Expand Down Expand Up @@ -105,7 +105,6 @@ export class EventBus {
this.removeGitListener = null;
this.removePortListeners?.();
this.removePortListeners = null;
this.gitWatcher.close();
for (const [socket, state] of this.clients) {
this.cleanupClient(socket, state);
}
Expand Down
1 change: 1 addition & 0 deletions packages/host-service/src/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { EventBus, registerEventBusRoute } from "./event-bus.ts";
export { type GitChangedEvent, GitWatcher } from "./git-watcher.ts";
export {
type AgentLifecycleEventType,
mapEventType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function createManager(state: FakeState) {
github: async () => {
throw new Error("github should not be used for direct PR linking");
},
gitWatcher: { onChanged: () => () => {} } as never,
});
}

Expand Down
Loading
Loading