Skip to content

Commit fb884bb

Browse files
committed
feat: update session notices and skill reloads
1 parent 98c0988 commit fb884bb

29 files changed

Lines changed: 304 additions & 75 deletions

‎packages/client/src/generated/types.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ export type SessionContextOutput = {
664664
readonly time: { readonly created: number }
665665
readonly sessionID: string
666666
readonly text: string
667+
readonly description?: string
667668
readonly type: "synthetic"
668669
}
669670
| {
@@ -932,6 +933,7 @@ export type SessionHistoryOutput = {
932933
readonly sessionID: string
933934
readonly messageID: string
934935
readonly text: string
936+
readonly description?: string
935937
}
936938
}
937939
| {
@@ -1425,6 +1427,7 @@ export type SessionEventsOutput =
14251427
readonly sessionID: string
14261428
readonly messageID: string
14271429
readonly text: string
1430+
readonly description?: string
14281431
}
14291432
}
14301433
| {
@@ -1824,6 +1827,7 @@ export type SessionMessageOutput = {
18241827
readonly time: { readonly created: number }
18251828
readonly sessionID: string
18261829
readonly text: string
1830+
readonly description?: string
18271831
readonly type: "synthetic"
18281832
}
18291833
| {
@@ -2004,6 +2008,7 @@ export type MessageListOutput = {
20042008
readonly time: { readonly created: number }
20052009
readonly sessionID: string
20062010
readonly text: string
2011+
readonly description?: string
20072012
readonly type: "synthetic"
20082013
}
20092014
| {
@@ -3523,6 +3528,7 @@ export type EventSubscribeOutput =
35233528
readonly sessionID: string
35243529
readonly messageID: string
35253530
readonly text: string
3531+
readonly description?: string
35263532
}
35273533
}
35283534
| {
@@ -3983,6 +3989,14 @@ export type EventSubscribeOutput =
39833989
readonly location?: { readonly directory: string; readonly workspaceID?: string }
39843990
readonly data: { readonly projectID: string }
39853991
}
3992+
| {
3993+
readonly id: string
3994+
readonly metadata?: { readonly [x: string]: unknown }
3995+
readonly type: "skill.updated"
3996+
readonly durable?: { readonly aggregateID: string; readonly seq: number; readonly version: number }
3997+
readonly location?: { readonly directory: string; readonly workspaceID?: string }
3998+
readonly data: {}
3999+
}
39864000
| {
39874001
readonly id: string
39884002
readonly metadata?: { readonly [x: string]: unknown }

‎packages/core/src/filesystem/watcher.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type ParcelWatcher from "@parcel/watcher"
66
import { makeLocationNode } from "../effect/app-node"
77
import { Cause, Context, Effect, Layer } from "effect"
88
import { FileSystemWatcher } from "@opencode-ai/schema/filesystem-watcher"
9+
import os from "os"
910
import path from "path"
1011
import { Config } from "../config"
1112
import { EventV2 } from "../event"
@@ -57,10 +58,14 @@ export class Service extends Context.Service<Service, Interface>()("@opencode/v2
5758
export const layer = Layer.effect(
5859
Service,
5960
Effect.gen(function* () {
60-
if (yield* Flag.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER) return Service.of({})
61+
if (Flag.OPENCODE_DISABLE_FILEWATCHER) return Service.of({})
6162

6263
const backend = getBackend()
6364
const location = yield* Location.Service
65+
if (path.resolve(location.directory) === path.resolve(os.homedir())) {
66+
yield* Effect.logInfo("watcher skipped home directory", { directory: location.directory })
67+
return Service.of({})
68+
}
6469
if (!backend) {
6570
yield* Effect.logError("watcher backend not supported", {
6671
directory: location.directory,
@@ -84,6 +89,7 @@ export const layer = Layer.effect(
8489
)
8590

8691
const callback: ParcelWatcher.SubscribeCallback = (_error, updates) => {
92+
if (_error) runFork(Effect.logError("watcher callback failed", { error: _error }))
8793
for (const update of updates) {
8894
if (update.type === "create") runFork(events.publish(Event.Updated, { file: update.path, event: "add" }))
8995
if (update.type === "update") runFork(events.publish(Event.Updated, { file: update.path, event: "change" }))
@@ -94,7 +100,11 @@ export const layer = Layer.effect(
94100
const subscribe = (directory: string, ignore: string[]) => {
95101
const pending = w.subscribe(directory, callback, { ignore, backend })
96102
return Effect.promise(() => pending).pipe(
97-
Effect.tap((subscription) => Effect.sync(() => subscriptions.push(subscription))),
103+
Effect.tap((subscription) =>
104+
Effect.sync(() => subscriptions.push(subscription)).pipe(
105+
Effect.andThen(Effect.logInfo("watcher subscribed", { directory, backend, ignores: ignore.length })),
106+
),
107+
),
98108
Effect.timeout(SUBSCRIBE_TIMEOUT_MS),
99109
Effect.catchCause((cause) => {
100110
pending.then((subscription) => subscription.unsubscribe()).catch(() => {})
@@ -106,11 +116,9 @@ export const layer = Layer.effect(
106116
const config = (yield* (yield* Config.Service).entries())
107117
.filter((entry): entry is Config.Document => entry.type === "document")
108118
.flatMap((item) => item.info.watcher?.ignore ?? [])
109-
if (yield* Flag.OPENCODE_EXPERIMENTAL_FILEWATCHER) {
110-
yield* Effect.forkScoped(
111-
subscribe(location.directory, [...Ignore.PATTERNS, ...config, ...protecteds(location.directory)]),
112-
)
113-
}
119+
yield* Effect.forkScoped(
120+
subscribe(location.directory, [...Ignore.PATTERNS, ...config, ...protecteds(location.directory)]),
121+
)
114122

115123
if (location.vcs?.type === "git") {
116124
const resolved = (yield* git.repo.discover(location.directory))?.gitDirectory

‎packages/core/src/flag/flag.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ export const Flag = {
3232
OPENCODE_SERVER_PASSWORD: process.env["OPENCODE_SERVER_PASSWORD"],
3333
OPENCODE_SERVER_USERNAME: process.env["OPENCODE_SERVER_USERNAME"],
3434
OPENCODE_DISABLE_FFF: fff === undefined ? process.platform === "win32" : truthy("OPENCODE_DISABLE_FFF"),
35+
OPENCODE_DISABLE_FILEWATCHER: truthy("OPENCODE_DISABLE_FILEWATCHER"),
3536

3637
// Experimental
37-
OPENCODE_EXPERIMENTAL_FILEWATCHER: Config.boolean("OPENCODE_EXPERIMENTAL_FILEWATCHER").pipe(
38-
Config.withDefault(false),
39-
),
40-
OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: Config.boolean("OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER").pipe(
41-
Config.withDefault(false),
42-
),
4338
OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT:
4439
copy === undefined ? process.platform === "win32" : truthy("OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT"),
4540
OPENCODE_MODELS_URL: process.env["OPENCODE_MODELS_URL"],

‎packages/core/src/session.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { Revert } from "@opencode-ai/schema/revert"
3939
import { FSUtil } from "./fs-util"
4040
import { SessionDurable } from "@opencode-ai/schema/durable-event-manifest"
4141
import { SkillV2 } from "./skill"
42+
import { Job } from "./job"
4243

4344
export const RevertState = Revert.State
4445
export type RevertState = Revert.State
@@ -191,9 +192,14 @@ export interface Interface {
191192
) => Effect.Effect<void, NotFoundError | BusyError | MessageDecodeError | OperationUnavailableError>
192193
readonly wait: (id: SessionSchema.ID) => Effect.Effect<void, NotFoundError>
193194
readonly active: Effect.Effect<ReadonlySet<SessionSchema.ID>>
195+
readonly background: (sessionID: SessionSchema.ID) => Effect.Effect<void, NotFoundError>
194196
readonly resume: (sessionID: SessionSchema.ID) => Effect.Effect<void, NotFoundError | SessionRunner.RunError>
195197
readonly interrupt: (sessionID: SessionSchema.ID) => Effect.Effect<void>
196-
readonly synthetic: (input: { sessionID: SessionSchema.ID; text: string }) => Effect.Effect<void, NotFoundError>
198+
readonly synthetic: (input: {
199+
sessionID: SessionSchema.ID
200+
text: string
201+
description?: string
202+
}) => Effect.Effect<void, NotFoundError>
197203
readonly revert: {
198204
readonly stage: (input: {
199205
sessionID: SessionSchema.ID
@@ -217,6 +223,7 @@ export const layer = Layer.effect(
217223
const execution = yield* SessionExecution.Service
218224
const store = yield* SessionStore.Service
219225
const locations = yield* LocationServiceMap.Service
226+
const jobs = yield* Job.Service
220227
const scope = yield* Scope.Scope
221228
const decodeMessage = Schema.decodeUnknownEffect(SessionMessage.Message)
222229
const isDurableSessionEvent = Schema.is(SessionEvent.Durable)
@@ -512,6 +519,22 @@ export const layer = Layer.effect(
512519
yield* execution.awaitIdle(sessionID)
513520
}),
514521
active: execution.active,
522+
background: Effect.fn("V2Session.background")(function* (sessionID) {
523+
yield* result.get(sessionID)
524+
const backgrounded = yield* jobs.backgroundAll({ sessionID })
525+
if (backgrounded.length === 0) return
526+
yield* result.synthetic({
527+
sessionID,
528+
text: [
529+
"User requested that active blocking work be moved to the background.",
530+
"",
531+
"Backgrounded work:",
532+
...backgrounded.map((job) => `- ${job.type}: ${job.title && job.title.length > 0 ? job.title : job.id}`),
533+
"",
534+
"The backgrounded work is still unfinished. Move on to other work if you can. If there is nothing else useful to do, finish your response. Do not wait, sleep, poll, or report the backgrounded work as complete until a later completion notification is added to the conversation.",
535+
].join("\n"),
536+
})
537+
}),
515538
resume: Effect.fn("V2Session.resume")(function* (sessionID) {
516539
yield* result.get(sessionID)
517540
yield* execution.resume(sessionID)
@@ -523,6 +546,7 @@ export const layer = Layer.effect(
523546
messageID: SessionMessage.ID.create(),
524547
timestamp: yield* DateTime.now,
525548
text: input.text,
549+
description: input.description,
526550
})
527551
yield* execution.resume(input.sessionID).pipe(Effect.ignore, Effect.forkIn(scope, { startImmediately: true }), Effect.asVoid)
528552
}),
@@ -587,6 +611,7 @@ export const node = makeGlobalNode({
587611
service: Service,
588612
layer: layer.pipe(Layer.orDie),
589613
deps: [
614+
Job.node,
590615
Database.node,
591616
EventV2.node,
592617
ProjectV2.node,

‎packages/core/src/session/message-updater.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function update(adapter: Adapter, event: SessionEvent.Event) {
153153
SessionMessage.Synthetic.make({
154154
sessionID: event.data.sessionID,
155155
text: event.data.text,
156+
description: event.data.description,
156157
id: event.data.messageID,
157158
type: "synthetic",
158159
time: { created: event.data.timestamp },

‎packages/core/src/skill.ts‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ export * as SkillV2 from "./skill"
22

33
import { makeLocationNode } from "./effect/app-node"
44
import path from "path"
5-
import { Context, Effect, Layer, Schema, Types } from "effect"
5+
import { Context, Effect, Layer, Schema, Stream, Types } from "effect"
6+
import { FileSystemWatcher } from "@opencode-ai/schema/filesystem-watcher"
67
import { Skill } from "@opencode-ai/schema/skill"
78
import { AgentV2 } from "./agent"
89
import { ConfigMarkdown } from "./config/markdown"
10+
import { EventV2 } from "./event"
911
import { FSUtil } from "./fs-util"
1012
import { PermissionV2 } from "./permission"
1113
import { AbsolutePath } from "./schema"
@@ -27,6 +29,8 @@ export type Source = typeof Source.Type
2729
export const Info = Skill.Info
2830
export type Info = Skill.Info
2931

32+
export const Event = Skill.Event
33+
3034
export const available = (skills: ReadonlyArray<Info>, agent: AgentV2.Info) =>
3135
skills.filter((skill) => PermissionV2.evaluate("skill", skill.name, agent.permissions).effect !== "deny")
3236

@@ -58,6 +62,7 @@ export const layer = Layer.effect(
5862
Effect.gen(function* () {
5963
const discovery = yield* SkillDiscovery.Service
6064
const fs = yield* FSUtil.Service
65+
const events = yield* EventV2.Service
6166

6267
const state = State.create<Data, Draft>({
6368
initial: () => ({ sources: [] }),
@@ -72,7 +77,15 @@ export const layer = Layer.effect(
7277

7378
const load = Effect.fn("SkillV2.load")(function* (source: Source) {
7479
const skills: Info[] = []
75-
if (source.type === "embedded") return [source.skill]
80+
if (source.type === "embedded") {
81+
yield* Effect.logDebug("skill source loaded", {
82+
source: Source.key(source),
83+
type: source.type,
84+
directories: [],
85+
skills: [source.skill.name],
86+
})
87+
return { skills: [source.skill], directories: [] }
88+
}
7689
const directories = source.type === "directory" ? [source.path] : yield* discovery.pull(source.url)
7790
for (const directory of directories) {
7891
const files = yield* fs
@@ -101,19 +114,42 @@ export const layer = Layer.effect(
101114
})
102115
}
103116
}
104-
return skills
117+
yield* Effect.logDebug("skill source loaded", {
118+
source: Source.key(source),
119+
type: source.type,
120+
directories,
121+
skills: skills.map((skill) => skill.name),
122+
})
123+
return { skills, directories }
105124
})
106125

107-
// QUESTION(Dax): Should local skill sources invalidate on filesystem watch
108-
// events, following the reload policy chosen for other context sources?
109-
const cache = new Map<string, Info[]>()
126+
const cache = new Map<string, { skills: Info[]; directories: readonly string[] }>()
127+
const invalidate = Effect.fn("SkillV2.invalidateFromWatcher")(function* (file: string) {
128+
const invalidated = Array.from(cache.entries()).filter(([, loaded]) =>
129+
loaded.directories.some((directory) => FSUtil.contains(directory, file)),
130+
)
131+
if (invalidated.length === 0) return
132+
for (const [key] of invalidated) cache.delete(key)
133+
yield* Effect.logInfo("skill cache invalidated", {
134+
file,
135+
sources: invalidated.map(([key]) => key),
136+
skills: invalidated.flatMap(([, loaded]) => loaded.skills.map((skill) => skill.name)),
137+
})
138+
yield* events.publish(Event.Updated, {}).pipe(Effect.asVoid)
139+
})
140+
141+
yield* events.subscribe(FileSystemWatcher.Event.Updated).pipe(
142+
Stream.runForEach((event) => invalidate(event.data.file)),
143+
Effect.forkScoped({ startImmediately: true }),
144+
)
145+
110146
const list = Effect.fn("SkillV2.list")(function* () {
111147
const skills = new Map<string, Info>()
112148
for (const source of state.get().sources) {
113149
const key = Source.key(source)
114150
const loaded = cache.get(key) ?? (yield* load(source))
115151
cache.set(key, loaded)
116-
for (const skill of loaded) skills.set(skill.name, skill)
152+
for (const skill of loaded.skills) skills.set(skill.name, skill)
117153
}
118154
return Array.from(skills.values())
119155
})
@@ -131,4 +167,4 @@ export const layer = Layer.effect(
131167

132168
export const locationLayer = layer.pipe(Layer.provide(SkillDiscovery.defaultLayer))
133169

134-
export const node = makeLocationNode({ service: Service, layer, deps: [SkillDiscovery.node, FSUtil.node] })
170+
export const node = makeLocationNode({ service: Service, layer, deps: [SkillDiscovery.node, FSUtil.node, EventV2.node] })

‎packages/core/test/filesystem/watcher.test.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { $ } from "bun"
22
import { describe, expect } from "bun:test"
33
import fs from "fs/promises"
44
import path from "path"
5-
import { ConfigProvider, Deferred, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
5+
import { Deferred, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
66
import { Config } from "@opencode-ai/core/config"
77
import { EventV2 } from "@opencode-ai/core/event"
88
import { FSUtil } from "@opencode-ai/core/fs-util"
@@ -27,13 +27,6 @@ const configLayer = Layer.succeed(
2727
}),
2828
)
2929

30-
const flagsLayer = ConfigProvider.layer(
31-
ConfigProvider.fromUnknown({
32-
OPENCODE_EXPERIMENTAL_FILEWATCHER: "true",
33-
OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: "false",
34-
}),
35-
)
36-
3730
function provide(directory: string, vcs?: Location.Interface["vcs"]) {
3831
const locationLayer = Layer.succeed(
3932
Location.Service,
@@ -44,7 +37,6 @@ function provide(directory: string, vcs?: Location.Interface["vcs"]) {
4437
Layer.provide(configLayer),
4538
Layer.provide(Git.defaultLayer),
4639
Layer.provide(locationLayer),
47-
Layer.provide(flagsLayer),
4840
),
4941
)
5042
}

‎packages/core/test/move-session.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Database } from "@opencode-ai/core/database/database"
99
import { FSUtil } from "@opencode-ai/core/fs-util"
1010
import { Git } from "@opencode-ai/core/git"
1111
import { EventV2 } from "@opencode-ai/core/event"
12+
import { Job } from "@opencode-ai/core/job"
1213
import { Project } from "@opencode-ai/core/project"
1314
import { ProjectTable } from "@opencode-ai/core/project/sql"
1415
import { ProjectDirectories } from "@opencode-ai/core/project/directories"
@@ -29,6 +30,7 @@ const project = Project.layer.pipe(
2930
Layer.provide(ProjectDirectories.defaultLayer),
3031
)
3132
const sessions = SessionV2.layer.pipe(
33+
Layer.provide(Job.layer),
3234
Layer.provide(locationServiceMapLayer),
3335
Layer.provide(Database.defaultLayer),
3436
Layer.provide(EventV2.defaultLayer),

‎packages/core/test/permission.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Deferred, Effect, Fiber, Layer } from "effect"
33
import { AgentV2 } from "@opencode-ai/core/agent"
44
import { Database } from "@opencode-ai/core/database/database"
55
import { EventV2 } from "@opencode-ai/core/event"
6+
import { Job } from "@opencode-ai/core/job"
67
import { Location } from "@opencode-ai/core/location"
78
import { PermissionV2 } from "@opencode-ai/core/permission"
89
import { PermissionTable } from "@opencode-ai/core/permission/sql"
@@ -24,6 +25,7 @@ const current = Layer.succeed(
2425
Location.Service.of(location({ directory: AbsolutePath.make("/project") })),
2526
)
2627
const sessions = SessionV2.layer.pipe(
28+
Layer.provide(Job.layer),
2729
Layer.provide(locationServiceMapLayer),
2830
Layer.provide(EventV2.defaultLayer),
2931
Layer.provide(Database.defaultLayer),

‎packages/core/test/session-compact.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { OpenAIChat } from "@opencode-ai/llm/protocols"
44
import { Config } from "@opencode-ai/core/config"
55
import { Database } from "@opencode-ai/core/database/database"
66
import { EventV2 } from "@opencode-ai/core/event"
7+
import { Job } from "@opencode-ai/core/job"
78
import { Location } from "@opencode-ai/core/location"
89
import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
910
import type { LocationServices } from "@opencode-ai/core/location-services"
@@ -60,6 +61,7 @@ const locations = Layer.effect(
6061
),
6162
)
6263
const sessions = SessionV2.layer.pipe(
64+
Layer.provide(Job.layer),
6365
Layer.provide(locations),
6466
Layer.provide(EventV2.defaultLayer),
6567
Layer.provide(Database.defaultLayer),

0 commit comments

Comments
 (0)