refactor(core): replace legacy logger with Effect logging (#31310)
This commit is contained in:
@@ -8,7 +8,6 @@ import { FSUtil } from "@opencode-ai/core/fs-util"
|
||||
import { Hash } from "@opencode-ai/core/util/hash"
|
||||
import { Config } from "@/config/config"
|
||||
import { Global } from "@opencode-ai/core/global"
|
||||
import * as Log from "@opencode-ai/core/util/log"
|
||||
|
||||
export const Patch = Schema.Struct({
|
||||
hash: Schema.String,
|
||||
@@ -28,7 +27,6 @@ export const FileDiff = Schema.Struct({
|
||||
}).annotate({ identifier: "SnapshotFileDiff" })
|
||||
export type FileDiff = typeof FileDiff.Type
|
||||
|
||||
const log = Log.create({ service: "snapshot" })
|
||||
const prune = "7.days"
|
||||
const limit = 2 * 1024 * 1024
|
||||
const core = ["-c", "core.longpaths=true", "-c", "core.symlinks=true"]
|
||||
@@ -153,7 +151,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
},
|
||||
)
|
||||
if (result.code === 0) return
|
||||
log.warn("failed to add snapshot files", {
|
||||
yield* Effect.logWarning("failed to add snapshot files", {
|
||||
exitCode: result.code,
|
||||
stderr: result.stderr,
|
||||
})
|
||||
@@ -206,7 +204,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
{ concurrency: 2 },
|
||||
)
|
||||
if (diff.code !== 0 || other.code !== 0) {
|
||||
log.warn("failed to list snapshot files", {
|
||||
yield* Effect.logWarning("failed to list snapshot files", {
|
||||
diffCode: diff.code,
|
||||
diffStderr: diff.stderr,
|
||||
otherCode: other.code,
|
||||
@@ -227,7 +225,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
// Remove newly-ignored files from snapshot index to prevent re-adding
|
||||
if (ignored.size > 0) {
|
||||
const ignoredFiles = Array.from(ignored)
|
||||
log.info("removing gitignored files from snapshot", { count: ignoredFiles.length })
|
||||
yield* Effect.logInfo("removing gitignored files from snapshot", { count: ignoredFiles.length })
|
||||
yield* drop(ignoredFiles)
|
||||
}
|
||||
|
||||
@@ -264,13 +262,13 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
if (!(yield* exists(state.gitdir))) return
|
||||
const result = yield* git(args(["gc", `--prune=${prune}`]), { cwd: state.directory })
|
||||
if (result.code !== 0) {
|
||||
log.warn("cleanup failed", {
|
||||
yield* Effect.logWarning("cleanup failed", {
|
||||
exitCode: result.code,
|
||||
stderr: result.stderr,
|
||||
})
|
||||
return
|
||||
}
|
||||
log.info("cleanup", { prune })
|
||||
yield* Effect.logInfo("cleanup", { prune })
|
||||
}),
|
||||
)
|
||||
})
|
||||
@@ -289,12 +287,12 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
yield* git(["--git-dir", state.gitdir, "config", "core.longpaths", "true"])
|
||||
yield* git(["--git-dir", state.gitdir, "config", "core.symlinks", "true"])
|
||||
yield* git(["--git-dir", state.gitdir, "config", "core.fsmonitor", "false"])
|
||||
log.info("initialized")
|
||||
yield* Effect.logInfo("initialized")
|
||||
}
|
||||
yield* add()
|
||||
const result = yield* git(args(["write-tree"]), { cwd: state.directory })
|
||||
const hash = result.text.trim()
|
||||
log.info("tracking", { hash, cwd: state.directory, git: state.gitdir })
|
||||
yield* Effect.logInfo("tracking", { hash, cwd: state.directory, git: state.gitdir })
|
||||
return hash
|
||||
}),
|
||||
)
|
||||
@@ -311,7 +309,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
},
|
||||
)
|
||||
if (result.code !== 0) {
|
||||
log.warn("failed to get diff", { hash, exitCode: result.code })
|
||||
yield* Effect.logWarning("failed to get diff", { hash, exitCode: result.code })
|
||||
return { hash, files: [] }
|
||||
}
|
||||
const files = result.text
|
||||
@@ -336,21 +334,21 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
const restore = Effect.fnUntraced(function* (snapshot: string) {
|
||||
return yield* locked(
|
||||
Effect.gen(function* () {
|
||||
log.info("restore", { commit: snapshot })
|
||||
yield* Effect.logInfo("restore", { commit: snapshot })
|
||||
const result = yield* git([...core, ...args(["read-tree", snapshot])], { cwd: state.worktree })
|
||||
if (result.code === 0) {
|
||||
const checkout = yield* git([...core, ...args(["checkout-index", "-a", "-f"])], {
|
||||
cwd: state.worktree,
|
||||
})
|
||||
if (checkout.code === 0) return
|
||||
log.error("failed to restore snapshot", {
|
||||
yield* Effect.logError("failed to restore snapshot", {
|
||||
snapshot,
|
||||
exitCode: checkout.code,
|
||||
stderr: checkout.stderr,
|
||||
})
|
||||
return
|
||||
}
|
||||
log.error("failed to restore snapshot", {
|
||||
yield* Effect.logError("failed to restore snapshot", {
|
||||
snapshot,
|
||||
exitCode: result.code,
|
||||
stderr: result.stderr,
|
||||
@@ -377,7 +375,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
}
|
||||
|
||||
const single = Effect.fnUntraced(function* (op: (typeof ops)[number]) {
|
||||
log.info("reverting", { file: op.file, hash: op.hash })
|
||||
yield* Effect.logInfo("reverting", { file: op.file, hash: op.hash })
|
||||
const result = yield* git([...core, ...args(["checkout", op.hash, "--", op.file])], {
|
||||
cwd: state.worktree,
|
||||
})
|
||||
@@ -386,10 +384,13 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
cwd: state.worktree,
|
||||
})
|
||||
if (tree.code === 0 && tree.text.trim()) {
|
||||
log.info("file existed in snapshot but checkout failed, keeping", { file: op.file, hash: op.hash })
|
||||
yield* Effect.logInfo("file existed in snapshot but checkout failed, keeping", {
|
||||
file: op.file,
|
||||
hash: op.hash,
|
||||
})
|
||||
return
|
||||
}
|
||||
log.info("file did not exist in snapshot, deleting", { file: op.file, hash: op.hash })
|
||||
yield* Effect.logInfo("file did not exist in snapshot, deleting", { file: op.file, hash: op.hash })
|
||||
yield* remove(op.file)
|
||||
})
|
||||
|
||||
@@ -422,7 +423,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
)
|
||||
|
||||
if (tree.code !== 0) {
|
||||
log.info("batched ls-tree failed, falling back to single-file revert", {
|
||||
yield* Effect.logInfo("batched ls-tree failed, falling back to single-file revert", {
|
||||
hash: first.hash,
|
||||
files: run.length,
|
||||
})
|
||||
@@ -442,7 +443,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
)
|
||||
const list = run.filter((item) => have.has(item.rel))
|
||||
if (list.length) {
|
||||
log.info("reverting", { hash: first.hash, files: list.length })
|
||||
yield* Effect.logInfo("reverting", { hash: first.hash, files: list.length })
|
||||
const result = yield* git(
|
||||
[...core, ...args(["checkout", first.hash, "--", ...list.map((item) => item.file)])],
|
||||
{
|
||||
@@ -450,7 +451,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
},
|
||||
)
|
||||
if (result.code !== 0) {
|
||||
log.info("batched checkout failed, falling back to single-file revert", {
|
||||
yield* Effect.logInfo("batched checkout failed, falling back to single-file revert", {
|
||||
hash: first.hash,
|
||||
files: list.length,
|
||||
})
|
||||
@@ -464,7 +465,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
|
||||
for (const op of run) {
|
||||
if (have.has(op.rel)) continue
|
||||
log.info("file did not exist in snapshot, deleting", { file: op.file, hash: op.hash })
|
||||
yield* Effect.logInfo("file did not exist in snapshot, deleting", { file: op.file, hash: op.hash })
|
||||
yield* remove(op.file)
|
||||
}
|
||||
|
||||
@@ -482,7 +483,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
cwd: state.worktree,
|
||||
})
|
||||
if (result.code !== 0) {
|
||||
log.warn("failed to get diff", {
|
||||
yield* Effect.logWarning("failed to get diff", {
|
||||
hash,
|
||||
exitCode: result.code,
|
||||
stderr: result.stderr,
|
||||
@@ -560,16 +561,18 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
{ stdin: refs.map((item) => item.ref).join("\n") + "\n" },
|
||||
)
|
||||
if (batch.exitCode !== 0) {
|
||||
log.info("git cat-file --batch failed during snapshot diff, falling back to per-file git show", {
|
||||
yield* Effect.logInfo(
|
||||
"git cat-file --batch failed during snapshot diff, falling back to per-file git show",
|
||||
{
|
||||
stderr: batch.stderr.toString("utf8"),
|
||||
refs: refs.length,
|
||||
})
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
const out = batch.stdout
|
||||
|
||||
const fail = (msg: string, extra?: Record<string, string>) => {
|
||||
log.info(msg, { ...extra, refs: refs.length })
|
||||
return undefined
|
||||
}
|
||||
|
||||
@@ -708,10 +711,7 @@ export const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Serv
|
||||
})
|
||||
|
||||
yield* cleanup().pipe(
|
||||
Effect.catchCause((cause) => {
|
||||
log.error("cleanup loop failed", { cause: Cause.pretty(cause) })
|
||||
return Effect.void
|
||||
}),
|
||||
Effect.catchCause((cause) => Effect.logError("cleanup loop failed", { cause: Cause.pretty(cause) })),
|
||||
Effect.repeat(Schedule.spaced(Duration.hours(1))),
|
||||
Effect.delay(Duration.minutes(1)),
|
||||
Effect.forkScoped,
|
||||
|
||||
Reference in New Issue
Block a user