mirror of
https://github.com/deepseek-ai/deepseek-harness.git
synced 2026-09-12 04:01:20 +00:00
Machine-produced by `pnpm run rescope-vendor --apply` plus the regeneration it prints: `pnpm install` for the lockfile, `pnpm run gen-third-party-notices`, `verify-translation-pairing --write` for the touched bilingual pairs, `gen-doc-graphs`, and one typert snapshot whose ids embed character offsets. `pnpm run rescope-vendor --check` verifies the result. Renames nine vendored packages (cordis, cosmokit, schemastery and the six @cordisjs plugins) and every reference that resolves them: manifest names and dependency keys, module specifiers including declare-module merges, cordis.yml plugin names, tsconfig paths, every Markdown fence, and `docs/` prose. Directory names, upstream versions, and dependency ranges are unchanged, so vendor/README.md still reads as an upstream snapshot; its manifest table gains an upstream-name column so THIRD_PARTY_NOTICES keeps MIT attribution pointed at each fork's origin. The tutorial tier follows the rename end to end: its yaml fences named plugins the Loader can no longer resolve, its `ts ignore-check` fences disagreed with the compiled fences beside them, and its prose quoted both. The contracts that told readers to keep upstream names — the root convention and the vendoring cookbook's tree comment and manifest invariant — now say to rescope instead. Two rules read `@deepseek-ai/` as "another workspace plugin": the client bundle purity gate now names the vendored libraries a browser bundle inlines, and the files where a bare `cordis` is an agent-preset id keep that product data.
174 lines
5.4 KiB
TypeScript
174 lines
5.4 KiB
TypeScript
import { Context } from '@deepseek-ai/cordis'
|
|
import type { Dict } from '@deepseek-ai/cosmokit'
|
|
import { Entry } from './entry.ts'
|
|
|
|
declare module './entry.ts' {
|
|
interface EntryOptions {
|
|
intercept?: Dict | null
|
|
isolate?: Dict<true | string> | null
|
|
}
|
|
|
|
interface Entry {
|
|
realm: LocalRealm
|
|
}
|
|
}
|
|
|
|
function swap<T extends {}>(target: T, source?: T | null) {
|
|
for (const key of Reflect.ownKeys(target)) {
|
|
Reflect.deleteProperty(target, key)
|
|
}
|
|
for (const key of Reflect.ownKeys(source || {})) {
|
|
Reflect.defineProperty(target, key, Reflect.getOwnPropertyDescriptor(source!, key)!)
|
|
}
|
|
}
|
|
|
|
/** Symbol realm used to isolate service implementations by entry or label. */
|
|
export abstract class Realm {
|
|
protected store: Dict<symbol> = Object.create(null)
|
|
|
|
abstract get suffix(): string
|
|
|
|
access(key: string, create = false) {
|
|
if (create) {
|
|
return this.store[key] ??= Symbol(`${key}${this.suffix}`)
|
|
} else {
|
|
return this.store[key] ?? Symbol(`${key}${this.suffix}`)
|
|
}
|
|
}
|
|
|
|
delete(key: string) {
|
|
delete this.store[key]
|
|
}
|
|
|
|
get size() {
|
|
return Object.keys(this.store).length
|
|
}
|
|
}
|
|
|
|
/** Entry-local isolation realm. */
|
|
export class LocalRealm extends Realm {
|
|
constructor(private entry: Entry) {
|
|
super()
|
|
}
|
|
|
|
get suffix() {
|
|
return '#' + this.entry.options.id
|
|
}
|
|
}
|
|
|
|
/** Named isolation realm shared by entries that use the same label. */
|
|
export class GlobalRealm extends Realm {
|
|
constructor(public label: string) {
|
|
super()
|
|
}
|
|
|
|
get suffix() {
|
|
return '@' + this.label
|
|
}
|
|
}
|
|
|
|
/** Install loader hooks that apply `intercept` and `isolate` entry options. */
|
|
export default function isolate(ctx: Context) {
|
|
const realms: Dict<GlobalRealm> = Object.create(null)
|
|
const delims: Dict<symbol> = Object.create(null)
|
|
|
|
function access(entry: Entry, name: string, create: true): symbol
|
|
function access(entry: Entry, name: string, create?: boolean): symbol | undefined
|
|
function access(entry: Entry, name: string, create = false) {
|
|
let realm: Realm | undefined
|
|
const label = entry.options.isolate?.[name]
|
|
if (!label) return
|
|
if (label === true) {
|
|
realm = entry.realm ??= new LocalRealm(entry)
|
|
} else if (create) {
|
|
realm = realms[label] ??= new GlobalRealm(label)
|
|
} else {
|
|
realm = realms[label]
|
|
}
|
|
return realm?.access(name, create)
|
|
}
|
|
|
|
ctx.on('loader/entry-init', (entry) => {
|
|
entry.ctx[Context.intercept] = Object.create(entry.ctx[Context.intercept])
|
|
entry.ctx[Context.isolate] = Object.create(entry.ctx[Context.isolate])
|
|
})
|
|
|
|
ctx.on('loader/patch-context', async (entry, next) => {
|
|
// step 1: generate new isolate map
|
|
const newMap: Dict<symbol> = Object.create(entry.parent.ctx[Context.isolate])
|
|
for (const name of Object.keys(entry.options.isolate ?? {})) {
|
|
newMap[name] = access(entry, name, true)
|
|
}
|
|
|
|
// step 2: generate service diff
|
|
const diff: Dict<[symbol, symbol, symbol, symbol]> = Object.create(null)
|
|
const oldMap = entry.ctx[Context.isolate]
|
|
for (const name in { ...newMap, ...delims }) {
|
|
if (newMap[name] === oldMap[name]) continue
|
|
const delim = delims[name] ??= Symbol(`delim:${name}`)
|
|
entry.ctx[delim] = Symbol(`${name}#${entry.id}`)
|
|
for (const symbol of [oldMap[name], newMap[name]]) {
|
|
const impl = symbol && entry.ctx.reflect.store[symbol]
|
|
if (!impl) continue
|
|
if (!impl.fiber) {
|
|
entry.ctx.logger.warn(new Error(`expected service ${name} to be implemented`))
|
|
continue
|
|
}
|
|
diff[name] = [oldMap[name], newMap[name], entry.ctx[delim], impl.fiber.ctx[delim]]
|
|
if (entry.ctx[delim] !== impl.fiber.ctx[delim]) break
|
|
}
|
|
}
|
|
|
|
// step 3: set prototype for transferred context
|
|
Object.setPrototypeOf(entry.ctx[Context.isolate], entry.parent.ctx[Context.isolate])
|
|
Object.setPrototypeOf(entry.ctx[Context.intercept], entry.parent.ctx[Context.intercept])
|
|
swap(entry.ctx[Context.isolate], newMap)
|
|
swap(entry.ctx[Context.intercept], entry.options.intercept)
|
|
|
|
// step 4: reload fiber
|
|
await next()
|
|
|
|
// step 5: replace service impl
|
|
for (const [symbol1, symbol2, flag1, flag2] of Object.values(diff)) {
|
|
if (flag1 === flag2 && entry.ctx.reflect.store[symbol1] && !entry.ctx.reflect.store[symbol2]) {
|
|
entry.ctx.reflect.store[symbol2] = entry.ctx.reflect.store[symbol1]
|
|
delete entry.ctx.reflect.store[symbol1]
|
|
}
|
|
}
|
|
|
|
// step 6: reflect notify
|
|
ctx.reflect.notify(Object.keys(diff), (ctx, name) => {
|
|
const [symbol1, symbol2, flag1, flag2] = diff[name]
|
|
const symbol3 = ctx[Context.isolate][name]
|
|
const flag3 = ctx[delims[name]]
|
|
return (symbol1 === symbol3 || symbol2 === symbol3) && (flag1 === flag3) !== (flag1 === flag2)
|
|
})
|
|
|
|
// step 7: clean up delimiters
|
|
for (const name in delims) {
|
|
if (!Reflect.ownKeys(newMap).includes(name)) {
|
|
delete entry.ctx[delims[name]]
|
|
}
|
|
}
|
|
})
|
|
|
|
ctx.on('loader/partial-dispose', (entry, legacy, active) => {
|
|
for (const [name, label] of Object.entries(legacy.isolate ?? {})) {
|
|
if (label === true) continue
|
|
if (active && entry.options.isolate?.[name] === label) continue
|
|
const realm = realms[label]
|
|
if (!realm) continue
|
|
|
|
// realm garbage collection
|
|
for (const entry of ctx.loader.entries()) {
|
|
// has reference to this realm
|
|
if (entry.options.isolate?.[name] === realm.label) return
|
|
}
|
|
realm.delete(name)
|
|
if (!realm.size) {
|
|
delete realms[realm.label]
|
|
}
|
|
}
|
|
})
|
|
}
|