Skip to content

Commit 314365b

Browse files
Felicityseanpo
authored andcommitted
fix(project): preference-create 404 not 500; explicit node wiring
Two real bugs, both caught by the httpapi exerciser's automatic per-route bad-ID probe, not by typecheck: 1. ProjectPreference.patch's create path (no expectedRevision) relied on the project_preference table's FK constraint to reject a nonexistent project, wrapped in Effect.orDie -- so a bad project ID crashed with an unhandled SQLite FOREIGN KEY error (500) instead of a typed 404. Added an explicit existence check with a new ProjectPreference.ProjectNotFound error, mapped to the same 404 the other project routes already use. 2. Extending ProjectV2's interface (this ticket) and adding SessionGoal/SessionLedger (TKT-317, #7) together pushed past whatever threshold makes AppNodeBuilder's transitive auto-discovery of a new global .node's own dependencies unreliable -- confirmed via clean-dev isolation before attributing anything (dev alone: 227/0; my branch before this fix: crashes and "Service not found" errors in three separate full-app composition sites). Explicit node listing needed in: - packages/server/src/routes.ts (v2 API, used by cli/sdk-next) - packages/opencode/src/server/routes/instance/httpapi/server.ts (the V1 HttpApiApp -- what test:httpapi actually runs against; SessionGoal/SessionLedger gap here is pre-existing from #7, not from this PR, but blocks this PR's own gate, so fixed here per Ethan's ruling rather than serialized on its own ticket) - packages/opencode/src/effect/app-runtime.ts (the CLI/TUI runtime -- the same gap here is invisible to any HTTP-shaped test, since nothing exercises it that way; found by enumeration, not by a failing test) Per Ethan's follow-up ask: enumerated every AppNodeBuilder/LayerNode .group assembly site in the repo (grep in FORK.md's new section), distinguished the five full-app composition sites (need every global node) from location-services.ts (a different category -- per-location services, global nodes don't belong) and everything else (narrow, single-feature builds). Documented in FORK.md so the next new global .node has a checklist instead of a minefield. Fixing #2 unmasked a third, unrelated issue: v2.session.goal.get now reaches its assertion instead of crashing first, and fails it in test:httpapi's mode=effect only ("a session with no goal set should report no data") -- coverage and auth modes both pass. Not caused by this diff (unrelated code, TKT-317's), filed as feedback anomalyco#145 and recorded as a new named exception in FORK.md's known-red table, matching the TKT-305 convention exactly. Full validation after all of the above: bun typecheck 30/30. Core 1126/0 (new: a regression test asserting patch-on-nonexistent-project fails typed, not crashed). App 722+41/0, unchanged. Full opencode suite 3257/1/22/1, the 1 is the documented umask caveat, unchanged. test:httpapi 227/0 in mode=coverage and mode=auth, 226/1 in mode=effect (the newly-filed, newly-named exception above) -- was 221/6 (all crashes) before this commit. lint 4865/1 against a freshly-verified clean-dev-with-Gemma's-merge baseline of 4864/1 (not the stale 4859/4860 figures from before her PR landed) -- a 1-count difference already inside the noise band FORK.md documents as not meaningful, confirmed by checking each of the touched files' findings against clean dev individually: all pre-existing, none new.
1 parent 8613459 commit 314365b

16 files changed

Lines changed: 300 additions & 12 deletions

File tree

‎FORK.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,54 @@ been reconciled, not just what is currently outstanding.
9898
confirmed, with evidence, as of TKT-305). Say so explicitly in every PR and report — local
9999
per-package tests are the gate, and an absence of review comments is not evidence of a clean diff.
100100

101+
## Adding a new global `.node`? Check every assembly site, not just one.
102+
103+
A `makeGlobalNode`-tagged service (`ProjectV2.node`, `SessionGoal.node`, `EventV2.node`, …) is a
104+
process-wide singleton, and `AppNodeBuilder`/`AppNodeBuilderV1`'s transitive auto-discovery of a
105+
new node's *own* dependencies is **not reliable past a certain graph size** — TKT-315 and TKT-317
106+
independently hit this from both directions: adding `ProjectV2.node`'s own new dependencies broke
107+
type-level resolution in two unrelated packages, and *not* explicitly listing `SessionGoal.node`/
108+
`SessionLedger.node` in a full-app composition site left those routes returning `500: Service not
109+
found` at runtime, silently, because nothing forced the omission to surface until something else
110+
exercised that exact path. **Do not rely on auto-discovery for a new global node. List it
111+
explicitly at every full-app assembly site below**, the same way `ProjectV2.node` now appears in
112+
all five.
113+
114+
**The five full-app composition sites** (each lists most/all global nodes; found by `grep -rln
115+
"AppNodeBuilder\.build\|AppNodeBuilderV1\.build\|LayerNode\.group(" packages/ --include="*.ts"
116+
--include="*.tsx" | grep -v "/test/" | grep -v "\.test\.ts$"`, then narrowed to the entries whose
117+
own node count made clear they were "assemble everything" sites rather than one narrow feature):
118+
119+
1. `packages/opencode/src/server/routes/instance/httpapi/server.ts` — the `app` list (~63 nodes).
120+
The V1 `HttpApiApp`; this is what `test:httpapi`'s exerciser actually runs against.
121+
2. `packages/opencode/src/effect/app-runtime.ts` — `AppLayer` (~52 nodes). Powers the interactive
122+
CLI/TUI (`opencode` run directly, not through the HTTP server) — a gap here is invisible to any
123+
HTTP-shaped test.
124+
3. `packages/server/src/routes.ts` — `applicationServices` (~15 nodes). The current v2
125+
`createRoutes`/`createEmbeddedRoutes`, used by `packages/cli` and `packages/sdk-next`.
126+
4. `packages/cli/src/commands/handlers/serve.ts` — the CLI daemon's own explicit
127+
`AppNodeBuilder.build(LayerNode.group([...]))` in `bind()`.
128+
5. `packages/sdk-next/src/opencode.ts` — the embedded-server SDK's own explicit list. Also needs
129+
the type-level `HttpRouter.provideRequest(...)` treatment below, not just the node list.
130+
131+
**Not on this list, and shouldn't be added to it:** `packages/core/src/location-services.ts`
132+
(`locationServices`, ~36 nodes) looks similar by node count but is a **different category** —
133+
per-location/per-workspace scoped services (`LayerNode.unbound`/`LocationServiceMap`), rebuilt per
134+
directory, not process-wide singletons. A global `.node` does not belong there. Everything else
135+
`grep` finds under `packages/` outside `test/` is a narrow, single- or few-node build for one
136+
specific feature (e.g. `packages/opencode/src/control-plane/workspace.ts`'s two `InstanceStore.node`
137+
uses) — not a checklist target.
138+
139+
**If the new node is consumed through `HttpRouter.toWebHandler`** (as in `sdk-next/opencode.ts`),
140+
listing it in the node graph is necessary but not sufficient. `HttpRouter.toWebHandler`'s type
141+
distinguishes `"Requires"` from `"GlobalRequires"` request-kinds, and only
142+
`HttpRouter.provideRequest(layer)` discharges the latter — plain `Layer.provide(...)` silently
143+
leaves it unsatisfied and the handler's call signature shifts from one argument to two
144+
(`(request, context: Context<...>) => Promise<Response>`), a type error at the call site, not at
145+
the provide site. `HttpRouter.serve` (used by `packages/cli`'s daemon) does not draw this same
146+
distinction, which is why the equivalent fix there was just adding the node to the existing
147+
`Layer.provide(AppNodeBuilder.build(...))` list.
148+
101149
## Merge-blocking gates
102150

103151
These gates are **merge-blocking conditions**, not aspirations. A PR that trips one does not

‎packages/cli/src/commands/handlers/serve.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NodeHttpServer } from "@effect/platform-node"
22
import { Credential } from "@opencode-ai/core/credential"
33
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
4+
import { EventV2 } from "@opencode-ai/core/event"
45
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
56
import { PermissionSaved } from "@opencode-ai/core/permission/saved"
67
import { ProjectV2 } from "@opencode-ai/core/project"
@@ -47,6 +48,7 @@ function bind(hostname: string, port: number, password: string) {
4748
AppNodeBuilder.build(
4849
LayerNode.group([
4950
Credential.node,
51+
EventV2.node,
5052
PermissionSaved.node,
5153
ProjectV2.node,
5254
SessionGoal.node,

‎packages/client/src/generated-effect/client.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,76 @@ const adaptGroup19 = (raw: RawClient["server.projectCopy"]) => ({
870870
refresh: Endpoint19_2(raw),
871871
})
872872

873+
const Endpoint20_0 = (raw: RawClient["server.project"]) => () =>
874+
raw["project.list"]({}).pipe(
875+
Effect.mapError(mapClientError),
876+
Effect.map((value) => value.data),
877+
)
878+
879+
type Endpoint20_1Request = Parameters<RawClient["server.project"]["project.get"]>[0]
880+
type Endpoint20_1Input = { readonly projectID: Endpoint20_1Request["params"]["projectID"] }
881+
const Endpoint20_1 = (raw: RawClient["server.project"]) => (input: Endpoint20_1Input) =>
882+
raw["project.get"]({ params: { projectID: input["projectID"] } }).pipe(
883+
Effect.mapError(mapClientError),
884+
Effect.map((value) => value.data),
885+
)
886+
887+
type Endpoint20_2Request = Parameters<RawClient["server.project"]["project.updateMetadata"]>[0]
888+
type Endpoint20_2Input = {
889+
readonly projectID: Endpoint20_2Request["params"]["projectID"]
890+
readonly name?: Endpoint20_2Request["payload"]["name"]
891+
readonly icon?: Endpoint20_2Request["payload"]["icon"]
892+
readonly commands?: Endpoint20_2Request["payload"]["commands"]
893+
}
894+
const Endpoint20_2 = (raw: RawClient["server.project"]) => (input: Endpoint20_2Input) =>
895+
raw["project.updateMetadata"]({
896+
params: { projectID: input["projectID"] },
897+
payload: { name: input["name"], icon: input["icon"], commands: input["commands"] },
898+
}).pipe(
899+
Effect.mapError(mapClientError),
900+
Effect.map((value) => value.data),
901+
)
902+
903+
type Endpoint20_3Request = Parameters<RawClient["server.project"]["project.preference.read"]>[0]
904+
type Endpoint20_3Input = { readonly projectID: Endpoint20_3Request["params"]["projectID"] }
905+
const Endpoint20_3 = (raw: RawClient["server.project"]) => (input: Endpoint20_3Input) =>
906+
raw["project.preference.read"]({ params: { projectID: input["projectID"] } }).pipe(
907+
Effect.mapError(mapClientError),
908+
Effect.map((value) => value.data),
909+
)
910+
911+
type Endpoint20_4Request = Parameters<RawClient["server.project"]["project.preference.write"]>[0]
912+
type Endpoint20_4Input = {
913+
readonly projectID: Endpoint20_4Request["params"]["projectID"]
914+
readonly favorite?: Endpoint20_4Request["payload"]["favorite"]
915+
readonly rank?: Endpoint20_4Request["payload"]["rank"]
916+
readonly hidden?: Endpoint20_4Request["payload"]["hidden"]
917+
readonly lastOpenedAt?: Endpoint20_4Request["payload"]["lastOpenedAt"]
918+
readonly expectedRevision?: Endpoint20_4Request["payload"]["expectedRevision"]
919+
}
920+
const Endpoint20_4 = (raw: RawClient["server.project"]) => (input: Endpoint20_4Input) =>
921+
raw["project.preference.write"]({
922+
params: { projectID: input["projectID"] },
923+
payload: {
924+
favorite: input["favorite"],
925+
rank: input["rank"],
926+
hidden: input["hidden"],
927+
lastOpenedAt: input["lastOpenedAt"],
928+
expectedRevision: input["expectedRevision"],
929+
},
930+
}).pipe(
931+
Effect.mapError(mapClientError),
932+
Effect.map((value) => value.data),
933+
)
934+
935+
const adaptGroup20 = (raw: RawClient["server.project"]) => ({
936+
list: Endpoint20_0(raw),
937+
get: Endpoint20_1(raw),
938+
updateMetadata: Endpoint20_2(raw),
939+
read: Endpoint20_3(raw),
940+
write: Endpoint20_4(raw),
941+
})
942+
873943
const adaptClient = (raw: RawClient) => ({
874944
health: adaptGroup0(raw["server.health"]),
875945
location: adaptGroup1(raw["server.location"]),
@@ -891,6 +961,7 @@ const adaptClient = (raw: RawClient) => ({
891961
questions: adaptGroup17(raw["server.question"]),
892962
references: adaptGroup18(raw["server.reference"]),
893963
projectCopies: adaptGroup19(raw["server.projectCopy"]),
964+
"server.project": adaptGroup20(raw["server.project"]),
894965
})
895966

896967
export const make = (options?: { readonly baseUrl?: URL | string }) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ export function make(options: ClientOptions) {
12261226
expectedRevision: input["expectedRevision"],
12271227
},
12281228
successStatus: 200,
1229-
declaredStatuses: [409, 401, 400],
1229+
declaredStatuses: [409, 404, 401, 400],
12301230
empty: false,
12311231
},
12321232
requestOptions,

‎packages/core/schema.json‎

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"version": "7",
33
"dialect": "sqlite",
4-
"id": "5f8c8aca-5b2c-42b0-a16f-4c5090078c1f",
4+
"id": "bdc2daff-9ba4-4ecb-b1a3-0ad1b3c784f7",
55
"prevIds": [
6-
"7ab0b4c6-7f9e-41e5-ba37-c4ce0d9e1bb3"
6+
"5f8c8aca-5b2c-42b0-a16f-4c5090078c1f"
77
],
88
"ddl": [
99
{
@@ -46,6 +46,10 @@
4646
"name": "project_directory",
4747
"entityType": "tables"
4848
},
49+
{
50+
"name": "project_preference",
51+
"entityType": "tables"
52+
},
4953
{
5054
"name": "project",
5155
"entityType": "tables"
@@ -668,6 +672,86 @@
668672
"entityType": "columns",
669673
"table": "project_directory"
670674
},
675+
{
676+
"type": "text",
677+
"notNull": false,
678+
"autoincrement": false,
679+
"default": null,
680+
"generated": null,
681+
"name": "project_id",
682+
"entityType": "columns",
683+
"table": "project_preference"
684+
},
685+
{
686+
"type": "integer",
687+
"notNull": true,
688+
"autoincrement": false,
689+
"default": "false",
690+
"generated": null,
691+
"name": "favorite",
692+
"entityType": "columns",
693+
"table": "project_preference"
694+
},
695+
{
696+
"type": "text",
697+
"notNull": false,
698+
"autoincrement": false,
699+
"default": null,
700+
"generated": null,
701+
"name": "rank",
702+
"entityType": "columns",
703+
"table": "project_preference"
704+
},
705+
{
706+
"type": "integer",
707+
"notNull": true,
708+
"autoincrement": false,
709+
"default": "false",
710+
"generated": null,
711+
"name": "hidden",
712+
"entityType": "columns",
713+
"table": "project_preference"
714+
},
715+
{
716+
"type": "integer",
717+
"notNull": false,
718+
"autoincrement": false,
719+
"default": null,
720+
"generated": null,
721+
"name": "time_last_opened",
722+
"entityType": "columns",
723+
"table": "project_preference"
724+
},
725+
{
726+
"type": "integer",
727+
"notNull": true,
728+
"autoincrement": false,
729+
"default": "0",
730+
"generated": null,
731+
"name": "revision",
732+
"entityType": "columns",
733+
"table": "project_preference"
734+
},
735+
{
736+
"type": "integer",
737+
"notNull": true,
738+
"autoincrement": false,
739+
"default": null,
740+
"generated": null,
741+
"name": "time_created",
742+
"entityType": "columns",
743+
"table": "project_preference"
744+
},
745+
{
746+
"type": "integer",
747+
"notNull": true,
748+
"autoincrement": false,
749+
"default": null,
750+
"generated": null,
751+
"name": "time_updated",
752+
"entityType": "columns",
753+
"table": "project_preference"
754+
},
671755
{
672756
"type": "text",
673757
"notNull": false,
@@ -1883,6 +1967,21 @@
18831967
"entityType": "fks",
18841968
"table": "project_directory"
18851969
},
1970+
{
1971+
"columns": [
1972+
"project_id"
1973+
],
1974+
"tableTo": "project",
1975+
"columnsTo": [
1976+
"id"
1977+
],
1978+
"onUpdate": "NO ACTION",
1979+
"onDelete": "CASCADE",
1980+
"nameExplicit": false,
1981+
"name": "fk_project_preference_project_id_project_id_fk",
1982+
"entityType": "fks",
1983+
"table": "project_preference"
1984+
},
18861985
{
18871986
"columns": [
18881987
"session_id"
@@ -2175,6 +2274,15 @@
21752274
"table": "permission",
21762275
"entityType": "pks"
21772276
},
2277+
{
2278+
"columns": [
2279+
"project_id"
2280+
],
2281+
"nameExplicit": false,
2282+
"name": "project_preference_pk",
2283+
"table": "project_preference",
2284+
"entityType": "pks"
2285+
},
21782286
{
21792287
"columns": [
21802288
"id"

‎packages/core/src/database/migration.gen.ts‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/src/database/migration/20260810161802_project_preference.ts renamed to packages/core/src/database/migration/20260810165612_project_preference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Effect } from "effect"
22
import type { DatabaseMigration } from "../migration"
33

44
export default {
5-
id: "20260810161802_project_preference",
5+
id: "20260810165612_project_preference",
66
up(tx) {
77
return Effect.gen(function* () {
88
yield* tx.run(`

‎packages/core/src/project.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export interface Interface {
107107
readonly projectID: ID
108108
readonly patch: ProjectPreference.Patch
109109
readonly expectedRevision?: number
110-
}) => Effect.Effect<ProjectPreference.Value, ProjectPreference.Conflict>
110+
}) => Effect.Effect<ProjectPreference.Value, ProjectPreference.Conflict | ProjectPreference.ProjectNotFound>
111111
}
112112

113113
export class Service extends Context.Service<Service, Interface>()("@opencode/ProjectV2") {}

‎packages/core/src/project/preference.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ProjectPreference as PreferenceSchema } from "@opencode-ai/schema/proje
66
import { NonNegativeInt } from "@opencode-ai/schema/schema"
77
import type { Database } from "../database/database"
88
import { ProjectSchema } from "./schema"
9-
import { ProjectPreferenceTable } from "./sql"
9+
import { ProjectPreferenceTable, ProjectTable } from "./sql"
1010

1111
type DatabaseService = Database.Interface["db"]
1212

@@ -23,6 +23,14 @@ export class Conflict extends Schema.TaggedErrorClass<Conflict>()("ProjectPrefer
2323
revision: NonNegativeInt,
2424
}) {}
2525

26+
/** The referenced project does not exist. Checked explicitly, rather than left to the
27+
* project_preference FK constraint: an unhandled constraint violation on the create path (no
28+
* prior row, so nothing for the revision check to catch) surfaced as a 500 instead of a 404 --
29+
* caught by the httpapi exerciser's automatic per-route bad-ID probe. */
30+
export class ProjectNotFound extends Schema.TaggedErrorClass<ProjectNotFound>()("ProjectPreference.ProjectNotFound", {
31+
projectID: ProjectSchema.ID,
32+
}) {}
33+
2634
const defaults = (projectID: ProjectSchema.ID): Value => ({
2735
projectID,
2836
favorite: false,
@@ -67,6 +75,14 @@ export const patch = Effect.fn("ProjectPreference.patch")(function* (
6775
readonly now: number
6876
},
6977
) {
78+
const project = yield* db
79+
.select({ id: ProjectTable.id })
80+
.from(ProjectTable)
81+
.where(eq(ProjectTable.id, input.projectID))
82+
.get()
83+
.pipe(Effect.orDie)
84+
if (!project) return yield* new ProjectNotFound({ projectID: input.projectID })
85+
7086
const set = {
7187
favorite: input.patch.favorite,
7288
rank: input.patch.rank,

0 commit comments

Comments
 (0)