| 1893 | }; |
| 1894 | |
| 1895 | export const createExecutor = <const TPlugins extends readonly AnyPlugin[] = readonly []>( |
| 1896 | config: ExecutorConfig<TPlugins>, |
| 1897 | ): Effect.Effect<Executor<TPlugins>, StorageFailure> => |
| 1898 | Effect.gen(function* () { |
| 1899 | const defaultPlugins = (): TPlugins => { |
| 1900 | const empty: readonly AnyPlugin[] = []; |
| 1901 | return empty as TPlugins; |
| 1902 | }; |
| 1903 | const { plugins: userPlugins = defaultPlugins() } = config; |
| 1904 | |
| 1905 | const tenant = String(config.tenant); |
| 1906 | const subject = config.subject != null ? String(config.subject) : null; |
| 1907 | |
| 1908 | const ownerBinding: OwnerBinding = { |
| 1909 | tenant: config.tenant, |
| 1910 | subject: config.subject ?? null, |
| 1911 | }; |
| 1912 | |
| 1913 | const ownedKeys = (owner: Owner): OwnedKeys => { |
| 1914 | if (owner === "org") return { tenant, owner, subject: ORG_SUBJECT }; |
| 1915 | if (subject == null) { |
| 1916 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: programmer error caught and surfaced as StorageError below by callers |
| 1917 | throw new StorageError({ |
| 1918 | message: `Cannot target owner "user": executor has no subject.`, |
| 1919 | cause: undefined, |
| 1920 | }); |
| 1921 | } |
| 1922 | return { tenant, owner, subject }; |
| 1923 | }; |
| 1924 | |
| 1925 | const requireUserSubject = (owner: Owner): Effect.Effect<void, StorageFailure> => |
| 1926 | owner === "user" && subject == null |
| 1927 | ? Effect.fail( |
| 1928 | new StorageError({ |
| 1929 | message: `Cannot target owner "user": executor has no subject.`, |
| 1930 | cause: undefined, |
| 1931 | }), |
| 1932 | ) |
| 1933 | : Effect.void; |
| 1934 | |
| 1935 | // Workspace-settings gate (`ExecutorConfig.orgWrites`). Called at the top |
| 1936 | // of every user-intent workspace-level mutation: with an explicit owner it |
| 1937 | // refuses only `"org"` targets; with no owner it guards a tenant-shared |
| 1938 | // surface outright. Deliberately NOT wired into the storage owner policy — |
| 1939 | // operational org-row writes (token refresh, tool-catalog re-sync) must |
| 1940 | // keep working for a denied member. |
| 1941 | const guardOrgWrite = (owner?: Owner): Effect.Effect<void, OrgWriteDeniedError> => |
| 1942 | Effect.gen(function* () { |
| 1943 | const access = |
| 1944 | config.orgWrites === "request" ? yield* currentOrgWriteAccess : config.orgWrites; |
| 1945 | if (access === "denied" && (owner === undefined || owner === "org")) { |
| 1946 | return yield* new OrgWriteDeniedError(); |
| 1947 | } |
| 1948 | }); |
| 1949 | |
| 1950 | // Built-in core-tools plugin: agent-facing static tools over the v2 surface. |
| 1951 | const plugins: readonly AnyPlugin[] = config.coreTools |
| 1952 | ? ([ |