@xiangyixie/dsh-superagent 0.11.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +97 -10
- package/README.md +322 -28
- package/bin/archive.js +185 -185
- package/bin/doctor.js +84 -84
- package/bin/sync.js +187 -0
- package/bin/workbench.js +98 -0
- package/docs/policy.example.json +38 -0
- package/lib/audit.js +555 -0
- package/lib/cost.js +267 -267
- package/lib/doctor.js +878 -876
- package/lib/guard.js +841 -0
- package/lib/index.js +2031 -1547
- package/lib/inject.js +138 -0
- package/lib/ledger.js +270 -270
- package/lib/memory.js +736 -736
- package/lib/orchestrator.js +3851 -3312
- package/lib/policy.js +739 -0
- package/lib/report.js +1068 -1068
- package/lib/store.js +873 -873
- package/lib/sync.js +456 -0
- package/lib/templates.js +690 -690
- package/lib/watch.js +810 -810
- package/lib/workbench.js +1119 -0
- package/lib/workspace.js +433 -433
- package/package.json +92 -80
- package/public/workbench.html +836 -0
package/bin/sync.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `dsh-superagent-sync` — carry one state directory between machines through a
|
|
4
|
+
* git remote, with no server and no dependency.
|
|
5
|
+
*
|
|
6
|
+
* This is an approximate sync, not a cluster: see the LIMITS block below and
|
|
7
|
+
* the "Syncing one state directory across machines" section of the README.
|
|
8
|
+
*/
|
|
9
|
+
import { buildIgnoreBlock, createSync } from '../lib/sync.js'
|
|
10
|
+
import { resolveStateRoot } from '../lib/store.js'
|
|
11
|
+
|
|
12
|
+
const USAGE = `Usage: dsh-superagent-sync <command> [options]
|
|
13
|
+
|
|
14
|
+
Carry the run state directory between machines through a git remote.
|
|
15
|
+
|
|
16
|
+
Commands:
|
|
17
|
+
init <git-remote> make the state directory a repository, write .gitignore,
|
|
18
|
+
and commit; <git-remote> becomes "origin"
|
|
19
|
+
pull fetch and merge the remote; uncommitted local work is
|
|
20
|
+
stashed first and reapplied after the merge
|
|
21
|
+
push commit local state, then push it to origin
|
|
22
|
+
status ahead/behind counts, uncommitted files, pending conflicts
|
|
23
|
+
|
|
24
|
+
Options:
|
|
25
|
+
--state-dir <path> run state directory (default: $DSH_HOME/state/superagent)
|
|
26
|
+
--json print the raw result as JSON
|
|
27
|
+
-h, --help show this message
|
|
28
|
+
|
|
29
|
+
Limits (approximate by design):
|
|
30
|
+
- two machines writing at the same moment can still conflict; the merge stops
|
|
31
|
+
with both sides kept and the paths listed, nothing is chosen for you
|
|
32
|
+
- there is no live sync: another machine sees this state only after push/pull
|
|
33
|
+
- keys are not distributed: each machine still needs its own model credential
|
|
34
|
+
- a conflicted run file must be resolved before the plugin mounts, otherwise
|
|
35
|
+
the store quarantines the unparsable bytes as <runId>.json.corrupt-<ts>
|
|
36
|
+
|
|
37
|
+
Ignored machine-local files (never pushed):
|
|
38
|
+
${IGNORE_PATTERNS_TEXT()}`
|
|
39
|
+
|
|
40
|
+
function IGNORE_PATTERNS_TEXT() {
|
|
41
|
+
return buildIgnoreBlock()
|
|
42
|
+
.split('\n')
|
|
43
|
+
.filter(line => line && !line.startsWith('#'))
|
|
44
|
+
.map(line => ` ${line}`)
|
|
45
|
+
.join('\n')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const COMMANDS = new Set(['init', 'pull', 'push', 'status'])
|
|
49
|
+
|
|
50
|
+
function parse(argv) {
|
|
51
|
+
const options = { json: false }
|
|
52
|
+
const positional = []
|
|
53
|
+
for (let index = 0; index < argv.length; index++) {
|
|
54
|
+
const arg = argv[index]
|
|
55
|
+
if (arg === '--state-dir') {
|
|
56
|
+
const value = argv[++index]
|
|
57
|
+
if (value === undefined || value.startsWith('--')) fail(2, '--state-dir needs a path')
|
|
58
|
+
options.stateDir = value
|
|
59
|
+
} else if (arg.startsWith('--state-dir=')) options.stateDir = arg.slice('--state-dir='.length)
|
|
60
|
+
else if (arg === '--json') options.json = true
|
|
61
|
+
else if (arg === '-h' || arg === '--help' || arg === 'help') {
|
|
62
|
+
console.log(USAGE)
|
|
63
|
+
process.exit(0)
|
|
64
|
+
} else if (arg.startsWith('-')) fail(2, `unknown option: ${arg}`)
|
|
65
|
+
else positional.push(arg)
|
|
66
|
+
}
|
|
67
|
+
const [command, ...rest] = positional
|
|
68
|
+
if (!command) fail(2, 'a command is required')
|
|
69
|
+
if (!COMMANDS.has(command)) fail(2, `unknown command: ${command}`)
|
|
70
|
+
options.command = command
|
|
71
|
+
options.args = rest
|
|
72
|
+
if (command === 'init' && rest.length === 0) fail(2, 'init needs a git remote')
|
|
73
|
+
if (command !== 'init' && rest.length > 0) fail(2, `${command} takes no positional argument (got ${rest[0]})`)
|
|
74
|
+
if (command === 'init' && rest.length > 1) fail(2, `init takes one remote, got ${rest.length}`)
|
|
75
|
+
return options
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function fail(code, message, hint) {
|
|
79
|
+
console.error(`[error] ${message}`)
|
|
80
|
+
if (hint) console.error(`hint: ${hint}`)
|
|
81
|
+
else console.error(`\n${USAGE}`)
|
|
82
|
+
process.exit(code)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function plural(count, word) {
|
|
86
|
+
return `${count} ${word}${count === 1 ? '' : 's'}`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function printInit(result) {
|
|
90
|
+
console.log(`initialized repository at ${result.root}`)
|
|
91
|
+
console.log(` remote origin → ${result.remote} (${result.remoteAction})`)
|
|
92
|
+
console.log(` branch ${result.branch ?? '(detached)'}`)
|
|
93
|
+
console.log(` commit ${result.commit ?? '(nothing to commit)'}`)
|
|
94
|
+
console.log(` ignore .gitignore ${result.ignoreChanged ? 'written' : 'already current'}`)
|
|
95
|
+
console.log(` kept out: ${result.ignoredPatterns.join(', ')}`)
|
|
96
|
+
console.log(
|
|
97
|
+
result.memorySynced
|
|
98
|
+
? ' memory memory/.gitignore rewritten; <scope>.jsonl notes are synced'
|
|
99
|
+
: ' memory memory/.gitignore was customized, so notes stay local (a "*" there keeps them out)',
|
|
100
|
+
)
|
|
101
|
+
console.log(`next: run "dsh-superagent-sync push" here, then init + pull on the other machine`)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function printPull(result) {
|
|
105
|
+
if (result.stash) {
|
|
106
|
+
const count = plural(result.stash.files, 'local change')
|
|
107
|
+
console.log(`stashed ${count} as ${result.stash.ref} — nothing local was overwritten ("git stash list" shows it)`)
|
|
108
|
+
}
|
|
109
|
+
if (result.conflicts.length > 0) {
|
|
110
|
+
console.log(`merge ${result.ref} stopped with ${plural(result.conflicts.length, 'conflict')}; both sides are still in the files:`)
|
|
111
|
+
for (const file of result.conflicts) console.log(` ${file}`)
|
|
112
|
+
console.log('nothing was resolved automatically and the stash was not reapplied')
|
|
113
|
+
console.log(`hint: edit each file by hand, then "git add <file>" and "git commit", then push`)
|
|
114
|
+
return 1
|
|
115
|
+
}
|
|
116
|
+
if (result.note) {
|
|
117
|
+
console.log(result.note)
|
|
118
|
+
} else {
|
|
119
|
+
console.log(result.merged ? `merged ${result.ref}` : `already up to date with ${result.ref}`)
|
|
120
|
+
}
|
|
121
|
+
if (result.stash) {
|
|
122
|
+
if (result.stash.reapplied) console.log(`reapplied ${result.stash.ref}`)
|
|
123
|
+
else {
|
|
124
|
+
console.log(`could not reapply ${result.stash.ref}: ${result.stash.popError ?? 'unknown reason'}`)
|
|
125
|
+
console.log(`the stash is kept; inspect with "git stash show -p ${result.stash.ref}" and apply it by hand`)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return 0
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function printPush(result) {
|
|
132
|
+
if (result.commit) console.log(`committed local state as ${result.commit}`)
|
|
133
|
+
else console.log('nothing new to commit')
|
|
134
|
+
if (result.pushed) {
|
|
135
|
+
console.log(`pushed ${result.branch} → origin/${result.branch}`)
|
|
136
|
+
return 0
|
|
137
|
+
}
|
|
138
|
+
if (result.rejected) {
|
|
139
|
+
console.log('push rejected: origin has commits this machine does not have')
|
|
140
|
+
console.log('hint: run "dsh-superagent-sync pull", resolve anything it reports, then push again')
|
|
141
|
+
} else {
|
|
142
|
+
console.log(`push failed: ${result.output || 'git gave no reason'}`)
|
|
143
|
+
}
|
|
144
|
+
return 1
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function printStatus(result) {
|
|
148
|
+
console.log(`state root ${result.root}`)
|
|
149
|
+
console.log(`remote ${result.remote ? `origin → ${result.remote}` : 'none'}`)
|
|
150
|
+
console.log(`branch ${result.branch ?? '(detached)'}${result.upstream ? ` → ${result.upstream}` : ' (no upstream yet)'}`)
|
|
151
|
+
if (result.ahead === null) console.log('position unknown (no upstream yet; push once to set it)')
|
|
152
|
+
else console.log(`position ahead ${result.ahead}, behind ${result.behind}`)
|
|
153
|
+
console.log(`local ${plural(result.dirty, 'uncommitted file')}`)
|
|
154
|
+
console.log(`stashes ${result.stashes.length === 0 ? 'none' : result.stashes.map(item => `${item.ref} ${item.subject}`).join('; ')}`)
|
|
155
|
+
if (result.conflicts.length === 0) console.log('conflicts none')
|
|
156
|
+
else {
|
|
157
|
+
console.log(`conflicts ${plural(result.conflicts.length, 'file')} left for you:`)
|
|
158
|
+
for (const file of result.conflicts) console.log(` ${file}`)
|
|
159
|
+
}
|
|
160
|
+
console.log(`last ${result.head ?? 'no commit yet'}`)
|
|
161
|
+
if (result.fetchError) console.log(`warning could not fetch: ${result.fetchError.split('\n')[0]}`)
|
|
162
|
+
return result.conflicts.length > 0 ? 1 : 0
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const options = parse(process.argv.slice(2))
|
|
166
|
+
const sync = createSync(resolveStateRoot(options.stateDir))
|
|
167
|
+
let result
|
|
168
|
+
try {
|
|
169
|
+
if (options.command === 'init') result = await sync.init({ remote: options.args[0] })
|
|
170
|
+
else if (options.command === 'pull') result = await sync.pull()
|
|
171
|
+
else if (options.command === 'push') result = await sync.push()
|
|
172
|
+
else result = await sync.status()
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (options.json) console.log(JSON.stringify({ error: error?.message ?? String(error), conflicts: error?.conflicts ?? [] }, null, 2))
|
|
175
|
+
fail(1, error?.message ?? String(error), 'check "dsh-superagent-sync status" and the git output above')
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (options.json) {
|
|
179
|
+
console.log(JSON.stringify(result, null, 2))
|
|
180
|
+
process.exit(0)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const printed = options.command === 'init' ? (printInit(result), 0)
|
|
184
|
+
: options.command === 'pull' ? printPull(result)
|
|
185
|
+
: options.command === 'push' ? printPush(result)
|
|
186
|
+
: printStatus(result)
|
|
187
|
+
process.exit(printed)
|
package/bin/workbench.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `dsh-superagent-workbench` — serve the local workspace panel from a terminal.
|
|
4
|
+
*
|
|
5
|
+
* This is the standalone entry point: it needs no host, and it reads the same
|
|
6
|
+
* run files the plugin writes when `--state-dir` points at a plugin state
|
|
7
|
+
* directory. The plugin's own `workbenchPort` option starts the same server
|
|
8
|
+
* inside the host.
|
|
9
|
+
*/
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
|
|
12
|
+
import { createStore } from '../lib/store.js'
|
|
13
|
+
import { createWorkbench } from '../lib/workbench.js'
|
|
14
|
+
|
|
15
|
+
const USAGE = `Usage: dsh-superagent-workbench [options]
|
|
16
|
+
|
|
17
|
+
Serve a loopback-only workspace panel: file tree, editor, diff preview, and the
|
|
18
|
+
files each recorded run changed. A fresh access token is printed on start.
|
|
19
|
+
|
|
20
|
+
Options:
|
|
21
|
+
--root <path> workspace the panel may read and edit (default: cwd)
|
|
22
|
+
--port <port> loopback port; 0 picks a free one (default: 0)
|
|
23
|
+
--state-dir <path> run state directory used for the Runs view
|
|
24
|
+
(default: <root>/.dsh-workbench)
|
|
25
|
+
-h, --help show this message`
|
|
26
|
+
|
|
27
|
+
function valueOf(argv, index, flag) {
|
|
28
|
+
const value = argv[index]
|
|
29
|
+
if (value === undefined || value.startsWith('--')) {
|
|
30
|
+
console.error(`${flag} needs a value\n\n${USAGE}`)
|
|
31
|
+
process.exit(2)
|
|
32
|
+
}
|
|
33
|
+
return value
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parse(argv) {
|
|
37
|
+
const options = { root: process.cwd(), port: 0, stateDir: null }
|
|
38
|
+
for (let index = 0; index < argv.length; index++) {
|
|
39
|
+
const arg = argv[index]
|
|
40
|
+
if (arg === '--root') options.root = valueOf(argv, ++index, '--root')
|
|
41
|
+
else if (arg.startsWith('--root=')) options.root = arg.slice('--root='.length)
|
|
42
|
+
else if (arg === '--port') options.port = Number(valueOf(argv, ++index, '--port'))
|
|
43
|
+
else if (arg.startsWith('--port=')) options.port = Number(arg.slice('--port='.length))
|
|
44
|
+
else if (arg === '--state-dir') options.stateDir = valueOf(argv, ++index, '--state-dir')
|
|
45
|
+
else if (arg.startsWith('--state-dir=')) options.stateDir = arg.slice('--state-dir='.length)
|
|
46
|
+
else if (arg === '-h' || arg === '--help') {
|
|
47
|
+
console.log(USAGE)
|
|
48
|
+
process.exit(0)
|
|
49
|
+
} else {
|
|
50
|
+
console.error(`unknown option ${arg}\n\n${USAGE}`)
|
|
51
|
+
process.exit(2)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
options.root = path.resolve(options.root)
|
|
55
|
+
options.stateDir = options.stateDir ? path.resolve(options.stateDir) : path.join(options.root, '.dsh-workbench')
|
|
56
|
+
if (!Number.isInteger(options.port) || options.port < 0 || options.port > 65535) {
|
|
57
|
+
console.error(`--port must be a whole number in 0–65535\n\n${USAGE}`)
|
|
58
|
+
process.exit(2)
|
|
59
|
+
}
|
|
60
|
+
return options
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const options = parse(process.argv.slice(2))
|
|
64
|
+
const store = createStore(options.stateDir)
|
|
65
|
+
await store.init()
|
|
66
|
+
|
|
67
|
+
const panel = createWorkbench({
|
|
68
|
+
port: options.port,
|
|
69
|
+
root: options.root,
|
|
70
|
+
stateRoot: store.root,
|
|
71
|
+
store,
|
|
72
|
+
version: 'standalone',
|
|
73
|
+
log: message => console.error(`[workbench] ${message}`),
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const { url } = await panel.start()
|
|
78
|
+
console.log(`workbench ${url}`)
|
|
79
|
+
console.log(`root ${options.root}`)
|
|
80
|
+
console.log(`runs ${store.root}`)
|
|
81
|
+
console.log(`open ${url}/?token=${panel.token}`)
|
|
82
|
+
console.log(`discovery ${panel.discoveryPath}`)
|
|
83
|
+
console.log('press Ctrl-C to stop')
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error(`cannot start the workbench: ${error?.message ?? error}`)
|
|
86
|
+
process.exit(1)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let stopping = false
|
|
90
|
+
const shutdown = async signal => {
|
|
91
|
+
if (stopping) return
|
|
92
|
+
stopping = true
|
|
93
|
+
console.error(`[workbench] ${signal}: closing`)
|
|
94
|
+
await panel.stop()
|
|
95
|
+
process.exit(0)
|
|
96
|
+
}
|
|
97
|
+
process.on('SIGINT', () => shutdown('SIGINT'))
|
|
98
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'))
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Copy this file to <stateDir>/policy.json (or point `policyFile` at it).
|
|
3
|
+
// Comments and trailing whitespace are allowed; unknown keys are an error
|
|
4
|
+
// reported with the line they are on.
|
|
5
|
+
"version": 1,
|
|
6
|
+
|
|
7
|
+
// What happens to an action no rule mentions. "allow" keeps today's
|
|
8
|
+
// behaviour; "confirm" holds everything until a person approves it;
|
|
9
|
+
// "deny" stops everything that is not explicitly allowed below.
|
|
10
|
+
"defaultLevel": "allow",
|
|
11
|
+
|
|
12
|
+
"paths": {
|
|
13
|
+
// Relative patterns match at any directory boundary, so the same file
|
|
14
|
+
// works wherever the workspace is mounted. Deny always wins over allow.
|
|
15
|
+
"allow": ["src/**", "docs/**", "test/**"],
|
|
16
|
+
"deny": ["**/.env", "**/.git/**", "**/node_modules/**", "/etc/**", "**/*.pem"]
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
"commands": {
|
|
20
|
+
// A bare entry is a prefix: "npm test" also permits "npm test -- --watch".
|
|
21
|
+
// An entry with * or ? is a glob over the whole command line. A composite
|
|
22
|
+
// line ("git status && ...") never gets more than "confirm" from a prefix.
|
|
23
|
+
"allow": ["git status", "git diff", "git log", "npm test", "npm run lint", "node --test"],
|
|
24
|
+
"deny": ["rm -rf", "git push", "npm publish", "sudo"]
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
"confirm": {
|
|
28
|
+
// Action classes that always need a person, whatever the default level is.
|
|
29
|
+
// "path.*" covers both path.read and path.write.
|
|
30
|
+
"actions": ["network"]
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
"runs": {
|
|
34
|
+
// Per-run override of defaultLevel, written by hand when one run deserves
|
|
35
|
+
// stricter limits than the rest.
|
|
36
|
+
"20261115T101500-abcd1234": { "level": "confirm", "note": "watch this one by hand" }
|
|
37
|
+
}
|
|
38
|
+
}
|